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: Carsten Dominik <carsten at orgmode 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/>.
28 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
29 ;; project planning with a fast and effective plain-text system.
31 ;; Org-mode develops organizational tasks around NOTES files that contain
32 ;; information about projects as plain text. Org-mode is implemented on
33 ;; top of outline-mode, which makes it possible to keep the content of
34 ;; large files well structured. Visibility cycling and structure editing
35 ;; help to work with the tree. Tables are easily created with a built-in
36 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
37 ;; and scheduling. It dynamically compiles entries into an agenda that
38 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
39 ;; Plain text URL-like links connect to websites, emails, Usenet
40 ;; messages, BBDB entries, and any files related to the projects. For
41 ;; printing and sharing of notes, an Org-mode file can be exported as a
42 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
43 ;; iCalendar file. It can also serve as a publishing tool for a set of
46 ;; Installation and Activation
47 ;; ---------------------------
48 ;; See the corresponding sections in the manual at
50 ;; http://orgmode.org/org.html#Installation
54 ;; The documentation of Org-mode can be found in the TeXInfo file. The
55 ;; distribution also contains a PDF version of it. At the homepage of
56 ;; Org-mode, you can read the same text online as HTML. There is also an
57 ;; excellent reference card made by Philip Rooke. This card can be found
58 ;; in the etc/ directory of Emacs 22.
60 ;; A list of recent changes can be found at
61 ;; http://orgmode.org/Changes.html
65 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
66 (defvar org-table-formula-constants-local nil
67 "Local version of `org-table-formula-constants'.")
68 (make-variable-buffer-local 'org-table-formula-constants-local
)
70 ;;;; Require other packages
78 (require 'format-spec
)
80 (load "org-loaddefs.el" t t t
)
85 ;; `org-outline-regexp' ought to be a defconst but is let-binding in
86 ;; some places -- e.g. see the macro `org-with-limited-levels'.
88 ;; In Org buffers, the value of `outline-regexp' is that of
89 ;; `org-outline-regexp'. The only function still directly relying on
90 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
91 ;; job when `orgstruct-mode' is active.
92 (defvar org-outline-regexp
"\\*+ "
93 "Regexp to match Org headlines.")
95 (defvar org-outline-regexp-bol
"^\\*+ "
96 "Regexp to match Org headlines.
97 This is similar to `org-outline-regexp' but additionally makes
98 sure that we are at the beginning of the line.")
100 (defvar org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
101 "Matches a headline, putting stars and text into groups.
102 Stars are put in group 1 and the trimmed body in group 2.")
104 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
105 (unless (boundp 'calendar-view-holidays-initially-flag
)
106 (org-defvaralias 'calendar-view-holidays-initially-flag
107 'view-calendar-holidays-initially
))
108 (unless (boundp 'calendar-view-diary-initially-flag
)
109 (org-defvaralias 'calendar-view-diary-initially-flag
110 'view-diary-entries-initially
))
111 (unless (boundp 'diary-fancy-buffer
)
112 (org-defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
))
114 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
115 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
116 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
117 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
118 (declare-function org-clock-get-last-clock-out-time
"org-clock" ())
119 (declare-function org-clock-timestamps-up
"org-clock" (&optional n
))
120 (declare-function org-clock-timestamps-down
"org-clock" (&optional n
))
121 (declare-function org-clock-sum-current-item
"org-clock" (&optional tstart
))
123 (declare-function orgtbl-mode
"org-table" (&optional arg
))
124 (declare-function org-clock-out
"org-clock" (&optional switch-to-state fail-quietly at-time
))
125 (declare-function org-beamer-mode
"ox-beamer" ())
126 (declare-function org-table-edit-field
"org-table" (arg))
127 (declare-function org-table-justify-field-maybe
"org-table" (&optional new
))
128 (declare-function org-table-set-constants
"org-table" ())
129 (declare-function org-table-calc-current-TBLFM
"org-table" (&optional arg
))
130 (declare-function org-id-get-create
"org-id" (&optional force
))
131 (declare-function org-id-find-id-file
"org-id" (id))
132 (declare-function org-tags-view
"org-agenda" (&optional todo-only match
))
133 (declare-function org-agenda-list
"org-agenda" (&optional arg start-day span
))
134 (declare-function org-agenda-redo
"org-agenda" (&optional all
))
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-restriction
"org-element" (element))
161 (declare-function org-element-type
"org-element" (element))
163 ;; load languages based on value of `org-babel-load-languages'
164 (defvar org-babel-load-languages
)
167 (defun org-babel-do-load-languages (sym value
)
168 "Load the languages defined in `org-babel-load-languages'."
169 (set-default sym value
)
171 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
174 (require (intern (concat "ob-" lang
))))
176 (funcall 'fmakunbound
177 (intern (concat "org-babel-execute:" lang
)))
178 (funcall 'fmakunbound
179 (intern (concat "org-babel-expand-body:" lang
)))))))
180 org-babel-load-languages
))
183 (defun org-babel-load-file (file &optional compile
)
184 "Load Emacs Lisp source code blocks in the Org-mode FILE.
185 This function exports the source code using `org-babel-tangle'
186 and then loads the resulting file using `load-file'. With prefix
187 arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
188 file to byte-code before it is loaded."
189 (interactive "fFile to load: \nP")
191 (let* ((age (lambda (file)
193 (time-subtract (current-time)
194 (nth 5 (or (file-attributes (file-truename file
))
195 (file-attributes file
)))))))
196 (base-name (file-name-sans-extension file
))
197 (exported-file (concat base-name
".el")))
198 ;; tangle if the org-mode file is newer than the elisp file
199 (unless (and (file-exists-p exported-file
)
200 (> (funcall age file
) (funcall age exported-file
)))
201 (org-babel-tangle-file file exported-file
"emacs-lisp"))
204 (progn (byte-compile-file exported-file
'load
)
205 "Compiled and loaded")
206 (progn (load-file exported-file
) "Loaded"))
209 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
210 "Languages which can be evaluated in Org-mode buffers.
211 This list can be used to load support for any of the languages
212 below, note that each language will depend on a different set of
213 system executables and/or Emacs modes. When a language is
214 \"loaded\", then code blocks in that language can be evaluated
215 with `org-babel-execute-src-block' bound by default to C-c
216 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
217 be set to remove code block evaluation from the C-c C-c
218 keybinding. By default only Emacs Lisp (which has no
219 requirements) is loaded."
221 :set
'org-babel-do-load-languages
223 :type
'(alist :tag
"Babel Languages"
226 (const :tag
"Awk" awk
)
229 (const :tag
"Asymptote" asymptote
)
230 (const :tag
"Calc" calc
)
231 (const :tag
"Clojure" clojure
)
232 (const :tag
"CSS" css
)
233 (const :tag
"Ditaa" ditaa
)
234 (const :tag
"Dot" dot
)
235 (const :tag
"Emacs Lisp" emacs-lisp
)
236 (const :tag
"Fortran" fortran
)
237 (const :tag
"Gnuplot" gnuplot
)
238 (const :tag
"Haskell" haskell
)
240 (const :tag
"Java" java
)
241 (const :tag
"Javascript" js
)
242 (const :tag
"LaTeX" latex
)
243 (const :tag
"Ledger" ledger
)
244 (const :tag
"Lilypond" lilypond
)
245 (const :tag
"Lisp" lisp
)
246 (const :tag
"Makefile" makefile
)
247 (const :tag
"Maxima" maxima
)
248 (const :tag
"Matlab" matlab
)
249 (const :tag
"Mscgen" mscgen
)
250 (const :tag
"Ocaml" ocaml
)
251 (const :tag
"Octave" octave
)
252 (const :tag
"Org" org
)
253 (const :tag
"Perl" perl
)
254 (const :tag
"Pico Lisp" picolisp
)
255 (const :tag
"PlantUML" plantuml
)
256 (const :tag
"Python" python
)
257 (const :tag
"Ruby" ruby
)
258 (const :tag
"Sass" sass
)
259 (const :tag
"Scala" scala
)
260 (const :tag
"Scheme" scheme
)
261 (const :tag
"Screen" screen
)
262 (const :tag
"Shell Script" sh
)
263 (const :tag
"Shen" shen
)
264 (const :tag
"Sql" sql
)
265 (const :tag
"Sqlite" sqlite
))
266 :value-type
(boolean :tag
"Activate" :value t
)))
268 ;;;; Customization variables
269 (defcustom org-clone-delete-id nil
270 "Remove ID property of clones of a subtree.
271 When non-nil, clones of a subtree don't inherit the ID property.
272 Otherwise they inherit the ID property with a new unique
282 (defun org-version (&optional here full message
)
283 "Show the org-mode version in the echo area.
284 With prefix argument HERE, insert it at point.
285 When FULL is non-nil, use a verbose version string.
286 When MESSAGE is non-nil, display a message with the version."
288 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
289 (save-load-suffixes (when (boundp 'load-suffixes
) load-suffixes
))
290 (load-suffixes (list ".el"))
291 (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
293 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
294 (org-load-noerror-mustsuffix (concat org-dir
"org-version"))))
295 (load-suffixes save-load-suffixes
)
296 (org-version (org-release))
297 (git-version (org-git-version))
298 (version (format "Org-mode version %s (%s @ %s)"
302 (if (string= org-dir org-install-dir
)
304 (concat "mixed installation! " org-install-dir
" and " org-dir
))
305 "org-loaddefs.el can not be found!")))
306 (_version (if full version org-version
)))
307 (if (org-called-interactively-p 'interactive
)
311 (if message
(message _version
))
314 (defconst org-version
(org-version))
316 ;;; Compatibility constants
318 ;;; The custom variables
321 "Outline-based notes management and organizer."
326 (defcustom org-mode-hook nil
327 "Mode hook for Org-mode, run after the mode was turned on."
331 (defcustom org-load-hook nil
332 "Hook that is run after org.el has been loaded."
336 (defcustom org-log-buffer-setup-hook nil
337 "Hook that is run after an Org log buffer is created."
342 (defvar org-modules
) ; defined below
343 (defvar org-modules-loaded nil
344 "Have the modules been loaded already?")
346 (defun org-load-modules-maybe (&optional force
)
347 "Load all extensions listed in `org-modules'."
348 (when (or force
(not org-modules-loaded
))
350 (condition-case nil
(require ext
)
351 (error (message "Problems while trying to load feature `%s'" ext
))))
353 (setq org-modules-loaded t
)))
355 (defun org-set-modules (var value
)
356 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
358 (when (featurep 'org
)
359 (org-load-modules-maybe 'force
)))
361 (defcustom org-modules
'(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail
)
362 "Modules that should always be loaded together with org.el.
364 If a description starts with <C>, the file is not part of Emacs
365 and loading it will require that you have downloaded and properly
366 installed the Org mode distribution.
368 You can also use this system to load external packages (i.e. neither Org
369 core modules, nor modules from the CONTRIB directory). Just add symbols
370 to the end of the list. If the package is called org-xyz.el, then you need
371 to add the symbol `xyz', and the package must have a call to:
375 For export specific modules, see also `org-export-backends'."
377 :set
'org-set-modules
379 :package-version
'(Org .
"8.0")
382 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
383 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
384 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
385 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
386 (const :tag
" docview: Links to doc-view buffers" org-docview
)
387 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
388 (const :tag
" habit: Track your consistency with habits" org-habit
)
389 (const :tag
" id: Global IDs for identifying entries" org-id
)
390 (const :tag
" info: Links to Info nodes" org-info
)
391 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
392 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
393 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
394 (const :tag
" mouse: Additional mouse support" org-mouse
)
395 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
396 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
397 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
399 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
400 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
401 (const :tag
"C bullets: Add overlays to headlines stars" org-bullets
)
402 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
403 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
404 (const :tag
"C collector: Collect properties into tables" org-collector
)
405 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
406 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
407 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
408 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
409 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
410 (const :tag
"C eval: Include command output as text" org-eval
)
411 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
412 (const :tag
"C favtable: Lookup table of favorite references and links" org-favtable
)
413 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
414 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
415 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
416 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
417 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
418 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
419 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
420 (const :tag
"C mac-message: Links to messages in Apple Mail" org-mac-message
)
421 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
422 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
423 (const :tag
"C mew: Links to Mew folders/messages" org-mew
)
424 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
425 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
426 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
427 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
428 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
429 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
430 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
431 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
432 (const :tag
"C track: Keep up with Org-mode development" org-track
)
433 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
434 (const :tag
"C vm: Links to VM folders/messages" org-vm
)
435 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
436 (const :tag
"C wl: Links to Wanderlust folders/messages" org-wl
)
437 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
439 (defvar org-export-registered-backends
) ; From ox.el
440 (declare-function org-export-derived-backend-p
"ox" (backend &rest backends
))
441 (defcustom org-export-backends
'(ascii html icalendar latex
)
442 "List of export back-ends that should be always available.
444 If a description starts with <C>, the file is not part of Emacs
445 and loading it will require that you have downloaded and properly
446 installed the Org mode distribution.
448 Unlike to `org-modules', libraries in this list will not be
449 loaded along with Org, but only once the export framework is
452 This variable needs to be set before org.el is loaded. If you
453 need to make a change while Emacs is running, use the customize
454 interface or run the following code, where VALUE stands for the
455 new value of the variable, after updating it:
458 \(setq org-export-registered-backends
461 \(or (memq backend val)
465 \(and (org-export-derived-backend-p b (car backend))
466 \(throw 'parentp t)))
469 org-export-registered-backends))
470 \(let ((new-list (mapcar 'car org-export-registered-backends)))
471 \(dolist (backend val)
473 \((not (load (format \"ox-%s\" backend) t t))
474 \(message \"Problems while trying to load export back-end `%s'\"
476 \((not (memq backend new-list)) (push backend new-list))))
477 \(set-default var new-list)))
479 Adding a back-end to this list will also pull the back-end it
484 :package-version
'(Org .
"8.0")
485 :initialize
'custom-initialize-set
486 :set
(lambda (var val
)
487 (if (not (featurep 'ox
)) (set-default var val
)
488 ;; Any back-end not required anymore (not present in VAL and not
489 ;; a parent of any back-end in the new value) is removed from the
490 ;; list of registered back-ends.
491 (setq org-export-registered-backends
494 (or (memq backend val
)
498 (and (org-export-derived-backend-p b
(car backend
))
502 org-export-registered-backends
))
503 ;; Now build NEW-LIST of both new back-ends and required
505 (let ((new-list (mapcar 'car org-export-registered-backends
)))
506 (dolist (backend val
)
508 ((not (load (format "ox-%s" backend
) t t
))
509 (message "Problems while trying to load export back-end `%s'"
511 ((not (memq backend new-list
)) (push backend new-list
))))
512 ;; Set VAR to that list with fixed dependencies.
513 (set-default var new-list
))))
514 :type
'(set :greedy t
515 (const :tag
" ascii Export buffer to ASCII format" ascii
)
516 (const :tag
" beamer Export buffer to Beamer presentation" beamer
)
517 (const :tag
" html Export buffer to HTML format" html
)
518 (const :tag
" icalendar Export buffer to iCalendar format" icalendar
)
519 (const :tag
" latex Export buffer to LaTeX format" latex
)
520 (const :tag
" man Export buffer to MAN format" man
)
521 (const :tag
" md Export buffer to Markdown format" md
)
522 (const :tag
" odt Export buffer to ODT format" odt
)
523 (const :tag
" org Export buffer to Org format" org
)
524 (const :tag
" texinfo Export buffer to Texinfo format" texinfo
)
525 (const :tag
"C confluence Export buffer to Confluence Wiki format" confluence
)
526 (const :tag
"C deck Export buffer to deck.js presentations" deck
)
527 (const :tag
"C freemind Export buffer to Freemind mindmap format" freemind
)
528 (const :tag
"C groff Export buffer to Groff format" groff
)
529 (const :tag
"C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter
)
530 (const :tag
"C RSS 2.0 Export buffer to RSS 2.0 format" rss
)
531 (const :tag
"C s5 Export buffer to s5 presentations" s5
)
532 (const :tag
"C taskjuggler Export buffer to TaskJuggler format" taskjuggler
)))
537 (condition-case nil
(require (intern (format "ox-%s" backend
)))
538 (error (message "Problems while trying to load export back-end `%s'"
540 org-export-backends
))
542 (defcustom org-support-shift-select nil
543 "Non-nil means make shift-cursor commands select text when possible.
545 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
546 start selecting a region, or enlarge regions started in this way.
547 In Org-mode, in special contexts, these same keys are used for
548 other purposes, important enough to compete with shift selection.
549 Org tries to balance these needs by supporting `shift-select-mode'
550 outside these special contexts, under control of this variable.
552 The default of this variable is nil, to avoid confusing behavior. Shifted
553 cursor keys will then execute Org commands in the following contexts:
554 - on a headline, changing TODO state (left/right) and priority (up/down)
555 - on a time stamp, changing the time
556 - in a plain list item, changing the bullet type
557 - in a property definition line, switching between allowed values
558 - in the BEGIN line of a clock table (changing the time block).
559 Outside these contexts, the commands will throw an error.
561 When this variable is t and the cursor is not in a special
562 context, Org-mode will support shift-selection for making and
563 enlarging regions. To make this more effective, the bullet
564 cycling will no longer happen anywhere in an item line, but only
565 if the cursor is exactly on the bullet.
567 If you set this variable to the symbol `always', then the keys
568 will not be special in headlines, property lines, and item lines,
569 to make shift selection work there as well. If this is what you
570 want, you can use the following alternative commands: `C-c C-t'
571 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
572 can be used to switch TODO sets, `C-c -' to cycle item bullet
573 types, and properties can be edited by hand or in column view.
575 However, when the cursor is on a timestamp, shift-cursor commands
576 will still edit the time stamp - this is just too good to give up.
578 XEmacs user should have this variable set to nil, because
579 `shift-select-mode' is in Emacs 23 or later only."
582 (const :tag
"Never" nil
)
583 (const :tag
"When outside special context" t
)
584 (const :tag
"Everywhere except timestamps" always
)))
586 (defcustom org-loop-over-headlines-in-active-region nil
587 "Shall some commands act upon headlines in the active region?
589 When set to `t', some commands will be performed in all headlines
590 within the active region.
592 When set to `start-level', some commands will be performed in all
593 headlines within the active region, provided that these headlines
594 are of the same level than the first one.
596 When set to a string, those commands will be performed on the
597 matching headlines within the active region. Such string must be
598 a tags/property/todo match as it is used in the agenda tags view.
600 The list of commands is: `org-schedule', `org-deadline',
601 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
602 `org-archive-to-archive-sibling'. The archiving commands skip
603 already archived entries."
604 :type
'(choice (const :tag
"Don't loop" nil
)
605 (const :tag
"All headlines in active region" t
)
606 (const :tag
"In active region, headlines at the same level than the first one" 'start-level
)
607 (string :tag
"Tags/Property/Todo matcher"))
612 (defgroup org-startup nil
613 "Options concerning startup of Org-mode."
617 (defcustom org-startup-folded t
618 "Non-nil means entering Org-mode will switch to OVERVIEW.
619 This can also be configured on a per-file basis by adding one of
620 the following lines anywhere in the buffer:
622 #+STARTUP: fold (or `overview', this is equivalent)
623 #+STARTUP: nofold (or `showall', this is equivalent)
625 #+STARTUP: showeverything
627 By default, this option is ignored when Org opens agenda files
628 for the first time. If you want the agenda to honor the startup
629 option, set `org-agenda-inhibit-startup' to nil."
632 (const :tag
"nofold: show all" nil
)
633 (const :tag
"fold: overview" t
)
634 (const :tag
"content: all headlines" content
)
635 (const :tag
"show everything, even drawers" showeverything
)))
637 (defcustom org-startup-truncated t
638 "Non-nil means entering Org-mode will set `truncate-lines'.
639 This is useful since some lines containing links can be very long and
640 uninteresting. Also tables look terrible when wrapped."
644 (defcustom org-startup-indented nil
645 "Non-nil means turn on `org-indent-mode' on startup.
646 This can also be configured on a per-file basis by adding one of
647 the following lines anywhere in the buffer:
651 :group
'org-structure
653 (const :tag
"Not" nil
)
654 (const :tag
"Globally (slow on startup in large files)" t
)))
656 (defcustom org-use-sub-superscripts t
657 "Non-nil means interpret \"_\" and \"^\" for display.
658 When this option is turned on, you can use TeX-like syntax for sub- and
659 superscripts. Several characters after \"_\" or \"^\" will be
660 considered as a single item - so grouping with {} is normally not
661 needed. For example, the following things will be parsed as single
662 sub- or superscripts.
664 10^24 or 10^tau several digits will be considered 1 item.
665 10^-12 or 10^-tau a leading sign with digits or a word
666 x^2-y^3 will be read as x^2 - y^3, because items are
667 terminated by almost any nonword/nondigit char.
668 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
670 Still, ambiguity is possible - so when in doubt use {} to enclose
671 the sub/superscript. If you set this variable to the symbol
672 `{}', the braces are *required* in order to trigger
673 interpretations as sub/superscript. This can be helpful in
674 documents that need \"_\" frequently in plain text."
678 (const :tag
"Always interpret" t
)
679 (const :tag
"Only with braces" {})
680 (const :tag
"Never interpret" nil
)))
682 (defcustom org-startup-with-beamer-mode nil
683 "Non-nil means turn on `org-beamer-mode' on startup.
684 This can also be configured on a per-file basis by adding one of
685 the following lines anywhere in the buffer:
692 (defcustom org-startup-align-all-tables nil
693 "Non-nil means align all tables when visiting a file.
694 This is useful when the column width in tables is forced with <N> cookies
695 in table fields. Such tables will look correct only after the first re-align.
696 This can also be configured on a per-file basis by adding one of
697 the following lines anywhere in the buffer:
703 (defcustom org-startup-with-inline-images nil
704 "Non-nil means show inline images when loading a new Org file.
705 This can also be configured on a per-file basis by adding one of
706 the following lines anywhere in the buffer:
707 #+STARTUP: inlineimages
708 #+STARTUP: noinlineimages"
713 (defcustom org-startup-with-latex-preview nil
714 "Non-nil means preview LaTeX fragments when loading a new Org file.
716 This can also be configured on a per-file basis by adding one of
717 the followinglines anywhere in the buffer:
718 #+STARTUP: latexpreview
719 #+STARTUP: nolatexpreview"
722 :package-version
'(Org .
"8.0")
725 (defcustom org-insert-mode-line-in-empty-file nil
726 "Non-nil means insert the first line setting Org-mode in empty files.
727 When the function `org-mode' is called interactively in an empty file, this
728 normally means that the file name does not automatically trigger Org-mode.
729 To ensure that the file will always be in Org-mode in the future, a
730 line enforcing Org-mode will be inserted into the buffer, if this option
735 (defcustom org-replace-disputed-keys nil
736 "Non-nil means use alternative key bindings for some keys.
737 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
738 These keys are also used by other packages like shift-selection-mode'
739 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
740 If you want to use Org-mode together with one of these other modes,
741 or more generally if you would like to move some Org-mode commands to
742 other keys, set this variable and configure the keys with the variable
745 This option is only relevant at load-time of Org-mode, and must be set
746 *before* org.el is loaded. Changing it requires a restart of Emacs to
751 (defcustom org-use-extra-keys nil
752 "Non-nil means use extra key sequence definitions for certain commands.
753 This happens automatically if you run XEmacs or if `window-system'
754 is nil. This variable lets you do the same manually. You must
755 set it before loading org.
757 Example: on Carbon Emacs 22 running graphically, with an external
758 keyboard on a Powerbook, the default way of setting M-left might
759 not work for either Alt or ESC. Setting this variable will make
764 (org-defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
)
766 (defcustom org-disputed-keys
767 '(([(shift up
)] .
[(meta p
)])
768 ([(shift down
)] .
[(meta n
)])
769 ([(shift left
)] .
[(meta -
)])
770 ([(shift right
)] .
[(meta +)])
771 ([(control shift right
)] .
[(meta shift
+)])
772 ([(control shift left
)] .
[(meta shift -
)]))
773 "Keys for which Org-mode and other modes compete.
774 This is an alist, cars are the default keys, second element specifies
775 the alternative to use when `org-replace-disputed-keys' is t.
777 Keys can be specified in any syntax supported by `define-key'.
778 The value of this option takes effect only at Org-mode's startup,
779 therefore you'll have to restart Emacs to apply it after changing."
784 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
785 Or return the original if not disputed.
786 Also apply the translations defined in `org-xemacs-key-equivalents'."
787 (when org-replace-disputed-keys
788 (let* ((nkey (key-description key
))
789 (x (org-find-if (lambda (x)
790 (equal (key-description (car x
)) nkey
))
792 (setq key
(if x
(cdr x
) key
))))
793 (when (featurep 'xemacs
)
794 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
797 (defun org-find-if (predicate seq
)
800 (if (funcall predicate
(car seq
))
801 (throw 'exit
(car seq
))
804 (defun org-defkey (keymap key def
)
805 "Define a key, possibly translated, as returned by `org-key'."
806 (define-key keymap
(org-key key
) def
))
808 (defcustom org-ellipsis nil
809 "The ellipsis to use in the Org-mode outline.
810 When nil, just use the standard three dots. When a string, use that instead,
811 When a face, use the standard 3 dots, but with the specified face.
812 The change affects only Org-mode (which will then use its own display table).
813 Changing this requires executing `M-x org-mode' in a buffer to become
816 :type
'(choice (const :tag
"Default" nil
)
817 (face :tag
"Face" :value org-warning
)
818 (string :tag
"String" :value
"...#")))
820 (defvar org-display-table nil
821 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
823 (defgroup org-keywords nil
824 "Keywords in Org-mode."
828 (defcustom org-deadline-string
"DEADLINE:"
829 "String to mark deadline entries.
830 A deadline is this string, followed by a time stamp. Should be a word,
831 terminated by a colon. You can insert a schedule keyword and
832 a timestamp with \\[org-deadline].
833 Changes become only effective after restarting Emacs."
837 (defcustom org-scheduled-string
"SCHEDULED:"
838 "String to mark scheduled TODO entries.
839 A schedule is this string, followed by a time stamp. Should be a word,
840 terminated by a colon. You can insert a schedule keyword and
841 a timestamp with \\[org-schedule].
842 Changes become only effective after restarting Emacs."
846 (defcustom org-closed-string
"CLOSED:"
847 "String used as the prefix for timestamps logging closing a TODO entry."
851 (defcustom org-clock-string
"CLOCK:"
852 "String used as prefix for timestamps clocking work hours on an item."
856 (defcustom org-closed-keep-when-no-todo nil
857 "Remove CLOSED: time-stamp when switching back to a non-todo state?"
861 :package-version
'(Org .
"8.0")
864 (defconst org-planning-or-clock-line-re
(concat "^[ \t]*\\("
865 org-scheduled-string
"\\|"
866 org-deadline-string
"\\|"
867 org-closed-string
"\\|"
868 org-clock-string
"\\)")
869 "Matches a line with planning or clock info.")
871 (defcustom org-comment-string
"COMMENT"
872 "Entries starting with this keyword will never be exported.
873 An entry can be toggled between COMMENT and normal with
874 \\[org-toggle-comment].
875 Changes become only effective after restarting Emacs."
879 (defcustom org-quote-string
"QUOTE"
880 "Entries starting with this keyword will be exported in fixed-width font.
881 Quoting applies only to the text in the entry following the headline, and does
882 not extend beyond the next headline, even if that is lower level.
883 An entry can be toggled between QUOTE and normal with
884 \\[org-toggle-fixed-width-section]."
888 (defconst org-repeat-re
889 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
890 "Regular expression for specifying repeated events.
891 After a match, group 1 contains the repeat expression.")
893 (defgroup org-structure nil
894 "Options concerning the general structure of Org-mode files."
898 (defgroup org-reveal-location nil
899 "Options about how to make context of a location visible."
900 :tag
"Org Reveal Location"
901 :group
'org-structure
)
903 (defconst org-context-choice
905 (const :tag
"Always" t
)
906 (const :tag
"Never" nil
)
907 (repeat :greedy t
:tag
"Individual contexts"
909 (choice :tag
"Context"
916 (const bookmark-jump
)
920 "Contexts for the reveal options.")
922 (defcustom org-show-hierarchy-above
'((default . t
))
923 "Non-nil means show full hierarchy when revealing a location.
924 Org-mode often shows locations in an org-mode file which might have
925 been invisible before. When this is set, the hierarchy of headings
926 above the exposed location is shown.
927 Turning this off for example for sparse trees makes them very compact.
928 Instead of t, this can also be an alist specifying this option for different
929 contexts. Valid contexts are
930 agenda when exposing an entry from the agenda
931 org-goto when using the command `org-goto' on key C-c C-j
932 occur-tree when using the command `org-occur' on key C-c /
933 tags-tree when constructing a sparse tree based on tags matches
934 link-search when exposing search matches associated with a link
935 mark-goto when exposing the jump goal of a mark
936 bookmark-jump when exposing a bookmark location
937 isearch when exiting from an incremental search
938 default default for all contexts not set explicitly"
939 :group
'org-reveal-location
940 :type org-context-choice
)
942 (defcustom org-show-following-heading
'((default . nil
))
943 "Non-nil means show following heading when revealing a location.
944 Org-mode often shows locations in an org-mode file which might have
945 been invisible before. When this is set, the heading following the
947 Turning this off for example for sparse trees makes them very compact,
948 but makes it harder to edit the location of the match. In such a case,
949 use the command \\[org-reveal] to show more context.
950 Instead of t, this can also be an alist specifying this option for different
951 contexts. See `org-show-hierarchy-above' for valid contexts."
952 :group
'org-reveal-location
953 :type org-context-choice
)
955 (defcustom org-show-siblings
'((default . nil
) (isearch t
) (bookmark-jump t
))
956 "Non-nil means show all sibling heading when revealing a location.
957 Org-mode often shows locations in an org-mode file which might have
958 been invisible before. When this is set, the sibling of the current entry
959 heading are all made visible. If `org-show-hierarchy-above' is t,
960 the same happens on each level of the hierarchy above the current entry.
962 By default this is on for the isearch context, off for all other contexts.
963 Turning this off for example for sparse trees makes them very compact,
964 but makes it harder to edit the location of the match. In such a case,
965 use the command \\[org-reveal] to show more context.
966 Instead of t, this can also be an alist specifying this option for different
967 contexts. See `org-show-hierarchy-above' for valid contexts."
968 :group
'org-reveal-location
969 :type org-context-choice
971 :package-version
'(Org .
"8.0"))
973 (defcustom org-show-entry-below
'((default . nil
))
974 "Non-nil means show the entry below a headline when revealing a location.
975 Org-mode often shows locations in an org-mode file which might have
976 been invisible before. When this is set, the text below the headline that is
977 exposed is also shown.
979 By default this is off for all contexts.
980 Instead of t, this can also be an alist specifying this option for different
981 contexts. See `org-show-hierarchy-above' for valid contexts."
982 :group
'org-reveal-location
983 :type org-context-choice
)
985 (defcustom org-indirect-buffer-display
'other-window
986 "How should indirect tree buffers be displayed?
987 This applies to indirect buffers created with the commands
988 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
990 current-window Display in the current window
991 other-window Just display in another window.
992 dedicated-frame Create one new frame, and re-use it each time.
993 new-frame Make a new frame each time. Note that in this case
994 previously-made indirect buffers are kept, and you need to
995 kill these buffers yourself."
996 :group
'org-structure
997 :group
'org-agenda-windows
999 (const :tag
"In current window" current-window
)
1000 (const :tag
"In current frame, other window" other-window
)
1001 (const :tag
"Each time a new frame" new-frame
)
1002 (const :tag
"One dedicated frame" dedicated-frame
)))
1004 (defcustom org-use-speed-commands nil
1005 "Non-nil means activate single letter commands at beginning of a headline.
1006 This may also be a function to test for appropriate locations where speed
1007 commands should be active."
1008 :group
'org-structure
1010 (const :tag
"Never" nil
)
1011 (const :tag
"At beginning of headline stars" t
)
1014 (defcustom org-speed-commands-user nil
1015 "Alist of additional speed commands.
1016 This list will be checked before `org-speed-commands-default'
1017 when the variable `org-use-speed-commands' is non-nil
1018 and when the cursor is at the beginning of a headline.
1019 The car if each entry is a string with a single letter, which must
1020 be assigned to `self-insert-command' in the global map.
1021 The cdr is either a command to be called interactively, a function
1022 to be called, or a form to be evaluated.
1023 An entry that is just a list with a single string will be interpreted
1024 as a descriptive headline that will be added when listing the speed
1025 commands in the Help buffer using the `?' speed command."
1026 :group
'org-structure
1027 :type
'(repeat :value
("k" . ignore
)
1028 (choice :value
("k" . ignore
)
1029 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
1030 (cons :tag
"Letter and Command"
1031 (string :tag
"Command letter")
1036 (defgroup org-cycle nil
1037 "Options concerning visibility cycling in Org-mode."
1039 :group
'org-structure
)
1041 (defcustom org-cycle-skip-children-state-if-no-children t
1042 "Non-nil means skip CHILDREN state in entries that don't have any."
1046 (defcustom org-cycle-max-level nil
1047 "Maximum level which should still be subject to visibility cycling.
1048 Levels higher than this will, for cycling, be treated as text, not a headline.
1049 When `org-odd-levels-only' is set, a value of N in this variable actually
1050 means 2N-1 stars as the limiting headline.
1051 When nil, cycle all levels.
1052 Note that the limiting level of cycling is also influenced by
1053 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
1054 `org-inlinetask-min-level' is, cycling will be limited to levels one less
1058 (const :tag
"No limit" nil
)
1059 (integer :tag
"Maximum level")))
1061 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
1062 "Names of drawers. Drawers are not opened by cycling on the headline above.
1063 Drawers only open with a TAB on the drawer line itself. A drawer looks like
1068 The drawer \"PROPERTIES\" is special for capturing properties through
1071 Drawers can be defined on the per-file basis with a line like:
1073 #+DRAWERS: HIDDEN STATE PROPERTIES"
1074 :group
'org-structure
1076 :type
'(repeat (string :tag
"Drawer Name")))
1078 (defcustom org-hide-block-startup nil
1079 "Non-nil means entering Org-mode will fold all blocks.
1080 This can also be set in on a per-file basis with
1082 #+STARTUP: hideblocks
1083 #+STARTUP: showblocks"
1088 (defcustom org-cycle-global-at-bob nil
1089 "Cycle globally if cursor is at beginning of buffer and not at a headline.
1090 This makes it possible to do global cycling without having to use S-TAB or
1091 \\[universal-argument] TAB. For this special case to work, the first line
1092 of the buffer must not be a headline -- it may be empty or some other text.
1093 When used in this way, `org-cycle-hook' is disabled temporarily to make
1094 sure the cursor stays at the beginning of the buffer. When this option is
1095 nil, don't do anything special at the beginning of the buffer."
1099 (defcustom org-cycle-level-after-item
/entry-creation t
1100 "Non-nil means cycle entry level or item indentation in new empty entries.
1102 When the cursor is at the end of an empty headline, i.e., with only stars
1103 and maybe a TODO keyword, TAB will then switch the entry to become a child,
1104 and then all possible ancestor states, before returning to the original state.
1105 This makes data entry extremely fast: M-RET to create a new headline,
1106 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
1108 When the cursor is at the end of an empty plain list item, one TAB will
1109 make it a subitem, two or more tabs will back up to make this an item
1110 higher up in the item hierarchy."
1114 (defcustom org-cycle-emulate-tab t
1115 "Where should `org-cycle' emulate TAB.
1117 white Only in completely white lines
1118 whitestart Only at the beginning of lines, before the first non-white char
1119 t Everywhere except in headlines
1120 exc-hl-bol Everywhere except at the start of a headline
1121 If TAB is used in a place where it does not emulate TAB, the current subtree
1122 visibility is cycled."
1124 :type
'(choice (const :tag
"Never" nil
)
1125 (const :tag
"Only in completely white lines" white
)
1126 (const :tag
"Before first char in a line" whitestart
)
1127 (const :tag
"Everywhere except in headlines" t
)
1128 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)))
1130 (defcustom org-cycle-separator-lines
2
1131 "Number of empty lines needed to keep an empty line between collapsed trees.
1132 If you leave an empty line between the end of a subtree and the following
1133 headline, this empty line is hidden when the subtree is folded.
1134 Org-mode will leave (exactly) one empty line visible if the number of
1135 empty lines is equal or larger to the number given in this variable.
1136 So the default 2 means at least 2 empty lines after the end of a subtree
1137 are needed to produce free space between a collapsed subtree and the
1140 If the number is negative, and the number of empty lines is at least -N,
1141 all empty lines are shown.
1143 Special case: when 0, never leave empty lines in collapsed view."
1146 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
1148 (defcustom org-pre-cycle-hook nil
1149 "Hook that is run before visibility cycling is happening.
1150 The function(s) in this hook must accept a single argument which indicates
1151 the new state that will be set right after running this hook. The
1152 argument is a symbol. Before a global state change, it can have the values
1153 `overview', `content', or `all'. Before a local state change, it can have
1154 the values `folded', `children', or `subtree'."
1158 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
1159 org-cycle-hide-drawers
1160 org-cycle-hide-inline-tasks
1161 org-cycle-show-empty-lines
1162 org-optimize-window-after-visibility-change
)
1163 "Hook that is run after `org-cycle' has changed the buffer visibility.
1164 The function(s) in this hook must accept a single argument which indicates
1165 the new state that was set by the most recent `org-cycle' command. The
1166 argument is a symbol. After a global state change, it can have the values
1167 `overview', `contents', or `all'. After a local state change, it can have
1168 the values `folded', `children', or `subtree'."
1172 (defgroup org-edit-structure nil
1173 "Options concerning structure editing in Org-mode."
1174 :tag
"Org Edit Structure"
1175 :group
'org-structure
)
1177 (defcustom org-odd-levels-only nil
1178 "Non-nil means skip even levels and only use odd levels for the outline.
1179 This has the effect that two stars are being added/taken away in
1180 promotion/demotion commands. It also influences how levels are
1181 handled by the exporters.
1182 Changing it requires restart of `font-lock-mode' to become effective
1183 for fontification also in regions already fontified.
1184 You may also set this on a per-file basis by adding one of the following
1185 lines to the buffer:
1189 :group
'org-edit-structure
1190 :group
'org-appearance
1193 (defcustom org-adapt-indentation t
1194 "Non-nil means adapt indentation to outline node level.
1196 When this variable is set, Org assumes that you write outlines by
1197 indenting text in each node to align with the headline (after the stars).
1198 The following issues are influenced by this variable:
1200 - When this is set and the *entire* text in an entry is indented, the
1201 indentation is increased by one space in a demotion command, and
1202 decreased by one in a promotion command. If any line in the entry
1203 body starts with text at column 0, indentation is not changed at all.
1205 - Property drawers and planning information is inserted indented when
1206 this variable s set. When nil, they will not be indented.
1208 - TAB indents a line relative to context. The lines below a headline
1209 will be indented when this variable is set.
1211 Note that this is all about true indentation, by adding and removing
1212 space characters. See also `org-indent.el' which does level-dependent
1213 indentation in a virtual way, i.e. at display time in Emacs."
1214 :group
'org-edit-structure
1217 (defcustom org-special-ctrl-a
/e nil
1218 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1220 When t, `C-a' will bring back the cursor to the beginning of the
1221 headline text, i.e. after the stars and after a possible TODO
1222 keyword. In an item, this will be the position after bullet and
1223 check-box, if any. When the cursor is already at that position,
1224 another `C-a' will bring it to the beginning of the line.
1226 `C-e' will jump to the end of the headline, ignoring the presence
1227 of tags in the headline. A second `C-e' will then jump to the
1228 true end of the line, after any tags. This also means that, when
1229 this variable is non-nil, `C-e' also will never jump beyond the
1230 end of the heading of a folded section, i.e. not after the
1233 When set to the symbol `reversed', the first `C-a' or `C-e' works
1234 normally, going to the true line boundary first. Only a directly
1235 following, identical keypress will bring the cursor to the
1238 This may also be a cons cell where the behavior for `C-a' and
1239 `C-e' is set separately."
1240 :group
'org-edit-structure
1242 (const :tag
"off" nil
)
1243 (const :tag
"on: after stars/bullet and before tags first" t
)
1244 (const :tag
"reversed: true line boundary first" reversed
)
1245 (cons :tag
"Set C-a and C-e separately"
1246 (choice :tag
"Special C-a"
1247 (const :tag
"off" nil
)
1248 (const :tag
"on: after stars/bullet first" t
)
1249 (const :tag
"reversed: before stars/bullet first" reversed
))
1250 (choice :tag
"Special C-e"
1251 (const :tag
"off" nil
)
1252 (const :tag
"on: before tags first" t
)
1253 (const :tag
"reversed: after tags first" reversed
)))))
1254 (org-defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
)
1256 (defcustom org-special-ctrl-k nil
1257 "Non-nil means `C-k' will behave specially in headlines.
1258 When nil, `C-k' will call the default `kill-line' command.
1259 When t, the following will happen while the cursor is in the headline:
1261 - When the cursor is at the beginning of a headline, kill the entire
1262 line and possible the folded subtree below the line.
1263 - When in the middle of the headline text, kill the headline up to the tags.
1264 - When after the headline text, kill the tags."
1265 :group
'org-edit-structure
1268 (defcustom org-ctrl-k-protect-subtree nil
1269 "Non-nil means, do not delete a hidden subtree with C-k.
1270 When set to the symbol `error', simply throw an error when C-k is
1271 used to kill (part-of) a headline that has hidden text behind it.
1272 Any other non-nil value will result in a query to the user, if it is
1273 OK to kill that hidden subtree. When nil, kill without remorse."
1274 :group
'org-edit-structure
1277 (const :tag
"Do not protect hidden subtrees" nil
)
1278 (const :tag
"Protect hidden subtrees with a security query" t
)
1279 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1281 (defcustom org-catch-invisible-edits nil
1282 "Check if in invisible region before inserting or deleting a character.
1285 nil Do not check, so just do invisible edits.
1286 error Throw an error and do nothing.
1287 show Make point visible, and do the requested edit.
1288 show-and-error Make point visible, then throw an error and abort the edit.
1289 smart Make point visible, and do insertion/deletion if it is
1290 adjacent to visible text and the change feels predictable.
1291 Never delete a previously invisible character or add in the
1292 middle or right after an invisible region. Basically, this
1293 allows insertion and backward-delete right before ellipses.
1294 FIXME: maybe in this case we should not even show?"
1295 :group
'org-edit-structure
1298 (const :tag
"Do not check" nil
)
1299 (const :tag
"Throw error when trying to edit" error
)
1300 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1301 (const :tag
"Show invisible part and do the edit" show
)
1302 (const :tag
"Be smart and do the right thing" smart
)))
1304 (defcustom org-yank-folded-subtrees t
1305 "Non-nil means when yanking subtrees, fold them.
1306 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1307 it starts with a heading and all other headings in it are either children
1308 or siblings, then fold all the subtrees. However, do this only if no
1309 text after the yank would be swallowed into a folded tree by this action."
1310 :group
'org-edit-structure
1313 (defcustom org-yank-adjusted-subtrees nil
1314 "Non-nil means when yanking subtrees, adjust the level.
1315 With this setting, `org-paste-subtree' is used to insert the subtree, see
1316 this function for details."
1317 :group
'org-edit-structure
1320 (defcustom org-M-RET-may-split-line
'((default . t
))
1321 "Non-nil means M-RET will split the line at the cursor position.
1322 When nil, it will go to the end of the line before making a
1324 You may also set this option in a different way for different
1325 contexts. Valid contexts are:
1327 headline when creating a new headline
1328 item when creating a new item
1329 table in a table field
1330 default the value to be used for all contexts not explicitly
1332 :group
'org-structure
1335 (const :tag
"Always" t
)
1336 (const :tag
"Never" nil
)
1337 (repeat :greedy t
:tag
"Individual contexts"
1339 (choice :tag
"Context"
1347 (defcustom org-insert-heading-respect-content nil
1348 "Non-nil means insert new headings after the current subtree.
1349 When nil, the new heading is created directly after the current line.
1350 The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
1351 this variable on for the duration of the command."
1352 :group
'org-structure
1355 (defcustom org-blank-before-new-entry
'((heading . auto
)
1356 (plain-list-item . auto
))
1357 "Should `org-insert-heading' leave a blank line before new heading/item?
1358 The value is an alist, with `heading' and `plain-list-item' as CAR,
1359 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1360 which case Org will look at the surrounding headings/items and try to
1361 make an intelligent decision whether to insert a blank line or not.
1363 For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
1364 the setting here is ignored and no empty line is inserted to avoid breaking
1365 the list structure."
1366 :group
'org-edit-structure
1368 (cons (const heading
)
1369 (choice (const :tag
"Never" nil
)
1370 (const :tag
"Always" t
)
1371 (const :tag
"Auto" auto
)))
1372 (cons (const plain-list-item
)
1373 (choice (const :tag
"Never" nil
)
1374 (const :tag
"Always" t
)
1375 (const :tag
"Auto" auto
)))))
1377 (defcustom org-insert-heading-hook nil
1378 "Hook being run after inserting a new heading."
1379 :group
'org-edit-structure
1382 (defcustom org-enable-fixed-width-editor t
1383 "Non-nil means lines starting with \":\" are treated as fixed-width.
1384 This currently only means they are never auto-wrapped.
1385 When nil, such lines will be treated like ordinary lines.
1386 See also the QUOTE keyword."
1387 :group
'org-edit-structure
1390 (defcustom org-goto-auto-isearch t
1391 "Non-nil means typing characters in `org-goto' starts incremental search.
1392 When nil, you can use these keybindings to navigate the buffer:
1394 q Quit the org-goto interface
1395 n Go to the next visible heading
1396 p Go to the previous visible heading
1397 f Go one heading forward on same level
1398 b Go one heading backward on same level
1399 u Go one heading up"
1400 :group
'org-edit-structure
1403 (defgroup org-sparse-trees nil
1404 "Options concerning sparse trees in Org-mode."
1405 :tag
"Org Sparse Trees"
1406 :group
'org-structure
)
1408 (defcustom org-highlight-sparse-tree-matches t
1409 "Non-nil means highlight all matches that define a sparse tree.
1410 The highlights will automatically disappear the next time the buffer is
1411 changed by an edit command."
1412 :group
'org-sparse-trees
1415 (defcustom org-remove-highlights-with-change t
1416 "Non-nil means any change to the buffer will remove temporary highlights.
1417 Such highlights are created by `org-occur' and `org-clock-display'.
1418 When nil, `C-c C-c needs to be used to get rid of the highlights.
1419 The highlights created by `org-preview-latex-fragment' always need
1420 `C-c C-c' to be removed."
1421 :group
'org-sparse-trees
1426 (defcustom org-occur-hook
'(org-first-headline-recenter)
1427 "Hook that is run after `org-occur' has constructed a sparse tree.
1428 This can be used to recenter the window to show as much of the structure
1430 :group
'org-sparse-trees
1433 (defgroup org-imenu-and-speedbar nil
1434 "Options concerning imenu and speedbar in Org-mode."
1435 :tag
"Org Imenu and Speedbar"
1436 :group
'org-structure
)
1438 (defcustom org-imenu-depth
2
1439 "The maximum level for Imenu access to Org-mode headlines.
1440 This also applied for speedbar access."
1441 :group
'org-imenu-and-speedbar
1444 (defgroup org-table nil
1445 "Options concerning tables in Org-mode."
1449 (defcustom org-enable-table-editor
'optimized
1450 "Non-nil means lines starting with \"|\" are handled by the table editor.
1451 When nil, such lines will be treated like ordinary lines.
1453 When equal to the symbol `optimized', the table editor will be optimized to
1455 - Automatic overwrite mode in front of whitespace in table fields.
1456 This makes the structure of the table stay in tact as long as the edited
1457 field does not exceed the column width.
1458 - Minimize the number of realigns. Normally, the table is aligned each time
1459 TAB or RET are pressed to move to another field. With optimization this
1460 happens only if changes to a field might have changed the column width.
1461 Optimization requires replacing the functions `self-insert-command',
1462 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1463 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1464 very good at guessing when a re-align will be necessary, but you can always
1465 force one with \\[org-ctrl-c-ctrl-c].
1467 If you would like to use the optimized version in Org-mode, but the
1468 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1470 This variable can be used to turn on and off the table editor during a session,
1471 but in order to toggle optimization, a restart is required.
1473 See also the variable `org-table-auto-blank-field'."
1476 (const :tag
"off" nil
)
1478 (const :tag
"on, optimized" optimized
)))
1480 (defcustom org-self-insert-cluster-for-undo
(or (featurep 'xemacs
)
1481 (version<= emacs-version
"24.1"))
1482 "Non-nil means cluster self-insert commands for undo when possible.
1483 If this is set, then, like in the Emacs command loop, 20 consecutive
1484 characters will be undone together.
1485 This is configurable, because there is some impact on typing performance."
1489 (defcustom org-table-tab-recognizes-table.el t
1490 "Non-nil means TAB will automatically notice a table.el table.
1491 When it sees such a table, it moves point into it and - if necessary -
1492 calls `table-recognize-table'."
1493 :group
'org-table-editing
1496 (defgroup org-link nil
1497 "Options concerning links in Org-mode."
1501 (defvar org-link-abbrev-alist-local nil
1502 "Buffer-local version of `org-link-abbrev-alist', which see.
1503 The value of this is taken from the #+LINK lines.")
1504 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1506 (defcustom org-link-abbrev-alist nil
1507 "Alist of link abbreviations.
1508 The car of each element is a string, to be replaced at the start of a link.
1509 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1510 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1512 [[linkkey:tag][description]]
1514 The 'linkkey' must be a word word, starting with a letter, followed
1515 by letters, numbers, '-' or '_'.
1517 If REPLACE is a string, the tag will simply be appended to create the link.
1518 If the string contains \"%s\", the tag will be inserted there. If the string
1519 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1520 at that point (see the function `url-hexify-string'). If the string contains
1521 the specifier \"%(my-function)\", then the custom function `my-function' will
1522 be invoked: this function takes the tag as its only argument and must return
1525 REPLACE may also be a function that will be called with the tag as the
1526 only argument to create the link, which should be returned as a string.
1528 See the manual for examples."
1532 (string :tag
"Protocol")
1534 (string :tag
"Format")
1537 (defcustom org-descriptive-links t
1538 "Non-nil means Org will display descriptive links.
1539 E.g. [[http://orgmode.org][Org website]] will be displayed as
1540 \"Org Website\", hiding the link itself and just displaying its
1541 description. When set to `nil', Org will display the full links
1544 You can interactively set the value of this variable by calling
1545 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1549 (defcustom org-link-file-path-type
'adaptive
1550 "How the path name in file links should be stored.
1553 relative Relative to the current directory, i.e. the directory of the file
1554 into which the link is being inserted.
1555 absolute Absolute path, if possible with ~ for home directory.
1556 noabbrev Absolute path, no abbreviation of home directory.
1557 adaptive Use relative path for files in the current directory and sub-
1558 directories of it. For other files, use an absolute path."
1566 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1567 "Types of links that should be activated in Org-mode files.
1568 This is a list of symbols, each leading to the activation of a certain link
1569 type. In principle, it does not hurt to turn on most link types - there may
1570 be a small gain when turning off unused link types. The types are:
1572 bracket The recommended [[link][description]] or [[link]] links with hiding.
1573 angle Links in angular brackets that may contain whitespace like
1574 <bbdb:Carsten Dominik>.
1575 plain Plain links in normal text, no whitespace, like http://google.com.
1576 radio Text that is matched by a radio target, see manual for details.
1577 tag Tag settings in a headline (link to tag search).
1578 date Time stamps (link to calendar).
1579 footnote Footnote labels.
1581 Changing this variable requires a restart of Emacs to become effective."
1583 :type
'(set :greedy t
1584 (const :tag
"Double bracket links" bracket
)
1585 (const :tag
"Angular bracket links" angle
)
1586 (const :tag
"Plain text links" plain
)
1587 (const :tag
"Radio target matches" radio
)
1588 (const :tag
"Tags" tag
)
1589 (const :tag
"Timestamps" date
)
1590 (const :tag
"Footnotes" footnote
)))
1592 (defcustom org-make-link-description-function nil
1593 "Function to use for generating link descriptions from links.
1594 When nil, the link location will be used. This function must take
1595 two parameters: the first one is the link, the second one is the
1596 description generated by `org-insert-link'. The function should
1597 return the description to use."
1601 (defgroup org-link-store nil
1602 "Options concerning storing links in Org-mode."
1603 :tag
"Org Store Link"
1606 (defcustom org-url-hexify-p t
1607 "When non-nil, hexify URL when creating a link."
1610 :group
'org-link-store
)
1612 (defcustom org-email-link-description-format
"Email %c: %.30s"
1613 "Format of the description part of a link to an email or usenet message.
1614 The following %-escapes will be replaced by corresponding information:
1616 %F full \"From\" field
1617 %f name, taken from \"From\" field, address if no name
1618 %T full \"To\" field
1619 %t first name in \"To\" field, address if no name
1620 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1621 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1626 You may use normal field width specification between the % and the letter.
1627 This is for example useful to limit the length of the subject.
1629 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1630 :group
'org-link-store
1633 (defcustom org-from-is-user-regexp
1635 (when (and user-mail-address
(not (string= user-mail-address
"")))
1636 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1637 (when (and user-full-name
(not (string= user-full-name
"")))
1638 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1639 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1640 "Regexp matched against the \"From:\" header of an email or usenet message.
1641 It should match if the message is from the user him/herself."
1642 :group
'org-link-store
1645 (defcustom org-context-in-file-links t
1646 "Non-nil means file links from `org-store-link' contain context.
1647 A search string will be added to the file name with :: as separator and
1648 used to find the context when the link is activated by the command
1649 `org-open-at-point'. When this option is t, the entire active region
1650 will be placed in the search string of the file link. If set to a
1651 positive integer, only the first n lines of context will be stored.
1653 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1654 negates this setting for the duration of the command."
1655 :group
'org-link-store
1656 :type
'(choice boolean integer
))
1658 (defcustom org-keep-stored-link-after-insertion nil
1659 "Non-nil means keep link in list for entire session.
1661 The command `org-store-link' adds a link pointing to the current
1662 location to an internal list. These links accumulate during a session.
1663 The command `org-insert-link' can be used to insert links into any
1664 Org-mode file (offering completion for all stored links). When this
1665 option is nil, every link which has been inserted once using \\[org-insert-link]
1666 will be removed from the list, to make completing the unused links
1668 :group
'org-link-store
1671 (defgroup org-link-follow nil
1672 "Options concerning following links in Org-mode."
1673 :tag
"Org Follow Link"
1676 (defcustom org-link-translation-function nil
1677 "Function to translate links with different syntax to Org syntax.
1678 This can be used to translate links created for example by the Planner
1679 or emacs-wiki packages to Org syntax.
1680 The function must accept two parameters, a TYPE containing the link
1681 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1682 which is everything after the link protocol. It should return a cons
1683 with possibly modified values of type and path.
1684 Org contains a function for this, so if you set this variable to
1685 `org-translate-link-from-planner', you should be able follow many
1686 links created by planner."
1687 :group
'org-link-follow
1690 (defcustom org-follow-link-hook nil
1691 "Hook that is run after a link has been followed."
1692 :group
'org-link-follow
1695 (defcustom org-tab-follows-link nil
1696 "Non-nil means on links TAB will follow the link.
1697 Needs to be set before org.el is loaded.
1698 This really should not be used, it does not make sense, and the
1699 implementation is bad."
1700 :group
'org-link-follow
1703 (defcustom org-return-follows-link nil
1704 "Non-nil means on links RET will follow the link.
1705 In tables, the special behavior of RET has precedence."
1706 :group
'org-link-follow
1709 (defcustom org-mouse-1-follows-link
1710 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1711 "Non-nil means mouse-1 on a link will follow the link.
1712 A longer mouse click will still set point. Does not work on XEmacs.
1713 Needs to be set before org.el is loaded."
1714 :group
'org-link-follow
1717 (defcustom org-mark-ring-length
4
1718 "Number of different positions to be recorded in the ring.
1719 Changing this requires a restart of Emacs to work correctly."
1720 :group
'org-link-follow
1723 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1724 "Non-nil means internal links in Org files must exactly match a headline.
1725 When nil, the link search tries to match a phrase with all words
1726 in the search text."
1727 :group
'org-link-follow
1730 (const :tag
"Use fuzzy text search" nil
)
1731 (const :tag
"Match only exact headline" t
)
1732 (const :tag
"Match exact headline or query to create it"
1735 (defcustom org-link-frame-setup
1736 '((vm . vm-visit-folder-other-frame
)
1737 (vm-imap . vm-visit-imap-folder-other-frame
)
1738 (gnus . org-gnus-no-new-news
)
1739 (file . find-file-other-window
)
1740 (wl . wl-other-frame
))
1741 "Setup the frame configuration for following links.
1742 When following a link with Emacs, it may often be useful to display
1743 this link in another window or frame. This variable can be used to
1744 set this up for the different types of links.
1747 `vm-visit-folder-other-window'
1748 `vm-visit-folder-other-frame'
1749 For Gnus, use any of
1752 `org-gnus-no-new-news'
1753 For FILE, use any of
1755 `find-file-other-window'
1756 `find-file-other-frame'
1757 For Wanderlust use any of
1760 For the calendar, use the variable `calendar-setup'.
1761 For BBDB, it is currently only possible to display the matches in
1763 :group
'org-link-follow
1767 (const vm-visit-folder
)
1768 (const vm-visit-folder-other-window
)
1769 (const vm-visit-folder-other-frame
)))
1773 (const gnus-other-frame
)
1774 (const org-gnus-no-new-news
)))
1778 (const find-file-other-window
)
1779 (const find-file-other-frame
)))
1783 (const wl-other-frame
)))))
1785 (defcustom org-display-internal-link-with-indirect-buffer nil
1786 "Non-nil means use indirect buffer to display infile links.
1787 Activating internal links (from one location in a file to another location
1788 in the same file) normally just jumps to the location. When the link is
1789 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1791 another window. When this option is set, the other window actually displays
1792 an indirect buffer clone of the current buffer, to avoid any visibility
1793 changes to the current buffer."
1794 :group
'org-link-follow
1797 (defcustom org-open-non-existing-files nil
1798 "Non-nil means `org-open-file' will open non-existing files.
1799 When nil, an error will be generated.
1800 This variable applies only to external applications because they
1801 might choke on non-existing files. If the link is to a file that
1802 will be opened in Emacs, the variable is ignored."
1803 :group
'org-link-follow
1806 (defcustom org-open-directory-means-index-dot-org nil
1807 "Non-nil means a link to a directory really means to index.org.
1808 When nil, following a directory link will run dired or open a finder/explorer
1809 window on that directory."
1810 :group
'org-link-follow
1813 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1814 "Function and arguments to call for following mailto links.
1815 This is a list with the first element being a Lisp function, and the
1816 remaining elements being arguments to the function. In string arguments,
1817 %a will be replaced by the address, and %s will be replaced by the subject
1818 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1819 :group
'org-link-follow
1821 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1822 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1823 (const :tag
"message-mail" (message-mail "%a" "%s"))
1824 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1826 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1827 "Non-nil means ask for confirmation before executing shell links.
1828 Shell links can be dangerous: just think about a link
1830 [[shell:rm -rf ~/*][Google Search]]
1832 This link would show up in your Org-mode document as \"Google Search\",
1833 but really it would remove your entire home directory.
1834 Therefore we advise against setting this variable to nil.
1835 Just change it to `y-or-n-p' if you want to confirm with a
1836 single keystroke rather than having to type \"yes\"."
1837 :group
'org-link-follow
1839 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1840 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1841 (const :tag
"no confirmation (dangerous)" nil
)))
1842 (put 'org-confirm-shell-link-function
1843 'safe-local-variable
1844 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1846 (defcustom org-confirm-shell-link-not-regexp
""
1847 "A regexp to skip confirmation for shell links."
1848 :group
'org-link-follow
1852 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1853 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1854 Elisp links can be dangerous: just think about a link
1856 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1858 This link would show up in your Org-mode document as \"Google Search\",
1859 but really it would remove your entire home directory.
1860 Therefore we advise against setting this variable to nil.
1861 Just change it to `y-or-n-p' if you want to confirm with a
1862 single keystroke rather than having to type \"yes\"."
1863 :group
'org-link-follow
1865 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1866 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1867 (const :tag
"no confirmation (dangerous)" nil
)))
1868 (put 'org-confirm-shell-link-function
1869 'safe-local-variable
1870 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1872 (defcustom org-confirm-elisp-link-not-regexp
""
1873 "A regexp to skip confirmation for Elisp links."
1874 :group
'org-link-follow
1878 (defconst org-file-apps-defaults-gnu
1882 "Default file applications on a UNIX or GNU/Linux system.
1883 See `org-file-apps'.")
1885 (defconst org-file-apps-defaults-macosx
1888 (system .
"open %s")
1890 ("eps.gz" .
"gv %s")
1892 ("fig" .
"xfig %s"))
1893 "Default file applications on a MacOS X system.
1894 The system \"open\" is known as a default, but we use X11 applications
1895 for some files for which the OS does not have a good default.
1896 See `org-file-apps'.")
1898 (defconst org-file-apps-defaults-windowsnt
1902 (list (if (featurep 'xemacs
)
1903 'mswindows-shell-execute
1907 (list (if (featurep 'xemacs
)
1908 'mswindows-shell-execute
1911 "Default file applications on a Windows NT system.
1912 The system \"open\" is used for most files.
1913 See `org-file-apps'.")
1915 (defcustom org-file-apps
1916 '((auto-mode . emacs
)
1917 ("\\.mm\\'" . default
)
1918 ("\\.x?html?\\'" . default
)
1919 ("\\.pdf\\'" . default
))
1920 "External applications for opening `file:path' items in a document.
1921 Org-mode uses system defaults for different file types, but
1922 you can use this variable to set the application for a given file
1923 extension. The entries in this list are cons cells where the car identifies
1924 files and the cdr the corresponding command. Possible values for the
1926 \"string\" A string as a file identifier can be interpreted in different
1927 ways, depending on its contents:
1929 - Alphanumeric characters only:
1930 Match links with this file extension.
1931 Example: (\"pdf\" . \"evince %s\")
1932 to open PDFs with evince.
1934 - Regular expression: Match links where the
1935 filename matches the regexp. If you want to
1936 use groups here, use shy groups.
1938 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1939 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1940 to open *.html and *.xhtml with firefox.
1942 - Regular expression which contains (non-shy) groups:
1943 Match links where the whole link, including \"::\", and
1944 anything after that, matches the regexp.
1945 In a custom command string, %1, %2, etc. are replaced with
1946 the parts of the link that were matched by the groups.
1947 For backwards compatibility, if a command string is given
1948 that does not use any of the group matches, this case is
1949 handled identically to the second one (i.e. match against
1951 In a custom lisp form, you can access the group matches with
1952 (match-string n link).
1954 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1955 to open [[file:document.pdf::5]] with evince at page 5.
1957 `directory' Matches a directory
1958 `remote' Matches a remote file, accessible through tramp or efs.
1959 Remote files most likely should be visited through Emacs
1960 because external applications cannot handle such paths.
1961 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1962 so all files Emacs knows how to handle. Using this with
1963 command `emacs' will open most files in Emacs. Beware that this
1964 will also open html files inside Emacs, unless you add
1965 (\"html\" . default) to the list as well.
1966 t Default for files not matched by any of the other options.
1967 `system' The system command to open files, like `open' on Windows
1968 and Mac OS X, and mailcap under GNU/Linux. This is the command
1969 that will be selected if you call `C-c C-o' with a double
1970 \\[universal-argument] \\[universal-argument] prefix.
1972 Possible values for the command are:
1973 `emacs' The file will be visited by the current Emacs process.
1974 `default' Use the default application for this file type, which is the
1975 association for t in the list, most likely in the system-specific
1977 This can be used to overrule an unwanted setting in the
1978 system-specific variable.
1979 `system' Use the system command for opening files, like \"open\".
1980 This command is specified by the entry whose car is `system'.
1981 Most likely, the system-specific version of this variable
1982 does define this command, but you can overrule/replace it
1984 string A command to be executed by a shell; %s will be replaced
1985 by the path to the file.
1986 sexp A Lisp form which will be evaluated. The file path will
1987 be available in the Lisp variable `file'.
1988 For more examples, see the system specific constants
1989 `org-file-apps-defaults-macosx'
1990 `org-file-apps-defaults-windowsnt'
1991 `org-file-apps-defaults-gnu'."
1992 :group
'org-link-follow
1994 (cons (choice :value
""
1995 (string :tag
"Extension")
1996 (const :tag
"System command to open files" system
)
1997 (const :tag
"Default for unrecognized files" t
)
1998 (const :tag
"Remote file" remote
)
1999 (const :tag
"Links to a directory" directory
)
2000 (const :tag
"Any files that have Emacs modes"
2003 (const :tag
"Visit with Emacs" emacs
)
2004 (const :tag
"Use default" default
)
2005 (const :tag
"Use the system command" system
)
2006 (string :tag
"Command")
2007 (sexp :tag
"Lisp form")))))
2009 (defcustom org-doi-server-url
"http://dx.doi.org/"
2010 "The URL of the DOI server."
2013 :group
'org-link-follow
)
2015 (defgroup org-refile nil
2016 "Options concerning refiling entries in Org-mode."
2020 (defcustom org-directory
"~/org"
2021 "Directory with org files.
2022 This is just a default location to look for Org files. There is no need
2023 at all to put your files into this directory. It is only used in the
2024 following situations:
2026 1. When a capture template specifies a target file that is not an
2027 absolute path. The path will then be interpreted relative to
2029 2. When a capture note is filed away in an interactive way (when exiting the
2030 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
2031 with `org-directory' as the default path."
2033 :group
'org-remember
2037 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
2038 "Default target for storing notes.
2039 Used as a fall back file for org-remember.el and org-capture.el, for
2040 templates that do not specify a target file."
2042 :group
'org-remember
2045 (const :tag
"Default from remember-data-file" nil
)
2048 (defcustom org-goto-interface
'outline
2049 "The default interface to be used for `org-goto'.
2051 outline The interface shows an outline of the relevant file
2052 and the correct heading is found by moving through
2053 the outline or by searching with incremental search.
2054 outline-path-completion Headlines in the current buffer are offered via
2055 completion. This is the interface also used by
2056 the refile command."
2059 (const :tag
"Outline" outline
)
2060 (const :tag
"Outline-path-completion" outline-path-completion
)))
2062 (defcustom org-goto-max-level
5
2063 "Maximum target level when running `org-goto' with refile interface."
2067 (defcustom org-reverse-note-order nil
2068 "Non-nil means store new notes at the beginning of a file or entry.
2069 When nil, new notes will be filed to the end of a file or entry.
2070 This can also be a list with cons cells of regular expressions that
2071 are matched against file names, and values."
2072 :group
'org-remember
2076 (const :tag
"Reverse always" t
)
2077 (const :tag
"Reverse never" nil
)
2078 (repeat :tag
"By file name regexp"
2079 (cons regexp boolean
))))
2081 (defcustom org-log-refile nil
2082 "Information to record when a task is refiled.
2084 Possible values are:
2086 nil Don't add anything
2087 time Add a time stamp to the task
2088 note Prompt for a note and add it with template `org-log-note-headings'
2090 This option can also be set with on a per-file-basis with
2092 #+STARTUP: nologrefile
2093 #+STARTUP: logrefile
2094 #+STARTUP: lognoterefile
2096 You can have local logging settings for a subtree by setting the LOGGING
2097 property to one or more of these keywords.
2099 When bulk-refiling from the agenda, the value `note' is forbidden and
2100 will temporarily be changed to `time'."
2102 :group
'org-progress
2105 (const :tag
"No logging" nil
)
2106 (const :tag
"Record timestamp" time
)
2107 (const :tag
"Record timestamp with note." note
)))
2109 (defcustom org-refile-targets nil
2110 "Targets for refiling entries with \\[org-refile].
2111 This is a list of cons cells. Each cell contains:
2112 - a specification of the files to be considered, either a list of files,
2113 or a symbol whose function or variable value will be used to retrieve
2114 a file name or a list of file names. If you use `org-agenda-files' for
2115 that, all agenda files will be scanned for targets. Nil means consider
2116 headings in the current buffer.
2117 - A specification of how to find candidate refile targets. This may be
2119 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
2120 This tag has to be present in all target headlines, inheritance will
2122 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
2124 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
2125 headlines that are refiling targets.
2126 - a cons cell (:level . N). Any headline of level N is considered a target.
2127 Note that, when `org-odd-levels-only' is set, level corresponds to
2128 order in hierarchy, not to the number of stars.
2129 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
2130 Note that, when `org-odd-levels-only' is set, level corresponds to
2131 order in hierarchy, not to the number of stars.
2133 Each element of this list generates a set of possible targets.
2134 The union of these sets is presented (with completion) to
2135 the user by `org-refile'.
2137 You can set the variable `org-refile-target-verify-function' to a function
2138 to verify each headline found by the simple criteria above.
2140 When this variable is nil, all top-level headlines in the current buffer
2141 are used, equivalent to the value `((nil . (:level . 1))'."
2145 (choice :value org-agenda-files
2146 (const :tag
"All agenda files" org-agenda-files
)
2147 (const :tag
"Current buffer" nil
)
2148 (function) (variable) (file))
2149 (choice :tag
"Identify target headline by"
2150 (cons :tag
"Specific tag" (const :value
:tag
) (string))
2151 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
2152 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
2153 (cons :tag
"Level number" (const :value
:level
) (integer))
2154 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
2156 (defcustom org-refile-target-verify-function nil
2157 "Function to verify if the headline at point should be a refile target.
2158 The function will be called without arguments, with point at the
2159 beginning of the headline. It should return t and leave point
2160 where it is if the headline is a valid target for refiling.
2162 If the target should not be selected, the function must return nil.
2163 In addition to this, it may move point to a place from where the search
2164 should be continued. For example, the function may decide that the entire
2165 subtree of the current entry should be excluded and move point to the end
2170 (defcustom org-refile-use-cache nil
2171 "Non-nil means cache refile targets to speed up the process.
2172 The cache for a particular file will be updated automatically when
2173 the buffer has been killed, or when any of the marker used for flagging
2174 refile targets no longer points at a live buffer.
2175 If you have added new entries to a buffer that might themselves be targets,
2176 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2177 find that easier, `C-u C-u C-u C-c C-w'."
2182 (defcustom org-refile-use-outline-path nil
2183 "Non-nil means provide refile targets as paths.
2184 So a level 3 headline will be available as level1/level2/level3.
2186 When the value is `file', also include the file name (without directory)
2187 into the path. In this case, you can also stop the completion after
2188 the file name, to get entries inserted as top level in the file.
2190 When `full-file-path', include the full file path."
2193 (const :tag
"Not" nil
)
2194 (const :tag
"Yes" t
)
2195 (const :tag
"Start with file name" file
)
2196 (const :tag
"Start with full file path" full-file-path
)))
2198 (defcustom org-outline-path-complete-in-steps t
2199 "Non-nil means complete the outline path in hierarchical steps.
2200 When Org-mode uses the refile interface to select an outline path
2201 \(see variable `org-refile-use-outline-path'), the completion of
2202 the path can be done is a single go, or if can be done in steps down
2203 the headline hierarchy. Going in steps is probably the best if you
2204 do not use a special completion package like `ido' or `icicles'.
2205 However, when using these packages, going in one step can be very
2206 fast, while still showing the whole path to the entry."
2210 (defcustom org-refile-allow-creating-parent-nodes nil
2211 "Non-nil means allow to create new nodes as refile targets.
2212 New nodes are then created by adding \"/new node name\" to the completion
2213 of an existing node. When the value of this variable is `confirm',
2214 new node creation must be confirmed by the user (recommended)
2215 When nil, the completion must match an existing entry.
2217 Note that, if the new heading is not seen by the criteria
2218 listed in `org-refile-targets', multiple instances of the same
2219 heading would be created by trying again to file under the new
2223 (const :tag
"Never" nil
)
2224 (const :tag
"Always" t
)
2225 (const :tag
"Prompt for confirmation" confirm
)))
2227 (defcustom org-refile-active-region-within-subtree nil
2228 "Non-nil means also refile active region within a subtree.
2230 By default `org-refile' doesn't allow refiling regions if they
2231 don't contain a set of subtrees, but it might be convenient to
2232 do so sometimes: in that case, the first line of the region is
2233 converted to a headline before refiling."
2238 (defgroup org-todo nil
2239 "Options concerning TODO items in Org-mode."
2243 (defgroup org-progress nil
2244 "Options concerning Progress logging in Org-mode."
2248 (defvar org-todo-interpretation-widgets
2249 '((:tag
"Sequence (cycling hits every state)" sequence
)
2250 (:tag
"Type (cycling directly to DONE)" type
))
2251 "The available interpretation symbols for customizing `org-todo-keywords'.
2252 Interested libraries should add to this list.")
2254 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2255 "List of TODO entry keyword sequences and their interpretation.
2256 \\<org-mode-map>This is a list of sequences.
2258 Each sequence starts with a symbol, either `sequence' or `type',
2259 indicating if the keywords should be interpreted as a sequence of
2260 action steps, or as different types of TODO items. The first
2261 keywords are states requiring action - these states will select a headline
2262 for inclusion into the global TODO list Org-mode produces. If one of
2263 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2264 signify that no further action is necessary. If \"|\" is not found,
2265 the last keyword is treated as the only DONE state of the sequence.
2267 The command \\[org-todo] cycles an entry through these states, and one
2268 additional state where no keyword is present. For details about this
2269 cycling, see the manual.
2271 TODO keywords and interpretation can also be set on a per-file basis with
2272 the special #+SEQ_TODO and #+TYP_TODO lines.
2274 Each keyword can optionally specify a character for fast state selection
2275 \(in combination with the variable `org-use-fast-todo-selection')
2276 and specifiers for state change logging, using the same syntax that
2277 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2278 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2279 indicates to record a time stamp each time this state is selected.
2281 Each keyword may also specify if a timestamp or a note should be
2282 recorded when entering or leaving the state, by adding additional
2283 characters in the parenthesis after the keyword. This looks like this:
2284 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2285 record only the time of the state change. With X and Y being either
2286 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2287 Y when leaving the state if and only if the *target* state does not
2288 define X. You may omit any of the fast-selection key or X or /Y,
2289 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2291 For backward compatibility, this variable may also be just a list
2292 of keywords. In this case the interpretation (sequence or type) will be
2293 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2295 :group
'org-keywords
2297 (repeat :tag
"Old syntax, just keywords"
2298 (string :tag
"Keyword"))
2299 (repeat :tag
"New syntax"
2302 :tag
"Interpretation"
2303 ;;Quick and dirty way to see
2304 ;;`org-todo-interpretations'. This takes the
2305 ;;place of item arguments
2313 org-todo-interpretation-widgets
))
2316 (string :tag
"Keyword"))))))
2318 (defvar org-todo-keywords-1 nil
2319 "All TODO and DONE keywords active in a buffer.")
2320 (make-variable-buffer-local 'org-todo-keywords-1
)
2321 (defvar org-todo-keywords-for-agenda nil
)
2322 (defvar org-done-keywords-for-agenda nil
)
2323 (defvar org-drawers-for-agenda nil
)
2324 (defvar org-todo-keyword-alist-for-agenda nil
)
2325 (defvar org-tag-alist-for-agenda nil
2326 "Alist of all tags from all agenda files.")
2327 (defvar org-tag-groups-alist-for-agenda nil
2328 "Alist of all groups tags from all current agenda files.")
2329 (defvar org-tag-groups-alist nil
)
2330 (make-variable-buffer-local 'org-tag-groups-alist
)
2331 (defvar org-agenda-contributing-files nil
)
2332 (defvar org-not-done-keywords nil
)
2333 (make-variable-buffer-local 'org-not-done-keywords
)
2334 (defvar org-done-keywords nil
)
2335 (make-variable-buffer-local 'org-done-keywords
)
2336 (defvar org-todo-heads nil
)
2337 (make-variable-buffer-local 'org-todo-heads
)
2338 (defvar org-todo-sets nil
)
2339 (make-variable-buffer-local 'org-todo-sets
)
2340 (defvar org-todo-log-states nil
)
2341 (make-variable-buffer-local 'org-todo-log-states
)
2342 (defvar org-todo-kwd-alist nil
)
2343 (make-variable-buffer-local 'org-todo-kwd-alist
)
2344 (defvar org-todo-key-alist nil
)
2345 (make-variable-buffer-local 'org-todo-key-alist
)
2346 (defvar org-todo-key-trigger nil
)
2347 (make-variable-buffer-local 'org-todo-key-trigger
)
2349 (defcustom org-todo-interpretation
'sequence
2350 "Controls how TODO keywords are interpreted.
2351 This variable is in principle obsolete and is only used for
2352 backward compatibility, if the interpretation of todo keywords is
2353 not given already in `org-todo-keywords'. See that variable for
2356 :group
'org-keywords
2357 :type
'(choice (const sequence
)
2360 (defcustom org-use-fast-todo-selection t
2361 "Non-nil means use the fast todo selection scheme with C-c C-t.
2362 This variable describes if and under what circumstances the cycling
2363 mechanism for TODO keywords will be replaced by a single-key, direct
2366 When nil, fast selection is never used.
2368 When the symbol `prefix', it will be used when `org-todo' is called
2369 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2370 `C-u t' in an agenda buffer.
2372 When t, fast selection is used by default. In this case, the prefix
2373 argument forces cycling instead.
2375 In all cases, the special interface is only used if access keys have
2376 actually been assigned by the user, i.e. if keywords in the configuration
2377 are followed by a letter in parenthesis, like TODO(t)."
2380 (const :tag
"Never" nil
)
2381 (const :tag
"By default" t
)
2382 (const :tag
"Only with C-u C-c C-t" prefix
)))
2384 (defcustom org-provide-todo-statistics t
2385 "Non-nil means update todo statistics after insert and toggle.
2386 ALL-HEADLINES means update todo statistics by including headlines
2387 with no TODO keyword as well, counting them as not done.
2388 A list of TODO keywords means the same, but skip keywords that are
2391 When this is set, todo statistics is updated in the parent of the
2392 current entry each time a todo state is changed."
2395 (const :tag
"Yes, only for TODO entries" t
)
2396 (const :tag
"Yes, including all entries" 'all-headlines
)
2397 (repeat :tag
"Yes, for TODOs in this list"
2398 (string :tag
"TODO keyword"))
2399 (other :tag
"No TODO statistics" nil
)))
2401 (defcustom org-hierarchical-todo-statistics t
2402 "Non-nil means TODO statistics covers just direct children.
2403 When nil, all entries in the subtree are considered.
2404 This has only an effect if `org-provide-todo-statistics' is set.
2405 To set this to nil for only a single subtree, use a COOKIE_DATA
2406 property and include the word \"recursive\" into the value."
2410 (defcustom org-after-todo-state-change-hook nil
2411 "Hook which is run after the state of a TODO item was changed.
2412 The new state (a string with a TODO keyword, or nil) is available in the
2413 Lisp variable `org-state'."
2417 (defvar org-blocker-hook nil
2418 "Hook for functions that are allowed to block a state change.
2420 Functions in this hook should not modify the buffer.
2421 Each function gets as its single argument a property list,
2422 see `org-trigger-hook' for more information about this list.
2424 If any of the functions in this hook returns nil, the state change
2427 (defvar org-trigger-hook nil
2428 "Hook for functions that are triggered by a state change.
2430 Each function gets as its single argument a property list with at
2431 least the following elements:
2433 (:type type-of-change :position pos-at-entry-start
2434 :from old-state :to new-state)
2436 Depending on the type, more properties may be present.
2438 This mechanism is currently implemented for:
2442 :type todo-state-change
2443 :from previous state (keyword as a string), or nil, or a symbol
2444 'todo' or 'done', to indicate the general type of state.
2445 :to new state, like in :from")
2447 (defcustom org-enforce-todo-dependencies nil
2448 "Non-nil means undone TODO entries will block switching the parent to DONE.
2449 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2450 be blocked if any prior sibling is not yet done.
2451 Finally, if the parent is blocked because of ordered siblings of its own,
2452 the child will also be blocked."
2453 :set
(lambda (var val
)
2456 (add-hook 'org-blocker-hook
2457 'org-block-todo-from-children-or-siblings-or-parent
)
2458 (remove-hook 'org-blocker-hook
2459 'org-block-todo-from-children-or-siblings-or-parent
)))
2463 (defcustom org-enforce-todo-checkbox-dependencies nil
2464 "Non-nil means unchecked boxes will block switching the parent to DONE.
2465 When this is nil, checkboxes have no influence on switching TODO states.
2466 When non-nil, you first need to check off all check boxes before the TODO
2467 entry can be switched to DONE.
2468 This variable needs to be set before org.el is loaded, and you need to
2469 restart Emacs after a change to make the change effective. The only way
2470 to change is while Emacs is running is through the customize interface."
2471 :set
(lambda (var val
)
2474 (add-hook 'org-blocker-hook
2475 'org-block-todo-from-checkboxes
)
2476 (remove-hook 'org-blocker-hook
2477 'org-block-todo-from-checkboxes
)))
2481 (defcustom org-treat-insert-todo-heading-as-state-change nil
2482 "Non-nil means inserting a TODO heading is treated as state change.
2483 So when the command \\[org-insert-todo-heading] is used, state change
2484 logging will apply if appropriate. When nil, the new TODO item will
2485 be inserted directly, and no logging will take place."
2489 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2490 "Non-nil means switching TODO states with S-cursor counts as state change.
2491 This is the default behavior. However, setting this to nil allows a
2492 convenient way to select a TODO state and bypass any logging associated
2497 (defcustom org-todo-state-tags-triggers nil
2498 "Tag changes that should be triggered by TODO state changes.
2499 This is a list. Each entry is
2501 (state-change (tag . flag) .......)
2503 State-change can be a string with a state, and empty string to indicate the
2504 state that has no TODO keyword, or it can be one of the symbols `todo'
2505 or `done', meaning any not-done or done state, respectively."
2509 (cons (choice :tag
"When changing to"
2510 (const :tag
"Not-done state" todo
)
2511 (const :tag
"Done state" done
)
2512 (string :tag
"State"))
2514 (cons :tag
"Tag action"
2516 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2518 (defcustom org-log-done nil
2519 "Information to record when a task moves to the DONE state.
2521 Possible values are:
2523 nil Don't add anything, just change the keyword
2524 time Add a time stamp to the task
2525 note Prompt for a note and add it with template `org-log-note-headings'
2527 This option can also be set with on a per-file-basis with
2529 #+STARTUP: nologdone
2531 #+STARTUP: lognotedone
2533 You can have local logging settings for a subtree by setting the LOGGING
2534 property to one or more of these keywords."
2536 :group
'org-progress
2538 (const :tag
"No logging" nil
)
2539 (const :tag
"Record CLOSED timestamp" time
)
2540 (const :tag
"Record CLOSED timestamp with note." note
)))
2542 ;; Normalize old uses of org-log-done.
2544 ((eq org-log-done t
) (setq org-log-done
'time
))
2545 ((and (listp org-log-done
) (memq 'done org-log-done
))
2546 (setq org-log-done
'note
)))
2548 (defcustom org-log-reschedule nil
2549 "Information to record when the scheduling date of a tasks is modified.
2551 Possible values are:
2553 nil Don't add anything, just change the date
2554 time Add a time stamp to the task
2555 note Prompt for a note and add it with template `org-log-note-headings'
2557 This option can also be set with on a per-file-basis with
2559 #+STARTUP: nologreschedule
2560 #+STARTUP: logreschedule
2561 #+STARTUP: lognotereschedule"
2563 :group
'org-progress
2565 (const :tag
"No logging" nil
)
2566 (const :tag
"Record timestamp" time
)
2567 (const :tag
"Record timestamp with note." note
)))
2569 (defcustom org-log-redeadline nil
2570 "Information to record when the deadline date of a tasks is modified.
2572 Possible values are:
2574 nil Don't add anything, just change the date
2575 time Add a time stamp to the task
2576 note Prompt for a note and add it with template `org-log-note-headings'
2578 This option can also be set with on a per-file-basis with
2580 #+STARTUP: nologredeadline
2581 #+STARTUP: logredeadline
2582 #+STARTUP: lognoteredeadline
2584 You can have local logging settings for a subtree by setting the LOGGING
2585 property to one or more of these keywords."
2587 :group
'org-progress
2589 (const :tag
"No logging" nil
)
2590 (const :tag
"Record timestamp" time
)
2591 (const :tag
"Record timestamp with note." note
)))
2593 (defcustom org-log-note-clock-out nil
2594 "Non-nil means record a note when clocking out of an item.
2595 This can also be configured on a per-file basis by adding one of
2596 the following lines anywhere in the buffer:
2598 #+STARTUP: lognoteclock-out
2599 #+STARTUP: nolognoteclock-out"
2601 :group
'org-progress
2604 (defcustom org-log-done-with-time t
2605 "Non-nil means the CLOSED time stamp will contain date and time.
2606 When nil, only the date will be recorded."
2607 :group
'org-progress
2610 (defcustom org-log-note-headings
2611 '((done .
"CLOSING NOTE %t")
2612 (state .
"State %-12s from %-12S %t")
2613 (note .
"Note taken on %t")
2614 (reschedule .
"Rescheduled from %S on %t")
2615 (delschedule .
"Not scheduled, was %S on %t")
2616 (redeadline .
"New deadline from %S on %t")
2617 (deldeadline .
"Removed deadline, was %S on %t")
2618 (refile .
"Refiled on %t")
2620 "Headings for notes added to entries.
2621 The value is an alist, with the car being a symbol indicating the note
2622 context, and the cdr is the heading to be used. The heading may also be the
2624 %t in the heading will be replaced by a time stamp.
2625 %T will be an active time stamp instead the default inactive one
2626 %d will be replaced by a short-format time stamp.
2627 %D will be replaced by an active short-format time stamp.
2628 %s will be replaced by the new TODO state, in double quotes.
2629 %S will be replaced by the old TODO state, in double quotes.
2630 %u will be replaced by the user name.
2631 %U will be replaced by the full user name.
2633 In fact, it is not a good idea to change the `state' entry, because
2634 agenda log mode depends on the format of these entries."
2636 :group
'org-progress
2637 :type
'(list :greedy t
2638 (cons (const :tag
"Heading when closing an item" done
) string
)
2640 "Heading when changing todo state (todo sequence only)"
2642 (cons (const :tag
"Heading when just taking a note" note
) string
)
2643 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2644 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2645 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2646 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2647 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2648 (cons (const :tag
"Heading when refiling" refile
) string
)))
2650 (unless (assq 'note org-log-note-headings
)
2651 (push '(note .
"%t") org-log-note-headings
))
2653 (defcustom org-log-into-drawer nil
2654 "Non-nil means insert state change notes and time stamps into a drawer.
2655 When nil, state changes notes will be inserted after the headline and
2656 any scheduling and clock lines, but not inside a drawer.
2658 The value of this variable should be the name of the drawer to use.
2659 LOGBOOK is proposed as the default drawer for this purpose, you can
2660 also set this to a string to define the drawer of your choice.
2662 A value of t is also allowed, representing \"LOGBOOK\".
2664 A value of t or nil can also be set with on a per-file-basis with
2666 #+STARTUP: logdrawer
2667 #+STARTUP: nologdrawer
2669 If this variable is set, `org-log-state-notes-insert-after-drawers'
2672 You can set the property LOG_INTO_DRAWER to overrule this setting for
2675 :group
'org-progress
2677 (const :tag
"Not into a drawer" nil
)
2678 (const :tag
"LOGBOOK" t
)
2679 (string :tag
"Other")))
2681 (org-defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
)
2683 (defun org-log-into-drawer ()
2684 "Return the value of `org-log-into-drawer', but let properties overrule.
2685 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2686 used instead of the default value."
2687 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit t
)))
2689 ((not p
) org-log-into-drawer
)
2690 ((equal p
"nil") nil
)
2691 ((equal p
"t") "LOGBOOK")
2694 (defcustom org-log-state-notes-insert-after-drawers nil
2695 "Non-nil means insert state change notes after any drawers in entry.
2696 Only the drawers that *immediately* follow the headline and the
2697 deadline/scheduled line are skipped.
2698 When nil, insert notes right after the heading and perhaps the line
2699 with deadline/scheduling if present.
2701 This variable will have no effect if `org-log-into-drawer' is
2704 :group
'org-progress
2707 (defcustom org-log-states-order-reversed t
2708 "Non-nil means the latest state note will be directly after heading.
2709 When nil, the state change notes will be ordered according to time.
2711 This option can also be set with on a per-file-basis with
2713 #+STARTUP: logstatesreversed
2714 #+STARTUP: nologstatesreversed"
2716 :group
'org-progress
2719 (defcustom org-todo-repeat-to-state nil
2720 "The TODO state to which a repeater should return the repeating task.
2721 By default this is the first task in a TODO sequence, or the previous state
2722 in a TODO_TYP set. But you can specify another task here.
2723 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2726 :type
'(choice (const :tag
"Head of sequence" nil
)
2727 (string :tag
"Specific state")))
2729 (defcustom org-log-repeat
'time
2730 "Non-nil means record moving through the DONE state when triggering repeat.
2731 An auto-repeating task is immediately switched back to TODO when
2732 marked DONE. If you are not logging state changes (by adding \"@\"
2733 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2734 record a closing note, there will be no record of the task moving
2735 through DONE. This variable forces taking a note anyway.
2737 nil Don't force a record
2738 time Record a time stamp
2739 note Prompt for a note and add it with template `org-log-note-headings'
2741 This option can also be set with on a per-file-basis with
2743 #+STARTUP: nologrepeat
2744 #+STARTUP: logrepeat
2745 #+STARTUP: lognoterepeat
2747 You can have local logging settings for a subtree by setting the LOGGING
2748 property to one or more of these keywords."
2750 :group
'org-progress
2752 (const :tag
"Don't force a record" nil
)
2753 (const :tag
"Force recording the DONE state" time
)
2754 (const :tag
"Force recording a note with the DONE state" note
)))
2757 (defgroup org-priorities nil
2758 "Priorities in Org-mode."
2759 :tag
"Org Priorities"
2762 (defcustom org-enable-priority-commands t
2763 "Non-nil means priority commands are active.
2764 When nil, these commands will be disabled, so that you never accidentally
2766 :group
'org-priorities
2769 (defcustom org-highest-priority ?A
2770 "The highest priority of TODO items. A character like ?A, ?B etc.
2771 Must have a smaller ASCII number than `org-lowest-priority'."
2772 :group
'org-priorities
2775 (defcustom org-lowest-priority ?C
2776 "The lowest priority of TODO items. A character like ?A, ?B etc.
2777 Must have a larger ASCII number than `org-highest-priority'."
2778 :group
'org-priorities
2781 (defcustom org-default-priority ?B
2782 "The default priority of TODO items.
2783 This is the priority an item gets if no explicit priority is given.
2784 When starting to cycle on an empty priority the first step in the cycle
2785 depends on `org-priority-start-cycle-with-default'. The resulting first
2786 step priority must not exceed the range from `org-highest-priority' to
2787 `org-lowest-priority' which means that `org-default-priority' has to be
2788 in this range exclusive or inclusive the range boundaries. Else the
2789 first step refuses to set the default and the second will fall back
2790 to (depending on the command used) the highest or lowest priority."
2791 :group
'org-priorities
2794 (defcustom org-priority-start-cycle-with-default t
2795 "Non-nil means start with default priority when starting to cycle.
2796 When this is nil, the first step in the cycle will be (depending on the
2797 command used) one higher or lower than the default priority.
2798 See also `org-default-priority'."
2799 :group
'org-priorities
2802 (defcustom org-get-priority-function nil
2803 "Function to extract the priority from a string.
2804 The string is normally the headline. If this is nil Org computes the
2805 priority from the priority cookie like [#A] in the headline. It returns
2806 an integer, increasing by 1000 for each priority level.
2807 The user can set a different function here, which should take a string
2808 as an argument and return the numeric priority."
2809 :group
'org-priorities
2813 (defgroup org-time nil
2814 "Options concerning time stamps and deadlines in Org-mode."
2818 (defcustom org-insert-labeled-timestamps-at-point nil
2819 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2820 When nil, these labeled time stamps are forces into the second line of an
2821 entry, just after the headline. When scheduling from the global TODO list,
2822 the time stamp will always be forced into the second line."
2826 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2827 "Formats for `format-time-string' which are used for time stamps.
2828 It is not recommended to change this constant.")
2830 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2831 "Number of minutes to round time stamps to.
2832 These are two values, the first applies when first creating a time stamp.
2833 The second applies when changing it with the commands `S-up' and `S-down'.
2834 When changing the time stamp, this means that it will change in steps
2835 of N minutes, as given by the second value.
2837 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2838 numbers should be factors of 60, so for example 5, 10, 15.
2840 When this is larger than 1, you can still force an exact time stamp by using
2841 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2842 and by using a prefix arg to `S-up/down' to specify the exact number
2843 of minutes to shift."
2845 :get
#'(lambda (var) ; Make sure both elements are there
2846 (if (integerp (default-value var
))
2847 (list (default-value var
) 5)
2848 (default-value var
)))
2850 (integer :tag
"when inserting times")
2851 (integer :tag
"when modifying times")))
2853 ;; Normalize old customizations of this variable.
2854 (when (integerp org-time-stamp-rounding-minutes
)
2855 (setq org-time-stamp-rounding-minutes
2856 (list org-time-stamp-rounding-minutes
2857 org-time-stamp-rounding-minutes
)))
2859 (defcustom org-display-custom-times nil
2860 "Non-nil means overlay custom formats over all time stamps.
2861 The formats are defined through the variable `org-time-stamp-custom-formats'.
2862 To turn this on on a per-file basis, insert anywhere in the file:
2863 #+STARTUP: customtime"
2867 (make-variable-buffer-local 'org-display-custom-times
)
2869 (defcustom org-time-stamp-custom-formats
2870 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2871 "Custom formats for time stamps. See `format-time-string' for the syntax.
2872 These are overlaid over the default ISO format if the variable
2873 `org-display-custom-times' is set. Time like %H:%M should be at the
2874 end of the second format. The custom formats are also honored by export
2875 commands, if custom time display is turned on at the time of export."
2879 (defun org-time-stamp-format (&optional long inactive
)
2880 "Get the right format for a time string."
2881 (let ((f (if long
(cdr org-time-stamp-formats
)
2882 (car org-time-stamp-formats
))))
2884 (concat "[" (substring f
1 -
1) "]")
2887 (defcustom org-time-clocksum-format
2888 '(:days
"%dd " :hours
"%d" :require-hours t
:minutes
":%02d" :require-minutes t
)
2889 "The format string used when creating CLOCKSUM lines.
2890 This is also used when Org mode generates a time duration.
2892 The value can be a single format string containing two
2893 %-sequences, which will be filled with the number of hours and
2894 minutes in that order.
2896 Alternatively, the value can be a plist associating any of the
2897 keys :years, :months, :weeks, :days, :hours or :minutes with
2898 format strings. The time duration is formatted using only the
2899 time components that are needed and concatenating the results.
2900 If a time unit in absent, it falls back to the next smallest
2903 The keys :require-years, :require-months, :require-days,
2904 :require-weeks, :require-hours, :require-minutes are also
2905 meaningful. A non-nil value for these keys indicates that the
2906 corresponding time component should always be included, even if
2912 \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
2915 means durations longer than a day will be expressed in days,
2916 hours and minutes, and durations less than a day will always be
2917 expressed in hours and minutes (even for durations less than an
2922 \(:days \"%dd\" :minutes \"%dm\")
2924 means durations longer than a day will be expressed in days and
2925 minutes, and durations less than a day will be expressed entirely
2926 in minutes (even for durations longer than an hour)."
2930 :package-version
'(Org .
"8.0")
2931 :type
'(choice (string :tag
"Format string")
2933 (group :inline t
(const :tag
"Years" :years
)
2934 (string :tag
"Format string"))
2936 (const :tag
"Always show years" :require-years
)
2938 (group :inline t
(const :tag
"Months" :months
)
2939 (string :tag
"Format string"))
2941 (const :tag
"Always show months" :require-months
)
2943 (group :inline t
(const :tag
"Weeks" :weeks
)
2944 (string :tag
"Format string"))
2946 (const :tag
"Always show weeks" :require-weeks
)
2948 (group :inline t
(const :tag
"Days" :days
)
2949 (string :tag
"Format string"))
2951 (const :tag
"Always show days" :require-days
)
2953 (group :inline t
(const :tag
"Hours" :hours
)
2954 (string :tag
"Format string"))
2956 (const :tag
"Always show hours" :require-hours
)
2958 (group :inline t
(const :tag
"Minutes" :minutes
)
2959 (string :tag
"Format string"))
2961 (const :tag
"Always show minutes" :require-minutes
)
2964 (defcustom org-time-clocksum-use-fractional nil
2965 "When non-nil, \\[org-clock-display] uses fractional times.
2966 See `org-time-clocksum-format' for more on time clock formats."
2972 (defcustom org-time-clocksum-use-effort-durations nil
2973 "When non-nil, \\[org-clock-display] uses effort durations.
2974 E.g. by default, one day is considered to be a 8 hours effort,
2975 so a task that has been clocked for 16 hours will be displayed
2976 as during 2 days in the clock display or in the clocktable.
2978 See `org-effort-durations' on how to set effort durations
2979 and `org-time-clocksum-format' for more on time clock formats."
2983 :package-version
'(Org .
"8.0")
2986 (defcustom org-time-clocksum-fractional-format
"%.2f"
2987 "The format string used when creating CLOCKSUM lines,
2988 or when Org mode generates a time duration, if
2989 `org-time-clocksum-use-fractional' is enabled.
2991 The value can be a single format string containing one
2992 %-sequence, which will be filled with the number of hours as
2995 Alternatively, the value can be a plist associating any of the
2996 keys :years, :months, :weeks, :days, :hours or :minutes with
2997 a format string. The time duration is formatted using the
2998 largest time unit which gives a non-zero integer part. If all
2999 specified formats have zero integer part, the smallest time unit
3002 :type
'(choice (string :tag
"Format string")
3003 (set (group :inline t
(const :tag
"Years" :years
)
3004 (string :tag
"Format string"))
3005 (group :inline t
(const :tag
"Months" :months
)
3006 (string :tag
"Format string"))
3007 (group :inline t
(const :tag
"Weeks" :weeks
)
3008 (string :tag
"Format string"))
3009 (group :inline t
(const :tag
"Days" :days
)
3010 (string :tag
"Format string"))
3011 (group :inline t
(const :tag
"Hours" :hours
)
3012 (string :tag
"Format string"))
3013 (group :inline t
(const :tag
"Minutes" :minutes
)
3014 (string :tag
"Format string")))))
3016 (defcustom org-deadline-warning-days
14
3017 "Number of days before expiration during which a deadline becomes active.
3018 This variable governs the display in sparse trees and in the agenda.
3019 When 0 or negative, it means use this number (the absolute value of it)
3020 even if a deadline has a different individual lead time specified.
3022 Custom commands can set this variable in the options section."
3024 :group
'org-agenda-daily
/weekly
3027 (defcustom org-scheduled-delay-days
0
3028 "Number of days before a scheduled item becomes active.
3029 This variable governs the display in sparse trees and in the agenda.
3030 The default value (i.e. 0) means: don't delay scheduled item.
3031 When negative, it means use this number (the absolute value of it)
3032 even if a scheduled item has a different individual delay time
3035 Custom commands can set this variable in the options section."
3037 :group
'org-agenda-daily
/weekly
3039 :package-version
'(Org .
"8.0")
3042 (defcustom org-read-date-prefer-future t
3043 "Non-nil means assume future for incomplete date input from user.
3044 This affects the following situations:
3045 1. The user gives a month but not a year.
3046 For example, if it is April and you enter \"feb 2\", this will be read
3047 as Feb 2, *next* year. \"May 5\", however, will be this year.
3048 2. The user gives a day, but no month.
3049 For example, if today is the 15th, and you enter \"3\", Org-mode will
3050 read this as the third of *next* month. However, if you enter \"17\",
3051 it will be considered as *this* month.
3053 If you set this variable to the symbol `time', then also the following
3056 3. If the user gives a time.
3057 If the time is before now, it will be interpreted as tomorrow.
3059 Currently none of this works for ISO week specifications.
3061 When this option is nil, the current day, month and year will always be
3064 See also `org-agenda-jump-prefer-future'."
3067 (const :tag
"Never" nil
)
3068 (const :tag
"Check month and day" t
)
3069 (const :tag
"Check month, day, and time" time
)))
3071 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
3072 "Should the agenda jump command prefer the future for incomplete dates?
3073 The default is to do the same as configured in `org-read-date-prefer-future'.
3074 But you can also set a deviating value here.
3075 This may t or nil, or the symbol `org-read-date-prefer-future'."
3080 (const :tag
"Use org-read-date-prefer-future"
3081 org-read-date-prefer-future
)
3082 (const :tag
"Never" nil
)
3083 (const :tag
"Always" t
)))
3085 (defcustom org-read-date-force-compatible-dates t
3086 "Should date/time prompt force dates that are guaranteed to work in Emacs?
3088 Depending on the system Emacs is running on, certain dates cannot
3089 be represented with the type used internally to represent time.
3090 Dates between 1970-1-1 and 2038-1-1 can always be represented
3091 correctly. Some systems allow for earlier dates, some for later,
3092 some for both. One way to find out it to insert any date into an
3093 Org buffer, putting the cursor on the year and hitting S-up and
3094 S-down to test the range.
3096 When this variable is set to t, the date/time prompt will not let
3097 you specify dates outside the 1970-2037 range, so it is certain that
3098 these dates will work in whatever version of Emacs you are
3099 running, and also that you can move a file from one Emacs implementation
3100 to another. WHenever Org is forcing the year for you, it will display
3103 When this variable is nil, Org will check if the date is
3104 representable in the specific Emacs implementation you are using.
3105 If not, it will force a year, usually the current year, and beep
3106 to remind you. Currently this setting is not recommended because
3107 the likelihood that you will open your Org files in an Emacs that
3108 has limited date range is not negligible.
3110 A workaround for this problem is to use diary sexp dates for time
3111 stamps outside of this range."
3116 (defcustom org-read-date-display-live t
3117 "Non-nil means display current interpretation of date prompt live.
3118 This display will be in an overlay, in the minibuffer."
3122 (defcustom org-read-date-popup-calendar t
3123 "Non-nil means pop up a calendar when prompting for a date.
3124 In the calendar, the date can be selected with mouse-1. However, the
3125 minibuffer will also be active, and you can simply enter the date as well.
3126 When nil, only the minibuffer will be available."
3129 (org-defvaralias 'org-popup-calendar-for-date-prompt
3130 'org-read-date-popup-calendar
)
3132 (make-obsolete-variable
3133 'org-read-date-minibuffer-setup-hook
3134 "Set `org-read-date-minibuffer-local-map' instead." "24.4")
3135 (defcustom org-read-date-minibuffer-setup-hook nil
3136 "Hook to be used to set up keys for the date/time interface.
3137 Add key definitions to `minibuffer-local-map', which will be a
3140 WARNING: This option is obsolete, you should use
3141 `org-read-date-minibuffer-local-map' to set up keys."
3145 (defcustom org-extend-today-until
0
3146 "The hour when your day really ends. Must be an integer.
3147 This has influence for the following applications:
3148 - When switching the agenda to \"today\". It it is still earlier than
3149 the time given here, the day recognized as TODAY is actually yesterday.
3150 - When a date is read from the user and it is still before the time given
3151 here, the current date and time will be assumed to be yesterday, 23:59.
3152 Also, timestamps inserted in capture templates follow this rule.
3154 IMPORTANT: This is a feature whose implementation is and likely will
3155 remain incomplete. Really, it is only here because past midnight seems to
3156 be the favorite working time of John Wiegley :-)"
3160 (defcustom org-use-effective-time nil
3161 "If non-nil, consider `org-extend-today-until' when creating timestamps.
3162 For example, if `org-extend-today-until' is 8, and it's 4am, then the
3163 \"effective time\" of any timestamps between midnight and 8am will be
3164 23:59 of the previous day."
3169 (defcustom org-use-last-clock-out-time-as-effective-time nil
3170 "When non-nil, use the last clock out time for `org-todo'.
3171 Note that this option has precedence over the combined use of
3172 `org-use-effective-time' and `org-extend-today-until'."
3175 :package-version
'(Org .
"8.0")
3178 (defcustom org-edit-timestamp-down-means-later nil
3179 "Non-nil means S-down will increase the time in a time stamp.
3180 When nil, S-up will increase."
3184 (defcustom org-calendar-follow-timestamp-change t
3185 "Non-nil means make the calendar window follow timestamp changes.
3186 When a timestamp is modified and the calendar window is visible, it will be
3187 moved to the new date."
3191 (defgroup org-tags nil
3192 "Options concerning tags in Org-mode."
3196 (defcustom org-tag-alist nil
3197 "List of tags allowed in Org-mode files.
3198 When this list is nil, Org-mode will base TAG input on what is already in the
3200 The value of this variable is an alist, the car of each entry must be a
3201 keyword as a string, the cdr may be a character that is used to select
3202 that tag through the fast-tag-selection interface.
3203 See the manual for details."
3207 (cons (string :tag
"Tag name")
3208 (character :tag
"Access char"))
3209 (list :tag
"Start radio group"
3211 (option (string :tag
"Group description")))
3212 (list :tag
"Group tags delimiter"
3214 (list :tag
"End radio group"
3216 (option (string :tag
"Group description")))
3217 (const :tag
"New line" (:newline
)))))
3219 (defcustom org-tag-persistent-alist nil
3220 "List of tags that will always appear in all Org-mode files.
3221 This is in addition to any in buffer settings or customizations
3223 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
3224 The value of this variable is an alist, the car of each entry must be a
3225 keyword as a string, the cdr may be a character that is used to select
3226 that tag through the fast-tag-selection interface.
3227 See the manual for details.
3228 To disable these tags on a per-file basis, insert anywhere in the file:
3233 (cons (string :tag
"Tag name")
3234 (character :tag
"Access char"))
3235 (const :tag
"Start radio group" (:startgroup
))
3236 (const :tag
"Group tags delimiter" (:grouptags
))
3237 (const :tag
"End radio group" (:endgroup
))
3238 (const :tag
"New line" (:newline
)))))
3240 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
3241 "If non-nil, always offer completion for all tags of all agenda files.
3242 Instead of customizing this variable directly, you might want to
3243 set it locally for capture buffers, because there no list of
3244 tags in that file can be created dynamically (there are none).
3246 (add-hook 'org-capture-mode-hook
3248 (set (make-local-variable
3249 'org-complete-tags-always-offer-all-agenda-tags)
3255 (defvar org-file-tags nil
3256 "List of tags that can be inherited by all entries in the file.
3257 The tags will be inherited if the variable `org-use-tag-inheritance'
3258 says they should be.
3259 This variable is populated from #+FILETAGS lines.")
3261 (defcustom org-use-fast-tag-selection
'auto
3262 "Non-nil means use fast tag selection scheme.
3263 This is a special interface to select and deselect tags with single keys.
3264 When nil, fast selection is never used.
3265 When the symbol `auto', fast selection is used if and only if selection
3266 characters for tags have been configured, either through the variable
3267 `org-tag-alist' or through a #+TAGS line in the buffer.
3268 When t, fast selection is always used and selection keys are assigned
3269 automatically if necessary."
3272 (const :tag
"Always" t
)
3273 (const :tag
"Never" nil
)
3274 (const :tag
"When selection characters are configured" 'auto
)))
3276 (defcustom org-fast-tag-selection-single-key nil
3277 "Non-nil means fast tag selection exits after first change.
3278 When nil, you have to press RET to exit it.
3279 During fast tag selection, you can toggle this flag with `C-c'.
3280 This variable can also have the value `expert'. In this case, the window
3281 displaying the tags menu is not even shown, until you press C-c again."
3284 (const :tag
"No" nil
)
3285 (const :tag
"Yes" t
)
3286 (const :tag
"Expert" expert
)))
3288 (defvar org-fast-tag-selection-include-todo nil
3289 "Non-nil means fast tags selection interface will also offer TODO states.
3290 This is an undocumented feature, you should not rely on it.")
3292 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
3293 "The column to which tags should be indented in a headline.
3294 If this number is positive, it specifies the column. If it is negative,
3295 it means that the tags should be flushright to that column. For example,
3296 -80 works well for a normal 80 character screen.
3297 When 0, place tags directly after headline text, with only one space in
3302 (defcustom org-auto-align-tags t
3303 "Non-nil keeps tags aligned when modifying headlines.
3304 Some operations (i.e. demoting) change the length of a headline and
3305 therefore shift the tags around. With this option turned on, after
3306 each such operation the tags are again aligned to `org-tags-column'."
3310 (defcustom org-use-tag-inheritance t
3311 "Non-nil means tags in levels apply also for sublevels.
3312 When nil, only the tags directly given in a specific line apply there.
3313 This may also be a list of tags that should be inherited, or a regexp that
3314 matches tags that should be inherited. Additional control is possible
3315 with the variable `org-tags-exclude-from-inheritance' which gives an
3316 explicit list of tags to be excluded from inheritance, even if the value of
3317 `org-use-tag-inheritance' would select it for inheritance.
3319 If this option is t, a match early-on in a tree can lead to a large
3320 number of matches in the subtree when constructing the agenda or creating
3321 a sparse tree. If you only want to see the first match in a tree during
3322 a search, check out the variable `org-tags-match-list-sublevels'."
3325 (const :tag
"Not" nil
)
3326 (const :tag
"Always" t
)
3327 (repeat :tag
"Specific tags" (string :tag
"Tag"))
3328 (regexp :tag
"Tags matched by regexp")))
3330 (defcustom org-tags-exclude-from-inheritance nil
3331 "List of tags that should never be inherited.
3332 This is a way to exclude a few tags from inheritance. For way to do
3333 the opposite, to actively allow inheritance for selected tags,
3334 see the variable `org-use-tag-inheritance'."
3336 :type
'(repeat (string :tag
"Tag")))
3338 (defun org-tag-inherit-p (tag)
3339 "Check if TAG is one that should be inherited."
3341 ((member tag org-tags-exclude-from-inheritance
) nil
)
3342 ((eq org-use-tag-inheritance t
) t
)
3343 ((not org-use-tag-inheritance
) nil
)
3344 ((stringp org-use-tag-inheritance
)
3345 (string-match org-use-tag-inheritance tag
))
3346 ((listp org-use-tag-inheritance
)
3347 (member tag org-use-tag-inheritance
))
3348 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3350 (defcustom org-tags-match-list-sublevels t
3351 "Non-nil means list also sublevels of headlines matching a search.
3352 This variable applies to tags/property searches, and also to stuck
3353 projects because this search is based on a tags match as well.
3355 When set to the symbol `indented', sublevels are indented with
3358 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3359 the sublevels of a headline matching a tag search often also match
3360 the same search. Listing all of them can create very long lists.
3361 Setting this variable to nil causes subtrees of a match to be skipped.
3363 This variable is semi-obsolete and probably should always be true. It
3364 is better to limit inheritance to certain tags using the variables
3365 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3368 (const :tag
"No, don't list them" nil
)
3369 (const :tag
"Yes, do list them" t
)
3370 (const :tag
"List them, indented with leading dots" indented
)))
3372 (defcustom org-tags-sort-function nil
3373 "When set, tags are sorted using this function as a comparator."
3376 (const :tag
"No sorting" nil
)
3377 (const :tag
"Alphabetical" string
<)
3378 (const :tag
"Reverse alphabetical" string
>)
3379 (function :tag
"Custom function" nil
)))
3381 (defvar org-tags-history nil
3382 "History of minibuffer reads for tags.")
3383 (defvar org-last-tags-completion-table nil
3384 "The last used completion table for tags.")
3385 (defvar org-after-tags-change-hook nil
3386 "Hook that is run after the tags in a line have changed.")
3388 (defgroup org-properties nil
3389 "Options concerning properties in Org-mode."
3390 :tag
"Org Properties"
3393 (defcustom org-property-format
"%-10s %s"
3394 "How property key/value pairs should be formatted by `indent-line'.
3395 When `indent-line' hits a property definition, it will format the line
3396 according to this format, mainly to make sure that the values are
3397 lined-up with respect to each other."
3398 :group
'org-properties
3401 (defcustom org-properties-postprocess-alist nil
3402 "Alist of properties and functions to adjust inserted values.
3403 Elements of this alist must be of the form
3405 ([string] [function])
3407 where [string] must be a property name and [function] must be a
3408 lambda expression: this lambda expression must take one argument,
3409 the value to adjust, and return the new value as a string.
3411 For example, this element will allow the property \"Remaining\"
3412 to be updated wrt the relation between the \"Effort\" property
3413 and the clock summary:
3415 ((\"Remaining\" (lambda(value)
3416 (let ((clocksum (org-clock-sum-current-item))
3417 (effort (org-duration-string-to-minutes
3418 (org-entry-get (point) \"Effort\"))))
3419 (org-minutes-to-clocksum-string (- effort clocksum))))))"
3420 :group
'org-properties
3422 :type
'(alist :key-type
(string :tag
"Property")
3423 :value-type
(function :tag
"Function")))
3425 (defcustom org-use-property-inheritance nil
3426 "Non-nil means properties apply also for sublevels.
3428 This setting is chiefly used during property searches. Turning it on can
3429 cause significant overhead when doing a search, which is why it is not
3432 When nil, only the properties directly given in the current entry count.
3433 When t, every property is inherited. The value may also be a list of
3434 properties that should have inheritance, or a regular expression matching
3435 properties that should be inherited.
3437 However, note that some special properties use inheritance under special
3438 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3439 and the properties ending in \"_ALL\" when they are used as descriptor
3440 for valid values of a property.
3442 Note for programmers:
3443 When querying an entry with `org-entry-get', you can control if inheritance
3444 should be used. By default, `org-entry-get' looks only at the local
3445 properties. You can request inheritance by setting the inherit argument
3446 to t (to force inheritance) or to `selective' (to respect the setting
3448 :group
'org-properties
3450 (const :tag
"Not" nil
)
3451 (const :tag
"Always" t
)
3452 (repeat :tag
"Specific properties" (string :tag
"Property"))
3453 (regexp :tag
"Properties matched by regexp")))
3455 (defun org-property-inherit-p (property)
3456 "Check if PROPERTY is one that should be inherited."
3458 ((eq org-use-property-inheritance t
) t
)
3459 ((not org-use-property-inheritance
) nil
)
3460 ((stringp org-use-property-inheritance
)
3461 (string-match org-use-property-inheritance property
))
3462 ((listp org-use-property-inheritance
)
3463 (member property org-use-property-inheritance
))
3464 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3466 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3467 "The default column format, if no other format has been defined.
3468 This variable can be set on the per-file basis by inserting a line
3470 #+COLUMNS: %25ITEM ....."
3471 :group
'org-properties
3474 (defcustom org-columns-ellipses
".."
3475 "The ellipses to be used when a field in column view is truncated.
3476 When this is the empty string, as many characters as possible are shown,
3477 but then there will be no visual indication that the field has been truncated.
3478 When this is a string of length N, the last N characters of a truncated
3479 field are replaced by this string. If the column is narrower than the
3480 ellipses string, only part of the ellipses string will be shown."
3481 :group
'org-properties
3484 (defcustom org-columns-modify-value-for-display-function nil
3485 "Function that modifies values for display in column view.
3486 For example, it can be used to cut out a certain part from a time stamp.
3487 The function must take 2 arguments:
3489 column-title The title of the column (*not* the property name)
3490 value The value that should be modified.
3492 The function should return the value that should be displayed,
3493 or nil if the normal value should be used."
3494 :group
'org-properties
3497 (defcustom org-effort-property
"Effort"
3498 "The property that is being used to keep track of effort estimates.
3499 Effort estimates given in this property need to have the format H:MM."
3500 :group
'org-properties
3501 :group
'org-progress
3502 :type
'(string :tag
"Property"))
3504 (defconst org-global-properties-fixed
3505 '(("VISIBILITY_ALL" .
"folded children content all")
3506 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3507 "List of property/value pairs that can be inherited by any entry.
3509 These are fixed values, for the preset properties. The user variable
3510 that can be used to add to this list is `org-global-properties'.
3512 The entries in this list are cons cells where the car is a property
3513 name and cdr is a string with the value. If the value represents
3514 multiple items like an \"_ALL\" property, separate the items by
3517 (defcustom org-global-properties nil
3518 "List of property/value pairs that can be inherited by any entry.
3520 This list will be combined with the constant `org-global-properties-fixed'.
3522 The entries in this list are cons cells where the car is a property
3523 name and cdr is a string with the value.
3525 You can set buffer-local values for the same purpose in the variable
3526 `org-file-properties' this by adding lines like
3528 #+PROPERTY: NAME VALUE"
3529 :group
'org-properties
3531 (cons (string :tag
"Property")
3532 (string :tag
"Value"))))
3534 (defvar org-file-properties nil
3535 "List of property/value pairs that can be inherited by any entry.
3536 Valid for the current buffer.
3537 This variable is populated from #+PROPERTY lines.")
3538 (make-variable-buffer-local 'org-file-properties
)
3540 (defgroup org-agenda nil
3541 "Options concerning agenda views in Org-mode."
3545 (defvar org-category nil
3546 "Variable used by org files to set a category for agenda display.
3547 Such files should use a file variable to set it, for example
3549 # -*- mode: org; org-category: \"ELisp\"
3551 or contain a special line
3555 If the file does not specify a category, then file's base name
3557 (make-variable-buffer-local 'org-category
)
3558 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3560 (defcustom org-agenda-files nil
3561 "The files to be used for agenda display.
3562 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3563 \\[org-remove-file]. You can also use customize to edit the list.
3565 If an entry is a directory, all files in that directory that are matched by
3566 `org-agenda-file-regexp' will be part of the file list.
3568 If the value of the variable is not a list but a single file name, then
3569 the list of agenda files is actually stored and maintained in that file, one
3570 agenda file per line. In this file paths can be given relative to
3571 `org-directory'. Tilde expansion and environment variable substitution
3575 (repeat :tag
"List of files and directories" file
)
3576 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3578 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3579 "Regular expression to match files for `org-agenda-files'.
3580 If any element in the list in that variable contains a directory instead
3581 of a normal file, all files in that directory that are matched by this
3582 regular expression will be included."
3586 (defcustom org-agenda-text-search-extra-files nil
3587 "List of extra files to be searched by text search commands.
3588 These files will be searched in addition to the agenda files by the
3589 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3590 Note that these files will only be searched for text search commands,
3591 not for the other agenda views like todo lists, tag searches or the weekly
3592 agenda. This variable is intended to list notes and possibly archive files
3593 that should also be searched by these two commands.
3594 In fact, if the first element in the list is the symbol `agenda-archives',
3595 then all archive files of all agenda files will be added to the search
3598 :type
'(set :greedy t
3599 (const :tag
"Agenda Archives" agenda-archives
)
3600 (repeat :inline t
(file))))
3602 (org-defvaralias 'org-agenda-multi-occur-extra-files
3603 'org-agenda-text-search-extra-files
)
3605 (defcustom org-agenda-skip-unavailable-files nil
3606 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3607 A nil value means to remove them, after a query, from the list."
3611 (defcustom org-calendar-to-agenda-key
[?c
]
3612 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3613 The command `org-calendar-goto-agenda' will be bound to this key. The
3614 default is the character `c' because then `c' can be used to switch back and
3615 forth between agenda and calendar."
3619 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3620 "The key to be installed in `calendar-mode-map' for adding diary entries.
3621 This option is irrelevant until `org-agenda-diary-file' has been configured
3622 to point to an Org-mode file. When that is the case, the command
3623 `org-agenda-diary-entry' will be bound to the key given here, by default
3624 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3625 if you want to continue doing this, you need to change this to a different
3630 (defcustom org-agenda-diary-file
'diary-file
3631 "File to which to add new entries with the `i' key in agenda and calendar.
3632 When this is the symbol `diary-file', the functionality in the Emacs
3633 calendar will be used to add entries to the `diary-file'. But when this
3634 points to a file, `org-agenda-diary-entry' will be used instead."
3637 (const :tag
"The standard Emacs diary file" diary-file
)
3638 (file :tag
"Special Org file diary entries")))
3640 (eval-after-load "calendar"
3642 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3643 'org-calendar-goto-agenda
)
3644 (add-hook 'calendar-mode-hook
3646 (unless (eq org-agenda-diary-file
'diary-file
)
3647 (define-key calendar-mode-map
3648 org-calendar-insert-diary-entry-key
3649 'org-agenda-diary-entry
))))))
3651 (defgroup org-latex nil
3652 "Options for embedding LaTeX code into Org-mode."
3656 (defcustom org-format-latex-options
3657 '(:foreground default
:background default
:scale
1.0
3658 :html-foreground
"Black" :html-background
"Transparent"
3659 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3660 "Options for creating images from LaTeX fragments.
3661 This is a property list with the following properties:
3662 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3663 `default' means use the foreground of the default face.
3664 `auto' means use the foreground from the text face.
3665 :background the background color, or \"Transparent\".
3666 `default' means use the background of the default face.
3667 `auto' means use the background from the text face.
3668 :scale a scaling factor for the size of the images, to get more pixels
3669 :html-foreground, :html-background, :html-scale
3670 the same numbers for HTML export.
3671 :matchers a list indicating which matchers should be used to
3672 find LaTeX fragments. Valid members of this list are:
3673 \"begin\" find environments
3674 \"$1\" find single characters surrounded by $.$
3675 \"$\" find math expressions surrounded by $...$
3676 \"$$\" find math expressions surrounded by $$....$$
3677 \"\\(\" find math expressions surrounded by \\(...\\)
3678 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3682 (defcustom org-format-latex-signal-error t
3683 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3684 When nil, just push out a message."
3689 (defcustom org-latex-to-mathml-jar-file nil
3690 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3691 Use this to specify additional executable file say a jar file.
3693 When using MathToWeb as the converter, specify the full-path to
3694 your mathtoweb.jar file."
3698 (const :tag
"None" nil
)
3699 (file :tag
"JAR file" :must-match t
)))
3701 (defcustom org-latex-to-mathml-convert-command nil
3702 "Command to convert LaTeX fragments to MathML.
3703 Replace format-specifiers in the command as noted below and use
3704 `shell-command' to convert LaTeX to MathML.
3705 %j: Executable file in fully expanded form as specified by
3706 `org-latex-to-mathml-jar-file'.
3707 %I: Input LaTeX file in fully expanded form
3708 %o: Output MathML file
3709 This command is used by `org-create-math-formula'.
3711 When using MathToWeb as the converter, set this to
3712 \"java -jar %j -unicode -force -df %o %I\"."
3716 (const :tag
"None" nil
)
3717 (string :tag
"\nShell command")))
3719 (defcustom org-latex-create-formula-image-program
'dvipng
3720 "Program to convert LaTeX fragments with.
3722 dvipng Process the LaTeX fragments to dvi file, then convert
3723 dvi files to png files using dvipng.
3724 This will also include processing of non-math environments.
3725 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3726 to convert pdf files to png files"
3730 (const :tag
"dvipng" dvipng
)
3731 (const :tag
"imagemagick" imagemagick
)))
3733 (defcustom org-latex-preview-ltxpng-directory
"ltxpng/"
3734 "Path to store latex preview images.
3735 A relative path here creates many directories relative to the
3736 processed org files paths. An absolute path puts all preview
3737 images at the same place."
3742 (defun org-format-latex-mathml-available-p ()
3743 "Return t if `org-latex-to-mathml-convert-command' is usable."
3745 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3746 org-latex-to-mathml-convert-command
)
3747 (let ((executable (car (split-string
3748 org-latex-to-mathml-convert-command
))))
3749 (when (executable-find executable
)
3751 "%j" org-latex-to-mathml-convert-command
)
3752 (file-readable-p org-latex-to-mathml-jar-file
)
3755 (defcustom org-format-latex-header
"\\documentclass{article}
3756 \\usepackage[usenames]{color}
3757 \\usepackage{amsmath}
3758 \\usepackage[mathscr]{eucal}
3759 \\pagestyle{empty} % do not remove
3762 % The settings below are copied from fullpage.sty
3763 \\setlength{\\textwidth}{\\paperwidth}
3764 \\addtolength{\\textwidth}{-3cm}
3765 \\setlength{\\oddsidemargin}{1.5cm}
3766 \\addtolength{\\oddsidemargin}{-2.54cm}
3767 \\setlength{\\evensidemargin}{\\oddsidemargin}
3768 \\setlength{\\textheight}{\\paperheight}
3769 \\addtolength{\\textheight}{-\\headheight}
3770 \\addtolength{\\textheight}{-\\headsep}
3771 \\addtolength{\\textheight}{-\\footskip}
3772 \\addtolength{\\textheight}{-3cm}
3773 \\setlength{\\topmargin}{1.5cm}
3774 \\addtolength{\\topmargin}{-2.54cm}"
3775 "The document header used for processing LaTeX fragments.
3776 It is imperative that this header make sure that no page number
3777 appears on the page. The package defined in the variables
3778 `org-latex-default-packages-alist' and `org-latex-packages-alist'
3779 will either replace the placeholder \"[PACKAGES]\" in this
3780 header, or they will be appended."
3784 (defun org-set-packages-alist (var val
)
3785 "Set the packages alist and make sure it has 3 elements per entry."
3786 (set var
(mapcar (lambda (x)
3787 (if (and (consp x
) (= (length x
) 2))
3788 (list (car x
) (nth 1 x
) t
)
3792 (defun org-get-packages-alist (var)
3793 "Get the packages alist and make sure it has 3 elements per entry."
3795 (if (and (consp x
) (= (length x
) 2))
3796 (list (car x
) (nth 1 x
) t
)
3798 (default-value var
)))
3800 (defcustom org-latex-default-packages-alist
3801 '(("AUTO" "inputenc" t
)
3805 ("" "longtable" nil
)
3817 "Alist of default packages to be inserted in the header.
3819 Change this only if one of the packages here causes an
3820 incompatibility with another package you are using.
3822 The packages in this list are needed by one part or another of
3823 Org mode to function properly:
3825 - inputenc, fontenc: for basic font and character selection
3826 - amstext: for subscript and superscript
3827 - textcomp, marvosymb, wasysym, latexsym, amssym: for various
3828 symbols used for interpreting the entities in `org-entities'.
3829 You can skip some of these packages if you don't use any of the
3831 - soul: for underline and strike-through
3832 - graphicx: for including images
3833 - float, wrapfig: for figure placement
3834 - longtable: for long tables
3835 - hyperref: for cross references
3837 Therefore you should not modify this variable unless you know
3838 what you are doing. The one reason to change it anyway is that
3839 you might be loading some other package that conflicts with one
3840 of the default packages. Each cell is of the format
3841 \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
3842 the package also needs to be included when compiling LaTeX
3843 snippets into images for inclusion into non-LaTeX output."
3845 :group
'org-export-latex
3846 :set
'org-set-packages-alist
3847 :get
'org-get-packages-alist
3851 (list :tag
"options/package pair"
3852 (string :tag
"options")
3853 (string :tag
"package")
3854 (boolean :tag
"Snippet"))
3855 (string :tag
"A line of LaTeX"))))
3857 (defcustom org-latex-packages-alist nil
3858 "Alist of packages to be inserted in every LaTeX header.
3860 These will be inserted after `org-latex-default-packages-alist'.
3861 Each cell is of the format:
3863 \(\"options\" \"package\" snippet-flag)
3865 SNIPPET-FLAG, when t, indicates that this package is also needed
3866 when turning LaTeX snippets into images for inclusion into
3869 Make sure that you only list packages here which:
3871 - you want in every file
3872 - do not conflict with the setup in `org-format-latex-header'.
3873 - do not conflict with the default packages in
3874 `org-latex-default-packages-alist'."
3876 :group
'org-export-latex
3877 :set
'org-set-packages-alist
3878 :get
'org-get-packages-alist
3881 (list :tag
"options/package pair"
3882 (string :tag
"options")
3883 (string :tag
"package")
3884 (boolean :tag
"Snippet"))
3885 (string :tag
"A line of LaTeX"))))
3887 (defgroup org-appearance nil
3888 "Settings for Org-mode appearance."
3889 :tag
"Org Appearance"
3892 (defcustom org-level-color-stars-only nil
3893 "Non-nil means fontify only the stars in each headline.
3894 When nil, the entire headline is fontified.
3895 Changing it requires restart of `font-lock-mode' to become effective
3896 also in regions already fontified."
3897 :group
'org-appearance
3900 (defcustom org-hide-leading-stars nil
3901 "Non-nil means hide the first N-1 stars in a headline.
3902 This works by using the face `org-hide' for these stars. This
3903 face is white for a light background, and black for a dark
3904 background. You may have to customize the face `org-hide' to
3906 Changing it requires restart of `font-lock-mode' to become effective
3907 also in regions already fontified.
3908 You may also set this on a per-file basis by adding one of the following
3909 lines to the buffer:
3911 #+STARTUP: hidestars
3912 #+STARTUP: showstars"
3913 :group
'org-appearance
3916 (defcustom org-hidden-keywords nil
3917 "List of symbols corresponding to keywords to be hidden the org buffer.
3918 For example, a value '(title) for this list will make the document's title
3919 appear in the buffer without the initial #+TITLE: keyword."
3920 :group
'org-appearance
3922 :type
'(set (const :tag
"#+AUTHOR" author
)
3923 (const :tag
"#+DATE" date
)
3924 (const :tag
"#+EMAIL" email
)
3925 (const :tag
"#+TITLE" title
)))
3927 (defcustom org-custom-properties nil
3928 "List of properties (as strings) with a special meaning.
3929 The default use of these custom properties is to let the user
3930 hide them with `org-toggle-custom-properties-visibility'."
3931 :group
'org-properties
3932 :group
'org-appearance
3934 :type
'(repeat (string :tag
"Property Name")))
3936 (defcustom org-fontify-done-headline nil
3937 "Non-nil means change the face of a headline if it is marked DONE.
3938 Normally, only the TODO/DONE keyword indicates the state of a headline.
3939 When this is non-nil, the headline after the keyword is set to the
3940 `org-headline-done' as an additional indication."
3941 :group
'org-appearance
3944 (defcustom org-fontify-emphasized-text t
3945 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3946 Changing this variable requires a restart of Emacs to take effect."
3947 :group
'org-appearance
3950 (defcustom org-fontify-whole-heading-line nil
3951 "Non-nil means fontify the whole line for headings.
3952 This is useful when setting a background color for the
3954 :group
'org-appearance
3957 (defcustom org-highlight-latex-and-related nil
3958 "Non-nil means highlight LaTeX related syntax in the buffer.
3959 When non nil, the value should be a list containing any of the
3961 `latex' Highlight LaTeX snippets and environments.
3962 `script' Highlight subscript and superscript.
3963 `entities' Highlight entities."
3964 :group
'org-appearance
3966 :package-version
'(Org .
"8.0")
3968 (const :tag
"No highlighting" nil
)
3969 (set :greedy t
:tag
"Highlight"
3970 (const :tag
"LaTeX snippets and environments" latex
)
3971 (const :tag
"Subscript and superscript" script
)
3972 (const :tag
"Entities" entities
))))
3974 (defcustom org-hide-emphasis-markers nil
3975 "Non-nil mean font-lock should hide the emphasis marker characters."
3976 :group
'org-appearance
3979 (defcustom org-pretty-entities nil
3980 "Non-nil means show entities as UTF8 characters.
3981 When nil, the \\name form remains in the buffer."
3982 :group
'org-appearance
3986 (defcustom org-pretty-entities-include-sub-superscripts t
3987 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3988 :group
'org-appearance
3992 (defvar org-emph-re nil
3993 "Regular expression for matching emphasis.
3994 After a match, the match groups contain these elements:
3995 0 The match of the full regular expression, including the characters
3996 before and after the proper match
3997 1 The character before the proper match, or empty at beginning of line
3998 2 The proper match, including the leading and trailing markers
3999 3 The leading marker like * or /, indicating the type of highlighting
4000 4 The text between the emphasis markers, not including the markers
4001 5 The character after the match, empty at the end of a line")
4002 (defvar org-verbatim-re nil
4003 "Regular expression for matching verbatim text.")
4004 (defvar org-emphasis-regexp-components
) ; defined just below
4005 (defvar org-emphasis-alist
) ; defined just below
4006 (defun org-set-emph-re (var val
)
4007 "Set variable and compute the emphasis regular expression."
4009 (when (and (boundp 'org-emphasis-alist
)
4010 (boundp 'org-emphasis-regexp-components
)
4011 org-emphasis-alist org-emphasis-regexp-components
)
4012 (let* ((e org-emphasis-regexp-components
)
4018 (body1 (concat body
"*?"))
4019 (markers (mapconcat 'car org-emphasis-alist
""))
4020 (vmarkers (mapconcat
4021 (lambda (x) (if (eq (nth 2 x
) 'verbatim
) (car x
) ""))
4022 org-emphasis-alist
"")))
4023 ;; make sure special characters appear at the right position in the class
4024 (if (string-match "\\^" markers
)
4025 (setq markers
(concat (replace-match "" t t markers
) "^")))
4026 (if (string-match "-" markers
)
4027 (setq markers
(concat (replace-match "" t t markers
) "-")))
4028 (if (string-match "\\^" vmarkers
)
4029 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
4030 (if (string-match "-" vmarkers
)
4031 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
4033 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
4034 (int-to-string nl
) "\\}")))
4037 (concat "\\([" pre
"]\\|^\\)"
4039 "\\([" markers
"]\\)"
4047 "\\([" post
"]\\|$\\)"))
4048 (setq org-verbatim-re
4049 (concat "\\([" pre
"]\\|^\\)"
4051 "\\([" vmarkers
"]\\)"
4059 "\\([" post
"]\\|$\\)")))))
4061 ;; This used to be a defcustom (Org <8.0) but allowing the users to
4062 ;; set this option proved cumbersome. See this message/thread:
4063 ;; http://article.gmane.org/gmane.emacs.orgmode/68681
4064 (defvar org-emphasis-regexp-components
4065 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
4066 "Components used to build the regular expression for emphasis.
4067 This is a list with five entries. Terminology: In an emphasis string
4068 like \" *strong word* \", we call the initial space PREMATCH, the final
4069 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
4070 and \"trong wor\" is the body. The different components in this variable
4071 specify what is allowed/forbidden in each part:
4073 pre Chars allowed as prematch. Beginning of line will be allowed too.
4074 post Chars allowed as postmatch. End of line will be allowed too.
4075 border The chars *forbidden* as border characters.
4076 body-regexp A regexp like \".\" to match a body character. Don't use
4077 non-shy groups here, and don't allow newline here.
4078 newline The maximum number of newlines allowed in an emphasis exp.
4080 You need to reload Org or to restart Emacs after customizing this.")
4082 (defcustom org-emphasis-alist
4086 ("=" org-code verbatim
)
4087 ("~" org-verbatim verbatim
)
4088 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))))
4089 "Alist of characters and faces to emphasize text.
4090 Text starting and ending with a special character will be emphasized,
4091 for example *bold*, _underlined_ and /italic/. This variable sets the
4092 marker characters and the face to be used by font-lock for highlighting
4093 in Org-mode Emacs buffers.
4095 You need to reload Org or to restart Emacs after customizing this."
4096 :group
'org-appearance
4097 :set
'org-set-emph-re
4099 :package-version
'(Org .
"8.0")
4102 (string :tag
"Marker character")
4104 (face :tag
"Font-lock-face")
4105 (plist :tag
"Face property list"))
4106 (option (const verbatim
)))))
4108 (defvar org-protecting-blocks
4109 '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
4110 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
4111 This is needed for font-lock setup.")
4113 ;;; Miscellaneous options
4115 (defgroup org-completion nil
4116 "Completion in Org-mode."
4117 :tag
"Org Completion"
4120 (defcustom org-completion-use-ido nil
4121 "Non-nil means use ido completion wherever possible.
4122 Note that `ido-mode' must be active for this variable to be relevant.
4123 If you decide to turn this variable on, you might well want to turn off
4124 `org-outline-path-complete-in-steps'.
4125 See also `org-completion-use-iswitchb'."
4126 :group
'org-completion
4129 (defcustom org-completion-use-iswitchb nil
4130 "Non-nil means use iswitchb completion wherever possible.
4131 Note that `iswitchb-mode' must be active for this variable to be relevant.
4132 If you decide to turn this variable on, you might well want to turn off
4133 `org-outline-path-complete-in-steps'.
4134 Note that this variable has only an effect if `org-completion-use-ido' is nil."
4135 :group
'org-completion
4138 (defcustom org-completion-fallback-command
'hippie-expand
4139 "The expansion command called by \\[pcomplete] in normal context.
4140 Normal means, no org-mode-specific context."
4141 :group
'org-completion
4144 ;;; Functions and variables from their packages
4145 ;; Declared here to avoid compiler warnings
4148 (defvar outline-mode-menu-heading
)
4149 (defvar outline-mode-menu-show
)
4150 (defvar outline-mode-menu-hide
)
4151 (defvar zmacs-regions
) ; XEmacs regions
4154 (defvar mark-active
)
4157 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
4158 (declare-function calendar-forward-day
"cal-move" (arg))
4159 (declare-function calendar-goto-date
"cal-move" (date))
4160 (declare-function calendar-goto-today
"cal-move" ())
4161 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
4162 (defvar calc-embedded-close-formula
)
4163 (defvar calc-embedded-open-formula
)
4164 (declare-function cdlatex-tab
"ext:cdlatex" ())
4165 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
4166 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
4167 (defvar font-lock-unfontify-region-function
)
4168 (declare-function iswitchb-read-buffer
"iswitchb"
4169 (prompt &optional default require-match start matches-set
))
4170 (defvar iswitchb-temp-buflist
)
4171 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
4172 (defvar org-agenda-tags-todo-honor-ignore-options
)
4173 (declare-function org-agenda-skip
"org-agenda" ())
4175 org-agenda-format-item
"org-agenda"
4176 (extra txt
&optional level category tags dotime noprefix remove-re habitp
))
4177 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
4178 (declare-function org-agenda-change-all-lines
"org-agenda"
4179 (newhead hdmarker
&optional fixface just-this
))
4180 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
4181 (declare-function org-agenda-maybe-redo
"org-agenda" ())
4182 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
4184 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
4185 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4186 "org-agenda" (&optional end
))
4187 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
4188 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
4189 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
4190 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
4191 (declare-function org-indent-mode
"org-indent" (&optional arg
))
4192 (declare-function parse-time-string
"parse-time" (string))
4193 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
4194 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
4195 (defvar remember-data-file
)
4196 (defvar texmathp-why
)
4197 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
4198 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
4200 (defvar org-latex-regexps
)
4202 ;;; Autoload and prepare some org modules
4204 ;; Some table stuff that needs to be defined here, because it is used
4205 ;; by the functions setting up org-mode or checking for table context.
4207 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
4208 "Detect an org-type or table-type table.")
4209 (defconst org-table-line-regexp
"^[ \t]*|"
4210 "Detect an org-type table line.")
4211 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
4212 "Detect an org-type table line.")
4213 (defconst org-table-hline-regexp
"^[ \t]*|-"
4214 "Detect an org-type table hline.")
4215 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
4216 "Detect a table-type table hline.")
4217 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
4218 "Detect the first line outside a table when searching from within it.
4219 This works for both table types.")
4221 ;; Autoload the functions in org-table.el that are needed by functions here.
4224 (org-autoload "org-table"
4225 '(org-table-begin org-table-blank-field org-table-end
)))
4227 (defconst org-TBLFM-regexp
"^[ \t]*#\\+TBLFM: "
4228 "Detect a #+TBLFM line.")
4231 (defun turn-on-orgtbl ()
4232 "Unconditionally turn on `orgtbl-mode'."
4233 (require 'org-table
)
4236 (defun org-at-table-p (&optional table-type
)
4237 "Return t if the cursor is inside an org-type table.
4238 If TABLE-TYPE is non-nil, also check for table.el-type tables."
4239 (if org-enable-table-editor
4241 (beginning-of-line 1)
4242 (looking-at (if table-type org-table-any-line-regexp
4243 org-table-line-regexp
)))
4245 (defsubst org-table-p
() (org-at-table-p))
4247 (defun org-at-table.el-p
()
4248 "Return t if and only if we are at a table.el table."
4249 (and (org-at-table-p 'any
)
4251 (goto-char (org-table-begin 'any
))
4252 (looking-at org-table1-hline-regexp
))))
4254 (defun org-table-recognize-table.el
()
4255 "If there is a table.el table nearby, recognize it and move into it."
4256 (if org-table-tab-recognizes-table.el
4257 (if (org-at-table.el-p
)
4259 (beginning-of-line 1)
4260 (if (looking-at org-table-dataline-regexp
)
4262 (if (looking-at org-table1-hline-regexp
)
4264 (beginning-of-line 2)
4265 (if (looking-at org-table-any-border-regexp
)
4266 (beginning-of-line -
1)))))
4267 (if (re-search-forward "|" (org-table-end t
) t
)
4270 (if (table--at-cell-p (point))
4272 (message "recognizing table.el table...")
4273 (table-recognize-table)
4274 (message "recognizing table.el table...done")))
4275 (error "This should not happen"))
4280 (defun org-at-table-hline-p ()
4281 "Return t if the cursor is inside a hline in a table."
4282 (if org-enable-table-editor
4284 (beginning-of-line 1)
4285 (looking-at org-table-hline-regexp
))
4288 (defvar org-table-clean-did-remove-column nil
)
4289 (defun org-table-map-tables (function &optional quietly
)
4290 "Apply FUNCTION to the start of all tables in the buffer."
4294 (goto-char (point-min))
4295 (while (re-search-forward org-table-any-line-regexp nil t
)
4297 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
4298 (beginning-of-line 1)
4299 (when (and (looking-at org-table-line-regexp
)
4300 ;; Exclude tables in src/example/verbatim/clocktable blocks
4301 (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
4302 (save-excursion (funcall function
))
4303 (or (looking-at org-table-line-regexp
)
4305 (re-search-forward org-table-any-border-regexp nil
1))))
4306 (unless quietly
(message "Mapping tables: done")))
4308 ;; Declare and autoload functions from ox.el and al.
4310 (declare-function org-export-get-environment
"ox"
4311 (&optional backend subtreep ext-plist
))
4312 (declare-function org-latex-guess-inputenc
"ox-latex" (header))
4314 ;; Declare and autoload functions from org-agenda.el
4317 (org-autoload "org-agenda"
4318 '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
4320 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock" (beg end
))
4321 (declare-function org-clock-update-mode-line
"org-clock" ())
4322 (declare-function org-resolve-clocks
"org-clock"
4323 (&optional also-non-dangling-p prompt last-valid
))
4325 (defun org-at-TBLFM-p (&optional pos
)
4326 "Return t when point (or POS) is in #+TBLFM line."
4329 (goto-char (or pos
(point)))
4330 (beginning-of-line 1)
4331 (looking-at org-TBLFM-regexp
)))
4333 (defvar org-clock-start-time
)
4334 (defvar org-clock-marker
(make-marker)
4335 "Marker recording the last clock-in.")
4336 (defvar org-clock-hd-marker
(make-marker)
4337 "Marker recording the last clock-in, but the headline position.")
4338 (defvar org-clock-heading
""
4339 "The heading of the current clock entry.")
4340 (defun org-clock-is-active ()
4341 "Return non-nil if clock is currently running.
4342 The return value is actually the clock marker."
4343 (marker-buffer org-clock-marker
))
4346 (org-autoload "org-clock" '(org-clock-remove-overlays
4347 org-clock-update-time-maybe
4348 org-clocktable-shift
)))
4350 (defun org-check-running-clock ()
4351 "Check if the current buffer contains the running clock.
4352 If yes, offer to stop it and to save the buffer with the changes."
4353 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4354 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4357 (when (y-or-n-p "Save changed buffer?")
4360 (defun org-clocktable-try-shift (dir n
)
4361 "Check if this line starts a clock table, if yes, shift the time block."
4362 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4363 (org-clocktable-shift dir n
)))
4366 (defun org-clock-persistence-insinuate ()
4367 "Set up hooks for clock persistence."
4368 (require 'org-clock
)
4369 (add-hook 'org-mode-hook
'org-clock-load
)
4370 (add-hook 'kill-emacs-hook
'org-clock-save
))
4372 ;; Define the variable already here, to make sure we have it.
4373 (defvar org-indent-mode nil
4374 "Non-nil if Org-Indent mode is enabled.
4375 Use the command `org-indent-mode' to change this variable.")
4377 ;; Autoload archiving code
4378 ;; The stuff that is needed for cycling and tags has to be defined here.
4380 (defgroup org-archive nil
4381 "Options concerning archiving in Org-mode."
4383 :group
'org-structure
)
4385 (defcustom org-archive-location
"%s_archive::"
4386 "The location where subtrees should be archived.
4388 The value of this variable is a string, consisting of two parts,
4389 separated by a double-colon. The first part is a filename and
4390 the second part is a headline.
4392 When the filename is omitted, archiving happens in the same file.
4393 %s in the filename will be replaced by the current file
4394 name (without the directory part). Archiving to a different file
4395 is useful to keep archived entries from contributing to the
4398 The archived entries will be filed as subtrees of the specified
4399 headline. When the headline is omitted, the subtrees are simply
4400 filed away at the end of the file, as top-level entries. Also in
4401 the heading you can use %s to represent the file name, this can be
4402 useful when using the same archive for a number of different files.
4404 Here are a few examples:
4406 If the current file is Projects.org, archive in file
4407 Projects.org_archive, as top-level trees. This is the default.
4409 \"::* Archived Tasks\"
4410 Archive in the current file, under the top-level headline
4411 \"* Archived Tasks\".
4413 \"~/org/archive.org::\"
4414 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4416 \"~/org/archive.org::* From %s\"
4417 Archive in file ~/org/archive.org (absolute path), under headlines
4418 \"From FILENAME\" where file name is the current file name.
4420 \"~/org/datetree.org::datetree/* Finished Tasks\"
4421 The \"datetree/\" string is special, signifying to archive
4422 items to the datetree. Items are placed in either the CLOSED
4423 date of the item, or the current date if there is no CLOSED date.
4424 The heading will be a subentry to the current date. There doesn't
4425 need to be a heading, but there always needs to be a slash after
4426 datetree. For example, to store archived items directly in the
4427 datetree, use \"~/org/datetree.org::datetree/\".
4429 \"basement::** Finished Tasks\"
4430 Archive in file ./basement (relative path), as level 3 trees
4431 below the level 2 heading \"** Finished Tasks\".
4433 You may set this option on a per-file basis by adding to the buffer a
4436 #+ARCHIVE: basement::** Finished Tasks
4438 You may also define it locally for a subtree by setting an ARCHIVE property
4439 in the entry. If such a property is found in an entry, or anywhere up
4440 the hierarchy, it will be used."
4444 (defcustom org-archive-tag
"ARCHIVE"
4445 "The tag that marks a subtree as archived.
4446 An archived subtree does not open during visibility cycling, and does
4447 not contribute to the agenda listings.
4448 After changing this, font-lock must be restarted in the relevant buffers to
4449 get the proper fontification."
4451 :group
'org-keywords
4454 (defcustom org-agenda-skip-archived-trees t
4455 "Non-nil means the agenda will skip any items located in archived trees.
4456 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4457 variable is no longer recommended, you should leave it at the value t.
4458 Instead, use the key `v' to cycle the archives-mode in the agenda."
4460 :group
'org-agenda-skip
4463 (defcustom org-columns-skip-archived-trees t
4464 "Non-nil means ignore archived trees when creating column view."
4466 :group
'org-properties
4469 (defcustom org-cycle-open-archived-trees nil
4470 "Non-nil means `org-cycle' will open archived trees.
4471 An archived tree is a tree marked with the tag ARCHIVE.
4472 When nil, archived trees will stay folded. You can still open them with
4473 normal outline commands like `show-all', but not with the cycling commands."
4478 (defcustom org-sparse-tree-open-archived-trees nil
4479 "Non-nil means sparse tree construction shows matches in archived trees.
4480 When nil, matches in these trees are highlighted, but the trees are kept in
4483 :group
'org-sparse-trees
4486 (defcustom org-sparse-tree-default-date-type
'scheduled-or-deadline
4487 "The default date type when building a sparse tree.
4488 When this is nil, a date is a scheduled or a deadline timestamp.
4489 Otherwise, these types are allowed:
4492 active: only active timestamps (<...>)
4493 inactive: only inactive timestamps (<...)
4494 scheduled: only scheduled timestamps
4495 deadline: only deadline timestamps"
4496 :type
'(choice (const :tag
"Scheduled or deadline" 'scheduled-or-deadline
)
4497 (const :tag
"All timestamps" all
)
4498 (const :tag
"Only active timestamps" active
)
4499 (const :tag
"Only inactive timestamps" inactive
)
4500 (const :tag
"Only scheduled timestamps" scheduled
)
4501 (const :tag
"Only deadline timestamps" deadline
)
4502 (const :tag
"Only closed timestamps" closed
))
4504 :group
'org-sparse-trees
)
4506 (defun org-cycle-hide-archived-subtrees (state)
4507 "Re-hide all archived subtrees after a visibility state change."
4508 (when (and (not org-cycle-open-archived-trees
)
4509 (not (memq state
'(overview folded
))))
4511 (let* ((globalp (memq state
'(contents all
)))
4512 (beg (if globalp
(point-min) (point)))
4513 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4514 (org-hide-archived-subtrees beg end
)
4516 (if (looking-at (concat ".*:" org-archive-tag
":"))
4517 (message "%s" (substitute-command-keys
4518 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4520 (defun org-force-cycle-archived ()
4521 "Cycle subtree even if it is archived."
4523 (setq this-command
'org-cycle
)
4524 (let ((org-cycle-open-archived-trees t
))
4525 (call-interactively 'org-cycle
)))
4527 (defun org-hide-archived-subtrees (beg end
)
4528 "Re-hide all archived subtrees after a visibility state change."
4530 (let* ((re (concat ":" org-archive-tag
":")))
4532 (while (re-search-forward re end t
)
4533 (when (org-at-heading-p)
4534 (org-flag-subtree t
)
4535 (org-end-of-subtree t
))))))
4537 (declare-function outline-end-of-heading
"outline" ())
4538 (declare-function outline-flag-region
"outline" (from to flag
))
4539 (defun org-flag-subtree (flag)
4541 (org-back-to-heading t
)
4542 (outline-end-of-heading)
4543 (outline-flag-region (point)
4544 (progn (org-end-of-subtree t
) (point))
4547 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4550 (org-autoload "org-archive"
4551 '(org-add-archive-files)))
4553 ;; Autoload Column View Code
4555 (declare-function org-columns-number-to-string
"org-colview" (n fmt
&optional printf
))
4556 (declare-function org-columns-get-format-and-top-level
"org-colview" ())
4557 (declare-function org-columns-compute
"org-colview" (property))
4559 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4560 '(org-columns-number-to-string
4561 org-columns-get-format-and-top-level
4563 org-columns-remove-overlays
))
4567 (declare-function org-id-store-link
"org-id")
4568 (declare-function org-id-locations-load
"org-id")
4569 (declare-function org-id-locations-save
"org-id")
4570 (defvar org-id-track-globally
)
4571 (org-autoload "org-id"
4574 org-id-get-with-outline-path-completion
4575 org-id-get-with-outline-drilling
))
4577 ;;; Variables for pre-computed regular expressions, all buffer local
4579 (defvar org-drawer-regexp
"^[ \t]*:PROPERTIES:[ \t]*$"
4580 "Matches first line of a hidden block.")
4581 (make-variable-buffer-local 'org-drawer-regexp
)
4582 (defvar org-todo-regexp nil
4583 "Matches any of the TODO state keywords.")
4584 (make-variable-buffer-local 'org-todo-regexp
)
4585 (defvar org-not-done-regexp nil
4586 "Matches any of the TODO state keywords except the last one.")
4587 (make-variable-buffer-local 'org-not-done-regexp
)
4588 (defvar org-not-done-heading-regexp nil
4589 "Matches a TODO headline that is not done.")
4590 (make-variable-buffer-local 'org-not-done-regexp
)
4591 (defvar org-todo-line-regexp nil
4592 "Matches a headline and puts TODO state into group 2 if present.")
4593 (make-variable-buffer-local 'org-todo-line-regexp
)
4594 (defvar org-complex-heading-regexp nil
4595 "Matches a headline and puts everything into groups:
4597 group 2: The todo keyword, maybe
4598 group 3: Priority cookie
4599 group 4: True headline
4601 (make-variable-buffer-local 'org-complex-heading-regexp
)
4602 (defvar org-complex-heading-regexp-format nil
4603 "Printf format to make regexp to match an exact headline.
4604 This regexp will match the headline of any node which has the
4605 exact headline text that is put into the format, but may have any
4606 TODO state, priority and tags.")
4607 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4608 (defvar org-todo-line-tags-regexp nil
4609 "Matches a headline and puts TODO state into group 2 if present.
4610 Also put tags into group 4 if tags are present.")
4611 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4612 (defvar org-ds-keyword-length
12
4613 "Maximum length of the DEADLINE and SCHEDULED keywords.")
4614 (make-variable-buffer-local 'org-ds-keyword-length
)
4615 (defvar org-deadline-regexp nil
4616 "Matches the DEADLINE keyword.")
4617 (make-variable-buffer-local 'org-deadline-regexp
)
4618 (defvar org-deadline-time-regexp nil
4619 "Matches the DEADLINE keyword together with a time stamp.")
4620 (make-variable-buffer-local 'org-deadline-time-regexp
)
4621 (defvar org-deadline-time-hour-regexp nil
4622 "Matches the DEADLINE keyword together with a time-and-hour stamp.")
4623 (make-variable-buffer-local 'org-deadline-time-hour-regexp
)
4624 (defvar org-deadline-line-regexp nil
4625 "Matches the DEADLINE keyword and the rest of the line.")
4626 (make-variable-buffer-local 'org-deadline-line-regexp
)
4627 (defvar org-scheduled-regexp nil
4628 "Matches the SCHEDULED keyword.")
4629 (make-variable-buffer-local 'org-scheduled-regexp
)
4630 (defvar org-scheduled-time-regexp nil
4631 "Matches the SCHEDULED keyword together with a time stamp.")
4632 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4633 (defvar org-scheduled-time-hour-regexp nil
4634 "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
4635 (make-variable-buffer-local 'org-scheduled-time-hour-regexp
)
4636 (defvar org-closed-time-regexp nil
4637 "Matches the CLOSED keyword together with a time stamp.")
4638 (make-variable-buffer-local 'org-closed-time-regexp
)
4640 (defvar org-keyword-time-regexp nil
4641 "Matches any of the 4 keywords, together with the time stamp.")
4642 (make-variable-buffer-local 'org-keyword-time-regexp
)
4643 (defvar org-keyword-time-not-clock-regexp nil
4644 "Matches any of the 3 keywords, together with the time stamp.")
4645 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4646 (defvar org-maybe-keyword-time-regexp nil
4647 "Matches a timestamp, possibly preceded by a keyword.")
4648 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4649 (defvar org-all-time-keywords nil
4650 "List of time keywords.")
4651 (make-variable-buffer-local 'org-all-time-keywords
)
4653 (defconst org-plain-time-of-day-regexp
4656 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4659 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4661 "Regular expression to match a plain time or time range.
4662 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4663 groups carry important information:
4665 1 the first time, range or not
4666 8 the second time, if it is a range.")
4668 (defconst org-plain-time-extension-regexp
4671 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4672 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4673 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4674 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4675 groups carry important information:
4678 9 minutes of duration")
4680 (defconst org-stamp-time-of-day-regexp
4682 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4683 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4685 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4686 "Regular expression to match a timestamp time or time range.
4687 After a match, the following groups carry important information:
4689 1 date plus weekday, for back referencing to make sure both times are on the same day
4690 2 the first time, range or not
4691 4 the second time, if it is a range.")
4693 (defconst org-startup-options
4694 '(("fold" org-startup-folded t
)
4695 ("overview" org-startup-folded t
)
4696 ("nofold" org-startup-folded nil
)
4697 ("showall" org-startup-folded nil
)
4698 ("showeverything" org-startup-folded showeverything
)
4699 ("content" org-startup-folded content
)
4700 ("indent" org-startup-indented t
)
4701 ("noindent" org-startup-indented nil
)
4702 ("hidestars" org-hide-leading-stars t
)
4703 ("showstars" org-hide-leading-stars nil
)
4704 ("odd" org-odd-levels-only t
)
4705 ("oddeven" org-odd-levels-only nil
)
4706 ("align" org-startup-align-all-tables t
)
4707 ("noalign" org-startup-align-all-tables nil
)
4708 ("inlineimages" org-startup-with-inline-images t
)
4709 ("noinlineimages" org-startup-with-inline-images nil
)
4710 ("latexpreview" org-startup-with-latex-preview t
)
4711 ("nolatexpreview" org-startup-with-latex-preview nil
)
4712 ("customtime" org-display-custom-times t
)
4713 ("logdone" org-log-done time
)
4714 ("lognotedone" org-log-done note
)
4715 ("nologdone" org-log-done nil
)
4716 ("lognoteclock-out" org-log-note-clock-out t
)
4717 ("nolognoteclock-out" org-log-note-clock-out nil
)
4718 ("logrepeat" org-log-repeat state
)
4719 ("lognoterepeat" org-log-repeat note
)
4720 ("logdrawer" org-log-into-drawer t
)
4721 ("nologdrawer" org-log-into-drawer nil
)
4722 ("logstatesreversed" org-log-states-order-reversed t
)
4723 ("nologstatesreversed" org-log-states-order-reversed nil
)
4724 ("nologrepeat" org-log-repeat nil
)
4725 ("logreschedule" org-log-reschedule time
)
4726 ("lognotereschedule" org-log-reschedule note
)
4727 ("nologreschedule" org-log-reschedule nil
)
4728 ("logredeadline" org-log-redeadline time
)
4729 ("lognoteredeadline" org-log-redeadline note
)
4730 ("nologredeadline" org-log-redeadline nil
)
4731 ("logrefile" org-log-refile time
)
4732 ("lognoterefile" org-log-refile note
)
4733 ("nologrefile" org-log-refile nil
)
4734 ("fninline" org-footnote-define-inline t
)
4735 ("nofninline" org-footnote-define-inline nil
)
4736 ("fnlocal" org-footnote-section nil
)
4737 ("fnauto" org-footnote-auto-label t
)
4738 ("fnprompt" org-footnote-auto-label nil
)
4739 ("fnconfirm" org-footnote-auto-label confirm
)
4740 ("fnplain" org-footnote-auto-label plain
)
4741 ("fnadjust" org-footnote-auto-adjust t
)
4742 ("nofnadjust" org-footnote-auto-adjust nil
)
4743 ("constcgs" constants-unit-system cgs
)
4744 ("constSI" constants-unit-system SI
)
4745 ("noptag" org-tag-persistent-alist nil
)
4746 ("hideblocks" org-hide-block-startup t
)
4747 ("nohideblocks" org-hide-block-startup nil
)
4748 ("beamer" org-startup-with-beamer-mode t
)
4749 ("entitiespretty" org-pretty-entities t
)
4750 ("entitiesplain" org-pretty-entities nil
))
4751 "Variable associated with STARTUP options for org-mode.
4752 Each element is a list of three items: the startup options (as written
4753 in the #+STARTUP line), the corresponding variable, and the value to set
4754 this variable to if the option is found. An optional forth element PUSH
4755 means to push this value onto the list in the variable.")
4757 (defun org-update-property-plist (key val props
)
4758 "Update PROPS with KEY and VAL."
4759 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4760 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4761 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4762 (previous (cdr (assoc key props
))))
4764 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4765 (cons (cons key val
) remainder
))))
4767 (defconst org-block-regexp
4768 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4769 "Regular expression for hiding blocks.")
4770 (defconst org-heading-keyword-regexp-format
4771 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4772 "Printf format for a regexp matching a headline with some keyword.
4773 This regexp will match the headline of any node which has the
4774 exact keyword that is put into the format. The keyword isn't in
4775 any group by default, but the stars and the body are.")
4776 (defconst org-heading-keyword-maybe-regexp-format
4777 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4778 "Printf format for a regexp matching a headline, possibly with some keyword.
4779 This regexp can match any headline with the specified keyword, or
4780 without a keyword. The keyword isn't in any group by default,
4781 but the stars and the body are.")
4783 (defcustom org-group-tags t
4784 "When non-nil (the default), use group tags.
4785 This can be turned on/off through `org-toggle-tags-groups'."
4790 (defun org-toggle-tags-groups ()
4791 "Toggle support for group tags.
4792 Support for group tags is controlled by the option
4793 `org-group-tags', which is non-nil by default."
4795 (setq org-group-tags
(not org-group-tags
))
4796 (cond ((and (derived-mode-p 'org-agenda-mode
)
4799 ((derived-mode-p 'org-mode
)
4800 (let ((org-inhibit-startup t
)) (org-mode))))
4801 (message "Groups tags support has been turned %s"
4802 (if org-group-tags
"on" "off")))
4804 (defun org-set-regexps-and-options-for-tags ()
4805 "Precompute regular expressions used for tags in the current buffer."
4806 (when (derived-mode-p 'org-mode
)
4807 (org-set-local 'org-file-tags nil
)
4808 (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
4811 tags ftags key value
)
4815 (goto-char (point-min))
4816 (while (re-search-forward re nil t
)
4817 (setq key
(upcase (org-match-string-no-properties 1))
4818 value
(org-match-string-no-properties 2))
4819 (if (stringp value
) (setq value
(org-trim value
)))
4822 (setq tags
(append tags
(if tags
'("\\n") nil
)
4823 (org-split-string value splitre
))))
4824 ((equal key
"FILETAGS")
4825 (when (string-match "\\S-" value
)
4830 (mapcar (lambda (x) (org-split-string x
":"))
4831 (org-split-string value
)))))))))))
4832 ;; Process the file tags.
4833 (and ftags
(org-set-local 'org-file-tags
4834 (mapcar 'org-add-prop-inherited ftags
)))
4835 (org-set-local 'org-tag-groups-alist nil
)
4836 ;; Process the tags.
4840 (while (setq e
(pop tags
))
4843 (progn (push '(:startgroup
) tgs
)
4844 (when (equal (nth 1 tags
) ":")
4845 (push (list (replace-regexp-in-string
4846 "(.+)$" "" (nth 0 tags
)))
4847 org-tag-groups-alist
)
4849 ((equal e
":") (push '(:grouptags
) tgs
))
4850 ((equal e
"}") (push '(:endgroup
) tgs
) (if g
(setq g nil
)))
4851 ((equal e
"\\n") (push '(:newline
) tgs
))
4852 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4853 (push (cons (match-string 1 e
)
4854 (string-to-char (match-string 2 e
))) tgs
)
4856 (setcar org-tag-groups-alist
4857 (append (car org-tag-groups-alist
)
4858 (list (match-string 1 e
)))))
4859 (if g
(setq g
(1+ g
))))
4860 (t (push (list e
) tgs
)
4862 (setcar org-tag-groups-alist
4863 (append (car org-tag-groups-alist
) (list e
))))
4864 (if g
(setq g
(1+ g
))))))
4865 (org-set-local 'org-tag-alist nil
)
4866 (while (setq e
(pop tgs
))
4867 (or (and (stringp (car e
))
4868 (assoc (car e
) org-tag-alist
))
4869 (push e org-tag-alist
)))
4870 ;; Return a list with tag variables
4871 (list org-file-tags org-tag-alist org-tag-groups-alist
))))))
4873 (defun org-set-regexps-and-options ()
4874 "Precompute regular expressions used in the current buffer."
4875 (when (derived-mode-p 'org-mode
)
4876 (org-set-local 'org-todo-kwd-alist nil
)
4877 (org-set-local 'org-todo-key-alist nil
)
4878 (org-set-local 'org-todo-key-trigger nil
)
4879 (org-set-local 'org-todo-keywords-1 nil
)
4880 (org-set-local 'org-done-keywords nil
)
4881 (org-set-local 'org-todo-heads nil
)
4882 (org-set-local 'org-todo-sets nil
)
4883 (org-set-local 'org-todo-log-states nil
)
4884 (org-set-local 'org-file-properties nil
)
4885 (let ((re (org-make-options-regexp
4886 '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
4887 "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
4888 "SETUPFILE" "OPTIONS")
4889 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4891 (scripts org-use-sub-superscripts
)
4892 kwds kws0 kwsa key log value cat arch const links hw dws
4893 tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
4898 (goto-char (point-min))
4904 (insert ext-setup-or-nil
)
4905 (let ((major-mode 'org-mode
))
4906 (setq ret
(save-match-data
4907 (org-set-regexps-and-options-for-tags)))))
4908 ;; Append setupfile tags to existing tags
4910 (delq nil
(append org-file-tags
(nth 0 ret
)))
4912 (delq nil
(append org-tag-alist
(nth 1 ret
)))
4913 org-tag-groups-alist
4914 (delq nil
(append org-tag-groups-alist
(nth 2 ret
))))))
4915 (and ext-setup-or-nil
4916 (string-match re ext-setup-or-nil start
)
4917 (setq start
(match-end 0)))
4918 (and (setq ext-setup-or-nil nil start
0)
4919 (re-search-forward re nil t
)))
4920 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4921 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4922 (if (stringp value
) (setq value
(org-trim value
)))
4924 ((equal key
"CATEGORY")
4926 ((member key
'("SEQ_TODO" "TODO"))
4927 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4928 ((equal key
"TYP_TODO")
4929 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4930 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4931 ;; general TODO-like setup
4932 (push (cons (intern (downcase (match-string 1 key
)))
4933 (org-split-string value splitre
)) kwds
))
4934 ((equal key
"COLUMNS")
4935 (org-set-local 'org-columns-default-format value
))
4937 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4938 (push (cons (match-string 1 value
)
4939 (org-trim (match-string 2 value
)))
4941 ((equal key
"PRIORITIES")
4942 (setq prio
(org-split-string value
" +")))
4943 ((equal key
"PROPERTY")
4944 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4945 (setq props
(org-update-property-plist (match-string 1 value
)
4946 (match-string 2 value
)
4948 ((equal key
"DRAWERS")
4949 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4950 ((equal key
"CONSTANTS")
4951 (org-table-set-constants))
4952 ((equal key
"STARTUP")
4953 (let ((opts (org-split-string value splitre
))
4955 (while (setq l
(pop opts
))
4956 (when (setq l
(assoc l org-startup-options
))
4957 (setq var
(nth 1 l
) val
(nth 2 l
))
4959 (set (make-local-variable var
) val
)
4960 (if (not (listp (symbol-value var
)))
4961 (set (make-local-variable var
) nil
))
4962 (set (make-local-variable var
) (symbol-value var
))
4963 (add-to-list var val
))))))
4964 ((equal key
"ARCHIVE")
4966 (remove-text-properties 0 (length arch
)
4967 '(face t fontified t
) arch
))
4968 ((equal key
"OPTIONS")
4969 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4970 (setq scripts
(read (match-string 2 value
)))))
4971 ((and (equal key
"SETUPFILE")
4972 ;; Prevent checking in Gnus messages
4973 (not buffer-read-only
))
4974 (setq setup-contents
(org-file-contents
4976 (org-remove-double-quotes value
))
4978 (if (not ext-setup-or-nil
)
4979 (setq ext-setup-or-nil setup-contents start
0)
4980 (setq ext-setup-or-nil
4981 (concat (substring ext-setup-or-nil
0 start
)
4982 "\n" setup-contents
"\n"
4983 (substring ext-setup-or-nil start
)))))))
4984 ;; search for property blocks
4985 (goto-char (point-min))
4986 (while (re-search-forward org-block-regexp nil t
)
4987 (when (equal "PROPERTY" (upcase (match-string 1)))
4988 (setq value
(replace-regexp-in-string
4989 "[\n\r]" " " (match-string 4)))
4990 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4991 (setq props
(org-update-property-plist (match-string 1 value
)
4992 (match-string 2 value
)
4994 (org-set-local 'org-use-sub-superscripts scripts
)
4996 (org-set-local 'org-category
(intern cat
))
4997 (push (cons "CATEGORY" cat
) props
))
4999 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
5000 (setq prio
(mapcar 'string-to-char prio
))
5001 (org-set-local 'org-highest-priority
(nth 0 prio
))
5002 (org-set-local 'org-lowest-priority
(nth 1 prio
))
5003 (org-set-local 'org-default-priority
(nth 2 prio
)))
5004 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
5005 (and drawers
(org-set-local 'org-drawers drawers
))
5006 (and arch
(org-set-local 'org-archive-location arch
))
5007 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
5008 ;; Process the TODO keywords
5010 ;; Use the global values as if they had been given locally.
5011 (setq kwds
(default-value 'org-todo-keywords
))
5012 (if (stringp (car kwds
))
5013 (setq kwds
(list (cons org-todo-interpretation
5014 (default-value 'org-todo-keywords
)))))
5015 (setq kwds
(reverse kwds
)))
5016 (setq kwds
(nreverse kwds
))
5018 (while (setq kws
(pop kwds
))
5020 (run-hook-with-args-until-success
5021 'org-todo-setup-filter-hook kws
)
5023 (setq inter
(pop kws
) sep
(member "|" kws
)
5024 kws0
(delete "|" (copy-sequence kws
))
5029 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
5031 (setq kw
(match-string 1 x
)
5032 key
(and (match-end 2) (match-string 2 x
))
5033 log
(org-extract-log-state-settings x
))
5034 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
5035 (and log
(push log org-todo-log-states
))
5037 (error "Invalid TODO keyword %s" x
)))
5039 kwsa
(if kwsa
(append '((:startgroup
))
5043 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
5044 tail
(list inter hw
(car dws
) (org-last dws
))))
5045 (add-to-list 'org-todo-heads hw
'append
)
5046 (push kws1 org-todo-sets
)
5047 (setq org-done-keywords
(append org-done-keywords dws nil
))
5048 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
5049 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
5050 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
5051 (setq org-todo-sets
(nreverse org-todo-sets
)
5052 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
5053 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
5054 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
5055 ;; Compute the regular expressions and other local variables.
5056 ;; Using `org-outline-regexp-bol' would complicate them much,
5057 ;; because of the fixed white space at the end of that string.
5058 (if (not org-done-keywords
)
5059 (setq org-done-keywords
(and org-todo-keywords-1
5060 (list (org-last org-todo-keywords-1
)))))
5061 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
5062 (length org-scheduled-string
)
5063 (length org-clock-string
)
5064 (length org-closed-string
)))
5066 (concat "^[ \t]*:\\("
5067 (mapconcat 'regexp-quote org-drawers
"\\|")
5069 org-not-done-keywords
5070 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
5073 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
5077 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
5079 org-not-done-heading-regexp
5080 (format org-heading-keyword-regexp-format org-not-done-regexp
)
5081 org-todo-line-regexp
5082 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
5083 org-complex-heading-regexp
5084 (concat "^\\(\\*+\\)"
5085 "\\(?: +" org-todo-regexp
"\\)?"
5086 "\\(?: +\\(\\[#.\\]\\)\\)?"
5087 "\\(?: +\\(.*?\\)\\)??"
5088 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
5090 org-complex-heading-regexp-format
5091 (concat "^\\(\\*+\\)"
5092 "\\(?: +" org-todo-regexp
"\\)?"
5093 "\\(?: +\\(\\[#.\\]\\)\\)?"
5095 ;; Stats cookies can be stuck to body.
5096 "\\(?:\\[[0-9%%/]+\\] *\\)?"
5098 "\\(?: *\\[[0-9%%/]+\\]\\)?"
5100 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
5102 org-todo-line-tags-regexp
5103 (concat "^\\(\\*+\\)"
5104 "\\(?: +" org-todo-regexp
"\\)?"
5105 "\\(?: +\\(.*?\\)\\)??"
5106 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
5108 org-deadline-regexp
(concat "\\<" org-deadline-string
)
5109 org-deadline-time-regexp
5110 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
5111 org-deadline-time-hour-regexp
5112 (concat "\\<" org-deadline-string
5113 " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
5114 org-deadline-line-regexp
5115 (concat "\\<\\(" org-deadline-string
"\\).*")
5116 org-scheduled-regexp
5117 (concat "\\<" org-scheduled-string
)
5118 org-scheduled-time-regexp
5119 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
5120 org-scheduled-time-hour-regexp
5121 (concat "\\<" org-scheduled-string
5122 " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
5123 org-closed-time-regexp
5124 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
5125 org-keyword-time-regexp
5126 (concat "\\<\\(" org-scheduled-string
5127 "\\|" org-deadline-string
5128 "\\|" org-closed-string
5129 "\\|" org-clock-string
"\\)"
5130 " *[[<]\\([^]>]+\\)[]>]")
5131 org-keyword-time-not-clock-regexp
5132 (concat "\\<\\(" org-scheduled-string
5133 "\\|" org-deadline-string
5134 "\\|" org-closed-string
5136 " *[[<]\\([^]>]+\\)[]>]")
5137 org-maybe-keyword-time-regexp
5138 (concat "\\(\\<\\(" org-scheduled-string
5139 "\\|" org-deadline-string
5140 "\\|" org-closed-string
5141 "\\|" org-clock-string
"\\)\\)?"
5142 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
5143 org-all-time-keywords
5144 (mapcar (lambda (w) (substring w
0 -
1))
5145 (list org-scheduled-string org-deadline-string
5146 org-clock-string org-closed-string
)))
5147 (org-compute-latex-and-related-regexp)
5148 (org-set-font-lock-defaults))))
5150 (defun org-file-contents (file &optional noerror
)
5151 "Return the contents of FILE, as a string."
5153 (not (file-readable-p file
)))
5155 (message "Cannot read file \"%s\"" file
)
5156 (error "Cannot read file \"%s\"" file
))
5158 (insert-file-contents file
)
5161 (defun org-extract-log-state-settings (x)
5162 "Extract the log state setting from a TODO keyword string.
5163 This will extract info from a string like \"WAIT(w@/!)\"."
5164 (let (kw key log1 log2
)
5165 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
5166 (setq kw
(match-string 1 x
)
5167 key
(and (match-end 2) (match-string 2 x
))
5168 log1
(and (match-end 3) (match-string 3 x
))
5169 log2
(and (match-end 4) (match-string 4 x
)))
5172 (and log1
(if (equal log1
"!") 'time
'note
))
5173 (and log2
(if (equal log2
"!") 'time
'note
)))))))
5175 (defun org-remove-keyword-keys (list)
5176 "Remove a pair of parenthesis at the end of each string in LIST."
5178 (if (string-match "(.*)$" x
)
5179 (substring x
0 (match-beginning 0))
5183 (defun org-assign-fast-keys (alist)
5184 "Assign fast keys to a keyword-key alist.
5185 Respect keys that are already there."
5186 (let (new e
(alt ?
0))
5187 (while (setq e
(pop alist
))
5188 (if (or (memq (car e
) '(:newline
:grouptags
:endgroup
:startgroup
))
5189 (cdr e
)) ;; Key already assigned.
5191 (let ((clist (string-to-list (downcase (car e
))))
5192 (used (append new alist
)))
5193 (when (= (car clist
) ?
@)
5195 (while (and clist
(rassoc (car clist
) used
))
5198 (while (rassoc alt used
)
5200 (push (cons (car e
) (or (car clist
) alt
)) new
))))
5203 ;;; Some variables used in various places
5205 (defvar org-window-configuration nil
5206 "Used in various places to store a window configuration.")
5207 (defvar org-selected-window nil
5208 "Used in various places to store a window configuration.")
5209 (defvar org-finish-function nil
5210 "Function to be called when `C-c C-c' is used.
5211 This is for getting out of special buffers like capture.")
5214 ;; FIXME: Occasionally check by commenting these, to make sure
5215 ;; no other functions uses these, forgetting to let-bind them.
5216 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
5217 (defvar org-last-state
)
5218 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
5220 ;; Defined somewhere in this file, but used before definition.
5221 (defvar org-entities
) ;; defined in org-entities.el
5222 (defvar org-struct-menu
)
5223 (defvar org-org-menu
)
5224 (defvar org-tbl-menu
)
5226 ;;;; Define the Org-mode
5228 ;; We use a before-change function to check if a table might need
5230 (defvar org-table-may-need-update t
5231 "Indicates that a table might need an update.
5232 This variable is set by `org-before-change-function'.
5233 `org-table-align' sets it back to nil.")
5234 (defun org-before-change-function (beg end
)
5235 "Every change indicates that a table might need an update."
5236 (setq org-table-may-need-update t
))
5237 (defvar org-mode-map
)
5238 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
5239 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
5240 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
5241 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
5242 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
5243 (defvar org-table-buffer-is-an nil
)
5245 (defvar bidi-paragraph-direction
)
5246 (defvar buffer-face-mode-face
)
5249 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
5250 (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"))
5251 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
5253 ;; Other stuff we need.
5254 (require 'time-date
)
5255 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
5259 ;; (require 'org-macs) moved higher up in the file before it is first used
5260 (require 'org-entities
)
5261 ;; (require 'org-compat) moved higher up in the file before it is first used
5262 (require 'org-faces
)
5264 (require 'org-pcomplete
)
5266 (require 'org-footnote
)
5267 (require 'org-macro
)
5273 (define-derived-mode org-mode outline-mode
"Org"
5274 "Outline-based notes management and organizer, alias
5275 \"Carsten's outline-mode for keeping track of everything.\"
5277 Org-mode develops organizational tasks around a NOTES file which
5278 contains information about projects as plain text. Org-mode is
5279 implemented on top of outline-mode, which is ideal to keep the content
5280 of large files well structured. It supports ToDo items, deadlines and
5281 time stamps, which magically appear in the diary listing of the Emacs
5282 calendar. Tables are easily created with a built-in table editor.
5283 Plain text URL-like links connect to websites, emails (VM), Usenet
5284 messages (Gnus), BBDB entries, and any files related to the project.
5285 For printing and sharing of notes, an Org-mode file (or a part of it)
5286 can be exported as a structured ASCII or HTML file.
5288 The following commands are available:
5292 ;; Get rid of Outline menus, they are not needed
5293 ;; Need to do this here because define-derived-mode sets up
5294 ;; the keymap so late. Still, it is a waste to call this each time
5295 ;; we switch another buffer into org-mode.
5296 (if (featurep 'xemacs
)
5297 (when (boundp 'outline-mode-menu-heading
)
5298 ;; Assume this is Greg's port, it uses easymenu
5299 (easy-menu-remove outline-mode-menu-heading
)
5300 (easy-menu-remove outline-mode-menu-show
)
5301 (easy-menu-remove outline-mode-menu-hide
))
5302 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
5303 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
5304 (define-key org-mode-map
[menu-bar show
] 'undefined
))
5306 (org-load-modules-maybe)
5307 (easy-menu-add org-org-menu
)
5308 (easy-menu-add org-tbl-menu
)
5309 (org-install-agenda-files-menu)
5310 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
5311 (add-to-invisibility-spec '(org-cwidth))
5312 (add-to-invisibility-spec '(org-hide-block . t
))
5313 (when (featurep 'xemacs
)
5314 (org-set-local 'line-move-ignore-invisible t
))
5315 (org-set-local 'outline-regexp org-outline-regexp
)
5316 (org-set-local 'outline-level
'org-outline-level
)
5317 (setq bidi-paragraph-direction
'left-to-right
)
5318 ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
5319 (set (make-local-variable 'paragraph-start
) "\f\\|[ \t]*$\\|\\*+ ")
5320 (when (and org-ellipsis
5321 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
5322 (fboundp 'make-glyph-code
))
5323 (unless org-display-table
5324 (setq org-display-table
(make-display-table)))
5325 (set-display-table-slot
5328 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
5330 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
5331 (setq buffer-display-table org-display-table
))
5332 (org-set-regexps-and-options-for-tags)
5333 (org-set-regexps-and-options)
5334 (when (and org-tag-faces
(not org-tags-special-faces-re
))
5335 ;; tag faces set outside customize.... force initialization.
5336 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
5338 (org-set-local 'calc-embedded-open-mode
"# ")
5339 ;; Modify a few syntax entries
5340 (modify-syntax-entry ?
@ "w")
5341 (modify-syntax-entry ?
\" "\"")
5342 (if org-startup-truncated
(setq truncate-lines t
))
5343 (when org-startup-indented
(require 'org-indent
) (org-indent-mode 1))
5344 (org-set-local 'font-lock-unfontify-region-function
5345 'org-unfontify-region
)
5346 ;; Activate before-change-function
5347 (org-set-local 'org-table-may-need-update t
)
5348 (org-add-hook 'before-change-functions
'org-before-change-function nil
5350 ;; Check for running clock before killing a buffer
5351 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5352 ;; Initialize macros templates.
5353 (org-macro-initialize-templates)
5354 ;; Initialize radio targets.
5355 (org-update-radio-target-regexp)
5357 (org-set-local 'indent-line-function
'org-indent-line
)
5358 (org-set-local 'indent-region-function
'org-indent-region
)
5359 ;; Filling and auto-filling.
5362 (org-setup-comments-handling)
5363 ;; Beginning/end of defun
5364 (org-set-local 'beginning-of-defun-function
'org-backward-element
)
5365 (org-set-local 'end-of-defun-function
'org-forward-element
)
5366 ;; Next error for sparse trees
5367 (org-set-local 'next-error-function
'org-occur-next-match
)
5368 ;; Make sure dependence stuff works reliably, even for users who set it
5370 (if org-enforce-todo-dependencies
5371 (add-hook 'org-blocker-hook
5372 'org-block-todo-from-children-or-siblings-or-parent
)
5373 (remove-hook 'org-blocker-hook
5374 'org-block-todo-from-children-or-siblings-or-parent
))
5375 (if org-enforce-todo-checkbox-dependencies
5376 (add-hook 'org-blocker-hook
5377 'org-block-todo-from-checkboxes
)
5378 (remove-hook 'org-blocker-hook
5379 'org-block-todo-from-checkboxes
))
5381 ;; Align options lines
5383 'align-mode-rules-list
5384 '((org-in-buffer-settings
5385 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5386 (modes .
'(org-mode)))))
5389 (org-set-local 'imenu-create-index-function
5390 'org-imenu-get-tree
)
5392 ;; Make isearch reveal context
5393 (if (or (featurep 'xemacs
)
5394 (not (boundp 'outline-isearch-open-invisible-function
)))
5395 ;; Emacs 21 and XEmacs make use of the hook
5396 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5397 ;; Emacs 22 deals with this through a special variable
5398 (org-set-local 'outline-isearch-open-invisible-function
5399 (lambda (&rest ignore
) (org-show-context 'isearch
)))
5400 (org-add-hook 'isearch-mode-end-hook
'org-fix-ellipsis-at-bol
'append
'local
))
5402 ;; Setup the pcomplete hooks
5403 (set (make-local-variable 'pcomplete-command-completion-function
)
5404 'org-pcomplete-initial
)
5405 (set (make-local-variable 'pcomplete-command-name-function
)
5406 'org-command-at-point
)
5407 (set (make-local-variable 'pcomplete-default-completion-function
)
5409 (set (make-local-variable 'pcomplete-parse-arguments-function
)
5410 'org-parse-arguments
)
5411 (set (make-local-variable 'pcomplete-termination-string
) "")
5412 (when (>= emacs-major-version
23)
5413 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
5415 ;; If empty file that did not turn on org-mode automatically, make it to.
5416 (if (and org-insert-mode-line-in-empty-file
5417 (org-called-interactively-p 'any
)
5418 (= (point-min) (point-max)))
5419 (insert "# -*- mode: org -*-\n\n"))
5420 (unless org-inhibit-startup
5422 (and org-startup-with-beamer-mode
(org-beamer-mode))
5423 (when org-startup-align-all-tables
5424 (org-table-map-tables 'org-table-align
'quietly
))
5425 (when org-startup-with-inline-images
5426 (org-display-inline-images))
5427 (when org-startup-with-latex-preview
5428 (org-preview-latex-fragment))
5429 (unless org-inhibit-startup-visibility-stuff
5430 (org-set-startup-visibility))))
5431 ;; Try to set org-hide correctly
5432 (set-face-foreground 'org-hide
(org-find-invisible-foreground)))
5434 ;; Update `customize-package-emacs-version-alist'
5435 (add-to-list 'customize-package-emacs-version-alist
5436 '(Org ("6.21b" .
"23.1") ("6.33x" .
"23.2")
5437 ("7.8.11" .
"24.1") ("7.9.4" .
"24.3")
5440 (defvar org-mode-transpose-word-syntax-table
5441 (let ((st (make-syntax-table)))
5442 (mapc (lambda(c) (modify-syntax-entry
5443 (string-to-char (car c
)) "w p" st
))
5447 (when (fboundp 'abbrev-table-put
)
5448 (abbrev-table-put org-mode-abbrev-table
5449 :parents
(list text-mode-abbrev-table
)))
5451 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5453 (defsubst org-fix-ellipsis-at-bol
()
5454 (save-excursion (goto-char (window-start)) (recenter 0)))
5456 (defun org-find-invisible-foreground ()
5457 (let ((candidates (remove
5460 (list (face-background 'default
)
5461 (face-background 'org-default
))
5464 (when (boundp alist
)
5465 (cdr (assoc 'background-color
(symbol-value alist
)))))
5466 '(default-frame-alist initial-frame-alist window-system-default-frame-alist
))
5467 (list (face-foreground 'org-hide
))))))
5468 (car (remove nil candidates
))))
5470 (defun org-current-time (&optional rounding-minutes past
)
5471 "Current time, possibly rounded to ROUNDING-MINUTES.
5472 When ROUNDING-MINUTES is not an integer, fall back on the car of
5473 `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
5474 the rounding returns a past time."
5475 (let ((r (or (and (integerp rounding-minutes
) rounding-minutes
)
5476 (car org-time-stamp-rounding-minutes
)))
5477 (time (decode-time)) res
)
5482 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5484 (if (and past
(< (org-float-time (time-subtract (current-time) res
)) 0))
5485 (seconds-to-time (- (org-float-time res
) (* r
60)))
5489 "Return today date, considering `org-extend-today-until'."
5491 (time-subtract (current-time)
5492 (list 0 (* 3600 org-extend-today-until
) 0))))
5494 ;;;; Font-Lock stuff, including the activators
5496 (defvar org-mouse-map
(make-sparse-keymap))
5497 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5498 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5499 (when org-mouse-1-follows-link
5500 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5501 (when org-tab-follows-link
5502 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5503 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5505 (require 'font-lock
)
5507 (defconst org-non-link-chars
"]\t\n\r<>")
5508 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5509 "shell" "elisp" "doi" "message"))
5510 (defvar org-link-types-re nil
5511 "Matches a link that has a url-like prefix like \"http:\"")
5512 (defvar org-link-re-with-space nil
5513 "Matches a link with spaces, optional angular brackets around it.")
5514 (defvar org-link-re-with-space2 nil
5515 "Matches a link with spaces, optional angular brackets around it.")
5516 (defvar org-link-re-with-space3 nil
5517 "Matches a link with spaces, only for internal part in bracket links.")
5518 (defvar org-angle-link-re nil
5519 "Matches link with angular brackets, spaces are allowed.")
5520 (defvar org-plain-link-re nil
5521 "Matches plain link, without spaces.")
5522 (defvar org-bracket-link-regexp nil
5523 "Matches a link in double brackets.")
5524 (defvar org-bracket-link-analytic-regexp nil
5525 "Regular expression used to analyze links.
5526 Here is what the match groups contain after a match:
5532 (defvar org-bracket-link-analytic-regexp
++ nil
5533 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5534 (defvar org-any-link-re nil
5535 "Regular expression matching any link.")
5537 (defconst org-match-sexp-depth
3
5538 "Number of stacked braces for sub/superscript matching.")
5540 (defun org-create-multibrace-regexp (left right n
)
5541 "Create a regular expression which will match a balanced sexp.
5542 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5543 as single character strings.
5544 The regexp returned will match the entire expression including the
5545 delimiters. It will also define a single group which contains the
5546 match except for the outermost delimiters. The maximum depth of
5547 stacked delimiters is N. Escaping delimiters is not possible."
5548 (let* ((nothing (concat "[^" left right
"]*?"))
5551 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5554 re
(concat re or next
)
5555 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5556 (concat left
"\\(" re
"\\)" right
)))
5558 (defvar org-match-substring-regexp
5560 "\\(\\S-\\)\\([_^]\\)\\("
5561 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5563 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5565 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5566 "The regular expression matching a sub- or superscript.")
5568 (defvar org-match-substring-with-braces-regexp
5570 "\\(\\S-\\)\\([_^]\\)\\("
5571 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5573 "The regular expression matching a sub- or superscript, forcing braces.")
5575 (defun org-make-link-regexps ()
5576 "Update the link regular expressions.
5577 This should be called after the variable `org-link-types' has changed."
5578 (setq org-link-types-re
5580 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5581 org-link-re-with-space
5583 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5584 "\\([^" org-non-link-chars
" ]"
5585 "[^" org-non-link-chars
"]*"
5586 "[^" org-non-link-chars
" ]\\)>?")
5587 org-link-re-with-space2
5589 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5590 "\\([^" org-non-link-chars
" ]"
5592 "[^" org-non-link-chars
" ]\\)>?")
5593 org-link-re-with-space3
5595 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5596 "\\([^" org-non-link-chars
" ]"
5600 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5601 "\\([^" org-non-link-chars
" ]"
5602 "[^" org-non-link-chars
"]*"
5606 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5607 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5608 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5609 org-bracket-link-regexp
5610 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5611 org-bracket-link-analytic-regexp
5614 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5617 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5619 org-bracket-link-analytic-regexp
++
5622 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5625 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5628 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5629 org-angle-link-re
"\\)\\|\\("
5630 org-plain-link-re
"\\)")))
5632 (org-make-link-regexps)
5634 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5635 "Regular expression for fast time stamp matching.")
5636 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5637 "Regular expression for fast time stamp matching.")
5638 (defconst org-ts-regexp0
5639 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5640 "Regular expression matching time strings for analysis.
5641 This one does not require the space after the date, so it can be used
5642 on a string that terminates immediately after the date.")
5643 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5644 "Regular expression matching time strings for analysis.")
5645 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5646 "Regular expression matching time stamps, with groups.")
5647 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5648 "Regular expression matching time stamps (also [..]), with groups.")
5649 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5650 "Regular expression matching a time stamp range.")
5651 (defconst org-tr-regexp-both
5652 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5653 "Regular expression matching a time stamp range.")
5654 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5655 org-ts-regexp
"\\)?")
5656 "Regular expression matching a time stamp or time stamp range.")
5657 (defconst org-tsr-regexp-both
5658 (concat org-ts-regexp-both
"\\(--?-?"
5659 org-ts-regexp-both
"\\)?")
5660 "Regular expression matching a time stamp or time stamp range.
5661 The time stamps may be either active or inactive.")
5663 (defvar org-emph-face nil
)
5665 (defun org-do-emphasis-faces (limit)
5666 "Run through the buffer and add overlays to emphasized strings."
5668 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5669 (if (not (= (char-after (match-beginning 3))
5670 (char-after (match-beginning 4))))
5673 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5674 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5678 (org-remove-flyspell-overlays-in
5679 (match-beginning 0) (match-end 0)))
5680 (add-text-properties (match-beginning 2) (match-end 2)
5681 '(font-lock-multiline t org-emphasis t
))
5682 (when org-hide-emphasis-markers
5683 (add-text-properties (match-end 4) (match-beginning 5)
5684 '(invisible org-link
))
5685 (add-text-properties (match-beginning 3) (match-end 3)
5686 '(invisible org-link
)))))
5690 (defun org-emphasize (&optional char
)
5691 "Insert or change an emphasis, i.e. a font like bold or italic.
5692 If there is an active region, change that region to a new emphasis.
5693 If there is no region, just insert the marker characters and position
5694 the cursor between them.
5695 CHAR should be the marker character. If it is a space, it means to
5696 remove the emphasis of the selected region.
5697 If CHAR is not given (for example in an interactive call) it will be
5700 (let ((erc org-emphasis-regexp-components
)
5702 (string "") beg end move c s
)
5703 (if (org-region-active-p)
5704 (setq beg
(region-beginning) end
(region-end)
5705 string
(buffer-substring beg end
))
5709 (message "Emphasis marker or tag: [%s]"
5710 (mapconcat (lambda(e) (car e
)) org-emphasis-alist
""))
5711 (setq char
(read-char-exclusive)))
5712 (if (equal char ?\
)
5713 (setq s
"" move nil
)
5714 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5715 (user-error "No such emphasis marker: \"%c\"" char
))
5716 (setq s
(char-to-string char
)))
5717 (while (and (> (length string
) 1)
5718 (equal (substring string
0 1) (substring string -
1))
5719 (assoc (substring string
0 1) org-emphasis-alist
))
5720 (setq string
(substring string
1 -
1)))
5721 (setq string
(concat s string s
))
5722 (if beg
(delete-region beg end
))
5724 (string-match (concat "[" (nth 0 erc
) "\n]")
5725 (char-to-string (char-before (point)))))
5728 (string-match (concat "[" (nth 1 erc
) "\n]")
5729 (char-to-string (char-after (point)))))
5730 (insert " ") (backward-char 1))
5732 (and move
(backward-char 1))))
5734 (defconst org-nonsticky-props
5735 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link
))
5737 (defsubst org-rear-nonsticky-at
(pos)
5738 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5740 (defun org-activate-plain-links (limit)
5741 "Run through the buffer and add overlays to links."
5743 (when (and (re-search-forward (concat org-plain-link-re
) limit t
)
5744 (not (org-in-src-block-p)))
5745 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5746 (setq f
(get-text-property (match-beginning 0) 'face
))
5747 (setq hl
(org-match-string-no-properties 0))
5748 (if (or (eq f
'org-tag
)
5749 (and (listp f
) (memq 'org-tag f
)))
5751 (add-text-properties (match-beginning 0) (match-end 0)
5752 (list 'mouse-face
'highlight
5754 'htmlize-link
`(:uri
,hl
)
5755 'keymap org-mouse-map
))
5756 (org-rear-nonsticky-at (match-end 0)))
5759 (defun org-activate-code (limit)
5760 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5762 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5763 (remove-text-properties (match-beginning 0) (match-end 0)
5764 '(display t invisible t intangible t
))
5767 (defcustom org-src-fontify-natively nil
5768 "When non-nil, fontify code in code blocks."
5771 :group
'org-appearance
5774 (defcustom org-allow-promoting-top-level-subtree nil
5775 "When non-nil, allow promoting a top level subtree.
5776 The leading star of the top level headline will be replaced
5780 :group
'org-appearance
)
5782 (defun org-fontify-meta-lines-and-blocks (limit)
5784 (org-fontify-meta-lines-and-blocks-1 limit
)
5785 (error (message "org-mode fontification error"))))
5787 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5788 "Fontify #+ lines and blocks."
5789 (let ((case-fold-search t
))
5790 (if (re-search-forward
5791 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5793 (let ((beg (match-beginning 0))
5794 (block-start (match-end 0))
5796 (lang (match-string 7))
5797 (beg1 (line-beginning-position 2))
5798 (dc1 (downcase (match-string 2)))
5799 (dc3 (downcase (match-string 3)))
5800 end end1 quoting block-type ovl
)
5802 ((member dc1
'("+html:" "+ascii:" "+latex:"))
5803 ;; a single line of backend-specific content
5804 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5805 (remove-text-properties (match-beginning 0) (match-end 0)
5806 '(display t invisible t intangible t
))
5807 (add-text-properties (match-beginning 1) (match-end 3)
5808 '(font-lock-fontified t face org-meta-line
))
5809 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5810 '(font-lock-fontified t face org-block
))
5811 ; for backend-specific code
5813 ((and (match-end 4) (equal dc3
"+begin"))
5815 (setq block-type
(downcase (match-string 5))
5816 quoting
(member block-type org-protecting-blocks
))
5817 (when (re-search-forward
5818 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5819 nil t
) ;; on purpose, we look further than LIMIT
5820 (setq end
(min (point-max) (match-end 0))
5821 end1
(min (point-max) (1- (match-beginning 0))))
5822 (setq block-end
(match-beginning 0))
5824 (remove-text-properties beg end
5825 '(display t invisible t intangible t
)))
5826 (add-text-properties
5828 '(font-lock-fontified t font-lock-multiline t
))
5829 (add-text-properties beg beg1
'(face org-meta-line
))
5830 (add-text-properties end1
(min (point-max) (1+ end
))
5831 '(face org-meta-line
)) ; for end_src
5833 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5834 (org-src-font-lock-fontify-block lang block-start block-end
)
5835 ;; remove old background overlays
5837 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5838 (delete-overlay ov
)))
5839 (overlays-at (/ (+ beg1 block-end
) 2)))
5840 ;; add a background overlay
5841 (setq ovl
(make-overlay beg1 block-end
))
5842 (overlay-put ovl
'face
'org-block-background
)
5843 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5845 (add-text-properties beg1
(min (point-max) (1+ end1
))
5846 '(face org-block
))) ; end of source block
5847 ((not org-fontify-quote-and-verse-blocks
))
5848 ((string= block-type
"quote")
5849 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5850 ((string= block-type
"verse")
5851 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5852 (add-text-properties beg beg1
'(face org-block-begin-line
))
5853 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5854 '(face org-block-end-line
))
5856 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5857 (add-text-properties
5859 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5860 '(font-lock-fontified t invisible t
)
5861 '(font-lock-fontified t face org-document-info-keyword
)))
5862 (add-text-properties
5863 (match-beginning 6) (min (point-max) (1+ (match-end 6)))
5864 (if (string-equal dc1
"+title:")
5865 '(font-lock-fontified t face org-document-title
)
5866 '(font-lock-fontified t face org-document-info
))))
5867 ((or (equal dc1
"+results")
5868 (member dc1
'("+begin:" "+end:" "+caption:" "+label:"
5869 "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
5870 "+call:" "+header:" "+headers:" "+name:"))
5871 (and (match-end 4) (equal dc3
"+attr")))
5872 (add-text-properties
5874 '(font-lock-fontified t face org-meta-line
))
5876 ((member dc3
'(" " ""))
5877 (add-text-properties
5879 '(font-lock-fontified t face font-lock-comment-face
)))
5880 ((not (member (char-after beg
) '(?\ ?
\t)))
5881 ;; just any other in-buffer setting, but not indented
5882 (add-text-properties
5884 '(font-lock-fontified t face org-meta-line
))
5888 (defun org-activate-angle-links (limit)
5889 "Run through the buffer and add overlays to links."
5890 (if (and (re-search-forward org-angle-link-re limit t
)
5891 (not (org-in-src-block-p)))
5893 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5894 (add-text-properties (match-beginning 0) (match-end 0)
5895 (list 'mouse-face
'highlight
5896 'keymap org-mouse-map
))
5897 (org-rear-nonsticky-at (match-end 0))
5900 (defun org-activate-footnote-links (limit)
5901 "Run through the buffer and add overlays to footnotes."
5902 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5904 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5905 (org-remove-flyspell-overlays-in beg end
)
5906 (add-text-properties beg end
5907 (list 'mouse-face
'highlight
5908 'keymap org-mouse-map
5910 (if (= (point-at-bol) beg
)
5911 "Footnote definition"
5912 "Footnote reference")
5913 'font-lock-fontified t
5914 'font-lock-multiline t
5915 'face
'org-footnote
))))))
5917 (defun org-activate-bracket-links (limit)
5918 "Run through the buffer and add overlays to bracketed links."
5919 (if (and (re-search-forward org-bracket-link-regexp limit t
)
5920 (not (org-in-src-block-p)))
5921 (let* ((hl (org-match-string-no-properties 1))
5922 (help (concat "LINK: " (save-match-data (org-link-unescape hl
))))
5923 (ip (org-maybe-intangible
5924 (list 'invisible
'org-link
5925 'keymap org-mouse-map
'mouse-face
'highlight
5926 'font-lock-multiline t
'help-echo help
5927 'htmlize-link
`(:uri
,hl
))))
5928 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5929 'font-lock-multiline t
'help-echo help
5930 'htmlize-link
`(:uri
,hl
))))
5931 ;; We need to remove the invisible property here. Table narrowing
5932 ;; may have made some of this invisible.
5933 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5934 (remove-text-properties (match-beginning 0) (match-end 0)
5938 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5939 (org-rear-nonsticky-at (match-beginning 3))
5940 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5941 (org-rear-nonsticky-at (match-end 3))
5942 (add-text-properties (match-end 3) (match-end 0) ip
)
5943 (org-rear-nonsticky-at (match-end 0)))
5944 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5945 (org-rear-nonsticky-at (match-beginning 1))
5946 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5947 (org-rear-nonsticky-at (match-end 1))
5948 (add-text-properties (match-end 1) (match-end 0) ip
)
5949 (org-rear-nonsticky-at (match-end 0)))
5952 (defun org-activate-dates (limit)
5953 "Run through the buffer and add overlays to dates."
5954 (if (and (re-search-forward org-tsr-regexp-both limit t
)
5955 (not (equal (char-before (match-beginning 0)) 91)))
5957 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5958 (add-text-properties (match-beginning 0) (match-end 0)
5959 (list 'mouse-face
'highlight
5960 'keymap org-mouse-map
))
5961 (org-rear-nonsticky-at (match-end 0))
5962 (when org-display-custom-times
5964 (org-display-custom-time (match-beginning 3) (match-end 3)))
5965 (org-display-custom-time (match-beginning 1) (match-end 1)))
5968 (defvar org-target-link-regexp nil
5969 "Regular expression matching radio targets in plain text.")
5970 (make-variable-buffer-local 'org-target-link-regexp
)
5971 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5972 "Regular expression matching a link target.")
5973 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5974 "Regular expression matching a radio target.")
5975 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5976 "Regular expression matching any target.")
5978 (defun org-activate-target-links (limit)
5979 "Run through the buffer and add overlays to target matches."
5980 (when org-target-link-regexp
5981 (let ((case-fold-search t
))
5982 (if (re-search-forward org-target-link-regexp limit t
)
5984 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5985 (add-text-properties (match-beginning 0) (match-end 0)
5986 (list 'mouse-face
'highlight
5987 'keymap org-mouse-map
5988 'help-echo
"Radio target link"
5989 'org-linked-text t
))
5990 (org-rear-nonsticky-at (match-end 0))
5993 (defun org-update-radio-target-regexp ()
5994 "Find all radio targets in this file and update the regular expression."
5996 (when (memq 'radio org-activate-links
)
5997 (setq org-target-link-regexp
5998 (org-make-target-link-regexp (org-all-targets 'radio
)))
5999 (org-restart-font-lock)))
6001 (defun org-hide-wide-columns (limit)
6003 (setq s
(text-property-any (point) (or limit
(point-max))
6006 (setq e
(next-single-property-change s
'org-cwidth
))
6007 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
6011 (defvar org-latex-and-related-regexp nil
6012 "Regular expression for highlighting LaTeX, entities and sub/superscript.")
6013 (defvar org-match-substring-regexp
)
6014 (defvar org-match-substring-with-braces-regexp
)
6016 (defun org-compute-latex-and-related-regexp ()
6017 "Compute regular expression for LaTeX, entities and sub/superscript.
6018 Result depends on variable `org-highlight-latex-and-related'."
6020 'org-latex-and-related-regexp
6022 (cond ((not (memq 'script org-highlight-latex-and-related
)) nil
)
6023 ((eq org-use-sub-superscripts
'{})
6024 (list org-match-substring-with-braces-regexp
))
6025 (org-use-sub-superscripts (list org-match-substring-regexp
))))
6027 (when (memq 'latex org-highlight-latex-and-related
)
6028 (let ((matchers (plist-get org-format-latex-options
:matchers
)))
6031 (and (member (car x
) matchers
) (nth 1 x
)))
6032 org-latex-regexps
)))))
6034 (when (memq 'entities org-highlight-latex-and-related
)
6035 (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
6036 (mapconcat 'identity
(append re-latex re-entities re-sub
) "\\|"))))
6038 (defun org-do-latex-and-related (limit)
6039 "Highlight LaTeX snippets and environments, entities and sub/superscript.
6040 LIMIT bounds the search for syntax to highlight. Stop at first
6041 highlighted object, if any. Return t if some highlighting was
6042 done, nil otherwise."
6043 (when (org-string-nw-p org-latex-and-related-regexp
)
6045 (while (re-search-forward org-latex-and-related-regexp limit t
)
6046 (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
6048 '(org-code org-verbatim underline
))
6049 (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
6053 (font-lock-prepend-text-property
6054 (+ offset
(match-beginning 0)) (match-end 0)
6055 'face
'org-latex-and-related
)
6056 (add-text-properties (+ offset
(match-beginning 0)) (match-end 0)
6057 '(font-lock-multiline t
)))
6061 (defun org-restart-font-lock ()
6062 "Restart `font-lock-mode', to force refontification."
6063 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
6065 (font-lock-mode 1)))
6067 (defun org-all-targets (&optional radio
)
6068 "Return a list of all targets in this file.
6069 When optional argument RADIO is non-nil, only find radio
6071 (let ((re (if radio org-radio-target-regexp org-target-regexp
)) rtn
)
6073 (goto-char (point-min))
6074 (while (re-search-forward re nil t
)
6075 ;; Make sure point is really within the object.
6077 (let ((obj (org-element-context)))
6078 (when (memq (org-element-type obj
) '(radio-target target
))
6079 (add-to-list 'rtn
(downcase (org-element-property :value obj
))))))
6082 (defun org-make-target-link-regexp (targets)
6083 "Make regular expression matching all strings in TARGETS.
6084 The regular expression finds the targets also if there is a line break
6091 (setq x
(regexp-quote x
))
6092 (while (string-match " +" x
)
6093 (setq x
(replace-match "\\s-+" t t x
)))
6099 (defun org-activate-tags (limit)
6100 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
6102 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
6103 (add-text-properties (match-beginning 1) (match-end 1)
6104 (list 'mouse-face
'highlight
6105 'keymap org-mouse-map
))
6106 (org-rear-nonsticky-at (match-end 1))
6109 (defun org-outline-level ()
6110 "Compute the outline level of the heading at point.
6111 If this is called at a normal headline, the level is the number of stars.
6112 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
6114 (if (not (condition-case nil
6115 (org-back-to-heading t
)
6118 (looking-at org-outline-regexp
)
6119 (1- (- (match-end 0) (match-beginning 0))))))
6121 (defvar org-font-lock-keywords nil
)
6123 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
6124 "Regular expression matching a property line.")
6126 (defvar org-font-lock-hook nil
6127 "Functions to be called for special font lock stuff.")
6129 (defvar org-font-lock-set-keywords-hook nil
6130 "Functions that can manipulate `org-font-lock-extra-keywords'.
6131 This is called after `org-font-lock-extra-keywords' is defined, but before
6132 it is installed to be used by font lock. This can be useful if something
6133 needs to be inserted at a specific position in the font-lock sequence.")
6135 (defun org-font-lock-hook (limit)
6136 "Run `org-font-lock-hook' within LIMIT."
6137 (run-hook-with-args 'org-font-lock-hook limit
))
6139 (defun org-set-font-lock-defaults ()
6140 "Set font lock defaults for the current buffer."
6141 (let* ((em org-fontify-emphasized-text
)
6142 (lk org-activate-links
)
6143 (org-font-lock-extra-keywords
6146 '(org-font-lock-hook)
6148 `(,(if org-fontify-whole-heading-line
6149 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
6150 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
6151 (1 (org-get-level-face 1))
6152 (2 (org-get-level-face 2))
6153 (3 (org-get-level-face 3)))
6155 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
6158 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
6159 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
6160 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
6161 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
6163 (list org-drawer-regexp
'(0 'org-special-keyword t
))
6164 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
6166 (list org-property-re
6167 '(1 'org-special-keyword t
)
6168 '(3 'org-property-value t
))
6170 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
6171 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
6172 (if (memq 'plain lk
) '(org-activate-plain-links (0 'org-link t
)))
6173 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
6174 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
6175 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
6176 (if (memq 'footnote lk
) '(org-activate-footnote-links))
6178 (list org-any-target-regexp
'(0 'org-target t
))
6180 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
6182 '("{{{.+}}}" (0 'org-macro t
))
6183 '(org-hide-wide-columns (0 nil append
))
6185 (list (format org-heading-keyword-regexp-format
6187 '(2 (org-get-todo-face 2) t
))
6189 (if org-fontify-done-headline
6190 (list (format org-heading-keyword-regexp-format
6193 (mapconcat 'regexp-quote org-done-keywords
"\\|")
6195 '(2 'org-headline-done t
))
6198 '(org-font-lock-add-priority-faces)
6200 '(org-font-lock-add-tag-faces)
6202 (if (and org-group-tags org-tag-groups-alist
)
6203 (list (concat org-outline-regexp-bol
".+\\(:"
6204 (regexp-opt (mapcar 'car org-tag-groups-alist
))
6206 '(1 'org-tag-group prepend
)))
6208 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
6209 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
6210 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
6211 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
6214 (if (featurep 'xemacs
)
6215 '(org-do-emphasis-faces (0 nil append
))
6216 '(org-do-emphasis-faces)))
6218 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
6219 1 'org-checkbox prepend
)
6220 (if (cdr (assq 'checkbox org-list-automatic-rules
))
6221 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
6222 (0 (org-get-checkbox-statistics-face) t
)))
6223 ;; Description list items
6224 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
6225 1 'org-list-dt prepend
)
6226 ;; ARCHIVEd headings
6228 org-outline-regexp-bol
6229 "\\(.*:" org-archive-tag
":.*\\)")
6230 '(1 'org-archived prepend
))
6232 '(org-do-latex-and-related)
6233 '(org-fontify-entities)
6234 '(org-raise-scripts)
6236 '(org-activate-code (1 'org-code t
))
6238 (list (format org-heading-keyword-regexp-format
6240 org-comment-string
"\\|" org-quote-string
6242 '(2 'org-special-keyword t
))
6243 ;; Blocks and meta lines
6244 '(org-fontify-meta-lines-and-blocks))))
6245 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
6246 (run-hooks 'org-font-lock-set-keywords-hook
)
6247 ;; Now set the full font-lock-keywords
6248 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
6249 (org-set-local 'font-lock-defaults
6250 '(org-font-lock-keywords t nil nil backward-paragraph
))
6251 (kill-local-variable 'font-lock-keywords
) nil
))
6253 (defun org-toggle-pretty-entities ()
6254 "Toggle the composition display of entities as UTF8 characters."
6256 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
6257 (org-restart-font-lock)
6258 (if org-pretty-entities
6259 (message "Entities are now displayed as UTF8 characters")
6262 (org-decompose-region (point-min) (point-max))
6263 (message "Entities are now displayed as plain text"))))
6265 (defvar org-custom-properties-overlays nil
6266 "List of overlays used for custom properties.")
6267 (make-variable-buffer-local 'org-custom-properties-overlays
)
6269 (defun org-toggle-custom-properties-visibility ()
6270 "Display or hide properties in `org-custom-properties'."
6272 (if org-custom-properties-overlays
6273 (progn (mapc 'delete-overlay org-custom-properties-overlays
)
6274 (setq org-custom-properties-overlays nil
))
6275 (unless (not org-custom-properties
)
6279 (goto-char (point-min))
6280 (while (re-search-forward org-property-re nil t
)
6282 (when (equal p
(substring (match-string 1) 1 -
1))
6283 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
6284 (overlay-put o
'invisible t
)
6285 (overlay-put o
'org-custom-property t
)
6286 (push o org-custom-properties-overlays
))))
6287 org-custom-properties
)))))))
6289 (defun org-fontify-entities (limit)
6290 "Find an entity to fontify."
6292 (when org-pretty-entities
6294 (while (re-search-forward
6295 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
6297 (if (and (not (org-in-indented-comment-line))
6298 (setq ee
(org-entity-get (match-string 1)))
6299 (= (length (nth 6 ee
)) 1))
6301 ((end (if (equal (match-string 2) "{}")
6304 (add-text-properties
6305 (match-beginning 0) end
6306 (list 'font-lock-fontified t
))
6307 (compose-region (match-beginning 0) end
6313 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
6314 "Fontify string S like in Org-mode."
6317 (let ((org-odd-levels-only odd-levels
))
6319 (font-lock-fontify-buffer)
6325 (defun org-get-level-face (n)
6326 "Get the right face for match N in font-lock matching of headlines."
6327 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
6328 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
6329 (if org-cycle-level-faces
6330 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
6331 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
6333 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
6335 (t (if org-level-color-stars-only nil org-f
))))
6338 (defun org-get-todo-face (kwd)
6339 "Get the right face for a TODO keyword KWD.
6340 If KWD is a number, get the corresponding match group."
6341 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6342 (or (org-face-from-face-or-color
6343 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
6344 (and (member kwd org-done-keywords
) 'org-done
)
6347 (defun org-face-from-face-or-color (context inherit face-or-color
)
6348 "Create a face list that inherits INHERIT, but sets the foreground color.
6349 When FACE-OR-COLOR is not a string, just return it."
6350 (if (stringp face-or-color
)
6351 (list :inherit inherit
6352 (cdr (assoc context org-faces-easy-properties
))
6356 (defun org-font-lock-add-tag-faces (limit)
6357 "Add the special tag faces."
6358 (when (and org-tag-faces org-tags-special-faces-re
)
6359 (while (re-search-forward org-tags-special-faces-re limit t
)
6360 (add-text-properties (match-beginning 1) (match-end 1)
6361 (list 'face
(org-get-tag-face 1)
6362 'font-lock-fontified t
))
6363 (backward-char 1))))
6365 (defun org-font-lock-add-priority-faces (limit)
6366 "Add the special priority faces."
6367 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
6368 (when (save-match-data (org-at-heading-p))
6369 (add-text-properties
6370 (match-beginning 0) (match-end 0)
6371 (list 'face
(or (org-face-from-face-or-color
6372 'priority
'org-priority
6373 (cdr (assoc (char-after (match-beginning 1))
6374 org-priority-faces
)))
6376 'font-lock-fontified t
)))))
6378 (defun org-get-tag-face (kwd)
6379 "Get the right face for a TODO keyword KWD.
6380 If KWD is a number, get the corresponding match group."
6381 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6382 (or (org-face-from-face-or-color
6383 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
6386 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
6387 "Remove fontification and activation overlays from links."
6388 (font-lock-default-unfontify-region beg end
)
6389 (let* ((buffer-undo-list t
)
6390 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
6391 (inhibit-modification-hooks t
)
6392 deactivate-mark buffer-file-name buffer-file-truename
)
6393 (org-decompose-region beg end
)
6394 (remove-text-properties beg end
6395 '(mouse-face t keymap t org-linked-text t
6396 invisible t intangible t
6397 org-no-flyspell t org-emphasis t
))
6398 (org-remove-font-lock-display-properties beg end
)))
6400 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
6401 ((raise 0.3) (height 0.7))
6404 "Display properties for showing superscripts and subscripts.")
6406 (defun org-remove-font-lock-display-properties (beg end
)
6407 "Remove specific display properties that have been added by font lock.
6408 The will remove the raise properties that are used to show superscripts
6412 (setq next
(next-single-property-change beg
'display nil end
)
6413 prop
(get-text-property beg
'display
))
6414 (if (member prop org-script-display
)
6415 (put-text-property beg next
'display nil
))
6418 (defun org-raise-scripts (limit)
6419 "Add raise properties to sub/superscripts."
6420 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6421 (if (re-search-forward
6422 (if (eq org-use-sub-superscripts t
)
6423 org-match-substring-regexp
6424 org-match-substring-with-braces-regexp
)
6426 (let* ((pos (point)) table-p comment-p
6427 (mpos (match-beginning 3))
6428 (emph-p (get-text-property mpos
'org-emphasis
))
6429 (link-p (get-text-property mpos
'mouse-face
))
6430 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6431 (goto-char (point-at-bol))
6432 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6433 comment-p
(org-looking-at-p "^[ \t]*#[ +]"))
6436 (if (member (char-after) '(?_ ?^
)) (goto-char (1- pos
)))
6437 (if (or comment-p emph-p link-p keyw-p
)
6439 (put-text-property (match-beginning 3) (match-end 0)
6441 (if (equal (char-after (match-beginning 2)) ?^
)
6442 (nth (if table-p
3 1) org-script-display
)
6443 (nth (if table-p
2 0) org-script-display
)))
6444 (add-text-properties (match-beginning 2) (match-end 2)
6446 'org-dwidth t
'org-dwidth-n
1))
6447 (if (and (eq (char-after (match-beginning 3)) ?
{)
6448 (eq (char-before (match-end 3)) ?
}))
6450 (add-text-properties
6451 (match-beginning 3) (1+ (match-beginning 3))
6452 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6453 (add-text-properties
6454 (1- (match-end 3)) (match-end 3)
6455 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6458 ;;;; Visibility cycling, including org-goto and indirect buffer
6462 (defvar org-cycle-global-status nil
)
6463 (make-variable-buffer-local 'org-cycle-global-status
)
6464 (put 'org-cycle-global-status
'org-state t
)
6465 (defvar org-cycle-subtree-status nil
)
6466 (make-variable-buffer-local 'org-cycle-subtree-status
)
6467 (put 'org-cycle-subtree-status
'org-state t
)
6469 (defvar org-inlinetask-min-level
)
6472 (defun org-cycle (&optional arg
)
6473 "TAB-action and visibility cycling for Org-mode.
6475 This is the command invoked in Org-mode by the TAB key. Its main purpose
6476 is outline visibility cycling, but it also invokes other actions
6477 in special contexts.
6479 - When this function is called with a prefix argument, rotate the entire
6480 buffer through 3 states (global cycling)
6481 1. OVERVIEW: Show only top-level headlines.
6482 2. CONTENTS: Show all headlines of all levels, but no body text.
6483 3. SHOW ALL: Show everything.
6484 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6485 determined by the variable `org-startup-folded', and by any VISIBILITY
6486 properties in the buffer.
6487 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6488 including any drawers.
6490 - When inside a table, re-align the table and move to the next field.
6492 - When point is at the beginning of a headline, rotate the subtree started
6493 by this line through 3 different states (local cycling)
6494 1. FOLDED: Only the main headline is shown.
6495 2. CHILDREN: The main headline and the direct children are shown.
6496 From this state, you can move to one of the children
6497 and zoom in further.
6498 3. SUBTREE: Show the entire subtree, including body text.
6499 If there is no subtree, switch directly from CHILDREN to FOLDED.
6501 - When point is at the beginning of an empty headline and the variable
6502 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6503 of the headline by demoting and promoting it to likely levels. This
6504 speeds up creation document structure by pressing TAB once or several
6505 times right after creating a new headline.
6507 - When there is a numeric prefix, go up to a heading with level ARG, do
6508 a `show-subtree' and return to the previous cursor position. If ARG
6509 is negative, go up that many levels.
6511 - When point is not at the beginning of a headline, execute the global
6512 binding for TAB, which is re-indenting the line. See the option
6513 `org-cycle-emulate-tab' for details.
6515 - Special case: if point is at the beginning of the buffer and there is
6516 no headline in line 1, this function will act as if called with prefix arg
6517 (C-u TAB, same as S-TAB) also when called without prefix arg.
6518 But only if also the variable `org-cycle-global-at-bob' is t."
6520 (org-load-modules-maybe)
6521 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6522 (and org-cycle-level-after-item
/entry-creation
6523 (or (org-cycle-level)
6524 (org-cycle-item-indentation))))
6525 (let* (message-log-max ; Don't populate the *Messages* buffer
6527 (or org-cycle-max-level
6528 (and (boundp 'org-inlinetask-min-level
)
6529 org-inlinetask-min-level
6530 (1- org-inlinetask-min-level
))))
6531 (nstars (and limit-level
6532 (if org-odd-levels-only
6533 (and limit-level
(1- (* limit-level
2)))
6536 (if (not (derived-mode-p 'org-mode
))
6538 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6539 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6540 (not (looking-at org-outline-regexp
))))
6543 (delq 'org-optimize-window-after-visibility-change
6544 (copy-sequence org-cycle-hook
))
6548 (if (or bob-special
(equal arg
'(4)))
6549 ;; special case: use global cycling
6555 (setq last-command
'dummy
)
6556 (org-set-startup-visibility)
6557 (message "Startup visibility, plus VISIBILITY properties"))
6561 (message "Entire buffer visible, including drawers"))
6563 ;; Table: enter it or move to the next field.
6564 ((org-at-table-p 'any
)
6565 (if (org-at-table.el-p
)
6566 (message "Use C-c ' to edit table.el tables")
6567 (if arg
(org-table-edit-field t
)
6568 (org-table-justify-field-maybe)
6569 (call-interactively 'org-table-next-field
))))
6571 ((run-hook-with-args-until-success
6572 'org-tab-after-check-for-table-hook
))
6574 ;; Global cycling: delegate to `org-cycle-internal-global'.
6575 ((eq arg t
) (org-cycle-internal-global))
6577 ;; Drawers: delegate to `org-flag-drawer'.
6578 ((and org-drawers org-drawer-regexp
6580 (beginning-of-line 1)
6581 (looking-at org-drawer-regexp
)))
6582 (org-flag-drawer ; toggle block visibility
6583 (not (get-char-property (match-end 0) 'invisible
))))
6585 ;; Show-subtree, ARG levels up from here.
6588 (org-back-to-heading)
6589 (outline-up-heading (if (< arg
0) (- arg
)
6590 (- (funcall outline-level
) arg
)))
6591 (org-show-subtree)))
6593 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6594 ((and (featurep 'org-inlinetask
)
6595 (org-inlinetask-at-task-p)
6596 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6597 (org-inlinetask-toggle-visibility))
6599 ((org-try-cdlatex-tab))
6601 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6602 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6603 (save-excursion (beginning-of-line 1)
6604 (looking-at org-outline-regexp
)))
6605 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6606 (org-cycle-internal-local))
6608 ;; From there: TAB emulation and template completion.
6609 (buffer-read-only (org-back-to-heading))
6611 ((run-hook-with-args-until-success
6612 'org-tab-after-check-for-cycling-hook
))
6614 ((org-try-structure-completion))
6616 ((run-hook-with-args-until-success
6617 'org-tab-before-tab-emulation-hook
))
6619 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6621 (not (looking-at org-outline-regexp
))))
6622 (call-interactively (global-key-binding "\t")))
6624 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6625 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6626 (or (and (eq org-cycle-emulate-tab
'white
)
6627 (= (match-end 0) (point-at-eol)))
6628 (and (eq org-cycle-emulate-tab
'whitestart
)
6629 (>= (match-end 0) pos
))))
6631 (eq org-cycle-emulate-tab t
))
6632 (call-interactively (global-key-binding "\t")))
6635 (org-back-to-heading)
6638 (defun org-cycle-internal-global ()
6639 "Do the global cycling action."
6640 ;; Hack to avoid display of messages for .org attachments in Gnus
6641 (let (message-log-max ; Don't populate the *Messages* buffer
6642 (ga (string-match "\\*fontification" (buffer-name))))
6644 ((and (eq last-command this-command
)
6645 (eq org-cycle-global-status
'overview
))
6646 ;; We just created the overview - now do table of contents
6647 ;; This can be slow in very large buffers, so indicate action
6648 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6649 (unless ga
(message "CONTENTS..."))
6651 (unless ga
(message "CONTENTS...done"))
6652 (setq org-cycle-global-status
'contents
)
6653 (run-hook-with-args 'org-cycle-hook
'contents
))
6655 ((and (eq last-command this-command
)
6656 (eq org-cycle-global-status
'contents
))
6657 ;; We just showed the table of contents - now show everything
6658 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6660 (unless ga
(message "SHOW ALL"))
6661 (setq org-cycle-global-status
'all
)
6662 (run-hook-with-args 'org-cycle-hook
'all
))
6665 ;; Default action: go to overview
6666 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6668 (unless ga
(message "OVERVIEW"))
6669 (setq org-cycle-global-status
'overview
)
6670 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6672 (defun org-cycle-internal-local ()
6673 "Do the local cycling action."
6674 (let (message-log-max ; Don't populate the *Messages* buffer
6675 (goal-column 0) eoh eol eos has-children children-skipped struct
)
6676 ;; First, determine end of headline (EOH), end of subtree or item
6677 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6682 (setq struct
(org-list-struct))
6683 (setq eoh
(point-at-eol))
6684 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6685 (setq has-children
(org-list-has-child-p (point) struct
)))
6686 (org-back-to-heading)
6687 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6688 (setq eos
(save-excursion (1- (org-end-of-subtree t t
))))
6691 (let ((level (funcall outline-level
)))
6692 (outline-next-heading)
6693 (and (org-at-heading-p t
)
6694 (> (funcall outline-level
) level
))))
6696 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6697 ;; Determine end invisible part of buffer (EOL)
6698 (beginning-of-line 2)
6699 ;; XEmacs doesn't have `next-single-char-property-change'
6700 (if (featurep 'xemacs
)
6701 (while (and (not (eobp)) ;; this is like `next-line'
6702 (get-char-property (1- (point)) 'invisible
))
6703 (beginning-of-line 2))
6704 (while (and (not (eobp)) ;; this is like `next-line'
6705 (get-char-property (1- (point)) 'invisible
))
6706 (goto-char (next-single-char-property-change (point) 'invisible
))
6707 (and (eolp) (beginning-of-line 2))))
6709 ;; Find out what to do next and set `this-command'
6712 ;; Nothing is hidden behind this heading
6713 (unless (org-before-first-heading-p)
6714 (run-hook-with-args 'org-pre-cycle-hook
'empty
))
6715 (message "EMPTY ENTRY")
6716 (setq org-cycle-subtree-status nil
)
6719 (outline-next-heading)
6720 (if (outline-invisible-p) (org-flag-heading nil
))))
6721 ((and (or (>= eol eos
)
6722 (not (string-match "\\S-" (buffer-substring eol eos
))))
6724 (not (setq children-skipped
6725 org-cycle-skip-children-state-if-no-children
))))
6726 ;; Entire subtree is hidden in one line: children view
6727 (unless (org-before-first-heading-p)
6728 (run-hook-with-args 'org-pre-cycle-hook
'children
))
6730 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6732 (org-with-limited-levels (show-children))
6733 ;; FIXME: This slows down the func way too much.
6734 ;; How keep drawers hidden in subtree anyway?
6735 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6736 ;; (org-cycle-hide-drawers 'subtree))
6738 ;; Fold every list in subtree to top-level items.
6739 (when (eq org-cycle-include-plain-lists
'integrate
)
6741 (org-back-to-heading)
6742 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6743 (beginning-of-line 1)
6744 (let* ((struct (org-list-struct))
6745 (prevs (org-list-prevs-alist struct
))
6746 (end (org-list-get-bottom-point struct
)))
6747 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6748 (org-list-get-all-items (point) struct prevs
))
6749 (goto-char (if (< end eos
) end eos
)))))))
6750 (message "CHILDREN")
6753 (outline-next-heading)
6754 (if (outline-invisible-p) (org-flag-heading nil
)))
6755 (setq org-cycle-subtree-status
'children
)
6756 (unless (org-before-first-heading-p)
6757 (run-hook-with-args 'org-cycle-hook
'children
)))
6758 ((or children-skipped
6759 (and (eq last-command this-command
)
6760 (eq org-cycle-subtree-status
'children
)))
6761 ;; We just showed the children, or no children are there,
6762 ;; now show everything.
6763 (unless (org-before-first-heading-p)
6764 (run-hook-with-args 'org-pre-cycle-hook
'subtree
))
6765 (outline-flag-region eoh eos nil
)
6766 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6767 (setq org-cycle-subtree-status
'subtree
)
6768 (unless (org-before-first-heading-p)
6769 (run-hook-with-args 'org-cycle-hook
'subtree
)))
6771 ;; Default action: hide the subtree.
6772 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6773 (outline-flag-region eoh eos t
)
6775 (setq org-cycle-subtree-status
'folded
)
6776 (unless (org-before-first-heading-p)
6777 (run-hook-with-args 'org-cycle-hook
'folded
))))))
6780 (defun org-global-cycle (&optional arg
)
6781 "Cycle the global visibility. For details see `org-cycle'.
6782 With \\[universal-argument] prefix arg, switch to startup visibility.
6783 With a numeric prefix, show all headlines up to that level."
6785 (let ((org-cycle-include-plain-lists
6786 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6790 (hide-sublevels arg
)
6791 (setq org-cycle-global-status
'contents
))
6793 (org-set-startup-visibility)
6794 (message "Startup visibility, plus VISIBILITY properties."))
6796 (org-cycle '(4))))))
6798 (defun org-set-startup-visibility ()
6799 "Set the visibility required by startup options and properties."
6801 ((eq org-startup-folded t
)
6803 ((eq org-startup-folded
'content
)
6804 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6805 (org-cycle '(4)) (org-cycle '(4)))))
6806 (unless (eq org-startup-folded
'showeverything
)
6807 (if org-hide-block-startup
(org-hide-block-all))
6808 (org-set-visibility-according-to-property 'no-cleanup
)
6809 (org-cycle-hide-archived-subtrees 'all
)
6810 (org-cycle-hide-drawers 'all
)
6811 (org-cycle-show-empty-lines t
)))
6813 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6814 "Switch subtree visibilities according to :VISIBILITY: property."
6816 (let (org-show-entry-below state
)
6818 (goto-char (point-min))
6819 (while (re-search-forward
6820 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6822 (setq state
(match-string 1))
6824 (org-back-to-heading t
)
6828 ((equal state
'("fold" "folded"))
6830 ((equal state
"children")
6831 (org-show-hidden-entry)
6833 ((equal state
"content")
6836 (org-narrow-to-subtree)
6838 ((member state
'("all" "showall"))
6841 (org-cycle-hide-archived-subtrees 'all
)
6842 (org-cycle-hide-drawers 'all
)
6843 (org-cycle-show-empty-lines 'all
)))))
6845 ;; This function uses outline-regexp instead of the more fundamental
6846 ;; org-outline-regexp so that org-cycle-global works outside of Org
6847 ;; buffers, where outline-regexp is needed.
6848 (defun org-overview ()
6849 "Switch to overview mode, showing only top-level headlines.
6850 Really, this shows all headlines with level equal or greater than the level
6851 of the first headline in the buffer. This is important, because if the
6852 first headline is not level one, then (hide-sublevels 1) gives confusing
6855 (let ((l (org-current-line))
6856 (level (save-excursion
6857 (goto-char (point-min))
6858 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6860 (goto-char (match-beginning 0))
6861 (funcall outline-level
))))))
6862 (and level
(hide-sublevels level
))
6866 (defun org-content (&optional arg
)
6867 "Show all headlines in the buffer, like a table of contents.
6868 With numerical argument N, show content up to level N."
6871 ;; Visit all headings and show their offspring
6872 (and (integerp arg
) (org-overview))
6873 (goto-char (point-max))
6875 (while (and (progn (condition-case nil
6876 (outline-previous-visible-heading 1)
6877 (error (goto-char (point-min))))
6879 (looking-at org-outline-regexp
))
6881 (show-children (1- arg
))
6883 (if (bobp) (throw 'exit nil
))))))
6886 (defun org-optimize-window-after-visibility-change (state)
6887 "Adjust the window after a change in outline visibility.
6888 This function is the default value of the hook `org-cycle-hook'."
6889 (when (get-buffer-window (current-buffer))
6891 ((eq state
'content
) nil
)
6892 ((eq state
'all
) nil
)
6893 ((eq state
'folded
) nil
)
6894 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6895 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6897 (defun org-remove-empty-overlays-at (pos)
6898 "Remove outline overlays that do not contain non-white stuff."
6901 (and (eq 'outline
(overlay-get o
'invisible
))
6902 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6904 (delete-overlay o
)))
6907 (defun org-clean-visibility-after-subtree-move ()
6908 "Fix visibility issues after moving a subtree."
6909 ;; First, find a reasonable region to look at:
6910 ;; Start two siblings above, end three below
6911 (let* ((beg (save-excursion
6912 (and (org-get-last-sibling)
6913 (org-get-last-sibling))
6915 (end (save-excursion
6916 (and (org-get-next-sibling)
6917 (org-get-next-sibling)
6918 (org-get-next-sibling))
6919 (if (org-at-heading-p)
6922 (level (looking-at "\\*+"))
6923 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6926 (narrow-to-region beg end
)
6928 ;; Properly fold already folded siblings
6929 (goto-char (point-min))
6930 (while (re-search-forward re nil t
)
6931 (if (and (not (outline-invisible-p))
6933 (goto-char (point-at-eol)) (outline-invisible-p)))
6935 (org-cycle-show-empty-lines 'overview
)
6936 (org-cycle-hide-drawers 'overview
)))))
6938 (defun org-cycle-show-empty-lines (state)
6939 "Show empty lines above all visible headlines.
6940 The region to be covered depends on STATE when called through
6941 `org-cycle-hook'. Lisp program can use t for STATE to get the
6942 entire buffer covered. Note that an empty line is only shown if there
6943 are at least `org-cycle-separator-lines' empty lines before the headline."
6944 (when (not (= org-cycle-separator-lines
0))
6946 (let* ((n (abs org-cycle-separator-lines
))
6948 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6949 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6950 (t (let ((ns (number-to-string (- n
2))))
6951 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6952 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6955 ((memq state
'(overview contents t
))
6956 (setq beg
(point-min) end
(point-max)))
6957 ((memq state
'(children folded
))
6958 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6959 (beginning-of-line 2)
6963 (while (re-search-forward re end t
)
6964 (unless (get-char-property (match-end 1) 'invisible
)
6965 (setq e
(match-end 1))
6966 (if (< org-cycle-separator-lines
0)
6967 (setq b
(save-excursion
6968 (goto-char (match-beginning 0))
6969 (org-back-over-empty-lines)
6971 (goto-char (max (point-min) (1- (point))))
6975 (setq b
(match-beginning 1)))
6976 (outline-flag-region b e nil
)))))))
6977 ;; Never hide empty lines at the end of the file.
6979 (goto-char (point-max))
6980 (outline-previous-heading)
6981 (outline-end-of-heading)
6982 (if (and (looking-at "[ \t\n]+")
6983 (= (match-end 0) (point-max)))
6984 (outline-flag-region (point) (match-end 0) nil
))))
6986 (defun org-show-empty-lines-in-parent ()
6987 "Move to the parent and re-show empty lines before visible headlines."
6989 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6990 (org-cycle-show-empty-lines context
))))
6992 (defun org-files-list ()
6993 "Return `org-agenda-files' list, plus all open org-mode files.
6994 This is useful for operations that need to scan all of a user's
6995 open and agenda-wise Org files."
6996 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6997 (dolist (buf (buffer-list))
6998 (with-current-buffer buf
6999 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
7000 (let ((file (expand-file-name (buffer-file-name))))
7001 (unless (member file files
)
7002 (push file files
))))))
7005 (defsubst org-entry-beginning-position
()
7006 "Return the beginning position of the current entry."
7007 (save-excursion (outline-back-to-heading t
) (point)))
7009 (defsubst org-entry-end-position
()
7010 "Return the end position of the current entry."
7011 (save-excursion (outline-next-heading) (point)))
7013 (defun org-cycle-hide-drawers (state)
7014 "Re-hide all drawers after a visibility state change."
7015 (when (and (derived-mode-p 'org-mode
)
7016 (not (memq state
'(overview folded contents
))))
7018 (let* ((globalp (memq state
'(contents all
)))
7019 (beg (if globalp
(point-min) (point)))
7020 (end (if globalp
(point-max)
7021 (if (eq state
'children
)
7022 (save-excursion (outline-next-heading) (point))
7023 (org-end-of-subtree t
)))))
7025 (while (re-search-forward org-drawer-regexp end t
)
7026 (org-flag-drawer t
))))))
7028 (defun org-cycle-hide-inline-tasks (state)
7029 "Re-hide inline task when switching to 'contents visibility state."
7030 (when (and (eq state
'contents
)
7031 (boundp 'org-inlinetask-min-level
)
7032 org-inlinetask-min-level
)
7033 (hide-sublevels (1- org-inlinetask-min-level
))))
7035 (defun org-flag-drawer (flag)
7036 "When FLAG is non-nil, hide the drawer we are within.
7037 Otherwise make it visible."
7039 (beginning-of-line 1)
7040 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
7041 (let ((b (match-end 0)))
7042 (if (re-search-forward
7044 (save-excursion (outline-next-heading) (point)) t
)
7045 (outline-flag-region b
(point-at-eol) flag
)
7046 (user-error ":END: line missing at position %s" b
))))))
7048 (defun org-subtree-end-visible-p ()
7049 "Is the end of the current subtree visible?"
7050 (pos-visible-in-window-p
7051 (save-excursion (org-end-of-subtree t
) (point))))
7053 (defun org-first-headline-recenter (&optional N
)
7054 "Move cursor to the first headline and recenter the headline.
7055 Optional argument N means put the headline into the Nth line of the window."
7056 (goto-char (point-min))
7057 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
7059 (recenter (prefix-numeric-value N
))))
7061 ;;; Saving and restoring visibility
7063 (defun org-outline-overlay-data (&optional use-markers
)
7064 "Return a list of the locations of all outline overlays.
7065 These are overlays with the `invisible' property value `outline'.
7066 The return value is a list of cons cells, with start and stop
7067 positions for each overlay.
7068 If USE-MARKERS is set, return the positions as markers."
7075 (when (eq (overlay-get o
'invisible
) 'outline
)
7076 (setq beg
(overlay-start o
)
7077 end
(overlay-end o
))
7078 (and beg end
(> end beg
)
7080 (cons (move-marker (make-marker) beg
)
7081 (move-marker (make-marker) end
))
7083 (overlays-in (point-min) (point-max))))))))
7085 (defun org-set-outline-overlay-data (data)
7086 "Create visibility overlays for all positions in DATA.
7087 DATA should have been made by `org-outline-overlay-data'."
7094 (outline-flag-region (car c
) (cdr c
) t
))
7097 ;;; Folding of blocks
7099 (defvar org-hide-block-overlays nil
7100 "Overlays hiding blocks.")
7101 (make-variable-buffer-local 'org-hide-block-overlays
)
7103 (defun org-block-map (function &optional start end
)
7104 "Call FUNCTION at the head of all source blocks in the current buffer.
7105 Optional arguments START and END can be used to limit the range."
7106 (let ((start (or start
(point-min)))
7107 (end (or end
(point-max))))
7110 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
7113 (goto-char (match-beginning 0))
7114 (funcall function
)))))))
7116 (defun org-hide-block-toggle-all ()
7117 "Toggle the visibility of all blocks in the current buffer."
7118 (org-block-map #'org-hide-block-toggle
))
7120 (defun org-hide-block-all ()
7121 "Fold all blocks in the current buffer."
7123 (org-show-block-all)
7124 (org-block-map #'org-hide-block-toggle-maybe
))
7126 (defun org-show-block-all ()
7127 "Unfold all blocks in the current buffer."
7129 (mapc 'delete-overlay org-hide-block-overlays
)
7130 (setq org-hide-block-overlays nil
))
7132 (defun org-hide-block-toggle-maybe ()
7133 "Toggle visibility of block at point."
7135 (let ((case-fold-search t
))
7137 (beginning-of-line 1)
7138 (looking-at org-block-regexp
))
7139 (progn (org-hide-block-toggle)
7140 t
) ;; to signal that we took action
7141 nil
))) ;; to signal that we did not
7143 (defun org-hide-block-toggle (&optional force
)
7144 "Toggle the visibility of the current block."
7148 (if (re-search-forward org-block-regexp nil t
)
7149 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
7150 (end (match-end 0)) ;; end of entire body
7152 (if (memq t
(mapcar (lambda (overlay)
7153 (eq (overlay-get overlay
'invisible
)
7155 (overlays-at start
)))
7156 (if (or (not force
) (eq force
'off
))
7158 (when (member ov org-hide-block-overlays
)
7159 (setq org-hide-block-overlays
7160 (delq ov org-hide-block-overlays
)))
7161 (when (eq (overlay-get ov
'invisible
)
7163 (delete-overlay ov
)))
7164 (overlays-at start
)))
7165 (setq ov
(make-overlay start end
))
7166 (overlay-put ov
'invisible
'org-hide-block
)
7167 ;; make the block accessible to isearch
7169 ov
'isearch-open-invisible
7171 (when (member ov org-hide-block-overlays
)
7172 (setq org-hide-block-overlays
7173 (delq ov org-hide-block-overlays
)))
7174 (when (eq (overlay-get ov
'invisible
)
7176 (delete-overlay ov
))))
7177 (push ov org-hide-block-overlays
)))
7178 (user-error "Not looking at a source block"))))
7180 ;; org-tab-after-check-for-cycling-hook
7181 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
7182 ;; Remove overlays when changing major mode
7183 (add-hook 'org-mode-hook
7184 (lambda () (org-add-hook 'change-major-mode-hook
7185 'org-show-block-all
'append
'local
)))
7189 (defvar org-goto-window-configuration nil
)
7190 (defvar org-goto-marker nil
)
7191 (defvar org-goto-map
)
7192 (defun org-goto-map ()
7193 "Set the keymap `org-goto'."
7195 (let ((map (make-sparse-keymap)))
7196 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
7197 mouse-drag-region universal-argument org-occur
))
7199 (while (setq cmd
(pop cmds
))
7200 (substitute-key-definition cmd cmd map global-map
)))
7201 (suppress-keymap map
)
7202 (org-defkey map
"\C-m" 'org-goto-ret
)
7203 (org-defkey map
[(return)] 'org-goto-ret
)
7204 (org-defkey map
[(left)] 'org-goto-left
)
7205 (org-defkey map
[(right)] 'org-goto-right
)
7206 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
7207 (org-defkey map
"\C-i" 'org-cycle
)
7208 (org-defkey map
[(tab)] 'org-cycle
)
7209 (org-defkey map
[(down)] 'outline-next-visible-heading
)
7210 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
7211 (if org-goto-auto-isearch
7212 (if (fboundp 'define-key-after
)
7213 (define-key-after map
[t] 'org-goto-local-auto-isearch)
7215 (org-defkey map "q" 'org-goto-quit)
7216 (org-defkey map "n" 'outline-next-visible-heading)
7217 (org-defkey map "p" 'outline-previous-visible-heading)
7218 (org-defkey map "f" 'outline-forward-same-level)
7219 (org-defkey map "b" 'outline-backward-same-level)
7220 (org-defkey map "u" 'outline-up-heading))
7221 (org-defkey map "/" 'org-occur)
7222 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
7223 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
7224 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
7225 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
7226 (org-defkey map "\C-c\C-u" 'outline-up-heading)
7229 (defconst org-goto-help
7230 "Browse buffer copy, to find location or copy text.%s
7231 RET=jump to location C-g=quit and return to previous location
7232 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
7234 (defvar org-goto-start-pos) ; dynamically scoped parameter
7236 (defun org-goto (&optional alternative-interface)
7237 "Look up a different location in the current file, keeping current visibility.
7239 When you want look-up or go to a different location in a
7240 document, the fastest way is often to fold the entire buffer and
7241 then dive into the tree. This method has the disadvantage, that
7242 the previous location will be folded, which may not be what you
7245 This command works around this by showing a copy of the current
7246 buffer in an indirect buffer, in overview mode. You can dive
7247 into the tree in that copy, use org-occur and incremental search
7248 to find a location. When pressing RET or `Q', the command
7249 returns to the original buffer in which the visibility is still
7250 unchanged. After RET it will also jump to the location selected
7251 in the indirect buffer and expose the headline hierarchy above.
7253 With a prefix argument, use the alternative interface: e.g. if
7254 `org-goto-interface' is 'outline use 'outline-path-completion."
7257 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
7258 (org-refile-use-outline-path t)
7259 (org-refile-target-verify-function nil)
7261 (if (not alternative-interface)
7263 (if (eq org-goto-interface 'outline)
7264 'outline-path-completion
7266 (org-goto-start-pos (point))
7268 (if (eq interface 'outline)
7269 (car (org-get-location (current-buffer) org-goto-help))
7270 (let ((pa (org-refile-get-location "Goto" nil nil t)))
7271 (org-refile-check-position pa)
7275 (org-mark-ring-push org-goto-start-pos)
7276 (goto-char selected-point)
7277 (if (or (outline-invisible-p) (org-invisible-p2))
7278 (org-show-context 'org-goto)))
7281 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
7282 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
7283 (defvar org-goto-local-auto-isearch-map) ; defined below
7285 (defun org-get-location (buf help)
7286 "Let the user select a location in the Org-mode buffer BUF.
7287 This function uses a recursive edit. It returns the selected position
7290 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
7291 (isearch-hide-immediately nil)
7292 (isearch-search-fun-function
7293 (lambda () 'org-goto-local-search-headings))
7294 (org-goto-selected-point org-goto-exit-command))
7296 (save-window-excursion
7297 (delete-other-windows)
7298 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
7299 (org-pop-to-buffer-same-window
7301 (make-indirect-buffer (current-buffer) "*org-goto*")
7302 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
7303 (with-output-to-temp-buffer "*Org Help*"
7304 (princ (format help (if org-goto-auto-isearch
7305 " Just type for auto-isearch."
7306 " n/p/f/b/u to navigate, q to quit."))))
7307 (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
7308 (setq buffer-read-only nil)
7309 (let ((org-startup-truncated t)
7310 (org-startup-folded nil)
7311 (org-startup-align-all-tables nil))
7314 (setq buffer-read-only t)
7315 (if (and (boundp 'org-goto-start-pos)
7316 (integer-or-marker-p org-goto-start-pos))
7317 (let ((org-show-hierarchy-above t)
7318 (org-show-siblings t)
7319 (org-show-following-heading t))
7320 (goto-char org-goto-start-pos)
7321 (and (outline-invisible-p) (org-show-context)))
7322 (goto-char (point-min)))
7323 (let (org-special-ctrl-a/e) (org-beginning-of-line))
7324 (message "Select location and press RET")
7325 (use-local-map org-goto-map)
7327 (kill-buffer "*org-goto*")
7328 (cons org-goto-selected-point org-goto-exit-command))))
7330 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
7331 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
7332 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
7333 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
7335 (defun org-goto-local-search-headings (string bound noerror)
7336 "Search and make sure that any matches are in headlines."
7338 (while (if isearch-forward
7339 (search-forward string bound noerror)
7340 (search-backward string bound noerror))
7341 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
7342 (and (member :headline context)
7343 (not (member :tags context))))
7344 (throw 'return (point))))))
7346 (defun org-goto-local-auto-isearch ()
7349 (goto-char (point-min))
7350 (let ((keys (this-command-keys)))
7351 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
7353 (isearch-process-search-char (string-to-char keys)))))
7355 (defun org-goto-ret (&optional arg)
7356 "Finish `org-goto' by going to the new location."
7358 (setq org-goto-selected-point (point)
7359 org-goto-exit-command 'return)
7362 (defun org-goto-left ()
7363 "Finish `org-goto' by going to the new location."
7365 (if (org-at-heading-p)
7367 (beginning-of-line 1)
7368 (setq org-goto-selected-point (point)
7369 org-goto-exit-command 'left)
7371 (user-error "Not on a heading")))
7373 (defun org-goto-right ()
7374 "Finish `org-goto' by going to the new location."
7376 (if (org-at-heading-p)
7378 (setq org-goto-selected-point (point)
7379 org-goto-exit-command 'right)
7381 (user-error "Not on a heading")))
7383 (defun org-goto-quit ()
7384 "Finish `org-goto' without cursor motion."
7386 (setq org-goto-selected-point nil)
7387 (setq org-goto-exit-command 'quit)
7390 ;;; Indirect buffer display of subtrees
7392 (defvar org-indirect-dedicated-frame nil
7393 "This is the frame being used for indirect tree display.")
7394 (defvar org-last-indirect-buffer nil)
7396 (defun org-tree-to-indirect-buffer (&optional arg)
7397 "Create indirect buffer and narrow it to current subtree.
7398 With a numerical prefix ARG, go up to this level and then take that tree.
7399 If ARG is negative, go up that many levels.
7401 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7402 indirect buffer previously made with this command, to avoid proliferation of
7403 indirect buffers. However, when you call the command with a \
7404 \\[universal-argument] prefix, or
7405 when `org-indirect-buffer-display' is `new-frame', the last buffer
7406 is kept so that you can work with several indirect buffers at the same time.
7407 If `org-indirect-buffer-display' is `dedicated-frame', the \
7408 \\[universal-argument] prefix also
7409 requests that a new frame be made for the new buffer, so that the dedicated
7410 frame is not changed."
7412 (let ((cbuf (current-buffer))
7413 (cwin (selected-window))
7415 beg end level heading ibuf)
7417 (org-back-to-heading t)
7419 (setq level (org-outline-level))
7420 (if (< arg 0) (setq arg (+ level arg)))
7421 (while (> (setq level (org-outline-level)) arg)
7422 (org-up-heading-safe)))
7424 heading (org-get-heading))
7425 (org-end-of-subtree t t)
7426 (if (org-at-heading-p) (backward-char 1))
7428 (if (and (buffer-live-p org-last-indirect-buffer)
7429 (not (eq org-indirect-buffer-display 'new-frame))
7431 (kill-buffer org-last-indirect-buffer))
7432 (setq ibuf (org-get-indirect-buffer cbuf)
7433 org-last-indirect-buffer ibuf)
7435 ((or (eq org-indirect-buffer-display 'new-frame)
7436 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7437 (select-frame (make-frame))
7438 (delete-other-windows)
7439 (org-pop-to-buffer-same-window ibuf)
7440 (org-set-frame-title heading))
7441 ((eq org-indirect-buffer-display 'dedicated-frame)
7443 (select-frame (or (and org-indirect-dedicated-frame
7444 (frame-live-p org-indirect-dedicated-frame)
7445 org-indirect-dedicated-frame)
7446 (setq org-indirect-dedicated-frame (make-frame)))))
7447 (delete-other-windows)
7448 (org-pop-to-buffer-same-window ibuf)
7449 (org-set-frame-title (concat "Indirect: " heading)))
7450 ((eq org-indirect-buffer-display 'current-window)
7451 (org-pop-to-buffer-same-window ibuf))
7452 ((eq org-indirect-buffer-display 'other-window)
7453 (pop-to-buffer ibuf))
7454 (t (error "Invalid value")))
7455 (if (featurep 'xemacs)
7456 (save-excursion (org-mode) (turn-on-font-lock)))
7457 (narrow-to-region beg end)
7460 (run-hook-with-args 'org-cycle-hook 'all)
7461 (and (window-live-p cwin) (select-window cwin))))
7463 (defun org-get-indirect-buffer (&optional buffer)
7464 (setq buffer (or buffer (current-buffer)))
7465 (let ((n 1) (base (buffer-name buffer)) bname)
7466 (while (buffer-live-p
7467 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7470 (make-indirect-buffer buffer bname 'clone)
7471 (error (make-indirect-buffer buffer bname)))))
7473 (defun org-set-frame-title (title)
7474 "Set the title of the current frame to the string TITLE."
7475 ;; FIXME: how to name a single frame in XEmacs???
7476 (unless (featurep 'xemacs)
7477 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7479 ;;;; Structure editing
7481 ;;; Inserting headlines
7483 (defun org-previous-line-empty-p (&optional next)
7484 "Is the previous line a blank line?
7485 When NEXT is non-nil, check the next line instead."
7488 (or (beginning-of-line (if next 2 0)) t)
7490 (looking-at "[ \t]*$")))))
7492 (defun org-insert-heading (&optional arg invisible-ok)
7493 "Insert a new heading or item with same depth at point.
7494 If point is in a plain list and ARG is nil, create a new list item.
7495 With one universal prefix argument, insert a heading even in lists.
7496 With two universal prefix arguments, insert the heading at the end
7497 of the parent subtree.
7499 If point is at the beginning of a headline, insert a sibling before
7500 the current headline. If point is not at the beginning, split the line
7501 and create a new headline with the text in the current line after point
7502 \(see `org-M-RET-may-split-line' on how to modify this behavior).
7504 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7505 This is important for non-interactive uses of the command."
7507 (if (org-called-interactively-p 'any) (org-reveal))
7509 ((or (= (buffer-size) 0)
7510 (and (not (save-excursion
7511 (and (ignore-errors (org-back-to-heading invisible-ok))
7512 (org-at-heading-p))))
7513 (or arg (not (org-in-item-p)))))
7515 (if (org-previous-line-empty-p) "" "\n")
7516 (if (org-in-src-block-p) ",* " "* "))
7517 (run-hooks 'org-insert-heading-hook))
7519 (and (not (org-in-item-p)) org-insert-heading-respect-content)
7520 (not (org-insert-item
7523 (looking-at org-list-full-item-re)
7524 (match-string 3)))))
7526 (when (org-buffer-narrowed-p)
7527 (setq begn (point-min) endn (point-max))
7529 (let* ((empty-line-p nil)
7530 (eops (equal arg '(16))) ; insert at end of parent subtree
7531 (org-insert-heading-respect-content
7532 (or (not (null arg)) org-insert-heading-respect-content))
7534 (on-heading (org-at-heading-p))
7535 ;; Get a level to fall back on
7538 (org-back-to-heading t)
7539 (looking-at org-outline-regexp)
7540 (make-string (1- (length (match-string 0))) ?*)))
7542 (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
7543 (head (save-excursion
7546 (org-back-to-heading invisible-ok)
7547 (when (and (not on-heading)
7548 (featurep 'org-inlinetask)
7549 (integerp org-inlinetask-min-level)
7550 (>= (length (match-string 0))
7551 org-inlinetask-min-level))
7552 ;; Find a heading level before the inline task
7553 (while (and (setq level (org-up-heading-safe))
7554 (>= level org-inlinetask-min-level)))
7555 (if (org-at-heading-p)
7556 (org-back-to-heading invisible-ok)
7557 (error "This should not happen")))
7558 (unless (and (save-excursion
7560 (org-backward-heading-same-level 1 invisible-ok))
7561 (= (point) (match-beginning 0)))
7562 (not (org-previous-line-empty-p t)))
7563 (setq empty-line-p (org-previous-line-empty-p)))
7565 (error (or fix-level "* ")))))
7566 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7567 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7568 pos hide-previous previous-pos)
7569 (if ;; At the beginning of a heading, open a new line for insertion
7570 (and (bolp) (org-at-heading-p)
7573 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7574 (open-line (if blank 2 1))
7576 (setq previous-pos (point-at-bol))
7578 (setq hide-previous (outline-invisible-p)))
7579 (and org-insert-heading-respect-content
7581 (while (outline-invisible-p)
7583 (org-up-heading-safe))))
7585 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7588 (goto-char (point-at-bol))
7589 (and (looking-at org-complex-heading-regexp)
7591 (> p (match-beginning 4)))))))
7594 ;; Insert a new line, possibly at end of parent subtree
7595 ((and (not arg) (not on-heading) (not on-empty-line)
7596 (not (save-excursion
7597 (beginning-of-line 1)
7598 (or (looking-at org-list-full-item-re)
7599 ;; Don't convert :end: lines to headline
7600 (looking-at "^\\s-*:end:")
7601 (looking-at "^\\s-*#\\+end_?")))))
7602 (beginning-of-line 1))
7603 (org-insert-heading-respect-content
7606 (org-end-of-subtree nil t)
7607 (and (looking-at "^\\*") (backward-char 1))
7608 (while (and (not (bobp))
7609 ;; Don't delete spaces in empty headlines
7610 (not (looking-back org-outline-regexp))
7611 (member (char-before) '(?\ ?\t ?\n)))
7612 (backward-delete-char 1)))
7614 (org-up-heading-safe)
7616 (goto-char (point-max))
7617 (org-end-of-subtree nil t))))
7618 (when (featurep 'org-inlinetask)
7619 (while (and (not (eobp))
7620 (looking-at "\\(\\*+\\)[ \t]+")
7621 (>= (length (match-string 1))
7622 org-inlinetask-min-level))
7623 (org-end-of-subtree nil t)))
7624 (or (bolp) (newline))
7625 (or (org-previous-line-empty-p)
7626 (and blank (newline)))
7627 (if (or empty-line-p eops) (open-line 1)))
7628 ;; Insert a headling containing text after point
7633 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7634 (setq tags (and (match-end 2) (match-string 2)))
7636 (delete-region (match-beginning 1) (match-end 1)))
7637 (setq pos (point-at-bol))
7638 (or split (end-of-line 1))
7639 (delete-horizontal-space)
7640 (if (string-match "\\`\\*+\\'"
7641 (buffer-substring (point-at-bol) (point)))
7643 (newline (if blank 2 1))
7649 (org-set-tags nil 'align))))
7651 (or split (end-of-line 1))
7652 (newline (cond ((and blank (not on-empty-line)) 2)
7654 (on-empty-line 0) (t 1)))))))
7655 (insert head) (just-one-space)
7658 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7659 (when (and org-insert-heading-respect-content hide-previous)
7661 (goto-char previous-pos)
7663 (when (and begn endn)
7664 (narrow-to-region (min (point) begn) (max (point) endn)))
7665 (run-hooks 'org-insert-heading-hook))))))
7667 (defun org-get-heading (&optional no-tags no-todo)
7668 "Return the heading of the current entry, without the stars.
7669 When NO-TAGS is non-nil, don't include tags.
7670 When NO-TODO is non-nil, don't include TODO keywords."
7672 (org-back-to-heading t)
7674 ((and no-tags no-todo)
7675 (looking-at org-complex-heading-regexp)
7678 (looking-at (concat org-outline-regexp
7680 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7683 (looking-at org-todo-line-regexp)
7685 (t (looking-at org-heading-regexp)
7686 (match-string 2)))))
7688 (defvar orgstruct-mode) ; defined below
7690 (defun org-heading-components ()
7691 "Return the components of the current heading.
7692 This is a list with the following elements:
7693 - the level as an integer
7694 - the reduced level, different if `org-odd-levels-only' is set.
7695 - the TODO keyword, or nil
7696 - the priority character, like ?A, or nil if no priority is given
7697 - the headline text itself, or the tags string if no headline text
7698 - the tags string, or nil."
7700 (org-back-to-heading t)
7701 (if (let (case-fold-search)
7705 org-complex-heading-regexp)))
7707 (list (length (match-string 1))
7708 (org-reduced-level (length (match-string 1)))
7713 (list (length (match-string 1))
7714 (org-reduced-level (length (match-string 1)))
7715 (org-match-string-no-properties 2)
7716 (and (match-end 3) (aref (match-string 3) 2))
7717 (org-match-string-no-properties 4)
7718 (org-match-string-no-properties 5))))))
7720 (defun org-get-entry ()
7721 "Get the entry text, after heading, entire subtree."
7723 (org-back-to-heading t)
7724 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7726 (defun org-insert-heading-after-current ()
7727 "Insert a new heading with same level as current, after current subtree."
7729 (org-back-to-heading)
7730 (org-insert-heading)
7731 (org-move-subtree-down)
7734 (defun org-insert-heading-respect-content (&optional arg invisible-ok)
7735 "Insert heading with `org-insert-heading-respect-content' set to t."
7737 (let ((org-insert-heading-respect-content t))
7738 (org-insert-heading arg invisible-ok)))
7740 (defun org-insert-todo-heading-respect-content (&optional force-state)
7741 "Insert TODO heading with `org-insert-heading-respect-content' set to t."
7743 (let ((org-insert-heading-respect-content t))
7744 (org-insert-todo-heading force-state t)))
7746 (defun org-insert-todo-heading (arg &optional force-heading)
7747 "Insert a new heading with the same level and TODO state as current heading.
7748 If the heading has no TODO state, or if the state is DONE, use the first
7749 state (TODO by default). Also one prefix arg, force first state. With two
7750 prefix args, force inserting at the end of the parent subtree."
7752 (when (or force-heading (not (org-insert-item 'checkbox)))
7753 (org-insert-heading (or (and (equal arg '(16)) '(16))
7756 (org-back-to-heading)
7757 (outline-previous-heading)
7758 (looking-at org-todo-line-regexp))
7762 (not (match-beginning 2))
7763 (member (match-string 2) org-done-keywords))
7764 (car org-todo-keywords-1)
7768 (run-hook-with-args-until-success
7769 'org-todo-get-default-hook new-mark-x nil)
7771 (beginning-of-line 1)
7772 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7773 (if org-treat-insert-todo-heading-as-state-change
7775 (insert new-mark " "))))
7776 (when org-provide-todo-statistics
7777 (org-update-parent-todo-statistics))))
7779 (defun org-insert-subheading (arg)
7780 "Insert a new subheading and demote it.
7781 Works for outline headings and for plain lists alike."
7783 (org-insert-heading arg)
7785 ((org-at-heading-p) (org-do-demote))
7786 ((org-at-item-p) (org-indent-item))))
7788 (defun org-insert-todo-subheading (arg)
7789 "Insert a new subheading with TODO keyword or checkbox and demote it.
7790 Works for outline headings and for plain lists alike."
7792 (org-insert-todo-heading arg)
7794 ((org-at-heading-p) (org-do-demote))
7795 ((org-at-item-p) (org-indent-item))))
7797 ;;; Promotion and Demotion
7799 (defvar org-after-demote-entry-hook nil
7800 "Hook run after an entry has been demoted.
7801 The cursor will be at the beginning of the entry.
7802 When a subtree is being demoted, the hook will be called for each node.")
7804 (defvar org-after-promote-entry-hook nil
7805 "Hook run after an entry has been promoted.
7806 The cursor will be at the beginning of the entry.
7807 When a subtree is being promoted, the hook will be called for each node.")
7809 (defun org-promote-subtree ()
7810 "Promote the entire subtree.
7811 See also `org-promote'."
7814 (org-with-limited-levels (org-map-tree 'org-promote)))
7815 (org-fix-position-after-promote))
7817 (defun org-demote-subtree ()
7818 "Demote the entire subtree. See `org-demote'.
7819 See also `org-promote'."
7822 (org-with-limited-levels (org-map-tree 'org-demote)))
7823 (org-fix-position-after-promote))
7826 (defun org-do-promote ()
7827 "Promote the current heading higher up the tree.
7828 If the region is active in `transient-mark-mode', promote all headings
7832 (if (org-region-active-p)
7833 (org-map-region 'org-promote (region-beginning) (region-end))
7835 (org-fix-position-after-promote))
7837 (defun org-do-demote ()
7838 "Demote the current heading lower down the tree.
7839 If the region is active in `transient-mark-mode', demote all headings
7843 (if (org-region-active-p)
7844 (org-map-region 'org-demote (region-beginning) (region-end))
7846 (org-fix-position-after-promote))
7848 (defun org-fix-position-after-promote ()
7849 "Make sure that after pro/demotion cursor position is right."
7850 (let ((pos (point)))
7851 (when (save-excursion
7852 (beginning-of-line 1)
7853 (looking-at org-todo-line-regexp)
7854 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7855 (cond ((eobp) (insert " "))
7856 ((eolp) (insert " "))
7857 ((equal (char-after) ?\ ) (forward-char 1))))))
7859 (defun org-current-level ()
7860 "Return the level of the current entry, or nil if before the first headline.
7861 The level is the number of stars at the beginning of the headline."
7863 (org-with-limited-levels
7864 (if (ignore-errors (org-back-to-heading t))
7865 (funcall outline-level)))))
7867 (defun org-get-previous-line-level ()
7868 "Return the outline depth of the last headline before the current line.
7869 Returns 0 for the first headline in the buffer, and nil if before the
7871 (let ((current-level (org-current-level))
7872 (prev-level (when (> (line-number-at-pos) 1)
7874 (beginning-of-line 0)
7875 (org-current-level)))))
7876 (cond ((null current-level) nil) ; Before first headline
7877 ((null prev-level) 0) ; At first headline
7880 (defun org-reduced-level (l)
7881 "Compute the effective level of a heading.
7882 This takes into account the setting of `org-odd-levels-only'."
7885 (org-odd-levels-only (1+ (floor (/ l 2))))
7888 (defun org-level-increment ()
7889 "Return the number of stars that will be added or removed at a
7890 time to headlines when structure editing, based on the value of
7891 `org-odd-levels-only'."
7892 (if org-odd-levels-only 2 1))
7894 (defun org-get-valid-level (level &optional change)
7895 "Rectify a level change under the influence of `org-odd-levels-only'
7896 LEVEL is a current level, CHANGE is by how much the level should be
7897 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7898 even level numbers will become the next higher odd number."
7899 (if org-odd-levels-only
7900 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7901 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7902 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7903 (max 1 (+ level (or change 0)))))
7905 (if (boundp 'define-obsolete-function-alias)
7906 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7907 (define-obsolete-function-alias 'org-get-legal-level
7908 'org-get-valid-level)
7909 (define-obsolete-function-alias 'org-get-legal-level
7910 'org-get-valid-level "23.1")))
7912 (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
7913 ;; ̀org-with-limited-levels'
7914 (defun org-promote ()
7915 "Promote the current heading higher up the tree.
7916 If the region is active in `transient-mark-mode', promote all headings
7918 (org-back-to-heading t)
7919 (let* ((level (save-match-data (funcall outline-level)))
7920 (after-change-functions (remove 'flyspell-after-change-function
7921 after-change-functions))
7922 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7923 (diff (abs (- level (length up-head) -1))))
7924 (cond ((and (= level 1) org-called-with-limited-levels
7925 org-allow-promoting-top-level-subtree)
7926 (replace-match "# " nil t))
7928 (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
7929 (t (replace-match up-head nil t)))
7930 ;; Fixup tag positioning
7932 (and org-auto-align-tags (org-set-tags nil t))
7933 (if org-adapt-indentation (org-fixup-indentation (- diff))))
7934 (run-hooks 'org-after-promote-entry-hook)))
7936 (defun org-demote ()
7937 "Demote the current heading lower down the tree.
7938 If the region is active in `transient-mark-mode', demote all headings
7940 (org-back-to-heading t)
7941 (let* ((level (save-match-data (funcall outline-level)))
7942 (after-change-functions (remove 'flyspell-after-change-function
7943 after-change-functions))
7944 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7945 (diff (abs (- level (length down-head) -1))))
7946 (replace-match down-head nil t)
7947 ;; Fixup tag positioning
7948 (and org-auto-align-tags (org-set-tags nil t))
7949 (if org-adapt-indentation (org-fixup-indentation diff))
7950 (run-hooks 'org-after-demote-entry-hook)))
7952 (defun org-cycle-level ()
7953 "Cycle the level of an empty headline through possible states.
7954 This goes first to child, then to parent, level, then up the hierarchy.
7955 After top level, it switches back to sibling level."
7957 (let ((org-adapt-indentation nil))
7958 (when (org-point-at-end-of-empty-headline)
7959 (setq this-command 'org-cycle-level) ; Only needed for caching
7960 (let ((cur-level (org-current-level))
7961 (prev-level (org-get-previous-line-level)))
7963 ;; If first headline in file, promote to top-level.
7965 (loop repeat (/ (- cur-level 1) (org-level-increment))
7966 do (org-do-promote)))
7967 ;; If same level as prev, demote one.
7968 ((= prev-level cur-level)
7970 ;; If parent is top-level, promote to top level if not already.
7972 (loop repeat (/ (- cur-level 1) (org-level-increment))
7973 do (org-do-promote)))
7974 ;; If top-level, return to prev-level.
7976 (loop repeat (/ (- prev-level 1) (org-level-increment))
7977 do (org-do-demote)))
7978 ;; If less than prev-level, promote one.
7979 ((< cur-level prev-level)
7981 ;; If deeper than prev-level, promote until higher than
7983 ((> cur-level prev-level)
7984 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7985 do (org-do-promote))))
7988 (defun org-map-tree (fun)
7989 "Call FUN for every heading underneath the current one."
7990 (org-back-to-heading)
7991 (let ((level (funcall outline-level)))
7995 (outline-next-heading)
7996 (> (funcall outline-level) level))
8000 (defun org-map-region (fun beg end)
8001 "Call FUN for every heading between BEG and END."
8002 (let ((org-ignore-region t))
8004 (setq end (copy-marker end))
8006 (if (and (re-search-forward org-outline-regexp-bol nil t)
8010 (outline-next-heading)
8015 (defvar org-property-end-re) ; silence byte-compiler
8016 (defun org-fixup-indentation (diff)
8017 "Change the indentation in the current entry by DIFF.
8018 However, if any line in the current entry has no indentation, or if it
8019 would end up with no indentation after the change, nothing at all is done."
8021 (let ((end (save-excursion (outline-next-heading)
8023 (prohibit (if (> diff 0)
8025 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
8027 (unless (save-excursion (end-of-line 1)
8028 (re-search-forward prohibit end t))
8029 (while (and (< (point) end)
8030 (re-search-forward "^[ \t]+" end t))
8031 (goto-char (match-end 0))
8032 (setq col (current-column))
8033 (if (< diff 0) (replace-match ""))
8034 (org-indent-to-column (+ diff col))))
8035 (move-marker end nil))))
8037 (defun org-convert-to-odd-levels ()
8038 "Convert an org-mode file with all levels allowed to one with odd levels.
8039 This will leave level 1 alone, convert level 2 to level 3, level 3 to
8042 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
8043 (let ((outline-level 'org-outline-level)
8044 (org-odd-levels-only nil) n)
8046 (goto-char (point-min))
8047 (while (re-search-forward "^\\*\\*+ " nil t)
8048 (setq n (- (length (match-string 0)) 2))
8049 (while (>= (setq n (1- n)) 0)
8051 (end-of-line 1))))))
8053 (defun org-convert-to-oddeven-levels ()
8054 "Convert an org-mode file with only odd levels to one with odd/even levels.
8055 This promotes level 3 to level 2, level 5 to level 3 etc. If the
8056 file contains a section with an even level, conversion would
8057 destroy the structure of the file. An error is signaled in this
8060 (goto-char (point-min))
8061 ;; First check if there are no even levels
8062 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
8063 (org-show-context t)
8064 (error "Not all levels are odd in this file. Conversion not possible"))
8065 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
8066 (let ((outline-regexp org-outline-regexp)
8067 (outline-level 'org-outline-level)
8068 (org-odd-levels-only nil) n)
8070 (goto-char (point-min))
8071 (while (re-search-forward "^\\*\\*+ " nil t)
8072 (setq n (/ (1- (length (match-string 0))) 2))
8073 (while (>= (setq n (1- n)) 0)
8075 (end-of-line 1))))))
8077 (defun org-tr-level (n)
8078 "Make N odd if required."
8079 (if org-odd-levels-only (1+ (/ n 2)) n))
8081 ;;; Vertical tree motion, cutting and pasting of subtrees
8083 (defun org-move-subtree-up (&optional arg)
8084 "Move the current subtree up past ARG headlines of the same level."
8086 (org-move-subtree-down (- (prefix-numeric-value arg))))
8088 (defun org-move-subtree-down (&optional arg)
8089 "Move the current subtree down past ARG headlines of the same level."
8091 (setq arg (prefix-numeric-value arg))
8092 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
8093 'org-get-last-sibling))
8094 (ins-point (make-marker))
8096 (col (current-column))
8097 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
8099 (org-back-to-heading)
8102 (setq ne-beg (org-back-over-empty-lines))
8105 (save-excursion (outline-end-of-heading)
8106 (setq folded (outline-invisible-p)))
8107 (outline-end-of-subtree))
8108 (outline-next-heading)
8109 (setq ne-end (org-back-over-empty-lines))
8112 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
8113 ;; include less whitespace
8116 (forward-line (- ne-beg ne-end))
8117 (setq beg (point))))
8118 ;; Find insertion point, with error handling
8120 (or (and (funcall movfunc) (looking-at org-outline-regexp))
8121 (progn (goto-char beg0)
8122 (user-error "Cannot move past superior level or buffer limit")))
8123 (setq cnt (1- cnt)))
8125 ;; Moving forward - still need to move over subtree
8126 (progn (org-end-of-subtree t t)
8128 (org-back-over-empty-lines)
8129 (or (bolp) (newline)))))
8130 (setq ne-ins (org-back-over-empty-lines))
8131 (move-marker ins-point (point))
8132 (setq txt (buffer-substring beg end))
8133 (org-save-markers-in-region beg end)
8134 (delete-region beg end)
8135 (org-remove-empty-overlays-at beg)
8136 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
8137 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
8138 (and (not (bolp)) (looking-at "\n") (forward-char 1))
8139 (let ((bbb (point)))
8140 (insert-before-markers txt)
8141 (org-reinstall-markers-in-region bbb)
8142 (move-marker ins-point bbb))
8143 (or (bolp) (insert "\n"))
8144 (setq ins-end (point))
8145 (goto-char ins-point)
8146 (org-skip-whitespace)
8147 (when (and (< arg 0)
8148 (org-first-sibling-p)
8150 ;; Move whitespace back to beginning
8153 (let ((kill-whole-line t))
8154 (kill-line (- ne-ins ne-beg)) (point)))
8155 (insert (make-string (- ne-ins ne-beg) ?\n)))
8156 (move-marker ins-point nil)
8161 (org-cycle-hide-drawers 'children))
8162 (org-clean-visibility-after-subtree-move)
8163 ;; move back to the initial column we were at
8164 (move-to-column col)))
8166 (defvar org-subtree-clip ""
8167 "Clipboard for cut and paste of subtrees.
8168 This is actually only a copy of the kill, because we use the normal kill
8169 ring. We need it to check if the kill was created by `org-copy-subtree'.")
8171 (defvar org-subtree-clip-folded nil
8172 "Was the last copied subtree folded?
8173 This is used to fold the tree back after pasting.")
8175 (defun org-cut-subtree (&optional n)
8176 "Cut the current subtree into the clipboard.
8177 With prefix arg N, cut this many sequential subtrees.
8178 This is a short-hand for marking the subtree and then cutting it."
8180 (org-copy-subtree n 'cut))
8182 (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
8183 "Cut the current subtree into the clipboard.
8184 With prefix arg N, cut this many sequential subtrees.
8185 This is a short-hand for marking the subtree and then copying it.
8186 If CUT is non-nil, actually cut the subtree.
8187 If FORCE-STORE-MARKERS is non-nil, store the relative locations
8188 of some markers in the region, even if CUT is non-nil. This is
8189 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
8191 (let (beg end folded (beg0 (point)))
8192 (if (org-called-interactively-p 'any)
8193 (org-back-to-heading nil) ; take what looks like a subtree
8194 (org-back-to-heading t)) ; take what is really there
8196 (skip-chars-forward " \t\r\n")
8199 (outline-next-heading)
8200 (save-excursion (outline-end-of-heading)
8201 (setq folded (outline-invisible-p)))
8203 (org-forward-heading-same-level (1- n) t)
8205 (org-end-of-subtree t t)))
8209 (setq org-subtree-clip-folded folded)
8210 (when (or cut force-store-markers)
8211 (org-save-markers-in-region beg end))
8212 (if cut (kill-region beg end) (copy-region-as-kill beg end))
8213 (setq org-subtree-clip (current-kill 0))
8214 (message "%s: Subtree(s) with %d characters"
8215 (if cut "Cut" "Copied")
8216 (length org-subtree-clip)))))
8218 (defun org-paste-subtree (&optional level tree for-yank)
8219 "Paste the clipboard as a subtree, with modification of headline level.
8220 The entire subtree is promoted or demoted in order to match a new headline
8223 If the cursor is at the beginning of a headline, the same level as
8224 that headline is used to paste the tree.
8226 If not, the new level is derived from the *visible* headings
8227 before and after the insertion point, and taken to be the inferior headline
8228 level of the two. So if the previous visible heading is level 3 and the
8229 next is level 4 (or vice versa), level 4 will be used for insertion.
8230 This makes sure that the subtree remains an independent subtree and does
8231 not swallow low level entries.
8233 You can also force a different level, either by using a numeric prefix
8234 argument, or by inserting the heading marker by hand. For example, if the
8235 cursor is after \"*****\", then the tree will be shifted to level 5.
8237 If optional TREE is given, use this text instead of the kill ring.
8239 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
8240 move back over whitespace before inserting, and move point to the end of
8241 the inserted text when done."
8243 (setq tree (or tree (and kill-ring (current-kill 0))))
8244 (unless (org-kill-is-subtree-p tree)
8246 (substitute-command-keys
8247 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
8248 (org-with-limited-levels
8249 (let* ((visp (not (outline-invisible-p)))
8251 (^re_ "\\(\\*+\\)[ \t]*")
8252 (old-level (if (string-match org-outline-regexp-bol txt)
8253 (- (match-end 0) (match-beginning 0) 1)
8255 (force-level (cond (level (prefix-numeric-value level))
8256 ((and (looking-at "[ \t]*$")
8258 "^\\*+$" (buffer-substring
8259 (point-at-bol) (point))))
8260 (- (match-end 1) (match-beginning 1)))
8262 (looking-at org-outline-regexp))
8263 (- (match-end 0) (point) 1))))
8264 (previous-level (save-excursion
8267 (outline-previous-visible-heading 1)
8268 (if (looking-at ^re_)
8269 (- (match-end 0) (match-beginning 0) 1)
8272 (next-level (save-excursion
8275 (or (looking-at org-outline-regexp)
8276 (outline-next-visible-heading 1))
8277 (if (looking-at ^re_)
8278 (- (match-end 0) (match-beginning 0) 1)
8281 (new-level (or force-level (max previous-level next-level)))
8282 (shift (if (or (= old-level -1)
8284 (= old-level new-level))
8286 (- new-level old-level)))
8287 (delta (if (> shift 0) -1 1))
8288 (func (if (> shift 0) 'org-demote 'org-promote))
8289 (org-odd-levels-only nil)
8291 ;; Remove the forced level indicator
8293 (delete-region (point-at-bol) (point)))
8295 (beginning-of-line (if (bolp) 1 2))
8297 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
8298 (insert-before-markers txt)
8299 (unless (string-match "\n\\'" txt) (insert "\n"))
8300 (setq newend (point))
8301 (org-reinstall-markers-in-region beg)
8304 (skip-chars-forward " \t\n\r")
8306 (if (and (outline-invisible-p) visp)
8307 (save-excursion (outline-show-heading)))
8308 ;; Shift if necessary
8311 (narrow-to-region beg end)
8312 (while (not (= shift 0))
8313 (org-map-region func (point-min) (point-max))
8314 (setq shift (+ delta shift)))
8315 (goto-char (point-min))
8316 (setq newend (point-max))))
8317 (when (or (org-called-interactively-p 'interactive) for-yank)
8318 (message "Clipboard pasted as level %d subtree" new-level))
8319 (if (and (not for-yank) ; in this case, org-yank will decide about folding
8321 (eq org-subtree-clip (current-kill 0))
8322 org-subtree-clip-folded)
8323 ;; The tree was folded before it was killed/copied
8325 (and for-yank (goto-char newend)))))
8327 (defun org-kill-is-subtree-p (&optional txt)
8328 "Check if the current kill is an outline subtree, or a set of trees.
8329 Returns nil if kill does not start with a headline, or if the first
8330 headline level is not the largest headline level in the tree.
8331 So this will actually accept several entries of equal levels as well,
8332 which is OK for `org-paste-subtree'.
8333 If optional TXT is given, check this string instead of the current kill."
8334 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
8335 (re (org-get-limited-outline-regexp))
8336 (^re (concat "^" re))
8337 (start-level (and kill
8339 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
8341 (- (match-end 2) (match-beginning 2) 1)))
8342 (start (1+ (or (match-beginning 2) -1))))
8343 (if (not start-level)
8345 nil) ;; does not even start with a heading
8347 (while (setq start (string-match ^re kill (1+ start)))
8348 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
8352 (defvar org-markers-to-move nil
8353 "Markers that should be moved with a cut-and-paste operation.
8354 Those markers are stored together with their positions relative to
8355 the start of the region.")
8357 (defun org-save-markers-in-region (beg end)
8358 "Check markers in region.
8359 If these markers are between BEG and END, record their position relative
8360 to BEG, so that after moving the block of text, we can put the markers back
8362 This function gets called just before an entry or tree gets cut from the
8363 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
8364 called immediately, to move the markers with the entries."
8365 (setq org-markers-to-move nil)
8366 (when (featurep 'org-clock)
8367 (org-clock-save-markers-for-cut-and-paste beg end))
8368 (when (featurep 'org-agenda)
8369 (org-agenda-save-markers-for-cut-and-paste beg end)))
8371 (defun org-check-and-save-marker (marker beg end)
8372 "Check if MARKER is between BEG and END.
8373 If yes, remember the marker and the distance to BEG."
8374 (when (and (marker-buffer marker)
8375 (equal (marker-buffer marker) (current-buffer)))
8376 (if (and (>= marker beg) (< marker end))
8377 (push (cons marker (- marker beg)) org-markers-to-move))))
8379 (defun org-reinstall-markers-in-region (beg)
8380 "Move all remembered markers to their position relative to BEG."
8382 (move-marker (car x) (+ beg (cdr x))))
8383 org-markers-to-move)
8384 (setq org-markers-to-move nil))
8386 (defun org-narrow-to-subtree ()
8387 "Narrow buffer to the current subtree."
8391 (org-with-limited-levels
8393 (progn (org-back-to-heading t) (point))
8394 (progn (org-end-of-subtree t t)
8395 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
8398 (defun org-narrow-to-block ()
8399 "Narrow buffer to the current block."
8401 (let* ((case-fold-search t)
8402 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
8403 "^[ \t]*#\\+end_.*")))
8405 (narrow-to-region (car blockp) (cdr blockp))
8406 (user-error "Not in a block"))))
8409 (defvar org-property-drawer-re))
8411 (defvar org-property-start-re) ;; defined below
8412 (defun org-clone-subtree-with-time-shift (n &optional shift)
8413 "Clone the task (subtree) at point N times.
8414 The clones will be inserted as siblings.
8416 In interactive use, the user will be prompted for the number of
8417 clones to be produced. If the entry has a timestamp, the user
8418 will also be prompted for a time shift, which may be a repeater
8419 as used in time stamps, for example `+3d'. To disable this,
8420 you can call the function with a universal prefix argument.
8422 When a valid repeater is given and the entry contains any time
8423 stamps, the clones will become a sequence in time, with time
8424 stamps in the subtree shifted for each clone produced. If SHIFT
8425 is nil or the empty string, time stamps will be left alone. The
8426 ID property of the original subtree is removed.
8428 If the original subtree did contain time stamps with a repeater,
8429 the following will happen:
8430 - the repeater will be removed in each clone
8431 - an additional clone will be produced, with the current, unshifted
8432 date(s) in the entry.
8433 - the original entry will be placed *after* all the clones, with
8435 - the start days in the repeater in the original entry will be shifted
8436 to past the last clone.
8437 In this way you can spell out a number of instances of a repeating task,
8438 and still retain the repeater to cover future instances of the task."
8439 (interactive "nNumber of clones to produce: ")
8442 (if (and (not (equal current-prefix-arg '(4)))
8444 (re-search-forward org-ts-regexp-both
8446 (org-end-of-subtree t)
8448 (read-from-minibuffer
8449 "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
8450 ""))) ;; No time shift
8452 (drawer-re org-drawer-regexp)
8453 beg end template task idprop
8454 shift-n shift-what doshift nmin nmax)
8455 (if (not (and (integerp n) (> n 0)))
8456 (error "Invalid number of replications %s" n))
8457 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
8458 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
8460 (error "Invalid shift specification %s" shift))
8462 (setq shift-n (string-to-number (match-string 1 shift))
8463 shift-what (cdr (assoc (match-string 2 shift)
8464 '(("d" . day) ("w" . week)
8465 ("m" . month) ("y" . year))))))
8466 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
8467 (setq nmin 1 nmax n)
8468 (org-back-to-heading t)
8470 (setq idprop (org-entry-get nil "ID"))
8471 (org-end-of-subtree t t)
8472 (or (bolp) (insert "\n"))
8474 (setq template (buffer-substring beg end))
8476 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
8477 (delete-region beg end)
8479 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
8481 (loop for n from nmin to nmax do
8486 (goto-char (point-min))
8488 (and idprop (if org-clone-delete-id
8489 (org-entry-delete nil "ID")
8490 (org-id-get-create t)))
8492 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
8494 (goto-char (point-min))
8495 (while (re-search-forward drawer-re nil t)
8497 (org-remove-empty-drawer-at d (point))) org-drawers)))
8498 (goto-char (point-min))
8500 (while (re-search-forward org-ts-regexp-both nil t)
8501 (org-timestamp-change (* n shift-n) shift-what))
8502 (unless (= n n-no-remove)
8503 (goto-char (point-min))
8504 (while (re-search-forward org-ts-regexp nil t)
8506 (goto-char (match-beginning 0))
8507 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
8508 (delete-region (match-beginning 1) (match-end 1)))))))
8509 (setq task (buffer-string)))
8515 (defun org-sort (with-case)
8516 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8517 Optional argument WITH-CASE means sort case-sensitively."
8520 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8521 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8523 (org-call-with-arg 'org-sort-entries with-case))))
8525 (defun org-sort-remove-invisible (s)
8526 "Remove invisible links from string S."
8527 (remove-text-properties 0 (length s) org-rm-props s)
8528 (while (string-match org-bracket-link-regexp s)
8529 (setq s (replace-match (if (match-end 2)
8531 (match-string 1 s)) t t s)))
8532 (let ((st (format " %s " s)))
8533 (while (string-match org-emph-re st)
8534 (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
8535 (setq s (substring st 1 -1)))
8538 (defvar org-priority-regexp) ; defined later in the file
8540 (defvar org-after-sorting-entries-or-items-hook nil
8541 "Hook that is run after a bunch of entries or items have been sorted.
8542 When children are sorted, the cursor is in the parent line when this
8543 hook gets called. When a region or a plain list is sorted, the cursor
8544 will be in the first entry of the sorted region/list.")
8546 (defun org-sort-entries
8547 (&optional with-case sorting-type getkey-func compare-func property)
8548 "Sort entries on a certain level of an outline tree.
8549 If there is an active region, the entries in the region are sorted.
8550 Else, if the cursor is before the first entry, sort the top-level items.
8551 Else, the children of the entry at point are sorted.
8553 Sorting can be alphabetically, numerically, by date/time as given by
8554 a time stamp, by a property, by priority order, or by a custom function.
8556 The command prompts for the sorting type unless it has been given to the
8557 function through the SORTING-TYPE argument, which needs to be a character,
8558 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
8559 precise meaning of each character:
8561 n Numerically, by converting the beginning of the entry/item to a number.
8562 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8563 o By order of TODO keywords.
8564 t By date/time, either the first active time stamp in the entry, or, if
8565 none exist, by the first inactive one.
8566 s By the scheduled date/time.
8567 d By deadline date/time.
8568 c By creation time, which is assumed to be the first inactive time stamp
8569 at the beginning of a line.
8570 p By priority according to the cookie.
8571 r By the value of a property.
8573 Capital letters will reverse the sort order.
8575 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8576 called with point at the beginning of the record. It must return either
8577 a string or a number that should serve as the sorting key for that record.
8579 Comparing entries ignores case by default. However, with an optional argument
8580 WITH-CASE, the sorting considers case as well.
8582 Sorting is done against the visible part of the headlines, it ignores hidden
8585 (let ((case-func (if with-case 'identity 'downcase))
8587 ;; The clock marker is lost when using `sort-subr', let's
8588 ;; store the clocking string.
8589 (when (equal (marker-buffer org-clock-marker) (current-buffer))
8591 (goto-char org-clock-marker)
8592 (looking-back "^.*") (match-string-no-properties 0))))
8593 start beg end stars re re2
8595 ;; Find beginning and end of region to sort
8597 ((org-region-active-p)
8598 ;; we will sort the region
8599 (setq end (region-end)
8601 (goto-char (region-beginning))
8602 (if (not (org-at-heading-p)) (outline-next-heading))
8603 (setq start (point)))
8604 ((or (org-at-heading-p)
8605 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8606 ;; we will sort the children of the current headline
8607 (org-back-to-heading)
8609 end (progn (org-end-of-subtree t t)
8610 (or (bolp) (insert "\n"))
8611 (org-back-over-empty-lines)
8616 (outline-next-heading))
8618 ;; we will sort the top-level entries in this file
8619 (goto-char (point-min))
8620 (or (org-at-heading-p) (outline-next-heading))
8621 (setq start (point))
8622 (goto-char (point-max))
8623 (beginning-of-line 1)
8624 (when (looking-at ".*?\\S-")
8625 ;; File ends in a non-white line
8628 (setq end (point-max))
8629 (setq what "top-level")
8634 (if (>= beg end) (user-error "Nothing to sort"))
8636 (looking-at "\\(\\*+\\)")
8637 (setq stars (match-string 1)
8638 re (concat "^" (regexp-quote stars) " +")
8639 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8640 txt (buffer-substring beg end))
8641 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8642 (if (and (not (equal stars "*")) (string-match re2 txt))
8643 (user-error "Region to sort contains a level above the first entry"))
8645 (unless sorting-type
8647 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8648 [t]ime
[s]cheduled [d]eadline [c]reated
8649 A/N/P/R/O/F/T/S/D/C means reversed:"
8651 (setq sorting-type (read-char-exclusive))
8654 (and (= (downcase sorting-type) ?f)
8656 (org-icompleting-read "Sort using function: "
8657 obarray 'fboundp t nil nil))
8658 (setq getkey-func (intern getkey-func))))
8660 (and (= (downcase sorting-type) ?r)
8663 (org-icompleting-read "Property: "
8664 (mapcar 'list (org-buffer-property-keys t))
8667 (message "Sorting entries...")
8670 (narrow-to-region start end)
8671 (let ((dcst (downcase sorting-type))
8672 (case-fold-search nil)
8673 (now (current-time)))
8675 (/= dcst sorting-type)
8676 ;; This function moves to the beginning character of the "record" to
8679 (if (re-search-forward re nil t)
8680 (goto-char (match-beginning 0))
8681 (goto-char (point-max))))
8682 ;; This function moves to the last character of the "record" being
8687 (outline-forward-same-level 1)
8689 (goto-char (point-max))))))
8690 ;; This function returns the value that gets sorted against.
8694 (if (looking-at org-complex-heading-regexp)
8695 (string-to-number (org-sort-remove-invisible (match-string 4)))
8698 (if (looking-at org-complex-heading-regexp)
8699 (funcall case-func (org-sort-remove-invisible (match-string 4)))
8702 (let ((end (save-excursion (outline-next-heading) (point))))
8703 (if (or (re-search-forward org-ts-regexp end t)
8704 (re-search-forward org-ts-regexp-both end t))
8705 (org-time-string-to-seconds (match-string 0))
8706 (org-float-time now))))
8708 (let ((end (save-excursion (outline-next-heading) (point))))
8709 (if (re-search-forward
8710 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8712 (org-time-string-to-seconds (match-string 0))
8713 (org-float-time now))))
8715 (let ((end (save-excursion (outline-next-heading) (point))))
8716 (if (re-search-forward org-scheduled-time-regexp end t)
8717 (org-time-string-to-seconds (match-string 1))
8718 (org-float-time now))))
8720 (let ((end (save-excursion (outline-next-heading) (point))))
8721 (if (re-search-forward org-deadline-time-regexp end t)
8722 (org-time-string-to-seconds (match-string 1))
8723 (org-float-time now))))
8725 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8726 (string-to-char (match-string 2))
8727 org-default-priority))
8729 (or (org-entry-get nil property) ""))
8731 (if (looking-at org-complex-heading-regexp)
8732 (- 9999 (length (member (match-string 2)
8733 org-todo-keywords-1)))))
8737 (setq tmp (funcall getkey-func))
8738 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8740 (error "Invalid key function `%s'" getkey-func)))
8741 (t (error "Invalid sorting type `%c'" sorting-type))))
8744 ((= dcst ?a) 'string<)
8745 ((= dcst ?f) compare-func)
8746 ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
8747 (run-hooks 'org-after-sorting-entries-or-items-hook)
8748 ;; Reset the clock marker if needed
8752 (search-forward cmstr nil t)
8753 (move-marker org-clock-marker (point))))
8754 (message "Sorting entries...done")))
8756 (defun org-do-sort (table what &optional with-case sorting-type)
8757 "Sort TABLE of WHAT according to SORTING-TYPE.
8758 The user will be prompted for the SORTING-TYPE if the call to this
8759 function does not specify it. WHAT is only for the prompt, to indicate
8760 what is being sorted. The sorting key will be extracted from
8761 the car of the elements of the table.
8762 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8763 (unless sorting-type
8765 "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
8767 (setq sorting-type (read-char-exclusive)))
8768 (let ((dcst (downcase sorting-type))
8769 extractfun comparefun)
8770 ;; Define the appropriate functions
8773 (setq extractfun 'string-to-number
8774 comparefun (if (= dcst sorting-type) '< '>)))
8776 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8777 (lambda(x) (downcase (org-sort-remove-invisible x))))
8778 comparefun (if (= dcst sorting-type)
8780 (lambda (a b) (and (not (string< a b))
8781 (not (string= a b)))))))
8785 (if (or (string-match org-ts-regexp x)
8786 (string-match org-ts-regexp-both x))
8788 (org-time-string-to-time (match-string 0 x)))
8790 comparefun (if (= dcst sorting-type) '< '>)))
8791 (t (error "Invalid sorting type `%c'" sorting-type)))
8793 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8795 (lambda (a b) (funcall comparefun (car a) (car b))))))
8798 ;;; The orgstruct minor mode
8800 ;; Define a minor mode which can be used in other modes in order to
8801 ;; integrate the org-mode structure editing commands.
8803 ;; This is really a hack, because the org-mode structure commands use
8804 ;; keys which normally belong to the major mode. Here is how it
8805 ;; works: The minor mode defines all the keys necessary to operate the
8806 ;; structure commands, but wraps the commands into a function which
8807 ;; tests if the cursor is currently at a headline or a plain list
8808 ;; item. If that is the case, the structure command is used,
8809 ;; temporarily setting many Org-mode variables like regular
8810 ;; expressions for filling etc. However, when any of those keys is
8811 ;; used at a different location, function uses `key-binding' to look
8812 ;; up if the key has an associated command in another currently active
8813 ;; keymap (minor modes, major mode, global), and executes that
8814 ;; command. There might be problems if any of the keys is otherwise
8815 ;; used as a prefix key.
8817 (defcustom orgstruct-heading-prefix-regexp nil
8818 "Regexp that matches the custom prefix of Org headlines in
8819 orgstruct(++)-mode."
8822 :package-version '(Org . "8.0")
8824 ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
8826 (defcustom orgstruct-setup-hook nil
8827 "Hook run after orgstruct-mode-map is filled."
8830 :package-version '(Org . "8.0")
8833 (defvar orgstruct-initialized nil)
8835 (defvar org-local-vars nil
8836 "List of local variables, for use by `orgstruct-mode'.")
8839 (define-minor-mode orgstruct-mode
8840 "Toggle the minor mode `orgstruct-mode'.
8841 This mode is for using Org-mode structure commands in other
8842 modes. The following keys behave as if Org-mode were active, if
8843 the cursor is on a headline, or on a plain list item (both as
8844 defined by Org-mode)."
8845 nil " OrgStruct" (make-sparse-keymap)
8846 (funcall (if orgstruct-mode
8847 'add-to-invisibility-spec
8848 'remove-from-invisibility-spec)
8850 (when orgstruct-mode
8851 (org-load-modules-maybe)
8852 (unless orgstruct-initialized
8854 (setq orgstruct-initialized t))))
8857 (defun turn-on-orgstruct ()
8858 "Unconditionally turn on `orgstruct-mode'."
8861 (defvar org-fb-vars nil)
8862 (make-variable-buffer-local 'org-fb-vars)
8863 (defun orgstruct++-mode (&optional arg)
8864 "Toggle `orgstruct-mode', the enhanced version of it.
8865 In addition to setting orgstruct-mode, this also exports all
8866 indentation and autofilling variables from org-mode into the
8867 buffer. It will also recognize item context in multiline items."
8869 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8871 (progn (orgstruct-mode -1)
8873 (org-set-local (car v)
8874 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
8877 (setq org-fb-vars nil)
8882 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
8883 (symbol-name (car x)))
8884 (setq var (car x) val (nth 1 x))
8885 (push (list var `(quote ,(eval var))) org-fb-vars)
8886 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8888 (org-set-local 'orgstruct-is-++ t))))
8890 (defvar orgstruct-is-++ nil
8891 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8892 (make-variable-buffer-local 'orgstruct-is-++)
8895 (defun turn-on-orgstruct++ ()
8896 "Unconditionally turn on `orgstruct++-mode'."
8897 (orgstruct++-mode 1))
8899 (defun orgstruct-error ()
8900 "Error when there is no default binding for a structure key."
8902 (funcall (if (fboundp 'user-error)
8905 "This key has no function outside structure elements"))
8907 (defun orgstruct-setup ()
8908 "Setup orgstruct keymap."
8909 (dolist (cell '((org-demote . t)
8913 (org-shiftmetaleft . t)
8914 (org-shiftmetaright . t)
8915 org-backward-element
8916 org-backward-heading-same-level
8921 org-forward-heading-same-level
8923 org-insert-heading-respect-content
8924 org-kill-note-or-show-branches
8929 org-narrow-to-subtree
8944 outline-next-visible-heading
8945 outline-previous-visible-heading
8949 (let ((f (or (car-safe cell) cell))
8950 (disable-when-heading-prefix (cdr-safe cell)))
8952 (dolist (binding (nconc (where-is-internal f org-mode-map)
8953 (where-is-internal f outline-mode-map)))
8954 ;; TODO use local-function-key-map
8955 (dolist (rep '(("<tab>" . "TAB")
8956 ("<return>" . "RET")
8957 ("<escape>" . "ESC")
8958 ("<delete>" . "DEL")))
8959 (setq binding (read-kbd-macro
8960 (let ((case-fold-search))
8961 (replace-regexp-in-string
8962 (regexp-quote (cdr rep))
8964 (key-description binding))))))
8965 (let ((key (lookup-key orgstruct-mode-map binding)))
8966 (when (or (not key) (numberp key))
8968 (org-defkey orgstruct-mode-map
8970 (orgstruct-make-binding f binding disable-when-heading-prefix))
8972 (run-hooks 'orgstruct-setup-hook))
8974 (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
8975 "Create a function for binding in the structure minor mode.
8976 FUN is the command to call inside a table. KEY is the key that
8977 should be checked in for a command to execute outside of tables.
8978 Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
8979 if `orgstruct-heading-prefix-regexp' is non-nil."
8980 (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
8983 (while (fboundp (intern nname))
8984 (setq nname (format "%s-%d" name (setq i (1+ i)))))
8985 (setq name (intern nname)))
8987 (let ((bindings '((org-heading-regexp
8989 orgstruct-heading-prefix-regexp
8990 "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
8992 (concat orgstruct-heading-prefix-regexp "\\*+ "))
8993 (org-outline-regexp-bol
8994 (concat "^" org-outline-regexp))
8995 (outline-regexp org-outline-regexp)
8996 (outline-heading-end-regexp "\n")
8997 (outline-level 'org-outline-level)
8998 (outline-heading-alist))))
9000 ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
9001 "Outside of structure, run the binding of `"
9002 (key-description key) "'."
9003 (when disable-when-heading-prefix
9005 "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
9006 "back to the default binding due to limitations of Org's implementation of\n"
9007 "`" (symbol-name fun) "'.")))
9010 ,(when disable-when-heading-prefix
9011 '(and orgstruct-heading-prefix-regexp
9012 (not (string= orgstruct-heading-prefix-regexp "")))))
9017 (org-context-p 'headline 'item
9018 ,(when (memq fun '(org-insert-heading))
9019 '(when orgstruct-is-++
9022 (let* ((orgstruct-mode)
9024 (loop with key = ,key
9027 ("<\\([^>]*\\)tab>" . "\\1TAB")
9028 ("<\\([^>]*\\)return>" . "\\1RET")
9029 ("<\\([^>]*\\)escape>" . "\\1ESC")
9030 ("<\\([^>]*\\)delete>" . "\\1DEL"))
9033 (setq key (read-kbd-macro
9034 (let ((case-fold-search))
9035 (replace-regexp-in-string
9038 (key-description key))))))
9039 thereis (key-binding key))))
9040 (if (keymapp binding)
9041 (set-temporary-overlay-map binding)
9042 (let ((func (or binding
9044 'orgstruct-error))))
9046 (call-interactively func)))))
9047 (org-run-like-in-org-mode
9051 (call-interactively ',fun)))))))))
9054 (defun org-contextualize-keys (alist contexts)
9055 "Return valid elements in ALIST depending on CONTEXTS.
9057 `org-agenda-custom-commands' or `org-capture-templates' are the
9058 values used for ALIST, and `org-agenda-custom-commands-contexts'
9059 or `org-capture-templates-contexts' are the associated contexts
9062 ;; normalize contexts
9064 (lambda(c) (cond ((listp (cadr c))
9065 (list (car c) (car c) (cadr c)))
9066 ((string= "" (cadr c))
9067 (list (car c) (car c) (caddr c)))
9070 ;; loop over all commands or templates
9071 (while (setq c (pop a))
9074 ((not (assoc (car c) contexts))
9076 ((and (assoc (car c) contexts)
9077 (setq vrules (org-contextualize-validate-key
9080 (when (not (equal (car vr) (cadr vr)))
9081 (setq repl vr))) vrules)
9082 (if (not repl) (push c r)
9083 (push (cadr repl) s)
9086 (cdr (or (assoc (cadr repl) alist)
9087 (error "Undefined key `%s' as contextual replacement for `%s'"
9088 (cadr repl) (car c)))))
9090 ;; Return limited ALIST, possibly with keys modified, and deduplicated
9095 (let ((tpl (car x)))
9099 (equal y tpl)) s))) x)))
9102 (defun org-contextualize-validate-key (key contexts)
9103 "Check CONTEXTS for agenda or capture KEY."
9105 (while (setq r (pop contexts))
9109 (and (equal key (car r))
9110 (if (functionp rr) (funcall rr)
9111 (or (and (eq (car rr) 'in-file)
9113 (string-match (cdr rr) (buffer-file-name)))
9114 (and (eq (car rr) 'in-mode)
9115 (string-match (cdr rr) (symbol-name major-mode)))
9116 (and (eq (car rr) 'in-buffer)
9117 (string-match (cdr rr) (buffer-name)))
9118 (when (and (eq (car rr) 'not-in-file)
9120 (not (string-match (cdr rr) (buffer-file-name))))
9121 (when (eq (car rr) 'not-in-mode)
9122 (not (string-match (cdr rr) (symbol-name major-mode))))
9123 (when (eq (car rr) 'not-in-buffer)
9124 (not (string-match (cdr rr) (buffer-name)))))))
9127 (delete-dups (delq nil res))))
9129 (defun org-context-p (&rest contexts)
9130 "Check if local context is any of CONTEXTS.
9131 Possible values in the list of contexts are `table', `headline', and `item'."
9132 (let ((pos (point)))
9133 (goto-char (point-at-bol))
9134 (prog1 (or (and (memq 'table contexts)
9135 (looking-at "[ \t]*|"))
9136 (and (memq 'headline contexts)
9137 (looking-at org-outline-regexp))
9138 (and (memq 'item contexts)
9139 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
9140 (and (memq 'item-body contexts)
9144 (defun org-get-local-variables ()
9145 "Return a list of all local variables in an Org mode buffer."
9147 (with-current-buffer (get-buffer-create "*Org tmp*")
9150 (setq varlist (buffer-local-variables)))
9151 (kill-buffer "*Org tmp*")
9158 (list (car x) (cdr x))))
9159 (if (and (not (get (car x) 'org-state))
9161 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
9162 (symbol-name (car x))))
9166 (defun org-clone-local-variables (from-buffer &optional regexp)
9167 "Clone local variables from FROM-BUFFER.
9168 Optional argument REGEXP selects variables to clone."
9171 (and (symbolp (car pair))
9173 (string-match regexp (symbol-name (car pair))))
9174 (set (make-local-variable (car pair))
9176 (buffer-local-variables from-buffer)))
9179 (defun org-run-like-in-org-mode (cmd)
9180 "Run a command, pretending that the current buffer is in Org-mode.
9181 This will temporarily bind local variables that are typically bound in
9182 Org-mode to the values they have in Org-mode, and then interactively
9184 (org-load-modules-maybe)
9185 (unless org-local-vars
9186 (setq org-local-vars (org-get-local-variables)))
9188 (dolist (var org-local-vars)
9189 (when (or (not (boundp (car var)))
9190 (eq (symbol-value (car var))
9191 (default-value (car var))))
9192 (push (list (car var) `(quote ,(cadr var))) binds)))
9194 (call-interactively (quote ,cmd))))))
9198 (defun org-get-category (&optional pos force-refresh)
9199 "Get the category applying to position POS."
9201 (if force-refresh (org-refresh-category-properties))
9202 (let ((pos (or pos (point))))
9203 (or (get-text-property pos 'org-category)
9204 (progn (org-refresh-category-properties)
9205 (get-text-property pos 'org-category))))))
9207 (defun org-refresh-category-properties ()
9208 "Refresh category text properties in the buffer."
9209 (let ((case-fold-search t)
9210 (inhibit-read-only t)
9212 ((null org-category)
9213 (if buffer-file-name
9214 (file-name-sans-extension
9215 (file-name-nondirectory buffer-file-name))
9217 ((symbolp org-category) (symbol-name org-category))
9219 beg end cat pos optionp)
9220 (org-with-silent-modifications
9224 (goto-char (point-min))
9225 (put-text-property (point) (point-max) 'org-category def-cat)
9226 (while (re-search-forward
9227 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
9228 (setq pos (match-end 0)
9229 optionp (equal (char-after (match-beginning 0)) ?#)
9230 cat (org-trim (match-string 2)))
9232 (setq beg (point-at-bol) end (point-max))
9233 (org-back-to-heading t)
9234 (setq beg (point) end (org-end-of-subtree t t)))
9235 (put-text-property beg end 'org-category cat)
9236 (put-text-property beg end 'org-category-position beg)
9237 (goto-char pos)))))))
9239 (defun org-refresh-properties (dprop tprop)
9240 "Refresh buffer text properties.
9241 DPROP is the drawer property and TPROP is the corresponding text
9243 (let ((case-fold-search t)
9244 (inhibit-read-only t) p)
9245 (org-with-silent-modifications
9249 (goto-char (point-min))
9250 (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
9251 (setq p (org-match-string-no-properties 1))
9253 (org-back-to-heading t)
9255 (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
9260 ;;; Link abbreviations
9262 (defun org-link-expand-abbrev (link)
9263 "Apply replacements as defined in `org-link-abbrev-alist'."
9264 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
9265 (let* ((key (match-string 1 link))
9266 (as (or (assoc key org-link-abbrev-alist-local)
9267 (assoc key org-link-abbrev-alist)))
9268 (tag (and (match-end 2) (match-string 3 link)))
9274 ((symbolp rpl) (funcall rpl tag))
9275 ((string-match "%(\\([^)]+\\))" rpl)
9278 (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
9279 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
9280 ((string-match "%h" rpl)
9281 (replace-match (url-hexify-string (or tag "")) t t rpl))
9282 (t (concat rpl tag)))))
9285 ;;; Storing and inserting links
9287 (defvar org-insert-link-history nil
9288 "Minibuffer history for links inserted with `org-insert-link'.")
9290 (defvar org-stored-links nil
9291 "Contains the links stored with `org-store-link'.")
9293 (defvar org-store-link-plist nil
9294 "Plist with info about the most recently link created with `org-store-link'.")
9296 (defvar org-link-protocols nil
9297 "Link protocols added to Org-mode using `org-add-link-type'.")
9299 (defvar org-store-link-functions nil
9300 "List of functions that are called to create and store a link.
9301 Each function will be called in turn until one returns a non-nil
9302 value. Each function should check if it is responsible for creating
9303 this link (for example by looking at the major mode).
9304 If not, it must exit and return nil.
9305 If yes, it should return a non-nil value after a calling
9306 `org-store-link-props' with a list of properties and values.
9307 Special properties are:
9309 :type The link prefix, like \"http\". This must be given.
9310 :link The link, like \"http://www.astro.uva.nl/~dominik\".
9311 This is obligatory as well.
9312 :description Optional default description for the second pair
9313 of brackets in an Org-mode link. The user can still change
9314 this when inserting this link into an Org-mode buffer.
9316 In addition to these, any additional properties can be specified
9317 and then used in capture templates.")
9319 (defun org-add-link-type (type &optional follow export)
9320 "Add TYPE to the list of `org-link-types'.
9321 Re-compute all regular expressions depending on `org-link-types'
9323 FOLLOW and EXPORT are two functions.
9325 FOLLOW should take the link path as the single argument and do whatever
9326 is necessary to follow the link, for example find a file or display
9329 EXPORT should format the link path for export to one of the export formats.
9330 It should be a function accepting three arguments:
9332 path the path of the link, the text after the prefix (like \"http:\")
9333 desc the description of the link, if any, or a description added by
9334 org-export-normalize-links if there is none
9335 format the export format, a symbol like `html' or `latex' or `ascii'..
9337 The function may use the FORMAT information to return different values
9338 depending on the format. The return value will be put literally into
9339 the exported file. If the return value is nil, this means Org should
9340 do what it normally does with links which do not have EXPORT defined.
9342 Org-mode has a built-in default for exporting links. If you are happy with
9343 this default, there is no need to define an export function for the link
9344 type. For a simple example of an export function, see `org-bbdb.el'."
9345 (add-to-list 'org-link-types type t)
9346 (org-make-link-regexps)
9347 (if (assoc type org-link-protocols)
9348 (setcdr (assoc type org-link-protocols) (list follow export))
9349 (push (list type follow export) org-link-protocols)))
9351 (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
9352 (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
9355 (defun org-store-link (arg)
9356 "\\<org-mode-map>Store an org-link to the current location.
9357 This link is added to `org-stored-links' and can later be inserted
9358 into an org-buffer with \\[org-insert-link].
9360 For some link types, a prefix arg is interpreted.
9361 For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
9362 For file links, arg negates `org-context-in-file-links'.
9364 A double prefix arg force skipping storing functions that are not
9367 A triple prefix arg force storing a link for each line in the
9370 (org-load-modules-maybe)
9371 (if (and (equal arg '(64)) (org-region-active-p))
9373 (let ((end (region-end)))
9374 (goto-char (region-beginning))
9376 (while (< (point-at-eol) end)
9377 (move-end-of-line 1) (activate-mark)
9378 (let (current-prefix-arg)
9379 (call-interactively 'org-store-link))
9380 (move-beginning-of-line 2)
9381 (set-mark (point)))))
9382 (org-with-limited-levels
9383 (setq org-store-link-plist nil)
9384 (let (link cpltxt desc description search
9385 txt custom-id agenda-link sfuns sfunsn)
9388 ;; Store a link using an external link type
9389 ((and (not (equal arg '(16)))
9392 nil (mapcar (lambda (f)
9393 (let (fs) (if (funcall f) (push f fs))))
9394 org-store-link-functions))
9395 sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
9396 (or (and (cdr sfuns)
9399 "Which function for creating the link? "
9400 sfunsn t (car sfunsn)))))
9401 (funcall (caar sfuns)))
9402 (setq link (plist-get org-store-link-plist :link)
9403 desc (or (plist-get org-store-link-plist
9404 :description) link))))
9406 ;; Store a link from a source code buffer
9407 ((org-src-edit-buffer-p)
9409 (while (or (not label)
9413 (goto-char (point-min))
9415 (regexp-quote (format org-coderef-label-format label))
9417 (when label (message "Label exists already") (sit-for 2))
9418 (setq label (read-string "Code line label: " label)))
9420 (setq link (format org-coderef-label-format label))
9421 (setq gc (- 79 (length link)))
9422 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
9424 (setq link (concat "(" label ")") desc nil)))
9426 ;; We are in the agenda, link to referenced location
9427 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
9428 (let ((m (or (get-text-property (point) 'org-hd-marker)
9429 (get-text-property (point) 'org-marker))))
9431 (org-with-point-at m
9433 (if (org-called-interactively-p 'any)
9434 (call-interactively 'org-store-link)
9435 (org-store-link nil)))))))
9437 ((eq major-mode 'calendar-mode)
9438 (let ((cd (calendar-cursor-to-date)))
9441 (car org-time-stamp-formats)
9443 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
9445 (org-store-link-props :type "calendar" :date cd)))
9447 ((eq major-mode 'help-mode)
9448 (setq link (concat "help:" (save-excursion
9449 (goto-char (point-min))
9450 (looking-at "^[^ ]+")
9452 (org-store-link-props :type "help"))
9454 ((eq major-mode 'w3-mode)
9455 (setq cpltxt (if (and (buffer-name)
9456 (not (string-match "Untitled" (buffer-name))))
9459 link (url-view-url t))
9460 (org-store-link-props :type "w3" :url (url-view-url t)))
9462 ((eq major-mode 'image-mode)
9463 (setq cpltxt (concat "file:"
9464 (abbreviate-file-name buffer-file-name))
9466 (org-store-link-props :type "image" :file buffer-file-name))
9468 ;; In dired, store a link to the file of the current line
9469 ((eq major-mode 'dired-mode)
9470 (let ((file (dired-get-filename nil t)))
9472 (abbreviate-file-name
9473 (expand-file-name (dired-get-filename nil t)))
9474 ;; otherwise, no file so use current directory.
9476 (setq cpltxt (concat "file:" file)
9479 ((setq search (run-hook-with-args-until-success
9480 'org-create-file-search-functions))
9481 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
9483 (setq cpltxt (or description link)))
9485 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
9486 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
9488 ;; Store a link using the target at point
9489 ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
9492 (abbreviate-file-name
9493 (buffer-file-name (buffer-base-buffer)))
9494 "::" (match-string 1))
9496 ((and (featurep 'org-id)
9497 (or (eq org-id-link-to-org-use-id t)
9498 (and (org-called-interactively-p 'any)
9499 (or (eq org-id-link-to-org-use-id 'create-if-interactive)
9500 (and (eq org-id-link-to-org-use-id
9501 'create-if-interactive-and-no-custom-id)
9503 (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
9504 ;; Store a link using the ID at point
9505 (setq link (condition-case nil
9506 (prog1 (org-id-store-link)
9507 (setq desc (plist-get org-store-link-plist
9510 ;; Probably before first headline, link only to file
9512 (abbreviate-file-name
9513 (buffer-file-name (buffer-base-buffer))))))))
9515 ;; Just link to current headline
9516 (setq cpltxt (concat "file:"
9517 (abbreviate-file-name
9518 (buffer-file-name (buffer-base-buffer)))))
9519 ;; Add a context search string
9520 (when (org-xor org-context-in-file-links arg)
9521 (let* ((ee (org-element-at-point))
9522 (et (org-element-type ee))
9523 (ev (plist-get (cadr ee) :value))
9524 (ek (plist-get (cadr ee) :key))
9525 (eok (and (stringp ek) (string-match "name" ek))))
9527 ((org-at-heading-p) nil)
9528 ((and (eq et 'keyword) eok) ev)
9529 ((org-region-active-p)
9530 (buffer-substring (region-beginning) (region-end)))))
9531 (when (or (null txt) (string-match "\\S-" txt))
9535 (org-make-org-heading-search-string txt)
9537 desc (or (and (eq et 'keyword) eok ev)
9538 (nth 4 (ignore-errors (org-heading-components)))
9540 (if (string-match "::\\'" cpltxt)
9541 (setq cpltxt (substring cpltxt 0 -2)))
9542 (setq link cpltxt))))
9544 ((buffer-file-name (buffer-base-buffer))
9545 ;; Just link to this file here.
9546 (setq cpltxt (concat "file:"
9547 (abbreviate-file-name
9548 (buffer-file-name (buffer-base-buffer)))))
9549 ;; Add a context string.
9550 (when (org-xor org-context-in-file-links arg)
9551 (setq txt (if (org-region-active-p)
9552 (buffer-substring (region-beginning) (region-end))
9553 (buffer-substring (point-at-bol) (point-at-eol))))
9554 ;; Only use search option if there is some text.
9555 (when (string-match "\\S-" txt)
9557 (concat cpltxt "::" (org-make-org-heading-search-string txt))
9561 ((org-called-interactively-p 'interactive)
9562 (user-error "No method for storing a link from this buffer"))
9564 (t (setq link nil)))
9566 ;; We're done setting link and desc, clean up
9567 (if (consp link) (setq cpltxt (car link) link (cdr link)))
9568 (setq link (or link cpltxt)
9569 desc (or desc cpltxt))
9570 (cond ((equal desc "NONE") (setq desc nil))
9571 ((string-match org-bracket-link-analytic-regexp desc)
9572 (let ((d0 (match-string 3 desc))
9573 (p0 (match-string 5 desc)))
9575 (replace-regexp-in-string
9576 org-bracket-link-regexp
9578 (if (equal (length (match-string 0 desc))
9579 (length desc)) "*" "")) desc)))))
9582 (if (not (and (or (org-called-interactively-p 'any)
9583 executing-kbd-macro) link))
9584 (or agenda-link (and link (org-make-link-string link desc)))
9585 (push (list link desc) org-stored-links)
9586 (message "Stored: %s" (or desc link))
9588 (setq link (concat "file:" (abbreviate-file-name
9589 (buffer-file-name)) "::#" custom-id))
9590 (push (list link desc) org-stored-links)))))))
9592 (defun org-store-link-props (&rest plist)
9593 "Store link properties, extract names and addresses."
9595 (when (setq x (plist-get plist :from))
9596 (setq adr (mail-extract-address-components x))
9597 (setq plist (plist-put plist :fromname (car adr)))
9598 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
9599 (when (setq x (plist-get plist :to))
9600 (setq adr (mail-extract-address-components x))
9601 (setq plist (plist-put plist :toname (car adr)))
9602 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
9603 (let ((from (plist-get plist :from))
9604 (to (plist-get plist :to)))
9605 (when (and from to org-from-is-user-regexp)
9607 (plist-put plist :fromto
9608 (if (string-match org-from-is-user-regexp from)
9610 (concat "from %f"))))))
9611 (setq org-store-link-plist plist))
9613 (defun org-add-link-props (&rest plist)
9614 "Add these properties to the link property list."
9617 (setq key (pop plist) value (pop plist))
9618 (setq org-store-link-plist
9619 (plist-put org-store-link-plist key value)))))
9621 (defun org-email-link-description (&optional fmt)
9622 "Return the description part of an email link.
9623 This takes information from `org-store-link-plist' and formats it
9624 according to FMT (default from `org-email-link-description-format')."
9625 (setq fmt (or fmt org-email-link-description-format))
9626 (let* ((p org-store-link-plist)
9627 (to (plist-get p :toaddress))
9628 (from (plist-get p :fromaddress))
9631 (cons "%c" (plist-get p :fromto))
9632 (cons "%F" (plist-get p :from))
9633 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
9634 (cons "%T" (plist-get p :to))
9635 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
9636 (cons "%s" (plist-get p :subject))
9637 (cons "%d" (plist-get p :date))
9638 (cons "%m" (plist-get p :message-id)))))
9639 (when (string-match "%c" fmt)
9640 ;; Check if the user wrote this message
9641 (if (and org-from-is-user-regexp from to
9642 (save-match-data (string-match org-from-is-user-regexp from)))
9643 (setq fmt (replace-match "to %t" t t fmt))
9644 (setq fmt (replace-match "from %f" t t fmt))))
9645 (org-replace-escapes fmt table)))
9647 (defun org-make-org-heading-search-string (&optional string)
9648 "Make search string for the current headline or STRING."
9650 (and (derived-mode-p 'org-mode)
9652 (org-back-to-heading t)
9653 (org-element-property :raw-value (org-element-at-point))))))
9654 (lines org-context-in-file-links))
9655 (or string (setq s (concat "*" s))) ; Add * for headlines
9656 (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
9657 (when (and string (integerp lines) (> lines 0))
9658 (let ((slines (org-split-string s "\n")))
9659 (when (< lines (length slines))
9662 (reverse (nthcdr (- (length slines) lines)
9663 (reverse slines))) "\n")))))
9664 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
9666 (defun org-make-link-string (link &optional description)
9667 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9668 (unless (string-match "\\S-" link)
9669 (error "Empty link"))
9670 (when (and description
9671 (stringp description)
9672 (not (string-match "\\S-" description)))
9673 (setq description nil))
9674 (when (stringp description)
9675 ;; Remove brackets from the description, they are fatal.
9676 (while (string-match "\\[" description)
9677 (setq description (replace-match "{" t t description)))
9678 (while (string-match "\\]" description)
9679 (setq description (replace-match "}" t t description))))
9680 (when (equal link description)
9681 ;; No description needed, it is identical
9682 (setq description nil))
9683 (when (and (not description)
9684 (not (string-match (org-image-file-name-regexp) link))
9685 (not (equal link (org-link-escape link))))
9686 (setq description (org-extract-attributes link)))
9688 (cond ((string-match (org-image-file-name-regexp) link) link)
9689 ((string-match org-link-types-re link)
9690 (concat (match-string 1 link)
9691 (org-link-escape (substring link (match-end 1)))))
9692 (t (org-link-escape link))))
9693 (concat "[[" link "]"
9694 (if description (concat "[" description "]") "")
9697 (defconst org-link-escape-chars
9698 '(?\ ?\[ ?\] ?\; ?\= ?\+)
9699 "List of characters that should be escaped in link.
9700 This is the list that is used for internal purposes.")
9702 (defconst org-link-escape-chars-browser
9704 "List of escapes for characters that are problematic in links.
9705 This is the list that is used before handing over to the browser.")
9707 (defun org-link-escape (text &optional table merge)
9708 "Return percent escaped representation of TEXT.
9709 TEXT is a string with the text to escape.
9710 Optional argument TABLE is a list with characters that should be
9711 escaped. When nil, `org-link-escape-chars' is used.
9712 If optional argument MERGE is set, merge TABLE into
9713 `org-link-escape-chars'."
9716 (mapc (lambda (defchr)
9717 (unless (member defchr table)
9718 (setq table (cons defchr table)))) org-link-escape-chars))
9720 (setq table org-link-escape-chars)))
9723 (if (or (member char table)
9724 (and (or (< char 32) (= char 37) (> char 126))
9726 (mapconcat (lambda (sequence-element)
9727 (format "%%%.2X" sequence-element))
9728 (or (encode-coding-char char 'utf-8)
9729 (error "Unable to percent escape character: %s"
9730 (char-to-string char))) "")
9731 (char-to-string char))) text ""))
9733 (defun org-link-unescape (str)
9734 "Unhex hexified Unicode strings as returned from the JavaScript function
9735 encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
9736 (unless (and (null str) (string= "" str))
9737 (let ((pos 0) (case-fold-search t) unhexed)
9738 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9739 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9740 (setq str (replace-match unhexed t t str))
9741 (setq pos (+ pos (length unhexed))))))
9744 (defun org-link-unescape-compound (hex)
9745 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
9746 Note: this function also decodes single byte encodings like
9747 `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
9749 (let* ((bytes (cdr (split-string hex "%")))
9754 (let* ((val (string-to-number (pop bytes) 16))
9758 ((>= val 252) (cons 6 252))
9759 ((>= val 248) (cons 5 248))
9760 ((>= val 240) (cons 4 240))
9761 ((>= val 224) (cons 3 224))
9762 ((>= val 192) (cons 2 192))
9765 (if (>= val 192) (setq eat (car shift-xor)))
9766 (setq val (logxor val (cdr shift-xor)))
9767 (setq sum (+ (lsh sum (car shift-xor)) val))
9768 (if (> eat 0) (setq eat (- eat 1)))
9770 ((= 0 eat) ;multi byte
9771 (setq ret (concat ret (org-char-to-string sum)))
9773 ((not bytes) ; single byte(s)
9774 (setq ret (org-link-unescape-single-byte-sequence hex))))
9775 )) ;; end (while bytes
9778 (defun org-link-unescape-single-byte-sequence (hex)
9779 "Unhexify hex-encoded single byte character sequences."
9780 (mapconcat (lambda (byte)
9781 (char-to-string (string-to-number byte 16)))
9782 (cdr (split-string hex "%")) ""))
9784 (defun org-xor (a b)
9788 (defun org-fixup-message-id-for-http (s)
9789 "Replace special characters in a message id, so it can be used in an http query."
9790 (when (string-match "%" s)
9791 (setq s (mapconcat (lambda (c)
9794 (char-to-string c)))
9796 (while (string-match "<" s)
9797 (setq s (replace-match "%3C" t t s)))
9798 (while (string-match ">" s)
9799 (setq s (replace-match "%3E" t t s)))
9800 (while (string-match "@" s)
9801 (setq s (replace-match "%40" t t s)))
9804 (defun org-link-prettify (link)
9805 "Return a human-readable representation of LINK.
9806 The car of LINK must be a raw link the cdr of LINK must be either
9807 a link description or nil."
9808 (let ((desc (or (cadr link) "<no description>")))
9809 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9810 "<" (car link) ">")))
9813 (defun org-insert-link-global ()
9814 "Insert a link like Org-mode does.
9815 This command can be called in any mode to insert a link in Org-mode syntax."
9817 (org-load-modules-maybe)
9818 (org-run-like-in-org-mode 'org-insert-link))
9820 (defun org-insert-all-links (&optional keep)
9821 "Insert all links in `org-stored-links'."
9823 (let ((links (copy-sequence org-stored-links)) l)
9824 (while (setq l (if keep (pop links) (pop org-stored-links)))
9826 (org-insert-link nil (car l) (or (cadr l) "<no description>"))
9829 (defun org-link-fontify-links-to-this-file ()
9830 "Fontify links to the current file in `org-stored-links'."
9831 (let ((f (buffer-file-name)) a b)
9832 (setq a (mapcar (lambda(l)
9834 (when (and (string-match "^file:\\(.+\\)::" ll)
9835 (equal f (expand-file-name (match-string 1 ll))))
9838 (when (featurep 'org-id)
9839 (setq b (mapcar (lambda(l)
9841 (when (and (string-match "^id:\\(.+\\)$" ll)
9842 (equal f (expand-file-name
9843 (or (org-id-find-id-file
9844 (match-string 1 ll)) ""))))
9848 (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
9849 (delq nil (append a b)))))
9851 (defvar org-link-links-in-this-file nil)
9852 (defun org-insert-link (&optional complete-file link-location default-description)
9853 "Insert a link. At the prompt, enter the link.
9855 Completion can be used to insert any of the link protocol prefixes like
9858 The history can be used to select a link previously stored with
9859 `org-store-link'. When the empty string is entered (i.e. if you just
9860 press RET at the prompt), the link defaults to the most recently
9861 stored link. As SPC triggers completion in the minibuffer, you need to
9862 use M-SPC or C-q SPC to force the insertion of a space character.
9864 You will also be prompted for a description, and if one is given, it will
9865 be displayed in the buffer instead of the link.
9867 If there is already a link at point, this command will allow you to edit link
9868 and description parts.
9870 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9871 be selected using completion. The path to the file will be relative to the
9872 current directory if the file is in the current directory or a subdirectory.
9873 Otherwise, the link will be the absolute path as completed in the minibuffer
9874 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9875 option `org-link-file-path-type'.
9877 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9878 the current directory or below.
9880 With three \\[universal-argument] prefixes, negate the meaning of
9881 `org-keep-stored-link-after-insertion'.
9883 If `org-make-link-description-function' is non-nil, this function will be
9884 called with the link target, and the result will be the default
9887 If the LINK-LOCATION parameter is non-nil, this value will be
9888 used as the link location instead of reading one interactively.
9890 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9891 be used as the default description."
9893 (let* ((wcf (current-window-configuration))
9894 (origbuf (current-buffer))
9895 (region (if (org-region-active-p)
9896 (buffer-substring (region-beginning) (region-end))))
9897 (remove (and region (list (region-beginning) (region-end))))
9899 tmphist ; byte-compile incorrectly complains about this
9900 (link link-location)
9901 (abbrevs org-link-abbrev-alist-local)
9902 entry file all-prefixes auto-desc)
9904 (link-location) ; specified by arg, just use it.
9905 ((org-in-regexp org-bracket-link-regexp 1)
9906 ;; We do have a link at point, and we are going to edit it.
9907 (setq remove (list (match-beginning 0) (match-end 0)))
9908 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9909 (setq link (read-string "Link: "
9911 (org-match-string-no-properties 1)))))
9912 ((or (org-in-regexp org-angle-link-re)
9913 (org-in-regexp org-plain-link-re))
9914 ;; Convert to bracket link
9915 (setq remove (list (match-beginning 0) (match-end 0))
9916 link (read-string "Link: "
9917 (org-remove-angle-brackets (match-string 0)))))
9918 ((member complete-file '((4) (16)))
9919 ;; Completing read for file names.
9920 (setq link (org-file-complete-link complete-file)))
9922 ;; Read link, with completion for stored links.
9923 (org-link-fontify-links-to-this-file)
9924 (org-switch-to-buffer-other-window "*Org Links*")
9925 (with-current-buffer "*Org Links*"
9927 (insert "Insert a link.
9928 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9929 (when org-stored-links
9930 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9931 (insert (mapconcat 'org-link-prettify
9932 (reverse org-stored-links) "\n")))
9933 (goto-char (point-min)))
9934 (let ((cw (selected-window)))
9935 (select-window (get-buffer-window "*Org Links*" 'visible))
9936 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9937 (unless (pos-visible-in-window-p (point-max))
9938 (org-fit-window-to-buffer))
9939 (and (window-live-p cw) (select-window cw)))
9940 ;; Fake a link history, containing the stored links.
9941 (setq tmphist (append (mapcar 'car org-stored-links)
9942 org-insert-link-history))
9943 (setq all-prefixes (append (mapcar 'car abbrevs)
9944 (mapcar 'car org-link-abbrev-alist)
9949 (org-completing-read
9952 (mapcar (lambda (x) (concat x ":"))
9954 (mapcar 'car org-stored-links))
9957 (caar org-stored-links)))
9958 (if (not (string-match "\\S-" link))
9959 (user-error "No link selected"))
9961 (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
9963 (if (or (member link all-prefixes)
9964 (and (equal ":" (substring link -1))
9965 (member (substring link 0 -1) all-prefixes)
9966 (setq link (substring link 0 -1))))
9967 (setq link (with-current-buffer origbuf
9968 (org-link-try-special-completion link)))))
9969 (set-window-configuration wcf)
9970 (kill-buffer "*Org Links*"))
9971 (setq entry (assoc link org-stored-links))
9972 (or entry (push link org-insert-link-history))
9973 (setq desc (or desc (nth 1 entry)))))
9975 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9976 (not org-keep-stored-link-after-insertion))
9977 (setq org-stored-links (delq (assoc link org-stored-links)
9980 (if (string-match org-plain-link-re link)
9981 ;; URL-like link, normalize the use of angular brackets.
9982 (setq link (org-remove-angle-brackets link)))
9984 ;; Check if we are linking to the current file with a search
9985 ;; option If yes, simplify the link by using only the search
9987 (when (and buffer-file-name
9988 (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
9989 (let* ((path (match-string 1 link))
9990 (case-fold-search nil)
9991 (search (match-string 2 link)))
9993 (if (equal (file-truename buffer-file-name) (file-truename path))
9994 ;; We are linking to this same file, with a search option
9995 (setq link search)))))
9997 ;; Check if we can/should use a relative path. If yes, simplify the link
9998 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9999 (let* ((type (match-string 1 link))
10000 (path (match-string 2 link))
10002 (case-fold-search nil))
10004 ((or (eq org-link-file-path-type 'absolute)
10005 (equal complete-file '(16)))
10006 (setq path (abbreviate-file-name (expand-file-name path))))
10007 ((eq org-link-file-path-type 'noabbrev)
10008 (setq path (expand-file-name path)))
10009 ((eq org-link-file-path-type 'relative)
10010 (setq path (file-relative-name path)))
10013 (if (string-match (concat "^" (regexp-quote
10015 (file-name-as-directory
10016 default-directory))))
10017 (expand-file-name path))
10018 ;; We are linking a file with relative path name.
10019 (setq path (substring (expand-file-name path)
10021 (setq path (abbreviate-file-name (expand-file-name path)))))))
10022 (setq link (concat type path))
10023 (if (equal desc origpath)
10024 (setq desc path))))
10026 (if org-make-link-description-function
10028 (or (condition-case nil
10029 (funcall org-make-link-description-function link desc)
10030 (error (progn (message "Can't get link description from `%s'"
10031 (symbol-name org-make-link-description-function))
10033 (read-string "Description: " default-description)))
10034 (if default-description (setq desc default-description)
10035 (setq desc (or (and auto-desc desc)
10036 (read-string "Description: " desc)))))
10038 (unless (string-match "\\S-" desc) (setq desc nil))
10039 (if remove (apply 'delete-region remove))
10040 (insert (org-make-link-string link desc))))
10042 (defun org-link-try-special-completion (type)
10043 "If there is completion support for link type TYPE, offer it."
10044 (let ((fun (intern (concat "org-" type "-complete-link"))))
10045 (if (functionp fun)
10047 (read-string "Link (no completion support): " (concat type ":")))))
10049 (defun org-file-complete-link (&optional arg)
10050 "Create a file link using completion."
10052 (setq file (org-iread-file-name "File: "))
10053 (let ((pwd (file-name-as-directory (expand-file-name ".")))
10054 (pwd1 (file-name-as-directory (abbreviate-file-name
10055 (expand-file-name ".")))))
10060 (abbreviate-file-name (expand-file-name file)))))
10061 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
10062 (setq link (concat "file:" (match-string 1 file))))
10063 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
10064 (expand-file-name file))
10066 "file:" (match-string 1 (expand-file-name file)))))
10067 (t (setq link (concat "file:" file)))))
10070 (defun org-iread-file-name (&rest args)
10071 "Read-file-name using `ido-mode' speedup if available.
10072 ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
10073 See `read-file-name' for a description of parameters."
10074 (org-without-partial-completion
10075 (if (and org-completion-use-ido
10076 (fboundp 'ido-read-file-name)
10077 (boundp 'ido-mode) ido-mode
10078 (listp (second args)))
10079 (let ((ido-enter-matching-directory nil))
10080 (apply 'ido-read-file-name args))
10081 (apply 'read-file-name args))))
10083 (defun org-completing-read (&rest args)
10084 "Completing-read with SPACE being a normal character."
10085 (let ((enable-recursive-minibuffers t)
10086 (minibuffer-local-completion-map
10087 (copy-keymap minibuffer-local-completion-map)))
10088 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
10089 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
10090 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
10091 (apply 'org-icompleting-read args)))
10093 (defun org-completing-read-no-i (&rest args)
10094 (let (org-completion-use-ido org-completion-use-iswitchb)
10095 (apply 'org-completing-read args)))
10097 (defun org-iswitchb-completing-read (prompt choices &rest args)
10098 "Use iswitch as a completing-read replacement to choose from choices.
10099 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
10101 (let* ((iswitchb-use-virtual-buffers nil)
10102 (iswitchb-make-buflist-hook
10104 (setq iswitchb-temp-buflist choices))))
10105 (iswitchb-read-buffer prompt)))
10107 (defun org-icompleting-read (&rest args)
10108 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
10109 (org-without-partial-completion
10110 (if (and org-completion-use-ido
10111 (fboundp 'ido-completing-read)
10112 (boundp 'ido-mode) ido-mode
10113 (listp (second args)))
10114 (let ((ido-enter-matching-directory nil))
10115 (apply 'ido-completing-read (concat (car args))
10116 (if (consp (car (nth 1 args)))
10117 (mapcar 'car (nth 1 args))
10120 (if (and org-completion-use-iswitchb
10121 (boundp 'iswitchb-mode) iswitchb-mode
10122 (listp (second args)))
10123 (apply 'org-iswitchb-completing-read (concat (car args))
10124 (if (consp (car (nth 1 args)))
10125 (mapcar 'car (nth 1 args))
10128 (apply 'completing-read args)))))
10130 (defun org-extract-attributes (s)
10131 "Extract the attributes cookie from a string and set as text property."
10132 (let (a attr (start 0) key value)
10134 (when (string-match "{{\\([^}]+\\)}}$" s)
10135 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
10136 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
10137 (setq key (match-string 1 a) value (match-string 2 a)
10138 start (match-end 0)
10139 attr (plist-put attr (intern key) value))))
10140 (org-add-props s nil 'org-attr attr))
10143 ;;; Opening/following a link
10145 (defvar org-link-search-failed nil)
10147 (defvar org-open-link-functions nil
10148 "Hook for functions finding a plain text link.
10149 These functions must take a single argument, the link content.
10150 They will be called for links that look like [[link text][description]]
10151 when LINK TEXT does not have a protocol like \"http:\" and does not look
10152 like a filename (e.g. \"./blue.png\").
10154 These functions will be called *before* Org attempts to resolve the
10155 link by doing text searches in the current buffer - so if you want a
10156 link \"[[target]]\" to still find \"<<target>>\", your function should
10157 handle this as a special case.
10159 When the function does handle the link, it must return a non-nil value.
10160 If it decides that it is not responsible for this link, it must return
10161 nil to indicate that that Org-mode can continue with other options
10162 like exact and fuzzy text search.")
10164 (defun org-next-link (&optional search-backward)
10165 "Move forward to the next link.
10166 If the link is in hidden text, expose it."
10168 (when (and org-link-search-failed (eq this-command last-command))
10169 (goto-char (point-min))
10170 (message "Link search wrapped back to beginning of buffer"))
10171 (setq org-link-search-failed nil)
10172 (let* ((pos (point))
10174 (a (assoc :link ct))
10175 (srch-fun (if search-backward 're-search-backward 're-search-forward)))
10176 (cond (a (goto-char (nth (if search-backward 1 2) a)))
10177 ((looking-at org-any-link-re)
10178 ;; Don't stay stuck at link without an org-link face
10179 (forward-char (if search-backward -1 1))))
10180 (if (funcall srch-fun org-any-link-re nil t)
10182 (goto-char (match-beginning 0))
10183 (if (outline-invisible-p) (org-show-context)))
10185 (setq org-link-search-failed t)
10186 (message "No further link found"))))
10188 (defun org-previous-link ()
10189 "Move backward to the previous link.
10190 If the link is in hidden text, expose it."
10192 (funcall 'org-next-link t))
10194 (defun org-translate-link (s)
10195 "Translate a link string if a translation function has been defined."
10196 (if (and org-link-translation-function
10197 (fboundp org-link-translation-function)
10198 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
10200 (setq s (funcall org-link-translation-function
10201 (match-string 1 s) (match-string 2 s)))
10202 (concat (car s) ":" (cdr s)))
10205 (defun org-translate-link-from-planner (type path)
10206 "Translate a link from Emacs Planner syntax so that Org can follow it.
10207 This is still an experimental function, your mileage may vary."
10209 ((member type '("http" "https" "news" "ftp"))
10210 ;; standard Internet links are the same.
10212 ((and (equal type "irc") (string-match "^//" path))
10213 ;; Planner has two / at the beginning of an irc link, we have 1.
10214 ;; We should have zero, actually....
10215 (setq path (substring path 1)))
10216 ((and (equal type "lisp") (string-match "^/" path))
10217 ;; Planner has a slash, we do not.
10218 (setq type "elisp" path (substring path 1)))
10219 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
10220 ;; A typical message link. Planner has the id after the final slash,
10221 ;; we separate it with a hash mark
10222 (setq path (concat (match-string 1 path) "#"
10223 (org-remove-angle-brackets (match-string 2 path))))))
10226 (defun org-find-file-at-mouse (ev)
10227 "Open file link or URL at mouse."
10229 (mouse-set-point ev)
10230 (org-open-at-point 'in-emacs))
10232 (defun org-open-at-mouse (ev)
10233 "Open file link or URL at mouse.
10234 See the docstring of `org-open-file' for details."
10236 (mouse-set-point ev)
10237 (if (eq major-mode 'org-agenda-mode)
10238 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
10239 (org-open-at-point))
10241 (defvar org-window-config-before-follow-link nil
10242 "The window configuration before following a link.
10243 This is saved in case the need arises to restore it.")
10245 (defvar org-open-link-marker (make-marker)
10246 "Marker pointing to the location where `org-open-at-point; was called.")
10249 (defun org-open-at-point-global ()
10250 "Follow a link like Org-mode does.
10251 This command can be called in any mode to follow a link that has
10254 (org-run-like-in-org-mode 'org-open-at-point))
10257 (defun org-open-link-from-string (s &optional arg reference-buffer)
10258 "Open a link in the string S, as if it was in Org-mode."
10259 (interactive "sLink: \nP")
10260 (let ((reference-buffer (or reference-buffer (current-buffer))))
10262 (let ((org-inhibit-startup (not reference-buffer)))
10265 (goto-char (point-min))
10266 (when reference-buffer
10267 (setq org-link-abbrev-alist-local
10268 (with-current-buffer reference-buffer
10269 org-link-abbrev-alist-local)))
10270 (org-open-at-point arg reference-buffer)))))
10272 (defvar org-open-at-point-functions nil
10273 "Hook that is run when following a link at point.
10275 Functions in this hook must return t if they identify and follow
10276 a link at point. If they don't find anything interesting at point,
10277 they must return nil.")
10279 (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
10280 (defun org-open-at-point (&optional arg reference-buffer)
10281 "Open link at or after point.
10282 If there is no link at point, this function will search forward up to
10283 the end of the current line.
10284 Normally, files will be opened by an appropriate application. If the
10285 optional prefix argument ARG is non-nil, Emacs will visit the file.
10286 With a double prefix argument, try to open outside of Emacs, in the
10287 application the system uses for this file type."
10289 ;; if in a code block, then open the block's results
10290 (unless (call-interactively #'org-babel-open-src-block-result)
10291 (org-load-modules-maybe)
10292 (move-marker org-open-link-marker (point))
10293 (setq org-window-config-before-follow-link (current-window-configuration))
10294 (org-remove-occur-highlights nil nil t)
10296 ((and (org-at-heading-p)
10297 (not (org-at-timestamp-p t))
10298 (not (org-in-regexp
10299 (concat org-plain-link-re "\\|"
10300 org-bracket-link-regexp "\\|"
10301 org-angle-link-re "\\|"
10302 "[ \t]:[^ \t\n]+:[ \t]*$")))
10303 (not (get-text-property (point) 'org-linked-text)))
10304 (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
10306 (lk (if (stringp lk0) (list lk0) lk0))
10307 (lkend (cdr lkall)))
10309 (search-forward l nil lkend)
10310 (goto-char (match-beginning 0))
10311 (org-open-at-point))
10313 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
10314 ((run-hook-with-args-until-success 'org-open-at-point-functions))
10315 ((and (org-at-timestamp-p t)
10316 (not (org-in-regexp org-bracket-link-regexp)))
10317 (org-follow-timestamp-link))
10318 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
10319 (not (org-in-regexp org-any-link-re)))
10320 (org-footnote-action))
10322 (let (type path link line search (pos (point)))
10325 (skip-chars-forward "^]\n\r")
10326 (when (org-in-regexp org-bracket-link-regexp 1)
10327 (setq link (org-extract-attributes
10328 (org-link-unescape (org-match-string-no-properties 1))))
10329 (while (string-match " *\n *" link)
10330 (setq link (replace-match " " t t link)))
10331 (setq link (org-link-expand-abbrev link))
10333 ((or (file-name-absolute-p link)
10334 (string-match "^\\.\\.?/" link))
10335 (setq type "file" path link))
10336 ((string-match org-link-re-with-space3 link)
10337 (setq type (match-string 1 link) path (match-string 2 link)))
10338 ((string-match "^help:+\\(.+\\)" link)
10339 (setq type "help" path (match-string 1 link)))
10340 (t (setq type "thisfile" path link)))
10343 (when (get-text-property (point) 'org-linked-text)
10344 (setq type "thisfile"
10345 pos (if (get-text-property (1+ (point)) 'org-linked-text)
10346 (1+ (point)) (point))
10347 path (buffer-substring
10348 (or (previous-single-property-change pos 'org-linked-text)
10350 (or (next-single-property-change pos 'org-linked-text)
10352 ;; Ensure we will search for a <<<radio>>> link, not
10353 ;; a simple reference like <<ref>>
10354 path (concat "<" path))
10358 (when (or (org-in-regexp org-angle-link-re)
10359 (let ((match (org-in-regexp org-plain-link-re)))
10360 ;; Check a plain link is not within a bracket link
10364 (goto-char (car match))
10365 (not (org-in-regexp org-bracket-link-regexp))))))
10366 (let ((line_ending (save-excursion (end-of-line) (point))))
10367 ;; We are in a line before a plain or bracket link
10368 (or (re-search-forward org-plain-link-re line_ending t)
10369 (re-search-forward org-bracket-link-regexp line_ending t))))
10370 (setq type (match-string 1)
10371 path (org-link-unescape (match-string 2)))
10374 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
10376 path (match-string 1))
10377 (while (string-match ":" path)
10378 (setq path (replace-match "+" t t path)))
10380 (when (org-in-regexp "<\\([^><\n]+\\)>")
10381 (setq type "tree-match"
10382 path (match-string 1))
10385 (user-error "No link found"))
10387 ;; switch back to reference buffer
10388 ;; needed when if called in a temporary buffer through
10389 ;; org-open-link-from-string
10390 (with-current-buffer (or reference-buffer (current-buffer))
10392 ;; Remove any trailing spaces in path
10393 (if (string-match " +\\'" path)
10394 (setq path (replace-match "" t t path)))
10395 (if (and org-link-translation-function
10396 (fboundp org-link-translation-function))
10397 ;; Check if we need to translate the link
10398 (let ((tmp (funcall org-link-translation-function type path)))
10399 (setq type (car tmp) path (cdr tmp))))
10403 ((assoc type org-link-protocols)
10404 (funcall (nth 1 (assoc type org-link-protocols)) path))
10406 ((equal type "help")
10407 (let ((f-or-v (intern path)))
10408 (cond ((fboundp f-or-v)
10409 (describe-function f-or-v))
10411 (describe-variable f-or-v))
10412 (t (error "Not a known function or variable")))))
10414 ((equal type "mailto")
10415 (let ((cmd (car org-link-mailto-program))
10416 (args (cdr org-link-mailto-program)) args1
10417 (address path) (subject "") a)
10418 (if (string-match "\\(.*\\)::\\(.*\\)" path)
10419 (setq address (match-string 1 path)
10420 subject (org-link-escape (match-string 2 path))))
10423 ((not (stringp (car args))) (push (pop args) args1))
10424 (t (setq a (pop args))
10425 (if (string-match "%a" a)
10426 (setq a (replace-match address t t a)))
10427 (if (string-match "%s" a)
10428 (setq a (replace-match subject t t a)))
10430 (apply cmd (nreverse args1))))
10432 ((member type '("http" "https" "ftp" "news"))
10433 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
10435 path org-link-escape-chars-browser)
10438 ((string= type "doi")
10439 (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
10441 path org-link-escape-chars-browser)
10444 ((member type '("message"))
10445 (browse-url (concat type ":" path)))
10447 ((string= type "tags")
10448 (org-tags-view arg path))
10450 ((string= type "tree-match")
10451 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
10453 ((string= type "file")
10454 (if (string-match "::\\([0-9]+\\)\\'" path)
10455 (setq line (string-to-number (match-string 1 path))
10456 path (substring path 0 (match-beginning 0)))
10457 (if (string-match "::\\(.+\\)\\'" path)
10458 (setq search (match-string 1 path)
10459 path (substring path 0 (match-beginning 0)))))
10460 (if (string-match "[*?{]" (file-name-nondirectory path))
10462 (org-open-file path arg line search)))
10464 ((string= type "shell")
10465 (let ((buf (generate-new-buffer "*Org Shell Output"))
10467 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
10468 (string-match org-confirm-shell-link-not-regexp cmd))
10469 (not org-confirm-shell-link-function)
10470 (funcall org-confirm-shell-link-function
10471 (format "Execute \"%s\" in shell? "
10472 (org-add-props cmd nil
10473 'face 'org-warning))))
10475 (message "Executing %s" cmd)
10476 (shell-command cmd buf)
10477 (if (featurep 'midnight)
10478 (setq clean-buffer-list-kill-buffer-names
10479 (cons buf clean-buffer-list-kill-buffer-names))))
10482 ((string= type "elisp")
10484 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
10485 (string-match org-confirm-elisp-link-not-regexp cmd))
10486 (not org-confirm-elisp-link-function)
10487 (funcall org-confirm-elisp-link-function
10488 (format "Execute \"%s\" as elisp? "
10489 (org-add-props cmd nil
10490 'face 'org-warning))))
10491 (message "%s => %s" cmd
10492 (if (equal (string-to-char cmd) ?\()
10494 (call-interactively (read cmd))))
10497 ((and (string= type "thisfile")
10498 (run-hook-with-args-until-success
10499 'org-open-link-functions path)))
10501 ((string= type "thisfile")
10503 (switch-to-buffer-other-window
10504 (org-get-buffer-for-internal-link (current-buffer)))
10505 (org-mark-ring-push))
10506 (let ((cmd `(org-link-search
10508 ,(cond ((equal arg '(4)) ''occur)
10509 ((equal arg '(16)) ''org-occur))
10511 (condition-case nil (let ((org-link-search-inhibit-query t))
10513 (error (progn (widen) (eval cmd))))))
10515 (t (browse-url-at-point)))))))
10516 (move-marker org-open-link-marker nil)
10517 (run-hook-with-args 'org-follow-link-hook)))
10519 (defun org-offer-links-in-entry (buffer marker &optional nth zero)
10520 "Offer links in the current entry and return the selected link.
10521 If there is only one link, return it.
10522 If NTH is an integer, return the NTH link found.
10523 If ZERO is a string, check also this string for a link, and if
10524 there is one, return it."
10525 (with-current-buffer buffer
10530 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
10531 "\\(" org-angle-link-re "\\)\\|"
10532 "\\(" org-plain-link-re "\\)"))
10534 (in-emacs (if (integerp nth) nil nth))
10535 have-zero end links link c)
10536 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
10537 (push (match-string 0 zero) links)
10538 (setq cnt (1- cnt) have-zero t))
10540 (org-back-to-heading t)
10541 (setq end (save-excursion (outline-next-heading) (point)))
10542 (while (re-search-forward re end t)
10543 (push (match-string 0) links))
10544 (setq links (org-uniquify (reverse links))))
10547 (message "No links"))
10548 ((equal (length links) 1)
10549 (setq link (car links)))
10550 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
10551 (setq link (nth (if have-zero nth (1- nth)) links)))
10552 (t ; we have to select a link
10554 (save-window-excursion
10555 (delete-other-windows)
10556 (with-output-to-temp-buffer "*Select Link*"
10558 (if (not (string-match org-bracket-link-regexp l))
10559 (princ (format "[%c] %s\n" (incf cnt)
10560 (org-remove-angle-brackets l)))
10562 (princ (format "[%c] %s (%s)\n" (incf cnt)
10563 (match-string 3 l) (match-string 1 l)))
10564 (princ (format "[%c] %s\n" (incf cnt)
10565 (match-string 1 l))))))
10567 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
10568 (message "Select link to open, RET to open all:")
10569 (setq c (read-char-exclusive))
10570 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
10571 (when (equal c ?q) (error "Abort"))
10572 (if (equal c ?\C-m)
10574 (setq nth (- c ?0))
10575 (if have-zero (setq nth (1+ nth)))
10576 (unless (and (integerp nth) (>= (length links) nth))
10577 (user-error "Invalid link selection"))
10578 (setq link (nth (1- nth) links)))))
10579 (cons link end))))))
10581 ;; Add special file links that specify the way of opening
10583 (org-add-link-type "file+sys" 'org-open-file-with-system)
10584 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
10585 (defun org-open-file-with-system (path)
10586 "Open file at PATH using the system way of opening it."
10587 (org-open-file path 'system))
10588 (defun org-open-file-with-emacs (path)
10589 "Open file at PATH in Emacs."
10590 (org-open-file path 'emacs))
10595 (defvar org-create-file-search-functions nil
10596 "List of functions to construct the right search string for a file link.
10597 These functions are called in turn with point at the location to
10598 which the link should point.
10600 A function in the hook should first test if it would like to
10601 handle this file type, for example by checking the `major-mode'
10602 or the file extension. If it decides not to handle this file, it
10603 should just return nil to give other functions a chance. If it
10604 does handle the file, it must return the search string to be used
10605 when following the link. The search string will be part of the
10606 file link, given after a double colon, and `org-open-at-point'
10607 will automatically search for it. If special measures must be
10608 taken to make the search successful, another function should be
10609 added to the companion hook `org-execute-file-search-functions',
10612 A function in this hook may also use `setq' to set the variable
10613 `description' to provide a suggestion for the descriptive text to
10614 be used for this link when it gets inserted into an Org-mode
10615 buffer with \\[org-insert-link].")
10617 (defvar org-execute-file-search-functions nil
10618 "List of functions to execute a file search triggered by a link.
10620 Functions added to this hook must accept a single argument, the
10621 search string that was part of the file link, the part after the
10622 double colon. The function must first check if it would like to
10623 handle this search, for example by checking the `major-mode' or
10624 the file extension. If it decides not to handle this search, it
10625 should just return nil to give other functions a chance. If it
10626 does handle the search, it must return a non-nil value to keep
10627 other functions from trying.
10629 Each function can access the current prefix argument through the
10630 variable `current-prefix-arg'. Note that a single prefix is used
10631 to force opening a link in Emacs, so it may be good to only use a
10632 numeric or double prefix to guide the search function.
10634 In case this is needed, a function in this hook can also restore
10635 the window configuration before `org-open-at-point' was called using:
10637 (set-window-configuration org-window-config-before-follow-link)")
10639 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
10640 (defun org-link-search (s &optional type avoid-pos stealth)
10641 "Search for a link search option.
10642 If S is surrounded by forward slashes, it is interpreted as a
10643 regular expression. In org-mode files, this will create an `org-occur'
10644 sparse tree. In ordinary files, `occur' will be used to list matches.
10645 If the current buffer is in `dired-mode', grep will be used to search
10646 in all files. If AVOID-POS is given, ignore matches near that position.
10648 When optional argument STEALTH is non-nil, do not modify
10649 visibility around point, thus ignoring
10650 `org-show-hierarchy-above', `org-show-following-heading' and
10651 `org-show-siblings' variables."
10652 (let ((case-fold-search t)
10653 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
10654 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
10655 (append '(("") (" ") ("\t") ("\n"))
10656 org-emphasis-alist)
10659 (pre nil) (post nil)
10660 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
10662 ;; First check if there are any special search functions
10663 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
10664 ;; Now try the builtin stuff
10665 ((and (equal (string-to-char s0) ?#)
10668 (goto-char (point-min))
10671 (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
10672 (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
10673 (setq type 'dedicated
10674 pos (match-beginning 0))))
10675 ;; There is an exact target for this
10677 (org-back-to-heading t)))
10679 (goto-char (point-min))
10682 (concat "<<" (regexp-quote s0) ">>") nil t)
10683 (setq type 'dedicated
10684 pos (match-beginning 0))))
10685 ;; There is an exact target for this
10688 (goto-char (point-min))
10691 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
10692 (setq type 'dedicated pos (match-beginning 0))))
10693 ;; Found an element with a matching #+name affiliated keyword.
10695 ((and (string-match "^(\\(.*\\))$" s0)
10697 (goto-char (point-min))
10700 (concat "[^[]" (regexp-quote
10701 (format org-coderef-label-format
10702 (match-string 1 s0))))
10704 (setq type 'dedicated
10705 pos (1+ (match-beginning 0))))))
10706 ;; There is a coderef target for this
10708 ((string-match "^/\\(.*\\)/$" s)
10709 ;; A regular expression
10711 ((derived-mode-p 'org-mode)
10712 (org-occur (match-string 1 s)))
10713 (t (org-do-occur (match-string 1 s)))))
10714 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
10715 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
10716 (goto-char (point-min))
10718 ((let (case-fold-search)
10719 (re-search-forward (format org-complex-heading-regexp-format
10722 ;; OK, found a match
10723 (setq type 'dedicated)
10724 (goto-char (match-beginning 0)))
10725 ((and (not org-link-search-inhibit-query)
10726 (eq org-link-search-must-match-exact-headline 'query-to-create)
10727 (y-or-n-p "No match - create this as a new heading? "))
10728 (goto-char (point-max))
10729 (or (bolp) (newline))
10730 (insert "* " s "\n")
10731 (beginning-of-line 0))
10734 (error "No match"))))
10736 ;; A normal search string
10737 (when (equal (string-to-char s) ?*)
10738 ;; Anchor on headlines, post may include tags.
10739 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
10740 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
10741 s (substring s 1)))
10742 (remove-text-properties
10744 '(face nil mouse-face nil keymap nil fontified nil) s)
10745 ;; Make a series of regular expressions to find a match
10746 (setq words (org-split-string s "[ \n\r\t]+")
10748 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
10749 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
10751 re2a_ (concat "\\(" (mapconcat 'downcase words
10752 "[ \t\r\n]+") "\\)[ \t\r\n]")
10753 re2a (concat "[ \t\r\n]" re2a_)
10754 re4_ (concat "\\(" (mapconcat 'downcase words
10755 "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
10756 re4 (concat "[^a-zA-Z_]" re4_)
10758 re1 (concat pre re2 post)
10759 re3 (concat pre (if pre re4_ re4) post)
10760 re5 (concat pre ".*" re4)
10761 re2 (concat pre re2)
10762 re2a (concat pre (if pre re2a_ re2a))
10763 re4 (concat pre (if pre re4_ re4))
10764 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
10765 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10768 ((eq type 'org-occur) (org-occur reall))
10769 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10770 (t (goto-char (point-min))
10772 (if (or (and (org-search-not-self 1 re0 nil t)
10773 (setq type 'dedicated))
10774 (org-search-not-self 1 re1 nil t)
10775 (org-search-not-self 1 re2 nil t)
10776 (org-search-not-self 1 re2a nil t)
10777 (org-search-not-self 1 re3 nil t)
10778 (org-search-not-self 1 re4 nil t)
10779 (org-search-not-self 1 re5 nil t))
10780 (goto-char (match-beginning 1))
10782 (error "No match"))))))
10783 (and (derived-mode-p 'org-mode)
10785 (org-show-context 'link-search))
10788 (defun org-search-not-self (group &rest args)
10789 "Execute `re-search-forward', but only accept matches that do not
10790 enclose the position of `org-open-link-marker'."
10791 (let ((m org-open-link-marker))
10793 (while (apply 're-search-forward args)
10794 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10795 (goto-char (match-end group))
10796 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10797 (> (match-beginning 0) (marker-position m))
10798 (< (match-end 0) (marker-position m)))
10800 (or (not (org-in-regexp
10801 org-bracket-link-analytic-regexp 1))
10802 (not (match-end 4)) ; no description
10803 (and (<= (match-beginning 4) (point))
10804 (>= (match-end 4) (point))))))
10805 (throw 'exit (point))))))))
10807 (defun org-get-buffer-for-internal-link (buffer)
10808 "Return a buffer to be used for displaying the link target of internal links."
10810 ((not org-display-internal-link-with-indirect-buffer)
10812 ((string-match "(Clone)$" (buffer-name buffer))
10813 (message "Buffer is already a clone, not making another one")
10814 ;; we also do not modify visibility in this case
10816 (t ; make a new indirect buffer for displaying the link
10817 (let* ((bn (buffer-name buffer))
10818 (ibn (concat bn "(Clone)"))
10819 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10820 (with-current-buffer ib (org-overview))
10823 (defun org-do-occur (regexp &optional cleanup)
10824 "Call the Emacs command `occur'.
10825 If CLEANUP is non-nil, remove the printout of the regular expression
10826 in the *Occur* buffer. This is useful if the regex is long and not useful
10830 (let ((cwin (selected-window)) win beg end)
10831 (when (setq win (get-buffer-window "*Occur*"))
10832 (select-window win))
10833 (goto-char (point-min))
10834 (when (re-search-forward "match[a-z]+" nil t)
10835 (setq beg (match-end 0))
10836 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10837 (setq end (1- (match-beginning 0)))))
10838 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10839 (goto-char (point-min))
10840 (select-window cwin))))
10842 ;;; The mark ring for links jumps
10844 (defvar org-mark-ring nil
10845 "Mark ring for positions before jumps in Org-mode.")
10846 (defvar org-mark-ring-last-goto nil
10847 "Last position in the mark ring used to go back.")
10848 ;; Fill and close the ring
10849 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10850 (loop for i from 1 to org-mark-ring-length do
10851 (push (make-marker) org-mark-ring))
10852 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10855 (defun org-mark-ring-push (&optional pos buffer)
10856 "Put the current position or POS into the mark ring and rotate it."
10858 (setq pos (or pos (point)))
10859 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10860 (move-marker (car org-mark-ring)
10862 (or buffer (current-buffer)))
10864 (substitute-command-keys
10865 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10867 (defun org-mark-ring-goto (&optional n)
10868 "Jump to the previous position in the mark ring.
10869 With prefix arg N, jump back that many stored positions. When
10870 called several times in succession, walk through the entire ring.
10871 Org-mode commands jumping to a different position in the current file,
10872 or to another Org-mode file, automatically push the old position
10876 (if (eq last-command this-command)
10877 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10878 (setq p org-mark-ring))
10879 (setq org-mark-ring-last-goto p)
10881 (org-pop-to-buffer-same-window (marker-buffer m))
10883 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10885 (defun org-remove-angle-brackets (s)
10886 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10887 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10889 (defun org-add-angle-brackets (s)
10890 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10891 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10893 (defun org-remove-double-quotes (s)
10894 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10895 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10898 ;;; Following specific links
10900 (defun org-follow-timestamp-link ()
10901 "Open an agenda view for the time-stamp date/range at point."
10903 ((org-at-date-range-p t)
10904 (let ((org-agenda-start-on-weekday)
10905 (t1 (match-string 1))
10906 (t2 (match-string 2)) tt1 tt2)
10907 (setq tt1 (time-to-days (org-time-string-to-time t1))
10908 tt2 (time-to-days (org-time-string-to-time t2)))
10909 (let ((org-agenda-buffer-tmp-name
10910 (format "*Org Agenda(a:%s)"
10911 (concat (substring t1 0 10) "--" (substring t2 0 10)))))
10912 (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
10913 ((org-at-timestamp-p t)
10914 (let ((org-agenda-buffer-tmp-name
10915 (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
10916 (org-agenda-list nil (time-to-days (org-time-string-to-time
10917 (substring (match-string 1) 0 10)))
10919 (t (error "This should not happen"))))
10922 ;;; Following file links
10923 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10924 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10925 (declare-function mailcap-mime-info
10926 "mailcap" (string &optional request no-decode))
10927 (defvar org-wait nil)
10928 (defun org-open-file (path &optional in-emacs line search)
10929 "Open the file at PATH.
10930 First, this expands any special file name abbreviations. Then the
10931 configuration variable `org-file-apps' is checked if it contains an
10932 entry for this file type, and if yes, the corresponding command is launched.
10934 If no application is found, Emacs simply visits the file.
10936 With optional prefix argument IN-EMACS, Emacs will visit the file.
10937 With a double \\[universal-argument] \\[universal-argument] \
10938 prefix arg, Org tries to avoid opening in Emacs
10939 and to use an external application to visit the file.
10941 Optional LINE specifies a line to go to, optional SEARCH a string
10942 to search for. If LINE or SEARCH is given, the file will be
10943 opened in Emacs, unless an entry from org-file-apps that makes
10944 use of groups in a regexp matches.
10946 If you want to change the way frames are used when following a
10947 link, please customize `org-link-frame-setup'.
10949 If the file does not exist, an error is thrown."
10950 (let* ((file (if (equal path "")
10952 (substitute-in-file-name (expand-file-name path))))
10953 (file-apps (append org-file-apps (org-default-apps)))
10954 (apps (org-remove-if
10955 'org-file-apps-entry-match-against-dlink-p file-apps))
10956 (apps-dlink (org-remove-if-not
10957 'org-file-apps-entry-match-against-dlink-p file-apps))
10958 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10959 (dirp (if remp nil (file-directory-p file)))
10960 (file (if (and dirp org-open-directory-means-index-dot-org)
10961 (concat (file-name-as-directory file) "index.org")
10963 (a-m-a-p (assq 'auto-mode apps))
10964 (dfile (downcase file))
10965 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10966 (link (cond ((and (eq line nil)
10970 (concat file "::" (number-to-string line)))
10972 (concat file "::" search))))
10973 (dlink (downcase link))
10974 (old-buffer (current-buffer))
10976 (old-mode major-mode)
10977 ext cmd link-match-data)
10978 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10979 (setq ext (match-string 1 dfile))
10980 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10981 (setq ext (match-string 1 dfile))))
10983 ((member in-emacs '((16) system))
10984 (setq cmd (cdr (assoc 'system apps))))
10985 (in-emacs (setq cmd 'emacs))
10987 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10988 (and dirp (cdr (assoc 'directory apps)))
10989 ; first, try matching against apps-dlink
10990 ; if we get a match here, store the match data for later
10991 (let ((match (assoc-default dlink apps-dlink
10994 (progn (setq link-match-data (match-data))
10996 (progn (setq in-emacs (or in-emacs line search))
10997 nil))) ; if we have no match in apps-dlink,
10998 ; always open the file in emacs if line or search
10999 ; is given (for backwards compatibility)
11000 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
11002 (cdr (assoc ext apps))
11003 (cdr (assoc t apps))))))
11004 (when (eq cmd 'system)
11005 (setq cmd (cdr (assoc 'system apps))))
11006 (when (eq cmd 'default)
11007 (setq cmd (cdr (assoc t apps))))
11008 (when (eq cmd 'mailcap)
11010 (mailcap-parse-mailcaps)
11011 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
11012 (command (mailcap-mime-info mime-type)))
11013 (if (stringp command)
11015 (setq cmd 'emacs))))
11016 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
11017 (not (file-exists-p file))
11018 (not org-open-non-existing-files))
11019 (user-error "No such file: %s" file))
11021 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
11022 ;; Remove quotes around the file name - we'll use shell-quote-argument.
11023 (while (string-match "['\"]%s['\"]" cmd)
11024 (setq cmd (replace-match "%s" t t cmd)))
11025 (while (string-match "%s" cmd)
11026 (setq cmd (replace-match
11028 (shell-quote-argument
11029 (convert-standard-filename file)))
11032 ;; Replace "%1", "%2" etc. in command with group matches from regex
11034 (let ((match-index 1)
11035 (number-of-groups (- (/ (length link-match-data) 2) 1)))
11036 (set-match-data link-match-data)
11037 (while (<= match-index number-of-groups)
11038 (let ((regex (concat "%" (number-to-string match-index)))
11039 (replace-with (match-string match-index dlink)))
11040 (while (string-match regex cmd)
11041 (setq cmd (replace-match replace-with t t cmd))))
11042 (setq match-index (+ match-index 1)))))
11044 (save-window-excursion
11045 (message "Running %s...done" cmd)
11046 (start-process-shell-command cmd nil cmd)
11047 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
11050 (funcall (cdr (assq 'file org-link-frame-setup)) file)
11052 (if line (org-goto-line line)
11053 (if search (org-link-search search))))
11055 (let ((file (convert-standard-filename file)))
11057 (set-match-data link-match-data)
11059 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
11060 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
11061 (or (not (equal old-buffer (current-buffer)))
11062 (not (equal old-pos (point))))
11063 (org-mark-ring-push old-pos old-buffer))))
11065 (defun org-file-apps-entry-match-against-dlink-p (entry)
11066 "This function returns non-nil if `entry' uses a regular
11067 expression which should be matched against the whole link by
11070 It assumes that is the case when the entry uses a regular
11071 expression which has at least one grouping construct and the
11072 action is either a lisp form or a command string containing
11073 '%1', i.e. using at least one subexpression match as a
11075 (let ((selector (car entry))
11076 (action (cdr entry)))
11077 (if (stringp selector)
11078 (and (> (regexp-opt-depth selector) 0)
11079 (or (and (stringp action)
11080 (string-match "%[0-9]" action))
11084 (defun org-default-apps ()
11085 "Return the default applications for this operating system."
11087 ((eq system-type 'darwin)
11088 org-file-apps-defaults-macosx)
11089 ((eq system-type 'windows-nt)
11090 org-file-apps-defaults-windowsnt)
11091 (t org-file-apps-defaults-gnu)))
11093 (defun org-apps-regexp-alist (list &optional add-auto-mode)
11094 "Convert extensions to regular expressions in the cars of LIST.
11095 Also, weed out any non-string entries, because the return value is used
11096 only for regexp matching.
11097 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
11098 point to the symbol `emacs', indicating that the file should
11099 be opened in Emacs."
11102 (mapcar (lambda (x)
11103 (if (not (stringp (car x)))
11105 (if (string-match "\\W" (car x))
11107 (cons (concat "\\." (car x) "\\'") (cdr x)))))
11110 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
11112 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
11113 (defun org-file-remote-p (file)
11114 "Test whether FILE specifies a location on a remote system.
11115 Return non-nil if the location is indeed remote.
11117 For example, the filename \"/user@host:/foo\" specifies a location
11118 on the system \"/user@host:\"."
11119 (cond ((fboundp 'file-remote-p)
11120 (file-remote-p file))
11121 ((fboundp 'tramp-handle-file-remote-p)
11122 (tramp-handle-file-remote-p file))
11123 ((and (boundp 'ange-ftp-name-format)
11124 (string-match (car ange-ftp-name-format) file))
11130 (defun org-get-org-file ()
11131 "Read a filename, with default directory `org-directory'."
11132 (let ((default (or org-default-notes-file remember-data-file)))
11133 (read-file-name (format "File name [%s]: " default)
11134 (file-name-as-directory org-directory)
11137 (defun org-notes-order-reversed-p ()
11138 "Check if the current file should receive notes in reversed order."
11140 ((not org-reverse-note-order) nil)
11141 ((eq t org-reverse-note-order) t)
11142 ((not (listp org-reverse-note-order)) nil)
11144 (let ((all org-reverse-note-order)
11146 (while (setq entry (pop all))
11147 (if (string-match (car entry) buffer-file-name)
11148 (throw 'exit (cdr entry))))
11151 (defvar org-refile-target-table nil
11152 "The list of refile targets, created by `org-refile'.")
11154 (defvar org-agenda-new-buffers nil
11155 "Buffers created to visit agenda files.")
11157 (defvar org-refile-cache nil
11158 "Cache for refile targets.")
11160 (defvar org-refile-markers nil
11161 "All the markers used for caching refile locations.")
11163 (defun org-refile-marker (pos)
11164 "Get a new refile marker, but only if caching is in use."
11165 (if (not org-refile-use-cache)
11167 (let ((m (make-marker)))
11168 (move-marker m pos)
11169 (push m org-refile-markers)
11172 (defun org-refile-cache-clear ()
11173 "Clear the refile cache and disable all the markers."
11174 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
11175 (setq org-refile-markers nil)
11176 (setq org-refile-cache nil)
11177 (message "Refile cache has been cleared"))
11179 (defun org-refile-cache-check-set (set)
11180 "Check if all the markers in the cache still have live buffers."
11183 (while (and set (setq marker (nth 3 (pop set))))
11184 ;; If `org-refile-use-outline-path' is 'file, marker may be nil
11185 (when (and marker (null (marker-buffer marker)))
11186 (message "Please regenerate the refile cache with `C-0 C-c C-w'")
11188 (throw 'exit nil)))
11191 (defun org-refile-cache-put (set &rest identifiers)
11192 "Push the refile targets SET into the cache, under IDENTIFIERS."
11193 (let* ((key (sha1 (prin1-to-string identifiers)))
11194 (entry (assoc key org-refile-cache)))
11197 (push (cons key set) org-refile-cache))))
11199 (defun org-refile-cache-get (&rest identifiers)
11200 "Retrieve the cached value for refile targets given by IDENTIFIERS."
11202 ((not org-refile-cache) nil)
11203 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
11205 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
11206 org-refile-cache))))
11207 (and set (org-refile-cache-check-set set) set)))))
11209 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
11210 "Produce a table with refile targets."
11211 (let ((case-fold-search nil)
11212 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
11213 (entries (or org-refile-targets '((nil . (:level . 1)))))
11214 targets tgs txt re files f desc descre fast-path-p level pos0)
11215 (message "Getting targets...")
11216 (with-current-buffer (or default-buffer (current-buffer))
11217 (while (setq entry (pop entries))
11218 (setq files (car entry) desc (cdr entry))
11219 (setq fast-path-p nil)
11221 ((null files) (setq files (list (current-buffer))))
11222 ((eq files 'org-agenda-files)
11223 (setq files (org-agenda-files 'unrestricted)))
11224 ((and (symbolp files) (fboundp files))
11225 (setq files (funcall files)))
11226 ((and (symbolp files) (boundp files))
11227 (setq files (symbol-value files))))
11228 (if (stringp files) (setq files (list files)))
11230 ((eq (car desc) :tag)
11231 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
11232 ((eq (car desc) :todo)
11233 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
11234 ((eq (car desc) :regexp)
11235 (setq descre (cdr desc)))
11236 ((eq (car desc) :level)
11237 (setq descre (concat "^\\*\\{" (number-to-string
11238 (if org-odd-levels-only
11239 (1- (* 2 (cdr desc)))
11242 ((eq (car desc) :maxlevel)
11243 (setq fast-path-p t)
11244 (setq descre (concat "^\\*\\{1," (number-to-string
11245 (if org-odd-levels-only
11246 (1- (* 2 (cdr desc)))
11249 (t (error "Bad refiling target description %s" desc)))
11250 (while (setq f (pop files))
11251 (with-current-buffer
11252 (if (bufferp f) f (org-get-agenda-file-buffer f))
11254 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
11256 (if (bufferp f) (setq f (buffer-file-name
11257 (buffer-base-buffer f))))
11258 (setq f (and f (expand-file-name f)))
11259 (if (eq org-refile-use-outline-path 'file)
11260 (push (list (file-name-nondirectory f) f nil nil) tgs))
11264 (goto-char (point-min))
11265 (while (re-search-forward descre nil t)
11266 (goto-char (setq pos0 (point-at-bol)))
11268 (when org-refile-target-verify-function
11270 (or (funcall org-refile-target-verify-function)
11272 (when (and (looking-at org-complex-heading-regexp)
11273 (not (member (match-string 4) excluded-entries))
11275 (setq level (org-reduced-level
11276 (- (match-end 1) (match-beginning 1)))
11277 txt (org-link-display-format (match-string 4))
11278 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
11279 re (format org-complex-heading-regexp-format
11280 (regexp-quote (match-string 4))))
11281 (when org-refile-use-outline-path
11282 (setq txt (mapconcat
11285 (if (eq org-refile-use-outline-path
11287 (list (file-name-nondirectory
11289 (buffer-base-buffer))))
11290 (if (eq org-refile-use-outline-path
11292 (list (buffer-file-name
11293 (buffer-base-buffer)))))
11294 (org-get-outline-path fast-path-p
11298 (push (list txt f re (org-refile-marker (point)))
11300 (when (= (point) pos0)
11301 ;; verification function has not moved point
11302 (goto-char (point-at-eol))))))))
11303 (when org-refile-use-cache
11304 (org-refile-cache-put tgs (buffer-file-name) descre))
11305 (setq targets (append tgs targets))))))
11306 (message "Getting targets...done")
11307 (nreverse targets)))
11309 (defun org-protect-slash (s)
11310 (while (string-match "/" s)
11311 (setq s (replace-match "\\" t t s)))
11314 (defvar org-olpa (make-vector 20 nil))
11316 (defun org-get-outline-path (&optional fastp level heading)
11317 "Return the outline path to the current entry, as a list.
11319 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
11320 routine which makes outline path derivations for an entire file,
11321 avoiding backtracing. Refile target collection makes use of that."
11325 (error "Outline path failure, more than 19 levels"))
11326 (loop for i from level upto 19 do
11327 (aset org-olpa i nil))
11329 (delq nil (append org-olpa nil))
11330 (aset org-olpa level heading)))
11331 (let (rtn case-fold-search)
11335 (while (org-up-heading-safe)
11336 (when (looking-at org-complex-heading-regexp)
11338 (replace-regexp-in-string
11339 ;; Remove statistical/checkboxes cookies
11340 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
11341 (org-match-string-no-properties 4)))
11345 (defun org-format-outline-path (path &optional width prefix separator)
11346 "Format the outline path PATH for display.
11347 WIDTH is the maximum number of characters that is available.
11348 PREFIX is a prefix to be included in the returned string,
11349 such as the file name.
11350 SEPARATOR is inserted between the different parts of the path,
11351 the default is \"/\"."
11352 (setq width (or width 79))
11353 (if prefix (setq width (- width (length prefix))))
11356 (let* ((nsteps (length path))
11357 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
11358 (maxwidth (if (<= total-width width)
11359 10000 ;; everything fits
11360 ;; we need to shorten the level headings
11361 (/ (- width nsteps) nsteps)))
11362 (org-odd-levels-only nil)
11364 (total (1+ (length prefix))))
11365 (setq maxwidth (max maxwidth 10))
11367 (if prefix (or separator "/"))
11371 (if (and (= n nsteps) (< maxwidth 10000))
11372 (setq maxwidth (- total-width total)))
11373 (if (< (length h) maxwidth)
11374 (progn (setq total (+ total (length h) 1)) h)
11375 (setq h (substring h 0 (- maxwidth 2))
11376 total (+ total maxwidth 1))
11377 (if (string-match "[ \t]+\\'" h)
11378 (setq h (substring h 0 (match-beginning 0))))
11379 (setq h (concat h "..")))
11380 (org-add-props h nil 'face
11381 (nth (% (1- n) org-n-level-faces)
11384 path (or separator "/"))))))
11386 (defun org-display-outline-path (&optional file current separator just-return-string)
11387 "Display the current outline path in the echo area.
11389 If FILE is non-nil, prepend the output with the file name.
11390 If CURRENT is non-nil, append the current heading to the output.
11391 SEPARATOR is passed through to `org-format-outline-path'. It separates
11392 the different parts of the path and defaults to \"/\".
11393 If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
11395 (let* (case-fold-search
11396 message-log-max ; Don't populate the *Messages* buffer
11397 (bfn (buffer-file-name (buffer-base-buffer)))
11398 (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
11400 (if current (setq path (append path
11402 (org-back-to-heading t)
11403 (if (looking-at org-complex-heading-regexp)
11404 (list (match-string 4)))))))
11406 (org-format-outline-path
11409 (and file bfn (concat (file-name-nondirectory bfn) separator))
11411 (if just-return-string
11412 (org-no-properties res)
11413 (message "%s" res))))
11415 (defvar org-refile-history nil
11416 "History for refiling operations.")
11418 (defvar org-after-refile-insert-hook nil
11419 "Hook run after `org-refile' has inserted its stuff at the new location.
11420 Note that this is still *before* the stuff will be removed from
11421 the *old* location.")
11423 (defvar org-capture-last-stored-marker)
11424 (defvar org-refile-keep nil
11425 "Non-nil means `org-refile' will copy instead of refile.")
11428 "Like `org-refile', but copy."
11430 (let ((org-refile-keep t))
11431 (funcall 'org-refile nil nil nil "Copy")))
11433 (defun org-refile (&optional goto default-buffer rfloc msg)
11434 "Move the entry or entries at point to another heading.
11435 The list of target headings is compiled using the information in
11436 `org-refile-targets', which see.
11438 At the target location, the entry is filed as a subitem of the target
11439 heading. Depending on `org-reverse-note-order', the new subitem will
11440 either be the first or the last subitem.
11442 If there is an active region, all entries in that region will be moved.
11443 However, the region must fulfill the requirement that the first heading
11444 is the first one sets the top-level of the moved text - at most siblings
11445 below it are allowed.
11447 With prefix arg GOTO, the command will only visit the target location
11448 and not actually move anything.
11450 With a double prefix arg \\[universal-argument] \\[universal-argument], \
11451 go to the location where the last refiling operation has put the subtree.
11452 With a prefix argument of `2', refile to the running clock.
11454 RFLOC can be a refile location obtained in a different way.
11456 MSG is a string to replace \"Refile\" in the default prompt with
11457 another verb. E.g. `org-copy' sets this parameter to \"Copy\".
11459 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
11461 If you are using target caching (see `org-refile-use-cache'),
11462 you have to clear the target cache in order to find new targets.
11463 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
11464 prefix argument (`C-u C-u C-u C-c C-w')."
11467 (if (member goto '(0 (64)))
11468 (org-refile-cache-clear)
11469 (let* ((actionmsg (or msg "Refile"))
11470 (cbuf (current-buffer))
11471 (regionp (org-region-active-p))
11472 (region-start (and regionp (region-beginning)))
11473 (region-end (and regionp (region-end)))
11474 (region-length (and regionp (- region-end region-start)))
11475 (filename (buffer-file-name (buffer-base-buffer cbuf)))
11476 pos it nbuf file re level reversed)
11477 (setq last-command nil)
11479 (goto-char region-start)
11480 (or (bolp) (goto-char (point-at-bol)))
11481 (setq region-start (point))
11482 (unless (or (org-kill-is-subtree-p
11483 (buffer-substring region-start region-end))
11484 (prog1 org-refile-active-region-within-subtree
11485 (org-toggle-heading)))
11486 (user-error "The region is not a (sequence of) subtree(s)")))
11487 (if (equal goto '(16))
11488 (org-refile-goto-last-stored)
11490 (and (equal goto 2)
11491 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
11493 (setq it (list (or org-clock-heading "running clock")
11495 (marker-buffer org-clock-hd-marker))
11497 (marker-position org-clock-hd-marker)))
11500 (let (heading-text)
11503 (org-back-to-heading t)
11505 (nth 4 (org-heading-components))))
11507 (org-refile-get-location
11508 (cond (goto "Goto")
11509 (regionp (concat actionmsg " region to"))
11510 (t (concat actionmsg " subtree \""
11511 heading-text "\" to")))
11513 (and (not (equal '(4) goto))
11514 org-refile-allow-creating-parent-nodes)
11516 (setq file (nth 1 it)
11519 (if (and (not goto)
11521 (equal (buffer-file-name) file)
11523 (and (>= pos region-start)
11524 (<= pos region-end))
11525 (and (>= pos (point))
11526 (< pos (save-excursion
11527 (org-end-of-subtree t t))))))
11528 (error "Cannot refile to position inside the tree or region"))
11530 (setq nbuf (or (find-buffer-visiting file)
11531 (find-file-noselect file)))
11534 (org-pop-to-buffer-same-window nbuf)
11536 (org-show-context 'org-goto))
11539 (org-kill-new (buffer-substring region-start region-end))
11540 (org-save-markers-in-region region-start region-end))
11541 (org-copy-subtree 1 nil t))
11542 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
11543 (find-file-noselect file)))
11544 (setq reversed (org-notes-order-reversed-p))
11551 (looking-at org-outline-regexp)
11552 (setq level (org-get-valid-level (funcall outline-level) 1))
11555 (or (outline-next-heading) (point-max))
11556 (or (save-excursion (org-get-next-sibling))
11557 (org-end-of-subtree t t)
11561 (goto-char (point-max))
11562 (goto-char (point-min))
11563 (or (outline-next-heading) (goto-char (point-max)))))
11564 (if (not (bolp)) (newline))
11565 (org-paste-subtree level)
11566 (when org-log-refile
11567 (org-add-log-setup 'refile nil nil 'findpos
11569 (unless (eq org-log-refile 'note)
11570 (save-excursion (org-add-log-note))))
11571 (and org-auto-align-tags
11572 (let ((org-loop-over-headlines-in-active-region nil))
11573 (org-set-tags nil t)))
11574 (with-demoted-errors
11575 (bookmark-set "org-refile-last-stored"))
11576 ;; If we are refiling for capture, make sure that the
11577 ;; last-capture pointers point here
11578 (when (org-bound-and-true-p org-refile-for-capture)
11579 (with-demoted-errors
11580 (bookmark-set "org-capture-last-stored-marker"))
11581 (move-marker org-capture-last-stored-marker (point)))
11582 (if (fboundp 'deactivate-mark) (deactivate-mark))
11583 (run-hooks 'org-after-refile-insert-hook))))
11584 (unless org-refile-keep
11586 (delete-region (point) (+ (point) region-length))
11587 (org-cut-subtree)))
11588 (when (featurep 'org-inlinetask)
11589 (org-inlinetask-remove-END-maybe))
11590 (setq org-markers-to-move nil)
11591 (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
11593 (defun org-refile-goto-last-stored ()
11594 "Go to the location where the last refile was stored."
11596 (bookmark-jump "org-refile-last-stored")
11597 (message "This is the location of the last refile"))
11599 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
11601 "Prompt the user for a refile location, using PROMPT.
11602 PROMPT should not be suffixed with a colon and a space, because
11603 this function appends the default value from
11604 `org-refile-history' automatically, if that is not empty.
11605 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
11606 this is used for the GOTO interface."
11607 (let ((org-refile-targets org-refile-targets)
11608 (org-refile-use-outline-path org-refile-use-outline-path)
11610 (when (and (derived-mode-p 'org-mode)
11611 (not org-refile-use-cache)
11615 (setq excluded-entries
11616 (append excluded-entries (list (org-get-heading t t)))))))
11617 (setq org-refile-target-table
11618 (org-refile-get-targets default-buffer excluded-entries)))
11619 (unless org-refile-target-table
11620 (user-error "No refile targets"))
11621 (let* ((cbuf (current-buffer))
11622 (partial-completion-mode nil)
11623 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
11624 (cfunc (if (and org-refile-use-outline-path
11625 org-outline-path-complete-in-steps)
11626 'org-olpath-completing-read
11627 'org-icompleting-read))
11628 (extra (if org-refile-use-outline-path "/" ""))
11629 (cbnex (concat (buffer-name) extra))
11630 (filename (and cfn (expand-file-name cfn)))
11633 (if (and (not (member org-refile-use-outline-path
11634 '(file full-file-path)))
11635 (not (equal filename (nth 1 x))))
11636 (cons (concat (car x) extra " ("
11637 (file-name-nondirectory (nth 1 x)) ")")
11639 (cons (concat (car x) extra) (cdr x))))
11640 org-refile-target-table))
11641 (completion-ignore-case t)
11643 (prompt (concat prompt
11644 (or (and (car org-refile-history)
11645 (concat " (default " (car org-refile-history) ")"))
11646 (and (assoc cbnex tbl) (setq cdef cbnex)
11647 (concat " (default " cbnex ")"))) ": "))
11648 pa answ parent-target child parent old-hist)
11649 (setq old-hist org-refile-history)
11650 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
11651 nil 'org-refile-history (or cdef (car org-refile-history))))
11652 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
11653 (org-refile-check-position pa)
11656 (when (or (not org-refile-history)
11657 (not (eq old-hist org-refile-history))
11658 (not (equal (car pa) (car org-refile-history))))
11659 (setq org-refile-history
11660 (cons (car pa) (if (assoc (car org-refile-history) tbl)
11662 (cdr org-refile-history))))
11663 (if (equal (car org-refile-history) (nth 1 org-refile-history))
11664 (pop org-refile-history)))
11666 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
11668 (setq parent (match-string 1 answ)
11669 child (match-string 2 answ))
11670 (setq parent-target (or (assoc parent tbl)
11671 (assoc (concat parent "/") tbl)))
11672 (when (and parent-target
11673 (or (eq new-nodes t)
11674 (and (eq new-nodes 'confirm)
11675 (y-or-n-p (format "Create new node \"%s\"? "
11677 (org-refile-new-child parent-target child)))
11678 (user-error "Invalid target location")))))
11680 (declare-function org-string-nw-p "org-macs" (s))
11681 (defun org-refile-check-position (refile-pointer)
11682 "Check if the refile pointer matches the headline to which it points."
11683 (let* ((file (nth 1 refile-pointer))
11684 (re (nth 2 refile-pointer))
11685 (pos (nth 3 refile-pointer))
11687 (if (and (not (markerp pos)) (not file))
11688 (user-error "Please save the buffer to a file before refiling")
11689 (when (org-string-nw-p re)
11690 (setq buffer (if (markerp pos)
11691 (marker-buffer pos)
11692 (or (find-buffer-visiting file)
11693 (find-file-noselect file))))
11694 (with-current-buffer buffer
11699 (beginning-of-line 1)
11700 (unless (org-looking-at-p re)
11701 (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
11703 (defun org-refile-new-child (parent-target child)
11704 "Use refile target PARENT-TARGET to add new CHILD below it."
11705 (unless parent-target
11706 (error "Cannot find parent for new node"))
11707 (let ((file (nth 1 parent-target))
11708 (pos (nth 3 parent-target))
11710 (with-current-buffer (or (find-buffer-visiting file)
11711 (find-file-noselect file))
11717 (goto-char (point-max))
11718 (if (not (bolp)) (newline)))
11719 (when (looking-at org-outline-regexp)
11720 (setq level (funcall outline-level))
11721 (org-end-of-subtree t t))
11722 (org-back-over-empty-lines)
11723 (insert "\n" (make-string
11724 (if pos (org-get-valid-level level 1) 1) ?*)
11726 (beginning-of-line 0)
11727 (list (concat (car parent-target) "/" child) file "" (point)))))))
11729 (defun org-olpath-completing-read (prompt collection &rest args)
11730 "Read an outline path like a file name."
11731 (let ((thetable collection)
11732 (org-completion-use-ido nil) ; does not work with ido.
11733 (org-completion-use-iswitchb nil)) ; or iswitchb
11735 'org-icompleting-read prompt
11736 (lambda (string predicate &optional flag)
11737 (let (rtn r f (l (length string)))
11741 (try-completion string thetable))
11744 (setq rtn (all-completions string thetable predicate))
11747 (setq r (substring x l))
11748 (if (string-match " ([^)]*)$" x)
11749 (setq f (match-string 0 x))
11751 (if (string-match "/" r)
11752 (concat string (substring r 0 (match-end 0)) f)
11757 (assoc string thetable)))))
11760 ;;;; Dynamic blocks
11762 (defun org-find-dblock (name)
11763 "Find the first dynamic block with name NAME in the buffer.
11764 If not found, stay at current position and return nil."
11765 (let ((case-fold-search t) pos)
11767 (goto-char (point-min))
11768 (setq pos (and (re-search-forward
11769 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
11770 (match-beginning 0))))
11771 (if pos (goto-char pos))
11774 (defconst org-dblock-start-re
11775 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
11776 "Matches the start line of a dynamic block, with parameters.")
11778 (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
11779 "Matches the end of a dynamic block.")
11781 (defun org-create-dblock (plist)
11782 "Create a dynamic block section, with parameters taken from PLIST.
11783 PLIST must contain a :name entry which is used as name of the block."
11784 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11787 (let ((col (current-column))
11788 (name (plist-get plist :name)))
11789 (insert "#+BEGIN: " name)
11791 (if (eq (car plist) :name)
11792 (setq plist (cddr plist))
11793 (insert " " (prin1-to-string (pop plist)))))
11794 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
11795 (beginning-of-line -2)))
11797 (defun org-prepare-dblock ()
11798 "Prepare dynamic block for refresh.
11799 This empties the block, puts the cursor at the insert position and returns
11800 the property list including an extra property :name with the block name."
11801 (unless (looking-at org-dblock-start-re)
11802 (user-error "Not at a dynamic block"))
11803 (let* ((begdel (1+ (match-end 0)))
11804 (name (org-no-properties (match-string 1)))
11805 (params (append (list :name name)
11806 (read (concat "(" (match-string 3) ")")))))
11808 (beginning-of-line 1)
11809 (skip-chars-forward " \t")
11810 (setq params (plist-put params :indentation-column (current-column))))
11811 (unless (re-search-forward org-dblock-end-re nil t)
11812 (error "Dynamic block not terminated"))
11815 (list :content (buffer-substring
11816 begdel (match-beginning 0)))))
11817 (delete-region begdel (match-beginning 0))
11822 (defun org-map-dblocks (&optional command)
11823 "Apply COMMAND to all dynamic blocks in the current buffer.
11824 If COMMAND is not given, use `org-update-dblock'."
11825 (let ((cmd (or command 'org-update-dblock)))
11827 (goto-char (point-min))
11828 (while (re-search-forward org-dblock-start-re nil t)
11829 (goto-char (match-beginning 0))
11831 (condition-case nil
11833 (error (message "Error during update of dynamic block"))))
11834 (unless (re-search-forward org-dblock-end-re nil t)
11835 (error "Dynamic block not terminated"))))))
11837 (defun org-dblock-update (&optional arg)
11838 "User command for updating dynamic blocks.
11839 Update the dynamic block at point. With prefix ARG, update all dynamic
11840 blocks in the buffer."
11843 (org-update-all-dblocks)
11844 (or (looking-at org-dblock-start-re)
11845 (org-beginning-of-dblock))
11846 (org-update-dblock)))
11848 (defun org-update-dblock ()
11849 "Update the dynamic block at point.
11850 This means to empty the block, parse for parameters and then call
11851 the correct writing function."
11853 (save-window-excursion
11854 (let* ((pos (point))
11855 (line (org-current-line))
11856 (params (org-prepare-dblock))
11857 (name (plist-get params :name))
11858 (indent (plist-get params :indentation-column))
11859 (cmd (intern (concat "org-dblock-write:" name))))
11860 (message "Updating dynamic block `%s' at line %d..." name line)
11861 (funcall cmd params)
11862 (message "Updating dynamic block `%s' at line %d...done" name line)
11864 (when (and indent (> indent 0))
11865 (setq indent (make-string indent ?\ ))
11867 (org-beginning-of-dblock)
11869 (while (not (looking-at org-dblock-end-re))
11871 (beginning-of-line 2))
11872 (when (looking-at org-dblock-end-re)
11873 (and (looking-at "[ \t]+")
11874 (replace-match ""))
11875 (insert indent)))))))
11877 (defun org-beginning-of-dblock ()
11878 "Find the beginning of the dynamic block at point.
11879 Error if there is no such block at point."
11880 (let ((pos (point))
11883 (if (and (re-search-backward org-dblock-start-re nil t)
11884 (setq beg (match-beginning 0))
11885 (re-search-forward org-dblock-end-re nil t)
11886 (> (match-end 0) pos))
11889 (error "Not in a dynamic block"))))
11891 (defun org-update-all-dblocks ()
11892 "Update all dynamic blocks in the buffer.
11893 This function can be used in a hook."
11895 (when (derived-mode-p 'org-mode)
11896 (org-map-dblocks 'org-update-dblock)))
11901 (defun org-get-export-keywords ()
11902 "Return a list of all currently understood export keywords.
11903 Export keywords include options, block names, attributes and
11904 keywords relative to each registered export back-end."
11909 (let ((props (cdr back-end)))
11910 ;; Back-end name (for keywords, like #+LATEX:)
11911 (push (upcase (symbol-name (car back-end))) keywords)
11912 ;; Back-end options.
11913 (mapc (lambda (option) (push (cadr option) keywords))
11914 (plist-get (cdr back-end) :options-alist))))
11915 (org-bound-and-true-p org-export-registered-backends))
11918 (defconst org-options-keywords
11919 '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
11920 "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
11921 "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
11922 "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
11923 "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
11925 (defcustom org-structure-template-alist
11926 '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11927 "<src lang=\"?\">\n\n</src>")
11928 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11929 "<example>\n?\n</example>")
11930 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11931 "<quote>\n?\n</quote>")
11932 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11933 "<verse>\n?\n</verse>")
11934 ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
11935 "<verbatim>\n?\n</verbatim>")
11936 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11937 "<center>\n?\n</center>")
11938 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11939 "<literal style=\"latex\">\n?\n</literal>")
11941 "<literal style=\"latex\">?</literal>")
11942 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11943 "<literal style=\"html\">\n?\n</literal>")
11945 "<literal style=\"html\">?</literal>")
11946 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11950 ("I" "#+INCLUDE: %file ?"
11951 "<include file=%file markup=\"?\">"))
11952 "Structure completion elements.
11953 This is a list of abbreviation keys and values. The value gets inserted
11954 if you type `<' followed by the key and then press the completion key,
11955 usually `M-TAB'. %file will be replaced by a file name after prompting
11956 for the file using completion. The cursor will be placed at the position
11957 of the `?` in the template.
11958 There are two templates for each key, the first uses the original Org syntax,
11959 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11960 the default when the /org-mtags.el/ module has been loaded. See also the
11961 variable `org-mtags-prefer-muse-templates'."
11962 :group 'org-completion
11964 (string :tag "Key")
11965 (string :tag "Template")
11966 (string :tag "Muse Template")))
11968 (defun org-try-structure-completion ()
11969 "Try to complete a structure template before point.
11970 This looks for strings like \"<e\" on an otherwise empty line and
11972 (let ((l (buffer-substring (point-at-bol) (point)))
11974 (when (and (looking-at "[ \t]*$")
11975 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11976 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11977 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11978 (match-beginning 1)) a)
11981 (defun org-complete-expand-structure-template (start cell)
11982 "Expand a structure template."
11983 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11984 (rpl (nth (if musep 2 1) cell))
11986 (delete-region start (point))
11987 (when (string-match "\\`#\\+" rpl)
11990 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11991 (setq ind (buffer-substring (point-at-bol) (point))))
11993 (setq start (point))
11994 (if (string-match "%file" rpl)
11995 (setq rpl (replace-match
11999 (abbreviate-file-name (read-file-name "Include file: ")))
12002 (setq rpl (mapconcat 'identity (split-string rpl "\n")
12003 (concat "\n" ind)))
12005 (if (re-search-backward "\\?" start t) (delete-char 1))))
12007 ;;;; TODO, DEADLINE, Comments
12009 (defun org-toggle-comment ()
12010 "Change the COMMENT state of an entry."
12013 (org-back-to-heading)
12014 (let (case-fold-search)
12016 ((looking-at (format org-heading-keyword-regexp-format
12017 org-comment-string))
12018 (goto-char (match-end 1))
12019 (looking-at (concat " +" org-comment-string))
12020 (replace-match "" t t)
12021 (when (eolp) (insert " ")))
12022 ((looking-at org-outline-regexp)
12023 (goto-char (match-end 0))
12024 (insert org-comment-string " "))))))
12026 (defvar org-last-todo-state-is-todo nil
12027 "This is non-nil when the last TODO state change led to a TODO state.
12028 If the last change removed the TODO tag or switched to DONE, then
12031 (defvar org-setting-tags nil) ; dynamically skipped
12033 (defvar org-todo-setup-filter-hook nil
12034 "Hook for functions that pre-filter todo specs.
12035 Each function takes a todo spec and returns either nil or the spec
12036 transformed into canonical form." )
12038 (defvar org-todo-get-default-hook nil
12039 "Hook for functions that get a default item for todo.
12040 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
12041 nil or a string to be used for the todo mark." )
12043 (defvar org-agenda-headline-snapshot-before-repeat)
12045 (defun org-current-effective-time ()
12046 "Return current time adjusted for `org-extend-today-until' variable."
12047 (let* ((ct (org-current-time))
12048 (dct (decode-time ct))
12051 (org-use-last-clock-out-time-as-effective-time
12052 (or (org-clock-get-last-clock-out-time) ct))
12053 ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
12054 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
12058 (defun org-todo-yesterday (&optional arg)
12059 "Like `org-todo' but the time of change will be 23:59 of yesterday."
12061 (if (eq major-mode 'org-agenda-mode)
12062 (apply 'org-agenda-todo-yesterday arg)
12063 (let* ((hour (third (decode-time
12064 (org-current-time))))
12065 (org-extend-today-until (1+ hour)))
12068 (defvar org-block-entry-blocking ""
12069 "First entry preventing the TODO state change.")
12071 (defun org-todo (&optional arg)
12072 "Change the TODO state of an item.
12073 The state of an item is given by a keyword at the start of the heading,
12075 *** TODO Write paper
12078 The different keywords are specified in the variable `org-todo-keywords'.
12079 By default the available states are \"TODO\" and \"DONE\".
12080 So for this example: when the item starts with TODO, it is changed to DONE.
12081 When it starts with DONE, the DONE is removed. And when neither TODO nor
12082 DONE are present, add TODO at the beginning of the heading.
12084 With \\[universal-argument] prefix arg, use completion to determine the new \
12086 With numeric prefix arg, switch to that state.
12087 With a double \\[universal-argument] prefix, switch to the next set of TODO \
12088 keywords (nextset).
12089 With a triple \\[universal-argument] prefix, circumvent any state blocking.
12090 With a numeric prefix arg of 0, inhibit note taking for the change.
12092 For calling through lisp, arg is also interpreted in the following way:
12093 'none -> empty state
12094 \"\"(empty string) -> switch to empty state
12095 'done -> switch to DONE
12096 'nextset -> switch to the next set of keywords
12097 'previousset -> switch to the previous set of keywords
12098 \"WAITING\" -> switch to the specified keyword, but only if it
12099 really is a member of `org-todo-keywords'."
12101 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12102 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12103 'region-start-level 'region))
12104 org-loop-over-headlines-in-active-region)
12107 org-loop-over-headlines-in-active-region
12108 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12109 (if (equal arg '(16)) (setq arg 'nextset))
12110 (let ((org-blocker-hook org-blocker-hook)
12113 (when (equal arg '(64))
12114 (setq arg nil org-blocker-hook nil))
12115 (when (and org-blocker-hook
12116 (or org-inhibit-blocking
12117 (org-entry-get nil "NOBLOCKING")))
12118 (setq org-blocker-hook nil))
12121 (org-back-to-heading t)
12122 (when (looking-at (concat "^\\*+ " org-comment-string))
12123 (org-toggle-comment)
12125 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
12126 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
12127 (looking-at "\\(?: *\\|[ \t]*$\\)"))
12128 (let* ((match-data (match-data))
12129 (startpos (point-at-bol))
12130 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
12131 (org-log-done org-log-done)
12132 (org-log-repeat org-log-repeat)
12133 (org-todo-log-states org-todo-log-states)
12134 (org-inhibit-logging
12136 (progn (setq arg nil) 'note) org-inhibit-logging))
12137 (this (match-string 1))
12138 (hl-pos (match-beginning 0))
12139 (head (org-get-todo-sequence-head this))
12140 (ass (assoc head org-todo-kwd-alist))
12141 (interpret (nth 1 ass))
12142 (done-word (nth 3 ass))
12143 (final-done-word (nth 4 ass))
12144 (org-last-state (or this ""))
12145 (completion-ignore-case t)
12146 (member (member this org-todo-keywords-1))
12147 (tail (cdr member))
12149 ((and org-todo-key-trigger
12150 (or (and (equal arg '(4))
12151 (eq org-use-fast-todo-selection 'prefix))
12152 (and (not arg) org-use-fast-todo-selection
12153 (not (eq org-use-fast-todo-selection
12155 ;; Use fast selection
12156 (org-fast-todo-selection))
12157 ((and (equal arg '(4))
12158 (or (not org-use-fast-todo-selection)
12159 (not org-todo-key-trigger)))
12160 ;; Read a state with completion
12161 (org-icompleting-read
12162 "State: " (mapcar 'list org-todo-keywords-1)
12166 (if tail (car tail) nil)
12167 (car org-todo-keywords-1)))
12169 (if (equal member org-todo-keywords-1)
12172 (nth (- (length org-todo-keywords-1)
12174 org-todo-keywords-1)
12175 (org-last org-todo-keywords-1))))
12176 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
12177 (setq arg nil))) ; hack to fall back to cycling
12179 ;; user or caller requests a specific state
12181 ((equal arg "") nil)
12182 ((eq arg 'none) nil)
12183 ((eq arg 'done) (or done-word (car org-done-keywords)))
12185 (or (car (cdr (member head org-todo-heads)))
12186 (car org-todo-heads)))
12187 ((eq arg 'previousset)
12188 (let ((org-todo-heads (reverse org-todo-heads)))
12189 (or (car (cdr (member head org-todo-heads)))
12190 (car org-todo-heads))))
12191 ((car (member arg org-todo-keywords-1)))
12193 (user-error "State `%s' not valid in this file" arg))
12194 ((nth (1- (prefix-numeric-value arg))
12195 org-todo-keywords-1))))
12196 ((null member) (or head (car org-todo-keywords-1)))
12197 ((equal this final-done-word) nil) ;; -> make empty
12198 ((null tail) nil) ;; -> first entry
12199 ((memq interpret '(type priority))
12200 (if (eq this-command last-command)
12202 (if (> (length tail) 0)
12203 (or done-word (car org-done-keywords))
12208 (run-hook-with-args-until-success
12209 'org-todo-get-default-hook org-state org-last-state)
12211 (next (if org-state (concat " " org-state " ") " "))
12212 (change-plist (list :type 'todo-state-change :from this :to org-state
12213 :position startpos))
12215 (when org-blocker-hook
12216 (setq org-last-todo-state-is-todo
12217 (not (member this org-done-keywords)))
12218 (unless (save-excursion
12220 (org-with-wide-buffer
12221 (run-hook-with-args-until-failure
12222 'org-blocker-hook change-plist))))
12223 (if (org-called-interactively-p 'interactive)
12224 (user-error "TODO state change from %s to %s blocked (by \"%s\")"
12225 this org-state org-block-entry-blocking)
12227 (message "TODO state change from %s to %s blocked (by \"%s\")"
12228 this org-state org-block-entry-blocking)
12229 (throw 'exit nil))))
12230 (store-match-data match-data)
12231 (replace-match next t t)
12232 (unless (pos-visible-in-window-p hl-pos)
12233 (message "TODO state changed to %s" (org-trim next)))
12235 (setq head (org-get-todo-sequence-head org-state)
12236 ass (assoc head org-todo-kwd-alist)
12237 interpret (nth 1 ass)
12238 done-word (nth 3 ass)
12239 final-done-word (nth 4 ass)))
12240 (when (memq arg '(nextset previousset))
12241 (message "Keyword-Set %d/%d: %s"
12242 (- (length org-todo-sets) -1
12243 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
12244 (length org-todo-sets)
12245 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
12246 (setq org-last-todo-state-is-todo
12247 (not (member org-state org-done-keywords)))
12248 (setq now-done-p (and (member org-state org-done-keywords)
12249 (not (member this org-done-keywords))))
12250 (and logging (org-local-logging logging))
12251 (when (and (or org-todo-log-states org-log-done)
12252 (not (eq org-inhibit-logging t))
12253 (not (memq arg '(nextset previousset))))
12254 ;; we need to look at recording a time and note
12255 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
12256 (nth 2 (assoc this org-todo-log-states))))
12257 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
12258 (setq dolog 'time))
12259 (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
12261 (member org-state org-not-done-keywords)
12262 (not (member this org-not-done-keywords))))
12263 ;; This is now a todo state and was not one before
12264 ;; If there was a CLOSED time stamp, get rid of it.
12265 (org-add-planning-info nil nil 'closed))
12266 (when (and now-done-p org-log-done)
12267 ;; It is now done, and it was not done before
12268 (org-add-planning-info 'closed (org-current-effective-time))
12269 (if (and (not dolog) (eq 'note org-log-done))
12270 (org-add-log-setup 'done org-state this 'findpos 'note)))
12271 (when (and org-state dolog)
12272 ;; This is a non-nil state, and we need to log it
12273 (org-add-log-setup 'state org-state this 'findpos dolog)))
12274 ;; Fixup tag positioning
12275 (org-todo-trigger-tag-changes org-state)
12276 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
12277 (when org-provide-todo-statistics
12278 (org-update-parent-todo-statistics))
12279 (run-hooks 'org-after-todo-state-change-hook)
12280 (if (and arg (not (member org-state org-done-keywords)))
12281 (setq head (org-get-todo-sequence-head org-state)))
12282 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
12283 ;; Do we need to trigger a repeat?
12285 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
12286 ;; This is for the agenda, take a snapshot of the headline.
12288 (setq org-agenda-headline-snapshot-before-repeat
12289 (org-get-heading))))
12290 (org-auto-repeat-maybe org-state))
12291 ;; Fixup cursor location if close to the keyword
12292 (if (and (outline-on-heading-p)
12294 (save-excursion (beginning-of-line 1)
12295 (looking-at org-todo-line-regexp))
12296 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
12298 (goto-char (or (match-end 2) (match-end 1)))
12299 (and (looking-at " ") (just-one-space))))
12300 (when org-trigger-hook
12302 (run-hook-with-args 'org-trigger-hook change-plist)))
12303 (when commentp (org-toggle-comment))))))))
12305 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
12306 "Block turning an entry into a TODO, using the hierarchy.
12307 This checks whether the current task should be blocked from state
12308 changes. Such blocking occurs when:
12310 1. The task has children which are not all in a completed state.
12312 2. A task has a parent with the property :ORDERED:, and there
12313 are siblings prior to the current task with incomplete
12316 3. The parent of the task is blocked because it has siblings that should
12317 be done first, or is child of a block grandparent TODO entry."
12319 (if (not org-enforce-todo-dependencies)
12320 t ; if locally turned off don't block
12322 ;; If this is not a todo state change, or if this entry is already DONE,
12324 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
12325 (member (plist-get change-plist :from)
12326 (cons 'done org-done-keywords))
12327 (member (plist-get change-plist :to)
12328 (cons 'todo org-not-done-keywords))
12329 (not (plist-get change-plist :to)))
12330 (throw 'dont-block t))
12331 ;; If this task has children, and any are undone, it's blocked
12333 (org-back-to-heading t)
12334 (let ((this-level (funcall outline-level)))
12335 (outline-next-heading)
12336 (let ((child-level (funcall outline-level)))
12337 (while (and (not (eobp))
12338 (> child-level this-level))
12339 ;; this todo has children, check whether they are all
12341 (if (and (not (org-entry-is-done-p))
12342 (org-entry-is-todo-p))
12343 (progn (setq org-block-entry-blocking (org-get-heading))
12344 (throw 'dont-block nil)))
12345 (outline-next-heading)
12346 (setq child-level (funcall outline-level))))))
12347 ;; Otherwise, if the task's parent has the :ORDERED: property, and
12348 ;; any previous siblings are undone, it's blocked
12350 (org-back-to-heading t)
12351 (let* ((pos (point))
12352 (parent-pos (and (org-up-heading-safe) (point))))
12353 (if (not parent-pos) (throw 'dont-block t)) ; no parent
12354 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12356 (re-search-forward org-not-done-heading-regexp pos t))
12357 (setq org-block-entry-blocking (match-string 0))
12358 (throw 'dont-block nil)) ; block, there is an older sibling not done.
12359 ;; Search further up the hierarchy, to see if an ancestor is blocked
12361 (goto-char parent-pos)
12362 (if (not (looking-at org-not-done-heading-regexp))
12363 (throw 'dont-block t)) ; do not block, parent is not a TODO
12365 (setq parent-pos (and (org-up-heading-safe) (point)))
12366 (if (not parent-pos) (throw 'dont-block t)) ; no parent
12367 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12369 (re-search-forward org-not-done-heading-regexp pos t)
12370 (setq org-block-entry-blocking (org-get-heading)))
12371 (throw 'dont-block nil)))))))) ; block, older sibling not done.
12373 (defcustom org-track-ordered-property-with-tag nil
12374 "Should the ORDERED property also be shown as a tag?
12375 The ORDERED property decides if an entry should require subtasks to be
12376 completed in sequence. Since a property is not very visible, setting
12377 this option means that toggling the ORDERED property with the command
12378 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
12379 not relevant for the behavior, but it makes things more visible.
12381 Note that toggling the tag with tags commands will not change the property
12382 and therefore not influence behavior!
12384 This can be t, meaning the tag ORDERED should be used, It can also be a
12385 string to select a different tag for this task."
12388 (const :tag "No tracking" nil)
12389 (const :tag "Track with ORDERED tag" t)
12390 (string :tag "Use other tag")))
12392 (defun org-toggle-ordered-property ()
12393 "Toggle the ORDERED property of the current entry.
12394 For better visibility, you can track the value of this property with a tag.
12395 See variable `org-track-ordered-property-with-tag'."
12397 (let* ((t1 org-track-ordered-property-with-tag)
12398 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
12400 (org-back-to-heading)
12401 (if (org-entry-get nil "ORDERED")
12403 (org-delete-property "ORDERED" "PROPERTIES")
12404 (and tag (org-toggle-tag tag 'off))
12405 (message "Subtasks can be completed in arbitrary order"))
12406 (org-entry-put nil "ORDERED" "t")
12407 (and tag (org-toggle-tag tag 'on))
12408 (message "Subtasks must be completed in sequence")))))
12410 (defvar org-blocked-by-checkboxes) ; dynamically scoped
12411 (defun org-block-todo-from-checkboxes (change-plist)
12412 "Block turning an entry into a TODO, using checkboxes.
12413 This checks whether the current task should be blocked from state
12414 changes because there are unchecked boxes in this entry."
12415 (if (not org-enforce-todo-checkbox-dependencies)
12416 t ; if locally turned off don't block
12418 ;; If this is not a todo state change, or if this entry is already DONE,
12420 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
12421 (member (plist-get change-plist :from)
12422 (cons 'done org-done-keywords))
12423 (member (plist-get change-plist :to)
12424 (cons 'todo org-not-done-keywords))
12425 (not (plist-get change-plist :to)))
12426 (throw 'dont-block t))
12427 ;; If this task has checkboxes that are not checked, it's blocked
12429 (org-back-to-heading t)
12430 (let ((beg (point)) end)
12431 (outline-next-heading)
12434 (if (org-list-search-forward
12435 (concat (org-item-beginning-re)
12436 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
12440 (if (boundp 'org-blocked-by-checkboxes)
12441 (setq org-blocked-by-checkboxes t))
12442 (throw 'dont-block nil)))))
12443 t))) ; do not block
12445 (defun org-entry-blocked-p ()
12446 "Is the current entry blocked?"
12447 (org-with-silent-modifications
12448 (if (org-entry-get nil "NOBLOCKING")
12449 nil ;; Never block this entry
12450 (not (run-hook-with-args-until-failure
12452 (list :type 'todo-state-change
12457 (defun org-update-statistics-cookies (all)
12458 "Update the statistics cookie, either from TODO or from checkboxes.
12459 This should be called with the cursor in a line with a statistics cookie."
12463 (org-update-checkbox-count 'all)
12464 (org-map-entries 'org-update-parent-todo-statistics))
12465 (if (not (org-at-heading-p))
12466 (org-update-checkbox-count)
12467 (let ((pos (point-marker))
12469 (ignore-errors (org-back-to-heading t))
12470 (if (not (org-at-heading-p))
12471 (org-update-checkbox-count)
12472 (setq l1 (org-outline-level))
12473 (setq end (save-excursion
12474 (outline-next-heading)
12475 (if (org-at-heading-p) (setq l2 (org-outline-level)))
12477 (if (and (save-excursion
12479 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
12480 (not (save-excursion (re-search-forward
12481 ":COOKIE_DATA:.*\\<todo\\>" end t))))
12482 (org-update-checkbox-count)
12483 (if (and l2 (> l2 l1))
12486 (org-update-parent-todo-statistics))
12488 (beginning-of-line 1)
12489 (while (re-search-forward
12490 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
12492 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
12494 (move-marker pos nil)))))
12496 (defvar org-entry-property-inherited-from) ;; defined below
12497 (defun org-update-parent-todo-statistics ()
12498 "Update any statistics cookie in the parent of the current headline.
12499 When `org-hierarchical-todo-statistics' is nil, statistics will cover
12500 the entire subtree and this will travel up the hierarchy and update
12501 statistics everywhere."
12502 (let* ((prop (save-excursion (org-up-heading-safe)
12503 (org-entry-get nil "COOKIE_DATA" 'inherit)))
12504 (recursive (or (not org-hierarchical-todo-statistics)
12505 (and prop (string-match "\\<recursive\\>" prop))))
12506 (lim (or (and prop (marker-position org-entry-property-inherited-from))
12509 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
12510 level ltoggle l1 new ndel
12511 (cnt-all 0) (cnt-done 0) is-percent kwd
12512 checkbox-beg ov ovs ove cookie-present)
12515 (beginning-of-line 1)
12516 (setq ltoggle (funcall outline-level))
12517 ;; Three situations are to consider:
12519 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
12520 ;; to the top-level ancestor on the headline;
12522 ;; 2. If parent has "recursive" property, repeat up to the
12523 ;; headline setting that property, taking inheritance into
12526 ;; 3. Else, move up to direct parent and proceed only once.
12527 (while (and (setq level (org-up-heading-safe))
12528 (or recursive first)
12530 (setq first nil cookie-present nil)
12534 (downcase (or (org-entry-get nil "COOKIE_DATA")
12537 (while (re-search-forward box-re (point-at-eol) t)
12538 (setq cnt-all 0 cnt-done 0 cookie-present t)
12539 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
12541 (unless (outline-next-heading) (throw 'exit nil))
12542 (while (and (looking-at org-complex-heading-regexp)
12543 (> (setq l1 (length (match-string 1))) level))
12544 (setq kwd (and (or recursive (= l1 ltoggle))
12546 (if (or (eq org-provide-todo-statistics 'all-headlines)
12547 (and (listp org-provide-todo-statistics)
12548 (or (member kwd org-provide-todo-statistics)
12549 (member kwd org-done-keywords))))
12550 (setq cnt-all (1+ cnt-all))
12551 (if (eq org-provide-todo-statistics t)
12552 (and kwd (setq cnt-all (1+ cnt-all)))))
12553 (and (member kwd org-done-keywords)
12554 (setq cnt-done (1+ cnt-done)))
12555 (outline-next-heading)))
12558 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
12559 (format "[%d/%d]" cnt-done cnt-all))
12560 ndel (- (match-end 0) checkbox-beg))
12561 ;; handle overlays when updating cookie from column view
12562 (when (setq ov (car (overlays-at checkbox-beg)))
12563 (setq ovs (overlay-start ov) ove (overlay-end ov))
12564 (delete-overlay ov))
12565 (goto-char checkbox-beg)
12567 (delete-region (point) (+ (point) ndel))
12568 (when org-auto-align-tags (org-fix-tags-on-the-fly))
12569 (when ov (move-overlay ov ovs ove)))
12570 (when cookie-present
12571 (run-hook-with-args 'org-after-todo-statistics-hook
12572 cnt-done (- cnt-all cnt-done))))))
12573 (run-hooks 'org-todo-statistics-hook)))
12575 (defvar org-after-todo-statistics-hook nil
12576 "Hook that is called after a TODO statistics cookie has been updated.
12577 Each function is called with two arguments: the number of not-done entries
12578 and the number of done entries.
12580 For example, the following function, when added to this hook, will switch
12581 an entry to DONE when all children are done, and back to TODO when new
12582 entries are set to a TODO status. Note that this hook is only called
12583 when there is a statistics cookie in the headline!
12585 (defun org-summary-todo (n-done n-not-done)
12586 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
12587 (let (org-log-done org-log-states) ; turn off logging
12588 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
12591 (defvar org-todo-statistics-hook nil
12592 "Hook that is run whenever Org thinks TODO statistics should be updated.
12593 This hook runs even if there is no statistics cookie present, in which case
12594 `org-after-todo-statistics-hook' would not run.")
12596 (defun org-todo-trigger-tag-changes (state)
12597 "Apply the changes defined in `org-todo-state-tags-triggers'."
12598 (let ((l org-todo-state-tags-triggers)
12600 (when (or (not state) (equal state ""))
12601 (setq changes (append changes (cdr (assoc "" l)))))
12602 (when (and (stringp state) (> (length state) 0))
12603 (setq changes (append changes (cdr (assoc state l)))))
12604 (when (member state org-not-done-keywords)
12605 (setq changes (append changes (cdr (assoc 'todo l)))))
12606 (when (member state org-done-keywords)
12607 (setq changes (append changes (cdr (assoc 'done l)))))
12608 (dolist (c changes)
12609 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
12611 (defun org-local-logging (value)
12612 "Get logging settings from a property VALUE."
12614 ;; directly set the variables, they are already local.
12615 (setq org-log-done nil
12617 org-todo-log-states nil)
12618 (setq words (org-split-string value))
12619 (while (setq w (pop words))
12621 ((setq a (assoc w org-startup-options))
12622 (and (member (nth 1 a) '(org-log-done org-log-repeat))
12623 (set (nth 1 a) (nth 2 a))))
12624 ((setq a (org-extract-log-state-settings w))
12625 (and (member (car a) org-todo-keywords-1)
12626 (push a org-todo-log-states)))))))
12628 (defun org-get-todo-sequence-head (kwd)
12629 "Return the head of the TODO sequence to which KWD belongs.
12630 If KWD is not set, check if there is a text property remembering the
12635 (or (get-text-property (point-at-bol) 'org-todo-head)
12637 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
12638 nil (point-at-eol)))
12639 (get-text-property p 'org-todo-head))))
12640 ((not (member kwd org-todo-keywords-1))
12641 (car org-todo-keywords-1))
12642 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
12644 (defun org-fast-todo-selection ()
12645 "Fast TODO keyword selection with single keys.
12646 Returns the new TODO keyword, or nil if no state change should occur."
12647 (let* ((fulltable org-todo-key-alist)
12648 (done-keywords org-done-keywords) ;; needed for the faces.
12649 (maxlen (apply 'max (mapcar
12651 (if (stringp (car x)) (string-width (car x)) 0))
12654 (fwidth (+ maxlen 3 1 3))
12655 (ncol (/ (- (window-width) 4) fwidth))
12659 (save-window-excursion
12661 (set-buffer (get-buffer-create " *Org todo*"))
12662 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
12664 (org-set-local 'org-done-keywords done-keywords)
12665 (setq tbl fulltable cnt 0)
12666 (while (setq e (pop tbl))
12668 ((equal e '(:startgroup))
12669 (push '() groups) (setq ingroup t)
12670 (when (not (= cnt 0))
12674 ((equal e '(:endgroup))
12675 (setq ingroup nil cnt 0)
12677 ((equal e '(:newline))
12678 (when (not (= cnt 0))
12682 (while (equal (car tbl) '(:newline))
12684 (setq tbl (cdr tbl)))))
12686 (setq tg (car e) c (cdr e))
12687 (if ingroup (push tg (car groups)))
12688 (setq tg (org-add-props tg nil 'face
12689 (org-get-todo-face tg)))
12690 (if (and (= cnt 0) (not ingroup)) (insert " "))
12691 (insert "[" c "] " tg (make-string
12692 (- fwidth 4 (length tg)) ?\ ))
12693 (when (= (setq cnt (1+ cnt)) ncol)
12695 (if ingroup (insert " "))
12698 (goto-char (point-min))
12699 (if (not expert) (org-fit-window-to-buffer))
12700 (message "[a-z..]:Set [SPC]:clear")
12701 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
12704 (and (= c ?q) (not (rassoc c fulltable))))
12705 (setq quit-flag t))
12707 ((setq e (rassoc c fulltable) tg (car e))
12709 (t (setq quit-flag t)))))))
12711 (defun org-entry-is-todo-p ()
12712 (member (org-get-todo-state) org-not-done-keywords))
12714 (defun org-entry-is-done-p ()
12715 (member (org-get-todo-state) org-done-keywords))
12717 (defun org-get-todo-state ()
12718 "Return the TODO keyword of the current subtree."
12720 (org-back-to-heading t)
12721 (and (looking-at org-todo-line-regexp)
12723 (match-string 2))))
12725 (defun org-at-date-range-p (&optional inactive-ok)
12726 "Is the cursor inside a date range?"
12730 (let ((pos (point)))
12731 (skip-chars-backward "^[<\r\n")
12732 (skip-chars-backward "<[")
12733 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12734 (>= (match-end 0) pos)
12736 (skip-chars-backward "^<[\r\n")
12737 (skip-chars-backward "<[")
12738 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12739 (>= (match-end 0) pos)
12743 (defun org-get-repeat (&optional tagline)
12744 "Check if there is a deadline/schedule with repeater in this entry."
12747 (org-back-to-heading t)
12748 (and (re-search-forward (if tagline
12749 (concat tagline "\\s-*" org-repeat-re)
12751 (org-entry-end-position) t)
12752 (match-string-no-properties 1)))))
12754 (defvar org-last-changed-timestamp)
12755 (defvar org-last-inserted-timestamp)
12756 (defvar org-log-post-message)
12757 (defvar org-log-note-purpose)
12758 (defvar org-log-note-how)
12759 (defvar org-log-note-extra)
12760 (defun org-auto-repeat-maybe (done-word)
12761 "Check if the current headline contains a repeated deadline/schedule.
12762 If yes, set TODO state back to what it was and change the base date
12763 of repeating deadline/scheduled time stamps to new date.
12764 This function is run automatically after each state change to a DONE state."
12765 ;; last-state is dynamically scoped into this function
12766 (let* ((repeat (org-get-repeat))
12767 (aa (assoc org-last-state org-todo-kwd-alist))
12768 (interpret (nth 1 aa))
12770 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
12771 (msg "Entry repeats: ")
12773 (org-todo-log-states nil)
12774 re type n what ts time to-state)
12776 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
12777 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
12778 org-todo-repeat-to-state))
12779 (unless (and to-state (member to-state org-todo-keywords-1))
12780 (setq to-state (if (eq interpret 'type) org-last-state head)))
12781 (org-todo to-state)
12782 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
12783 (org-entry-put nil "LAST_REPEAT" (format-time-string
12784 (org-time-stamp-format t t))))
12785 (when org-log-repeat
12786 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
12787 (memq 'org-add-log-note post-command-hook))
12788 ;; OK, we are already setup for some record
12789 (if (eq org-log-repeat 'note)
12790 ;; make sure we take a note, not only a time stamp
12791 (setq org-log-note-how 'note))
12792 ;; Set up for taking a record
12793 (org-add-log-setup 'state (or done-word (car org-done-keywords))
12795 'findpos org-log-repeat)))
12796 (org-back-to-heading t)
12797 (org-add-planning-info nil nil 'closed)
12798 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12799 org-deadline-time-regexp "\\)\\|\\("
12800 org-ts-regexp "\\)"))
12801 (while (re-search-forward
12802 re (save-excursion (outline-next-heading) (point)) t)
12803 (setq type (if (match-end 1) org-scheduled-string
12804 (if (match-end 3) org-deadline-string "Plain:"))
12805 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
12806 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
12807 (setq n (string-to-number (match-string 2 ts))
12808 what (match-string 3 ts))
12809 (if (equal what "w") (setq n (* n 7) what "d"))
12810 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
12811 (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
12812 ;; Preparation, see if we need to modify the start date for the change
12813 (when (match-end 1)
12814 (setq time (save-match-data (org-time-string-to-time ts)))
12816 ((equal (match-string 1 ts) ".")
12817 ;; Shift starting date to today
12818 (org-timestamp-change
12819 (- (org-today) (time-to-days time))
12821 ((equal (match-string 1 ts) "+")
12822 (let ((nshiftmax 10) (nshift 0))
12823 (while (or (= nshift 0)
12824 (<= (time-to-days time)
12825 (time-to-days (current-time))))
12826 (when (= (incf nshift) nshiftmax)
12827 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
12829 (org-timestamp-change n (cdr (assoc what whata)))
12830 (org-at-timestamp-p t)
12831 (setq ts (match-string 1))
12832 (setq time (save-match-data (org-time-string-to-time ts)))))
12833 (org-timestamp-change (- n) (cdr (assoc what whata)))
12834 ;; rematch, so that we have everything in place for the real shift
12835 (org-at-timestamp-p t)
12836 (setq ts (match-string 1))
12837 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
12838 (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
12839 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12840 (setq org-log-post-message msg)
12841 (message "%s" msg))))
12843 (defun org-show-todo-tree (arg)
12844 "Make a compact tree which shows all headlines marked with TODO.
12845 The tree will show the lines where the regexp matches, and all higher
12846 headlines above the match.
12847 With a \\[universal-argument] prefix, prompt for a regexp to match.
12848 With a numeric prefix N, construct a sparse tree for the Nth element
12849 of `org-todo-keywords-1'."
12851 (let ((case-fold-search nil)
12853 (cond ((null arg) org-not-done-regexp)
12855 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12856 (mapcar 'list org-todo-keywords-1))))
12858 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12860 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12861 (regexp-quote (nth (1- (prefix-numeric-value arg))
12862 org-todo-keywords-1)))
12863 (t (user-error "Invalid prefix argument: %s" arg)))))
12864 (message "%d TODO entries found"
12865 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12867 (defun org-deadline (arg &optional time)
12868 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12869 With one universal prefix argument, remove any deadline from the item.
12870 With two universal prefix arguments, prompt for a warning delay.
12871 With argument TIME, set the deadline at the corresponding date. TIME
12872 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12874 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12875 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12876 'region-start-level 'region))
12877 org-loop-over-headlines-in-active-region)
12879 `(org-deadline ',arg ,time)
12880 org-loop-over-headlines-in-active-region
12881 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12882 (let* ((old-date (org-entry-get nil "DEADLINE"))
12883 (old-date-time (if old-date (org-time-string-to-time old-date)))
12884 (repeater (and old-date
12886 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12888 (match-string 1 old-date))))
12891 (when (and old-date org-log-redeadline)
12892 (org-add-log-setup 'deldeadline nil old-date 'findpos
12893 org-log-redeadline))
12894 (org-remove-timestamp-with-keyword org-deadline-string)
12895 (message "Item no longer has a deadline."))
12898 (if (re-search-forward
12899 org-deadline-time-regexp
12900 (save-excursion (outline-next-heading) (point)) t)
12901 (let* ((rpl0 (match-string 1))
12902 (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
12904 (concat org-deadline-string
12910 (org-read-date nil t nil "Warn starting from" old-date-time)))
12911 (time-to-days old-date-time))))
12913 (user-error "No deadline information to update"))))
12915 (org-add-planning-info 'deadline time 'closed)
12916 (when (and old-date org-log-redeadline
12917 (not (equal old-date
12918 (substring org-last-inserted-timestamp 1 -1))))
12919 (org-add-log-setup 'redeadline nil old-date 'findpos
12920 org-log-redeadline))
12923 (org-back-to-heading t)
12924 (when (re-search-forward (concat org-deadline-string " "
12925 org-last-inserted-timestamp)
12927 (outline-next-heading) (point)) t)
12928 (goto-char (1- (match-end 0)))
12929 (insert " " repeater)
12930 (setq org-last-inserted-timestamp
12931 (concat (substring org-last-inserted-timestamp 0 -1)
12933 (substring org-last-inserted-timestamp -1))))))
12934 (message "Deadline on %s" org-last-inserted-timestamp))))))
12936 (defun org-schedule (arg &optional time)
12937 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12938 With one universal prefix argument, remove any scheduling date from the item.
12939 With two universal prefix arguments, prompt for a delay cookie.
12940 With argument TIME, scheduled at the corresponding date. TIME can
12941 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12943 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12944 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12945 'region-start-level 'region))
12946 org-loop-over-headlines-in-active-region)
12948 `(org-schedule ',arg ,time)
12949 org-loop-over-headlines-in-active-region
12950 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12951 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12952 (old-date-time (if old-date (org-time-string-to-time old-date)))
12953 (repeater (and old-date
12955 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12957 (match-string 1 old-date))))
12961 (when (and old-date org-log-reschedule)
12962 (org-add-log-setup 'delschedule nil old-date 'findpos
12963 org-log-reschedule))
12964 (org-remove-timestamp-with-keyword org-scheduled-string)
12965 (message "Item is no longer scheduled.")))
12968 (if (re-search-forward
12969 org-scheduled-time-regexp
12970 (save-excursion (outline-next-heading) (point)) t)
12971 (let* ((rpl0 (match-string 1))
12972 (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
12974 (concat org-scheduled-string
12980 (org-read-date nil t nil "Delay until" old-date-time)))
12981 (time-to-days old-date-time))))
12983 (user-error "No scheduled information to update"))))
12985 (org-add-planning-info 'scheduled time 'closed)
12986 (when (and old-date org-log-reschedule
12987 (not (equal old-date
12988 (substring org-last-inserted-timestamp 1 -1))))
12989 (org-add-log-setup 'reschedule nil old-date 'findpos
12990 org-log-reschedule))
12993 (org-back-to-heading t)
12994 (when (re-search-forward (concat org-scheduled-string " "
12995 org-last-inserted-timestamp)
12997 (outline-next-heading) (point)) t)
12998 (goto-char (1- (match-end 0)))
12999 (insert " " repeater)
13000 (setq org-last-inserted-timestamp
13001 (concat (substring org-last-inserted-timestamp 0 -1)
13003 (substring org-last-inserted-timestamp -1))))))
13004 (message "Scheduled to %s" org-last-inserted-timestamp))))))
13006 (defun org-get-scheduled-time (pom &optional inherit)
13007 "Get the scheduled time as a time tuple, of a format suitable
13008 for calling org-schedule with, or if there is no scheduling,
13010 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
13012 (apply 'encode-time (org-parse-time-string time)))))
13014 (defun org-get-deadline-time (pom &optional inherit)
13015 "Get the deadline as a time tuple, of a format suitable for
13016 calling org-deadline with, or if there is no scheduling, returns
13018 (let ((time (org-entry-get pom "DEADLINE" inherit)))
13020 (apply 'encode-time (org-parse-time-string time)))))
13022 (defun org-remove-timestamp-with-keyword (keyword)
13023 "Remove all time stamps with KEYWORD in the current entry."
13024 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
13027 (org-back-to-heading t)
13029 (outline-next-heading)
13030 (while (re-search-backward re beg t)
13032 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
13033 (equal (char-before) ?\ ))
13034 (backward-delete-char 1)
13035 (if (string-match "^[ \t]*$" (buffer-substring
13036 (point-at-bol) (point-at-eol)))
13037 (delete-region (point-at-bol)
13038 (min (point-max) (1+ (point-at-eol))))))))))
13040 (defun org-add-planning-info (what &optional time &rest remove)
13041 "Insert new timestamp with keyword in the line directly after the headline.
13042 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
13043 If non is given, the user is prompted for a date.
13044 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
13047 (let (org-time-was-given org-end-time-was-given ts
13048 end default-time default-input)
13051 (when (and (memq what '(scheduled deadline))
13053 (and (stringp time)
13054 (string-match "^[-+]+[0-9]" time))))
13055 ;; Try to get a default date/time from existing timestamp
13057 (org-back-to-heading t)
13058 (setq end (save-excursion (outline-next-heading) (point)))
13059 (when (re-search-forward (if (eq what 'scheduled)
13060 org-scheduled-time-regexp
13061 org-deadline-time-regexp)
13063 (setq ts (match-string 1)
13065 (apply 'encode-time (org-parse-time-string ts))
13066 default-input (and ts (org-get-compact-tod ts))))))
13070 ;; This is a string (relative or absolute), set proper date
13071 (apply 'encode-time
13072 (org-read-date-analyze
13073 time default-time (decode-time default-time)))
13074 ;; If necessary, get the time from the user
13075 (or time (org-read-date nil 'to-time nil nil
13076 default-time default-input)))))
13078 (when (and org-insert-labeled-timestamps-at-point
13079 (member what '(scheduled deadline)))
13081 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
13082 (org-insert-time-stamp time org-time-was-given
13083 nil nil nil (list org-end-time-was-given))
13087 (let (col list elt ts buffer-invisibility-spec)
13088 (org-back-to-heading t)
13089 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
13090 (goto-char (match-end 1))
13091 (setq col (current-column))
13092 (goto-char (match-end 0))
13093 (if (eobp) (insert "\n") (forward-char 1))
13094 (when (and (not what)
13097 org-keyword-time-not-clock-regexp))))
13098 ;; Nothing to add, nothing to remove...... :-)
13100 (if (and (not (looking-at org-outline-regexp))
13101 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
13103 (not (equal (match-string 1) org-clock-string)))
13104 (narrow-to-region (match-beginning 0) (match-end 0))
13105 (insert-before-markers "\n")
13107 (narrow-to-region (point) (point))
13108 (and org-adapt-indentation (org-indent-to-column col)))
13109 ;; Check if we have to remove something.
13110 (setq list (cons what remove))
13112 (setq elt (pop list))
13113 (when (or (and (eq elt 'scheduled)
13114 (re-search-forward org-scheduled-time-regexp nil t))
13115 (and (eq elt 'deadline)
13116 (re-search-forward org-deadline-time-regexp nil t))
13117 (and (eq elt 'closed)
13118 (re-search-forward org-closed-time-regexp nil t)))
13120 (if (looking-at "--+<[^>]+>") (replace-match ""))))
13121 (and (looking-at "[ \t]+") (replace-match ""))
13122 (and org-adapt-indentation (bolp) (org-indent-to-column col))
13125 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
13126 (cond ((eq what 'scheduled) org-scheduled-string)
13127 ((eq what 'deadline) org-deadline-string)
13128 ((eq what 'closed) org-closed-string))
13130 (setq ts (org-insert-time-stamp
13132 (or org-time-was-given
13133 (and (eq what 'closed) org-log-done-with-time))
13135 nil nil (list org-end-time-was-given)))
13137 (if (not (or (bolp) (eq (char-before) ?\ )
13138 (memq (char-after) '(32 10))
13141 (goto-char (point-min))
13143 (if (and (looking-at "[ \t]*\n")
13144 (equal (char-before) ?\n))
13145 (delete-region (1- (point)) (point-at-eol)))
13148 (defvar org-log-note-marker (make-marker))
13149 (defvar org-log-note-purpose nil)
13150 (defvar org-log-note-state nil)
13151 (defvar org-log-note-previous-state nil)
13152 (defvar org-log-note-how nil)
13153 (defvar org-log-note-extra nil)
13154 (defvar org-log-note-window-configuration nil)
13155 (defvar org-log-note-return-to (make-marker))
13156 (defvar org-log-note-effective-time nil
13157 "Remembered current time so that dynamically scoped
13158 `org-extend-today-until' affects tha timestamps in state change
13161 (defvar org-log-post-message nil
13162 "Message to be displayed after a log note has been stored.
13163 The auto-repeater uses this.")
13165 (defun org-add-note ()
13166 "Add a note to the current entry.
13167 This is done in the same way as adding a state change note."
13169 (org-add-log-setup 'note nil nil 'findpos nil))
13171 (defvar org-property-end-re)
13172 (defun org-add-log-setup (&optional purpose state prev-state
13174 "Set up the post command hook to take a note.
13175 If this is about to TODO state change, the new state is expected in STATE.
13176 When FINDPOS is non-nil, find the correct position for the note in
13177 the current entry. If not, assume that it can be inserted at point.
13178 HOW is an indicator what kind of note should be created.
13179 EXTRA is additional text that will be inserted into the notes buffer."
13180 (let* ((org-log-into-drawer (org-log-into-drawer))
13181 (drawer (cond ((stringp org-log-into-drawer)
13182 org-log-into-drawer)
13183 (org-log-into-drawer "LOGBOOK"))))
13187 (org-back-to-heading t)
13188 (narrow-to-region (point) (save-excursion
13189 (outline-next-heading) (point)))
13190 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
13191 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
13193 (goto-char (match-end 0))
13196 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
13199 (goto-char (match-end 0))
13200 (or org-log-states-order-reversed
13201 (and (re-search-forward org-property-end-re nil t)
13202 (goto-char (1- (match-beginning 0))))))
13203 (insert "\n:" drawer ":\n:END:")
13204 (beginning-of-line 0)
13206 (beginning-of-line 2)
13209 ((and org-log-state-notes-insert-after-drawers
13211 (forward-line) (looking-at org-drawer-regexp)))
13213 (while (looking-at org-drawer-regexp)
13214 (goto-char (match-end 0))
13215 (re-search-forward org-property-end-re (point-max) t)
13217 (forward-line -1)))
13218 (unless org-log-states-order-reversed
13219 (and (= (char-after) ?\n) (forward-char 1))
13220 (org-skip-over-state-notes)
13221 (skip-chars-backward " \t\n\r")))
13222 (move-marker org-log-note-marker (point))
13223 (setq org-log-note-purpose purpose
13224 org-log-note-state state
13225 org-log-note-previous-state prev-state
13226 org-log-note-how how
13227 org-log-note-extra extra
13228 org-log-note-effective-time (org-current-effective-time))
13229 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
13231 (defun org-skip-over-state-notes ()
13232 "Skip past the list of State notes in an entry."
13233 (if (looking-at "\n[ \t]*- State") (forward-char 1))
13234 (when (ignore-errors (goto-char (org-in-item-p)))
13235 (let* ((struct (org-list-struct))
13236 (prevs (org-list-prevs-alist struct)))
13237 (while (looking-at "[ \t]*- State")
13238 (goto-char (or (org-list-get-next-item (point) struct prevs)
13239 (org-list-get-item-end (point) struct)))))))
13241 (defun org-add-log-note (&optional purpose)
13242 "Pop up a window for taking a note, and add this note later at point."
13243 (remove-hook 'post-command-hook 'org-add-log-note)
13244 (setq org-log-note-window-configuration (current-window-configuration))
13245 (delete-other-windows)
13246 (move-marker org-log-note-return-to (point))
13247 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
13248 (goto-char org-log-note-marker)
13249 (org-switch-to-buffer-other-window "*Org Note*")
13251 (if (memq org-log-note-how '(time state))
13252 (let (current-prefix-arg) (org-store-log-note))
13253 (let ((org-inhibit-startup t)) (org-mode))
13254 (insert (format "# Insert note for %s.
13255 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
13257 ((eq org-log-note-purpose 'clock-out) "stopped clock")
13258 ((eq org-log-note-purpose 'done) "closed todo item")
13259 ((eq org-log-note-purpose 'state)
13260 (format "state change from \"%s\" to \"%s\""
13261 (or org-log-note-previous-state "")
13262 (or org-log-note-state "")))
13263 ((eq org-log-note-purpose 'reschedule)
13265 ((eq org-log-note-purpose 'delschedule)
13266 "no longer scheduled")
13267 ((eq org-log-note-purpose 'redeadline)
13268 "changing deadline")
13269 ((eq org-log-note-purpose 'deldeadline)
13270 "removing deadline")
13271 ((eq org-log-note-purpose 'refile)
13273 ((eq org-log-note-purpose 'note)
13275 (t (error "This should not happen")))))
13276 (if org-log-note-extra (insert org-log-note-extra))
13277 (org-set-local 'org-finish-function 'org-store-log-note)
13278 (run-hooks 'org-log-buffer-setup-hook)))
13280 (defvar org-note-abort nil) ; dynamically scoped
13281 (defun org-store-log-note ()
13282 "Finish taking a log note, and insert it to where it belongs."
13283 (let ((txt (buffer-string)))
13284 (kill-buffer (current-buffer))
13285 (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
13287 (while (string-match "\\`# .*\n[ \t\n]*" txt)
13288 (setq txt (replace-match "" t t txt)))
13289 (if (string-match "\\s-+\\'" txt)
13290 (setq txt (replace-match "" t t txt)))
13291 (setq lines (org-split-string txt "\n"))
13292 (when (and note (string-match "\\S-" note))
13294 (org-replace-escapes
13296 (list (cons "%u" (user-login-name))
13297 (cons "%U" user-full-name)
13298 (cons "%t" (format-time-string
13299 (org-time-stamp-format 'long 'inactive)
13300 org-log-note-effective-time))
13301 (cons "%T" (format-time-string
13302 (org-time-stamp-format 'long nil)
13303 org-log-note-effective-time))
13304 (cons "%d" (format-time-string
13305 (org-time-stamp-format nil 'inactive)
13306 org-log-note-effective-time))
13307 (cons "%D" (format-time-string
13308 (org-time-stamp-format nil nil)
13309 org-log-note-effective-time))
13310 (cons "%s" (if org-log-note-state
13311 (concat "\"" org-log-note-state "\"")
13313 (cons "%S" (if org-log-note-previous-state
13314 (concat "\"" org-log-note-previous-state "\"")
13316 (if lines (setq note (concat note " \\\\")))
13318 (when (or current-prefix-arg org-note-abort)
13319 (when org-log-into-drawer
13320 (org-remove-empty-drawer-at
13321 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
13322 org-log-note-marker))
13325 (with-current-buffer (marker-buffer org-log-note-marker)
13327 (goto-char org-log-note-marker)
13328 (move-marker org-log-note-marker nil)
13330 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
13331 (setq ind (save-excursion
13332 (if (ignore-errors (goto-char (org-in-item-p)))
13333 (let ((struct (org-list-struct)))
13335 (org-list-get-top-point struct) struct))
13336 (skip-chars-backward " \r\t\n")
13338 ((and (org-at-heading-p)
13339 org-adapt-indentation)
13340 (1+ (org-current-level)))
13341 ((org-at-heading-p) 0)
13342 (t (org-get-indentation))))))
13343 (setq bul (org-list-bullet-string "-"))
13344 (org-indent-line-to ind)
13345 (insert bul (pop lines))
13346 (let ((ind-body (+ (length bul) ind)))
13349 (org-indent-line-to ind-body)
13350 (insert (pop lines))))
13351 (message "Note stored")
13352 (org-back-to-heading t)
13353 (org-cycle-hide-drawers 'children))
13354 ;; Fix `buffer-undo-list' when `org-store-log-note' is called
13355 ;; from within `org-add-log-note' because `buffer-undo-list'
13356 ;; is then modified outside of `org-with-remote-undo'.
13357 (when (eq this-command 'org-agenda-todo)
13358 (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
13359 ;; Don't add undo information when called from `org-agenda-todo'
13360 (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
13361 (set-window-configuration org-log-note-window-configuration)
13362 (with-current-buffer (marker-buffer org-log-note-return-to)
13363 (goto-char org-log-note-return-to))
13364 (move-marker org-log-note-return-to nil)
13365 (and org-log-post-message (message "%s" org-log-post-message))))
13367 (defun org-remove-empty-drawer-at (drawer pos)
13368 "Remove an empty drawer DRAWER at position POS.
13369 POS may also be a marker."
13370 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
13376 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
13377 (replace-match ""))))))
13379 (defvar org-ts-type nil)
13380 (defun org-sparse-tree (&optional arg type)
13381 "Create a sparse tree, prompt for the details.
13382 This command can create sparse trees. You first need to select the type
13383 of match used to create the tree:
13385 t Show all TODO entries.
13386 T Show entries with a specific TODO keyword.
13387 m Show entries selected by a tags/property match.
13388 p Enter a property name and its value (both with completion on existing
13389 names/values) and show entries with that property.
13390 r Show entries matching a regular expression (`/' can be used as well).
13391 b Show deadlines and scheduled items before a date.
13392 a Show deadlines and scheduled items after a date.
13393 d Show deadlines due within `org-deadline-warning-days'.
13394 D Show deadlines and scheduled items between a date range."
13396 (let (ans kwd value ts-type)
13397 (setq type (or type org-sparse-tree-default-date-type))
13398 (setq org-ts-type type)
13399 (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"
13400 (cond ((eq type 'all) "all timestamps")
13401 ((eq type 'scheduled) "only scheduled")
13402 ((eq type 'deadline) "only deadline")
13403 ((eq type 'active) "only active timestamps")
13404 ((eq type 'inactive) "only inactive timestamps")
13405 ((eq type 'scheduled-or-deadline) "scheduled/deadline")
13406 ((eq type 'closed) "with a closed time-stamp")
13407 (t "scheduled/deadline")))
13408 (setq ans (read-char-exclusive))
13412 arg (cadr (member type '(scheduled-or-deadline
13413 all scheduled deadline active inactive closed)))))
13415 (call-interactively 'org-check-deadlines))
13417 (call-interactively 'org-check-before-date))
13419 (call-interactively 'org-check-after-date))
13421 (call-interactively 'org-check-dates-range))
13423 (call-interactively 'org-show-todo-tree))
13425 (org-show-todo-tree '(4)))
13426 ((member ans '(?T ?m))
13427 (call-interactively 'org-match-sparse-tree))
13428 ((member ans '(?p ?P))
13429 (setq kwd (org-icompleting-read "Property: "
13430 (mapcar 'list (org-buffer-property-keys))))
13431 (setq value (org-icompleting-read "Value: "
13432 (mapcar 'list (org-property-values kwd))))
13433 (unless (string-match "\\`{.*}\\'" value)
13434 (setq value (concat "\"" value "\"")))
13435 (org-match-sparse-tree arg (concat kwd "=" value)))
13436 ((member ans '(?r ?R ?/))
13437 (call-interactively 'org-occur))
13438 (t (user-error "No such sparse tree command \"%c\"" ans)))))
13440 (defvar org-occur-highlights nil
13441 "List of overlays used for occur matches.")
13442 (make-variable-buffer-local 'org-occur-highlights)
13443 (defvar org-occur-parameters nil
13444 "Parameters of the active org-occur calls.
13445 This is a list, each call to org-occur pushes as cons cell,
13446 containing the regular expression and the callback, onto the list.
13447 The list can contain several entries if `org-occur' has been called
13448 several time with the KEEP-PREVIOUS argument. Otherwise, this list
13449 will only contain one set of parameters. When the highlights are
13450 removed (for example with `C-c C-c', or with the next edit (depending
13451 on `org-remove-highlights-with-change'), this variable is emptied
13453 (make-variable-buffer-local 'org-occur-parameters)
13455 (defun org-occur (regexp &optional keep-previous callback)
13456 "Make a compact tree which shows all matches of REGEXP.
13457 The tree will show the lines where the regexp matches, and all higher
13458 headlines above the match. It will also show the heading after the match,
13459 to make sure editing the matching entry is easy.
13460 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
13461 call to `org-occur' will be kept, to allow stacking of calls to this
13463 If CALLBACK is non-nil, it is a function which is called to confirm
13464 that the match should indeed be shown."
13465 (interactive "sRegexp: \nP")
13466 (when (equal regexp "")
13467 (user-error "Regexp cannot be empty"))
13468 (unless keep-previous
13469 (org-remove-occur-highlights nil nil t))
13470 (push (cons regexp callback) org-occur-parameters)
13473 (goto-char (point-min))
13474 (if (or (not keep-previous) ; do not want to keep
13475 (not org-occur-highlights)) ; no previous matches
13478 (while (re-search-forward regexp nil t)
13479 (when (or (not callback)
13480 (save-match-data (funcall callback)))
13481 (setq cnt (1+ cnt))
13482 (when org-highlight-sparse-tree-matches
13483 (org-highlight-new-match (match-beginning 0) (match-end 0)))
13484 (org-show-context 'occur-tree))))
13485 (when org-remove-highlights-with-change
13486 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
13488 (unless org-sparse-tree-open-archived-trees
13489 (org-hide-archived-subtrees (point-min) (point-max)))
13490 (run-hooks 'org-occur-hook)
13491 (if (org-called-interactively-p 'interactive)
13492 (message "%d match(es) for regexp %s" cnt regexp))
13495 (defun org-occur-next-match (&optional n reset)
13496 "Function for `next-error-function' to find sparse tree matches.
13497 N is the number of matches to move, when negative move backwards.
13498 RESET is entirely ignored - this function always goes back to the
13499 starting point when no match is found."
13500 (let* ((limit (if (< n 0) (point-min) (point-max)))
13501 (search-func (if (< n 0)
13502 'previous-single-char-property-change
13503 'next-single-char-property-change))
13508 (while (setq p1 (funcall search-func (point) 'org-type))
13509 (when (equal p1 limit)
13511 (error "No more matches"))
13512 (when (equal (get-char-property p1 'org-type) 'org-occur)
13516 (throw 'exit (point))))
13519 (error "No more matches"))))
13521 (defun org-show-context (&optional key)
13522 "Make sure point and context are visible.
13523 How much context is shown depends upon the variables
13524 `org-show-hierarchy-above', `org-show-following-heading',
13525 `org-show-entry-below' and `org-show-siblings'."
13526 (let ((heading-p (org-at-heading-p t))
13527 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
13528 (following-p (org-get-alist-option org-show-following-heading key))
13529 (entry-p (org-get-alist-option org-show-entry-below key))
13530 (siblings-p (org-get-alist-option org-show-siblings key)))
13531 ;; Show heading or entry text
13532 (if (and heading-p (not entry-p))
13533 (org-flag-heading nil) ; only show the heading
13534 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
13535 (org-show-hidden-entry))) ; show entire entry
13537 ;; Show next sibling, or heading below text
13539 (and (if heading-p (org-goto-sibling) (outline-next-heading))
13540 (org-flag-heading nil))))
13541 (when siblings-p (org-show-siblings))
13543 ;; show all higher headings, possibly with siblings
13545 (while (and (condition-case nil
13546 (progn (org-up-heading-all 1) t)
13549 (org-flag-heading nil)
13550 (when siblings-p (org-show-siblings)))))
13551 (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
13553 (defvar org-reveal-start-hook nil
13554 "Hook run before revealing a location.")
13556 (defun org-reveal (&optional siblings)
13557 "Show current entry, hierarchy above it, and the following headline.
13558 This can be used to show a consistent set of context around locations
13559 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
13560 not t for the search context.
13562 With optional argument SIBLINGS, on each level of the hierarchy all
13563 siblings are shown. This repairs the tree structure to what it would
13564 look like when opened with hierarchical calls to `org-cycle'.
13565 With double optional argument \\[universal-argument] \\[universal-argument], \
13566 go to the parent and show the
13569 (run-hooks 'org-reveal-start-hook)
13570 (let ((org-show-hierarchy-above t)
13571 (org-show-following-heading t)
13572 (org-show-siblings (if siblings t org-show-siblings)))
13573 (org-show-context nil))
13574 (when (equal siblings '(16))
13576 (when (org-up-heading-safe)
13578 (run-hook-with-args 'org-cycle-hook 'subtree)))))
13580 (defun org-highlight-new-match (beg end)
13581 "Highlight from BEG to END and mark the highlight is an occur headline."
13582 (let ((ov (make-overlay beg end)))
13583 (overlay-put ov 'face 'secondary-selection)
13584 (overlay-put ov 'org-type 'org-occur)
13585 (push ov org-occur-highlights)))
13587 (defun org-remove-occur-highlights (&optional beg end noremove)
13588 "Remove the occur highlights from the buffer.
13589 BEG and END are ignored. If NOREMOVE is nil, remove this function
13590 from the `before-change-functions' in the current buffer."
13592 (unless org-inhibit-highlight-removal
13593 (mapc 'delete-overlay org-occur-highlights)
13594 (setq org-occur-highlights nil)
13595 (setq org-occur-parameters nil)
13597 (remove-hook 'before-change-functions
13598 'org-remove-occur-highlights 'local))))
13602 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
13603 "Regular expression matching the priority indicator.")
13605 (defvar org-remove-priority-next-time nil)
13607 (defun org-priority-up ()
13608 "Increase the priority of the current item."
13610 (org-priority 'up))
13612 (defun org-priority-down ()
13613 "Decrease the priority of the current item."
13615 (org-priority 'down))
13617 (defun org-priority (&optional action show)
13618 "Change the priority of an item.
13619 ACTION can be `set', `up', `down', or a character."
13621 (if (equal action '(4))
13622 (org-show-priority)
13623 (unless org-enable-priority-commands
13624 (user-error "Priority commands are disabled"))
13625 (setq action (or action 'set))
13626 (let (current new news have remove)
13628 (org-back-to-heading t)
13629 (if (looking-at org-priority-regexp)
13630 (setq current (string-to-char (match-string 2))
13633 ((eq action 'remove)
13634 (setq remove t new ?\ ))
13635 ((or (eq action 'set)
13636 (if (featurep 'xemacs) (characterp action) (integerp action)))
13637 (if (not (eq action 'set))
13639 (message "Priority %c-%c, SPC to remove: "
13640 org-highest-priority org-lowest-priority)
13642 (setq new (read-char-exclusive))))
13643 (if (and (= (upcase org-highest-priority) org-highest-priority)
13644 (= (upcase org-lowest-priority) org-lowest-priority))
13645 (setq new (upcase new)))
13646 (cond ((equal new ?\ ) (setq remove t))
13647 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
13648 (user-error "Priority must be between `%c' and `%c'"
13649 org-highest-priority org-lowest-priority))))
13652 (1- current) ; normal cycling
13653 ;; last priority was empty
13654 (if (eq last-command this-command)
13655 org-lowest-priority ; wrap around empty to lowest
13657 (if org-priority-start-cycle-with-default
13658 org-default-priority
13659 (1- org-default-priority))))))
13662 (1+ current) ; normal cycling
13663 ;; last priority was empty
13664 (if (eq last-command this-command)
13665 org-highest-priority ; wrap around empty to highest
13667 (if org-priority-start-cycle-with-default
13668 org-default-priority
13669 (1+ org-default-priority))))))
13670 (t (user-error "Invalid action")))
13671 (if (or (< (upcase new) org-highest-priority)
13672 (> (upcase new) org-lowest-priority))
13673 (if (and (memq action '(up down))
13674 (not have) (not (eq last-command this-command)))
13675 ;; `new' is from default priority
13677 "The default can not be set, see `org-default-priority' why")
13678 ;; normal cycling: `new' is beyond highest/lowest priority
13679 ;; and is wrapped around to the empty priority
13681 (setq news (format "%c" new))
13684 (replace-match "" t t nil 1)
13685 (replace-match news t t nil 2))
13687 (user-error "No priority cookie found in line")
13688 (let ((case-fold-search nil))
13689 (looking-at org-todo-line-regexp))
13692 (goto-char (match-end 2))
13693 (insert " [#" news "]"))
13694 (goto-char (match-beginning 3))
13695 (insert "[#" news "] "))))
13696 (org-preserve-lc (org-set-tags nil 'align)))
13698 (message "Priority removed")
13699 (message "Priority of current item set to %s" news)))))
13701 (defun org-show-priority ()
13702 "Show the priority of the current item.
13703 This priority is composed of the main priority given with the [#A] cookies,
13704 and by additional input from the age of a schedules or deadline entry."
13706 (let ((pri (if (eq major-mode 'org-agenda-mode)
13707 (org-get-at-bol 'priority)
13710 (beginning-of-line)
13711 (and (looking-at org-heading-regexp)
13712 (org-get-priority (match-string 0))))))))
13713 (message "Priority is %d" (if pri pri -1000))))
13715 (defun org-get-priority (s)
13716 "Find priority cookie and return priority."
13718 (if (functionp org-get-priority-function)
13719 (funcall org-get-priority-function)
13720 (if (not (string-match org-priority-regexp s))
13721 (* 1000 (- org-lowest-priority org-default-priority))
13722 (* 1000 (- org-lowest-priority
13723 (string-to-char (match-string 2 s))))))))
13727 (defvar org-agenda-archives-mode)
13728 (defvar org-map-continue-from nil
13729 "Position from where mapping should continue.
13730 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
13732 (defvar org-scanner-tags nil
13733 "The current tag list while the tags scanner is running.")
13734 (defvar org-trust-scanner-tags nil
13735 "Should `org-get-tags-at' use the tags for the scanner.
13736 This is for internal dynamical scoping only.
13737 When this is non-nil, the function `org-get-tags-at' will return the value
13738 of `org-scanner-tags' instead of building the list by itself. This
13739 can lead to large speed-ups when the tags scanner is used in a file with
13740 many entries, and when the list of tags is retrieved, for example to
13741 obtain a list of properties. Building the tags list for each entry in such
13742 a file becomes an N^2 operation - but with this variable set, it scales
13745 (defun org-scan-tags (action matcher todo-only &optional start-level)
13746 "Sca headline tags with inheritance and produce output ACTION.
13748 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
13749 or `agenda' to produce an entry list for an agenda view. It can also be
13750 a Lisp form or a function that should be called at each matched headline, in
13751 this case the return value is a list of all return values from these calls.
13753 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
13754 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
13755 only lines with a not-done TODO keyword are included in the output.
13756 This should be the same variable that was scoped into
13757 and set by `org-make-tags-matcher' when it constructed MATCHER.
13759 START-LEVEL can be a string with asterisks, reducing the scope to
13760 headlines matching this string."
13761 (require 'org-agenda)
13762 (let* ((re (concat "^"
13764 ;; Get the correct level to match
13765 (concat "\\*\\{" (number-to-string start-level) "\\} ")
13766 org-outline-regexp)
13768 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
13769 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
13770 (props (list 'face 'default
13771 'done-face 'org-agenda-done
13772 'undone-face 'default
13773 'mouse-face 'highlight
13774 'org-not-done-regexp org-not-done-regexp
13775 'org-todo-regexp org-todo-regexp
13776 'org-complex-heading-regexp org-complex-heading-regexp
13778 (format "mouse-2 or RET jump to org file %s"
13779 (abbreviate-file-name
13780 (or (buffer-file-name (buffer-base-buffer))
13781 (buffer-name (buffer-base-buffer)))))))
13782 (case-fold-search nil)
13783 (org-map-continue-from nil)
13784 lspos tags tags-list
13785 (tags-alist (list (cons 0 org-file-tags)))
13786 (llast 0) rtn rtn1 level category i txt
13787 todo marker entry priority)
13788 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
13789 (setq action (list 'lambda nil action)))
13791 (goto-char (point-min))
13792 (when (eq action 'sparse-tree)
13794 (org-remove-occur-highlights))
13795 (while (re-search-forward re nil t)
13796 (setq org-map-continue-from nil)
13798 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
13799 tags (if (match-end 4) (org-match-string-no-properties 4)))
13800 (goto-char (setq lspos (match-beginning 0)))
13801 (setq level (org-reduced-level (org-outline-level))
13802 category (org-get-category))
13803 (setq i llast llast level)
13804 ;; remove tag lists from same and sublevels
13805 (while (>= i level)
13806 (when (setq entry (assoc i tags-alist))
13807 (setq tags-alist (delete entry tags-alist)))
13809 ;; add the next tags
13811 (setq tags (org-split-string tags ":")
13813 (cons (cons level tags) tags-alist)))
13814 ;; compile tags for current headline
13816 (if org-use-tag-inheritance
13817 (apply 'append (mapcar 'cdr (reverse tags-alist)))
13819 org-scanner-tags tags-list)
13820 (when org-use-tag-inheritance
13821 (setcdr (car tags-alist)
13822 (mapcar (lambda (x)
13823 (setq x (copy-sequence x))
13824 (org-add-prop-inherited x))
13825 (cdar tags-alist))))
13826 (when (and tags org-use-tag-inheritance
13827 (or (not (eq t org-use-tag-inheritance))
13828 org-tags-exclude-from-inheritance))
13829 ;; selective inheritance, remove uninherited ones
13830 (setcdr (car tags-alist)
13831 (org-remove-uninherited-tags (cdar tags-alist))))
13834 ;; eval matcher only when the todo condition is OK
13835 (and (or (not todo-only) (member todo org-not-done-keywords))
13836 (let ((case-fold-search t) (org-trust-scanner-tags t))
13839 ;; Call the skipper, but return t if it does not skip,
13840 ;; so that the `and' form continues evaluating
13842 (unless (eq action 'sparse-tree) (org-agenda-skip))
13845 ;; Check if timestamps are deselecting this entry
13846 (or (not todo-only)
13847 (and (member todo org-not-done-keywords)
13848 (or (not org-agenda-tags-todo-honor-ignore-options)
13849 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
13851 ;; select this headline
13853 ((eq action 'sparse-tree)
13854 (and org-highlight-sparse-tree-matches
13855 (org-get-heading) (match-end 0)
13856 (org-highlight-new-match
13857 (match-beginning 1) (match-end 1)))
13858 (org-show-context 'tags-tree))
13859 ((eq action 'agenda)
13860 (setq txt (org-agenda-format-item
13863 (if (eq org-tags-match-list-sublevels 'indented)
13864 (make-string (1- level) ?.) "")
13868 priority (org-get-priority txt))
13870 (setq marker (org-agenda-new-marker))
13871 (org-add-props txt props
13872 'org-marker marker 'org-hd-marker marker 'org-category category
13874 'priority priority 'type "tagsmatch")
13876 ((functionp action)
13877 (setq org-map-continue-from nil)
13879 (setq rtn1 (funcall action))
13881 (t (user-error "Invalid action")))
13883 ;; if we are to skip sublevels, jump to end of subtree
13884 (unless org-tags-match-list-sublevels
13885 (org-end-of-subtree t)
13886 (backward-char 1))))
13887 ;; Get the correct position from where to continue
13888 (if org-map-continue-from
13889 (goto-char org-map-continue-from)
13890 (and (= (point) lspos) (end-of-line 1)))))
13891 (when (and (eq action 'sparse-tree)
13892 (not org-sparse-tree-open-archived-trees))
13893 (org-hide-archived-subtrees (point-min) (point-max)))
13896 (defun org-remove-uninherited-tags (tags)
13897 "Remove all tags that are not inherited from the list TAGS."
13899 ((eq org-use-tag-inheritance t)
13900 (if org-tags-exclude-from-inheritance
13901 (org-delete-all org-tags-exclude-from-inheritance tags)
13903 ((not org-use-tag-inheritance) nil)
13904 ((stringp org-use-tag-inheritance)
13907 (if (and (string-match org-use-tag-inheritance x)
13908 (not (member x org-tags-exclude-from-inheritance)))
13911 ((listp org-use-tag-inheritance)
13914 (if (member x org-use-tag-inheritance) x nil))
13917 (defun org-match-sparse-tree (&optional todo-only match)
13918 "Create a sparse tree according to tags string MATCH.
13919 MATCH can contain positive and negative selection of tags, like
13920 \"+WORK+URGENT-WITHBOSS\".
13921 If optional argument TODO-ONLY is non-nil, only select lines that are
13924 (org-agenda-prepare-buffers (list (current-buffer)))
13925 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13927 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13929 (defvar org-cached-props nil)
13930 (defun org-cached-entry-get (pom property)
13931 (if (or (eq t org-use-property-inheritance)
13932 (and (stringp org-use-property-inheritance)
13933 (string-match org-use-property-inheritance property))
13934 (and (listp org-use-property-inheritance)
13935 (member property org-use-property-inheritance)))
13936 ;; Caching is not possible, check it directly
13937 (org-entry-get pom property 'inherit)
13938 ;; Get all properties, so that we can do complicated checks easily
13939 (cdr (assoc property (or org-cached-props
13940 (setq org-cached-props
13941 (org-entry-properties pom)))))))
13943 (defun org-global-tags-completion-table (&optional files)
13944 "Return the list of all tags in all agenda buffer/files.
13945 Optional FILES argument is a list of files which can be used
13946 instead of the agenda files."
13953 (set-buffer (find-file-noselect file))
13954 (append (org-get-buffer-tags)
13955 (mapcar (lambda (x) (if (stringp (car-safe x))
13956 (list (car-safe x)) nil))
13958 (if (and files (car files))
13960 (org-agenda-files))))))))
13962 (defun org-make-tags-matcher (match)
13963 "Create the TAGS/TODO matcher form for the selection string MATCH.
13965 The variable `todo-only' is scoped dynamically into this function.
13966 It will be set to t if the matcher restricts matching to TODO entries,
13967 otherwise will not be touched.
13969 Returns a cons of the selection string MATCH and the constructed
13970 lisp form implementing the matcher. The matcher is to be evaluated
13971 at an Org entry, with point on the headline, and returns t if the
13972 entry matches the selection string MATCH. The returned lisp form
13973 references two variables with information about the entry, which
13974 must be bound around the form's evaluation: todo, the TODO keyword
13975 at the entry (or nil of none); and tags-list, the list of all tags
13976 at the entry including inherited ones. Additionally, the category
13977 of the entry (if any) must be specified as the text property
13978 'org-category on the headline.
13980 See also `org-scan-tags'.
13982 (declare (special todo-only))
13983 (unless (boundp 'todo-only)
13984 (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
13986 ;; Get a new match request, with completion against the global
13987 ;; tags table and the local tags in current buffer
13988 (let ((org-last-tags-completion-table
13990 (delq nil (append (org-get-buffer-tags)
13991 (org-global-tags-completion-table))))))
13992 (setq match (org-completing-read-no-i
13993 "Match: " 'org-tags-completion-function nil nil nil
13994 'org-tags-history))))
13996 ;; Parse the string and create a lisp form
13997 (let ((match0 match)
13998 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
14000 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
14001 orterms term orlist re-p str-p level-p level-op time-p
14002 prop-p pn pv po gv rest)
14003 ;; Expand group tags
14004 (setq match (org-tags-expand match))
14005 (if (string-match "/+" match)
14006 ;; match contains also a todo-matching request
14008 (setq tagsmatch (substring match 0 (match-beginning 0))
14009 todomatch (substring match (match-end 0)))
14010 (if (string-match "^!" todomatch)
14011 (setq todo-only t todomatch (substring todomatch 1)))
14012 (if (string-match "^\\s-*$" todomatch)
14013 (setq todomatch nil)))
14014 ;; only matching tags
14015 (setq tagsmatch match todomatch nil))
14017 ;; Make the tags matcher
14018 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
14019 (setq tagsmatcher t)
14020 (setq orterms (org-split-string tagsmatch "|") orlist nil)
14021 (while (setq term (pop orterms))
14022 (while (and (equal (substring term -1) "\\") orterms)
14023 (setq term (concat term "|" (pop orterms)))) ; repair bad split
14024 (while (string-match re term)
14025 (setq rest (substring term (match-end 0))
14026 minus (and (match-end 1)
14027 (equal (match-string 1 term) "-"))
14028 tag (save-match-data (replace-regexp-in-string
14030 (match-string 2 term)))
14031 re-p (equal (string-to-char tag) ?{)
14032 level-p (match-end 4)
14033 prop-p (match-end 5)
14035 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
14037 (setq level-op (org-op-to-function (match-string 3 term)))
14038 `(,level-op level ,(string-to-number
14039 (match-string 4 term))))
14041 (setq pn (match-string 5 term)
14042 po (match-string 6 term)
14043 pv (match-string 7 term)
14044 re-p (equal (string-to-char pv) ?{)
14045 str-p (equal (string-to-char pv) ?\")
14046 time-p (save-match-data
14047 (string-match "^\"[[<].*[]>]\"$" pv))
14048 pv (if (or re-p str-p) (substring pv 1 -1) pv))
14049 (if time-p (setq pv (org-matcher-time pv)))
14050 (setq po (org-op-to-function po (if time-p 'time str-p)))
14052 ((equal pn "CATEGORY")
14053 (setq gv '(get-text-property (point) 'org-category)))
14057 (setq gv `(org-cached-entry-get nil ,pn))))
14060 `(not (string-match ,pv (or ,gv "")))
14061 `(string-match ,pv (or ,gv "")))
14063 `(,po (or ,gv "") ,pv)
14064 `(,po (string-to-number (or ,gv ""))
14065 ,(string-to-number pv) ))))
14066 (t `(member ,tag tags-list)))
14067 mm (if minus (list 'not mm) mm)
14069 (push mm tagsmatcher))
14070 (push (if (> (length tagsmatcher) 1)
14071 (cons 'and tagsmatcher)
14074 (setq tagsmatcher nil))
14075 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
14077 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
14078 ;; Make the todo matcher
14079 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
14080 (setq todomatcher t)
14081 (setq orterms (org-split-string todomatch "|") orlist nil)
14082 (while (setq term (pop orterms))
14083 (while (string-match re term)
14084 (setq minus (and (match-end 1)
14085 (equal (match-string 1 term) "-"))
14086 kwd (match-string 2 term)
14087 re-p (equal (string-to-char kwd) ?{)
14088 term (substring term (match-end 0))
14090 `(string-match ,(substring kwd 1 -1) todo)
14091 (list 'equal 'todo kwd))
14092 mm (if minus (list 'not mm) mm))
14093 (push mm todomatcher))
14094 (push (if (> (length todomatcher) 1)
14095 (cons 'and todomatcher)
14098 (setq todomatcher nil))
14099 (setq todomatcher (if (> (length orlist) 1)
14100 (cons 'or orlist) (car orlist))))
14102 ;; Return the string and lisp forms of the matcher
14103 (setq matcher (if todomatcher
14104 (list 'and tagsmatcher todomatcher)
14107 (setq matcher (list 'and '(member todo org-not-done-keywords)
14109 (cons match0 matcher)))
14111 (defun org-tags-expand (match &optional single-as-list downcased)
14112 "Expand group tags in MATCH.
14114 This replaces every group tag in MATCH with a regexp tag search.
14115 For example, a group tag \"Work\" defined as { Work : Lab Conf }
14116 will be replaced like this:
14118 Work => {\\(?:Work\\|Lab\\|Conf\\)}
14119 +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
14120 -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
14122 Replacing by a regexp preserves the structure of the match.
14123 E.g., this expansion
14125 Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
14127 will match anything tagged with \"Lab\" and \"Home\", or tagged
14128 with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
14130 When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
14131 assumed to be a single group tag, and the function will return
14132 the list of tags in this group.
14134 When DOWNCASE is non-nil, expand downcased TAGS."
14136 (let* ((case-fold-search t)
14137 (stable org-mode-syntax-table)
14138 (tal (or org-tag-groups-alist-for-agenda
14139 org-tag-groups-alist))
14141 (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
14142 (tml (mapcar 'car tal))
14143 (rtnmatch match) rpl)
14144 ;; @ and _ are allowed as word-components in tags
14145 (modify-syntax-entry ?@ "w" stable)
14146 (modify-syntax-entry ?_ "w" stable)
14149 (concat "\\(?1:[+-]?\\)\\(?2:\\<"
14150 (regexp-opt tml) "\\>\\)") rtnmatch))
14151 (let* ((dir (match-string 1 rtnmatch))
14152 (tag (match-string 2 rtnmatch))
14153 (tag (if downcased (downcase tag) tag)))
14154 (setq tml (delete tag tml))
14155 (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
14156 (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
14157 (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
14158 (if (stringp rpl) (org-add-props rpl '(grouptag t)))
14159 (setq rtnmatch (replace-match rpl t t rtnmatch)))))
14161 (or (reverse rpl) (list rtnmatch))
14163 (if single-as-list (list (if downcased (downcase match) match))
14166 (defun org-op-to-function (op &optional stringp)
14167 "Turn an operator into the appropriate function."
14170 ((equal op "<" ) '(< string< org-time<))
14171 ((equal op ">" ) '(> org-string> org-time>))
14172 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
14173 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
14174 ((member op '("=" "==")) '(= string= org-time=))
14175 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
14176 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
14178 (defun org<> (a b) (not (= a b)))
14179 (defun org-string<= (a b) (or (string= a b) (string< a b)))
14180 (defun org-string>= (a b) (not (string< a b)))
14181 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
14182 (defun org-string<> (a b) (not (string= a b)))
14183 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
14184 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
14185 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
14186 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
14187 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
14188 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
14190 "Convert S to a floating point time.
14191 If S is already a number, just return it. If it is a string, parse
14192 it as a time string and apply `float-time' to it. If S is nil, just return 0."
14196 (condition-case nil
14197 (float-time (apply 'encode-time (org-parse-time-string s)))
14201 (defun org-time-today ()
14202 "Time in seconds today at 0:00.
14203 Returns the float number of seconds since the beginning of the
14204 epoch to the beginning of today (00:00)."
14205 (float-time (apply 'encode-time
14206 (append '(0 0 0) (nthcdr 3 (decode-time))))))
14208 (defun org-matcher-time (s)
14209 "Interpret a time comparison value."
14212 ((string= s "<now>") (float-time))
14213 ((string= s "<today>") (org-time-today))
14214 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
14215 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
14216 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
14217 (+ (org-time-today)
14218 (* (string-to-number (match-string 1 s))
14219 (cdr (assoc (match-string 2 s)
14220 '(("d" . 86400.0) ("w" . 604800.0)
14221 ("m" . 2678400.0) ("y" . 31557600.0)))))))
14224 (defun org-match-any-p (re list)
14225 "Does re match any element of list?"
14226 (setq list (mapcar (lambda (x) (string-match re x)) list))
14229 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
14230 (defvar org-tags-overlay (make-overlay 1 1))
14231 (org-detach-overlay org-tags-overlay)
14233 (defun org-get-local-tags-at (&optional pos)
14234 "Get a list of tags defined in the current headline."
14235 (org-get-tags-at pos 'local))
14237 (defun org-get-local-tags ()
14238 "Get a list of tags defined in the current headline."
14239 (org-get-tags-at nil 'local))
14241 (defun org-get-tags-at (&optional pos local)
14242 "Get a list of all headline tags applicable at POS.
14243 POS defaults to point. If tags are inherited, the list contains
14244 the targets in the same sequence as the headlines appear, i.e.
14245 the tags of the current headline come last.
14246 When LOCAL is non-nil, only return tags from the current headline,
14247 ignore inherited ones."
14249 (if (and org-trust-scanner-tags
14250 (or (not pos) (equal pos (point)))
14253 (let (tags ltags lastpos parent)
14257 (goto-char (or pos (point)))
14260 (condition-case nil
14262 (org-back-to-heading t)
14263 (while (not (equal lastpos (point)))
14264 (setq lastpos (point))
14266 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
14267 (setq ltags (org-split-string
14268 (org-match-string-no-properties 1) ":"))
14270 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
14273 (org-remove-uninherited-tags ltags)
14276 (or org-use-tag-inheritance (throw 'done t))
14277 (if local (throw 'done t))
14278 (or (org-up-heading-safe) (error nil))
14283 (reverse (delete-dups
14285 (org-remove-uninherited-tags
14286 org-file-tags) tags)))))))))
14288 (defun org-add-prop-inherited (s)
14289 (add-text-properties 0 (length s) '(inherited t) s)
14292 (defun org-toggle-tag (tag &optional onoff)
14293 "Toggle the tag TAG for the current line.
14294 If ONOFF is `on' or `off', don't toggle but set to this state."
14297 (org-back-to-heading t)
14298 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
14301 (setq current (match-string 1))
14302 (replace-match ""))
14304 (setq current (nreverse (org-split-string current ":")))
14308 (or (member tag current) (push tag current)))
14310 (or (not (member tag current)) (setq current (delete tag current))))
14311 (t (if (member tag current)
14312 (setq current (delete tag current))
14314 (push tag current))))
14318 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
14319 (org-set-tags nil t))
14320 (delete-horizontal-space))
14321 (run-hooks 'org-after-tags-change-hook))
14324 (defun org-align-tags-here (to-col)
14325 ;; Assumes that this is a headline
14326 (let ((pos (point)) (col (current-column)) ncol tags-l p)
14327 (beginning-of-line 1)
14328 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14329 (< pos (match-beginning 2)))
14331 (setq tags-l (- (match-end 2) (match-beginning 2)))
14332 (goto-char (match-beginning 1))
14334 (delete-region (point) (1+ (match-beginning 2)))
14335 (setq ncol (max (current-column)
14339 (- (abs to-col) tags-l))))
14341 (insert (make-string (- ncol (current-column)) ?\ ))
14342 (setq ncol (current-column))
14343 (when indent-tabs-mode (tabify p (point-at-eol)))
14344 (org-move-to-column (min ncol col) t))
14347 (defun org-set-tags-command (&optional arg just-align)
14348 "Call the set-tags command for the current entry."
14350 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
14351 (org-set-tags arg just-align)
14353 (unless (and (org-region-active-p)
14354 org-loop-over-headlines-in-active-region)
14355 (org-back-to-heading t))
14356 (org-set-tags arg just-align))))
14358 (defun org-set-tags-to (data)
14359 "Set the tags of the current entry to DATA, replacing the current tags.
14360 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
14361 If DATA is nil or the empty string, any tags will be removed."
14362 (interactive "sTags: ")
14366 ((equal data "") "")
14368 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
14371 (concat ":" (mapconcat 'identity data ":") ":"))))
14374 (org-back-to-heading t)
14375 (when (looking-at org-complex-heading-regexp)
14378 (goto-char (match-beginning 5))
14380 (delete-region (point) (point-at-eol))
14381 (org-set-tags nil 'align))
14382 (goto-char (point-at-eol))
14384 (org-set-tags nil 'align)))
14385 (beginning-of-line 1)
14386 (if (looking-at ".*?\\([ \t]+\\)$")
14387 (delete-region (match-beginning 1) (match-end 1))))))
14389 (defun org-align-all-tags ()
14390 "Align the tags i all headings."
14393 (or (ignore-errors (org-back-to-heading t))
14394 (outline-next-heading))
14395 (if (org-at-heading-p)
14397 (message "No headings"))))
14399 (defvar org-indent-indentation-per-level)
14400 (defun org-set-tags (&optional arg just-align)
14401 "Set the tags for the current headline.
14402 With prefix ARG, realign all tags in headings in the current buffer."
14404 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
14405 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
14406 'region-start-level 'region))
14407 org-loop-over-headlines-in-active-region)
14409 ;; We don't use ARG and JUST-ALIGN here these args are not
14410 ;; useful when looping over headlines
14412 org-loop-over-headlines-in-active-region
14413 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
14414 (let* ((re org-outline-regexp-bol)
14415 (current (unless arg (org-get-tags-string)))
14416 (col (current-column))
14417 (org-setting-tags t)
14418 table current-tags inherited-tags ; computed below when needed
14419 tags p0 c0 c1 rpl di tc level)
14422 (goto-char (point-min))
14423 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
14424 (while (re-search-forward re nil t)
14425 (org-set-tags nil t)
14427 (message "All tags realigned to column %d" org-tags-column))
14429 (setq tags current)
14430 ;; Get a new set of tags from the user
14432 (setq table (append org-tag-persistent-alist
14433 (or org-tag-alist (org-get-buffer-tags))
14435 org-complete-tags-always-offer-all-agenda-tags
14436 (org-global-tags-completion-table
14437 (org-agenda-files))))
14438 org-last-tags-completion-table table
14439 current-tags (org-split-string current ":")
14440 inherited-tags (nreverse
14441 (nthcdr (length current-tags)
14442 (nreverse (org-get-tags-at))))
14444 (if (or (eq t org-use-fast-tag-selection)
14445 (and org-use-fast-tag-selection
14446 (delq nil (mapcar 'cdr table))))
14447 (org-fast-tag-selection
14448 current-tags inherited-tags table
14449 (if org-fast-tag-selection-include-todo
14450 org-todo-key-alist))
14451 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
14453 (org-icompleting-read "Tags: "
14454 'org-tags-completion-function
14455 nil nil current 'org-tags-history))))))
14456 (while (string-match "[-+&]+" tags)
14457 ;; No boolean logic, just a list
14458 (setq tags (replace-match ":" t t tags))))
14460 (setq tags (replace-regexp-in-string "[,]" ":" tags))
14462 (if org-tags-sort-function
14463 (setq tags (mapconcat 'identity
14464 (sort (org-split-string
14465 tags (org-re "[^[:alnum:]_@#%]+"))
14466 org-tags-sort-function) ":")))
14468 (if (string-match "\\`[\t ]*\\'" tags)
14470 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
14471 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
14473 ;; Insert new tags at the correct column
14474 (beginning-of-line 1)
14475 (setq level (or (and (looking-at org-outline-regexp)
14476 (- (match-end 0) (point) 1))
14479 ((and (equal current "") (equal tags "")))
14480 ((re-search-forward
14481 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
14483 (if (equal tags "")
14485 (goto-char (match-beginning 0))
14486 (setq c0 (current-column)
14487 ;; compute offset for the case of org-indent-mode active
14488 di (if org-indent-mode
14489 (* (1- org-indent-indentation-per-level) (1- level))
14491 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
14492 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
14493 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
14494 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
14495 (replace-match rpl t t)
14496 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
14498 (t (error "Tags alignment failed")))
14499 (org-move-to-column col)
14501 (run-hooks 'org-after-tags-change-hook))))))
14503 (defun org-change-tag-in-region (beg end tag off)
14504 "Add or remove TAG for each entry in the region.
14505 This works in the agenda, and also in an org-mode buffer."
14507 (list (region-beginning) (region-end)
14508 (let ((org-last-tags-completion-table
14509 (if (derived-mode-p 'org-mode)
14511 (delq nil (append (org-get-buffer-tags)
14512 (org-global-tags-completion-table))))
14513 (org-global-tags-completion-table))))
14514 (org-icompleting-read
14515 "Tag: " 'org-tags-completion-function nil nil nil
14516 'org-tags-history))
14518 (message "[s]et or
[r]emove? ")
14519 (equal (read-char-exclusive) ?r))))
14520 (if (fboundp 'deactivate-mark) (deactivate-mark))
14521 (let ((agendap (equal major-mode 'org-agenda-mode))
14522 l1 l2 m buf pos newhead (cnt 0))
14524 (setq l2 (1- (org-current-line)))
14526 (setq l1 (org-current-line))
14527 (loop for l from l1 to l2 do
14529 (setq m (get-text-property (point) 'org-hd-marker))
14530 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
14532 (setq buf (if agendap (marker-buffer m) (current-buffer))
14533 pos (if agendap m (point)))
14534 (with-current-buffer buf
14538 (setq cnt (1+ cnt))
14539 (org-toggle-tag tag (if off 'off 'on))
14540 (setq newhead (org-get-heading)))))
14541 (and agendap (org-agenda-change-all-lines newhead m))))
14542 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
14544 (defun org-tags-completion-function (string predicate &optional flag)
14545 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
14546 (confirm (lambda (x) (stringp (car x)))))
14547 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
14548 (setq s1 (match-string 1 string)
14549 s2 (match-string 2 string))
14550 (setq s1 "" s2 string))
14554 (setq rtn (try-completion s2 ctable confirm))
14557 (concat s1 s2 (substring rtn (length s2))
14558 (if (and org-add-colon-after-tag-completion
14559 (assoc rtn ctable))
14564 (all-completions s2 ctable confirm))
14567 (assoc s2 ctable)))))
14569 (defun org-fast-tag-insert (kwd tags face &optional end)
14570 "Insert KDW, and the TAGS, the latter with face FACE.
14572 (insert (format "%-12s" (concat kwd ":"))
14573 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
14576 (defun org-fast-tag-show-exit (flag)
14579 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
14580 (replace-match ""))
14583 (org-move-to-column (- (window-width) 19) t)
14584 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
14586 (defun org-set-current-tags-overlay (current prefix)
14587 "Add an overlay to CURRENT tag with PREFIX."
14588 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
14589 (if (featurep 'xemacs)
14590 (org-overlay-display org-tags-overlay (concat prefix s)
14591 'secondary-selection)
14592 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
14593 (org-overlay-display org-tags-overlay (concat prefix s)))))
14595 (defvar org-last-tag-selection-key nil)
14596 (defun org-fast-tag-selection (current inherited table &optional todo-table)
14597 "Fast tag selection with single keys.
14598 CURRENT is the current list of tags in the headline, INHERITED is the
14599 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
14600 possibly with grouping information. TODO-TABLE is a similar table with
14601 TODO keywords, should these have keys assigned to them.
14602 If the keys are nil, a-z are automatically assigned.
14603 Returns the new tags string, or nil to not change the current settings."
14604 (let* ((fulltable (append table todo-table))
14605 (maxlen (apply 'max (mapcar
14607 (if (stringp (car x)) (string-width (car x)) 0))
14609 (buf (current-buffer))
14610 (expert (eq org-fast-tag-selection-single-key 'expert))
14612 (fwidth (+ maxlen 3 1 3))
14613 (ncol (/ (- (window-width) 4) fwidth))
14616 tg cnt e c char c1 c2 ntable tbl rtn
14617 ov-start ov-end ov-prefix
14618 (exit-after-next org-fast-tag-selection-single-key)
14619 (done-keywords org-done-keywords)
14622 (beginning-of-line 1)
14624 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14625 (setq ov-start (match-beginning 1)
14626 ov-end (match-end 1)
14628 (setq ov-start (1- (point-at-eol))
14629 ov-end (1+ ov-start))
14630 (skip-chars-forward "^\n\r")
14633 (buffer-substring (1- (point)) (point))
14634 (if (> (current-column) org-tags-column)
14636 (make-string (- org-tags-column (current-column)) ?\ ))))))
14637 (move-overlay org-tags-overlay ov-start ov-end)
14638 (save-window-excursion
14640 (set-buffer (get-buffer-create " *Org tags*"))
14641 (delete-other-windows)
14642 (split-window-vertically)
14643 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
14645 (org-set-local 'org-done-keywords done-keywords)
14646 (org-fast-tag-insert "Inherited" inherited i-face "\n")
14647 (org-fast-tag-insert "Current" current c-face "\n\n")
14648 (org-fast-tag-show-exit exit-after-next)
14649 (org-set-current-tags-overlay current ov-prefix)
14650 (setq tbl fulltable char ?a cnt 0)
14651 (while (setq e (pop tbl))
14653 ((equal (car e) :startgroup)
14654 (push '() groups) (setq ingroup t)
14655 (when (not (= cnt 0))
14658 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
14659 ((equal (car e) :endgroup)
14660 (setq ingroup nil cnt 0)
14661 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
14662 ((equal e '(:newline))
14663 (when (not (= cnt 0))
14667 (while (equal (car tbl) '(:newline))
14669 (setq tbl (cdr tbl)))))
14670 ((equal e '(:grouptags)) nil)
14672 (setq tg (copy-sequence (car e)) c2 nil)
14675 ;; automatically assign a character.
14676 (setq c1 (string-to-char
14677 (downcase (substring
14678 tg (if (= (string-to-char tg) ?@) 1 0)))))
14679 (if (or (rassoc c1 ntable) (rassoc c1 table))
14680 (while (or (rassoc char ntable) (rassoc char table))
14681 (setq char (1+ char)))
14683 (setq c (or c2 char)))
14684 (if ingroup (push tg (car groups)))
14685 (setq tg (org-add-props tg nil 'face
14687 ((not (assoc tg table))
14688 (org-get-todo-face tg))
14689 ((member tg current) c-face)
14690 ((member tg inherited) i-face))))
14691 (if (equal (caar tbl) :grouptags)
14692 (org-add-props tg nil 'face 'org-tag-group))
14693 (if (and (= cnt 0) (not ingroup)) (insert " "))
14694 (insert "[" c "] " tg (make-string
14695 (- fwidth 4 (length tg)) ?\ ))
14696 (push (cons tg c) ntable)
14697 (when (= (setq cnt (1+ cnt)) ncol)
14699 (if ingroup (insert " "))
14701 (setq ntable (nreverse ntable))
14703 (goto-char (point-min))
14704 (if (not expert) (org-fit-window-to-buffer))
14708 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
14709 (if (not groups) "no " "")
14710 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
14711 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
14712 (setq org-last-tag-selection-key c)
14714 ((= c ?\r) (throw 'exit t))
14716 (setq groups (not groups))
14717 (goto-char (point-min))
14718 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
14721 (org-fast-tag-show-exit
14722 (setq exit-after-next (not exit-after-next)))
14724 (delete-other-windows)
14725 (set-window-buffer (split-window-vertically) " *Org tags*")
14726 (org-switch-to-buffer-other-window " *Org tags*")
14727 (org-fit-window-to-buffer)))
14729 (and (= c ?q) (not (rassoc c ntable))))
14730 (org-detach-overlay org-tags-overlay)
14731 (setq quit-flag t))
14734 (if exit-after-next (setq exit-after-next 'now)))
14736 (condition-case nil
14737 (setq tg (org-icompleting-read
14740 (with-current-buffer buf
14741 (org-get-buffer-tags)))))
14742 (quit (setq tg "")))
14743 (when (string-match "\\S-" tg)
14744 (add-to-list 'buffer-tags (list tg))
14745 (if (member tg current)
14746 (setq current (delete tg current))
14747 (push tg current)))
14748 (if exit-after-next (setq exit-after-next 'now)))
14749 ((setq e (rassoc c todo-table) tg (car e))
14750 (with-current-buffer buf
14751 (save-excursion (org-todo tg)))
14752 (if exit-after-next (setq exit-after-next 'now)))
14753 ((setq e (rassoc c ntable) tg (car e))
14754 (if (member tg current)
14755 (setq current (delete tg current))
14756 (loop for g in groups do
14759 (setq current (delete x current)))
14762 (if exit-after-next (setq exit-after-next 'now))))
14764 ;; Create a sorted list
14768 (assoc b (cdr (memq (assoc a ntable) ntable))))))
14769 (if (eq exit-after-next 'now) (throw 'exit t))
14770 (goto-char (point-min))
14771 (beginning-of-line 2)
14772 (delete-region (point) (point-at-eol))
14773 (org-fast-tag-insert "Current" current c-face)
14774 (org-set-current-tags-overlay current ov-prefix)
14775 (while (re-search-forward
14776 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
14777 (setq tg (match-string 1))
14778 (add-text-properties
14779 (match-beginning 1) (match-end 1)
14782 ((member tg current) c-face)
14783 ((member tg inherited) i-face)
14784 (t (get-text-property (match-beginning 1) 'face))))))
14785 (goto-char (point-min)))))
14786 (org-detach-overlay org-tags-overlay)
14788 (mapconcat 'identity current ":")
14791 (defun org-get-tags-string ()
14792 "Get the TAGS string in the current headline."
14793 (unless (org-at-heading-p t)
14794 (user-error "Not on a heading"))
14796 (beginning-of-line 1)
14797 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14798 (org-match-string-no-properties 1)
14801 (defun org-get-tags ()
14802 "Get the list of tags specified in the current headline."
14803 (org-split-string (org-get-tags-string) ":"))
14805 (defun org-get-buffer-tags ()
14806 "Get a table of all tags used in the buffer, for completion."
14809 (goto-char (point-min))
14810 (while (re-search-forward
14811 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
14812 (when (equal (char-after (point-at-bol 0)) ?*)
14813 (mapc (lambda (x) (add-to-list 'tags x))
14814 (org-split-string (org-match-string-no-properties 1) ":")))))
14815 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
14816 (mapcar 'list tags)))
14818 ;;;; The mapping API
14820 (defun org-map-entries (func &optional match scope &rest skip)
14821 "Call FUNC at each headline selected by MATCH in SCOPE.
14823 FUNC is a function or a lisp form. The function will be called without
14824 arguments, with the cursor positioned at the beginning of the headline.
14825 The return values of all calls to the function will be collected and
14826 returned as a list.
14828 The call to FUNC will be wrapped into a save-excursion form, so FUNC
14829 does not need to preserve point. After evaluation, the cursor will be
14830 moved to the end of the line (presumably of the headline of the
14831 processed entry) and search continues from there. Under some
14832 circumstances, this may not produce the wanted results. For example,
14833 if you have removed (e.g. archived) the current (sub)tree it could
14834 mean that the next entry will be skipped entirely. In such cases, you
14835 can specify the position from where search should continue by making
14836 FUNC set the variable `org-map-continue-from' to the desired buffer
14839 MATCH is a tags/property/todo match as it is used in the agenda tags view.
14840 Only headlines that are matched by this query will be considered during
14841 the iteration. When MATCH is nil or t, all headlines will be
14842 visited by the iteration.
14844 SCOPE determines the scope of this command. It can be any of:
14846 nil The current buffer, respecting the restriction if any
14847 tree The subtree started with the entry at point
14848 region The entries within the active region, if any
14850 The entries within the active region, but only those at
14851 the same level than the first one.
14852 file The current buffer, without restriction
14854 The current buffer, and any archives associated with it
14855 agenda All agenda files
14856 agenda-with-archives
14857 All agenda files with any archive files associated with them
14859 If this is a list, all files in the list will be scanned
14861 The remaining args are treated as settings for the skipping facilities of
14862 the scanner. The following items can be given here:
14864 archive skip trees with the archive tag
14865 comment skip trees with the COMMENT keyword
14866 function or Emacs Lisp form:
14867 will be used as value for `org-agenda-skip-function', so
14868 whenever the function returns a position, FUNC will not be
14869 called for that entry and search will continue from the
14872 If your function needs to retrieve the tags including inherited tags
14873 at the *current* entry, you can use the value of the variable
14874 `org-scanner-tags' which will be much faster than getting the value
14875 with `org-get-tags-at'. If your function gets properties with
14876 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
14877 to t around the call to `org-entry-properties' to get the same speedup.
14878 Note that if your function moves around to retrieve tags and properties at
14879 a *different* entry, you cannot use these techniques."
14880 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
14881 (not (org-region-active-p)))
14882 (let* ((org-agenda-archives-mode nil) ; just to make sure
14883 (org-agenda-skip-archived-trees (memq 'archive skip))
14884 (org-agenda-skip-comment-trees (memq 'comment skip))
14885 (org-agenda-skip-function
14886 (car (org-delete-all '(comment archive) skip)))
14887 (org-tags-match-list-sublevels t)
14888 (start-level (eq scope 'region-start-level))
14890 org-todo-keywords-for-agenda
14891 org-done-keywords-for-agenda
14892 org-todo-keyword-alist-for-agenda
14893 org-drawers-for-agenda
14894 org-tag-alist-for-agenda
14898 ((eq match t) (setq matcher t))
14899 ((eq match nil) (setq matcher t))
14900 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
14902 (save-window-excursion
14904 (cond ((eq scope 'tree)
14905 (org-back-to-heading t)
14906 (org-narrow-to-subtree)
14908 ((and (or (eq scope 'region) (eq scope 'region-start-level))
14909 (org-region-active-p))
14910 ;; If needed, set start-level to a string like "2"
14913 (goto-char (region-beginning))
14914 (unless (org-at-heading-p) (outline-next-heading))
14915 (setq start-level (org-current-level))))
14916 (narrow-to-region (region-beginning)
14918 (goto-char (region-end))
14919 (unless (and (bolp) (org-at-heading-p))
14920 (outline-next-heading))
14926 (org-agenda-prepare-buffers
14927 (list (buffer-file-name (current-buffer))))
14928 (setq res (org-scan-tags func matcher todo-only start-level)))
14929 ;; Get the right scope
14931 ((and scope (listp scope) (symbolp (car scope)))
14932 (setq scope (eval scope)))
14933 ((eq scope 'agenda)
14934 (setq scope (org-agenda-files t)))
14935 ((eq scope 'agenda-with-archives)
14936 (setq scope (org-agenda-files t))
14937 (setq scope (org-add-archive-files scope)))
14939 (setq scope (list (buffer-file-name))))
14940 ((eq scope 'file-with-archives)
14941 (setq scope (org-add-archive-files (list (buffer-file-name))))))
14942 (org-agenda-prepare-buffers scope)
14943 (while (setq file (pop scope))
14944 (with-current-buffer (org-find-base-buffer-visiting file)
14948 (goto-char (point-min))
14949 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
14954 ;;; Setting and retrieving properties
14956 (defconst org-special-properties
14957 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
14958 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
14959 "The special properties valid in Org-mode.
14961 These are properties that are not defined in the property drawer,
14962 but in some other way.")
14964 (defconst org-default-properties
14965 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
14966 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
14967 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
14968 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
14969 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
14970 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
14971 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
14972 "Some properties that are used by Org-mode for various purposes.
14973 Being in this list makes sure that they are offered for completion.")
14975 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
14976 "Regular expression matching the first line of a property drawer.")
14978 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
14979 "Regular expression matching the last line of a property drawer.")
14981 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
14982 "Regular expression matching the first line of a property drawer.")
14984 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
14985 "Regular expression matching the first line of a property drawer.")
14987 (defconst org-property-drawer-re
14988 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
14989 org-property-end-re "\\)\n?")
14990 "Matches an entire property drawer.")
14992 (defconst org-clock-drawer-re
14993 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
14994 org-property-end-re "\\)\n?")
14995 "Matches an entire clock drawer.")
14997 (defsubst org-re-property (property)
14998 "Return a regexp matching a PROPERTY line.
14999 Match group 1 will be set to the value."
15000 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
15002 (defsubst org-re-property-keyword (property)
15003 "Return a regexp matching a PROPERTY line, possibly with no
15004 value for the property."
15005 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
15007 (defun org-property-action ()
15008 "Do an action on properties."
15011 (org-at-property-p)
15012 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
15013 (setq c (read-char-exclusive))
15016 (call-interactively 'org-set-property))
15018 (call-interactively 'org-delete-property))
15020 (call-interactively 'org-delete-property-globally))
15022 (call-interactively 'org-compute-property-at-point))
15023 (t (user-error "No such property action %c" c)))))
15025 (defun org-inc-effort ()
15026 "Increment the value of the effort property in the current entry."
15028 (org-set-effort nil t))
15030 (defvar org-clock-effort) ;; Defined in org-clock.el
15031 (defvar org-clock-current-task) ;; Defined in org-clock.el
15032 (defun org-set-effort (&optional value increment)
15033 "Set the effort property of the current entry.
15034 With numerical prefix arg, use the nth allowed value, 0 stands for the
15035 10th allowed value.
15037 When INCREMENT is non-nil, set the property to the next allowed value."
15039 (if (equal value 0) (setq value 10))
15040 (let* ((completion-ignore-case t)
15041 (prop org-effort-property)
15042 (cur (org-entry-get nil prop))
15043 (allowed (org-property-get-allowed-values nil prop 'table))
15044 (existing (mapcar 'list (org-property-values prop)))
15045 (heading (nth 4 (org-heading-components)))
15048 ((stringp value) value)
15049 ((and allowed (integerp value))
15050 (or (car (nth (1- value) allowed))
15051 (car (org-last allowed))))
15052 ((and allowed increment)
15053 (or (caadr (member (list cur) allowed))
15054 (user-error "Allowed effort values are not set")))
15056 (message "Select 1-9,0, [RET%s]: %s"
15057 (if cur (concat "=" cur) "")
15058 (mapconcat 'car allowed " "))
15059 (setq rpl (read-char-exclusive))
15060 (if (equal rpl ?\r)
15062 (setq rpl (- rpl ?0))
15063 (if (equal rpl 0) (setq rpl 10))
15064 (if (and (> rpl 0) (<= rpl (length allowed)))
15065 (car (nth (1- rpl) allowed))
15066 (org-completing-read "Effort: " allowed nil))))
15068 (let (org-completion-use-ido org-completion-use-iswitchb)
15069 (org-completing-read
15070 (concat "Effort " (if (and cur (string-match "\\S-" cur))
15071 (concat "[" cur "]") "")
15073 existing nil nil "" nil cur))))))
15074 (unless (equal (org-entry-get nil prop) val)
15075 (org-entry-put nil prop val))
15077 (org-back-to-heading t)
15078 (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
15079 (when (string= heading org-clock-current-task)
15080 (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
15081 (org-clock-update-mode-line))
15082 (message "%s is now %s" prop val)))
15084 (defun org-at-property-p ()
15085 "Is cursor inside a property drawer?"
15087 (beginning-of-line 1)
15088 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
15089 (save-match-data ;; Used by calling procedures
15091 (range (unless (org-before-first-heading-p)
15092 (org-get-property-block))))
15093 (and range (<= (car range) p) (< p (cdr range))))))))
15095 (defun org-get-property-block (&optional beg end force)
15096 "Return the (beg . end) range of the body of the property drawer.
15097 BEG and END are the beginning and end of the current subtree, or of
15098 the part before the first headline. If they are not given, they will
15099 be found. If the drawer does not exist and FORCE is non-nil, create
15103 (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
15104 (progn (org-back-to-heading t) (point))))
15105 (end (or end (and (not (outline-next-heading)) (point-max))
15108 (if (re-search-forward org-property-start-re end t)
15109 (setq beg (1+ (match-end 0)))
15112 (org-insert-property-drawer)
15113 (setq end (progn (outline-next-heading) (point))))
15116 (if (re-search-forward org-property-start-re end t)
15117 (setq beg (1+ (match-end 0)))))
15118 (if (re-search-forward org-property-end-re end t)
15119 (setq end (match-beginning 0))
15120 (or force (throw 'exit nil))
15124 (insert ":END:\n"))
15127 (defun org-entry-properties (&optional pom which specific)
15128 "Get all properties of the entry at point-or-marker POM.
15129 This includes the TODO keyword, the tags, time strings for deadline,
15130 scheduled, and clocking, and any additional properties defined in the
15131 entry. The return value is an alist, keys may occur multiple times
15132 if the property key was used several times.
15133 POM may also be nil, in which case the current entry is used.
15134 If WHICH is nil or `all', get all properties. If WHICH is
15135 `special' or `standard', only get that subclass. If WHICH
15136 is a string only get exactly this property. SPECIFIC can be a string, the
15137 specific property we are interested in. Specifying it can speed
15138 things up because then unnecessary parsing is avoided."
15139 (setq which (or which 'all))
15140 (org-with-point-at pom
15141 (let ((clockstr (substring org-clock-string 0 -1))
15142 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
15143 (case-fold-search nil)
15144 beg end range props sum-props key key1 value string clocksum clocksumt)
15146 (when (condition-case nil
15147 (and (derived-mode-p 'org-mode) (org-back-to-heading t))
15150 (setq sum-props (get-text-property (point) 'org-summaries))
15151 (setq clocksum (get-text-property (point) :org-clock-minutes)
15152 clocksumt (get-text-property (point) :org-clock-minutes-today))
15153 (outline-next-heading)
15155 (when (memq which '(all special))
15156 ;; Get the special properties, like TODO and tags
15158 (when (and (or (not specific) (string= specific "TODO"))
15159 (looking-at org-todo-line-regexp) (match-end 2))
15160 (push (cons "TODO" (org-match-string-no-properties 2)) props))
15161 (when (and (or (not specific) (string= specific "PRIORITY"))
15162 (looking-at org-priority-regexp))
15163 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
15164 (when (or (not specific) (string= specific "FILE"))
15165 (push (cons "FILE" buffer-file-name) props))
15166 (when (and (or (not specific) (string= specific "TAGS"))
15167 (setq value (org-get-tags-string))
15168 (string-match "\\S-" value))
15169 (push (cons "TAGS" value) props))
15170 (when (and (or (not specific) (string= specific "ALLTAGS"))
15171 (setq value (org-get-tags-at)))
15172 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
15175 (when (or (not specific) (string= specific "BLOCKED"))
15176 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
15177 (when (or (not specific)
15179 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
15180 "TIMESTAMP" "TIMESTAMP_IA")))
15182 (while (re-search-forward org-maybe-keyword-time-regexp end t)
15183 (setq key (if (match-end 1)
15184 (substring (org-match-string-no-properties 1)
15186 string (if (equal key clockstr)
15188 (buffer-substring-no-properties
15189 (match-beginning 3) (goto-char
15191 (substring (org-match-string-no-properties 3)
15193 ;; Get the correct property name from the key. This is
15194 ;; necessary if the user has configured time keywords.
15195 (setq key1 (concat key ":"))
15199 (if (= (char-after (match-beginning 3)) ?\[)
15200 "TIMESTAMP_IA" "TIMESTAMP")))
15201 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
15202 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
15203 ((equal key1 org-closed-string) (setq key "CLOSED"))
15204 ((equal key1 org-clock-string) (setq key "CLOCK")))
15205 (if (and specific (equal key specific) (not (equal key "CLOCK")))
15207 (push (cons key string) props)
15208 ;; no need to search further if match is found
15210 (when (or (equal key "CLOCK") (not (assoc key props)))
15211 (push (cons key string) props)))))))
15213 (when (memq which '(all standard))
15214 ;; Get the standard properties, like :PROP: ...
15215 (setq range (org-get-property-block beg end))
15217 (goto-char (car range))
15218 (while (re-search-forward
15219 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
15221 (setq key (org-match-string-no-properties 1)
15222 value (org-trim (or (org-match-string-no-properties 2) "")))
15223 (unless (member key excluded)
15224 (push (cons key (or value "")) props)))))
15226 (push (cons "CLOCKSUM"
15227 (org-columns-number-to-string (/ (float clocksum) 60.)
15231 (push (cons "CLOCKSUM_T"
15232 (org-columns-number-to-string (/ (float clocksumt) 60.)
15235 (unless (assoc "CATEGORY" props)
15236 (push (cons "CATEGORY" (org-get-category)) props))
15237 (append sum-props (nreverse props)))))))
15239 (defun org-entry-get (pom property &optional inherit literal-nil)
15240 "Get value of PROPERTY for entry or content at point-or-marker POM.
15241 If INHERIT is non-nil and the entry does not have the property,
15242 then also check higher levels of the hierarchy.
15243 If INHERIT is the symbol `selective', use inheritance only if the setting
15244 in `org-use-property-inheritance' selects PROPERTY for inheritance.
15245 If the property is present but empty, the return value is the empty string.
15246 If the property is not present at all, nil is returned.
15248 If LITERAL-NIL is set, return the string value \"nil\" as a string,
15249 do not interpret it as the list atom nil. This is used for inheritance
15250 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
15251 (org-with-point-at pom
15252 (if (and inherit (if (eq inherit 'selective)
15253 (org-property-inherit-p property)
15255 (org-entry-get-with-inheritance property literal-nil)
15256 (if (member property org-special-properties)
15257 ;; We need a special property. Use `org-entry-properties' to
15258 ;; retrieve it, but specify the wanted property
15259 (cdr (assoc property (org-entry-properties nil 'special property)))
15260 (let ((range (org-get-property-block)))
15261 (when (and range (not (eq (car range) (cdr range))))
15262 (let* ((props (list (or (assoc property org-file-properties)
15263 (assoc property org-global-properties)
15264 (assoc property org-global-properties-fixed))))
15266 (when (re-search-forward
15267 (org-re-property key) (cdr range) t)
15269 (org-update-property-plist
15272 (org-match-string-no-properties 1) "")
15275 (goto-char (car range))
15276 (funcall ap property)
15277 (goto-char (car range))
15278 (while (funcall ap (concat property "+")))
15279 (setq val (cdr (assoc property props)))
15280 (when val (if literal-nil val (org-not-nil val))))))))))
15282 (defun org-property-or-variable-value (var &optional inherit)
15283 "Check if there is a property fixing the value of VAR.
15284 If yes, return this value. If not, return the current value of the variable."
15285 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
15286 (if (and prop (stringp prop) (string-match "\\S-" prop))
15288 (symbol-value var))))
15290 (defun org-entry-delete (pom property &optional delete-empty-drawer)
15291 "Delete the property PROPERTY from entry at point-or-marker POM.
15292 When optional argument DELETE-EMPTY-DRAWER is a string, it defines
15293 an empty drawer to delete."
15294 (org-with-point-at pom
15295 (if (member property org-special-properties)
15296 nil ; cannot delete these properties.
15297 (let ((range (org-get-property-block)))
15299 (goto-char (car range))
15301 (org-re-property property)
15304 (delete-region (match-beginning 0) (1+ (point-at-eol)))
15305 (and delete-empty-drawer
15306 (org-remove-empty-drawer-at
15307 delete-empty-drawer (car range)))
15311 ;; Multi-values properties are properties that contain multiple values
15312 ;; These values are assumed to be single words, separated by whitespace.
15313 (defun org-entry-add-to-multivalued-property (pom property value)
15314 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
15315 (let* ((old (org-entry-get pom property))
15316 (values (and old (org-split-string old "[ \t]"))))
15317 (setq value (org-entry-protect-space value))
15318 (unless (member value values)
15319 (setq values (append values (list value)))
15320 (org-entry-put pom property
15321 (mapconcat 'identity values " ")))))
15323 (defun org-entry-remove-from-multivalued-property (pom property value)
15324 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
15325 (let* ((old (org-entry-get pom property))
15326 (values (and old (org-split-string old "[ \t]"))))
15327 (setq value (org-entry-protect-space value))
15328 (when (member value values)
15329 (setq values (delete value values))
15330 (org-entry-put pom property
15331 (mapconcat 'identity values " ")))))
15333 (defun org-entry-member-in-multivalued-property (pom property value)
15334 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
15335 (let* ((old (org-entry-get pom property))
15336 (values (and old (org-split-string old "[ \t]"))))
15337 (setq value (org-entry-protect-space value))
15338 (member value values)))
15340 (defun org-entry-get-multivalued-property (pom property)
15341 "Return a list of values in a multivalued property."
15342 (let* ((value (org-entry-get pom property))
15343 (values (and value (org-split-string value "[ \t]"))))
15344 (mapcar 'org-entry-restore-space values)))
15346 (defun org-entry-put-multivalued-property (pom property &rest values)
15347 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
15348 VALUES should be a list of strings. Spaces will be protected."
15349 (org-entry-put pom property
15350 (mapconcat 'org-entry-protect-space values " "))
15351 (let* ((value (org-entry-get pom property))
15352 (values (and value (org-split-string value "[ \t]"))))
15353 (mapcar 'org-entry-restore-space values)))
15355 (defun org-entry-protect-space (s)
15356 "Protect spaces and newline in string S."
15357 (while (string-match " " s)
15358 (setq s (replace-match "%20" t t s)))
15359 (while (string-match "\n" s)
15360 (setq s (replace-match "%0A" t t s)))
15363 (defun org-entry-restore-space (s)
15364 "Restore spaces and newline in string S."
15365 (while (string-match "%20" s)
15366 (setq s (replace-match " " t t s)))
15367 (while (string-match "%0A" s)
15368 (setq s (replace-match "\n" t t s)))
15371 (defvar org-entry-property-inherited-from (make-marker)
15372 "Marker pointing to the entry from where a property was inherited.
15373 Each call to `org-entry-get-with-inheritance' will set this marker to the
15374 location of the entry where the inheritance search matched. If there was
15375 no match, the marker will point nowhere.
15376 Note that also `org-entry-get' calls this function, if the INHERIT flag
15379 (defun org-entry-get-with-inheritance (property &optional literal-nil)
15380 "Get PROPERTY of entry or content at point, search higher levels if needed.
15381 The search will stop at the first ancestor which has the property defined.
15382 If the value found is \"nil\", return nil to show that the property
15383 should be considered as undefined (this is the meaning of nil here).
15384 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
15385 (move-marker org-entry-property-inherited-from nil)
15392 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
15393 (or (ignore-errors (org-back-to-heading t))
15394 (goto-char (point-min)))
15395 (move-marker org-entry-property-inherited-from (point))
15397 (or (ignore-errors (org-up-heading-safe))
15398 (throw 'ex nil))))))
15400 (cdr (assoc property org-file-properties))
15401 (cdr (assoc property org-global-properties))
15402 (cdr (assoc property org-global-properties-fixed))))
15403 (if literal-nil tmp (org-not-nil tmp))))
15405 (defvar org-property-changed-functions nil
15406 "Hook called when the value of a property has changed.
15407 Each hook function should accept two arguments, the name of the property
15408 and the new value.")
15410 (defun org-entry-put (pom property value)
15411 "Set PROPERTY to VALUE for entry at point-or-marker POM."
15412 (org-with-point-at pom
15413 (org-back-to-heading t)
15414 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
15417 ((equal property "TODO")
15418 (when (and (stringp value) (string-match "\\S-" value)
15419 (not (member value org-todo-keywords-1)))
15420 (user-error "\"%s\" is not a valid TODO state" value))
15421 (if (or (not value)
15422 (not (string-match "\\S-" value)))
15423 (setq value 'none))
15425 (org-set-tags nil 'align))
15426 ((equal property "PRIORITY")
15427 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
15428 (string-to-char value) ?\ ))
15429 (org-set-tags nil 'align))
15430 ((equal property "CLOCKSUM")
15431 (if (not (re-search-forward
15432 (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
15433 (error "Cannot find a clock log")
15434 (goto-char (- (match-end 1) 2))
15436 ((eq value 'earlier) (org-timestamp-down))
15437 ((eq value 'later) (org-timestamp-up)))
15438 (org-clock-sum-current-item)))
15439 ((equal property "SCHEDULED")
15440 (if (re-search-forward org-scheduled-time-regexp end t)
15442 ((eq value 'earlier) (org-timestamp-change -1 'day))
15443 ((eq value 'later) (org-timestamp-change 1 'day))
15444 (t (call-interactively 'org-schedule)))
15445 (call-interactively 'org-schedule)))
15446 ((equal property "DEADLINE")
15447 (if (re-search-forward org-deadline-time-regexp end t)
15449 ((eq value 'earlier) (org-timestamp-change -1 'day))
15450 ((eq value 'later) (org-timestamp-change 1 'day))
15451 (t (call-interactively 'org-deadline)))
15452 (call-interactively 'org-deadline)))
15453 ((member property org-special-properties)
15454 (error "The %s property can not yet be set with `org-entry-put'"
15456 (t ; a non-special property
15457 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
15458 (setq range (org-get-property-block beg end 'force))
15459 (goto-char (car range))
15460 (if (re-search-forward
15461 (org-re-property-keyword property) (cdr range) t)
15463 (delete-region (match-beginning 0) (match-end 0))
15464 (goto-char (match-beginning 0)))
15465 (goto-char (cdr range))
15469 (insert ":" property ":")
15470 (and value (insert " " value))
15471 (org-indent-line)))))
15472 (run-hook-with-args 'org-property-changed-functions property value)))
15474 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
15475 "Get all property keys in the current buffer.
15476 With INCLUDE-SPECIALS, also list the special properties that reflect things
15477 like tags and TODO state.
15478 With INCLUDE-DEFAULTS, also include properties that has special meaning
15479 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
15481 With INCLUDE-COLUMNS, also include property names given in COLUMN
15482 formats in the current buffer."
15483 (let (rtn range cfmt s p)
15487 (goto-char (point-min))
15488 (while (re-search-forward org-property-start-re nil t)
15489 (setq range (org-get-property-block))
15490 (goto-char (car range))
15491 (while (re-search-forward
15492 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
15494 (add-to-list 'rtn (org-match-string-no-properties 1)))
15495 (outline-next-heading))))
15497 (when include-specials
15498 (setq rtn (append org-special-properties rtn)))
15500 (when include-defaults
15501 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
15502 (add-to-list 'rtn org-effort-property))
15504 (when include-columns
15508 (goto-char (point-min))
15509 (while (re-search-forward
15510 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
15512 (setq cfmt (match-string 2) s 0)
15513 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
15515 (setq s (match-end 0)
15516 p (match-string 1 cfmt))
15517 (unless (or (equal p "ITEM")
15518 (member p org-special-properties))
15519 (add-to-list 'rtn (match-string 1 cfmt))))))))
15521 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
15523 (defun org-property-values (key)
15524 "Return a list of all values of property KEY in the current buffer."
15528 (goto-char (point-min))
15529 (let ((re (org-re-property key))
15531 (while (re-search-forward re nil t)
15532 (add-to-list 'values (org-trim (match-string 1))))
15533 (delete "" values)))))
15535 (defun org-insert-property-drawer ()
15536 "Insert a property drawer into the current entry."
15537 (org-back-to-heading t)
15538 (looking-at org-outline-regexp)
15539 (let ((indent (if org-adapt-indentation
15540 (- (match-end 0) (match-beginning 0))
15543 (re (concat "^[ \t]*" org-keyword-time-regexp))
15545 (outline-next-heading)
15548 (while (re-search-forward re end t))
15549 (setq hiddenp (outline-invisible-p))
15551 (and (equal (char-after) ?\n) (forward-char 1))
15552 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
15553 (if (member (match-string 1) '("CLOCK:" ":END:"))
15554 ;; just skip this line
15555 (beginning-of-line 2)
15556 ;; Drawer start, find the end
15557 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
15558 (beginning-of-line 1)))
15559 (org-skip-over-state-notes)
15560 (skip-chars-backward " \t\n\r")
15561 (if (eq (char-before) ?*) (forward-char 1))
15562 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
15563 (beginning-of-line 0)
15564 (org-indent-to-column indent)
15565 (beginning-of-line 2)
15566 (org-indent-to-column indent)
15567 (beginning-of-line 0)
15570 (org-back-to-heading t)
15572 (org-flag-drawer t))))
15574 (defun org-insert-drawer (&optional arg drawer)
15575 "Insert a drawer at point.
15577 Optional argument DRAWER, when non-nil, is a string representing
15578 drawer's name. Otherwise, the user is prompted for a name.
15580 If a region is active, insert the drawer around that region
15583 Point is left between drawer's boundaries."
15585 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
15587 ;; SYSTEM-DRAWERS is a list of drawer names that are used
15588 ;; internally by Org. They are meant to be inserted
15590 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
15591 ;; Remove system drawers from list. Note: For some reason,
15592 ;; `org-completing-read' ignores the predicate while
15593 ;; `completing-read' handles it fine.
15594 (drawer (if arg "PROPERTIES"
15597 "Drawer: " org-drawers
15598 (lambda (d) (not (member d system-drawers))))))))
15600 ;; With C-u, fall back on `org-insert-property-drawer'
15601 (arg (org-insert-property-drawer))
15602 ;; With an active region, insert a drawer at point.
15603 ((not (org-region-active-p))
15605 (unless (bolp) (insert "\n"))
15606 (insert (format ":%s:\n\n:END:\n" drawer))
15607 (forward-line -2)))
15608 ;; Otherwise, insert the drawer at point
15610 (let ((rbeg (region-beginning))
15611 (rend (copy-marker (region-end))))
15615 (beginning-of-line)
15616 (when (save-excursion
15617 (re-search-forward org-outline-regexp-bol rend t))
15618 (user-error "Drawers cannot contain headlines"))
15619 ;; Position point at the beginning of the first
15620 ;; non-blank line in region. Insert drawer's opening
15621 ;; there, then indent it.
15622 (org-skip-whitespace)
15623 (beginning-of-line)
15624 (insert ":" drawer ":\n")
15626 (indent-for-tab-command)
15627 ;; Move point to the beginning of the first blank line
15628 ;; after the last non-blank line in region. Insert
15629 ;; drawer's closing, then indent it.
15631 (skip-chars-backward " \r\t\n")
15633 (deactivate-mark t)
15634 (indent-for-tab-command)
15635 (unless (eolp) (insert "\n")))
15636 ;; Clear marker, whatever the outcome of insertion is.
15637 (set-marker rend nil)))))))
15639 (defvar org-property-set-functions-alist nil
15640 "Property set function alist.
15641 Each entry should have the following format:
15643 (PROPERTY . READ-FUNCTION)
15645 The read function will be called with the same argument as
15646 `org-completing-read'.")
15648 (defun org-set-property-function (property)
15649 "Get the function that should be used to set PROPERTY.
15650 This is computed according to `org-property-set-functions-alist'."
15651 (or (cdr (assoc property org-property-set-functions-alist))
15652 'org-completing-read))
15654 (defun org-read-property-value (property)
15655 "Read PROPERTY value from user."
15656 (let* ((completion-ignore-case t)
15657 (allowed (org-property-get-allowed-values nil property 'table))
15658 (cur (org-entry-get nil property))
15659 (prompt (concat property " value"
15660 (if (and cur (string-match "\\S-" cur))
15661 (concat " [" cur "]") "") ": "))
15662 (set-function (org-set-property-function property))
15664 (funcall set-function prompt allowed nil
15665 (not (get-text-property 0 'org-unrestricted
15667 (let (org-completion-use-ido org-completion-use-iswitchb)
15668 (funcall set-function prompt
15669 (mapcar 'list (org-property-values property))
15670 nil nil "" nil cur)))))
15675 (defvar org-last-set-property nil)
15676 (defvar org-last-set-property-value nil)
15677 (defun org-read-property-name ()
15678 "Read a property name."
15679 (let* ((completion-ignore-case t)
15680 (keys (org-buffer-property-keys nil t t))
15681 (default-prop (or (save-excursion
15683 (beginning-of-line)
15684 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
15685 (null (string= (match-string 1) "END"))
15686 (match-string 1))))
15687 org-last-set-property))
15688 (property (org-icompleting-read
15690 (if default-prop (concat " [" default-prop "]") "")
15692 (mapcar 'list keys)
15695 (if (member property keys)
15697 (or (cdr (assoc (downcase property)
15698 (mapcar (lambda (x) (cons (downcase x) x))
15702 (defun org-set-property-and-value (use-last)
15703 "Allow to set [PROPERTY]: [value] direction from prompt.
15704 When use-default, don't even ask, just use the last
15705 \"[PROPERTY]: [value]\" string from the history."
15707 (let* ((completion-ignore-case t)
15708 (pv (or (and use-last org-last-set-property-value)
15709 (org-completing-read
15710 "Enter a \"[Property]: [value]\" pair: "
15711 nil nil nil nil nil
15712 org-last-set-property-value)))
15714 (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
15715 (setq prop (match-string 1 pv)
15716 val (match-string 2 pv))
15717 (org-set-property prop val))))
15719 (defun org-set-property (property value)
15720 "In the current entry, set PROPERTY to VALUE.
15721 When called interactively, this will prompt for a property name, offering
15722 completion on existing and default properties. And then it will prompt
15723 for a value, offering completion either on allowed values (via an inherited
15724 xxx_ALL property) or on existing values in other instances of this property
15725 in the current file."
15726 (interactive (list nil nil))
15727 (let* ((property (or property (org-read-property-name)))
15728 (value (or value (org-read-property-value property)))
15729 (fn (cdr (assoc property org-properties-postprocess-alist))))
15730 (setq org-last-set-property property)
15731 (setq org-last-set-property-value (concat property ": " value))
15732 ;; Possibly postprocess the inserted value:
15733 (when fn (setq value (funcall fn value)))
15734 (unless (equal (org-entry-get nil property) value)
15735 (org-entry-put nil property value))))
15737 (defun org-delete-property (property &optional delete-empty-drawer)
15738 "In the current entry, delete PROPERTY.
15739 When optional argument DELETE-EMPTY-DRAWER is a string, it defines
15740 an empty drawer to delete."
15742 (let* ((completion-ignore-case t)
15743 (prop (org-icompleting-read "Property: "
15744 (org-entry-properties nil 'standard))))
15746 (message "Property %s %s" property
15747 (if (org-entry-delete nil property delete-empty-drawer)
15749 "was not present in the entry")))
15751 (defun org-delete-property-globally (property)
15752 "Remove PROPERTY globally, from all entries."
15754 (let* ((completion-ignore-case t)
15755 (prop (org-icompleting-read
15756 "Globally remove property: "
15757 (mapcar 'list (org-buffer-property-keys)))))
15762 (goto-char (point-min))
15764 (while (re-search-forward
15765 (org-re-property property)
15767 (setq cnt (1+ cnt))
15768 (delete-region (match-beginning 0) (1+ (point-at-eol))))
15769 (message "Property \"%s\" removed from %d entries" property cnt)))))
15771 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
15773 (defun org-compute-property-at-point ()
15774 "Compute the property at point.
15775 This looks for an enclosing column format, extracts the operator and
15776 then applies it to the property in the column format's scope."
15778 (unless (org-at-property-p)
15779 (user-error "Not at a property"))
15780 (let ((prop (org-match-string-no-properties 2)))
15781 (org-columns-get-format-and-top-level)
15782 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
15783 (user-error "No operator defined for property %s" prop))
15784 (org-columns-compute prop)))
15786 (defvar org-property-allowed-value-functions nil
15787 "Hook for functions supplying allowed values for a specific property.
15788 The functions must take a single argument, the name of the property, and
15789 return a flat list of allowed values. If \":ETC\" is one of
15790 the values, this means that these values are intended as defaults for
15791 completion, but that other values should be allowed too.
15792 The functions must return nil if they are not responsible for this
15795 (defun org-property-get-allowed-values (pom property &optional table)
15796 "Get allowed values for the property PROPERTY.
15797 When TABLE is non-nil, return an alist that can directly be used for
15801 ((equal property "TODO")
15802 (setq vals (org-with-point-at pom
15803 (append org-todo-keywords-1 '("")))))
15804 ((equal property "PRIORITY")
15805 (let ((n org-lowest-priority))
15806 (while (>= n org-highest-priority)
15807 (push (char-to-string n) vals)
15809 ((member property org-special-properties))
15810 ((setq vals (run-hook-with-args-until-success
15811 'org-property-allowed-value-functions property)))
15813 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
15814 (when (and vals (string-match "\\S-" vals))
15815 (setq vals (car (read-from-string (concat "(" vals ")"))))
15816 (setq vals (mapcar (lambda (x)
15817 (cond ((stringp x) x)
15818 ((numberp x) (number-to-string x))
15819 ((symbolp x) (symbol-name x))
15822 (when (member ":ETC" vals)
15823 (setq vals (remove ":ETC" vals))
15824 (org-add-props (car vals) '(org-unrestricted t)))
15825 (if table (mapcar 'list vals) vals)))
15827 (defun org-property-previous-allowed-value (&optional previous)
15828 "Switch to the next allowed value for this property."
15830 (org-property-next-allowed-value t))
15832 (defun org-property-next-allowed-value (&optional previous)
15833 "Switch to the next allowed value for this property."
15835 (unless (org-at-property-p)
15836 (user-error "Not at a property"))
15837 (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
15838 (key (match-string 2))
15839 (value (match-string 3))
15840 (allowed (or (org-property-get-allowed-values (point) key)
15841 (and (member value '("[ ]" "[-]" "[X]"))
15843 (heading (save-match-data (nth 4 (org-heading-components))))
15846 (user-error "Allowed values for this property have not been defined"))
15847 (if previous (setq allowed (reverse allowed)))
15848 (if (member value allowed)
15849 (setq nval (car (cdr (member value allowed)))))
15850 (setq nval (or nval (car allowed)))
15851 (if (equal nval value)
15852 (user-error "Only one allowed value for this property"))
15853 (org-at-property-p)
15854 (replace-match (concat " :" key ": " nval) t t)
15856 (beginning-of-line 1)
15857 (skip-chars-forward " \t")
15858 (when (equal prop org-effort-property)
15860 (org-back-to-heading t)
15861 (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
15862 (when (string= org-clock-current-task heading)
15863 (setq org-clock-effort nval)
15864 (org-clock-update-mode-line)))
15865 (run-hook-with-args 'org-property-changed-functions key nval)))
15867 (defun org-find-olp (path &optional this-buffer)
15868 "Return a marker pointing to the entry at outline path OLP.
15869 If anything goes wrong, throw an error.
15870 You can wrap this call to catch the error like this:
15872 (condition-case msg
15873 (org-mobile-locate-entry (match-string 4))
15874 (error (nth 1 msg)))
15876 The return value will then be either a string with the error message,
15877 or a marker if everything is OK.
15879 If THIS-BUFFER is set, the outline path does not contain a file,
15881 (let* ((file (if this-buffer buffer-file-name (pop path)))
15882 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
15886 limit re end found pos heading cnt flevel)
15887 (unless buffer (error "File not found :%s" file))
15888 (with-current-buffer buffer
15892 (setq limit (point-max))
15893 (goto-char (point-min))
15894 (while (setq heading (pop path))
15895 (setq re (format org-complex-heading-regexp-format
15896 (regexp-quote heading)))
15897 (setq cnt 0 pos (point))
15898 (while (re-search-forward re end t)
15899 (setq level (- (match-end 1) (match-beginning 1)))
15900 (if (and (>= level lmin) (<= level lmax))
15901 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
15902 (when (= cnt 0) (error "Heading not found on level %d: %s"
15904 (when (> cnt 1) (error "Heading not unique on level %d: %s"
15907 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
15908 (setq end (save-excursion (org-end-of-subtree t t))))
15909 (when (org-at-heading-p)
15910 (point-marker)))))))
15912 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
15913 "Find node HEADING in BUFFER.
15914 Return a marker to the heading if it was found, or nil if not.
15915 If POS-ONLY is set, return just the position instead of a marker.
15917 The heading text must match exact, but it may have a TODO keyword,
15918 a priority cookie and tags in the standard locations."
15919 (with-current-buffer (or buffer (current-buffer))
15923 (goto-char (point-min))
15924 (let (case-fold-search)
15925 (if (re-search-forward
15926 (format org-complex-heading-regexp-format
15927 (regexp-quote heading)) nil t)
15929 (match-beginning 0)
15930 (move-marker (make-marker) (match-beginning 0)))))))))
15932 (defun org-find-exact-heading-in-directory (heading &optional dir)
15933 "Find Org node headline HEADING in all .org files in directory DIR.
15934 When the target headline is found, return a marker to this location."
15935 (let ((files (directory-files (or dir default-directory)
15936 nil "\\`[^.#].*\\.org\\'"))
15937 file visiting m buffer)
15939 (while (setq file (pop files))
15940 (message "trying %s" file)
15941 (setq visiting (org-find-base-buffer-visiting file))
15942 (setq buffer (or visiting (find-file-noselect file)))
15943 (setq m (org-find-exact-headline-in-buffer
15945 (when (and (not m) (not visiting)) (kill-buffer buffer))
15946 (and m (throw 'found m))))))
15948 (defun org-find-entry-with-id (ident)
15949 "Locate the entry that contains the ID property with exact value IDENT.
15950 IDENT can be a string, a symbol or a number, this function will search for
15951 the string representation of it.
15952 Return the position where this entry starts, or nil if there is no such entry."
15953 (interactive "sID: ")
15955 ((stringp ident) ident)
15956 ((symbol-name ident) (symbol-name ident))
15957 ((numberp ident) (number-to-string ident))
15958 (t (error "IDENT %s must be a string, symbol or number" ident))))
15959 (case-fold-search nil))
15963 (goto-char (point-min))
15964 (when (re-search-forward
15965 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
15967 (org-back-to-heading t)
15972 (defvar org-last-changed-timestamp nil)
15973 (defvar org-last-inserted-timestamp nil
15974 "The last time stamp inserted with `org-insert-time-stamp'.")
15975 (defvar org-time-was-given) ; dynamically scoped parameter
15976 (defvar org-end-time-was-given) ; dynamically scoped parameter
15977 (defvar org-ts-what) ; dynamically scoped parameter
15979 (defun org-time-stamp (arg &optional inactive)
15980 "Prompt for a date/time and insert a time stamp.
15981 If the user specifies a time like HH:MM or if this command is
15982 called with at least one prefix argument, the time stamp contains
15983 the date and the time. Otherwise, only the date is be included.
15985 All parts of a date not specified by the user is filled in from
15986 the current date/time. So if you just press return without
15987 typing anything, the time stamp will represent the current
15990 If there is already a timestamp at the cursor, it will be
15993 With two universal prefix arguments, insert an active timestamp
15994 with the current time without prompting the user."
15998 ;; Default time is either today, or, when entering a range,
15999 ;; the range start.
16000 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
16002 (re-search-backward
16003 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
16004 (- (point) 20) t)))
16005 (apply 'encode-time (org-parse-time-string (match-string 1)))
16007 (default-input (and ts (org-get-compact-tod ts)))
16008 (repeater (save-excursion
16010 (beginning-of-line)
16011 (when (re-search-forward
16012 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
16013 (save-excursion (progn (end-of-line) (point))) t)
16014 (match-string 0)))))
16015 org-time-was-given org-end-time-was-given time)
16017 ((and (org-at-timestamp-p t)
16018 (memq last-command '(org-time-stamp org-time-stamp-inactive))
16019 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
16021 (setq time (let ((this-command this-command))
16022 (org-read-date arg 'totime nil nil
16023 default-time default-input inactive)))
16024 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
16025 ((org-at-timestamp-p t)
16026 (setq time (let ((this-command this-command))
16027 (org-read-date arg 'totime nil nil default-time default-input inactive)))
16028 (when (org-at-timestamp-p t) ; just to get the match data
16029 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
16031 (setq org-last-changed-timestamp
16032 (org-insert-time-stamp
16033 time (or org-time-was-given arg)
16034 inactive nil nil (list org-end-time-was-given)))
16035 (when repeater (goto-char (1- (point))) (insert " " repeater)
16036 (setq org-last-changed-timestamp
16037 (concat (substring org-last-inserted-timestamp 0 -1)
16038 " " repeater ">"))))
16039 (message "Timestamp updated"))
16041 (org-insert-time-stamp (current-time) t))
16043 (setq time (let ((this-command this-command))
16044 (org-read-date arg 'totime nil nil default-time default-input inactive)))
16045 (org-insert-time-stamp time (or org-time-was-given arg) inactive
16046 nil nil (list org-end-time-was-given))))))
16048 ;; FIXME: can we use this for something else, like computing time differences?
16049 (defun org-get-compact-tod (s)
16050 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
16051 (let* ((t1 (match-string 1 s))
16052 (h1 (string-to-number (match-string 2 s)))
16053 (m1 (string-to-number (match-string 3 s)))
16054 (t2 (and (match-end 4) (match-string 5 s)))
16055 (h2 (and t2 (string-to-number (match-string 6 s))))
16056 (m2 (and t2 (string-to-number (match-string 7 s))))
16060 (setq dh (- h2 h1) dm (- m2 m1))
16061 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
16062 (concat t1 "+" (number-to-string dh)
16063 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
16065 (defun org-time-stamp-inactive (&optional arg)
16066 "Insert an inactive time stamp.
16067 An inactive time stamp is enclosed in square brackets instead of angle
16068 brackets. It is inactive in the sense that it does not trigger agenda entries,
16069 does not link to the calendar and cannot be changed with the S-cursor keys.
16070 So these are more for recording a certain time/date."
16072 (org-time-stamp arg 'inactive))
16074 (defvar org-date-ovl (make-overlay 1 1))
16075 (overlay-put org-date-ovl 'face 'org-date-selected)
16076 (org-detach-overlay org-date-ovl)
16078 (defvar org-ans1) ; dynamically scoped parameter
16079 (defvar org-ans2) ; dynamically scoped parameter
16081 (defvar org-plain-time-of-day-regexp) ; defined below
16083 (defvar org-overriding-default-time nil) ; dynamically scoped
16084 (defvar org-read-date-overlay nil)
16085 (defvar org-dcst nil) ; dynamically scoped
16086 (defvar org-read-date-history nil)
16087 (defvar org-read-date-final-answer nil)
16088 (defvar org-read-date-analyze-futurep nil)
16089 (defvar org-read-date-analyze-forced-year nil)
16090 (defvar org-read-date-inactive)
16092 (defvar org-read-date-minibuffer-local-map
16093 (let ((map (make-sparse-keymap)))
16094 (set-keymap-parent map minibuffer-local-map)
16095 (org-defkey map (kbd ".")
16096 (lambda () (interactive)
16097 ;; Are we at the beginning of the prompt?
16098 (if (looking-back "^[^:]+: ")
16099 (org-eval-in-calendar '(calendar-goto-today))
16101 (org-defkey map (kbd "C-.")
16102 (lambda () (interactive)
16103 (org-eval-in-calendar '(calendar-goto-today))))
16104 (org-defkey map [(meta shift left)]
16105 (lambda () (interactive)
16106 (org-eval-in-calendar '(calendar-backward-month 1))))
16107 (org-defkey map [(meta shift right)]
16108 (lambda () (interactive)
16109 (org-eval-in-calendar '(calendar-forward-month 1))))
16110 (org-defkey map [(meta shift up)]
16111 (lambda () (interactive)
16112 (org-eval-in-calendar '(calendar-backward-year 1))))
16113 (org-defkey map [(meta shift down)]
16114 (lambda () (interactive)
16115 (org-eval-in-calendar '(calendar-forward-year 1))))
16116 (org-defkey map [?\e (shift left)]
16117 (lambda () (interactive)
16118 (org-eval-in-calendar '(calendar-backward-month 1))))
16119 (org-defkey map [?\e (shift right)]
16120 (lambda () (interactive)
16121 (org-eval-in-calendar '(calendar-forward-month 1))))
16122 (org-defkey map [?\e (shift up)]
16123 (lambda () (interactive)
16124 (org-eval-in-calendar '(calendar-backward-year 1))))
16125 (org-defkey map [?\e (shift down)]
16126 (lambda () (interactive)
16127 (org-eval-in-calendar '(calendar-forward-year 1))))
16128 (org-defkey map [(shift up)]
16129 (lambda () (interactive)
16130 (org-eval-in-calendar '(calendar-backward-week 1))))
16131 (org-defkey map [(shift down)]
16132 (lambda () (interactive)
16133 (org-eval-in-calendar '(calendar-forward-week 1))))
16134 (org-defkey map [(shift left)]
16135 (lambda () (interactive)
16136 (org-eval-in-calendar '(calendar-backward-day 1))))
16137 (org-defkey map [(shift right)]
16138 (lambda () (interactive)
16139 (org-eval-in-calendar '(calendar-forward-day 1))))
16140 (org-defkey map "!"
16141 (lambda () (interactive)
16142 (org-eval-in-calendar '(diary-view-entries))
16144 (org-defkey map ">"
16145 (lambda () (interactive)
16146 (org-eval-in-calendar '(scroll-calendar-left 1))))
16147 (org-defkey map "<"
16148 (lambda () (interactive)
16149 (org-eval-in-calendar '(scroll-calendar-right 1))))
16150 (org-defkey map "\C-v"
16151 (lambda () (interactive)
16152 (org-eval-in-calendar
16153 '(calendar-scroll-left-three-months 1))))
16154 (org-defkey map "\M-v"
16155 (lambda () (interactive)
16156 (org-eval-in-calendar
16157 '(calendar-scroll-right-three-months 1))))
16159 "Keymap for minibuffer commands when using `org-read-date'.")
16161 (defun org-read-date (&optional org-with-time to-time from-string prompt
16162 default-time default-input inactive)
16163 "Read a date, possibly a time, and make things smooth for the user.
16164 The prompt will suggest to enter an ISO date, but you can also enter anything
16165 which will at least partially be understood by `parse-time-string'.
16166 Unrecognized parts of the date will default to the current day, month, year,
16167 hour and minute. If this command is called to replace a timestamp at point,
16168 or to enter the second timestamp of a range, the default time is taken
16169 from the existing stamp. Furthermore, the command prefers the future,
16170 so if you are giving a date where the year is not given, and the day-month
16171 combination is already past in the current year, it will assume you
16172 mean next year. For details, see the manual. A few examples:
16174 3-2-5 --> 2003-02-05
16175 feb 15 --> currentyear-02-15
16176 2/15 --> currentyear-02-15
16177 sep 12 9 --> 2009-09-12
16178 12:45 --> today 12:45
16179 22 sept 0:34 --> currentyear-09-22 0:34
16180 12 --> currentyear-currentmonth-12
16181 Fri --> nearest Friday after today
16182 -Tue --> last Tuesday
16185 Furthermore you can specify a relative date by giving, as the *first* thing
16186 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
16187 change in days weeks, months, years.
16188 With a single plus or minus, the date is relative to today. With a double
16189 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
16190 +4d --> four days from today
16191 +4 --> same as above
16192 +2w --> two weeks from today
16193 ++5 --> five days from default date
16195 The function understands only English month and weekday abbreviations.
16197 While prompting, a calendar is popped up - you can also select the
16198 date with the mouse (button 1). The calendar shows a period of three
16199 months. To scroll it to other months, use the keys `>' and `<'.
16200 If you don't like the calendar, turn it off with
16201 \(setq org-read-date-popup-calendar nil)
16203 With optional argument TO-TIME, the date will immediately be converted
16204 to an internal time.
16205 With an optional argument ORG-WITH-TIME, the prompt will suggest to
16206 also insert a time. Note that when ORG-WITH-TIME is not set, you can
16207 still enter a time, and this function will inform the calling routine
16208 about this change. The calling routine may then choose to change the
16209 format used to insert the time stamp into the buffer to include the time.
16210 With optional argument FROM-STRING, read from this string instead from
16211 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
16212 the time/date that is used for everything that is not specified by the
16214 (require 'parse-time)
16215 (let* ((org-time-stamp-rounding-minutes
16216 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
16217 (org-dcst org-display-custom-times)
16218 (ct (org-current-time))
16219 (org-def (or org-overriding-default-time default-time ct))
16220 (org-defdecode (decode-time org-def))
16222 (when (< (nth 2 org-defdecode) org-extend-today-until)
16223 (setcar (nthcdr 2 org-defdecode) -1)
16224 (setcar (nthcdr 1 org-defdecode) 59)
16225 (setq org-def (apply 'encode-time org-defdecode)
16226 org-defdecode (decode-time org-def)))))
16227 (mouse-autoselect-window nil) ; Don't let the mouse jump
16228 (calendar-frame-setup nil)
16229 (calendar-setup nil)
16230 (calendar-move-hook nil)
16231 (calendar-view-diary-initially-flag nil)
16232 (calendar-view-holidays-initially-flag nil)
16233 (timestr (format-time-string
16234 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
16235 (prompt (concat (if prompt (concat prompt " ") "")
16236 (format "Date+time [%s]: " timestr)))
16237 ans (org-ans0 "") org-ans1 org-ans2 final)
16240 (from-string (setq ans from-string))
16241 (org-read-date-popup-calendar
16243 (save-window-excursion
16245 (org-eval-in-calendar '(setq cursor-type nil) t)
16248 (calendar-forward-day (- (time-to-days org-def)
16249 (calendar-absolute-from-gregorian
16250 (calendar-current-date))))
16251 (org-eval-in-calendar nil t)
16252 (let* ((old-map (current-local-map))
16253 (map (copy-keymap calendar-mode-map))
16254 (minibuffer-local-map
16255 (copy-keymap org-read-date-minibuffer-local-map)))
16256 (org-defkey map (kbd "RET") 'org-calendar-select)
16257 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
16258 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
16261 (use-local-map map)
16262 (setq org-read-date-inactive inactive)
16263 (add-hook 'post-command-hook 'org-read-date-display)
16264 (setq org-ans0 (read-string prompt default-input
16265 'org-read-date-history nil))
16266 ;; org-ans0: from prompt
16267 ;; org-ans1: from mouse click
16268 ;; org-ans2: from calendar motion
16269 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
16270 (remove-hook 'post-command-hook 'org-read-date-display)
16271 (use-local-map old-map)
16272 (when org-read-date-overlay
16273 (delete-overlay org-read-date-overlay)
16274 (setq org-read-date-overlay nil)))))
16275 (bury-buffer "*Calendar*")))))
16277 (t ; Naked prompt only
16279 (setq ans (read-string prompt default-input
16280 'org-read-date-history timestr))
16281 (when org-read-date-overlay
16282 (delete-overlay org-read-date-overlay)
16283 (setq org-read-date-overlay nil)))))
16285 (setq final (org-read-date-analyze ans org-def org-defdecode))
16287 (when org-read-date-analyze-forced-year
16288 (message "Year was forced into %s"
16289 (if org-read-date-force-compatible-dates
16290 "compatible range (1970-2037)"
16291 "range representable on this machine"))
16294 ;; One round trip to get rid of 34th of August and stuff like that....
16295 (setq final (decode-time (apply 'encode-time final)))
16297 (setq org-read-date-final-answer ans)
16300 (apply 'encode-time final)
16301 (if (and (boundp 'org-time-was-given) org-time-was-given)
16302 (format "%04d-%02d-%02d %02d:%02d"
16303 (nth 5 final) (nth 4 final) (nth 3 final)
16304 (nth 2 final) (nth 1 final))
16305 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
16308 (defvar org-defdecode)
16309 (defvar org-with-time)
16310 (defun org-read-date-display ()
16311 "Display the current date prompt interpretation in the minibuffer."
16312 (when org-read-date-display-live
16313 (when org-read-date-overlay
16314 (delete-overlay org-read-date-overlay))
16315 (when (minibufferp (current-buffer))
16318 (while (not (equal (buffer-substring
16319 (max (point-min) (- (point) 4)) (point))
16322 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
16323 " " (or org-ans1 org-ans2)))
16324 (org-end-time-was-given nil)
16325 (f (org-read-date-analyze ans org-def org-defdecode))
16327 org-time-stamp-custom-formats
16328 org-time-stamp-formats))
16329 (fmt (if (or org-with-time
16330 (and (boundp 'org-time-was-given) org-time-was-given))
16333 (txt (format-time-string fmt (apply 'encode-time f)))
16334 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
16335 (txt (concat "=> " txt)))
16336 (when (and org-end-time-was-given
16337 (string-match org-plain-time-of-day-regexp txt))
16338 (setq txt (concat (substring txt 0 (match-end 0)) "-"
16339 org-end-time-was-given
16340 (substring txt (match-end 0)))))
16341 (when org-read-date-analyze-futurep
16342 (setq txt (concat txt " (=>F)")))
16343 (setq org-read-date-overlay
16344 (make-overlay (1- (point-at-eol)) (point-at-eol)))
16345 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
16347 (defun org-read-date-analyze (ans org-def org-defdecode)
16348 "Analyze the combined answer of the date prompt."
16349 ;; FIXME: cleanup and comment
16350 (let ((nowdecode (decode-time (current-time)))
16351 delta deltan deltaw deltadef year month day
16352 hour minute second wday pm h2 m2 tl wday1
16353 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
16354 (setq org-read-date-analyze-futurep nil
16355 org-read-date-analyze-forced-year nil)
16356 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
16359 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
16360 (setq ans (replace-match "" t t ans)
16362 deltaw (nth 1 delta)
16363 deltadef (nth 2 delta)))
16365 ;; Check if there is an iso week date in there. If yes, store the
16366 ;; info and postpone interpreting it until the rest of the parsing
16368 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
16369 (setq iso-year (if (match-end 1)
16370 (org-small-year-to-year
16371 (string-to-number (match-string 1 ans))))
16372 iso-weekday (if (match-end 3)
16373 (string-to-number (match-string 3 ans)))
16374 iso-week (string-to-number (match-string 2 ans)))
16375 (setq ans (replace-match "" t t ans)))
16377 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
16378 (when (string-match
16379 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
16380 (setq year (if (match-end 2)
16381 (string-to-number (match-string 2 ans))
16382 (progn (setq kill-year t)
16383 (string-to-number (format-time-string "%Y"))))
16384 month (string-to-number (match-string 3 ans))
16385 day (string-to-number (match-string 4 ans)))
16386 (if (< year 100) (setq year (+ 2000 year)))
16387 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
16390 ;; Help matching dotted european dates
16391 (when (string-match
16392 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
16393 (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
16395 (string-to-number (format-time-string "%Y")))
16396 day (string-to-number (match-string 1 ans))
16397 month (string-to-number (match-string 2 ans))
16398 ans (replace-match (format "%04d-%02d-%02d" year month day)
16401 ;; Help matching american dates, like 5/30 or 5/30/7
16402 (when (string-match
16403 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
16404 (setq year (if (match-end 4)
16405 (string-to-number (match-string 4 ans))
16406 (progn (setq kill-year t)
16407 (string-to-number (format-time-string "%Y"))))
16408 month (string-to-number (match-string 1 ans))
16409 day (string-to-number (match-string 2 ans)))
16410 (if (< year 100) (setq year (+ 2000 year)))
16411 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
16413 ;; Help matching am/pm times, because `parse-time-string' does not do that.
16414 ;; If there is a time with am/pm, and *no* time without it, we convert
16415 ;; so that matching will be successful.
16416 (loop for i from 1 to 2 do ; twice, for end time as well
16417 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
16418 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
16419 (setq hour (string-to-number (match-string 1 ans))
16420 minute (if (match-end 3)
16421 (string-to-number (match-string 3 ans))
16424 (string-to-char (downcase (match-string 4 ans)))))
16425 (if (and (= hour 12) (not pm))
16427 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
16428 (setq ans (replace-match (format "%02d:%02d" hour minute)
16431 ;; Check if a time range is given as a duration
16432 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
16433 (setq hour (string-to-number (match-string 1 ans))
16434 h2 (+ hour (string-to-number (match-string 3 ans)))
16435 minute (string-to-number (match-string 2 ans))
16436 m2 (+ minute (if (match-end 5) (string-to-number
16437 (match-string 5 ans))0)))
16438 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
16439 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
16442 ;; Check if there is a time range
16443 (when (boundp 'org-end-time-was-given)
16444 (setq org-time-was-given nil)
16445 (when (and (string-match org-plain-time-of-day-regexp ans)
16447 (setq org-end-time-was-given (match-string 8 ans))
16448 (setq ans (concat (substring ans 0 (match-beginning 7))
16449 (substring ans (match-end 7))))))
16451 (setq tl (parse-time-string ans)
16452 day (or (nth 3 tl) (nth 3 org-defdecode))
16453 month (or (nth 4 tl)
16454 (if (and org-read-date-prefer-future
16455 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
16456 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
16457 (nth 4 org-defdecode)))
16458 year (or (and (not kill-year) (nth 5 tl))
16459 (if (and org-read-date-prefer-future
16460 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
16461 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
16462 (nth 5 org-defdecode)))
16463 hour (or (nth 2 tl) (nth 2 org-defdecode))
16464 minute (or (nth 1 tl) (nth 1 org-defdecode))
16465 second (or (nth 0 tl) 0)
16468 (when (and (eq org-read-date-prefer-future 'time)
16469 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
16470 (equal day (nth 3 nowdecode))
16471 (equal month (nth 4 nowdecode))
16472 (equal year (nth 5 nowdecode))
16474 (or (< (nth 2 tl) (nth 2 nowdecode))
16475 (and (= (nth 2 tl) (nth 2 nowdecode))
16477 (< (nth 1 tl) (nth 1 nowdecode)))))
16481 ;; Special date definitions below
16484 ;; There was an iso week
16487 (setq year (or iso-year year)
16488 day (or iso-weekday wday 1)
16489 wday nil ; to make sure that the trigger below does not match
16490 iso-date (calendar-gregorian-from-absolute
16491 (calendar-absolute-from-iso
16492 (list iso-week day year))))
16493 ; FIXME: Should we also push ISO weeks into the future?
16494 ; (when (and org-read-date-prefer-future
16496 ; (< (calendar-absolute-from-gregorian iso-date)
16497 ; (time-to-days (current-time))))
16498 ; (setq year (1+ year)
16499 ; iso-date (calendar-gregorian-from-absolute
16500 ; (calendar-absolute-from-iso
16501 ; (list iso-week day year)))))
16502 (setq month (car iso-date)
16503 year (nth 2 iso-date)
16504 day (nth 1 iso-date)))
16508 (let ((now (decode-time (current-time))))
16509 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
16510 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
16511 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
16512 ((equal deltaw "m") (setq month (+ month deltan)))
16513 ((equal deltaw "y") (setq year (+ year deltan)))))
16514 ((and wday (not (nth 3 tl)))
16515 ;; Weekday was given, but no day, so pick that day in the week
16516 ;; on or after the derived date.
16517 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
16518 (unless (equal wday wday1)
16519 (setq day (+ day (% (- wday wday1 -7) 7))))))
16520 (if (and (boundp 'org-time-was-given)
16522 (setq org-time-was-given t))
16523 (if (< year 100) (setq year (+ 2000 year)))
16524 ;; Check of the date is representable
16525 (if org-read-date-force-compatible-dates
16528 (setq year 1970 org-read-date-analyze-forced-year t))
16530 (setq year 2037 org-read-date-analyze-forced-year t)))
16531 (condition-case nil
16532 (ignore (encode-time second minute hour day month year))
16534 (setq year (nth 5 org-defdecode))
16535 (setq org-read-date-analyze-forced-year t))))
16536 (setq org-read-date-analyze-futurep futurep)
16537 (list second minute hour day month year)))
16539 (defvar parse-time-weekdays)
16540 (defun org-read-date-get-relative (s today default)
16541 "Check string S for special relative date string.
16542 TODAY and DEFAULT are internal times, for today and for a default.
16543 Return shift list (N what def-flag)
16544 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
16545 N is the number of WHATs to shift.
16546 DEF-FLAG is t when a double ++ or -- indicates shift relative to
16547 the DEFAULT date rather than TODAY."
16548 (require 'parse-time)
16552 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
16554 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
16555 "\\([ \t]\\|$\\)") s)
16556 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
16557 (let* ((dir (if (> (match-end 1) (match-beginning 1))
16558 (string-to-char (substring (match-string 1 s) -1))
16560 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
16561 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
16562 (what (if (match-end 3) (match-string 3 s) "d"))
16563 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
16564 (date (if rel default today))
16565 (wday (nth 6 (decode-time date)))
16569 (setq delta (mod (+ 7 (- wday1 wday)) 7))
16570 (if (= delta 0) (setq delta 7))
16573 (setq delta (- delta 7))
16574 (if (= delta 0) (setq delta -7))))
16575 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
16576 (list delta "d" rel))
16577 (list (* n (if (= dir ?-) -1 1)) what rel)))))
16579 (defun org-order-calendar-date-args (arg1 arg2 arg3)
16580 "Turn a user-specified date into the internal representation.
16581 The internal representation needed by the calendar is (month day year).
16582 This is a wrapper to handle the brain-dead convention in calendar that
16583 user function argument order change dependent on argument order."
16584 (if (boundp 'calendar-date-style)
16586 ((eq calendar-date-style 'american)
16587 (list arg1 arg2 arg3))
16588 ((eq calendar-date-style 'european)
16589 (list arg2 arg1 arg3))
16590 ((eq calendar-date-style 'iso)
16591 (list arg2 arg3 arg1)))
16592 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
16593 (if (org-bound-and-true-p european-calendar-style)
16594 (list arg2 arg1 arg3)
16595 (list arg1 arg2 arg3)))))
16597 (defun org-eval-in-calendar (form &optional keepdate)
16598 "Eval FORM in the calendar window and return to current window.
16599 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
16600 otherwise stick to the current value of `org-ans2'."
16601 (let ((sf (selected-frame))
16602 (sw (selected-window)))
16603 (select-window (get-buffer-window "*Calendar*" t))
16605 (when (and (not keepdate) (calendar-cursor-to-date))
16606 (let* ((date (calendar-cursor-to-date))
16607 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16608 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
16609 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
16611 (org-select-frame-set-input-focus sf)))
16613 (defun org-calendar-select ()
16614 "Return to `org-read-date' with the date currently selected.
16615 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16617 (when (calendar-cursor-to-date)
16618 (let* ((date (calendar-cursor-to-date))
16619 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16620 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
16621 (if (active-minibuffer-window) (exit-minibuffer))))
16623 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
16624 "Insert a date stamp for the date given by the internal TIME.
16625 WITH-HM means use the stamp format that includes the time of the day.
16626 INACTIVE means use square brackets instead of angular ones, so that the
16627 stamp will not contribute to the agenda.
16628 PRE and POST are optional strings to be inserted before and after the
16630 The command returns the inserted time stamp."
16631 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
16633 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
16634 (insert-before-markers (or pre ""))
16635 (when (listp extra)
16636 (setq extra (car extra))
16637 (if (and (stringp extra)
16638 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
16639 (setq extra (format "-%02d:%02d"
16640 (string-to-number (match-string 1 extra))
16641 (string-to-number (match-string 2 extra))))
16644 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
16645 (insert-before-markers (setq stamp (format-time-string fmt time)))
16646 (insert-before-markers (or post ""))
16647 (setq org-last-inserted-timestamp stamp)))
16649 (defun org-toggle-time-stamp-overlays ()
16650 "Toggle the use of custom time stamp formats."
16652 (setq org-display-custom-times (not org-display-custom-times))
16653 (unless org-display-custom-times
16654 (let ((p (point-min)) (bmp (buffer-modified-p)))
16655 (while (setq p (next-single-property-change p 'display))
16656 (if (and (get-text-property p 'display)
16657 (eq (get-text-property p 'face) 'org-date))
16658 (remove-text-properties
16659 p (setq p (next-single-property-change p 'display))
16661 (set-buffer-modified-p bmp)))
16662 (if (featurep 'xemacs)
16663 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
16664 (org-restart-font-lock)
16665 (setq org-table-may-need-update t)
16666 (if org-display-custom-times
16667 (message "Time stamps are overlaid with custom format")
16668 (message "Time stamp overlays removed")))
16670 (defun org-display-custom-time (beg end)
16671 "Overlay modified time stamp format over timestamp between BEG and END."
16672 (let* ((ts (buffer-substring beg end))
16673 t1 w1 with-hm tf time str w2 (off 0))
16675 (setq t1 (org-parse-time-string ts t))
16676 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
16677 (setq off (- (match-end 0) (match-beginning 0)))))
16678 (setq end (- end off))
16679 (setq w1 (- end beg)
16680 with-hm (and (nth 1 t1) (nth 2 t1))
16681 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
16682 time (org-fix-decoded-time t1)
16684 (format-time-string
16685 (substring tf 1 -1) (apply 'encode-time time))
16686 nil 'mouse-face 'highlight)
16688 (if (not (= w2 w1))
16689 (add-text-properties (1+ beg) (+ 2 beg)
16690 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
16691 (if (featurep 'xemacs)
16693 (put-text-property beg end 'invisible t)
16694 (put-text-property beg end 'end-glyph (make-glyph str)))
16695 (put-text-property beg end 'display str))))
16697 (defun org-translate-time (string)
16698 "Translate all timestamps in STRING to custom format.
16699 But do this only if the variable `org-display-custom-times' is set."
16700 (when org-display-custom-times
16703 (re org-ts-regexp-both)
16704 t1 with-hm inactive tf time str beg end)
16705 (while (setq start (string-match re string start))
16706 (setq beg (match-beginning 0)
16708 t1 (save-match-data
16709 (org-parse-time-string (substring string beg end) t))
16710 with-hm (and (nth 1 t1) (nth 2 t1))
16711 inactive (equal (substring string beg (1+ beg)) "[")
16712 tf (funcall (if with-hm 'cdr 'car)
16713 org-time-stamp-custom-formats)
16714 time (org-fix-decoded-time t1)
16715 str (format-time-string
16717 (if inactive "[" "<") (substring tf 1 -1)
16718 (if inactive "]" ">"))
16719 (apply 'encode-time time))
16720 string (replace-match str t t string)
16721 start (+ start (length str)))))))
16724 (defun org-fix-decoded-time (time)
16725 "Set 0 instead of nil for the first 6 elements of time.
16726 Don't touch the rest."
16728 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
16730 (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
16732 (defun org-time-stamp-to-now (timestamp-string &optional seconds)
16733 "Difference between TIMESTAMP-STRING and now in days.
16734 If SECONDS is non-nil, return the difference in seconds."
16735 (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
16736 (- (funcall fdiff (org-time-string-to-time timestamp-string))
16737 (funcall fdiff (current-time)))))
16739 (defun org-deadline-close (timestamp-string &optional ndays)
16740 "Is the time in TIMESTAMP-STRING close to the current date?"
16741 (setq ndays (or ndays (org-get-wdays timestamp-string)))
16742 (and (< (org-time-stamp-to-now timestamp-string) ndays)
16743 (not (org-entry-is-done-p))))
16745 (defun org-get-wdays (ts &optional delay zero-delay)
16746 "Get the deadline lead time appropriate for timestring TS.
16747 When DELAY is non-nil, get the delay time for scheduled items
16748 instead of the deadline lead time. When ZERO-DELAY is non-nil
16749 and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
16750 don't try to find the delay cookie in the scheduled timestamp."
16751 (let ((tv (if delay org-scheduled-delay-days
16752 org-deadline-warning-days)))
16754 ((or (and delay (< tv 0))
16755 (and delay zero-delay (<= tv 0))
16756 (and (not delay) (<= tv 0)))
16757 ;; Enforce this value no matter what
16759 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
16760 ;; lead time is specified.
16761 (floor (* (string-to-number (match-string 1 ts))
16762 (cdr (assoc (match-string 2 ts)
16763 '(("d" . 1) ("w" . 7)
16764 ("m" . 30.4) ("y" . 365.25)
16765 ("h" . 0.041667)))))))
16766 ;; go for the default.
16769 (defun org-calendar-select-mouse (ev)
16770 "Return to `org-read-date' with the date currently selected.
16771 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16773 (mouse-set-point ev)
16774 (when (calendar-cursor-to-date)
16775 (let* ((date (calendar-cursor-to-date))
16776 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16777 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
16778 (if (active-minibuffer-window) (exit-minibuffer))))
16780 (defun org-check-deadlines (ndays)
16781 "Check if there are any deadlines due or past due.
16782 A deadline is considered due if it happens within `org-deadline-warning-days'
16783 days from today's date. If the deadline appears in an entry marked DONE,
16784 it is not shown. The prefix arg NDAYS can be used to test that many
16785 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
16787 (let* ((org-warn-days
16789 ((equal ndays '(4)) 100000)
16790 (ndays (prefix-numeric-value ndays))
16791 (t (abs org-deadline-warning-days))))
16792 (case-fold-search nil)
16793 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
16795 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
16797 (message "%d deadlines past-due or due within %d days"
16798 (org-occur regexp nil callback)
16801 (defsubst org-re-timestamp (type)
16802 "Return a regexp for timestamp TYPE.
16803 Allowed values for TYPE are:
16805 all: all timestamps
16806 active: only active timestamps (<...>)
16807 inactive: only inactive timestamps ([...])
16808 scheduled: only scheduled timestamps
16809 deadline: only deadline timestamps
16810 closed: only closed time-stamps
16812 When TYPE is nil, fall back on returning a regexp that matches
16813 both scheduled and deadline timestamps."
16814 (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9>
\n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
16815 ((eq type 'active) org-ts-regexp)
16816 ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]")
16817 ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
16818 ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
16819 ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]"))
16820 ((eq type 'scheduled-or-deadline)
16821 (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
16823 (defun org-check-before-date (date)
16824 "Check if there are deadlines or scheduled entries before DATE."
16825 (interactive (list (org-read-date)))
16826 (let ((case-fold-search nil)
16827 (regexp (org-re-timestamp org-ts-type))
16829 (lambda () (time-less-p
16830 (org-time-string-to-time (match-string 1))
16831 (org-time-string-to-time date)))))
16832 (message "%d entries before %s"
16833 (org-occur regexp nil callback) date)))
16835 (defun org-check-after-date (date)
16836 "Check if there are deadlines or scheduled entries after DATE."
16837 (interactive (list (org-read-date)))
16838 (let ((case-fold-search nil)
16839 (regexp (org-re-timestamp org-ts-type))
16843 (org-time-string-to-time (match-string 1))
16844 (org-time-string-to-time date))))))
16845 (message "%d entries after %s"
16846 (org-occur regexp nil callback) date)))
16848 (defun org-check-dates-range (start-date end-date)
16849 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
16850 (interactive (list (org-read-date nil nil nil "Range starts")
16851 (org-read-date nil nil nil "Range end")))
16852 (let ((case-fold-search nil)
16853 (regexp (org-re-timestamp org-ts-type))
16856 (let ((match (match-string 1)))
16859 (org-time-string-to-time match)
16860 (org-time-string-to-time start-date)))
16862 (org-time-string-to-time match)
16863 (org-time-string-to-time end-date)))))))
16864 (message "%d entries between %s and %s"
16865 (org-occur regexp nil callback) start-date end-date)))
16867 (defun org-evaluate-time-range (&optional to-buffer)
16868 "Evaluate a time range by computing the difference between start and end.
16869 Normally the result is just printed in the echo area, but with prefix arg
16870 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
16871 If the time range is actually in a table, the result is inserted into the
16873 For time difference computation, a year is assumed to be exactly 365
16874 days in order to avoid rounding problems."
16877 (org-clock-update-time-maybe)
16879 (unless (org-at-date-range-p t)
16880 (goto-char (point-at-bol))
16881 (re-search-forward org-tr-regexp-both (point-at-eol) t))
16882 (if (not (org-at-date-range-p t))
16883 (user-error "Not at a time-stamp range, and none found in current line")))
16884 (let* ((ts1 (match-string 1))
16885 (ts2 (match-string 2))
16886 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
16887 (match-end (match-end 0))
16888 (time1 (org-time-string-to-time ts1))
16889 (time2 (org-time-string-to-time ts2))
16890 (t1 (org-float-time time1))
16891 (t2 (org-float-time time2))
16892 (diff (abs (- t2 t1)))
16893 (negative (< (- t2 t1) 0))
16894 ;; (ys (floor (* 365 24 60 60)))
16897 (fy "%dy %dd %02d:%02d")
16899 (fd "%dd %02d:%02d")
16904 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16906 d (floor (/ diff ds)) diff (mod diff ds)
16907 h (floor (/ diff hs)) diff (mod diff hs)
16908 m (floor (/ diff 60)))
16909 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16911 d (floor (+ (/ diff ds) 0.5))
16913 (if (not to-buffer)
16914 (message "%s" (org-make-tdiff-string y d h m))
16915 (if (org-at-table-p)
16917 (goto-char match-end)
16919 (and (looking-at " *|") (goto-char (match-end 0))))
16920 (goto-char match-end))
16922 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
16923 (replace-match ""))
16924 (if negative (insert " -"))
16925 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
16926 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
16927 (insert " " (format fh h m))))
16928 (if align (org-table-align))
16929 (message "Time difference inserted")))))
16931 (defun org-make-tdiff-string (y d h m)
16934 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
16936 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
16938 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
16940 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
16942 (apply 'format fmt (nreverse l))))
16944 (defun org-time-string-to-time (s &optional buffer pos)
16945 "Convert a timestamp string into internal time."
16946 (condition-case errdata
16947 (apply 'encode-time (org-parse-time-string s))
16948 (error (error "Bad timestamp `%s'%s\nError was: %s"
16949 s (if (not (and buffer pos))
16951 (format " at %d in buffer `%s'" pos buffer))
16954 (defun org-time-string-to-seconds (s)
16955 "Convert a timestamp string to a number of seconds."
16956 (org-float-time (org-time-string-to-time s)))
16958 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
16959 "Convert a time stamp to an absolute day number.
16960 If there is a specifier for a cyclic time stamp, get the closest
16962 PREFER and SHOW-ALL are passed through to `org-closest-date'.
16963 The variable `date' is bound by the calendar when this is called."
16965 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
16966 (if (org-diary-sexp-entry (match-string 1 s) "" date)
16969 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
16970 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
16971 (time-to-days (current-time))) (match-string 0 s)
16974 (condition-case errdata
16975 (apply 'encode-time (org-parse-time-string s))
16976 (error (error "Bad timestamp `%s'%s\nError was: %s"
16977 s (if (not (and buffer pos))
16979 (format " at %d in buffer `%s'" pos buffer))
16980 (cdr errdata))))))))
16982 (defun org-days-to-iso-week (days)
16983 "Return the iso week number."
16985 (car (calendar-iso-from-absolute days)))
16987 (defun org-small-year-to-year (year)
16988 "Convert 2-digit years into 4-digit years.
16989 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
16990 The year 2000 cannot be abbreviated. Any year larger than 99
16991 is returned unchanged."
16993 (setq year (+ 2000 year))
16995 (setq year (+ 1900 year))))
16998 (defun org-time-from-absolute (d)
16999 "Return the time corresponding to date D.
17000 D may be an absolute day number, or a calendar-type list (month day year)."
17001 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
17002 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
17004 (defun org-calendar-holiday ()
17005 "List of holidays, for Diary display in Org-mode."
17006 (require 'holidays)
17008 (if (fboundp 'calendar-check-holidays)
17009 'calendar-check-holidays 'check-calendar-holidays) date)))
17010 (if hl (mapconcat 'identity hl "; "))))
17012 (defun org-diary-sexp-entry (sexp entry date)
17013 "Process a SEXP diary ENTRY for DATE."
17014 (require 'diary-lib)
17015 (let ((result (if calendar-debug-sexp
17016 (let ((stack-trace-on-error t))
17017 (eval (car (read-from-string sexp))))
17018 (condition-case nil
17019 (eval (car (read-from-string sexp)))
17022 (message "Bad sexp at line %d in %s: %s"
17024 (buffer-file-name) sexp)
17026 (cond ((stringp result) (split-string result "; "))
17027 ((and (consp result)
17028 (not (consp (cdr result)))
17029 (stringp (cdr result))) (cdr result))
17030 ((and (consp result)
17031 (stringp (car result))) result)
17034 (defun org-diary-to-ical-string (frombuf)
17035 "Get iCalendar entries from diary entries in buffer FROMBUF.
17036 This uses the icalendar.el library."
17037 (let* ((tmpdir (if (featurep 'xemacs)
17039 temporary-file-directory))
17040 (tmpfile (make-temp-name
17041 (expand-file-name "orgics" tmpdir)))
17043 (with-current-buffer frombuf
17044 (icalendar-export-region (point-min) (point-max) tmpfile)
17045 (setq buf (find-buffer-visiting tmpfile))
17047 (goto-char (point-min))
17048 (if (re-search-forward "^BEGIN:VEVENT" nil t)
17049 (setq b (match-beginning 0)))
17050 (goto-char (point-max))
17051 (if (re-search-backward "^END:VEVENT" nil t)
17052 (setq e (match-end 0)))
17053 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
17055 (delete-file tmpfile)
17058 (defun org-closest-date (start current change prefer show-all)
17059 "Find the date closest to CURRENT that is consistent with START and CHANGE.
17060 When PREFER is `past', return a date that is either CURRENT or past.
17061 When PREFER is `future', return a date that is either CURRENT or future.
17062 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
17063 ;; Make the proper lists from the dates
17065 (let ((a1 '(("h" . hour)
17070 (shour (nth 2 (org-parse-time-string start)))
17071 dn dw sday cday n1 n2 n0
17072 d m y y1 y2 date1 date2 nmonths nm ny m2)
17074 (setq start (org-date-to-gregorian start)
17075 current (org-date-to-gregorian
17078 (time-to-days (current-time))))
17079 sday (calendar-absolute-from-gregorian start)
17080 cday (calendar-absolute-from-gregorian current))
17082 (if (<= cday sday) (throw 'exit sday))
17084 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
17085 (setq dn (string-to-number (match-string 1 change))
17086 dw (cdr (assoc (match-string 2 change) a1)))
17087 (user-error "Invalid change specifier: %s" change))
17088 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
17091 (let ((missing-hours
17092 (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
17094 (setq n1 (if (zerop missing-hours) cday
17095 (- cday (1+ (floor (/ missing-hours 24)))))
17096 n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
17098 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
17101 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
17102 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
17103 (setq date1 (list m d y1)
17104 n1 (calendar-absolute-from-gregorian date1)
17105 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
17106 n2 (calendar-absolute-from-gregorian date2)))
17108 ;; approx number of month between the two dates
17109 (setq nmonths (floor (/ (- cday sday) 30.436875)))
17110 ;; How often does dn fit in there?
17111 (setq d (nth 1 start) m (car start) y (nth 2 start)
17112 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
17114 ny (floor (/ m 12))
17117 (while (> m 12) (setq m (- m 12) y (1+ y)))
17118 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
17119 (setq m2 (+ m dn) y2 y)
17120 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
17121 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
17122 (while (<= n2 cday)
17123 (setq n1 n2 m m2 y y2)
17124 (setq m2 (+ m dn) y2 y)
17125 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
17126 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
17127 ;; Make sure n1 is the earlier date
17128 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
17131 ((eq prefer 'past) (if (= cday n2) n2 n1))
17132 ((eq prefer 'future) (if (= cday n1) n1 n2))
17133 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
17135 ((eq prefer 'past) (if (= cday n2) n2 n1))
17136 ((eq prefer 'future) (if (= cday n1) n1 n2))
17137 (t (if (= cday n1) n1 n2)))))))
17139 (defun org-date-to-gregorian (date)
17140 "Turn any specification of DATE into a Gregorian date for the calendar."
17141 (cond ((integerp date) (calendar-gregorian-from-absolute date))
17142 ((and (listp date) (= (length date) 3)) date)
17144 (setq date (org-parse-time-string date))
17145 (list (nth 4 date) (nth 3 date) (nth 5 date)))
17147 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
17149 (defun org-parse-time-string (s &optional nodefault)
17150 "Parse the standard Org-mode time string.
17151 This should be a lot faster than the normal `parse-time-string'.
17152 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
17153 hour and minute fields will be nil if not given."
17154 (cond ((string-match org-ts-regexp0 s)
17156 (if (or (match-beginning 8) (not nodefault))
17157 (string-to-number (or (match-string 8 s) "0")))
17158 (if (or (match-beginning 7) (not nodefault))
17159 (string-to-number (or (match-string 7 s) "0")))
17160 (string-to-number (match-string 4 s))
17161 (string-to-number (match-string 3 s))
17162 (string-to-number (match-string 2 s))
17164 ((string-match "^<[^>]+>$" s)
17165 (decode-time (seconds-to-time (org-matcher-time s))))
17166 (t (error "Not a standard Org-mode time string: %s" s))))
17168 (defun org-timestamp-up (&optional arg)
17169 "Increase the date item at the cursor by one.
17170 If the cursor is on the year, change the year. If it is on the month,
17171 the day or the time, change that.
17172 With prefix ARG, change by that many units."
17174 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
17176 (defun org-timestamp-down (&optional arg)
17177 "Decrease the date item at the cursor by one.
17178 If the cursor is on the year, change the year. If it is on the month,
17179 the day or the time, change that.
17180 With prefix ARG, change by that many units."
17182 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
17184 (defun org-timestamp-up-day (&optional arg)
17185 "Increase the date in the time stamp by one day.
17186 With prefix ARG, change that many days."
17188 (if (and (not (org-at-timestamp-p t))
17189 (org-at-heading-p))
17191 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
17193 (defun org-timestamp-down-day (&optional arg)
17194 "Decrease the date in the time stamp by one day.
17195 With prefix ARG, change that many days."
17197 (if (and (not (org-at-timestamp-p t))
17198 (org-at-heading-p))
17200 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
17202 (defun org-at-timestamp-p (&optional inactive-ok)
17203 "Determine if the cursor is in or at a timestamp."
17205 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
17207 (ans (or (looking-at tsr)
17209 (skip-chars-backward "^[<\n\r\t")
17210 (if (> (point) (point-min)) (backward-char 1))
17211 (and (looking-at tsr)
17212 (> (- (match-end 0) pos) -1))))))
17214 (boundp 'org-ts-what)
17217 ((= pos (match-beginning 0)) 'bracket)
17218 ;; Point is considered to be "on the bracket" whether
17219 ;; it's really on it or right after it.
17220 ((= pos (1- (match-end 0))) 'bracket)
17221 ((= pos (match-end 0)) 'after)
17222 ((org-pos-in-match-range pos 2) 'year)
17223 ((org-pos-in-match-range pos 3) 'month)
17224 ((org-pos-in-match-range pos 7) 'hour)
17225 ((org-pos-in-match-range pos 8) 'minute)
17226 ((or (org-pos-in-match-range pos 4)
17227 (org-pos-in-match-range pos 5)) 'day)
17228 ((and (> pos (or (match-end 8) (match-end 5)))
17229 (< pos (match-end 0)))
17230 (- pos (or (match-end 8) (match-end 5))))
17234 (defun org-toggle-timestamp-type ()
17235 "Toggle the type (<active> or [inactive]) of a time stamp."
17237 (when (org-at-timestamp-p t)
17238 (let ((beg (match-beginning 0)) (end (match-end 0))
17239 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
17242 (while (re-search-forward "[][<>]" end t)
17243 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
17245 (message "Timestamp is now %sactive"
17246 (if (equal (char-after beg) ?<) "" "in")))))
17248 (defun org-at-clock-log-p nil
17249 "Is the cursor on the clock log line?"
17251 (move-beginning-of-line 1)
17252 (looking-at "^[ \t]*CLOCK:")))
17254 (defvar org-clock-history) ; defined in org-clock.el
17255 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
17256 (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
17257 "Change the date in the time stamp at point.
17258 The date will be changed by N times WHAT. WHAT can be `day', `month',
17259 `year', `minute', `second'. If WHAT is not given, the cursor position
17260 in the timestamp determines what will be changed.
17261 When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
17262 (let ((origin (point)) origin-cat
17264 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
17267 ts time time0 fixnext clrgx)
17268 (if (not (org-at-timestamp-p t))
17269 (user-error "Not at a timestamp"))
17270 (if (and (not what) (eq org-ts-what 'bracket))
17271 (org-toggle-timestamp-type)
17272 ;; Point isn't on brackets. Remember the part of the time-stamp
17273 ;; the point was in. Indeed, size of time-stamps may change,
17274 ;; but point must be kept in the same category nonetheless.
17275 (setq origin-cat org-ts-what)
17276 (if (and (not what) (not (eq org-ts-what 'day))
17277 org-display-custom-times
17278 (get-text-property (point) 'display)
17279 (not (get-text-property (1- (point)) 'display)))
17280 (setq org-ts-what 'day))
17281 (setq org-ts-what (or what org-ts-what)
17282 inactive (= (char-after (match-beginning 0)) ?\[)
17283 ts (match-string 0))
17285 (when (string-match
17286 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
17288 (setq extra (match-string 1 ts))
17289 (if suppress-tmp-delay
17290 (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
17291 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
17293 (setq time0 (org-parse-time-string ts))
17295 (eq org-ts-what 'minute)
17296 (not current-prefix-arg))
17297 ;; This looks like s-up and s-down. Change by one rounding step.
17298 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
17299 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
17300 (setcar (cdr time0) (+ (nth 1 time0)
17301 (if (> n 0) (- rem) (- dm rem))))))
17303 (encode-time (or (car time0) 0)
17304 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
17305 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
17306 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
17307 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
17308 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
17310 (when (and (member org-ts-what '(hour minute))
17312 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
17313 (setq extra (org-modify-ts-extra
17315 (if (eq org-ts-what 'hour) 2 5)
17317 (when (integerp org-ts-what)
17318 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
17319 (if (eq what 'calendar)
17320 (let ((cal-date (org-get-date-from-calendar)))
17321 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
17322 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
17323 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
17324 (setcar time0 (or (car time0) 0))
17325 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
17326 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
17327 (setq time (apply 'encode-time time0))))
17328 ;; Insert the new time-stamp, and ensure point stays in the same
17329 ;; category as before (i.e. not after the last position in that
17331 (let ((pos (point)))
17332 ;; Stay before inserted string. `save-excursion' is of no use.
17333 (setq org-last-changed-timestamp
17334 (org-insert-time-stamp time with-hm inactive nil nil extra))
17337 (looking-at org-ts-regexp3)
17339 ;; `day' category ends before `hour' if any, or at
17340 ;; the end of the day name.
17341 ((eq origin-cat 'day)
17342 (min (or (match-beginning 7) (1- (match-end 5))) origin))
17343 ((eq origin-cat 'hour) (min (match-end 7) origin))
17344 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
17345 ((integerp origin-cat) (min (1- (match-end 0)) origin))
17346 ;; `year' and `month' have both fixed size: point
17347 ;; couldn't have moved into another part.
17349 ;; Update clock if on a CLOCK line.
17350 (org-clock-update-time-maybe)
17351 ;; Maybe adjust the closest clock in `org-clock-history'
17352 (when org-clock-adjust-closest
17353 (if (not (and (org-at-clock-log-p)
17354 (< 1 (length (delq nil (mapcar 'marker-position
17355 org-clock-history))))))
17356 (message "No clock to adjust")
17357 (cond ((save-excursion ; fix previous clock?
17358 (re-search-backward org-ts-regexp0 nil t)
17359 (org-looking-back (concat org-clock-string " \\[")))
17360 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
17361 ((save-excursion ; fix next clock?
17362 (re-search-backward org-ts-regexp0 nil t)
17363 (looking-at (concat org-ts-regexp0 "\\] =>")))
17364 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
17365 (save-window-excursion
17366 ;; Find closest clock to point, adjust the previous/next one in history
17367 (let* ((p (save-excursion (org-back-to-heading t)))
17368 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
17370 (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
17371 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
17373 (message "No clock to adjust")
17375 (org-goto-marker-or-bmk clfixpos)
17377 (when (re-search-forward clrgx nil t)
17378 (goto-char (match-beginning 1))
17379 (let (org-clock-adjust-closest)
17380 (org-timestamp-change n org-ts-what updown))
17381 (message "Clock adjusted in %s for heading: %s"
17382 (file-name-nondirectory (buffer-file-name))
17383 (org-get-heading t t)))))))))
17384 ;; Try to recenter the calendar window, if any.
17385 (if (and org-calendar-follow-timestamp-change
17386 (get-buffer-window "*Calendar*" t)
17387 (memq org-ts-what '(day month year)))
17388 (org-recenter-calendar (time-to-days time))))))
17390 (defun org-modify-ts-extra (s pos n dm)
17391 "Change the different parts of the lead-time and repeat fields in timestamp."
17392 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
17394 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
17396 ((or (org-pos-in-match-range pos 2)
17397 (org-pos-in-match-range pos 3))
17398 (setq m (string-to-number (match-string 3 s))
17399 h (string-to-number (match-string 2 s)))
17400 (if (org-pos-in-match-range pos 2)
17402 (setq n (* dm (org-no-warnings (signum n))))
17403 (when (not (= 0 (setq rem (% m dm))))
17404 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
17406 (if (< m 0) (setq m (+ m 60) h (1- h)))
17407 (if (> m 59) (setq m (- m 60) h (1+ h)))
17408 (setq h (min 24 (max 0 h)))
17409 (setq ng 1 new (format "-%02d:%02d" h m)))
17410 ((org-pos-in-match-range pos 6)
17411 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
17412 ((org-pos-in-match-range pos 5)
17413 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
17415 ((org-pos-in-match-range pos 9)
17416 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
17417 ((org-pos-in-match-range pos 8)
17418 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
17422 (substring s 0 (match-beginning ng))
17424 (substring s (match-end ng))))))
17427 (defun org-recenter-calendar (date)
17428 "If the calendar is visible, recenter it to DATE."
17429 (let ((cwin (get-buffer-window "*Calendar*" t)))
17431 (let ((calendar-move-hook nil))
17432 (with-selected-window cwin
17433 (calendar-goto-date (if (listp date) date
17434 (calendar-gregorian-from-absolute date))))))))
17436 (defun org-goto-calendar (&optional arg)
17437 "Go to the Emacs calendar at the current date.
17438 If there is a time stamp in the current line, go to that date.
17439 A prefix ARG can be used to force the current date."
17441 (let ((tsr org-ts-regexp) diff
17442 (calendar-move-hook nil)
17443 (calendar-view-holidays-initially-flag nil)
17444 (calendar-view-diary-initially-flag nil))
17445 (if (or (org-at-timestamp-p)
17447 (beginning-of-line 1)
17448 (looking-at (concat ".*" tsr))))
17449 (let ((d1 (time-to-days (current-time)))
17451 (org-time-string-to-time (match-string 1)))))
17452 (setq diff (- d2 d1))))
17454 (calendar-goto-today)
17455 (if (and diff (not arg)) (calendar-forward-day diff))))
17457 (defun org-get-date-from-calendar ()
17458 "Return a list (month day year) of date at point in calendar."
17459 (with-current-buffer "*Calendar*"
17461 (calendar-cursor-to-date))))
17463 (defun org-date-from-calendar ()
17464 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
17465 If there is already a time stamp at the cursor position, update it."
17467 (if (org-at-timestamp-p t)
17468 (org-timestamp-change 0 'calendar)
17469 (let ((cal-date (org-get-date-from-calendar)))
17470 (org-insert-time-stamp
17471 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
17473 (defcustom org-effort-durations
17476 ("w" . ,(* 60 8 5))
17477 ("m" . ,(* 60 8 5 4))
17478 ("y" . ,(* 60 8 5 40)))
17479 "Conversion factor to minutes for an effort modifier.
17481 Each entry has the form (MODIFIER . MINUTES).
17483 In an effort string, a number followed by MODIFIER is multiplied
17484 by the specified number of MINUTES to obtain an effort in
17487 For example, if the value of this variable is ((\"hours\" . 60)), then an
17488 effort string \"2hours\" is equivalent to 120 minutes."
17491 :type '(alist :key-type (string :tag "Modifier")
17492 :value-type (number :tag "Minutes")))
17494 (defun org-minutes-to-clocksum-string (m)
17495 "Format number of minutes as a clocksum string.
17496 The format is determined by `org-time-clocksum-format',
17497 `org-time-clocksum-use-fractional' and
17498 `org-time-clocksum-fractional-format' and
17499 `org-time-clocksum-use-effort-durations'."
17500 (let ((clocksum "")
17501 (m (round m)) ; Don't allow fractions of minutes
17503 (setq h (if org-time-clocksum-use-effort-durations
17504 (cdr (assoc "h" org-effort-durations)) 60)
17505 d (if org-time-clocksum-use-effort-durations
17506 (/ (cdr (assoc "d" org-effort-durations)) h) 24)
17507 w (if org-time-clocksum-use-effort-durations
17508 (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
17509 mo (if org-time-clocksum-use-effort-durations
17510 (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
17511 y (if org-time-clocksum-use-effort-durations
17512 (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
17513 ;; fractional format
17514 (if org-time-clocksum-use-fractional
17516 ;; single format string
17517 ((stringp org-time-clocksum-fractional-format)
17518 (format org-time-clocksum-fractional-format (/ m (float h))))
17519 ;; choice of fractional formats for different time units
17520 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
17521 (> (/ (truncate m) (* y d h)) 0))
17522 (format fmt (/ m (* y d (float h)))))
17523 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
17524 (> (/ (truncate m) (* mo d h)) 0))
17525 (format fmt (/ m (* mo d (float h)))))
17526 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
17527 (> (/ (truncate m) (* w d h)) 0))
17528 (format fmt (/ m (* w d (float h)))))
17529 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
17530 (> (/ (truncate m) (* d h)) 0))
17531 (format fmt (/ m (* d (float h)))))
17532 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
17533 (> (/ (truncate m) h) 0))
17534 (format fmt (/ m (float h))))
17535 ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
17537 ;; fall back to smallest time unit with a format
17538 ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
17539 (format fmt (/ m (float h))))
17540 ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
17541 (format fmt (/ m (* d (float h)))))
17542 ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
17543 (format fmt (/ m (* w d (float h)))))
17544 ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
17545 (format fmt (/ m (* mo d (float h)))))
17546 ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
17547 (format fmt (/ m (* y d (float h))))))
17548 ;; standard (non-fractional) format, with single format string
17549 (if (stringp org-time-clocksum-format)
17550 (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
17551 ;; separate formats components
17552 (and (setq fmt (plist-get org-time-clocksum-format :years))
17553 (or (> (setq n (/ (truncate m) (* y d h))) 0)
17554 (plist-get org-time-clocksum-format :require-years))
17555 (setq clocksum (concat clocksum (format fmt n))
17556 m (- m (* n y d h))))
17557 (and (setq fmt (plist-get org-time-clocksum-format :months))
17558 (or (> (setq n (/ (truncate m) (* mo d h))) 0)
17559 (plist-get org-time-clocksum-format :require-months))
17560 (setq clocksum (concat clocksum (format fmt n))
17561 m (- m (* n mo d h))))
17562 (and (setq fmt (plist-get org-time-clocksum-format :weeks))
17563 (or (> (setq n (/ (truncate m) (* w d h))) 0)
17564 (plist-get org-time-clocksum-format :require-weeks))
17565 (setq clocksum (concat clocksum (format fmt n))
17566 m (- m (* n w d h))))
17567 (and (setq fmt (plist-get org-time-clocksum-format :days))
17568 (or (> (setq n (/ (truncate m) (* d h))) 0)
17569 (plist-get org-time-clocksum-format :require-days))
17570 (setq clocksum (concat clocksum (format fmt n))
17571 m (- m (* n d h))))
17572 (and (setq fmt (plist-get org-time-clocksum-format :hours))
17573 (or (> (setq n (/ (truncate m) h)) 0)
17574 (plist-get org-time-clocksum-format :require-hours))
17575 (setq clocksum (concat clocksum (format fmt n))
17577 (and (setq fmt (plist-get org-time-clocksum-format :minutes))
17578 (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
17579 (setq clocksum (concat clocksum (format fmt m))))
17580 ;; return formatted time duration
17583 (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
17584 (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
17585 "Org mode version 8.0")
17587 (defun org-hours-to-clocksum-string (n)
17588 (org-minutes-to-clocksum-string (* n 60)))
17590 (defun org-hh:mm-string-to-minutes (s)
17591 "Convert a string H:MM to a number of minutes.
17592 If the string is just a number, interpret it as minutes.
17593 In fact, the first hh:mm or number in the string will be taken,
17594 there can be extra stuff in the string.
17595 If no number is found, the return value is 0."
17598 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
17599 (+ (* (string-to-number (match-string 1 s)) 60)
17600 (string-to-number (match-string 2 s))))
17601 ((string-match "\\([0-9]+\\)" s)
17602 (string-to-number (match-string 1 s)))
17605 (defcustom org-image-actual-width t
17606 "Should we use the actual width of images when inlining them?
17608 When set to `t', always use the image width.
17610 When set to a number, use imagemagick (when available) to set
17611 the image's width to this value.
17613 When set to a number in a list, try to get the width from any
17614 #+ATTR.* keyword if it matches a width specification like
17616 #+ATTR_HTML: :width 300px
17618 and fall back on that number if none is found.
17620 When set to nil, try to get the width from an #+ATTR.* keyword
17621 and fall back on the original width if none is found.
17623 This requires Emacs >= 24.1, build with imagemagick support."
17624 :group 'org-appearance
17626 :package-version '(Org . "8.0")
17628 (const :tag "Use the image width" t)
17629 (integer :tag "Use a number of pixels")
17630 (list :tag "Use #+ATTR* or a number of pixels" (integer))
17631 (const :tag "Use #+ATTR* or don't resize" nil)))
17633 (defcustom org-agenda-inhibit-startup nil
17634 "Inhibit startup when preparing agenda buffers.
17635 When this variable is `t' (the default), the initialization of
17636 the Org agenda buffers is inhibited: e.g. the visibility state
17637 is not set, the tables are not re-aligned, etc."
17640 :group 'org-agenda)
17642 (defun org-duration-string-to-minutes (s &optional output-to-string)
17643 "Convert a duration string S to minutes.
17645 A bare number is interpreted as minutes, modifiers can be set by
17646 customizing `org-effort-durations' (which see).
17648 Entries containing a colon are interpreted as H:MM by
17649 `org-hh:mm-string-to-minutes'."
17651 (re (concat "\\([0-9.]+\\) *\\("
17652 (regexp-opt (mapcar 'car org-effort-durations))
17654 (while (string-match re s)
17655 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
17656 (string-to-number (match-string 1 s))))
17657 (setq s (replace-match "" nil t s)))
17658 (setq result (floor result))
17659 (incf result (org-hh:mm-string-to-minutes s))
17660 (if output-to-string (number-to-string result) result)))
17664 (defun org-save-all-org-buffers ()
17665 "Save all Org-mode buffers without user confirmation."
17667 (message "Saving all Org-mode buffers...")
17668 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
17669 (when (featurep 'org-id) (org-id-locations-save))
17670 (message "Saving all Org-mode buffers... done"))
17672 (defun org-revert-all-org-buffers ()
17673 "Revert all Org-mode buffers.
17674 Prompt for confirmation when there are unsaved changes.
17675 Be sure you know what you are doing before letting this function
17676 overwrite your changes.
17678 This function is useful in a setup where one tracks org files
17679 with a version control system, to revert on one machine after pulling
17680 changes from another. I believe the procedure must be like this:
17682 1. M-x org-save-all-org-buffers
17683 2. Pull changes from the other machine, resolve conflicts
17684 3. M-x org-revert-all-org-buffers"
17686 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
17687 (user-error "Abort"))
17689 (save-window-excursion
17692 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
17693 (with-current-buffer b buffer-file-name))
17694 (org-pop-to-buffer-same-window b)
17695 (revert-buffer t 'no-confirm)))
17697 (when (and (featurep 'org-id) org-id-track-globally)
17698 (org-id-locations-load)))))
17703 (defun org-switchb (&optional arg)
17704 "Switch between Org buffers.
17705 With one prefix argument, restrict available buffers to files.
17706 With two prefix arguments, restrict available buffers to agenda files.
17708 Defaults to `iswitchb' for buffer name completion.
17709 Set `org-completion-use-ido' to make it use ido instead."
17711 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
17712 ((equal arg '(16)) (org-buffer-list 'agenda))
17713 (t (org-buffer-list))))
17714 (org-completion-use-iswitchb org-completion-use-iswitchb)
17715 (org-completion-use-ido org-completion-use-ido))
17716 (unless (or org-completion-use-ido org-completion-use-iswitchb)
17717 (setq org-completion-use-iswitchb t))
17718 (org-pop-to-buffer-same-window
17719 (org-icompleting-read "Org buffer: "
17720 (mapcar 'list (mapcar 'buffer-name blist))
17723 ;;; Define some older names previously used for this functionality
17725 (defalias 'org-ido-switchb 'org-switchb)
17727 (defalias 'org-iswitchb 'org-switchb)
17729 (defun org-buffer-list (&optional predicate exclude-tmp)
17730 "Return a list of Org buffers.
17731 PREDICATE can be `export', `files' or `agenda'.
17733 export restrict the list to Export buffers.
17734 files restrict the list to buffers visiting Org files.
17735 agenda restrict the list to buffers visiting agenda files.
17737 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
17739 (agenda-files (and (eq predicate 'agenda)
17740 (mapcar 'file-truename (org-agenda-files t))))
17743 ((eq predicate 'files)
17744 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
17745 ((eq predicate 'export)
17746 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
17747 ((eq predicate 'agenda)
17749 (with-current-buffer b
17750 (and (derived-mode-p 'org-mode)
17751 (setq bfn (buffer-file-name b))
17752 (member (file-truename bfn) agenda-files)))))
17753 (t (lambda (b) (with-current-buffer b
17754 (or (derived-mode-p 'org-mode)
17755 (string-match "\*Org .*Export"
17756 (buffer-name b)))))))))
17760 (if (and (funcall filter b)
17761 (or (not exclude-tmp)
17762 (not (string-match "tmp" (buffer-name b)))))
17767 (defun org-agenda-files (&optional unrestricted archives)
17768 "Get the list of agenda files.
17769 Optional UNRESTRICTED means return the full list even if a restriction
17770 is currently in place.
17771 When ARCHIVES is t, include all archive files that are really being
17772 used by the agenda files. If ARCHIVE is `ifmode', do this only if
17773 `org-agenda-archives-mode' is t."
17776 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
17777 ((stringp org-agenda-files) (org-read-agenda-file-list))
17778 ((listp org-agenda-files) org-agenda-files)
17779 (t (error "Invalid value of `org-agenda-files'")))))
17780 (setq files (apply 'append
17781 (mapcar (lambda (f)
17782 (if (file-directory-p f)
17784 f t org-agenda-file-regexp)
17787 (when org-agenda-skip-unavailable-files
17788 (setq files (delq nil
17791 (and (file-readable-p file) file)))
17793 (when (or (eq archives t)
17794 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
17795 (setq files (org-add-archive-files files)))
17798 (defun org-agenda-file-p (&optional file)
17799 "Return non-nil, if FILE is an agenda file.
17800 If FILE is omitted, use the file associated with the current
17802 (member (or file (buffer-file-name))
17803 (org-agenda-files t)))
17805 (defun org-edit-agenda-file-list ()
17806 "Edit the list of agenda files.
17807 Depending on setup, this either uses customize to edit the variable
17808 `org-agenda-files', or it visits the file that is holding the list. In the
17809 latter case, the buffer is set up in a way that saving it automatically kills
17810 the buffer and restores the previous window configuration."
17812 (if (stringp org-agenda-files)
17813 (let ((cw (current-window-configuration)))
17814 (find-file org-agenda-files)
17815 (org-set-local 'org-window-configuration cw)
17816 (org-add-hook 'after-save-hook
17818 (set-window-configuration
17819 (prog1 org-window-configuration
17820 (kill-buffer (current-buffer))))
17821 (org-install-agenda-files-menu)
17822 (message "New agenda file list installed"))
17824 (message "%s" (substitute-command-keys
17825 "Edit list and finish with \\[save-buffer]")))
17826 (customize-variable 'org-agenda-files)))
17828 (defun org-store-new-agenda-file-list (list)
17829 "Set new value for the agenda file list and save it correctly."
17830 (if (stringp org-agenda-files)
17831 (let ((fe (org-read-agenda-file-list t)) b u)
17832 (while (setq b (find-buffer-visiting org-agenda-files))
17834 (with-temp-file org-agenda-files
17837 (lambda (f) ;; Keep un-expanded entries.
17838 (if (setq u (assoc f fe))
17843 (let ((org-mode-hook nil) (org-inhibit-startup t)
17844 (org-insert-mode-line-in-empty-file nil))
17845 (setq org-agenda-files list)
17846 (customize-save-variable 'org-agenda-files org-agenda-files))))
17848 (defun org-read-agenda-file-list (&optional pair-with-expansion)
17849 "Read the list of agenda files from a file.
17850 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
17851 filenames, used by `org-store-new-agenda-file-list' to write back
17852 un-expanded file names."
17853 (when (file-directory-p org-agenda-files)
17854 (error "`org-agenda-files' cannot be a single directory"))
17855 (when (stringp org-agenda-files)
17857 (insert-file-contents org-agenda-files)
17860 (let ((e (expand-file-name (substitute-in-file-name f)
17862 (if pair-with-expansion
17865 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
17868 (defun org-cycle-agenda-files ()
17869 "Cycle through the files in `org-agenda-files'.
17870 If the current buffer visits an agenda file, find the next one in the list.
17871 If the current buffer does not, find the first agenda file."
17873 (let* ((fs (org-agenda-files t))
17874 (files (append fs (list (car fs))))
17875 (tcf (if buffer-file-name (file-truename buffer-file-name)))
17877 (unless files (user-error "No agenda files"))
17879 (while (setq file (pop files))
17880 (if (equal (file-truename file) tcf)
17882 (find-file (car files))
17884 (find-file (car fs)))
17885 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
17887 (defun org-agenda-file-to-front (&optional to-end)
17888 "Move/add the current file to the top of the agenda file list.
17889 If the file is not present in the list, it is added to the front. If it is
17890 present, it is moved there. With optional argument TO-END, add/move to the
17893 (let ((org-agenda-skip-unavailable-files nil)
17894 (file-alist (mapcar (lambda (x)
17895 (cons (file-truename x) x))
17896 (org-agenda-files t)))
17897 (ctf (file-truename
17898 (or buffer-file-name
17899 (user-error "Please save the current buffer to a file"))))
17901 (setq x (assoc ctf file-alist) had x)
17903 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
17905 (setq file-alist (append (delq x file-alist) (list x)))
17906 (setq file-alist (cons x (delq x file-alist))))
17907 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
17908 (org-install-agenda-files-menu)
17909 (message "File %s to %s of agenda file list"
17910 (if had "moved" "added") (if to-end "end" "front"))))
17912 (defun org-remove-file (&optional file)
17913 "Remove current file from the list of files in variable `org-agenda-files'.
17914 These are the files which are being checked for agenda entries.
17915 Optional argument FILE means use this file instead of the current."
17917 (let* ((org-agenda-skip-unavailable-files nil)
17918 (file (or file buffer-file-name
17919 (user-error "Current buffer does not visit a file")))
17920 (true-file (file-truename file))
17921 (afile (abbreviate-file-name file))
17922 (files (delq nil (mapcar
17924 (if (equal true-file
17927 (org-agenda-files t)))))
17928 (if (not (= (length files) (length (org-agenda-files t))))
17930 (org-store-new-agenda-file-list files)
17931 (org-install-agenda-files-menu)
17932 (message "Removed file: %s" afile))
17933 (message "File was not in list: %s (not removed)" afile))))
17935 (defun org-file-menu-entry (file)
17936 (vector file (list 'find-file file) t))
17938 (defun org-check-agenda-file (file)
17939 "Make sure FILE exists. If not, ask user what to do."
17940 (when (not (file-exists-p file))
17941 (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
17942 (abbreviate-file-name file))
17943 (let ((r (downcase (read-char-exclusive))))
17946 (org-remove-file file)
17947 (throw 'nextfile t))
17948 (t (error "Abort"))))))
17950 (defun org-get-agenda-file-buffer (file)
17951 "Get a buffer visiting FILE. If the buffer needs to be created, add
17952 it to the list of buffers which might be released later."
17953 (let ((buf (org-find-base-buffer-visiting file)))
17955 buf ; just return it
17956 ;; Make a new buffer and remember it
17957 (setq buf (find-file-noselect file))
17958 (if buf (push buf org-agenda-new-buffers))
17961 (defun org-release-buffers (blist)
17962 "Release all buffers in list, asking the user for confirmation when needed.
17963 When a buffer is unmodified, it is just killed. When modified, it is saved
17964 \(if the user agrees) and then killed."
17966 (while (setq buf (pop blist))
17967 (setq file (buffer-file-name buf))
17968 (when (and (buffer-modified-p buf)
17970 (y-or-n-p (format "Save file %s? " file)))
17971 (with-current-buffer buf (save-buffer)))
17972 (kill-buffer buf))))
17974 (defun org-agenda-prepare-buffers (files)
17975 "Create buffers for all agenda files, protect archived trees and comments."
17977 (let ((pa '(:org-archived t))
17978 (pc '(:org-comment t))
17979 (pall '(:org-archived t :org-comment t))
17980 (inhibit-read-only t)
17981 (org-inhibit-startup org-agenda-inhibit-startup)
17982 (rea (concat ":" org-archive-tag ":"))
17984 (setq org-tag-alist-for-agenda nil
17985 org-tag-groups-alist-for-agenda nil)
17988 (while (setq file (pop files))
17992 (org-check-agenda-file file)
17993 (set-buffer (org-get-agenda-file-buffer file)))
17995 (org-set-regexps-and-options-for-tags)
17996 (goto-char (point-min))
17997 (let ((case-fold-search t))
17998 (when (search-forward "#+setupfile" nil t)
17999 ;; Don't set all regexps and options systematically as
18000 ;; this is only run for setting agenda tags from setup
18002 (org-set-regexps-and-options)))
18003 (org-refresh-category-properties)
18004 (org-refresh-properties org-effort-property 'org-effort)
18005 (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
18006 (setq org-todo-keywords-for-agenda
18007 (append org-todo-keywords-for-agenda org-todo-keywords-1))
18008 (setq org-done-keywords-for-agenda
18009 (append org-done-keywords-for-agenda org-done-keywords))
18010 (setq org-todo-keyword-alist-for-agenda
18011 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
18012 (setq org-drawers-for-agenda
18013 (append org-drawers-for-agenda org-drawers))
18014 (setq org-tag-alist-for-agenda
18016 (append org-tag-alist-for-agenda
18018 org-tag-persistent-alist)))
18020 (setq org-tag-groups-alist-for-agenda
18021 (org-uniquify-alist
18022 (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
18023 (org-with-silent-modifications
18025 (remove-text-properties (point-min) (point-max) pall)
18026 (when org-agenda-skip-archived-trees
18027 (goto-char (point-min))
18028 (while (re-search-forward rea nil t)
18029 (if (org-at-heading-p t)
18030 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
18031 (goto-char (point-min))
18032 (setq re (format org-heading-keyword-regexp-format
18033 org-comment-string))
18034 (while (re-search-forward re nil t)
18035 (add-text-properties
18036 (match-beginning 0) (org-end-of-subtree t) pc))))))))
18037 (setq org-todo-keywords-for-agenda
18038 (org-uniquify org-todo-keywords-for-agenda))
18039 (setq org-todo-keyword-alist-for-agenda
18040 (org-uniquify org-todo-keyword-alist-for-agenda))))
18043 ;;;; CDLaTeX minor mode
18045 (defvar org-cdlatex-mode-map (make-sparse-keymap)
18046 "Keymap for the minor `org-cdlatex-mode'.")
18048 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
18049 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
18050 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
18051 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
18052 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
18054 (defvar org-cdlatex-texmathp-advice-is-done nil
18055 "Flag remembering if we have applied the advice to texmathp already.")
18057 (define-minor-mode org-cdlatex-mode
18058 "Toggle the minor `org-cdlatex-mode'.
18059 This mode supports entering LaTeX environment and math in LaTeX fragments
18061 \\{org-cdlatex-mode-map}"
18063 (when org-cdlatex-mode
18065 (run-hooks 'cdlatex-mode-hook)
18066 (cdlatex-compute-tables))
18067 (unless org-cdlatex-texmathp-advice-is-done
18068 (setq org-cdlatex-texmathp-advice-is-done t)
18069 (defadvice texmathp (around org-math-always-on activate)
18070 "Always return t in org-mode buffers.
18071 This is because we want to insert math symbols without dollars even outside
18072 the LaTeX math segments. If Orgmode thinks that point is actually inside
18073 an embedded LaTeX fragment, let texmathp do its job.
18074 \\[org-cdlatex-mode-map]"
18078 ((not (derived-mode-p 'org-mode)) ad-do-it)
18079 ((eq this-command 'cdlatex-math-symbol)
18080 (setq ad-return-value t
18081 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
18083 (let ((p (org-inside-LaTeX-fragment-p)))
18084 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
18085 (setq ad-return-value t
18086 texmathp-why '("Org-mode embedded math" . 0))
18087 (if p ad-do-it)))))))))
18089 (defun turn-on-org-cdlatex ()
18090 "Unconditionally turn on `org-cdlatex-mode'."
18091 (org-cdlatex-mode 1))
18093 (defun org-try-cdlatex-tab ()
18094 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
18095 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
18096 - inside a LaTeX fragment, or
18097 - after the first word in a line, where an abbreviation expansion could
18098 insert a LaTeX environment."
18099 (when org-cdlatex-mode
18101 ;; Before any word on the line: No expansion possible.
18102 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
18103 ;; Just after first word on the line: Expand it. Make sure it
18104 ;; cannot happen on headlines, though.
18106 (skip-chars-backward "a-zA-Z0-9*")
18107 (skip-chars-backward " \t")
18108 (and (bolp) (not (org-at-heading-p))))
18110 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
18112 (defun org-cdlatex-underscore-caret (&optional arg)
18113 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
18114 Revert to the normal definition outside of these fragments."
18116 (if (org-inside-LaTeX-fragment-p)
18117 (call-interactively 'cdlatex-sub-superscript)
18118 (let (org-cdlatex-mode)
18119 (call-interactively (key-binding (vector last-input-event))))))
18121 (defun org-cdlatex-math-modify (&optional arg)
18122 "Execute `cdlatex-math-modify' in LaTeX fragments.
18123 Revert to the normal definition outside of these fragments."
18125 (if (org-inside-LaTeX-fragment-p)
18126 (call-interactively 'cdlatex-math-modify)
18127 (let (org-cdlatex-mode)
18128 (call-interactively (key-binding (vector last-input-event))))))
18132 ;;;; LaTeX fragments
18134 (defvar org-latex-regexps
18135 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
18136 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
18137 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
18138 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
18139 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
18140 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
18141 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
18142 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
18143 "Regular expressions for matching embedded LaTeX.")
18145 (defun org-inside-LaTeX-fragment-p ()
18146 "Test if point is inside a LaTeX fragment.
18147 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
18148 sequence appearing also before point.
18149 Even though the matchers for math are configurable, this function assumes
18150 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
18151 delimiters are skipped when they have been removed by customization.
18152 The return value is nil, or a cons cell with the delimiter and the
18153 position of this delimiter.
18155 This function does a reasonably good job, but can locally be fooled by
18156 for example currency specifications. For example it will assume being in
18157 inline math after \"$22.34\". The LaTeX fragment formatter will only format
18158 fragments that are properly closed, but during editing, we have to live
18159 with the uncertainty caused by missing closing delimiters. This function
18160 looks only before point, not after."
18162 (let ((pos (point))
18163 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
18165 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
18167 dd-on str (start 0) m re)
18170 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
18171 re (nth 1 (assoc "$" org-latex-regexps)))
18172 (while (string-match re str start)
18174 ((= (match-end 0) (length str))
18175 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
18176 ((= (match-end 0) (- (length str) 5))
18178 (t (setq start (match-end 0))))))
18179 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
18181 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
18182 (and (match-beginning 2) (throw 'exit nil))
18184 (while (re-search-backward "\\$\\$" lim t)
18185 (setq dd-on (not dd-on)))
18187 (if dd-on (cons "$$" m))))))
18189 (defun org-inside-latex-macro-p ()
18190 "Is point inside a LaTeX macro or its arguments?"
18193 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
18195 (defvar org-latex-fragment-image-overlays nil
18196 "List of overlays carrying the images of latex fragments.")
18197 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
18199 (defun org-remove-latex-fragment-image-overlays ()
18200 "Remove all overlays with LaTeX fragment images in current buffer."
18201 (mapc 'delete-overlay org-latex-fragment-image-overlays)
18202 (setq org-latex-fragment-image-overlays nil))
18204 (defun org-preview-latex-fragment (&optional subtree)
18205 "Preview the LaTeX fragment at point, or all locally or globally.
18206 If the cursor is in a LaTeX fragment, create the image and overlay
18207 it over the source code. If there is no fragment at point, display
18208 all fragments in the current text, from one headline to the next. With
18209 prefix SUBTREE, display all fragments in the current subtree. With a
18210 double prefix arg \\[universal-argument] \\[universal-argument], or when \
18211 the cursor is before the first headline,
18212 display all fragments in the buffer.
18213 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
18215 (unless buffer-file-name
18216 (user-error "Can't preview LaTeX fragment in a non-file buffer"))
18217 (org-remove-latex-fragment-image-overlays)
18220 (let (beg end at msg)
18222 ((or (equal subtree '(16))
18223 (not (save-excursion
18224 (re-search-backward org-outline-regexp-bol nil t))))
18225 (setq beg (point-min) end (point-max)
18226 msg "Creating images for buffer...%s"))
18227 ((equal subtree '(4))
18228 (org-back-to-heading)
18229 (setq beg (point) end (org-end-of-subtree t)
18230 msg "Creating images for subtree...%s"))
18232 (if (setq at (org-inside-LaTeX-fragment-p))
18233 (goto-char (max (point-min) (- (cdr at) 2)))
18234 (org-back-to-heading))
18235 (setq beg (point) end (progn (outline-next-heading) (point))
18236 msg (if at "Creating image...%s"
18237 "Creating images for entry...%s"))))
18239 (narrow-to-region beg end)
18242 (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
18243 (file-name-nondirectory
18244 buffer-file-name)))
18245 default-directory 'overlays msg at 'forbuffer
18246 org-latex-create-formula-image-program)
18247 (message msg "done. Use `C-c C-c' to remove images.")))))
18249 (defun org-format-latex (prefix &optional dir overlays msg at
18250 forbuffer processing-type)
18251 "Replace LaTeX fragments with links to an image, and produce images.
18252 Some of the options can be changed using the variable
18253 `org-format-latex-options'."
18254 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
18255 (let* ((prefixnodir (file-name-nondirectory prefix))
18256 (absprefix (expand-file-name prefix dir))
18257 (todir (file-name-directory absprefix))
18258 (opt org-format-latex-options)
18259 (optnew org-format-latex-options)
18260 (matchers (plist-get opt :matchers))
18261 (re-list org-latex-regexps)
18262 (cnt 0) txt hash link beg end re e checkdir
18264 m n block-type block linkfile movefile ov)
18265 ;; Check the different regular expressions
18266 (while (setq e (pop re-list))
18267 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
18268 block (if block-type "\n\n" ""))
18269 (when (member m matchers)
18270 (goto-char (point-min))
18271 (while (re-search-forward re nil t)
18272 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
18274 (not (eq (get-char-property (match-beginning n)
18276 'org-latex-overlay))))
18278 ((eq processing-type 'verbatim))
18279 ((eq processing-type 'mathjax)
18280 ;; Prepare for MathJax processing.
18281 (setq string (match-string n))
18282 (when (member m '("$" "$1"))
18284 (delete-region (match-beginning n) (match-end n))
18285 (goto-char (match-beginning n))
18286 (insert (concat "\\(" (substring string 1 -1) "\\)")))))
18287 ((or (eq processing-type 'dvipng)
18288 (eq processing-type 'imagemagick))
18289 ;; Process to an image.
18290 (setq txt (match-string n)
18291 beg (match-beginning n) end (match-end n)
18293 (let ((face (face-at-point))
18294 (fg (plist-get opt :foreground))
18295 (bg (plist-get opt :background))
18296 ;; Ensure full list is printed.
18297 print-length print-level)
18299 ;; Get the colors from the face at point.
18301 (when (eq fg 'auto)
18302 (setq fg (face-attribute face :foreground nil 'default)))
18303 (when (eq bg 'auto)
18304 (setq bg (face-attribute face :background nil 'default)))
18305 (setq optnew (copy-sequence opt))
18306 (plist-put optnew :foreground fg)
18307 (plist-put optnew :background bg))
18308 (setq hash (sha1 (prin1-to-string
18309 (list org-format-latex-header
18310 org-latex-default-packages-alist
18311 org-latex-packages-alist
18312 org-format-latex-options
18313 forbuffer txt fg bg)))
18314 linkfile (format "%s_%s.png" prefix hash)
18315 movefile (format "%s_%s.png" absprefix hash)))
18316 (setq link (concat block "[[file:" linkfile "]]" block))
18317 (if msg (message msg cnt))
18319 (unless checkdir ; Ensure the directory exists.
18321 (or (file-directory-p todir) (make-directory todir t)))
18322 (unless (file-exists-p movefile)
18323 (org-create-formula-image
18324 txt movefile optnew forbuffer processing-type))
18328 (if (eq (overlay-get o 'org-overlay-type)
18329 'org-latex-overlay)
18330 (delete-overlay o)))
18331 (overlays-in beg end))
18332 (setq ov (make-overlay beg end))
18333 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
18334 (if (featurep 'xemacs)
18336 (overlay-put ov 'invisible t)
18339 (make-glyph (vector 'png :file movefile))))
18342 (list 'image :type 'png :file movefile :ascent 'center)))
18343 (push ov org-latex-fragment-image-overlays)
18345 (delete-region beg end)
18346 (insert (org-add-props link
18347 (list 'org-latex-src
18348 (replace-regexp-in-string
18350 'org-latex-src-embed-type
18351 (if block-type 'paragraph 'character))))))
18352 ((eq processing-type 'mathml)
18353 ;; Process to MathML
18354 (unless (save-match-data (org-format-latex-mathml-available-p))
18355 (user-error "LaTeX to MathML converter not configured"))
18356 (setq txt (match-string n)
18357 beg (match-beginning n) end (match-end n)
18359 (if msg (message msg cnt))
18361 (delete-region beg end)
18362 (insert (org-format-latex-as-mathml
18363 txt block-type prefix dir)))
18365 (error "Unknown conversion type %s for LaTeX fragments"
18366 processing-type)))))))))
18368 (defun org-create-math-formula (latex-frag &optional mathml-file)
18369 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
18370 Use `org-latex-to-mathml-convert-command'. If the conversion is
18371 sucessful, return the portion between \"<math...> </math>\"
18372 elements otherwise return nil. When MATHML-FILE is specified,
18373 write the results in to that file. When invoked as an
18374 interactive command, prompt for LATEX-FRAG, with initial value
18375 set to the current active region and echo the results for user
18377 (interactive (list (let ((frag (when (org-region-active-p)
18378 (buffer-substring-no-properties
18379 (region-beginning) (region-end)))))
18380 (read-string "LaTeX Fragment: " frag nil frag))))
18381 (unless latex-frag (error "Invalid LaTeX fragment"))
18382 (let* ((tmp-in-file (file-relative-name
18383 (make-temp-name (expand-file-name "ltxmathml-in"))))
18384 (ignore (write-region latex-frag nil tmp-in-file))
18385 (tmp-out-file (file-relative-name
18386 (make-temp-name (expand-file-name "ltxmathml-out"))))
18388 org-latex-to-mathml-convert-command
18389 `((?j . ,(shell-quote-argument
18390 (expand-file-name org-latex-to-mathml-jar-file)))
18391 (?I . ,(shell-quote-argument tmp-in-file))
18392 (?o . ,(shell-quote-argument tmp-out-file)))))
18393 mathml shell-command-output)
18394 (when (org-called-interactively-p 'any)
18395 (unless (org-format-latex-mathml-available-p)
18396 (user-error "LaTeX to MathML converter not configured")))
18397 (message "Running %s" cmd)
18398 (setq shell-command-output (shell-command-to-string cmd))
18400 (when (file-readable-p tmp-out-file)
18401 (with-current-buffer (find-file-noselect tmp-out-file t)
18402 (goto-char (point-min))
18403 (when (re-search-forward
18406 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
18408 (regexp-quote "</math>")) nil t)
18409 (prog1 (match-string 0) (kill-buffer))))))
18413 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
18415 (write-region mathml nil mathml-file))
18416 (when (org-called-interactively-p 'any)
18418 ((message "LaTeX to MathML conversion failed")
18419 (message shell-command-output)))
18420 (delete-file tmp-in-file)
18421 (when (file-exists-p tmp-out-file)
18422 (delete-file tmp-out-file))
18425 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
18426 prefix &optional dir)
18427 "Use `org-create-math-formula' but check local cache first."
18428 (let* ((absprefix (expand-file-name prefix dir))
18429 (print-length nil) (print-level nil)
18430 (formula-id (concat
18435 org-latex-to-mathml-convert-command)))))
18436 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
18437 (formula-cache-dir (file-name-directory formula-cache)))
18439 (unless (file-directory-p formula-cache-dir)
18440 (make-directory formula-cache-dir t))
18442 (unless (file-exists-p formula-cache)
18443 (org-create-math-formula latex-frag formula-cache))
18445 (if (file-exists-p formula-cache)
18446 ;; Successful conversion. Return the link to MathML file.
18448 (format "[[file:%s]]" (file-relative-name formula-cache dir))
18449 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
18450 'org-latex-src-embed-type (if latex-frag-type
18451 'paragraph 'character)))
18452 ;; Failed conversion. Return the LaTeX fragment verbatim
18455 (defun org-create-formula-image (string tofile options buffer &optional type)
18456 "Create an image from LaTeX source using dvipng or convert.
18457 This function calls either `org-create-formula-image-with-dvipng'
18458 or `org-create-formula-image-with-imagemagick' depending on the
18459 value of `org-latex-create-formula-image-program' or on the value
18460 of the optional TYPE variable.
18462 Note: ultimately these two function should be combined as they
18463 share a good deal of logic."
18464 (org-check-external-command
18465 "latex" "needed to convert LaTeX fragments to images")
18467 (case (or type org-latex-create-formula-image-program)
18469 (org-check-external-command
18470 "dvipng" "needed to convert LaTeX fragments to images")
18471 #'org-create-formula-image-with-dvipng)
18473 (org-check-external-command
18474 "convert" "you need to install imagemagick")
18475 #'org-create-formula-image-with-imagemagick)
18477 "Invalid value of `org-latex-create-formula-image-program'")))
18478 string tofile options buffer))
18480 (declare-function org-export--get-global-options "ox" (&optional backend))
18481 (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
18482 (defun org-create-formula--latex-header ()
18483 "Return LaTeX header appropriate for previewing a LaTeX snippet."
18484 (org-latex-guess-inputenc
18485 (org-splice-latex-header
18486 org-format-latex-header
18487 org-latex-default-packages-alist
18488 org-latex-packages-alist t
18490 (org-combine-plists
18491 (org-export--get-global-options 'latex)
18492 (org-export--get-inbuffer-options 'latex))
18495 ;; This function borrows from Ganesh Swami's latex2png.el
18496 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
18497 "This calls dvipng."
18498 (require 'ox-latex)
18499 (let* ((tmpdir (if (featurep 'xemacs)
18501 temporary-file-directory))
18502 (texfilebase (make-temp-name
18503 (expand-file-name "orgtex" tmpdir)))
18504 (texfile (concat texfilebase ".tex"))
18505 (dvifile (concat texfilebase ".dvi"))
18506 (pngfile (concat texfilebase ".png"))
18507 (fnh (if (featurep 'xemacs)
18508 (font-height (face-font 'default))
18509 (face-attribute 'default :height nil)))
18510 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
18511 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
18512 (fg (or (plist-get options (if buffer :foreground :html-foreground))
18514 (bg (or (plist-get options (if buffer :background :html-background))
18516 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
18517 (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
18518 (if (eq bg 'default) (setq bg (org-dvipng-color :background))
18519 (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
18520 (let ((latex-header (org-create-formula--latex-header)))
18521 (with-temp-file texfile
18522 (insert latex-header)
18523 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
18524 (let ((dir default-directory))
18525 (condition-case nil
18528 (call-process "latex" nil nil nil texfile))
18531 (if (not (file-exists-p dvifile))
18532 (progn (message "Failed to create dvi file from %s" texfile) nil)
18533 (condition-case nil
18534 (if (featurep 'xemacs)
18535 (call-process "dvipng" nil nil nil
18540 (call-process "dvipng" nil nil nil
18543 ;;"-x" scale "-y" scale
18548 (if (not (file-exists-p pngfile))
18549 (if org-format-latex-signal-error
18550 (error "Failed to create png file from %s" texfile)
18551 (message "Failed to create png file from %s" texfile)
18553 ;; Use the requested file name and clean up
18554 (copy-file pngfile tofile 'replace)
18555 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
18556 (if (file-exists-p (concat texfilebase e))
18557 (delete-file (concat texfilebase e))))
18560 (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
18561 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
18562 "This calls convert, which is included into imagemagick."
18563 (require 'ox-latex)
18564 (let* ((tmpdir (if (featurep 'xemacs)
18566 temporary-file-directory))
18567 (texfilebase (make-temp-name
18568 (expand-file-name "orgtex" tmpdir)))
18569 (texfile (concat texfilebase ".tex"))
18570 (pdffile (concat texfilebase ".pdf"))
18571 (pngfile (concat texfilebase ".png"))
18572 (fnh (if (featurep 'xemacs)
18573 (font-height (face-font 'default))
18574 (face-attribute 'default :height nil)))
18575 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
18576 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
18577 (fg (or (plist-get options (if buffer :foreground :html-foreground))
18579 (bg (or (plist-get options (if buffer :background :html-background))
18581 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
18582 (setq fg (org-latex-color-format fg)))
18583 (if (eq bg 'default) (setq bg (org-latex-color :background))
18584 (setq bg (org-latex-color-format
18585 (if (string= bg "Transparent") "white" bg))))
18586 (let ((latex-header (org-create-formula--latex-header)))
18587 (with-temp-file texfile
18588 (insert latex-header)
18589 (insert "\n\\begin{document}\n"
18590 "\\definecolor{fg}{rgb}{" fg "}\n"
18591 "\\definecolor{bg}{rgb}{" bg "}\n"
18592 "\n\\pagecolor{bg}\n"
18596 "\n\\end{document}\n")))
18597 (org-latex-compile texfile t)
18598 (if (not (file-exists-p pdffile))
18599 (progn (message "Failed to create pdf file from %s" texfile) nil)
18600 (condition-case nil
18601 (if (featurep 'xemacs)
18602 (call-process "convert" nil nil nil
18608 ;; "-sharpen" "0x1.0"
18610 (call-process "convert" nil nil nil
18616 ;; "-sharpen" "0x1.0"
18619 (if (not (file-exists-p pngfile))
18620 (if org-format-latex-signal-error
18621 (error "Failed to create png file from %s" texfile)
18622 (message "Failed to create png file from %s" texfile)
18624 ;; Use the requested file name and clean up
18625 (copy-file pngfile tofile 'replace)
18626 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
18627 (if (file-exists-p (concat texfilebase e))
18628 (delete-file (concat texfilebase e))))
18631 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
18632 "Fill a LaTeX header template TPL.
18633 In the template, the following place holders will be recognized:
18635 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
18636 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
18637 [PACKAGES] \\usepackage statements for PKG
18638 [NO-PACKAGES] do not include PKG
18639 [EXTRA] the string EXTRA
18640 [NO-EXTRA] do not include EXTRA
18642 For backward compatibility, if both the positive and the negative place
18643 holder is missing, the positive one (without the \"NO-\") will be
18644 assumed to be present at the end of the template.
18645 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
18647 SNIPPETS-P indicates if this is run to create snippet images for HTML."
18648 (let (rpl (end ""))
18649 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
18650 (setq rpl (if (or (match-end 1) (not def-pkg))
18651 "" (org-latex-packages-to-string def-pkg snippets-p t))
18652 tpl (replace-match rpl t t tpl))
18653 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
18655 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
18656 (setq rpl (if (or (match-end 1) (not pkg))
18657 "" (org-latex-packages-to-string pkg snippets-p t))
18658 tpl (replace-match rpl t t tpl))
18661 (org-latex-packages-to-string pkg snippets-p)))))
18663 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
18664 (setq rpl (if (or (match-end 1) (not extra))
18665 "" (concat extra "\n"))
18666 tpl (replace-match rpl t t tpl))
18667 (if (and extra (string-match "\\S-" extra))
18668 (setq end (concat end "\n" extra))))
18670 (if (string-match "\\S-" end)
18671 (concat tpl "\n" end)
18674 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
18675 "Turn an alist of packages into a string with the \\usepackage macros."
18676 (setq pkg (mapconcat (lambda(p)
18679 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
18680 (format "%% Package %s omitted" (cadr p)))
18681 ((equal "" (car p))
18682 (format "\\usepackage{%s}" (cadr p)))
18684 (format "\\usepackage[%s]{%s}"
18685 (car p) (cadr p)))))
18688 (if newline (concat pkg "\n") pkg))
18690 (defun org-dvipng-color (attr)
18691 "Return a RGB color specification for dvipng."
18692 (apply 'format "rgb %s %s %s"
18693 (mapcar 'org-normalize-color
18694 (if (featurep 'xemacs)
18695 (color-rgb-components
18696 (face-property 'default
18697 (cond ((eq attr :foreground) 'foreground)
18698 ((eq attr :background) 'background))))
18699 (color-values (face-attribute 'default attr nil))))))
18701 (defun org-dvipng-color-format (color-name)
18702 "Convert COLOR-NAME to a RGB color value for dvipng."
18703 (apply 'format "rgb %s %s %s"
18704 (mapcar 'org-normalize-color
18705 (color-values color-name))))
18707 (defun org-latex-color (attr)
18708 "Return a RGB color for the LaTeX color package."
18709 (apply 'format "%s,%s,%s"
18710 (mapcar 'org-normalize-color
18711 (if (featurep 'xemacs)
18712 (color-rgb-components
18713 (face-property 'default
18714 (cond ((eq attr :foreground) 'foreground)
18715 ((eq attr :background) 'background))))
18716 (color-values (face-attribute 'default attr nil))))))
18718 (defun org-latex-color-format (color-name)
18719 "Convert COLOR-NAME to a RGB color value."
18720 (apply 'format "%s,%s,%s"
18721 (mapcar 'org-normalize-color
18722 (color-values color-name))))
18724 (defun org-normalize-color (value)
18725 "Return string to be used as color value for an RGB component."
18726 (format "%g" (/ value 65535.0)))
18732 (defvar org-inline-image-overlays nil)
18733 (make-variable-buffer-local 'org-inline-image-overlays)
18735 (defun org-toggle-inline-images (&optional include-linked)
18736 "Toggle the display of inline images.
18737 INCLUDE-LINKED is passed to `org-display-inline-images'."
18739 (if org-inline-image-overlays
18741 (org-remove-inline-images)
18742 (message "Inline image display turned off"))
18743 (org-display-inline-images include-linked)
18744 (if (and (org-called-interactively-p)
18745 org-inline-image-overlays)
18746 (message "%d images displayed inline"
18747 (length org-inline-image-overlays))
18748 (message "No images to display inline"))))
18750 (defun org-redisplay-inline-images ()
18751 "Refresh the display of inline images."
18753 (if (not org-inline-image-overlays)
18754 (org-toggle-inline-images)
18755 (org-toggle-inline-images)
18756 (org-toggle-inline-images)))
18758 (defun org-display-inline-images (&optional include-linked refresh beg end)
18759 "Display inline images.
18760 Normally only links without a description part are inlined, because this
18761 is how it will work for export. When INCLUDE-LINKED is set, also links
18762 with a description part will be inlined. This can be nice for a quick
18763 look at those images, but it does not reflect what exported files will look
18765 When REFRESH is set, refresh existing images between BEG and END.
18766 This will create new image displays only if necessary.
18767 BEG and END default to the buffer boundaries."
18770 (org-remove-inline-images)
18771 (if (fboundp 'clear-image-cache) (clear-image-cache)))
18775 (setq beg (or beg (point-min)) end (or end (point-max)))
18777 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
18778 (substring (org-image-file-name-regexp) 0 -2)
18779 "\\)\\]" (if include-linked "" "\\]")))
18780 (case-fold-search t)
18781 old file ov img type attrwidth width)
18782 (while (re-search-forward re end t)
18783 (setq old (get-char-property-and-overlay (match-beginning 1)
18784 'org-image-overlay)
18785 file (expand-file-name
18786 (concat (or (match-string 3) "") (match-string 4))))
18787 (when (image-type-available-p 'imagemagick)
18788 (setq attrwidth (if (or (listp org-image-actual-width)
18789 (null org-image-actual-width))
18792 (when (re-search-backward
18793 "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
18795 (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
18796 (string-to-number (match-string 1))))))
18797 width (cond ((eq org-image-actual-width t) nil)
18798 ((null org-image-actual-width) attrwidth)
18799 ((numberp org-image-actual-width)
18800 org-image-actual-width)
18801 ((listp org-image-actual-width)
18802 (or attrwidth (car org-image-actual-width))))
18803 type (if width 'imagemagick)))
18804 (when (file-exists-p file)
18805 (if (and (car-safe old) refresh)
18806 (image-refresh (overlay-get (cdr old) 'display))
18807 (setq img (save-match-data (create-image file type nil :width width)))
18809 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
18810 (overlay-put ov 'display img)
18811 (overlay-put ov 'face 'default)
18812 (overlay-put ov 'org-image-overlay t)
18813 (overlay-put ov 'modification-hooks
18814 (list 'org-display-inline-remove-overlay))
18815 (push ov org-inline-image-overlays)))))))))
18817 (define-obsolete-function-alias
18818 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
18820 (defun org-display-inline-remove-overlay (ov after beg end &optional len)
18821 "Remove inline-display overlay if a corresponding region is modified."
18822 (let ((inhibit-modification-hooks t))
18823 (when (and ov after)
18824 (delete ov org-inline-image-overlays)
18825 (delete-overlay ov))))
18827 (defun org-remove-inline-images ()
18828 "Remove inline display of images."
18830 (mapc 'delete-overlay org-inline-image-overlays)
18831 (setq org-inline-image-overlays nil))
18835 ;; Outline functions from `outline-mode-prefix-map'
18836 ;; that can be remapped in Org:
18837 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
18838 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
18839 (define-key org-mode-map [remap outline-forward-same-level]
18840 'org-forward-heading-same-level)
18841 (define-key org-mode-map [remap outline-backward-same-level]
18842 'org-backward-heading-same-level)
18843 (define-key org-mode-map [remap show-branches]
18844 'org-kill-note-or-show-branches)
18845 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
18846 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
18847 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
18849 ;; Outline functions from `outline-mode-prefix-map' that can not
18850 ;; be remapped in Org:
18852 ;; - the column "key binding" shows whether the Outline function is still
18853 ;; available in Org mode on the same key that it has been bound to in
18855 ;; - "overridden": key used for a different functionality in Org mode
18856 ;; - else: key still bound to the same Outline function in Org mode
18858 ;; | Outline function | key binding | Org replacement |
18859 ;; |------------------------------------+-------------+-----------------------|
18860 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
18861 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
18862 ;; | `outline-up-heading' | `C-c C-u' | still same function |
18863 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
18864 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
18865 ;; | `show-entry' | overridden | no replacement |
18866 ;; | `show-children' | `C-c C-i' | visibility cycling |
18867 ;; | `show-branches' | `C-c C-k' | still same function |
18868 ;; | `show-subtree' | overridden | visibility cycling |
18869 ;; | `show-all' | overridden | no replacement |
18870 ;; | `hide-subtree' | overridden | visibility cycling |
18871 ;; | `hide-body' | overridden | no replacement |
18872 ;; | `hide-entry' | overridden | visibility cycling |
18873 ;; | `hide-leaves' | overridden | no replacement |
18874 ;; | `hide-sublevels' | overridden | no replacement |
18875 ;; | `hide-other' | overridden | no replacement |
18877 ;; Make `C-c C-x' a prefix key
18878 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
18880 ;; TAB key with modifiers
18881 (org-defkey org-mode-map "\C-i" 'org-cycle)
18882 (org-defkey org-mode-map [(tab)] 'org-cycle)
18883 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
18884 (org-defkey org-mode-map "\M-\t" 'pcomplete)
18885 ;; The following line is necessary under Suse GNU/Linux
18886 (unless (featurep 'xemacs)
18887 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
18888 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
18889 (define-key org-mode-map [backtab] 'org-shifttab)
18891 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
18892 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
18893 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
18895 ;; Cursor keys with modifiers
18896 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
18897 (org-defkey org-mode-map [(meta right)] 'org-metaright)
18898 (org-defkey org-mode-map [(meta up)] 'org-metaup)
18899 (org-defkey org-mode-map [(meta down)] 'org-metadown)
18901 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
18902 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
18903 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
18904 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
18906 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
18907 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
18908 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
18909 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
18911 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
18912 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
18913 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
18914 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
18917 (define-key org-mode-map org-babel-key-prefix org-babel-map)
18918 (mapc (lambda (pair)
18919 (define-key org-babel-map (car pair) (cdr pair)))
18920 org-babel-key-bindings)
18922 ;;; Extra keys for tty access.
18923 ;; We only set them when really needed because otherwise the
18924 ;; menus don't show the simple keys
18926 (when (or org-use-extra-keys
18927 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
18928 (not window-system))
18929 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
18930 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
18931 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
18932 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
18933 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
18934 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
18935 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
18936 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
18937 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
18938 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
18939 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
18940 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
18941 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
18942 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
18943 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
18944 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
18945 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
18946 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
18947 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
18948 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
18949 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
18950 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
18951 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
18952 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
18953 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
18954 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
18955 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
18956 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
18958 ;; All the other keys
18960 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
18961 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
18962 (if (boundp 'narrow-map)
18963 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
18964 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
18965 (if (boundp 'narrow-map)
18966 (org-defkey narrow-map "b" 'org-narrow-to-block)
18967 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
18968 (if (boundp 'narrow-map)
18969 (org-defkey narrow-map "e" 'org-narrow-to-element)
18970 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
18971 (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
18972 (org-defkey org-mode-map "\M-}" 'org-forward-element)
18973 (org-defkey org-mode-map "\M-{" 'org-backward-element)
18974 (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
18975 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
18976 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
18977 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
18978 (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
18979 (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
18980 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
18981 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
18982 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
18983 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
18984 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
18985 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
18986 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
18987 (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
18988 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
18989 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
18990 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
18991 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
18992 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
18993 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
18994 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
18995 (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
18996 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
18997 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
18998 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
18999 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
19000 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
19001 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
19002 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
19003 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
19004 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
19005 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
19006 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
19007 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
19008 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
19009 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
19010 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
19011 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
19012 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
19013 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
19014 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
19015 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
19016 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
19017 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
19018 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
19019 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
19020 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
19021 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
19022 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
19023 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
19024 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
19025 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
19026 (org-defkey org-mode-map "\C-c^" 'org-sort)
19027 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
19028 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
19029 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
19030 (org-defkey org-mode-map [remap open-line] 'org-open-line)
19031 (org-defkey org-mode-map "\C-m" 'org-return)
19032 (org-defkey org-mode-map "\C-j" 'org-return-indent)
19033 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
19034 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
19035 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
19036 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
19037 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
19038 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
19039 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
19040 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
19041 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
19042 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
19043 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
19044 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
19045 (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
19046 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
19047 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
19048 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
19049 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
19050 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
19051 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
19052 (org-defkey org-mode-map "\M-h" 'org-mark-element)
19053 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
19054 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
19056 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
19057 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
19058 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
19060 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
19061 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
19062 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
19063 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
19064 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
19065 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
19066 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
19067 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
19068 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
19069 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
19070 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
19071 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
19072 (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
19073 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
19074 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
19075 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
19076 (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
19077 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
19078 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
19079 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
19080 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
19081 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
19082 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
19084 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
19085 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
19086 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
19087 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
19088 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
19090 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
19092 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
19094 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
19095 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
19097 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
19100 (when (featurep 'xemacs)
19101 (org-defkey org-mode-map 'button3 'popup-mode-menu))
19104 (defconst org-speed-commands-default
19106 ("Outline Navigation")
19107 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
19108 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
19109 ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
19110 ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
19111 ("F" . org-next-block)
19112 ("B" . org-previous-block)
19113 ("u" . (org-speed-move-safe 'outline-up-heading))
19115 ("g" . (org-refile t))
19116 ("Outline Visibility")
19118 ("C" . org-shifttab)
19119 (" " . org-display-outline-path)
19120 ("s" . org-narrow-to-subtree)
19121 ("=" . org-columns)
19122 ("Outline Structure Editing")
19123 ("U" . org-shiftmetaup)
19124 ("D" . org-shiftmetadown)
19125 ("r" . org-metaright)
19126 ("l" . org-metaleft)
19127 ("R" . org-shiftmetaright)
19128 ("L" . org-shiftmetaleft)
19129 ("i" . (progn (forward-char 1) (call-interactively
19130 'org-insert-heading-respect-content)))
19133 ("a" . org-archive-subtree-default-with-confirmation)
19134 ("@" . org-mark-subtree)
19135 ("#" . org-toggle-comment)
19137 ("I" . org-clock-in)
19138 ("O" . org-clock-out)
19139 ("Meta Data Editing")
19141 ("," . (org-priority))
19142 ("0" . (org-priority ?\ ))
19143 ("1" . (org-priority ?A))
19144 ("2" . (org-priority ?B))
19145 ("3" . (org-priority ?C))
19146 (":" . org-set-tags-command)
19147 ("e" . org-set-effort)
19148 ("E" . org-inc-effort)
19149 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
19150 (org-entry-put (point) "APPT_WARNTIME" m)))
19151 ("Agenda Views etc")
19153 ("/" . org-sparse-tree)
19155 ("o" . org-open-at-point)
19156 ("?" . org-speed-command-help)
19157 ("<" . (org-agenda-set-restriction-lock 'subtree))
19158 (">" . (org-agenda-remove-restriction-lock))
19160 "The default speed commands.")
19162 (defun org-print-speed-command (e)
19163 (if (> (length (car e)) 1)
19168 (princ (make-string (length (car e)) ?-))
19172 (if (symbolp (cdr e))
19173 (princ (symbol-name (cdr e)))
19177 (defun org-speed-command-help ()
19178 "Show the available speed commands."
19180 (if (not org-use-speed-commands)
19181 (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
19182 (with-output-to-temp-buffer "*Help*"
19183 (princ "User-defined Speed commands\n===========================\n")
19184 (mapc 'org-print-speed-command org-speed-commands-user)
19186 (princ "Built-in Speed commands\n=======================\n")
19187 (mapc 'org-print-speed-command org-speed-commands-default))
19188 (with-current-buffer "*Help*"
19189 (setq truncate-lines t))))
19191 (defun org-speed-move-safe (cmd)
19192 "Execute CMD, but make sure that the cursor always ends up in a headline.
19193 If not, return to the original position and throw an error."
19195 (let ((pos (point)))
19196 (call-interactively cmd)
19197 (unless (and (bolp) (org-at-heading-p))
19199 (error "Boundary reached while executing %s" cmd))))
19201 (defvar org-self-insert-command-undo-counter 0)
19203 (defvar org-table-auto-blank-field) ; defined in org-table.el
19204 (defvar org-speed-command nil)
19206 (define-obsolete-function-alias
19207 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
19209 (defun org-speed-command-activate (keys)
19210 "Hook for activating single-letter speed commands.
19211 `org-speed-commands-default' specifies a minimal command set.
19212 Use `org-speed-commands-user' for further customization."
19213 (when (or (and (bolp) (looking-at org-outline-regexp))
19214 (and (functionp org-use-speed-commands)
19215 (funcall org-use-speed-commands)))
19216 (cdr (assoc keys (append org-speed-commands-user
19217 org-speed-commands-default)))))
19219 (define-obsolete-function-alias
19220 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
19222 (defun org-babel-speed-command-activate (keys)
19223 "Hook for activating single-letter code block commands."
19224 (when (and (bolp) (looking-at org-babel-src-block-regexp))
19225 (cdr (assoc keys org-babel-key-bindings))))
19227 (defcustom org-speed-command-hook
19228 '(org-speed-command-default-hook org-babel-speed-command-hook)
19229 "Hook for activating speed commands at strategic locations.
19230 Hook functions are called in sequence until a valid handler is
19233 Each hook takes a single argument, a user-pressed command key
19234 which is also a `self-insert-command' from the global map.
19236 Within the hook, examine the cursor position and the command key
19237 and return nil or a valid handler as appropriate. Handler could
19238 be one of an interactive command, a function, or a form.
19240 Set `org-use-speed-commands' to non-nil value to enable this
19241 hook. The default setting is `org-speed-command-activate'."
19242 :group 'org-structure
19246 (defun org-self-insert-command (N)
19247 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
19248 If the cursor is in a table looking at whitespace, the whitespace is
19249 overwritten, and the table is not marked as requiring realignment."
19251 (org-check-before-invisible-edit 'insert)
19253 ((and org-use-speed-commands
19254 (setq org-speed-command
19255 (run-hook-with-args-until-success
19256 'org-speed-command-hook (this-command-keys))))
19258 ((commandp org-speed-command)
19259 (setq this-command org-speed-command)
19260 (call-interactively org-speed-command))
19261 ((functionp org-speed-command)
19262 (funcall org-speed-command))
19263 ((and org-speed-command (listp org-speed-command))
19264 (eval org-speed-command))
19265 (t (let (org-use-speed-commands)
19266 (call-interactively 'org-self-insert-command)))))
19270 ;; check if we blank the field, and if that triggers align
19271 (and (featurep 'org-table) org-table-auto-blank-field
19272 (member last-command
19273 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
19274 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
19275 ;; got extra space, this field does not determine column width
19276 (let (org-table-may-need-update) (org-table-blank-field))
19277 ;; no extra space, this field may determine column width
19278 (org-table-blank-field)))
19281 (looking-at "[^|\n]* |"))
19282 (let (org-table-may-need-update)
19283 (goto-char (1- (match-end 0)))
19284 (backward-delete-char 1)
19285 (goto-char (match-beginning 0))
19286 (self-insert-command N)))
19288 (setq org-table-may-need-update t)
19289 (self-insert-command N)
19290 (org-fix-tags-on-the-fly)
19291 (if org-self-insert-cluster-for-undo
19292 (if (not (eq last-command 'org-self-insert-command))
19293 (setq org-self-insert-command-undo-counter 1)
19294 (if (>= org-self-insert-command-undo-counter 20)
19295 (setq org-self-insert-command-undo-counter 1)
19296 (and (> org-self-insert-command-undo-counter 0)
19297 buffer-undo-list (listp buffer-undo-list)
19298 (not (cadr buffer-undo-list)) ; remove nil entry
19299 (setcdr buffer-undo-list (cddr buffer-undo-list)))
19300 (setq org-self-insert-command-undo-counter
19301 (1+ org-self-insert-command-undo-counter))))))))
19303 (defun org-check-before-invisible-edit (kind)
19304 "Check is editing if kind KIND would be dangerous with invisible text around.
19305 The detailed reaction depends on the user option `org-catch-invisible-edits'."
19306 ;; First, try to get out of here as quickly as possible, to reduce overhead
19307 (if (and org-catch-invisible-edits
19308 (or (not (boundp 'visible-mode)) (not visible-mode))
19309 (or (get-char-property (point) 'invisible)
19310 (get-char-property (max (point-min) (1- (point))) 'invisible)))
19311 ;; OK, we need to take a closer look
19312 (let* ((invisible-at-point (get-char-property (point) 'invisible))
19313 (invisible-before-point (if (bobp) nil (get-char-property
19314 (1- (point)) 'invisible)))
19315 (border-and-ok-direction
19317 ;; Check if we are acting predictably before invisible text
19318 (and invisible-at-point (not invisible-before-point)
19319 (memq kind '(insert delete-backward)))
19320 ;; Check if we are acting predictably after invisible text
19321 ;; This works not well, and I have turned it off. It seems
19322 ;; better to always show and stop after invisible text.
19323 ;; (and (not invisible-at-point) invisible-before-point
19324 ;; (memq kind '(insert delete)))
19326 (when (or (memq invisible-at-point '(outline org-hide-block t))
19327 (memq invisible-before-point '(outline org-hide-block t)))
19328 (if (eq org-catch-invisible-edits 'error)
19329 (user-error "Editing in invisible areas is prohibited, make them visible first"))
19330 (if (and org-custom-properties-overlays
19331 (y-or-n-p "Display invisible properties in this buffer? "))
19332 (org-toggle-custom-properties-visibility)
19333 ;; Make the area visible
19335 (if invisible-before-point
19336 (goto-char (previous-single-char-property-change
19337 (point) 'invisible)))
19340 ((eq org-catch-invisible-edits 'show)
19341 ;; That's it, we do the edit after showing
19343 "Unfolding invisible region around point before editing")
19345 ((and (eq org-catch-invisible-edits 'smart)
19346 border-and-ok-direction)
19347 (message "Unfolding invisible region around point before editing"))
19349 ;; Don't do the edit, make the user repeat it in full visibility
19350 (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
19352 (defun org-fix-tags-on-the-fly ()
19353 (when (and (equal (char-after (point-at-bol)) ?*)
19354 (org-at-heading-p))
19355 (org-align-tags-here org-tags-column)))
19357 (defun org-delete-backward-char (N)
19358 "Like `delete-backward-char', insert whitespace at field end in tables.
19359 When deleting backwards, in tables this function will insert whitespace in
19360 front of the next \"|\" separator, to keep the table aligned. The table will
19361 still be marked for re-alignment if the field did fill the entire column,
19362 because, in this case the deletion might narrow the column."
19365 (org-check-before-invisible-edit 'delete-backward)
19366 (if (and (org-table-p)
19368 (string-match "|" (buffer-substring (point-at-bol) (point)))
19369 (looking-at ".*?|"))
19370 (let ((pos (point))
19371 (noalign (looking-at "[^|\n\r]* |"))
19372 (c org-table-may-need-update))
19373 (backward-delete-char N)
19374 (if (not overwrite-mode)
19376 (skip-chars-forward "^|")
19378 (goto-char (1- pos))))
19379 ;; noalign: if there were two spaces at the end, this field
19380 ;; does not determine the width of the column.
19381 (if noalign (setq org-table-may-need-update c)))
19382 (backward-delete-char N)
19383 (org-fix-tags-on-the-fly))))
19385 (defun org-delete-char (N)
19386 "Like `delete-char', but insert whitespace at field end in tables.
19387 When deleting characters, in tables this function will insert whitespace in
19388 front of the next \"|\" separator, to keep the table aligned. The table will
19389 still be marked for re-alignment if the field did fill the entire column,
19390 because, in this case the deletion might narrow the column."
19393 (org-check-before-invisible-edit 'delete)
19394 (if (and (org-table-p)
19396 (not (= (char-after) ?|))
19398 (if (looking-at ".*?|")
19399 (let ((pos (point))
19400 (noalign (looking-at "[^|\n\r]* |"))
19401 (c org-table-may-need-update))
19403 (concat (substring (match-string 0) 1 -1) " |") nil t)
19405 ;; noalign: if there were two spaces at the end, this field
19406 ;; does not determine the width of the column.
19407 (if noalign (setq org-table-may-need-update c)))
19410 (org-fix-tags-on-the-fly))))
19412 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
19413 (put 'org-self-insert-command 'delete-selection t)
19414 (put 'orgtbl-self-insert-command 'delete-selection t)
19415 (put 'org-delete-char 'delete-selection 'supersede)
19416 (put 'org-delete-backward-char 'delete-selection 'supersede)
19417 (put 'org-yank 'delete-selection 'yank)
19419 ;; Make `flyspell-mode' delay after some commands
19420 (put 'org-self-insert-command 'flyspell-delayed t)
19421 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
19422 (put 'org-delete-char 'flyspell-delayed t)
19423 (put 'org-delete-backward-char 'flyspell-delayed t)
19425 ;; Make pabbrev-mode expand after org-mode commands
19426 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
19427 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
19429 ;; How to do this: Measure non-white length of current string
19430 ;; If equal to column width, we should realign.
19432 (defun org-remap (map &rest commands)
19433 "In MAP, remap the functions given in COMMANDS.
19434 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
19437 (setq old (pop commands) new (pop commands))
19438 (if (fboundp 'command-remapping)
19439 (org-defkey map (vector 'remap old) new)
19440 (substitute-key-definition old new map global-map)))))
19442 (defun org-transpose-words ()
19443 "Transpose words for Org.
19444 This uses the `org-mode-transpose-word-syntax-table' syntax
19445 table, which interprets characters in `org-emphasis-alist' as
19446 word constituants."
19448 (with-syntax-table org-mode-transpose-word-syntax-table
19449 (call-interactively 'transpose-words)))
19450 (org-remap org-mode-map 'transpose-words 'org-transpose-words)
19452 (when (eq org-enable-table-editor 'optimized)
19453 ;; If the user wants maximum table support, we need to hijack
19454 ;; some standard editing functions
19455 (org-remap org-mode-map
19456 'self-insert-command 'org-self-insert-command
19457 'delete-char 'org-delete-char
19458 'delete-backward-char 'org-delete-backward-char)
19459 (org-defkey org-mode-map "|" 'org-force-self-insert))
19461 (defvar org-ctrl-c-ctrl-c-hook nil
19462 "Hook for functions attaching themselves to `C-c C-c'.
19464 This can be used to add additional functionality to the C-c C-c
19465 key which executes context-dependent commands. This hook is run
19466 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
19467 run after the last test.
19469 Each function will be called with no arguments. The function
19470 must check if the context is appropriate for it to act. If yes,
19471 it should do its thing and then return a non-nil value. If the
19472 context is wrong, just do nothing and return nil.")
19474 (defvar org-ctrl-c-ctrl-c-final-hook nil
19475 "Hook for functions attaching themselves to `C-c C-c'.
19477 This can be used to add additional functionality to the C-c C-c
19478 key which executes context-dependent commands. This hook is run
19479 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
19480 before the first test.
19482 Each function will be called with no arguments. The function
19483 must check if the context is appropriate for it to act. If yes,
19484 it should do its thing and then return a non-nil value. If the
19485 context is wrong, just do nothing and return nil.")
19487 (defvar org-tab-first-hook nil
19488 "Hook for functions to attach themselves to TAB.
19489 See `org-ctrl-c-ctrl-c-hook' for more information.
19490 This hook runs as the first action when TAB is pressed, even before
19491 `org-cycle' messes around with the `outline-regexp' to cater for
19492 inline tasks and plain list item folding.
19493 If any function in this hook returns t, any other actions that
19494 would have been caused by TAB (such as table field motion or visibility
19495 cycling) will not occur.")
19497 (defvar org-tab-after-check-for-table-hook nil
19498 "Hook for functions to attach themselves to TAB.
19499 See `org-ctrl-c-ctrl-c-hook' for more information.
19500 This hook runs after it has been established that the cursor is not in a
19501 table, but before checking if the cursor is in a headline or if global cycling
19503 If any function in this hook returns t, not other actions like visibility
19504 cycling will be done.")
19506 (defvar org-tab-after-check-for-cycling-hook nil
19507 "Hook for functions to attach themselves to TAB.
19508 See `org-ctrl-c-ctrl-c-hook' for more information.
19509 This hook runs after it has been established that not table field motion and
19510 not visibility should be done because of current context. This is probably
19511 the place where a package like yasnippets can hook in.")
19513 (defvar org-tab-before-tab-emulation-hook nil
19514 "Hook for functions to attach themselves to TAB.
19515 See `org-ctrl-c-ctrl-c-hook' for more information.
19516 This hook runs after every other options for TAB have been exhausted, but
19517 before indentation and \t insertion takes place.")
19519 (defvar org-metaleft-hook nil
19520 "Hook for functions attaching themselves to `M-left'.
19521 See `org-ctrl-c-ctrl-c-hook' for more information.")
19522 (defvar org-metaright-hook nil
19523 "Hook for functions attaching themselves to `M-right'.
19524 See `org-ctrl-c-ctrl-c-hook' for more information.")
19525 (defvar org-metaup-hook nil
19526 "Hook for functions attaching themselves to `M-up'.
19527 See `org-ctrl-c-ctrl-c-hook' for more information.")
19528 (defvar org-metadown-hook nil
19529 "Hook for functions attaching themselves to `M-down'.
19530 See `org-ctrl-c-ctrl-c-hook' for more information.")
19531 (defvar org-shiftmetaleft-hook nil
19532 "Hook for functions attaching themselves to `M-S-left'.
19533 See `org-ctrl-c-ctrl-c-hook' for more information.")
19534 (defvar org-shiftmetaright-hook nil
19535 "Hook for functions attaching themselves to `M-S-right'.
19536 See `org-ctrl-c-ctrl-c-hook' for more information.")
19537 (defvar org-shiftmetaup-hook nil
19538 "Hook for functions attaching themselves to `M-S-up'.
19539 See `org-ctrl-c-ctrl-c-hook' for more information.")
19540 (defvar org-shiftmetadown-hook nil
19541 "Hook for functions attaching themselves to `M-S-down'.
19542 See `org-ctrl-c-ctrl-c-hook' for more information.")
19543 (defvar org-metareturn-hook nil
19544 "Hook for functions attaching themselves to `M-RET'.
19545 See `org-ctrl-c-ctrl-c-hook' for more information.")
19546 (defvar org-shiftup-hook nil
19547 "Hook for functions attaching themselves to `S-up'.
19548 See `org-ctrl-c-ctrl-c-hook' for more information.")
19549 (defvar org-shiftup-final-hook nil
19550 "Hook for functions attaching themselves to `S-up'.
19551 This one runs after all other options except shift-select have been excluded.
19552 See `org-ctrl-c-ctrl-c-hook' for more information.")
19553 (defvar org-shiftdown-hook nil
19554 "Hook for functions attaching themselves to `S-down'.
19555 See `org-ctrl-c-ctrl-c-hook' for more information.")
19556 (defvar org-shiftdown-final-hook nil
19557 "Hook for functions attaching themselves to `S-down'.
19558 This one runs after all other options except shift-select have been excluded.
19559 See `org-ctrl-c-ctrl-c-hook' for more information.")
19560 (defvar org-shiftleft-hook nil
19561 "Hook for functions attaching themselves to `S-left'.
19562 See `org-ctrl-c-ctrl-c-hook' for more information.")
19563 (defvar org-shiftleft-final-hook nil
19564 "Hook for functions attaching themselves to `S-left'.
19565 This one runs after all other options except shift-select have been excluded.
19566 See `org-ctrl-c-ctrl-c-hook' for more information.")
19567 (defvar org-shiftright-hook nil
19568 "Hook for functions attaching themselves to `S-right'.
19569 See `org-ctrl-c-ctrl-c-hook' for more information.")
19570 (defvar org-shiftright-final-hook nil
19571 "Hook for functions attaching themselves to `S-right'.
19572 This one runs after all other options except shift-select have been excluded.
19573 See `org-ctrl-c-ctrl-c-hook' for more information.")
19575 (defun org-modifier-cursor-error ()
19576 "Throw an error, a modified cursor command was applied in wrong context."
19577 (user-error "This command is active in special context like tables, headlines or items"))
19579 (defun org-shiftselect-error ()
19580 "Throw an error because Shift-Cursor command was applied in wrong context."
19581 (if (and (boundp 'shift-select-mode) shift-select-mode)
19582 (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
19583 (user-error "This command works only in special context like headlines or timestamps")))
19585 (defun org-call-for-shift-select (cmd)
19586 (let ((this-command-keys-shift-translated t))
19587 (call-interactively cmd)))
19589 (defun org-shifttab (&optional arg)
19590 "Global visibility cycling or move to previous table field.
19591 Call `org-table-previous-field' within a table.
19592 When ARG is nil, cycle globally through visibility states.
19593 When ARG is a numeric prefix, show contents of this level."
19596 ((org-at-table-p) (call-interactively 'org-table-previous-field))
19598 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
19599 (message "Content view to level: %d" arg)
19600 (org-content (prefix-numeric-value arg2))
19601 (org-cycle-show-empty-lines t)
19602 (setq org-cycle-global-status 'overview)))
19603 (t (call-interactively 'org-global-cycle))))
19605 (defun org-shiftmetaleft ()
19606 "Promote subtree or delete table column.
19607 Calls `org-promote-subtree', `org-outdent-item-tree', or
19608 `org-table-delete-column', depending on context. See the
19609 individual commands for more information."
19612 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
19613 ((org-at-table-p) (call-interactively 'org-table-delete-column))
19614 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
19615 ((if (not (org-region-active-p)) (org-at-item-p)
19616 (save-excursion (goto-char (region-beginning))
19618 (call-interactively 'org-outdent-item-tree))
19619 (t (org-modifier-cursor-error))))
19621 (defun org-shiftmetaright ()
19622 "Demote subtree or insert table column.
19623 Calls `org-demote-subtree', `org-indent-item-tree', or
19624 `org-table-insert-column', depending on context. See the
19625 individual commands for more information."
19628 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
19629 ((org-at-table-p) (call-interactively 'org-table-insert-column))
19630 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
19631 ((if (not (org-region-active-p)) (org-at-item-p)
19632 (save-excursion (goto-char (region-beginning))
19634 (call-interactively 'org-indent-item-tree))
19635 (t (org-modifier-cursor-error))))
19637 (defun org-shiftmetaup (&optional arg)
19638 "Move subtree up or kill table row.
19639 Calls `org-move-subtree-up' or `org-table-kill-row' or
19640 `org-move-item-up' or `org-timestamp-up', depending on context.
19641 See the individual commands for more information."
19644 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
19645 ((org-at-table-p) (call-interactively 'org-table-kill-row))
19646 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
19647 ((org-at-item-p) (call-interactively 'org-move-item-up))
19648 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
19649 (call-interactively 'org-timestamp-up)))
19650 (t (call-interactively 'org-drag-line-backward))))
19652 (defun org-shiftmetadown (&optional arg)
19653 "Move subtree down or insert table row.
19654 Calls `org-move-subtree-down' or `org-table-insert-row' or
19655 `org-move-item-down' or `org-timestamp-up', depending on context.
19656 See the individual commands for more information."
19659 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
19660 ((org-at-table-p) (call-interactively 'org-table-insert-row))
19661 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
19662 ((org-at-item-p) (call-interactively 'org-move-item-down))
19663 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
19664 (call-interactively 'org-timestamp-down)))
19665 (t (call-interactively 'org-drag-line-forward))))
19667 (defsubst org-hidden-tree-error ()
19669 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
19671 (defun org-metaleft (&optional arg)
19672 "Promote heading or move table column to left.
19673 Calls `org-do-promote' or `org-table-move-column', depending on context.
19674 With no specific context, calls the Emacs default `backward-word'.
19675 See the individual commands for more information."
19678 ((run-hook-with-args-until-success 'org-metaleft-hook))
19679 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
19680 ((org-with-limited-levels
19681 (or (org-at-heading-p)
19682 (and (org-region-active-p)
19684 (goto-char (region-beginning))
19685 (org-at-heading-p)))))
19686 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
19687 (call-interactively 'org-do-promote))
19688 ;; At an inline task.
19689 ((org-at-heading-p)
19690 (call-interactively 'org-inlinetask-promote))
19691 ((or (org-at-item-p)
19692 (and (org-region-active-p)
19694 (goto-char (region-beginning))
19696 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
19697 (call-interactively 'org-outdent-item))
19698 (t (call-interactively 'backward-word))))
19700 (defun org-metaright (&optional arg)
19701 "Demote a subtree, a list item or move table column to right.
19702 In front of a drawer or a block keyword, indent it correctly.
19703 With no specific context, calls the Emacs default `forward-word'.
19704 See the individual commands for more information."
19707 ((run-hook-with-args-until-success 'org-metaright-hook))
19708 ((org-at-table-p) (call-interactively 'org-table-move-column))
19709 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
19710 ((org-at-block-p) (call-interactively 'org-indent-block))
19711 ((org-with-limited-levels
19712 (or (org-at-heading-p)
19713 (and (org-region-active-p)
19715 (goto-char (region-beginning))
19716 (org-at-heading-p)))))
19717 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
19718 (call-interactively 'org-do-demote))
19719 ;; At an inline task.
19720 ((org-at-heading-p)
19721 (call-interactively 'org-inlinetask-demote))
19722 ((or (org-at-item-p)
19723 (and (org-region-active-p)
19725 (goto-char (region-beginning))
19727 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
19728 (call-interactively 'org-indent-item))
19729 (t (call-interactively 'forward-word))))
19731 (defun org-check-for-hidden (what)
19732 "Check if there are hidden headlines/items in the current visual line.
19733 WHAT can be either `headlines' or `items'. If the current line is
19734 an outline or item heading and it has a folded subtree below it,
19735 this function returns t, nil otherwise."
19737 ((eq what 'headlines) org-outline-regexp-bol)
19738 ((eq what 'items) (org-item-beginning-re))
19739 (t (error "This should not happen"))))
19743 (unless (org-region-active-p)
19744 (setq beg (point-at-bol))
19745 (beginning-of-line 2)
19746 (while (and (not (eobp)) ;; this is like `next-line'
19747 (get-char-property (1- (point)) 'invisible))
19748 (beginning-of-line 2))
19751 (goto-char (point-at-eol))
19752 (setq end (max end (point)))
19753 (while (re-search-forward re end t)
19754 (if (get-char-property (match-beginning 0) 'invisible)
19758 (defun org-metaup (&optional arg)
19759 "Move subtree up or move table row up.
19760 Calls `org-move-subtree-up' or `org-table-move-row' or
19761 `org-move-item-up', depending on context. See the individual commands
19762 for more information."
19765 ((run-hook-with-args-until-success 'org-metaup-hook))
19766 ((org-region-active-p)
19767 (let* ((a (min (region-beginning) (region-end)))
19768 (b (1- (max (region-beginning) (region-end))))
19769 (c (save-excursion (goto-char a)
19770 (move-beginning-of-line 0)))
19771 (d (save-excursion (goto-char a)
19772 (move-end-of-line 0) (point))))
19773 (transpose-regions a b c d)
19775 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
19776 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
19777 ((org-at-item-p) (call-interactively 'org-move-item-up))
19778 (t (org-drag-element-backward))))
19780 (defun org-metadown (&optional arg)
19781 "Move subtree down or move table row down.
19782 Calls `org-move-subtree-down' or `org-table-move-row' or
19783 `org-move-item-down', depending on context. See the individual
19784 commands for more information."
19787 ((run-hook-with-args-until-success 'org-metadown-hook))
19788 ((org-region-active-p)
19789 (let* ((a (min (region-beginning) (region-end)))
19790 (b (max (region-beginning) (region-end)))
19791 (c (save-excursion (goto-char b)
19792 (move-beginning-of-line 1)))
19793 (d (save-excursion (goto-char b)
19794 (move-end-of-line 1) (1+ (point)))))
19795 (transpose-regions a b c d)
19797 ((org-at-table-p) (call-interactively 'org-table-move-row))
19798 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
19799 ((org-at-item-p) (call-interactively 'org-move-item-down))
19800 (t (org-drag-element-forward))))
19802 (defun org-shiftup (&optional arg)
19803 "Increase item in timestamp or increase priority of current headline.
19804 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
19805 depending on context. See the individual commands for more information."
19808 ((run-hook-with-args-until-success 'org-shiftup-hook))
19809 ((and org-support-shift-select (org-region-active-p))
19810 (org-call-for-shift-select 'previous-line))
19811 ((org-at-timestamp-p t)
19812 (call-interactively (if org-edit-timestamp-down-means-later
19813 'org-timestamp-down 'org-timestamp-up)))
19814 ((and (not (eq org-support-shift-select 'always))
19815 org-enable-priority-commands
19816 (org-at-heading-p))
19817 (call-interactively 'org-priority-up))
19818 ((and (not org-support-shift-select) (org-at-item-p))
19819 (call-interactively 'org-previous-item))
19820 ((org-clocktable-try-shift 'up arg))
19821 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
19822 (org-support-shift-select
19823 (org-call-for-shift-select 'previous-line))
19824 (t (org-shiftselect-error))))
19826 (defun org-shiftdown (&optional arg)
19827 "Decrease item in timestamp or decrease priority of current headline.
19828 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
19829 depending on context. See the individual commands for more information."
19832 ((run-hook-with-args-until-success 'org-shiftdown-hook))
19833 ((and org-support-shift-select (org-region-active-p))
19834 (org-call-for-shift-select 'next-line))
19835 ((org-at-timestamp-p t)
19836 (call-interactively (if org-edit-timestamp-down-means-later
19837 'org-timestamp-up 'org-timestamp-down)))
19838 ((and (not (eq org-support-shift-select 'always))
19839 org-enable-priority-commands
19840 (org-at-heading-p))
19841 (call-interactively 'org-priority-down))
19842 ((and (not org-support-shift-select) (org-at-item-p))
19843 (call-interactively 'org-next-item))
19844 ((org-clocktable-try-shift 'down arg))
19845 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
19846 (org-support-shift-select
19847 (org-call-for-shift-select 'next-line))
19848 (t (org-shiftselect-error))))
19850 (defun org-shiftright (&optional arg)
19851 "Cycle the thing at point or in the current line, depending on context.
19852 Depending on context, this does one of the following:
19854 - switch a timestamp at point one day into the future
19855 - on a headline, switch to the next TODO keyword.
19856 - on an item, switch entire list to the next bullet type
19857 - on a property line, switch to the next allowed value
19858 - on a clocktable definition line, move time block into the future"
19861 ((run-hook-with-args-until-success 'org-shiftright-hook))
19862 ((and org-support-shift-select (org-region-active-p))
19863 (org-call-for-shift-select 'forward-char))
19864 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
19865 ((and (not (eq org-support-shift-select 'always))
19866 (org-at-heading-p))
19867 (let ((org-inhibit-logging
19868 (not org-treat-S-cursor-todo-selection-as-state-change))
19869 (org-inhibit-blocking
19870 (not org-treat-S-cursor-todo-selection-as-state-change)))
19871 (org-call-with-arg 'org-todo 'right)))
19872 ((or (and org-support-shift-select
19873 (not (eq org-support-shift-select 'always))
19874 (org-at-item-bullet-p))
19875 (and (not org-support-shift-select) (org-at-item-p)))
19876 (org-call-with-arg 'org-cycle-list-bullet nil))
19877 ((and (not (eq org-support-shift-select 'always))
19878 (org-at-property-p))
19879 (call-interactively 'org-property-next-allowed-value))
19880 ((org-clocktable-try-shift 'right arg))
19881 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
19882 (org-support-shift-select
19883 (org-call-for-shift-select 'forward-char))
19884 (t (org-shiftselect-error))))
19886 (defun org-shiftleft (&optional arg)
19887 "Cycle the thing at point or in the current line, depending on context.
19888 Depending on context, this does one of the following:
19890 - switch a timestamp at point one day into the past
19891 - on a headline, switch to the previous TODO keyword.
19892 - on an item, switch entire list to the previous bullet type
19893 - on a property line, switch to the previous allowed value
19894 - on a clocktable definition line, move time block into the past"
19897 ((run-hook-with-args-until-success 'org-shiftleft-hook))
19898 ((and org-support-shift-select (org-region-active-p))
19899 (org-call-for-shift-select 'backward-char))
19900 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
19901 ((and (not (eq org-support-shift-select 'always))
19902 (org-at-heading-p))
19903 (let ((org-inhibit-logging
19904 (not org-treat-S-cursor-todo-selection-as-state-change))
19905 (org-inhibit-blocking
19906 (not org-treat-S-cursor-todo-selection-as-state-change)))
19907 (org-call-with-arg 'org-todo 'left)))
19908 ((or (and org-support-shift-select
19909 (not (eq org-support-shift-select 'always))
19910 (org-at-item-bullet-p))
19911 (and (not org-support-shift-select) (org-at-item-p)))
19912 (org-call-with-arg 'org-cycle-list-bullet 'previous))
19913 ((and (not (eq org-support-shift-select 'always))
19914 (org-at-property-p))
19915 (call-interactively 'org-property-previous-allowed-value))
19916 ((org-clocktable-try-shift 'left arg))
19917 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
19918 (org-support-shift-select
19919 (org-call-for-shift-select 'backward-char))
19920 (t (org-shiftselect-error))))
19922 (defun org-shiftcontrolright ()
19923 "Switch to next TODO set."
19926 ((and org-support-shift-select (org-region-active-p))
19927 (org-call-for-shift-select 'forward-word))
19928 ((and (not (eq org-support-shift-select 'always))
19929 (org-at-heading-p))
19930 (org-call-with-arg 'org-todo 'nextset))
19931 (org-support-shift-select
19932 (org-call-for-shift-select 'forward-word))
19933 (t (org-shiftselect-error))))
19935 (defun org-shiftcontrolleft ()
19936 "Switch to previous TODO set."
19939 ((and org-support-shift-select (org-region-active-p))
19940 (org-call-for-shift-select 'backward-word))
19941 ((and (not (eq org-support-shift-select 'always))
19942 (org-at-heading-p))
19943 (org-call-with-arg 'org-todo 'previousset))
19944 (org-support-shift-select
19945 (org-call-for-shift-select 'backward-word))
19946 (t (org-shiftselect-error))))
19948 (defun org-shiftcontrolup (&optional n)
19949 "Change timestamps synchronously up in CLOCK log lines.
19950 Optional argument N tells to change by that many units."
19952 (cond ((and (not org-support-shift-select)
19953 (org-at-clock-log-p)
19954 (org-at-timestamp-p t))
19955 (org-clock-timestamps-up n))
19956 (t (org-shiftselect-error))))
19958 (defun org-shiftcontroldown (&optional n)
19959 "Change timestamps synchronously down in CLOCK log lines.
19960 Optional argument N tells to change by that many units."
19962 (cond ((and (not org-support-shift-select)
19963 (org-at-clock-log-p)
19964 (org-at-timestamp-p t))
19965 (org-clock-timestamps-down n))
19966 (t (org-shiftselect-error))))
19968 (defun org-ctrl-c-ret ()
19969 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
19972 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
19973 (t (call-interactively 'org-insert-heading))))
19975 (defun org-find-visible ()
19977 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
19978 (get-char-property s 'invisible)))
19980 (defun org-find-invisible ()
19982 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
19983 (not (get-char-property s 'invisible))))
19986 (defun org-copy-visible (beg end)
19987 "Copy the visible parts of the region."
19992 (narrow-to-region beg end)
19993 (setq s (goto-char (point-min)))
19994 (while (not (= (point) (point-max)))
19995 (goto-char (org-find-invisible))
19996 (push (buffer-substring s (point)) snippets)
19997 (setq s (goto-char (org-find-visible))))))
19998 (kill-new (apply 'concat (nreverse snippets)))))
20000 (defun org-copy-special ()
20001 "Copy region in table or copy current subtree.
20002 Calls `org-table-copy' or `org-copy-subtree', depending on context.
20003 See the individual commands for more information."
20005 (call-interactively
20006 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
20008 (defun org-cut-special ()
20009 "Cut region in table or cut current subtree.
20010 Calls `org-table-copy' or `org-cut-subtree', depending on context.
20011 See the individual commands for more information."
20013 (call-interactively
20014 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
20016 (defun org-paste-special (arg)
20017 "Paste rectangular region into table, or past subtree relative to level.
20018 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
20019 See the individual commands for more information."
20021 (if (org-at-table-p)
20022 (org-table-paste-rectangle)
20023 (org-paste-subtree arg)))
20025 (defsubst org-in-fixed-width-region-p ()
20026 "Is point in a fixed-width region?"
20028 (eq 'fixed-width (org-element-type (org-element-at-point)))))
20030 (defun org-edit-special (&optional arg)
20031 "Call a special editor for the element at point.
20032 When at a table, call the formula editor with `org-table-edit-formulas'.
20033 When in a source code block, call `org-edit-src-code'.
20034 When in a fixed-width region, call `org-edit-fixed-width-region'.
20035 When at an #+INCLUDE keyword, visit the included file.
20036 On a link, call `ffap' to visit the link at point.
20037 Otherwise, return a user error."
20039 (let ((element (org-element-at-point)))
20040 (assert (not buffer-read-only) nil
20041 "Buffer is read-only: %s" (buffer-name))
20042 (case (org-element-type element)
20044 (if (not arg) (org-edit-src-code)
20045 (let* ((info (org-babel-get-src-block-info))
20046 (lang (nth 0 info))
20047 (params (nth 2 info))
20048 (session (cdr (assq :session params))))
20049 (if (not session) (org-edit-src-code)
20050 ;; At a src-block with a session and function called with
20051 ;; an ARG: switch to the buffer related to the inferior
20054 (funcall (intern (concat "org-babel-prep-session:" lang))
20055 session params))))))
20057 (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
20059 (org-remove-double-quotes
20060 (car (org-split-string (org-element-property :value element)))))
20061 (user-error "No special environment to edit here")))
20063 (if (eq (org-element-property :type element) 'table.el)
20064 (org-edit-src-code)
20065 (call-interactively 'org-table-edit-formulas)))
20066 ;; Only Org tables contain `table-row' type elements.
20067 (table-row (call-interactively 'org-table-edit-formulas))
20068 ((example-block export-block) (org-edit-src-code))
20069 (fixed-width (org-edit-fixed-width-region))
20071 ;; No notable element at point. Though, we may be at a link,
20072 ;; which is an object. Thus, scan deeper.
20073 (if (eq (org-element-type (org-element-context element)) 'link)
20074 (call-interactively 'ffap)
20075 (user-error "No special environment to edit here"))))))
20077 (defvar org-table-coordinate-overlays) ; defined in org-table.el
20078 (defun org-ctrl-c-ctrl-c (&optional arg)
20079 "Set tags in headline, or update according to changed information at point.
20081 This command does many different things, depending on context:
20083 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
20084 this is what we do.
20086 - If the cursor is on a statistics cookie, update it.
20088 - If the cursor is in a headline, prompt for tags and insert them
20089 into the current line, aligned to `org-tags-column'. When called
20090 with prefix arg, realign all tags in the current buffer.
20092 - If the cursor is in one of the special #+KEYWORD lines, this
20093 triggers scanning the buffer for these lines and updating the
20096 - If the cursor is inside a table, realign the table. This command
20097 works even if the automatic table editor has been turned off.
20099 - If the cursor is on a #+TBLFM line, re-apply the formulas to
20102 - If the cursor is at a footnote reference or definition, jump to
20103 the corresponding definition or references, respectively.
20105 - If the cursor is a the beginning of a dynamic block, update it.
20107 - If the current buffer is a capture buffer, close note and file it.
20109 - If the cursor is on a <<<target>>>, update radio targets and
20110 corresponding links in this buffer.
20112 - If the cursor is on a numbered item in a plain list, renumber the
20115 - If the cursor is on a checkbox, toggle it.
20117 - If the cursor is on a code block, evaluate it. The variable
20118 `org-confirm-babel-evaluate' can be used to control prompting
20119 before code block evaluation, by default every code block
20120 evaluation requires confirmation. Code block evaluation can be
20121 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
20124 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
20125 org-occur-highlights
20126 org-latex-fragment-image-overlays)
20127 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
20128 (org-remove-occur-highlights)
20129 (org-remove-latex-fragment-image-overlays)
20130 (message "Temporary highlights/overlays removed from current buffer"))
20131 ((and (local-variable-p 'org-finish-function (current-buffer))
20132 (fboundp org-finish-function))
20133 (funcall org-finish-function))
20134 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
20136 (let* ((context (org-element-context)) (type (org-element-type context)))
20137 ;; Test if point is within a blank line.
20138 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
20139 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
20140 (user-error "C-c C-c can do nothing useful at this location"))
20141 ;; For convenience: at the first line of a paragraph on the
20142 ;; same line as an item, apply function on that item instead.
20143 (when (eq type 'paragraph)
20144 (let ((parent (org-element-property :parent context)))
20145 (when (and (eq (org-element-type parent) 'item)
20146 (= (point-at-bol) (org-element-property :begin parent)))
20147 (setq context parent type 'item))))
20148 ;; Act according to type of element or object at point.
20150 (clock (org-clock-update-time-maybe))
20153 (goto-char (org-element-property :post-affiliated context))
20154 (org-update-dblock)))
20155 (footnote-definition
20156 (goto-char (org-element-property :post-affiliated context))
20157 (call-interactively 'org-footnote-action))
20158 (footnote-reference (call-interactively 'org-footnote-action))
20159 ((headline inlinetask)
20160 (save-excursion (goto-char (org-element-property :begin context))
20161 (call-interactively 'org-set-tags)))
20163 ;; At an item: a double C-u set checkbox to "[-]"
20164 ;; unconditionally, whereas a single one will toggle its
20165 ;; presence. Without an universal argument, if the item
20166 ;; has a checkbox, toggle it. Otherwise repair the list.
20167 (let* ((box (org-element-property :checkbox context))
20168 (struct (org-element-property :structure context))
20169 (old-struct (copy-tree struct))
20170 (parents (org-list-parents-alist struct))
20171 (prevs (org-list-prevs-alist struct))
20172 (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
20173 (org-list-set-checkbox
20174 (org-element-property :begin context) struct
20175 (cond ((equal arg '(16)) "[-]")
20176 ((and (not box) (equal arg '(4))) "[ ]")
20177 ((or (not box) (equal arg '(4))) nil)
20178 ((eq box 'on) "[ ]")
20180 ;; Mimic `org-list-write-struct' but with grabbing
20181 ;; a return value from `org-list-struct-fix-box'.
20182 (org-list-struct-fix-ind struct parents 2)
20183 (org-list-struct-fix-item-end struct)
20184 (org-list-struct-fix-bul struct prevs)
20185 (org-list-struct-fix-ind struct parents)
20187 (org-list-struct-fix-box struct parents prevs orderedp)))
20188 (if (and box (equal struct old-struct))
20189 (if (equal arg '(16))
20190 (message "Checkboxes already reset")
20191 (user-error "Cannot toggle this checkbox: %s"
20193 "all subitems checked"
20194 "unchecked subitems")))
20195 (org-list-struct-apply-struct struct old-struct)
20196 (org-update-checkbox-count-maybe))
20198 (message "Checkboxes were removed due to empty box at line %d"
20199 (org-current-line block-item))))))
20201 (let ((org-inhibit-startup-visibility-stuff t)
20202 (org-startup-align-all-tables nil))
20203 (when (boundp 'org-table-coordinate-overlays)
20204 (mapc 'delete-overlay org-table-coordinate-overlays)
20205 (setq org-table-coordinate-overlays nil))
20206 (org-save-outline-visibility 'use-markers (org-mode-restart)))
20207 (message "Local setup has been refreshed"))
20209 ;; At a plain list, with a double C-u argument, set
20210 ;; checkboxes of each item to "[-]", whereas a single one
20211 ;; will toggle their presence according to the state of the
20212 ;; first item in the list. Without an argument, repair the
20214 (let* ((begin (org-element-property :contents-begin context))
20215 (beginm (move-marker (make-marker) begin))
20216 (struct (org-element-property :structure context))
20217 (old-struct (copy-tree struct))
20218 (first-box (save-excursion
20220 (looking-at org-list-full-item-re)
20221 (match-string-no-properties 3)))
20222 (new-box (cond ((equal arg '(16)) "[-]")
20223 ((equal arg '(4)) (unless first-box "[ ]"))
20224 ((equal first-box "[X]") "[ ]")
20228 (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
20229 (org-list-get-all-items
20230 begin struct (org-list-prevs-alist struct))))
20231 ((and first-box (eq (point) begin))
20232 ;; For convenience, when point is at bol on the first
20233 ;; item of the list and no argument is provided, simply
20234 ;; toggle checkbox of that item, if any.
20235 (org-list-set-checkbox begin struct new-box)))
20236 (org-list-write-struct
20237 struct (org-list-parents-alist struct) old-struct)
20238 (org-update-checkbox-count-maybe)
20239 (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
20240 ((property-drawer node-property)
20241 (call-interactively 'org-property-action))
20242 ((radio-target target)
20243 (call-interactively 'org-update-radio-target-regexp))
20245 (call-interactively 'org-update-statistics-cookies))
20246 ((table table-cell table-row)
20247 ;; At a table, recalculate every field and align it. Also
20248 ;; send the table if necessary. If the table has
20249 ;; a `table.el' type, just give up. At a table row or
20250 ;; cell, maybe recalculate line but always align table.
20251 (if (eq (org-element-property :type context) 'table.el)
20252 (message "Use C-c ' to edit table.el tables")
20253 (let ((org-enable-table-editor t))
20254 (if (or (eq type 'table)
20255 ;; Check if point is at a TBLFM line.
20256 (and (eq type 'table-row)
20257 (= (point) (org-element-property :end context))))
20259 (if (org-at-TBLFM-p)
20260 (progn (require 'org-table)
20261 (org-table-calc-current-TBLFM))
20262 (goto-char (org-element-property :contents-begin context))
20263 (org-call-with-arg 'org-table-recalculate (or arg t))
20264 (orgtbl-send-table 'maybe)))
20265 (org-table-maybe-eval-formula)
20266 (cond (arg (call-interactively 'org-table-recalculate))
20267 ((org-table-maybe-recalculate-line))
20268 (t (org-table-align)))))))
20269 (timestamp (org-timestamp-change 0 'day))
20271 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
20273 "C-c C-c can do nothing useful at this location")))))))))
20275 (defun org-mode-restart ()
20276 "Restart Org-mode, to scan again for special lines.
20277 Also updates the keyword regular expressions."
20280 (message "Org-mode restarted"))
20282 (defun org-kill-note-or-show-branches ()
20283 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
20285 (if (not org-finish-function)
20288 (call-interactively 'show-branches))
20289 (let ((org-note-abort t))
20290 (funcall org-finish-function))))
20292 (defun org-open-line (n)
20293 "Insert a new row in tables, call `open-line' elsewhere."
20295 (if (org-at-table-p)
20296 (org-table-insert-row)
20299 (defun org-return (&optional indent)
20300 "Goto next table row or insert a newline.
20301 Calls `org-table-next-row' or `newline', depending on context.
20302 See the individual commands for more information."
20306 ((or (bobp) (org-in-src-block-p))
20307 (if indent (newline-and-indent) (newline)))
20309 (org-table-justify-field-maybe)
20310 (call-interactively 'org-table-next-row))
20311 ;; when `newline-and-indent' is called within a list, make sure
20312 ;; text moved stays inside the item.
20313 ((and (org-in-item-p) indent)
20314 (if (and (org-at-item-p) (>= (point) (match-end 0)))
20316 (save-match-data (newline))
20317 (org-indent-line-to (length (match-string 0))))
20318 (let ((ind (org-get-indentation)))
20320 (if (org-looking-back org-list-end-re)
20322 (org-indent-line-to ind)))))
20323 ((and org-return-follows-link
20324 (org-at-timestamp-p t)
20325 (not (eq org-ts-what 'after)))
20326 (org-follow-timestamp-link))
20327 ((and org-return-follows-link
20328 (let ((tprop (get-text-property (point) 'face)))
20329 (or (eq tprop 'org-link)
20330 (and (listp tprop) (memq 'org-link tprop)))))
20331 (call-interactively 'org-open-at-point))
20332 ((and (org-at-heading-p)
20334 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
20338 (t (if indent (newline-and-indent) (newline))))))
20340 (defun org-return-indent ()
20341 "Goto next table row or insert a newline and indent.
20342 Calls `org-table-next-row' or `newline-and-indent', depending on
20343 context. See the individual commands for more information."
20347 (defun org-ctrl-c-star ()
20348 "Compute table, or change heading status of lines.
20349 Calls `org-table-recalculate' or `org-toggle-heading',
20350 depending on context."
20354 (call-interactively 'org-table-recalculate))
20356 ;; Convert all lines in region to list items
20357 (call-interactively 'org-toggle-heading))))
20359 (defun org-ctrl-c-minus ()
20360 "Insert separator line in table or modify bullet status of line.
20361 Also turns a plain line or a region of lines into list items.
20362 Calls `org-table-insert-hline', `org-toggle-item', or
20363 `org-cycle-list-bullet', depending on context."
20367 (call-interactively 'org-table-insert-hline))
20368 ((org-region-active-p)
20369 (call-interactively 'org-toggle-item))
20371 (call-interactively 'org-cycle-list-bullet))
20373 (call-interactively 'org-toggle-item))))
20375 (defun org-toggle-item (arg)
20376 "Convert headings or normal lines to items, items to normal lines.
20377 If there is no active region, only the current line is considered.
20379 If the first non blank line in the region is a headline, convert
20380 all headlines to items, shifting text accordingly.
20382 If it is an item, convert all items to normal lines.
20384 If it is normal text, change region into a list of items.
20385 With a prefix argument ARG, change the region in a single item."
20389 ;; Shift text in current section to IND, from point to END.
20390 ;; The function leaves point to END line.
20392 (let ((min-i 1000) (end (copy-marker end)))
20393 ;; First determine the minimum indentation (MIN-I) of
20397 (while (< (point) end)
20398 (let ((i (org-get-indentation)))
20400 ;; Skip blank lines and inline tasks.
20401 ((looking-at "^[ \t]*$"))
20402 ((looking-at org-outline-regexp-bol))
20403 ;; We can't find less than 0 indentation.
20404 ((zerop i) (throw 'exit (setq min-i 0)))
20405 ((< i min-i) (setq min-i i))))
20407 ;; Then indent each line so that a line indented to
20408 ;; MIN-I becomes indented to IND. Ignore blank lines
20409 ;; and inline tasks in the process.
20410 (let ((delta (- ind min-i)))
20411 (while (< (point) end)
20412 (unless (or (looking-at "^[ \t]*$")
20413 (looking-at org-outline-regexp-bol))
20414 (org-indent-line-to (+ (org-get-indentation) delta)))
20415 (forward-line)))))))
20418 ;; Return beginning of first non-blank line, starting from
20423 (skip-chars-forward " \r\t\n")
20426 ;; Determine boundaries of changes.
20427 (if (org-region-active-p)
20428 (setq beg (funcall skip-blanks (region-beginning))
20429 end (copy-marker (region-end)))
20430 (setq beg (funcall skip-blanks (point-at-bol))
20431 end (copy-marker (point-at-eol))))
20432 ;; Depending on the starting line, choose an action on the text
20433 ;; between BEG and END.
20434 (org-with-limited-levels
20438 ;; Case 1. Start at an item: de-itemize. Note that it only
20439 ;; happens when a region is active: `org-ctrl-c-minus'
20440 ;; would call `org-cycle-list-bullet' otherwise.
20442 (while (< (point) end)
20443 (when (org-at-item-p)
20444 (skip-chars-forward " \t")
20445 (delete-region (point) (match-end 0)))
20447 ;; Case 2. Start at an heading: convert to items.
20448 ((org-at-heading-p)
20449 (let* ((bul (org-list-bullet-string "-"))
20450 (bul-len (length bul))
20451 ;; Indentation of the first heading. It should be
20452 ;; relative to the indentation of its parent, if any.
20453 (start-ind (save-excursion
20455 ((not org-adapt-indentation) 0)
20456 ((not (outline-previous-heading)) 0)
20457 (t (length (match-string 0))))))
20458 ;; Level of first heading. Further headings will be
20459 ;; compared to it to determine hierarchy in the list.
20460 (ref-level (org-reduced-level (org-outline-level))))
20461 (while (< (point) end)
20462 (let* ((level (org-reduced-level (org-outline-level)))
20463 (delta (max 0 (- level ref-level))))
20464 ;; If current headline is less indented than the first
20465 ;; one, set it as reference, in order to preserve
20467 (when (< level ref-level) (setq ref-level level))
20468 (replace-match bul t t)
20469 (org-indent-line-to (+ start-ind (* delta bul-len)))
20470 ;; Ensure all text down to END (or SECTION-END) belongs
20471 ;; to the newly created item.
20472 (let ((section-end (save-excursion
20473 (or (outline-next-heading) (point)))))
20475 (funcall shift-text
20476 (+ start-ind (* (1+ delta) bul-len))
20477 (min end section-end)))))))
20478 ;; Case 3. Normal line with ARG: make the first line of region
20479 ;; an item, and shift indentation of others lines to
20480 ;; set them as item's body.
20481 (arg (let* ((bul (org-list-bullet-string "-"))
20482 (bul-len (length bul))
20483 (ref-ind (org-get-indentation)))
20484 (skip-chars-forward " \t")
20487 (while (< (point) end)
20488 ;; Ensure that lines less indented than first one
20489 ;; still get included in item body.
20490 (funcall shift-text
20491 (+ ref-ind bul-len)
20492 (min end (save-excursion (or (outline-next-heading)
20495 ;; Case 4. Normal line without ARG: turn each non-item line
20498 (while (< (point) end)
20499 (unless (or (org-at-heading-p) (org-at-item-p))
20500 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
20502 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
20503 (forward-line))))))))
20505 (defun org-toggle-heading (&optional nstars)
20506 "Convert headings to normal text, or items or text to headings.
20507 If there is no active region, only convert the current line.
20509 With a \\[universal-argument] prefix, convert the whole list at
20510 point into heading.
20514 - If the first non blank line is a headline, remove the stars
20515 from all headlines in the region.
20517 - If it is a normal line, turn each and every normal line (i.e.,
20518 not an heading or an item) in the region into headings. If you
20519 want to convert only the first line of this region, use one
20520 universal prefix argument.
20522 - If it is a plain list item, turn all plain list items into headings.
20524 When converting a line into a heading, the number of stars is chosen
20525 such that the lines become children of the current entry. However,
20526 when a numeric prefix argument is given, its value determines the
20527 number of stars to add."
20531 ;; Return beginning of first non-blank line, starting from
20536 (while (org-at-comment-p) (forward-line))
20537 (skip-chars-forward " \r\t\n")
20540 ;; Determine boundaries of changes. If a universal prefix has
20541 ;; been given, put the list in a region. If region ends at a bol,
20542 ;; do not consider the last line to be in the region.
20544 (when (and current-prefix-arg (org-at-item-p))
20545 (if (listp current-prefix-arg) (setq current-prefix-arg 1))
20546 (org-mark-element))
20548 (if (org-region-active-p)
20549 (setq beg (funcall skip-blanks (region-beginning))
20550 end (copy-marker (save-excursion
20551 (goto-char (region-end))
20552 (if (bolp) (point) (point-at-eol)))))
20553 (setq beg (funcall skip-blanks (point-at-bol))
20554 end (copy-marker (point-at-eol))))
20555 ;; Ensure inline tasks don't count as headings.
20556 (org-with-limited-levels
20560 ;; Case 1. Started at an heading: de-star headings.
20561 ((org-at-heading-p)
20562 (while (< (point) end)
20563 (when (org-at-heading-p t)
20564 (looking-at org-outline-regexp) (replace-match "")
20567 ;; Case 2. Started at an item: change items into headlines.
20568 ;; One star will be added by `org-list-to-subtree'.
20570 (let* ((stars (make-string
20571 ;; subtract the star that will be added again by
20572 ;; `org-list-to-subtree'
20573 (if (numberp nstars) (1- nstars)
20574 (or (org-current-level) 0))
20577 (cond (nstars "") ; stars from prefix only
20578 ((equal stars "") "") ; before first heading
20579 (org-odd-levels-only "*") ; inside heading, odd
20580 (t "")))) ; inside heading, oddeven
20581 (while (< (point) end)
20582 (when (org-at-item-p)
20583 ;; Pay attention to cases when region ends before list.
20584 (let* ((struct (org-list-struct))
20585 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
20587 (narrow-to-region (point) list-end)
20589 (org-list-to-subtree
20590 (org-list-parse-list t)
20591 '(:istart (concat stars add-stars (funcall get-stars depth))
20592 :icount (concat stars add-stars (funcall get-stars depth)))))))
20595 ;; Case 3. Started at normal text: make every line an heading,
20596 ;; skipping headlines and items.
20599 (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
20601 (cond (nstars "") ; stars from prefix only
20602 ((equal stars "") "*") ; before first heading
20603 (org-odd-levels-only "**") ; inside heading, odd
20604 (t "*"))) ; inside heading, oddeven
20605 (rpl (concat stars add-stars " "))
20606 (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
20607 (while (< (point) (if (equal nstars '(4)) lend end))
20608 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
20609 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
20610 (replace-match (concat rpl (match-string 2))) (setq toggled t))
20611 (forward-line)))))))
20612 (unless toggled (message "Cannot toggle heading from here"))))
20614 (defun org-meta-return (&optional arg)
20615 "Insert a new heading or wrap a region in a table.
20616 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
20617 See the individual commands for more information."
20619 (org-check-before-invisible-edit 'insert)
20621 ((run-hook-with-args-until-success 'org-metareturn-hook))
20622 ((or (org-at-drawer-p) (org-in-drawer-p) (org-at-property-p))
20623 (newline-and-indent))
20625 (call-interactively 'org-table-wrap-region))
20626 (t (call-interactively 'org-insert-heading))))
20630 (defsubst org-in-subtree-not-table-p ()
20631 "Are we in a subtree and not in a table?"
20632 (and (not (org-before-first-heading-p))
20633 (not (org-at-table-p))))
20635 ;; Define the Org-mode menus
20636 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
20638 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
20639 ["Next Field" org-cycle (org-at-table-p)]
20640 ["Previous Field" org-shifttab (org-at-table-p)]
20641 ["Next Row" org-return (org-at-table-p)]
20643 ["Blank Field" org-table-blank-field (org-at-table-p)]
20644 ["Edit Field" org-table-edit-field (org-at-table-p)]
20645 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
20648 ["Move Column Left" org-metaleft (org-at-table-p)]
20649 ["Move Column Right" org-metaright (org-at-table-p)]
20650 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
20651 ["Insert Column" org-shiftmetaright (org-at-table-p)])
20653 ["Move Row Up" org-metaup (org-at-table-p)]
20654 ["Move Row Down" org-metadown (org-at-table-p)]
20655 ["Delete Row" org-shiftmetaup (org-at-table-p)]
20656 ["Insert Row" org-shiftmetadown (org-at-table-p)]
20657 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
20659 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
20661 ["Copy Rectangle" org-copy-special (org-at-table-p)]
20662 ["Cut Rectangle" org-cut-special (org-at-table-p)]
20663 ["Paste Rectangle" org-paste-special (org-at-table-p)]
20664 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
20667 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
20668 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
20669 ["Edit Formulas" org-edit-special (org-at-table-p)]
20671 ["Recalculate line" org-table-recalculate (org-at-table-p)]
20672 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
20673 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
20675 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
20677 ["Sum Column/Rectangle" org-table-sum
20678 (or (org-at-table-p) (org-region-active-p))]
20679 ["Which Column?" org-table-current-column (org-at-table-p)])
20681 org-table-toggle-formula-debugger
20682 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
20683 ["Show Col/Row Numbers"
20684 org-table-toggle-coordinate-overlays
20686 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
20688 ["Create" org-table-create (and (not (org-at-table-p))
20689 org-enable-table-editor)]
20690 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
20691 ["Import from File" org-table-import (not (org-at-table-p))]
20692 ["Export to File" org-table-export (org-at-table-p)]
20694 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
20696 (easy-menu-define org-org-menu org-mode-map "Org menu"
20699 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
20700 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
20701 ["Sparse Tree..." org-sparse-tree t]
20702 ["Reveal Context" org-reveal t]
20703 ["Show All" show-all t]
20705 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
20707 ["New Heading" org-insert-heading t]
20708 ("Navigate Headings"
20709 ["Up" outline-up-heading t]
20710 ["Next" outline-next-visible-heading t]
20711 ["Previous" outline-previous-visible-heading t]
20712 ["Next Same Level" outline-forward-same-level t]
20713 ["Previous Same Level" outline-backward-same-level t]
20715 ["Jump" org-goto t])
20717 ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
20719 ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
20720 ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
20722 ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
20723 ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
20724 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
20726 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
20728 ["Copy visible text" org-copy-visible t]
20730 ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
20731 ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
20732 ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
20733 ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
20735 ["Sort Region/Children" org-sort t]
20737 ["Convert to odd levels" org-convert-to-odd-levels t]
20738 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
20740 ["Emphasis..." org-emphasize t]
20741 ["Edit Source Example" org-edit-special t]
20743 ["Footnote new/jump" org-footnote-action t]
20744 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
20746 ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
20748 ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
20749 ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
20750 ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
20754 ["Store Link (Global)" org-store-link t]
20755 ["Find existing link to here" org-occur-link-in-agenda-files t]
20756 ["Insert Link" org-insert-link t]
20757 ["Follow Link" org-open-at-point t]
20759 ["Next link" org-next-link t]
20760 ["Previous link" org-previous-link t]
20762 ["Descriptive Links"
20763 org-toggle-link-display
20765 :selected org-descriptive-links
20768 org-toggle-link-display
20770 :selected (not org-descriptive-links)])
20773 ["TODO/DONE/-" org-todo t]
20775 ["Next keyword" org-shiftright (org-at-heading-p)]
20776 ["Previous keyword" org-shiftleft (org-at-heading-p)]
20777 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
20778 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
20779 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
20780 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
20781 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
20783 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
20784 :selected org-enforce-todo-dependencies :style toggle :active t]
20785 "Settings for tree at point"
20786 ["Do Children sequentially" org-toggle-ordered-property :style radio
20787 :selected (org-entry-get nil "ORDERED")
20788 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
20789 ["Do Children parallel" org-toggle-ordered-property :style radio
20790 :selected (not (org-entry-get nil "ORDERED"))
20791 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
20793 ["Set Priority" org-priority t]
20794 ["Priority Up" org-shiftup t]
20795 ["Priority Down" org-shiftdown t]
20797 ["Get news from all feeds" org-feed-update-all t]
20798 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
20799 ["Customize feeds" (customize-variable 'org-feed-alist) t])
20800 ("TAGS and Properties"
20801 ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
20802 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
20804 ["Set property" org-set-property (not (org-before-first-heading-p))]
20805 ["Column view of properties" org-columns t]
20806 ["Insert Column View DBlock" org-insert-columns-dblock t])
20807 ("Dates and Scheduling"
20808 ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
20809 ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
20811 ["1 Day Later" org-shiftright (org-at-timestamp-p)]
20812 ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
20813 ["1 ... Later" org-shiftup (org-at-timestamp-p)]
20814 ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
20815 ["Compute Time Range" org-evaluate-time-range t]
20816 ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
20817 ["Deadline" org-deadline (not (org-before-first-heading-p))]
20819 ["Custom time format" org-toggle-time-stamp-overlays
20820 :style radio :selected org-display-custom-times]
20822 ["Goto Calendar" org-goto-calendar t]
20823 ["Date from Calendar" org-date-from-calendar t]
20825 ["Start/Restart Timer" org-timer-start t]
20826 ["Pause/Continue Timer" org-timer-pause-or-continue t]
20827 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
20828 ["Insert Timer String" org-timer t]
20829 ["Insert Timer Item" org-timer-item t])
20831 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
20832 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
20833 ["Clock out" org-clock-out t]
20834 ["Clock cancel" org-clock-cancel t]
20836 ["Mark as default task" org-clock-mark-default-task t]
20837 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
20838 ["Goto running clock" org-clock-goto t]
20840 ["Display times" org-clock-display t]
20841 ["Create clock table" org-clock-report t]
20843 ["Record DONE time"
20844 (progn (setq org-log-done (not org-log-done))
20845 (message "Switching to %s will %s record a timestamp"
20846 (car org-done-keywords)
20847 (if org-log-done "automatically" "not")))
20848 :style toggle :selected org-log-done])
20850 ["Agenda Command..." org-agenda t]
20851 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
20852 ("File List for Agenda")
20853 ("Special views current file"
20854 ["TODO Tree" org-show-todo-tree t]
20855 ["Check Deadlines" org-check-deadlines t]
20856 ["Timeline" org-timeline t]
20857 ["Tags/Property tree" org-match-sparse-tree t])
20859 ["Export/Publish..." org-export-dispatch t]
20861 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
20862 :selected org-cdlatex-mode]
20863 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
20864 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
20865 ["Modify math symbol" org-cdlatex-math-modify
20866 (org-inside-LaTeX-fragment-p)]
20867 ["Insert citation" org-reftex-citation t]
20869 ["Template for BEAMER" (org-beamer-insert-options-template) t])
20872 ["Push Files and Views" org-mobile-push t]
20873 ["Get Captured and Flagged" org-mobile-pull t]
20874 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
20876 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
20879 ["Show Version" org-version t]
20880 ["Info Documentation" org-info t])
20882 ["Browse Org Group" org-customize t]
20884 ["Expand This Menu" org-create-customize-menu
20885 (fboundp 'customize-menu-create)])
20886 ["Send bug report" org-submit-bug-report t]
20889 ["Refresh setup current buffer" org-mode-restart t]
20890 ["Reload Org (after update)" org-reload t]
20891 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
20894 (defun org-info (&optional node)
20895 "Read documentation for Org-mode in the info system.
20896 With optional NODE, go directly to that node."
20898 (info (format "(org)%s" (or node ""))))
20901 (defun org-submit-bug-report ()
20902 "Submit a bug report on Org-mode via mail.
20904 Don't hesitate to report any problems or inaccurate documentation.
20906 If you don't have setup sending mail from (X)Emacs, please copy the
20907 output buffer into your mail program, as it gives us important
20908 information about your Org-mode version and configuration."
20910 (require 'reporter)
20911 (org-load-modules-maybe)
20912 (org-require-autoloaded-modules)
20913 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
20914 (reporter-submit-bug-report
20915 "emacs-orgmode@gnu.org"
20916 (org-version nil 'full)
20918 (save-window-excursion
20919 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
20920 (delete-other-windows)
20922 (insert "You are about to submit a bug report to the Org-mode mailing list.
20924 We would like to add your full Org-mode and Outline configuration to the
20925 bug report. This greatly simplifies the work of the maintainer and
20926 other experts on the mailing list.
20928 HOWEVER, some variables you have customized may contain private
20929 information. The names of customers, colleagues, or friends, might
20930 appear in the form of file names, tags, todo states, or search strings.
20931 If you answer yes to the prompt, you might want to check and remove
20932 such private information before sending the email.")
20933 (add-text-properties (point-min) (point-max) '(face org-warning))
20934 (when (yes-or-no-p "Include your Org-mode configuration ")
20938 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
20939 (or (and (symbol-value v)
20940 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
20942 (get v 'custom-type) (get v 'standard-value)
20943 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
20945 (kill-buffer (get-buffer "*Warn about privacy*"))
20948 "Remember to cover the basics, that is, what you expected to happen and
20949 what in fact did happen. You don't know how to make a good report? See
20951 http://orgmode.org/manual/Feedback.html#Feedback
20953 Your bug report will be posted to the Org-mode mailing list.
20954 ------------------------------------------------------------------------")
20956 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
20957 (replace-match "\\1Bug: \\3 [\\2]")))))
20960 (defun org-install-agenda-files-menu ()
20961 (let ((bl (buffer-list)))
20964 (set-buffer (pop bl))
20965 (if (derived-mode-p 'org-mode) (setq bl nil)))
20966 (when (derived-mode-p 'org-mode)
20968 '("Org") "File List for Agenda"
20971 ["Edit File List" (org-edit-agenda-file-list) t]
20972 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
20973 ["Remove Current File from List" org-remove-file t]
20974 ["Cycle through agenda files" org-cycle-agenda-files t]
20975 ["Occur in all agenda files" org-occur-in-agenda-files t]
20977 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
20981 (defun org-require-autoloaded-modules ()
20984 '(org-agenda org-archive org-attach org-clock org-colview org-id
20985 org-remember org-table org-timer)))
20988 (defun org-reload (&optional uncompiled)
20989 "Reload all org lisp files.
20990 With prefix arg UNCOMPILED, load the uncompiled versions."
20992 (require 'loadhist)
20993 (let* ((org-dir (org-find-library-dir "org"))
20994 (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
20995 (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
20996 (remove-re (mapconcat 'identity
20997 (mapcar (lambda (f) (concat "^" f "$"))
20998 (list (if (featurep 'xemacs)
21000 "org-colview-xemacs")
21001 "org" "org-loaddefs" "org-version"))
21003 (feats (delete-dups
21004 (mapcar 'file-name-sans-extension
21005 (mapcar 'file-name-nondirectory
21007 (mapcar 'feature-file
21014 (if (and (string-match feature-re f)
21015 (not (string-match remove-re f)))
21019 (list "org-version" "org")))
21020 (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
21021 (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
21022 load-uncore load-misses)
21025 (mapcar (lambda (f)
21026 (or (org-load-noerror-mustsuffix (concat org-dir f))
21027 (and (string= org-dir contrib-dir)
21028 (org-load-noerror-mustsuffix (concat contrib-dir f)))
21029 (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
21030 (add-to-list 'load-uncore f 'append)
21035 (message "The following feature%s found in load-path, please check if that's correct:\n%s"
21036 (if (> (length load-uncore) 1) "s were" " was") load-uncore))
21038 (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
21039 (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
21040 (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
21043 (defun org-customize ()
21044 "Call the customize function with org as argument."
21046 (org-load-modules-maybe)
21047 (org-require-autoloaded-modules)
21048 (customize-browse 'org))
21050 (defun org-create-customize-menu ()
21051 "Create a full customization menu for Org-mode, insert it into the menu."
21053 (org-load-modules-maybe)
21054 (org-require-autoloaded-modules)
21055 (if (fboundp 'customize-menu-create)
21058 '("Org") "Customize"
21059 `(["Browse Org group" org-customize t]
21061 ,(customize-menu-create 'org)
21062 ["Set" Custom-set t]
21063 ["Save" Custom-save t]
21064 ["Reset to Current" Custom-reset-current t]
21065 ["Reset to Saved" Custom-reset-saved t]
21066 ["Reset to Standard Settings" Custom-reset-standard t]))
21067 (message "\"Org\"-menu now contains full customization menu"))
21068 (error "Cannot expand menu (outdated version of cus-edit.el)")))
21070 ;;;; Miscellaneous stuff
21072 ;;; Generally useful functions
21074 (defun org-get-at-bol (property)
21075 "Get text property PROPERTY at beginning of line."
21076 (get-text-property (point-at-bol) property))
21078 (defun org-find-text-property-in-string (prop s)
21079 "Return the first non-nil value of property PROP in string S."
21080 (or (get-text-property 0 prop s)
21081 (get-text-property (or (next-single-property-change 0 prop s) 0)
21084 (defun org-display-warning (message) ;; Copied from Emacs-Muse
21085 "Display the given MESSAGE as a warning."
21086 (if (fboundp 'display-warning)
21087 (display-warning 'org message
21088 (if (featurep 'xemacs) 'warning :warning))
21089 (let ((buf (get-buffer-create "*Org warnings*")))
21090 (with-current-buffer buf
21091 (goto-char (point-max))
21092 (insert "Warning (Org): " message)
21095 (display-buffer buf)
21098 (defun org-eval (form)
21099 "Eval FORM and return result."
21100 (condition-case error
21102 (error (format "%%![Error: %s]" error))))
21104 (defun org-in-clocktable-p ()
21105 "Check if the cursor is in a clocktable."
21106 (let ((pos (point)) start)
21109 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
21110 (setq start (match-beginning 0))
21111 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
21112 (>= (match-end 0) pos)
21115 (defun org-in-commented-line ()
21116 "Is point in a line starting with `#'?"
21117 (equal (char-after (point-at-bol)) ?#))
21119 (defun org-in-indented-comment-line ()
21120 "Is point in a line starting with `#' after some white space?"
21123 (goto-char (point-at-bol))
21124 (looking-at "[ \t]*#"))))
21126 (defun org-in-verbatim-emphasis ()
21128 (and (org-in-regexp org-emph-re 2)
21129 (>= (point) (match-beginning 3))
21130 (<= (point) (match-end 4))
21131 (member (match-string 3) '("=" "~")))))
21133 (defun org-goto-marker-or-bmk (marker &optional bookmark)
21134 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
21135 (if (and marker (marker-buffer marker)
21136 (buffer-live-p (marker-buffer marker)))
21138 (org-pop-to-buffer-same-window (marker-buffer marker))
21139 (if (or (> marker (point-max)) (< marker (point-min)))
21142 (org-show-context 'org-goto))
21144 (bookmark-jump bookmark)
21145 (error "Cannot find location"))))
21147 (defun org-quote-csv-field (s)
21148 "Quote field for inclusion in CSV material."
21149 (if (string-match "[\",]" s)
21150 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
21153 (defun org-force-self-insert (N)
21154 "Needed to enforce self-insert under remapping."
21156 (self-insert-command N))
21158 (defun org-string-width (s)
21159 "Compute width of string, ignoring invisible characters.
21160 This ignores character with invisibility property `org-link', and also
21161 characters with property `org-cwidth', because these will become invisible
21162 upon the next fontification round."
21164 (when (or (eq t buffer-invisibility-spec)
21165 (assq 'org-link buffer-invisibility-spec))
21166 (while (setq b (text-property-any 0 (length s)
21167 'invisible 'org-link s))
21168 (setq s (concat (substring s 0 b)
21169 (substring s (or (next-single-property-change
21170 b 'invisible s) (length s)))))))
21171 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
21172 (setq s (concat (substring s 0 b)
21173 (substring s (or (next-single-property-change
21174 b 'org-cwidth s) (length s))))))
21175 (setq l (string-width s) b -1)
21176 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
21177 (setq l (- l (get-text-property b 'org-dwidth-n s))))
21180 (defun org-shorten-string (s maxlength)
21181 "Shorten string S so tht it is no longer than MAXLENGTH characters.
21182 If the string is shorter or has length MAXLENGTH, just return the
21183 original string. If it is longer, the functions finds a space in the
21184 string, breaks this string off at that locations and adds three dots
21185 as ellipsis. Including the ellipsis, the string will not be longer
21186 than MAXLENGTH. If finding a good breaking point in the string does
21187 not work, the string is just chopped off in the middle of a word
21189 (if (<= (length s) maxlength)
21191 (let* ((n (max (- maxlength 4) 1))
21192 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
21193 (if (string-match re s)
21194 (concat (match-string 1 s) "...")
21195 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
21197 (defun org-get-indentation (&optional line)
21198 "Get the indentation of the current line, interpreting tabs.
21199 When LINE is given, assume it represents a line and compute its indentation."
21201 (if (string-match "^ *" (org-remove-tabs line))
21204 (beginning-of-line 1)
21205 (skip-chars-forward " \t")
21206 (current-column))))
21208 (defun org-get-string-indentation (s)
21209 "What indentation has S due to SPACE and TAB at the beginning of the string?"
21210 (let ((n -1) (i 0) (w tab-width) c)
21212 (while (< (setq n (1+ n)) (length s))
21213 (setq c (aref s n))
21214 (cond ((= c ?\ ) (setq i (1+ i)))
21215 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
21216 (t (throw 'exit t)))))
21219 (defun org-remove-tabs (s &optional width)
21220 "Replace tabulators in S with spaces.
21221 Assumes that s is a single line, starting in column 0."
21222 (setq width (or width tab-width))
21223 (while (string-match "\t" s)
21224 (setq s (replace-match
21226 (- (* width (/ (+ (match-beginning 0) width) width))
21227 (match-beginning 0)) ?\ )
21231 (defun org-fix-indentation (line ind)
21232 "Fix indentation in LINE.
21233 IND is a cons cell with target and minimum indentation.
21234 If the current indentation in LINE is smaller than the minimum,
21235 leave it alone. If it is larger than ind, set it to the target."
21236 (let* ((l (org-remove-tabs line))
21237 (i (org-get-indentation l))
21238 (i1 (car ind)) (i2 (cdr ind)))
21239 (if (>= i i2) (setq l (substring line i2)))
21241 (concat (make-string i1 ?\ ) l)
21244 (defun org-remove-indentation (code &optional n)
21245 "Remove the maximum common indentation from the lines in CODE.
21246 N may optionally be the number of spaces to remove."
21249 (org-do-remove-indentation n)
21252 (defun org-do-remove-indentation (&optional n)
21253 "Remove the maximum common indentation from the buffer."
21254 (untabify (point-min) (point-max))
21255 (let ((min 10000) re)
21258 (goto-char (point-min))
21259 (while (re-search-forward "^ *[^ \n]" nil t)
21260 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
21261 (unless (or (= min 0) (= min 10000))
21262 (setq re (format "^ \\{%d\\}" min))
21263 (goto-char (point-min))
21264 (while (re-search-forward re nil t)
21269 (defun org-fill-template (template alist)
21270 "Find each %key of ALIST in TEMPLATE and replace it."
21271 (let ((case-fold-search nil)
21273 (setq alist (sort (copy-sequence alist)
21274 (lambda (a b) (< (length (car a)) (length (car b))))))
21275 (while (setq entry (pop alist))
21277 (replace-regexp-in-string
21278 (concat "%" (regexp-quote (car entry)))
21279 (or (cdr entry) "") template t t)))
21282 (defun org-base-buffer (buffer)
21283 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
21286 (or (buffer-base-buffer buffer)
21289 (defun org-trim (s)
21290 "Remove whitespace at beginning and end of string."
21291 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
21292 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
21295 (defun org-wrap (string &optional width lines)
21296 "Wrap string to either a number of lines, or a width in characters.
21297 If WIDTH is non-nil, the string is wrapped to that width, however many lines
21298 that costs. If there is a word longer than WIDTH, the text is actually
21299 wrapped to the length of that word.
21300 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
21301 many lines, whatever width that takes.
21302 The return value is a list of lines, without newlines at the end."
21303 (let* ((words (org-split-string string "[ \t\n]+"))
21304 (maxword (apply 'max (mapcar 'org-string-width words)))
21307 (org-do-wrap words (max maxword width)))
21310 (setq ll (org-do-wrap words maxword))
21311 (if (<= (length ll) lines)
21314 (while (> (length ll) lines)
21316 (setq ll (org-do-wrap words w)))
21318 (t (error "Cannot wrap this")))))
21320 (defun org-do-wrap (words width)
21321 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
21324 (setq line (pop words))
21325 (while (and words (< (+ (length line) (length (car words))) width))
21326 (setq line (concat line " " (pop words))))
21327 (setq lines (push line lines)))
21330 (defun org-split-string (string &optional separators)
21331 "Splits STRING into substrings at SEPARATORS.
21332 No empty strings are returned if there are matches at the beginning
21333 and end of string."
21334 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
21338 (while (and (string-match rexp string
21340 (= start (match-beginning 0))
21341 (< start (length string)))
21343 (< (match-beginning 0) (length string)))
21345 (or (eq (match-beginning 0) 0)
21346 (and (eq (match-beginning 0) (match-end 0))
21347 (eq (match-beginning 0) start))
21349 (cons (substring string start (match-beginning 0))
21351 (setq start (match-end 0)))
21352 (or (eq start (length string))
21354 (cons (substring string start)
21358 (defun org-quote-vert (s)
21359 "Replace \"|\" with \"\\vert\"."
21360 (while (string-match "|" s)
21361 (setq s (replace-match "\\vert" t t s)))
21364 (defun org-uuidgen-p (s)
21365 "Is S an ID created by UUIDGEN?"
21366 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
21368 (defun org-in-src-block-p (&optional inside)
21369 "Whether point is in a code source block.
21370 When INSIDE is non-nil, don't consider we are within a src block
21371 when point is at #+BEGIN_SRC or #+END_SRC."
21372 (let ((case-fold-search t) ov)
21373 (or (and (setq ov (overlays-at (point)))
21374 (memq 'org-block-background
21375 (overlay-properties (car ov))))
21379 (beginning-of-line)
21380 (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
21382 (defun org-context ()
21383 "Return a list of contexts of the current cursor position.
21384 If several contexts apply, all are returned.
21385 Each context entry is a list with a symbol naming the context, and
21386 two positions indicating start and end of the context. Possible
21389 :headline anywhere in a headline
21390 :headline-stars on the leading stars in a headline
21391 :todo-keyword on a TODO keyword (including DONE) in a headline
21392 :tags on the TAGS in a headline
21393 :priority on the priority cookie in a headline
21394 :item on the first line of a plain list item
21395 :item-bullet on the bullet/number of a plain list item
21396 :checkbox on the checkbox in a plain list item
21397 :table in an org-mode table
21398 :table-special on a special filed in a table
21399 :table-table in a table.el table
21400 :clocktable in a clocktable
21401 :src-block in a source block
21402 :link on a hyperlink
21403 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
21404 :target on a <<target>>
21405 :radio-target on a <<<radio-target>>>
21406 :latex-fragment on a LaTeX fragment
21407 :latex-preview on a LaTeX fragment with overlaid preview image
21409 This function expects the position to be visible because it uses font-lock
21410 faces as a help to recognize the following contexts: :table-special, :link,
21412 (let* ((f (get-text-property (point) 'face))
21413 (faces (if (listp f) f (list f)))
21414 (case-fold-search t)
21415 (p (point)) clist o)
21416 ;; First the large context
21418 ((org-at-heading-p t)
21419 (push (list :headline (point-at-bol) (point-at-eol)) clist)
21421 (beginning-of-line 1)
21422 (looking-at org-todo-line-tags-regexp))
21423 (push (org-point-in-group p 1 :headline-stars) clist)
21424 (push (org-point-in-group p 2 :todo-keyword) clist)
21425 (push (org-point-in-group p 4 :tags) clist))
21427 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
21428 (if (looking-at "\\[#[A-Z0-9]\\]")
21429 (push (org-point-in-group p 0 :priority) clist)))
21432 (push (org-point-in-group p 2 :item-bullet) clist)
21433 (push (list :item (point-at-bol)
21434 (save-excursion (org-end-of-item) (point)))
21436 (and (org-at-item-checkbox-p)
21437 (push (org-point-in-group p 0 :checkbox) clist)))
21440 (push (list :table (org-table-begin) (org-table-end)) clist)
21441 (if (memq 'org-formula faces)
21442 (push (list :table-special
21443 (previous-single-property-change p 'face)
21444 (next-single-property-change p 'face)) clist)))
21445 ((org-at-table-p 'any)
21446 (push (list :table-table) clist)))
21449 (let ((case-fold-search t))
21450 ;; New the "medium" contexts: clocktables, source blocks
21451 (cond ((org-in-clocktable-p)
21452 (push (list :clocktable
21453 (and (or (looking-at "#\\+BEGIN: clocktable")
21454 (search-backward "#+BEGIN: clocktable" nil t))
21455 (match-beginning 0))
21456 (and (re-search-forward "#\\+END:?" nil t)
21457 (match-end 0))) clist))
21458 ((org-in-src-block-p)
21459 (push (list :src-block
21460 (and (or (looking-at "#\\+BEGIN_SRC")
21461 (search-backward "#+BEGIN_SRC" nil t))
21462 (match-beginning 0))
21463 (and (search-forward "#+END_SRC" nil t)
21464 (match-beginning 0))) clist))))
21467 ;; Now the small context
21469 ((org-at-timestamp-p)
21470 (push (org-point-in-group p 0 :timestamp) clist))
21471 ((memq 'org-link faces)
21473 (previous-single-property-change p 'face)
21474 (next-single-property-change p 'face)) clist))
21475 ((memq 'org-special-keyword faces)
21476 (push (list :keyword
21477 (previous-single-property-change p 'face)
21478 (next-single-property-change p 'face)) clist))
21480 (push (org-point-in-group p 0 :target) clist)
21481 (goto-char (1- (match-beginning 0)))
21482 (if (looking-at org-radio-target-regexp)
21483 (push (org-point-in-group p 0 :radio-target) clist))
21485 ((setq o (car (delq nil
21488 (if (memq x org-latex-fragment-image-overlays) x))
21489 (overlays-at (point))))))
21490 (push (list :latex-fragment
21491 (overlay-start o) (overlay-end o)) clist)
21492 (push (list :latex-preview
21493 (overlay-start o) (overlay-end o)) clist))
21494 ((org-inside-LaTeX-fragment-p)
21495 ;; FIXME: positions wrong.
21496 (push (list :latex-fragment (point) (point)) clist)))
21498 (setq clist (nreverse (delq nil clist)))
21501 ;; FIXME: Compare with at-regexp-p Do we need both?
21502 (defun org-in-regexp (re &optional nlines visually)
21503 "Check if point is inside a match of regexp.
21504 Normally only the current line is checked, but you can include NLINES extra
21505 lines both before and after point into the search.
21506 If VISUALLY is set, require that the cursor is not after the match but
21507 really on, so that the block visually is on the match."
21509 (let ((pos (point))
21510 (eol (point-at-eol (+ 1 (or nlines 0))))
21511 (inc (if visually 1 0)))
21513 (beginning-of-line (- 1 (or nlines 0)))
21514 (while (re-search-forward re eol t)
21515 (if (and (<= (match-beginning 0) pos)
21516 (>= (+ inc (match-end 0)) pos))
21517 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
21519 (defun org-at-regexp-p (regexp)
21520 "Is point inside a match of REGEXP in the current line?"
21523 (let ((pos (point)) (end (point-at-eol)))
21524 (beginning-of-line 1)
21525 (while (re-search-forward regexp end t)
21526 (if (and (<= (match-beginning 0) pos)
21527 (>= (match-end 0) pos))
21531 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
21532 "Non-nil when point is between matches of START-RE and END-RE.
21534 Also return a non-nil value when point is on one of the matches.
21536 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
21537 buffer positions. Default values are the positions of headlines
21538 surrounding the point.
21540 The functions returns a cons cell whose car (resp. cdr) is the
21541 position before START-RE (resp. after END-RE)."
21543 (let ((pos (point))
21544 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
21545 (limit-down (or lim-down (save-excursion (outline-next-heading))))
21548 ;; Point is on a block when on START-RE or if START-RE can be
21549 ;; found before it...
21550 (and (or (org-at-regexp-p start-re)
21551 (re-search-backward start-re limit-up t))
21552 (setq beg (match-beginning 0))
21553 ;; ... and END-RE after it...
21554 (goto-char (match-end 0))
21555 (re-search-forward end-re limit-down t)
21556 (> (setq end (match-end 0)) pos)
21557 ;; ... without another START-RE in-between.
21558 (goto-char (match-beginning 0))
21559 (not (re-search-backward start-re (1+ beg) t))
21561 (cons beg end))))))
21563 (defun org-in-block-p (names)
21564 "Non-nil when point belongs to a block whose name belongs to NAMES.
21566 NAMES is a list of strings containing names of blocks.
21568 Return first block name matched, or nil. Beware that in case of
21569 nested blocks, the returned name may not belong to the closest
21573 (let ((case-fold-search t)
21574 (lim-up (save-excursion (outline-previous-heading)))
21575 (lim-down (save-excursion (outline-next-heading))))
21576 (mapc (lambda (name)
21577 (let ((n (regexp-quote name)))
21578 (when (org-between-regexps-p
21579 (concat "^[ \t]*#\\+begin_" n)
21580 (concat "^[ \t]*#\\+end_" n)
21586 (defun org-in-drawer-p ()
21587 "Is point within a drawer?"
21589 (let ((case-fold-search t)
21590 (lim-up (save-excursion (outline-previous-heading)))
21591 (lim-down (save-excursion (outline-next-heading))))
21592 (org-between-regexps-p
21593 (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
21595 lim-up lim-down))))
21597 (defun org-occur-in-agenda-files (regexp &optional nlines)
21598 "Call `multi-occur' with buffers for all agenda files."
21599 (interactive "sOrg-files matching: \np")
21600 (let* ((files (org-agenda-files))
21601 (tnames (mapcar 'file-truename files))
21602 (extra org-agenda-text-search-extra-files)
21604 (when (eq (car extra) 'agenda-archives)
21605 (setq extra (cdr extra))
21606 (setq files (org-add-archive-files files)))
21607 (while (setq f (pop extra))
21608 (unless (member (file-truename f) tnames)
21609 (add-to-list 'files f 'append)
21610 (add-to-list 'tnames (file-truename f) 'append)))
21612 (mapcar (lambda (x)
21613 (with-current-buffer
21614 (or (get-file-buffer x) (find-file-noselect x))
21620 (if (boundp 'occur-mode-find-occurrence-hook)
21622 (add-hook 'occur-mode-find-occurrence-hook
21624 (when (derived-mode-p 'org-mode)
21627 (defadvice occur-mode-goto-occurrence
21628 (after org-occur-reveal activate)
21629 (and (derived-mode-p 'org-mode) (org-reveal)))
21630 (defadvice occur-mode-goto-occurrence-other-window
21631 (after org-occur-reveal activate)
21632 (and (derived-mode-p 'org-mode) (org-reveal)))
21633 (defadvice occur-mode-display-occurrence
21634 (after org-occur-reveal activate)
21635 (when (derived-mode-p 'org-mode)
21636 (let ((pos (occur-mode-find-occurrence)))
21637 (with-current-buffer (marker-buffer pos)
21642 (defun org-occur-link-in-agenda-files ()
21643 "Create a link and search for it in the agendas.
21644 The link is not stored in `org-stored-links', it is just created
21645 for the search purpose."
21647 (let ((link (condition-case nil
21648 (org-store-link nil)
21649 (error "Unable to create a link to here"))))
21650 (org-occur-in-agenda-files (regexp-quote link))))
21652 (defun org-reverse-string (string)
21653 "Return the reverse of STRING."
21654 (apply 'string (reverse (string-to-list string))))
21656 (defsubst org-uniquify (list)
21657 "Non-destructively remove duplicate elements from LIST."
21658 (let ((res (copy-sequence list))) (delete-dups res)))
21660 (defun org-uniquify-alist (alist)
21661 "Merge elements of ALIST with the same key.
21663 For example, in this alist:
21665 \(org-uniquify-alist '((a 1) (b 2) (a 3)))
21666 => '((a 1 3) (b 2))
21668 merge (a 1) and (a 3) into (a 1 3).
21670 The function returns the new ALIST."
21675 (if (not (assoc (car e) rtn))
21677 (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
21678 (setq rtn (assq-delete-all (car e) rtn))
21683 (defun org-delete-all (elts list)
21684 "Remove all elements in ELTS from LIST."
21686 (setq list (delete (pop elts) list)))
21689 (defun org-count (cl-item cl-seq)
21690 "Count the number of occurrences of ITEM in SEQ.
21691 Taken from `count' in cl-seq.el with all keyword arguments removed."
21692 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
21693 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
21694 (while (< cl-start cl-end)
21695 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
21696 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
21697 (setq cl-start (1+ cl-start)))
21700 (defun org-remove-if (predicate seq)
21701 "Remove everything from SEQ that fulfills PREDICATE."
21705 (if (not (funcall predicate e)) (push e res)))
21708 (defun org-remove-if-not (predicate seq)
21709 "Remove everything from SEQ that does not fulfill PREDICATE."
21713 (if (funcall predicate e) (push e res)))
21716 (defun org-reduce (cl-func cl-seq &rest cl-keys)
21717 "Reduce two-argument FUNCTION across SEQ.
21718 Taken from `reduce' in cl-seq.el with all keyword arguments but
21719 \":initial-value\" removed."
21720 (let ((cl-accum (cond ((memq :initial-value cl-keys)
21721 (cadr (memq :initial-value cl-keys)))
21722 (cl-seq (pop cl-seq))
21723 (t (funcall cl-func)))))
21725 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
21728 (defun org-back-over-empty-lines ()
21729 "Move backwards over whitespace, to the beginning of the first empty line.
21730 Returns the number of empty lines passed."
21731 (let ((pos (point)))
21732 (if (cdr (assoc 'heading org-blank-before-new-entry))
21733 (skip-chars-backward " \t\n\r")
21735 (forward-line -1)))
21736 (beginning-of-line 2)
21737 (goto-char (min (point) pos))
21738 (count-lines (point) pos)))
21740 (defun org-skip-whitespace ()
21741 (skip-chars-forward " \t\n\r"))
21743 (defun org-point-in-group (point group &optional context)
21744 "Check if POINT is in match-group GROUP.
21745 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
21746 match. If the match group does not exist or point is not inside it,
21748 (and (match-beginning group)
21749 (>= point (match-beginning group))
21750 (<= point (match-end group))
21752 (list context (match-beginning group) (match-end group))
21755 (defun org-switch-to-buffer-other-window (&rest args)
21756 "Switch to buffer in a second window on the current frame.
21757 In particular, do not allow pop-up frames.
21758 Returns the newly created buffer."
21760 (apply 'switch-to-buffer-other-window args)))
21762 (defun org-combine-plists (&rest plists)
21763 "Create a single property list from all plists in PLISTS.
21764 The process starts by copying the first list, and then setting properties
21765 from the other lists. Settings in the last list are the most significant
21766 ones and overrule settings in the other lists."
21767 (let ((rtn (copy-sequence (pop plists)))
21770 (setq ls (pop plists))
21772 (setq p (pop ls) v (pop ls))
21773 (setq rtn (plist-put rtn p v))))
21776 (defun org-replace-escapes (string table)
21777 "Replace %-escapes in STRING with values in TABLE.
21778 TABLE is an association list with keys like \"%a\" and string values.
21779 The sequences in STRING may contain normal field width and padding information,
21780 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
21781 so values can contain further %-escapes if they are define later in TABLE."
21782 (let ((tbl (copy-alist table))
21783 (case-fold-search nil)
21786 (while (setq e (pop tbl))
21787 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
21788 (when (and (cdr e) (string-match re (cdr e)))
21789 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
21791 (add-text-properties 0 3 (list 'sref sref) safe)
21792 (setcdr e (replace-match safe t t (cdr e)))))
21793 (while (string-match re string)
21794 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
21796 (setq string (replace-match rpl t t string))))
21797 (while (setq pchg (next-property-change pchg string))
21798 (let ((sref (get-text-property pchg 'sref string)))
21799 (when (and sref (string-match "SREF" string pchg))
21800 (setq string (replace-match sref t t string)))))
21803 (defun org-sublist (list start end)
21804 "Return a section of LIST, from START to END.
21805 Counting starts at 1."
21806 (let (rtn (c start))
21807 (setq list (nthcdr (1- start) list))
21808 (while (and list (<= c end))
21809 (push (pop list) rtn)
21813 (defun org-find-base-buffer-visiting (file)
21814 "Like `find-buffer-visiting' but always return the base buffer and
21815 not an indirect buffer."
21816 (let ((buf (or (get-file-buffer file)
21817 (find-buffer-visiting file))))
21819 (or (buffer-base-buffer buf) buf)
21822 (defun org-image-file-name-regexp (&optional extensions)
21823 "Return regexp matching the file names of images.
21824 If EXTENSIONS is given, only match these."
21825 (if (and (not extensions) (fboundp 'image-file-name-regexp))
21826 (image-file-name-regexp)
21827 (let ((image-file-name-extensions
21829 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
21830 "xbm" "xpm" "pbm" "pgm" "ppm"))))
21832 (regexp-opt (nconc (mapcar 'upcase
21833 image-file-name-extensions)
21834 image-file-name-extensions)
21838 (defun org-file-image-p (file &optional extensions)
21839 "Return non-nil if FILE is an image."
21841 (string-match (org-image-file-name-regexp extensions) file)))
21843 (defun org-get-cursor-date (&optional with-time)
21844 "Return the date at cursor in as a time.
21845 This works in the calendar and in the agenda, anywhere else it just
21846 returns the current time.
21847 If WITH-TIME is non-nil, returns the time of the event at point (in
21848 the agenda) or the current time of the day."
21849 (let (date day defd tp tm hod mod)
21851 (setq tp (get-text-property (point) 'time))
21852 (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
21853 (setq hod (string-to-number (match-string 1 tp))
21854 mod (string-to-number (match-string 2 tp))))
21855 (or tp (setq hod (nth 2 (decode-time (current-time)))
21856 mod (nth 1 (decode-time (current-time))))))
21858 ((eq major-mode 'calendar-mode)
21859 (setq date (calendar-cursor-to-date)
21860 defd (encode-time 0 (or mod 0) (or hod 0)
21861 (nth 1 date) (nth 0 date) (nth 2 date))))
21862 ((eq major-mode 'org-agenda-mode)
21863 (setq day (get-text-property (point) 'day))
21865 (setq date (calendar-gregorian-from-absolute day)
21866 defd (encode-time 0 (or mod 0) (or hod 0)
21867 (nth 1 date) (nth 0 date) (nth 2 date))))))
21868 (or defd (current-time))))
21870 (defun org-mark-subtree (&optional up)
21871 "Mark the current subtree.
21872 This puts point at the start of the current subtree, and mark at
21873 the end. If a numeric prefix UP is given, move up into the
21874 hierarchy of headlines by UP levels before marking the subtree."
21876 (org-with-limited-levels
21877 (cond ((org-at-heading-p) (beginning-of-line))
21878 ((org-before-first-heading-p) (user-error "Not in a subtree"))
21879 (t (outline-previous-visible-heading 1))))
21880 (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
21881 (if (org-called-interactively-p 'any)
21882 (call-interactively 'org-mark-element)
21883 (org-mark-element)))
21888 (defun org-indent-line ()
21889 "Indent line depending on context."
21891 (let* ((pos (point))
21892 (itemp (org-at-item-p))
21893 (case-fold-search t)
21894 (org-drawer-regexp (or org-drawer-regexp "\000"))
21895 (inline-task-p (and (featurep 'org-inlinetask)
21896 (org-inlinetask-in-task-p)))
21897 (inline-re (and inline-task-p
21898 (org-inlinetask-outline-regexp)))
21900 (if (and orgstruct-is-++ (eq pos (point)))
21901 (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
21902 (indent-according-to-mode))
21903 (beginning-of-line 1)
21906 ((looking-at org-outline-regexp) (setq column 0))
21907 ;; Footnote definition
21908 ((looking-at org-footnote-definition-re) (setq column 0))
21909 ;; Literal examples
21910 ((looking-at "[ \t]*:\\( \\|$\\)")
21911 (setq column (org-get-indentation))) ; do nothing
21913 ((ignore-errors (goto-char (org-in-item-p)))
21914 (setq column (if itemp
21915 (org-get-indentation)
21916 (org-list-item-body-column (point))))
21919 ((and (looking-at "[ \t]*:END:")
21920 (save-excursion (re-search-backward org-drawer-regexp nil t)))
21922 (goto-char (1- (match-beginning 1)))
21923 (setq column (current-column))))
21925 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
21927 (re-search-backward
21928 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
21929 (setq column (org-get-indentation (match-string 0))))
21930 ((and (not (looking-at "[ \t]*#\\+begin_"))
21931 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
21933 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
21935 (cond ((equal (downcase (match-string 1)) "src")
21936 ;; src blocks: let `org-edit-src-exit' handle them
21937 (org-get-indentation))
21938 ((equal (downcase (match-string 1)) "example")
21939 (max (org-get-indentation)
21940 (org-get-indentation (match-string 0))))
21942 (org-get-indentation (match-string 0))))))
21943 ;; This line has nothing special, look at the previous relevant
21944 ;; line to compute indentation
21946 (beginning-of-line 0)
21947 (while (and (not (bobp))
21948 (not (looking-at org-table-line-regexp))
21949 (not (looking-at org-drawer-regexp))
21950 ;; When point started in an inline task, do not move
21951 ;; above task starting line.
21952 (not (and inline-task-p (looking-at inline-re)))
21953 ;; Skip drawers, blocks, empty lines, verbatim,
21954 ;; comments, tables, footnotes definitions, lists,
21956 (or (and (looking-at "[ \t]*:END:")
21957 (re-search-backward org-drawer-regexp nil t))
21958 (and (looking-at "[ \t]*#\\+end_")
21959 (re-search-backward "[ \t]*#\\+begin_"nil t))
21960 (looking-at "[ \t]*[\n:#|]")
21961 (looking-at org-footnote-definition-re)
21962 (and (not inline-task-p)
21963 (featurep 'org-inlinetask)
21964 (org-inlinetask-in-task-p)
21965 (or (org-inlinetask-goto-beginning) t))))
21966 (beginning-of-line 0))
21968 ;; There was a list item above.
21970 (and (ignore-errors (goto-char (org-in-item-p)))
21972 (org-list-get-top-point (org-list-struct)))))
21973 (looking-at org-list-full-item-re)
21974 (setq column (length (match-string 0))))
21975 ;; There was an heading above.
21976 ((looking-at "\\*+[ \t]+")
21977 (if (not org-adapt-indentation)
21979 (goto-char (match-end 0))
21980 (setq column (current-column))))
21981 ;; A drawer had started and is unfinished
21982 ((looking-at org-drawer-regexp)
21983 (goto-char (1- (match-beginning 1)))
21984 (setq column (current-column)))
21985 ;; Else, nothing noticeable found: get indentation and go on.
21986 (t (setq column (org-get-indentation))))))
21987 ;; Now apply indentation and move cursor accordingly
21989 (if (<= (current-column) (current-indentation))
21990 (org-indent-line-to column)
21991 (save-excursion (org-indent-line-to column)))
21992 ;; Special polishing for properties, see `org-property-format'
21993 (setq column (current-column))
21994 (beginning-of-line 1)
21996 "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
21997 (replace-match (concat (match-string 1)
21998 (format org-property-format
21999 (match-string 2) (match-string 3)))
22001 (org-move-to-column column))))
22003 (defun org-indent-drawer ()
22004 "Indent the drawer at point."
22007 (e (and (save-excursion (re-search-forward ":END:" nil t))
22012 (when (overlays-at (point))
22013 (member 'invisible (overlay-properties
22014 (car (overlays-at (point)))))))))
22015 (when folded (org-cycle))
22016 (indent-for-tab-command)
22017 (while (and (move-beginning-of-line 2) (< (point) e))
22018 (indent-for-tab-command))
22020 (when folded (org-cycle)))
22021 (message "Drawer at point indented"))
22023 (defun org-indent-block ()
22024 "Indent the block at point."
22027 (case-fold-search t)
22028 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
22033 (when (overlays-at (point))
22034 (member 'invisible (overlay-properties
22035 (car (overlays-at (point)))))))))
22036 (when folded (org-cycle))
22037 (indent-for-tab-command)
22038 (while (and (move-beginning-of-line 2) (< (point) e))
22039 (indent-for-tab-command))
22041 (when folded (org-cycle)))
22042 (message "Block at point indented"))
22044 (defun org-indent-region (start end)
22048 (let ((line-end (org-current-line end)))
22050 (while (< (org-current-line) line-end)
22051 (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
22052 (t (call-interactively 'org-indent-line)))
22053 (move-beginning-of-line 2)))))
22058 ;; We use our own fill-paragraph and auto-fill functions.
22060 ;; `org-fill-paragraph' relies on adaptive filling and context
22061 ;; checking. Appropriate `fill-prefix' is computed with
22062 ;; `org-adaptive-fill-function'.
22064 ;; `org-auto-fill-function' takes care of auto-filling. It calls
22065 ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
22066 ;; `org-adaptive-fill-function' value. Internally,
22067 ;; `org-comment-line-break-function' breaks the line.
22069 ;; `org-setup-filling' installs filling and auto-filling related
22070 ;; variables during `org-mode' initialization.
22072 (defun org-setup-filling ()
22074 ;; Prevent auto-fill from inserting unwanted new items.
22075 (when (boundp 'fill-nobreak-predicate)
22077 'fill-nobreak-predicate
22079 (append fill-nobreak-predicate
22080 '(org-fill-paragraph-separate-nobreak-p
22081 org-fill-line-break-nobreak-p
22082 org-fill-paragraph-with-timestamp-nobreak-p)))))
22083 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
22084 (org-set-local 'auto-fill-inhibit-regexp nil)
22085 (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
22086 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
22087 (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
22089 (defvar org-element-paragraph-separate) ; org-element.el
22090 (defun org-fill-paragraph-separate-nobreak-p ()
22091 "Non-nil when a new line at point would end current paragraph."
22092 (looking-at (substring org-element-paragraph-separate 1)))
22094 (defun org-fill-line-break-nobreak-p ()
22095 "Non-nil when a new line at point would create an Org line break."
22097 (skip-chars-backward "[ \t]")
22098 (skip-chars-backward "\\\\")
22099 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
22101 (defun org-fill-paragraph-with-timestamp-nobreak-p ()
22102 "Non-nil when a new line at point would split a timestamp."
22103 (and (org-at-timestamp-p t)
22104 (not (looking-at org-ts-regexp-both))))
22106 (declare-function message-in-body-p "message" ())
22107 (defvar orgtbl-line-start-regexp) ; From org-table.el
22108 (defun org-adaptive-fill-function ()
22109 "Compute a fill prefix for the current line.
22110 Return fill prefix, as a string, or nil if current line isn't
22111 meant to be filled."
22114 (when (derived-mode-p 'message-mode)
22116 (beginning-of-line)
22117 (cond ((or (not (message-in-body-p))
22118 (looking-at orgtbl-line-start-regexp))
22120 ((looking-at message-cite-prefix-regexp)
22121 (throw 'exit (match-string-no-properties 0)))
22122 ((looking-at org-outline-regexp)
22123 (throw 'exit (make-string (length (match-string 0)) ? ))))))
22124 (org-with-wide-buffer
22125 (let* ((p (line-beginning-position))
22126 (element (save-excursion
22127 (beginning-of-line)
22128 (or (ignore-errors (org-element-at-point))
22129 (user-error "An element cannot be parsed line %d"
22130 (line-number-at-pos (point))))))
22131 (type (org-element-type element))
22132 (post-affiliated (org-element-property :post-affiliated element)))
22133 (unless (and post-affiliated (< p post-affiliated))
22135 (comment (looking-at "[ \t]*# ?") (match-string 0))
22136 (footnote-definition "")
22138 (make-string (org-list-item-body-column
22139 (or post-affiliated
22140 (org-element-property :begin element)))
22143 ;; Fill prefix is usually the same as the current line,
22144 ;; except if the paragraph is at the beginning of an item.
22145 (let ((parent (org-element-property :parent element)))
22146 (cond ((eq (org-element-type parent) 'item)
22147 (make-string (org-list-item-body-column
22148 (org-element-property :begin parent))
22150 ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
22154 ;; Only fill contents if P is within block boundaries.
22155 (let* ((cbeg (save-excursion (goto-char post-affiliated)
22158 (cend (save-excursion
22159 (goto-char (org-element-property :end element))
22160 (skip-chars-backward " \r\t\n")
22161 (line-beginning-position))))
22162 (when (and (>= p cbeg) (< p cend))
22163 (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
22167 (declare-function message-goto-body "message" ())
22168 (defvar message-cite-prefix-regexp) ; From message.el
22169 (defun org-fill-paragraph (&optional justify)
22170 "Fill element at point, when applicable.
22172 This function only applies to comment blocks, comments, example
22173 blocks and paragraphs. Also, as a special case, re-align table
22174 when point is at one.
22176 If JUSTIFY is non-nil (interactively, with prefix argument),
22177 justify as well. If `sentence-end-double-space' is non-nil, then
22178 period followed by one space does not end a sentence, so don't
22179 break a line there. The variable `fill-column' controls the
22182 For convenience, when point is at a plain list, an item or
22183 a footnote definition, try to fill the first paragraph within."
22185 (if (and (derived-mode-p 'message-mode)
22186 (or (not (message-in-body-p))
22187 (save-excursion (move-beginning-of-line 1)
22188 (looking-at message-cite-prefix-regexp))))
22189 ;; First ensure filling is correct in message-mode.
22190 (let ((fill-paragraph-function
22191 (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
22192 (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
22193 (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
22194 (paragraph-separate
22195 (cadadr (assoc 'paragraph-separate org-fb-vars))))
22196 (fill-paragraph nil))
22197 (with-syntax-table org-mode-transpose-word-syntax-table
22198 ;; Move to end of line in order to get the first paragraph
22199 ;; within a plain list or a footnote definition.
22200 (let ((element (save-excursion
22202 (or (ignore-errors (org-element-at-point))
22203 (user-error "An element cannot be parsed line %d"
22204 (line-number-at-pos (point)))))))
22205 ;; First check if point is in a blank line at the beginning of
22206 ;; the buffer. In that case, ignore filling.
22207 (case (org-element-type element)
22208 ;; Use major mode filling function is src blocks.
22209 (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
22210 ;; Align Org tables, leave table.el tables as-is.
22211 (table-row (org-table-align) t)
22213 (when (eq (org-element-property :type element) 'org)
22215 (goto-char (org-element-property :post-affiliated element))
22216 (org-table-align)))
22219 ;; Paragraphs may contain `line-break' type objects.
22220 (let ((beg (max (point-min)
22221 (org-element-property :contents-begin element)))
22222 (end (min (point-max)
22223 (org-element-property :contents-end element))))
22224 ;; Do nothing if point is at an affiliated keyword.
22225 (if (< (line-end-position) beg) t
22226 (when (derived-mode-p 'message-mode)
22227 ;; In `message-mode', do not fill following citation
22228 ;; in current paragraph nor text before message body.
22229 (let ((body-start (save-excursion (message-goto-body))))
22230 (when body-start (setq beg (max body-start beg))))
22231 (when (save-excursion
22233 (concat "^" message-cite-prefix-regexp) end t))
22234 (setq end (match-beginning 0))))
22235 ;; Fill paragraph, taking line breaks into account.
22236 ;; For that, slice the paragraph using line breaks as
22237 ;; separators, and fill the parts in reverse order to
22238 ;; avoid messing with markers.
22243 (fill-region-as-paragraph pos (point) justify)
22245 ;; Find the list of ending positions for line breaks
22246 ;; in the current paragraph. Add paragraph
22247 ;; beginning to include first slice.
22251 (org-element--parse-objects
22252 beg end nil (org-element-restriction 'paragraph))
22254 (lambda (lb) (org-element-property :end lb)))))))
22256 ;; Contents of `comment-block' type elements should be
22257 ;; filled as plain text, but only if point is within block
22260 (let* ((case-fold-search t)
22261 (beg (save-excursion
22262 (goto-char (org-element-property :begin element))
22263 (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
22266 (end (save-excursion
22267 (goto-char (org-element-property :end element))
22268 (re-search-backward "^[ \t]*#\\+end_comment" nil t)
22269 (line-beginning-position))))
22270 (when (and (>= (point) beg) (< (point) end))
22271 (fill-region-as-paragraph
22274 (re-search-backward "^[ \t]*$" beg 'move)
22275 (line-beginning-position))
22277 (beginning-of-line)
22278 (re-search-forward "^[ \t]*$" end 'move)
22279 (line-beginning-position))
22283 (comment (fill-comment-paragraph justify))
22284 ;; Ignore every other element.
22287 (defun org-auto-fill-function ()
22288 "Auto-fill function."
22289 ;; Check if auto-filling is meaningful.
22290 (let ((fc (current-fill-column)))
22291 (when (and fc (> (current-column) fc))
22292 (let* ((fill-prefix (org-adaptive-fill-function))
22293 ;; Enforce empty fill prefix, if required. Otherwise, it
22294 ;; will be computed again.
22295 (adaptive-fill-mode (not (equal fill-prefix ""))))
22296 (when fill-prefix (do-auto-fill))))))
22298 (defun org-comment-line-break-function (&optional soft)
22299 "Break line at point and indent, continuing comment if within one.
22300 The inserted newline is marked hard if variable
22301 `use-hard-newlines' is true, unless optional argument SOFT is
22303 (if soft (insert-and-inherit ?\n) (newline 1))
22304 (save-excursion (forward-char -1) (delete-horizontal-space))
22305 (delete-horizontal-space)
22306 (indent-to-left-margin)
22307 (insert-before-markers-and-inherit fill-prefix))
22312 ;; Org comments syntax is quite complex. It requires the entire line
22313 ;; to be just a comment. Also, even with the right syntax at the
22314 ;; beginning of line, some some elements (i.e. verse-block or
22315 ;; example-block) don't accept comments. Usual Emacs comment commands
22316 ;; cannot cope with those requirements. Therefore, Org replaces them.
22318 ;; Org still relies on `comment-dwim', but cannot trust
22319 ;; `comment-only-p'. So, `comment-region-function' and
22320 ;; `uncomment-region-function' both point
22321 ;; to`org-comment-or-uncomment-region'. Eventually,
22322 ;; `org-insert-comment' takes care of insertion of comments at the
22323 ;; beginning of line.
22325 ;; `org-setup-comments-handling' install comments related variables
22326 ;; during `org-mode' initialization.
22328 (defun org-setup-comments-handling ()
22330 (org-set-local 'comment-use-syntax nil)
22331 (org-set-local 'comment-start "# ")
22332 (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
22333 (org-set-local 'comment-insert-comment-function 'org-insert-comment)
22334 (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
22335 (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
22337 (defun org-insert-comment ()
22338 "Insert an empty comment above current line.
22339 If the line is empty, insert comment at its beginning."
22340 (beginning-of-line)
22341 (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
22345 (defvar comment-empty-lines) ; From newcomment.el.
22346 (defun org-comment-or-uncomment-region (beg end &rest ignore)
22347 "Comment or uncomment each non-blank line in the region.
22348 Uncomment each non-blank line between BEG and END if it only
22349 contains commented lines. Otherwise, comment them."
22352 (narrow-to-region (save-excursion (goto-char beg)
22353 (skip-chars-forward " \r\t\n" end)
22354 (line-beginning-position))
22355 (save-excursion (goto-char end)
22356 (skip-chars-backward " \r\t\n" beg)
22357 (line-end-position)))
22359 ;; UNCOMMENTP is non-nil when every non blank line between
22360 ;; BEG and END is a comment.
22362 (goto-char (point-min))
22363 (while (and (not (eobp))
22364 (let ((element (org-element-at-point)))
22365 (and (eq (org-element-type element) 'comment)
22366 (goto-char (min (point-max)
22367 (org-element-property
22368 :end element)))))))
22371 ;; Only blank lines and comments in region: uncomment it.
22373 (goto-char (point-min))
22374 (while (not (eobp))
22375 (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
22376 (replace-match "" nil nil nil 1))
22378 ;; Comment each line in region.
22379 (let ((min-indent (point-max)))
22380 ;; First find the minimum indentation across all lines.
22382 (goto-char (point-min))
22383 (while (and (not (eobp)) (not (zerop min-indent)))
22384 (unless (looking-at "[ \t]*$")
22385 (setq min-indent (min min-indent (current-indentation))))
22387 ;; Then loop over all lines.
22389 (goto-char (point-min))
22390 (while (not (eobp))
22391 (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
22392 ;; Don't get fooled by invisible text (e.g. link path)
22393 ;; when moving to column MIN-INDENT.
22394 (let ((buffer-invisibility-spec nil))
22395 (org-move-to-column min-indent t))
22396 (insert comment-start))
22397 (forward-line))))))))
22402 ;; This section contains tools to operate on timestamp objects, as
22403 ;; returned by, e.g. `org-element-context'.
22405 (defun org-timestamp-has-time-p (timestamp)
22406 "Non-nil when TIMESTAMP has a time specified."
22407 (org-element-property :hour-start timestamp))
22409 (defun org-timestamp-format (timestamp format &optional end utc)
22410 "Format a TIMESTAMP element into a string.
22412 FORMAT is a format specifier to be passed to
22413 `format-time-string'.
22415 When optional argument END is non-nil, use end of date-range or
22416 time-range, if possible.
22418 When optional argument UTC is non-nil, time will be expressed as
22420 (format-time-string
22422 (apply 'encode-time
22425 (lambda (prop) (or (org-element-property prop timestamp) 0))
22426 (if end '(:minute-end :hour-end :day-end :month-end :year-end)
22427 '(:minute-start :hour-start :day-start :month-start
22431 (defun org-timestamp-split-range (timestamp &optional end)
22432 "Extract a timestamp object from a date or time range.
22434 TIMESTAMP is a timestamp object. END, when non-nil, means extract
22435 the end of the range. Otherwise, extract its start.
22437 Return a new timestamp object sharing the same parent as
22439 (let ((type (org-element-property :type timestamp)))
22440 (if (memq type '(active inactive diary)) timestamp
22441 (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
22443 (org-element-put-property
22444 split-ts :type (if (eq type 'active-range) 'active 'inactive))
22445 ;; Copy start properties over end properties if END is
22446 ;; non-nil. Otherwise, copy end properties over `start' ones.
22447 (let ((p-alist '((:minute-start . :minute-end)
22448 (:hour-start . :hour-end)
22449 (:day-start . :day-end)
22450 (:month-start . :month-end)
22451 (:year-start . :year-end))))
22452 (dolist (p-cell p-alist)
22453 (org-element-put-property
22455 (funcall (if end 'car 'cdr) p-cell)
22456 (org-element-property
22457 (funcall (if end 'cdr 'car) p-cell) split-ts)))
22458 ;; Eventually refresh `:raw-value'.
22459 (org-element-put-property split-ts :raw-value nil)
22460 (org-element-put-property
22461 split-ts :raw-value (org-element-interpret-data split-ts)))))))
22463 (defun org-timestamp-translate (timestamp &optional boundary)
22464 "Apply `org-translate-time' on a TIMESTAMP object.
22465 When optional argument BOUNDARY is non-nil, it is either the
22466 symbol `start' or `end'. In this case, only translate the
22467 starting or ending part of TIMESTAMP if it is a date or time
22468 range. Otherwise, translate both parts."
22469 (if (and (not boundary)
22470 (memq (org-element-property :type timestamp)
22471 '(active-range inactive-range)))
22473 (org-translate-time
22474 (org-element-property :raw-value
22475 (org-timestamp-split-range timestamp)))
22477 (org-translate-time
22478 (org-element-property :raw-value
22479 (org-timestamp-split-range timestamp t))))
22480 (org-translate-time
22481 (org-element-property
22483 (if (not boundary) timestamp
22484 (org-timestamp-split-range timestamp (eq boundary 'end)))))))
22490 (defun org-toggle-fixed-width-section (arg)
22491 "Toggle the fixed-width export.
22492 If there is no active region, the QUOTE keyword at the current headline is
22493 inserted or removed. When present, it causes the text between this headline
22494 and the next to be exported as fixed-width text, and unmodified.
22495 If there is an active region, this command adds or removes a colon as the
22496 first character of this line. If the first character of a line is a colon,
22497 this line is also exported in fixed-width font."
22500 (regionp (org-region-active-p))
22501 (beg (if regionp (region-beginning) (point)))
22502 (end (if regionp (region-end)))
22503 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
22504 (case-fold-search nil)
22505 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
22510 (setq cc (current-column))
22511 (beginning-of-line 1)
22512 (setq off (looking-at re))
22513 (while (> nlines 0)
22514 (setq nlines (1- nlines))
22515 (beginning-of-line 1)
22518 (org-move-to-column cc t)
22521 ((and off (looking-at re))
22522 (replace-match "" t t nil 1))
22523 ((not off) (org-move-to-column cc t) (insert ": ")))
22526 (org-back-to-heading)
22528 ((looking-at (format org-heading-keyword-regexp-format
22530 (goto-char (match-end 1))
22531 (looking-at (concat " +" org-quote-string))
22532 (replace-match "" t t)
22533 (when (eolp) (insert " ")))
22534 ((looking-at org-outline-regexp)
22535 (goto-char (match-end 0))
22536 (insert org-quote-string " ")))))))
22538 (defun org-reftex-citation ()
22539 "Use reftex-citation to insert a citation into the buffer.
22540 This looks for a line like
22542 #+BIBLIOGRAPHY: foo plain option:-d
22544 and derives from it that foo.bib is the bibliography file relevant
22545 for this document. It then installs the necessary environment for RefTeX
22546 to work in this buffer and calls `reftex-citation' to insert a citation
22549 Export of such citations to both LaTeX and HTML is handled by the contributed
22550 package org-exp-bibtex by Taru Karttunen."
22552 (let ((reftex-docstruct-symbol 'rds)
22553 (reftex-cite-format "\\cite{%l}")
22558 (let ((case-fold-search t)
22559 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
22560 (if (not (save-excursion
22561 (or (re-search-forward re nil t)
22562 (re-search-backward re nil t))))
22563 (error "No bibliography defined in file")
22564 (setq bib (concat (match-string 1) ".bib")
22565 rds (list (list 'bib bib)))))))
22566 (call-interactively 'reftex-citation)))
22568 ;;;; Functions extending outline functionality
22570 (defun org-beginning-of-line (&optional arg)
22571 "Go to the beginning of the current line. If that is invisible, continue
22572 to a visible line beginning. This makes the function of C-a more intuitive.
22573 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
22574 first attempt, and only move to after the tags when the cursor is already
22575 beyond the end of the headline."
22577 (let ((pos (point))
22578 (special (if (consp org-special-ctrl-a/e)
22579 (car org-special-ctrl-a/e)
22580 org-special-ctrl-a/e))
22582 (if (org-bound-and-true-p visual-line-mode)
22583 (beginning-of-visual-line 1)
22584 (beginning-of-line 1))
22585 (if (and arg (fboundp 'move-beginning-of-line))
22586 (call-interactively 'move-beginning-of-line)
22590 (if (org-truely-invisible-p)
22591 (while (and (not (bobp)) (org-truely-invisible-p))
22593 (beginning-of-line 1))
22594 (forward-char 1))))
22597 ((and (looking-at org-complex-heading-regexp)
22598 (= (char-after (match-end 1)) ?\ ))
22599 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
22603 (cond ((> pos refpos) refpos)
22604 ((= pos (point)) refpos)
22606 (cond ((> pos (point)) (point))
22607 ((not (eq last-command this-command)) (point))
22610 ;; Being at an item and not looking at an the item means point
22611 ;; was previously moved to beginning of a visual line, which
22612 ;; doesn't contain the item. Therefore, do nothing special,
22614 (when (looking-at org-list-full-item-re)
22615 ;; Set special position at first white space character after
22616 ;; bullet, and check-box, if any.
22617 (let ((after-bullet
22618 (let ((box (match-end 3)))
22619 (if (not box) (match-end 1)
22620 (let ((after (char-after box)))
22621 (if (and after (= after ? )) (1+ box) box))))))
22622 ;; Special case: Move point to special position when
22623 ;; currently after it or at beginning of line.
22625 (when (or (> pos after-bullet) (= (point) pos))
22626 (goto-char after-bullet))
22627 ;; Reversed case: Move point to special position when
22628 ;; point was already at beginning of line and command is
22630 (when (and (= (point) pos) (eq last-command this-command))
22631 (goto-char after-bullet))))))))
22633 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
22635 (defun org-end-of-line (&optional arg)
22636 "Go to the end of the line.
22637 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
22638 tags on the first attempt, and only move to after the tags when
22639 the cursor is already beyond the end of the headline."
22641 (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
22642 org-special-ctrl-a/e))
22643 (move-fun (cond ((org-bound-and-true-p visual-line-mode)
22644 'end-of-visual-line)
22645 ((fboundp 'move-end-of-line) 'move-end-of-line)
22646 (t 'end-of-line))))
22647 (if (or (not special) arg) (call-interactively move-fun)
22648 (let* ((element (save-excursion (beginning-of-line)
22649 (org-element-at-point)))
22650 (type (org-element-type element)))
22652 ((memq type '(headline inlinetask))
22653 (let ((pos (point)))
22654 (beginning-of-line 1)
22655 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
22657 (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
22658 (goto-char (match-beginning 1))
22659 (goto-char (match-end 0)))
22660 (if (or (< pos (match-end 0))
22661 (not (eq this-command last-command)))
22662 (goto-char (match-end 0))
22663 (goto-char (match-beginning 1))))
22664 (call-interactively move-fun))))
22665 ((org-element-property :hiddenp element)
22666 ;; If element is hidden, `move-end-of-line' would put point
22667 ;; after it. Use `end-of-line' to stay on current line.
22668 (call-interactively 'end-of-line))
22669 (t (call-interactively move-fun)))))
22670 (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
22672 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
22673 (define-key org-mode-map "\C-e" 'org-end-of-line)
22675 (defun org-backward-sentence (&optional arg)
22676 "Go to beginning of sentence, or beginning of table field.
22677 This will call `backward-sentence' or `org-table-beginning-of-field',
22678 depending on context."
22681 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
22682 (t (call-interactively 'backward-sentence))))
22684 (defun org-forward-sentence (&optional arg)
22685 "Go to end of sentence, or end of table field.
22686 This will call `forward-sentence' or `org-table-end-of-field',
22687 depending on context."
22690 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
22691 (t (call-interactively 'forward-sentence))))
22693 (define-key org-mode-map "\M-a" 'org-backward-sentence)
22694 (define-key org-mode-map "\M-e" 'org-forward-sentence)
22696 (defun org-kill-line (&optional arg)
22697 "Kill line, to tags or end of line."
22700 ((or (not org-special-ctrl-k)
22702 (not (org-at-heading-p)))
22703 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
22704 org-ctrl-k-protect-subtree)
22705 (if (or (eq org-ctrl-k-protect-subtree 'error)
22706 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
22707 (user-error "C-k aborted as it would kill a hidden subtree")))
22708 (call-interactively
22709 (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
22710 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
22711 (kill-region (point) (match-beginning 1))
22712 (org-set-tags nil t))
22713 (t (kill-region (point) (point-at-eol)))))
22715 (define-key org-mode-map "\C-k" 'org-kill-line)
22717 (defun org-yank (&optional arg)
22718 "Yank. If the kill is a subtree, treat it specially.
22719 This command will look at the current kill and check if is a single
22720 subtree, or a series of subtrees[1]. If it passes the test, and if the
22721 cursor is at the beginning of a line or after the stars of a currently
22722 empty headline, then the yank is handled specially. How exactly depends
22723 on the value of the following variables, both set by default.
22725 org-yank-folded-subtrees
22726 When set, the subtree(s) will be folded after insertion, but only
22727 if doing so would now swallow text after the yanked text.
22729 org-yank-adjusted-subtrees
22730 When set, the subtree will be promoted or demoted in order to
22731 fit into the local outline tree structure, which means that the level
22732 will be adjusted so that it becomes the smaller one of the two
22733 *visible* surrounding headings.
22735 Any prefix to this command will cause `yank' to be called directly with
22736 no special treatment. In particular, a simple \\[universal-argument] prefix \
22738 plainly yank the text as it is.
22740 \[1] The test checks if the first non-white line is a heading
22741 and if there are no other headings with fewer stars."
22743 (org-yank-generic 'yank arg))
22745 (defun org-yank-generic (command arg)
22746 "Perform some yank-like command.
22748 This function implements the behavior described in the `org-yank'
22749 documentation. However, it has been generalized to work for any
22750 interactive command with similar behavior."
22752 ;; pretend to be command COMMAND
22753 (setq this-command command)
22756 (call-interactively command)
22758 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
22759 (and (org-kill-is-subtree-p)
22761 (and (looking-at "[ \t]*$")
22764 (buffer-substring (point-at-bol) (point)))))))
22767 ((and subtreep org-yank-folded-subtrees)
22768 (let ((beg (point))
22770 (if (and subtreep org-yank-adjusted-subtrees)
22771 (org-paste-subtree nil nil 'for-yank)
22772 (call-interactively command))
22776 (when (and (bolp) subtreep
22777 (not (setq swallowp
22778 (org-yank-folding-would-swallow-text beg end))))
22779 (org-with-limited-levels
22780 (or (looking-at org-outline-regexp)
22781 (re-search-forward org-outline-regexp-bol end t))
22782 (while (and (< (point) end) (looking-at org-outline-regexp))
22784 (org-cycle-show-empty-lines 'folded)
22785 (condition-case nil
22786 (outline-forward-same-level 1)
22787 (error (goto-char end))))))
22790 "Inserted text not folded because that would swallow text"))
22793 (skip-chars-forward " \t\n\r")
22794 (beginning-of-line 1)
22795 (push-mark beg 'nomsg)))
22796 ((and subtreep org-yank-adjusted-subtrees)
22797 (let ((beg (point-at-bol)))
22798 (org-paste-subtree nil nil 'for-yank)
22799 (push-mark beg 'nomsg)))
22801 (call-interactively command))))))
22803 (defun org-yank-folding-would-swallow-text (beg end)
22804 "Would hide-subtree at BEG swallow any text after END?"
22806 (org-with-limited-levels
22809 (when (or (looking-at org-outline-regexp)
22810 (re-search-forward org-outline-regexp-bol end t))
22811 (setq level (org-outline-level)))
22813 (skip-chars-forward " \t\r\n\v\f")
22815 (and (bolp) (looking-at org-outline-regexp)
22816 (<= (org-outline-level) level)))
22817 nil ; Nothing would be swallowed
22818 t))))) ; something would swallow
22820 (define-key org-mode-map "\C-y" 'org-yank)
22822 (defun org-truely-invisible-p ()
22823 "Check if point is at a character currently not visible.
22824 This version does not only check the character property, but also
22826 ;; Early versions of noutline don't have `outline-invisible-p'.
22827 (if (org-bound-and-true-p visible-mode)
22829 (outline-invisible-p)))
22831 (defun org-invisible-p2 ()
22832 "Check if point is at a character currently not visible."
22834 (if (and (eolp) (not (bobp))) (backward-char 1))
22835 ;; Early versions of noutline don't have `outline-invisible-p'.
22836 (outline-invisible-p)))
22838 (defun org-back-to-heading (&optional invisible-ok)
22839 "Call `outline-back-to-heading', but provide a better error message."
22840 (condition-case nil
22841 (outline-back-to-heading invisible-ok)
22842 (error (error "Before first headline at position %d in buffer %s"
22843 (point) (current-buffer)))))
22845 (defun org-before-first-heading-p ()
22846 "Before first heading?"
22849 (null (re-search-backward org-outline-regexp-bol nil t))))
22851 (defun org-at-heading-p (&optional ignored)
22852 (outline-on-heading-p t))
22853 ;; Compatibility alias with Org versions < 7.8.03
22854 (defalias 'org-on-heading-p 'org-at-heading-p)
22856 (defun org-at-comment-p nil
22857 "Is cursor in a line starting with a # character?"
22859 (beginning-of-line)
22860 (looking-at "^#")))
22862 (defun org-at-drawer-p nil
22863 "Is cursor at a drawer keyword?"
22865 (move-beginning-of-line 1)
22866 (looking-at org-drawer-regexp)))
22868 (defun org-at-block-p nil
22869 "Is cursor at a block keyword?"
22871 (move-beginning-of-line 1)
22872 (looking-at org-block-regexp)))
22874 (defun org-point-at-end-of-empty-headline ()
22875 "If point is at the end of an empty headline, return t, else nil.
22876 If the heading only contains a TODO keyword, it is still still considered
22878 (and (looking-at "[ \t]*$")
22879 (when org-todo-line-regexp
22881 (beginning-of-line 1)
22882 (let ((case-fold-search nil))
22883 (looking-at org-todo-line-regexp)
22884 (string= (match-string 3) ""))))))
22886 (defun org-at-heading-or-item-p ()
22887 (or (org-at-heading-p) (org-at-item-p)))
22889 (defun org-at-target-p ()
22890 (or (org-in-regexp org-radio-target-regexp)
22891 (org-in-regexp org-target-regexp)))
22892 ;; Compatibility alias with Org versions < 7.8.03
22893 (defalias 'org-on-target-p 'org-at-target-p)
22895 (defun org-up-heading-all (arg)
22896 "Move to the heading line of which the present line is a subheading.
22897 This function considers both visible and invisible heading lines.
22898 With argument, move up ARG levels."
22899 (if (fboundp 'outline-up-heading-all)
22900 (outline-up-heading-all arg) ; emacs 21 version of outline.el
22901 (outline-up-heading arg t))) ; emacs 22 version of outline.el
22903 (defun org-up-heading-safe ()
22904 "Move to the heading line of which the present line is a subheading.
22905 This version will not throw an error. It will return the level of the
22906 headline found, or nil if no higher level is found.
22908 Also, this function will be a lot faster than `outline-up-heading',
22909 because it relies on stars being the outline starters. This can really
22910 make a significant difference in outlines with very many siblings."
22911 (let (start-level re)
22912 (org-back-to-heading t)
22913 (setq start-level (funcall outline-level))
22914 (if (equal start-level 1)
22916 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
22917 (if (re-search-backward re nil t)
22918 (funcall outline-level)))))
22920 (defun org-first-sibling-p ()
22921 "Is this heading the first child of its parents?"
22923 (let ((re org-outline-regexp-bol)
22925 (unless (org-at-heading-p t)
22926 (user-error "Not at a heading"))
22927 (setq level (funcall outline-level))
22929 (if (not (re-search-backward re nil t))
22931 (setq l (funcall outline-level))
22934 (defun org-goto-sibling (&optional previous)
22935 "Goto the next sibling, even if it is invisible.
22936 When PREVIOUS is set, go to the previous sibling instead. Returns t
22937 when a sibling was found. When none is found, return nil and don't
22939 (let ((fun (if previous 're-search-backward 're-search-forward))
22941 (re org-outline-regexp-bol)
22943 (when (condition-case nil (org-back-to-heading t) (error nil))
22944 (setq level (funcall outline-level))
22946 (or previous (forward-char 1))
22947 (while (funcall fun re nil t)
22948 (setq l (funcall outline-level))
22949 (when (< l level) (goto-char pos) (throw 'exit nil))
22950 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
22954 (defun org-show-siblings ()
22955 "Show all siblings of the current headline."
22957 (while (org-goto-sibling) (org-flag-heading nil)))
22959 (while (org-goto-sibling 'previous)
22960 (org-flag-heading nil))))
22962 (defun org-goto-first-child ()
22963 "Goto the first child, even if it is invisible.
22964 Return t when a child was found. Otherwise don't move point and
22966 (let (level (pos (point)) (re org-outline-regexp-bol))
22967 (when (condition-case nil (org-back-to-heading t) (error nil))
22968 (setq level (outline-level))
22970 (if (and (re-search-forward re nil t) (> (outline-level) level))
22971 (progn (goto-char (match-beginning 0)) t)
22972 (goto-char pos) nil))))
22974 (defun org-show-hidden-entry ()
22975 "Show an entry where even the heading is hidden."
22979 (defun org-flag-heading (flag &optional entry)
22980 "Flag the current heading. FLAG non-nil means make invisible.
22981 When ENTRY is non-nil, show the entire entry."
22983 (org-back-to-heading t)
22984 ;; Check if we should show the entire entry
22989 (and (outline-next-heading)
22990 (org-flag-heading nil))))
22991 (outline-flag-region (max (point-min) (1- (point)))
22992 (save-excursion (outline-end-of-heading) (point))
22995 (defun org-get-next-sibling ()
22996 "Move to next heading of the same level, and return point.
22997 If there is no such heading, return nil.
22998 This is like outline-next-sibling, but invisible headings are ok."
22999 (let ((level (funcall outline-level)))
23000 (outline-next-heading)
23001 (while (and (not (eobp)) (> (funcall outline-level) level))
23002 (outline-next-heading))
23003 (if (or (eobp) (< (funcall outline-level) level))
23007 (defun org-get-last-sibling ()
23008 "Move to previous heading of the same level, and return point.
23009 If there is no such heading, return nil."
23010 (let ((opoint (point))
23011 (level (funcall outline-level)))
23012 (outline-previous-heading)
23013 (when (and (/= (point) opoint) (outline-on-heading-p t))
23014 (while (and (> (funcall outline-level) level)
23016 (outline-previous-heading))
23017 (if (< (funcall outline-level) level)
23021 (defun org-end-of-subtree (&optional invisible-ok to-heading)
23022 "Goto to the end of a subtree."
23023 ;; This contains an exact copy of the original function, but it uses
23024 ;; `org-back-to-heading', to make it work also in invisible
23025 ;; trees. And is uses an invisible-ok argument.
23026 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
23027 ;; Furthermore, when used inside Org, finding the end of a large subtree
23028 ;; with many children and grandchildren etc, this can be much faster
23029 ;; than the outline version.
23030 (org-back-to-heading invisible-ok)
23032 (level (funcall outline-level)))
23033 (if (and (derived-mode-p 'org-mode) (< level 1000))
23034 ;; A true heading (not a plain list item), in Org-mode
23035 ;; This means we can easily find the end by looking
23036 ;; only for the right number of stars. Using a regexp to do
23037 ;; this is so much faster than using a Lisp loop.
23038 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
23040 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
23041 ;; something else, do it the slow way
23042 (while (and (not (eobp))
23043 (or first (> (funcall outline-level) level)))
23045 (outline-next-heading)))
23047 (if (memq (preceding-char) '(?\n ?\^M))
23049 ;; Go to end of line before heading
23051 (if (memq (preceding-char) '(?\n ?\^M))
23052 ;; leave blank line before heading
23053 (forward-char -1))))))
23056 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
23057 "Use Org version in org-mode, for dramatic speed-up."
23058 (if (derived-mode-p 'org-mode)
23060 (org-end-of-subtree nil t)
23061 (unless (eobp) (backward-char 1)))
23064 (defun org-end-of-meta-data-and-drawers ()
23065 "Jump to the first text after meta data and drawers in the current entry.
23066 This will move over empty lines, lines with planning time stamps,
23067 clocking lines, and drawers."
23068 (org-back-to-heading t)
23069 (let ((end (save-excursion (outline-next-heading) (point)))
23070 (re (concat "\\(" org-drawer-regexp "\\)"
23071 "\\|" "[ \t]*" org-keyword-time-regexp)))
23073 (while (re-search-forward re end t)
23074 (if (not (match-end 1))
23075 ;; empty or planning line
23077 ;; a drawer, find the end
23078 (re-search-forward "^[ \t]*:END:" end 'move)
23080 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
23083 (defun org-forward-heading-same-level (arg &optional invisible-ok)
23084 "Move forward to the ARG'th subheading at same level as this one.
23085 Stop at the first and last subheadings of a superior heading.
23086 Normally this only looks at visible headings, but when INVISIBLE-OK is
23087 non-nil it will also look at invisible ones."
23089 (if (not (ignore-errors (org-back-to-heading invisible-ok)))
23090 (if (and arg (< arg 0))
23091 (goto-char (point-min))
23092 (outline-next-heading))
23094 (let ((level (- (match-end 0) (match-beginning 0) 1))
23095 (f (if (and arg (< arg 0))
23096 're-search-backward
23097 're-search-forward))
23098 (count (if arg (abs arg) 1))
23100 (while (and (prog1 (> count 0)
23101 (forward-char (if (and arg (< arg 0)) -1 1)))
23102 (funcall f org-outline-regexp-bol nil 'move))
23103 (let ((l (- (match-end 0) (match-beginning 0) 1)))
23104 (cond ((< l level) (setq count 0))
23108 (goto-char (line-beginning-position))
23109 (not (outline-invisible-p)))))
23110 (setq count (1- count))
23112 (setq result (point)))))))
23113 (goto-char result))
23114 (beginning-of-line 1)))
23116 (defun org-backward-heading-same-level (arg &optional invisible-ok)
23117 "Move backward to the ARG'th subheading at same level as this one.
23118 Stop at the first and last subheadings of a superior heading."
23120 (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
23122 (defun org-next-block (arg &optional backward block-regexp)
23123 "Jump to the next block.
23124 With a prefix argument ARG, jump forward ARG many source blocks.
23125 When BACKWARD is non-nil, jump to the previous block.
23126 When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
23128 (let ((re (or block-regexp org-block-regexp))
23129 (re-search-fn (or (and backward 're-search-backward)
23130 're-search-forward)))
23131 (if (looking-at re) (forward-char 1))
23132 (condition-case nil
23133 (funcall re-search-fn re nil nil arg)
23134 (error (error "No %s code blocks" (if backward "previous" "further" ))))
23135 (goto-char (match-beginning 0)) (org-show-context)))
23137 (defun org-previous-block (arg &optional block-regexp)
23138 "Jump to the previous block.
23139 With a prefix argument ARG, jump backward ARG many source blocks.
23140 When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
23142 (org-next-block arg t block-regexp))
23144 (defun org-forward-element ()
23145 "Move forward by one element.
23146 Move to the next element at the same level, when possible."
23148 (cond ((eobp) (user-error "Cannot move further down"))
23149 ((org-with-limited-levels (org-at-heading-p))
23150 (let ((origin (point)))
23151 (goto-char (org-end-of-subtree nil t))
23152 (unless (org-with-limited-levels (org-at-heading-p))
23154 (user-error "Cannot move further down"))))
23156 (let* ((elem (org-element-at-point))
23157 (end (org-element-property :end elem))
23158 (parent (org-element-property :parent elem)))
23159 (if (and parent (= (org-element-property :contents-end parent) end))
23160 (goto-char (org-element-property :end parent))
23161 (goto-char end))))))
23163 (defun org-backward-element ()
23164 "Move backward by one element.
23165 Move to the previous element at the same level, when possible."
23167 (cond ((bobp) (user-error "Cannot move further up"))
23168 ((org-with-limited-levels (org-at-heading-p))
23169 ;; At a headline, move to the previous one, if any, or stay
23171 (let ((origin (point)))
23172 (org-with-limited-levels (org-backward-heading-same-level 1))
23173 ;; When current headline has no sibling above, move to its
23175 (when (= (point) origin)
23176 (or (org-with-limited-levels (org-up-heading-safe))
23177 (progn (goto-char origin)
23178 (user-error "Cannot move further up"))))))
23180 (let* ((trail (org-element-at-point 'keep-trail))
23182 (prev-elem (nth 1 trail))
23183 (beg (org-element-property :begin elem)))
23185 ;; Move to beginning of current element if point isn't
23187 ((/= (point) beg) (goto-char beg))
23188 (prev-elem (goto-char (org-element-property :begin prev-elem)))
23189 ((org-before-first-heading-p) (goto-char (point-min)))
23190 (t (org-back-to-heading)))))))
23192 (defun org-up-element ()
23193 "Move to upper element."
23195 (if (org-with-limited-levels (org-at-heading-p))
23196 (unless (org-up-heading-safe) (user-error "No surrounding element"))
23197 (let* ((elem (org-element-at-point))
23198 (parent (org-element-property :parent elem)))
23199 (if parent (goto-char (org-element-property :begin parent))
23200 (if (org-with-limited-levels (org-before-first-heading-p))
23201 (user-error "No surrounding element")
23202 (org-with-limited-levels (org-back-to-heading)))))))
23204 (defvar org-element-greater-elements)
23205 (defun org-down-element ()
23206 "Move to inner element."
23208 (let ((element (org-element-at-point)))
23210 ((memq (org-element-type element) '(plain-list table))
23211 (goto-char (org-element-property :contents-begin element))
23213 ((memq (org-element-type element) org-element-greater-elements)
23214 ;; If contents are hidden, first disclose them.
23215 (when (org-element-property :hiddenp element) (org-cycle))
23216 (goto-char (or (org-element-property :contents-begin element)
23217 (user-error "No content for this element"))))
23218 (t (user-error "No inner element")))))
23220 (defun org-drag-element-backward ()
23221 "Move backward element at point."
23223 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
23224 (let* ((trail (org-element-at-point 'keep-trail))
23226 (prev-elem (nth 1 trail)))
23227 ;; Error out if no previous element or previous element is
23228 ;; a parent of the current one.
23229 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
23230 (user-error "Cannot drag element backward")
23231 (let ((pos (point)))
23232 (org-element-swap-A-B prev-elem elem)
23233 (goto-char (+ (org-element-property :begin prev-elem)
23234 (- pos (org-element-property :begin elem)))))))))
23236 (defun org-drag-element-forward ()
23237 "Move forward element at point."
23239 (let* ((pos (point))
23240 (elem (org-element-at-point)))
23241 (when (= (point-max) (org-element-property :end elem))
23242 (user-error "Cannot drag element forward"))
23243 (goto-char (org-element-property :end elem))
23244 (let ((next-elem (org-element-at-point)))
23245 (when (or (org-element-nested-p elem next-elem)
23246 (and (eq (org-element-type next-elem) 'headline)
23247 (not (eq (org-element-type elem) 'headline))))
23249 (user-error "Cannot drag element forward"))
23250 ;; Compute new position of point: it's shifted by NEXT-ELEM
23251 ;; body's length (without final blanks) and by the length of
23252 ;; blanks between ELEM and NEXT-ELEM.
23253 (let ((size-next (- (save-excursion
23254 (goto-char (org-element-property :end next-elem))
23255 (skip-chars-backward " \r\t\n")
23257 ;; Small correction if buffer doesn't end
23258 ;; with a newline character.
23259 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
23260 (org-element-property :begin next-elem)))
23261 (size-blank (- (org-element-property :end elem)
23263 (goto-char (org-element-property :end elem))
23264 (skip-chars-backward " \r\t\n")
23267 (org-element-swap-A-B elem next-elem)
23268 (goto-char (+ pos size-next size-blank))))))
23270 (defun org-drag-line-forward (arg)
23271 "Drag the line at point ARG lines forward."
23273 (dotimes (n (abs arg))
23274 (let ((c (current-column)))
23277 (beginning-of-line 2)
23278 (transpose-lines 1)
23279 (beginning-of-line 0))
23280 (transpose-lines 1)
23281 (beginning-of-line -1))
23282 (org-move-to-column c))))
23284 (defun org-drag-line-backward (arg)
23285 "Drag the line at point ARG lines backward."
23287 (org-drag-line-forward (- arg)))
23289 (defun org-mark-element ()
23290 "Put point at beginning of this element, mark at end.
23292 Interactively, if this command is repeated or (in Transient Mark
23293 mode) if the mark is active, it marks the next element after the
23294 ones already marked."
23296 (let (deactivate-mark)
23297 (if (and (org-called-interactively-p 'any)
23298 (or (and (eq last-command this-command) (mark t))
23299 (and transient-mark-mode mark-active)))
23303 (goto-char (org-element-property :end (org-element-at-point)))))
23304 (let ((element (org-element-at-point)))
23306 (push-mark (org-element-property :end element) t t)
23307 (goto-char (org-element-property :begin element))))))
23309 (defun org-narrow-to-element ()
23310 "Narrow buffer to current element."
23312 (let ((elem (org-element-at-point)))
23314 ((eq (car elem) 'headline)
23316 (org-element-property :begin elem)
23317 (org-element-property :end elem)))
23318 ((memq (car elem) org-element-greater-elements)
23320 (org-element-property :contents-begin elem)
23321 (org-element-property :contents-end elem)))
23324 (org-element-property :begin elem)
23325 (org-element-property :end elem))))))
23327 (defun org-transpose-element ()
23328 "Transpose current and previous elements, keeping blank lines between.
23329 Point is moved after both elements."
23331 (org-skip-whitespace)
23332 (let ((end (org-element-property :end (org-element-at-point))))
23333 (org-drag-element-backward)
23336 (defun org-unindent-buffer ()
23337 "Un-indent the visible part of the buffer.
23338 Relative indentation (between items, inside blocks, etc.) isn't
23341 (unless (eq major-mode 'org-mode)
23342 (user-error "Cannot un-indent a buffer not in Org mode"))
23343 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
23344 unindent-tree ; For byte-compiler.
23350 (if (memq (org-element-type element) '(headline section))
23351 (funcall unindent-tree (org-element-contents element))
23355 (org-element-property :begin element)
23356 (org-element-property :end element))
23357 (org-do-remove-indentation)))))
23358 (reverse contents))))))
23359 (funcall unindent-tree (org-element-contents parse-tree))))
23361 (defun org-show-subtree ()
23362 "Show everything after this heading at deeper levels."
23364 (outline-flag-region
23367 (org-end-of-subtree t t))
23370 (defun org-show-entry ()
23371 "Show the body directly following this heading.
23372 Show the heading too, if it is currently invisible."
23375 (condition-case nil
23377 (org-back-to-heading t)
23378 (outline-flag-region
23379 (max (point-min) (1- (point)))
23381 (if (re-search-forward
23382 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
23383 (match-beginning 1)
23386 (org-cycle-hide-drawers 'children))
23389 (defun org-make-options-regexp (kwds &optional extra)
23390 "Make a regular expression for keyword lines."
23393 (mapconcat 'regexp-quote kwds "\\|")
23394 (if extra (concat "\\|" extra))
23395 "\\):[ \t]*\\(.*\\)"))
23397 ;; Make isearch reveal the necessary context
23398 (defun org-isearch-end ()
23399 "Reveal context after isearch exits."
23400 (when isearch-success ; only if search was successful
23401 (if (featurep 'xemacs)
23402 ;; Under XEmacs, the hook is run in the correct place,
23403 ;; we directly show the context.
23404 (org-show-context 'isearch)
23405 ;; In Emacs the hook runs *before* restoring the overlays.
23406 ;; So we have to use a one-time post-command-hook to do this.
23407 ;; (Emacs 22 has a special variable, see function `org-mode')
23408 (unless (and (boundp 'isearch-mode-end-hook-quit)
23409 isearch-mode-end-hook-quit)
23410 ;; Only when the isearch was not quitted.
23411 (org-add-hook 'post-command-hook 'org-isearch-post-command
23413 (org-fix-ellipsis-at-bol)))
23415 (defun org-isearch-post-command ()
23416 "Remove self from hook, and show context."
23417 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
23418 (org-show-context 'isearch))
23421 ;;;; Integration with and fixes for other packages
23425 (defvar org-imenu-markers nil
23426 "All markers currently used by Imenu.")
23427 (make-variable-buffer-local 'org-imenu-markers)
23429 (defun org-imenu-new-marker (&optional pos)
23430 "Return a new marker for use by Imenu, and remember the marker."
23431 (let ((m (make-marker)))
23432 (move-marker m (or pos (point)))
23433 (push m org-imenu-markers)
23436 (defun org-imenu-get-tree ()
23437 "Produce the index for Imenu."
23438 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
23439 (setq org-imenu-markers nil)
23440 (let* ((n org-imenu-depth)
23441 (re (concat "^" (org-get-limited-outline-regexp)))
23442 (subs (make-vector (1+ n) nil))
23444 m level head0 head)
23448 (goto-char (point-max))
23449 (while (re-search-backward re nil t)
23450 (setq level (org-reduced-level (funcall outline-level)))
23451 (when (and (<= level n)
23452 (looking-at org-complex-heading-regexp)
23453 (setq head0 (org-match-string-no-properties 4)))
23454 (setq head (org-link-display-format head0)
23455 m (org-imenu-new-marker))
23456 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
23457 (if (>= level last-level)
23458 (push (cons head m) (aref subs level))
23459 (push (cons head (aref subs (1+ level))) (aref subs level))
23460 (loop for i from (1+ level) to n do (aset subs i nil)))
23461 (setq last-level level)))))
23464 (eval-after-load "imenu"
23466 (add-hook 'imenu-after-jump-hook
23468 (if (derived-mode-p 'org-mode)
23469 (org-show-context 'org-goto))))))
23471 (defun org-link-display-format (link)
23472 "Replace a link with its the description.
23473 If there is no description, use the link target."
23475 (if (string-match org-bracket-link-analytic-regexp link)
23476 (replace-match (if (match-end 5)
23477 (match-string 5 link)
23478 (concat (match-string 1 link)
23479 (match-string 3 link)))
23483 (defun org-toggle-link-display ()
23484 "Toggle the literal or descriptive display of links."
23486 (if org-descriptive-links
23487 (progn (org-remove-from-invisibility-spec '(org-link))
23488 (org-restart-font-lock)
23489 (setq org-descriptive-links nil))
23490 (progn (add-to-invisibility-spec '(org-link))
23491 (org-restart-font-lock)
23492 (setq org-descriptive-links t))))
23494 ;; Speedbar support
23496 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
23497 "Overlay marking the agenda restriction line in speedbar.")
23498 (overlay-put org-speedbar-restriction-lock-overlay
23499 'face 'org-agenda-restriction-lock)
23500 (overlay-put org-speedbar-restriction-lock-overlay
23501 'help-echo "Agendas are currently limited to this item.")
23502 (org-detach-overlay org-speedbar-restriction-lock-overlay)
23504 (defun org-speedbar-set-agenda-restriction ()
23505 "Restrict future agenda commands to the location at point in speedbar.
23506 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
23508 (require 'org-agenda)
23509 (let (p m tp np dir txt)
23511 ((setq p (text-property-any (point-at-bol) (point-at-eol)
23513 (setq m (get-text-property p 'org-imenu-marker))
23514 (with-current-buffer (marker-buffer m)
23516 (org-agenda-set-restriction-lock 'subtree)))
23517 ((setq p (text-property-any (point-at-bol) (point-at-eol)
23518 'speedbar-function 'speedbar-find-file))
23519 (setq tp (previous-single-property-change
23520 (1+ p) 'speedbar-function)
23521 np (next-single-property-change
23522 tp 'speedbar-function)
23523 dir (speedbar-line-directory)
23524 txt (buffer-substring-no-properties (or tp (point-min))
23525 (or np (point-max))))
23526 (with-current-buffer (find-file-noselect
23527 (let ((default-directory dir))
23528 (expand-file-name txt)))
23529 (unless (derived-mode-p 'org-mode)
23530 (user-error "Cannot restrict to non-Org-mode file"))
23531 (org-agenda-set-restriction-lock 'file)))
23532 (t (user-error "Don't know how to restrict Org-mode's agenda")))
23533 (move-overlay org-speedbar-restriction-lock-overlay
23534 (point-at-bol) (point-at-eol))
23535 (setq current-prefix-arg nil)
23536 (org-agenda-maybe-redo)))
23538 (eval-after-load "speedbar"
23540 (speedbar-add-supported-extension ".org")
23541 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
23542 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
23543 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
23544 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
23545 (add-hook 'speedbar-visiting-tag-hook
23546 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
23548 ;;; Fixes and Hacks for problems with other packages
23550 ;; Make flyspell not check words in links, to not mess up our keymap
23551 (defvar org-element-affiliated-keywords) ; From org-element.el
23552 (defvar org-element-block-name-alist) ; From org-element.el
23553 (defun org-mode-flyspell-verify ()
23554 "Don't let flyspell put overlays at active buttons, or on
23555 {todo,all-time,additional-option-like}-keywords."
23556 (require 'org-element) ; For `org-element-affiliated-keywords'
23557 (let ((pos (max (1- (point)) (point-min)))
23558 (word (thing-at-point 'word)))
23559 (and (not (get-text-property pos 'keymap))
23560 (not (get-text-property pos 'org-no-flyspell))
23561 (not (member word org-todo-keywords-1))
23562 (not (member word org-all-time-keywords))
23563 (not (member word org-options-keywords))
23564 (not (member word (mapcar 'car org-startup-options)))
23565 (not (member-ignore-case word org-element-affiliated-keywords))
23566 (not (member-ignore-case word (org-get-export-keywords)))
23567 (not (member-ignore-case
23568 word (mapcar 'car org-element-block-name-alist)))
23569 (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
23571 (defun org-remove-flyspell-overlays-in (beg end)
23572 "Remove flyspell overlays in region."
23573 (and (org-bound-and-true-p flyspell-mode)
23574 (fboundp 'flyspell-delete-region-overlays)
23575 (flyspell-delete-region-overlays beg end))
23576 (add-text-properties beg end '(org-no-flyspell t)))
23578 ;; Make `bookmark-jump' shows the jump location if it was hidden.
23579 (eval-after-load "bookmark"
23580 '(if (boundp 'bookmark-after-jump-hook)
23581 ;; We can use the hook
23582 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
23583 ;; Hook not available, use advice
23584 (defadvice bookmark-jump (after org-make-visible activate)
23585 "Make the position visible."
23586 (org-bookmark-jump-unhide))))
23588 ;; Make sure saveplace shows the location if it was hidden
23589 (eval-after-load "saveplace"
23590 '(defadvice save-place-find-file-hook (after org-make-visible activate)
23591 "Make the position visible."
23592 (org-bookmark-jump-unhide)))
23594 ;; Make sure ecb shows the location if it was hidden
23595 (eval-after-load "ecb"
23596 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
23597 "Make hierarchy visible when jumping into location from ECB tree buffer."
23598 (if (derived-mode-p 'org-mode)
23599 (org-show-context))))
23601 (defun org-bookmark-jump-unhide ()
23602 "Unhide the current position, to show the bookmark location."
23603 (and (derived-mode-p 'org-mode)
23604 (or (outline-invisible-p)
23605 (save-excursion (goto-char (max (point-min) (1- (point))))
23606 (outline-invisible-p)))
23607 (org-show-context 'bookmark-jump)))
23609 ;; Make session.el ignore our circular variable
23610 (eval-after-load "session"
23611 '(add-to-list 'session-globals-exclude 'org-mark-ring))
23617 (run-hooks 'org-load-hook)
23619 ;;; org.el ends here