1 ;;; org.el --- Outline-based notes management and organizer
3 ;; Carstens outline-mode for keeping track of everything.
4 ;; Copyright (C) 2004-2016 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-bound 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-add-archive-files
"org-archive" (files))
116 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
117 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
118 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
119 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
120 (declare-function org-clock-get-last-clock-out-time
"org-clock" ())
121 (declare-function org-clock-timestamps-up
"org-clock" (&optional n
))
122 (declare-function org-clock-timestamps-down
"org-clock" (&optional n
))
123 (declare-function org-clock-remove-overlays
"org-clock" (&optional beg end noremove
))
124 (declare-function org-clock-sum-current-item
"org-clock" (&optional tstart
))
125 (declare-function org-clock-update-time-maybe
"org-clock" ())
126 (declare-function org-clocktable-shift
"org-clock" (dir n
))
128 (declare-function orgtbl-mode
"org-table" (&optional arg
))
129 (declare-function org-clock-out
"org-clock" (&optional switch-to-state fail-quietly at-time
))
130 (declare-function org-beamer-mode
"ox-beamer" ())
131 (declare-function org-table-edit-field
"org-table" (arg))
132 (declare-function org-table-justify-field-maybe
"org-table" (&optional new
))
133 (declare-function org-table-set-constants
"org-table" ())
134 (declare-function org-table-calc-current-TBLFM
"org-table" (&optional arg
))
135 (declare-function org-id-get-create
"org-id" (&optional force
))
136 (declare-function org-id-find-id-file
"org-id" (id))
137 (declare-function org-tags-view
"org-agenda" (&optional todo-only match
))
138 (declare-function org-agenda-list
"org-agenda" (&optional arg start-day span
))
139 (declare-function org-agenda-redo
"org-agenda" (&optional all
))
140 (declare-function org-table-align
"org-table" ())
141 (declare-function org-table-begin
"org-table" (&optional table-type
))
142 (declare-function org-table-blank-field
"org-table" ())
143 (declare-function org-table-end
"org-table" (&optional table-type
))
144 (declare-function org-table-insert-row
"org-table" (&optional arg
))
145 (declare-function org-table-paste-rectangle
"org-table" ())
146 (declare-function org-table-maybe-eval-formula
"org-table" ())
147 (declare-function org-table-maybe-recalculate-line
"org-table" ())
149 (declare-function org-element--parse-objects
"org-element"
150 (beg end acc restriction
))
151 (declare-function org-element-at-point
"org-element" (&optional keep-trail
))
152 (declare-function org-element-contents
"org-element" (element))
153 (declare-function org-element-context
"org-element" (&optional element
))
154 (declare-function org-element-interpret-data
"org-element"
155 (data &optional parent
))
156 (declare-function org-element-map
"org-element"
157 (data types fun
&optional info first-match no-recursion
))
158 (declare-function org-element-nested-p
"org-element" (elem-a elem-b
))
159 (declare-function org-element-parse-buffer
"org-element"
160 (&optional granularity visible-only
))
161 (declare-function org-element-property
"org-element" (property element
))
162 (declare-function org-element-put-property
"org-element"
163 (element property value
))
164 (declare-function org-element-swap-A-B
"org-element" (elem-a elem-b
))
165 (declare-function org-element--parse-objects
"org-element"
166 (beg end acc restriction
))
167 (declare-function org-element-parse-buffer
"org-element"
168 (&optional granularity visible-only
))
169 (declare-function org-element-restriction
"org-element" (element))
170 (declare-function org-element-type
"org-element" (element))
172 ;; load languages based on value of `org-babel-load-languages'
173 (defvar org-babel-load-languages
)
176 (defun org-babel-do-load-languages (sym value
)
177 "Load the languages defined in `org-babel-load-languages'."
178 (set-default sym value
)
180 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
183 (require (intern (concat "ob-" lang
))))
185 (funcall 'fmakunbound
186 (intern (concat "org-babel-execute:" lang
)))
187 (funcall 'fmakunbound
188 (intern (concat "org-babel-expand-body:" lang
)))))))
189 org-babel-load-languages
))
191 (declare-function org-babel-tangle-file
"ob-tangle" (file &optional target-file lang
))
193 (defun org-babel-load-file (file &optional compile
)
194 "Load Emacs Lisp source code blocks in the Org-mode FILE.
195 This function exports the source code using `org-babel-tangle'
196 and then loads the resulting file using `load-file'. With prefix
197 arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
198 file to byte-code before it is loaded."
199 (interactive "fFile to load: \nP")
201 (let* ((age (lambda (file)
203 (time-subtract (current-time)
204 (nth 5 (or (file-attributes (file-truename file
))
205 (file-attributes file
)))))))
206 (base-name (file-name-sans-extension file
))
207 (exported-file (concat base-name
".el")))
208 ;; tangle if the org-mode file is newer than the elisp file
209 (unless (and (file-exists-p exported-file
)
210 (> (funcall age file
) (funcall age exported-file
)))
212 (car (org-babel-tangle-file file exported-file
"emacs-lisp"))))
215 (progn (byte-compile-file exported-file
'load
)
216 "Compiled and loaded")
217 (progn (load-file exported-file
) "Loaded"))
220 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
221 "Languages which can be evaluated in Org-mode buffers.
222 This list can be used to load support for any of the languages
223 below, note that each language will depend on a different set of
224 system executables and/or Emacs modes. When a language is
225 \"loaded\", then code blocks in that language can be evaluated
226 with `org-babel-execute-src-block' bound by default to C-c
227 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
228 be set to remove code block evaluation from the C-c C-c
229 keybinding. By default only Emacs Lisp (which has no
230 requirements) is loaded."
232 :set
'org-babel-do-load-languages
234 :type
'(alist :tag
"Babel Languages"
237 (const :tag
"Awk" awk
)
240 (const :tag
"Asymptote" asymptote
)
241 (const :tag
"Calc" calc
)
242 (const :tag
"Clojure" clojure
)
243 (const :tag
"CSS" css
)
244 (const :tag
"Ditaa" ditaa
)
245 (const :tag
"Dot" dot
)
246 (const :tag
"Emacs Lisp" emacs-lisp
)
247 (const :tag
"Fortran" fortran
)
248 (const :tag
"Gnuplot" gnuplot
)
249 (const :tag
"Haskell" haskell
)
251 (const :tag
"Java" java
)
252 (const :tag
"Javascript" js
)
253 (const :tag
"LaTeX" latex
)
254 (const :tag
"Ledger" ledger
)
255 (const :tag
"Lilypond" lilypond
)
256 (const :tag
"Lisp" lisp
)
257 (const :tag
"Makefile" makefile
)
258 (const :tag
"Maxima" maxima
)
259 (const :tag
"Matlab" matlab
)
260 (const :tag
"Mscgen" mscgen
)
261 (const :tag
"Ocaml" ocaml
)
262 (const :tag
"Octave" octave
)
263 (const :tag
"Org" org
)
264 (const :tag
"Perl" perl
)
265 (const :tag
"Pico Lisp" picolisp
)
266 (const :tag
"PlantUML" plantuml
)
267 (const :tag
"Python" python
)
268 (const :tag
"Ruby" ruby
)
269 (const :tag
"Sass" sass
)
270 (const :tag
"Scala" scala
)
271 (const :tag
"Scheme" scheme
)
272 (const :tag
"Screen" screen
)
273 (const :tag
"Shell Script" sh
)
274 (const :tag
"Shen" shen
)
275 (const :tag
"Sql" sql
)
276 (const :tag
"Sqlite" sqlite
))
277 :value-type
(boolean :tag
"Activate" :value t
)))
279 ;;;; Customization variables
280 (defcustom org-clone-delete-id nil
281 "Remove ID property of clones of a subtree.
282 When non-nil, clones of a subtree don't inherit the ID property.
283 Otherwise they inherit the ID property with a new unique
293 (defun org-version (&optional here full message
)
294 "Show the org-mode version in the echo area.
295 With prefix argument HERE, insert it at point.
296 When FULL is non-nil, use a verbose version string.
297 When MESSAGE is non-nil, display a message with the version."
299 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
300 (save-load-suffixes (when (boundp 'load-suffixes
) load-suffixes
))
301 (load-suffixes (list ".el"))
302 (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
304 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
305 (org-load-noerror-mustsuffix (concat org-dir
"org-version"))))
306 (load-suffixes save-load-suffixes
)
307 (org-version (org-release))
308 (git-version (org-git-version))
309 (version (format "Org-mode version %s (%s @ %s)"
313 (if (string= org-dir org-install-dir
)
315 (concat "mixed installation! " org-install-dir
" and " org-dir
))
316 "org-loaddefs.el can not be found!")))
317 (version1 (if full version org-version
)))
318 (if (org-called-interactively-p 'interactive
)
322 (if message
(message version1
))
325 (defconst org-version
(org-version))
327 ;;; Compatibility constants
329 ;;; The custom variables
332 "Outline-based notes management and organizer."
337 (defcustom org-mode-hook nil
338 "Mode hook for Org-mode, run after the mode was turned on."
342 (defcustom org-load-hook nil
343 "Hook that is run after org.el has been loaded."
347 (defcustom org-log-buffer-setup-hook nil
348 "Hook that is run after an Org log buffer is created."
353 (defvar org-modules
) ; defined below
354 (defvar org-modules-loaded nil
355 "Have the modules been loaded already?")
357 (defun org-load-modules-maybe (&optional force
)
358 "Load all extensions listed in `org-modules'."
359 (when (or force
(not org-modules-loaded
))
361 (condition-case nil
(require ext
)
362 (error (message "Problems while trying to load feature `%s'" ext
))))
364 (setq org-modules-loaded t
)))
366 (defun org-set-modules (var value
)
367 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
369 (when (featurep 'org
)
370 (org-load-modules-maybe 'force
)))
372 (defcustom org-modules
'(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail
)
373 "Modules that should always be loaded together with org.el.
375 If a description starts with <C>, the file is not part of Emacs
376 and loading it will require that you have downloaded and properly
377 installed the Org mode distribution.
379 You can also use this system to load external packages (i.e. neither Org
380 core modules, nor modules from the CONTRIB directory). Just add symbols
381 to the end of the list. If the package is called org-xyz.el, then you need
382 to add the symbol `xyz', and the package must have a call to:
384 (provide \\='org-xyz)
386 For export specific modules, see also `org-export-backends'."
388 :set
'org-set-modules
390 :package-version
'(Org .
"8.0")
393 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
394 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
395 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
396 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
397 (const :tag
" docview: Links to doc-view buffers" org-docview
)
398 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
399 (const :tag
" habit: Track your consistency with habits" org-habit
)
400 (const :tag
" id: Global IDs for identifying entries" org-id
)
401 (const :tag
" info: Links to Info nodes" org-info
)
402 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
403 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
404 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
405 (const :tag
" mouse: Additional mouse support" org-mouse
)
406 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
407 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
408 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
410 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
411 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
412 (const :tag
"C bullets: Add overlays to headlines stars" org-bullets
)
413 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
414 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
415 (const :tag
"C collector: Collect properties into tables" org-collector
)
416 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
417 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
418 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
419 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
420 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
421 (const :tag
"C eval: Include command output as text" org-eval
)
422 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
423 (const :tag
"C favtable: Lookup table of favorite references and links" org-favtable
)
424 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
425 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
426 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
427 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
428 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
429 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
430 (const :tag
"C mac-link: Grab links and url from various mac Applications" org-mac-link
)
431 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
432 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
433 (const :tag
"C mew: Links to Mew folders/messages" org-mew
)
434 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
435 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
436 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
437 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
438 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
439 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
440 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
441 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
442 (const :tag
"C track: Keep up with Org-mode development" org-track
)
443 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
444 (const :tag
"C vm: Links to VM folders/messages" org-vm
)
445 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
446 (const :tag
"C wl: Links to Wanderlust folders/messages" org-wl
)
447 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
449 (defvar org-export--registered-backends
) ; From ox.el.
450 (declare-function org-export-derived-backend-p
"ox" (backend &rest backends
))
451 (declare-function org-export-backend-name
"ox" (backend))
452 (defcustom org-export-backends
'(ascii html icalendar latex
)
453 "List of export back-ends that should be always available.
455 If a description starts with <C>, the file is not part of Emacs
456 and loading it will require that you have downloaded and properly
457 installed the Org mode distribution.
459 Unlike to `org-modules', libraries in this list will not be
460 loaded along with Org, but only once the export framework is
463 This variable needs to be set before org.el is loaded. If you
464 need to make a change while Emacs is running, use the customize
465 interface or run the following code, where VAL stands for the new
466 value of the variable, after updating it:
469 (setq org-export--registered-backends
472 (let ((name (org-export-backend-name backend)))
476 (and (org-export-derived-backend-p b name)
477 (throw \\='parentp t)))))))
478 org-export--registered-backends))
479 (let ((new-list (mapcar \\='org-export-backend-name
480 org-export--registered-backends)))
481 (dolist (backend val)
483 ((not (load (format \"ox-%s\" backend) t t))
484 (message \"Problems while trying to load export back-end \\=`%s\\='\"
486 ((not (memq backend new-list)) (push backend new-list))))
487 (set-default \\='org-export-backends new-list)))
489 Adding a back-end to this list will also pull the back-end it
494 :package-version
'(Org .
"8.0")
495 :initialize
'custom-initialize-set
496 :set
(lambda (var val
)
497 (if (not (featurep 'ox
)) (set-default var val
)
498 ;; Any back-end not required anymore (not present in VAL and not
499 ;; a parent of any back-end in the new value) is removed from the
500 ;; list of registered back-ends.
501 (setq org-export--registered-backends
504 (let ((name (org-export-backend-name backend
)))
508 (and (org-export-derived-backend-p b name
)
509 (throw 'parentp t
)))))))
510 org-export--registered-backends
))
511 ;; Now build NEW-LIST of both new back-ends and required
513 (let ((new-list (mapcar 'org-export-backend-name
514 org-export--registered-backends
)))
515 (dolist (backend val
)
517 ((not (load (format "ox-%s" backend
) t t
))
518 (message "Problems while trying to load export back-end `%s'"
520 ((not (memq backend new-list
)) (push backend new-list
))))
521 ;; Set VAR to that list with fixed dependencies.
522 (set-default var new-list
))))
523 :type
'(set :greedy t
524 (const :tag
" ascii Export buffer to ASCII format" ascii
)
525 (const :tag
" beamer Export buffer to Beamer presentation" beamer
)
526 (const :tag
" html Export buffer to HTML format" html
)
527 (const :tag
" icalendar Export buffer to iCalendar format" icalendar
)
528 (const :tag
" latex Export buffer to LaTeX format" latex
)
529 (const :tag
" man Export buffer to MAN format" man
)
530 (const :tag
" md Export buffer to Markdown format" md
)
531 (const :tag
" odt Export buffer to ODT format" odt
)
532 (const :tag
" org Export buffer to Org format" org
)
533 (const :tag
" texinfo Export buffer to Texinfo format" texinfo
)
534 (const :tag
"C confluence Export buffer to Confluence Wiki format" confluence
)
535 (const :tag
"C deck Export buffer to deck.js presentations" deck
)
536 (const :tag
"C freemind Export buffer to Freemind mindmap format" freemind
)
537 (const :tag
"C groff Export buffer to Groff format" groff
)
538 (const :tag
"C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter
)
539 (const :tag
"C RSS 2.0 Export buffer to RSS 2.0 format" rss
)
540 (const :tag
"C s5 Export buffer to s5 presentations" s5
)
541 (const :tag
"C taskjuggler Export buffer to TaskJuggler format" taskjuggler
)))
546 (condition-case nil
(require (intern (format "ox-%s" backend
)))
547 (error (message "Problems while trying to load export back-end `%s'"
549 org-export-backends
))
551 (defcustom org-support-shift-select nil
552 "Non-nil means make shift-cursor commands select text when possible.
554 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
555 start selecting a region, or enlarge regions started in this way.
556 In Org-mode, in special contexts, these same keys are used for
557 other purposes, important enough to compete with shift selection.
558 Org tries to balance these needs by supporting `shift-select-mode'
559 outside these special contexts, under control of this variable.
561 The default of this variable is nil, to avoid confusing behavior. Shifted
562 cursor keys will then execute Org commands in the following contexts:
563 - on a headline, changing TODO state (left/right) and priority (up/down)
564 - on a time stamp, changing the time
565 - in a plain list item, changing the bullet type
566 - in a property definition line, switching between allowed values
567 - in the BEGIN line of a clock table (changing the time block).
568 Outside these contexts, the commands will throw an error.
570 When this variable is t and the cursor is not in a special
571 context, Org-mode will support shift-selection for making and
572 enlarging regions. To make this more effective, the bullet
573 cycling will no longer happen anywhere in an item line, but only
574 if the cursor is exactly on the bullet.
576 If you set this variable to the symbol `always', then the keys
577 will not be special in headlines, property lines, and item lines,
578 to make shift selection work there as well. If this is what you
579 want, you can use the following alternative commands: `C-c C-t'
580 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
581 can be used to switch TODO sets, `C-c -' to cycle item bullet
582 types, and properties can be edited by hand or in column view.
584 However, when the cursor is on a timestamp, shift-cursor commands
585 will still edit the time stamp - this is just too good to give up.
587 XEmacs user should have this variable set to nil, because
588 `shift-select-mode' is in Emacs 23 or later only."
591 (const :tag
"Never" nil
)
592 (const :tag
"When outside special context" t
)
593 (const :tag
"Everywhere except timestamps" always
)))
595 (defcustom org-loop-over-headlines-in-active-region nil
596 "Shall some commands act upon headlines in the active region?
598 When set to t, some commands will be performed in all headlines
599 within the active region.
601 When set to `start-level', some commands will be performed in all
602 headlines within the active region, provided that these headlines
603 are of the same level than the first one.
605 When set to a string, those commands will be performed on the
606 matching headlines within the active region. Such string must be
607 a tags/property/todo match as it is used in the agenda tags view.
609 The list of commands is: `org-schedule', `org-deadline',
610 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
611 `org-archive-to-archive-sibling'. The archiving commands skip
612 already archived entries."
613 :type
'(choice (const :tag
"Don't loop" nil
)
614 (const :tag
"All headlines in active region" t
)
615 (const :tag
"In active region, headlines at the same level than the first one" start-level
)
616 (string :tag
"Tags/Property/Todo matcher"))
621 (defgroup org-startup nil
622 "Options concerning startup of Org-mode."
626 (defcustom org-startup-folded t
627 "Non-nil means entering Org-mode will switch to OVERVIEW.
628 This can also be configured on a per-file basis by adding one of
629 the following lines anywhere in the buffer:
631 #+STARTUP: fold (or `overview', this is equivalent)
632 #+STARTUP: nofold (or `showall', this is equivalent)
634 #+STARTUP: showeverything
636 By default, this option is ignored when Org opens agenda files
637 for the first time. If you want the agenda to honor the startup
638 option, set `org-agenda-inhibit-startup' to nil."
641 (const :tag
"nofold: show all" nil
)
642 (const :tag
"fold: overview" t
)
643 (const :tag
"content: all headlines" content
)
644 (const :tag
"show everything, even drawers" showeverything
)))
646 (defcustom org-startup-truncated t
647 "Non-nil means entering Org-mode will set `truncate-lines'.
648 This is useful since some lines containing links can be very long and
649 uninteresting. Also tables look terrible when wrapped."
653 (defcustom org-startup-indented nil
654 "Non-nil means turn on `org-indent-mode' on startup.
655 This can also be configured on a per-file basis by adding one of
656 the following lines anywhere in the buffer:
660 :group
'org-structure
662 (const :tag
"Not" nil
)
663 (const :tag
"Globally (slow on startup in large files)" t
)))
665 (defcustom org-use-sub-superscripts t
666 "Non-nil means interpret \"_\" and \"^\" for display.
668 If you want to control how Org exports those characters, see
669 `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
670 used to be an alias for `org-export-with-sub-superscripts' in
671 Org <8.0, it is not anymore.
673 When this option is turned on, you can use TeX-like syntax for
674 sub- and superscripts within the buffer. Several characters after
675 \"_\" or \"^\" will be considered as a single item - so grouping
676 with {} is normally not needed. For example, the following things
677 will be parsed as single sub- or superscripts:
679 10^24 or 10^tau several digits will be considered 1 item.
680 10^-12 or 10^-tau a leading sign with digits or a word
681 x^2-y^3 will be read as x^2 - y^3, because items are
682 terminated by almost any nonword/nondigit char.
683 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
685 Still, ambiguity is possible. So when in doubt, use {} to enclose
686 the sub/superscript. If you set this variable to the symbol `{}',
687 the braces are *required* in order to trigger interpretations as
688 sub/superscript. This can be helpful in documents that need \"_\"
689 frequently in plain text."
692 :package-version
'(Org .
"8.0")
694 (const :tag
"Always interpret" t
)
695 (const :tag
"Only with braces" {})
696 (const :tag
"Never interpret" nil
)))
698 (defcustom org-startup-with-beamer-mode nil
699 "Non-nil means turn on `org-beamer-mode' on startup.
700 This can also be configured on a per-file basis by adding one of
701 the following lines anywhere in the buffer:
708 (defcustom org-startup-align-all-tables nil
709 "Non-nil means align all tables when visiting a file.
710 This is useful when the column width in tables is forced with <N> cookies
711 in table fields. Such tables will look correct only after the first re-align.
712 This can also be configured on a per-file basis by adding one of
713 the following lines anywhere in the buffer:
719 (defcustom org-startup-with-inline-images nil
720 "Non-nil means show inline images when loading a new Org file.
721 This can also be configured on a per-file basis by adding one of
722 the following lines anywhere in the buffer:
723 #+STARTUP: inlineimages
724 #+STARTUP: noinlineimages"
729 (defcustom org-startup-with-latex-preview nil
730 "Non-nil means preview LaTeX fragments when loading a new Org file.
732 This can also be configured on a per-file basis by adding one of
733 the following lines anywhere in the buffer:
734 #+STARTUP: latexpreview
735 #+STARTUP: nolatexpreview"
738 :package-version
'(Org .
"8.0")
741 (defcustom org-insert-mode-line-in-empty-file nil
742 "Non-nil means insert the first line setting Org-mode in empty files.
743 When the function `org-mode' is called interactively in an empty file, this
744 normally means that the file name does not automatically trigger Org-mode.
745 To ensure that the file will always be in Org-mode in the future, a
746 line enforcing Org-mode will be inserted into the buffer, if this option
751 (defcustom org-replace-disputed-keys nil
752 "Non-nil means use alternative key bindings for some keys.
753 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
754 These keys are also used by other packages like shift-selection-mode'
755 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
756 If you want to use Org-mode together with one of these other modes,
757 or more generally if you would like to move some Org-mode commands to
758 other keys, set this variable and configure the keys with the variable
761 This option is only relevant at load-time of Org-mode, and must be set
762 *before* org.el is loaded. Changing it requires a restart of Emacs to
767 (defcustom org-use-extra-keys nil
768 "Non-nil means use extra key sequence definitions for certain commands.
769 This happens automatically if you run XEmacs or if `window-system'
770 is nil. This variable lets you do the same manually. You must
771 set it before loading org.
773 Example: on Carbon Emacs 22 running graphically, with an external
774 keyboard on a Powerbook, the default way of setting M-left might
775 not work for either Alt or ESC. Setting this variable will make
780 (org-defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
)
782 (defcustom org-disputed-keys
783 '(([(shift up
)] .
[(meta p
)])
784 ([(shift down
)] .
[(meta n
)])
785 ([(shift left
)] .
[(meta -
)])
786 ([(shift right
)] .
[(meta +)])
787 ([(control shift right
)] .
[(meta shift
+)])
788 ([(control shift left
)] .
[(meta shift -
)]))
789 "Keys for which Org-mode and other modes compete.
790 This is an alist, cars are the default keys, second element specifies
791 the alternative to use when `org-replace-disputed-keys' is t.
793 Keys can be specified in any syntax supported by `define-key'.
794 The value of this option takes effect only at Org-mode's startup,
795 therefore you'll have to restart Emacs to apply it after changing."
800 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
801 Or return the original if not disputed.
802 Also apply the translations defined in `org-xemacs-key-equivalents'."
803 (when org-replace-disputed-keys
804 (let* ((nkey (key-description key
))
805 (x (org-find-if (lambda (x)
806 (equal (key-description (car x
)) nkey
))
808 (setq key
(if x
(cdr x
) key
))))
809 (when (featurep 'xemacs
)
810 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
813 (defun org-find-if (predicate seq
)
816 (if (funcall predicate
(car seq
))
817 (throw 'exit
(car seq
))
820 (defun org-defkey (keymap key def
)
821 "Define a key, possibly translated, as returned by `org-key'."
822 (define-key keymap
(org-key key
) def
))
824 (defcustom org-ellipsis nil
825 "The ellipsis to use in the Org-mode outline.
826 When nil, just use the standard three dots.
827 When a string, use that string instead.
828 When a face, use the standard 3 dots, but with the specified face.
829 The change affects only Org-mode (which will then use its own display table).
830 Changing this requires executing `\\[org-mode]' in a buffer to become
833 :type
'(choice (const :tag
"Default" nil
)
834 (face :tag
"Face" :value org-warning
)
835 (string :tag
"String" :value
"...#")))
837 (defvar org-display-table nil
838 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
840 (defgroup org-keywords nil
841 "Keywords in Org-mode."
845 (defcustom org-deadline-string
"DEADLINE:"
846 "String to mark deadline entries.
847 A deadline is this string, followed by a time stamp. Should be a word,
848 terminated by a colon. You can insert a schedule keyword and
849 a timestamp with \\[org-deadline].
850 Changes become only effective after restarting Emacs."
854 (defcustom org-scheduled-string
"SCHEDULED:"
855 "String to mark scheduled TODO entries.
856 A schedule is this string, followed by a time stamp. Should be a word,
857 terminated by a colon. You can insert a schedule keyword and
858 a timestamp with \\[org-schedule].
859 Changes become only effective after restarting Emacs."
863 (defcustom org-closed-string
"CLOSED:"
864 "String used as the prefix for timestamps logging closing a TODO entry."
868 (defcustom org-clock-string
"CLOCK:"
869 "String used as prefix for timestamps clocking work hours on an item."
873 (defcustom org-closed-keep-when-no-todo nil
874 "Remove CLOSED: time-stamp when switching back to a non-todo state?"
878 :package-version
'(Org .
"8.0")
881 (defconst org-planning-or-clock-line-re
(concat "^[ \t]*\\("
882 org-scheduled-string
"\\|"
883 org-deadline-string
"\\|"
884 org-closed-string
"\\|"
885 org-clock-string
"\\)")
886 "Matches a line with planning or clock info.")
888 (defcustom org-comment-string
"COMMENT"
889 "Entries starting with this keyword will never be exported.
890 An entry can be toggled between COMMENT and normal with
891 \\[org-toggle-comment].
892 Changes become only effective after restarting Emacs."
896 (defcustom org-quote-string
"QUOTE"
897 "Entries starting with this keyword will be exported in fixed-width font.
898 Quoting applies only to the text in the entry following the headline, and does
899 not extend beyond the next headline, even if that is lower level.
900 An entry can be toggled between QUOTE and normal with
901 \\[org-toggle-fixed-width-section]."
905 (defconst org-repeat-re
906 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
907 "Regular expression for specifying repeated events.
908 After a match, group 1 contains the repeat expression.")
910 (defgroup org-structure nil
911 "Options concerning the general structure of Org-mode files."
915 (defgroup org-reveal-location nil
916 "Options about how to make context of a location visible."
917 :tag
"Org Reveal Location"
918 :group
'org-structure
)
920 (defconst org-context-choice
922 (const :tag
"Always" t
)
923 (const :tag
"Never" nil
)
924 (repeat :greedy t
:tag
"Individual contexts"
926 (choice :tag
"Context"
933 (const bookmark-jump
)
937 "Contexts for the reveal options.")
939 (defcustom org-show-hierarchy-above
'((default . t
))
940 "Non-nil means show full hierarchy when revealing a location.
941 Org-mode often shows locations in an org-mode file which might have
942 been invisible before. When this is set, the hierarchy of headings
943 above the exposed location is shown.
944 Turning this off for example for sparse trees makes them very compact.
945 Instead of t, this can also be an alist specifying this option for different
946 contexts. Valid contexts are
947 agenda when exposing an entry from the agenda
948 org-goto when using the command `org-goto' on key C-c C-j
949 occur-tree when using the command `org-occur' on key C-c /
950 tags-tree when constructing a sparse tree based on tags matches
951 link-search when exposing search matches associated with a link
952 mark-goto when exposing the jump goal of a mark
953 bookmark-jump when exposing a bookmark location
954 isearch when exiting from an incremental search
955 default default for all contexts not set explicitly"
956 :group
'org-reveal-location
957 :type org-context-choice
)
959 (defcustom org-show-following-heading
'((default . nil
))
960 "Non-nil means show following heading when revealing a location.
961 Org-mode often shows locations in an org-mode file which might have
962 been invisible before. When this is set, the heading following the
964 Turning this off for example for sparse trees makes them very compact,
965 but makes it harder to edit the location of the match. In such a case,
966 use the command \\[org-reveal] to show more context.
967 Instead of t, this can also be an alist specifying this option for different
968 contexts. See `org-show-hierarchy-above' for valid contexts."
969 :group
'org-reveal-location
970 :type org-context-choice
)
972 (defcustom org-show-siblings
'((default . nil
) (isearch t
) (bookmark-jump t
))
973 "Non-nil means show all sibling heading when revealing a location.
974 Org-mode often shows locations in an org-mode file which might have
975 been invisible before. When this is set, the sibling of the current entry
976 heading are all made visible. If `org-show-hierarchy-above' is t,
977 the same happens on each level of the hierarchy above the current entry.
979 By default this is on for the isearch context, off for all other contexts.
980 Turning this off for example for sparse trees makes them very compact,
981 but makes it harder to edit the location of the match. In such a case,
982 use the command \\[org-reveal] to show more context.
983 Instead of t, this can also be an alist specifying this option for different
984 contexts. See `org-show-hierarchy-above' for valid contexts."
985 :group
'org-reveal-location
986 :type org-context-choice
988 :package-version
'(Org .
"8.0"))
990 (defcustom org-show-entry-below
'((default . nil
))
991 "Non-nil means show the entry below a headline when revealing a location.
992 Org-mode often shows locations in an org-mode file which might have
993 been invisible before. When this is set, the text below the headline that is
994 exposed is also shown.
996 By default this is off for all contexts.
997 Instead of t, this can also be an alist specifying this option for different
998 contexts. See `org-show-hierarchy-above' for valid contexts."
999 :group
'org-reveal-location
1000 :type org-context-choice
)
1002 (defcustom org-indirect-buffer-display
'other-window
1003 "How should indirect tree buffers be displayed?
1004 This applies to indirect buffers created with the commands
1005 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
1007 current-window Display in the current window
1008 other-window Just display in another window.
1009 dedicated-frame Create one new frame, and re-use it each time.
1010 new-frame Make a new frame each time. Note that in this case
1011 previously-made indirect buffers are kept, and you need to
1012 kill these buffers yourself."
1013 :group
'org-structure
1014 :group
'org-agenda-windows
1016 (const :tag
"In current window" current-window
)
1017 (const :tag
"In current frame, other window" other-window
)
1018 (const :tag
"Each time a new frame" new-frame
)
1019 (const :tag
"One dedicated frame" dedicated-frame
)))
1021 (defcustom org-use-speed-commands nil
1022 "Non-nil means activate single letter commands at beginning of a headline.
1023 This may also be a function to test for appropriate locations where speed
1024 commands should be active."
1025 :group
'org-structure
1027 (const :tag
"Never" nil
)
1028 (const :tag
"At beginning of headline stars" t
)
1031 (defcustom org-speed-commands-user nil
1032 "Alist of additional speed commands.
1033 This list will be checked before `org-speed-commands-default'
1034 when the variable `org-use-speed-commands' is non-nil
1035 and when the cursor is at the beginning of a headline.
1036 The car if each entry is a string with a single letter, which must
1037 be assigned to `self-insert-command' in the global map.
1038 The cdr is either a command to be called interactively, a function
1039 to be called, or a form to be evaluated.
1040 An entry that is just a list with a single string will be interpreted
1041 as a descriptive headline that will be added when listing the speed
1042 commands in the Help buffer using the `?' speed command."
1043 :group
'org-structure
1044 :type
'(repeat :value
("k" . ignore
)
1045 (choice :value
("k" . ignore
)
1046 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
1047 (cons :tag
"Letter and Command"
1048 (string :tag
"Command letter")
1053 (defcustom org-bookmark-names-plist
1054 '(:last-capture
"org-capture-last-stored"
1055 :last-refile
"org-refile-last-stored"
1056 :last-capture-marker
"org-capture-last-stored-marker")
1057 "Names for bookmarks automatically set by some Org commands.
1058 This can provide strings as names for a number of bookmarks Org sets
1059 automatically. The following keys are currently implemented:
1061 :last-capture-marker
1063 When a key does not show up in the property list, the corresponding bookmark
1065 :group
'org-structure
1068 (defgroup org-cycle nil
1069 "Options concerning visibility cycling in Org-mode."
1071 :group
'org-structure
)
1073 (defcustom org-cycle-skip-children-state-if-no-children t
1074 "Non-nil means skip CHILDREN state in entries that don't have any."
1078 (defcustom org-cycle-max-level nil
1079 "Maximum level which should still be subject to visibility cycling.
1080 Levels higher than this will, for cycling, be treated as text, not a headline.
1081 When `org-odd-levels-only' is set, a value of N in this variable actually
1082 means 2N-1 stars as the limiting headline.
1083 When nil, cycle all levels.
1084 Note that the limiting level of cycling is also influenced by
1085 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
1086 `org-inlinetask-min-level' is, cycling will be limited to levels one less
1090 (const :tag
"No limit" nil
)
1091 (integer :tag
"Maximum level")))
1093 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
1094 "Names of drawers. Drawers are not opened by cycling on the headline above.
1095 Drawers only open with a TAB on the drawer line itself. A drawer looks like
1100 The drawer \"PROPERTIES\" is special for capturing properties through
1103 Drawers can be defined on the per-file basis with a line like:
1105 #+DRAWERS: HIDDEN STATE PROPERTIES"
1106 :group
'org-structure
1108 :type
'(repeat (string :tag
"Drawer Name")))
1110 (defcustom org-hide-block-startup nil
1111 "Non-nil means entering Org-mode will fold all blocks.
1112 This can also be set in on a per-file basis with
1114 #+STARTUP: hideblocks
1115 #+STARTUP: showblocks"
1120 (defcustom org-cycle-global-at-bob nil
1121 "Cycle globally if cursor is at beginning of buffer and not at a headline.
1122 This makes it possible to do global cycling without having to use S-TAB or
1123 \\[universal-argument] TAB. For this special case to work, the first line
1124 of the buffer must not be a headline -- it may be empty or some other text.
1125 When used in this way, `org-cycle-hook' is disabled temporarily to make
1126 sure the cursor stays at the beginning of the buffer. When this option is
1127 nil, don't do anything special at the beginning of the buffer."
1131 (defcustom org-cycle-level-after-item
/entry-creation t
1132 "Non-nil means cycle entry level or item indentation in new empty entries.
1134 When the cursor is at the end of an empty headline, i.e., with only stars
1135 and maybe a TODO keyword, TAB will then switch the entry to become a child,
1136 and then all possible ancestor states, before returning to the original state.
1137 This makes data entry extremely fast: M-RET to create a new headline,
1138 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
1140 When the cursor is at the end of an empty plain list item, one TAB will
1141 make it a subitem, two or more tabs will back up to make this an item
1142 higher up in the item hierarchy."
1146 (defcustom org-cycle-emulate-tab t
1147 "Where should `org-cycle' emulate TAB.
1149 white Only in completely white lines
1150 whitestart Only at the beginning of lines, before the first non-white char
1151 t Everywhere except in headlines
1152 exc-hl-bol Everywhere except at the start of a headline
1153 If TAB is used in a place where it does not emulate TAB, the current subtree
1154 visibility is cycled."
1156 :type
'(choice (const :tag
"Never" nil
)
1157 (const :tag
"Only in completely white lines" white
)
1158 (const :tag
"Before first char in a line" whitestart
)
1159 (const :tag
"Everywhere except in headlines" t
)
1160 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)))
1162 (defcustom org-cycle-separator-lines
2
1163 "Number of empty lines needed to keep an empty line between collapsed trees.
1164 If you leave an empty line between the end of a subtree and the following
1165 headline, this empty line is hidden when the subtree is folded.
1166 Org-mode will leave (exactly) one empty line visible if the number of
1167 empty lines is equal or larger to the number given in this variable.
1168 So the default 2 means at least 2 empty lines after the end of a subtree
1169 are needed to produce free space between a collapsed subtree and the
1172 If the number is negative, and the number of empty lines is at least -N,
1173 all empty lines are shown.
1175 Special case: when 0, never leave empty lines in collapsed view."
1178 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
1180 (defcustom org-pre-cycle-hook nil
1181 "Hook that is run before visibility cycling is happening.
1182 The function(s) in this hook must accept a single argument which indicates
1183 the new state that will be set right after running this hook. The
1184 argument is a symbol. Before a global state change, it can have the values
1185 `overview', `content', or `all'. Before a local state change, it can have
1186 the values `folded', `children', or `subtree'."
1190 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
1191 org-cycle-hide-drawers
1192 org-cycle-hide-inline-tasks
1193 org-cycle-show-empty-lines
1194 org-optimize-window-after-visibility-change
)
1195 "Hook that is run after `org-cycle' has changed the buffer visibility.
1196 The function(s) in this hook must accept a single argument which indicates
1197 the new state that was set by the most recent `org-cycle' command. The
1198 argument is a symbol. After a global state change, it can have the values
1199 `overview', `contents', or `all'. After a local state change, it can have
1200 the values `folded', `children', or `subtree'."
1204 (defgroup org-edit-structure nil
1205 "Options concerning structure editing in Org-mode."
1206 :tag
"Org Edit Structure"
1207 :group
'org-structure
)
1209 (defcustom org-odd-levels-only nil
1210 "Non-nil means skip even levels and only use odd levels for the outline.
1211 This has the effect that two stars are being added/taken away in
1212 promotion/demotion commands. It also influences how levels are
1213 handled by the exporters.
1214 Changing it requires restart of `font-lock-mode' to become effective
1215 for fontification also in regions already fontified.
1216 You may also set this on a per-file basis by adding one of the following
1217 lines to the buffer:
1221 :group
'org-edit-structure
1222 :group
'org-appearance
1225 (defcustom org-adapt-indentation t
1226 "Non-nil means adapt indentation to outline node level.
1228 When this variable is set, Org assumes that you write outlines by
1229 indenting text in each node to align with the headline (after the stars).
1230 The following issues are influenced by this variable:
1232 - When this is set and the *entire* text in an entry is indented, the
1233 indentation is increased by one space in a demotion command, and
1234 decreased by one in a promotion command. If any line in the entry
1235 body starts with text at column 0, indentation is not changed at all.
1237 - Property drawers and planning information is inserted indented when
1238 this variable s set. When nil, they will not be indented.
1240 - TAB indents a line relative to context. The lines below a headline
1241 will be indented when this variable is set.
1243 Note that this is all about true indentation, by adding and removing
1244 space characters. See also `org-indent.el' which does level-dependent
1245 indentation in a virtual way, i.e. at display time in Emacs."
1246 :group
'org-edit-structure
1249 (defcustom org-special-ctrl-a
/e nil
1250 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1252 When t, `C-a' will bring back the cursor to the beginning of the
1253 headline text, i.e. after the stars and after a possible TODO
1254 keyword. In an item, this will be the position after bullet and
1255 check-box, if any. When the cursor is already at that position,
1256 another `C-a' will bring it to the beginning of the line.
1258 `C-e' will jump to the end of the headline, ignoring the presence
1259 of tags in the headline. A second `C-e' will then jump to the
1260 true end of the line, after any tags. This also means that, when
1261 this variable is non-nil, `C-e' also will never jump beyond the
1262 end of the heading of a folded section, i.e. not after the
1265 When set to the symbol `reversed', the first `C-a' or `C-e' works
1266 normally, going to the true line boundary first. Only a directly
1267 following, identical keypress will bring the cursor to the
1270 This may also be a cons cell where the behavior for `C-a' and
1271 `C-e' is set separately."
1272 :group
'org-edit-structure
1274 (const :tag
"off" nil
)
1275 (const :tag
"on: after stars/bullet and before tags first" t
)
1276 (const :tag
"reversed: true line boundary first" reversed
)
1277 (cons :tag
"Set C-a and C-e separately"
1278 (choice :tag
"Special C-a"
1279 (const :tag
"off" nil
)
1280 (const :tag
"on: after stars/bullet first" t
)
1281 (const :tag
"reversed: before stars/bullet first" reversed
))
1282 (choice :tag
"Special C-e"
1283 (const :tag
"off" nil
)
1284 (const :tag
"on: before tags first" t
)
1285 (const :tag
"reversed: after tags first" reversed
)))))
1286 (org-defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
)
1288 (defcustom org-special-ctrl-k nil
1289 "Non-nil means `C-k' will behave specially in headlines.
1290 When nil, `C-k' will call the default `kill-line' command.
1291 When t, the following will happen while the cursor is in the headline:
1293 - When the cursor is at the beginning of a headline, kill the entire
1294 line and possible the folded subtree below the line.
1295 - When in the middle of the headline text, kill the headline up to the tags.
1296 - When after the headline text, kill the tags."
1297 :group
'org-edit-structure
1300 (defcustom org-ctrl-k-protect-subtree nil
1301 "Non-nil means, do not delete a hidden subtree with C-k.
1302 When set to the symbol `error', simply throw an error when C-k is
1303 used to kill (part-of) a headline that has hidden text behind it.
1304 Any other non-nil value will result in a query to the user, if it is
1305 OK to kill that hidden subtree. When nil, kill without remorse."
1306 :group
'org-edit-structure
1309 (const :tag
"Do not protect hidden subtrees" nil
)
1310 (const :tag
"Protect hidden subtrees with a security query" t
)
1311 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1313 (defcustom org-special-ctrl-o t
1314 "Non-nil means, make `C-o' insert a row in tables."
1315 :group
'org-edit-structure
1318 (defcustom org-catch-invisible-edits nil
1319 "Check if in invisible region before inserting or deleting a character.
1322 nil Do not check, so just do invisible edits.
1323 error Throw an error and do nothing.
1324 show Make point visible, and do the requested edit.
1325 show-and-error Make point visible, then throw an error and abort the edit.
1326 smart Make point visible, and do insertion/deletion if it is
1327 adjacent to visible text and the change feels predictable.
1328 Never delete a previously invisible character or add in the
1329 middle or right after an invisible region. Basically, this
1330 allows insertion and backward-delete right before ellipses.
1331 FIXME: maybe in this case we should not even show?"
1332 :group
'org-edit-structure
1335 (const :tag
"Do not check" nil
)
1336 (const :tag
"Throw error when trying to edit" error
)
1337 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1338 (const :tag
"Show invisible part and do the edit" show
)
1339 (const :tag
"Be smart and do the right thing" smart
)))
1341 (defcustom org-yank-folded-subtrees t
1342 "Non-nil means when yanking subtrees, fold them.
1343 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1344 it starts with a heading and all other headings in it are either children
1345 or siblings, then fold all the subtrees. However, do this only if no
1346 text after the yank would be swallowed into a folded tree by this action."
1347 :group
'org-edit-structure
1350 (defcustom org-yank-adjusted-subtrees nil
1351 "Non-nil means when yanking subtrees, adjust the level.
1352 With this setting, `org-paste-subtree' is used to insert the subtree, see
1353 this function for details."
1354 :group
'org-edit-structure
1357 (defcustom org-M-RET-may-split-line
'((default . t
))
1358 "Non-nil means M-RET will split the line at the cursor position.
1359 When nil, it will go to the end of the line before making a
1361 You may also set this option in a different way for different
1362 contexts. Valid contexts are:
1364 headline when creating a new headline
1365 item when creating a new item
1366 table in a table field
1367 default the value to be used for all contexts not explicitly
1369 :group
'org-structure
1372 (const :tag
"Always" t
)
1373 (const :tag
"Never" nil
)
1374 (repeat :greedy t
:tag
"Individual contexts"
1376 (choice :tag
"Context"
1384 (defcustom org-insert-heading-respect-content nil
1385 "Non-nil means insert new headings after the current subtree.
1386 When nil, the new heading is created directly after the current line.
1387 The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
1388 this variable on for the duration of the command."
1389 :group
'org-structure
1392 (defcustom org-blank-before-new-entry
'((heading . auto
)
1393 (plain-list-item . auto
))
1394 "Should `org-insert-heading' leave a blank line before new heading/item?
1395 The value is an alist, with `heading' and `plain-list-item' as CAR,
1396 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1397 which case Org will look at the surrounding headings/items and try to
1398 make an intelligent decision whether to insert a blank line or not.
1400 For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
1401 the setting here is ignored and no empty line is inserted to avoid breaking
1402 the list structure."
1403 :group
'org-edit-structure
1405 (cons (const heading
)
1406 (choice (const :tag
"Never" nil
)
1407 (const :tag
"Always" t
)
1408 (const :tag
"Auto" auto
)))
1409 (cons (const plain-list-item
)
1410 (choice (const :tag
"Never" nil
)
1411 (const :tag
"Always" t
)
1412 (const :tag
"Auto" auto
)))))
1414 (defcustom org-insert-heading-hook nil
1415 "Hook being run after inserting a new heading."
1416 :group
'org-edit-structure
1419 (defcustom org-enable-fixed-width-editor t
1420 "Non-nil means lines starting with \":\" are treated as fixed-width.
1421 This currently only means they are never auto-wrapped.
1422 When nil, such lines will be treated like ordinary lines.
1423 See also the QUOTE keyword."
1424 :group
'org-edit-structure
1427 (defcustom org-goto-auto-isearch t
1428 "Non-nil means typing characters in `org-goto' starts incremental search.
1429 When nil, you can use these keybindings to navigate the buffer:
1431 q Quit the org-goto interface
1432 n Go to the next visible heading
1433 p Go to the previous visible heading
1434 f Go one heading forward on same level
1435 b Go one heading backward on same level
1436 u Go one heading up"
1437 :group
'org-edit-structure
1440 (defgroup org-sparse-trees nil
1441 "Options concerning sparse trees in Org-mode."
1442 :tag
"Org Sparse Trees"
1443 :group
'org-structure
)
1445 (defcustom org-highlight-sparse-tree-matches t
1446 "Non-nil means highlight all matches that define a sparse tree.
1447 The highlights will automatically disappear the next time the buffer is
1448 changed by an edit command."
1449 :group
'org-sparse-trees
1452 (defcustom org-remove-highlights-with-change t
1453 "Non-nil means any change to the buffer will remove temporary highlights.
1454 Such highlights are created by `org-occur' and `org-clock-display'.
1455 When nil, `C-c C-c' needs to be used to get rid of the highlights.
1456 The highlights created by `org-preview-latex-fragment' always need
1457 `C-c C-c' to be removed."
1458 :group
'org-sparse-trees
1463 (defcustom org-occur-hook
'(org-first-headline-recenter)
1464 "Hook that is run after `org-occur' has constructed a sparse tree.
1465 This can be used to recenter the window to show as much of the structure
1467 :group
'org-sparse-trees
1470 (defgroup org-imenu-and-speedbar nil
1471 "Options concerning imenu and speedbar in Org-mode."
1472 :tag
"Org Imenu and Speedbar"
1473 :group
'org-structure
)
1475 (defcustom org-imenu-depth
2
1476 "The maximum level for Imenu access to Org-mode headlines.
1477 This also applied for speedbar access."
1478 :group
'org-imenu-and-speedbar
1481 (defgroup org-table nil
1482 "Options concerning tables in Org-mode."
1486 (defcustom org-enable-table-editor
'optimized
1487 "Non-nil means lines starting with \"|\" are handled by the table editor.
1488 When nil, such lines will be treated like ordinary lines.
1490 When equal to the symbol `optimized', the table editor will be optimized to
1492 - Automatic overwrite mode in front of whitespace in table fields.
1493 This makes the structure of the table stay in tact as long as the edited
1494 field does not exceed the column width.
1495 - Minimize the number of realigns. Normally, the table is aligned each time
1496 TAB or RET are pressed to move to another field. With optimization this
1497 happens only if changes to a field might have changed the column width.
1498 Optimization requires replacing the functions `self-insert-command',
1499 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1500 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1501 very good at guessing when a re-align will be necessary, but you can always
1502 force one with \\[org-ctrl-c-ctrl-c].
1504 If you would like to use the optimized version in Org-mode, but the
1505 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1507 This variable can be used to turn on and off the table editor during a session,
1508 but in order to toggle optimization, a restart is required.
1510 See also the variable `org-table-auto-blank-field'."
1513 (const :tag
"off" nil
)
1515 (const :tag
"on, optimized" optimized
)))
1517 (defcustom org-self-insert-cluster-for-undo
(or (featurep 'xemacs
)
1518 (version<= emacs-version
"24.1"))
1519 "Non-nil means cluster self-insert commands for undo when possible.
1520 If this is set, then, like in the Emacs command loop, 20 consecutive
1521 characters will be undone together.
1522 This is configurable, because there is some impact on typing performance."
1526 (defcustom org-table-tab-recognizes-table.el t
1527 "Non-nil means TAB will automatically notice a table.el table.
1528 When it sees such a table, it moves point into it and - if necessary -
1529 calls `table-recognize-table'."
1530 :group
'org-table-editing
1533 (defgroup org-link nil
1534 "Options concerning links in Org-mode."
1538 (defvar org-link-abbrev-alist-local nil
1539 "Buffer-local version of `org-link-abbrev-alist', which see.
1540 The value of this is taken from the #+LINK lines.")
1541 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1543 (defcustom org-link-abbrev-alist nil
1544 "Alist of link abbreviations.
1545 The car of each element is a string, to be replaced at the start of a link.
1546 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1547 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1549 [[linkkey:tag][description]]
1551 The `linkkey' must be a word word, starting with a letter, followed
1552 by letters, numbers, `-' or `_'.
1554 If REPLACE is a string, the tag will simply be appended to create the link.
1555 If the string contains \"%s\", the tag will be inserted there. If the string
1556 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1557 at that point (see the function `url-hexify-string'). If the string contains
1558 the specifier \"%(my-function)\", then the custom function `my-function' will
1559 be invoked: this function takes the tag as its only argument and must return
1562 REPLACE may also be a function that will be called with the tag as the
1563 only argument to create the link, which should be returned as a string.
1565 See the manual for examples."
1569 (string :tag
"Protocol")
1571 (string :tag
"Format")
1574 (defcustom org-descriptive-links t
1575 "Non-nil means Org will display descriptive links.
1576 E.g. [[http://orgmode.org][Org website]] will be displayed as
1577 \"Org Website\", hiding the link itself and just displaying its
1578 description. When set to nil, Org will display the full links
1581 You can interactively set the value of this variable by calling
1582 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1586 (defcustom org-link-file-path-type
'adaptive
1587 "How the path name in file links should be stored.
1590 relative Relative to the current directory, i.e. the directory of the file
1591 into which the link is being inserted.
1592 absolute Absolute path, if possible with ~ for home directory.
1593 noabbrev Absolute path, no abbreviation of home directory.
1594 adaptive Use relative path for files in the current directory and sub-
1595 directories of it. For other files, use an absolute path."
1603 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1604 "Types of links that should be activated in Org-mode files.
1605 This is a list of symbols, each leading to the activation of a certain link
1606 type. In principle, it does not hurt to turn on most link types - there may
1607 be a small gain when turning off unused link types. The types are:
1609 bracket The recommended [[link][description]] or [[link]] links with hiding.
1610 angle Links in angular brackets that may contain whitespace like
1611 <bbdb:Carsten Dominik>.
1612 plain Plain links in normal text, no whitespace, like http://google.com.
1613 radio Text that is matched by a radio target, see manual for details.
1614 tag Tag settings in a headline (link to tag search).
1615 date Time stamps (link to calendar).
1616 footnote Footnote labels.
1618 Changing this variable requires a restart of Emacs to become effective."
1620 :type
'(set :greedy t
1621 (const :tag
"Double bracket links" bracket
)
1622 (const :tag
"Angular bracket links" angle
)
1623 (const :tag
"Plain text links" plain
)
1624 (const :tag
"Radio target matches" radio
)
1625 (const :tag
"Tags" tag
)
1626 (const :tag
"Timestamps" date
)
1627 (const :tag
"Footnotes" footnote
)))
1629 (defcustom org-make-link-description-function nil
1630 "Function to use for generating link descriptions from links.
1631 When nil, the link location will be used. This function must take
1632 two parameters: the first one is the link, the second one is the
1633 description generated by `org-insert-link'. The function should
1634 return the description to use."
1636 :type
'(choice (const nil
) (function)))
1638 (defgroup org-link-store nil
1639 "Options concerning storing links in Org-mode."
1640 :tag
"Org Store Link"
1643 (defcustom org-url-hexify-p t
1644 "When non-nil, hexify URL when creating a link."
1647 :group
'org-link-store
)
1649 (defcustom org-email-link-description-format
"Email %c: %.30s"
1650 "Format of the description part of a link to an email or usenet message.
1651 The following %-escapes will be replaced by corresponding information:
1653 %F full \"From\" field
1654 %f name, taken from \"From\" field, address if no name
1655 %T full \"To\" field
1656 %t first name in \"To\" field, address if no name
1657 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1658 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1663 You may use normal field width specification between the % and the letter.
1664 This is for example useful to limit the length of the subject.
1666 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1667 :group
'org-link-store
1670 (defcustom org-from-is-user-regexp
1672 (when (and user-mail-address
(not (string= user-mail-address
"")))
1673 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1674 (when (and user-full-name
(not (string= user-full-name
"")))
1675 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1676 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1677 "Regexp matched against the \"From:\" header of an email or usenet message.
1678 It should match if the message is from the user him/herself."
1679 :group
'org-link-store
1682 (defcustom org-context-in-file-links t
1683 "Non-nil means file links from `org-store-link' contain context.
1684 A search string will be added to the file name with :: as separator and
1685 used to find the context when the link is activated by the command
1686 `org-open-at-point'. When this option is t, the entire active region
1687 will be placed in the search string of the file link. If set to a
1688 positive integer, only the first n lines of context will be stored.
1690 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1691 negates this setting for the duration of the command."
1692 :group
'org-link-store
1693 :type
'(choice boolean integer
))
1695 (defcustom org-keep-stored-link-after-insertion nil
1696 "Non-nil means keep link in list for entire session.
1698 The command `org-store-link' adds a link pointing to the current
1699 location to an internal list. These links accumulate during a session.
1700 The command `org-insert-link' can be used to insert links into any
1701 Org-mode file (offering completion for all stored links). When this
1702 option is nil, every link which has been inserted once using \\[org-insert-link]
1703 will be removed from the list, to make completing the unused links
1705 :group
'org-link-store
1708 (defgroup org-link-follow nil
1709 "Options concerning following links in Org-mode."
1710 :tag
"Org Follow Link"
1713 (defcustom org-link-translation-function nil
1714 "Function to translate links with different syntax to Org syntax.
1715 This can be used to translate links created for example by the Planner
1716 or emacs-wiki packages to Org syntax.
1717 The function must accept two parameters, a TYPE containing the link
1718 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1719 which is everything after the link protocol. It should return a cons
1720 with possibly modified values of type and path.
1721 Org contains a function for this, so if you set this variable to
1722 `org-translate-link-from-planner', you should be able follow many
1723 links created by planner."
1724 :group
'org-link-follow
1725 :type
'(choice (const nil
) (function)))
1727 (defcustom org-follow-link-hook nil
1728 "Hook that is run after a link has been followed."
1729 :group
'org-link-follow
1732 (defcustom org-tab-follows-link nil
1733 "Non-nil means on links TAB will follow the link.
1734 Needs to be set before org.el is loaded.
1735 This really should not be used, it does not make sense, and the
1736 implementation is bad."
1737 :group
'org-link-follow
1740 (defcustom org-return-follows-link nil
1741 "Non-nil means on links RET will follow the link.
1742 In tables, the special behavior of RET has precedence."
1743 :group
'org-link-follow
1746 (defcustom org-mouse-1-follows-link
1747 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1748 "Non-nil means mouse-1 on a link will follow the link.
1749 A longer mouse click will still set point. Does not work on XEmacs.
1750 Needs to be set before org.el is loaded."
1751 :group
'org-link-follow
1753 :package-version
'(Org .
"8.3")
1755 (const :tag
"A double click follows the link" double
)
1756 (const :tag
"Unconditionally follow the link with mouse-1" t
)
1757 (integer :tag
"mouse-1 click does not follow the link if longer than N ms" 450)))
1759 (defcustom org-mark-ring-length
4
1760 "Number of different positions to be recorded in the ring.
1761 Changing this requires a restart of Emacs to work correctly."
1762 :group
'org-link-follow
1765 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1766 "Non-nil means internal links in Org files must exactly match a headline.
1767 When nil, the link search tries to match a phrase with all words
1768 in the search text."
1769 :group
'org-link-follow
1772 (const :tag
"Use fuzzy text search" nil
)
1773 (const :tag
"Match only exact headline" t
)
1774 (const :tag
"Match exact headline or query to create it"
1777 (defcustom org-link-frame-setup
1778 '((vm . vm-visit-folder-other-frame
)
1779 (vm-imap . vm-visit-imap-folder-other-frame
)
1780 (gnus . org-gnus-no-new-news
)
1781 (file . find-file-other-window
)
1782 (wl . wl-other-frame
))
1783 "Setup the frame configuration for following links.
1784 When following a link with Emacs, it may often be useful to display
1785 this link in another window or frame. This variable can be used to
1786 set this up for the different types of links.
1789 `vm-visit-folder-other-window'
1790 `vm-visit-folder-other-frame'
1791 For Gnus, use any of
1794 `org-gnus-no-new-news'
1795 For FILE, use any of
1797 `find-file-other-window'
1798 `find-file-other-frame'
1799 For Wanderlust use any of
1802 For the calendar, use the variable `calendar-setup'.
1803 For BBDB, it is currently only possible to display the matches in
1805 :group
'org-link-follow
1809 (const vm-visit-folder
)
1810 (const vm-visit-folder-other-window
)
1811 (const vm-visit-folder-other-frame
)))
1812 (cons (const vm-imap
)
1814 (const vm-visit-imap-folder
)
1815 (const vm-visit-imap-folder-other-window
)
1816 (const vm-visit-imap-folder-other-frame
)))
1820 (const gnus-other-frame
)
1821 (const org-gnus-no-new-news
)))
1825 (const find-file-other-window
)
1826 (const find-file-other-frame
)))
1830 (const wl-other-frame
)))))
1832 (defcustom org-display-internal-link-with-indirect-buffer nil
1833 "Non-nil means use indirect buffer to display infile links.
1834 Activating internal links (from one location in a file to another location
1835 in the same file) normally just jumps to the location. When the link is
1836 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1838 another window. When this option is set, the other window actually displays
1839 an indirect buffer clone of the current buffer, to avoid any visibility
1840 changes to the current buffer."
1841 :group
'org-link-follow
1844 (defcustom org-open-non-existing-files nil
1845 "Non-nil means `org-open-file' will open non-existing files.
1846 When nil, an error will be generated.
1847 This variable applies only to external applications because they
1848 might choke on non-existing files. If the link is to a file that
1849 will be opened in Emacs, the variable is ignored."
1850 :group
'org-link-follow
1853 (defcustom org-open-directory-means-index-dot-org nil
1854 "Non-nil means a link to a directory really means to index.org.
1855 When nil, following a directory link will run dired or open a finder/explorer
1856 window on that directory."
1857 :group
'org-link-follow
1860 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1861 "Function and arguments to call for following mailto links.
1862 This is a list with the first element being a Lisp function, and the
1863 remaining elements being arguments to the function. In string arguments,
1864 %a will be replaced by the address, and %s will be replaced by the subject
1865 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1866 :group
'org-link-follow
1868 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1869 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1870 (const :tag
"message-mail" (message-mail "%a" "%s"))
1871 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1873 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1874 "Non-nil means ask for confirmation before executing shell links.
1875 Shell links can be dangerous: just think about a link
1877 [[shell:rm -rf ~/*][Google Search]]
1879 This link would show up in your Org-mode document as \"Google Search\",
1880 but really it would remove your entire home directory.
1881 Therefore we advise against setting this variable to nil.
1882 Just change it to `y-or-n-p' if you want to confirm with a
1883 single keystroke rather than having to type \"yes\"."
1884 :group
'org-link-follow
1886 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1887 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1888 (const :tag
"no confirmation (dangerous)" nil
)))
1889 (put 'org-confirm-shell-link-function
1890 'safe-local-variable
1891 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1893 (defcustom org-confirm-shell-link-not-regexp
""
1894 "A regexp to skip confirmation for shell links."
1895 :group
'org-link-follow
1899 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1900 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1901 Elisp links can be dangerous: just think about a link
1903 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1905 This link would show up in your Org-mode document as \"Google Search\",
1906 but really it would remove your entire home directory.
1907 Therefore we advise against setting this variable to nil.
1908 Just change it to `y-or-n-p' if you want to confirm with a
1909 single keystroke rather than having to type \"yes\"."
1910 :group
'org-link-follow
1912 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1913 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1914 (const :tag
"no confirmation (dangerous)" nil
)))
1915 (put 'org-confirm-shell-link-function
1916 'safe-local-variable
1917 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1919 (defcustom org-confirm-elisp-link-not-regexp
""
1920 "A regexp to skip confirmation for Elisp links."
1921 :group
'org-link-follow
1925 (defconst org-file-apps-defaults-gnu
1929 "Default file applications on a UNIX or GNU/Linux system.
1930 See `org-file-apps'.")
1932 (defconst org-file-apps-defaults-macosx
1935 (system .
"open %s")
1937 ("eps.gz" .
"gv %s")
1939 ("fig" .
"xfig %s"))
1940 "Default file applications on a MacOS X system.
1941 The system \"open\" is known as a default, but we use X11 applications
1942 for some files for which the OS does not have a good default.
1943 See `org-file-apps'.")
1945 (defconst org-file-apps-defaults-windowsnt
1949 (list (if (featurep 'xemacs
)
1950 'mswindows-shell-execute
1954 (list (if (featurep 'xemacs
)
1955 'mswindows-shell-execute
1958 "Default file applications on a Windows NT system.
1959 The system \"open\" is used for most files.
1960 See `org-file-apps'.")
1962 (defcustom org-file-apps
1963 '((auto-mode . emacs
)
1964 ("\\.mm\\'" . default
)
1965 ("\\.x?html?\\'" . default
)
1966 ("\\.pdf\\'" . default
))
1967 "External applications for opening `file:path' items in a document.
1968 Org-mode uses system defaults for different file types, but
1969 you can use this variable to set the application for a given file
1970 extension. The entries in this list are cons cells where the car identifies
1971 files and the cdr the corresponding command. Possible values for the
1973 \"string\" A string as a file identifier can be interpreted in different
1974 ways, depending on its contents:
1976 - Alphanumeric characters only:
1977 Match links with this file extension.
1978 Example: (\"pdf\" . \"evince %s\")
1979 to open PDFs with evince.
1981 - Regular expression: Match links where the
1982 filename matches the regexp. If you want to
1983 use groups here, use shy groups.
1985 Example: (\"\\.x?html\\\\='\" . \"firefox %s\")
1986 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1987 to open *.html and *.xhtml with firefox.
1989 - Regular expression which contains (non-shy) groups:
1990 Match links where the whole link, including \"::\", and
1991 anything after that, matches the regexp.
1992 In a custom command string, %1, %2, etc. are replaced with
1993 the parts of the link that were matched by the groups.
1994 For backwards compatibility, if a command string is given
1995 that does not use any of the group matches, this case is
1996 handled identically to the second one (i.e. match against
1998 In a custom lisp form, you can access the group matches with
1999 (match-string n link).
2001 Example: (\"\\.pdf::\\(\\d+\\)\\\\='\" . \"evince -p %1 %s\")
2002 to open [[file:document.pdf::5]] with evince at page 5.
2004 `directory' Matches a directory
2005 `remote' Matches a remote file, accessible through tramp or efs.
2006 Remote files most likely should be visited through Emacs
2007 because external applications cannot handle such paths.
2008 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
2009 so all files Emacs knows how to handle. Using this with
2010 command `emacs' will open most files in Emacs. Beware that this
2011 will also open html files inside Emacs, unless you add
2012 (\"html\" . default) to the list as well.
2013 t Default for files not matched by any of the other options.
2014 `system' The system command to open files, like `open' on Windows
2015 and Mac OS X, and mailcap under GNU/Linux. This is the command
2016 that will be selected if you call `C-c C-o' with a double
2017 \\[universal-argument] \\[universal-argument] prefix.
2019 Possible values for the command are:
2020 `emacs' The file will be visited by the current Emacs process.
2021 `default' Use the default application for this file type, which is the
2022 association for t in the list, most likely in the system-specific
2024 This can be used to overrule an unwanted setting in the
2025 system-specific variable.
2026 `system' Use the system command for opening files, like \"open\".
2027 This command is specified by the entry whose car is `system'.
2028 Most likely, the system-specific version of this variable
2029 does define this command, but you can overrule/replace it
2031 string A command to be executed by a shell; %s will be replaced
2032 by the path to the file.
2033 sexp A Lisp form which will be evaluated. The file path will
2034 be available in the Lisp variable `file'.
2035 For more examples, see the system specific constants
2036 `org-file-apps-defaults-macosx'
2037 `org-file-apps-defaults-windowsnt'
2038 `org-file-apps-defaults-gnu'."
2039 :group
'org-link-follow
2041 (cons (choice :value
""
2042 (string :tag
"Extension")
2043 (const :tag
"System command to open files" system
)
2044 (const :tag
"Default for unrecognized files" t
)
2045 (const :tag
"Remote file" remote
)
2046 (const :tag
"Links to a directory" directory
)
2047 (const :tag
"Any files that have Emacs modes"
2050 (const :tag
"Visit with Emacs" emacs
)
2051 (const :tag
"Use default" default
)
2052 (const :tag
"Use the system command" system
)
2053 (string :tag
"Command")
2054 (sexp :tag
"Lisp form")))))
2056 (defcustom org-doi-server-url
"http://dx.doi.org/"
2057 "The URL of the DOI server."
2060 :group
'org-link-follow
)
2062 (defgroup org-refile nil
2063 "Options concerning refiling entries in Org-mode."
2067 (defcustom org-directory
"~/org"
2068 "Directory with org files.
2069 This is just a default location to look for Org files. There is no need
2070 at all to put your files into this directory. It is only used in the
2071 following situations:
2073 1. When a capture template specifies a target file that is not an
2074 absolute path. The path will then be interpreted relative to
2076 2. When a capture note is filed away in an interactive way (when exiting the
2077 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
2078 with `org-directory' as the default path."
2083 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
2084 "Default target for storing notes.
2085 Used as a fall back file for org-capture.el, for templates that
2086 do not specify a target file."
2090 (const :tag
"Default from remember-data-file" nil
)
2093 (defcustom org-goto-interface
'outline
2094 "The default interface to be used for `org-goto'.
2096 outline The interface shows an outline of the relevant file
2097 and the correct heading is found by moving through
2098 the outline or by searching with incremental search.
2099 outline-path-completion Headlines in the current buffer are offered via
2100 completion. This is the interface also used by
2101 the refile command."
2104 (const :tag
"Outline" outline
)
2105 (const :tag
"Outline-path-completion" outline-path-completion
)))
2107 (defcustom org-goto-max-level
5
2108 "Maximum target level when running `org-goto' with refile interface."
2112 (defcustom org-reverse-note-order nil
2113 "Non-nil means store new notes at the beginning of a file or entry.
2114 When nil, new notes will be filed to the end of a file or entry.
2115 This can also be a list with cons cells of regular expressions that
2116 are matched against file names, and values."
2120 (const :tag
"Reverse always" t
)
2121 (const :tag
"Reverse never" nil
)
2122 (repeat :tag
"By file name regexp"
2123 (cons regexp boolean
))))
2125 (defcustom org-log-refile nil
2126 "Information to record when a task is refiled.
2128 Possible values are:
2130 nil Don't add anything
2131 time Add a time stamp to the task
2132 note Prompt for a note and add it with template `org-log-note-headings'
2134 This option can also be set with on a per-file-basis with
2136 #+STARTUP: nologrefile
2137 #+STARTUP: logrefile
2138 #+STARTUP: lognoterefile
2140 You can have local logging settings for a subtree by setting the LOGGING
2141 property to one or more of these keywords.
2143 When bulk-refiling from the agenda, the value `note' is forbidden and
2144 will temporarily be changed to `time'."
2146 :group
'org-progress
2149 (const :tag
"No logging" nil
)
2150 (const :tag
"Record timestamp" time
)
2151 (const :tag
"Record timestamp with note." note
)))
2153 (defcustom org-refile-targets nil
2154 "Targets for refiling entries with \\[org-refile].
2155 This is a list of cons cells. Each cell contains:
2156 - a specification of the files to be considered, either a list of files,
2157 or a symbol whose function or variable value will be used to retrieve
2158 a file name or a list of file names. If you use `org-agenda-files' for
2159 that, all agenda files will be scanned for targets. Nil means consider
2160 headings in the current buffer.
2161 - A specification of how to find candidate refile targets. This may be
2163 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
2164 This tag has to be present in all target headlines, inheritance will
2166 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
2168 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
2169 headlines that are refiling targets.
2170 - a cons cell (:level . N). Any headline of level N is considered a target.
2171 Note that, when `org-odd-levels-only' is set, level corresponds to
2172 order in hierarchy, not to the number of stars.
2173 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
2174 Note that, when `org-odd-levels-only' is set, level corresponds to
2175 order in hierarchy, not to the number of stars.
2177 Each element of this list generates a set of possible targets.
2178 The union of these sets is presented (with completion) to
2179 the user by `org-refile'.
2181 You can set the variable `org-refile-target-verify-function' to a function
2182 to verify each headline found by the simple criteria above.
2184 When this variable is nil, all top-level headlines in the current buffer
2185 are used, equivalent to the value `((nil . (:level . 1))'."
2189 (choice :value org-agenda-files
2190 (const :tag
"All agenda files" org-agenda-files
)
2191 (const :tag
"Current buffer" nil
)
2192 (function) (variable) (file))
2193 (choice :tag
"Identify target headline by"
2194 (cons :tag
"Specific tag" (const :value
:tag
) (string))
2195 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
2196 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
2197 (cons :tag
"Level number" (const :value
:level
) (integer))
2198 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
2200 (defcustom org-refile-target-verify-function nil
2201 "Function to verify if the headline at point should be a refile target.
2202 The function will be called without arguments, with point at the
2203 beginning of the headline. It should return t and leave point
2204 where it is if the headline is a valid target for refiling.
2206 If the target should not be selected, the function must return nil.
2207 In addition to this, it may move point to a place from where the search
2208 should be continued. For example, the function may decide that the entire
2209 subtree of the current entry should be excluded and move point to the end
2216 (defcustom org-refile-use-cache nil
2217 "Non-nil means cache refile targets to speed up the process.
2218 The cache for a particular file will be updated automatically when
2219 the buffer has been killed, or when any of the marker used for flagging
2220 refile targets no longer points at a live buffer.
2221 If you have added new entries to a buffer that might themselves be targets,
2222 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2223 find that easier, `C-u C-u C-u C-c C-w'."
2228 (defcustom org-refile-use-outline-path nil
2229 "Non-nil means provide refile targets as paths.
2230 So a level 3 headline will be available as level1/level2/level3.
2232 When the value is `file', also include the file name (without directory)
2233 into the path. In this case, you can also stop the completion after
2234 the file name, to get entries inserted as top level in the file.
2236 When `full-file-path', include the full file path."
2239 (const :tag
"Not" nil
)
2240 (const :tag
"Yes" t
)
2241 (const :tag
"Start with file name" file
)
2242 (const :tag
"Start with full file path" full-file-path
)))
2244 (defcustom org-outline-path-complete-in-steps t
2245 "Non-nil means complete the outline path in hierarchical steps.
2246 When Org-mode uses the refile interface to select an outline path
2247 \(see variable `org-refile-use-outline-path'), the completion of
2248 the path can be done is a single go, or if can be done in steps down
2249 the headline hierarchy. Going in steps is probably the best if you
2250 do not use a special completion package like `ido' or `icicles'.
2251 However, when using these packages, going in one step can be very
2252 fast, while still showing the whole path to the entry."
2256 (defcustom org-refile-allow-creating-parent-nodes nil
2257 "Non-nil means allow the creation of new nodes as refile targets.
2258 New nodes are then created by adding \"/new node name\" to the completion
2259 of an existing node. When the value of this variable is `confirm',
2260 new node creation must be confirmed by the user (recommended).
2261 When nil, the completion must match an existing entry.
2263 Note that, if the new heading is not seen by the criteria
2264 listed in `org-refile-targets', multiple instances of the same
2265 heading would be created by trying again to file under the new
2269 (const :tag
"Never" nil
)
2270 (const :tag
"Always" t
)
2271 (const :tag
"Prompt for confirmation" confirm
)))
2273 (defcustom org-refile-active-region-within-subtree nil
2274 "Non-nil means also refile active region within a subtree.
2276 By default `org-refile' doesn't allow refiling regions if they
2277 don't contain a set of subtrees, but it might be convenient to
2278 do so sometimes: in that case, the first line of the region is
2279 converted to a headline before refiling."
2284 (defgroup org-todo nil
2285 "Options concerning TODO items in Org-mode."
2289 (defgroup org-progress nil
2290 "Options concerning Progress logging in Org-mode."
2294 (defvar org-todo-interpretation-widgets
2295 '((:tag
"Sequence (cycling hits every state)" sequence
)
2296 (:tag
"Type (cycling directly to DONE)" type
))
2297 "The available interpretation symbols for customizing `org-todo-keywords'.
2298 Interested libraries should add to this list.")
2300 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2301 "List of TODO entry keyword sequences and their interpretation.
2302 \\<org-mode-map>This is a list of sequences.
2304 Each sequence starts with a symbol, either `sequence' or `type',
2305 indicating if the keywords should be interpreted as a sequence of
2306 action steps, or as different types of TODO items. The first
2307 keywords are states requiring action - these states will select a headline
2308 for inclusion into the global TODO list Org-mode produces. If one of
2309 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2310 signify that no further action is necessary. If \"|\" is not found,
2311 the last keyword is treated as the only DONE state of the sequence.
2313 The command \\[org-todo] cycles an entry through these states, and one
2314 additional state where no keyword is present. For details about this
2315 cycling, see the manual.
2317 TODO keywords and interpretation can also be set on a per-file basis with
2318 the special #+SEQ_TODO and #+TYP_TODO lines.
2320 Each keyword can optionally specify a character for fast state selection
2321 \(in combination with the variable `org-use-fast-todo-selection')
2322 and specifiers for state change logging, using the same syntax that
2323 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2324 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2325 indicates to record a time stamp each time this state is selected.
2327 Each keyword may also specify if a timestamp or a note should be
2328 recorded when entering or leaving the state, by adding additional
2329 characters in the parenthesis after the keyword. This looks like this:
2330 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2331 record only the time of the state change. With X and Y being either
2332 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2333 Y when leaving the state if and only if the *target* state does not
2334 define X. You may omit any of the fast-selection key or X or /Y,
2335 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2337 For backward compatibility, this variable may also be just a list
2338 of keywords. In this case the interpretation (sequence or type) will be
2339 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2341 :group
'org-keywords
2343 (repeat :tag
"Old syntax, just keywords"
2344 (string :tag
"Keyword"))
2345 (repeat :tag
"New syntax"
2348 :tag
"Interpretation"
2349 ;;Quick and dirty way to see
2350 ;;`org-todo-interpretations'. This takes the
2351 ;;place of item arguments
2359 org-todo-interpretation-widgets
))
2362 (string :tag
"Keyword"))))))
2364 (defvar org-todo-keywords-1 nil
2365 "All TODO and DONE keywords active in a buffer.")
2366 (make-variable-buffer-local 'org-todo-keywords-1
)
2367 (defvar org-todo-keywords-for-agenda nil
)
2368 (defvar org-done-keywords-for-agenda nil
)
2369 (defvar org-drawers-for-agenda nil
)
2370 (defvar org-todo-keyword-alist-for-agenda nil
)
2371 (defvar org-tag-alist-for-agenda nil
2372 "Alist of all tags from all agenda files.")
2373 (defvar org-tag-groups-alist-for-agenda nil
2374 "Alist of all groups tags from all current agenda files.")
2375 (defvar org-tag-groups-alist nil
)
2376 (make-variable-buffer-local 'org-tag-groups-alist
)
2377 (defvar org-agenda-contributing-files nil
)
2378 (defvar org-not-done-keywords nil
)
2379 (make-variable-buffer-local 'org-not-done-keywords
)
2380 (defvar org-done-keywords nil
)
2381 (make-variable-buffer-local 'org-done-keywords
)
2382 (defvar org-todo-heads nil
)
2383 (make-variable-buffer-local 'org-todo-heads
)
2384 (defvar org-todo-sets nil
)
2385 (make-variable-buffer-local 'org-todo-sets
)
2386 (defvar org-todo-log-states nil
)
2387 (make-variable-buffer-local 'org-todo-log-states
)
2388 (defvar org-todo-kwd-alist nil
)
2389 (make-variable-buffer-local 'org-todo-kwd-alist
)
2390 (defvar org-todo-key-alist nil
)
2391 (make-variable-buffer-local 'org-todo-key-alist
)
2392 (defvar org-todo-key-trigger nil
)
2393 (make-variable-buffer-local 'org-todo-key-trigger
)
2395 (defcustom org-todo-interpretation
'sequence
2396 "Controls how TODO keywords are interpreted.
2397 This variable is in principle obsolete and is only used for
2398 backward compatibility, if the interpretation of todo keywords is
2399 not given already in `org-todo-keywords'. See that variable for
2402 :group
'org-keywords
2403 :type
'(choice (const sequence
)
2406 (defcustom org-use-fast-todo-selection t
2407 "Non-nil means use the fast todo selection scheme with C-c C-t.
2408 This variable describes if and under what circumstances the cycling
2409 mechanism for TODO keywords will be replaced by a single-key, direct
2412 When nil, fast selection is never used.
2414 When the symbol `prefix', it will be used when `org-todo' is called
2415 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2416 `C-u t' in an agenda buffer.
2418 When t, fast selection is used by default. In this case, the prefix
2419 argument forces cycling instead.
2421 In all cases, the special interface is only used if access keys have
2422 actually been assigned by the user, i.e. if keywords in the configuration
2423 are followed by a letter in parenthesis, like TODO(t)."
2426 (const :tag
"Never" nil
)
2427 (const :tag
"By default" t
)
2428 (const :tag
"Only with C-u C-c C-t" prefix
)))
2430 (defcustom org-provide-todo-statistics t
2431 "Non-nil means update todo statistics after insert and toggle.
2432 ALL-HEADLINES means update todo statistics by including headlines
2433 with no TODO keyword as well, counting them as not done.
2434 A list of TODO keywords means the same, but skip keywords that are
2437 When this is set, todo statistics is updated in the parent of the
2438 current entry each time a todo state is changed."
2441 (const :tag
"Yes, only for TODO entries" t
)
2442 (const :tag
"Yes, including all entries" all-headlines
)
2443 (repeat :tag
"Yes, for TODOs in this list"
2444 (string :tag
"TODO keyword"))
2445 (other :tag
"No TODO statistics" nil
)))
2447 (defcustom org-hierarchical-todo-statistics t
2448 "Non-nil means TODO statistics covers just direct children.
2449 When nil, all entries in the subtree are considered.
2450 This has only an effect if `org-provide-todo-statistics' is set.
2451 To set this to nil for only a single subtree, use a COOKIE_DATA
2452 property and include the word \"recursive\" into the value."
2456 (defcustom org-after-todo-state-change-hook nil
2457 "Hook which is run after the state of a TODO item was changed.
2458 The new state (a string with a TODO keyword, or nil) is available in the
2459 Lisp variable `org-state'."
2463 (defvar org-blocker-hook nil
2464 "Hook for functions that are allowed to block a state change.
2466 Functions in this hook should not modify the buffer.
2467 Each function gets as its single argument a property list,
2468 see `org-trigger-hook' for more information about this list.
2470 If any of the functions in this hook returns nil, the state change
2473 (defvar org-trigger-hook nil
2474 "Hook for functions that are triggered by a state change.
2476 Each function gets as its single argument a property list with at
2477 least the following elements:
2479 (:type type-of-change :position pos-at-entry-start
2480 :from old-state :to new-state)
2482 Depending on the type, more properties may be present.
2484 This mechanism is currently implemented for:
2488 :type todo-state-change
2489 :from previous state (keyword as a string), or nil, or a symbol
2490 `todo' or `done', to indicate the general type of state.
2491 :to new state, like in :from")
2493 (defcustom org-enforce-todo-dependencies nil
2494 "Non-nil means undone TODO entries will block switching the parent to DONE.
2495 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2496 be blocked if any prior sibling is not yet done.
2497 Finally, if the parent is blocked because of ordered siblings of its own,
2498 the child will also be blocked."
2499 :set
(lambda (var val
)
2502 (add-hook 'org-blocker-hook
2503 'org-block-todo-from-children-or-siblings-or-parent
)
2504 (remove-hook 'org-blocker-hook
2505 'org-block-todo-from-children-or-siblings-or-parent
)))
2509 (defcustom org-enforce-todo-checkbox-dependencies nil
2510 "Non-nil means unchecked boxes will block switching the parent to DONE.
2511 When this is nil, checkboxes have no influence on switching TODO states.
2512 When non-nil, you first need to check off all check boxes before the TODO
2513 entry can be switched to DONE.
2514 This variable needs to be set before org.el is loaded, and you need to
2515 restart Emacs after a change to make the change effective. The only way
2516 to change is while Emacs is running is through the customize interface."
2517 :set
(lambda (var val
)
2520 (add-hook 'org-blocker-hook
2521 'org-block-todo-from-checkboxes
)
2522 (remove-hook 'org-blocker-hook
2523 'org-block-todo-from-checkboxes
)))
2527 (defcustom org-treat-insert-todo-heading-as-state-change nil
2528 "Non-nil means inserting a TODO heading is treated as state change.
2529 So when the command \\[org-insert-todo-heading] is used, state change
2530 logging will apply if appropriate. When nil, the new TODO item will
2531 be inserted directly, and no logging will take place."
2535 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2536 "Non-nil means switching TODO states with S-cursor counts as state change.
2537 This is the default behavior. However, setting this to nil allows a
2538 convenient way to select a TODO state and bypass any logging associated
2543 (defcustom org-todo-state-tags-triggers nil
2544 "Tag changes that should be triggered by TODO state changes.
2545 This is a list. Each entry is
2547 (state-change (tag . flag) .......)
2549 State-change can be a string with a state, and empty string to indicate the
2550 state that has no TODO keyword, or it can be one of the symbols `todo'
2551 or `done', meaning any not-done or done state, respectively."
2555 (cons (choice :tag
"When changing to"
2556 (const :tag
"Not-done state" todo
)
2557 (const :tag
"Done state" done
)
2558 (string :tag
"State"))
2560 (cons :tag
"Tag action"
2562 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2564 (defcustom org-log-done nil
2565 "Information to record when a task moves to the DONE state.
2567 Possible values are:
2569 nil Don't add anything, just change the keyword
2570 time Add a time stamp to the task
2571 note Prompt for a note and add it with template `org-log-note-headings'
2573 This option can also be set with on a per-file-basis with
2575 #+STARTUP: nologdone
2577 #+STARTUP: lognotedone
2579 You can have local logging settings for a subtree by setting the LOGGING
2580 property to one or more of these keywords."
2582 :group
'org-progress
2584 (const :tag
"No logging" nil
)
2585 (const :tag
"Record CLOSED timestamp" time
)
2586 (const :tag
"Record CLOSED timestamp with note." note
)))
2588 ;; Normalize old uses of org-log-done.
2590 ((eq org-log-done t
) (setq org-log-done
'time
))
2591 ((and (listp org-log-done
) (memq 'done org-log-done
))
2592 (setq org-log-done
'note
)))
2594 (defcustom org-log-reschedule nil
2595 "Information to record when the scheduling date of a tasks is modified.
2597 Possible values are:
2599 nil Don't add anything, just change the date
2600 time Add a time stamp to the task
2601 note Prompt for a note and add it with template `org-log-note-headings'
2603 This option can also be set with on a per-file-basis with
2605 #+STARTUP: nologreschedule
2606 #+STARTUP: logreschedule
2607 #+STARTUP: lognotereschedule"
2609 :group
'org-progress
2611 (const :tag
"No logging" nil
)
2612 (const :tag
"Record timestamp" time
)
2613 (const :tag
"Record timestamp with note." note
)))
2615 (defcustom org-log-redeadline nil
2616 "Information to record when the deadline date of a tasks is modified.
2618 Possible values are:
2620 nil Don't add anything, just change the date
2621 time Add a time stamp to the task
2622 note Prompt for a note and add it with template `org-log-note-headings'
2624 This option can also be set with on a per-file-basis with
2626 #+STARTUP: nologredeadline
2627 #+STARTUP: logredeadline
2628 #+STARTUP: lognoteredeadline
2630 You can have local logging settings for a subtree by setting the LOGGING
2631 property to one or more of these keywords."
2633 :group
'org-progress
2635 (const :tag
"No logging" nil
)
2636 (const :tag
"Record timestamp" time
)
2637 (const :tag
"Record timestamp with note." note
)))
2639 (defcustom org-log-note-clock-out nil
2640 "Non-nil means record a note when clocking out of an item.
2641 This can also be configured on a per-file basis by adding one of
2642 the following lines anywhere in the buffer:
2644 #+STARTUP: lognoteclock-out
2645 #+STARTUP: nolognoteclock-out"
2647 :group
'org-progress
2650 (defcustom org-log-done-with-time t
2651 "Non-nil means the CLOSED time stamp will contain date and time.
2652 When nil, only the date will be recorded."
2653 :group
'org-progress
2656 (defcustom org-log-note-headings
2657 '((done .
"CLOSING NOTE %t")
2658 (state .
"State %-12s from %-12S %t")
2659 (note .
"Note taken on %t")
2660 (reschedule .
"Rescheduled from %S on %t")
2661 (delschedule .
"Not scheduled, was %S on %t")
2662 (redeadline .
"New deadline from %S on %t")
2663 (deldeadline .
"Removed deadline, was %S on %t")
2664 (refile .
"Refiled on %t")
2666 "Headings for notes added to entries.
2667 The value is an alist, with the car being a symbol indicating the note
2668 context, and the cdr is the heading to be used. The heading may also be the
2670 %t in the heading will be replaced by a time stamp.
2671 %T will be an active time stamp instead the default inactive one
2672 %d will be replaced by a short-format time stamp.
2673 %D will be replaced by an active short-format time stamp.
2674 %s will be replaced by the new TODO state, in double quotes.
2675 %S will be replaced by the old TODO state, in double quotes.
2676 %u will be replaced by the user name.
2677 %U will be replaced by the full user name.
2679 In fact, it is not a good idea to change the `state' entry, because
2680 agenda log mode depends on the format of these entries."
2682 :group
'org-progress
2683 :type
'(list :greedy t
2684 (cons (const :tag
"Heading when closing an item" done
) string
)
2686 "Heading when changing todo state (todo sequence only)"
2688 (cons (const :tag
"Heading when just taking a note" note
) string
)
2689 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2690 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2691 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2692 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2693 (cons (const :tag
"Heading when refiling" refile
) string
)
2694 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
2696 (unless (assq 'note org-log-note-headings
)
2697 (push '(note .
"%t") org-log-note-headings
))
2699 (defcustom org-log-into-drawer nil
2700 "Non-nil means insert state change notes and time stamps into a drawer.
2701 When nil, state changes notes will be inserted after the headline and
2702 any scheduling and clock lines, but not inside a drawer.
2704 The value of this variable should be the name of the drawer to use.
2705 LOGBOOK is proposed as the default drawer for this purpose, you can
2706 also set this to a string to define the drawer of your choice.
2708 A value of t is also allowed, representing \"LOGBOOK\".
2710 A value of t or nil can also be set with on a per-file-basis with
2712 #+STARTUP: logdrawer
2713 #+STARTUP: nologdrawer
2715 If this variable is set, `org-log-state-notes-insert-after-drawers'
2718 You can set the property LOG_INTO_DRAWER to overrule this setting for
2721 :group
'org-progress
2723 (const :tag
"Not into a drawer" nil
)
2724 (const :tag
"LOGBOOK" t
)
2725 (string :tag
"Other")))
2727 (org-defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
)
2729 (defun org-log-into-drawer ()
2730 "Return the value of `org-log-into-drawer', but let properties overrule.
2731 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2732 used instead of the default value."
2733 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit t
)))
2735 ((not p
) org-log-into-drawer
)
2736 ((equal p
"nil") nil
)
2737 ((equal p
"t") "LOGBOOK")
2740 (defcustom org-log-state-notes-insert-after-drawers nil
2741 "Non-nil means insert state change notes after any drawers in entry.
2742 Only the drawers that *immediately* follow the headline and the
2743 deadline/scheduled line are skipped.
2744 When nil, insert notes right after the heading and perhaps the line
2745 with deadline/scheduling if present.
2747 This variable will have no effect if `org-log-into-drawer' is
2750 :group
'org-progress
2753 (defcustom org-log-states-order-reversed t
2754 "Non-nil means the latest state note will be directly after heading.
2755 When nil, the state change notes will be ordered according to time.
2757 This option can also be set with on a per-file-basis with
2759 #+STARTUP: logstatesreversed
2760 #+STARTUP: nologstatesreversed"
2762 :group
'org-progress
2765 (defcustom org-todo-repeat-to-state nil
2766 "The TODO state to which a repeater should return the repeating task.
2767 By default this is the first task in a TODO sequence, or the previous state
2768 in a TODO_TYP set. But you can specify another task here.
2769 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2772 :type
'(choice (const :tag
"Head of sequence" nil
)
2773 (string :tag
"Specific state")))
2775 (defcustom org-log-repeat
'time
2776 "Non-nil means record moving through the DONE state when triggering repeat.
2777 An auto-repeating task is immediately switched back to TODO when
2778 marked DONE. If you are not logging state changes (by adding \"@\"
2779 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2780 record a closing note, there will be no record of the task moving
2781 through DONE. This variable forces taking a note anyway.
2783 nil Don't force a record
2784 time Record a time stamp
2785 note Prompt for a note and add it with template `org-log-note-headings'
2787 This option can also be set with on a per-file-basis with
2789 #+STARTUP: nologrepeat
2790 #+STARTUP: logrepeat
2791 #+STARTUP: lognoterepeat
2793 You can have local logging settings for a subtree by setting the LOGGING
2794 property to one or more of these keywords."
2796 :group
'org-progress
2798 (const :tag
"Don't force a record" nil
)
2799 (const :tag
"Force recording the DONE state" time
)
2800 (const :tag
"Force recording a note with the DONE state" note
)))
2803 (defgroup org-priorities nil
2804 "Priorities in Org-mode."
2805 :tag
"Org Priorities"
2808 (defcustom org-enable-priority-commands t
2809 "Non-nil means priority commands are active.
2810 When nil, these commands will be disabled, so that you never accidentally
2812 :group
'org-priorities
2815 (defcustom org-highest-priority ?A
2816 "The highest priority of TODO items. A character like ?A, ?B etc.
2817 Must have a smaller ASCII number than `org-lowest-priority'."
2818 :group
'org-priorities
2821 (defcustom org-lowest-priority ?C
2822 "The lowest priority of TODO items. A character like ?A, ?B etc.
2823 Must have a larger ASCII number than `org-highest-priority'."
2824 :group
'org-priorities
2827 (defcustom org-default-priority ?B
2828 "The default priority of TODO items.
2829 This is the priority an item gets if no explicit priority is given.
2830 When starting to cycle on an empty priority the first step in the cycle
2831 depends on `org-priority-start-cycle-with-default'. The resulting first
2832 step priority must not exceed the range from `org-highest-priority' to
2833 `org-lowest-priority' which means that `org-default-priority' has to be
2834 in this range exclusive or inclusive the range boundaries. Else the
2835 first step refuses to set the default and the second will fall back
2836 to (depending on the command used) the highest or lowest priority."
2837 :group
'org-priorities
2840 (defcustom org-priority-start-cycle-with-default t
2841 "Non-nil means start with default priority when starting to cycle.
2842 When this is nil, the first step in the cycle will be (depending on the
2843 command used) one higher or lower than the default priority.
2844 See also `org-default-priority'."
2845 :group
'org-priorities
2848 (defcustom org-get-priority-function nil
2849 "Function to extract the priority from a string.
2850 The string is normally the headline. If this is nil Org computes the
2851 priority from the priority cookie like [#A] in the headline. It returns
2852 an integer, increasing by 1000 for each priority level.
2853 The user can set a different function here, which should take a string
2854 as an argument and return the numeric priority."
2855 :group
'org-priorities
2861 (defgroup org-time nil
2862 "Options concerning time stamps and deadlines in Org-mode."
2866 (defcustom org-insert-labeled-timestamps-at-point nil
2867 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2868 When nil, these labeled time stamps are forces into the second line of an
2869 entry, just after the headline. When scheduling from the global TODO list,
2870 the time stamp will always be forced into the second line."
2874 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2875 "Formats for `format-time-string' which are used for time stamps.
2876 It is not recommended to change this constant.")
2878 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2879 "Number of minutes to round time stamps to.
2880 These are two values, the first applies when first creating a time stamp.
2881 The second applies when changing it with the commands `S-up' and `S-down'.
2882 When changing the time stamp, this means that it will change in steps
2883 of N minutes, as given by the second value.
2885 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2886 numbers should be factors of 60, so for example 5, 10, 15.
2888 When this is larger than 1, you can still force an exact time stamp by using
2889 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2890 and by using a prefix arg to `S-up/down' to specify the exact number
2891 of minutes to shift."
2893 :get
#'(lambda (var) ; Make sure both elements are there
2894 (if (integerp (default-value var
))
2895 (list (default-value var
) 5)
2896 (default-value var
)))
2898 (integer :tag
"when inserting times")
2899 (integer :tag
"when modifying times")))
2901 ;; Normalize old customizations of this variable.
2902 (when (integerp org-time-stamp-rounding-minutes
)
2903 (setq org-time-stamp-rounding-minutes
2904 (list org-time-stamp-rounding-minutes
2905 org-time-stamp-rounding-minutes
)))
2907 (defcustom org-display-custom-times nil
2908 "Non-nil means overlay custom formats over all time stamps.
2909 The formats are defined through the variable `org-time-stamp-custom-formats'.
2910 To turn this on on a per-file basis, insert anywhere in the file:
2911 #+STARTUP: customtime"
2915 (make-variable-buffer-local 'org-display-custom-times
)
2917 (defcustom org-time-stamp-custom-formats
2918 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2919 "Custom formats for time stamps. See `format-time-string' for the syntax.
2920 These are overlaid over the default ISO format if the variable
2921 `org-display-custom-times' is set. Time like %H:%M should be at the
2922 end of the second format. The custom formats are also honored by export
2923 commands, if custom time display is turned on at the time of export."
2927 (defun org-time-stamp-format (&optional long inactive
)
2928 "Get the right format for a time string."
2929 (let ((f (if long
(cdr org-time-stamp-formats
)
2930 (car org-time-stamp-formats
))))
2932 (concat "[" (substring f
1 -
1) "]")
2935 (defcustom org-time-clocksum-format
2936 '(:days
"%dd " :hours
"%d" :require-hours t
:minutes
":%02d" :require-minutes t
)
2937 "The format string used when creating CLOCKSUM lines.
2938 This is also used when Org mode generates a time duration.
2940 The value can be a single format string containing two
2941 %-sequences, which will be filled with the number of hours and
2942 minutes in that order.
2944 Alternatively, the value can be a plist associating any of the
2945 keys :years, :months, :weeks, :days, :hours or :minutes with
2946 format strings. The time duration is formatted using only the
2947 time components that are needed and concatenating the results.
2948 If a time unit in absent, it falls back to the next smallest
2951 The keys :require-years, :require-months, :require-days,
2952 :require-weeks, :require-hours, :require-minutes are also
2953 meaningful. A non-nil value for these keys indicates that the
2954 corresponding time component should always be included, even if
2960 (:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
2963 means durations longer than a day will be expressed in days,
2964 hours and minutes, and durations less than a day will always be
2965 expressed in hours and minutes (even for durations less than an
2970 (:days \"%dd\" :minutes \"%dm\")
2972 means durations longer than a day will be expressed in days and
2973 minutes, and durations less than a day will be expressed entirely
2974 in minutes (even for durations longer than an hour)."
2978 :package-version
'(Org .
"8.0")
2979 :type
'(choice (string :tag
"Format string")
2981 (group :inline t
(const :tag
"Years" :years
)
2982 (string :tag
"Format string"))
2984 (const :tag
"Always show years" :require-years
)
2986 (group :inline t
(const :tag
"Months" :months
)
2987 (string :tag
"Format string"))
2989 (const :tag
"Always show months" :require-months
)
2991 (group :inline t
(const :tag
"Weeks" :weeks
)
2992 (string :tag
"Format string"))
2994 (const :tag
"Always show weeks" :require-weeks
)
2996 (group :inline t
(const :tag
"Days" :days
)
2997 (string :tag
"Format string"))
2999 (const :tag
"Always show days" :require-days
)
3001 (group :inline t
(const :tag
"Hours" :hours
)
3002 (string :tag
"Format string"))
3004 (const :tag
"Always show hours" :require-hours
)
3006 (group :inline t
(const :tag
"Minutes" :minutes
)
3007 (string :tag
"Format string"))
3009 (const :tag
"Always show minutes" :require-minutes
)
3012 (defcustom org-time-clocksum-use-fractional nil
3013 "When non-nil, \\[org-clock-display] uses fractional times.
3014 See `org-time-clocksum-format' for more on time clock formats."
3020 (defcustom org-time-clocksum-use-effort-durations nil
3021 "When non-nil, \\[org-clock-display] uses effort durations.
3022 E.g. by default, one day is considered to be a 8 hours effort,
3023 so a task that has been clocked for 16 hours will be displayed
3024 as during 2 days in the clock display or in the clocktable.
3026 See `org-effort-durations' on how to set effort durations
3027 and `org-time-clocksum-format' for more on time clock formats."
3031 :package-version
'(Org .
"8.0")
3034 (defcustom org-time-clocksum-fractional-format
"%.2f"
3035 "The format string used when creating CLOCKSUM lines,
3036 or when Org mode generates a time duration, if
3037 `org-time-clocksum-use-fractional' is enabled.
3039 The value can be a single format string containing one
3040 %-sequence, which will be filled with the number of hours as
3043 Alternatively, the value can be a plist associating any of the
3044 keys :years, :months, :weeks, :days, :hours or :minutes with
3045 a format string. The time duration is formatted using the
3046 largest time unit which gives a non-zero integer part. If all
3047 specified formats have zero integer part, the smallest time unit
3050 :type
'(choice (string :tag
"Format string")
3051 (set (group :inline t
(const :tag
"Years" :years
)
3052 (string :tag
"Format string"))
3053 (group :inline t
(const :tag
"Months" :months
)
3054 (string :tag
"Format string"))
3055 (group :inline t
(const :tag
"Weeks" :weeks
)
3056 (string :tag
"Format string"))
3057 (group :inline t
(const :tag
"Days" :days
)
3058 (string :tag
"Format string"))
3059 (group :inline t
(const :tag
"Hours" :hours
)
3060 (string :tag
"Format string"))
3061 (group :inline t
(const :tag
"Minutes" :minutes
)
3062 (string :tag
"Format string")))))
3064 (defcustom org-deadline-warning-days
14
3065 "Number of days before expiration during which a deadline becomes active.
3066 This variable governs the display in sparse trees and in the agenda.
3067 When 0 or negative, it means use this number (the absolute value of it)
3068 even if a deadline has a different individual lead time specified.
3070 Custom commands can set this variable in the options section."
3072 :group
'org-agenda-daily
/weekly
3075 (defcustom org-scheduled-delay-days
0
3076 "Number of days before a scheduled item becomes active.
3077 This variable governs the display in sparse trees and in the agenda.
3078 The default value (i.e. 0) means: don't delay scheduled item.
3079 When negative, it means use this number (the absolute value of it)
3080 even if a scheduled item has a different individual delay time
3083 Custom commands can set this variable in the options section."
3085 :group
'org-agenda-daily
/weekly
3087 :package-version
'(Org .
"8.0")
3090 (defcustom org-read-date-prefer-future t
3091 "Non-nil means assume future for incomplete date input from user.
3092 This affects the following situations:
3093 1. The user gives a month but not a year.
3094 For example, if it is April and you enter \"feb 2\", this will be read
3095 as Feb 2, *next* year. \"May 5\", however, will be this year.
3096 2. The user gives a day, but no month.
3097 For example, if today is the 15th, and you enter \"3\", Org-mode will
3098 read this as the third of *next* month. However, if you enter \"17\",
3099 it will be considered as *this* month.
3101 If you set this variable to the symbol `time', then also the following
3104 3. If the user gives a time.
3105 If the time is before now, it will be interpreted as tomorrow.
3107 Currently none of this works for ISO week specifications.
3109 When this option is nil, the current day, month and year will always be
3112 See also `org-agenda-jump-prefer-future'."
3115 (const :tag
"Never" nil
)
3116 (const :tag
"Check month and day" t
)
3117 (const :tag
"Check month, day, and time" time
)))
3119 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
3120 "Should the agenda jump command prefer the future for incomplete dates?
3121 The default is to do the same as configured in `org-read-date-prefer-future'.
3122 But you can also set a deviating value here.
3123 This may t or nil, or the symbol `org-read-date-prefer-future'."
3128 (const :tag
"Use org-read-date-prefer-future"
3129 org-read-date-prefer-future
)
3130 (const :tag
"Never" nil
)
3131 (const :tag
"Always" t
)))
3133 (defcustom org-read-date-force-compatible-dates t
3134 "Should date/time prompt force dates that are guaranteed to work in Emacs?
3136 Depending on the system Emacs is running on, certain dates cannot
3137 be represented with the type used internally to represent time.
3138 Dates between 1970-1-1 and 2038-1-1 can always be represented
3139 correctly. Some systems allow for earlier dates, some for later,
3140 some for both. One way to find out it to insert any date into an
3141 Org buffer, putting the cursor on the year and hitting S-up and
3142 S-down to test the range.
3144 When this variable is set to t, the date/time prompt will not let
3145 you specify dates outside the 1970-2037 range, so it is certain that
3146 these dates will work in whatever version of Emacs you are
3147 running, and also that you can move a file from one Emacs implementation
3148 to another. WHenever Org is forcing the year for you, it will display
3151 When this variable is nil, Org will check if the date is
3152 representable in the specific Emacs implementation you are using.
3153 If not, it will force a year, usually the current year, and beep
3154 to remind you. Currently this setting is not recommended because
3155 the likelihood that you will open your Org files in an Emacs that
3156 has limited date range is not negligible.
3158 A workaround for this problem is to use diary sexp dates for time
3159 stamps outside of this range."
3164 (defcustom org-read-date-display-live t
3165 "Non-nil means display current interpretation of date prompt live.
3166 This display will be in an overlay, in the minibuffer."
3170 (defcustom org-read-date-popup-calendar t
3171 "Non-nil means pop up a calendar when prompting for a date.
3172 In the calendar, the date can be selected with mouse-1. However, the
3173 minibuffer will also be active, and you can simply enter the date as well.
3174 When nil, only the minibuffer will be available."
3177 (org-defvaralias 'org-popup-calendar-for-date-prompt
3178 'org-read-date-popup-calendar
)
3180 (make-obsolete-variable
3181 'org-read-date-minibuffer-setup-hook
3182 "Set `org-read-date-minibuffer-local-map' instead." "24.4")
3183 (defcustom org-read-date-minibuffer-setup-hook nil
3184 "Hook to be used to set up keys for the date/time interface.
3185 Add key definitions to `minibuffer-local-map', which will be a
3188 WARNING: This option is obsolete, you should use
3189 `org-read-date-minibuffer-local-map' to set up keys."
3193 (defcustom org-extend-today-until
0
3194 "The hour when your day really ends. Must be an integer.
3195 This has influence for the following applications:
3196 - When switching the agenda to \"today\". It it is still earlier than
3197 the time given here, the day recognized as TODAY is actually yesterday.
3198 - When a date is read from the user and it is still before the time given
3199 here, the current date and time will be assumed to be yesterday, 23:59.
3200 Also, timestamps inserted in capture templates follow this rule.
3202 IMPORTANT: This is a feature whose implementation is and likely will
3203 remain incomplete. Really, it is only here because past midnight seems to
3204 be the favorite working time of John Wiegley :-)"
3208 (defcustom org-use-effective-time nil
3209 "If non-nil, consider `org-extend-today-until' when creating timestamps.
3210 For example, if `org-extend-today-until' is 8, and it's 4am, then the
3211 \"effective time\" of any timestamps between midnight and 8am will be
3212 23:59 of the previous day."
3217 (defcustom org-use-last-clock-out-time-as-effective-time nil
3218 "When non-nil, use the last clock out time for `org-todo'.
3219 Note that this option has precedence over the combined use of
3220 `org-use-effective-time' and `org-extend-today-until'."
3223 :package-version
'(Org .
"8.0")
3226 (defcustom org-edit-timestamp-down-means-later nil
3227 "Non-nil means S-down will increase the time in a time stamp.
3228 When nil, S-up will increase."
3232 (defcustom org-calendar-follow-timestamp-change t
3233 "Non-nil means make the calendar window follow timestamp changes.
3234 When a timestamp is modified and the calendar window is visible, it will be
3235 moved to the new date."
3239 (defgroup org-tags nil
3240 "Options concerning tags in Org-mode."
3244 (defcustom org-tag-alist nil
3245 "List of tags allowed in Org-mode files.
3246 When this list is nil, Org-mode will base TAG input on what is already in the
3248 The value of this variable is an alist, the car of each entry must be a
3249 keyword as a string, the cdr may be a character that is used to select
3250 that tag through the fast-tag-selection interface.
3251 See the manual for details."
3255 (cons (string :tag
"Tag name")
3256 (character :tag
"Access char"))
3257 (list :tag
"Start radio group"
3259 (option (string :tag
"Group description")))
3260 (list :tag
"Group tags delimiter"
3262 (list :tag
"End radio group"
3264 (option (string :tag
"Group description")))
3265 (const :tag
"New line" (:newline
)))))
3267 (defcustom org-tag-persistent-alist nil
3268 "List of tags that will always appear in all Org-mode files.
3269 This is in addition to any in buffer settings or customizations
3271 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
3272 The value of this variable is an alist, the car of each entry must be a
3273 keyword as a string, the cdr may be a character that is used to select
3274 that tag through the fast-tag-selection interface.
3275 See the manual for details.
3276 To disable these tags on a per-file basis, insert anywhere in the file:
3281 (cons (string :tag
"Tag name")
3282 (character :tag
"Access char"))
3283 (const :tag
"Start radio group" (:startgroup
))
3284 (const :tag
"Group tags delimiter" (:grouptags
))
3285 (const :tag
"End radio group" (:endgroup
))
3286 (const :tag
"New line" (:newline
)))))
3288 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
3289 "If non-nil, always offer completion for all tags of all agenda files.
3290 Instead of customizing this variable directly, you might want to
3291 set it locally for capture buffers, because there no list of
3292 tags in that file can be created dynamically (there are none).
3294 (add-hook \\='org-capture-mode-hook
3296 (set (make-local-variable
3297 \\='org-complete-tags-always-offer-all-agenda-tags)
3303 (defvar org-file-tags nil
3304 "List of tags that can be inherited by all entries in the file.
3305 The tags will be inherited if the variable `org-use-tag-inheritance'
3306 says they should be.
3307 This variable is populated from #+FILETAGS lines.")
3309 (defcustom org-use-fast-tag-selection
'auto
3310 "Non-nil means use fast tag selection scheme.
3311 This is a special interface to select and deselect tags with single keys.
3312 When nil, fast selection is never used.
3313 When the symbol `auto', fast selection is used if and only if selection
3314 characters for tags have been configured, either through the variable
3315 `org-tag-alist' or through a #+TAGS line in the buffer.
3316 When t, fast selection is always used and selection keys are assigned
3317 automatically if necessary."
3320 (const :tag
"Always" t
)
3321 (const :tag
"Never" nil
)
3322 (const :tag
"When selection characters are configured" auto
)))
3324 (defcustom org-fast-tag-selection-single-key nil
3325 "Non-nil means fast tag selection exits after first change.
3326 When nil, you have to press RET to exit it.
3327 During fast tag selection, you can toggle this flag with `C-c'.
3328 This variable can also have the value `expert'. In this case, the window
3329 displaying the tags menu is not even shown, until you press C-c again."
3332 (const :tag
"No" nil
)
3333 (const :tag
"Yes" t
)
3334 (const :tag
"Expert" expert
)))
3336 (defvar org-fast-tag-selection-include-todo nil
3337 "Non-nil means fast tags selection interface will also offer TODO states.
3338 This is an undocumented feature, you should not rely on it.")
3340 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
3341 "The column to which tags should be indented in a headline.
3342 If this number is positive, it specifies the column. If it is negative,
3343 it means that the tags should be flushright to that column. For example,
3344 -80 works well for a normal 80 character screen.
3345 When 0, place tags directly after headline text, with only one space in
3350 (defcustom org-auto-align-tags t
3351 "Non-nil keeps tags aligned when modifying headlines.
3352 Some operations (i.e. demoting) change the length of a headline and
3353 therefore shift the tags around. With this option turned on, after
3354 each such operation the tags are again aligned to `org-tags-column'."
3358 (defcustom org-use-tag-inheritance t
3359 "Non-nil means tags in levels apply also for sublevels.
3360 When nil, only the tags directly given in a specific line apply there.
3361 This may also be a list of tags that should be inherited, or a regexp that
3362 matches tags that should be inherited. Additional control is possible
3363 with the variable `org-tags-exclude-from-inheritance' which gives an
3364 explicit list of tags to be excluded from inheritance, even if the value of
3365 `org-use-tag-inheritance' would select it for inheritance.
3367 If this option is t, a match early-on in a tree can lead to a large
3368 number of matches in the subtree when constructing the agenda or creating
3369 a sparse tree. If you only want to see the first match in a tree during
3370 a search, check out the variable `org-tags-match-list-sublevels'."
3373 (const :tag
"Not" nil
)
3374 (const :tag
"Always" t
)
3375 (repeat :tag
"Specific tags" (string :tag
"Tag"))
3376 (regexp :tag
"Tags matched by regexp")))
3378 (defcustom org-tags-exclude-from-inheritance nil
3379 "List of tags that should never be inherited.
3380 This is a way to exclude a few tags from inheritance. For way to do
3381 the opposite, to actively allow inheritance for selected tags,
3382 see the variable `org-use-tag-inheritance'."
3384 :type
'(repeat (string :tag
"Tag")))
3386 (defun org-tag-inherit-p (tag)
3387 "Check if TAG is one that should be inherited."
3389 ((member tag org-tags-exclude-from-inheritance
) nil
)
3390 ((eq org-use-tag-inheritance t
) t
)
3391 ((not org-use-tag-inheritance
) nil
)
3392 ((stringp org-use-tag-inheritance
)
3393 (string-match org-use-tag-inheritance tag
))
3394 ((listp org-use-tag-inheritance
)
3395 (member tag org-use-tag-inheritance
))
3396 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3398 (defcustom org-tags-match-list-sublevels t
3399 "Non-nil means list also sublevels of headlines matching a search.
3400 This variable applies to tags/property searches, and also to stuck
3401 projects because this search is based on a tags match as well.
3403 When set to the symbol `indented', sublevels are indented with
3406 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3407 the sublevels of a headline matching a tag search often also match
3408 the same search. Listing all of them can create very long lists.
3409 Setting this variable to nil causes subtrees of a match to be skipped.
3411 This variable is semi-obsolete and probably should always be true. It
3412 is better to limit inheritance to certain tags using the variables
3413 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3416 (const :tag
"No, don't list them" nil
)
3417 (const :tag
"Yes, do list them" t
)
3418 (const :tag
"List them, indented with leading dots" indented
)))
3420 (defcustom org-tags-sort-function nil
3421 "When set, tags are sorted using this function as a comparator."
3424 (const :tag
"No sorting" nil
)
3425 (const :tag
"Alphabetical" string
<)
3426 (const :tag
"Reverse alphabetical" string
>)
3427 (function :tag
"Custom function" nil
)))
3429 (defvar org-tags-history nil
3430 "History of minibuffer reads for tags.")
3431 (defvar org-last-tags-completion-table nil
3432 "The last used completion table for tags.")
3433 (defvar org-after-tags-change-hook nil
3434 "Hook that is run after the tags in a line have changed.")
3436 (defgroup org-properties nil
3437 "Options concerning properties in Org-mode."
3438 :tag
"Org Properties"
3441 (defcustom org-property-format
"%-10s %s"
3442 "How property key/value pairs should be formatted by `indent-line'.
3443 When `indent-line' hits a property definition, it will format the line
3444 according to this format, mainly to make sure that the values are
3445 lined-up with respect to each other."
3446 :group
'org-properties
3449 (defcustom org-properties-postprocess-alist nil
3450 "Alist of properties and functions to adjust inserted values.
3451 Elements of this alist must be of the form
3453 ([string] [function])
3455 where [string] must be a property name and [function] must be a
3456 lambda expression: this lambda expression must take one argument,
3457 the value to adjust, and return the new value as a string.
3459 For example, this element will allow the property \"Remaining\"
3460 to be updated wrt the relation between the \"Effort\" property
3461 and the clock summary:
3463 ((\"Remaining\" (lambda(value)
3464 (let ((clocksum (org-clock-sum-current-item))
3465 (effort (org-duration-string-to-minutes
3466 (org-entry-get (point) \"Effort\"))))
3467 (org-minutes-to-clocksum-string (- effort clocksum))))))"
3468 :group
'org-properties
3470 :type
'(alist :key-type
(string :tag
"Property")
3471 :value-type
(function :tag
"Function")))
3473 (defcustom org-use-property-inheritance nil
3474 "Non-nil means properties apply also for sublevels.
3476 This setting is chiefly used during property searches. Turning it on can
3477 cause significant overhead when doing a search, which is why it is not
3480 When nil, only the properties directly given in the current entry count.
3481 When t, every property is inherited. The value may also be a list of
3482 properties that should have inheritance, or a regular expression matching
3483 properties that should be inherited.
3485 However, note that some special properties use inheritance under special
3486 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3487 and the properties ending in \"_ALL\" when they are used as descriptor
3488 for valid values of a property.
3490 Note for programmers:
3491 When querying an entry with `org-entry-get', you can control if inheritance
3492 should be used. By default, `org-entry-get' looks only at the local
3493 properties. You can request inheritance by setting the inherit argument
3494 to t (to force inheritance) or to `selective' (to respect the setting
3496 :group
'org-properties
3498 (const :tag
"Not" nil
)
3499 (const :tag
"Always" t
)
3500 (repeat :tag
"Specific properties" (string :tag
"Property"))
3501 (regexp :tag
"Properties matched by regexp")))
3503 (defun org-property-inherit-p (property)
3504 "Check if PROPERTY is one that should be inherited."
3506 ((eq org-use-property-inheritance t
) t
)
3507 ((not org-use-property-inheritance
) nil
)
3508 ((stringp org-use-property-inheritance
)
3509 (string-match org-use-property-inheritance property
))
3510 ((listp org-use-property-inheritance
)
3511 (member property org-use-property-inheritance
))
3512 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3514 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3515 "The default column format, if no other format has been defined.
3516 This variable can be set on the per-file basis by inserting a line
3518 #+COLUMNS: %25ITEM ....."
3519 :group
'org-properties
3522 (defcustom org-columns-ellipses
".."
3523 "The ellipses to be used when a field in column view is truncated.
3524 When this is the empty string, as many characters as possible are shown,
3525 but then there will be no visual indication that the field has been truncated.
3526 When this is a string of length N, the last N characters of a truncated
3527 field are replaced by this string. If the column is narrower than the
3528 ellipses string, only part of the ellipses string will be shown."
3529 :group
'org-properties
3532 (defcustom org-columns-modify-value-for-display-function nil
3533 "Function that modifies values for display in column view.
3534 For example, it can be used to cut out a certain part from a time stamp.
3535 The function must take 2 arguments:
3537 column-title The title of the column (*not* the property name)
3538 value The value that should be modified.
3540 The function should return the value that should be displayed,
3541 or nil if the normal value should be used."
3542 :group
'org-properties
3543 :type
'(choice (const nil
) (function)))
3545 (defcustom org-effort-property
"Effort"
3546 "The property that is being used to keep track of effort estimates.
3547 Effort estimates given in this property need to have the format H:MM."
3548 :group
'org-properties
3549 :group
'org-progress
3550 :type
'(string :tag
"Property"))
3552 (defconst org-global-properties-fixed
3553 '(("VISIBILITY_ALL" .
"folded children content all")
3554 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3555 "List of property/value pairs that can be inherited by any entry.
3557 These are fixed values, for the preset properties. The user variable
3558 that can be used to add to this list is `org-global-properties'.
3560 The entries in this list are cons cells where the car is a property
3561 name and cdr is a string with the value. If the value represents
3562 multiple items like an \"_ALL\" property, separate the items by
3565 (defcustom org-global-properties nil
3566 "List of property/value pairs that can be inherited by any entry.
3568 This list will be combined with the constant `org-global-properties-fixed'.
3570 The entries in this list are cons cells where the car is a property
3571 name and cdr is a string with the value.
3573 You can set buffer-local values for the same purpose in the variable
3574 `org-file-properties' this by adding lines like
3576 #+PROPERTY: NAME VALUE"
3577 :group
'org-properties
3579 (cons (string :tag
"Property")
3580 (string :tag
"Value"))))
3582 (defvar org-file-properties nil
3583 "List of property/value pairs that can be inherited by any entry.
3584 Valid for the current buffer.
3585 This variable is populated from #+PROPERTY lines.")
3586 (make-variable-buffer-local 'org-file-properties
)
3588 (defgroup org-agenda nil
3589 "Options concerning agenda views in Org-mode."
3593 (defvar org-category nil
3594 "Variable used by org files to set a category for agenda display.
3595 Such files should use a file variable to set it, for example
3597 # -*- mode: org; org-category: \"ELisp\"
3599 or contain a special line
3603 If the file does not specify a category, then file's base name
3605 (make-variable-buffer-local 'org-category
)
3606 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3608 (defcustom org-agenda-files nil
3609 "The files to be used for agenda display.
3610 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3611 \\[org-remove-file]. You can also use customize to edit the list.
3613 If an entry is a directory, all files in that directory that are matched by
3614 `org-agenda-file-regexp' will be part of the file list.
3616 If the value of the variable is not a list but a single file name, then
3617 the list of agenda files is actually stored and maintained in that file, one
3618 agenda file per line. In this file paths can be given relative to
3619 `org-directory'. Tilde expansion and environment variable substitution
3623 (repeat :tag
"List of files and directories" file
)
3624 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3626 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3627 "Regular expression to match files for `org-agenda-files'.
3628 If any element in the list in that variable contains a directory instead
3629 of a normal file, all files in that directory that are matched by this
3630 regular expression will be included."
3634 (defcustom org-agenda-text-search-extra-files nil
3635 "List of extra files to be searched by text search commands.
3636 These files will be searched in addition to the agenda files by the
3637 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3638 Note that these files will only be searched for text search commands,
3639 not for the other agenda views like todo lists, tag searches or the weekly
3640 agenda. This variable is intended to list notes and possibly archive files
3641 that should also be searched by these two commands.
3642 In fact, if the first element in the list is the symbol `agenda-archives',
3643 then all archive files of all agenda files will be added to the search
3646 :type
'(set :greedy t
3647 (const :tag
"Agenda Archives" agenda-archives
)
3648 (repeat :inline t
(file))))
3650 (org-defvaralias 'org-agenda-multi-occur-extra-files
3651 'org-agenda-text-search-extra-files
)
3653 (defcustom org-agenda-skip-unavailable-files nil
3654 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3655 A nil value means to remove them, after a query, from the list."
3659 (defcustom org-calendar-to-agenda-key
[?c
]
3660 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3661 The command `org-calendar-goto-agenda' will be bound to this key. The
3662 default is the character `c' because then `c' can be used to switch back and
3663 forth between agenda and calendar."
3667 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3668 "The key to be installed in `calendar-mode-map' for adding diary entries.
3669 This option is irrelevant until `org-agenda-diary-file' has been configured
3670 to point to an Org-mode file. When that is the case, the command
3671 `org-agenda-diary-entry' will be bound to the key given here, by default
3672 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3673 if you want to continue doing this, you need to change this to a different
3678 (defcustom org-agenda-diary-file
'diary-file
3679 "File to which to add new entries with the `i' key in agenda and calendar.
3680 When this is the symbol `diary-file', the functionality in the Emacs
3681 calendar will be used to add entries to the `diary-file'. But when this
3682 points to a file, `org-agenda-diary-entry' will be used instead."
3685 (const :tag
"The standard Emacs diary file" diary-file
)
3686 (file :tag
"Special Org file diary entries")))
3688 (eval-after-load "calendar"
3690 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3691 'org-calendar-goto-agenda
)
3692 (add-hook 'calendar-mode-hook
3694 (unless (eq org-agenda-diary-file
'diary-file
)
3695 (define-key calendar-mode-map
3696 org-calendar-insert-diary-entry-key
3697 'org-agenda-diary-entry
))))))
3699 (defgroup org-latex nil
3700 "Options for embedding LaTeX code into Org-mode."
3704 (defcustom org-format-latex-options
3705 '(:foreground default
:background default
:scale
1.0
3706 :html-foreground
"Black" :html-background
"Transparent"
3707 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3708 "Options for creating images from LaTeX fragments.
3709 This is a property list with the following properties:
3710 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3711 `default' means use the foreground of the default face.
3712 `auto' means use the foreground from the text face.
3713 :background the background color, or \"Transparent\".
3714 `default' means use the background of the default face.
3715 `auto' means use the background from the text face.
3716 :scale a scaling factor for the size of the images, to get more pixels
3717 :html-foreground, :html-background, :html-scale
3718 the same numbers for HTML export.
3719 :matchers a list indicating which matchers should be used to
3720 find LaTeX fragments. Valid members of this list are:
3721 \"begin\" find environments
3722 \"$1\" find single characters surrounded by $.$
3723 \"$\" find math expressions surrounded by $...$
3724 \"$$\" find math expressions surrounded by $$....$$
3725 \"\\(\" find math expressions surrounded by \\(...\\)
3726 \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
3730 (defcustom org-format-latex-signal-error t
3731 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3732 When nil, just push out a message."
3737 (defcustom org-latex-to-mathml-jar-file nil
3738 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3739 Use this to specify additional executable file say a jar file.
3741 When using MathToWeb as the converter, specify the full-path to
3742 your mathtoweb.jar file."
3746 (const :tag
"None" nil
)
3747 (file :tag
"JAR file" :must-match t
)))
3749 (defcustom org-latex-to-mathml-convert-command nil
3750 "Command to convert LaTeX fragments to MathML.
3751 Replace format-specifiers in the command as noted below and use
3752 `shell-command' to convert LaTeX to MathML.
3753 %j: Executable file in fully expanded form as specified by
3754 `org-latex-to-mathml-jar-file'.
3755 %I: Input LaTeX file in fully expanded form
3756 %o: Output MathML file
3757 This command is used by `org-create-math-formula'.
3759 When using MathToWeb as the converter, set this to
3760 \"java -jar %j -unicode -force -df %o %I\"."
3764 (const :tag
"None" nil
)
3765 (string :tag
"\nShell command")))
3767 (defcustom org-latex-create-formula-image-program
'dvipng
3768 "Program to convert LaTeX fragments with.
3770 dvipng Process the LaTeX fragments to dvi file, then convert
3771 dvi files to png files using dvipng.
3772 This will also include processing of non-math environments.
3773 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3774 to convert pdf files to png files"
3778 (const :tag
"dvipng" dvipng
)
3779 (const :tag
"imagemagick" imagemagick
)))
3781 (defcustom org-latex-preview-ltxpng-directory
"ltxpng/"
3782 "Path to store latex preview images.
3783 A relative path here creates many directories relative to the
3784 processed org files paths. An absolute path puts all preview
3785 images at the same place."
3790 (defun org-format-latex-mathml-available-p ()
3791 "Return t if `org-latex-to-mathml-convert-command' is usable."
3793 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3794 org-latex-to-mathml-convert-command
)
3795 (let ((executable (car (split-string
3796 org-latex-to-mathml-convert-command
))))
3797 (when (executable-find executable
)
3799 "%j" org-latex-to-mathml-convert-command
)
3800 (file-readable-p org-latex-to-mathml-jar-file
)
3803 (defcustom org-format-latex-header
"\\documentclass{article}
3804 \\usepackage[usenames]{color}
3807 \\pagestyle{empty} % do not remove
3808 % The settings below are copied from fullpage.sty
3809 \\setlength{\\textwidth}{\\paperwidth}
3810 \\addtolength{\\textwidth}{-3cm}
3811 \\setlength{\\oddsidemargin}{1.5cm}
3812 \\addtolength{\\oddsidemargin}{-2.54cm}
3813 \\setlength{\\evensidemargin}{\\oddsidemargin}
3814 \\setlength{\\textheight}{\\paperheight}
3815 \\addtolength{\\textheight}{-\\headheight}
3816 \\addtolength{\\textheight}{-\\headsep}
3817 \\addtolength{\\textheight}{-\\footskip}
3818 \\addtolength{\\textheight}{-3cm}
3819 \\setlength{\\topmargin}{1.5cm}
3820 \\addtolength{\\topmargin}{-2.54cm}"
3821 "The document header used for processing LaTeX fragments.
3822 It is imperative that this header make sure that no page number
3823 appears on the page. The package defined in the variables
3824 `org-latex-default-packages-alist' and `org-latex-packages-alist'
3825 will either replace the placeholder \"[PACKAGES]\" in this
3826 header, or they will be appended."
3830 (defun org-set-packages-alist (var val
)
3831 "Set the packages alist and make sure it has 3 elements per entry."
3832 (set var
(mapcar (lambda (x)
3833 (if (and (consp x
) (= (length x
) 2))
3834 (list (car x
) (nth 1 x
) t
)
3838 (defun org-get-packages-alist (var)
3839 "Get the packages alist and make sure it has 3 elements per entry."
3841 (if (and (consp x
) (= (length x
) 2))
3842 (list (car x
) (nth 1 x
) t
)
3844 (default-value var
)))
3846 (defcustom org-latex-default-packages-alist
3847 '(("AUTO" "inputenc" t
)
3851 ("" "longtable" nil
)
3855 ("normalem" "ulem" t
)
3863 "Alist of default packages to be inserted in the header.
3865 Change this only if one of the packages here causes an
3866 incompatibility with another package you are using.
3868 The packages in this list are needed by one part or another of
3869 Org mode to function properly:
3871 - inputenc, fontenc: for basic font and character selection
3872 - fixltx2e: Important patches of LaTeX itself
3873 - graphicx: for including images
3874 - longtable: For multipage tables
3875 - float, wrapfig: for figure placement
3876 - rotating: for sideways figures and tables
3877 - ulem: for underline and strike-through
3878 - amsmath: for subscript and superscript and math environments
3879 - textcomp, marvosymb, wasysym, amssymb: for various symbols used
3880 for interpreting the entities in `org-entities'. You can skip
3881 some of these packages if you don't use any of their symbols.
3882 - hyperref: for cross references
3884 Therefore you should not modify this variable unless you know
3885 what you are doing. The one reason to change it anyway is that
3886 you might be loading some other package that conflicts with one
3887 of the default packages. Each element is either a cell or
3890 A cell is of the format:
3892 ( \"options\" \"package\" SNIPPET-FLAG).
3894 If SNIPPET-FLAG is non-nil, the package also needs to be included
3895 when compiling LaTeX snippets into images for inclusion into
3898 A string will be inserted as-is in the header of the document."
3900 :group
'org-export-latex
3901 :set
'org-set-packages-alist
3902 :get
'org-get-packages-alist
3906 (list :tag
"options/package pair"
3907 (string :tag
"options")
3908 (string :tag
"package")
3909 (boolean :tag
"Snippet"))
3910 (string :tag
"A line of LaTeX"))))
3912 (defcustom org-latex-packages-alist nil
3913 "Alist of packages to be inserted in every LaTeX header.
3915 These will be inserted after `org-latex-default-packages-alist'.
3916 Each element is either a cell or a string.
3918 A cell is of the format:
3920 (\"options\" \"package\" SNIPPET-FLAG)
3922 SNIPPET-FLAG, when non-nil, indicates that this package is also
3923 needed when turning LaTeX snippets into images for inclusion into
3926 A string will be inserted as-is in the header of the document.
3928 Make sure that you only list packages here which:
3930 - you want in every file;
3931 - do not conflict with the setup in `org-format-latex-header';
3932 - do not conflict with the default packages in
3933 `org-latex-default-packages-alist'."
3935 :group
'org-export-latex
3936 :set
'org-set-packages-alist
3937 :get
'org-get-packages-alist
3940 (list :tag
"options/package pair"
3941 (string :tag
"options")
3942 (string :tag
"package")
3943 (boolean :tag
"Snippet"))
3944 (string :tag
"A line of LaTeX"))))
3946 (defgroup org-appearance nil
3947 "Settings for Org-mode appearance."
3948 :tag
"Org Appearance"
3951 (defcustom org-level-color-stars-only nil
3952 "Non-nil means fontify only the stars in each headline.
3953 When nil, the entire headline is fontified.
3954 Changing it requires restart of `font-lock-mode' to become effective
3955 also in regions already fontified."
3956 :group
'org-appearance
3959 (defcustom org-hide-leading-stars nil
3960 "Non-nil means hide the first N-1 stars in a headline.
3961 This works by using the face `org-hide' for these stars. This
3962 face is white for a light background, and black for a dark
3963 background. You may have to customize the face `org-hide' to
3965 Changing it requires restart of `font-lock-mode' to become effective
3966 also in regions already fontified.
3967 You may also set this on a per-file basis by adding one of the following
3968 lines to the buffer:
3970 #+STARTUP: hidestars
3971 #+STARTUP: showstars"
3972 :group
'org-appearance
3975 (defcustom org-hidden-keywords nil
3976 "List of symbols corresponding to keywords to be hidden the org buffer.
3977 For example, a value \\='(title) for this list will make the document's title
3978 appear in the buffer without the initial #+TITLE: keyword."
3979 :group
'org-appearance
3981 :type
'(set (const :tag
"#+AUTHOR" author
)
3982 (const :tag
"#+DATE" date
)
3983 (const :tag
"#+EMAIL" email
)
3984 (const :tag
"#+TITLE" title
)))
3986 (defcustom org-custom-properties nil
3987 "List of properties (as strings) with a special meaning.
3988 The default use of these custom properties is to let the user
3989 hide them with `org-toggle-custom-properties-visibility'."
3990 :group
'org-properties
3991 :group
'org-appearance
3993 :type
'(repeat (string :tag
"Property Name")))
3995 (defcustom org-fontify-done-headline nil
3996 "Non-nil means change the face of a headline if it is marked DONE.
3997 Normally, only the TODO/DONE keyword indicates the state of a headline.
3998 When this is non-nil, the headline after the keyword is set to the
3999 `org-headline-done' as an additional indication."
4000 :group
'org-appearance
4003 (defcustom org-fontify-emphasized-text t
4004 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
4005 Changing this variable requires a restart of Emacs to take effect."
4006 :group
'org-appearance
4009 (defcustom org-fontify-whole-heading-line nil
4010 "Non-nil means fontify the whole line for headings.
4011 This is useful when setting a background color for the
4013 :group
'org-appearance
4016 (defcustom org-highlight-latex-and-related nil
4017 "Non-nil means highlight LaTeX related syntax in the buffer.
4018 When non nil, the value should be a list containing any of the
4020 `latex' Highlight LaTeX snippets and environments.
4021 `script' Highlight subscript and superscript.
4022 `entities' Highlight entities."
4023 :group
'org-appearance
4025 :package-version
'(Org .
"8.0")
4027 (const :tag
"No highlighting" nil
)
4028 (set :greedy t
:tag
"Highlight"
4029 (const :tag
"LaTeX snippets and environments" latex
)
4030 (const :tag
"Subscript and superscript" script
)
4031 (const :tag
"Entities" entities
))))
4033 (defcustom org-hide-emphasis-markers nil
4034 "Non-nil mean font-lock should hide the emphasis marker characters."
4035 :group
'org-appearance
4038 (defcustom org-pretty-entities nil
4039 "Non-nil means show entities as UTF8 characters.
4040 When nil, the \\name form remains in the buffer."
4041 :group
'org-appearance
4045 (defcustom org-pretty-entities-include-sub-superscripts t
4046 "Non-nil means, pretty entity display includes formatting sub/superscripts."
4047 :group
'org-appearance
4051 (defvar org-emph-re nil
4052 "Regular expression for matching emphasis.
4053 After a match, the match groups contain these elements:
4054 0 The match of the full regular expression, including the characters
4055 before and after the proper match
4056 1 The character before the proper match, or empty at beginning of line
4057 2 The proper match, including the leading and trailing markers
4058 3 The leading marker like * or /, indicating the type of highlighting
4059 4 The text between the emphasis markers, not including the markers
4060 5 The character after the match, empty at the end of a line")
4061 (defvar org-verbatim-re nil
4062 "Regular expression for matching verbatim text.")
4063 (defvar org-emphasis-regexp-components
) ; defined just below
4064 (defvar org-emphasis-alist
) ; defined just below
4065 (defun org-set-emph-re (var val
)
4066 "Set variable and compute the emphasis regular expression."
4068 (when (and (boundp 'org-emphasis-alist
)
4069 (boundp 'org-emphasis-regexp-components
)
4070 org-emphasis-alist org-emphasis-regexp-components
)
4071 (let* ((e org-emphasis-regexp-components
)
4077 (body1 (concat body
"*?"))
4078 (markers (mapconcat 'car org-emphasis-alist
""))
4079 (vmarkers (mapconcat
4080 (lambda (x) (if (eq (nth 2 x
) 'verbatim
) (car x
) ""))
4081 org-emphasis-alist
"")))
4082 ;; make sure special characters appear at the right position in the class
4083 (if (string-match "\\^" markers
)
4084 (setq markers
(concat (replace-match "" t t markers
) "^")))
4085 (if (string-match "-" markers
)
4086 (setq markers
(concat (replace-match "" t t markers
) "-")))
4087 (if (string-match "\\^" vmarkers
)
4088 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
4089 (if (string-match "-" vmarkers
)
4090 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
4092 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
4093 (int-to-string nl
) "\\}")))
4096 (concat "\\([" pre
"]\\|^\\)"
4098 "\\([" markers
"]\\)"
4106 "\\([" post
"]\\|$\\)"))
4107 (setq org-verbatim-re
4108 (concat "\\([" pre
"]\\|^\\)"
4110 "\\([" vmarkers
"]\\)"
4118 "\\([" post
"]\\|$\\)")))))
4120 ;; This used to be a defcustom (Org <8.0) but allowing the users to
4121 ;; set this option proved cumbersome. See this message/thread:
4122 ;; http://article.gmane.org/gmane.emacs.orgmode/68681
4123 (defvar org-emphasis-regexp-components
4124 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
4125 "Components used to build the regular expression for emphasis.
4126 This is a list with five entries. Terminology: In an emphasis string
4127 like \" *strong word* \", we call the initial space PREMATCH, the final
4128 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
4129 and \"trong wor\" is the body. The different components in this variable
4130 specify what is allowed/forbidden in each part:
4132 pre Chars allowed as prematch. Beginning of line will be allowed too.
4133 post Chars allowed as postmatch. End of line will be allowed too.
4134 border The chars *forbidden* as border characters.
4135 body-regexp A regexp like \".\" to match a body character. Don't use
4136 non-shy groups here, and don't allow newline here.
4137 newline The maximum number of newlines allowed in an emphasis exp.
4139 You need to reload Org or to restart Emacs after customizing this.")
4141 (defcustom org-emphasis-alist
4145 ("=" org-verbatim verbatim
)
4146 ("~" org-code verbatim
)
4147 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))))
4148 "Alist of characters and faces to emphasize text.
4149 Text starting and ending with a special character will be emphasized,
4150 for example *bold*, _underlined_ and /italic/. This variable sets the
4151 marker characters and the face to be used by font-lock for highlighting
4152 in Org-mode Emacs buffers.
4154 You need to reload Org or to restart Emacs after customizing this."
4155 :group
'org-appearance
4156 :set
'org-set-emph-re
4158 :package-version
'(Org .
"8.0")
4161 (string :tag
"Marker character")
4163 (face :tag
"Font-lock-face")
4164 (plist :tag
"Face property list"))
4165 (option (const verbatim
)))))
4167 (defvar org-protecting-blocks
4168 '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
4169 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
4170 This is needed for font-lock setup.")
4172 ;;; Miscellaneous options
4174 (defgroup org-completion nil
4175 "Completion in Org-mode."
4176 :tag
"Org Completion"
4179 (defcustom org-completion-use-ido nil
4180 "Non-nil means use ido completion wherever possible.
4181 Note that `ido-mode' must be active for this variable to be relevant.
4182 If you decide to turn this variable on, you might well want to turn off
4183 `org-outline-path-complete-in-steps'.
4184 See also `org-completion-use-iswitchb'."
4185 :group
'org-completion
4188 (defcustom org-completion-use-iswitchb nil
4189 "Non-nil means use iswitchb completion wherever possible.
4190 Note that `iswitchb-mode' must be active for this variable to be relevant.
4191 If you decide to turn this variable on, you might well want to turn off
4192 `org-outline-path-complete-in-steps'.
4193 Note that this variable has only an effect if `org-completion-use-ido' is nil."
4194 :group
'org-completion
4197 (defcustom org-completion-fallback-command
'hippie-expand
4198 "The expansion command called by \\[pcomplete] in normal context.
4199 Normal means, no org-mode-specific context."
4200 :group
'org-completion
4203 ;;; Functions and variables from their packages
4204 ;; Declared here to avoid compiler warnings
4207 (defvar outline-mode-menu-heading
)
4208 (defvar outline-mode-menu-show
)
4209 (defvar outline-mode-menu-hide
)
4210 (defvar zmacs-regions
) ; XEmacs regions
4213 (defvar mark-active
)
4216 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
4217 (declare-function calendar-forward-day
"cal-move" (arg))
4218 (declare-function calendar-goto-date
"cal-move" (date))
4219 (declare-function calendar-goto-today
"cal-move" ())
4220 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
4221 (defvar calc-embedded-close-formula
)
4222 (defvar calc-embedded-open-formula
)
4223 (declare-function cdlatex-tab
"ext:cdlatex" ())
4224 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
4225 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
4226 (defvar font-lock-unfontify-region-function
)
4227 (declare-function iswitchb-read-buffer
"iswitchb"
4228 (prompt &optional default require-match start matches-set
))
4229 (defvar iswitchb-temp-buflist
)
4230 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
4231 (defvar org-agenda-tags-todo-honor-ignore-options
)
4232 (declare-function org-agenda-skip
"org-agenda" ())
4234 org-agenda-format-item
"org-agenda"
4235 (extra txt
&optional level category tags dotime noprefix remove-re habitp
))
4236 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
4237 (declare-function org-agenda-change-all-lines
"org-agenda"
4238 (newhead hdmarker
&optional fixface just-this
))
4239 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
4240 (declare-function org-agenda-maybe-redo
"org-agenda" ())
4241 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
4243 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
4244 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4245 "org-agenda" (&optional end
))
4246 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
4247 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
4248 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
4249 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
4250 (declare-function org-indent-mode
"org-indent" (&optional arg
))
4251 (declare-function parse-time-string
"parse-time" (string))
4252 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
4253 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
4254 (defvar remember-data-file
)
4255 (defvar texmathp-why
)
4256 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
4257 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
4259 (defvar org-latex-regexps
)
4261 ;;; Autoload and prepare some org modules
4263 ;; Some table stuff that needs to be defined here, because it is used
4264 ;; by the functions setting up org-mode or checking for table context.
4266 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
4267 "Detect an org-type or table-type table.")
4268 (defconst org-table-line-regexp
"^[ \t]*|"
4269 "Detect an org-type table line.")
4270 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
4271 "Detect an org-type table line.")
4272 (defconst org-table-hline-regexp
"^[ \t]*|-"
4273 "Detect an org-type table hline.")
4274 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
4275 "Detect a table-type table hline.")
4276 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
4277 "Detect the first line outside a table when searching from within it.
4278 This works for both table types.")
4280 (defconst org-TBLFM-regexp
"^[ \t]*#\\+TBLFM: "
4281 "Detect a #+TBLFM line.")
4284 (defun turn-on-orgtbl ()
4285 "Unconditionally turn on `orgtbl-mode'."
4286 (require 'org-table
)
4289 (defun org-at-table-p (&optional table-type
)
4290 "Return t if the cursor is inside an org-type table.
4291 If TABLE-TYPE is non-nil, also check for table.el-type tables."
4292 (if org-enable-table-editor
4294 (beginning-of-line 1)
4295 (looking-at (if table-type org-table-any-line-regexp
4296 org-table-line-regexp
)))
4298 (defsubst org-table-p
() (org-at-table-p))
4300 (defun org-at-table.el-p
()
4301 "Return t if and only if we are at a table.el table."
4302 (and (org-at-table-p 'any
)
4304 (goto-char (org-table-begin 'any
))
4305 (looking-at org-table1-hline-regexp
))))
4307 (defun org-table-recognize-table.el
()
4308 "If there is a table.el table nearby, recognize it and move into it."
4309 (if org-table-tab-recognizes-table.el
4310 (if (org-at-table.el-p
)
4312 (beginning-of-line 1)
4313 (if (looking-at org-table-dataline-regexp
)
4315 (if (looking-at org-table1-hline-regexp
)
4317 (beginning-of-line 2)
4318 (if (looking-at org-table-any-border-regexp
)
4319 (beginning-of-line -
1)))))
4320 (if (re-search-forward "|" (org-table-end t
) t
)
4323 (if (table--at-cell-p (point))
4325 (message "recognizing table.el table...")
4326 (table-recognize-table)
4327 (message "recognizing table.el table...done")))
4328 (error "This should not happen"))
4333 (defun org-at-table-hline-p ()
4334 "Return t if the cursor is inside a hline in a table."
4335 (if org-enable-table-editor
4337 (beginning-of-line 1)
4338 (looking-at org-table-hline-regexp
))
4341 (defun org-table-map-tables (function &optional quietly
)
4342 "Apply FUNCTION to the start of all tables in the buffer."
4346 (goto-char (point-min))
4347 (while (re-search-forward org-table-any-line-regexp nil t
)
4349 (message "Mapping tables: %d%%"
4350 (floor (* 100.0 (point)) (buffer-size))))
4351 (beginning-of-line 1)
4352 (when (and (looking-at org-table-line-regexp
)
4353 ;; Exclude tables in src/example/verbatim/clocktable blocks
4354 (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
4355 (save-excursion (funcall function
))
4356 (or (looking-at org-table-line-regexp
)
4358 (re-search-forward org-table-any-border-regexp nil
1))))
4359 (unless quietly
(message "Mapping tables: done")))
4361 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock" (beg end
))
4362 (declare-function org-clock-update-mode-line
"org-clock" ())
4363 (declare-function org-resolve-clocks
"org-clock"
4364 (&optional also-non-dangling-p prompt last-valid
))
4366 (defun org-at-TBLFM-p (&optional pos
)
4367 "Return t when point (or POS) is in #+TBLFM line."
4370 (goto-char (or pos
(point)))
4371 (beginning-of-line 1)
4372 (looking-at org-TBLFM-regexp
)))
4374 (defvar org-clock-start-time
)
4375 (defvar org-clock-marker
(make-marker)
4376 "Marker recording the last clock-in.")
4377 (defvar org-clock-hd-marker
(make-marker)
4378 "Marker recording the last clock-in, but the headline position.")
4379 (defvar org-clock-heading
""
4380 "The heading of the current clock entry.")
4381 (defun org-clock-is-active ()
4382 "Return the buffer where the clock is currently running.
4383 Return nil if no clock is running."
4384 (marker-buffer org-clock-marker
))
4386 (defun org-check-running-clock ()
4387 "Check if the current buffer contains the running clock.
4388 If yes, offer to stop it and to save the buffer with the changes."
4389 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4390 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4393 (when (y-or-n-p "Save changed buffer?")
4396 (defun org-clocktable-try-shift (dir n
)
4397 "Check if this line starts a clock table, if yes, shift the time block."
4398 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4399 (org-clocktable-shift dir n
)))
4402 (defun org-clock-persistence-insinuate ()
4403 "Set up hooks for clock persistence."
4404 (require 'org-clock
)
4405 (add-hook 'org-mode-hook
'org-clock-load
)
4406 (add-hook 'kill-emacs-hook
'org-clock-save
))
4408 (defgroup org-archive nil
4409 "Options concerning archiving in Org-mode."
4411 :group
'org-structure
)
4413 (defcustom org-archive-location
"%s_archive::"
4414 "The location where subtrees should be archived.
4416 The value of this variable is a string, consisting of two parts,
4417 separated by a double-colon. The first part is a filename and
4418 the second part is a headline.
4420 When the filename is omitted, archiving happens in the same file.
4421 %s in the filename will be replaced by the current file
4422 name (without the directory part). Archiving to a different file
4423 is useful to keep archived entries from contributing to the
4426 The archived entries will be filed as subtrees of the specified
4427 headline. When the headline is omitted, the subtrees are simply
4428 filed away at the end of the file, as top-level entries. Also in
4429 the heading you can use %s to represent the file name, this can be
4430 useful when using the same archive for a number of different files.
4432 Here are a few examples:
4434 If the current file is Projects.org, archive in file
4435 Projects.org_archive, as top-level trees. This is the default.
4437 \"::* Archived Tasks\"
4438 Archive in the current file, under the top-level headline
4439 \"* Archived Tasks\".
4441 \"~/org/archive.org::\"
4442 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4444 \"~/org/archive.org::* From %s\"
4445 Archive in file ~/org/archive.org (absolute path), under headlines
4446 \"From FILENAME\" where file name is the current file name.
4448 \"~/org/datetree.org::datetree/* Finished Tasks\"
4449 The \"datetree/\" string is special, signifying to archive
4450 items to the datetree. Items are placed in either the CLOSED
4451 date of the item, or the current date if there is no CLOSED date.
4452 The heading will be a subentry to the current date. There doesn't
4453 need to be a heading, but there always needs to be a slash after
4454 datetree. For example, to store archived items directly in the
4455 datetree, use \"~/org/datetree.org::datetree/\".
4457 \"basement::** Finished Tasks\"
4458 Archive in file ./basement (relative path), as level 3 trees
4459 below the level 2 heading \"** Finished Tasks\".
4461 You may set this option on a per-file basis by adding to the buffer a
4464 #+ARCHIVE: basement::** Finished Tasks
4466 You may also define it locally for a subtree by setting an ARCHIVE property
4467 in the entry. If such a property is found in an entry, or anywhere up
4468 the hierarchy, it will be used."
4472 (defcustom org-archive-tag
"ARCHIVE"
4473 "The tag that marks a subtree as archived.
4474 An archived subtree does not open during visibility cycling, and does
4475 not contribute to the agenda listings.
4476 After changing this, font-lock must be restarted in the relevant buffers to
4477 get the proper fontification."
4479 :group
'org-keywords
4482 (defcustom org-agenda-skip-archived-trees t
4483 "Non-nil means the agenda will skip any items located in archived trees.
4484 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4485 variable is no longer recommended, you should leave it at the value t.
4486 Instead, use the key `v' to cycle the archives-mode in the agenda."
4488 :group
'org-agenda-skip
4491 (defcustom org-columns-skip-archived-trees t
4492 "Non-nil means ignore archived trees when creating column view."
4494 :group
'org-properties
4497 (defcustom org-cycle-open-archived-trees nil
4498 "Non-nil means `org-cycle' will open archived trees.
4499 An archived tree is a tree marked with the tag ARCHIVE.
4500 When nil, archived trees will stay folded. You can still open them with
4501 normal outline commands like `show-all', but not with the cycling commands."
4506 (defcustom org-sparse-tree-open-archived-trees nil
4507 "Non-nil means sparse tree construction shows matches in archived trees.
4508 When nil, matches in these trees are highlighted, but the trees are kept in
4511 :group
'org-sparse-trees
4514 (defcustom org-sparse-tree-default-date-type
'scheduled-or-deadline
4515 "The default date type when building a sparse tree.
4516 When this is nil, a date is a scheduled or a deadline timestamp.
4517 Otherwise, these types are allowed:
4520 active: only active timestamps (<...>)
4521 inactive: only inactive timestamps (<...)
4522 scheduled: only scheduled timestamps
4523 deadline: only deadline timestamps"
4524 :type
'(choice (const :tag
"Scheduled or deadline" scheduled-or-deadline
)
4525 (const :tag
"All timestamps" all
)
4526 (const :tag
"Only active timestamps" active
)
4527 (const :tag
"Only inactive timestamps" inactive
)
4528 (const :tag
"Only scheduled timestamps" scheduled
)
4529 (const :tag
"Only deadline timestamps" deadline
)
4530 (const :tag
"Only closed timestamps" closed
))
4532 :group
'org-sparse-trees
)
4534 (defun org-cycle-hide-archived-subtrees (state)
4535 "Re-hide all archived subtrees after a visibility state change."
4536 (when (and (not org-cycle-open-archived-trees
)
4537 (not (memq state
'(overview folded
))))
4539 (let* ((globalp (memq state
'(contents all
)))
4540 (beg (if globalp
(point-min) (point)))
4541 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4542 (org-hide-archived-subtrees beg end
)
4544 (if (looking-at (concat ".*:" org-archive-tag
":"))
4545 (message "%s" (substitute-command-keys
4546 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4548 (defun org-force-cycle-archived ()
4549 "Cycle subtree even if it is archived."
4551 (setq this-command
'org-cycle
)
4552 (let ((org-cycle-open-archived-trees t
))
4553 (call-interactively 'org-cycle
)))
4555 (defun org-hide-archived-subtrees (beg end
)
4556 "Re-hide all archived subtrees after a visibility state change."
4558 (let* ((re (concat ":" org-archive-tag
":")))
4560 (while (re-search-forward re end t
)
4561 (when (org-at-heading-p)
4562 (org-flag-subtree t
)
4563 (org-end-of-subtree t
))))))
4565 (declare-function outline-end-of-heading
"outline" ())
4566 (declare-function outline-flag-region
"outline" (from to flag
))
4567 (defun org-flag-subtree (flag)
4569 (org-back-to-heading t
)
4570 (outline-end-of-heading)
4571 (outline-flag-region (point)
4572 (progn (org-end-of-subtree t
) (point))
4575 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4577 ;; Declare Column View Code
4579 (declare-function org-columns-number-to-string
"org-colview" (n fmt
&optional printf
))
4580 (declare-function org-columns-get-format-and-top-level
"org-colview" ())
4581 (declare-function org-columns-compute
"org-colview" (property))
4585 (declare-function org-id-store-link
"org-id")
4586 (declare-function org-id-locations-load
"org-id")
4587 (declare-function org-id-locations-save
"org-id")
4588 (defvar org-id-track-globally
)
4590 ;;; Variables for pre-computed regular expressions, all buffer local
4592 (defvar org-drawer-regexp
"^[ \t]*:PROPERTIES:[ \t]*$"
4593 "Matches first line of a hidden block.")
4594 (make-variable-buffer-local 'org-drawer-regexp
)
4595 (defvar org-todo-regexp nil
4596 "Matches any of the TODO state keywords.")
4597 (make-variable-buffer-local 'org-todo-regexp
)
4598 (defvar org-not-done-regexp nil
4599 "Matches any of the TODO state keywords except the last one.")
4600 (make-variable-buffer-local 'org-not-done-regexp
)
4601 (defvar org-not-done-heading-regexp nil
4602 "Matches a TODO headline that is not done.")
4603 (make-variable-buffer-local 'org-not-done-regexp
)
4604 (defvar org-todo-line-regexp nil
4605 "Matches a headline and puts TODO state into group 2 if present.")
4606 (make-variable-buffer-local 'org-todo-line-regexp
)
4607 (defvar org-complex-heading-regexp nil
4608 "Matches a headline and puts everything into groups:
4610 group 2: The todo keyword, maybe
4611 group 3: Priority cookie
4612 group 4: True headline
4614 (make-variable-buffer-local 'org-complex-heading-regexp
)
4615 (defvar org-complex-heading-regexp-format nil
4616 "Printf format to make regexp to match an exact headline.
4617 This regexp will match the headline of any node which has the
4618 exact headline text that is put into the format, but may have any
4619 TODO state, priority and tags.")
4620 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4621 (defvar org-todo-line-tags-regexp nil
4622 "Matches a headline and puts TODO state into group 2 if present.
4623 Also put tags into group 4 if tags are present.")
4624 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4625 (defvar org-ds-keyword-length
12
4626 "Maximum length of the DEADLINE and SCHEDULED keywords.")
4627 (make-variable-buffer-local 'org-ds-keyword-length
)
4628 (defvar org-deadline-regexp nil
4629 "Matches the DEADLINE keyword.")
4630 (make-variable-buffer-local 'org-deadline-regexp
)
4631 (defvar org-deadline-time-regexp nil
4632 "Matches the DEADLINE keyword together with a time stamp.")
4633 (make-variable-buffer-local 'org-deadline-time-regexp
)
4634 (defvar org-deadline-time-hour-regexp nil
4635 "Matches the DEADLINE keyword together with a time-and-hour stamp.")
4636 (make-variable-buffer-local 'org-deadline-time-hour-regexp
)
4637 (defvar org-deadline-line-regexp nil
4638 "Matches the DEADLINE keyword and the rest of the line.")
4639 (make-variable-buffer-local 'org-deadline-line-regexp
)
4640 (defvar org-scheduled-regexp nil
4641 "Matches the SCHEDULED keyword.")
4642 (make-variable-buffer-local 'org-scheduled-regexp
)
4643 (defvar org-scheduled-time-regexp nil
4644 "Matches the SCHEDULED keyword together with a time stamp.")
4645 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4646 (defvar org-scheduled-time-hour-regexp nil
4647 "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
4648 (make-variable-buffer-local 'org-scheduled-time-hour-regexp
)
4649 (defvar org-closed-time-regexp nil
4650 "Matches the CLOSED keyword together with a time stamp.")
4651 (make-variable-buffer-local 'org-closed-time-regexp
)
4653 (defvar org-keyword-time-regexp nil
4654 "Matches any of the 4 keywords, together with the time stamp.")
4655 (make-variable-buffer-local 'org-keyword-time-regexp
)
4656 (defvar org-keyword-time-not-clock-regexp nil
4657 "Matches any of the 3 keywords, together with the time stamp.")
4658 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4659 (defvar org-maybe-keyword-time-regexp nil
4660 "Matches a timestamp, possibly preceded by a keyword.")
4661 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4662 (defvar org-all-time-keywords nil
4663 "List of time keywords.")
4664 (make-variable-buffer-local 'org-all-time-keywords
)
4666 (defconst org-plain-time-of-day-regexp
4669 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4672 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4674 "Regular expression to match a plain time or time range.
4675 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4676 groups carry important information:
4678 1 the first time, range or not
4679 8 the second time, if it is a range.")
4681 (defconst org-plain-time-extension-regexp
4684 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4685 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4686 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4687 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4688 groups carry important information:
4691 9 minutes of duration")
4693 (defconst org-stamp-time-of-day-regexp
4695 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4696 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4698 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4699 "Regular expression to match a timestamp time or time range.
4700 After a match, the following groups carry important information:
4702 1 date plus weekday, for back referencing to make sure both times are on the same day
4703 2 the first time, range or not
4704 4 the second time, if it is a range.")
4706 (defconst org-startup-options
4707 '(("fold" org-startup-folded t
)
4708 ("overview" org-startup-folded t
)
4709 ("nofold" org-startup-folded nil
)
4710 ("showall" org-startup-folded nil
)
4711 ("showeverything" org-startup-folded showeverything
)
4712 ("content" org-startup-folded content
)
4713 ("indent" org-startup-indented t
)
4714 ("noindent" org-startup-indented nil
)
4715 ("hidestars" org-hide-leading-stars t
)
4716 ("showstars" org-hide-leading-stars nil
)
4717 ("odd" org-odd-levels-only t
)
4718 ("oddeven" org-odd-levels-only nil
)
4719 ("align" org-startup-align-all-tables t
)
4720 ("noalign" org-startup-align-all-tables nil
)
4721 ("inlineimages" org-startup-with-inline-images t
)
4722 ("noinlineimages" org-startup-with-inline-images nil
)
4723 ("latexpreview" org-startup-with-latex-preview t
)
4724 ("nolatexpreview" org-startup-with-latex-preview nil
)
4725 ("customtime" org-display-custom-times t
)
4726 ("logdone" org-log-done time
)
4727 ("lognotedone" org-log-done note
)
4728 ("nologdone" org-log-done nil
)
4729 ("lognoteclock-out" org-log-note-clock-out t
)
4730 ("nolognoteclock-out" org-log-note-clock-out nil
)
4731 ("logrepeat" org-log-repeat state
)
4732 ("lognoterepeat" org-log-repeat note
)
4733 ("logdrawer" org-log-into-drawer t
)
4734 ("nologdrawer" org-log-into-drawer nil
)
4735 ("logstatesreversed" org-log-states-order-reversed t
)
4736 ("nologstatesreversed" org-log-states-order-reversed nil
)
4737 ("nologrepeat" org-log-repeat nil
)
4738 ("logreschedule" org-log-reschedule time
)
4739 ("lognotereschedule" org-log-reschedule note
)
4740 ("nologreschedule" org-log-reschedule nil
)
4741 ("logredeadline" org-log-redeadline time
)
4742 ("lognoteredeadline" org-log-redeadline note
)
4743 ("nologredeadline" org-log-redeadline nil
)
4744 ("logrefile" org-log-refile time
)
4745 ("lognoterefile" org-log-refile note
)
4746 ("nologrefile" org-log-refile nil
)
4747 ("fninline" org-footnote-define-inline t
)
4748 ("nofninline" org-footnote-define-inline nil
)
4749 ("fnlocal" org-footnote-section nil
)
4750 ("fnauto" org-footnote-auto-label t
)
4751 ("fnprompt" org-footnote-auto-label nil
)
4752 ("fnconfirm" org-footnote-auto-label confirm
)
4753 ("fnplain" org-footnote-auto-label plain
)
4754 ("fnadjust" org-footnote-auto-adjust t
)
4755 ("nofnadjust" org-footnote-auto-adjust nil
)
4756 ("constcgs" constants-unit-system cgs
)
4757 ("constSI" constants-unit-system SI
)
4758 ("noptag" org-tag-persistent-alist nil
)
4759 ("hideblocks" org-hide-block-startup t
)
4760 ("nohideblocks" org-hide-block-startup nil
)
4761 ("beamer" org-startup-with-beamer-mode t
)
4762 ("entitiespretty" org-pretty-entities t
)
4763 ("entitiesplain" org-pretty-entities nil
))
4764 "Variable associated with STARTUP options for org-mode.
4765 Each element is a list of three items: the startup options (as written
4766 in the #+STARTUP line), the corresponding variable, and the value to set
4767 this variable to if the option is found. An optional forth element PUSH
4768 means to push this value onto the list in the variable.")
4770 (defun org-update-property-plist (key val props
)
4771 "Update PROPS with KEY and VAL."
4772 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4773 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4774 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4775 (previous (cdr (assoc key props
))))
4777 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4778 (cons (cons key val
) remainder
))))
4780 (defconst org-block-regexp
4781 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4782 "Regular expression for hiding blocks.")
4783 (defconst org-heading-keyword-regexp-format
4784 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4785 "Printf format for a regexp matching a headline with some keyword.
4786 This regexp will match the headline of any node which has the
4787 exact keyword that is put into the format. The keyword isn't in
4788 any group by default, but the stars and the body are.")
4789 (defconst org-heading-keyword-maybe-regexp-format
4790 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4791 "Printf format for a regexp matching a headline, possibly with some keyword.
4792 This regexp can match any headline with the specified keyword, or
4793 without a keyword. The keyword isn't in any group by default,
4794 but the stars and the body are.")
4796 (defcustom org-group-tags t
4797 "When non-nil (the default), use group tags.
4798 This can be turned on/off through `org-toggle-tags-groups'."
4803 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4805 (defun org-toggle-tags-groups ()
4806 "Toggle support for group tags.
4807 Support for group tags is controlled by the option
4808 `org-group-tags', which is non-nil by default."
4810 (setq org-group-tags
(not org-group-tags
))
4811 (cond ((and (derived-mode-p 'org-agenda-mode
)
4814 ((derived-mode-p 'org-mode
)
4815 (let ((org-inhibit-startup t
)) (org-mode))))
4816 (message "Groups tags support has been turned %s"
4817 (if org-group-tags
"on" "off")))
4819 (defun org-set-regexps-and-options-for-tags ()
4820 "Precompute variables used for tags."
4821 (when (derived-mode-p 'org-mode
)
4822 (org-set-local 'org-file-tags nil
)
4823 (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
4826 tags ftags key value
)
4830 (goto-char (point-min))
4831 (while (re-search-forward re nil t
)
4832 (setq key
(upcase (org-match-string-no-properties 1))
4833 value
(org-match-string-no-properties 2))
4834 (if (stringp value
) (setq value
(org-trim value
)))
4837 (setq tags
(append tags
(if tags
'("\\n") nil
)
4838 (org-split-string value splitre
))))
4839 ((equal key
"FILETAGS")
4840 (when (string-match "\\S-" value
)
4845 (mapcar (lambda (x) (org-split-string x
":"))
4846 (org-split-string value
)))))))))))
4847 ;; Process the file tags.
4848 (and ftags
(org-set-local 'org-file-tags
4849 (mapcar 'org-add-prop-inherited ftags
)))
4850 (org-set-local 'org-tag-groups-alist nil
)
4851 ;; Process the tags.
4852 (when (and (not tags
) org-tag-alist
)
4855 (lambda (tg) (cond ((eq (car tg
) :startgroup
) "{")
4856 ((eq (car tg
) :endgroup
) "}")
4857 ((eq (car tg
) :grouptags
) ":")
4858 ((eq (car tg
) :newline
) "\n")
4860 (if (characterp (cdr tg
))
4861 (format "(%s)" (char-to-string (cdr tg
))) "")))))
4867 (progn (push '(:startgroup
) tgs
)
4868 (when (equal (nth 1 tags
) ":")
4869 (push (list (replace-regexp-in-string
4870 "(.+)$" "" (nth 0 tags
)))
4871 org-tag-groups-alist
)
4873 ((equal e
":") (push '(:grouptags
) tgs
))
4874 ((equal e
"}") (push '(:endgroup
) tgs
) (if g
(setq g nil
)))
4875 ((equal e
"\\n") (push '(:newline
) tgs
))
4876 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4877 (push (cons (match-string 1 e
)
4878 (string-to-char (match-string 2 e
)))
4881 (setcar org-tag-groups-alist
4882 (append (car org-tag-groups-alist
)
4883 (list (match-string 1 e
)))))
4884 (if g
(setq g
(1+ g
))))
4885 (t (push (list e
) tgs
)
4887 (setcar org-tag-groups-alist
4888 (append (car org-tag-groups-alist
) (list e
))))
4889 (if g
(setq g
(1+ g
))))))
4890 (org-set-local 'org-tag-alist nil
)
4892 (or (and (stringp (car e
))
4893 (assoc (car e
) org-tag-alist
))
4894 (push e org-tag-alist
)))
4895 ;; Return a list with tag variables
4896 (list org-file-tags org-tag-alist org-tag-groups-alist
)))))
4898 (defvar org-ota nil
)
4899 (defun org-set-regexps-and-options ()
4900 "Precompute regular expressions used in the current buffer."
4901 (when (derived-mode-p 'org-mode
)
4902 (org-set-local 'org-todo-kwd-alist nil
)
4903 (org-set-local 'org-todo-key-alist nil
)
4904 (org-set-local 'org-todo-key-trigger nil
)
4905 (org-set-local 'org-todo-keywords-1 nil
)
4906 (org-set-local 'org-done-keywords nil
)
4907 (org-set-local 'org-todo-heads nil
)
4908 (org-set-local 'org-todo-sets nil
)
4909 (org-set-local 'org-todo-log-states nil
)
4910 (org-set-local 'org-file-properties nil
)
4911 (let ((re (org-make-options-regexp
4912 '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
4913 "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
4914 "SETUPFILE" "OPTIONS")
4915 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4917 (scripts org-use-sub-superscripts
)
4918 kwds kws0 kwsa key log value cat arch const links hw dws
4919 tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
4924 (goto-char (point-min))
4931 (insert ext-setup-or-nil
)
4932 (let ((major-mode 'org-mode
) org-ota
)
4933 (setq ret
(save-match-data
4934 (org-set-regexps-and-options-for-tags)))))
4935 ;; Append setupfile tags to existing tags
4938 (delq nil
(append org-file-tags
(nth 0 ret
)))
4940 (delq nil
(append org-tag-alist
(nth 1 ret
)))
4941 org-tag-groups-alist
4942 (delq nil
(append org-tag-groups-alist
(nth 2 ret
))))))
4943 (and ext-setup-or-nil
4944 (string-match re ext-setup-or-nil start
)
4945 (setq start
(match-end 0)))
4946 (and (setq ext-setup-or-nil nil start
0)
4947 (re-search-forward re nil t
)))
4948 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4949 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4950 (if (stringp value
) (setq value
(org-trim value
)))
4952 ((equal key
"CATEGORY")
4954 ((member key
'("SEQ_TODO" "TODO"))
4955 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4956 ((equal key
"TYP_TODO")
4957 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4958 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4959 ;; general TODO-like setup
4960 (push (cons (intern (downcase (match-string 1 key
)))
4961 (org-split-string value splitre
))
4963 ((equal key
"COLUMNS")
4964 (org-set-local 'org-columns-default-format value
))
4966 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4967 (push (cons (match-string 1 value
)
4968 (org-trim (match-string 2 value
)))
4970 ((equal key
"PRIORITIES")
4971 (setq prio
(org-split-string value
" +")))
4972 ((equal key
"PROPERTY")
4973 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4974 (setq props
(org-update-property-plist (match-string 1 value
)
4975 (match-string 2 value
)
4977 ((equal key
"DRAWERS")
4978 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4979 ((equal key
"CONSTANTS")
4980 (org-table-set-constants))
4981 ((equal key
"STARTUP")
4982 (let ((opts (org-split-string value splitre
))
4985 (when (setq l
(assoc l org-startup-options
))
4986 (setq var
(nth 1 l
) val
(nth 2 l
))
4988 (set (make-local-variable var
) val
)
4989 (if (not (listp (symbol-value var
)))
4990 (set (make-local-variable var
) nil
))
4991 (set (make-local-variable var
) (symbol-value var
))
4992 (add-to-list var val
))))))
4993 ((equal key
"ARCHIVE")
4995 (remove-text-properties 0 (length arch
)
4996 '(face t fontified t
) arch
))
4997 ((equal key
"OPTIONS")
4998 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4999 (setq scripts
(read (match-string 2 value
)))))
5000 ((and (equal key
"SETUPFILE")
5001 ;; Prevent checking in Gnus messages
5002 (not buffer-read-only
))
5003 (setq setup-contents
(org-file-contents
5005 (org-remove-double-quotes value
))
5007 (if (not ext-setup-or-nil
)
5008 (setq ext-setup-or-nil setup-contents start
0)
5009 (setq ext-setup-or-nil
5010 (concat (substring ext-setup-or-nil
0 start
)
5011 "\n" setup-contents
"\n"
5012 (substring ext-setup-or-nil start
)))))))
5013 ;; search for property blocks
5014 (goto-char (point-min))
5015 (while (re-search-forward org-block-regexp nil t
)
5016 (when (equal "PROPERTY" (upcase (match-string 1)))
5017 (setq value
(replace-regexp-in-string
5018 "[\n\r]" " " (match-string 4)))
5019 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
5020 (setq props
(org-update-property-plist (match-string 1 value
)
5021 (match-string 2 value
)
5023 (org-set-local 'org-use-sub-superscripts scripts
)
5025 (org-set-local 'org-category
(intern cat
))
5026 (push (cons "CATEGORY" cat
) props
))
5028 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
5029 (setq prio
(mapcar 'string-to-char prio
))
5030 (org-set-local 'org-highest-priority
(nth 0 prio
))
5031 (org-set-local 'org-lowest-priority
(nth 1 prio
))
5032 (org-set-local 'org-default-priority
(nth 2 prio
)))
5033 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
5034 (and drawers
(org-set-local 'org-drawers drawers
))
5035 (and arch
(org-set-local 'org-archive-location arch
))
5036 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
5037 ;; Process the TODO keywords
5039 ;; Use the global values as if they had been given locally.
5040 (setq kwds
(default-value 'org-todo-keywords
))
5041 (if (stringp (car kwds
))
5042 (setq kwds
(list (cons org-todo-interpretation
5043 (default-value 'org-todo-keywords
)))))
5044 (setq kwds
(reverse kwds
)))
5045 (setq kwds
(nreverse kwds
))
5049 (run-hook-with-args-until-success
5050 'org-todo-setup-filter-hook kws
)
5052 (setq inter
(pop kws
) sep
(member "|" kws
)
5053 kws0
(delete "|" (copy-sequence kws
))
5058 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
5060 (setq kw
(match-string 1 x
)
5061 key
(and (match-end 2) (match-string 2 x
))
5062 log
(org-extract-log-state-settings x
))
5063 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
5064 (and log
(push log org-todo-log-states
))
5066 (error "Invalid TODO keyword %s" x
)))
5068 kwsa
(if kwsa
(append '((:startgroup
))
5072 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
5073 tail
(list inter hw
(car dws
) (org-last dws
))))
5074 (add-to-list 'org-todo-heads hw
'append
)
5075 (push kws1 org-todo-sets
)
5076 (setq org-done-keywords
(append org-done-keywords dws nil
))
5077 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
5078 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
5079 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
5080 (setq org-todo-sets
(nreverse org-todo-sets
)
5081 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
5082 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
5083 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
5084 ;; Compute the regular expressions and other local variables.
5085 ;; Using `org-outline-regexp-bol' would complicate them much,
5086 ;; because of the fixed white space at the end of that string.
5087 (if (not org-done-keywords
)
5088 (setq org-done-keywords
(and org-todo-keywords-1
5089 (list (org-last org-todo-keywords-1
)))))
5090 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
5091 (length org-scheduled-string
)
5092 (length org-clock-string
)
5093 (length org-closed-string
)))
5095 (concat "^[ \t]*:\\("
5096 (mapconcat 'regexp-quote org-drawers
"\\|")
5098 org-not-done-keywords
5099 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
5102 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
5106 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
5108 org-not-done-heading-regexp
5109 (format org-heading-keyword-regexp-format org-not-done-regexp
)
5110 org-todo-line-regexp
5111 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
5112 org-complex-heading-regexp
5113 (concat "^\\(\\*+\\)"
5114 "\\(?: +" org-todo-regexp
"\\)?"
5115 "\\(?: +\\(\\[#.\\]\\)\\)?"
5116 "\\(?: +\\(.*?\\)\\)??"
5117 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
5119 org-complex-heading-regexp-format
5120 (concat "^\\(\\*+\\)"
5121 "\\(?: +" org-todo-regexp
"\\)?"
5122 "\\(?: +\\(\\[#.\\]\\)\\)?"
5124 ;; Stats cookies can be stuck to body.
5125 "\\(?:\\[[0-9%%/]+\\] *\\)*"
5127 "\\(?: *\\[[0-9%%/]+\\]\\)*"
5129 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
5131 org-todo-line-tags-regexp
5132 (concat "^\\(\\*+\\)"
5133 "\\(?: +" org-todo-regexp
"\\)?"
5134 "\\(?: +\\(.*?\\)\\)??"
5135 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
5137 org-deadline-regexp
(concat "\\<" org-deadline-string
)
5138 org-deadline-time-regexp
5139 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
5140 org-deadline-time-hour-regexp
5141 (concat "\\<" org-deadline-string
5142 " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
5143 org-deadline-line-regexp
5144 (concat "\\<\\(" org-deadline-string
"\\).*")
5145 org-scheduled-regexp
5146 (concat "\\<" org-scheduled-string
)
5147 org-scheduled-time-regexp
5148 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
5149 org-scheduled-time-hour-regexp
5150 (concat "\\<" org-scheduled-string
5151 " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
5152 org-closed-time-regexp
5153 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
5154 org-keyword-time-regexp
5155 (concat "\\<\\(" org-scheduled-string
5156 "\\|" org-deadline-string
5157 "\\|" org-closed-string
5158 "\\|" org-clock-string
"\\)"
5159 " *[[<]\\([^]>]+\\)[]>]")
5160 org-keyword-time-not-clock-regexp
5161 (concat "\\<\\(" org-scheduled-string
5162 "\\|" org-deadline-string
5163 "\\|" org-closed-string
5165 " *[[<]\\([^]>]+\\)[]>]")
5166 org-maybe-keyword-time-regexp
5167 (concat "\\(\\<\\(" org-scheduled-string
5168 "\\|" org-deadline-string
5169 "\\|" org-closed-string
5170 "\\|" org-clock-string
"\\)\\)?"
5171 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
5172 org-all-time-keywords
5173 (mapcar (lambda (w) (substring w
0 -
1))
5174 (list org-scheduled-string org-deadline-string
5175 org-clock-string org-closed-string
)))
5177 (org-compute-latex-and-related-regexp))))
5179 (defun org-file-contents (file &optional noerror
)
5180 "Return the contents of FILE, as a string."
5181 (if (or (not file
) (not (file-readable-p file
)))
5183 (error "Cannot read file \"%s\"" file
)
5184 (message "Cannot read file \"%s\"" file
)
5187 (insert-file-contents file
)
5190 (defun org-extract-log-state-settings (x)
5191 "Extract the log state setting from a TODO keyword string.
5192 This will extract info from a string like \"WAIT(w@/!)\"."
5193 (let (kw key log1 log2
)
5194 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
5195 (setq kw
(match-string 1 x
)
5196 key
(and (match-end 2) (match-string 2 x
))
5197 log1
(and (match-end 3) (match-string 3 x
))
5198 log2
(and (match-end 4) (match-string 4 x
)))
5201 (and log1
(if (equal log1
"!") 'time
'note
))
5202 (and log2
(if (equal log2
"!") 'time
'note
)))))))
5204 (defun org-remove-keyword-keys (list)
5205 "Remove a pair of parenthesis at the end of each string in LIST."
5207 (if (string-match "(.*)$" x
)
5208 (substring x
0 (match-beginning 0))
5212 (defun org-assign-fast-keys (alist)
5213 "Assign fast keys to a keyword-key alist.
5214 Respect keys that are already there."
5217 (if (or (memq (car e
) '(:newline
:grouptags
:endgroup
:startgroup
))
5218 (cdr e
)) ;; Key already assigned.
5220 (let ((clist (string-to-list (downcase (car e
))))
5221 (used (append new alist
)))
5222 (when (= (car clist
) ?
@)
5224 (while (and clist
(rassoc (car clist
) used
))
5227 (while (rassoc alt used
)
5229 (push (cons (car e
) (or (car clist
) alt
)) new
))))
5232 ;;; Some variables used in various places
5234 (defvar org-window-configuration nil
5235 "Used in various places to store a window configuration.")
5236 (defvar org-selected-window nil
5237 "Used in various places to store a window configuration.")
5238 (defvar org-finish-function nil
5239 "Function to be called when `C-c C-c' is used.
5240 This is for getting out of special buffers like capture.")
5243 ;; FIXME: Occasionally check by commenting these, to make sure
5244 ;; no other functions uses these, forgetting to let-bind them.
5245 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
5246 (defvar org-last-state
)
5247 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
5249 ;; Defined somewhere in this file, but used before definition.
5250 (defvar org-entities
) ;; defined in org-entities.el
5251 (defvar org-struct-menu
)
5252 (defvar org-org-menu
)
5253 (defvar org-tbl-menu
)
5255 ;;;; Define the Org-mode
5257 ;; We use a before-change function to check if a table might need
5259 (defvar org-table-may-need-update t
5260 "Indicates that a table might need an update.
5261 This variable is set by `org-before-change-function'.
5262 `org-table-align' sets it back to nil.")
5263 (defun org-before-change-function (beg end
)
5264 "Every change indicates that a table might need an update."
5265 (setq org-table-may-need-update t
))
5266 (defvar org-mode-map
)
5267 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
5268 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
5269 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
5270 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
5271 (defvar org-table-buffer-is-an nil
)
5273 (defvar bidi-paragraph-direction
)
5274 (defvar buffer-face-mode-face
)
5277 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
5278 (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"))
5279 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
5281 ;; Other stuff we need.
5282 (require 'time-date
)
5286 ;; (require 'org-macs) moved higher up in the file before it is first used
5287 (require 'org-entities
)
5288 ;; (require 'org-compat) moved higher up in the file before it is first used
5289 (require 'org-faces
)
5291 (require 'org-pcomplete
)
5293 (require 'org-footnote
)
5294 (require 'org-macro
)
5300 (define-derived-mode org-mode outline-mode
"Org"
5301 "Outline-based notes management and organizer, alias
5302 \"Carsten's outline-mode for keeping track of everything.\"
5304 Org-mode develops organizational tasks around a NOTES file which
5305 contains information about projects as plain text. Org-mode is
5306 implemented on top of outline-mode, which is ideal to keep the content
5307 of large files well structured. It supports ToDo items, deadlines and
5308 time stamps, which magically appear in the diary listing of the Emacs
5309 calendar. Tables are easily created with a built-in table editor.
5310 Plain text URL-like links connect to websites, emails (VM), Usenet
5311 messages (Gnus), BBDB entries, and any files related to the project.
5312 For printing and sharing of notes, an Org-mode file (or a part of it)
5313 can be exported as a structured ASCII or HTML file.
5315 The following commands are available:
5319 ;; Get rid of Outline menus, they are not needed
5320 ;; Need to do this here because define-derived-mode sets up
5321 ;; the keymap so late. Still, it is a waste to call this each time
5322 ;; we switch another buffer into org-mode.
5323 (if (featurep 'xemacs
)
5324 (when (boundp 'outline-mode-menu-heading
)
5325 ;; Assume this is Greg's port, it uses easymenu
5326 (easy-menu-remove outline-mode-menu-heading
)
5327 (easy-menu-remove outline-mode-menu-show
)
5328 (easy-menu-remove outline-mode-menu-hide
))
5329 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
5330 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
5331 (define-key org-mode-map
[menu-bar show
] 'undefined
))
5333 (org-load-modules-maybe)
5334 (when (featurep 'xemacs
)
5335 (easy-menu-add org-org-menu
)
5336 (easy-menu-add org-tbl-menu
))
5337 (org-install-agenda-files-menu)
5338 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
5339 (add-to-invisibility-spec '(org-cwidth))
5340 (add-to-invisibility-spec '(org-hide-block . t
))
5341 (when (featurep 'xemacs
)
5342 (org-set-local 'line-move-ignore-invisible t
))
5343 (org-set-local 'outline-regexp org-outline-regexp
)
5344 (org-set-local 'outline-level
'org-outline-level
)
5345 (setq bidi-paragraph-direction
'left-to-right
)
5346 (when (and org-ellipsis
5347 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
5348 (fboundp 'make-glyph-code
))
5349 (unless org-display-table
5350 (setq org-display-table
(make-display-table)))
5351 (set-display-table-slot
5354 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
5356 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
5357 (setq buffer-display-table org-display-table
))
5358 (org-set-regexps-and-options-for-tags)
5359 (org-set-regexps-and-options)
5360 (org-set-font-lock-defaults)
5361 (when (and org-tag-faces
(not org-tags-special-faces-re
))
5362 ;; tag faces set outside customize.... force initialization.
5363 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
5365 (org-set-local 'calc-embedded-open-mode
"# ")
5366 ;; Modify a few syntax entries
5367 (modify-syntax-entry ?
@ "w")
5368 (modify-syntax-entry ?
\" "\"")
5369 (modify-syntax-entry ?
\\ "_")
5370 (modify-syntax-entry ?~
"_")
5371 (if org-startup-truncated
(setq truncate-lines t
))
5372 (when org-startup-indented
(require 'org-indent
) (org-indent-mode 1))
5373 (org-set-local 'font-lock-unfontify-region-function
5374 'org-unfontify-region
)
5375 ;; Activate before-change-function
5376 (org-set-local 'org-table-may-need-update t
)
5377 (org-add-hook 'before-change-functions
'org-before-change-function nil
5379 ;; Check for running clock before killing a buffer
5380 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5381 ;; Initialize macros templates.
5382 (org-macro-initialize-templates)
5383 ;; Initialize radio targets.
5384 (org-update-radio-target-regexp)
5386 (org-set-local 'indent-line-function
'org-indent-line
)
5387 (org-set-local 'indent-region-function
'org-indent-region
)
5388 ;; Filling and auto-filling.
5391 (org-setup-comments-handling)
5392 ;; Beginning/end of defun
5393 (org-set-local 'beginning-of-defun-function
'org-backward-element
)
5394 (org-set-local 'end-of-defun-function
5396 (if (not (org-at-heading-p))
5397 (org-forward-element)
5398 (org-forward-element)
5399 (forward-char -
1))))
5400 ;; Next error for sparse trees
5401 (org-set-local 'next-error-function
'org-occur-next-match
)
5402 ;; Make sure dependence stuff works reliably, even for users who set it
5404 (if org-enforce-todo-dependencies
5405 (add-hook 'org-blocker-hook
5406 'org-block-todo-from-children-or-siblings-or-parent
)
5407 (remove-hook 'org-blocker-hook
5408 'org-block-todo-from-children-or-siblings-or-parent
))
5409 (if org-enforce-todo-checkbox-dependencies
5410 (add-hook 'org-blocker-hook
5411 'org-block-todo-from-checkboxes
)
5412 (remove-hook 'org-blocker-hook
5413 'org-block-todo-from-checkboxes
))
5415 ;; Align options lines
5417 'align-mode-rules-list
5418 '((org-in-buffer-settings
5419 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5420 (modes .
'(org-mode)))))
5423 (org-set-local 'imenu-create-index-function
5424 'org-imenu-get-tree
)
5426 ;; Make isearch reveal context
5427 (if (or (featurep 'xemacs
)
5428 (not (boundp 'outline-isearch-open-invisible-function
)))
5429 ;; Emacs 21 and XEmacs make use of the hook
5430 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5431 ;; Emacs 22 deals with this through a special variable
5432 (org-set-local 'outline-isearch-open-invisible-function
5433 (lambda (&rest ignore
) (org-show-context 'isearch
))))
5435 ;; Setup the pcomplete hooks
5436 (set (make-local-variable 'pcomplete-command-completion-function
)
5437 'org-pcomplete-initial
)
5438 (set (make-local-variable 'pcomplete-command-name-function
)
5439 'org-command-at-point
)
5440 (set (make-local-variable 'pcomplete-default-completion-function
)
5442 (set (make-local-variable 'pcomplete-parse-arguments-function
)
5443 'org-parse-arguments
)
5444 (set (make-local-variable 'pcomplete-termination-string
) "")
5445 (when (>= emacs-major-version
23)
5446 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
5448 ;; If empty file that did not turn on org-mode automatically, make it to.
5449 (if (and org-insert-mode-line-in-empty-file
5450 (org-called-interactively-p 'any
)
5451 (= (point-min) (point-max)))
5452 (insert "# -*- mode: org -*-\n\n"))
5453 (unless org-inhibit-startup
5455 (and org-startup-with-beamer-mode
(org-beamer-mode))
5456 (when org-startup-align-all-tables
5457 (org-table-map-tables 'org-table-align
'quietly
))
5458 (when org-startup-with-inline-images
5459 (org-display-inline-images))
5460 (when org-startup-with-latex-preview
5461 (org-preview-latex-fragment))
5462 (unless org-inhibit-startup-visibility-stuff
5463 (org-set-startup-visibility))))
5464 ;; Try to set org-hide correctly
5465 (let ((foreground (org-find-invisible-foreground)))
5467 (set-face-foreground 'org-hide foreground
))))
5469 ;; Update `customize-package-emacs-version-alist'
5470 (add-to-list 'customize-package-emacs-version-alist
5471 '(Org ("6.21b" .
"23.1") ("6.33x" .
"23.2")
5472 ("7.8.11" .
"24.1") ("7.9.4" .
"24.3")
5473 ("8.2.6" .
"24.4")))
5475 (defvar org-mode-transpose-word-syntax-table
5476 (let ((st (make-syntax-table)))
5477 (mapc (lambda(c) (modify-syntax-entry
5478 (string-to-char (car c
)) "w p" st
))
5482 (when (fboundp 'abbrev-table-put
)
5483 (abbrev-table-put org-mode-abbrev-table
5484 :parents
(list text-mode-abbrev-table
)))
5486 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5488 (defun org-find-invisible-foreground ()
5489 (let ((candidates (remove
5492 (list (face-background 'default
)
5493 (face-background 'org-default
))
5496 (when (boundp alist
)
5497 (cdr (assoc 'background-color
(symbol-value alist
)))))
5498 '(default-frame-alist initial-frame-alist window-system-default-frame-alist
))
5499 (list (face-foreground 'org-hide
))))))
5500 (car (remove nil candidates
))))
5502 (defun org-current-time (&optional rounding-minutes past
)
5503 "Current time, possibly rounded to ROUNDING-MINUTES.
5504 When ROUNDING-MINUTES is not an integer, fall back on the car of
5505 `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
5506 the rounding returns a past time."
5507 (let ((r (or (and (integerp rounding-minutes
) rounding-minutes
)
5508 (car org-time-stamp-rounding-minutes
)))
5509 (time (decode-time)) res
)
5514 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5516 (if (and past
(< (float-time (time-subtract (current-time) res
)) 0))
5517 (seconds-to-time (- (float-time res
) (* r
60)))
5521 "Return today date, considering `org-extend-today-until'."
5523 (time-subtract (current-time)
5524 (list 0 (* 3600 org-extend-today-until
) 0))))
5526 ;;;; Font-Lock stuff, including the activators
5528 (defvar org-mouse-map
(make-sparse-keymap))
5529 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5530 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5531 (when org-mouse-1-follows-link
5532 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5533 (when org-tab-follows-link
5534 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5535 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5537 (require 'font-lock
)
5539 (defconst org-non-link-chars
"]\t\n\r<>")
5540 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5541 "shell" "elisp" "doi" "message"))
5542 (defvar org-link-types-re nil
5543 "Matches a link that has a url-like prefix like \"http:\"")
5544 (defvar org-link-re-with-space nil
5545 "Matches a link with spaces, optional angular brackets around it.")
5546 (defvar org-link-re-with-space2 nil
5547 "Matches a link with spaces, optional angular brackets around it.")
5548 (defvar org-link-re-with-space3 nil
5549 "Matches a link with spaces, only for internal part in bracket links.")
5550 (defvar org-angle-link-re nil
5551 "Matches link with angular brackets, spaces are allowed.")
5552 (defvar org-plain-link-re nil
5553 "Matches plain link, without spaces.")
5554 (defvar org-bracket-link-regexp nil
5555 "Matches a link in double brackets.")
5556 (defvar org-bracket-link-analytic-regexp nil
5557 "Regular expression used to analyze links.
5558 Here is what the match groups contain after a match:
5564 (defvar org-bracket-link-analytic-regexp
++ nil
5565 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5566 (defvar org-any-link-re nil
5567 "Regular expression matching any link.")
5569 (defconst org-match-sexp-depth
3
5570 "Number of stacked braces for sub/superscript matching.")
5572 (defun org-create-multibrace-regexp (left right n
)
5573 "Create a regular expression which will match a balanced sexp.
5574 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5575 as single character strings.
5576 The regexp returned will match the entire expression including the
5577 delimiters. It will also define a single group which contains the
5578 match except for the outermost delimiters. The maximum depth of
5579 stacked delimiters is N. Escaping delimiters is not possible."
5580 (let* ((nothing (concat "[^" left right
"]*?"))
5583 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5586 re
(concat re or next
)
5587 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5588 (concat left
"\\(" re
"\\)" right
)))
5590 (defvar org-match-substring-regexp
5592 "\\(\\S-\\)\\([_^]\\)\\("
5593 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5595 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5597 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5598 "The regular expression matching a sub- or superscript.")
5600 (defvar org-match-substring-with-braces-regexp
5602 "\\(\\S-\\)\\([_^]\\)\\("
5603 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5605 "The regular expression matching a sub- or superscript, forcing braces.")
5607 (defun org-make-link-regexps ()
5608 "Update the link regular expressions.
5609 This should be called after the variable `org-link-types' has changed."
5610 (let ((types-re (regexp-opt org-link-types t
)))
5611 (setq org-link-types-re
5612 (concat "\\`" types-re
":")
5613 org-link-re-with-space
5614 (concat "<?" types-re
":"
5615 "\\([^" org-non-link-chars
" ]"
5616 "[^" org-non-link-chars
"]*"
5617 "[^" org-non-link-chars
" ]\\)>?")
5618 org-link-re-with-space2
5619 (concat "<?" types-re
":"
5620 "\\([^" org-non-link-chars
" ]"
5622 "[^" org-non-link-chars
" ]\\)>?")
5623 org-link-re-with-space3
5624 (concat "<?" types-re
":"
5625 "\\([^" org-non-link-chars
" ]"
5628 (concat "<" types-re
":"
5629 "\\([^" org-non-link-chars
" ]"
5630 "[^" org-non-link-chars
"]*"
5635 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5636 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5637 org-bracket-link-regexp
5638 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5639 org-bracket-link-analytic-regexp
5642 "\\(" types-re
":\\)?"
5645 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5647 org-bracket-link-analytic-regexp
++
5650 "\\(" (regexp-opt (cons "coderef" org-link-types
) t
) ":\\)?"
5653 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5656 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5657 org-angle-link-re
"\\)\\|\\("
5658 org-plain-link-re
"\\)"))))
5660 (org-make-link-regexps)
5662 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5663 "Regular expression for fast time stamp matching.")
5664 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5665 "Regular expression for fast time stamp matching.")
5666 (defconst org-ts-regexp0
5667 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5668 "Regular expression matching time strings for analysis.
5669 This one does not require the space after the date, so it can be used
5670 on a string that terminates immediately after the date.")
5671 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5672 "Regular expression matching time strings for analysis.")
5673 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5674 "Regular expression matching time stamps, with groups.")
5675 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5676 "Regular expression matching time stamps (also [..]), with groups.")
5677 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5678 "Regular expression matching a time stamp range.")
5679 (defconst org-tr-regexp-both
5680 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5681 "Regular expression matching a time stamp range.")
5682 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5683 org-ts-regexp
"\\)?")
5684 "Regular expression matching a time stamp or time stamp range.")
5685 (defconst org-tsr-regexp-both
5686 (concat org-ts-regexp-both
"\\(--?-?"
5687 org-ts-regexp-both
"\\)?")
5688 "Regular expression matching a time stamp or time stamp range.
5689 The time stamps may be either active or inactive.")
5691 (defvar org-emph-face nil
)
5693 (defun org-do-emphasis-faces (limit)
5694 "Run through the buffer and add overlays to emphasized strings."
5696 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5697 (let* ((border (char-after (match-beginning 3)))
5698 (bre (regexp-quote (char-to-string border
))))
5699 (if (and (not (= border
(char-after (match-beginning 4))))
5700 (not (save-match-data
5701 (string-match (concat bre
".*" bre
)
5702 (replace-regexp-in-string
5704 (substring (match-string 2) 1 -
1))))))
5707 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5708 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5712 (org-remove-flyspell-overlays-in
5713 (match-beginning 0) (match-end 0)))
5714 (add-text-properties (match-beginning 2) (match-end 2)
5715 '(font-lock-multiline t org-emphasis t
))
5716 (when org-hide-emphasis-markers
5717 (add-text-properties (match-end 4) (match-beginning 5)
5718 '(invisible org-link
))
5719 (add-text-properties (match-beginning 3) (match-end 3)
5720 '(invisible org-link
))))))
5721 (goto-char (1+ (match-beginning 0))))
5724 (defun org-emphasize (&optional char
)
5725 "Insert or change an emphasis, i.e. a font like bold or italic.
5726 If there is an active region, change that region to a new emphasis.
5727 If there is no region, just insert the marker characters and position
5728 the cursor between them.
5729 CHAR should be the marker character. If it is a space, it means to
5730 remove the emphasis of the selected region.
5731 If CHAR is not given (for example in an interactive call) it will be
5734 (let ((erc org-emphasis-regexp-components
)
5736 (string "") beg end move c s
)
5737 (if (org-region-active-p)
5738 (setq beg
(region-beginning) end
(region-end)
5739 string
(buffer-substring beg end
))
5743 (message "Emphasis marker or tag: [%s]"
5744 (mapconcat (lambda(e) (car e
)) org-emphasis-alist
""))
5745 (setq char
(read-char-exclusive)))
5746 (if (equal char ?\
)
5747 (setq s
"" move nil
)
5748 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5749 (user-error "No such emphasis marker: \"%c\"" char
))
5750 (setq s
(char-to-string char
)))
5751 (while (and (> (length string
) 1)
5752 (equal (substring string
0 1) (substring string -
1))
5753 (assoc (substring string
0 1) org-emphasis-alist
))
5754 (setq string
(substring string
1 -
1)))
5755 (setq string
(concat s string s
))
5756 (if beg
(delete-region beg end
))
5758 (string-match (concat "[" (nth 0 erc
) "\n]")
5759 (char-to-string (char-before (point)))))
5762 (string-match (concat "[" (nth 1 erc
) "\n]")
5763 (char-to-string (char-after (point)))))
5764 (insert " ") (backward-char 1))
5766 (and move
(backward-char 1))))
5768 (defconst org-nonsticky-props
5769 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link
))
5771 (defsubst org-rear-nonsticky-at
(pos)
5772 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5774 (defun org-activate-plain-links (limit)
5775 "Run through the buffer and add overlays to links."
5777 (when (and (re-search-forward (concat org-plain-link-re
) limit t
)
5778 (not (org-in-src-block-p)))
5779 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5780 (setq f
(get-text-property (match-beginning 0) 'face
))
5781 (setq hl
(org-match-string-no-properties 0))
5782 (if (or (eq f
'org-tag
)
5783 (and (listp f
) (memq 'org-tag f
)))
5785 (add-text-properties (match-beginning 0) (match-end 0)
5786 (list 'mouse-face
'highlight
5788 'htmlize-link
`(:uri
,hl
)
5789 'keymap org-mouse-map
))
5790 (org-rear-nonsticky-at (match-end 0)))
5793 (defun org-activate-code (limit)
5794 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5796 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5797 (remove-text-properties (match-beginning 0) (match-end 0)
5798 '(display t invisible t intangible t
))
5801 (defcustom org-src-fontify-natively nil
5802 "When non-nil, fontify code in code blocks."
5805 :group
'org-appearance
5808 (defcustom org-allow-promoting-top-level-subtree nil
5809 "When non-nil, allow promoting a top level subtree.
5810 The leading star of the top level headline will be replaced
5814 :group
'org-appearance
)
5816 (defun org-fontify-meta-lines-and-blocks (limit)
5818 (org-fontify-meta-lines-and-blocks-1 limit
)
5819 (error (message "org-mode fontification error"))))
5821 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5822 "Fontify #+ lines and blocks."
5823 (let ((case-fold-search t
))
5824 (if (re-search-forward
5825 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5827 (let ((beg (match-beginning 0))
5828 (block-start (match-end 0))
5830 (lang (match-string 7))
5831 (beg1 (line-beginning-position 2))
5832 (dc1 (downcase (match-string 2)))
5833 (dc3 (downcase (match-string 3)))
5834 end end1 quoting block-type ovl
)
5836 ((member dc1
'("+html:" "+ascii:" "+latex:"))
5837 ;; a single line of backend-specific content
5838 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5839 (remove-text-properties (match-beginning 0) (match-end 0)
5840 '(display t invisible t intangible t
))
5841 (add-text-properties (match-beginning 1) (match-end 3)
5842 '(font-lock-fontified t face org-meta-line
))
5843 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5844 '(font-lock-fontified t face org-block
))
5845 ; for backend-specific code
5847 ((and (match-end 4) (equal dc3
"+begin"))
5849 (setq block-type
(downcase (match-string 5))
5850 quoting
(member block-type org-protecting-blocks
))
5851 (when (re-search-forward
5852 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5853 nil t
) ;; on purpose, we look further than LIMIT
5854 (setq end
(min (point-max) (match-end 0))
5855 end1
(min (point-max) (1- (match-beginning 0))))
5856 (setq block-end
(match-beginning 0))
5858 (remove-text-properties beg end
5859 '(display t invisible t intangible t
)))
5860 (add-text-properties
5862 '(font-lock-fontified t font-lock-multiline t
))
5863 (add-text-properties beg beg1
'(face org-meta-line
))
5864 (add-text-properties end1
(min (point-max) (1+ end
))
5865 '(face org-meta-line
)) ; for end_src
5867 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5868 (org-src-font-lock-fontify-block lang block-start block-end
)
5869 ;; remove old background overlays
5871 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5872 (delete-overlay ov
)))
5873 (overlays-at (/ (+ beg1 block-end
) 2)))
5874 ;; add a background overlay
5875 (setq ovl
(make-overlay beg1 block-end
))
5876 (overlay-put ovl
'face
'org-block-background
)
5877 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5879 (add-text-properties beg1
(min (point-max) (1+ end1
))
5880 '(face org-block
))) ; end of source block
5881 ((not org-fontify-quote-and-verse-blocks
))
5882 ((string= block-type
"quote")
5883 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5884 ((string= block-type
"verse")
5885 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5886 (add-text-properties beg beg1
'(face org-block-begin-line
))
5887 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5888 '(face org-block-end-line
))
5890 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5891 (add-text-properties
5893 (if (member (intern (substring dc1
1 -
1)) org-hidden-keywords
)
5894 '(font-lock-fontified t invisible t
)
5895 '(font-lock-fontified t face org-document-info-keyword
)))
5896 (add-text-properties
5897 (match-beginning 6) (min (point-max) (1+ (match-end 6)))
5898 (if (string-equal dc1
"+title:")
5899 '(font-lock-fontified t face org-document-title
)
5900 '(font-lock-fontified t face org-document-info
))))
5901 ((or (equal dc1
"+results")
5902 (member dc1
'("+begin:" "+end:" "+caption:" "+label:"
5903 "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
5904 "+call:" "+header:" "+headers:" "+name:"))
5905 (and (match-end 4) (equal dc3
"+attr")))
5906 (add-text-properties
5908 '(font-lock-fontified t face org-meta-line
))
5910 ((member dc3
'(" " ""))
5911 (add-text-properties
5913 '(font-lock-fontified t face font-lock-comment-face
)))
5914 ((not (member (char-after beg
) '(?\ ?
\t)))
5915 ;; just any other in-buffer setting, but not indented
5916 (add-text-properties
5918 '(font-lock-fontified t face org-meta-line
))
5922 (defun org-activate-angle-links (limit)
5923 "Run through the buffer and add overlays to links."
5924 (if (and (re-search-forward org-angle-link-re limit t
)
5925 (not (org-in-src-block-p)))
5927 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5928 (add-text-properties (match-beginning 0) (match-end 0)
5929 (list 'mouse-face
'highlight
5930 'keymap org-mouse-map
))
5931 (org-rear-nonsticky-at (match-end 0))
5934 (defun org-activate-footnote-links (limit)
5935 "Run through the buffer and add overlays to footnotes."
5936 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5938 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5939 (org-remove-flyspell-overlays-in beg end
)
5940 (add-text-properties beg end
5941 (list 'mouse-face
'highlight
5942 'keymap org-mouse-map
5944 (if (= (point-at-bol) beg
)
5945 "Footnote definition"
5946 "Footnote reference")
5947 'font-lock-fontified t
5948 'font-lock-multiline t
5949 'face
'org-footnote
))))))
5951 (defun org-activate-bracket-links (limit)
5952 "Run through the buffer and add overlays to bracketed links."
5953 (if (and (re-search-forward org-bracket-link-regexp limit t
)
5954 (not (org-in-src-block-p)))
5955 (let* ((hl (org-match-string-no-properties 1))
5956 (help (concat "LINK: " (save-match-data (org-link-unescape hl
))))
5957 (ip (org-maybe-intangible
5958 (list 'invisible
'org-link
5959 'keymap org-mouse-map
'mouse-face
'highlight
5960 'font-lock-multiline t
'help-echo help
5961 'htmlize-link
`(:uri
,hl
))))
5962 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5963 'font-lock-multiline t
'help-echo help
5964 'htmlize-link
`(:uri
,hl
))))
5965 ;; We need to remove the invisible property here. Table narrowing
5966 ;; may have made some of this invisible.
5967 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5968 (remove-text-properties (match-beginning 0) (match-end 0)
5972 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5973 (org-rear-nonsticky-at (match-beginning 3))
5974 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5975 (org-rear-nonsticky-at (match-end 3))
5976 (add-text-properties (match-end 3) (match-end 0) ip
)
5977 (org-rear-nonsticky-at (match-end 0)))
5978 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5979 (org-rear-nonsticky-at (match-beginning 1))
5980 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5981 (org-rear-nonsticky-at (match-end 1))
5982 (add-text-properties (match-end 1) (match-end 0) ip
)
5983 (org-rear-nonsticky-at (match-end 0)))
5986 (defun org-activate-dates (limit)
5987 "Run through the buffer and add overlays to dates."
5988 (if (and (re-search-forward org-tsr-regexp-both limit t
)
5989 (not (equal (char-before (match-beginning 0)) 91)))
5991 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5992 (add-text-properties (match-beginning 0) (match-end 0)
5993 (list 'mouse-face
'highlight
5994 'keymap org-mouse-map
))
5995 (org-rear-nonsticky-at (match-end 0))
5996 (when org-display-custom-times
5998 (org-display-custom-time (match-beginning 3) (match-end 3)))
5999 (org-display-custom-time (match-beginning 1) (match-end 1)))
6002 (defvar org-target-link-regexp nil
6003 "Regular expression matching radio targets in plain text.")
6004 (make-variable-buffer-local 'org-target-link-regexp
)
6005 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
6006 "Regular expression matching a link target.")
6007 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
6008 "Regular expression matching a radio target.")
6009 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
6010 "Regular expression matching any target.")
6012 (defun org-activate-target-links (limit)
6013 "Run through the buffer and add overlays to target matches."
6014 (when org-target-link-regexp
6015 (let ((case-fold-search t
))
6016 (if (re-search-forward org-target-link-regexp limit t
)
6018 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
6019 (add-text-properties (match-beginning 0) (match-end 0)
6020 (list 'mouse-face
'highlight
6021 'keymap org-mouse-map
6022 'help-echo
"Radio target link"
6023 'org-linked-text t
))
6024 (org-rear-nonsticky-at (match-end 0))
6027 (defun org-update-radio-target-regexp ()
6028 "Find all radio targets in this file and update the regular expression."
6030 (when (memq 'radio org-activate-links
)
6031 (setq org-target-link-regexp
6032 (org-make-target-link-regexp (org-all-targets 'radio
)))
6033 (org-restart-font-lock)))
6035 (defun org-hide-wide-columns (limit)
6037 (setq s
(text-property-any (point) (or limit
(point-max))
6040 (setq e
(next-single-property-change s
'org-cwidth
))
6041 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
6045 (defvar org-latex-and-related-regexp nil
6046 "Regular expression for highlighting LaTeX, entities and sub/superscript.")
6047 (defvar org-match-substring-regexp
)
6048 (defvar org-match-substring-with-braces-regexp
)
6050 (defun org-compute-latex-and-related-regexp ()
6051 "Compute regular expression for LaTeX, entities and sub/superscript.
6052 Result depends on variable `org-highlight-latex-and-related'."
6054 'org-latex-and-related-regexp
6056 (cond ((not (memq 'script org-highlight-latex-and-related
)) nil
)
6057 ((eq org-use-sub-superscripts
'{})
6058 (list org-match-substring-with-braces-regexp
))
6059 (org-use-sub-superscripts (list org-match-substring-regexp
))))
6061 (when (memq 'latex org-highlight-latex-and-related
)
6062 (let ((matchers (plist-get org-format-latex-options
:matchers
)))
6065 (and (member (car x
) matchers
) (nth 1 x
)))
6066 org-latex-regexps
)))))
6068 (when (memq 'entities org-highlight-latex-and-related
)
6069 (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
6070 (mapconcat 'identity
(append re-latex re-entities re-sub
) "\\|"))))
6072 (defun org-do-latex-and-related (limit)
6073 "Highlight LaTeX snippets and environments, entities and sub/superscript.
6074 LIMIT bounds the search for syntax to highlight. Stop at first
6075 highlighted object, if any. Return t if some highlighting was
6076 done, nil otherwise."
6077 (when (org-string-nw-p org-latex-and-related-regexp
)
6079 (while (re-search-forward org-latex-and-related-regexp limit t
)
6080 (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
6082 '(org-code org-verbatim underline
))
6083 (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
6087 (font-lock-prepend-text-property
6088 (+ offset
(match-beginning 0)) (match-end 0)
6089 'face
'org-latex-and-related
)
6090 (add-text-properties (+ offset
(match-beginning 0)) (match-end 0)
6091 '(font-lock-multiline t
)))
6095 (defun org-restart-font-lock ()
6096 "Restart `font-lock-mode', to force refontification."
6097 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
6099 (font-lock-mode 1)))
6101 (defun org-all-targets (&optional radio
)
6102 "Return a list of all targets in this file.
6103 When optional argument RADIO is non-nil, only find radio
6105 (let ((re (if radio org-radio-target-regexp org-target-regexp
)) rtn
)
6107 (goto-char (point-min))
6108 (while (re-search-forward re nil t
)
6109 ;; Make sure point is really within the object.
6111 (let ((obj (org-element-context)))
6112 (when (memq (org-element-type obj
) '(radio-target target
))
6113 (add-to-list 'rtn
(downcase (org-element-property :value obj
))))))
6116 (defun org-make-target-link-regexp (targets)
6117 "Make regular expression matching all strings in TARGETS.
6118 The regular expression finds the targets also if there is a line break
6125 (setq x
(regexp-quote x
))
6126 (while (string-match " +" x
)
6127 (setq x
(replace-match "\\s-+" t t x
)))
6133 (defun org-activate-tags (limit)
6134 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
6136 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
6137 (add-text-properties (match-beginning 1) (match-end 1)
6138 (list 'mouse-face
'highlight
6139 'keymap org-mouse-map
))
6140 (org-rear-nonsticky-at (match-end 1))
6143 (defun org-outline-level ()
6144 "Compute the outline level of the heading at point.
6145 If this is called at a normal headline, the level is the number of stars.
6146 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
6148 (if (not (condition-case nil
6149 (org-back-to-heading t
)
6152 (looking-at org-outline-regexp
)
6153 (1- (- (match-end 0) (match-beginning 0))))))
6155 (defvar org-font-lock-keywords nil
)
6157 (defsubst org-re-property
(property &optional literal allow-null
)
6158 "Return a regexp matching a PROPERTY line.
6160 When optional argument LITERAL is non-nil, do not quote PROPERTY.
6161 This is useful when PROPERTY is a regexp. When ALLOW-NULL is
6162 non-nil, match properties even without a value.
6164 Match group 3 is set to the value when it exists. If there is no
6165 value and ALLOW-NULL is non-nil, it is set to the empty string."
6168 (format "\\(?1::\\(?2:%s\\):\\)"
6169 (if literal property
(regexp-quote property
)))
6171 "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$"
6172 "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$")))
6174 (defconst org-property-re
6175 (org-re-property ".*?" 'literal t
)
6176 "Regular expression matching a property line.
6177 There are four matching groups:
6178 1: :PROPKEY: including the leading and trailing colon,
6179 2: PROPKEY without the leading and trailing colon,
6180 3: PROPVAL without leading or trailing spaces,
6181 4: the indentation of the current line,
6182 5: trailing whitespace.")
6184 (defvar org-font-lock-hook nil
6185 "Functions to be called for special font lock stuff.")
6187 (defvar org-font-lock-set-keywords-hook nil
6188 "Functions that can manipulate `org-font-lock-extra-keywords'.
6189 This is called after `org-font-lock-extra-keywords' is defined, but before
6190 it is installed to be used by font lock. This can be useful if something
6191 needs to be inserted at a specific position in the font-lock sequence.")
6193 (defun org-font-lock-hook (limit)
6194 "Run `org-font-lock-hook' within LIMIT."
6195 (run-hook-with-args 'org-font-lock-hook limit
))
6197 (defun org-set-font-lock-defaults ()
6198 "Set font lock defaults for the current buffer."
6199 (let* ((em org-fontify-emphasized-text
)
6200 (lk org-activate-links
)
6201 (org-font-lock-extra-keywords
6204 '(org-font-lock-hook)
6206 `(,(if org-fontify-whole-heading-line
6207 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
6208 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
6209 (1 (org-get-level-face 1))
6210 (2 (org-get-level-face 2))
6211 (3 (org-get-level-face 3)))
6213 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
6216 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
6217 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
6218 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
6219 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
6221 (list org-drawer-regexp
'(0 'org-special-keyword t
))
6222 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
6224 (list org-property-re
6225 '(1 'org-special-keyword t
)
6226 '(3 'org-property-value t
))
6228 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
6229 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
6230 (if (memq 'plain lk
) '(org-activate-plain-links (0 'org-link t
)))
6231 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
6232 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
6233 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
6234 (if (memq 'footnote lk
) '(org-activate-footnote-links))
6236 (list org-any-target-regexp
'(0 'org-target t
))
6238 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
6240 '("{{{.+}}}" (0 'org-macro t
))
6241 '(org-hide-wide-columns (0 nil append
))
6243 (list (format org-heading-keyword-regexp-format
6245 '(2 (org-get-todo-face 2) t
))
6247 (if org-fontify-done-headline
6248 (list (format org-heading-keyword-regexp-format
6251 (mapconcat 'regexp-quote org-done-keywords
"\\|")
6253 '(2 'org-headline-done t
))
6256 '(org-font-lock-add-priority-faces)
6258 '(org-font-lock-add-tag-faces)
6260 (if (and org-group-tags org-tag-groups-alist
)
6261 (list (concat org-outline-regexp-bol
".+\\(:"
6262 (regexp-opt (mapcar 'car org-tag-groups-alist
))
6264 '(1 'org-tag-group prepend
)))
6266 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
6267 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
6268 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
6269 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
6272 (if (featurep 'xemacs
)
6273 '(org-do-emphasis-faces (0 nil append
))
6274 '(org-do-emphasis-faces)))
6276 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
6277 1 'org-checkbox prepend
)
6278 (if (cdr (assq 'checkbox org-list-automatic-rules
))
6279 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
6280 (0 (org-get-checkbox-statistics-face) t
)))
6281 ;; Description list items
6282 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
6283 1 'org-list-dt prepend
)
6284 ;; ARCHIVEd headings
6286 org-outline-regexp-bol
6287 "\\(.*:" org-archive-tag
":.*\\)")
6288 '(1 'org-archived prepend
))
6290 '(org-do-latex-and-related)
6291 '(org-fontify-entities)
6292 '(org-raise-scripts)
6294 '(org-activate-code (1 'org-code t
))
6296 (list (format org-heading-keyword-regexp-format
6298 org-comment-string
"\\|" org-quote-string
6300 '(2 'org-special-keyword t
))
6301 ;; Blocks and meta lines
6302 '(org-fontify-meta-lines-and-blocks))))
6303 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
6304 (run-hooks 'org-font-lock-set-keywords-hook
)
6305 ;; Now set the full font-lock-keywords
6306 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
6307 (org-set-local 'font-lock-defaults
6308 '(org-font-lock-keywords t nil nil backward-paragraph
))
6309 (kill-local-variable 'font-lock-keywords
) nil
))
6311 (defun org-toggle-pretty-entities ()
6312 "Toggle the composition display of entities as UTF8 characters."
6314 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
6315 (org-restart-font-lock)
6316 (if org-pretty-entities
6317 (message "Entities are now displayed as UTF8 characters")
6320 (org-decompose-region (point-min) (point-max))
6321 (message "Entities are now displayed as plain text"))))
6323 (defvar org-custom-properties-overlays nil
6324 "List of overlays used for custom properties.")
6325 (make-variable-buffer-local 'org-custom-properties-overlays
)
6327 (defun org-toggle-custom-properties-visibility ()
6328 "Display or hide properties in `org-custom-properties'."
6330 (if org-custom-properties-overlays
6331 (progn (mapc 'delete-overlay org-custom-properties-overlays
)
6332 (setq org-custom-properties-overlays nil
))
6333 (unless (not org-custom-properties
)
6337 (goto-char (point-min))
6338 (while (re-search-forward org-property-re nil t
)
6340 (when (equal p
(substring (match-string 1) 1 -
1))
6341 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
6342 (overlay-put o
'invisible t
)
6343 (overlay-put o
'org-custom-property t
)
6344 (push o org-custom-properties-overlays
))))
6345 org-custom-properties
)))))))
6347 (defun org-fontify-entities (limit)
6348 "Find an entity to fontify."
6350 (when org-pretty-entities
6352 (while (re-search-forward
6353 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
6355 (if (and (not (org-in-indented-comment-line))
6356 (setq ee
(org-entity-get (match-string 1)))
6357 (= (length (nth 6 ee
)) 1))
6359 ((end (if (equal (match-string 2) "{}")
6362 (add-text-properties
6363 (match-beginning 0) end
6364 (list 'font-lock-fontified t
))
6365 (compose-region (match-beginning 0) end
6371 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
6372 "Fontify string S like in Org-mode."
6375 (let ((org-odd-levels-only odd-levels
))
6377 (org-font-lock-ensure)
6383 (defun org-get-level-face (n)
6384 "Get the right face for match N in font-lock matching of headlines."
6385 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
6386 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
6387 (if org-cycle-level-faces
6388 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
6389 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
6391 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
6393 (t (if org-level-color-stars-only nil org-f
))))
6396 (defun org-get-todo-face (kwd)
6397 "Get the right face for a TODO keyword KWD.
6398 If KWD is a number, get the corresponding match group."
6399 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6400 (or (org-face-from-face-or-color
6401 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
6402 (and (member kwd org-done-keywords
) 'org-done
)
6405 (defun org-face-from-face-or-color (context inherit face-or-color
)
6406 "Create a face list that inherits INHERIT, but sets the foreground color.
6407 When FACE-OR-COLOR is not a string, just return it."
6408 (if (stringp face-or-color
)
6409 (list :inherit inherit
6410 (cdr (assoc context org-faces-easy-properties
))
6414 (defun org-font-lock-add-tag-faces (limit)
6415 "Add the special tag faces."
6416 (when (and org-tag-faces org-tags-special-faces-re
)
6417 (while (re-search-forward org-tags-special-faces-re limit t
)
6418 (add-text-properties (match-beginning 1) (match-end 1)
6419 (list 'face
(org-get-tag-face 1)
6420 'font-lock-fontified t
))
6421 (backward-char 1))))
6423 (defun org-font-lock-add-priority-faces (limit)
6424 "Add the special priority faces."
6425 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
6426 (when (save-match-data (org-at-heading-p))
6427 (add-text-properties
6428 (match-beginning 0) (match-end 0)
6429 (list 'face
(or (org-face-from-face-or-color
6430 'priority
'org-priority
6431 (cdr (assoc (char-after (match-beginning 1))
6432 org-priority-faces
)))
6434 'font-lock-fontified t
)))))
6436 (defun org-get-tag-face (kwd)
6437 "Get the right face for a TODO keyword KWD.
6438 If KWD is a number, get the corresponding match group."
6439 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6440 (or (org-face-from-face-or-color
6441 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
6444 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
6445 "Remove fontification and activation overlays from links."
6446 (font-lock-default-unfontify-region beg end
)
6447 (let* ((buffer-undo-list t
)
6448 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
6449 (inhibit-modification-hooks t
)
6450 deactivate-mark buffer-file-name buffer-file-truename
)
6451 (org-decompose-region beg end
)
6452 (remove-text-properties beg end
6453 '(mouse-face t keymap t org-linked-text t
6454 invisible t intangible t
6455 org-no-flyspell t org-emphasis t
))
6456 (org-remove-font-lock-display-properties beg end
)))
6458 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
6459 ((raise 0.3) (height 0.7))
6462 "Display properties for showing superscripts and subscripts.")
6464 (defun org-remove-font-lock-display-properties (beg end
)
6465 "Remove specific display properties that have been added by font lock.
6466 The will remove the raise properties that are used to show superscripts
6470 (setq next
(next-single-property-change beg
'display nil end
)
6471 prop
(get-text-property beg
'display
))
6472 (if (member prop org-script-display
)
6473 (put-text-property beg next
'display nil
))
6476 (defun org-raise-scripts (limit)
6477 "Add raise properties to sub/superscripts."
6478 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6479 (if (re-search-forward
6480 (if (eq org-use-sub-superscripts t
)
6481 org-match-substring-regexp
6482 org-match-substring-with-braces-regexp
)
6484 (let* ((pos (point)) table-p comment-p
6485 (mpos (match-beginning 3))
6486 (emph-p (get-text-property mpos
'org-emphasis
))
6487 (link-p (get-text-property mpos
'mouse-face
))
6488 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6489 (goto-char (point-at-bol))
6490 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6491 comment-p
(org-looking-at-p "^[ \t]*#[ +]"))
6494 (if (member (char-after) '(?_ ?^
)) (goto-char (1- pos
)))
6495 (if (or comment-p emph-p link-p keyw-p
)
6497 (put-text-property (match-beginning 3) (match-end 0)
6499 (if (equal (char-after (match-beginning 2)) ?^
)
6500 (nth (if table-p
3 1) org-script-display
)
6501 (nth (if table-p
2 0) org-script-display
)))
6502 (add-text-properties (match-beginning 2) (match-end 2)
6504 'org-dwidth t
'org-dwidth-n
1))
6505 (if (and (eq (char-after (match-beginning 3)) ?
{)
6506 (eq (char-before (match-end 3)) ?
}))
6508 (add-text-properties
6509 (match-beginning 3) (1+ (match-beginning 3))
6510 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6511 (add-text-properties
6512 (1- (match-end 3)) (match-end 3)
6513 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6516 ;;;; Visibility cycling, including org-goto and indirect buffer
6520 (defvar org-cycle-global-status nil
)
6521 (make-variable-buffer-local 'org-cycle-global-status
)
6522 (put 'org-cycle-global-status
'org-state t
)
6523 (defvar org-cycle-subtree-status nil
)
6524 (make-variable-buffer-local 'org-cycle-subtree-status
)
6525 (put 'org-cycle-subtree-status
'org-state t
)
6527 (defvar org-inlinetask-min-level
)
6529 (defun org-unlogged-message (&rest args
)
6530 "Display a message, but avoid logging it in the *Messages* buffer."
6531 (let ((message-log-max nil
))
6532 (apply 'message args
)))
6535 (defun org-cycle (&optional arg
)
6536 "TAB-action and visibility cycling for Org-mode.
6538 This is the command invoked in Org-mode by the TAB key. Its main purpose
6539 is outline visibility cycling, but it also invokes other actions
6540 in special contexts.
6542 - When this function is called with a prefix argument, rotate the entire
6543 buffer through 3 states (global cycling)
6544 1. OVERVIEW: Show only top-level headlines.
6545 2. CONTENTS: Show all headlines of all levels, but no body text.
6546 3. SHOW ALL: Show everything.
6547 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6548 determined by the variable `org-startup-folded', and by any VISIBILITY
6549 properties in the buffer.
6550 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6551 including any drawers.
6553 - When inside a table, re-align the table and move to the next field.
6555 - When point is at the beginning of a headline, rotate the subtree started
6556 by this line through 3 different states (local cycling)
6557 1. FOLDED: Only the main headline is shown.
6558 2. CHILDREN: The main headline and the direct children are shown.
6559 From this state, you can move to one of the children
6560 and zoom in further.
6561 3. SUBTREE: Show the entire subtree, including body text.
6562 If there is no subtree, switch directly from CHILDREN to FOLDED.
6564 - When point is at the beginning of an empty headline and the variable
6565 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6566 of the headline by demoting and promoting it to likely levels. This
6567 speeds up creation document structure by pressing TAB once or several
6568 times right after creating a new headline.
6570 - When there is a numeric prefix, go up to a heading with level ARG, do
6571 a `show-subtree' and return to the previous cursor position. If ARG
6572 is negative, go up that many levels.
6574 - When point is not at the beginning of a headline, execute the global
6575 binding for TAB, which is re-indenting the line. See the option
6576 `org-cycle-emulate-tab' for details.
6578 - Special case: if point is at the beginning of the buffer and there is
6579 no headline in line 1, this function will act as if called with prefix arg
6580 (C-u TAB, same as S-TAB) also when called without prefix arg.
6581 But only if also the variable `org-cycle-global-at-bob' is t."
6583 (org-load-modules-maybe)
6584 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6585 (and org-cycle-level-after-item
/entry-creation
6586 (or (org-cycle-level)
6587 (org-cycle-item-indentation))))
6589 (or org-cycle-max-level
6590 (and (boundp 'org-inlinetask-min-level
)
6591 org-inlinetask-min-level
6592 (1- org-inlinetask-min-level
))))
6593 (nstars (and limit-level
6594 (if org-odd-levels-only
6595 (and limit-level
(1- (* limit-level
2)))
6598 (if (not (derived-mode-p 'org-mode
))
6600 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6601 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6602 (not (looking-at org-outline-regexp
))))
6605 (delq 'org-optimize-window-after-visibility-change
6606 (copy-sequence org-cycle-hook
))
6610 (if (or bob-special
(equal arg
'(4)))
6611 ;; special case: use global cycling
6617 (setq last-command
'dummy
)
6618 (org-set-startup-visibility)
6619 (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
6623 (org-unlogged-message "Entire buffer visible, including drawers"))
6625 ;; Try cdlatex TAB completion
6626 ((org-try-cdlatex-tab))
6628 ;; Table: enter it or move to the next field.
6629 ((org-at-table-p 'any
)
6630 (if (org-at-table.el-p
)
6631 (message "%s" "Use C-c ' to edit table.el tables")
6632 (if arg
(org-table-edit-field t
)
6633 (org-table-justify-field-maybe)
6634 (call-interactively 'org-table-next-field
))))
6636 ((run-hook-with-args-until-success
6637 'org-tab-after-check-for-table-hook
))
6639 ;; Global cycling: delegate to `org-cycle-internal-global'.
6640 ((eq arg t
) (org-cycle-internal-global))
6642 ;; Drawers: delegate to `org-flag-drawer'.
6643 ((and org-drawers org-drawer-regexp
6645 (beginning-of-line 1)
6646 (looking-at org-drawer-regexp
)))
6647 (org-flag-drawer ; toggle block visibility
6648 (not (get-char-property (match-end 0) 'invisible
))))
6650 ;; Show-subtree, ARG levels up from here.
6653 (org-back-to-heading)
6654 (outline-up-heading (if (< arg
0) (- arg
)
6655 (- (funcall outline-level
) arg
)))
6656 (org-show-subtree)))
6658 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6659 ((and (featurep 'org-inlinetask
)
6660 (org-inlinetask-at-task-p)
6661 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6662 (org-inlinetask-toggle-visibility))
6664 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6665 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6666 (save-excursion (beginning-of-line 1)
6667 (looking-at org-outline-regexp
)))
6668 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6669 (org-cycle-internal-local))
6671 ;; From there: TAB emulation and template completion.
6672 (buffer-read-only (org-back-to-heading))
6674 ((run-hook-with-args-until-success
6675 'org-tab-after-check-for-cycling-hook
))
6677 ((org-try-structure-completion))
6679 ((run-hook-with-args-until-success
6680 'org-tab-before-tab-emulation-hook
))
6682 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6684 (not (looking-at org-outline-regexp
))))
6685 (call-interactively (global-key-binding "\t")))
6687 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6688 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6689 (or (and (eq org-cycle-emulate-tab
'white
)
6690 (= (match-end 0) (point-at-eol)))
6691 (and (eq org-cycle-emulate-tab
'whitestart
)
6692 (>= (match-end 0) pos
))))
6694 (eq org-cycle-emulate-tab t
))
6695 (call-interactively (global-key-binding "\t")))
6698 (org-back-to-heading)
6701 (defun org-cycle-internal-global ()
6702 "Do the global cycling action."
6703 ;; Hack to avoid display of messages for .org attachments in Gnus
6704 (let ((ga (string-match "\\*fontification" (buffer-name))))
6706 ((and (eq last-command this-command
)
6707 (eq org-cycle-global-status
'overview
))
6708 ;; We just created the overview - now do table of contents
6709 ;; This can be slow in very large buffers, so indicate action
6710 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6711 (unless ga
(org-unlogged-message "CONTENTS..."))
6713 (unless ga
(org-unlogged-message "CONTENTS...done"))
6714 (setq org-cycle-global-status
'contents
)
6715 (run-hook-with-args 'org-cycle-hook
'contents
))
6717 ((and (eq last-command this-command
)
6718 (eq org-cycle-global-status
'contents
))
6719 ;; We just showed the table of contents - now show everything
6720 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6722 (unless ga
(org-unlogged-message "SHOW ALL"))
6723 (setq org-cycle-global-status
'all
)
6724 (run-hook-with-args 'org-cycle-hook
'all
))
6727 ;; Default action: go to overview
6728 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6730 (unless ga
(org-unlogged-message "OVERVIEW"))
6731 (setq org-cycle-global-status
'overview
)
6732 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6734 (defvar org-called-with-limited-levels nil
6735 "Non-nil when `org-with-limited-levels' is currently active.")
6737 (defun org-cycle-internal-local ()
6738 "Do the local cycling action."
6739 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6740 ;; First, determine end of headline (EOH), end of subtree or item
6741 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6746 (setq struct
(org-list-struct))
6747 (setq eoh
(point-at-eol))
6748 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6749 (setq has-children
(org-list-has-child-p (point) struct
)))
6750 (org-back-to-heading)
6751 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6752 (setq eos
(save-excursion (org-end-of-subtree t t
)
6753 (when (bolp) (backward-char)) (point)))
6756 (let ((level (funcall outline-level
)))
6757 (outline-next-heading)
6758 (and (org-at-heading-p t
)
6759 (> (funcall outline-level
) level
))))
6761 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6762 ;; Determine end invisible part of buffer (EOL)
6763 (beginning-of-line 2)
6764 ;; XEmacs doesn't have `next-single-char-property-change'
6765 (if (featurep 'xemacs
)
6766 (while (and (not (eobp)) ;; this is like `next-line'
6767 (get-char-property (1- (point)) 'invisible
))
6768 (beginning-of-line 2))
6769 (while (and (not (eobp)) ;; this is like `next-line'
6770 (get-char-property (1- (point)) 'invisible
))
6771 (goto-char (next-single-char-property-change (point) 'invisible
))
6772 (and (eolp) (beginning-of-line 2))))
6774 ;; Find out what to do next and set `this-command'
6777 ;; Nothing is hidden behind this heading
6778 (unless (org-before-first-heading-p)
6779 (run-hook-with-args 'org-pre-cycle-hook
'empty
))
6780 (org-unlogged-message "EMPTY ENTRY")
6781 (setq org-cycle-subtree-status nil
)
6784 (outline-next-heading)
6785 (if (outline-invisible-p) (org-flag-heading nil
))))
6786 ((and (or (>= eol eos
)
6787 (not (string-match "\\S-" (buffer-substring eol eos
))))
6789 (not (setq children-skipped
6790 org-cycle-skip-children-state-if-no-children
))))
6791 ;; Entire subtree is hidden in one line: children view
6792 (unless (org-before-first-heading-p)
6793 (run-hook-with-args 'org-pre-cycle-hook
'children
))
6795 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6797 (org-with-limited-levels (show-children))
6798 ;; FIXME: This slows down the func way too much.
6799 ;; How keep drawers hidden in subtree anyway?
6800 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6801 ;; (org-cycle-hide-drawers 'subtree))
6803 ;; Fold every list in subtree to top-level items.
6804 (when (eq org-cycle-include-plain-lists
'integrate
)
6806 (org-back-to-heading)
6807 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6808 (beginning-of-line 1)
6809 (let* ((struct (org-list-struct))
6810 (prevs (org-list-prevs-alist struct
))
6811 (end (org-list-get-bottom-point struct
)))
6812 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6813 (org-list-get-all-items (point) struct prevs
))
6814 (goto-char (if (< end eos
) end eos
)))))))
6815 (org-unlogged-message "CHILDREN")
6818 (outline-next-heading)
6819 (if (outline-invisible-p) (org-flag-heading nil
)))
6820 (setq org-cycle-subtree-status
'children
)
6821 (unless (org-before-first-heading-p)
6822 (run-hook-with-args 'org-cycle-hook
'children
)))
6823 ((or children-skipped
6824 (and (eq last-command this-command
)
6825 (eq org-cycle-subtree-status
'children
)))
6826 ;; We just showed the children, or no children are there,
6827 ;; now show everything.
6828 (unless (org-before-first-heading-p)
6829 (run-hook-with-args 'org-pre-cycle-hook
'subtree
))
6830 (outline-flag-region eoh eos nil
)
6831 (org-unlogged-message
6832 (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6833 (setq org-cycle-subtree-status
'subtree
)
6834 (unless (org-before-first-heading-p)
6835 (run-hook-with-args 'org-cycle-hook
'subtree
)))
6837 ;; Default action: hide the subtree.
6838 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6839 (outline-flag-region eoh eos t
)
6840 (org-unlogged-message "FOLDED")
6841 (setq org-cycle-subtree-status
'folded
)
6842 (unless (org-before-first-heading-p)
6843 (run-hook-with-args 'org-cycle-hook
'folded
))))))
6846 (defun org-global-cycle (&optional arg
)
6847 "Cycle the global visibility. For details see `org-cycle'.
6848 With \\[universal-argument] prefix arg, switch to startup visibility.
6849 With a numeric prefix, show all headlines up to that level."
6851 (let ((org-cycle-include-plain-lists
6852 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6856 (hide-sublevels arg
)
6857 (setq org-cycle-global-status
'contents
))
6859 (org-set-startup-visibility)
6860 (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
6862 (org-cycle '(4))))))
6864 (defun org-set-startup-visibility ()
6865 "Set the visibility required by startup options and properties."
6867 ((eq org-startup-folded t
)
6869 ((eq org-startup-folded
'content
)
6871 ((or (eq org-startup-folded
'showeverything
)
6872 (eq org-startup-folded nil
))
6874 (unless (eq org-startup-folded
'showeverything
)
6875 (if org-hide-block-startup
(org-hide-block-all))
6876 (org-set-visibility-according-to-property 'no-cleanup
)
6877 (org-cycle-hide-archived-subtrees 'all
)
6878 (org-cycle-hide-drawers 'all
)
6879 (org-cycle-show-empty-lines t
)))
6881 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6882 "Switch subtree visibilities according to :VISIBILITY: property."
6884 (let (org-show-entry-below state
)
6886 (goto-char (point-min))
6887 (while (re-search-forward
6888 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6890 (setq state
(match-string 1))
6892 (org-back-to-heading t
)
6896 ((equal state
'("fold" "folded"))
6898 ((equal state
"children")
6899 (org-show-hidden-entry)
6901 ((equal state
"content")
6904 (org-narrow-to-subtree)
6906 ((member state
'("all" "showall"))
6909 (org-cycle-hide-archived-subtrees 'all
)
6910 (org-cycle-hide-drawers 'all
)
6911 (org-cycle-show-empty-lines 'all
)))))
6913 ;; This function uses outline-regexp instead of the more fundamental
6914 ;; org-outline-regexp so that org-cycle-global works outside of Org
6915 ;; buffers, where outline-regexp is needed.
6916 (defun org-overview ()
6917 "Switch to overview mode, showing only top-level headlines.
6918 This shows all headlines with a level equal or greater than the level
6919 of the first headline in the buffer. This is important, because if the
6920 first headline is not level one, then (hide-sublevels 1) gives confusing
6926 (goto-char (point-min))
6927 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6929 (goto-char (match-beginning 0))
6930 (funcall outline-level
))))))
6931 (and level
(hide-sublevels level
)))))
6933 (defun org-content (&optional arg
)
6934 "Show all headlines in the buffer, like a table of contents.
6935 With numerical argument N, show content up to level N."
6939 ;; Visit all headings and show their offspring
6940 (and (integerp arg
) (org-overview))
6941 (goto-char (point-max))
6943 (while (and (progn (condition-case nil
6944 (outline-previous-visible-heading 1)
6945 (error (goto-char (point-min))))
6947 (looking-at org-outline-regexp
))
6949 (show-children (1- arg
))
6951 (if (bobp) (throw 'exit nil
))))))
6953 (defun org-optimize-window-after-visibility-change (state)
6954 "Adjust the window after a change in outline visibility.
6955 This function is the default value of the hook `org-cycle-hook'."
6956 (when (get-buffer-window (current-buffer))
6958 ((eq state
'content
) nil
)
6959 ((eq state
'all
) nil
)
6960 ((eq state
'folded
) nil
)
6961 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6962 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6964 (defun org-remove-empty-overlays-at (pos)
6965 "Remove outline overlays that do not contain non-white stuff."
6968 (and (eq 'outline
(overlay-get o
'invisible
))
6969 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6971 (delete-overlay o
)))
6974 (defun org-clean-visibility-after-subtree-move ()
6975 "Fix visibility issues after moving a subtree."
6976 ;; First, find a reasonable region to look at:
6977 ;; Start two siblings above, end three below
6978 (let* ((beg (save-excursion
6979 (and (org-get-last-sibling)
6980 (org-get-last-sibling))
6982 (end (save-excursion
6983 (and (org-get-next-sibling)
6984 (org-get-next-sibling)
6985 (org-get-next-sibling))
6986 (if (org-at-heading-p)
6989 (level (looking-at "\\*+"))
6990 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6993 (narrow-to-region beg end
)
6995 ;; Properly fold already folded siblings
6996 (goto-char (point-min))
6997 (while (re-search-forward re nil t
)
6998 (if (and (not (outline-invisible-p))
7000 (goto-char (point-at-eol)) (outline-invisible-p)))
7002 (org-cycle-show-empty-lines 'overview
)
7003 (org-cycle-hide-drawers 'overview
)))))
7005 (defun org-cycle-show-empty-lines (state)
7006 "Show empty lines above all visible headlines.
7007 The region to be covered depends on STATE when called through
7008 `org-cycle-hook'. Lisp program can use t for STATE to get the
7009 entire buffer covered. Note that an empty line is only shown if there
7010 are at least `org-cycle-separator-lines' empty lines before the headline."
7011 (when (not (= org-cycle-separator-lines
0))
7013 (let* ((n (abs org-cycle-separator-lines
))
7015 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
7016 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
7017 (t (let ((ns (number-to-string (- n
2))))
7018 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
7019 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
7022 ((memq state
'(overview contents t
))
7023 (setq beg
(point-min) end
(point-max)))
7024 ((memq state
'(children folded
))
7025 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
7026 (beginning-of-line 2)
7030 (while (re-search-forward re end t
)
7031 (unless (get-char-property (match-end 1) 'invisible
)
7032 (setq e
(match-end 1))
7033 (if (< org-cycle-separator-lines
0)
7034 (setq b
(save-excursion
7035 (goto-char (match-beginning 0))
7036 (org-back-over-empty-lines)
7038 (goto-char (max (point-min) (1- (point))))
7042 (setq b
(match-beginning 1)))
7043 (outline-flag-region b e nil
)))))))
7044 ;; Never hide empty lines at the end of the file.
7046 (goto-char (point-max))
7047 (outline-previous-heading)
7048 (outline-end-of-heading)
7049 (if (and (looking-at "[ \t\n]+")
7050 (= (match-end 0) (point-max)))
7051 (outline-flag-region (point) (match-end 0) nil
))))
7053 (defun org-show-empty-lines-in-parent ()
7054 "Move to the parent and re-show empty lines before visible headlines."
7056 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
7057 (org-cycle-show-empty-lines context
))))
7059 (defun org-files-list ()
7060 "Return `org-agenda-files' list, plus all open org-mode files.
7061 This is useful for operations that need to scan all of a user's
7062 open and agenda-wise Org files."
7063 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
7064 (dolist (buf (buffer-list))
7065 (with-current-buffer buf
7066 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
7067 (let ((file (expand-file-name (buffer-file-name))))
7068 (unless (member file files
)
7069 (push file files
))))))
7072 (defsubst org-entry-beginning-position
()
7073 "Return the beginning position of the current entry."
7074 (save-excursion (outline-back-to-heading t
) (point)))
7076 (defsubst org-entry-end-position
()
7077 "Return the end position of the current entry."
7078 (save-excursion (outline-next-heading) (point)))
7080 (defun org-cycle-hide-drawers (state)
7081 "Re-hide all drawers after a visibility state change."
7082 (when (and (derived-mode-p 'org-mode
)
7083 (not (memq state
'(overview folded contents
))))
7085 (let* ((globalp (memq state
'(contents all
)))
7086 (beg (if globalp
(point-min) (point)))
7087 (end (if globalp
(point-max)
7088 (if (eq state
'children
)
7089 (save-excursion (outline-next-heading) (point))
7090 (org-end-of-subtree t
)))))
7092 (while (re-search-forward org-drawer-regexp end t
)
7093 (org-flag-drawer t
))))))
7095 (defun org-cycle-hide-inline-tasks (state)
7096 "Re-hide inline tasks when switching to `contents' or `children'
7100 (when (org-bound-and-true-p org-inlinetask-min-level
)
7101 (hide-sublevels (1- org-inlinetask-min-level
))))
7103 (when (featurep 'org-inlinetask
)
7105 (while (and (outline-next-heading)
7106 (org-inlinetask-at-task-p))
7107 (org-inlinetask-toggle-visibility)
7108 (org-inlinetask-goto-end)))))))
7110 (defun org-flag-drawer (flag)
7111 "When FLAG is non-nil, hide the drawer we are within.
7112 Otherwise make it visible."
7114 (beginning-of-line 1)
7115 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
7116 (let ((b (match-end 0)))
7117 (if (re-search-forward
7119 (save-excursion (outline-next-heading) (point)) t
)
7120 (outline-flag-region b
(point-at-eol) flag
)
7121 (user-error ":END: line missing at position %s" b
))))))
7123 (defun org-subtree-end-visible-p ()
7124 "Is the end of the current subtree visible?"
7125 (pos-visible-in-window-p
7126 (save-excursion (org-end-of-subtree t
) (point))))
7128 (defun org-first-headline-recenter ()
7129 "Move cursor to the first headline and recenter the headline."
7130 (goto-char (point-min))
7131 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
7132 (set-window-start (selected-window) (point-at-bol))))
7134 ;;; Saving and restoring visibility
7136 (defun org-outline-overlay-data (&optional use-markers
)
7137 "Return a list of the locations of all outline overlays.
7138 These are overlays with the `invisible' property value `outline'.
7139 The return value is a list of cons cells, with start and stop
7140 positions for each overlay.
7141 If USE-MARKERS is set, return the positions as markers."
7148 (when (eq (overlay-get o
'invisible
) 'outline
)
7149 (setq beg
(overlay-start o
)
7150 end
(overlay-end o
))
7151 (and beg end
(> end beg
)
7153 (cons (move-marker (make-marker) beg
)
7154 (move-marker (make-marker) end
))
7156 (overlays-in (point-min) (point-max))))))))
7158 (defun org-set-outline-overlay-data (data)
7159 "Create visibility overlays for all positions in DATA.
7160 DATA should have been made by `org-outline-overlay-data'."
7167 (outline-flag-region (car c
) (cdr c
) t
))
7170 ;;; Folding of blocks
7172 (defvar org-hide-block-overlays nil
7173 "Overlays hiding blocks.")
7174 (make-variable-buffer-local 'org-hide-block-overlays
)
7176 (defun org-block-map (function &optional start end
)
7177 "Call FUNCTION at the head of all source blocks in the current buffer.
7178 Optional arguments START and END can be used to limit the range."
7179 (let ((start (or start
(point-min)))
7180 (end (or end
(point-max))))
7183 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
7186 (goto-char (match-beginning 0))
7187 (funcall function
)))))))
7189 (defun org-hide-block-toggle-all ()
7190 "Toggle the visibility of all blocks in the current buffer."
7191 (org-block-map #'org-hide-block-toggle
))
7193 (defun org-hide-block-all ()
7194 "Fold all blocks in the current buffer."
7196 (org-show-block-all)
7197 (org-block-map #'org-hide-block-toggle-maybe
))
7199 (defun org-show-block-all ()
7200 "Unfold all blocks in the current buffer."
7202 (mapc 'delete-overlay org-hide-block-overlays
)
7203 (setq org-hide-block-overlays nil
))
7205 (defun org-hide-block-toggle-maybe ()
7206 "Toggle visibility of block at point."
7208 (let ((case-fold-search t
))
7210 (beginning-of-line 1)
7211 (looking-at org-block-regexp
))
7212 (progn (org-hide-block-toggle)
7213 t
) ;; to signal that we took action
7214 nil
))) ;; to signal that we did not
7216 (defun org-hide-block-toggle (&optional force
)
7217 "Toggle the visibility of the current block."
7221 (if (re-search-forward org-block-regexp nil t
)
7222 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
7223 (end (match-end 0)) ;; end of entire body
7225 (if (memq t
(mapcar (lambda (overlay)
7226 (eq (overlay-get overlay
'invisible
)
7228 (overlays-at start
)))
7229 (if (or (not force
) (eq force
'off
))
7231 (when (member ov org-hide-block-overlays
)
7232 (setq org-hide-block-overlays
7233 (delq ov org-hide-block-overlays
)))
7234 (when (eq (overlay-get ov
'invisible
)
7236 (delete-overlay ov
)))
7237 (overlays-at start
)))
7238 (setq ov
(make-overlay start end
))
7239 (overlay-put ov
'invisible
'org-hide-block
)
7240 ;; make the block accessible to isearch
7242 ov
'isearch-open-invisible
7244 (when (member ov org-hide-block-overlays
)
7245 (setq org-hide-block-overlays
7246 (delq ov org-hide-block-overlays
)))
7247 (when (eq (overlay-get ov
'invisible
)
7249 (delete-overlay ov
))))
7250 (push ov org-hide-block-overlays
)))
7251 (user-error "Not looking at a source block"))))
7253 ;; org-tab-after-check-for-cycling-hook
7254 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
7255 ;; Remove overlays when changing major mode
7256 (add-hook 'org-mode-hook
7257 (lambda () (org-add-hook 'change-major-mode-hook
7258 'org-show-block-all
'append
'local
)))
7262 (defvar org-goto-window-configuration nil
)
7263 (defvar org-goto-marker nil
)
7264 (defvar org-goto-map
)
7265 (defun org-goto-map ()
7266 "Set the keymap `org-goto'."
7268 (let ((map (make-sparse-keymap)))
7269 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
7270 mouse-drag-region universal-argument org-occur
)))
7272 (substitute-key-definition cmd cmd map global-map
)))
7273 (suppress-keymap map
)
7274 (org-defkey map
"\C-m" 'org-goto-ret
)
7275 (org-defkey map
[(return)] 'org-goto-ret
)
7276 (org-defkey map
[(left)] 'org-goto-left
)
7277 (org-defkey map
[(right)] 'org-goto-right
)
7278 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
7279 (org-defkey map
"\C-i" 'org-cycle
)
7280 (org-defkey map
[(tab)] 'org-cycle
)
7281 (org-defkey map
[(down)] 'outline-next-visible-heading
)
7282 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
7283 (if org-goto-auto-isearch
7284 (if (fboundp 'define-key-after
)
7285 (define-key-after map
[t] 'org-goto-local-auto-isearch)
7287 (org-defkey map "q" 'org-goto-quit)
7288 (org-defkey map "n" 'outline-next-visible-heading)
7289 (org-defkey map "p" 'outline-previous-visible-heading)
7290 (org-defkey map "f" 'outline-forward-same-level)
7291 (org-defkey map "b" 'outline-backward-same-level)
7292 (org-defkey map "u" 'outline-up-heading))
7293 (org-defkey map "/" 'org-occur)
7294 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
7295 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
7296 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
7297 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
7298 (org-defkey map "\C-c\C-u" 'outline-up-heading)
7301 (defconst org-goto-help
7302 "Browse buffer copy, to find location or copy text.%s
7303 RET=jump to location C-g=quit and return to previous location
7304 [Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
7306 (defvar org-goto-start-pos) ; dynamically scoped parameter
7308 (defun org-goto (&optional alternative-interface)
7309 "Look up a different location in the current file, keeping current visibility.
7311 When you want look-up or go to a different location in a
7312 document, the fastest way is often to fold the entire buffer and
7313 then dive into the tree. This method has the disadvantage, that
7314 the previous location will be folded, which may not be what you
7317 This command works around this by showing a copy of the current
7318 buffer in an indirect buffer, in overview mode. You can dive
7319 into the tree in that copy, use org-occur and incremental search
7320 to find a location. When pressing RET or `Q', the command
7321 returns to the original buffer in which the visibility is still
7322 unchanged. After RET it will also jump to the location selected
7323 in the indirect buffer and expose the headline hierarchy above.
7325 With a prefix argument, use the alternative interface: e.g., if
7326 `org-goto-interface' is `outline' use `outline-path-completion'."
7329 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
7330 (org-refile-use-outline-path t)
7331 (org-refile-target-verify-function nil)
7333 (if (not alternative-interface)
7335 (if (eq org-goto-interface 'outline)
7336 'outline-path-completion
7338 (org-goto-start-pos (point))
7340 (if (eq interface 'outline)
7341 (car (org-get-location (current-buffer) org-goto-help))
7342 (let ((pa (org-refile-get-location "Goto" nil nil t)))
7343 (org-refile-check-position pa)
7347 (org-mark-ring-push org-goto-start-pos)
7348 (goto-char selected-point)
7349 (if (or (outline-invisible-p) (org-invisible-p2))
7350 (org-show-context 'org-goto)))
7353 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
7354 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
7355 (defvar org-goto-local-auto-isearch-map) ; defined below
7357 (defun org-get-location (buf help)
7358 "Let the user select a location in the Org-mode buffer BUF.
7359 This function uses a recursive edit. It returns the selected position
7362 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
7363 (isearch-hide-immediately nil)
7364 (isearch-search-fun-function
7365 (lambda () 'org-goto-local-search-headings))
7366 (org-goto-selected-point org-goto-exit-command))
7368 (save-window-excursion
7369 (delete-other-windows)
7370 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
7371 (org-pop-to-buffer-same-window
7373 (make-indirect-buffer (current-buffer) "*org-goto*")
7374 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
7375 (with-output-to-temp-buffer "*Org Help*"
7376 (princ (format help (if org-goto-auto-isearch
7377 " Just type for auto-isearch."
7378 " n/p/f/b/u to navigate, q to quit."))))
7379 (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
7380 (setq buffer-read-only nil)
7381 (let ((org-startup-truncated t)
7382 (org-startup-folded nil)
7383 (org-startup-align-all-tables nil))
7386 (setq buffer-read-only t)
7387 (if (and (boundp 'org-goto-start-pos)
7388 (integer-or-marker-p org-goto-start-pos))
7389 (let ((org-show-hierarchy-above t)
7390 (org-show-siblings t)
7391 (org-show-following-heading t))
7392 (goto-char org-goto-start-pos)
7393 (and (outline-invisible-p) (org-show-context)))
7394 (goto-char (point-min)))
7395 (let (org-special-ctrl-a/e) (org-beginning-of-line))
7396 (message "Select location and press RET")
7397 (use-local-map org-goto-map)
7399 (kill-buffer "*org-goto*")
7400 (cons org-goto-selected-point org-goto-exit-command))))
7402 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
7403 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
7404 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
7405 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
7407 (defun org-goto-local-search-headings (string bound noerror)
7408 "Search and make sure that any matches are in headlines."
7410 (while (if isearch-forward
7411 (search-forward string bound noerror)
7412 (search-backward string bound noerror))
7413 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
7414 (and (member :headline context)
7415 (not (member :tags context))))
7416 (throw 'return (point))))))
7418 (defun org-goto-local-auto-isearch ()
7421 (goto-char (point-min))
7422 (let ((keys (this-command-keys)))
7423 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
7425 (isearch-process-search-char (string-to-char keys)))))
7427 (defun org-goto-ret (&optional arg)
7428 "Finish `org-goto' by going to the new location."
7430 (setq org-goto-selected-point (point)
7431 org-goto-exit-command 'return)
7434 (defun org-goto-left ()
7435 "Finish `org-goto' by going to the new location."
7437 (if (org-at-heading-p)
7439 (beginning-of-line 1)
7440 (setq org-goto-selected-point (point)
7441 org-goto-exit-command 'left)
7443 (user-error "Not on a heading")))
7445 (defun org-goto-right ()
7446 "Finish `org-goto' by going to the new location."
7448 (if (org-at-heading-p)
7450 (setq org-goto-selected-point (point)
7451 org-goto-exit-command 'right)
7453 (user-error "Not on a heading")))
7455 (defun org-goto-quit ()
7456 "Finish `org-goto' without cursor motion."
7458 (setq org-goto-selected-point nil)
7459 (setq org-goto-exit-command 'quit)
7462 ;;; Indirect buffer display of subtrees
7464 (defvar org-indirect-dedicated-frame nil
7465 "This is the frame being used for indirect tree display.")
7466 (defvar org-last-indirect-buffer nil)
7468 (defun org-tree-to-indirect-buffer (&optional arg)
7469 "Create indirect buffer and narrow it to current subtree.
7470 With a numerical prefix ARG, go up to this level and then take that tree.
7471 If ARG is negative, go up that many levels.
7473 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7474 indirect buffer previously made with this command, to avoid proliferation of
7475 indirect buffers. However, when you call the command with a \
7476 \\[universal-argument] prefix, or
7477 when `org-indirect-buffer-display' is `new-frame', the last buffer
7478 is kept so that you can work with several indirect buffers at the same time.
7479 If `org-indirect-buffer-display' is `dedicated-frame', the \
7480 \\[universal-argument] prefix also
7481 requests that a new frame be made for the new buffer, so that the dedicated
7482 frame is not changed."
7484 (let ((cbuf (current-buffer))
7485 (cwin (selected-window))
7487 beg end level heading ibuf)
7489 (org-back-to-heading t)
7491 (setq level (org-outline-level))
7492 (if (< arg 0) (setq arg (+ level arg)))
7493 (while (> (setq level (org-outline-level)) arg)
7494 (org-up-heading-safe)))
7496 heading (org-get-heading))
7497 (org-end-of-subtree t t)
7498 (if (org-at-heading-p) (backward-char 1))
7500 (if (and (buffer-live-p org-last-indirect-buffer)
7501 (not (eq org-indirect-buffer-display 'new-frame))
7503 (kill-buffer org-last-indirect-buffer))
7504 (setq ibuf (org-get-indirect-buffer cbuf)
7505 org-last-indirect-buffer ibuf)
7507 ((or (eq org-indirect-buffer-display 'new-frame)
7508 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7509 (select-frame (make-frame))
7510 (delete-other-windows)
7511 (org-pop-to-buffer-same-window ibuf)
7512 (org-set-frame-title heading))
7513 ((eq org-indirect-buffer-display 'dedicated-frame)
7515 (select-frame (or (and org-indirect-dedicated-frame
7516 (frame-live-p org-indirect-dedicated-frame)
7517 org-indirect-dedicated-frame)
7518 (setq org-indirect-dedicated-frame (make-frame)))))
7519 (delete-other-windows)
7520 (org-pop-to-buffer-same-window ibuf)
7521 (org-set-frame-title (concat "Indirect: " heading)))
7522 ((eq org-indirect-buffer-display 'current-window)
7523 (org-pop-to-buffer-same-window ibuf))
7524 ((eq org-indirect-buffer-display 'other-window)
7525 (pop-to-buffer ibuf))
7526 (t (error "Invalid value")))
7527 (if (featurep 'xemacs)
7528 (save-excursion (org-mode) (turn-on-font-lock)))
7529 (narrow-to-region beg end)
7532 (run-hook-with-args 'org-cycle-hook 'all)
7533 (and (window-live-p cwin) (select-window cwin))))
7535 (defun org-get-indirect-buffer (&optional buffer)
7536 (setq buffer (or buffer (current-buffer)))
7537 (let ((n 1) (base (buffer-name buffer)) bname)
7538 (while (buffer-live-p
7539 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7542 (make-indirect-buffer buffer bname 'clone)
7543 (error (make-indirect-buffer buffer bname)))))
7545 (defun org-set-frame-title (title)
7546 "Set the title of the current frame to the string TITLE."
7547 ;; FIXME: how to name a single frame in XEmacs???
7548 (unless (featurep 'xemacs)
7549 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7551 ;;;; Structure editing
7553 ;;; Inserting headlines
7555 (defun org-previous-line-empty-p (&optional next)
7556 "Is the previous line a blank line?
7557 When NEXT is non-nil, check the next line instead."
7560 (or (beginning-of-line (if next 2 0)) t)
7562 (looking-at "[ \t]*$")))))
7564 (defun org-insert-heading (&optional arg invisible-ok)
7565 "Insert a new heading or an item with the same depth at point.
7567 If point is at the beginning of a heading or a list item, insert
7568 a new heading or a new item above the current one. If point is
7569 at the beginning of a normal line, turn the line into a heading.
7571 If point is in the middle of a headline or a list item, split the
7572 headline or the item and create a new headline/item with the text
7573 in the current line after point \(see `org-M-RET-may-split-line'
7574 on how to modify this behavior).
7576 With one universal prefix argument, set the user option
7577 `org-insert-heading-respect-content' to t for the duration of
7578 the command. This modifies the behavior described above in this
7579 ways: on list items and at the beginning of normal lines, force
7580 the insertion of a heading after the current subtree.
7582 With two universal prefix arguments, insert the heading at the
7583 end of the grandparent subtree. For example, if point is within
7584 a 2nd-level heading, then it will insert a 2nd-level heading at
7585 the end of the 1st-level parent heading.
7587 When INVISIBLE-OK is set, stop at invisible headlines when going
7588 back. This is important for non-interactive uses of the
7591 (if (org-called-interactively-p 'any) (org-reveal))
7592 (let ((itemp (org-in-item-p))
7593 (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
7594 (respect-content (or org-insert-heading-respect-content
7596 (initial-content "")
7597 (adjust-empty-lines t))
7601 ((or (= (buffer-size) 0)
7602 (and (not (save-excursion
7603 (and (ignore-errors (org-back-to-heading invisible-ok))
7604 (org-at-heading-p))))
7605 (or arg (not itemp))))
7606 ;; At beginning of buffer or so high up that only a heading
7608 (cond ((and (bolp) (not respect-content)) (insert "* "))
7609 ((not respect-content)
7610 (unless may-split (end-of-line))
7612 ((re-search-forward org-outline-regexp-bol nil t)
7616 (t (goto-char (point-max))
7618 (run-hooks 'org-insert-heading-hook))
7620 ((and itemp (not (member arg '((4) (16)))))
7625 ;; Maybe move at the end of the subtree
7626 (when (equal arg '(16))
7627 (org-up-heading-safe)
7628 (org-end-of-subtree t))
7633 (on-heading (org-at-heading-p))
7634 (empty-line-p (if on-heading
7635 (org-previous-line-empty-p)
7636 ;; We will decide later
7638 ;; Get a level string to fall back on
7640 (if (org-before-first-heading-p) "*"
7642 (org-back-to-heading t)
7643 (if (org-previous-line-empty-p) (setq empty-line-p t))
7644 (looking-at org-outline-regexp)
7645 (make-string (1- (length (match-string 0))) ?*))))
7650 (org-back-to-heading invisible-ok)
7651 (when (and (not on-heading)
7652 (featurep 'org-inlinetask)
7653 (integerp org-inlinetask-min-level)
7654 (>= (length (match-string 0))
7655 org-inlinetask-min-level))
7656 ;; Find a heading level before the inline task
7657 (while (and (setq level (org-up-heading-safe))
7658 (>= level org-inlinetask-min-level)))
7659 (if (org-at-heading-p)
7660 (org-back-to-heading invisible-ok)
7661 (error "This should not happen")))
7662 (unless (and (save-excursion
7664 (org-backward-heading-same-level
7666 (= (point) (match-beginning 0)))
7667 (not (org-previous-line-empty-p t)))
7668 (setq empty-line-p (or empty-line-p
7669 (org-previous-line-empty-p))))
7671 (error (or fix-level "* ")))))
7672 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7673 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7674 pos hide-previous previous-pos)
7676 ;; If we insert after content, move there and clean up whitespace
7677 (when (and respect-content
7678 (not (org-looking-at-p org-outline-regexp-bol)))
7679 (if (not (org-before-first-heading-p))
7680 (org-end-of-subtree nil t)
7681 (re-search-forward org-outline-regexp-bol)
7682 (beginning-of-line 0))
7683 (skip-chars-backward " \r\n")
7684 (and (not (looking-back "^\\*+" (line-beginning-position)))
7685 (looking-at "[ \t]+") (replace-match ""))
7686 (unless (eobp) (forward-char 1))
7687 (when (looking-at "^\\*")
7688 (unless (bobp) (backward-char 1))
7691 ;; If we are splitting, grab the text that should be moved to the new headline
7693 (if (org-on-heading-p)
7694 ;; This is a heading, we split intelligently (keeping tags)
7695 (let ((pos (point)))
7696 (goto-char (point-at-bol))
7697 (unless (looking-at org-complex-heading-regexp)
7698 (error "This should not happen"))
7699 (when (and (match-beginning 4)
7700 (> pos (match-beginning 4))
7701 (< pos (match-end 4)))
7702 (setq initial-content (buffer-substring pos (match-end 4)))
7704 (delete-region (point) (match-end 4))
7705 (if (looking-at "[ \t]*$")
7707 (insert (make-string (length initial-content) ?\ )))
7708 (setq initial-content (org-trim initial-content)))
7711 (setq initial-content
7712 (org-trim (buffer-substring (point) (point-at-eol))))
7713 (delete-region (point) (point-at-eol))))
7715 ;; If we are at the beginning of the line, insert before it. Else after
7717 ((and (bolp) (looking-at "[ \t]*$")))
7718 ((and (bolp) (not (looking-at "[ \t]*$")))
7721 (goto-char (point-at-eol))
7724 ;; Insert the new heading
7727 (insert initial-content)
7728 (when adjust-empty-lines
7730 (and blank (not (org-previous-line-empty-p))))
7731 (org-N-empty-lines-before-current (if blank 1 0))))
7732 (run-hooks 'org-insert-heading-hook)))))))
7734 (defun org-N-empty-lines-before-current (N)
7735 "Make the number of empty lines before current exactly N.
7736 So this will delete or add empty lines."
7740 (skip-chars-backward " \r\t\n")
7741 (unless (bolp) (forward-line))
7742 (delete-region (point) p))
7743 (when (> N 0) (insert (make-string N ?\n)))))
7745 (defun org-get-heading (&optional no-tags no-todo)
7746 "Return the heading of the current entry, without the stars.
7747 When NO-TAGS is non-nil, don't include tags.
7748 When NO-TODO is non-nil, don't include TODO keywords."
7750 (org-back-to-heading t)
7752 ((and no-tags no-todo)
7753 (looking-at org-complex-heading-regexp)
7756 (looking-at (concat org-outline-regexp
7758 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7761 (looking-at org-todo-line-regexp)
7763 (t (looking-at org-heading-regexp)
7764 (match-string 2)))))
7766 (defvar orgstruct-mode) ; defined below
7768 (defun org-heading-components ()
7769 "Return the components of the current heading.
7770 This is a list with the following elements:
7771 - the level as an integer
7772 - the reduced level, different if `org-odd-levels-only' is set.
7773 - the TODO keyword, or nil
7774 - the priority character, like ?A, or nil if no priority is given
7775 - the headline text itself, or the tags string if no headline text
7776 - the tags string, or nil."
7778 (org-back-to-heading t)
7779 (if (let (case-fold-search)
7783 org-complex-heading-regexp)))
7785 (list (length (match-string 1))
7786 (org-reduced-level (length (match-string 1)))
7791 (list (length (match-string 1))
7792 (org-reduced-level (length (match-string 1)))
7793 (org-match-string-no-properties 2)
7794 (and (match-end 3) (aref (match-string 3) 2))
7795 (org-match-string-no-properties 4)
7796 (org-match-string-no-properties 5))))))
7798 (defun org-get-entry ()
7799 "Get the entry text, after heading, entire subtree."
7801 (org-back-to-heading t)
7802 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7804 (defun org-insert-heading-after-current ()
7805 "Insert a new heading with same level as current, after current subtree."
7807 (org-back-to-heading)
7808 (org-insert-heading)
7809 (org-move-subtree-down)
7812 (defun org-insert-heading-respect-content (&optional invisible-ok)
7813 "Insert heading with `org-insert-heading-respect-content' set to t."
7815 (org-insert-heading '(4) invisible-ok))
7817 (defun org-insert-todo-heading-respect-content (&optional force-state)
7818 "Insert TODO heading with `org-insert-heading-respect-content' set to t."
7820 (org-insert-todo-heading force-state '(4)))
7822 (defun org-insert-todo-heading (arg &optional force-heading)
7823 "Insert a new heading with the same level and TODO state as current heading.
7824 If the heading has no TODO state, or if the state is DONE, use the first
7825 state (TODO by default). Also with one prefix arg, force first state. With
7826 two prefix args, force inserting at the end of the parent subtree."
7828 (when (or force-heading (not (org-insert-item 'checkbox)))
7829 (org-insert-heading (or (and (equal arg '(16)) '(16))
7832 (org-back-to-heading)
7833 (outline-previous-heading)
7834 (looking-at org-todo-line-regexp))
7837 (if (or (equal arg '(4))
7838 (not (match-beginning 2))
7839 (member (match-string 2) org-done-keywords))
7840 (car org-todo-keywords-1)
7844 (run-hook-with-args-until-success
7845 'org-todo-get-default-hook new-mark-x nil)
7847 (beginning-of-line 1)
7848 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7849 (if org-treat-insert-todo-heading-as-state-change
7851 (insert new-mark " "))))
7852 (when org-provide-todo-statistics
7853 (org-update-parent-todo-statistics))))
7855 (defun org-insert-subheading (arg)
7856 "Insert a new subheading and demote it.
7857 Works for outline headings and for plain lists alike."
7859 (org-insert-heading arg)
7861 ((org-at-heading-p) (org-do-demote))
7862 ((org-at-item-p) (org-indent-item))))
7864 (defun org-insert-todo-subheading (arg)
7865 "Insert a new subheading with TODO keyword or checkbox and demote it.
7866 Works for outline headings and for plain lists alike."
7868 (org-insert-todo-heading arg)
7870 ((org-at-heading-p) (org-do-demote))
7871 ((org-at-item-p) (org-indent-item))))
7873 ;;; Promotion and Demotion
7875 (defvar org-after-demote-entry-hook nil
7876 "Hook run after an entry has been demoted.
7877 The cursor will be at the beginning of the entry.
7878 When a subtree is being demoted, the hook will be called for each node.")
7880 (defvar org-after-promote-entry-hook nil
7881 "Hook run after an entry has been promoted.
7882 The cursor will be at the beginning of the entry.
7883 When a subtree is being promoted, the hook will be called for each node.")
7885 (defun org-promote-subtree ()
7886 "Promote the entire subtree.
7887 See also `org-promote'."
7890 (org-with-limited-levels (org-map-tree 'org-promote)))
7891 (org-fix-position-after-promote))
7893 (defun org-demote-subtree ()
7894 "Demote the entire subtree. See `org-demote'.
7895 See also `org-promote'."
7898 (org-with-limited-levels (org-map-tree 'org-demote)))
7899 (org-fix-position-after-promote))
7902 (defun org-do-promote ()
7903 "Promote the current heading higher up the tree.
7904 If the region is active in `transient-mark-mode', promote all headings
7908 (if (org-region-active-p)
7909 (org-map-region 'org-promote (region-beginning) (region-end))
7911 (org-fix-position-after-promote))
7913 (defun org-do-demote ()
7914 "Demote the current heading lower down the tree.
7915 If the region is active in `transient-mark-mode', demote all headings
7919 (if (org-region-active-p)
7920 (org-map-region 'org-demote (region-beginning) (region-end))
7922 (org-fix-position-after-promote))
7924 (defun org-fix-position-after-promote ()
7925 "Make sure that after pro/demotion cursor position is right."
7926 (let ((pos (point)))
7927 (when (save-excursion
7928 (beginning-of-line 1)
7929 (looking-at org-todo-line-regexp)
7930 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7931 (cond ((eobp) (insert " "))
7932 ((eolp) (insert " "))
7933 ((equal (char-after) ?\ ) (forward-char 1))))))
7935 (defun org-current-level ()
7936 "Return the level of the current entry, or nil if before the first headline.
7937 The level is the number of stars at the beginning of the headline."
7939 (org-with-limited-levels
7940 (if (ignore-errors (org-back-to-heading t))
7941 (funcall outline-level)))))
7943 (defun org-get-previous-line-level ()
7944 "Return the outline depth of the last headline before the current line.
7945 Returns 0 for the first headline in the buffer, and nil if before the
7947 (and (org-current-level)
7948 (or (and (/= (line-beginning-position) (point-min))
7949 (save-excursion (beginning-of-line 0) (org-current-level)))
7952 (defun org-reduced-level (l)
7953 "Compute the effective level of a heading.
7954 This takes into account the setting of `org-odd-levels-only'."
7957 (org-odd-levels-only (1+ (floor (/ l 2))))
7960 (defun org-level-increment ()
7961 "Return the number of stars that will be added or removed at a
7962 time to headlines when structure editing, based on the value of
7963 `org-odd-levels-only'."
7964 (if org-odd-levels-only 2 1))
7966 (defun org-get-valid-level (level &optional change)
7967 "Rectify a level change under the influence of `org-odd-levels-only'
7968 LEVEL is a current level, CHANGE is by how much the level should be
7969 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7970 even level numbers will become the next higher odd number."
7971 (if org-odd-levels-only
7972 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7973 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7974 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7975 (max 1 (+ level (or change 0)))))
7977 (if (boundp 'define-obsolete-function-alias)
7978 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7979 (define-obsolete-function-alias 'org-get-legal-level
7980 'org-get-valid-level)
7981 (define-obsolete-function-alias 'org-get-legal-level
7982 'org-get-valid-level "23.1")))
7984 (defun org-promote ()
7985 "Promote the current heading higher up the tree.
7986 If the region is active in `transient-mark-mode', promote all headings
7988 (org-back-to-heading t)
7989 (let* ((level (save-match-data (funcall outline-level)))
7990 (after-change-functions (remove 'flyspell-after-change-function
7991 after-change-functions))
7992 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7993 (diff (abs (- level (length up-head) -1))))
7994 (cond ((and (= level 1) org-called-with-limited-levels
7995 org-allow-promoting-top-level-subtree)
7996 (replace-match "# " nil t))
7998 (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
7999 (t (replace-match up-head nil t)))
8000 ;; Fixup tag positioning
8002 (and org-auto-align-tags (org-set-tags nil 'ignore-column))
8003 (if org-adapt-indentation (org-fixup-indentation (- diff))))
8004 (run-hooks 'org-after-promote-entry-hook)))
8006 (defun org-demote ()
8007 "Demote the current heading lower down the tree.
8008 If the region is active in `transient-mark-mode', demote all headings
8010 (org-back-to-heading t)
8011 (let* ((level (save-match-data (funcall outline-level)))
8012 (after-change-functions (remove 'flyspell-after-change-function
8013 after-change-functions))
8014 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
8015 (diff (abs (- level (length down-head) -1))))
8016 (replace-match down-head nil t)
8017 ;; Fixup tag positioning
8018 (and org-auto-align-tags (org-set-tags nil 'ignore-column))
8019 (if org-adapt-indentation (org-fixup-indentation diff))
8020 (run-hooks 'org-after-demote-entry-hook)))
8022 (defun org-cycle-level ()
8023 "Cycle the level of an empty headline through possible states.
8024 This goes first to child, then to parent, level, then up the hierarchy.
8025 After top level, it switches back to sibling level."
8027 (let ((org-adapt-indentation nil))
8028 (when (org-point-at-end-of-empty-headline)
8029 (setq this-command 'org-cycle-level) ; Only needed for caching
8030 (let ((cur-level (org-current-level))
8031 (prev-level (org-get-previous-line-level)))
8033 ;; If first headline in file, promote to top-level.
8035 (loop repeat (/ (- cur-level 1) (org-level-increment))
8036 do (org-do-promote)))
8037 ;; If same level as prev, demote one.
8038 ((= prev-level cur-level)
8040 ;; If parent is top-level, promote to top level if not already.
8042 (loop repeat (/ (- cur-level 1) (org-level-increment))
8043 do (org-do-promote)))
8044 ;; If top-level, return to prev-level.
8046 (loop repeat (/ (- prev-level 1) (org-level-increment))
8047 do (org-do-demote)))
8048 ;; If less than prev-level, promote one.
8049 ((< cur-level prev-level)
8051 ;; If deeper than prev-level, promote until higher than
8053 ((> cur-level prev-level)
8054 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
8055 do (org-do-promote))))
8058 (defun org-map-tree (fun)
8059 "Call FUN for every heading underneath the current one."
8060 (org-back-to-heading)
8061 (let ((level (funcall outline-level)))
8065 (outline-next-heading)
8066 (> (funcall outline-level) level))
8070 (defun org-map-region (fun beg end)
8071 "Call FUN for every heading between BEG and END."
8072 (let ((org-ignore-region t))
8074 (setq end (copy-marker end))
8076 (if (and (re-search-forward org-outline-regexp-bol nil t)
8080 (outline-next-heading)
8085 (defvar org-property-end-re) ; silence byte-compiler
8086 (defun org-fixup-indentation (diff)
8087 "Change the indentation in the current entry by DIFF.
8088 However, if any line in the current entry has no indentation, or if it
8089 would end up with no indentation after the change, nothing at all is done."
8091 (let ((end (save-excursion (outline-next-heading)
8093 (prohibit (if (> diff 0)
8095 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
8097 (unless (save-excursion (end-of-line 1)
8098 (re-search-forward prohibit end t))
8099 (while (and (< (point) end)
8100 (re-search-forward "^[ \t]+" end t))
8101 (goto-char (match-end 0))
8102 (setq col (current-column))
8103 (if (< diff 0) (replace-match ""))
8104 (org-indent-to-column (+ diff col))))
8105 (move-marker end nil))))
8107 (defun org-convert-to-odd-levels ()
8108 "Convert an org-mode file with all levels allowed to one with odd levels.
8109 This will leave level 1 alone, convert level 2 to level 3, level 3 to
8112 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
8113 (let ((outline-level 'org-outline-level)
8114 (org-odd-levels-only nil) n)
8116 (goto-char (point-min))
8117 (while (re-search-forward "^\\*\\*+ " nil t)
8118 (setq n (- (length (match-string 0)) 2))
8119 (while (>= (setq n (1- n)) 0)
8121 (end-of-line 1))))))
8123 (defun org-convert-to-oddeven-levels ()
8124 "Convert an org-mode file with only odd levels to one with odd/even levels.
8125 This promotes level 3 to level 2, level 5 to level 3 etc. If the
8126 file contains a section with an even level, conversion would
8127 destroy the structure of the file. An error is signaled in this
8130 (goto-char (point-min))
8131 ;; First check if there are no even levels
8132 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
8133 (org-show-context t)
8134 (error "Not all levels are odd in this file. Conversion not possible"))
8135 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
8136 (let ((outline-regexp org-outline-regexp)
8137 (outline-level 'org-outline-level)
8138 (org-odd-levels-only nil) n)
8140 (goto-char (point-min))
8141 (while (re-search-forward "^\\*\\*+ " nil t)
8142 (setq n (/ (1- (length (match-string 0))) 2))
8143 (while (>= (setq n (1- n)) 0)
8145 (end-of-line 1))))))
8147 (defun org-tr-level (n)
8148 "Make N odd if required."
8149 (if org-odd-levels-only (1+ (/ n 2)) n))
8151 ;;; Vertical tree motion, cutting and pasting of subtrees
8153 (defun org-move-subtree-up (&optional arg)
8154 "Move the current subtree up past ARG headlines of the same level."
8156 (org-move-subtree-down (- (prefix-numeric-value arg))))
8158 (defun org-move-subtree-down (&optional arg)
8159 "Move the current subtree down past ARG headlines of the same level."
8161 (setq arg (prefix-numeric-value arg))
8162 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
8163 'org-get-last-sibling))
8164 (ins-point (make-marker))
8166 (col (current-column))
8167 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
8169 (org-back-to-heading)
8172 (setq ne-beg (org-back-over-empty-lines))
8175 (save-excursion (outline-end-of-heading)
8176 (setq folded (outline-invisible-p)))
8177 (progn (org-end-of-subtree nil t)
8178 (unless (eobp) (backward-char))))
8179 (outline-next-heading)
8180 (setq ne-end (org-back-over-empty-lines))
8183 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
8184 ;; include less whitespace
8187 (forward-line (- ne-beg ne-end))
8188 (setq beg (point))))
8189 ;; Find insertion point, with error handling
8191 (or (and (funcall movfunc) (looking-at org-outline-regexp))
8192 (progn (goto-char beg0)
8193 (user-error "Cannot move past superior level or buffer limit")))
8194 (setq cnt (1- cnt)))
8196 ;; Moving forward - still need to move over subtree
8197 (progn (org-end-of-subtree t t)
8199 (org-back-over-empty-lines)
8200 (or (bolp) (newline)))))
8201 (setq ne-ins (org-back-over-empty-lines))
8202 (move-marker ins-point (point))
8203 (setq txt (buffer-substring beg end))
8204 (org-save-markers-in-region beg end)
8205 (delete-region beg end)
8206 (org-remove-empty-overlays-at beg)
8207 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
8208 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
8209 (and (not (bolp)) (looking-at "\n") (forward-char 1))
8210 (let ((bbb (point)))
8211 (insert-before-markers txt)
8212 (org-reinstall-markers-in-region bbb)
8213 (move-marker ins-point bbb))
8214 (or (bolp) (insert "\n"))
8215 (setq ins-end (point))
8216 (goto-char ins-point)
8217 (org-skip-whitespace)
8218 (when (and (< arg 0)
8219 (org-first-sibling-p)
8221 ;; Move whitespace back to beginning
8224 (let ((kill-whole-line t))
8225 (kill-line (- ne-ins ne-beg)) (point)))
8226 (insert (make-string (- ne-ins ne-beg) ?\n)))
8227 (move-marker ins-point nil)
8232 (org-cycle-hide-drawers 'children))
8233 (org-clean-visibility-after-subtree-move)
8234 ;; move back to the initial column we were at
8235 (move-to-column col)))
8237 (defvar org-subtree-clip ""
8238 "Clipboard for cut and paste of subtrees.
8239 This is actually only a copy of the kill, because we use the normal kill
8240 ring. We need it to check if the kill was created by `org-copy-subtree'.")
8242 (defvar org-subtree-clip-folded nil
8243 "Was the last copied subtree folded?
8244 This is used to fold the tree back after pasting.")
8246 (defun org-cut-subtree (&optional n)
8247 "Cut the current subtree into the clipboard.
8248 With prefix arg N, cut this many sequential subtrees.
8249 This is a short-hand for marking the subtree and then cutting it."
8251 (org-copy-subtree n 'cut))
8253 (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
8254 "Copy the current subtree it in the clipboard.
8255 With prefix arg N, copy this many sequential subtrees.
8256 This is a short-hand for marking the subtree and then copying it.
8257 If CUT is non-nil, actually cut the subtree.
8258 If FORCE-STORE-MARKERS is non-nil, store the relative locations
8259 of some markers in the region, even if CUT is non-nil. This is
8260 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
8262 (let (beg end folded (beg0 (point)))
8263 (if (org-called-interactively-p 'any)
8264 (org-back-to-heading nil) ; take what looks like a subtree
8265 (org-back-to-heading t)) ; take what is really there
8267 (skip-chars-forward " \t\r\n")
8270 (outline-next-heading)
8271 (save-excursion (outline-end-of-heading)
8272 (setq folded (outline-invisible-p)))
8274 (org-forward-heading-same-level (1- n) t)
8276 (org-end-of-subtree t t)))
8280 (setq org-subtree-clip-folded folded)
8281 (when (or cut force-store-markers)
8282 (org-save-markers-in-region beg end))
8283 (if cut (kill-region beg end) (copy-region-as-kill beg end))
8284 (setq org-subtree-clip (current-kill 0))
8285 (message "%s: Subtree(s) with %d characters"
8286 (if cut "Cut" "Copied")
8287 (length org-subtree-clip)))))
8289 (defun org-paste-subtree (&optional level tree for-yank)
8290 "Paste the clipboard as a subtree, with modification of headline level.
8291 The entire subtree is promoted or demoted in order to match a new headline
8294 If the cursor is at the beginning of a headline, the same level as
8295 that headline is used to paste the tree.
8297 If not, the new level is derived from the *visible* headings
8298 before and after the insertion point, and taken to be the inferior headline
8299 level of the two. So if the previous visible heading is level 3 and the
8300 next is level 4 (or vice versa), level 4 will be used for insertion.
8301 This makes sure that the subtree remains an independent subtree and does
8302 not swallow low level entries.
8304 You can also force a different level, either by using a numeric prefix
8305 argument, or by inserting the heading marker by hand. For example, if the
8306 cursor is after \"*****\", then the tree will be shifted to level 5.
8308 If optional TREE is given, use this text instead of the kill ring.
8310 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
8311 move back over whitespace before inserting, and move point to the end of
8312 the inserted text when done."
8314 (setq tree (or tree (and kill-ring (current-kill 0))))
8315 (unless (org-kill-is-subtree-p tree)
8317 (substitute-command-keys
8318 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
8319 (org-with-limited-levels
8320 (let* ((visp (not (outline-invisible-p)))
8322 (^re_ "\\(\\*+\\)[ \t]*")
8323 (old-level (if (string-match org-outline-regexp-bol txt)
8324 (- (match-end 0) (match-beginning 0) 1)
8326 (force-level (cond (level (prefix-numeric-value level))
8327 ((and (looking-at "[ \t]*$")
8329 "^\\*+$" (buffer-substring
8330 (point-at-bol) (point))))
8331 (- (match-end 0) (match-beginning 0)))
8333 (looking-at org-outline-regexp))
8334 (- (match-end 0) (point) 1))))
8335 (previous-level (save-excursion
8338 (outline-previous-visible-heading 1)
8339 (if (looking-at ^re_)
8340 (- (match-end 0) (match-beginning 0) 1)
8343 (next-level (save-excursion
8346 (or (looking-at org-outline-regexp)
8347 (outline-next-visible-heading 1))
8348 (if (looking-at ^re_)
8349 (- (match-end 0) (match-beginning 0) 1)
8352 (new-level (or force-level (max previous-level next-level)))
8353 (shift (if (or (= old-level -1)
8355 (= old-level new-level))
8357 (- new-level old-level)))
8358 (delta (if (> shift 0) -1 1))
8359 (func (if (> shift 0) 'org-demote 'org-promote))
8360 (org-odd-levels-only nil)
8362 ;; Remove the forced level indicator
8364 (delete-region (point-at-bol) (point)))
8366 (beginning-of-line (if (bolp) 1 2))
8368 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
8369 (insert-before-markers txt)
8370 (unless (string-match "\n\\'" txt) (insert "\n"))
8371 (setq newend (point))
8372 (org-reinstall-markers-in-region beg)
8375 (skip-chars-forward " \t\n\r")
8377 (if (and (outline-invisible-p) visp)
8378 (save-excursion (outline-show-heading)))
8379 ;; Shift if necessary
8382 (narrow-to-region beg end)
8383 (while (not (= shift 0))
8384 (org-map-region func (point-min) (point-max))
8385 (setq shift (+ delta shift)))
8386 (goto-char (point-min))
8387 (setq newend (point-max))))
8388 (when (or (org-called-interactively-p 'interactive) for-yank)
8389 (message "Clipboard pasted as level %d subtree" new-level))
8390 (if (and (not for-yank) ; in this case, org-yank will decide about folding
8392 (eq org-subtree-clip (current-kill 0))
8393 org-subtree-clip-folded)
8394 ;; The tree was folded before it was killed/copied
8396 (and for-yank (goto-char newend)))))
8398 (defun org-kill-is-subtree-p (&optional txt)
8399 "Check if the current kill is an outline subtree, or a set of trees.
8400 Returns nil if kill does not start with a headline, or if the first
8401 headline level is not the largest headline level in the tree.
8402 So this will actually accept several entries of equal levels as well,
8403 which is OK for `org-paste-subtree'.
8404 If optional TXT is given, check this string instead of the current kill."
8405 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
8406 (re (org-get-limited-outline-regexp))
8407 (^re (concat "^" re))
8408 (start-level (and kill
8410 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
8412 (- (match-end 2) (match-beginning 2) 1)))
8413 (start (1+ (or (match-beginning 2) -1))))
8414 (if (not start-level)
8416 nil) ;; does not even start with a heading
8418 (while (setq start (string-match ^re kill (1+ start)))
8419 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
8423 (defvar org-markers-to-move nil
8424 "Markers that should be moved with a cut-and-paste operation.
8425 Those markers are stored together with their positions relative to
8426 the start of the region.")
8428 (defun org-save-markers-in-region (beg end)
8429 "Check markers in region.
8430 If these markers are between BEG and END, record their position relative
8431 to BEG, so that after moving the block of text, we can put the markers back
8433 This function gets called just before an entry or tree gets cut from the
8434 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
8435 called immediately, to move the markers with the entries."
8436 (setq org-markers-to-move nil)
8437 (when (featurep 'org-clock)
8438 (org-clock-save-markers-for-cut-and-paste beg end))
8439 (when (featurep 'org-agenda)
8440 (org-agenda-save-markers-for-cut-and-paste beg end)))
8442 (defun org-check-and-save-marker (marker beg end)
8443 "Check if MARKER is between BEG and END.
8444 If yes, remember the marker and the distance to BEG."
8445 (when (and (marker-buffer marker)
8446 (equal (marker-buffer marker) (current-buffer)))
8447 (if (and (>= marker beg) (< marker end))
8448 (push (cons marker (- marker beg)) org-markers-to-move))))
8450 (defun org-reinstall-markers-in-region (beg)
8451 "Move all remembered markers to their position relative to BEG."
8453 (move-marker (car x) (+ beg (cdr x))))
8454 org-markers-to-move)
8455 (setq org-markers-to-move nil))
8457 (defun org-narrow-to-subtree ()
8458 "Narrow buffer to the current subtree."
8462 (org-with-limited-levels
8464 (progn (org-back-to-heading t) (point))
8465 (progn (org-end-of-subtree t t)
8466 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
8469 (defun org-narrow-to-block ()
8470 "Narrow buffer to the current block."
8472 (let* ((case-fold-search t)
8473 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
8474 "^[ \t]*#\\+end_.*")))
8476 (narrow-to-region (car blockp) (cdr blockp))
8477 (user-error "Not in a block"))))
8480 (defvar org-property-drawer-re))
8482 (defvar org-property-start-re) ;; defined below
8483 (defun org-clone-subtree-with-time-shift (n &optional shift)
8484 "Clone the task (subtree) at point N times.
8485 The clones will be inserted as siblings.
8487 In interactive use, the user will be prompted for the number of
8488 clones to be produced. If the entry has a timestamp, the user
8489 will also be prompted for a time shift, which may be a repeater
8490 as used in time stamps, for example `+3d'. To disable this,
8491 you can call the function with a universal prefix argument.
8493 When a valid repeater is given and the entry contains any time
8494 stamps, the clones will become a sequence in time, with time
8495 stamps in the subtree shifted for each clone produced. If SHIFT
8496 is nil or the empty string, time stamps will be left alone. The
8497 ID property of the original subtree is removed.
8499 If the original subtree did contain time stamps with a repeater,
8500 the following will happen:
8501 - the repeater will be removed in each clone
8502 - an additional clone will be produced, with the current, unshifted
8503 date(s) in the entry.
8504 - the original entry will be placed *after* all the clones, with
8506 - the start days in the repeater in the original entry will be shifted
8507 to past the last clone.
8508 In this way you can spell out a number of instances of a repeating task,
8509 and still retain the repeater to cover future instances of the task."
8510 (interactive "nNumber of clones to produce: ")
8513 (if (and (not (equal current-prefix-arg '(4)))
8515 (re-search-forward org-ts-regexp-both
8517 (org-end-of-subtree t)
8519 (read-from-minibuffer
8520 "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
8521 ""))) ;; No time shift
8523 (drawer-re org-drawer-regexp)
8524 beg end template task idprop
8525 shift-n shift-what doshift nmin nmax)
8526 (if (not (and (integerp n) (> n 0)))
8527 (error "Invalid number of replications %s" n))
8528 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
8529 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
8531 (error "Invalid shift specification %s" shift))
8533 (setq shift-n (string-to-number (match-string 1 shift))
8534 shift-what (cdr (assoc (match-string 2 shift)
8535 '(("d" . day) ("w" . week)
8536 ("m" . month) ("y" . year))))))
8537 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
8538 (setq nmin 1 nmax n)
8539 (org-back-to-heading t)
8541 (setq idprop (org-entry-get nil "ID"))
8542 (org-end-of-subtree t t)
8543 (or (bolp) (insert "\n"))
8545 (setq template (buffer-substring beg end))
8547 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
8548 (delete-region beg end)
8550 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
8552 (loop for n from nmin to nmax do
8557 (goto-char (point-min))
8559 (and idprop (if org-clone-delete-id
8560 (org-entry-delete nil "ID")
8561 (org-id-get-create t)))
8563 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
8565 (goto-char (point-min))
8566 (while (re-search-forward drawer-re nil t)
8568 (org-remove-empty-drawer-at d (point)))
8570 (goto-char (point-min))
8572 (while (re-search-forward org-ts-regexp-both nil t)
8573 (org-timestamp-change (* n shift-n) shift-what))
8574 (unless (= n n-no-remove)
8575 (goto-char (point-min))
8576 (while (re-search-forward org-ts-regexp nil t)
8578 (goto-char (match-beginning 0))
8579 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
8580 (delete-region (match-beginning 1) (match-end 1)))))))
8581 (setq task (buffer-string)))
8587 (defun org-sort (with-case)
8588 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8589 Optional argument WITH-CASE means sort case-sensitively."
8592 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8593 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8595 (org-call-with-arg 'org-sort-entries with-case))))
8597 (defun org-sort-remove-invisible (s)
8598 "Remove invisible links from string S."
8599 (remove-text-properties 0 (length s) org-rm-props s)
8600 (while (string-match org-bracket-link-regexp s)
8601 (setq s (replace-match (if (match-end 2)
8605 (let ((st (format " %s " s)))
8606 (while (string-match org-emph-re st)
8607 (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
8608 (setq s (substring st 1 -1)))
8611 (defvar org-priority-regexp) ; defined later in the file
8613 (defvar org-after-sorting-entries-or-items-hook nil
8614 "Hook that is run after a bunch of entries or items have been sorted.
8615 When children are sorted, the cursor is in the parent line when this
8616 hook gets called. When a region or a plain list is sorted, the cursor
8617 will be in the first entry of the sorted region/list.")
8619 (defun org-sort-entries
8620 (&optional with-case sorting-type getkey-func compare-func property)
8621 "Sort entries on a certain level of an outline tree.
8622 If there is an active region, the entries in the region are sorted.
8623 Else, if the cursor is before the first entry, sort the top-level items.
8624 Else, the children of the entry at point are sorted.
8626 Sorting can be alphabetically, numerically, by date/time as given by
8627 a time stamp, by a property, by priority order, or by a custom function.
8629 The command prompts for the sorting type unless it has been given to the
8630 function through the SORTING-TYPE argument, which needs to be a character,
8631 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
8632 precise meaning of each character:
8634 n Numerically, by converting the beginning of the entry/item to a number.
8635 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8636 o By order of TODO keywords.
8637 t By date/time, either the first active time stamp in the entry, or, if
8638 none exist, by the first inactive one.
8639 s By the scheduled date/time.
8640 d By deadline date/time.
8641 c By creation time, which is assumed to be the first inactive time stamp
8642 at the beginning of a line.
8643 p By priority according to the cookie.
8644 r By the value of a property.
8646 Capital letters will reverse the sort order.
8648 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8649 called with point at the beginning of the record. It must return either
8650 a string or a number that should serve as the sorting key for that record.
8652 Comparing entries ignores case by default. However, with an optional argument
8653 WITH-CASE, the sorting considers case as well.
8655 Sorting is done against the visible part of the headlines, it ignores hidden
8658 (let ((case-func (if with-case 'identity 'downcase))
8660 ;; The clock marker is lost when using `sort-subr', let's
8661 ;; store the clocking string.
8662 (when (equal (marker-buffer org-clock-marker) (current-buffer))
8664 (goto-char org-clock-marker)
8665 (buffer-substring-no-properties (line-beginning-position)
8667 start beg end stars re re2
8669 ;; Find beginning and end of region to sort
8671 ((org-region-active-p)
8672 ;; we will sort the region
8673 (setq end (region-end)
8675 (goto-char (region-beginning))
8676 (if (not (org-at-heading-p)) (outline-next-heading))
8677 (setq start (point)))
8678 ((or (org-at-heading-p)
8679 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8680 ;; we will sort the children of the current headline
8681 (org-back-to-heading)
8683 end (progn (org-end-of-subtree t t)
8684 (or (bolp) (insert "\n"))
8685 (when (>= (org-back-over-empty-lines) 1)
8691 (outline-next-heading))
8693 ;; we will sort the top-level entries in this file
8694 (goto-char (point-min))
8695 (or (org-at-heading-p) (outline-next-heading))
8696 (setq start (point))
8697 (goto-char (point-max))
8698 (beginning-of-line 1)
8699 (when (looking-at ".*?\\S-")
8700 ;; File ends in a non-white line
8703 (setq end (point-max))
8704 (setq what "top-level")
8709 (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
8711 (looking-at "\\(\\*+\\)")
8712 (setq stars (match-string 1)
8713 re (concat "^" (regexp-quote stars) " +")
8714 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8715 txt (buffer-substring beg end))
8716 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8717 (if (and (not (equal stars "*")) (string-match re2 txt))
8718 (user-error "Region to sort contains a level above the first entry"))
8720 (unless sorting-type
8722 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8723 [t]ime
[s]cheduled [d]eadline [c]reated
8724 A/N/P/R/O/F/T/S/D/C means reversed:"
8726 (setq sorting-type (read-char-exclusive))
8729 (and (= (downcase sorting-type) ?f)
8731 (org-icompleting-read "Sort using function: "
8732 obarray 'fboundp t nil nil))
8733 (setq getkey-func (intern getkey-func))))
8735 (and (= (downcase sorting-type) ?r)
8738 (org-icompleting-read "Property: "
8739 (mapcar 'list (org-buffer-property-keys t))
8742 (message "Sorting entries...")
8745 (narrow-to-region start end)
8746 (let ((dcst (downcase sorting-type))
8747 (case-fold-search nil)
8748 (now (current-time)))
8750 (/= dcst sorting-type)
8751 ;; This function moves to the beginning character of the "record" to
8754 (if (re-search-forward re nil t)
8755 (goto-char (match-beginning 0))
8756 (goto-char (point-max))))
8757 ;; This function moves to the last character of the "record" being
8762 (outline-forward-same-level 1)
8764 (goto-char (point-max))))))
8765 ;; This function returns the value that gets sorted against.
8769 (if (looking-at org-complex-heading-regexp)
8770 (string-to-number (org-sort-remove-invisible (match-string 4)))
8773 (if (looking-at org-complex-heading-regexp)
8774 (funcall case-func (org-sort-remove-invisible (match-string 4)))
8777 (let ((end (save-excursion (outline-next-heading) (point))))
8778 (if (or (re-search-forward org-ts-regexp end t)
8779 (re-search-forward org-ts-regexp-both end t))
8780 (org-time-string-to-seconds (match-string 0))
8783 (let ((end (save-excursion (outline-next-heading) (point))))
8784 (if (re-search-forward
8785 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8787 (org-time-string-to-seconds (match-string 0))
8790 (let ((end (save-excursion (outline-next-heading) (point))))
8791 (if (re-search-forward org-scheduled-time-regexp end t)
8792 (org-time-string-to-seconds (match-string 1))
8795 (let ((end (save-excursion (outline-next-heading) (point))))
8796 (if (re-search-forward org-deadline-time-regexp end t)
8797 (org-time-string-to-seconds (match-string 1))
8800 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8801 (string-to-char (match-string 2))
8802 org-default-priority))
8804 (or (org-entry-get nil property) ""))
8806 (if (looking-at org-complex-heading-regexp)
8807 (- 9999 (length (member (match-string 2)
8808 org-todo-keywords-1)))))
8812 (setq tmp (funcall getkey-func))
8813 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8815 (error "Invalid key function `%s'" getkey-func)))
8816 (t (error "Invalid sorting type `%c'" sorting-type))))
8819 ((= dcst ?a) 'string<)
8820 ((= dcst ?f) compare-func)
8821 ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
8822 (run-hooks 'org-after-sorting-entries-or-items-hook)
8823 ;; Reset the clock marker if needed
8827 (search-forward cmstr nil t)
8828 (move-marker org-clock-marker (point))))
8829 (message "Sorting entries...done")))
8831 (defun org-do-sort (table what &optional with-case sorting-type)
8832 "Sort TABLE of WHAT according to SORTING-TYPE.
8833 The user will be prompted for the SORTING-TYPE if the call to this
8834 function does not specify it. WHAT is only for the prompt, to indicate
8835 what is being sorted. The sorting key will be extracted from
8836 the car of the elements of the table.
8837 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8838 (unless sorting-type
8840 "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
8842 (setq sorting-type (read-char-exclusive)))
8843 (let ((dcst (downcase sorting-type))
8844 extractfun comparefun)
8845 ;; Define the appropriate functions
8848 (setq extractfun 'string-to-number
8849 comparefun (if (= dcst sorting-type) '< '>)))
8851 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8852 (lambda(x) (downcase (org-sort-remove-invisible x))))
8853 comparefun (if (= dcst sorting-type)
8855 (lambda (a b) (and (not (string< a b))
8856 (not (string= a b)))))))
8860 (if (or (string-match org-ts-regexp x)
8861 (string-match org-ts-regexp-both x))
8863 (org-time-string-to-time (match-string 0 x)))
8865 comparefun (if (= dcst sorting-type) '< '>)))
8866 (t (error "Invalid sorting type `%c'" sorting-type)))
8868 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8870 (lambda (a b) (funcall comparefun (car a) (car b))))))
8873 ;;; The orgstruct minor mode
8875 ;; Define a minor mode which can be used in other modes in order to
8876 ;; integrate the org-mode structure editing commands.
8878 ;; This is really a hack, because the org-mode structure commands use
8879 ;; keys which normally belong to the major mode. Here is how it
8880 ;; works: The minor mode defines all the keys necessary to operate the
8881 ;; structure commands, but wraps the commands into a function which
8882 ;; tests if the cursor is currently at a headline or a plain list
8883 ;; item. If that is the case, the structure command is used,
8884 ;; temporarily setting many Org-mode variables like regular
8885 ;; expressions for filling etc. However, when any of those keys is
8886 ;; used at a different location, function uses `key-binding' to look
8887 ;; up if the key has an associated command in another currently active
8888 ;; keymap (minor modes, major mode, global), and executes that
8889 ;; command. There might be problems if any of the keys is otherwise
8890 ;; used as a prefix key.
8892 (defcustom orgstruct-heading-prefix-regexp ""
8893 "Regexp that matches the custom prefix of Org headlines in
8894 orgstruct(++)-mode."
8897 :package-version '(Org . "8.3")
8899 ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
8901 (defcustom orgstruct-setup-hook nil
8902 "Hook run after orgstruct-mode-map is filled."
8905 :package-version '(Org . "8.0")
8908 (defvar orgstruct-initialized nil)
8910 (defvar org-local-vars nil
8911 "List of local variables, for use by `orgstruct-mode'.")
8914 (define-minor-mode orgstruct-mode
8915 "Toggle the minor mode `orgstruct-mode'.
8916 This mode is for using Org-mode structure commands in other
8917 modes. The following keys behave as if Org-mode were active, if
8918 the cursor is on a headline, or on a plain list item (both as
8919 defined by Org-mode)."
8920 nil " OrgStruct" (make-sparse-keymap)
8921 (funcall (if orgstruct-mode
8922 'add-to-invisibility-spec
8923 'remove-from-invisibility-spec)
8925 (when orgstruct-mode
8926 (org-load-modules-maybe)
8927 (unless orgstruct-initialized
8929 (setq orgstruct-initialized t))))
8932 (defun turn-on-orgstruct ()
8933 "Unconditionally turn on `orgstruct-mode'."
8936 (defvar org-fb-vars nil)
8937 (make-variable-buffer-local 'org-fb-vars)
8938 (defun orgstruct++-mode (&optional arg)
8939 "Toggle `orgstruct-mode', the enhanced version of it.
8940 In addition to setting orgstruct-mode, this also exports all
8941 indentation and autofilling variables from org-mode into the
8942 buffer. It will also recognize item context in multiline items."
8944 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8946 (progn (orgstruct-mode -1)
8948 (org-set-local (car v)
8949 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
8952 (setq org-fb-vars nil)
8953 (unless org-local-vars
8954 (setq org-local-vars (org-get-local-variables)))
8959 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
8960 (symbol-name (car x)))
8961 (setq var (car x) val (nth 1 x))
8962 (push (list var `(quote ,(eval var))) org-fb-vars)
8963 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8965 (org-set-local 'orgstruct-is-++ t))))
8967 (defvar orgstruct-is-++ nil
8968 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8969 (make-variable-buffer-local 'orgstruct-is-++)
8972 (defun turn-on-orgstruct++ ()
8973 "Unconditionally turn on `orgstruct++-mode'."
8974 (orgstruct++-mode 1))
8976 (defun orgstruct-error ()
8977 "Error when there is no default binding for a structure key."
8979 (funcall (if (fboundp 'user-error)
8982 "This key has no function outside structure elements"))
8984 (defun orgstruct-setup ()
8985 "Setup orgstruct keymap."
8986 (dolist (cell '((org-demote . t)
8990 (org-shiftmetaleft . t)
8991 (org-shiftmetaright . t)
8992 org-backward-element
8993 org-backward-heading-same-level
8998 org-forward-heading-same-level
9000 org-insert-heading-respect-content
9001 org-kill-note-or-show-branches
9006 org-narrow-to-subtree
9021 outline-next-visible-heading
9022 outline-previous-visible-heading
9026 (let ((f (or (car-safe cell) cell))
9027 (disable-when-heading-prefix (cdr-safe cell)))
9029 (let ((new-bindings))
9030 (dolist (binding (nconc (where-is-internal f org-mode-map)
9031 (where-is-internal f outline-mode-map)))
9032 (push binding new-bindings)
9033 ;; TODO use local-function-key-map
9034 (dolist (rep '(("<tab>" . "TAB")
9035 ("<return>" . "RET")
9036 ("<escape>" . "ESC")
9037 ("<delete>" . "DEL")))
9038 (setq binding (read-kbd-macro
9039 (let ((case-fold-search))
9040 (replace-regexp-in-string
9041 (regexp-quote (cdr rep))
9043 (key-description binding)))))
9044 (pushnew binding new-bindings :test 'equal)))
9045 (dolist (binding new-bindings)
9046 (let ((key (lookup-key orgstruct-mode-map binding)))
9047 (when (or (not key) (numberp key))
9049 (org-defkey orgstruct-mode-map
9051 (orgstruct-make-binding f binding disable-when-heading-prefix))
9053 (run-hooks 'orgstruct-setup-hook))
9055 (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
9056 "Create a function for binding in the structure minor mode.
9057 FUN is the command to call inside a table. KEY is the key that
9058 should be checked in for a command to execute outside of tables.
9059 Non-nil `disable-when-heading-prefix' means to disable the command
9060 if `orgstruct-heading-prefix-regexp' is not empty."
9061 (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
9064 (while (fboundp (intern nname))
9065 (setq nname (format "%s-%d" name (setq i (1+ i)))))
9066 (setq name (intern nname)))
9068 (let ((bindings '((org-heading-regexp
9070 orgstruct-heading-prefix-regexp
9071 "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
9073 (concat orgstruct-heading-prefix-regexp "\\*+ "))
9074 (org-outline-regexp-bol
9075 (concat "^" org-outline-regexp))
9076 (outline-regexp org-outline-regexp)
9077 (outline-heading-end-regexp "\n")
9078 (outline-level 'org-outline-level)
9079 (outline-heading-alist))))
9081 ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
9082 "Outside of structure, run the binding of `"
9083 (key-description key) "'."
9084 (when disable-when-heading-prefix
9086 "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
9087 "back to the default binding due to limitations of Org's implementation of\n"
9088 "`" (symbol-name fun) "'.")))
9091 ,(and disable-when-heading-prefix
9092 '(not (string= orgstruct-heading-prefix-regexp ""))))
9097 (org-context-p 'headline 'item
9099 '(org-insert-heading
9100 org-insert-heading-respect-content
9102 '(when orgstruct-is-++
9105 (let* ((orgstruct-mode)
9112 ("<\\([^>]*\\)tab>" . "\\1TAB")
9113 ("<\\([^>]*\\)return>" . "\\1RET")
9114 ("<\\([^>]*\\)escape>" . "\\1ESC")
9115 ("<\\([^>]*\\)delete>" . "\\1DEL"))
9118 (setq key (read-kbd-macro
9119 (let ((case-fold-search))
9120 (replace-regexp-in-string
9123 (key-description key))))))
9124 (when (key-binding key)
9125 (throw 'exit (key-binding key))))))))
9126 (if (keymapp binding)
9127 (org-set-transient-map binding)
9128 (let ((func (or binding
9130 'orgstruct-error))))
9132 (call-interactively func)))))
9133 (org-run-like-in-org-mode
9137 (call-interactively ',fun)))))))))
9140 (defun org-contextualize-keys (alist contexts)
9141 "Return valid elements in ALIST depending on CONTEXTS.
9143 `org-agenda-custom-commands' or `org-capture-templates' are the
9144 values used for ALIST, and `org-agenda-custom-commands-contexts'
9145 or `org-capture-templates-contexts' are the associated contexts
9148 ;; normalize contexts
9150 (lambda(c) (cond ((listp (cadr c))
9151 (list (car c) (car c) (cadr c)))
9152 ((string= "" (cadr c))
9153 (list (car c) (car c) (caddr c)))
9157 ;; loop over all commands or templates
9161 ((not (assoc (car c) contexts))
9163 ((and (assoc (car c) contexts)
9164 (setq vrules (org-contextualize-validate-key
9167 (when (not (equal (car vr) (cadr vr)))
9170 (if (not repl) (push c r)
9171 (push (cadr repl) s)
9174 (cdr (or (assoc (cadr repl) alist)
9175 (error "Undefined key `%s' as contextual replacement for `%s'"
9176 (cadr repl) (car c)))))
9178 ;; Return limited ALIST, possibly with keys modified, and deduplicated
9183 (let ((tpl (car x)))
9192 (defun org-contextualize-validate-key (key contexts)
9193 "Check CONTEXTS for agenda or capture KEY."
9195 (dolist (r contexts)
9199 (and (equal key (car r))
9200 (if (functionp rr) (funcall rr)
9201 (or (and (eq (car rr) 'in-file)
9203 (string-match (cdr rr) (buffer-file-name)))
9204 (and (eq (car rr) 'in-mode)
9205 (string-match (cdr rr) (symbol-name major-mode)))
9206 (and (eq (car rr) 'in-buffer)
9207 (string-match (cdr rr) (buffer-name)))
9208 (when (and (eq (car rr) 'not-in-file)
9210 (not (string-match (cdr rr) (buffer-file-name))))
9211 (when (eq (car rr) 'not-in-mode)
9212 (not (string-match (cdr rr) (symbol-name major-mode))))
9213 (when (eq (car rr) 'not-in-buffer)
9214 (not (string-match (cdr rr) (buffer-name)))))))
9217 (delete-dups (delq nil res))))
9219 (defun org-context-p (&rest contexts)
9220 "Check if local context is any of CONTEXTS.
9221 Possible values in the list of contexts are `table', `headline', and `item'."
9222 (let ((pos (point)))
9223 (goto-char (point-at-bol))
9224 (prog1 (or (and (memq 'table contexts)
9225 (looking-at "[ \t]*|"))
9226 (and (memq 'headline contexts)
9227 (looking-at org-outline-regexp))
9228 (and (memq 'item contexts)
9229 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
9230 (and (memq 'item-body contexts)
9234 (defun org-get-local-variables ()
9235 "Return a list of all local variables in an Org mode buffer."
9237 (with-current-buffer (get-buffer-create "*Org tmp*")
9240 (setq varlist (buffer-local-variables)))
9241 (kill-buffer "*Org tmp*")
9248 (list (car x) (cdr x))))
9249 (if (and (not (get (car x) 'org-state))
9251 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
9252 (symbol-name (car x))))
9256 (defun org-clone-local-variables (from-buffer &optional regexp)
9257 "Clone local variables from FROM-BUFFER.
9258 Optional argument REGEXP selects variables to clone."
9261 (and (symbolp (car pair))
9263 (string-match regexp (symbol-name (car pair))))
9264 (set (make-local-variable (car pair))
9266 (buffer-local-variables from-buffer)))
9269 (defun org-run-like-in-org-mode (cmd)
9270 "Run a command, pretending that the current buffer is in Org-mode.
9271 This will temporarily bind local variables that are typically bound in
9272 Org-mode to the values they have in Org-mode, and then interactively
9274 (org-load-modules-maybe)
9275 (unless org-local-vars
9276 (setq org-local-vars (org-get-local-variables)))
9278 (dolist (var org-local-vars)
9279 (when (or (not (boundp (car var)))
9280 (eq (symbol-value (car var))
9281 (default-value (car var))))
9282 (push (list (car var) `(quote ,(cadr var))) binds)))
9284 (call-interactively (quote ,cmd))))))
9288 (defun org-get-category (&optional pos force-refresh)
9289 "Get the category applying to position POS."
9291 (if force-refresh (org-refresh-category-properties))
9292 (let ((pos (or pos (point))))
9293 (or (get-text-property pos 'org-category)
9294 (progn (org-refresh-category-properties)
9295 (get-text-property pos 'org-category))))))
9297 (defun org-refresh-category-properties ()
9298 "Refresh category text properties in the buffer."
9299 (let ((case-fold-search t)
9300 (inhibit-read-only t)
9302 ((null org-category)
9303 (if buffer-file-name
9304 (file-name-sans-extension
9305 (file-name-nondirectory buffer-file-name))
9307 ((symbolp org-category) (symbol-name org-category))
9309 beg end cat pos optionp)
9310 (org-with-silent-modifications
9314 (goto-char (point-min))
9315 (put-text-property (point) (point-max) 'org-category def-cat)
9316 (while (re-search-forward
9317 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
9318 (setq pos (match-end 0)
9319 optionp (equal (char-after (match-beginning 0)) ?#)
9320 cat (org-trim (match-string 2)))
9322 (setq beg (point-at-bol) end (point-max))
9323 (org-back-to-heading t)
9324 (setq beg (point) end (org-end-of-subtree t t)))
9325 (put-text-property beg end 'org-category cat)
9326 (put-text-property beg end 'org-category-position beg)
9327 (goto-char pos)))))))
9329 (defun org-refresh-properties (dprop tprop)
9330 "Refresh buffer text properties.
9331 DPROP is the drawer property and TPROP is the corresponding text
9333 (let ((case-fold-search t)
9334 (inhibit-read-only t) p)
9335 (org-with-silent-modifications
9339 (goto-char (point-min))
9340 (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
9341 (setq p (org-match-string-no-properties 1))
9343 (org-back-to-heading t)
9345 (point-at-bol) (or (outline-next-heading) (point-max)) tprop p))))))))
9350 ;;; Link abbreviations
9352 (defun org-link-expand-abbrev (link)
9353 "Apply replacements as defined in `org-link-abbrev-alist'."
9354 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
9355 (let* ((key (match-string 1 link))
9356 (as (or (assoc key org-link-abbrev-alist-local)
9357 (assoc key org-link-abbrev-alist)))
9358 (tag (and (match-end 2) (match-string 3 link)))
9364 ((symbolp rpl) (funcall rpl tag))
9365 ((string-match "%(\\([^)]+\\))" rpl)
9368 (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
9369 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
9370 ((string-match "%h" rpl)
9371 (replace-match (url-hexify-string (or tag "")) t t rpl))
9372 (t (concat rpl tag)))))
9375 ;;; Storing and inserting links
9377 (defvar org-insert-link-history nil
9378 "Minibuffer history for links inserted with `org-insert-link'.")
9380 (defvar org-stored-links nil
9381 "Contains the links stored with `org-store-link'.")
9383 (defvar org-store-link-plist nil
9384 "Plist with info about the most recently link created with `org-store-link'.")
9386 (defvar org-link-protocols nil
9387 "Link protocols added to Org-mode using `org-add-link-type'.")
9389 (defvar org-store-link-functions nil
9390 "List of functions that are called to create and store a link.
9391 Each function will be called in turn until one returns a non-nil
9392 value. Each function should check if it is responsible for creating
9393 this link (for example by looking at the major mode).
9394 If not, it must exit and return nil.
9395 If yes, it should return a non-nil value after a calling
9396 `org-store-link-props' with a list of properties and values.
9397 Special properties are:
9399 :type The link prefix, like \"http\". This must be given.
9400 :link The link, like \"http://www.astro.uva.nl/~dominik\".
9401 This is obligatory as well.
9402 :description Optional default description for the second pair
9403 of brackets in an Org-mode link. The user can still change
9404 this when inserting this link into an Org-mode buffer.
9406 In addition to these, any additional properties can be specified
9407 and then used in capture templates.")
9409 (defun org-add-link-type (type &optional follow export)
9410 "Add TYPE to the list of `org-link-types'.
9411 Re-compute all regular expressions depending on `org-link-types'
9413 FOLLOW and EXPORT are two functions.
9415 FOLLOW should take the link path as the single argument and do whatever
9416 is necessary to follow the link, for example find a file or display
9419 EXPORT should format the link path for export to one of the export formats.
9420 It should be a function accepting three arguments:
9422 path the path of the link, the text after the prefix (like \"http:\")
9423 desc the description of the link, if any, or a description added by
9424 org-export-normalize-links if there is none
9425 format the export format, a symbol like `html' or `latex' or `ascii'..
9427 The function may use the FORMAT information to return different values
9428 depending on the format. The return value will be put literally into
9429 the exported file. If the return value is nil, this means Org should
9430 do what it normally does with links which do not have EXPORT defined.
9432 Org-mode has a built-in default for exporting links. If you are happy with
9433 this default, there is no need to define an export function for the link
9434 type. For a simple example of an export function, see `org-bbdb.el'."
9435 (add-to-list 'org-link-types type t)
9436 (org-make-link-regexps)
9437 (if (assoc type org-link-protocols)
9438 (setcdr (assoc type org-link-protocols) (list follow export))
9439 (push (list type follow export) org-link-protocols)))
9441 (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
9442 (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
9445 (defun org-store-link (arg)
9446 "\\<org-mode-map>Store an org-link to the current location.
9447 This link is added to `org-stored-links' and can later be inserted
9448 into an org-buffer with \\[org-insert-link].
9450 For some link types, a prefix arg is interpreted.
9451 For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
9452 For file links, arg negates `org-context-in-file-links'.
9454 A double prefix arg force skipping storing functions that are not
9457 A triple prefix arg force storing a link for each line in the
9460 (org-load-modules-maybe)
9461 (if (and (equal arg '(64)) (org-region-active-p))
9463 (let ((end (region-end)))
9464 (goto-char (region-beginning))
9466 (while (< (point-at-eol) end)
9467 (move-end-of-line 1) (activate-mark)
9468 (let (current-prefix-arg)
9469 (call-interactively 'org-store-link))
9470 (move-beginning-of-line 2)
9471 (set-mark (point)))))
9472 (org-with-limited-levels
9473 (setq org-store-link-plist nil)
9474 (let (link cpltxt desc description search
9475 txt custom-id agenda-link sfuns sfunsn)
9478 ;; Store a link using an external link type
9479 ((and (not (equal arg '(16)))
9482 nil (mapcar (lambda (f)
9483 (let (fs) (if (funcall f) (push f fs))))
9484 org-store-link-functions))
9485 sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
9486 (or (and (cdr sfuns)
9489 "Which function for creating the link? "
9490 sfunsn nil t (car sfunsn)))))
9491 (funcall (caar sfuns)))
9492 (setq link (plist-get org-store-link-plist :link)
9493 desc (or (plist-get org-store-link-plist
9497 ;; Store a link from a source code buffer
9498 ((org-src-edit-buffer-p)
9500 (while (or (not label)
9504 (goto-char (point-min))
9506 (regexp-quote (format org-coderef-label-format label))
9508 (when label (message "Label exists already") (sit-for 2))
9509 (setq label (read-string "Code line label: " label)))
9511 (setq link (format org-coderef-label-format label))
9512 (setq gc (- 79 (length link)))
9513 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
9515 (setq link (concat "(" label ")") desc nil)))
9517 ;; We are in the agenda, link to referenced location
9518 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
9519 (let ((m (or (get-text-property (point) 'org-hd-marker)
9520 (get-text-property (point) 'org-marker))))
9522 (org-with-point-at m
9524 (if (org-called-interactively-p 'any)
9525 (call-interactively 'org-store-link)
9526 (org-store-link nil)))))))
9528 ((eq major-mode 'calendar-mode)
9529 (let ((cd (calendar-cursor-to-date)))
9532 (car org-time-stamp-formats)
9534 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
9536 (org-store-link-props :type "calendar" :date cd)))
9538 ((eq major-mode 'help-mode)
9539 (setq link (concat "help:" (save-excursion
9540 (goto-char (point-min))
9541 (looking-at "^[^ ]+")
9543 (org-store-link-props :type "help"))
9545 ((eq major-mode 'w3-mode)
9546 (setq cpltxt (if (and (buffer-name)
9547 (not (string-match "Untitled" (buffer-name))))
9550 link (url-view-url t))
9551 (org-store-link-props :type "w3" :url (url-view-url t)))
9553 ((eq major-mode 'image-mode)
9554 (setq cpltxt (concat "file:"
9555 (abbreviate-file-name buffer-file-name))
9557 (org-store-link-props :type "image" :file buffer-file-name))
9559 ;; In dired, store a link to the file of the current line
9560 ((eq major-mode 'dired-mode)
9561 (let ((file (dired-get-filename nil t)))
9563 (abbreviate-file-name
9564 (expand-file-name (dired-get-filename nil t)))
9565 ;; otherwise, no file so use current directory.
9567 (setq cpltxt (concat "file:" file)
9570 ((setq search (run-hook-with-args-until-success
9571 'org-create-file-search-functions))
9572 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
9574 (setq cpltxt (or description link)))
9576 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
9577 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
9579 ;; Store a link using the target at point
9580 ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
9583 (abbreviate-file-name
9584 (buffer-file-name (buffer-base-buffer)))
9585 "::" (match-string 1))
9587 ((and (featurep 'org-id)
9588 (or (eq org-id-link-to-org-use-id t)
9589 (and (org-called-interactively-p 'any)
9590 (or (eq org-id-link-to-org-use-id 'create-if-interactive)
9591 (and (eq org-id-link-to-org-use-id
9592 'create-if-interactive-and-no-custom-id)
9594 (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
9595 ;; Store a link using the ID at point
9596 (setq link (condition-case nil
9597 (prog1 (org-id-store-link)
9598 (setq desc (or (plist-get org-store-link-plist
9602 ;; Probably before first headline, link only to file
9604 (abbreviate-file-name
9605 (buffer-file-name (buffer-base-buffer))))))))
9607 ;; Just link to current headline
9608 (setq cpltxt (concat "file:"
9609 (abbreviate-file-name
9610 (buffer-file-name (buffer-base-buffer)))))
9611 ;; Add a context search string
9612 (when (org-xor org-context-in-file-links arg)
9613 (let* ((ee (org-element-at-point))
9614 (et (org-element-type ee))
9615 (ev (plist-get (cadr ee) :value))
9616 (ek (plist-get (cadr ee) :key))
9617 (eok (and (stringp ek) (string-match "name" ek))))
9619 ((org-at-heading-p) nil)
9620 ((and (eq et 'keyword) eok) ev)
9621 ((org-region-active-p)
9622 (buffer-substring (region-beginning) (region-end)))))
9623 (when (or (null txt) (string-match "\\S-" txt))
9627 (org-make-org-heading-search-string txt)
9629 desc (or (and (eq et 'keyword) eok ev)
9630 (nth 4 (ignore-errors (org-heading-components)))
9632 (if (string-match "::\\'" cpltxt)
9633 (setq cpltxt (substring cpltxt 0 -2)))
9634 (setq link cpltxt))))
9636 ((buffer-file-name (buffer-base-buffer))
9637 ;; Just link to this file here.
9638 (setq cpltxt (concat "file:"
9639 (abbreviate-file-name
9640 (buffer-file-name (buffer-base-buffer)))))
9641 ;; Add a context string.
9642 (when (org-xor org-context-in-file-links arg)
9643 (setq txt (if (org-region-active-p)
9644 (buffer-substring (region-beginning) (region-end))
9645 (buffer-substring (point-at-bol) (point-at-eol))))
9646 ;; Only use search option if there is some text.
9647 (when (string-match "\\S-" txt)
9649 (concat cpltxt "::" (org-make-org-heading-search-string txt))
9653 ((org-called-interactively-p 'interactive)
9654 (user-error "No method for storing a link from this buffer"))
9656 (t (setq link nil)))
9658 ;; We're done setting link and desc, clean up
9659 (if (consp link) (setq cpltxt (car link) link (cdr link)))
9660 (setq link (or link cpltxt)
9661 desc (or desc cpltxt))
9662 (cond ((equal desc "NONE") (setq desc nil))
9663 ((and desc (string-match org-bracket-link-analytic-regexp desc))
9664 (let ((d0 (match-string 3 desc))
9665 (p0 (match-string 5 desc)))
9667 (replace-regexp-in-string
9668 org-bracket-link-regexp
9670 (if (equal (length (match-string 0 desc))
9671 (length desc)) "*" "")) desc)))))
9674 (if (not (and (or (org-called-interactively-p 'any)
9675 executing-kbd-macro)
9677 (or agenda-link (and link (org-make-link-string link desc)))
9678 (push (list link desc) org-stored-links)
9679 (message "Stored: %s" (or desc link))
9681 (setq link (concat "file:" (abbreviate-file-name
9682 (buffer-file-name)) "::#" custom-id))
9683 (push (list link desc) org-stored-links))
9684 (car org-stored-links))))))
9686 (defun org-store-link-props (&rest plist)
9687 "Store link properties, extract names and addresses."
9689 (when (setq x (plist-get plist :from))
9690 (setq adr (mail-extract-address-components x))
9691 (setq plist (plist-put plist :fromname (car adr)))
9692 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
9693 (when (setq x (plist-get plist :to))
9694 (setq adr (mail-extract-address-components x))
9695 (setq plist (plist-put plist :toname (car adr)))
9696 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
9697 (let ((from (plist-get plist :from))
9698 (to (plist-get plist :to)))
9699 (when (and from to org-from-is-user-regexp)
9701 (plist-put plist :fromto
9702 (if (string-match org-from-is-user-regexp from)
9704 (concat "from %f"))))))
9705 (setq org-store-link-plist plist))
9707 (defun org-add-link-props (&rest plist)
9708 "Add these properties to the link property list."
9711 (setq key (pop plist) value (pop plist))
9712 (setq org-store-link-plist
9713 (plist-put org-store-link-plist key value)))))
9715 (defun org-email-link-description (&optional fmt)
9716 "Return the description part of an email link.
9717 This takes information from `org-store-link-plist' and formats it
9718 according to FMT (default from `org-email-link-description-format')."
9719 (setq fmt (or fmt org-email-link-description-format))
9720 (let* ((p org-store-link-plist)
9721 (to (plist-get p :toaddress))
9722 (from (plist-get p :fromaddress))
9725 (cons "%c" (plist-get p :fromto))
9726 (cons "%F" (plist-get p :from))
9727 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
9728 (cons "%T" (plist-get p :to))
9729 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
9730 (cons "%s" (plist-get p :subject))
9731 (cons "%d" (plist-get p :date))
9732 (cons "%m" (plist-get p :message-id)))))
9733 (when (string-match "%c" fmt)
9734 ;; Check if the user wrote this message
9735 (if (and org-from-is-user-regexp from to
9736 (save-match-data (string-match org-from-is-user-regexp from)))
9737 (setq fmt (replace-match "to %t" t t fmt))
9738 (setq fmt (replace-match "from %f" t t fmt))))
9739 (org-replace-escapes fmt table)))
9741 (defun org-make-org-heading-search-string (&optional string)
9742 "Make search string for the current headline or STRING."
9744 (and (derived-mode-p 'org-mode)
9746 (org-back-to-heading t)
9747 (org-element-property :raw-value (org-element-at-point))))))
9748 (lines org-context-in-file-links))
9749 (or string (setq s (concat "*" s))) ; Add * for headlines
9750 (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
9751 (when (and string (integerp lines) (> lines 0))
9752 (let ((slines (org-split-string s "\n")))
9753 (when (< lines (length slines))
9756 (reverse (nthcdr (- (length slines) lines)
9757 (reverse slines))) "\n")))))
9758 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
9760 (defun org-make-link-string (link &optional description)
9761 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9762 (unless (string-match "\\S-" link)
9763 (error "Empty link"))
9764 (when (and description
9765 (stringp description)
9766 (not (string-match "\\S-" description)))
9767 (setq description nil))
9768 (when (stringp description)
9769 ;; Remove brackets from the description, they are fatal.
9770 (while (string-match "\\[" description)
9771 (setq description (replace-match "{" t t description)))
9772 (while (string-match "\\]" description)
9773 (setq description (replace-match "}" t t description))))
9774 (when (equal link description)
9775 ;; No description needed, it is identical
9776 (setq description nil))
9777 (when (and (not description)
9778 (not (string-match (org-image-file-name-regexp) link))
9779 (not (equal link (org-link-escape link))))
9780 (setq description (org-extract-attributes link)))
9782 (cond ((string-match (org-image-file-name-regexp) link) link)
9783 ((string-match org-link-types-re link)
9784 (concat (match-string 1 link)
9785 (org-link-escape (substring link (match-end 1)))))
9786 (t (org-link-escape link))))
9787 (concat "[[" link "]"
9788 (if description (concat "[" description "]") "")
9791 (defconst org-link-escape-chars
9792 '(?\ ?\[ ?\] ?\; ?\= ?\+)
9793 "List of characters that should be escaped in link.
9794 This is the list that is used for internal purposes.")
9796 (defconst org-link-escape-chars-browser
9798 "List of escapes for characters that are problematic in links.
9799 This is the list that is used before handing over to the browser.")
9801 (defun org-link-escape (text &optional table merge)
9802 "Return percent escaped representation of TEXT.
9803 TEXT is a string with the text to escape.
9804 Optional argument TABLE is a list with characters that should be
9805 escaped. When nil, `org-link-escape-chars' is used.
9806 If optional argument MERGE is set, merge TABLE into
9807 `org-link-escape-chars'."
9810 (mapc (lambda (defchr)
9811 (unless (member defchr table)
9812 (setq table (cons defchr table)))) org-link-escape-chars))
9814 (setq table org-link-escape-chars)))
9817 (if (or (member char table)
9818 (and (or (< char 32) (= char 37) (> char 126))
9820 (mapconcat (lambda (sequence-element)
9821 (format "%%%.2X" sequence-element))
9822 (or (encode-coding-char char 'utf-8)
9823 (error "Unable to percent escape character: %s"
9824 (char-to-string char))) "")
9825 (char-to-string char))) text ""))
9827 (defun org-link-unescape (str)
9828 "Unhex hexified Unicode strings as returned from the JavaScript function
9829 encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
9830 (unless (and (null str) (string= "" str))
9831 (let ((pos 0) (case-fold-search t) unhexed)
9832 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9833 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9834 (setq str (replace-match unhexed t t str))
9835 (setq pos (+ pos (length unhexed))))))
9838 (defun org-link-unescape-compound (hex)
9839 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
9840 Note: this function also decodes single byte encodings like
9841 `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
9843 (let* ((bytes (cdr (split-string hex "%")))
9848 (let* ((val (string-to-number (pop bytes) 16))
9852 ((>= val 252) (cons 6 252))
9853 ((>= val 248) (cons 5 248))
9854 ((>= val 240) (cons 4 240))
9855 ((>= val 224) (cons 3 224))
9856 ((>= val 192) (cons 2 192))
9859 (if (>= val 192) (setq eat (car shift-xor)))
9860 (setq val (logxor val (cdr shift-xor)))
9861 (setq sum (+ (lsh sum (car shift-xor)) val))
9862 (if (> eat 0) (setq eat (- eat 1)))
9864 ((= 0 eat) ;multi byte
9865 (setq ret (concat ret (org-char-to-string sum)))
9867 ((not bytes) ; single byte(s)
9868 (setq ret (org-link-unescape-single-byte-sequence hex))))
9869 )) ;; end (while bytes
9872 (defun org-link-unescape-single-byte-sequence (hex)
9873 "Unhexify hex-encoded single byte character sequences."
9874 (mapconcat (lambda (byte)
9875 (char-to-string (string-to-number byte 16)))
9876 (cdr (split-string hex "%")) ""))
9878 (defun org-xor (a b)
9882 (defun org-fixup-message-id-for-http (s)
9883 "Replace special characters in a message id, so it can be used in an http query."
9884 (when (string-match "%" s)
9885 (setq s (mapconcat (lambda (c)
9888 (char-to-string c)))
9890 (while (string-match "<" s)
9891 (setq s (replace-match "%3C" t t s)))
9892 (while (string-match ">" s)
9893 (setq s (replace-match "%3E" t t s)))
9894 (while (string-match "@" s)
9895 (setq s (replace-match "%40" t t s)))
9898 (defun org-link-prettify (link)
9899 "Return a human-readable representation of LINK.
9900 The car of LINK must be a raw link the cdr of LINK must be either
9901 a link description or nil."
9902 (let ((desc (or (cadr link) "<no description>")))
9903 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9904 "<" (car link) ">")))
9907 (defun org-insert-link-global ()
9908 "Insert a link like Org-mode does.
9909 This command can be called in any mode to insert a link in Org-mode syntax."
9911 (org-load-modules-maybe)
9912 (org-run-like-in-org-mode 'org-insert-link))
9914 (defun org-insert-all-links (&optional keep)
9915 "Insert all links in `org-stored-links'."
9917 (let ((links (copy-sequence org-stored-links)) l)
9918 (while (setq l (if keep (pop links) (pop org-stored-links)))
9920 (org-insert-link nil (car l) (or (cadr l) "<no description>"))
9923 (defun org-link-fontify-links-to-this-file ()
9924 "Fontify links to the current file in `org-stored-links'."
9925 (let ((f (buffer-file-name)) a b)
9926 (setq a (mapcar (lambda(l)
9928 (when (and (string-match "^file:\\(.+\\)::" ll)
9929 (equal f (expand-file-name (match-string 1 ll))))
9932 (when (featurep 'org-id)
9933 (setq b (mapcar (lambda(l)
9935 (when (and (string-match "^id:\\(.+\\)$" ll)
9936 (equal f (expand-file-name
9937 (or (org-id-find-id-file
9938 (match-string 1 ll)) ""))))
9942 (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
9943 (delq nil (append a b)))))
9945 (defvar org-link-links-in-this-file nil)
9946 (defun org-insert-link (&optional complete-file link-location default-description)
9947 "Insert a link. At the prompt, enter the link.
9949 Completion can be used to insert any of the link protocol prefixes like
9952 The history can be used to select a link previously stored with
9953 `org-store-link'. When the empty string is entered (i.e. if you just
9954 press RET at the prompt), the link defaults to the most recently
9955 stored link. As SPC triggers completion in the minibuffer, you need to
9956 use M-SPC or C-q SPC to force the insertion of a space character.
9958 You will also be prompted for a description, and if one is given, it will
9959 be displayed in the buffer instead of the link.
9961 If there is already a link at point, this command will allow you to edit link
9962 and description parts.
9964 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9965 be selected using completion. The path to the file will be relative to the
9966 current directory if the file is in the current directory or a subdirectory.
9967 Otherwise, the link will be the absolute path as completed in the minibuffer
9968 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9969 option `org-link-file-path-type'.
9971 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9972 the current directory or below.
9974 With three \\[universal-argument] prefixes, negate the meaning of
9975 `org-keep-stored-link-after-insertion'.
9977 If `org-make-link-description-function' is non-nil, this function will be
9978 called with the link target, and the result will be the default
9981 If the LINK-LOCATION parameter is non-nil, this value will be
9982 used as the link location instead of reading one interactively.
9984 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9985 be used as the default description."
9987 (let* ((wcf (current-window-configuration))
9988 (origbuf (current-buffer))
9989 (region (if (org-region-active-p)
9990 (buffer-substring (region-beginning) (region-end))))
9991 (remove (and region (list (region-beginning) (region-end))))
9993 tmphist ; byte-compile incorrectly complains about this
9994 (link link-location)
9995 (abbrevs org-link-abbrev-alist-local)
9996 entry file all-prefixes auto-desc)
9998 (link-location) ; specified by arg, just use it.
9999 ((org-in-regexp org-bracket-link-regexp 1)
10000 ;; We do have a link at point, and we are going to edit it.
10001 (setq remove (list (match-beginning 0) (match-end 0)))
10002 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
10003 (setq link (read-string "Link: "
10005 (org-match-string-no-properties 1)))))
10006 ((or (org-in-regexp org-angle-link-re)
10007 (org-in-regexp org-plain-link-re))
10008 ;; Convert to bracket link
10009 (setq remove (list (match-beginning 0) (match-end 0))
10010 link (read-string "Link: "
10011 (org-remove-angle-brackets (match-string 0)))))
10012 ((member complete-file '((4) (16)))
10013 ;; Completing read for file names.
10014 (setq link (org-file-complete-link complete-file)))
10016 ;; Read link, with completion for stored links.
10017 (org-link-fontify-links-to-this-file)
10018 (org-switch-to-buffer-other-window "*Org Links*")
10019 (with-current-buffer "*Org Links*"
10021 (insert "Insert a link.
10022 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
10023 (when org-stored-links
10024 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
10025 (insert (mapconcat 'org-link-prettify
10026 (reverse org-stored-links) "\n")))
10027 (goto-char (point-min)))
10028 (let ((cw (selected-window)))
10029 (select-window (get-buffer-window "*Org Links*" 'visible))
10030 (with-current-buffer "*Org Links*" (setq truncate-lines t))
10031 (unless (pos-visible-in-window-p (point-max))
10032 (org-fit-window-to-buffer))
10033 (and (window-live-p cw) (select-window cw)))
10034 ;; Fake a link history, containing the stored links.
10035 (setq tmphist (append (mapcar 'car org-stored-links)
10036 org-insert-link-history))
10037 (setq all-prefixes (append (mapcar 'car abbrevs)
10038 (mapcar 'car org-link-abbrev-alist)
10043 (org-completing-read
10046 (mapcar (lambda (x) (concat x ":"))
10048 (mapcar 'car org-stored-links))
10051 (caar org-stored-links)))
10052 (if (not (string-match "\\S-" link))
10053 (user-error "No link selected"))
10055 (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
10057 (if (or (member link all-prefixes)
10058 (and (equal ":" (substring link -1))
10059 (member (substring link 0 -1) all-prefixes)
10060 (setq link (substring link 0 -1))))
10061 (setq link (with-current-buffer origbuf
10062 (org-link-try-special-completion link)))))
10063 (set-window-configuration wcf)
10064 (kill-buffer "*Org Links*"))
10065 (setq entry (assoc link org-stored-links))
10066 (or entry (push link org-insert-link-history))
10067 (setq desc (or desc (nth 1 entry)))))
10069 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
10070 (not org-keep-stored-link-after-insertion))
10071 (setq org-stored-links (delq (assoc link org-stored-links)
10072 org-stored-links)))
10074 (if (and (string-match org-plain-link-re link)
10075 (not (string-match org-ts-regexp link)))
10076 ;; URL-like link, normalize the use of angular brackets.
10077 (setq link (org-remove-angle-brackets link)))
10079 ;; Check if we are linking to the current file with a search
10080 ;; option If yes, simplify the link by using only the search
10082 (when (and buffer-file-name
10083 (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
10084 (let* ((path (match-string 1 link))
10085 (case-fold-search nil)
10086 (search (match-string 2 link)))
10088 (if (equal (file-truename buffer-file-name) (file-truename path))
10089 ;; We are linking to this same file, with a search option
10090 (setq link search)))))
10092 ;; Check if we can/should use a relative path. If yes, simplify the link
10093 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
10094 (let* ((type (match-string 1 link))
10095 (path (match-string 2 link))
10097 (case-fold-search nil))
10099 ((or (eq org-link-file-path-type 'absolute)
10100 (equal complete-file '(16)))
10101 (setq path (abbreviate-file-name (expand-file-name path))))
10102 ((eq org-link-file-path-type 'noabbrev)
10103 (setq path (expand-file-name path)))
10104 ((eq org-link-file-path-type 'relative)
10105 (setq path (file-relative-name path)))
10108 (if (string-match (concat "^" (regexp-quote
10110 (file-name-as-directory
10111 default-directory))))
10112 (expand-file-name path))
10113 ;; We are linking a file with relative path name.
10114 (setq path (substring (expand-file-name path)
10116 (setq path (abbreviate-file-name (expand-file-name path)))))))
10117 (setq link (concat type path))
10118 (if (equal desc origpath)
10119 (setq desc path))))
10121 (if org-make-link-description-function
10123 (or (condition-case nil
10124 (funcall org-make-link-description-function link desc)
10125 (error (progn (message "Can't get link description from `%s'"
10126 (symbol-name org-make-link-description-function))
10128 (read-string "Description: " default-description)))
10129 (if default-description (setq desc default-description)
10130 (setq desc (or (and auto-desc desc)
10131 (read-string "Description: " desc)))))
10133 (unless (string-match "\\S-" desc) (setq desc nil))
10134 (if remove (apply 'delete-region remove))
10135 (insert (org-make-link-string link desc))))
10137 (defun org-link-try-special-completion (type)
10138 "If there is completion support for link type TYPE, offer it."
10139 (let ((fun (intern (concat "org-" type "-complete-link"))))
10140 (if (functionp fun)
10142 (read-string "Link (no completion support): " (concat type ":")))))
10144 (defun org-file-complete-link (&optional arg)
10145 "Create a file link using completion."
10147 (setq file (org-iread-file-name "File: "))
10148 (let ((pwd (file-name-as-directory (expand-file-name ".")))
10149 (pwd1 (file-name-as-directory (abbreviate-file-name
10150 (expand-file-name ".")))))
10155 (abbreviate-file-name (expand-file-name file)))))
10156 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
10157 (setq link (concat "file:" (match-string 1 file))))
10158 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
10159 (expand-file-name file))
10161 "file:" (match-string 1 (expand-file-name file)))))
10162 (t (setq link (concat "file:" file)))))
10165 (defun org-iread-file-name (&rest args)
10166 "Read-file-name using `ido-mode' speedup if available.
10167 ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
10168 See `read-file-name' for a description of parameters."
10169 (org-without-partial-completion
10170 (if (and org-completion-use-ido
10171 (fboundp 'ido-read-file-name)
10172 (boundp 'ido-mode) ido-mode
10173 (listp (second args)))
10174 (let ((ido-enter-matching-directory nil))
10175 (apply 'ido-read-file-name args))
10176 (apply 'read-file-name args))))
10178 (defun org-completing-read (&rest args)
10179 "Completing-read with SPACE being a normal character."
10180 (let ((enable-recursive-minibuffers t)
10181 (minibuffer-local-completion-map
10182 (copy-keymap minibuffer-local-completion-map)))
10183 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
10184 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
10185 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
10186 (apply 'org-icompleting-read args)))
10188 (defun org-completing-read-no-i (&rest args)
10189 (let (org-completion-use-ido org-completion-use-iswitchb)
10190 (apply 'org-completing-read args)))
10192 (defun org-iswitchb-completing-read (prompt choices &rest args)
10193 "Use iswitch as a completing-read replacement to choose from choices.
10194 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
10196 (let* ((iswitchb-use-virtual-buffers nil)
10197 (iswitchb-make-buflist-hook
10199 (setq iswitchb-temp-buflist choices))))
10200 (iswitchb-read-buffer prompt)))
10202 (defun org-icompleting-read (&rest args)
10203 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
10204 (org-without-partial-completion
10205 (if (and org-completion-use-ido
10206 (fboundp 'ido-completing-read)
10207 (boundp 'ido-mode) ido-mode
10208 (listp (second args)))
10209 (let ((ido-enter-matching-directory nil))
10210 (apply 'ido-completing-read (concat (car args))
10211 (if (consp (car (nth 1 args)))
10212 (mapcar 'car (nth 1 args))
10215 (if (and org-completion-use-iswitchb
10216 (boundp 'iswitchb-mode) iswitchb-mode
10217 (listp (second args)))
10218 (apply 'org-iswitchb-completing-read (concat (car args))
10219 (if (consp (car (nth 1 args)))
10220 (mapcar 'car (nth 1 args))
10223 (apply 'completing-read args)))))
10225 (defun org-extract-attributes (s)
10226 "Extract the attributes cookie from a string and set as text property."
10227 (let (a attr (start 0) key value)
10229 (when (string-match "{{\\([^}]+\\)}}$" s)
10230 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
10231 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
10232 (setq key (match-string 1 a) value (match-string 2 a)
10233 start (match-end 0)
10234 attr (plist-put attr (intern key) value))))
10235 (org-add-props s nil 'org-attr attr))
10238 ;;; Opening/following a link
10240 (defvar org-link-search-failed nil)
10242 (defvar org-open-link-functions nil
10243 "Hook for functions finding a plain text link.
10244 These functions must take a single argument, the link content.
10245 They will be called for links that look like [[link text][description]]
10246 when LINK TEXT does not have a protocol like \"http:\" and does not look
10247 like a filename (e.g. \"./blue.png\").
10249 These functions will be called *before* Org attempts to resolve the
10250 link by doing text searches in the current buffer - so if you want a
10251 link \"[[target]]\" to still find \"<<target>>\", your function should
10252 handle this as a special case.
10254 When the function does handle the link, it must return a non-nil value.
10255 If it decides that it is not responsible for this link, it must return
10256 nil to indicate that that Org-mode can continue with other options
10257 like exact and fuzzy text search.")
10259 (defun org-next-link (&optional search-backward)
10260 "Move forward to the next link.
10261 If the link is in hidden text, expose it."
10263 (when (and org-link-search-failed (eq this-command last-command))
10264 (goto-char (point-min))
10265 (message "Link search wrapped back to beginning of buffer"))
10266 (setq org-link-search-failed nil)
10267 (let* ((pos (point))
10269 (a (assoc :link ct))
10270 (srch-fun (if search-backward 're-search-backward 're-search-forward)))
10271 (cond (a (goto-char (nth (if search-backward 1 2) a)))
10272 ((looking-at org-any-link-re)
10273 ;; Don't stay stuck at link without an org-link face
10274 (forward-char (if search-backward -1 1))))
10275 (if (funcall srch-fun org-any-link-re nil t)
10277 (goto-char (match-beginning 0))
10278 (if (outline-invisible-p) (org-show-context)))
10280 (setq org-link-search-failed t)
10281 (message "No further link found"))))
10283 (defun org-previous-link ()
10284 "Move backward to the previous link.
10285 If the link is in hidden text, expose it."
10287 (funcall 'org-next-link t))
10289 (defun org-translate-link (s)
10290 "Translate a link string if a translation function has been defined."
10291 (if (and org-link-translation-function
10292 (fboundp org-link-translation-function)
10293 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
10295 (setq s (funcall org-link-translation-function
10296 (match-string 1 s) (match-string 2 s)))
10297 (concat (car s) ":" (cdr s)))
10300 (defun org-translate-link-from-planner (type path)
10301 "Translate a link from Emacs Planner syntax so that Org can follow it.
10302 This is still an experimental function, your mileage may vary."
10304 ((member type '("http" "https" "news" "ftp"))
10305 ;; standard Internet links are the same.
10307 ((and (equal type "irc") (string-match "^//" path))
10308 ;; Planner has two / at the beginning of an irc link, we have 1.
10309 ;; We should have zero, actually....
10310 (setq path (substring path 1)))
10311 ((and (equal type "lisp") (string-match "^/" path))
10312 ;; Planner has a slash, we do not.
10313 (setq type "elisp" path (substring path 1)))
10314 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
10315 ;; A typical message link. Planner has the id after the final slash,
10316 ;; we separate it with a hash mark
10317 (setq path (concat (match-string 1 path) "#"
10318 (org-remove-angle-brackets (match-string 2 path))))))
10321 (defun org-find-file-at-mouse (ev)
10322 "Open file link or URL at mouse."
10324 (mouse-set-point ev)
10325 (org-open-at-point 'in-emacs))
10327 (defun org-open-at-mouse (ev)
10328 "Open file link or URL at mouse.
10329 See the docstring of `org-open-file' for details."
10331 (mouse-set-point ev)
10332 (if (eq major-mode 'org-agenda-mode)
10333 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
10334 (org-open-at-point))
10336 (defvar org-window-config-before-follow-link nil
10337 "The window configuration before following a link.
10338 This is saved in case the need arises to restore it.")
10340 (defvar org-open-link-marker (make-marker)
10341 "Marker pointing to the location where `org-open-at-point' was called.")
10344 (defun org-open-at-point-global ()
10345 "Follow a link like Org-mode does.
10346 This command can be called in any mode to follow a link that has
10349 (org-run-like-in-org-mode 'org-open-at-point))
10352 (defun org-open-link-from-string (s &optional arg reference-buffer)
10353 "Open a link in the string S, as if it was in Org-mode."
10354 (interactive "sLink: \nP")
10355 (let ((reference-buffer (or reference-buffer (current-buffer))))
10357 (let ((org-inhibit-startup (not reference-buffer)))
10360 (goto-char (point-min))
10361 (when reference-buffer
10362 (setq org-link-abbrev-alist-local
10363 (with-current-buffer reference-buffer
10364 org-link-abbrev-alist-local)))
10365 (org-open-at-point arg reference-buffer)))))
10367 (defvar org-open-at-point-functions nil
10368 "Hook that is run when following a link at point.
10370 Functions in this hook must return t if they identify and follow
10371 a link at point. If they don't find anything interesting at point,
10372 they must return nil.")
10374 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
10375 (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
10376 (defun org-open-at-point (&optional arg reference-buffer)
10377 "Open link at or after point.
10378 If there is no link at point, this function will search forward up to
10379 the end of the current line.
10380 Normally, files will be opened by an appropriate application. If the
10381 optional prefix argument ARG is non-nil, Emacs will visit the file.
10382 With a double prefix argument, try to open outside of Emacs, in the
10383 application the system uses for this file type."
10385 ;; if in a code block, then open the block's results
10386 (unless (call-interactively #'org-babel-open-src-block-result)
10387 (org-load-modules-maybe)
10388 (move-marker org-open-link-marker (point))
10389 (setq org-window-config-before-follow-link (current-window-configuration))
10390 (org-remove-occur-highlights nil nil t)
10392 ((and (org-at-heading-p)
10393 (not (org-at-timestamp-p t))
10394 (not (org-in-regexp
10395 (concat org-plain-link-re "\\|"
10396 org-bracket-link-regexp "\\|"
10397 org-angle-link-re "\\|"
10398 "[ \t]:[^ \t\n]+:[ \t]*$")))
10399 (not (get-text-property (point) 'org-linked-text)))
10400 (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
10402 (lk (if (stringp lk0) (list lk0) lk0))
10403 (lkend (cdr lkall)))
10405 (search-forward l nil lkend)
10406 (goto-char (match-beginning 0))
10407 (org-open-at-point))
10409 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
10410 ((run-hook-with-args-until-success 'org-open-at-point-functions))
10411 ((and (org-at-timestamp-p t)
10412 (not (org-in-regexp org-bracket-link-regexp)))
10413 (org-follow-timestamp-link))
10414 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
10415 (not (org-in-regexp org-any-link-re)))
10416 (org-footnote-action))
10418 (let (type path link line search (pos (point)))
10421 (or (org-in-regexp org-plain-link-re)
10422 (skip-chars-forward "^]\n\r"))
10423 (when (org-in-regexp org-bracket-link-regexp 1)
10424 (setq link (org-extract-attributes
10425 (org-link-unescape (org-match-string-no-properties 1))))
10426 (while (string-match " *\n *" link)
10427 (setq link (replace-match " " t t link)))
10428 (setq link (org-link-expand-abbrev link))
10430 ((or (file-name-absolute-p link)
10431 (string-match "^\\.\\.?/" link))
10432 (setq type "file" path link))
10433 ((string-match org-link-re-with-space3 link)
10434 (setq type (match-string 1 link) path (match-string 2 link)))
10435 ((string-match "^help:+\\(.+\\)" link)
10436 (setq type "help" path (match-string 1 link)))
10437 (t (setq type "thisfile" path link)))
10440 (when (get-text-property (point) 'org-linked-text)
10441 (setq type "thisfile"
10442 pos (if (get-text-property (1+ (point)) 'org-linked-text)
10443 (1+ (point)) (point))
10444 path (buffer-substring
10445 (or (previous-single-property-change pos 'org-linked-text)
10447 (or (next-single-property-change pos 'org-linked-text)
10449 ;; Ensure we will search for a <<<radio>>> link, not
10450 ;; a simple reference like <<ref>>
10451 path (concat "<" path))
10455 (when (or (org-in-regexp org-angle-link-re)
10456 (let ((match (org-in-regexp org-plain-link-re)))
10457 ;; Check a plain link is not within a bracket link
10462 (goto-char (car match))
10463 (not (org-in-regexp org-bracket-link-regexp)))))))
10464 (let ((line_ending (save-excursion (end-of-line) (point))))
10465 ;; We are in a line before a plain or bracket link
10466 (or (re-search-forward org-plain-link-re line_ending t)
10467 (re-search-forward org-bracket-link-regexp line_ending t))))
10468 (setq type (match-string 1)
10469 path (org-link-unescape (match-string 2)))
10472 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
10474 path (match-string 1))
10475 (while (string-match ":" path)
10476 (setq path (replace-match "+" t t path)))
10478 (when (org-in-regexp "<\\([^><\n]+\\)>")
10479 (setq type "tree-match"
10480 path (match-string 1))
10483 (user-error "No link found"))
10485 ;; switch back to reference buffer
10486 ;; needed when if called in a temporary buffer through
10487 ;; org-open-link-from-string
10488 (with-current-buffer (or reference-buffer (current-buffer))
10490 ;; Remove any trailing spaces in path
10491 (if (string-match " +\\'" path)
10492 (setq path (replace-match "" t t path)))
10493 (if (and org-link-translation-function
10494 (fboundp org-link-translation-function))
10495 ;; Check if we need to translate the link
10496 (let ((tmp (funcall org-link-translation-function type path)))
10497 (setq type (car tmp) path (cdr tmp))))
10501 ((assoc type org-link-protocols)
10502 (funcall (nth 1 (assoc type org-link-protocols)) path))
10504 ((equal type "help")
10505 (let ((f-or-v (intern path)))
10506 (cond ((fboundp f-or-v)
10507 (describe-function f-or-v))
10509 (describe-variable f-or-v))
10510 (t (error "Not a known function or variable")))))
10512 ((equal type "mailto")
10513 (let ((cmd (car org-link-mailto-program))
10514 (args (cdr org-link-mailto-program)) args1
10515 (address path) (subject "") a)
10516 (if (string-match "\\(.*\\)::\\(.*\\)" path)
10517 (setq address (match-string 1 path)
10518 subject (org-link-escape (match-string 2 path))))
10521 ((not (stringp (car args))) (push (pop args) args1))
10522 (t (setq a (pop args))
10523 (if (string-match "%a" a)
10524 (setq a (replace-match address t t a)))
10525 (if (string-match "%s" a)
10526 (setq a (replace-match subject t t a)))
10528 (apply cmd (nreverse args1))))
10530 ((member type '("http" "https" "ftp" "news"))
10533 (if (org-string-match-p
10534 (concat "[[:nonascii:]"
10535 org-link-escape-chars-browser "]")
10537 (org-link-escape path org-link-escape-chars-browser)
10540 ((string= type "doi")
10542 (concat org-doi-server-url
10543 (if (org-string-match-p
10544 (concat "[[:nonascii:]"
10545 org-link-escape-chars-browser "]")
10547 (org-link-escape path org-link-escape-chars-browser)
10550 ((member type '("message"))
10551 (browse-url (concat type ":" path)))
10553 ((string= type "tags")
10554 (org-tags-view arg path))
10556 ((string= type "tree-match")
10557 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
10559 ((string= type "file")
10560 (if (string-match "::\\([0-9]+\\)\\'" path)
10561 (setq line (string-to-number (match-string 1 path))
10562 path (substring path 0 (match-beginning 0)))
10563 (if (string-match "::\\(.+\\)\\'" path)
10564 (setq search (match-string 1 path)
10565 path (substring path 0 (match-beginning 0)))))
10566 (if (string-match "[*?{]" (file-name-nondirectory path))
10568 (org-open-file path arg line search)))
10570 ((string= type "shell")
10571 (let ((buf (generate-new-buffer "*Org Shell Output"))
10573 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
10574 (string-match org-confirm-shell-link-not-regexp cmd))
10575 (not org-confirm-shell-link-function)
10576 (funcall org-confirm-shell-link-function
10577 (format "Execute \"%s\" in shell? "
10578 (org-add-props cmd nil
10579 'face 'org-warning))))
10581 (message "Executing %s" cmd)
10582 (shell-command cmd buf)
10583 (if (featurep 'midnight)
10584 (setq clean-buffer-list-kill-buffer-names
10585 (cons buf clean-buffer-list-kill-buffer-names))))
10588 ((string= type "elisp")
10590 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
10591 (string-match org-confirm-elisp-link-not-regexp cmd))
10592 (not org-confirm-elisp-link-function)
10593 (funcall org-confirm-elisp-link-function
10594 (format "Execute \"%s\" as elisp? "
10595 (org-add-props cmd nil
10596 'face 'org-warning))))
10597 (message "%s => %s" cmd
10598 (if (equal (string-to-char cmd) ?\()
10600 (call-interactively (read cmd))))
10603 ((and (string= type "thisfile")
10604 (or (run-hook-with-args-until-success
10605 'org-open-link-functions path)
10607 (string-match "^id:" link)
10608 (or (featurep 'org-id) (require 'org-id))
10610 (funcall (nth 1 (assoc "id" org-link-protocols))
10611 (substring path 3))
10614 ((string= type "thisfile")
10616 (switch-to-buffer-other-window
10617 (org-get-buffer-for-internal-link (current-buffer)))
10618 (org-mark-ring-push))
10619 (let ((cmd `(org-link-search
10621 ,(cond ((equal arg '(4)) ''occur)
10622 ((equal arg '(16)) ''org-occur))
10624 (condition-case nil (let ((org-link-search-inhibit-query t))
10626 (error (progn (widen) (eval cmd))))))
10628 (t (browse-url-at-point)))))))
10629 (move-marker org-open-link-marker nil)
10630 (run-hook-with-args 'org-follow-link-hook)))
10632 (defsubst org-uniquify (list)
10633 "Non-destructively remove duplicate elements from LIST."
10634 (let ((res (copy-sequence list))) (delete-dups res)))
10636 (defun org-offer-links-in-entry (buffer marker &optional nth zero)
10637 "Offer links in the current entry and return the selected link.
10638 If there is only one link, return it.
10639 If NTH is an integer, return the NTH link found.
10640 If ZERO is a string, check also this string for a link, and if
10641 there is one, return it."
10642 (with-current-buffer buffer
10647 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
10648 "\\(" org-angle-link-re "\\)\\|"
10649 "\\(" org-plain-link-re "\\)"))
10651 (in-emacs (if (integerp nth) nil nth))
10652 have-zero end links link c)
10653 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
10654 (push (match-string 0 zero) links)
10655 (setq cnt (1- cnt) have-zero t))
10657 (org-back-to-heading t)
10658 (setq end (save-excursion (outline-next-heading) (point)))
10659 (while (re-search-forward re end t)
10660 (push (match-string 0) links))
10661 (setq links (org-uniquify (reverse links))))
10664 (message "No links"))
10665 ((equal (length links) 1)
10666 (setq link (car links)))
10667 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
10668 (setq link (nth (if have-zero nth (1- nth)) links)))
10669 (t ; we have to select a link
10671 (save-window-excursion
10672 (delete-other-windows)
10673 (with-output-to-temp-buffer "*Select Link*"
10675 (if (not (string-match org-bracket-link-regexp l))
10676 (princ (format "[%c] %s\n" (incf cnt)
10677 (org-remove-angle-brackets l)))
10679 (princ (format "[%c] %s (%s)\n" (incf cnt)
10680 (match-string 3 l) (match-string 1 l)))
10681 (princ (format "[%c] %s\n" (incf cnt)
10682 (match-string 1 l))))))
10684 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
10685 (message "Select link to open, RET to open all:")
10686 (setq c (read-char-exclusive))
10687 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
10688 (when (equal c ?q) (error "Abort"))
10689 (if (equal c ?\C-m)
10691 (setq nth (- c ?0))
10692 (if have-zero (setq nth (1+ nth)))
10693 (unless (and (integerp nth) (>= (length links) nth))
10694 (user-error "Invalid link selection"))
10695 (setq link (nth (1- nth) links)))))
10696 (cons link end))))))
10698 ;; Add special file links that specify the way of opening
10700 (org-add-link-type "file+sys" 'org-open-file-with-system)
10701 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
10702 (defun org-open-file-with-system (path)
10703 "Open file at PATH using the system way of opening it."
10704 (org-open-file path 'system))
10705 (defun org-open-file-with-emacs (path)
10706 "Open file at PATH in Emacs."
10707 (org-open-file path 'emacs))
10712 (defvar org-create-file-search-functions nil
10713 "List of functions to construct the right search string for a file link.
10714 These functions are called in turn with point at the location to
10715 which the link should point.
10717 A function in the hook should first test if it would like to
10718 handle this file type, for example by checking the `major-mode'
10719 or the file extension. If it decides not to handle this file, it
10720 should just return nil to give other functions a chance. If it
10721 does handle the file, it must return the search string to be used
10722 when following the link. The search string will be part of the
10723 file link, given after a double colon, and `org-open-at-point'
10724 will automatically search for it. If special measures must be
10725 taken to make the search successful, another function should be
10726 added to the companion hook `org-execute-file-search-functions',
10729 A function in this hook may also use `setq' to set the variable
10730 `description' to provide a suggestion for the descriptive text to
10731 be used for this link when it gets inserted into an Org-mode
10732 buffer with \\[org-insert-link].")
10734 (defvar org-execute-file-search-functions nil
10735 "List of functions to execute a file search triggered by a link.
10737 Functions added to this hook must accept a single argument, the
10738 search string that was part of the file link, the part after the
10739 double colon. The function must first check if it would like to
10740 handle this search, for example by checking the `major-mode' or
10741 the file extension. If it decides not to handle this search, it
10742 should just return nil to give other functions a chance. If it
10743 does handle the search, it must return a non-nil value to keep
10744 other functions from trying.
10746 Each function can access the current prefix argument through the
10747 variable `current-prefix-arg'. Note that a single prefix is used
10748 to force opening a link in Emacs, so it may be good to only use a
10749 numeric or double prefix to guide the search function.
10751 In case this is needed, a function in this hook can also restore
10752 the window configuration before `org-open-at-point' was called using:
10754 (set-window-configuration org-window-config-before-follow-link)")
10756 (defun org-link-search (s &optional type avoid-pos stealth)
10757 "Search for a link search option.
10758 If S is surrounded by forward slashes, it is interpreted as a
10759 regular expression. In org-mode files, this will create an `org-occur'
10760 sparse tree. In ordinary files, `occur' will be used to list matches.
10761 If the current buffer is in `dired-mode', grep will be used to search
10762 in all files. If AVOID-POS is given, ignore matches near that position.
10764 When optional argument STEALTH is non-nil, do not modify
10765 visibility around point, thus ignoring
10766 `org-show-hierarchy-above', `org-show-following-heading' and
10767 `org-show-siblings' variables."
10768 (let ((case-fold-search t)
10769 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
10770 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
10771 (append '(("") (" ") ("\t") ("\n"))
10772 org-emphasis-alist)
10775 (pre nil) (post nil)
10776 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
10778 ;; First check if there are any special search functions
10779 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
10780 ;; Now try the builtin stuff
10781 ((and (equal (string-to-char s0) ?#)
10784 (goto-char (point-min))
10787 (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
10788 (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
10789 (setq type 'dedicated
10790 pos (match-beginning 0))))
10791 ;; There is an exact target for this
10793 (org-back-to-heading t)))
10795 (goto-char (point-min))
10798 (concat "<<" (regexp-quote s0) ">>") nil t)
10799 (setq type 'dedicated
10800 pos (match-beginning 0))))
10801 ;; There is an exact target for this
10804 (goto-char (point-min))
10807 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
10808 (setq type 'dedicated pos (match-beginning 0))))
10809 ;; Found an element with a matching #+name affiliated keyword.
10811 ((and (string-match "^(\\(.*\\))$" s0)
10813 (goto-char (point-min))
10816 (concat "[^[]" (regexp-quote
10817 (format org-coderef-label-format
10818 (match-string 1 s0))))
10820 (setq type 'dedicated
10821 pos (1+ (match-beginning 0))))))
10822 ;; There is a coderef target for this
10824 ((string-match "^/\\(.*\\)/$" s)
10825 ;; A regular expression
10827 ((derived-mode-p 'org-mode)
10828 (org-occur (match-string 1 s)))
10829 (t (org-do-occur (match-string 1 s)))))
10830 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
10831 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
10832 (goto-char (point-min))
10834 ((let (case-fold-search)
10835 (re-search-forward (format org-complex-heading-regexp-format
10838 ;; OK, found a match
10839 (setq type 'dedicated)
10840 (goto-char (match-beginning 0)))
10841 ((and (not org-link-search-inhibit-query)
10842 (eq org-link-search-must-match-exact-headline 'query-to-create)
10843 (y-or-n-p "No match - create this as a new heading? "))
10844 (goto-char (point-max))
10845 (or (bolp) (newline))
10846 (insert "* " s "\n")
10847 (beginning-of-line 0))
10850 (error "No match"))))
10852 ;; A normal search string
10853 (when (equal (string-to-char s) ?*)
10854 ;; Anchor on headlines, post may include tags.
10855 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
10856 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
10857 s (substring s 1)))
10858 (remove-text-properties
10860 '(face nil mouse-face nil keymap nil fontified nil) s)
10861 ;; Make a series of regular expressions to find a match
10862 (setq words (org-split-string s "[ \n\r\t]+")
10864 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
10865 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
10867 re2a_ (concat "\\(" (mapconcat 'downcase words
10868 "[ \t\r\n]+") "\\)[ \t\r\n]")
10869 re2a (concat "[ \t\r\n]" re2a_)
10870 re4_ (concat "\\(" (mapconcat 'downcase words
10871 "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
10872 re4 (concat "[^a-zA-Z_]" re4_)
10874 re1 (concat pre re2 post)
10875 re3 (concat pre (if pre re4_ re4) post)
10876 re5 (concat pre ".*" re4)
10877 re2 (concat pre re2)
10878 re2a (concat pre (if pre re2a_ re2a))
10879 re4 (concat pre (if pre re4_ re4))
10880 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
10881 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10884 ((eq type 'org-occur) (org-occur reall))
10885 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10886 (t (goto-char (point-min))
10888 (if (or (and (org-search-not-self 1 re0 nil t)
10889 (setq type 'dedicated))
10890 (org-search-not-self 1 re1 nil t)
10891 (org-search-not-self 1 re2 nil t)
10892 (org-search-not-self 1 re2a nil t)
10893 (org-search-not-self 1 re3 nil t)
10894 (org-search-not-self 1 re4 nil t)
10895 (org-search-not-self 1 re5 nil t))
10896 (goto-char (match-beginning 1))
10898 (error "No match"))))))
10899 (and (derived-mode-p 'org-mode)
10901 (org-show-context 'link-search))
10904 (defun org-search-not-self (group &rest args)
10905 "Execute `re-search-forward', but only accept matches that do not
10906 enclose the position of `org-open-link-marker'."
10907 (let ((m org-open-link-marker))
10909 (while (apply #'re-search-forward args)
10910 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10911 (goto-char (match-end group))
10912 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10913 (> (match-beginning 0) (marker-position m))
10914 (< (match-end 0) (marker-position m)))
10916 (or (not (org-in-regexp
10917 org-bracket-link-analytic-regexp 1))
10918 (not (match-end 4)) ; no description
10919 (and (<= (match-beginning 4) (point))
10920 (>= (match-end 4) (point))))))
10921 (throw 'exit (point))))))))
10923 (defun org-get-buffer-for-internal-link (buffer)
10924 "Return a buffer to be used for displaying the link target of internal links."
10926 ((not org-display-internal-link-with-indirect-buffer)
10928 ((string-match "(Clone)$" (buffer-name buffer))
10929 (message "Buffer is already a clone, not making another one")
10930 ;; we also do not modify visibility in this case
10932 (t ; make a new indirect buffer for displaying the link
10933 (let* ((bn (buffer-name buffer))
10934 (ibn (concat bn "(Clone)"))
10935 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10936 (with-current-buffer ib (org-overview))
10939 (defun org-do-occur (regexp &optional cleanup)
10940 "Call the Emacs command `occur'.
10941 If CLEANUP is non-nil, remove the printout of the regular expression
10942 in the *Occur* buffer. This is useful if the regex is long and not useful
10946 (let ((cwin (selected-window)) win beg end)
10947 (when (setq win (get-buffer-window "*Occur*"))
10948 (select-window win))
10949 (goto-char (point-min))
10950 (when (re-search-forward "match[a-z]+" nil t)
10951 (setq beg (match-end 0))
10952 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10953 (setq end (1- (match-beginning 0)))))
10954 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10955 (goto-char (point-min))
10956 (select-window cwin))))
10958 ;;; The mark ring for links jumps
10960 (defvar org-mark-ring nil
10961 "Mark ring for positions before jumps in Org-mode.")
10962 (defvar org-mark-ring-last-goto nil
10963 "Last position in the mark ring used to go back.")
10964 ;; Fill and close the ring
10965 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10966 (loop for i from 1 to org-mark-ring-length do
10967 (push (make-marker) org-mark-ring))
10968 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10971 (defun org-mark-ring-push (&optional pos buffer)
10972 "Put the current position or POS into the mark ring and rotate it."
10974 (setq pos (or pos (point)))
10975 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10976 (move-marker (car org-mark-ring)
10978 (or buffer (current-buffer)))
10980 (substitute-command-keys
10981 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10983 (defun org-mark-ring-goto (&optional n)
10984 "Jump to the previous position in the mark ring.
10985 With prefix arg N, jump back that many stored positions. When
10986 called several times in succession, walk through the entire ring.
10987 Org-mode commands jumping to a different position in the current file,
10988 or to another Org-mode file, automatically push the old position
10992 (if (eq last-command this-command)
10993 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10994 (setq p org-mark-ring))
10995 (setq org-mark-ring-last-goto p)
10997 (org-pop-to-buffer-same-window (marker-buffer m))
10999 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
11001 (defun org-remove-angle-brackets (s)
11002 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
11003 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
11005 (defun org-add-angle-brackets (s)
11006 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
11007 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
11009 (defun org-remove-double-quotes (s)
11010 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
11011 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
11014 ;;; Following specific links
11016 (defun org-follow-timestamp-link ()
11017 "Open an agenda view for the time-stamp date/range at point."
11019 ((org-at-date-range-p t)
11020 (let ((org-agenda-start-on-weekday)
11021 (t1 (match-string 1))
11022 (t2 (match-string 2)) tt1 tt2)
11023 (setq tt1 (time-to-days (org-time-string-to-time t1))
11024 tt2 (time-to-days (org-time-string-to-time t2)))
11025 (let ((org-agenda-buffer-tmp-name
11026 (format "*Org Agenda(a:%s)"
11027 (concat (substring t1 0 10) "--" (substring t2 0 10)))))
11028 (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
11029 ((org-at-timestamp-p t)
11030 (let ((org-agenda-buffer-tmp-name
11031 (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
11032 (org-agenda-list nil (time-to-days (org-time-string-to-time
11033 (substring (match-string 1) 0 10)))
11035 (t (error "This should not happen"))))
11038 ;;; Following file links
11039 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
11040 (declare-function mailcap-extension-to-mime "mailcap" (extn))
11041 (declare-function mailcap-mime-info
11042 "mailcap" (string &optional request no-decode))
11043 (defvar org-wait nil)
11044 (defun org-open-file (path &optional in-emacs line search)
11045 "Open the file at PATH.
11046 First, this expands any special file name abbreviations. Then the
11047 configuration variable `org-file-apps' is checked if it contains an
11048 entry for this file type, and if yes, the corresponding command is launched.
11050 If no application is found, Emacs simply visits the file.
11052 With optional prefix argument IN-EMACS, Emacs will visit the file.
11053 With a double \\[universal-argument] \\[universal-argument] \
11054 prefix arg, Org tries to avoid opening in Emacs
11055 and to use an external application to visit the file.
11057 Optional LINE specifies a line to go to, optional SEARCH a string
11058 to search for. If LINE or SEARCH is given, the file will be
11059 opened in Emacs, unless an entry from org-file-apps that makes
11060 use of groups in a regexp matches.
11062 If you want to change the way frames are used when following a
11063 link, please customize `org-link-frame-setup'.
11065 If the file does not exist, an error is thrown."
11066 (let* ((file (if (equal path "")
11068 (substitute-in-file-name (expand-file-name path))))
11069 (file-apps (append org-file-apps (org-default-apps)))
11070 (apps (org-remove-if
11071 'org-file-apps-entry-match-against-dlink-p file-apps))
11072 (apps-dlink (org-remove-if-not
11073 'org-file-apps-entry-match-against-dlink-p file-apps))
11074 (remp (and (assq 'remote apps) (org-file-remote-p file)))
11075 (dirp (if remp nil (file-directory-p file)))
11076 (file (if (and dirp org-open-directory-means-index-dot-org)
11077 (concat (file-name-as-directory file) "index.org")
11079 (a-m-a-p (assq 'auto-mode apps))
11080 (dfile (downcase file))
11081 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
11082 (link (cond ((and (eq line nil)
11086 (concat file "::" (number-to-string line)))
11088 (concat file "::" search))))
11089 (dlink (downcase link))
11090 (old-buffer (current-buffer))
11092 (old-mode major-mode)
11093 ext cmd link-match-data)
11094 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
11095 (setq ext (match-string 1 dfile))
11096 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
11097 (setq ext (match-string 1 dfile))))
11099 ((member in-emacs '((16) system))
11100 (setq cmd (cdr (assoc 'system apps))))
11101 (in-emacs (setq cmd 'emacs))
11103 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
11104 (and dirp (cdr (assoc 'directory apps)))
11105 ; first, try matching against apps-dlink
11106 ; if we get a match here, store the match data for later
11107 (let ((match (assoc-default dlink apps-dlink
11110 (progn (setq link-match-data (match-data))
11112 (progn (setq in-emacs (or in-emacs line search))
11113 nil))) ; if we have no match in apps-dlink,
11114 ; always open the file in emacs if line or search
11115 ; is given (for backwards compatibility)
11116 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
11118 (cdr (assoc ext apps))
11119 (cdr (assoc t apps))))))
11120 (when (eq cmd 'system)
11121 (setq cmd (cdr (assoc 'system apps))))
11122 (when (eq cmd 'default)
11123 (setq cmd (cdr (assoc t apps))))
11124 (when (eq cmd 'mailcap)
11126 (mailcap-parse-mailcaps)
11127 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
11128 (command (mailcap-mime-info mime-type)))
11129 (if (stringp command)
11131 (setq cmd 'emacs))))
11132 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
11133 (not (file-exists-p file))
11134 (not org-open-non-existing-files))
11135 (user-error "No such file: %s" file))
11137 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
11138 ;; Remove quotes around the file name - we'll use shell-quote-argument.
11139 (while (string-match "['\"]%s['\"]" cmd)
11140 (setq cmd (replace-match "%s" t t cmd)))
11141 (while (string-match "%s" cmd)
11142 (setq cmd (replace-match
11144 (shell-quote-argument
11145 (convert-standard-filename file)))
11148 ;; Replace "%1", "%2" etc. in command with group matches from regex
11150 (let ((match-index 1)
11151 (number-of-groups (- (/ (length link-match-data) 2) 1)))
11152 (set-match-data link-match-data)
11153 (while (<= match-index number-of-groups)
11154 (let ((regex (concat "%" (number-to-string match-index)))
11155 (replace-with (match-string match-index dlink)))
11156 (while (string-match regex cmd)
11157 (setq cmd (replace-match replace-with t t cmd))))
11158 (setq match-index (+ match-index 1)))))
11160 (save-window-excursion
11161 (message "Running %s...done" cmd)
11162 (start-process-shell-command cmd nil cmd)
11163 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
11166 (funcall (cdr (assq 'file org-link-frame-setup)) file)
11168 (if line (org-goto-line line)
11169 (if search (org-link-search search))))
11171 (let ((file (convert-standard-filename file)))
11173 (set-match-data link-match-data)
11175 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
11176 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
11177 (or (not (equal old-buffer (current-buffer)))
11178 (not (equal old-pos (point))))
11179 (org-mark-ring-push old-pos old-buffer))))
11181 (defun org-file-apps-entry-match-against-dlink-p (entry)
11182 "This function returns non-nil if `entry' uses a regular
11183 expression which should be matched against the whole link by
11186 It assumes that is the case when the entry uses a regular
11187 expression which has at least one grouping construct and the
11188 action is either a lisp form or a command string containing
11189 `%1', i.e. using at least one subexpression match as a
11191 (let ((selector (car entry))
11192 (action (cdr entry)))
11193 (if (stringp selector)
11194 (and (> (regexp-opt-depth selector) 0)
11195 (or (and (stringp action)
11196 (string-match "%[0-9]" action))
11200 (defun org-default-apps ()
11201 "Return the default applications for this operating system."
11203 ((eq system-type 'darwin)
11204 org-file-apps-defaults-macosx)
11205 ((eq system-type 'windows-nt)
11206 org-file-apps-defaults-windowsnt)
11207 (t org-file-apps-defaults-gnu)))
11209 (defun org-apps-regexp-alist (list &optional add-auto-mode)
11210 "Convert extensions to regular expressions in the cars of LIST.
11211 Also, weed out any non-string entries, because the return value is used
11212 only for regexp matching.
11213 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
11214 point to the symbol `emacs', indicating that the file should
11215 be opened in Emacs."
11218 (mapcar (lambda (x)
11219 (if (not (stringp (car x)))
11221 (if (string-match "\\W" (car x))
11223 (cons (concat "\\." (car x) "\\'") (cdr x)))))
11226 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
11228 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
11229 (defun org-file-remote-p (file)
11230 "Test whether FILE specifies a location on a remote system.
11231 Return non-nil if the location is indeed remote.
11233 For example, the filename \"/user@host:/foo\" specifies a location
11234 on the system \"/user@host:\"."
11235 (cond ((fboundp 'file-remote-p)
11236 (file-remote-p file))
11237 ((fboundp 'tramp-handle-file-remote-p)
11238 (tramp-handle-file-remote-p file))
11239 ((and (boundp 'ange-ftp-name-format)
11240 (string-match (car ange-ftp-name-format) file))
11246 (defun org-get-org-file ()
11247 "Read a filename, with default directory `org-directory'."
11248 (let ((default (or org-default-notes-file remember-data-file)))
11249 (read-file-name (format "File name [%s]: " default)
11250 (file-name-as-directory org-directory)
11253 (defun org-notes-order-reversed-p ()
11254 "Check if the current file should receive notes in reversed order."
11256 ((not org-reverse-note-order) nil)
11257 ((eq t org-reverse-note-order) t)
11258 ((not (listp org-reverse-note-order)) nil)
11260 (dolist (entry org-reverse-note-order)
11261 (if (string-match (car entry) buffer-file-name)
11262 (throw 'exit (cdr entry))))))))
11264 (defvar org-refile-target-table nil
11265 "The list of refile targets, created by `org-refile'.")
11267 (defvar org-agenda-new-buffers nil
11268 "Buffers created to visit agenda files.")
11270 (defvar org-refile-cache nil
11271 "Cache for refile targets.")
11273 (defvar org-refile-markers nil
11274 "All the markers used for caching refile locations.")
11276 (defun org-refile-marker (pos)
11277 "Get a new refile marker, but only if caching is in use."
11278 (if (not org-refile-use-cache)
11280 (let ((m (make-marker)))
11281 (move-marker m pos)
11282 (push m org-refile-markers)
11285 (defun org-refile-cache-clear ()
11286 "Clear the refile cache and disable all the markers."
11287 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
11288 (setq org-refile-markers nil)
11289 (setq org-refile-cache nil)
11290 (message "Refile cache has been cleared"))
11292 (defun org-refile-cache-check-set (set)
11293 "Check if all the markers in the cache still have live buffers."
11296 (while (and set (setq marker (nth 3 (pop set))))
11297 ;; If `org-refile-use-outline-path' is 'file, marker may be nil
11298 (when (and marker (null (marker-buffer marker)))
11299 (message "Please regenerate the refile cache with `C-0 C-c C-w'")
11301 (throw 'exit nil)))
11304 (defun org-refile-cache-put (set &rest identifiers)
11305 "Push the refile targets SET into the cache, under IDENTIFIERS."
11306 (let* ((key (sha1 (prin1-to-string identifiers)))
11307 (entry (assoc key org-refile-cache)))
11310 (push (cons key set) org-refile-cache))))
11312 (defun org-refile-cache-get (&rest identifiers)
11313 "Retrieve the cached value for refile targets given by IDENTIFIERS."
11315 ((not org-refile-cache) nil)
11316 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
11318 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
11319 org-refile-cache))))
11320 (and set (org-refile-cache-check-set set) set)))))
11322 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
11323 "Produce a table with refile targets."
11324 (let ((case-fold-search nil)
11325 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
11326 (entries (or org-refile-targets '((nil . (:level . 1)))))
11327 targets tgs txt re files desc descre fast-path-p level pos0)
11328 (message "Getting targets...")
11329 (with-current-buffer (or default-buffer (current-buffer))
11330 (dolist (entry entries)
11331 (setq files (car entry) desc (cdr entry))
11332 (setq fast-path-p nil)
11334 ((null files) (setq files (list (current-buffer))))
11335 ((eq files 'org-agenda-files)
11336 (setq files (org-agenda-files 'unrestricted)))
11337 ((and (symbolp files) (fboundp files))
11338 (setq files (funcall files)))
11339 ((and (symbolp files) (boundp files))
11340 (setq files (symbol-value files))))
11341 (if (stringp files) (setq files (list files)))
11343 ((eq (car desc) :tag)
11344 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
11345 ((eq (car desc) :todo)
11346 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
11347 ((eq (car desc) :regexp)
11348 (setq descre (cdr desc)))
11349 ((eq (car desc) :level)
11350 (setq descre (concat "^\\*\\{" (number-to-string
11351 (if org-odd-levels-only
11352 (1- (* 2 (cdr desc)))
11355 ((eq (car desc) :maxlevel)
11356 (setq fast-path-p t)
11357 (setq descre (concat "^\\*\\{1," (number-to-string
11358 (if org-odd-levels-only
11359 (1- (* 2 (cdr desc)))
11362 (t (error "Bad refiling target description %s" desc)))
11364 (with-current-buffer
11365 (if (bufferp f) f (org-get-agenda-file-buffer f))
11367 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
11369 (if (bufferp f) (setq f (buffer-file-name
11370 (buffer-base-buffer f))))
11371 (setq f (and f (expand-file-name f)))
11372 (if (eq org-refile-use-outline-path 'file)
11373 (push (list (file-name-nondirectory f) f nil nil) tgs))
11377 (goto-char (point-min))
11378 (while (re-search-forward descre nil t)
11379 (goto-char (setq pos0 (point-at-bol)))
11381 (when org-refile-target-verify-function
11383 (or (funcall org-refile-target-verify-function)
11385 (when (and (looking-at org-complex-heading-regexp)
11386 (not (member (match-string 4) excluded-entries))
11388 (setq level (org-reduced-level
11389 (- (match-end 1) (match-beginning 1)))
11390 txt (org-link-display-format (match-string 4))
11391 txt (replace-regexp-in-string "\\( *[[0-9]+/?[0-9]*%?]\\)+$" "" txt)
11392 re (format org-complex-heading-regexp-format
11393 (regexp-quote (match-string 4))))
11394 (when org-refile-use-outline-path
11395 (setq txt (mapconcat
11398 (if (eq org-refile-use-outline-path
11400 (list (file-name-nondirectory
11402 (buffer-base-buffer))))
11403 (if (eq org-refile-use-outline-path
11405 (list (buffer-file-name
11406 (buffer-base-buffer)))))
11407 (org-get-outline-path fast-path-p
11411 (push (list txt f re (org-refile-marker (point)))
11413 (when (= (point) pos0)
11414 ;; verification function has not moved point
11415 (goto-char (point-at-eol))))))))
11416 (when org-refile-use-cache
11417 (org-refile-cache-put tgs (buffer-file-name) descre))
11418 (setq targets (append tgs targets))))))
11419 (message "Getting targets...done")
11420 (nreverse targets)))
11422 (defun org-protect-slash (s)
11423 (while (string-match "/" s)
11424 (setq s (replace-match "\\" t t s)))
11427 (defvar org-olpa (make-vector 20 nil))
11429 (defun org-get-outline-path (&optional fastp level heading)
11430 "Return the outline path to the current entry, as a list.
11432 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
11433 routine which makes outline path derivations for an entire file,
11434 avoiding backtracing. Refile target collection makes use of that."
11438 (error "Outline path failure, more than 19 levels"))
11439 (loop for i from level upto 19 do
11440 (aset org-olpa i nil))
11442 (delq nil (append org-olpa nil))
11443 (aset org-olpa level heading)))
11444 (let (rtn case-fold-search)
11448 (while (org-up-heading-safe)
11449 (when (looking-at org-complex-heading-regexp)
11451 (replace-regexp-in-string
11452 ;; Remove statistical/checkboxes cookies
11453 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
11454 (org-match-string-no-properties 4)))
11458 (defun org-format-outline-path (path &optional width prefix separator)
11459 "Format the outline path PATH for display.
11460 WIDTH is the maximum number of characters that is available.
11461 PREFIX is a prefix to be included in the returned string,
11462 such as the file name.
11463 SEPARATOR is inserted between the different parts of the path,
11464 the default is \"/\"."
11465 (setq width (or width 79))
11466 (if prefix (setq width (- width (length prefix))))
11469 (let* ((nsteps (length path))
11470 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
11471 (maxwidth (if (<= total-width width)
11472 10000 ;; everything fits
11473 ;; we need to shorten the level headings
11474 (/ (- width nsteps) nsteps)))
11475 (org-odd-levels-only nil)
11477 (total (1+ (length prefix))))
11478 (setq maxwidth (max maxwidth 10))
11480 (if prefix (or separator "/"))
11484 (if (and (= n nsteps) (< maxwidth 10000))
11485 (setq maxwidth (- total-width total)))
11486 (if (< (length h) maxwidth)
11487 (progn (setq total (+ total (length h) 1)) h)
11488 (setq h (substring h 0 (- maxwidth 2))
11489 total (+ total maxwidth 1))
11490 (if (string-match "[ \t]+\\'" h)
11491 (setq h (substring h 0 (match-beginning 0))))
11492 (setq h (concat h "..")))
11493 (org-add-props h nil 'face
11494 (nth (% (1- n) org-n-level-faces)
11497 path (or separator "/"))))))
11499 (defun org-display-outline-path (&optional file current separator just-return-string)
11500 "Display the current outline path in the echo area.
11502 If FILE is non-nil, prepend the output with the file name.
11503 If CURRENT is non-nil, append the current heading to the output.
11504 SEPARATOR is passed through to `org-format-outline-path'. It separates
11505 the different parts of the path and defaults to \"/\".
11506 If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
11508 (let* (case-fold-search
11509 (bfn (buffer-file-name (buffer-base-buffer)))
11510 (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
11512 (if current (setq path (append path
11514 (org-back-to-heading t)
11515 (if (looking-at org-complex-heading-regexp)
11516 (list (match-string 4)))))))
11518 (org-format-outline-path
11521 (and file bfn (concat (file-name-nondirectory bfn) separator))
11523 (if just-return-string
11524 (org-no-properties res)
11525 (org-unlogged-message "%s" res))))
11527 (defvar org-refile-history nil
11528 "History for refiling operations.")
11530 (defvar org-after-refile-insert-hook nil
11531 "Hook run after `org-refile' has inserted its stuff at the new location.
11532 Note that this is still *before* the stuff will be removed from
11533 the *old* location.")
11535 (defvar org-capture-last-stored-marker)
11536 (defvar org-refile-keep nil
11537 "Non-nil means `org-refile' will copy instead of refile.")
11540 "Like `org-refile', but copy."
11542 (let ((org-refile-keep t))
11543 (funcall 'org-refile nil nil nil "Copy")))
11545 (defun org-refile (&optional goto default-buffer rfloc msg)
11546 "Move the entry or entries at point to another heading.
11547 The list of target headings is compiled using the information in
11548 `org-refile-targets', which see.
11550 At the target location, the entry is filed as a subitem of the target
11551 heading. Depending on `org-reverse-note-order', the new subitem will
11552 either be the first or the last subitem.
11554 If there is an active region, all entries in that region will be moved.
11555 However, the region must fulfill the requirement that the first heading
11556 is the first one sets the top-level of the moved text - at most siblings
11557 below it are allowed.
11559 With prefix arg GOTO, the command will only visit the target location
11560 and not actually move anything.
11562 With a double prefix arg \\[universal-argument] \\[universal-argument], \
11563 go to the location where the last refiling operation has put the subtree.
11565 With a numeric prefix argument of `2', refile to the running clock.
11567 With a numeric prefix argument of `3', emulate `org-refile-keep'
11568 being set to t and copy to the target location, don't move it.
11569 Beware that keeping refiled entries may result in duplicated ID
11572 RFLOC can be a refile location obtained in a different way.
11574 MSG is a string to replace \"Refile\" in the default prompt with
11575 another verb. E.g. `org-copy' sets this parameter to \"Copy\".
11577 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
11579 If you are using target caching (see `org-refile-use-cache'),
11580 you have to clear the target cache in order to find new targets.
11581 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
11582 prefix argument (`C-u C-u C-u C-c C-w')."
11585 (if (member goto '(0 (64)))
11586 (org-refile-cache-clear)
11587 (let* ((actionmsg (cond (msg msg)
11588 ((equal goto 3) "Refile (and keep)")
11590 (cbuf (current-buffer))
11591 (regionp (org-region-active-p))
11592 (region-start (and regionp (region-beginning)))
11593 (region-end (and regionp (region-end)))
11594 (filename (buffer-file-name (buffer-base-buffer cbuf)))
11595 (org-refile-keep (if (equal goto 3) t org-refile-keep))
11596 pos it nbuf file re level reversed)
11597 (setq last-command nil)
11599 (goto-char region-start)
11600 (or (bolp) (goto-char (point-at-bol)))
11601 (setq region-start (point))
11602 (unless (or (org-kill-is-subtree-p
11603 (buffer-substring region-start region-end))
11604 (prog1 org-refile-active-region-within-subtree
11605 (let ((s (point-at-eol)))
11606 (org-toggle-heading)
11607 (setq region-end (+ (- (point-at-eol) s) region-end)))))
11608 (user-error "The region is not a (sequence of) subtree(s)")))
11609 (if (equal goto '(16))
11610 (org-refile-goto-last-stored)
11612 (and (equal goto 2)
11613 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
11615 (setq it (list (or org-clock-heading "running clock")
11617 (marker-buffer org-clock-hd-marker))
11619 (marker-position org-clock-hd-marker)))
11622 (let (heading-text)
11624 (unless (and goto (listp goto))
11625 (org-back-to-heading t)
11627 (nth 4 (org-heading-components))))
11629 (org-refile-get-location
11630 (cond ((and goto (listp goto)) "Goto")
11631 (regionp (concat actionmsg " region to"))
11632 (t (concat actionmsg " subtree \""
11633 heading-text "\" to")))
11635 (and (not (equal '(4) goto))
11636 org-refile-allow-creating-parent-nodes)
11638 (setq file (nth 1 it)
11641 (if (and (not goto)
11643 (equal (buffer-file-name) file)
11645 (and (>= pos region-start)
11646 (<= pos region-end))
11647 (and (>= pos (point))
11648 (< pos (save-excursion
11649 (org-end-of-subtree t t))))))
11650 (error "Cannot refile to position inside the tree or region"))
11652 (setq nbuf (or (find-buffer-visiting file)
11653 (find-file-noselect file)))
11654 (if (and goto (not (equal goto 3)))
11656 (org-pop-to-buffer-same-window nbuf)
11658 (org-show-context 'org-goto))
11661 (org-kill-new (buffer-substring region-start region-end))
11662 (org-save-markers-in-region region-start region-end))
11663 (org-copy-subtree 1 nil t))
11664 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
11665 (find-file-noselect file)))
11666 (setq reversed (org-notes-order-reversed-p))
11673 (looking-at org-outline-regexp)
11674 (setq level (org-get-valid-level (funcall outline-level) 1))
11677 (or (outline-next-heading) (point-max))
11678 (or (save-excursion (org-get-next-sibling))
11679 (org-end-of-subtree t t)
11683 (goto-char (point-max))
11684 (goto-char (point-min))
11685 (or (outline-next-heading) (goto-char (point-max)))))
11686 (if (not (bolp)) (newline))
11687 (org-paste-subtree level)
11688 (when org-log-refile
11689 (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
11690 (unless (eq org-log-refile 'note)
11691 (save-excursion (org-add-log-note))))
11692 (and org-auto-align-tags
11693 (let ((org-loop-over-headlines-in-active-region nil))
11694 (org-set-tags nil t)))
11695 (let ((bookmark-name (plist-get org-bookmark-names-plist
11697 (when bookmark-name
11698 (with-demoted-errors
11699 (bookmark-set bookmark-name))))
11700 ;; If we are refiling for capture, make sure that the
11701 ;; last-capture pointers point here
11702 (when (org-bound-and-true-p org-refile-for-capture)
11703 (let ((bookmark-name (plist-get org-bookmark-names-plist
11704 :last-capture-marker)))
11705 (when bookmark-name
11706 (with-demoted-errors
11707 (bookmark-set bookmark-name))))
11708 (move-marker org-capture-last-stored-marker (point)))
11709 (if (fboundp 'deactivate-mark) (deactivate-mark))
11710 (run-hooks 'org-after-refile-insert-hook))))
11711 (unless org-refile-keep
11713 (delete-region (point) (+ (point) (- region-end region-start)))
11715 (and (org-back-to-heading t) (point))
11716 (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
11717 (when (featurep 'org-inlinetask)
11718 (org-inlinetask-remove-END-maybe))
11719 (setq org-markers-to-move nil)
11720 (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
11722 (defun org-refile-goto-last-stored ()
11723 "Go to the location where the last refile was stored."
11725 (bookmark-jump "org-refile-last-stored")
11726 (message "This is the location of the last refile"))
11728 (defun org-refile--get-location (refloc tbl)
11729 "When user refile to REFLOC, find the associated target in TBL.
11730 Also check `org-refile-target-table'."
11734 (lambda (r) (or (assoc r tbl)
11735 (assoc r org-refile-target-table)))
11736 (list (replace-regexp-in-string "/$" "" refloc)
11737 (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
11739 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
11741 "Prompt the user for a refile location, using PROMPT.
11742 PROMPT should not be suffixed with a colon and a space, because
11743 this function appends the default value from
11744 `org-refile-history' automatically, if that is not empty.
11745 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
11746 this is used for the GOTO interface."
11747 (let ((org-refile-targets org-refile-targets)
11748 (org-refile-use-outline-path org-refile-use-outline-path)
11750 (when (and (derived-mode-p 'org-mode)
11751 (not org-refile-use-cache)
11755 (setq excluded-entries
11756 (append excluded-entries (list (org-get-heading t t)))))))
11757 (setq org-refile-target-table
11758 (org-refile-get-targets default-buffer excluded-entries)))
11759 (unless org-refile-target-table
11760 (user-error "No refile targets"))
11761 (let* ((cbuf (current-buffer))
11762 (partial-completion-mode nil)
11763 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
11764 (cfunc (if (and org-refile-use-outline-path
11765 org-outline-path-complete-in-steps)
11766 'org-olpath-completing-read
11767 'org-icompleting-read))
11768 (extra (if org-refile-use-outline-path "/" ""))
11769 (cbnex (concat (buffer-name) extra))
11770 (filename (and cfn (expand-file-name cfn)))
11773 (if (and (not (member org-refile-use-outline-path
11774 '(file full-file-path)))
11775 (not (equal filename (nth 1 x))))
11776 (cons (concat (car x) extra " ("
11777 (file-name-nondirectory (nth 1 x)) ")")
11779 (cons (concat (car x) extra) (cdr x))))
11780 org-refile-target-table))
11781 (completion-ignore-case t)
11783 (prompt (concat prompt
11784 (or (and (car org-refile-history)
11785 (concat " (default " (car org-refile-history) ")"))
11786 (and (assoc cbnex tbl) (setq cdef cbnex)
11787 (concat " (default " cbnex ")"))) ": "))
11788 pa answ parent-target child parent old-hist)
11789 (setq old-hist org-refile-history)
11790 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
11791 nil 'org-refile-history (or cdef (car org-refile-history))))
11792 (if (setq pa (org-refile--get-location answ tbl))
11794 (org-refile-check-position pa)
11795 (when (or (not org-refile-history)
11796 (not (eq old-hist org-refile-history))
11797 (not (equal (car pa) (car org-refile-history))))
11798 (setq org-refile-history
11799 (cons (car pa) (if (assoc (car org-refile-history) tbl)
11801 (cdr org-refile-history))))
11802 (if (equal (car org-refile-history) (nth 1 org-refile-history))
11803 (pop org-refile-history)))
11805 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
11807 (setq parent (match-string 1 answ)
11808 child (match-string 2 answ))
11809 (setq parent-target (org-refile--get-location parent tbl))
11810 (when (and parent-target
11811 (or (eq new-nodes t)
11812 (and (eq new-nodes 'confirm)
11813 (y-or-n-p (format "Create new node \"%s\"? "
11815 (org-refile-new-child parent-target child)))
11816 (user-error "Invalid target location")))))
11818 (declare-function org-string-nw-p "org-macs" (s))
11819 (defun org-refile-check-position (refile-pointer)
11820 "Check if the refile pointer matches the headline to which it points."
11821 (let* ((file (nth 1 refile-pointer))
11822 (re (nth 2 refile-pointer))
11823 (pos (nth 3 refile-pointer))
11825 (if (and (not (markerp pos)) (not file))
11826 (user-error "Please save the buffer to a file before refiling")
11827 (when (org-string-nw-p re)
11828 (setq buffer (if (markerp pos)
11829 (marker-buffer pos)
11830 (or (find-buffer-visiting file)
11831 (find-file-noselect file))))
11832 (with-current-buffer buffer
11837 (beginning-of-line 1)
11838 (unless (org-looking-at-p re)
11839 (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
11841 (defun org-refile-new-child (parent-target child)
11842 "Use refile target PARENT-TARGET to add new CHILD below it."
11843 (unless parent-target
11844 (error "Cannot find parent for new node"))
11845 (let ((file (nth 1 parent-target))
11846 (pos (nth 3 parent-target))
11848 (with-current-buffer (or (find-buffer-visiting file)
11849 (find-file-noselect file))
11855 (goto-char (point-max))
11856 (if (not (bolp)) (newline)))
11857 (when (looking-at org-outline-regexp)
11858 (setq level (funcall outline-level))
11859 (org-end-of-subtree t t))
11860 (org-back-over-empty-lines)
11861 (insert "\n" (make-string
11862 (if pos (org-get-valid-level level 1) 1) ?*)
11864 (beginning-of-line 0)
11865 (list (concat (car parent-target) "/" child) file "" (point)))))))
11867 (defun org-olpath-completing-read (prompt collection &rest args)
11868 "Read an outline path like a file name."
11869 (let ((thetable collection)
11870 (org-completion-use-ido nil) ; does not work with ido.
11871 (org-completion-use-iswitchb nil)) ; or iswitchb
11873 'org-icompleting-read prompt
11874 (lambda (string predicate &optional flag)
11875 (let (rtn r f (l (length string)))
11879 (try-completion string thetable))
11882 (setq rtn (all-completions string thetable predicate))
11885 (setq r (substring x l))
11886 (if (string-match " ([^)]*)$" x)
11887 (setq f (match-string 0 x))
11889 (if (string-match "/" r)
11890 (concat string (substring r 0 (match-end 0)) f)
11895 (assoc string thetable)))))
11898 ;;;; Dynamic blocks
11900 (defun org-find-dblock (name)
11901 "Find the first dynamic block with name NAME in the buffer.
11902 If not found, stay at current position and return nil."
11903 (let ((case-fold-search t) pos)
11905 (goto-char (point-min))
11906 (setq pos (and (re-search-forward
11907 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
11908 (match-beginning 0))))
11909 (if pos (goto-char pos))
11912 (defconst org-dblock-start-re
11913 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
11914 "Matches the start line of a dynamic block, with parameters.")
11916 (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
11917 "Matches the end of a dynamic block.")
11919 (defun org-create-dblock (plist)
11920 "Create a dynamic block section, with parameters taken from PLIST.
11921 PLIST must contain a :name entry which is used as name of the block."
11922 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11925 (let ((col (current-column))
11926 (name (plist-get plist :name)))
11927 (insert "#+BEGIN: " name)
11929 (if (eq (car plist) :name)
11930 (setq plist (cddr plist))
11931 (insert " " (prin1-to-string (pop plist)))))
11932 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
11933 (beginning-of-line -2)))
11935 (defun org-prepare-dblock ()
11936 "Prepare dynamic block for refresh.
11937 This empties the block, puts the cursor at the insert position and returns
11938 the property list including an extra property :name with the block name."
11939 (unless (looking-at org-dblock-start-re)
11940 (user-error "Not at a dynamic block"))
11941 (let* ((begdel (1+ (match-end 0)))
11942 (name (org-no-properties (match-string 1)))
11943 (params (append (list :name name)
11944 (read (concat "(" (match-string 3) ")")))))
11946 (beginning-of-line 1)
11947 (skip-chars-forward " \t")
11948 (setq params (plist-put params :indentation-column (current-column))))
11949 (unless (re-search-forward org-dblock-end-re nil t)
11950 (error "Dynamic block not terminated"))
11953 (list :content (buffer-substring
11954 begdel (match-beginning 0)))))
11955 (delete-region begdel (match-beginning 0))
11960 (defun org-map-dblocks (&optional command)
11961 "Apply COMMAND to all dynamic blocks in the current buffer.
11962 If COMMAND is not given, use `org-update-dblock'."
11963 (let ((cmd (or command 'org-update-dblock)))
11965 (goto-char (point-min))
11966 (while (re-search-forward org-dblock-start-re nil t)
11967 (goto-char (match-beginning 0))
11969 (condition-case nil
11971 (error (message "Error during update of dynamic block"))))
11972 (unless (re-search-forward org-dblock-end-re nil t)
11973 (error "Dynamic block not terminated"))))))
11975 (defun org-dblock-update (&optional arg)
11976 "User command for updating dynamic blocks.
11977 Update the dynamic block at point. With prefix ARG, update all dynamic
11978 blocks in the buffer."
11981 (org-update-all-dblocks)
11982 (or (looking-at org-dblock-start-re)
11983 (org-beginning-of-dblock))
11984 (org-update-dblock)))
11986 (defun org-update-dblock ()
11987 "Update the dynamic block at point.
11988 This means to empty the block, parse for parameters and then call
11989 the correct writing function."
11992 (let* ((win (selected-window))
11994 (line (org-current-line))
11995 (params (org-prepare-dblock))
11996 (name (plist-get params :name))
11997 (indent (plist-get params :indentation-column))
11998 (cmd (intern (concat "org-dblock-write:" name))))
11999 (message "Updating dynamic block `%s' at line %d..." name line)
12000 (funcall cmd params)
12001 (message "Updating dynamic block `%s' at line %d...done" name line)
12003 (when (and indent (> indent 0))
12004 (setq indent (make-string indent ?\ ))
12006 (select-window win)
12007 (org-beginning-of-dblock)
12009 (while (not (looking-at org-dblock-end-re))
12011 (beginning-of-line 2))
12012 (when (looking-at org-dblock-end-re)
12013 (and (looking-at "[ \t]+")
12014 (replace-match ""))
12015 (insert indent)))))))
12017 (defun org-beginning-of-dblock ()
12018 "Find the beginning of the dynamic block at point.
12019 Error if there is no such block at point."
12020 (let ((pos (point))
12023 (if (and (re-search-backward org-dblock-start-re nil t)
12024 (setq beg (match-beginning 0))
12025 (re-search-forward org-dblock-end-re nil t)
12026 (> (match-end 0) pos))
12029 (error "Not in a dynamic block"))))
12031 (defun org-update-all-dblocks ()
12032 "Update all dynamic blocks in the buffer.
12033 This function can be used in a hook."
12035 (when (derived-mode-p 'org-mode)
12036 (org-map-dblocks 'org-update-dblock)))
12041 (declare-function org-export-backend-name "org-export" (cl-x))
12042 (declare-function org-export-backend-options "org-export" (cl-x))
12043 (defun org-get-export-keywords ()
12044 "Return a list of all currently understood export keywords.
12045 Export keywords include options, block names, attributes and
12046 keywords relative to each registered export back-end."
12049 (org-bound-and-true-p org-export--registered-backends)
12050 (delq nil keywords))
12051 ;; Back-end name (for keywords, like #+LATEX:)
12052 (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
12053 (dolist (option-entry (org-export-backend-options backend))
12054 ;; Back-end options.
12055 (push (nth 1 option-entry) keywords)))))
12057 (defconst org-options-keywords
12058 '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
12059 "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
12060 "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
12061 "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
12062 "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
12064 (defcustom org-structure-template-alist
12065 '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
12066 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
12067 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
12068 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
12069 ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
12070 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
12071 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
12072 "<literal style=\"latex\">\n?\n</literal>")
12073 ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
12074 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
12075 "<literal style=\"html\">\n?\n</literal>")
12076 ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
12077 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
12078 ("A" "#+ASCII: " "")
12079 ("i" "#+INDEX: ?" "#+INDEX: ?")
12080 ("I" "#+INCLUDE: %file ?"
12081 "<include file=%file markup=\"?\">"))
12082 "Structure completion elements.
12083 This is a list of abbreviation keys and values. The value gets inserted
12084 if you type `<' followed by the key and then press the completion key,
12085 usually `M-TAB'. %file will be replaced by a file name after prompting
12086 for the file using completion. The cursor will be placed at the position
12087 of the `?' in the template.
12088 There are two templates for each key, the first uses the original Org syntax,
12089 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
12090 the default when the /org-mtags.el/ module has been loaded. See also the
12091 variable `org-mtags-prefer-muse-templates'."
12092 :group 'org-completion
12095 (string :tag "Key")
12096 (string :tag "Template")
12097 (string :tag "Muse Template"))))
12099 (defun org-try-structure-completion ()
12100 "Try to complete a structure template before point.
12101 This looks for strings like \"<e\" on an otherwise empty line and
12103 (let ((l (buffer-substring (point-at-bol) (point)))
12105 (when (and (looking-at "[ \t]*$")
12106 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
12107 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
12108 (org-complete-expand-structure-template (+ -1 (point-at-bol)
12109 (match-beginning 1)) a)
12112 (defun org-complete-expand-structure-template (start cell)
12113 "Expand a structure template."
12114 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
12115 (rpl (nth (if musep 2 1) cell))
12117 (delete-region start (point))
12118 (when (string-match "\\`#\\+" rpl)
12121 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
12122 (setq ind (buffer-substring (point-at-bol) (point))))
12124 (setq start (point))
12125 (if (string-match "%file" rpl)
12126 (setq rpl (replace-match
12130 (abbreviate-file-name (read-file-name "Include file: ")))
12133 (setq rpl (mapconcat 'identity (split-string rpl "\n")
12134 (concat "\n" ind)))
12136 (if (re-search-backward "\\?" start t) (delete-char 1))))
12138 ;;;; TODO, DEADLINE, Comments
12140 (defun org-toggle-comment ()
12141 "Change the COMMENT state of an entry."
12144 (org-back-to-heading)
12145 (let (case-fold-search)
12147 ((looking-at (format org-heading-keyword-regexp-format
12148 org-comment-string))
12149 (goto-char (match-end 1))
12150 (looking-at (concat " +" org-comment-string))
12151 (replace-match "" t t)
12152 (when (eolp) (insert " ")))
12153 ((looking-at org-outline-regexp)
12154 (goto-char (match-end 0))
12155 (insert org-comment-string " "))))))
12157 (defvar org-last-todo-state-is-todo nil
12158 "This is non-nil when the last TODO state change led to a TODO state.
12159 If the last change removed the TODO tag or switched to DONE, then
12162 (defvar org-setting-tags nil) ; dynamically skipped
12164 (defvar org-todo-setup-filter-hook nil
12165 "Hook for functions that pre-filter todo specs.
12166 Each function takes a todo spec and returns either nil or the spec
12167 transformed into canonical form." )
12169 (defvar org-todo-get-default-hook nil
12170 "Hook for functions that get a default item for todo.
12171 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
12172 nil or a string to be used for the todo mark." )
12174 (defvar org-agenda-headline-snapshot-before-repeat)
12176 (defun org-current-effective-time ()
12177 "Return current time adjusted for `org-extend-today-until' variable."
12178 (let* ((ct (org-current-time))
12179 (dct (decode-time ct))
12182 (org-use-last-clock-out-time-as-effective-time
12183 (or (org-clock-get-last-clock-out-time) ct))
12184 ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
12185 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
12189 (defun org-todo-yesterday (&optional arg)
12190 "Like `org-todo' but the time of change will be 23:59 of yesterday."
12192 (if (eq major-mode 'org-agenda-mode)
12193 (apply 'org-agenda-todo-yesterday arg)
12194 (let* ((hour (third (decode-time
12195 (org-current-time))))
12196 (org-extend-today-until (1+ hour)))
12199 (defvar org-block-entry-blocking ""
12200 "First entry preventing the TODO state change.")
12202 (defun org-todo (&optional arg)
12203 "Change the TODO state of an item.
12204 The state of an item is given by a keyword at the start of the heading,
12206 *** TODO Write paper
12209 The different keywords are specified in the variable `org-todo-keywords'.
12210 By default the available states are \"TODO\" and \"DONE\".
12211 So for this example: when the item starts with TODO, it is changed to DONE.
12212 When it starts with DONE, the DONE is removed. And when neither TODO nor
12213 DONE are present, add TODO at the beginning of the heading.
12215 With \\[universal-argument] prefix arg, use completion to determine the new \
12217 With numeric prefix arg, switch to that state.
12218 With a double \\[universal-argument] prefix, switch to the next set of TODO \
12219 keywords (nextset).
12220 With a triple \\[universal-argument] prefix, circumvent any state blocking.
12221 With a numeric prefix arg of 0, inhibit note taking for the change.
12223 For calling through lisp, arg is also interpreted in the following way:
12224 `none' -> empty state
12225 \"\" (empty string) -> switch to empty state
12226 `done' -> switch to DONE
12227 `nextset' -> switch to the next set of keywords
12228 `previousset' -> switch to the previous set of keywords
12229 \"WAITING\" -> switch to the specified keyword, but only if it
12230 really is a member of `org-todo-keywords'."
12232 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12233 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12234 'region-start-level 'region))
12235 org-loop-over-headlines-in-active-region)
12238 org-loop-over-headlines-in-active-region
12239 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12240 (if (equal arg '(16)) (setq arg 'nextset))
12241 (let ((org-blocker-hook org-blocker-hook)
12244 (when (equal arg '(64))
12245 (setq arg nil org-blocker-hook nil))
12246 (when (and org-blocker-hook
12247 (or org-inhibit-blocking
12248 (org-entry-get nil "NOBLOCKING")))
12249 (setq org-blocker-hook nil))
12252 (org-back-to-heading t)
12253 (when (looking-at (concat "^\\*+ " org-comment-string))
12254 (org-toggle-comment)
12256 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
12257 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
12258 (looking-at "\\(?: *\\|[ \t]*$\\)"))
12259 (let* ((match-data (match-data))
12260 (startpos (point-at-bol))
12261 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
12262 (org-log-done org-log-done)
12263 (org-log-repeat org-log-repeat)
12264 (org-todo-log-states org-todo-log-states)
12265 (org-inhibit-logging
12267 (progn (setq arg nil) 'note) org-inhibit-logging))
12268 (this (match-string 1))
12269 (hl-pos (match-beginning 0))
12270 (head (org-get-todo-sequence-head this))
12271 (ass (assoc head org-todo-kwd-alist))
12272 (interpret (nth 1 ass))
12273 (done-word (nth 3 ass))
12274 (final-done-word (nth 4 ass))
12275 (org-last-state (or this ""))
12276 (completion-ignore-case t)
12277 (member (member this org-todo-keywords-1))
12278 (tail (cdr member))
12280 ((and org-todo-key-trigger
12281 (or (and (equal arg '(4))
12282 (eq org-use-fast-todo-selection 'prefix))
12283 (and (not arg) org-use-fast-todo-selection
12284 (not (eq org-use-fast-todo-selection
12286 ;; Use fast selection
12287 (org-fast-todo-selection))
12288 ((and (equal arg '(4))
12289 (or (not org-use-fast-todo-selection)
12290 (not org-todo-key-trigger)))
12291 ;; Read a state with completion
12292 (org-icompleting-read
12293 "State: " (mapcar 'list org-todo-keywords-1)
12297 (if tail (car tail) nil)
12298 (car org-todo-keywords-1)))
12300 (if (equal member org-todo-keywords-1)
12303 (nth (- (length org-todo-keywords-1)
12305 org-todo-keywords-1)
12306 (org-last org-todo-keywords-1))))
12307 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
12308 (setq arg nil))) ; hack to fall back to cycling
12310 ;; user or caller requests a specific state
12312 ((equal arg "") nil)
12313 ((eq arg 'none) nil)
12314 ((eq arg 'done) (or done-word (car org-done-keywords)))
12316 (or (car (cdr (member head org-todo-heads)))
12317 (car org-todo-heads)))
12318 ((eq arg 'previousset)
12319 (let ((org-todo-heads (reverse org-todo-heads)))
12320 (or (car (cdr (member head org-todo-heads)))
12321 (car org-todo-heads))))
12322 ((car (member arg org-todo-keywords-1)))
12324 (user-error "State `%s' not valid in this file" arg))
12325 ((nth (1- (prefix-numeric-value arg))
12326 org-todo-keywords-1))))
12327 ((null member) (or head (car org-todo-keywords-1)))
12328 ((equal this final-done-word) nil) ;; -> make empty
12329 ((null tail) nil) ;; -> first entry
12330 ((memq interpret '(type priority))
12331 (if (eq this-command last-command)
12333 (if (> (length tail) 0)
12334 (or done-word (car org-done-keywords))
12339 (run-hook-with-args-until-success
12340 'org-todo-get-default-hook org-state org-last-state)
12342 (next (if org-state (concat " " org-state " ") " "))
12343 (change-plist (list :type 'todo-state-change :from this :to org-state
12344 :position startpos))
12346 (when org-blocker-hook
12347 (setq org-last-todo-state-is-todo
12348 (not (member this org-done-keywords)))
12349 (unless (save-excursion
12351 (org-with-wide-buffer
12352 (run-hook-with-args-until-failure
12353 'org-blocker-hook change-plist))))
12354 (if (org-called-interactively-p 'interactive)
12355 (user-error "TODO state change from %s to %s blocked (by \"%s\")"
12356 this org-state org-block-entry-blocking)
12358 (message "TODO state change from %s to %s blocked (by \"%s\")"
12359 this org-state org-block-entry-blocking)
12360 (throw 'exit nil))))
12361 (store-match-data match-data)
12362 (replace-match next t t)
12363 (unless (pos-visible-in-window-p hl-pos)
12364 (message "TODO state changed to %s" (org-trim next)))
12366 (setq head (org-get-todo-sequence-head org-state)
12367 ass (assoc head org-todo-kwd-alist)
12368 interpret (nth 1 ass)
12369 done-word (nth 3 ass)
12370 final-done-word (nth 4 ass)))
12371 (when (memq arg '(nextset previousset))
12372 (message "Keyword-Set %d/%d: %s"
12373 (- (length org-todo-sets) -1
12374 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
12375 (length org-todo-sets)
12376 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
12377 (setq org-last-todo-state-is-todo
12378 (not (member org-state org-done-keywords)))
12379 (setq now-done-p (and (member org-state org-done-keywords)
12380 (not (member this org-done-keywords))))
12381 (and logging (org-local-logging logging))
12382 (when (and (or org-todo-log-states org-log-done)
12383 (not (eq org-inhibit-logging t))
12384 (not (memq arg '(nextset previousset))))
12385 ;; we need to look at recording a time and note
12386 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
12387 (nth 2 (assoc this org-todo-log-states))))
12388 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
12389 (setq dolog 'time))
12390 (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
12392 (member org-state org-not-done-keywords)
12393 (not (member this org-not-done-keywords))))
12394 ;; This is now a todo state and was not one before
12395 ;; If there was a CLOSED time stamp, get rid of it.
12396 (org-add-planning-info nil nil 'closed))
12397 (when (and now-done-p org-log-done)
12398 ;; It is now done, and it was not done before
12399 (org-add-planning-info 'closed (org-current-effective-time))
12400 (if (and (not dolog) (eq 'note org-log-done))
12401 (org-add-log-setup 'done org-state this 'findpos 'note)))
12402 (when (and org-state dolog)
12403 ;; This is a non-nil state, and we need to log it
12404 (org-add-log-setup 'state org-state this 'findpos dolog)))
12405 ;; Fixup tag positioning
12406 (org-todo-trigger-tag-changes org-state)
12407 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
12408 (when org-provide-todo-statistics
12409 (org-update-parent-todo-statistics))
12410 (run-hooks 'org-after-todo-state-change-hook)
12411 (if (and arg (not (member org-state org-done-keywords)))
12412 (setq head (org-get-todo-sequence-head org-state)))
12413 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
12414 ;; Do we need to trigger a repeat?
12416 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
12417 ;; This is for the agenda, take a snapshot of the headline.
12419 (setq org-agenda-headline-snapshot-before-repeat
12420 (org-get-heading))))
12421 (org-auto-repeat-maybe org-state))
12422 ;; Fixup cursor location if close to the keyword
12423 (if (and (outline-on-heading-p)
12425 (save-excursion (beginning-of-line 1)
12426 (looking-at org-todo-line-regexp))
12427 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
12429 (goto-char (or (match-end 2) (match-end 1)))
12430 (and (looking-at " ") (just-one-space))))
12431 (when org-trigger-hook
12433 (run-hook-with-args 'org-trigger-hook change-plist)))
12434 (when commentp (org-toggle-comment))))))))
12436 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
12437 "Block turning an entry into a TODO, using the hierarchy.
12438 This checks whether the current task should be blocked from state
12439 changes. Such blocking occurs when:
12441 1. The task has children which are not all in a completed state.
12443 2. A task has a parent with the property :ORDERED:, and there
12444 are siblings prior to the current task with incomplete
12447 3. The parent of the task is blocked because it has siblings that should
12448 be done first, or is child of a block grandparent TODO entry."
12450 (if (not org-enforce-todo-dependencies)
12451 t ; if locally turned off don't block
12453 ;; If this is not a todo state change, or if this entry is already DONE,
12455 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
12456 (member (plist-get change-plist :from)
12457 (cons 'done org-done-keywords))
12458 (member (plist-get change-plist :to)
12459 (cons 'todo org-not-done-keywords))
12460 (not (plist-get change-plist :to)))
12461 (throw 'dont-block t))
12462 ;; If this task has children, and any are undone, it's blocked
12464 (org-back-to-heading t)
12465 (let ((this-level (funcall outline-level)))
12466 (outline-next-heading)
12467 (let ((child-level (funcall outline-level)))
12468 (while (and (not (eobp))
12469 (> child-level this-level))
12470 ;; this todo has children, check whether they are all
12472 (if (and (not (org-entry-is-done-p))
12473 (org-entry-is-todo-p))
12474 (progn (setq org-block-entry-blocking (org-get-heading))
12475 (throw 'dont-block nil)))
12476 (outline-next-heading)
12477 (setq child-level (funcall outline-level))))))
12478 ;; Otherwise, if the task's parent has the :ORDERED: property, and
12479 ;; any previous siblings are undone, it's blocked
12481 (org-back-to-heading t)
12482 (let* ((pos (point))
12483 (parent-pos (and (org-up-heading-safe) (point))))
12484 (if (not parent-pos) (throw 'dont-block t)) ; no parent
12485 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12487 (re-search-forward org-not-done-heading-regexp pos t))
12488 (setq org-block-entry-blocking (match-string 0))
12489 (throw 'dont-block nil)) ; block, there is an older sibling not done.
12490 ;; Search further up the hierarchy, to see if an ancestor is blocked
12492 (goto-char parent-pos)
12493 (if (not (looking-at org-not-done-heading-regexp))
12494 (throw 'dont-block t)) ; do not block, parent is not a TODO
12496 (setq parent-pos (and (org-up-heading-safe) (point)))
12497 (if (not parent-pos) (throw 'dont-block t)) ; no parent
12498 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12500 (re-search-forward org-not-done-heading-regexp pos t)
12501 (setq org-block-entry-blocking (org-get-heading)))
12502 (throw 'dont-block nil)))))))) ; block, older sibling not done.
12504 (defcustom org-track-ordered-property-with-tag nil
12505 "Should the ORDERED property also be shown as a tag?
12506 The ORDERED property decides if an entry should require subtasks to be
12507 completed in sequence. Since a property is not very visible, setting
12508 this option means that toggling the ORDERED property with the command
12509 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
12510 not relevant for the behavior, but it makes things more visible.
12512 Note that toggling the tag with tags commands will not change the property
12513 and therefore not influence behavior!
12515 This can be t, meaning the tag ORDERED should be used, It can also be a
12516 string to select a different tag for this task."
12519 (const :tag "No tracking" nil)
12520 (const :tag "Track with ORDERED tag" t)
12521 (string :tag "Use other tag")))
12523 (defun org-toggle-ordered-property ()
12524 "Toggle the ORDERED property of the current entry.
12525 For better visibility, you can track the value of this property with a tag.
12526 See variable `org-track-ordered-property-with-tag'."
12528 (let* ((t1 org-track-ordered-property-with-tag)
12529 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
12531 (org-back-to-heading)
12532 (if (org-entry-get nil "ORDERED")
12534 (org-delete-property "ORDERED" "PROPERTIES")
12535 (and tag (org-toggle-tag tag 'off))
12536 (message "Subtasks can be completed in arbitrary order"))
12537 (org-entry-put nil "ORDERED" "t")
12538 (and tag (org-toggle-tag tag 'on))
12539 (message "Subtasks must be completed in sequence")))))
12541 (defvar org-blocked-by-checkboxes) ; dynamically scoped
12542 (defun org-block-todo-from-checkboxes (change-plist)
12543 "Block turning an entry into a TODO, using checkboxes.
12544 This checks whether the current task should be blocked from state
12545 changes because there are unchecked boxes in this entry."
12546 (if (not org-enforce-todo-checkbox-dependencies)
12547 t ; if locally turned off don't block
12549 ;; If this is not a todo state change, or if this entry is already DONE,
12551 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
12552 (member (plist-get change-plist :from)
12553 (cons 'done org-done-keywords))
12554 (member (plist-get change-plist :to)
12555 (cons 'todo org-not-done-keywords))
12556 (not (plist-get change-plist :to)))
12557 (throw 'dont-block t))
12558 ;; If this task has checkboxes that are not checked, it's blocked
12560 (org-back-to-heading t)
12561 (let ((beg (point)) end)
12562 (outline-next-heading)
12565 (if (org-list-search-forward
12566 (concat (org-item-beginning-re)
12567 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
12571 (if (boundp 'org-blocked-by-checkboxes)
12572 (setq org-blocked-by-checkboxes t))
12573 (throw 'dont-block nil)))))
12574 t))) ; do not block
12576 (defun org-entry-blocked-p ()
12577 "Is the current entry blocked?"
12578 (org-with-silent-modifications
12579 (if (org-entry-get nil "NOBLOCKING")
12580 nil ;; Never block this entry
12581 (not (run-hook-with-args-until-failure
12583 (list :type 'todo-state-change
12588 (defun org-update-statistics-cookies (all)
12589 "Update the statistics cookie, either from TODO or from checkboxes.
12590 This should be called with the cursor in a line with a statistics cookie."
12594 (org-update-checkbox-count 'all)
12595 (org-map-entries 'org-update-parent-todo-statistics))
12596 (if (not (org-at-heading-p))
12597 (org-update-checkbox-count)
12598 (let ((pos (point-marker))
12600 (ignore-errors (org-back-to-heading t))
12601 (if (not (org-at-heading-p))
12602 (org-update-checkbox-count)
12603 (setq l1 (org-outline-level))
12604 (setq end (save-excursion
12605 (outline-next-heading)
12606 (if (org-at-heading-p) (setq l2 (org-outline-level)))
12608 (if (and (save-excursion
12610 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
12611 (not (save-excursion (re-search-forward
12612 ":COOKIE_DATA:.*\\<todo\\>" end t))))
12613 (org-update-checkbox-count)
12614 (if (and l2 (> l2 l1))
12617 (org-update-parent-todo-statistics))
12619 (beginning-of-line 1)
12620 (while (re-search-forward
12621 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
12623 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
12625 (move-marker pos nil)))))
12627 (defvar org-entry-property-inherited-from) ;; defined below
12628 (defun org-update-parent-todo-statistics ()
12629 "Update any statistics cookie in the parent of the current headline.
12630 When `org-hierarchical-todo-statistics' is nil, statistics will cover
12631 the entire subtree and this will travel up the hierarchy and update
12632 statistics everywhere."
12633 (let* ((prop (save-excursion (org-up-heading-safe)
12634 (org-entry-get nil "COOKIE_DATA" 'inherit)))
12635 (recursive (or (not org-hierarchical-todo-statistics)
12636 (and prop (string-match "\\<recursive\\>" prop))))
12637 (lim (or (and prop (marker-position org-entry-property-inherited-from))
12640 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
12641 level ltoggle l1 new ndel
12642 (cnt-all 0) (cnt-done 0) is-percent kwd
12643 checkbox-beg ov ovs ove cookie-present)
12646 (beginning-of-line 1)
12647 (setq ltoggle (funcall outline-level))
12648 ;; Three situations are to consider:
12650 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
12651 ;; to the top-level ancestor on the headline;
12653 ;; 2. If parent has "recursive" property, repeat up to the
12654 ;; headline setting that property, taking inheritance into
12657 ;; 3. Else, move up to direct parent and proceed only once.
12658 (while (and (setq level (org-up-heading-safe))
12659 (or recursive first)
12661 (setq first nil cookie-present nil)
12665 (downcase (or (org-entry-get nil "COOKIE_DATA")
12668 (while (re-search-forward box-re (point-at-eol) t)
12669 (setq cnt-all 0 cnt-done 0 cookie-present t)
12670 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
12672 (unless (outline-next-heading) (throw 'exit nil))
12673 (while (and (looking-at org-complex-heading-regexp)
12674 (> (setq l1 (length (match-string 1))) level))
12675 (setq kwd (and (or recursive (= l1 ltoggle))
12677 (if (or (eq org-provide-todo-statistics 'all-headlines)
12678 (and (listp org-provide-todo-statistics)
12679 (or (member kwd org-provide-todo-statistics)
12680 (member kwd org-done-keywords))))
12681 (setq cnt-all (1+ cnt-all))
12682 (if (eq org-provide-todo-statistics t)
12683 (and kwd (setq cnt-all (1+ cnt-all)))))
12684 (and (member kwd org-done-keywords)
12685 (setq cnt-done (1+ cnt-done)))
12686 (outline-next-heading)))
12689 (format "[%d%%]" (floor (* 100.0 cnt-done)
12691 (format "[%d/%d]" cnt-done cnt-all))
12692 ndel (- (match-end 0) checkbox-beg))
12693 ;; handle overlays when updating cookie from column view
12694 (when (setq ov (car (overlays-at checkbox-beg)))
12695 (setq ovs (overlay-start ov) ove (overlay-end ov))
12696 (delete-overlay ov))
12697 (goto-char checkbox-beg)
12699 (delete-region (point) (+ (point) ndel))
12700 (when org-auto-align-tags (org-fix-tags-on-the-fly))
12701 (when ov (move-overlay ov ovs ove)))
12702 (when cookie-present
12703 (run-hook-with-args 'org-after-todo-statistics-hook
12704 cnt-done (- cnt-all cnt-done))))))
12705 (run-hooks 'org-todo-statistics-hook)))
12707 (defvar org-after-todo-statistics-hook nil
12708 "Hook that is called after a TODO statistics cookie has been updated.
12709 Each function is called with two arguments: the number of not-done entries
12710 and the number of done entries.
12712 For example, the following function, when added to this hook, will switch
12713 an entry to DONE when all children are done, and back to TODO when new
12714 entries are set to a TODO status. Note that this hook is only called
12715 when there is a statistics cookie in the headline!
12717 (defun org-summary-todo (n-done n-not-done)
12718 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
12719 (let (org-log-done org-log-states) ; turn off logging
12720 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
12723 (defvar org-todo-statistics-hook nil
12724 "Hook that is run whenever Org thinks TODO statistics should be updated.
12725 This hook runs even if there is no statistics cookie present, in which case
12726 `org-after-todo-statistics-hook' would not run.")
12728 (defun org-todo-trigger-tag-changes (state)
12729 "Apply the changes defined in `org-todo-state-tags-triggers'."
12730 (let ((l org-todo-state-tags-triggers)
12732 (when (or (not state) (equal state ""))
12733 (setq changes (append changes (cdr (assoc "" l)))))
12734 (when (and (stringp state) (> (length state) 0))
12735 (setq changes (append changes (cdr (assoc state l)))))
12736 (when (member state org-not-done-keywords)
12737 (setq changes (append changes (cdr (assoc 'todo l)))))
12738 (when (member state org-done-keywords)
12739 (setq changes (append changes (cdr (assoc 'done l)))))
12740 (dolist (c changes)
12741 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
12743 (defun org-local-logging (value)
12744 "Get logging settings from a property VALUE."
12745 ;; Directly set the variables, they are already local.
12746 (setq org-log-done nil
12748 org-todo-log-states nil)
12749 (dolist (w (org-split-string value))
12752 ((setq a (assoc w org-startup-options))
12753 (and (member (nth 1 a) '(org-log-done org-log-repeat))
12754 (set (nth 1 a) (nth 2 a))))
12755 ((setq a (org-extract-log-state-settings w))
12756 (and (member (car a) org-todo-keywords-1)
12757 (push a org-todo-log-states)))))))
12759 (defun org-get-todo-sequence-head (kwd)
12760 "Return the head of the TODO sequence to which KWD belongs.
12761 If KWD is not set, check if there is a text property remembering the
12766 (or (get-text-property (point-at-bol) 'org-todo-head)
12768 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
12769 nil (point-at-eol)))
12770 (get-text-property p 'org-todo-head))))
12771 ((not (member kwd org-todo-keywords-1))
12772 (car org-todo-keywords-1))
12773 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
12775 (defun org-fast-todo-selection ()
12776 "Fast TODO keyword selection with single keys.
12777 Returns the new TODO keyword, or nil if no state change should occur."
12778 (let* ((fulltable org-todo-key-alist)
12779 (done-keywords org-done-keywords) ;; needed for the faces.
12780 (maxlen (apply 'max (mapcar
12782 (if (stringp (car x)) (string-width (car x)) 0))
12785 (fwidth (+ maxlen 3 1 3))
12786 (ncol (/ (- (window-width) 4) fwidth))
12790 (save-window-excursion
12792 (set-buffer (get-buffer-create " *Org todo*"))
12793 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
12795 (org-set-local 'org-done-keywords done-keywords)
12796 (setq tbl fulltable cnt 0)
12799 ((equal e '(:startgroup))
12800 (push '() groups) (setq ingroup t)
12801 (when (not (= cnt 0))
12805 ((equal e '(:endgroup))
12806 (setq ingroup nil cnt 0)
12808 ((equal e '(:newline))
12809 (when (not (= cnt 0))
12813 (while (equal (car tbl) '(:newline))
12815 (setq tbl (cdr tbl)))))
12817 (setq tg (car e) c (cdr e))
12818 (if ingroup (push tg (car groups)))
12819 (setq tg (org-add-props tg nil 'face
12820 (org-get-todo-face tg)))
12821 (if (and (= cnt 0) (not ingroup)) (insert " "))
12822 (insert "[" c "] " tg (make-string
12823 (- fwidth 4 (length tg)) ?\ ))
12824 (when (= (setq cnt (1+ cnt)) ncol)
12826 (if ingroup (insert " "))
12829 (goto-char (point-min))
12830 (if (not expert) (org-fit-window-to-buffer))
12831 (message "[a-z..]:Set [SPC]:clear")
12832 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
12835 (and (= c ?q) (not (rassoc c fulltable))))
12836 (setq quit-flag t))
12838 ((setq e (rassoc c fulltable) tg (car e))
12840 (t (setq quit-flag t)))))))
12842 (defun org-entry-is-todo-p ()
12843 (member (org-get-todo-state) org-not-done-keywords))
12845 (defun org-entry-is-done-p ()
12846 (member (org-get-todo-state) org-done-keywords))
12848 (defun org-get-todo-state ()
12849 "Return the TODO keyword of the current subtree."
12851 (org-back-to-heading t)
12852 (and (looking-at org-todo-line-regexp)
12854 (match-string 2))))
12856 (defun org-at-date-range-p (&optional inactive-ok)
12857 "Is the cursor inside a date range?"
12861 (let ((pos (point)))
12862 (skip-chars-backward "^[<\r\n")
12863 (skip-chars-backward "<[")
12864 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12865 (>= (match-end 0) pos)
12867 (skip-chars-backward "^<[\r\n")
12868 (skip-chars-backward "<[")
12869 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12870 (>= (match-end 0) pos)
12874 (defun org-get-repeat (&optional tagline)
12875 "Check if there is a deadline/schedule with repeater in this entry."
12878 (org-back-to-heading t)
12879 (and (re-search-forward (if tagline
12880 (concat tagline "\\s-*" org-repeat-re)
12882 (org-entry-end-position) t)
12883 (match-string-no-properties 1)))))
12885 (defvar org-last-changed-timestamp)
12886 (defvar org-last-inserted-timestamp)
12887 (defvar org-log-post-message)
12888 (defvar org-log-note-purpose)
12889 (defvar org-log-note-how)
12890 (defvar org-log-note-extra)
12891 (defun org-auto-repeat-maybe (done-word)
12892 "Check if the current headline contains a repeated deadline/schedule.
12893 If yes, set TODO state back to what it was and change the base date
12894 of repeating deadline/scheduled time stamps to new date.
12895 This function is run automatically after each state change to a DONE state."
12896 ;; last-state is dynamically scoped into this function
12897 (let* ((repeat (org-get-repeat))
12898 (aa (assoc org-last-state org-todo-kwd-alist))
12899 (interpret (nth 1 aa))
12901 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
12902 (msg "Entry repeats: ")
12904 (org-todo-log-states nil)
12905 re type n what ts time to-state)
12907 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
12908 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
12909 org-todo-repeat-to-state))
12910 (unless (and to-state (member to-state org-todo-keywords-1))
12911 (setq to-state (if (eq interpret 'type) org-last-state head)))
12912 (org-todo to-state)
12913 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
12914 (org-entry-put nil "LAST_REPEAT" (format-time-string
12915 (org-time-stamp-format t t))))
12916 (when org-log-repeat
12917 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
12918 (memq 'org-add-log-note post-command-hook))
12919 ;; OK, we are already setup for some record
12920 (if (eq org-log-repeat 'note)
12921 ;; make sure we take a note, not only a time stamp
12922 (setq org-log-note-how 'note))
12923 ;; Set up for taking a record
12924 (org-add-log-setup 'state (or done-word (car org-done-keywords))
12926 'findpos org-log-repeat)))
12927 (org-back-to-heading t)
12928 (org-add-planning-info nil nil 'closed)
12929 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12930 org-deadline-time-regexp "\\)\\|\\("
12931 org-ts-regexp "\\)"))
12932 (while (re-search-forward
12933 re (save-excursion (outline-next-heading) (point)) t)
12934 (setq type (if (match-end 1) org-scheduled-string
12935 (if (match-end 3) org-deadline-string "Plain:"))
12936 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
12937 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
12938 (setq n (string-to-number (match-string 2 ts))
12939 what (match-string 3 ts))
12940 (if (equal what "w") (setq n (* n 7) what "d"))
12941 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
12942 (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
12943 ;; Preparation, see if we need to modify the start date for the change
12944 (when (match-end 1)
12945 (setq time (save-match-data (org-time-string-to-time ts)))
12947 ((equal (match-string 1 ts) ".")
12948 ;; Shift starting date to today
12949 (org-timestamp-change
12950 (- (org-today) (time-to-days time))
12952 ((equal (match-string 1 ts) "+")
12953 (let ((nshiftmax 10) (nshift 0))
12954 (while (or (= nshift 0)
12955 (<= (time-to-days time)
12956 (time-to-days (current-time))))
12957 (when (= (incf nshift) nshiftmax)
12958 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
12960 (org-timestamp-change n (cdr (assoc what whata)))
12961 (org-at-timestamp-p t)
12962 (setq ts (match-string 1))
12963 (setq time (save-match-data (org-time-string-to-time ts)))))
12964 (org-timestamp-change (- n) (cdr (assoc what whata)))
12965 ;; rematch, so that we have everything in place for the real shift
12966 (org-at-timestamp-p t)
12967 (setq ts (match-string 1))
12968 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
12969 (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
12970 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12971 (setq org-log-post-message msg)
12972 (message "%s" msg))))
12974 (defun org-show-todo-tree (arg)
12975 "Make a compact tree which shows all headlines marked with TODO.
12976 The tree will show the lines where the regexp matches, and all higher
12977 headlines above the match.
12978 With a \\[universal-argument] prefix, prompt for a regexp to match.
12979 With a numeric prefix N, construct a sparse tree for the Nth element
12980 of `org-todo-keywords-1'."
12982 (let ((case-fold-search nil)
12984 (cond ((null arg) org-not-done-regexp)
12986 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12987 (mapcar 'list org-todo-keywords-1))))
12989 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12991 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12992 (regexp-quote (nth (1- (prefix-numeric-value arg))
12993 org-todo-keywords-1)))
12994 (t (user-error "Invalid prefix argument: %s" arg)))))
12995 (message "%d TODO entries found"
12996 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12998 (defun org-deadline (arg &optional time)
12999 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
13000 With one universal prefix argument, remove any deadline from the item.
13001 With two universal prefix arguments, prompt for a warning delay.
13002 With argument TIME, set the deadline at the corresponding date. TIME
13003 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
13005 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13006 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13007 'region-start-level 'region))
13008 org-loop-over-headlines-in-active-region)
13010 `(org-deadline ',arg ,time)
13011 org-loop-over-headlines-in-active-region
13012 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13013 (let* ((old-date (org-entry-get nil "DEADLINE"))
13014 (old-date-time (if old-date (org-time-string-to-time old-date)))
13015 (repeater (and old-date
13017 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
13019 (match-string 1 old-date))))
13022 (when (and old-date org-log-redeadline)
13023 (org-add-log-setup 'deldeadline nil old-date 'findpos
13024 org-log-redeadline))
13025 (org-remove-timestamp-with-keyword org-deadline-string)
13026 (message "Item no longer has a deadline."))
13029 (org-back-to-heading t)
13030 (if (re-search-forward
13031 org-deadline-time-regexp
13032 (save-excursion (outline-next-heading) (point)) t)
13033 (let* ((rpl0 (match-string 1))
13034 (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
13036 (concat org-deadline-string
13042 (org-read-date nil t nil "Warn starting from" old-date-time)))
13043 (time-to-days old-date-time))))
13045 (user-error "No deadline information to update"))))
13047 (org-add-planning-info 'deadline time 'closed)
13048 (when (and old-date org-log-redeadline
13049 (not (equal old-date
13050 (substring org-last-inserted-timestamp 1 -1))))
13051 (org-add-log-setup 'redeadline nil old-date 'findpos
13052 org-log-redeadline))
13055 (org-back-to-heading t)
13056 (when (re-search-forward (concat org-deadline-string " "
13057 org-last-inserted-timestamp)
13059 (outline-next-heading) (point)) t)
13060 (goto-char (1- (match-end 0)))
13061 (insert " " repeater)
13062 (setq org-last-inserted-timestamp
13063 (concat (substring org-last-inserted-timestamp 0 -1)
13065 (substring org-last-inserted-timestamp -1))))))
13066 (message "Deadline on %s" org-last-inserted-timestamp))))))
13068 (defun org-schedule (arg &optional time)
13069 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
13070 With one universal prefix argument, remove any scheduling date from the item.
13071 With two universal prefix arguments, prompt for a delay cookie.
13072 With argument TIME, scheduled at the corresponding date. TIME can
13073 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
13075 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13076 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13077 'region-start-level 'region))
13078 org-loop-over-headlines-in-active-region)
13080 `(org-schedule ',arg ,time)
13081 org-loop-over-headlines-in-active-region
13082 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13083 (let* ((old-date (org-entry-get nil "SCHEDULED"))
13084 (old-date-time (if old-date (org-time-string-to-time old-date)))
13085 (repeater (and old-date
13087 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
13089 (match-string 1 old-date))))
13093 (when (and old-date org-log-reschedule)
13094 (org-add-log-setup 'delschedule nil old-date 'findpos
13095 org-log-reschedule))
13096 (org-remove-timestamp-with-keyword org-scheduled-string)
13097 (message "Item is no longer scheduled.")))
13100 (org-back-to-heading t)
13101 (if (re-search-forward
13102 org-scheduled-time-regexp
13103 (save-excursion (outline-next-heading) (point)) t)
13104 (let* ((rpl0 (match-string 1))
13105 (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
13107 (concat org-scheduled-string
13113 (org-read-date nil t nil "Delay until" old-date-time)))
13114 (time-to-days old-date-time))))
13116 (user-error "No scheduled information to update"))))
13118 (org-add-planning-info 'scheduled time 'closed)
13119 (when (and old-date org-log-reschedule
13120 (not (equal old-date
13121 (substring org-last-inserted-timestamp 1 -1))))
13122 (org-add-log-setup 'reschedule nil old-date 'findpos
13123 org-log-reschedule))
13126 (org-back-to-heading t)
13127 (when (re-search-forward (concat org-scheduled-string " "
13128 org-last-inserted-timestamp)
13130 (outline-next-heading) (point)) t)
13131 (goto-char (1- (match-end 0)))
13132 (insert " " repeater)
13133 (setq org-last-inserted-timestamp
13134 (concat (substring org-last-inserted-timestamp 0 -1)
13136 (substring org-last-inserted-timestamp -1))))))
13137 (message "Scheduled to %s" org-last-inserted-timestamp))))))
13139 (defun org-get-scheduled-time (pom &optional inherit)
13140 "Get the scheduled time as a time tuple, of a format suitable
13141 for calling org-schedule with, or if there is no scheduling,
13143 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
13145 (apply 'encode-time (org-parse-time-string time)))))
13147 (defun org-get-deadline-time (pom &optional inherit)
13148 "Get the deadline as a time tuple, of a format suitable for
13149 calling org-deadline with, or if there is no scheduling, returns
13151 (let ((time (org-entry-get pom "DEADLINE" inherit)))
13153 (apply 'encode-time (org-parse-time-string time)))))
13155 (defun org-remove-timestamp-with-keyword (keyword)
13156 "Remove all time stamps with KEYWORD in the current entry."
13157 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
13160 (org-back-to-heading t)
13162 (outline-next-heading)
13163 (while (re-search-backward re beg t)
13165 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
13166 (equal (char-before) ?\ ))
13167 (backward-delete-char 1)
13168 (if (string-match "^[ \t]*$" (buffer-substring
13169 (point-at-bol) (point-at-eol)))
13170 (delete-region (point-at-bol)
13171 (min (point-max) (1+ (point-at-eol))))))))))
13173 (defvar org-time-was-given) ; dynamically scoped parameter
13174 (defvar org-end-time-was-given) ; dynamically scoped parameter
13176 (defun org-add-planning-info (what &optional time &rest remove)
13177 "Insert new timestamp with keyword in the line directly after the headline.
13178 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
13179 If non is given, the user is prompted for a date.
13180 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
13183 (let (org-time-was-given org-end-time-was-given ts
13184 end default-time default-input)
13187 (when (and (memq what '(scheduled deadline))
13189 (and (stringp time)
13190 (string-match "^[-+]+[0-9]" time))))
13191 ;; Try to get a default date/time from existing timestamp
13193 (org-back-to-heading t)
13194 (setq end (save-excursion (outline-next-heading) (point)))
13195 (when (re-search-forward (if (eq what 'scheduled)
13196 org-scheduled-time-regexp
13197 org-deadline-time-regexp)
13199 (setq ts (match-string 1)
13201 (apply 'encode-time (org-parse-time-string ts))
13202 default-input (and ts (org-get-compact-tod ts))))))
13206 ;; This is a string (relative or absolute), set proper date
13207 (apply 'encode-time
13208 (org-read-date-analyze
13209 time default-time (decode-time default-time)))
13210 ;; If necessary, get the time from the user
13211 (or time (org-read-date nil 'to-time nil nil
13212 default-time default-input)))))
13214 (when (and org-insert-labeled-timestamps-at-point
13215 (member what '(scheduled deadline)))
13217 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
13218 (org-insert-time-stamp time org-time-was-given
13219 nil nil nil (list org-end-time-was-given))
13223 (let (col list elt ts buffer-invisibility-spec)
13224 (org-back-to-heading t)
13225 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
13226 (goto-char (match-end 1))
13227 (setq col (current-column))
13228 (goto-char (match-end 0))
13229 (if (eobp) (insert "\n") (forward-char 1))
13230 (when (and (not what)
13233 org-keyword-time-not-clock-regexp))))
13234 ;; Nothing to add, nothing to remove...... :-)
13236 (if (and (not (looking-at org-outline-regexp))
13237 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
13239 (not (equal (match-string 1) org-clock-string)))
13240 (narrow-to-region (match-beginning 0) (match-end 0))
13241 (insert-before-markers "\n")
13243 (narrow-to-region (point) (point))
13244 (and org-adapt-indentation (org-indent-to-column col)))
13245 ;; Check if we have to remove something.
13246 (setq list (cons what remove))
13248 (setq elt (pop list))
13249 (when (or (and (eq elt 'scheduled)
13250 (re-search-forward org-scheduled-time-regexp nil t))
13251 (and (eq elt 'deadline)
13252 (re-search-forward org-deadline-time-regexp nil t))
13253 (and (eq elt 'closed)
13254 (re-search-forward org-closed-time-regexp nil t)))
13256 (if (looking-at "--+<[^>]+>") (replace-match ""))))
13257 (and (looking-at "[ \t]+") (replace-match ""))
13258 (and org-adapt-indentation (bolp) (org-indent-to-column col))
13261 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
13262 (cond ((eq what 'scheduled) org-scheduled-string)
13263 ((eq what 'deadline) org-deadline-string)
13264 ((eq what 'closed) org-closed-string))
13266 (setq ts (org-insert-time-stamp
13268 (or org-time-was-given
13269 (and (eq what 'closed) org-log-done-with-time))
13271 nil nil (list org-end-time-was-given)))
13273 (if (not (or (bolp) (eq (char-before) ?\ )
13274 (memq (char-after) '(32 10))
13277 (goto-char (point-min))
13279 (if (and (looking-at "[ \t]*\n")
13280 (equal (char-before) ?\n))
13281 (delete-region (1- (point)) (point-at-eol)))
13284 (defvar org-log-note-marker (make-marker))
13285 (defvar org-log-note-purpose nil)
13286 (defvar org-log-note-state nil)
13287 (defvar org-log-note-previous-state nil)
13288 (defvar org-log-note-how nil)
13289 (defvar org-log-note-extra nil)
13290 (defvar org-log-note-window-configuration nil)
13291 (defvar org-log-note-return-to (make-marker))
13292 (defvar org-log-note-effective-time nil
13293 "Remembered current time so that dynamically scoped
13294 `org-extend-today-until' affects tha timestamps in state change
13297 (defvar org-log-post-message nil
13298 "Message to be displayed after a log note has been stored.
13299 The auto-repeater uses this.")
13301 (defun org-add-note ()
13302 "Add a note to the current entry.
13303 This is done in the same way as adding a state change note."
13305 (org-add-log-setup 'note nil nil 'findpos nil))
13307 (defvar org-property-end-re)
13308 (defun org-add-log-setup (&optional purpose state prev-state
13310 "Set up the post command hook to take a note.
13311 If this is about to TODO state change, the new state is expected in STATE.
13312 When FINDPOS is non-nil, find the correct position for the note in
13313 the current entry. If not, assume that it can be inserted at point.
13314 HOW is an indicator what kind of note should be created.
13315 EXTRA is additional text that will be inserted into the notes buffer."
13316 (let* ((org-log-into-drawer (org-log-into-drawer))
13317 (drawer (cond ((stringp org-log-into-drawer)
13318 org-log-into-drawer)
13319 (org-log-into-drawer "LOGBOOK"))))
13323 (org-back-to-heading t)
13324 (narrow-to-region (point) (save-excursion
13325 (outline-next-heading) (point)))
13326 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
13327 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
13329 (goto-char (match-end 0))
13332 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
13335 (goto-char (match-end 0))
13336 (or org-log-states-order-reversed
13337 (and (re-search-forward org-property-end-re nil t)
13338 (goto-char (1- (match-beginning 0))))))
13339 (insert "\n:" drawer ":\n:END:")
13340 (beginning-of-line 0)
13342 (beginning-of-line 2)
13345 ((and org-log-state-notes-insert-after-drawers
13347 (forward-line) (looking-at org-drawer-regexp)))
13349 (while (looking-at org-drawer-regexp)
13350 (goto-char (match-end 0))
13351 (re-search-forward org-property-end-re (point-max) t)
13353 (forward-line -1)))
13354 (unless org-log-states-order-reversed
13355 (and (= (char-after) ?\n) (forward-char 1))
13356 (org-skip-over-state-notes)
13357 (skip-chars-backward " \t\n\r")))
13358 (move-marker org-log-note-marker (point))
13359 (setq org-log-note-purpose purpose
13360 org-log-note-state state
13361 org-log-note-previous-state prev-state
13362 org-log-note-how how
13363 org-log-note-extra extra
13364 org-log-note-effective-time (org-current-effective-time))
13365 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
13367 (defun org-skip-over-state-notes ()
13368 "Skip past the list of State notes in an entry."
13369 (if (looking-at "\n[ \t]*- State") (forward-char 1))
13370 (when (ignore-errors (goto-char (org-in-item-p)))
13371 (let* ((struct (org-list-struct))
13372 (prevs (org-list-prevs-alist struct)))
13373 (while (looking-at "[ \t]*- State")
13374 (goto-char (or (org-list-get-next-item (point) struct prevs)
13375 (org-list-get-item-end (point) struct)))))))
13377 (defun org-add-log-note (&optional purpose)
13378 "Pop up a window for taking a note, and add this note later at point."
13379 (remove-hook 'post-command-hook 'org-add-log-note)
13380 (setq org-log-note-window-configuration (current-window-configuration))
13381 (delete-other-windows)
13382 (move-marker org-log-note-return-to (point))
13383 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
13384 (goto-char org-log-note-marker)
13385 (org-switch-to-buffer-other-window "*Org Note*")
13387 (if (memq org-log-note-how '(time state))
13388 (let (current-prefix-arg) (org-store-log-note))
13389 (let ((org-inhibit-startup t)) (org-mode))
13390 (insert (format "# Insert note for %s.
13391 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
13393 ((eq org-log-note-purpose 'clock-out) "stopped clock")
13394 ((eq org-log-note-purpose 'done) "closed todo item")
13395 ((eq org-log-note-purpose 'state)
13396 (format "state change from \"%s\" to \"%s\""
13397 (or org-log-note-previous-state "")
13398 (or org-log-note-state "")))
13399 ((eq org-log-note-purpose 'reschedule)
13401 ((eq org-log-note-purpose 'delschedule)
13402 "no longer scheduled")
13403 ((eq org-log-note-purpose 'redeadline)
13404 "changing deadline")
13405 ((eq org-log-note-purpose 'deldeadline)
13406 "removing deadline")
13407 ((eq org-log-note-purpose 'refile)
13409 ((eq org-log-note-purpose 'note)
13411 (t (error "This should not happen")))))
13412 (if org-log-note-extra (insert org-log-note-extra))
13413 (org-set-local 'org-finish-function 'org-store-log-note)
13414 (run-hooks 'org-log-buffer-setup-hook)))
13416 (defvar org-note-abort nil) ; dynamically scoped
13417 (defun org-store-log-note ()
13418 "Finish taking a log note, and insert it to where it belongs."
13419 (let ((txt (buffer-string)))
13420 (kill-buffer (current-buffer))
13421 (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
13423 (while (string-match "\\`# .*\n[ \t\n]*" txt)
13424 (setq txt (replace-match "" t t txt)))
13425 (if (string-match "\\s-+\\'" txt)
13426 (setq txt (replace-match "" t t txt)))
13427 (setq lines (org-split-string txt "\n"))
13428 (when (and note (string-match "\\S-" note))
13430 (org-replace-escapes
13432 (list (cons "%u" (user-login-name))
13433 (cons "%U" user-full-name)
13434 (cons "%t" (format-time-string
13435 (org-time-stamp-format 'long 'inactive)
13436 org-log-note-effective-time))
13437 (cons "%T" (format-time-string
13438 (org-time-stamp-format 'long nil)
13439 org-log-note-effective-time))
13440 (cons "%d" (format-time-string
13441 (org-time-stamp-format nil 'inactive)
13442 org-log-note-effective-time))
13443 (cons "%D" (format-time-string
13444 (org-time-stamp-format nil nil)
13445 org-log-note-effective-time))
13446 (cons "%s" (if org-log-note-state
13447 (concat "\"" org-log-note-state "\"")
13449 (cons "%S" (if org-log-note-previous-state
13450 (concat "\"" org-log-note-previous-state "\"")
13452 (if lines (setq note (concat note " \\\\")))
13454 (when (or current-prefix-arg org-note-abort)
13455 (when org-log-into-drawer
13456 (org-remove-empty-drawer-at
13457 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
13458 org-log-note-marker))
13461 (with-current-buffer (marker-buffer org-log-note-marker)
13463 (goto-char org-log-note-marker)
13464 (move-marker org-log-note-marker nil)
13466 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
13467 (setq ind (save-excursion
13468 (if (ignore-errors (goto-char (org-in-item-p)))
13469 (let ((struct (org-list-struct)))
13471 (org-list-get-top-point struct) struct))
13472 (skip-chars-backward " \r\t\n")
13474 ((and (org-at-heading-p)
13475 org-adapt-indentation)
13476 (1+ (org-current-level)))
13477 ((org-at-heading-p) 0)
13478 (t (org-get-indentation))))))
13479 (setq bul (org-list-bullet-string "-"))
13480 (org-indent-line-to ind)
13481 (insert bul (pop lines))
13482 (let ((ind-body (+ (length bul) ind)))
13485 (org-indent-line-to ind-body)
13486 (insert (pop lines))))
13487 (message "Note stored")
13488 (org-back-to-heading t)
13489 (org-cycle-hide-drawers 'children))
13490 ;; Fix `buffer-undo-list' when `org-store-log-note' is called
13491 ;; from within `org-add-log-note' because `buffer-undo-list'
13492 ;; is then modified outside of `org-with-remote-undo'.
13493 (when (eq this-command 'org-agenda-todo)
13494 (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
13495 ;; Don't add undo information when called from `org-agenda-todo'
13496 (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
13497 (set-window-configuration org-log-note-window-configuration)
13498 (with-current-buffer (marker-buffer org-log-note-return-to)
13499 (goto-char org-log-note-return-to))
13500 (move-marker org-log-note-return-to nil)
13501 (and org-log-post-message (message "%s" org-log-post-message))))
13503 (defun org-remove-empty-drawer-at (drawer pos)
13504 "Remove an empty drawer DRAWER at position POS.
13505 POS may also be a marker."
13506 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
13512 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
13513 (replace-match ""))))))
13515 (defvar org-ts-type nil)
13516 (defun org-sparse-tree (&optional arg type)
13517 "Create a sparse tree, prompt for the details.
13518 This command can create sparse trees. You first need to select the type
13519 of match used to create the tree:
13521 t Show all TODO entries.
13522 T Show entries with a specific TODO keyword.
13523 m Show entries selected by a tags/property match.
13524 p Enter a property name and its value (both with completion on existing
13525 names/values) and show entries with that property.
13526 r Show entries matching a regular expression (`/' can be used as well).
13527 b Show deadlines and scheduled items before a date.
13528 a Show deadlines and scheduled items after a date.
13529 d Show deadlines due within `org-deadline-warning-days'.
13530 D Show deadlines and scheduled items between a date range."
13532 (setq type (or type org-sparse-tree-default-date-type))
13533 (setq org-ts-type type)
13534 (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
13535 [d]eadlines [b]efore-date [a]fter-date [D]ates range
13536 [c]ycle through date types: %s"
13538 (all "all timestamps")
13539 (scheduled "only scheduled")
13540 (deadline "only deadline")
13541 (active "only active timestamps")
13542 (inactive "only inactive timestamps")
13543 (scheduled-or-deadline "scheduled/deadline")
13544 (closed "with a closed time-stamp")
13545 (otherwise "scheduled/deadline")))
13546 (let ((answer (read-char-exclusive)))
13551 (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
13552 inactive closed)))))
13553 (?d (call-interactively #'org-check-deadlines))
13554 (?b (call-interactively #'org-check-before-date))
13555 (?a (call-interactively #'org-check-after-date))
13556 (?D (call-interactively #'org-check-dates-range))
13557 (?t (call-interactively #'org-show-todo-tree))
13558 (?T (org-show-todo-tree '(4)))
13559 (?m (call-interactively #'org-match-sparse-tree))
13561 (let* ((kwd (org-icompleting-read
13562 "Property: " (mapcar #'list (org-buffer-property-keys))))
13563 (value (org-icompleting-read
13564 "Value: " (mapcar #'list (org-property-values kwd)))))
13565 (unless (string-match "\\`{.*}\\'" value)
13566 (setq value (concat "\"" value "\"")))
13567 (org-match-sparse-tree arg (concat kwd "=" value))))
13568 ((?r ?R ?/) (call-interactively #'org-occur))
13569 (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
13571 (defvar org-occur-highlights nil
13572 "List of overlays used for occur matches.")
13573 (make-variable-buffer-local 'org-occur-highlights)
13574 (defvar org-occur-parameters nil
13575 "Parameters of the active org-occur calls.
13576 This is a list, each call to org-occur pushes as cons cell,
13577 containing the regular expression and the callback, onto the list.
13578 The list can contain several entries if `org-occur' has been called
13579 several time with the KEEP-PREVIOUS argument. Otherwise, this list
13580 will only contain one set of parameters. When the highlights are
13581 removed (for example with `C-c C-c', or with the next edit (depending
13582 on `org-remove-highlights-with-change'), this variable is emptied
13584 (make-variable-buffer-local 'org-occur-parameters)
13586 (defun org-occur (regexp &optional keep-previous callback)
13587 "Make a compact tree which shows all matches of REGEXP.
13588 The tree will show the lines where the regexp matches, and all higher
13589 headlines above the match. It will also show the heading after the match,
13590 to make sure editing the matching entry is easy.
13591 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
13592 call to `org-occur' will be kept, to allow stacking of calls to this
13594 If CALLBACK is non-nil, it is a function which is called to confirm
13595 that the match should indeed be shown."
13596 (interactive "sRegexp: \nP")
13597 (when (equal regexp "")
13598 (user-error "Regexp cannot be empty"))
13599 (unless keep-previous
13600 (org-remove-occur-highlights nil nil t))
13601 (push (cons regexp callback) org-occur-parameters)
13604 (goto-char (point-min))
13605 (if (or (not keep-previous) ; do not want to keep
13606 (not org-occur-highlights)) ; no previous matches
13609 (while (re-search-forward regexp nil t)
13610 (when (or (not callback)
13611 (save-match-data (funcall callback)))
13612 (setq cnt (1+ cnt))
13613 (when org-highlight-sparse-tree-matches
13614 (org-highlight-new-match (match-beginning 0) (match-end 0)))
13615 (org-show-context 'occur-tree))))
13616 (when org-remove-highlights-with-change
13617 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
13619 (unless org-sparse-tree-open-archived-trees
13620 (org-hide-archived-subtrees (point-min) (point-max)))
13621 (run-hooks 'org-occur-hook)
13622 (if (org-called-interactively-p 'interactive)
13623 (message "%d match(es) for regexp %s" cnt regexp))
13626 (defun org-occur-next-match (&optional n reset)
13627 "Function for `next-error-function' to find sparse tree matches.
13628 N is the number of matches to move, when negative move backwards.
13629 RESET is entirely ignored - this function always goes back to the
13630 starting point when no match is found."
13631 (let* ((limit (if (< n 0) (point-min) (point-max)))
13632 (search-func (if (< n 0)
13633 'previous-single-char-property-change
13634 'next-single-char-property-change))
13639 (while (setq p1 (funcall search-func (point) 'org-type))
13640 (when (equal p1 limit)
13642 (error "No more matches"))
13643 (when (equal (get-char-property p1 'org-type) 'org-occur)
13647 (throw 'exit (point))))
13650 (error "No more matches"))))
13652 (defun org-show-context (&optional key)
13653 "Make sure point and context are visible.
13654 How much context is shown depends upon the variables
13655 `org-show-hierarchy-above', `org-show-following-heading',
13656 `org-show-entry-below' and `org-show-siblings'."
13657 (let ((heading-p (org-at-heading-p t))
13658 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
13659 (following-p (org-get-alist-option org-show-following-heading key))
13660 (entry-p (org-get-alist-option org-show-entry-below key))
13661 (siblings-p (org-get-alist-option org-show-siblings key)))
13662 ;; Show heading or entry text
13663 (if (and heading-p (not entry-p))
13664 (org-flag-heading nil) ; only show the heading
13665 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
13666 (org-show-hidden-entry))) ; show entire entry
13668 ;; Show next sibling, or heading below text
13670 (and (if heading-p (org-goto-sibling) (outline-next-heading))
13671 (org-flag-heading nil))))
13672 (when siblings-p (org-show-siblings))
13674 ;; show all higher headings, possibly with siblings
13676 (while (and (condition-case nil
13677 (progn (org-up-heading-all 1) t)
13680 (org-flag-heading nil)
13681 (when siblings-p (org-show-siblings)))))))
13683 (defvar org-reveal-start-hook nil
13684 "Hook run before revealing a location.")
13686 (defun org-reveal (&optional siblings)
13687 "Show current entry, hierarchy above it, and the following headline.
13688 This can be used to show a consistent set of context around locations
13689 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
13690 not t for the search context.
13692 With optional argument SIBLINGS, on each level of the hierarchy all
13693 siblings are shown. This repairs the tree structure to what it would
13694 look like when opened with hierarchical calls to `org-cycle'.
13695 With double optional argument \\[universal-argument] \\[universal-argument], \
13696 go to the parent and show the
13699 (run-hooks 'org-reveal-start-hook)
13700 (let ((org-show-hierarchy-above t)
13701 (org-show-following-heading t)
13702 (org-show-siblings (if siblings t org-show-siblings)))
13703 (org-show-context nil))
13704 (when (equal siblings '(16))
13706 (when (org-up-heading-safe)
13708 (run-hook-with-args 'org-cycle-hook 'subtree)))))
13710 (defun org-highlight-new-match (beg end)
13711 "Highlight from BEG to END and mark the highlight is an occur headline."
13712 (let ((ov (make-overlay beg end)))
13713 (overlay-put ov 'face 'secondary-selection)
13714 (overlay-put ov 'org-type 'org-occur)
13715 (push ov org-occur-highlights)))
13717 (defun org-remove-occur-highlights (&optional beg end noremove)
13718 "Remove the occur highlights from the buffer.
13719 BEG and END are ignored. If NOREMOVE is nil, remove this function
13720 from the `before-change-functions' in the current buffer."
13722 (unless org-inhibit-highlight-removal
13723 (mapc 'delete-overlay org-occur-highlights)
13724 (setq org-occur-highlights nil)
13725 (setq org-occur-parameters nil)
13727 (remove-hook 'before-change-functions
13728 'org-remove-occur-highlights 'local))))
13732 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
13733 "Regular expression matching the priority indicator.")
13735 (defvar org-remove-priority-next-time nil)
13737 (defun org-priority-up ()
13738 "Increase the priority of the current item."
13740 (org-priority 'up))
13742 (defun org-priority-down ()
13743 "Decrease the priority of the current item."
13745 (org-priority 'down))
13747 (defun org-priority (&optional action show)
13748 "Change the priority of an item.
13749 ACTION can be `set', `up', `down', or a character."
13751 (if (equal action '(4))
13752 (org-show-priority)
13753 (unless org-enable-priority-commands
13754 (user-error "Priority commands are disabled"))
13755 (setq action (or action 'set))
13756 (let (current new news have remove)
13758 (org-back-to-heading t)
13759 (if (looking-at org-priority-regexp)
13760 (setq current (string-to-char (match-string 2))
13763 ((eq action 'remove)
13764 (setq remove t new ?\ ))
13765 ((or (eq action 'set)
13766 (if (featurep 'xemacs) (characterp action) (integerp action)))
13767 (if (not (eq action 'set))
13769 (message "Priority %c-%c, SPC to remove: "
13770 org-highest-priority org-lowest-priority)
13772 (setq new (read-char-exclusive))))
13773 (if (and (= (upcase org-highest-priority) org-highest-priority)
13774 (= (upcase org-lowest-priority) org-lowest-priority))
13775 (setq new (upcase new)))
13776 (cond ((equal new ?\ ) (setq remove t))
13777 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
13778 (user-error "Priority must be between `%c' and `%c'"
13779 org-highest-priority org-lowest-priority))))
13782 (1- current) ; normal cycling
13783 ;; last priority was empty
13784 (if (eq last-command this-command)
13785 org-lowest-priority ; wrap around empty to lowest
13787 (if org-priority-start-cycle-with-default
13788 org-default-priority
13789 (1- org-default-priority))))))
13792 (1+ current) ; normal cycling
13793 ;; last priority was empty
13794 (if (eq last-command this-command)
13795 org-highest-priority ; wrap around empty to highest
13797 (if org-priority-start-cycle-with-default
13798 org-default-priority
13799 (1+ org-default-priority))))))
13800 (t (user-error "Invalid action")))
13801 (if (or (< (upcase new) org-highest-priority)
13802 (> (upcase new) org-lowest-priority))
13803 (if (and (memq action '(up down))
13804 (not have) (not (eq last-command this-command)))
13805 ;; `new' is from default priority
13807 "The default can not be set, see `org-default-priority' why")
13808 ;; normal cycling: `new' is beyond highest/lowest priority
13809 ;; and is wrapped around to the empty priority
13811 (setq news (format "%c" new))
13814 (replace-match "" t t nil 1)
13815 (replace-match news t t nil 2))
13817 (user-error "No priority cookie found in line")
13818 (let ((case-fold-search nil))
13819 (looking-at org-todo-line-regexp))
13822 (goto-char (match-end 2))
13823 (insert " [#" news "]"))
13824 (goto-char (match-beginning 3))
13825 (insert "[#" news "] "))))
13826 (org-preserve-lc (org-set-tags nil 'align)))
13828 (message "Priority removed")
13829 (message "Priority of current item set to %s" news)))))
13831 (defun org-show-priority ()
13832 "Show the priority of the current item.
13833 This priority is composed of the main priority given with the [#A] cookies,
13834 and by additional input from the age of a schedules or deadline entry."
13836 (let ((pri (if (eq major-mode 'org-agenda-mode)
13837 (org-get-at-bol 'priority)
13840 (beginning-of-line)
13841 (and (looking-at org-heading-regexp)
13842 (org-get-priority (match-string 0))))))))
13843 (message "Priority is %d" (if pri pri -1000))))
13845 (defun org-get-priority (s)
13846 "Find priority cookie and return priority."
13848 (if (functionp org-get-priority-function)
13849 (funcall org-get-priority-function)
13850 (if (not (string-match org-priority-regexp s))
13851 (* 1000 (- org-lowest-priority org-default-priority))
13852 (* 1000 (- org-lowest-priority
13853 (string-to-char (match-string 2 s))))))))
13857 (defvar org-agenda-archives-mode)
13858 (defvar org-map-continue-from nil
13859 "Position from where mapping should continue.
13860 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
13862 (defvar org-scanner-tags nil
13863 "The current tag list while the tags scanner is running.")
13864 (defvar org-trust-scanner-tags nil
13865 "Should `org-get-tags-at' use the tags for the scanner.
13866 This is for internal dynamical scoping only.
13867 When this is non-nil, the function `org-get-tags-at' will return the value
13868 of `org-scanner-tags' instead of building the list by itself. This
13869 can lead to large speed-ups when the tags scanner is used in a file with
13870 many entries, and when the list of tags is retrieved, for example to
13871 obtain a list of properties. Building the tags list for each entry in such
13872 a file becomes an N^2 operation - but with this variable set, it scales
13875 (defun org-scan-tags (action matcher todo-only &optional start-level)
13876 "Scan headline tags with inheritance and produce output ACTION.
13878 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
13879 or `agenda' to produce an entry list for an agenda view. It can also be
13880 a Lisp form or a function that should be called at each matched headline, in
13881 this case the return value is a list of all return values from these calls.
13883 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
13884 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
13885 only lines with a not-done TODO keyword are included in the output.
13886 This should be the same variable that was scoped into
13887 and set by `org-make-tags-matcher' when it constructed MATCHER.
13889 START-LEVEL can be a string with asterisks, reducing the scope to
13890 headlines matching this string."
13891 (require 'org-agenda)
13892 (let* ((re (concat "^"
13894 ;; Get the correct level to match
13895 (concat "\\*\\{" (number-to-string start-level) "\\} ")
13896 org-outline-regexp)
13898 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
13899 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
13900 (props (list 'face 'default
13901 'done-face 'org-agenda-done
13902 'undone-face 'default
13903 'mouse-face 'highlight
13904 'org-not-done-regexp org-not-done-regexp
13905 'org-todo-regexp org-todo-regexp
13906 'org-complex-heading-regexp org-complex-heading-regexp
13908 (format "mouse-2 or RET jump to org file %s"
13909 (abbreviate-file-name
13910 (or (buffer-file-name (buffer-base-buffer))
13911 (buffer-name (buffer-base-buffer)))))))
13912 (org-map-continue-from nil)
13913 lspos tags tags-list
13914 (tags-alist (list (cons 0 org-file-tags)))
13915 (llast 0) rtn rtn1 level category i txt
13916 todo marker entry priority)
13917 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
13918 (setq action (list 'lambda nil action)))
13920 (goto-char (point-min))
13921 (when (eq action 'sparse-tree)
13923 (org-remove-occur-highlights))
13924 (while (let (case-fold-search)
13925 (re-search-forward re nil t))
13926 (setq org-map-continue-from nil)
13928 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
13929 tags (if (match-end 4) (org-match-string-no-properties 4)))
13930 (goto-char (setq lspos (match-beginning 0)))
13931 (setq level (org-reduced-level (org-outline-level))
13932 category (org-get-category))
13933 (setq i llast llast level)
13934 ;; remove tag lists from same and sublevels
13935 (while (>= i level)
13936 (when (setq entry (assoc i tags-alist))
13937 (setq tags-alist (delete entry tags-alist)))
13939 ;; add the next tags
13941 (setq tags (org-split-string tags ":")
13943 (cons (cons level tags) tags-alist)))
13944 ;; compile tags for current headline
13946 (if org-use-tag-inheritance
13947 (apply 'append (mapcar 'cdr (reverse tags-alist)))
13949 org-scanner-tags tags-list)
13950 (when org-use-tag-inheritance
13951 (setcdr (car tags-alist)
13952 (mapcar (lambda (x)
13953 (setq x (copy-sequence x))
13954 (org-add-prop-inherited x))
13955 (cdar tags-alist))))
13956 (when (and tags org-use-tag-inheritance
13957 (or (not (eq t org-use-tag-inheritance))
13958 org-tags-exclude-from-inheritance))
13959 ;; selective inheritance, remove uninherited ones
13960 (setcdr (car tags-alist)
13961 (org-remove-uninherited-tags (cdar tags-alist))))
13964 ;; eval matcher only when the todo condition is OK
13965 (and (or (not todo-only) (member todo org-not-done-keywords))
13966 (let ((case-fold-search t) (org-trust-scanner-tags t))
13969 ;; Call the skipper, but return t if it does not skip,
13970 ;; so that the `and' form continues evaluating
13972 (unless (eq action 'sparse-tree) (org-agenda-skip))
13975 ;; Check if timestamps are deselecting this entry
13976 (or (not todo-only)
13977 (and (member todo org-not-done-keywords)
13978 (or (not org-agenda-tags-todo-honor-ignore-options)
13979 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
13981 ;; select this headline
13983 ((eq action 'sparse-tree)
13984 (and org-highlight-sparse-tree-matches
13985 (org-get-heading) (match-end 0)
13986 (org-highlight-new-match
13987 (match-beginning 1) (match-end 1)))
13988 (org-show-context 'tags-tree))
13989 ((eq action 'agenda)
13990 (setq txt (org-agenda-format-item
13993 (if (eq org-tags-match-list-sublevels 'indented)
13994 (make-string (1- level) ?.) "")
13998 priority (org-get-priority txt))
14000 (setq marker (org-agenda-new-marker))
14001 (org-add-props txt props
14002 'org-marker marker 'org-hd-marker marker 'org-category category
14004 'priority priority 'type "tagsmatch")
14006 ((functionp action)
14007 (setq org-map-continue-from nil)
14009 (setq rtn1 (funcall action))
14011 (t (user-error "Invalid action")))
14013 ;; if we are to skip sublevels, jump to end of subtree
14014 (unless org-tags-match-list-sublevels
14015 (org-end-of-subtree t)
14016 (backward-char 1))))
14017 ;; Get the correct position from where to continue
14018 (if org-map-continue-from
14019 (goto-char org-map-continue-from)
14020 (and (= (point) lspos) (end-of-line 1)))))
14021 (when (and (eq action 'sparse-tree)
14022 (not org-sparse-tree-open-archived-trees))
14023 (org-hide-archived-subtrees (point-min) (point-max)))
14026 (defun org-remove-uninherited-tags (tags)
14027 "Remove all tags that are not inherited from the list TAGS."
14029 ((eq org-use-tag-inheritance t)
14030 (if org-tags-exclude-from-inheritance
14031 (org-delete-all org-tags-exclude-from-inheritance tags)
14033 ((not org-use-tag-inheritance) nil)
14034 ((stringp org-use-tag-inheritance)
14037 (if (and (string-match org-use-tag-inheritance x)
14038 (not (member x org-tags-exclude-from-inheritance)))
14041 ((listp org-use-tag-inheritance)
14044 (if (member x org-use-tag-inheritance) x nil))
14047 (defun org-match-sparse-tree (&optional todo-only match)
14048 "Create a sparse tree according to tags string MATCH.
14049 MATCH can contain positive and negative selection of tags, like
14050 \"+WORK+URGENT-WITHBOSS\".
14051 If optional argument TODO-ONLY is non-nil, only select lines that are
14054 (org-agenda-prepare-buffers (list (current-buffer)))
14055 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
14057 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
14059 (defvar org-cached-props nil)
14060 (defun org-cached-entry-get (pom property)
14061 (if (or (eq t org-use-property-inheritance)
14062 (and (stringp org-use-property-inheritance)
14063 (string-match org-use-property-inheritance property))
14064 (and (listp org-use-property-inheritance)
14065 (member property org-use-property-inheritance)))
14066 ;; Caching is not possible, check it directly
14067 (org-entry-get pom property 'inherit)
14068 ;; Get all properties, so that we can do complicated checks easily
14069 (cdr (assoc property (or org-cached-props
14070 (setq org-cached-props
14071 (org-entry-properties pom)))))))
14073 (defun org-global-tags-completion-table (&optional files)
14074 "Return the list of all tags in all agenda buffer/files.
14075 Optional FILES argument is a list of files which can be used
14076 instead of the agenda files."
14083 (set-buffer (find-file-noselect file))
14084 (append (org-get-buffer-tags)
14085 (mapcar (lambda (x) (if (stringp (car-safe x))
14086 (list (car-safe x)) nil))
14088 (if (and files (car files))
14090 (org-agenda-files))))))))
14092 (defun org-make-tags-matcher (match)
14093 "Create the TAGS/TODO matcher form for the selection string MATCH.
14095 The variable `todo-only' is scoped dynamically into this function.
14096 It will be set to t if the matcher restricts matching to TODO entries,
14097 otherwise will not be touched.
14099 Returns a cons of the selection string MATCH and the constructed
14100 lisp form implementing the matcher. The matcher is to be evaluated
14101 at an Org entry, with point on the headline, and returns t if the
14102 entry matches the selection string MATCH. The returned lisp form
14103 references two variables with information about the entry, which
14104 must be bound around the form's evaluation: todo, the TODO keyword
14105 at the entry (or nil of none); and tags-list, the list of all tags
14106 at the entry including inherited ones. Additionally, the category
14107 of the entry (if any) must be specified as the text property
14108 'org-category on the headline.
14110 See also `org-scan-tags'.
14112 (declare (special todo-only))
14113 (unless (boundp 'todo-only)
14114 (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
14116 ;; Get a new match request, with completion against the global
14117 ;; tags table and the local tags in current buffer
14118 (let ((org-last-tags-completion-table
14120 (delq nil (append (org-get-buffer-tags)
14121 (org-global-tags-completion-table))))))
14122 (setq match (org-completing-read-no-i
14123 "Match: " 'org-tags-completion-function nil nil nil
14124 'org-tags-history))))
14126 ;; Parse the string and create a lisp form
14127 (let ((match0 match)
14128 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
14130 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
14131 orterms orlist re-p str-p level-p level-op time-p
14132 prop-p pn pv po gv rest (start 0) (ss 0))
14133 ;; Expand group tags
14134 (setq match (org-tags-expand match))
14136 ;; Check if there is a TODO part of this match, which would be the
14137 ;; part after a "/". TO make sure that this slash is not part of
14138 ;; a property value to be matched against, we also check that there
14139 ;; is no " after that slash.
14140 ;; First, find the last slash
14141 (while (string-match "/+" match ss)
14142 (setq start (match-beginning 0) ss (match-end 0)))
14143 (if (and (string-match "/+" match start)
14144 (not (save-match-data (string-match "\"" match start))))
14145 ;; match contains also a todo-matching request
14147 (setq tagsmatch (substring match 0 (match-beginning 0))
14148 todomatch (substring match (match-end 0)))
14149 (if (string-match "^!" todomatch)
14150 (setq todo-only t todomatch (substring todomatch 1)))
14151 (if (string-match "^\\s-*$" todomatch)
14152 (setq todomatch nil)))
14153 ;; only matching tags
14154 (setq tagsmatch match todomatch nil))
14156 ;; Make the tags matcher
14157 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
14158 (setq tagsmatcher t)
14159 (setq orterms (org-split-string tagsmatch "|") orlist nil)
14160 (dolist (term orterms)
14161 (while (and (equal (substring term -1) "\\") orterms)
14162 (setq term (concat term "|" (pop orterms)))) ; repair bad split
14163 (while (string-match re term)
14164 (setq rest (substring term (match-end 0))
14165 minus (and (match-end 1)
14166 (equal (match-string 1 term) "-"))
14167 tag (save-match-data (replace-regexp-in-string
14169 (match-string 2 term)))
14170 re-p (equal (string-to-char tag) ?{)
14171 level-p (match-end 4)
14172 prop-p (match-end 5)
14174 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
14176 (setq level-op (org-op-to-function (match-string 3 term)))
14177 `(,level-op level ,(string-to-number
14178 (match-string 4 term))))
14180 (setq pn (match-string 5 term)
14181 po (match-string 6 term)
14182 pv (match-string 7 term)
14183 re-p (equal (string-to-char pv) ?{)
14184 str-p (equal (string-to-char pv) ?\")
14185 time-p (save-match-data
14186 (string-match "^\"[[<].*[]>]\"$" pv))
14187 pv (if (or re-p str-p) (substring pv 1 -1) pv))
14188 (if time-p (setq pv (org-matcher-time pv)))
14189 (setq po (org-op-to-function po (if time-p 'time str-p)))
14191 ((equal pn "CATEGORY")
14192 (setq gv '(get-text-property (point) 'org-category)))
14196 (setq gv `(org-cached-entry-get nil ,pn))))
14199 `(not (string-match ,pv (or ,gv "")))
14200 `(string-match ,pv (or ,gv "")))
14202 `(,po (or ,gv "") ,pv)
14203 `(,po (string-to-number (or ,gv ""))
14204 ,(string-to-number pv) ))))
14205 (t `(member ,tag tags-list)))
14206 mm (if minus (list 'not mm) mm)
14208 (push mm tagsmatcher))
14209 (push (if (> (length tagsmatcher) 1)
14210 (cons 'and tagsmatcher)
14213 (setq tagsmatcher nil))
14214 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
14216 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
14217 ;; Make the todo matcher
14218 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
14219 (setq todomatcher t)
14220 (setq orterms (org-split-string todomatch "|") orlist nil)
14221 (dolist (term orterms)
14222 (while (string-match re term)
14223 (setq minus (and (match-end 1)
14224 (equal (match-string 1 term) "-"))
14225 kwd (match-string 2 term)
14226 re-p (equal (string-to-char kwd) ?{)
14227 term (substring term (match-end 0))
14229 `(string-match ,(substring kwd 1 -1) todo)
14230 (list 'equal 'todo kwd))
14231 mm (if minus (list 'not mm) mm))
14232 (push mm todomatcher))
14233 (push (if (> (length todomatcher) 1)
14234 (cons 'and todomatcher)
14237 (setq todomatcher nil))
14238 (setq todomatcher (if (> (length orlist) 1)
14239 (cons 'or orlist) (car orlist))))
14241 ;; Return the string and lisp forms of the matcher
14242 (setq matcher (if todomatcher
14243 (list 'and tagsmatcher todomatcher)
14246 (setq matcher (list 'and '(member todo org-not-done-keywords)
14248 (cons match0 matcher)))
14250 (defun org-tags-expand (match &optional single-as-list downcased)
14251 "Expand group tags in MATCH.
14253 This replaces every group tag in MATCH with a regexp tag search.
14254 For example, a group tag \"Work\" defined as { Work : Lab Conf }
14255 will be replaced like this:
14257 Work => {\\(?:Work\\|Lab\\|Conf\\)}
14258 +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
14259 -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
14261 Replacing by a regexp preserves the structure of the match.
14262 E.g., this expansion
14264 Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
14266 will match anything tagged with \"Lab\" and \"Home\", or tagged
14267 with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
14269 When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
14270 assumed to be a single group tag, and the function will return
14271 the list of tags in this group.
14273 When DOWNCASE is non-nil, expand downcased TAGS."
14275 (let* ((case-fold-search t)
14276 (stable org-mode-syntax-table)
14277 (tal (or org-tag-groups-alist-for-agenda
14278 org-tag-groups-alist))
14280 (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
14281 (tml (mapcar 'car tal))
14282 (rtnmatch match) rpl)
14283 ;; @ and _ are allowed as word-components in tags
14284 (modify-syntax-entry ?@ "w" stable)
14285 (modify-syntax-entry ?_ "w" stable)
14287 (with-syntax-table stable
14289 (concat "\\(?1:[+-]?\\)\\(?2:\\<"
14290 (regexp-opt tml) "\\>\\)")
14292 (let* ((dir (match-string 1 rtnmatch))
14293 (tag (match-string 2 rtnmatch))
14294 (tag (if downcased (downcase tag) tag)))
14295 (setq tml (delete tag tml))
14296 (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
14297 (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
14298 (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
14299 (if (stringp rpl) (org-add-props rpl '(grouptag t)))
14300 (setq rtnmatch (replace-match rpl t t rtnmatch)))))
14302 (or (reverse rpl) (list rtnmatch))
14304 (if single-as-list (list (if downcased (downcase match) match))
14307 (defun org-op-to-function (op &optional stringp)
14308 "Turn an operator into the appropriate function."
14311 ((equal op "<" ) '(< string< org-time<))
14312 ((equal op ">" ) '(> org-string> org-time>))
14313 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
14314 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
14315 ((member op '("=" "==")) '(= string= org-time=))
14316 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
14317 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
14319 (defun org<> (a b) (not (= a b)))
14320 (defun org-string<= (a b) (or (string= a b) (string< a b)))
14321 (defun org-string>= (a b) (not (string< a b)))
14322 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
14323 (defun org-string<> (a b) (not (string= a b)))
14324 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
14325 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
14326 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
14327 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
14328 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
14329 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
14331 "Convert S to a floating point time.
14332 If S is already a number, just return it. If it is a string, parse
14333 it as a time string and apply `float-time' to it. If S is nil, just return 0."
14337 (condition-case nil
14338 (float-time (apply 'encode-time (org-parse-time-string s)))
14342 (defun org-time-today ()
14343 "Time in seconds today at 0:00.
14344 Returns the float number of seconds since the beginning of the
14345 epoch to the beginning of today (00:00)."
14346 (float-time (apply 'encode-time
14347 (append '(0 0 0) (nthcdr 3 (decode-time))))))
14349 (defun org-matcher-time (s)
14350 "Interpret a time comparison value."
14353 ((string= s "<now>") (float-time))
14354 ((string= s "<today>") (org-time-today))
14355 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
14356 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
14357 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
14358 (+ (org-time-today)
14359 (* (string-to-number (match-string 1 s))
14360 (cdr (assoc (match-string 2 s)
14361 '(("d" . 86400.0) ("w" . 604800.0)
14362 ("m" . 2678400.0) ("y" . 31557600.0)))))))
14365 (defun org-match-any-p (re list)
14366 "Does re match any element of list?"
14367 (setq list (mapcar (lambda (x) (string-match re x)) list))
14370 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
14371 (defvar org-tags-overlay (make-overlay 1 1))
14372 (org-detach-overlay org-tags-overlay)
14374 (defun org-get-local-tags-at (&optional pos)
14375 "Get a list of tags defined in the current headline."
14376 (org-get-tags-at pos 'local))
14378 (defun org-get-local-tags ()
14379 "Get a list of tags defined in the current headline."
14380 (org-get-tags-at nil 'local))
14382 (defun org-get-tags-at (&optional pos local)
14383 "Get a list of all headline tags applicable at POS.
14384 POS defaults to point. If tags are inherited, the list contains
14385 the targets in the same sequence as the headlines appear, i.e.
14386 the tags of the current headline come last.
14387 When LOCAL is non-nil, only return tags from the current headline,
14388 ignore inherited ones."
14390 (if (and org-trust-scanner-tags
14391 (or (not pos) (equal pos (point)))
14394 (let (tags ltags lastpos parent)
14398 (goto-char (or pos (point)))
14401 (condition-case nil
14403 (org-back-to-heading t)
14404 (while (not (equal lastpos (point)))
14405 (setq lastpos (point))
14407 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
14408 (setq ltags (org-split-string
14409 (org-match-string-no-properties 1) ":"))
14411 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
14414 (org-remove-uninherited-tags ltags)
14417 (or org-use-tag-inheritance (throw 'done t))
14418 (if local (throw 'done t))
14419 (or (org-up-heading-safe) (error nil))
14424 (reverse (delete-dups
14426 (org-remove-uninherited-tags
14430 (defun org-add-prop-inherited (s)
14431 (add-text-properties 0 (length s) '(inherited t) s)
14434 (defun org-toggle-tag (tag &optional onoff)
14435 "Toggle the tag TAG for the current line.
14436 If ONOFF is `on' or `off', don't toggle but set to this state."
14439 (org-back-to-heading t)
14440 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
14443 (setq current (match-string 1))
14444 (replace-match ""))
14446 (setq current (nreverse (org-split-string current ":")))
14450 (or (member tag current) (push tag current)))
14452 (or (not (member tag current)) (setq current (delete tag current))))
14453 (t (if (member tag current)
14454 (setq current (delete tag current))
14456 (push tag current))))
14460 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
14461 (org-set-tags nil t))
14462 (delete-horizontal-space))
14463 (run-hooks 'org-after-tags-change-hook))
14466 (defun org-align-tags-here (to-col)
14467 ;; Assumes that this is a headline
14468 "Align tags on the current headline to TO-COL."
14469 (let ((pos (point)) (col (current-column)) ncol tags-l p)
14470 (beginning-of-line 1)
14471 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14472 (< pos (match-beginning 2)))
14474 (setq tags-l (- (match-end 2) (match-beginning 2)))
14475 (goto-char (match-beginning 1))
14477 (delete-region (point) (1+ (match-beginning 2)))
14478 (setq ncol (max (current-column)
14482 (- (abs to-col) tags-l))))
14484 (insert (make-string (- ncol (current-column)) ?\ ))
14485 (setq ncol (current-column))
14486 (when indent-tabs-mode (tabify p (point-at-eol)))
14487 (org-move-to-column (min ncol col)))
14490 (defun org-set-tags-command (&optional arg just-align)
14491 "Call the set-tags command for the current entry."
14493 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
14494 (org-set-tags arg just-align)
14496 (unless (and (org-region-active-p)
14497 org-loop-over-headlines-in-active-region)
14498 (org-back-to-heading t))
14499 (org-set-tags arg just-align))))
14501 (defun org-set-tags-to (data)
14502 "Set the tags of the current entry to DATA, replacing the current tags.
14503 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
14504 If DATA is nil or the empty string, any tags will be removed."
14505 (interactive "sTags: ")
14509 ((equal data "") "")
14511 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
14514 (concat ":" (mapconcat 'identity data ":") ":"))))
14517 (org-back-to-heading t)
14518 (when (looking-at org-complex-heading-regexp)
14521 (goto-char (match-beginning 5))
14523 (delete-region (point) (point-at-eol))
14524 (org-set-tags nil 'align))
14525 (goto-char (point-at-eol))
14527 (org-set-tags nil 'align)))
14528 (beginning-of-line 1)
14529 (if (looking-at ".*?\\([ \t]+\\)$")
14530 (delete-region (match-beginning 1) (match-end 1))))))
14532 (defun org-align-all-tags ()
14533 "Align the tags i all headings."
14536 (or (ignore-errors (org-back-to-heading t))
14537 (outline-next-heading))
14538 (if (org-at-heading-p)
14540 (message "No headings"))))
14542 (defvar org-indent-indentation-per-level)
14543 (defun org-set-tags (&optional arg just-align)
14544 "Set the tags for the current headline.
14545 With prefix ARG, realign all tags in headings in the current buffer.
14546 When JUST-ALIGN is non-nil, only align tags."
14548 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
14549 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
14550 'region-start-level 'region))
14551 org-loop-over-headlines-in-active-region)
14553 ;; We don't use ARG and JUST-ALIGN here because these args
14554 ;; are not useful when looping over headlines.
14556 org-loop-over-headlines-in-active-region
14557 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
14558 (let* ((re org-outline-regexp-bol)
14559 (current (unless arg (org-get-tags-string)))
14560 (col (current-column))
14561 (org-setting-tags t)
14562 table current-tags inherited-tags ; computed below when needed
14563 tags p0 c0 c1 rpl di tc level)
14566 (goto-char (point-min))
14567 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
14568 (while (re-search-forward re nil t)
14569 (org-set-tags nil t)
14571 (message "All tags realigned to column %d" org-tags-column))
14573 (setq tags current)
14574 ;; Get a new set of tags from the user
14576 (setq table (append org-tag-persistent-alist
14577 (or org-tag-alist (org-get-buffer-tags))
14579 org-complete-tags-always-offer-all-agenda-tags
14580 (org-global-tags-completion-table
14581 (org-agenda-files))))
14582 org-last-tags-completion-table table
14583 current-tags (org-split-string current ":")
14584 inherited-tags (nreverse
14585 (nthcdr (length current-tags)
14586 (nreverse (org-get-tags-at))))
14588 (if (or (eq t org-use-fast-tag-selection)
14589 (and org-use-fast-tag-selection
14590 (delq nil (mapcar 'cdr table))))
14591 (org-fast-tag-selection
14592 current-tags inherited-tags table
14593 (if org-fast-tag-selection-include-todo
14594 org-todo-key-alist))
14595 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
14597 (org-icompleting-read "Tags: "
14598 'org-tags-completion-function
14599 nil nil current 'org-tags-history))))))
14600 (while (string-match "[-+&]+" tags)
14601 ;; No boolean logic, just a list
14602 (setq tags (replace-match ":" t t tags))))
14604 (setq tags (replace-regexp-in-string "[,]" ":" tags))
14606 (if org-tags-sort-function
14607 (setq tags (mapconcat 'identity
14608 (sort (org-split-string
14609 tags (org-re "[^[:alnum:]_@#%]+"))
14610 org-tags-sort-function) ":")))
14612 (if (string-match "\\`[\t ]*\\'" tags)
14614 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
14615 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
14617 ;; Insert new tags at the correct column
14618 (beginning-of-line 1)
14619 (setq level (or (and (looking-at org-outline-regexp)
14620 (- (match-end 0) (point) 1))
14623 ((and (equal current "") (equal tags "")))
14624 ((re-search-forward
14625 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
14627 (if (equal tags "")
14629 (goto-char (match-beginning 0))
14630 (setq c0 (current-column)
14631 ;; compute offset for the case of org-indent-mode active
14632 di (if (org-bound-and-true-p org-indent-mode)
14633 (* (1- org-indent-indentation-per-level) (1- level))
14635 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
14636 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
14637 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
14638 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
14639 (replace-match rpl t t)
14640 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
14642 (t (error "Tags alignment failed")))
14643 (org-move-to-column col)
14645 (run-hooks 'org-after-tags-change-hook))))))
14647 (defun org-change-tag-in-region (beg end tag off)
14648 "Add or remove TAG for each entry in the region.
14649 This works in the agenda, and also in an org-mode buffer."
14651 (list (region-beginning) (region-end)
14652 (let ((org-last-tags-completion-table
14653 (if (derived-mode-p 'org-mode)
14655 (delq nil (append (org-get-buffer-tags)
14656 (org-global-tags-completion-table))))
14657 (org-global-tags-completion-table))))
14658 (org-icompleting-read
14659 "Tag: " 'org-tags-completion-function nil nil nil
14660 'org-tags-history))
14662 (message "[s]et or
[r]emove? ")
14663 (equal (read-char-exclusive) ?r))))
14664 (if (fboundp 'deactivate-mark) (deactivate-mark))
14665 (let ((agendap (equal major-mode 'org-agenda-mode))
14666 l1 l2 m buf pos newhead (cnt 0))
14668 (setq l2 (1- (org-current-line)))
14670 (setq l1 (org-current-line))
14671 (loop for l from l1 to l2 do
14673 (setq m (get-text-property (point) 'org-hd-marker))
14674 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
14676 (setq buf (if agendap (marker-buffer m) (current-buffer))
14677 pos (if agendap m (point)))
14678 (with-current-buffer buf
14682 (setq cnt (1+ cnt))
14683 (org-toggle-tag tag (if off 'off 'on))
14684 (setq newhead (org-get-heading)))))
14685 (and agendap (org-agenda-change-all-lines newhead m))))
14686 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
14688 (defun org-tags-completion-function (string predicate &optional flag)
14689 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
14690 (confirm (lambda (x) (stringp (car x)))))
14691 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
14692 (setq s1 (match-string 1 string)
14693 s2 (match-string 2 string))
14694 (setq s1 "" s2 string))
14698 (setq rtn (try-completion s2 ctable confirm))
14701 (concat s1 s2 (substring rtn (length s2))
14702 (if (and org-add-colon-after-tag-completion
14703 (assoc rtn ctable))
14708 (all-completions s2 ctable confirm))
14711 (assoc s2 ctable)))))
14713 (defun org-fast-tag-insert (kwd tags face &optional end)
14714 "Insert KDW, and the TAGS, the latter with face FACE.
14716 (insert (format "%-12s" (concat kwd ":"))
14717 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
14720 (defun org-fast-tag-show-exit (flag)
14723 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
14724 (replace-match ""))
14727 (org-move-to-column (- (window-width) 19) t)
14728 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
14730 (defun org-set-current-tags-overlay (current prefix)
14731 "Add an overlay to CURRENT tag with PREFIX."
14732 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
14733 (if (featurep 'xemacs)
14734 (org-overlay-display org-tags-overlay (concat prefix s)
14735 'secondary-selection)
14736 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
14737 (org-overlay-display org-tags-overlay (concat prefix s)))))
14739 (defvar org-last-tag-selection-key nil)
14740 (defun org-fast-tag-selection (current inherited table &optional todo-table)
14741 "Fast tag selection with single keys.
14742 CURRENT is the current list of tags in the headline, INHERITED is the
14743 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
14744 possibly with grouping information. TODO-TABLE is a similar table with
14745 TODO keywords, should these have keys assigned to them.
14746 If the keys are nil, a-z are automatically assigned.
14747 Returns the new tags string, or nil to not change the current settings."
14748 (let* ((fulltable (append table todo-table))
14749 (maxlen (apply 'max (mapcar
14751 (if (stringp (car x)) (string-width (car x)) 0))
14753 (buf (current-buffer))
14754 (expert (eq org-fast-tag-selection-single-key 'expert))
14756 (fwidth (+ maxlen 3 1 3))
14757 (ncol (/ (- (window-width) 4) fwidth))
14760 tg cnt c char c1 c2 ntable tbl rtn
14761 ov-start ov-end ov-prefix
14762 (exit-after-next org-fast-tag-selection-single-key)
14763 (done-keywords org-done-keywords)
14766 (beginning-of-line 1)
14768 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14769 (setq ov-start (match-beginning 1)
14770 ov-end (match-end 1)
14772 (setq ov-start (1- (point-at-eol))
14773 ov-end (1+ ov-start))
14774 (skip-chars-forward "^\n\r")
14777 (buffer-substring (1- (point)) (point))
14778 (if (> (current-column) org-tags-column)
14780 (make-string (- org-tags-column (current-column)) ?\ ))))))
14781 (move-overlay org-tags-overlay ov-start ov-end)
14782 (save-window-excursion
14784 (set-buffer (get-buffer-create " *Org tags*"))
14785 (delete-other-windows)
14786 (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
14787 (org-switch-to-buffer-other-window " *Org tags*"))
14789 (org-set-local 'org-done-keywords done-keywords)
14790 (org-fast-tag-insert "Inherited" inherited i-face "\n")
14791 (org-fast-tag-insert "Current" current c-face "\n\n")
14792 (org-fast-tag-show-exit exit-after-next)
14793 (org-set-current-tags-overlay current ov-prefix)
14794 (setq tbl fulltable char ?a cnt 0)
14797 ((equal (car e) :startgroup)
14798 (push '() groups) (setq ingroup t)
14799 (when (not (= cnt 0))
14802 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
14803 ((equal (car e) :endgroup)
14804 (setq ingroup nil cnt 0)
14805 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
14806 ((equal e '(:newline))
14807 (when (not (= cnt 0))
14811 (while (equal (car tbl) '(:newline))
14813 (setq tbl (cdr tbl)))))
14814 ((equal e '(:grouptags)) nil)
14816 (setq tg (copy-sequence (car e)) c2 nil)
14819 ;; automatically assign a character.
14820 (setq c1 (string-to-char
14821 (downcase (substring
14822 tg (if (= (string-to-char tg) ?@) 1 0)))))
14823 (if (or (rassoc c1 ntable) (rassoc c1 table))
14824 (while (or (rassoc char ntable) (rassoc char table))
14825 (setq char (1+ char)))
14827 (setq c (or c2 char)))
14828 (if ingroup (push tg (car groups)))
14829 (setq tg (org-add-props tg nil 'face
14831 ((not (assoc tg table))
14832 (org-get-todo-face tg))
14833 ((member tg current) c-face)
14834 ((member tg inherited) i-face))))
14835 (if (equal (caar tbl) :grouptags)
14836 (org-add-props tg nil 'face 'org-tag-group))
14837 (if (and (= cnt 0) (not ingroup)) (insert " "))
14838 (insert "[" c "] " tg (make-string
14839 (- fwidth 4 (length tg)) ?\ ))
14840 (push (cons tg c) ntable)
14841 (when (= (setq cnt (1+ cnt)) ncol)
14843 (if ingroup (insert " "))
14845 (setq ntable (nreverse ntable))
14847 (goto-char (point-min))
14848 (if (not expert) (org-fit-window-to-buffer))
14852 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
14853 (if (not groups) "no " "")
14854 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
14855 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
14856 (setq org-last-tag-selection-key c)
14858 ((= c ?\r) (throw 'exit t))
14860 (setq groups (not groups))
14861 (goto-char (point-min))
14862 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
14865 (org-fast-tag-show-exit
14866 (setq exit-after-next (not exit-after-next)))
14868 (delete-other-windows)
14869 (set-window-buffer (split-window-vertically) " *Org tags*")
14870 (org-switch-to-buffer-other-window " *Org tags*")
14871 (org-fit-window-to-buffer)))
14873 (and (= c ?q) (not (rassoc c ntable))))
14874 (org-detach-overlay org-tags-overlay)
14875 (setq quit-flag t))
14878 (if exit-after-next (setq exit-after-next 'now)))
14880 (condition-case nil
14881 (setq tg (org-icompleting-read
14884 (with-current-buffer buf
14885 (org-get-buffer-tags)))))
14886 (quit (setq tg "")))
14887 (when (string-match "\\S-" tg)
14888 (add-to-list 'buffer-tags (list tg))
14889 (if (member tg current)
14890 (setq current (delete tg current))
14891 (push tg current)))
14892 (if exit-after-next (setq exit-after-next 'now)))
14893 ((setq e (rassoc c todo-table) tg (car e))
14894 (with-current-buffer buf
14895 (save-excursion (org-todo tg)))
14896 (if exit-after-next (setq exit-after-next 'now)))
14897 ((setq e (rassoc c ntable) tg (car e))
14898 (if (member tg current)
14899 (setq current (delete tg current))
14900 (loop for g in groups do
14903 (setq current (delete x current)))
14906 (if exit-after-next (setq exit-after-next 'now))))
14908 ;; Create a sorted list
14912 (assoc b (cdr (memq (assoc a ntable) ntable))))))
14913 (if (eq exit-after-next 'now) (throw 'exit t))
14914 (goto-char (point-min))
14915 (beginning-of-line 2)
14916 (delete-region (point) (point-at-eol))
14917 (org-fast-tag-insert "Current" current c-face)
14918 (org-set-current-tags-overlay current ov-prefix)
14919 (while (re-search-forward
14920 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
14921 (setq tg (match-string 1))
14922 (add-text-properties
14923 (match-beginning 1) (match-end 1)
14926 ((member tg current) c-face)
14927 ((member tg inherited) i-face)
14928 (t (get-text-property (match-beginning 1) 'face))))))
14929 (goto-char (point-min)))))
14930 (org-detach-overlay org-tags-overlay)
14932 (mapconcat 'identity current ":")
14935 (defun org-get-tags-string ()
14936 "Get the TAGS string in the current headline."
14937 (unless (org-at-heading-p t)
14938 (user-error "Not on a heading"))
14940 (beginning-of-line 1)
14941 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14942 (org-match-string-no-properties 1)
14945 (defun org-get-tags ()
14946 "Get the list of tags specified in the current headline."
14947 (org-split-string (org-get-tags-string) ":"))
14949 (defun org-get-buffer-tags ()
14950 "Get a table of all tags used in the buffer, for completion."
14953 (goto-char (point-min))
14954 (while (re-search-forward
14955 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
14956 (when (equal (char-after (point-at-bol 0)) ?*)
14957 (mapc (lambda (x) (add-to-list 'tags x))
14958 (org-split-string (org-match-string-no-properties 1) ":")))))
14959 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
14960 (mapcar 'list tags)))
14962 ;;;; The mapping API
14964 (defun org-map-entries (func &optional match scope &rest skip)
14965 "Call FUNC at each headline selected by MATCH in SCOPE.
14967 FUNC is a function or a lisp form. The function will be called without
14968 arguments, with the cursor positioned at the beginning of the headline.
14969 The return values of all calls to the function will be collected and
14970 returned as a list.
14972 The call to FUNC will be wrapped into a save-excursion form, so FUNC
14973 does not need to preserve point. After evaluation, the cursor will be
14974 moved to the end of the line (presumably of the headline of the
14975 processed entry) and search continues from there. Under some
14976 circumstances, this may not produce the wanted results. For example,
14977 if you have removed (e.g. archived) the current (sub)tree it could
14978 mean that the next entry will be skipped entirely. In such cases, you
14979 can specify the position from where search should continue by making
14980 FUNC set the variable `org-map-continue-from' to the desired buffer
14983 MATCH is a tags/property/todo match as it is used in the agenda tags view.
14984 Only headlines that are matched by this query will be considered during
14985 the iteration. When MATCH is nil or t, all headlines will be
14986 visited by the iteration.
14988 SCOPE determines the scope of this command. It can be any of:
14990 nil The current buffer, respecting the restriction if any
14991 tree The subtree started with the entry at point
14992 region The entries within the active region, if any
14994 The entries within the active region, but only those at
14995 the same level than the first one.
14996 file The current buffer, without restriction
14998 The current buffer, and any archives associated with it
14999 agenda All agenda files
15000 agenda-with-archives
15001 All agenda files with any archive files associated with them
15003 If this is a list, all files in the list will be scanned
15005 The remaining args are treated as settings for the skipping facilities of
15006 the scanner. The following items can be given here:
15008 archive skip trees with the archive tag
15009 comment skip trees with the COMMENT keyword
15010 function or Emacs Lisp form:
15011 will be used as value for `org-agenda-skip-function', so
15012 whenever the function returns a position, FUNC will not be
15013 called for that entry and search will continue from the
15016 If your function needs to retrieve the tags including inherited tags
15017 at the *current* entry, you can use the value of the variable
15018 `org-scanner-tags' which will be much faster than getting the value
15019 with `org-get-tags-at'. If your function gets properties with
15020 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
15021 to t around the call to `org-entry-properties' to get the same speedup.
15022 Note that if your function moves around to retrieve tags and properties at
15023 a *different* entry, you cannot use these techniques."
15024 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
15025 (not (org-region-active-p)))
15026 (let* ((org-agenda-archives-mode nil) ; just to make sure
15027 (org-agenda-skip-archived-trees (memq 'archive skip))
15028 (org-agenda-skip-comment-trees (memq 'comment skip))
15029 (org-agenda-skip-function
15030 (car (org-delete-all '(comment archive) skip)))
15031 (org-tags-match-list-sublevels t)
15032 (start-level (eq scope 'region-start-level))
15034 org-todo-keywords-for-agenda
15035 org-done-keywords-for-agenda
15036 org-todo-keyword-alist-for-agenda
15037 org-drawers-for-agenda
15038 org-tag-alist-for-agenda
15042 ((eq match t) (setq matcher t))
15043 ((eq match nil) (setq matcher t))
15044 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
15048 (cond ((eq scope 'tree)
15049 (org-back-to-heading t)
15050 (org-narrow-to-subtree)
15052 ((and (or (eq scope 'region) (eq scope 'region-start-level))
15053 (org-region-active-p))
15054 ;; If needed, set start-level to a string like "2"
15057 (goto-char (region-beginning))
15058 (unless (org-at-heading-p) (outline-next-heading))
15059 (setq start-level (org-current-level))))
15060 (narrow-to-region (region-beginning)
15062 (goto-char (region-end))
15063 (unless (and (bolp) (org-at-heading-p))
15064 (outline-next-heading))
15070 (org-agenda-prepare-buffers
15071 (and buffer-file-name (list buffer-file-name)))
15072 (setq res (org-scan-tags func matcher todo-only start-level)))
15073 ;; Get the right scope
15075 ((and scope (listp scope) (symbolp (car scope)))
15076 (setq scope (eval scope)))
15077 ((eq scope 'agenda)
15078 (setq scope (org-agenda-files t)))
15079 ((eq scope 'agenda-with-archives)
15080 (setq scope (org-agenda-files t))
15081 (setq scope (org-add-archive-files scope)))
15083 (setq scope (and buffer-file-name (list buffer-file-name))))
15084 ((eq scope 'file-with-archives)
15085 (setq scope (org-add-archive-files (list (buffer-file-name))))))
15086 (org-agenda-prepare-buffers scope)
15087 (dolist (file scope)
15088 (with-current-buffer (org-find-base-buffer-visiting file)
15092 (goto-char (point-min))
15093 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
15098 ;;; Setting and retrieving properties
15100 (defconst org-special-properties
15101 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
15102 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
15103 "The special properties valid in Org-mode.
15105 These are properties that are not defined in the property drawer,
15106 but in some other way.")
15108 (defconst org-default-properties
15109 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
15110 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
15111 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
15112 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
15113 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
15114 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
15115 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
15116 "Some properties that are used by Org-mode for various purposes.
15117 Being in this list makes sure that they are offered for completion.")
15119 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
15120 "Regular expression matching the first line of a property drawer.")
15122 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
15123 "Regular expression matching the last line of a property drawer.")
15125 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
15126 "Regular expression matching the first line of a property drawer.")
15128 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
15129 "Regular expression matching the first line of a property drawer.")
15131 (defconst org-property-drawer-re
15132 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
15133 org-property-end-re "\\)\n?")
15134 "Matches an entire property drawer.")
15136 (defconst org-clock-drawer-re
15137 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
15138 org-property-end-re "\\)\n?")
15139 "Matches an entire clock drawer.")
15141 (defun org-property-action ()
15142 "Do an action on properties."
15145 (org-at-property-p)
15146 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
15147 (setq c (read-char-exclusive))
15150 (call-interactively 'org-set-property))
15152 (call-interactively 'org-delete-property))
15154 (call-interactively 'org-delete-property-globally))
15156 (call-interactively 'org-compute-property-at-point))
15157 (t (user-error "No such property action %c" c)))))
15159 (defun org-inc-effort ()
15160 "Increment the value of the effort property in the current entry."
15162 (org-set-effort nil t))
15164 (defvar org-clock-effort) ;; Defined in org-clock.el
15165 (defvar org-clock-current-task) ;; Defined in org-clock.el
15166 (defun org-set-effort (&optional value increment)
15167 "Set the effort property of the current entry.
15168 With numerical prefix arg, use the nth allowed value, 0 stands for the
15169 10th allowed value.
15171 When INCREMENT is non-nil, set the property to the next allowed value."
15173 (if (equal value 0) (setq value 10))
15174 (let* ((completion-ignore-case t)
15175 (prop org-effort-property)
15176 (cur (org-entry-get nil prop))
15177 (allowed (org-property-get-allowed-values nil prop 'table))
15178 (existing (mapcar 'list (org-property-values prop)))
15179 (heading (nth 4 (org-heading-components)))
15182 ((stringp value) value)
15183 ((and allowed (integerp value))
15184 (or (car (nth (1- value) allowed))
15185 (car (org-last allowed))))
15186 ((and allowed increment)
15187 (or (caadr (member (list cur) allowed))
15188 (user-error "Allowed effort values are not set")))
15190 (message "Select 1-9,0, [RET%s]: %s"
15191 (if cur (concat "=" cur) "")
15192 (mapconcat 'car allowed " "))
15193 (setq rpl (read-char-exclusive))
15194 (if (equal rpl ?\r)
15196 (setq rpl (- rpl ?0))
15197 (if (equal rpl 0) (setq rpl 10))
15198 (if (and (> rpl 0) (<= rpl (length allowed)))
15199 (car (nth (1- rpl) allowed))
15200 (org-completing-read "Effort: " allowed nil))))
15202 (let (org-completion-use-ido org-completion-use-iswitchb)
15203 (org-completing-read
15204 (concat "Effort " (if (and cur (string-match "\\S-" cur))
15205 (concat "[" cur "]") "")
15207 existing nil nil "" nil cur))))))
15208 (unless (equal (org-entry-get nil prop) val)
15209 (org-entry-put nil prop val))
15211 (org-back-to-heading t)
15212 (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
15213 (when (string= heading org-clock-current-task)
15214 (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
15215 (org-clock-update-mode-line))
15216 (message "%s is now %s" prop val)))
15218 (defun org-at-property-p ()
15219 "Is cursor inside a property drawer?"
15221 (when (equal 'node-property (car (org-element-at-point)))
15222 (beginning-of-line 1)
15223 (looking-at org-property-re))))
15225 (defun org-get-property-block (&optional beg end force)
15226 "Return the (beg . end) range of the body of the property drawer.
15227 BEG and END are the beginning and end of the current subtree, or of
15228 the part before the first headline. If they are not given, they will
15229 be found. If the drawer does not exist and FORCE is non-nil, create
15233 (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
15234 (progn (org-back-to-heading t) (point))))
15235 (end (or end (and (not (outline-next-heading)) (point-max))
15238 (if (re-search-forward org-property-start-re end t)
15239 (setq beg (1+ (match-end 0)))
15242 (org-insert-property-drawer)
15243 (setq end (progn (outline-next-heading) (point))))
15246 (if (re-search-forward org-property-start-re end t)
15247 (setq beg (1+ (match-end 0)))))
15248 (if (re-search-forward org-property-end-re end t)
15249 (setq end (match-beginning 0))
15250 (or force (throw 'exit nil))
15254 (insert ":END:\n"))
15257 (defun org-entry-properties (&optional pom which specific)
15258 "Get all properties of the entry at point-or-marker POM.
15259 This includes the TODO keyword, the tags, time strings for deadline,
15260 scheduled, and clocking, and any additional properties defined in the
15261 entry. The return value is an alist, keys may occur multiple times
15262 if the property key was used several times.
15263 POM may also be nil, in which case the current entry is used.
15264 If WHICH is nil or `all', get all properties. If WHICH is
15265 `special' or `standard', only get that subclass. If WHICH
15266 is a string only get exactly this property. SPECIFIC can be a string, the
15267 specific property we are interested in. Specifying it can speed
15268 things up because then unnecessary parsing is avoided."
15269 (setq which (or which 'all))
15270 (org-with-wide-buffer
15271 (org-with-point-at pom
15272 (let ((clockstr (substring org-clock-string 0 -1))
15273 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
15274 (case-fold-search nil)
15275 beg end range props sum-props key key1 value string clocksum clocksumt)
15276 (when (and (derived-mode-p 'org-mode)
15277 (ignore-errors (org-back-to-heading t)))
15279 (setq sum-props (get-text-property (point) 'org-summaries))
15280 (setq clocksum (get-text-property (point) :org-clock-minutes)
15281 clocksumt (get-text-property (point) :org-clock-minutes-today))
15282 (outline-next-heading)
15284 (when (memq which '(all special))
15285 ;; Get the special properties, like TODO and tags
15287 (when (and (or (not specific) (string= specific "TODO"))
15288 (looking-at org-todo-line-regexp) (match-end 2))
15289 (push (cons "TODO" (org-match-string-no-properties 2)) props))
15290 (when (and (or (not specific) (string= specific "PRIORITY"))
15291 (looking-at org-priority-regexp))
15292 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
15293 (when (or (not specific) (string= specific "FILE"))
15294 (push (cons "FILE" buffer-file-name) props))
15295 (when (and (or (not specific) (string= specific "TAGS"))
15296 (setq value (org-get-tags-string))
15297 (string-match "\\S-" value))
15298 (push (cons "TAGS" value) props))
15299 (when (and (or (not specific) (string= specific "ALLTAGS"))
15300 (setq value (org-get-tags-at)))
15301 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
15304 (when (or (not specific) (string= specific "BLOCKED"))
15305 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
15306 (when (or (not specific)
15308 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
15309 "TIMESTAMP" "TIMESTAMP_IA")))
15311 (while (and (re-search-forward org-maybe-keyword-time-regexp end t)
15312 (not (text-property-any 0 (length (match-string 0))
15313 'face 'font-lock-comment-face
15314 (match-string 0))))
15315 (setq key (if (match-end 1)
15316 (substring (org-match-string-no-properties 1)
15318 string (if (equal key clockstr)
15320 (buffer-substring-no-properties
15321 (match-beginning 3) (goto-char
15323 (substring (org-match-string-no-properties 3)
15325 ;; Get the correct property name from the key. This is
15326 ;; necessary if the user has configured time keywords.
15327 (setq key1 (concat key ":"))
15331 (if (= (char-after (match-beginning 3)) ?\[)
15332 "TIMESTAMP_IA" "TIMESTAMP")))
15333 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
15334 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
15335 ((equal key1 org-closed-string) (setq key "CLOSED"))
15336 ((equal key1 org-clock-string) (setq key "CLOCK")))
15337 (if (and specific (equal key specific) (not (equal key "CLOCK")))
15339 (push (cons key string) props)
15340 ;; no need to search further if match is found
15342 (when (or (equal key "CLOCK") (not (assoc key props)))
15343 (push (cons key string) props)))))))
15345 (when (memq which '(all standard))
15346 ;; Get the standard properties, like :PROP: ...
15347 (setq range (org-get-property-block beg end))
15349 (goto-char (car range))
15350 (while (re-search-forward org-property-re
15352 (setq key (org-match-string-no-properties 2)
15353 value (org-trim (or (org-match-string-no-properties 3) "")))
15354 (unless (member key excluded)
15355 (push (cons key (or value "")) props)))))
15357 (push (cons "CLOCKSUM"
15358 (org-columns-number-to-string (/ (float clocksum) 60.)
15362 (push (cons "CLOCKSUM_T"
15363 (org-columns-number-to-string (/ (float clocksumt) 60.)
15366 (unless (assoc "CATEGORY" props)
15367 (push (cons "CATEGORY" (org-get-category)) props))
15368 (append sum-props (nreverse props)))))))
15370 (defun org-entry-get (pom property &optional inherit literal-nil)
15371 "Get value of PROPERTY for entry or content at point-or-marker POM.
15372 If INHERIT is non-nil and the entry does not have the property,
15373 then also check higher levels of the hierarchy.
15374 If INHERIT is the symbol `selective', use inheritance only if the setting
15375 in `org-use-property-inheritance' selects PROPERTY for inheritance.
15376 If the property is present but empty, the return value is the empty string.
15377 If the property is not present at all, nil is returned.
15379 Return the value as a string.
15381 If LITERAL-NIL is set, return the string value \"nil\" as a string,
15382 do not interpret it as the list atom nil. This is used for inheritance
15383 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
15384 (org-with-point-at pom
15385 (if (and inherit (if (eq inherit 'selective)
15386 (org-property-inherit-p property)
15388 (org-entry-get-with-inheritance property literal-nil)
15389 (if (member property org-special-properties)
15390 ;; We need a special property. Use `org-entry-properties'
15391 ;; to retrieve it, but specify the wanted property
15392 (cdr (assoc property (org-entry-properties nil 'special property)))
15393 (org-with-wide-buffer
15394 (let ((range (org-get-property-block)))
15395 (when (and range (not (eq (car range) (cdr range)))
15397 (goto-char (car range))
15399 (concat (org-re-property property) "\\|"
15400 (org-re-property (concat property "+")))
15403 (list (or (assoc property org-file-properties)
15404 (assoc property org-global-properties)
15405 (assoc property org-global-properties-fixed))))
15407 (when (re-search-forward
15408 (org-re-property key) (cdr range) t)
15410 (org-update-property-plist
15413 (org-match-string-no-properties 3) "")
15416 (goto-char (car range))
15417 (funcall ap property)
15418 (goto-char (car range))
15419 (while (funcall ap (concat property "+")))
15420 (setq val (cdr (assoc property props)))
15421 (when val (if literal-nil val (org-not-nil val)))))))))))
15423 (defun org-property-or-variable-value (var &optional inherit)
15424 "Check if there is a property fixing the value of VAR.
15425 If yes, return this value. If not, return the current value of the variable."
15426 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
15427 (if (and prop (stringp prop) (string-match "\\S-" prop))
15429 (symbol-value var))))
15431 (defun org-entry-delete (pom property &optional delete-empty-drawer)
15432 "Delete the property PROPERTY from entry at point-or-marker POM.
15433 When optional argument DELETE-EMPTY-DRAWER is a string, it defines
15434 an empty drawer to delete."
15435 (org-with-point-at pom
15436 (if (member property org-special-properties)
15437 nil ; cannot delete these properties.
15438 (let ((range (org-get-property-block)))
15440 (goto-char (car range))
15442 (org-re-property property nil t)
15445 (delete-region (match-beginning 0) (1+ (point-at-eol)))
15446 (and delete-empty-drawer
15447 (org-remove-empty-drawer-at
15448 delete-empty-drawer (car range)))
15452 ;; Multi-values properties are properties that contain multiple values
15453 ;; These values are assumed to be single words, separated by whitespace.
15454 (defun org-entry-add-to-multivalued-property (pom property value)
15455 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
15456 (let* ((old (org-entry-get pom property))
15457 (values (and old (org-split-string old "[ \t]"))))
15458 (setq value (org-entry-protect-space value))
15459 (unless (member value values)
15460 (setq values (append values (list value)))
15461 (org-entry-put pom property
15462 (mapconcat 'identity values " ")))))
15464 (defun org-entry-remove-from-multivalued-property (pom property value)
15465 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
15466 (let* ((old (org-entry-get pom property))
15467 (values (and old (org-split-string old "[ \t]"))))
15468 (setq value (org-entry-protect-space value))
15469 (when (member value values)
15470 (setq values (delete value values))
15471 (org-entry-put pom property
15472 (mapconcat 'identity values " ")))))
15474 (defun org-entry-member-in-multivalued-property (pom property value)
15475 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
15476 (let* ((old (org-entry-get pom property))
15477 (values (and old (org-split-string old "[ \t]"))))
15478 (setq value (org-entry-protect-space value))
15479 (member value values)))
15481 (defun org-entry-get-multivalued-property (pom property)
15482 "Return a list of values in a multivalued property."
15483 (let* ((value (org-entry-get pom property))
15484 (values (and value (org-split-string value "[ \t]"))))
15485 (mapcar 'org-entry-restore-space values)))
15487 (defun org-entry-put-multivalued-property (pom property &rest values)
15488 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
15489 VALUES should be a list of strings. Spaces will be protected."
15490 (org-entry-put pom property
15491 (mapconcat 'org-entry-protect-space values " "))
15492 (let* ((value (org-entry-get pom property))
15493 (values (and value (org-split-string value "[ \t]"))))
15494 (mapcar 'org-entry-restore-space values)))
15496 (defun org-entry-protect-space (s)
15497 "Protect spaces and newline in string S."
15498 (while (string-match " " s)
15499 (setq s (replace-match "%20" t t s)))
15500 (while (string-match "\n" s)
15501 (setq s (replace-match "%0A" t t s)))
15504 (defun org-entry-restore-space (s)
15505 "Restore spaces and newline in string S."
15506 (while (string-match "%20" s)
15507 (setq s (replace-match " " t t s)))
15508 (while (string-match "%0A" s)
15509 (setq s (replace-match "\n" t t s)))
15512 (defvar org-entry-property-inherited-from (make-marker)
15513 "Marker pointing to the entry from where a property was inherited.
15514 Each call to `org-entry-get-with-inheritance' will set this marker to the
15515 location of the entry where the inheritance search matched. If there was
15516 no match, the marker will point nowhere.
15517 Note that also `org-entry-get' calls this function, if the INHERIT flag
15520 (defun org-entry-get-with-inheritance (property &optional literal-nil)
15521 "Get PROPERTY of entry or content at point, search higher levels if needed.
15522 The search will stop at the first ancestor which has the property defined.
15523 If the value found is \"nil\", return nil to show that the property
15524 should be considered as undefined (this is the meaning of nil here).
15525 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
15526 (move-marker org-entry-property-inherited-from nil)
15533 (when (setq tmp (org-entry-get nil property nil literal-nil))
15534 (or (ignore-errors (org-back-to-heading t))
15535 (goto-char (point-min)))
15536 (move-marker org-entry-property-inherited-from (point))
15538 (or (ignore-errors (org-up-heading-safe))
15539 (throw 'ex nil))))))
15541 (cdr (assoc property org-file-properties))
15542 (cdr (assoc property org-global-properties))
15543 (cdr (assoc property org-global-properties-fixed))))
15544 (if literal-nil tmp (org-not-nil tmp))))
15546 (defvar org-property-changed-functions nil
15547 "Hook called when the value of a property has changed.
15548 Each hook function should accept two arguments, the name of the property
15549 and the new value.")
15551 (defun org-entry-put (pom property value)
15552 "Set PROPERTY to VALUE for entry at point-or-marker POM.
15553 If the value is nil, it is converted to the empty string.
15554 If it is not a string, an error is raised."
15555 (cond ((null value) (setq value ""))
15556 ((not (stringp value))
15557 (error "Properties values should be strings.")))
15558 (org-with-point-at pom
15559 (org-back-to-heading t)
15560 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
15563 ((equal property "TODO")
15564 (when (and (string-match "\\S-" value)
15565 (not (member value org-todo-keywords-1)))
15566 (user-error "\"%s\" is not a valid TODO state" value))
15567 (if (or (not value)
15568 (not (string-match "\\S-" value)))
15569 (setq value 'none))
15571 (org-set-tags nil 'align))
15572 ((equal property "PRIORITY")
15573 (org-priority (if (and value (string-match "\\S-" value))
15574 (string-to-char value) ?\ ))
15575 (org-set-tags nil 'align))
15576 ((equal property "CLOCKSUM")
15577 (if (not (re-search-forward
15578 (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
15579 (error "Cannot find a clock log")
15580 (goto-char (- (match-end 1) 2))
15582 ((eq value 'earlier) (org-timestamp-down))
15583 ((eq value 'later) (org-timestamp-up)))
15584 (org-clock-sum-current-item)))
15585 ((equal property "SCHEDULED")
15586 (if (re-search-forward org-scheduled-time-regexp end t)
15588 ((eq value 'earlier) (org-timestamp-change -1 'day))
15589 ((eq value 'later) (org-timestamp-change 1 'day))
15590 (t (call-interactively 'org-schedule)))
15591 (call-interactively 'org-schedule)))
15592 ((equal property "DEADLINE")
15593 (if (re-search-forward org-deadline-time-regexp end t)
15595 ((eq value 'earlier) (org-timestamp-change -1 'day))
15596 ((eq value 'later) (org-timestamp-change 1 'day))
15597 (t (call-interactively 'org-deadline)))
15598 (call-interactively 'org-deadline)))
15599 ((member property org-special-properties)
15600 (error "The %s property can not yet be set with `org-entry-put'"
15602 (t ; a non-special property
15603 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
15604 (setq range (org-get-property-block beg end 'force))
15605 (goto-char (car range))
15606 (if (re-search-forward
15607 (org-re-property property nil t) (cdr range) t)
15609 (delete-region (match-beginning 0) (match-end 0))
15610 (goto-char (match-beginning 0)))
15611 (goto-char (cdr range))
15615 (insert ":" property ":")
15616 (and value (insert " " value))
15617 (org-indent-line)))))
15618 (run-hook-with-args 'org-property-changed-functions property value)))
15620 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
15621 "Get all property keys in the current buffer.
15622 With INCLUDE-SPECIALS, also list the special properties that reflect things
15623 like tags and TODO state.
15624 With INCLUDE-DEFAULTS, also include properties that has special meaning
15625 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
15627 With INCLUDE-COLUMNS, also include property names given in COLUMN
15628 formats in the current buffer."
15629 (let (rtn range cfmt s p)
15633 (goto-char (point-min))
15634 (while (re-search-forward org-property-start-re nil t)
15635 (setq range (org-get-property-block))
15636 (goto-char (car range))
15637 (while (re-search-forward org-property-re
15639 (add-to-list 'rtn (org-match-string-no-properties 2)))
15640 (outline-next-heading))))
15642 (when include-specials
15643 (setq rtn (append org-special-properties rtn)))
15645 (when include-defaults
15646 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
15647 (add-to-list 'rtn org-effort-property))
15649 (when include-columns
15653 (goto-char (point-min))
15654 (while (re-search-forward
15655 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
15657 (setq cfmt (match-string 2) s 0)
15658 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
15660 (setq s (match-end 0)
15661 p (match-string 1 cfmt))
15662 (unless (or (equal p "ITEM")
15663 (member p org-special-properties))
15664 (add-to-list 'rtn (match-string 1 cfmt))))))))
15666 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
15668 (defun org-property-values (key)
15669 "Return a list of all values of property KEY in the current buffer."
15673 (goto-char (point-min))
15674 (let ((re (org-re-property key))
15676 (while (re-search-forward re nil t)
15677 (add-to-list 'values (org-trim (match-string 3))))
15678 (delete "" values)))))
15680 (defun org-insert-property-drawer ()
15681 "Insert a property drawer into the current entry."
15682 (org-back-to-heading t)
15683 (looking-at org-outline-regexp)
15684 (let ((indent (if org-adapt-indentation
15685 (- (match-end 0) (match-beginning 0))
15688 (re (concat "^[ \t]*" org-keyword-time-regexp))
15690 (outline-next-heading)
15693 (while (re-search-forward re end t))
15694 (setq hiddenp (outline-invisible-p))
15696 (and (equal (char-after) ?\n) (forward-char 1))
15697 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
15698 (if (member (match-string 1) '("CLOCK:" ":END:"))
15699 ;; just skip this line
15700 (beginning-of-line 2)
15701 ;; Drawer start, find the end
15702 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
15703 (beginning-of-line 1)))
15704 (org-skip-over-state-notes)
15705 (skip-chars-backward " \t\n\r")
15706 (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
15708 (goto-char (point-at-eol))
15709 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
15710 (beginning-of-line 0)
15711 (org-indent-to-column indent)
15712 (beginning-of-line 2)
15713 (org-indent-to-column indent)
15714 (beginning-of-line 0)
15717 (org-back-to-heading t)
15719 (org-flag-drawer t))))
15721 (defun org-insert-drawer (&optional arg drawer)
15722 "Insert a drawer at point.
15724 Optional argument DRAWER, when non-nil, is a string representing
15725 drawer's name. Otherwise, the user is prompted for a name.
15727 If a region is active, insert the drawer around that region
15730 Point is left between drawer's boundaries."
15732 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
15734 ;; SYSTEM-DRAWERS is a list of drawer names that are used
15735 ;; internally by Org. They are meant to be inserted
15737 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
15738 ;; Remove system drawers from list. Note: For some reason,
15739 ;; `org-completing-read' ignores the predicate while
15740 ;; `completing-read' handles it fine.
15741 (drawer (if arg "PROPERTIES"
15744 "Drawer: " org-drawers
15745 (lambda (d) (not (member d system-drawers))))))))
15747 ;; With C-u, fall back on `org-insert-property-drawer'
15748 (arg (org-insert-property-drawer))
15749 ;; With an active region, insert a drawer at point.
15750 ((not (org-region-active-p))
15752 (unless (bolp) (insert "\n"))
15753 (insert (format ":%s:\n\n:END:\n" drawer))
15754 (forward-line -2)))
15755 ;; Otherwise, insert the drawer at point
15757 (let ((rbeg (region-beginning))
15758 (rend (copy-marker (region-end))))
15762 (beginning-of-line)
15763 (when (save-excursion
15764 (re-search-forward org-outline-regexp-bol rend t))
15765 (user-error "Drawers cannot contain headlines"))
15766 ;; Position point at the beginning of the first
15767 ;; non-blank line in region. Insert drawer's opening
15768 ;; there, then indent it.
15769 (org-skip-whitespace)
15770 (beginning-of-line)
15771 (insert ":" drawer ":\n")
15773 (indent-for-tab-command)
15774 ;; Move point to the beginning of the first blank line
15775 ;; after the last non-blank line in region. Insert
15776 ;; drawer's closing, then indent it.
15778 (skip-chars-backward " \r\t\n")
15780 (deactivate-mark t)
15781 (indent-for-tab-command)
15782 (unless (eolp) (insert "\n")))
15783 ;; Clear marker, whatever the outcome of insertion is.
15784 (set-marker rend nil)))))))
15786 (defvar org-property-set-functions-alist nil
15787 "Property set function alist.
15788 Each entry should have the following format:
15790 (PROPERTY . READ-FUNCTION)
15792 The read function will be called with the same argument as
15793 `org-completing-read'.")
15795 (defun org-set-property-function (property)
15796 "Get the function that should be used to set PROPERTY.
15797 This is computed according to `org-property-set-functions-alist'."
15798 (or (cdr (assoc property org-property-set-functions-alist))
15799 'org-completing-read))
15801 (defun org-read-property-value (property)
15802 "Read PROPERTY value from user."
15803 (let* ((completion-ignore-case t)
15804 (allowed (org-property-get-allowed-values nil property 'table))
15805 (cur (org-entry-get nil property))
15806 (prompt (concat property " value"
15807 (if (and cur (string-match "\\S-" cur))
15808 (concat " [" cur "]") "") ": "))
15809 (set-function (org-set-property-function property))
15811 (funcall set-function prompt allowed nil
15812 (not (get-text-property 0 'org-unrestricted
15814 (let (org-completion-use-ido org-completion-use-iswitchb)
15815 (funcall set-function prompt
15816 (mapcar 'list (org-property-values property))
15817 nil nil "" nil cur)))))
15820 (defvar org-last-set-property nil)
15821 (defvar org-last-set-property-value nil)
15822 (defun org-read-property-name ()
15823 "Read a property name."
15824 (let* ((completion-ignore-case t)
15825 (keys (org-buffer-property-keys nil t t))
15826 (default-prop (or (save-excursion
15828 (beginning-of-line)
15829 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
15830 (null (string= (match-string 1) "END"))
15831 (match-string 1))))
15832 org-last-set-property))
15833 (property (org-icompleting-read
15835 (if default-prop (concat " [" default-prop "]") "")
15837 (mapcar 'list keys)
15840 (if (member property keys)
15842 (or (cdr (assoc (downcase property)
15843 (mapcar (lambda (x) (cons (downcase x) x))
15847 (defun org-set-property-and-value (use-last)
15848 "Allow to set [PROPERTY]: [value] direction from prompt.
15849 When use-default, don't even ask, just use the last
15850 \"[PROPERTY]: [value]\" string from the history."
15852 (let* ((completion-ignore-case t)
15853 (pv (or (and use-last org-last-set-property-value)
15854 (org-completing-read
15855 "Enter a \"[Property]: [value]\" pair: "
15856 nil nil nil nil nil
15857 org-last-set-property-value)))
15859 (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
15860 (setq prop (match-string 1 pv)
15861 val (match-string 2 pv))
15862 (org-set-property prop val))))
15864 (defun org-set-property (property value)
15865 "In the current entry, set PROPERTY to VALUE.
15866 When called interactively, this will prompt for a property name, offering
15867 completion on existing and default properties. And then it will prompt
15868 for a value, offering completion either on allowed values (via an inherited
15869 xxx_ALL property) or on existing values in other instances of this property
15870 in the current file."
15871 (interactive (list nil nil))
15872 (let* ((property (or property (org-read-property-name)))
15873 (value (or value (org-read-property-value property)))
15874 (fn (cdr (assoc property org-properties-postprocess-alist))))
15875 (setq org-last-set-property property)
15876 (setq org-last-set-property-value (concat property ": " value))
15877 ;; Possibly postprocess the inserted value:
15878 (when fn (setq value (funcall fn value)))
15879 (unless (equal (org-entry-get nil property) value)
15880 (org-entry-put nil property value))))
15882 (defun org-delete-property (property &optional delete-empty-drawer)
15883 "In the current entry, delete PROPERTY.
15884 When optional argument DELETE-EMPTY-DRAWER is a string, it defines
15885 an empty drawer to delete."
15887 (let* ((completion-ignore-case t)
15888 (cat (org-entry-get (point) "CATEGORY"))
15889 (props0 (org-entry-properties nil 'standard))
15890 (props (if cat props0
15891 (delete `("CATEGORY" . ,(org-get-category)) props0)))
15892 (prop (if (< 1 (length props))
15893 (org-icompleting-read "Property: " props nil t)
15897 (message "No property to delete in this entry")
15898 (org-entry-delete nil property delete-empty-drawer)
15899 (message "Property \"%s\" deleted" property)))
15901 (defun org-delete-property-globally (property)
15902 "Remove PROPERTY globally, from all entries."
15904 (let* ((completion-ignore-case t)
15905 (prop (org-icompleting-read
15906 "Globally remove property: "
15907 (mapcar 'list (org-buffer-property-keys)))))
15912 (goto-char (point-min))
15914 (while (re-search-forward
15915 (org-re-property property)
15917 (setq cnt (1+ cnt))
15918 (delete-region (match-beginning 0) (1+ (point-at-eol))))
15919 (message "Property \"%s\" removed from %d entries" property cnt)))))
15921 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
15923 (defun org-compute-property-at-point ()
15924 "Compute the property at point.
15925 This looks for an enclosing column format, extracts the operator and
15926 then applies it to the property in the column format's scope."
15928 (unless (org-at-property-p)
15929 (user-error "Not at a property"))
15930 (let ((prop (org-match-string-no-properties 2)))
15931 (org-columns-get-format-and-top-level)
15932 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
15933 (user-error "No operator defined for property %s" prop))
15934 (org-columns-compute prop)))
15936 (defvar org-property-allowed-value-functions nil
15937 "Hook for functions supplying allowed values for a specific property.
15938 The functions must take a single argument, the name of the property, and
15939 return a flat list of allowed values. If \":ETC\" is one of
15940 the values, this means that these values are intended as defaults for
15941 completion, but that other values should be allowed too.
15942 The functions must return nil if they are not responsible for this
15945 (defun org-property-get-allowed-values (pom property &optional table)
15946 "Get allowed values for the property PROPERTY.
15947 When TABLE is non-nil, return an alist that can directly be used for
15951 ((equal property "TODO")
15952 (setq vals (org-with-point-at pom
15953 (append org-todo-keywords-1 '("")))))
15954 ((equal property "PRIORITY")
15955 (let ((n org-lowest-priority))
15956 (while (>= n org-highest-priority)
15957 (push (char-to-string n) vals)
15959 ((member property org-special-properties))
15960 ((setq vals (run-hook-with-args-until-success
15961 'org-property-allowed-value-functions property)))
15963 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
15964 (when (and vals (string-match "\\S-" vals))
15965 (setq vals (car (read-from-string (concat "(" vals ")"))))
15966 (setq vals (mapcar (lambda (x)
15967 (cond ((stringp x) x)
15968 ((numberp x) (number-to-string x))
15969 ((symbolp x) (symbol-name x))
15972 (when (member ":ETC" vals)
15973 (setq vals (remove ":ETC" vals))
15974 (org-add-props (car vals) '(org-unrestricted t)))
15975 (if table (mapcar 'list vals) vals)))
15977 (defun org-property-previous-allowed-value (&optional previous)
15978 "Switch to the next allowed value for this property."
15980 (org-property-next-allowed-value t))
15982 (defun org-property-next-allowed-value (&optional previous)
15983 "Switch to the next allowed value for this property."
15985 (unless (org-at-property-p)
15986 (user-error "Not at a property"))
15987 (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
15988 (key (match-string 2))
15989 (value (match-string 3))
15990 (allowed (or (org-property-get-allowed-values (point) key)
15991 (and (member value '("[ ]" "[-]" "[X]"))
15993 (heading (save-match-data (nth 4 (org-heading-components))))
15996 (user-error "Allowed values for this property have not been defined"))
15997 (if previous (setq allowed (reverse allowed)))
15998 (if (member value allowed)
15999 (setq nval (car (cdr (member value allowed)))))
16000 (setq nval (or nval (car allowed)))
16001 (if (equal nval value)
16002 (user-error "Only one allowed value for this property"))
16003 (org-at-property-p)
16004 (replace-match (concat " :" key ": " nval) t t)
16006 (beginning-of-line 1)
16007 (skip-chars-forward " \t")
16008 (when (equal prop org-effort-property)
16010 (org-back-to-heading t)
16011 (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
16012 (when (string= org-clock-current-task heading)
16013 (setq org-clock-effort nval)
16014 (org-clock-update-mode-line)))
16015 (run-hook-with-args 'org-property-changed-functions key nval)))
16017 (defun org-find-olp (path &optional this-buffer)
16018 "Return a marker pointing to the entry at outline path OLP.
16019 If anything goes wrong, throw an error.
16020 You can wrap this call to catch the error like this:
16022 (condition-case msg
16023 (org-mobile-locate-entry (match-string 4))
16024 (error (nth 1 msg)))
16026 The return value will then be either a string with the error message,
16027 or a marker if everything is OK.
16029 If THIS-BUFFER is set, the outline path does not contain a file,
16031 (let* ((file (if this-buffer buffer-file-name (pop path)))
16032 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
16036 limit re end found pos heading cnt flevel)
16037 (unless buffer (error "File not found :%s" file))
16038 (with-current-buffer buffer
16042 (setq limit (point-max))
16043 (goto-char (point-min))
16044 (dolist (heading path)
16045 (setq re (format org-complex-heading-regexp-format
16046 (regexp-quote heading)))
16047 (setq cnt 0 pos (point))
16048 (while (re-search-forward re end t)
16049 (setq level (- (match-end 1) (match-beginning 1)))
16050 (if (and (>= level lmin) (<= level lmax))
16051 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
16052 (when (= cnt 0) (error "Heading not found on level %d: %s"
16054 (when (> cnt 1) (error "Heading not unique on level %d: %s"
16057 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
16058 (setq end (save-excursion (org-end-of-subtree t t))))
16059 (when (org-at-heading-p)
16060 (point-marker)))))))
16062 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
16063 "Find node HEADING in BUFFER.
16064 Return a marker to the heading if it was found, or nil if not.
16065 If POS-ONLY is set, return just the position instead of a marker.
16067 The heading text must match exact, but it may have a TODO keyword,
16068 a priority cookie and tags in the standard locations."
16069 (with-current-buffer (or buffer (current-buffer))
16073 (goto-char (point-min))
16074 (let (case-fold-search)
16075 (if (re-search-forward
16076 (format org-complex-heading-regexp-format
16077 (regexp-quote heading)) nil t)
16079 (match-beginning 0)
16080 (move-marker (make-marker) (match-beginning 0)))))))))
16082 (defun org-find-exact-heading-in-directory (heading &optional dir)
16083 "Find Org node headline HEADING in all .org files in directory DIR.
16084 When the target headline is found, return a marker to this location."
16085 (let ((files (directory-files (or dir default-directory)
16086 nil "\\`[^.#].*\\.org\\'"))
16087 file visiting m buffer)
16089 (dolist (file files)
16090 (message "trying %s" file)
16091 (setq visiting (org-find-base-buffer-visiting file))
16092 (setq buffer (or visiting (find-file-noselect file)))
16093 (setq m (org-find-exact-headline-in-buffer
16095 (when (and (not m) (not visiting)) (kill-buffer buffer))
16096 (and m (throw 'found m))))))
16098 (defun org-find-entry-with-id (ident)
16099 "Locate the entry that contains the ID property with exact value IDENT.
16100 IDENT can be a string, a symbol or a number, this function will search for
16101 the string representation of it.
16102 Return the position where this entry starts, or nil if there is no such entry."
16103 (interactive "sID: ")
16105 ((stringp ident) ident)
16106 ((symbol-name ident) (symbol-name ident))
16107 ((numberp ident) (number-to-string ident))
16108 (t (error "IDENT %s must be a string, symbol or number" ident))))
16109 (case-fold-search nil))
16113 (goto-char (point-min))
16114 (when (re-search-forward
16115 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
16117 (org-back-to-heading t)
16122 (defvar org-last-changed-timestamp nil)
16123 (defvar org-last-inserted-timestamp nil
16124 "The last time stamp inserted with `org-insert-time-stamp'.")
16125 (defvar org-ts-what) ; dynamically scoped parameter
16127 (defun org-time-stamp (arg &optional inactive)
16128 "Prompt for a date/time and insert a time stamp.
16129 If the user specifies a time like HH:MM or if this command is
16130 called with at least one prefix argument, the time stamp contains
16131 the date and the time. Otherwise, only the date is be included.
16133 All parts of a date not specified by the user is filled in from
16134 the current date/time. So if you just press return without
16135 typing anything, the time stamp will represent the current
16138 If there is already a timestamp at the cursor, it will be
16141 With two universal prefix arguments, insert an active timestamp
16142 with the current time without prompting the user.
16144 When called from lisp, the timestamp is inactive if INACTIVE is
16149 ;; Default time is either today, or, when entering a range,
16150 ;; the range start.
16151 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
16153 (re-search-backward
16154 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
16155 (- (point) 20) t)))
16156 (apply 'encode-time (org-parse-time-string (match-string 1)))
16158 (default-input (and ts (org-get-compact-tod ts)))
16159 (repeater (save-excursion
16161 (beginning-of-line)
16162 (when (re-search-forward
16163 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
16164 (save-excursion (progn (end-of-line) (point))) t)
16165 (match-string 0)))))
16166 org-time-was-given org-end-time-was-given time)
16168 ((and (org-at-timestamp-p t)
16169 (memq last-command '(org-time-stamp org-time-stamp-inactive))
16170 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
16172 (setq time (let ((this-command this-command))
16173 (org-read-date arg 'totime nil nil
16174 default-time default-input inactive)))
16175 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
16176 ((org-at-timestamp-p t)
16177 (setq time (let ((this-command this-command))
16178 (org-read-date arg 'totime nil nil default-time default-input inactive)))
16179 (when (org-at-timestamp-p t) ; just to get the match data
16180 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
16182 (setq org-last-changed-timestamp
16183 (org-insert-time-stamp
16184 time (or org-time-was-given arg)
16185 inactive nil nil (list org-end-time-was-given)))
16186 (when repeater (goto-char (1- (point))) (insert " " repeater)
16187 (setq org-last-changed-timestamp
16188 (concat (substring org-last-inserted-timestamp 0 -1)
16189 " " repeater ">"))))
16190 (message "Timestamp updated"))
16192 (org-insert-time-stamp (current-time) t inactive))
16194 (setq time (let ((this-command this-command))
16195 (org-read-date arg 'totime nil nil default-time default-input inactive)))
16196 (org-insert-time-stamp time (or org-time-was-given arg) inactive
16197 nil nil (list org-end-time-was-given))))))
16199 ;; FIXME: can we use this for something else, like computing time differences?
16200 (defun org-get-compact-tod (s)
16201 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
16202 (let* ((t1 (match-string 1 s))
16203 (h1 (string-to-number (match-string 2 s)))
16204 (m1 (string-to-number (match-string 3 s)))
16205 (t2 (and (match-end 4) (match-string 5 s)))
16206 (h2 (and t2 (string-to-number (match-string 6 s))))
16207 (m2 (and t2 (string-to-number (match-string 7 s))))
16211 (setq dh (- h2 h1) dm (- m2 m1))
16212 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
16213 (concat t1 "+" (number-to-string dh)
16214 (and (/= 0 dm) (format ":%02d" dm)))))))
16216 (defun org-time-stamp-inactive (&optional arg)
16217 "Insert an inactive time stamp.
16218 An inactive time stamp is enclosed in square brackets instead of angle
16219 brackets. It is inactive in the sense that it does not trigger agenda entries,
16220 does not link to the calendar and cannot be changed with the S-cursor keys.
16221 So these are more for recording a certain time/date."
16223 (org-time-stamp arg 'inactive))
16225 (defvar org-date-ovl (make-overlay 1 1))
16226 (overlay-put org-date-ovl 'face 'org-date-selected)
16227 (org-detach-overlay org-date-ovl)
16229 (defvar org-ans1) ; dynamically scoped parameter
16230 (defvar org-ans2) ; dynamically scoped parameter
16232 (defvar org-plain-time-of-day-regexp) ; defined below
16234 (defvar org-overriding-default-time nil) ; dynamically scoped
16235 (defvar org-read-date-overlay nil)
16236 (defvar org-dcst nil) ; dynamically scoped
16237 (defvar org-read-date-history nil)
16238 (defvar org-read-date-final-answer nil)
16239 (defvar org-read-date-analyze-futurep nil)
16240 (defvar org-read-date-analyze-forced-year nil)
16241 (defvar org-read-date-inactive)
16243 (defvar org-read-date-minibuffer-local-map
16244 (let* ((org-replace-disputed-keys nil)
16245 (map (make-sparse-keymap)))
16246 (set-keymap-parent map minibuffer-local-map)
16247 (org-defkey map (kbd ".")
16248 (lambda () (interactive)
16249 ;; Are we at the beginning of the prompt?
16250 (if (looking-back "^[^:]+: ")
16251 (org-eval-in-calendar '(calendar-goto-today))
16253 (org-defkey map (kbd "C-.")
16254 (lambda () (interactive)
16255 (org-eval-in-calendar '(calendar-goto-today))))
16256 (org-defkey map [(meta shift left)]
16257 (lambda () (interactive)
16258 (org-eval-in-calendar '(calendar-backward-month 1))))
16259 (org-defkey map [(meta shift right)]
16260 (lambda () (interactive)
16261 (org-eval-in-calendar '(calendar-forward-month 1))))
16262 (org-defkey map [(meta shift up)]
16263 (lambda () (interactive)
16264 (org-eval-in-calendar '(calendar-backward-year 1))))
16265 (org-defkey map [(meta shift down)]
16266 (lambda () (interactive)
16267 (org-eval-in-calendar '(calendar-forward-year 1))))
16268 (org-defkey map [?\e (shift left)]
16269 (lambda () (interactive)
16270 (org-eval-in-calendar '(calendar-backward-month 1))))
16271 (org-defkey map [?\e (shift right)]
16272 (lambda () (interactive)
16273 (org-eval-in-calendar '(calendar-forward-month 1))))
16274 (org-defkey map [?\e (shift up)]
16275 (lambda () (interactive)
16276 (org-eval-in-calendar '(calendar-backward-year 1))))
16277 (org-defkey map [?\e (shift down)]
16278 (lambda () (interactive)
16279 (org-eval-in-calendar '(calendar-forward-year 1))))
16280 (org-defkey map [(shift up)]
16281 (lambda () (interactive)
16282 (org-eval-in-calendar '(calendar-backward-week 1))))
16283 (org-defkey map [(shift down)]
16284 (lambda () (interactive)
16285 (org-eval-in-calendar '(calendar-forward-week 1))))
16286 (org-defkey map [(shift left)]
16287 (lambda () (interactive)
16288 (org-eval-in-calendar '(calendar-backward-day 1))))
16289 (org-defkey map [(shift right)]
16290 (lambda () (interactive)
16291 (org-eval-in-calendar '(calendar-forward-day 1))))
16292 (org-defkey map "!"
16293 (lambda () (interactive)
16294 (org-eval-in-calendar '(diary-view-entries))
16296 (org-defkey map ">"
16297 (lambda () (interactive)
16298 (org-eval-in-calendar '(scroll-calendar-left 1))))
16299 (org-defkey map "<"
16300 (lambda () (interactive)
16301 (org-eval-in-calendar '(scroll-calendar-right 1))))
16302 (org-defkey map "\C-v"
16303 (lambda () (interactive)
16304 (org-eval-in-calendar
16305 '(calendar-scroll-left-three-months 1))))
16306 (org-defkey map "\M-v"
16307 (lambda () (interactive)
16308 (org-eval-in-calendar
16309 '(calendar-scroll-right-three-months 1))))
16311 "Keymap for minibuffer commands when using `org-read-date'.")
16314 (defvar org-defdecode)
16315 (defvar org-with-time)
16317 (defun org-read-date (&optional org-with-time to-time from-string prompt
16318 default-time default-input inactive)
16319 "Read a date, possibly a time, and make things smooth for the user.
16320 The prompt will suggest to enter an ISO date, but you can also enter anything
16321 which will at least partially be understood by `parse-time-string'.
16322 Unrecognized parts of the date will default to the current day, month, year,
16323 hour and minute. If this command is called to replace a timestamp at point,
16324 or to enter the second timestamp of a range, the default time is taken
16325 from the existing stamp. Furthermore, the command prefers the future,
16326 so if you are giving a date where the year is not given, and the day-month
16327 combination is already past in the current year, it will assume you
16328 mean next year. For details, see the manual. A few examples:
16330 3-2-5 --> 2003-02-05
16331 feb 15 --> currentyear-02-15
16332 2/15 --> currentyear-02-15
16333 sep 12 9 --> 2009-09-12
16334 12:45 --> today 12:45
16335 22 sept 0:34 --> currentyear-09-22 0:34
16336 12 --> currentyear-currentmonth-12
16337 Fri --> nearest Friday after today
16338 -Tue --> last Tuesday
16341 Furthermore you can specify a relative date by giving, as the *first* thing
16342 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
16343 change in days weeks, months, years.
16344 With a single plus or minus, the date is relative to today. With a double
16345 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
16346 +4d --> four days from today
16347 +4 --> same as above
16348 +2w --> two weeks from today
16349 ++5 --> five days from default date
16351 The function understands only English month and weekday abbreviations.
16353 While prompting, a calendar is popped up - you can also select the
16354 date with the mouse (button 1). The calendar shows a period of three
16355 months. To scroll it to other months, use the keys `>' and `<'.
16356 If you don't like the calendar, turn it off with
16357 (setq org-read-date-popup-calendar nil)
16359 With optional argument TO-TIME, the date will immediately be converted
16360 to an internal time.
16361 With an optional argument ORG-WITH-TIME, the prompt will suggest to
16362 also insert a time. Note that when ORG-WITH-TIME is not set, you can
16363 still enter a time, and this function will inform the calling routine
16364 about this change. The calling routine may then choose to change the
16365 format used to insert the time stamp into the buffer to include the time.
16366 With optional argument FROM-STRING, read from this string instead from
16367 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
16368 the time/date that is used for everything that is not specified by the
16370 (require 'parse-time)
16371 (let* ((org-time-stamp-rounding-minutes
16372 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
16373 (org-dcst org-display-custom-times)
16374 (ct (org-current-time))
16375 (org-def (or org-overriding-default-time default-time ct))
16376 (org-defdecode (decode-time org-def))
16378 (when (< (nth 2 org-defdecode) org-extend-today-until)
16379 (setcar (nthcdr 2 org-defdecode) -1)
16380 (setcar (nthcdr 1 org-defdecode) 59)
16381 (setq org-def (apply 'encode-time org-defdecode)
16382 org-defdecode (decode-time org-def)))))
16383 (mouse-autoselect-window nil) ; Don't let the mouse jump
16384 (calendar-frame-setup nil)
16385 (calendar-setup nil)
16386 (calendar-move-hook nil)
16387 (calendar-view-diary-initially-flag nil)
16388 (calendar-view-holidays-initially-flag nil)
16389 (timestr (format-time-string
16390 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
16391 (prompt (concat (if prompt (concat prompt " ") "")
16392 (format "Date+time [%s]: " timestr)))
16393 ans (org-ans0 "") org-ans1 org-ans2 final)
16396 (from-string (setq ans from-string))
16397 (org-read-date-popup-calendar
16399 (save-window-excursion
16401 (org-eval-in-calendar '(setq cursor-type nil) t)
16404 (calendar-forward-day (- (time-to-days org-def)
16405 (calendar-absolute-from-gregorian
16406 (calendar-current-date))))
16407 (org-eval-in-calendar nil t)
16408 (let* ((old-map (current-local-map))
16409 (map (copy-keymap calendar-mode-map))
16410 (minibuffer-local-map
16411 (copy-keymap org-read-date-minibuffer-local-map)))
16412 (org-defkey map (kbd "RET") 'org-calendar-select)
16413 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
16414 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
16417 (use-local-map map)
16418 (setq org-read-date-inactive inactive)
16419 (add-hook 'post-command-hook 'org-read-date-display)
16420 (setq org-ans0 (read-string prompt default-input
16421 'org-read-date-history nil))
16422 ;; org-ans0: from prompt
16423 ;; org-ans1: from mouse click
16424 ;; org-ans2: from calendar motion
16425 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
16426 (remove-hook 'post-command-hook 'org-read-date-display)
16427 (use-local-map old-map)
16428 (when org-read-date-overlay
16429 (delete-overlay org-read-date-overlay)
16430 (setq org-read-date-overlay nil)))))
16431 (bury-buffer "*Calendar*")))))
16433 (t ; Naked prompt only
16435 (setq ans (read-string prompt default-input
16436 'org-read-date-history timestr))
16437 (when org-read-date-overlay
16438 (delete-overlay org-read-date-overlay)
16439 (setq org-read-date-overlay nil)))))
16441 (setq final (org-read-date-analyze ans org-def org-defdecode))
16443 (when org-read-date-analyze-forced-year
16444 (message "Year was forced into %s"
16445 (if org-read-date-force-compatible-dates
16446 "compatible range (1970-2037)"
16447 "range representable on this machine"))
16450 ;; One round trip to get rid of 34th of August and stuff like that....
16451 (setq final (decode-time (apply 'encode-time final)))
16453 (setq org-read-date-final-answer ans)
16456 (apply 'encode-time final)
16457 (if (and (boundp 'org-time-was-given) org-time-was-given)
16458 (format "%04d-%02d-%02d %02d:%02d"
16459 (nth 5 final) (nth 4 final) (nth 3 final)
16460 (nth 2 final) (nth 1 final))
16461 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
16463 (defun org-read-date-display ()
16464 "Display the current date prompt interpretation in the minibuffer."
16465 (when org-read-date-display-live
16466 (when org-read-date-overlay
16467 (delete-overlay org-read-date-overlay))
16468 (when (minibufferp (current-buffer))
16471 (while (not (equal (buffer-substring
16472 (max (point-min) (- (point) 4)) (point))
16475 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
16476 " " (or org-ans1 org-ans2)))
16477 (org-end-time-was-given nil)
16478 (f (org-read-date-analyze ans org-def org-defdecode))
16480 org-time-stamp-custom-formats
16481 org-time-stamp-formats))
16482 (fmt (if (or org-with-time
16483 (and (boundp 'org-time-was-given) org-time-was-given))
16486 (txt (format-time-string fmt (apply 'encode-time f)))
16487 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
16488 (txt (concat "=> " txt)))
16489 (when (and org-end-time-was-given
16490 (string-match org-plain-time-of-day-regexp txt))
16491 (setq txt (concat (substring txt 0 (match-end 0)) "-"
16492 org-end-time-was-given
16493 (substring txt (match-end 0)))))
16494 (when org-read-date-analyze-futurep
16495 (setq txt (concat txt " (=>F)")))
16496 (setq org-read-date-overlay
16497 (make-overlay (1- (point-at-eol)) (point-at-eol)))
16498 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
16500 (defun org-read-date-analyze (ans org-def org-defdecode)
16501 "Analyze the combined answer of the date prompt."
16502 ;; FIXME: cleanup and comment
16503 (let ((nowdecode (decode-time))
16504 delta deltan deltaw deltadef year month day
16505 hour minute second wday pm h2 m2 tl wday1
16506 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
16507 (setq org-read-date-analyze-futurep nil
16508 org-read-date-analyze-forced-year nil)
16509 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
16512 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
16513 (setq ans (replace-match "" t t ans)
16515 deltaw (nth 1 delta)
16516 deltadef (nth 2 delta)))
16518 ;; Check if there is an iso week date in there. If yes, store the
16519 ;; info and postpone interpreting it until the rest of the parsing
16521 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
16522 (setq iso-year (if (match-end 1)
16523 (org-small-year-to-year
16524 (string-to-number (match-string 1 ans))))
16525 iso-weekday (if (match-end 3)
16526 (string-to-number (match-string 3 ans)))
16527 iso-week (string-to-number (match-string 2 ans)))
16528 (setq ans (replace-match "" t t ans)))
16530 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
16531 (when (string-match
16532 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
16533 (setq year (if (match-end 2)
16534 (string-to-number (match-string 2 ans))
16535 (progn (setq kill-year t)
16536 (string-to-number (format-time-string "%Y"))))
16537 month (string-to-number (match-string 3 ans))
16538 day (string-to-number (match-string 4 ans)))
16539 (if (< year 100) (setq year (+ 2000 year)))
16540 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
16543 ;; Help matching dotted european dates
16544 (when (string-match
16545 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
16546 (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
16548 (string-to-number (format-time-string "%Y")))
16549 day (string-to-number (match-string 1 ans))
16550 month (string-to-number (match-string 2 ans))
16551 ans (replace-match (format "%04d-%02d-%02d" year month day)
16554 ;; Help matching american dates, like 5/30 or 5/30/7
16555 (when (string-match
16556 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
16557 (setq year (if (match-end 4)
16558 (string-to-number (match-string 4 ans))
16559 (progn (setq kill-year t)
16560 (string-to-number (format-time-string "%Y"))))
16561 month (string-to-number (match-string 1 ans))
16562 day (string-to-number (match-string 2 ans)))
16563 (if (< year 100) (setq year (+ 2000 year)))
16564 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
16566 ;; Help matching am/pm times, because `parse-time-string' does not do that.
16567 ;; If there is a time with am/pm, and *no* time without it, we convert
16568 ;; so that matching will be successful.
16569 (loop for i from 1 to 2 do ; twice, for end time as well
16570 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
16571 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
16572 (setq hour (string-to-number (match-string 1 ans))
16573 minute (if (match-end 3)
16574 (string-to-number (match-string 3 ans))
16577 (string-to-char (downcase (match-string 4 ans)))))
16578 (if (and (= hour 12) (not pm))
16580 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
16581 (setq ans (replace-match (format "%02d:%02d" hour minute)
16584 ;; Check if a time range is given as a duration
16585 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
16586 (setq hour (string-to-number (match-string 1 ans))
16587 h2 (+ hour (string-to-number (match-string 3 ans)))
16588 minute (string-to-number (match-string 2 ans))
16589 m2 (+ minute (if (match-end 5) (string-to-number
16590 (match-string 5 ans))0)))
16591 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
16592 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
16595 ;; Check if there is a time range
16596 (when (boundp 'org-end-time-was-given)
16597 (setq org-time-was-given nil)
16598 (when (and (string-match org-plain-time-of-day-regexp ans)
16600 (setq org-end-time-was-given (match-string 8 ans))
16601 (setq ans (concat (substring ans 0 (match-beginning 7))
16602 (substring ans (match-end 7))))))
16604 (setq tl (parse-time-string ans)
16605 day (or (nth 3 tl) (nth 3 org-defdecode))
16606 month (or (nth 4 tl)
16607 (if (and org-read-date-prefer-future
16608 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
16609 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
16610 (nth 4 org-defdecode)))
16611 year (or (and (not kill-year) (nth 5 tl))
16612 (if (and org-read-date-prefer-future
16613 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
16614 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
16615 (nth 5 org-defdecode)))
16616 hour (or (nth 2 tl) (nth 2 org-defdecode))
16617 minute (or (nth 1 tl) (nth 1 org-defdecode))
16618 second (or (nth 0 tl) 0)
16621 (when (and (eq org-read-date-prefer-future 'time)
16622 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
16623 (equal day (nth 3 nowdecode))
16624 (equal month (nth 4 nowdecode))
16625 (equal year (nth 5 nowdecode))
16627 (or (< (nth 2 tl) (nth 2 nowdecode))
16628 (and (= (nth 2 tl) (nth 2 nowdecode))
16630 (< (nth 1 tl) (nth 1 nowdecode)))))
16634 ;; Special date definitions below
16637 ;; There was an iso week
16640 (setq year (or iso-year year)
16641 day (or iso-weekday wday 1)
16642 wday nil ; to make sure that the trigger below does not match
16643 iso-date (calendar-gregorian-from-absolute
16644 (calendar-absolute-from-iso
16645 (list iso-week day year))))
16646 ; FIXME: Should we also push ISO weeks into the future?
16647 ; (when (and org-read-date-prefer-future
16649 ; (< (calendar-absolute-from-gregorian iso-date)
16650 ; (time-to-days (current-time))))
16651 ; (setq year (1+ year)
16652 ; iso-date (calendar-gregorian-from-absolute
16653 ; (calendar-absolute-from-iso
16654 ; (list iso-week day year)))))
16655 (setq month (car iso-date)
16656 year (nth 2 iso-date)
16657 day (nth 1 iso-date)))
16661 (let ((now (decode-time)))
16662 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
16663 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
16664 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
16665 ((equal deltaw "m") (setq month (+ month deltan)))
16666 ((equal deltaw "y") (setq year (+ year deltan)))))
16667 ((and wday (not (nth 3 tl)))
16668 ;; Weekday was given, but no day, so pick that day in the week
16669 ;; on or after the derived date.
16670 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
16671 (unless (equal wday wday1)
16672 (setq day (+ day (% (- wday wday1 -7) 7))))))
16673 (if (and (boundp 'org-time-was-given)
16675 (setq org-time-was-given t))
16676 (if (< year 100) (setq year (+ 2000 year)))
16677 ;; Check of the date is representable
16678 (if org-read-date-force-compatible-dates
16681 (setq year 1970 org-read-date-analyze-forced-year t))
16683 (setq year 2037 org-read-date-analyze-forced-year t)))
16684 (condition-case nil
16685 (ignore (encode-time second minute hour day month year))
16687 (setq year (nth 5 org-defdecode))
16688 (setq org-read-date-analyze-forced-year t))))
16689 (setq org-read-date-analyze-futurep futurep)
16690 (list second minute hour day month year)))
16692 (defvar parse-time-weekdays)
16693 (defun org-read-date-get-relative (s today default)
16694 "Check string S for special relative date string.
16695 TODAY and DEFAULT are internal times, for today and for a default.
16696 Return shift list (N what def-flag)
16697 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
16698 N is the number of WHATs to shift.
16699 DEF-FLAG is t when a double ++ or -- indicates shift relative to
16700 the DEFAULT date rather than TODAY."
16701 (require 'parse-time)
16705 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
16707 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
16708 "\\([ \t]\\|$\\)") s)
16709 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
16710 (let* ((dir (if (> (match-end 1) (match-beginning 1))
16711 (string-to-char (substring (match-string 1 s) -1))
16713 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
16714 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
16715 (what (if (match-end 3) (match-string 3 s) "d"))
16716 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
16717 (date (if rel default today))
16718 (wday (nth 6 (decode-time date)))
16722 (setq delta (mod (+ 7 (- wday1 wday)) 7))
16723 (if (= delta 0) (setq delta 7))
16726 (setq delta (- delta 7))
16727 (if (= delta 0) (setq delta -7))))
16728 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
16729 (list delta "d" rel))
16730 (list (* n (if (= dir ?-) -1 1)) what rel)))))
16732 (defun org-order-calendar-date-args (arg1 arg2 arg3)
16733 "Turn a user-specified date into the internal representation.
16734 The internal representation needed by the calendar is (month day year).
16735 This is a wrapper to handle the brain-dead convention in calendar that
16736 user function argument order change dependent on argument order."
16737 (if (boundp 'calendar-date-style)
16739 ((eq calendar-date-style 'american)
16740 (list arg1 arg2 arg3))
16741 ((eq calendar-date-style 'european)
16742 (list arg2 arg1 arg3))
16743 ((eq calendar-date-style 'iso)
16744 (list arg2 arg3 arg1)))
16745 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
16746 (if (org-bound-and-true-p european-calendar-style)
16747 (list arg2 arg1 arg3)
16748 (list arg1 arg2 arg3)))))
16750 (defun org-eval-in-calendar (form &optional keepdate)
16751 "Eval FORM in the calendar window and return to current window.
16752 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
16753 otherwise stick to the current value of `org-ans2'."
16754 (let ((sf (selected-frame))
16755 (sw (selected-window)))
16756 (select-window (get-buffer-window "*Calendar*" t))
16758 (when (and (not keepdate) (calendar-cursor-to-date))
16759 (let* ((date (calendar-cursor-to-date))
16760 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16761 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
16762 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
16764 (org-select-frame-set-input-focus sf)))
16766 (defun org-calendar-select ()
16767 "Return to `org-read-date' with the date currently selected.
16768 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16770 (when (calendar-cursor-to-date)
16771 (let* ((date (calendar-cursor-to-date))
16772 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16773 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
16774 (if (active-minibuffer-window) (exit-minibuffer))))
16776 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
16777 "Insert a date stamp for the date given by the internal TIME.
16778 WITH-HM means use the stamp format that includes the time of the day.
16779 INACTIVE means use square brackets instead of angular ones, so that the
16780 stamp will not contribute to the agenda.
16781 PRE and POST are optional strings to be inserted before and after the
16783 The command returns the inserted time stamp."
16784 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
16786 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
16787 (insert-before-markers (or pre ""))
16788 (when (listp extra)
16789 (setq extra (car extra))
16790 (if (and (stringp extra)
16791 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
16792 (setq extra (format "-%02d:%02d"
16793 (string-to-number (match-string 1 extra))
16794 (string-to-number (match-string 2 extra))))
16797 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
16798 (insert-before-markers (setq stamp (format-time-string fmt time)))
16799 (insert-before-markers (or post ""))
16800 (setq org-last-inserted-timestamp stamp)))
16802 (defun org-toggle-time-stamp-overlays ()
16803 "Toggle the use of custom time stamp formats."
16805 (setq org-display-custom-times (not org-display-custom-times))
16806 (unless org-display-custom-times
16807 (let ((p (point-min)) (bmp (buffer-modified-p)))
16808 (while (setq p (next-single-property-change p 'display))
16809 (if (and (get-text-property p 'display)
16810 (eq (get-text-property p 'face) 'org-date))
16811 (remove-text-properties
16812 p (setq p (next-single-property-change p 'display))
16814 (set-buffer-modified-p bmp)))
16815 (if (featurep 'xemacs)
16816 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
16817 (org-restart-font-lock)
16818 (setq org-table-may-need-update t)
16819 (if org-display-custom-times
16820 (message "Time stamps are overlaid with custom format")
16821 (message "Time stamp overlays removed")))
16823 (defun org-display-custom-time (beg end)
16824 "Overlay modified time stamp format over timestamp between BEG and END."
16825 (let* ((ts (buffer-substring beg end))
16826 t1 w1 with-hm tf time str w2 (off 0))
16828 (setq t1 (org-parse-time-string ts t))
16829 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
16830 (setq off (- (match-end 0) (match-beginning 0)))))
16831 (setq end (- end off))
16832 (setq w1 (- end beg)
16833 with-hm (and (nth 1 t1) (nth 2 t1))
16834 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
16835 time (org-fix-decoded-time t1)
16837 (format-time-string
16838 (substring tf 1 -1) (apply 'encode-time time))
16839 nil 'mouse-face 'highlight)
16841 (if (not (= w2 w1))
16842 (add-text-properties (1+ beg) (+ 2 beg)
16843 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
16844 (if (featurep 'xemacs)
16846 (put-text-property beg end 'invisible t)
16847 (put-text-property beg end 'end-glyph (make-glyph str)))
16848 (put-text-property beg end 'display str))))
16850 (defun org-translate-time (string)
16851 "Translate all timestamps in STRING to custom format.
16852 But do this only if the variable `org-display-custom-times' is set."
16853 (when org-display-custom-times
16856 (re org-ts-regexp-both)
16857 t1 with-hm inactive tf time str beg end)
16858 (while (setq start (string-match re string start))
16859 (setq beg (match-beginning 0)
16861 t1 (save-match-data
16862 (org-parse-time-string (substring string beg end) t))
16863 with-hm (and (nth 1 t1) (nth 2 t1))
16864 inactive (equal (substring string beg (1+ beg)) "[")
16865 tf (funcall (if with-hm 'cdr 'car)
16866 org-time-stamp-custom-formats)
16867 time (org-fix-decoded-time t1)
16868 str (format-time-string
16870 (if inactive "[" "<") (substring tf 1 -1)
16871 (if inactive "]" ">"))
16872 (apply 'encode-time time))
16873 string (replace-match str t t string)
16874 start (+ start (length str)))))))
16877 (defun org-fix-decoded-time (time)
16878 "Set 0 instead of nil for the first 6 elements of time.
16879 Don't touch the rest."
16881 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
16883 (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
16885 (defun org-time-stamp-to-now (timestamp-string &optional seconds)
16886 "Difference between TIMESTAMP-STRING and now in days.
16887 If SECONDS is non-nil, return the difference in seconds."
16888 (let ((fdiff (if seconds 'float-time 'time-to-days)))
16889 (- (funcall fdiff (org-time-string-to-time timestamp-string))
16890 (funcall fdiff (current-time)))))
16892 (defun org-deadline-close (timestamp-string &optional ndays)
16893 "Is the time in TIMESTAMP-STRING close to the current date?"
16894 (setq ndays (or ndays (org-get-wdays timestamp-string)))
16895 (and (< (org-time-stamp-to-now timestamp-string) ndays)
16896 (not (org-entry-is-done-p))))
16898 (defun org-get-wdays (ts &optional delay zero-delay)
16899 "Get the deadline lead time appropriate for timestring TS.
16900 When DELAY is non-nil, get the delay time for scheduled items
16901 instead of the deadline lead time. When ZERO-DELAY is non-nil
16902 and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
16903 don't try to find the delay cookie in the scheduled timestamp."
16904 (let ((tv (if delay org-scheduled-delay-days
16905 org-deadline-warning-days)))
16907 ((or (and delay (< tv 0))
16908 (and delay zero-delay (<= tv 0))
16909 (and (not delay) (<= tv 0)))
16910 ;; Enforce this value no matter what
16912 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
16913 ;; lead time is specified.
16914 (floor (* (string-to-number (match-string 1 ts))
16915 (cdr (assoc (match-string 2 ts)
16916 '(("d" . 1) ("w" . 7)
16917 ("m" . 30.4) ("y" . 365.25)
16918 ("h" . 0.041667)))))))
16919 ;; go for the default.
16922 (defun org-calendar-select-mouse (ev)
16923 "Return to `org-read-date' with the date currently selected.
16924 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16926 (mouse-set-point ev)
16927 (when (calendar-cursor-to-date)
16928 (let* ((date (calendar-cursor-to-date))
16929 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16930 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
16931 (if (active-minibuffer-window) (exit-minibuffer))))
16933 (defun org-check-deadlines (ndays)
16934 "Check if there are any deadlines due or past due.
16935 A deadline is considered due if it happens within `org-deadline-warning-days'
16936 days from today's date. If the deadline appears in an entry marked DONE,
16937 it is not shown. The prefix arg NDAYS can be used to test that many
16938 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
16940 (let* ((org-warn-days
16942 ((equal ndays '(4)) 100000)
16943 (ndays (prefix-numeric-value ndays))
16944 (t (abs org-deadline-warning-days))))
16945 (case-fold-search nil)
16946 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
16948 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
16950 (message "%d deadlines past-due or due within %d days"
16951 (org-occur regexp nil callback)
16954 (defsubst org-re-timestamp (type)
16955 "Return a regexp for timestamp TYPE.
16956 Allowed values for TYPE are:
16958 all: all timestamps
16959 active: only active timestamps (<...>)
16960 inactive: only inactive timestamps ([...])
16961 scheduled: only scheduled timestamps
16962 deadline: only deadline timestamps
16963 closed: only closed time-stamps
16965 When TYPE is nil, fall back on returning a regexp that matches
16966 both scheduled and deadline timestamps."
16967 (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9>\r\n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
16968 ((eq type 'active) org-ts-regexp)
16969 ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]")
16970 ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
16971 ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
16972 ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"))
16973 ((eq type 'scheduled-or-deadline)
16974 (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
16976 (defun org-check-before-date (date)
16977 "Check if there are deadlines or scheduled entries before DATE."
16978 (interactive (list (org-read-date)))
16979 (let ((case-fold-search nil)
16980 (regexp (org-re-timestamp org-ts-type))
16982 (lambda () (time-less-p
16983 (org-time-string-to-time (match-string 1))
16984 (org-time-string-to-time date)))))
16985 (message "%d entries before %s"
16986 (org-occur regexp nil callback) date)))
16988 (defun org-check-after-date (date)
16989 "Check if there are deadlines or scheduled entries after DATE."
16990 (interactive (list (org-read-date)))
16991 (let ((case-fold-search nil)
16992 (regexp (org-re-timestamp org-ts-type))
16996 (org-time-string-to-time (match-string 1))
16997 (org-time-string-to-time date))))))
16998 (message "%d entries after %s"
16999 (org-occur regexp nil callback) date)))
17001 (defun org-check-dates-range (start-date end-date)
17002 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
17003 (interactive (list (org-read-date nil nil nil "Range starts")
17004 (org-read-date nil nil nil "Range end")))
17005 (let ((case-fold-search nil)
17006 (regexp (org-re-timestamp org-ts-type))
17009 (let ((match (match-string 1)))
17012 (org-time-string-to-time match)
17013 (org-time-string-to-time start-date)))
17015 (org-time-string-to-time match)
17016 (org-time-string-to-time end-date)))))))
17017 (message "%d entries between %s and %s"
17018 (org-occur regexp nil callback) start-date end-date)))
17020 (defun org-evaluate-time-range (&optional to-buffer)
17021 "Evaluate a time range by computing the difference between start and end.
17022 Normally the result is just printed in the echo area, but with prefix arg
17023 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
17024 If the time range is actually in a table, the result is inserted into the
17026 For time difference computation, a year is assumed to be exactly 365
17027 days in order to avoid rounding problems."
17030 (org-clock-update-time-maybe)
17032 (unless (org-at-date-range-p t)
17033 (goto-char (point-at-bol))
17034 (re-search-forward org-tr-regexp-both (point-at-eol) t))
17035 (if (not (org-at-date-range-p t))
17036 (user-error "Not at a time-stamp range, and none found in current line")))
17037 (let* ((ts1 (match-string 1))
17038 (ts2 (match-string 2))
17039 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
17040 (match-end (match-end 0))
17041 (time1 (org-time-string-to-time ts1))
17042 (time2 (org-time-string-to-time ts2))
17043 (t1 (float-time time1))
17044 (t2 (float-time time2))
17045 (diff (abs (- t2 t1)))
17046 (negative (< (- t2 t1) 0))
17047 ;; (ys (floor (* 365 24 60 60)))
17050 (fy "%dy %dd %02d:%02d")
17052 (fd "%dd %02d:%02d")
17057 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
17059 d (floor (/ diff ds)) diff (mod diff ds)
17060 h (floor (/ diff hs)) diff (mod diff hs)
17061 m (floor (/ diff 60)))
17062 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
17064 d (floor (+ (/ diff ds) 0.5))
17066 (if (not to-buffer)
17067 (message "%s" (org-make-tdiff-string y d h m))
17068 (if (org-at-table-p)
17070 (goto-char match-end)
17072 (and (looking-at " *|") (goto-char (match-end 0))))
17073 (goto-char match-end))
17075 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
17076 (replace-match ""))
17077 (if negative (insert " -"))
17078 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
17079 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
17080 (insert " " (format fh h m))))
17081 (if align (org-table-align))
17082 (message "Time difference inserted")))))
17084 (defun org-make-tdiff-string (y d h m)
17087 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
17089 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
17091 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
17093 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
17095 (apply 'format fmt (nreverse l))))
17097 (defun org-time-string-to-time (s &optional buffer pos)
17098 "Convert a timestamp string into internal time."
17099 (condition-case errdata
17100 (apply 'encode-time (org-parse-time-string s))
17101 (error (error "Bad timestamp `%s'%s\nError was: %s"
17102 s (if (not (and buffer pos))
17104 (format-message " at %d in buffer `%s'" pos buffer))
17107 (defun org-time-string-to-seconds (s)
17108 "Convert a timestamp string to a number of seconds."
17109 (float-time (org-time-string-to-time s)))
17111 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
17112 "Convert a time stamp to an absolute day number.
17113 If there is a specifier for a cyclic time stamp, get the closest
17115 PREFER and SHOW-ALL are passed through to `org-closest-date'.
17116 The variable `date' is bound by the calendar when this is called."
17118 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
17119 (if (org-diary-sexp-entry (match-string 1 s) "" date)
17122 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
17123 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
17124 (time-to-days (current-time))) (match-string 0 s)
17127 (condition-case errdata
17128 (apply 'encode-time (org-parse-time-string s))
17129 (error (error "Bad timestamp `%s'%s\nError was: %s"
17130 s (if (not (and buffer pos))
17132 (format-message " at %d in buffer `%s'" pos buffer))
17133 (cdr errdata))))))))
17135 (defun org-days-to-iso-week (days)
17136 "Return the iso week number."
17138 (car (calendar-iso-from-absolute days)))
17140 (defun org-small-year-to-year (year)
17141 "Convert 2-digit years into 4-digit years.
17142 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
17143 The year 2000 cannot be abbreviated. Any year larger than 99
17144 is returned unchanged."
17146 (setq year (+ 2000 year))
17148 (setq year (+ 1900 year))))
17151 (defun org-time-from-absolute (d)
17152 "Return the time corresponding to date D.
17153 D may be an absolute day number, or a calendar-type list (month day year)."
17154 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
17155 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
17157 (defun org-calendar-holiday ()
17158 "List of holidays, for Diary display in Org-mode."
17159 (require 'holidays)
17161 (if (fboundp 'calendar-check-holidays)
17162 'calendar-check-holidays 'check-calendar-holidays) date)))
17163 (if hl (mapconcat 'identity hl "; "))))
17165 (defun org-diary-sexp-entry (sexp entry date)
17166 "Process a SEXP diary ENTRY for DATE."
17167 (require 'diary-lib)
17168 (let ((result (if calendar-debug-sexp
17169 (let ((stack-trace-on-error t))
17170 (eval (car (read-from-string sexp))))
17171 (condition-case nil
17172 (eval (car (read-from-string sexp)))
17175 (message "Bad sexp at line %d in %s: %s"
17177 (buffer-file-name) sexp)
17179 (cond ((stringp result) (split-string result "; "))
17180 ((and (consp result)
17181 (not (consp (cdr result)))
17182 (stringp (cdr result))) (cdr result))
17183 ((and (consp result)
17184 (stringp (car result))) result)
17187 (defun org-diary-to-ical-string (frombuf)
17188 "Get iCalendar entries from diary entries in buffer FROMBUF.
17189 This uses the icalendar.el library."
17190 (let* ((tmpdir (if (featurep 'xemacs)
17192 temporary-file-directory))
17193 (tmpfile (make-temp-name
17194 (expand-file-name "orgics" tmpdir)))
17196 (with-current-buffer frombuf
17197 (icalendar-export-region (point-min) (point-max) tmpfile)
17198 (setq buf (find-buffer-visiting tmpfile))
17200 (goto-char (point-min))
17201 (if (re-search-forward "^BEGIN:VEVENT" nil t)
17202 (setq b (match-beginning 0)))
17203 (goto-char (point-max))
17204 (if (re-search-backward "^END:VEVENT" nil t)
17205 (setq e (match-end 0)))
17206 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
17208 (delete-file tmpfile)
17211 (defun org-closest-date (start current change prefer show-all)
17212 "Find the date closest to CURRENT that is consistent with START and CHANGE.
17213 When PREFER is `past', return a date that is either CURRENT or past.
17214 When PREFER is `future', return a date that is either CURRENT or future.
17215 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
17216 ;; Make the proper lists from the dates
17218 (let ((a1 '(("h" . hour)
17223 (shour (nth 2 (org-parse-time-string start)))
17224 dn dw sday cday n1 n2 n0
17225 d m y y1 y2 date1 date2 nmonths nm ny m2)
17227 (setq start (org-date-to-gregorian start)
17228 current (org-date-to-gregorian
17231 (time-to-days (current-time))))
17232 sday (calendar-absolute-from-gregorian start)
17233 cday (calendar-absolute-from-gregorian current))
17235 (if (<= cday sday) (throw 'exit sday))
17237 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
17238 (setq dn (string-to-number (match-string 1 change))
17239 dw (cdr (assoc (match-string 2 change) a1)))
17240 (user-error "Invalid change specifier: %s" change))
17241 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
17244 (let ((missing-hours
17245 (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
17247 (setq n1 (if (zerop missing-hours) cday
17248 (- cday (1+ (floor (/ missing-hours 24)))))
17249 n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
17251 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
17254 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
17255 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
17256 (setq date1 (list m d y1)
17257 n1 (calendar-absolute-from-gregorian date1)
17258 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
17259 n2 (calendar-absolute-from-gregorian date2)))
17261 ;; approx number of month between the two dates
17262 (setq nmonths (floor (/ (- cday sday) 30.436875)))
17263 ;; How often does dn fit in there?
17264 (setq d (nth 1 start) m (car start) y (nth 2 start)
17265 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
17267 ny (floor (/ m 12))
17270 (while (> m 12) (setq m (- m 12) y (1+ y)))
17271 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
17272 (setq m2 (+ m dn) y2 y)
17273 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
17274 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
17275 (while (<= n2 cday)
17276 (setq n1 n2 m m2 y y2)
17277 (setq m2 (+ m dn) y2 y)
17278 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
17279 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
17280 ;; Make sure n1 is the earlier date
17281 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
17284 ((eq prefer 'past) (if (= cday n2) n2 n1))
17285 ((eq prefer 'future) (if (= cday n1) n1 n2))
17286 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
17288 ((eq prefer 'past) (if (= cday n2) n2 n1))
17289 ((eq prefer 'future) (if (= cday n1) n1 n2))
17290 (t (if (= cday n1) n1 n2)))))))
17292 (defun org-date-to-gregorian (date)
17293 "Turn any specification of DATE into a Gregorian date for the calendar."
17294 (cond ((integerp date) (calendar-gregorian-from-absolute date))
17295 ((and (listp date) (= (length date) 3)) date)
17297 (setq date (org-parse-time-string date))
17298 (list (nth 4 date) (nth 3 date) (nth 5 date)))
17300 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
17302 (defun org-parse-time-string (s &optional nodefault)
17303 "Parse the standard Org-mode time string.
17304 This should be a lot faster than the normal `parse-time-string'.
17305 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
17306 hour and minute fields will be nil if not given."
17307 (cond ((string-match org-ts-regexp0 s)
17309 (if (or (match-beginning 8) (not nodefault))
17310 (string-to-number (or (match-string 8 s) "0")))
17311 (if (or (match-beginning 7) (not nodefault))
17312 (string-to-number (or (match-string 7 s) "0")))
17313 (string-to-number (match-string 4 s))
17314 (string-to-number (match-string 3 s))
17315 (string-to-number (match-string 2 s))
17317 ((string-match "^<[^>]+>$" s)
17318 (decode-time (seconds-to-time (org-matcher-time s))))
17319 (t (error "Not a standard Org-mode time string: %s" s))))
17321 (defun org-timestamp-up (&optional arg)
17322 "Increase the date item at the cursor by one.
17323 If the cursor is on the year, change the year. If it is on the month,
17324 the day or the time, change that.
17325 With prefix ARG, change by that many units."
17327 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
17329 (defun org-timestamp-down (&optional arg)
17330 "Decrease the date item at the cursor by one.
17331 If the cursor is on the year, change the year. If it is on the month,
17332 the day or the time, change that.
17333 With prefix ARG, change by that many units."
17335 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
17337 (defun org-timestamp-up-day (&optional arg)
17338 "Increase the date in the time stamp by one day.
17339 With prefix ARG, change that many days."
17341 (if (and (not (org-at-timestamp-p t))
17342 (org-at-heading-p))
17344 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
17346 (defun org-timestamp-down-day (&optional arg)
17347 "Decrease the date in the time stamp by one day.
17348 With prefix ARG, change that many days."
17350 (if (and (not (org-at-timestamp-p t))
17351 (org-at-heading-p))
17353 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
17355 (defun org-at-timestamp-p (&optional inactive-ok)
17356 "Determine if the cursor is in or at a timestamp."
17358 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
17360 (ans (or (looking-at tsr)
17362 (skip-chars-backward "^[<\n\r\t")
17363 (if (> (point) (point-min)) (backward-char 1))
17364 (and (looking-at tsr)
17365 (> (- (match-end 0) pos) -1))))))
17367 (boundp 'org-ts-what)
17370 ((= pos (match-beginning 0)) 'bracket)
17371 ;; Point is considered to be "on the bracket" whether
17372 ;; it's really on it or right after it.
17373 ((= pos (1- (match-end 0))) 'bracket)
17374 ((= pos (match-end 0)) 'after)
17375 ((org-pos-in-match-range pos 2) 'year)
17376 ((org-pos-in-match-range pos 3) 'month)
17377 ((org-pos-in-match-range pos 7) 'hour)
17378 ((org-pos-in-match-range pos 8) 'minute)
17379 ((or (org-pos-in-match-range pos 4)
17380 (org-pos-in-match-range pos 5)) 'day)
17381 ((and (> pos (or (match-end 8) (match-end 5)))
17382 (< pos (match-end 0)))
17383 (- pos (or (match-end 8) (match-end 5))))
17387 (defun org-toggle-timestamp-type ()
17388 "Toggle the type (<active> or [inactive]) of a time stamp."
17390 (when (org-at-timestamp-p t)
17391 (let ((beg (match-beginning 0)) (end (match-end 0))
17392 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
17395 (while (re-search-forward "[][<>]" end t)
17396 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
17398 (message "Timestamp is now %sactive"
17399 (if (equal (char-after beg) ?<) "" "in")))))
17401 (defun org-at-clock-log-p nil
17402 "Is the cursor on the clock log line?"
17404 (move-beginning-of-line 1)
17405 (looking-at "^[ \t]*CLOCK:")))
17407 (defvar org-clock-history) ; defined in org-clock.el
17408 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
17409 (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
17410 "Change the date in the time stamp at point.
17411 The date will be changed by N times WHAT. WHAT can be `day', `month',
17412 `year', `minute', `second'. If WHAT is not given, the cursor position
17413 in the timestamp determines what will be changed.
17414 When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
17415 (let ((origin (point)) origin-cat
17417 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
17420 ts time time0 fixnext clrgx)
17421 (if (not (org-at-timestamp-p t))
17422 (user-error "Not at a timestamp"))
17423 (if (and (not what) (eq org-ts-what 'bracket))
17424 (org-toggle-timestamp-type)
17425 ;; Point isn't on brackets. Remember the part of the time-stamp
17426 ;; the point was in. Indeed, size of time-stamps may change,
17427 ;; but point must be kept in the same category nonetheless.
17428 (setq origin-cat org-ts-what)
17429 (if (and (not what) (not (eq org-ts-what 'day))
17430 org-display-custom-times
17431 (get-text-property (point) 'display)
17432 (not (get-text-property (1- (point)) 'display)))
17433 (setq org-ts-what 'day))
17434 (setq org-ts-what (or what org-ts-what)
17435 inactive (= (char-after (match-beginning 0)) ?\[)
17436 ts (match-string 0))
17438 (when (string-match
17439 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
17441 (setq extra (match-string 1 ts))
17442 (if suppress-tmp-delay
17443 (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
17444 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
17446 (setq time0 (org-parse-time-string ts))
17448 (eq org-ts-what 'minute)
17449 (not current-prefix-arg))
17450 ;; This looks like s-up and s-down. Change by one rounding step.
17451 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
17452 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
17453 (setcar (cdr time0) (+ (nth 1 time0)
17454 (if (> n 0) (- rem) (- dm rem))))))
17456 (encode-time (or (car time0) 0)
17457 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
17458 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
17459 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
17460 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
17461 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))))
17462 (when (and (member org-ts-what '(hour minute))
17464 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
17465 (setq extra (org-modify-ts-extra
17467 (if (eq org-ts-what 'hour) 2 5)
17469 (when (integerp org-ts-what)
17470 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
17471 (if (eq what 'calendar)
17472 (let ((cal-date (org-get-date-from-calendar)))
17473 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
17474 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
17475 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
17476 (setcar time0 (or (car time0) 0))
17477 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
17478 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
17479 (setq time (apply 'encode-time time0))))
17480 ;; Insert the new time-stamp, and ensure point stays in the same
17481 ;; category as before (i.e. not after the last position in that
17483 (let ((pos (point)))
17484 ;; Stay before inserted string. `save-excursion' is of no use.
17485 (setq org-last-changed-timestamp
17486 (org-insert-time-stamp time with-hm inactive nil nil extra))
17489 (looking-at org-ts-regexp3)
17491 ;; `day' category ends before `hour' if any, or at
17492 ;; the end of the day name.
17493 ((eq origin-cat 'day)
17494 (min (or (match-beginning 7) (1- (match-end 5))) origin))
17495 ((eq origin-cat 'hour) (min (match-end 7) origin))
17496 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
17497 ((integerp origin-cat) (min (1- (match-end 0)) origin))
17498 ;; `year' and `month' have both fixed size: point
17499 ;; couldn't have moved into another part.
17501 ;; Update clock if on a CLOCK line.
17502 (org-clock-update-time-maybe)
17503 ;; Maybe adjust the closest clock in `org-clock-history'
17504 (when org-clock-adjust-closest
17505 (if (not (and (org-at-clock-log-p)
17506 (< 1 (length (delq nil (mapcar 'marker-position
17507 org-clock-history))))))
17508 (message "No clock to adjust")
17509 (cond ((save-excursion ; fix previous clock?
17510 (re-search-backward org-ts-regexp0 nil t)
17511 (org-looking-back (concat org-clock-string " \\[")))
17512 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
17513 ((save-excursion ; fix next clock?
17514 (re-search-backward org-ts-regexp0 nil t)
17515 (looking-at (concat org-ts-regexp0 "\\] =>")))
17516 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
17517 (save-window-excursion
17518 ;; Find closest clock to point, adjust the previous/next one in history
17519 (let* ((p (save-excursion (org-back-to-heading t)))
17520 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
17522 (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
17523 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
17525 (message "No clock to adjust")
17527 (org-goto-marker-or-bmk clfixpos)
17529 (when (re-search-forward clrgx nil t)
17530 (goto-char (match-beginning 1))
17531 (let (org-clock-adjust-closest)
17532 (org-timestamp-change n org-ts-what updown))
17533 (message "Clock adjusted in %s for heading: %s"
17534 (file-name-nondirectory (buffer-file-name))
17535 (org-get-heading t t)))))))))
17536 ;; Try to recenter the calendar window, if any.
17537 (if (and org-calendar-follow-timestamp-change
17538 (get-buffer-window "*Calendar*" t)
17539 (memq org-ts-what '(day month year)))
17540 (org-recenter-calendar (time-to-days time))))))
17542 (defun org-modify-ts-extra (s pos n dm)
17543 "Change the different parts of the lead-time and repeat fields in timestamp."
17544 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
17546 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
17548 ((or (org-pos-in-match-range pos 2)
17549 (org-pos-in-match-range pos 3))
17550 (setq m (string-to-number (match-string 3 s))
17551 h (string-to-number (match-string 2 s)))
17552 (if (org-pos-in-match-range pos 2)
17554 (setq n (* dm (org-no-warnings (signum n))))
17555 (when (not (= 0 (setq rem (% m dm))))
17556 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
17558 (if (< m 0) (setq m (+ m 60) h (1- h)))
17559 (if (> m 59) (setq m (- m 60) h (1+ h)))
17560 (setq h (min 24 (max 0 h)))
17561 (setq ng 1 new (format "-%02d:%02d" h m)))
17562 ((org-pos-in-match-range pos 6)
17563 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
17564 ((org-pos-in-match-range pos 5)
17565 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
17567 ((org-pos-in-match-range pos 9)
17568 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
17569 ((org-pos-in-match-range pos 8)
17570 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
17574 (substring s 0 (match-beginning ng))
17576 (substring s (match-end ng))))))
17579 (defun org-recenter-calendar (date)
17580 "If the calendar is visible, recenter it to DATE."
17581 (let ((cwin (get-buffer-window "*Calendar*" t)))
17583 (let ((calendar-move-hook nil))
17584 (with-selected-window cwin
17585 (calendar-goto-date (if (listp date) date
17586 (calendar-gregorian-from-absolute date))))))))
17588 (defun org-goto-calendar (&optional arg)
17589 "Go to the Emacs calendar at the current date.
17590 If there is a time stamp in the current line, go to that date.
17591 A prefix ARG can be used to force the current date."
17593 (let ((tsr org-ts-regexp) diff
17594 (calendar-move-hook nil)
17595 (calendar-view-holidays-initially-flag nil)
17596 (calendar-view-diary-initially-flag nil))
17597 (if (or (org-at-timestamp-p)
17599 (beginning-of-line 1)
17600 (looking-at (concat ".*" tsr))))
17601 (let ((d1 (time-to-days (current-time)))
17603 (org-time-string-to-time (match-string 1)))))
17604 (setq diff (- d2 d1))))
17606 (calendar-goto-today)
17607 (if (and diff (not arg)) (calendar-forward-day diff))))
17609 (defun org-get-date-from-calendar ()
17610 "Return a list (month day year) of date at point in calendar."
17611 (with-current-buffer "*Calendar*"
17613 (calendar-cursor-to-date))))
17615 (defun org-date-from-calendar ()
17616 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
17617 If there is already a time stamp at the cursor position, update it."
17619 (if (org-at-timestamp-p t)
17620 (org-timestamp-change 0 'calendar)
17621 (let ((cal-date (org-get-date-from-calendar)))
17622 (org-insert-time-stamp
17623 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
17625 (defcustom org-effort-durations
17628 ("w" . ,(* 60 8 5))
17629 ("m" . ,(* 60 8 5 4))
17630 ("y" . ,(* 60 8 5 40)))
17631 "Conversion factor to minutes for an effort modifier.
17633 Each entry has the form (MODIFIER . MINUTES).
17635 In an effort string, a number followed by MODIFIER is multiplied
17636 by the specified number of MINUTES to obtain an effort in
17639 For example, if the value of this variable is ((\"hours\" . 60)), then an
17640 effort string \"2hours\" is equivalent to 120 minutes."
17643 :type '(alist :key-type (string :tag "Modifier")
17644 :value-type (number :tag "Minutes")))
17646 (defun org-minutes-to-clocksum-string (m)
17647 "Format number of minutes as a clocksum string.
17648 The format is determined by `org-time-clocksum-format',
17649 `org-time-clocksum-use-fractional' and
17650 `org-time-clocksum-fractional-format' and
17651 `org-time-clocksum-use-effort-durations'."
17652 (let ((clocksum "")
17653 (m (round m)) ; Don't allow fractions of minutes
17655 (setq h (if org-time-clocksum-use-effort-durations
17656 (cdr (assoc "h" org-effort-durations)) 60)
17657 d (if org-time-clocksum-use-effort-durations
17658 (/ (cdr (assoc "d" org-effort-durations)) h) 24)
17659 w (if org-time-clocksum-use-effort-durations
17660 (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
17661 mo (if org-time-clocksum-use-effort-durations
17662 (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
17663 y (if org-time-clocksum-use-effort-durations
17664 (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
17665 ;; fractional format
17666 (if org-time-clocksum-use-fractional
17668 ;; single format string
17669 ((stringp org-time-clocksum-fractional-format)
17670 (format org-time-clocksum-fractional-format (/ m (float h))))
17671 ;; choice of fractional formats for different time units
17672 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
17673 (> (/ (truncate m) (* y d h)) 0))
17674 (format fmt (/ m (* y d (float h)))))
17675 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
17676 (> (/ (truncate m) (* mo d h)) 0))
17677 (format fmt (/ m (* mo d (float h)))))
17678 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
17679 (> (/ (truncate m) (* w d h)) 0))
17680 (format fmt (/ m (* w d (float h)))))
17681 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
17682 (> (/ (truncate m) (* d h)) 0))
17683 (format fmt (/ m (* d (float h)))))
17684 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
17685 (> (/ (truncate m) h) 0))
17686 (format fmt (/ m (float h))))
17687 ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
17689 ;; fall back to smallest time unit with a format
17690 ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
17691 (format fmt (/ m (float h))))
17692 ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
17693 (format fmt (/ m (* d (float h)))))
17694 ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
17695 (format fmt (/ m (* w d (float h)))))
17696 ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
17697 (format fmt (/ m (* mo d (float h)))))
17698 ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
17699 (format fmt (/ m (* y d (float h))))))
17700 ;; standard (non-fractional) format, with single format string
17701 (if (stringp org-time-clocksum-format)
17702 (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
17703 ;; separate formats components
17704 (and (setq fmt (plist-get org-time-clocksum-format :years))
17705 (or (> (setq n (/ (truncate m) (* y d h))) 0)
17706 (plist-get org-time-clocksum-format :require-years))
17707 (setq clocksum (concat clocksum (format fmt n))
17708 m (- m (* n y d h))))
17709 (and (setq fmt (plist-get org-time-clocksum-format :months))
17710 (or (> (setq n (/ (truncate m) (* mo d h))) 0)
17711 (plist-get org-time-clocksum-format :require-months))
17712 (setq clocksum (concat clocksum (format fmt n))
17713 m (- m (* n mo d h))))
17714 (and (setq fmt (plist-get org-time-clocksum-format :weeks))
17715 (or (> (setq n (/ (truncate m) (* w d h))) 0)
17716 (plist-get org-time-clocksum-format :require-weeks))
17717 (setq clocksum (concat clocksum (format fmt n))
17718 m (- m (* n w d h))))
17719 (and (setq fmt (plist-get org-time-clocksum-format :days))
17720 (or (> (setq n (/ (truncate m) (* d h))) 0)
17721 (plist-get org-time-clocksum-format :require-days))
17722 (setq clocksum (concat clocksum (format fmt n))
17723 m (- m (* n d h))))
17724 (and (setq fmt (plist-get org-time-clocksum-format :hours))
17725 (or (> (setq n (/ (truncate m) h)) 0)
17726 (plist-get org-time-clocksum-format :require-hours))
17727 (setq clocksum (concat clocksum (format fmt n))
17729 (and (setq fmt (plist-get org-time-clocksum-format :minutes))
17730 (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
17731 (setq clocksum (concat clocksum (format fmt m))))
17732 ;; return formatted time duration
17735 (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
17736 (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
17737 "Org mode version 8.0")
17739 (defun org-hours-to-clocksum-string (n)
17740 (org-minutes-to-clocksum-string (* n 60)))
17742 (defun org-hh:mm-string-to-minutes (s)
17743 "Convert a string H:MM to a number of minutes.
17744 If the string is just a number, interpret it as minutes.
17745 In fact, the first hh:mm or number in the string will be taken,
17746 there can be extra stuff in the string.
17747 If no number is found, the return value is 0."
17750 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
17751 (+ (* (string-to-number (match-string 1 s)) 60)
17752 (string-to-number (match-string 2 s))))
17753 ((string-match "\\([0-9]+\\)" s)
17754 (string-to-number (match-string 1 s)))
17757 (defcustom org-image-actual-width t
17758 "Should we use the actual width of images when inlining them?
17760 When set to t, always use the image width.
17762 When set to a number, use imagemagick (when available) to set
17763 the image's width to this value.
17765 When set to a number in a list, try to get the width from any
17766 #+ATTR.* keyword if it matches a width specification like
17768 #+ATTR_HTML: :width 300px
17770 and fall back on that number if none is found.
17772 When set to nil, try to get the width from an #+ATTR.* keyword
17773 and fall back on the original width if none is found.
17775 This requires Emacs >= 24.1, build with imagemagick support."
17776 :group 'org-appearance
17778 :package-version '(Org . "8.0")
17780 (const :tag "Use the image width" t)
17781 (integer :tag "Use a number of pixels")
17782 (list :tag "Use #+ATTR* or a number of pixels" (integer))
17783 (const :tag "Use #+ATTR* or don't resize" nil)))
17785 (defcustom org-agenda-inhibit-startup nil
17786 "Inhibit startup when preparing agenda buffers.
17787 When this variable is t, the initialization of the Org agenda
17788 buffers is inhibited: e.g. the visibility state is not set, the
17789 tables are not re-aligned, etc."
17792 :group 'org-agenda)
17794 (defcustom org-agenda-ignore-drawer-properties nil
17795 "Avoid updating text properties when building the agenda.
17796 Properties are used to prepare buffers for effort estimates, appointments,
17797 and subtree-local categories.
17798 If you don't use these in the agenda, you can add them to this list and
17799 agenda building will be a bit faster.
17800 The value is a list, with zero or more of the symbols `effort', `appt',
17802 :type '(set :greedy t
17807 :group 'org-agenda)
17809 (defun org-duration-string-to-minutes (s &optional output-to-string)
17810 "Convert a duration string S to minutes.
17812 A bare number is interpreted as minutes, modifiers can be set by
17813 customizing `org-effort-durations' (which see).
17815 Entries containing a colon are interpreted as H:MM by
17816 `org-hh:mm-string-to-minutes'."
17818 (re (concat "\\([0-9.]+\\) *\\("
17819 (regexp-opt (mapcar 'car org-effort-durations))
17821 (while (string-match re s)
17822 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
17823 (string-to-number (match-string 1 s))))
17824 (setq s (replace-match "" nil t s)))
17825 (setq result (floor result))
17826 (incf result (org-hh:mm-string-to-minutes s))
17827 (if output-to-string (number-to-string result) result)))
17831 (defun org-save-all-org-buffers ()
17832 "Save all Org-mode buffers without user confirmation."
17834 (message "Saving all Org-mode buffers...")
17835 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
17836 (when (featurep 'org-id) (org-id-locations-save))
17837 (message "Saving all Org-mode buffers... done"))
17839 (defun org-revert-all-org-buffers ()
17840 "Revert all Org-mode buffers.
17841 Prompt for confirmation when there are unsaved changes.
17842 Be sure you know what you are doing before letting this function
17843 overwrite your changes.
17845 This function is useful in a setup where one tracks org files
17846 with a version control system, to revert on one machine after pulling
17847 changes from another. I believe the procedure must be like this:
17849 1. M-x org-save-all-org-buffers
17850 2. Pull changes from the other machine, resolve conflicts
17851 3. M-x org-revert-all-org-buffers"
17853 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
17854 (user-error "Abort"))
17856 (save-window-excursion
17859 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
17860 (with-current-buffer b buffer-file-name))
17861 (org-pop-to-buffer-same-window b)
17862 (revert-buffer t 'no-confirm)))
17864 (when (and (featurep 'org-id) org-id-track-globally)
17865 (org-id-locations-load)))))
17870 (defun org-switchb (&optional arg)
17871 "Switch between Org buffers.
17872 With one prefix argument, restrict available buffers to files.
17873 With two prefix arguments, restrict available buffers to agenda files.
17875 Defaults to `iswitchb' for buffer name completion.
17876 Set `org-completion-use-ido' to make it use ido instead."
17878 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
17879 ((equal arg '(16)) (org-buffer-list 'agenda))
17880 (t (org-buffer-list))))
17881 (org-completion-use-iswitchb org-completion-use-iswitchb)
17882 (org-completion-use-ido org-completion-use-ido))
17883 (unless (or org-completion-use-ido org-completion-use-iswitchb)
17884 (setq org-completion-use-iswitchb t))
17885 (org-pop-to-buffer-same-window
17886 (org-icompleting-read "Org buffer: "
17887 (mapcar 'list (mapcar 'buffer-name blist))
17890 ;;; Define some older names previously used for this functionality
17892 (defalias 'org-ido-switchb 'org-switchb)
17894 (defalias 'org-iswitchb 'org-switchb)
17896 (defun org-buffer-list (&optional predicate exclude-tmp)
17897 "Return a list of Org buffers.
17898 PREDICATE can be `export', `files' or `agenda'.
17900 export restrict the list to Export buffers.
17901 files restrict the list to buffers visiting Org files.
17902 agenda restrict the list to buffers visiting agenda files.
17904 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
17906 (agenda-files (and (eq predicate 'agenda)
17907 (mapcar 'file-truename (org-agenda-files t))))
17910 ((eq predicate 'files)
17911 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
17912 ((eq predicate 'export)
17913 (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
17914 ((eq predicate 'agenda)
17916 (with-current-buffer b
17917 (and (derived-mode-p 'org-mode)
17918 (setq bfn (buffer-file-name b))
17919 (member (file-truename bfn) agenda-files)))))
17920 (t (lambda (b) (with-current-buffer b
17921 (or (derived-mode-p 'org-mode)
17922 (string-match "\\*Org .*Export"
17923 (buffer-name b)))))))))
17927 (if (and (funcall filter b)
17928 (or (not exclude-tmp)
17929 (not (string-match "tmp" (buffer-name b)))))
17934 (defun org-agenda-files (&optional unrestricted archives)
17935 "Get the list of agenda files.
17936 Optional UNRESTRICTED means return the full list even if a restriction
17937 is currently in place.
17938 When ARCHIVES is t, include all archive files that are really being
17939 used by the agenda files. If ARCHIVE is `ifmode', do this only if
17940 `org-agenda-archives-mode' is t."
17943 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
17944 ((stringp org-agenda-files) (org-read-agenda-file-list))
17945 ((listp org-agenda-files) org-agenda-files)
17946 (t (error "Invalid value of `org-agenda-files'")))))
17947 (setq files (apply 'append
17948 (mapcar (lambda (f)
17949 (if (file-directory-p f)
17951 f t org-agenda-file-regexp)
17954 (when org-agenda-skip-unavailable-files
17955 (setq files (delq nil
17958 (and (file-readable-p file) file)))
17960 (when (or (eq archives t)
17961 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
17962 (setq files (org-add-archive-files files)))
17965 (defun org-agenda-file-p (&optional file)
17966 "Return non-nil, if FILE is an agenda file.
17967 If FILE is omitted, use the file associated with the current
17969 (member (or file (buffer-file-name))
17970 (org-agenda-files t)))
17972 (defun org-edit-agenda-file-list ()
17973 "Edit the list of agenda files.
17974 Depending on setup, this either uses customize to edit the variable
17975 `org-agenda-files', or it visits the file that is holding the list. In the
17976 latter case, the buffer is set up in a way that saving it automatically kills
17977 the buffer and restores the previous window configuration."
17979 (if (stringp org-agenda-files)
17980 (let ((cw (current-window-configuration)))
17981 (find-file org-agenda-files)
17982 (org-set-local 'org-window-configuration cw)
17983 (org-add-hook 'after-save-hook
17985 (set-window-configuration
17986 (prog1 org-window-configuration
17987 (kill-buffer (current-buffer))))
17988 (org-install-agenda-files-menu)
17989 (message "New agenda file list installed"))
17991 (message "%s" (substitute-command-keys
17992 "Edit list and finish with \\[save-buffer]")))
17993 (customize-variable 'org-agenda-files)))
17995 (defun org-store-new-agenda-file-list (list)
17996 "Set new value for the agenda file list and save it correctly."
17997 (if (stringp org-agenda-files)
17998 (let ((fe (org-read-agenda-file-list t)) b u)
17999 (while (setq b (find-buffer-visiting org-agenda-files))
18001 (with-temp-file org-agenda-files
18004 (lambda (f) ;; Keep un-expanded entries.
18005 (if (setq u (assoc f fe))
18010 (let ((org-mode-hook nil) (org-inhibit-startup t)
18011 (org-insert-mode-line-in-empty-file nil))
18012 (setq org-agenda-files list)
18013 (customize-save-variable 'org-agenda-files org-agenda-files))))
18015 (defun org-read-agenda-file-list (&optional pair-with-expansion)
18016 "Read the list of agenda files from a file.
18017 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
18018 filenames, used by `org-store-new-agenda-file-list' to write back
18019 un-expanded file names."
18020 (when (file-directory-p org-agenda-files)
18021 (error "`org-agenda-files' cannot be a single directory"))
18022 (when (stringp org-agenda-files)
18024 (insert-file-contents org-agenda-files)
18027 (let ((e (expand-file-name (substitute-in-file-name f)
18029 (if pair-with-expansion
18032 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
18035 (defun org-cycle-agenda-files ()
18036 "Cycle through the files in `org-agenda-files'.
18037 If the current buffer visits an agenda file, find the next one in the list.
18038 If the current buffer does not, find the first agenda file."
18040 (let* ((fs (org-agenda-files t))
18041 (files (append fs (list (car fs))))
18042 (tcf (if buffer-file-name (file-truename buffer-file-name)))
18044 (unless files (user-error "No agenda files"))
18046 (dolist (file files)
18047 (if (equal (file-truename file) tcf)
18049 (find-file (car files))
18051 (find-file (car fs)))
18052 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
18054 (defun org-agenda-file-to-front (&optional to-end)
18055 "Move/add the current file to the top of the agenda file list.
18056 If the file is not present in the list, it is added to the front. If it is
18057 present, it is moved there. With optional argument TO-END, add/move to the
18060 (let ((org-agenda-skip-unavailable-files nil)
18061 (file-alist (mapcar (lambda (x)
18062 (cons (file-truename x) x))
18063 (org-agenda-files t)))
18064 (ctf (file-truename
18065 (or buffer-file-name
18066 (user-error "Please save the current buffer to a file"))))
18068 (setq x (assoc ctf file-alist) had x)
18070 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
18072 (setq file-alist (append (delq x file-alist) (list x)))
18073 (setq file-alist (cons x (delq x file-alist))))
18074 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
18075 (org-install-agenda-files-menu)
18076 (message "File %s to %s of agenda file list"
18077 (if had "moved" "added") (if to-end "end" "front"))))
18079 (defun org-remove-file (&optional file)
18080 "Remove current file from the list of files in variable `org-agenda-files'.
18081 These are the files which are being checked for agenda entries.
18082 Optional argument FILE means use this file instead of the current."
18084 (let* ((org-agenda-skip-unavailable-files nil)
18085 (file (or file buffer-file-name
18086 (user-error "Current buffer does not visit a file")))
18087 (true-file (file-truename file))
18088 (afile (abbreviate-file-name file))
18089 (files (delq nil (mapcar
18091 (if (equal true-file
18094 (org-agenda-files t)))))
18095 (if (not (= (length files) (length (org-agenda-files t))))
18097 (org-store-new-agenda-file-list files)
18098 (org-install-agenda-files-menu)
18099 (message "Removed file: %s" afile))
18100 (message "File was not in list: %s (not removed)" afile))))
18102 (defun org-file-menu-entry (file)
18103 (vector file (list 'find-file file) t))
18105 (defun org-check-agenda-file (file)
18106 "Make sure FILE exists. If not, ask user what to do."
18107 (when (not (file-exists-p file))
18108 (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
18109 (abbreviate-file-name file))
18110 (let ((r (downcase (read-char-exclusive))))
18113 (org-remove-file file)
18114 (throw 'nextfile t))
18115 (t (error "Abort"))))))
18117 (defun org-get-agenda-file-buffer (file)
18118 "Get a buffer visiting FILE. If the buffer needs to be created, add
18119 it to the list of buffers which might be released later."
18120 (let ((buf (org-find-base-buffer-visiting file)))
18122 buf ; just return it
18123 ;; Make a new buffer and remember it
18124 (setq buf (find-file-noselect file))
18125 (if buf (push buf org-agenda-new-buffers))
18128 (defun org-release-buffers (blist)
18129 "Release all buffers in list, asking the user for confirmation when needed.
18130 When a buffer is unmodified, it is just killed. When modified, it is saved
18131 \(if the user agrees) and then killed."
18133 (dolist (buf blist)
18134 (setq file (buffer-file-name buf))
18135 (when (and (buffer-modified-p buf)
18137 (y-or-n-p (format "Save file %s? " file)))
18138 (with-current-buffer buf (save-buffer)))
18139 (kill-buffer buf))))
18141 (defun org-agenda-prepare-buffers (files)
18142 "Create buffers for all agenda files, protect archived trees and comments."
18144 (let ((pa '(:org-archived t))
18145 (pc '(:org-comment t))
18146 (pall '(:org-archived t :org-comment t))
18147 (inhibit-read-only t)
18148 (org-inhibit-startup org-agenda-inhibit-startup)
18149 (rea (concat ":" org-archive-tag ":"))
18151 (setq org-tag-alist-for-agenda nil
18152 org-tag-groups-alist-for-agenda nil)
18155 (dolist (file files)
18159 (org-check-agenda-file file)
18160 (set-buffer (org-get-agenda-file-buffer file)))
18162 (org-set-regexps-and-options-for-tags)
18164 (goto-char (point-min))
18165 (let ((case-fold-search t))
18166 (when (search-forward "#+setupfile" nil t)
18167 ;; Don't set all regexps and options systematically as
18168 ;; this is only run for setting agenda tags from setup
18170 (org-set-regexps-and-options)))
18171 (or (memq 'category org-agenda-ignore-drawer-properties)
18172 (org-refresh-category-properties))
18173 (or (memq 'effort org-agenda-ignore-drawer-properties)
18174 (org-refresh-properties org-effort-property 'org-effort))
18175 (or (memq 'appt org-agenda-ignore-drawer-properties)
18176 (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
18177 (setq org-todo-keywords-for-agenda
18178 (append org-todo-keywords-for-agenda org-todo-keywords-1))
18179 (setq org-done-keywords-for-agenda
18180 (append org-done-keywords-for-agenda org-done-keywords))
18181 (setq org-todo-keyword-alist-for-agenda
18182 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
18183 (setq org-drawers-for-agenda
18184 (append org-drawers-for-agenda org-drawers))
18185 (setq org-tag-alist-for-agenda
18187 (append org-tag-alist-for-agenda
18189 org-tag-persistent-alist)))
18191 (setq org-tag-groups-alist-for-agenda
18192 (org-uniquify-alist
18193 (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
18194 (org-with-silent-modifications
18196 (remove-text-properties (point-min) (point-max) pall)
18197 (when org-agenda-skip-archived-trees
18198 (goto-char (point-min))
18199 (while (re-search-forward rea nil t)
18200 (if (org-at-heading-p t)
18201 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
18202 (goto-char (point-min))
18203 (setq re (format org-heading-keyword-regexp-format
18204 org-comment-string))
18205 (while (re-search-forward re nil t)
18206 (add-text-properties
18207 (match-beginning 0) (org-end-of-subtree t) pc))))
18208 (goto-char pos)))))
18209 (setq org-todo-keywords-for-agenda
18210 (org-uniquify org-todo-keywords-for-agenda))
18211 (setq org-todo-keyword-alist-for-agenda
18212 (org-uniquify org-todo-keyword-alist-for-agenda))))
18215 ;;;; CDLaTeX minor mode
18217 (defvar org-cdlatex-mode-map (make-sparse-keymap)
18218 "Keymap for the minor `org-cdlatex-mode'.")
18220 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
18221 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
18222 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
18223 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
18224 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
18226 (defvar org-cdlatex-texmathp-advice-is-done nil
18227 "Flag remembering if we have applied the advice to texmathp already.")
18229 (define-minor-mode org-cdlatex-mode
18230 "Toggle the minor `org-cdlatex-mode'.
18231 This mode supports entering LaTeX environment and math in LaTeX fragments
18233 \\{org-cdlatex-mode-map}"
18235 (when org-cdlatex-mode
18237 (run-hooks 'cdlatex-mode-hook)
18238 (cdlatex-compute-tables))
18239 (unless org-cdlatex-texmathp-advice-is-done
18240 (setq org-cdlatex-texmathp-advice-is-done t)
18241 (defadvice texmathp (around org-math-always-on activate)
18242 "Always return t in org-mode buffers.
18243 This is because we want to insert math symbols without dollars even outside
18244 the LaTeX math segments. If Orgmode thinks that point is actually inside
18245 an embedded LaTeX fragment, let texmathp do its job.
18246 \\[org-cdlatex-mode-map]"
18250 ((not (derived-mode-p 'org-mode)) ad-do-it)
18251 ((eq this-command 'cdlatex-math-symbol)
18252 (setq ad-return-value t
18253 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
18255 (let ((p (org-inside-LaTeX-fragment-p)))
18256 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
18257 (setq ad-return-value t
18258 texmathp-why '("Org-mode embedded math" . 0))
18259 (if p ad-do-it)))))))))
18261 (defun turn-on-org-cdlatex ()
18262 "Unconditionally turn on `org-cdlatex-mode'."
18263 (org-cdlatex-mode 1))
18265 (defun org-try-cdlatex-tab ()
18266 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
18267 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
18268 - inside a LaTeX fragment, or
18269 - after the first word in a line, where an abbreviation expansion could
18270 insert a LaTeX environment."
18271 (when org-cdlatex-mode
18273 ;; Before any word on the line: No expansion possible.
18274 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
18275 ;; Just after first word on the line: Expand it. Make sure it
18276 ;; cannot happen on headlines, though.
18278 (skip-chars-backward "a-zA-Z0-9*")
18279 (skip-chars-backward " \t")
18280 (and (bolp) (not (org-at-heading-p))))
18282 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
18284 (defun org-cdlatex-underscore-caret (&optional arg)
18285 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
18286 Revert to the normal definition outside of these fragments."
18288 (if (org-inside-LaTeX-fragment-p)
18289 (call-interactively 'cdlatex-sub-superscript)
18290 (let (org-cdlatex-mode)
18291 (call-interactively (key-binding (vector last-input-event))))))
18293 (defun org-cdlatex-math-modify (&optional arg)
18294 "Execute `cdlatex-math-modify' in LaTeX fragments.
18295 Revert to the normal definition outside of these fragments."
18297 (if (org-inside-LaTeX-fragment-p)
18298 (call-interactively 'cdlatex-math-modify)
18299 (let (org-cdlatex-mode)
18300 (call-interactively (key-binding (vector last-input-event))))))
18304 ;;;; LaTeX fragments
18306 (defvar org-latex-regexps
18307 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
18308 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
18309 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
18310 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
18311 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
18312 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
18313 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
18314 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
18315 "Regular expressions for matching embedded LaTeX.")
18317 (defun org-inside-LaTeX-fragment-p ()
18318 "Test if point is inside a LaTeX fragment.
18319 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
18320 sequence appearing also before point.
18321 Even though the matchers for math are configurable, this function assumes
18322 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
18323 delimiters are skipped when they have been removed by customization.
18324 The return value is nil, or a cons cell with the delimiter and the
18325 position of this delimiter.
18327 This function does a reasonably good job, but can locally be fooled by
18328 for example currency specifications. For example it will assume being in
18329 inline math after \"$22.34\". The LaTeX fragment formatter will only format
18330 fragments that are properly closed, but during editing, we have to live
18331 with the uncertainty caused by missing closing delimiters. This function
18332 looks only before point, not after."
18334 (let ((pos (point))
18335 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
18337 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
18339 dd-on str (start 0) m re)
18342 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
18343 re (nth 1 (assoc "$" org-latex-regexps)))
18344 (while (string-match re str start)
18346 ((= (match-end 0) (length str))
18347 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
18348 ((= (match-end 0) (- (length str) 5))
18350 (t (setq start (match-end 0))))))
18351 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
18353 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
18354 (and (match-beginning 2) (throw 'exit nil))
18356 (while (re-search-backward "\\$\\$" lim t)
18357 (setq dd-on (not dd-on)))
18359 (if dd-on (cons "$$" m))))))
18361 (defun org-inside-latex-macro-p ()
18362 "Is point inside a LaTeX macro or its arguments?"
18365 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
18367 (defvar org-latex-fragment-image-overlays nil
18368 "List of overlays carrying the images of latex fragments.")
18369 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
18371 (defun org-remove-latex-fragment-image-overlays ()
18372 "Remove all overlays with LaTeX fragment images in current buffer."
18373 (mapc 'delete-overlay org-latex-fragment-image-overlays)
18374 (setq org-latex-fragment-image-overlays nil))
18376 (defun org-preview-latex-fragment (&optional subtree)
18377 "Preview the LaTeX fragment at point, or all locally or globally.
18378 If the cursor is in a LaTeX fragment, create the image and overlay
18379 it over the source code. If there is no fragment at point, display
18380 all fragments in the current text, from one headline to the next. With
18381 prefix SUBTREE, display all fragments in the current subtree. With a
18382 double prefix arg \\[universal-argument] \\[universal-argument], or when \
18383 the cursor is before the first headline,
18384 display all fragments in the buffer.
18385 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
18387 (unless buffer-file-name
18388 (user-error "Can't preview LaTeX fragment in a non-file buffer"))
18389 (when (display-graphic-p)
18390 (org-remove-latex-fragment-image-overlays)
18393 (let (beg end at msg)
18395 ((or (equal subtree '(16))
18396 (not (save-excursion
18397 (re-search-backward org-outline-regexp-bol nil t))))
18398 (setq beg (point-min) end (point-max)
18399 msg "Creating images for buffer...%s"))
18400 ((equal subtree '(4))
18401 (org-back-to-heading)
18402 (setq beg (point) end (org-end-of-subtree t)
18403 msg "Creating images for subtree...%s"))
18405 (if (setq at (org-inside-LaTeX-fragment-p))
18406 (goto-char (max (point-min) (- (cdr at) 2)))
18407 (org-back-to-heading))
18408 (setq beg (point) end (progn (outline-next-heading) (point))
18409 msg (if at "Creating image...%s"
18410 "Creating images for entry...%s"))))
18412 (narrow-to-region beg end)
18415 (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
18416 (file-name-nondirectory
18417 buffer-file-name)))
18418 default-directory 'overlays msg at 'forbuffer
18419 org-latex-create-formula-image-program)
18420 (message msg "done. Use `C-c C-c' to remove images."))))))
18422 (defun org-format-latex (prefix &optional dir overlays msg at
18423 forbuffer processing-type)
18424 "Replace LaTeX fragments with links to an image, and produce images.
18425 Some of the options can be changed using the variable
18426 `org-format-latex-options'."
18427 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
18428 (let* ((prefixnodir (file-name-nondirectory prefix))
18429 (absprefix (expand-file-name prefix dir))
18430 (todir (file-name-directory absprefix))
18431 (opt org-format-latex-options)
18432 (optnew org-format-latex-options)
18433 (matchers (plist-get opt :matchers))
18434 (re-list org-latex-regexps)
18435 (cnt 0) txt hash link beg end re checkdir
18437 m n block-type block linkfile movefile ov)
18438 ;; Check the different regular expressions
18439 (dolist (e re-list)
18440 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
18441 block (if block-type "\n\n" ""))
18442 (when (member m matchers)
18443 (goto-char (point-min))
18444 (while (re-search-forward re nil t)
18445 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
18447 (not (eq (get-char-property (match-beginning n)
18449 'org-latex-overlay))))
18451 ((eq processing-type 'verbatim))
18452 ((eq processing-type 'mathjax)
18453 ;; Prepare for MathJax processing.
18454 (setq string (match-string n))
18455 (when (member m '("$" "$1"))
18457 (delete-region (match-beginning n) (match-end n))
18458 (goto-char (match-beginning n))
18459 (insert (concat "\\(" (substring string 1 -1) "\\)")))))
18460 ((or (eq processing-type 'dvipng)
18461 (eq processing-type 'imagemagick))
18462 ;; Process to an image.
18463 (setq txt (match-string n)
18464 beg (match-beginning n) end (match-end n)
18466 (let ((face (face-at-point))
18467 (fg (plist-get opt :foreground))
18468 (bg (plist-get opt :background))
18469 ;; Ensure full list is printed.
18470 print-length print-level)
18472 ;; Get the colors from the face at point.
18474 (when (eq fg 'auto)
18475 (setq fg (face-attribute face :foreground nil 'default)))
18476 (when (eq bg 'auto)
18477 (setq bg (face-attribute face :background nil 'default)))
18478 (setq optnew (copy-sequence opt))
18479 (plist-put optnew :foreground fg)
18480 (plist-put optnew :background bg))
18481 (setq hash (sha1 (prin1-to-string
18482 (list org-format-latex-header
18483 org-latex-default-packages-alist
18484 org-latex-packages-alist
18485 org-format-latex-options
18486 forbuffer txt fg bg)))
18487 linkfile (format "%s_%s.png" prefix hash)
18488 movefile (format "%s_%s.png" absprefix hash)))
18489 (setq link (concat block "[[file:" linkfile "]]" block))
18490 (if msg (message msg cnt))
18492 (unless checkdir ; Ensure the directory exists.
18494 (or (file-directory-p todir) (make-directory todir t)))
18495 (unless (file-exists-p movefile)
18496 (org-create-formula-image
18497 txt movefile optnew forbuffer processing-type))
18501 (if (eq (overlay-get o 'org-overlay-type)
18502 'org-latex-overlay)
18503 (delete-overlay o)))
18504 (overlays-in beg end))
18505 (setq ov (make-overlay beg end))
18506 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
18507 (if (featurep 'xemacs)
18509 (overlay-put ov 'invisible t)
18512 (make-glyph (vector 'png :file movefile))))
18515 (list 'image :type 'png :file movefile :ascent 'center)))
18516 (push ov org-latex-fragment-image-overlays)
18518 (delete-region beg end)
18519 (insert (org-add-props link
18520 (list 'org-latex-src
18521 (replace-regexp-in-string
18523 'org-latex-src-embed-type
18524 (if block-type 'paragraph 'character))))))
18525 ((eq processing-type 'mathml)
18526 ;; Process to MathML
18527 (unless (save-match-data (org-format-latex-mathml-available-p))
18528 (user-error "LaTeX to MathML converter not configured"))
18529 (setq txt (match-string n)
18530 beg (match-beginning n) end (match-end n)
18532 (if msg (message msg cnt))
18534 (delete-region beg end)
18535 (insert (org-format-latex-as-mathml
18536 txt block-type prefix dir)))
18538 (error "Unknown conversion type %s for LaTeX fragments"
18539 processing-type)))))))))
18541 (defun org-create-math-formula (latex-frag &optional mathml-file)
18542 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
18543 Use `org-latex-to-mathml-convert-command'. If the conversion is
18544 sucessful, return the portion between \"<math...> </math>\"
18545 elements otherwise return nil. When MATHML-FILE is specified,
18546 write the results in to that file. When invoked as an
18547 interactive command, prompt for LATEX-FRAG, with initial value
18548 set to the current active region and echo the results for user
18550 (interactive (list (let ((frag (when (org-region-active-p)
18551 (buffer-substring-no-properties
18552 (region-beginning) (region-end)))))
18553 (read-string "LaTeX Fragment: " frag nil frag))))
18554 (unless latex-frag (error "Invalid LaTeX fragment"))
18555 (let* ((tmp-in-file (file-relative-name
18556 (make-temp-name (expand-file-name "ltxmathml-in"))))
18557 (ignore (write-region latex-frag nil tmp-in-file))
18558 (tmp-out-file (file-relative-name
18559 (make-temp-name (expand-file-name "ltxmathml-out"))))
18561 org-latex-to-mathml-convert-command
18562 `((?j . ,(shell-quote-argument
18563 (expand-file-name org-latex-to-mathml-jar-file)))
18564 (?I . ,(shell-quote-argument tmp-in-file))
18565 (?o . ,(shell-quote-argument tmp-out-file)))))
18566 mathml shell-command-output)
18567 (when (org-called-interactively-p 'any)
18568 (unless (org-format-latex-mathml-available-p)
18569 (user-error "LaTeX to MathML converter not configured")))
18570 (message "Running %s" cmd)
18571 (setq shell-command-output (shell-command-to-string cmd))
18573 (when (file-readable-p tmp-out-file)
18574 (with-current-buffer (find-file-noselect tmp-out-file t)
18575 (goto-char (point-min))
18576 (when (re-search-forward
18579 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
18581 (regexp-quote "</math>")) nil t)
18582 (prog1 (match-string 0) (kill-buffer))))))
18586 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
18588 (write-region mathml nil mathml-file))
18589 (when (org-called-interactively-p 'any)
18591 ((message "LaTeX to MathML conversion failed")
18592 (message shell-command-output)))
18593 (delete-file tmp-in-file)
18594 (when (file-exists-p tmp-out-file)
18595 (delete-file tmp-out-file))
18598 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
18599 prefix &optional dir)
18600 "Use `org-create-math-formula' but check local cache first."
18601 (let* ((absprefix (expand-file-name prefix dir))
18602 (print-length nil) (print-level nil)
18603 (formula-id (concat
18608 org-latex-to-mathml-convert-command)))))
18609 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
18610 (formula-cache-dir (file-name-directory formula-cache)))
18612 (unless (file-directory-p formula-cache-dir)
18613 (make-directory formula-cache-dir t))
18615 (unless (file-exists-p formula-cache)
18616 (org-create-math-formula latex-frag formula-cache))
18618 (if (file-exists-p formula-cache)
18619 ;; Successful conversion. Return the link to MathML file.
18621 (format "[[file:%s]]" (file-relative-name formula-cache dir))
18622 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
18623 'org-latex-src-embed-type (if latex-frag-type
18624 'paragraph 'character)))
18625 ;; Failed conversion. Return the LaTeX fragment verbatim
18628 (defun org-create-formula-image (string tofile options buffer &optional type)
18629 "Create an image from LaTeX source using dvipng or convert.
18630 This function calls either `org-create-formula-image-with-dvipng'
18631 or `org-create-formula-image-with-imagemagick' depending on the
18632 value of `org-latex-create-formula-image-program' or on the value
18633 of the optional TYPE variable.
18635 Note: ultimately these two function should be combined as they
18636 share a good deal of logic."
18637 (org-check-external-command
18638 "latex" "needed to convert LaTeX fragments to images")
18640 (case (or type org-latex-create-formula-image-program)
18642 (org-check-external-command
18643 "dvipng" "needed to convert LaTeX fragments to images")
18644 #'org-create-formula-image-with-dvipng)
18646 (org-check-external-command
18647 "convert" "you need to install imagemagick")
18648 #'org-create-formula-image-with-imagemagick)
18650 "Invalid value of `org-latex-create-formula-image-program'")))
18651 string tofile options buffer))
18653 (declare-function org-export-get-backend "ox" (name))
18654 (declare-function org-export--get-global-options "ox" (&optional backend))
18655 (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
18656 (declare-function org-latex-guess-inputenc "ox-latex" (header))
18657 (declare-function org-latex-guess-babel-language "ox-latex" (header info))
18658 (defun org-create-formula--latex-header ()
18659 "Return LaTeX header appropriate for previewing a LaTeX snippet."
18660 (let ((info (org-combine-plists (org-export--get-global-options
18661 (org-export-get-backend 'latex))
18662 (org-export--get-inbuffer-options
18663 (org-export-get-backend 'latex)))))
18664 (org-latex-guess-babel-language
18665 (org-latex-guess-inputenc
18666 (org-splice-latex-header
18667 org-format-latex-header
18668 org-latex-default-packages-alist
18669 org-latex-packages-alist t
18670 (plist-get info :latex-header)))
18673 ;; This function borrows from Ganesh Swami's latex2png.el
18674 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
18675 "This calls dvipng."
18676 (require 'ox-latex)
18677 (let* ((tmpdir (if (featurep 'xemacs)
18679 temporary-file-directory))
18680 (texfilebase (make-temp-name
18681 (expand-file-name "orgtex" tmpdir)))
18682 (texfile (concat texfilebase ".tex"))
18683 (dvifile (concat texfilebase ".dvi"))
18684 (pngfile (concat texfilebase ".png"))
18685 (fnh (if (featurep 'xemacs)
18686 (font-height (face-font 'default))
18687 (face-attribute 'default :height nil)))
18688 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
18689 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
18690 (fg (or (plist-get options (if buffer :foreground :html-foreground))
18692 (bg (or (plist-get options (if buffer :background :html-background))
18694 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
18695 (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
18696 (if (eq bg 'default) (setq bg (org-dvipng-color :background))
18697 (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
18698 (let ((latex-header (org-create-formula--latex-header)))
18699 (with-temp-file texfile
18700 (insert latex-header)
18701 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
18702 (let ((dir default-directory))
18703 (condition-case nil
18706 (call-process "latex" nil nil nil texfile))
18709 (if (not (file-exists-p dvifile))
18710 (progn (message "Failed to create dvi file from %s" texfile) nil)
18711 (condition-case nil
18712 (if (featurep 'xemacs)
18713 (call-process "dvipng" nil nil nil
18718 (call-process "dvipng" nil nil nil
18721 ;;"-x" scale "-y" scale
18726 (if (not (file-exists-p pngfile))
18727 (if org-format-latex-signal-error
18728 (error "Failed to create png file from %s" texfile)
18729 (message "Failed to create png file from %s" texfile)
18731 ;; Use the requested file name and clean up
18732 (copy-file pngfile tofile 'replace)
18733 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
18734 (if (file-exists-p (concat texfilebase e))
18735 (delete-file (concat texfilebase e))))
18738 (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
18739 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
18740 "This calls convert, which is included into imagemagick."
18741 (require 'ox-latex)
18742 (let* ((tmpdir (if (featurep 'xemacs)
18744 temporary-file-directory))
18745 (texfilebase (make-temp-name
18746 (expand-file-name "orgtex" tmpdir)))
18747 (texfile (concat texfilebase ".tex"))
18748 (pdffile (concat texfilebase ".pdf"))
18749 (pngfile (concat texfilebase ".png"))
18750 (fnh (if (featurep 'xemacs)
18751 (font-height (face-font 'default))
18752 (face-attribute 'default :height nil)))
18753 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
18754 (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
18755 (fg (or (plist-get options (if buffer :foreground :html-foreground))
18757 (bg (or (plist-get options (if buffer :background :html-background))
18759 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
18760 (setq fg (org-latex-color-format fg)))
18761 (if (eq bg 'default) (setq bg (org-latex-color :background))
18762 (setq bg (org-latex-color-format
18763 (if (string= bg "Transparent") "white" bg))))
18764 (let ((latex-header (org-create-formula--latex-header)))
18765 (with-temp-file texfile
18766 (insert latex-header)
18767 (insert "\n\\begin{document}\n"
18768 "\\definecolor{fg}{rgb}{" fg "}\n"
18769 "\\definecolor{bg}{rgb}{" bg "}\n"
18770 "\n\\pagecolor{bg}\n"
18774 "\n\\end{document}\n")))
18775 (org-latex-compile texfile t)
18776 (if (not (file-exists-p pdffile))
18777 (progn (message "Failed to create pdf file from %s" texfile) nil)
18778 (condition-case nil
18779 (if (featurep 'xemacs)
18780 (call-process "convert" nil nil nil
18786 ;; "-sharpen" "0x1.0"
18788 (call-process "convert" nil nil nil
18794 ;; "-sharpen" "0x1.0"
18797 (if (not (file-exists-p pngfile))
18798 (if org-format-latex-signal-error
18799 (error "Failed to create png file from %s" texfile)
18800 (message "Failed to create png file from %s" texfile)
18802 ;; Use the requested file name and clean up
18803 (copy-file pngfile tofile 'replace)
18804 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
18805 (if (file-exists-p (concat texfilebase e))
18806 (delete-file (concat texfilebase e))))
18809 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
18810 "Fill a LaTeX header template TPL.
18811 In the template, the following place holders will be recognized:
18813 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
18814 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
18815 [PACKAGES] \\usepackage statements for PKG
18816 [NO-PACKAGES] do not include PKG
18817 [EXTRA] the string EXTRA
18818 [NO-EXTRA] do not include EXTRA
18820 For backward compatibility, if both the positive and the negative place
18821 holder is missing, the positive one (without the \"NO-\") will be
18822 assumed to be present at the end of the template.
18823 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
18825 SNIPPETS-P indicates if this is run to create snippet images for HTML."
18826 (let (rpl (end ""))
18827 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
18828 (setq rpl (if (or (match-end 1) (not def-pkg))
18829 "" (org-latex-packages-to-string def-pkg snippets-p t))
18830 tpl (replace-match rpl t t tpl))
18831 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
18833 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
18834 (setq rpl (if (or (match-end 1) (not pkg))
18835 "" (org-latex-packages-to-string pkg snippets-p t))
18836 tpl (replace-match rpl t t tpl))
18839 (org-latex-packages-to-string pkg snippets-p)))))
18841 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
18842 (setq rpl (if (or (match-end 1) (not extra))
18843 "" (concat extra "\n"))
18844 tpl (replace-match rpl t t tpl))
18845 (if (and extra (string-match "\\S-" extra))
18846 (setq end (concat end "\n" extra))))
18848 (if (string-match "\\S-" end)
18849 (concat tpl "\n" end)
18852 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
18853 "Turn an alist of packages into a string with the \\usepackage macros."
18854 (setq pkg (mapconcat (lambda(p)
18857 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
18858 (format "%% Package %s omitted" (cadr p)))
18859 ((equal "" (car p))
18860 (format "\\usepackage{%s}" (cadr p)))
18862 (format "\\usepackage[%s]{%s}"
18863 (car p) (cadr p)))))
18866 (if newline (concat pkg "\n") pkg))
18868 (defun org-dvipng-color (attr)
18869 "Return a RGB color specification for dvipng."
18870 (apply 'format "rgb %s %s %s"
18871 (mapcar 'org-normalize-color
18872 (if (featurep 'xemacs)
18873 (color-rgb-components
18874 (face-property 'default
18875 (cond ((eq attr :foreground) 'foreground)
18876 ((eq attr :background) 'background))))
18877 (color-values (face-attribute 'default attr nil))))))
18879 (defun org-dvipng-color-format (color-name)
18880 "Convert COLOR-NAME to a RGB color value for dvipng."
18881 (apply 'format "rgb %s %s %s"
18882 (mapcar 'org-normalize-color
18883 (color-values color-name))))
18885 (defun org-latex-color (attr)
18886 "Return a RGB color for the LaTeX color package."
18887 (apply 'format "%s,%s,%s"
18888 (mapcar 'org-normalize-color
18889 (if (featurep 'xemacs)
18890 (color-rgb-components
18891 (face-property 'default
18892 (cond ((eq attr :foreground) 'foreground)
18893 ((eq attr :background) 'background))))
18894 (color-values (face-attribute 'default attr nil))))))
18896 (defun org-latex-color-format (color-name)
18897 "Convert COLOR-NAME to a RGB color value."
18898 (apply 'format "%s,%s,%s"
18899 (mapcar 'org-normalize-color
18900 (color-values color-name))))
18902 (defun org-normalize-color (value)
18903 "Return string to be used as color value for an RGB component."
18904 (format "%g" (/ value 65535.0)))
18910 (defvar org-inline-image-overlays nil)
18911 (make-variable-buffer-local 'org-inline-image-overlays)
18913 (defun org-toggle-inline-images (&optional include-linked)
18914 "Toggle the display of inline images.
18915 INCLUDE-LINKED is passed to `org-display-inline-images'."
18917 (if org-inline-image-overlays
18919 (org-remove-inline-images)
18920 (message "Inline image display turned off"))
18921 (org-display-inline-images include-linked)
18922 (if (and (org-called-interactively-p)
18923 org-inline-image-overlays)
18924 (message "%d images displayed inline"
18925 (length org-inline-image-overlays))
18926 (message "No images to display inline"))))
18928 (defun org-redisplay-inline-images ()
18929 "Refresh the display of inline images."
18931 (if (not org-inline-image-overlays)
18932 (org-toggle-inline-images)
18933 (org-toggle-inline-images)
18934 (org-toggle-inline-images)))
18936 (defun org-display-inline-images (&optional include-linked refresh beg end)
18937 "Display inline images.
18938 Normally only links without a description part are inlined, because this
18939 is how it will work for export. When INCLUDE-LINKED is set, also links
18940 with a description part will be inlined. This can be nice for a quick
18941 look at those images, but it does not reflect what exported files will look
18943 When REFRESH is set, refresh existing images between BEG and END.
18944 This will create new image displays only if necessary.
18945 BEG and END default to the buffer boundaries."
18947 (when (display-graphic-p)
18949 (org-remove-inline-images)
18950 (if (fboundp 'clear-image-cache) (clear-image-cache)))
18954 (setq beg (or beg (point-min)) end (or end (point-max)))
18956 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
18957 (substring (org-image-file-name-regexp) 0 -2)
18958 "\\)\\]" (if include-linked "" "\\]")))
18959 (case-fold-search t)
18960 old file ov img type attrwidth width)
18961 (while (re-search-forward re end t)
18962 (setq old (get-char-property-and-overlay (match-beginning 1)
18963 'org-image-overlay)
18964 file (expand-file-name
18965 (concat (or (match-string 3) "") (match-string 4))))
18966 (when (image-type-available-p 'imagemagick)
18967 (setq attrwidth (if (or (listp org-image-actual-width)
18968 (null org-image-actual-width))
18971 (when (re-search-backward
18972 "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
18974 (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
18975 (string-to-number (match-string 1))))))
18976 width (cond ((eq org-image-actual-width t) nil)
18977 ((null org-image-actual-width) attrwidth)
18978 ((numberp org-image-actual-width)
18979 org-image-actual-width)
18980 ((listp org-image-actual-width)
18981 (or attrwidth (car org-image-actual-width))))
18982 type (if width 'imagemagick)))
18983 (when (file-exists-p file)
18984 (if (and (car-safe old) refresh)
18985 (image-refresh (overlay-get (cdr old) 'display))
18986 (setq img (save-match-data (create-image file type nil :width width)))
18988 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
18989 (overlay-put ov 'display img)
18990 (overlay-put ov 'face 'default)
18991 (overlay-put ov 'org-image-overlay t)
18992 (overlay-put ov 'modification-hooks
18993 (list 'org-display-inline-remove-overlay))
18994 (push ov org-inline-image-overlays))))))))))
18996 (define-obsolete-function-alias
18997 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
18999 (defun org-display-inline-remove-overlay (ov after beg end &optional len)
19000 "Remove inline-display overlay if a corresponding region is modified."
19001 (let ((inhibit-modification-hooks t))
19002 (when (and ov after)
19003 (delete ov org-inline-image-overlays)
19004 (delete-overlay ov))))
19006 (defun org-remove-inline-images ()
19007 "Remove inline display of images."
19009 (mapc 'delete-overlay org-inline-image-overlays)
19010 (setq org-inline-image-overlays nil))
19014 ;; Outline functions from `outline-mode-prefix-map'
19015 ;; that can be remapped in Org:
19016 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
19017 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
19018 (define-key org-mode-map [remap outline-forward-same-level]
19019 'org-forward-heading-same-level)
19020 (define-key org-mode-map [remap outline-backward-same-level]
19021 'org-backward-heading-same-level)
19022 (define-key org-mode-map [remap show-branches]
19023 'org-kill-note-or-show-branches)
19024 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
19025 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
19026 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
19028 ;; Outline functions from `outline-mode-prefix-map' that can not
19029 ;; be remapped in Org:
19031 ;; - the column "key binding" shows whether the Outline function is still
19032 ;; available in Org mode on the same key that it has been bound to in
19034 ;; - "overridden": key used for a different functionality in Org mode
19035 ;; - else: key still bound to the same Outline function in Org mode
19037 ;; | Outline function | key binding | Org replacement |
19038 ;; |------------------------------------+-------------+-----------------------|
19039 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
19040 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
19041 ;; | `outline-up-heading' | `C-c C-u' | still same function |
19042 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
19043 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
19044 ;; | `show-entry' | overridden | no replacement |
19045 ;; | `show-children' | `C-c C-i' | visibility cycling |
19046 ;; | `show-branches' | `C-c C-k' | still same function |
19047 ;; | `show-subtree' | overridden | visibility cycling |
19048 ;; | `show-all' | overridden | no replacement |
19049 ;; | `hide-subtree' | overridden | visibility cycling |
19050 ;; | `hide-body' | overridden | no replacement |
19051 ;; | `hide-entry' | overridden | visibility cycling |
19052 ;; | `hide-leaves' | overridden | no replacement |
19053 ;; | `hide-sublevels' | overridden | no replacement |
19054 ;; | `hide-other' | overridden | no replacement |
19056 ;; Make `C-c C-x' a prefix key
19057 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
19059 ;; TAB key with modifiers
19060 (org-defkey org-mode-map "\C-i" 'org-cycle)
19061 (org-defkey org-mode-map [(tab)] 'org-cycle)
19062 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
19063 (org-defkey org-mode-map "\M-\t" #'pcomplete)
19064 ;; The following line is necessary under Suse GNU/Linux
19065 (unless (featurep 'xemacs)
19066 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
19067 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
19068 (define-key org-mode-map [backtab] 'org-shifttab)
19070 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
19071 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
19072 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
19074 ;; Cursor keys with modifiers
19075 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
19076 (org-defkey org-mode-map [(meta right)] 'org-metaright)
19077 (org-defkey org-mode-map [(meta up)] 'org-metaup)
19078 (org-defkey org-mode-map [(meta down)] 'org-metadown)
19080 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
19081 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
19082 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
19083 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
19085 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
19086 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
19087 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
19088 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
19090 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
19091 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
19092 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
19093 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
19096 (define-key org-mode-map org-babel-key-prefix org-babel-map)
19097 (mapc (lambda (pair)
19098 (define-key org-babel-map (car pair) (cdr pair)))
19099 org-babel-key-bindings)
19101 ;;; Extra keys for tty access.
19102 ;; We only set them when really needed because otherwise the
19103 ;; menus don't show the simple keys
19105 (when (or org-use-extra-keys
19106 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
19107 (not window-system))
19108 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
19109 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
19110 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
19111 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
19112 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
19113 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
19114 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
19115 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
19116 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
19117 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
19118 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
19119 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
19120 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
19121 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
19122 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
19123 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
19124 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
19125 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
19126 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
19127 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
19128 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
19129 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
19130 (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
19131 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
19132 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
19133 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
19134 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
19135 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
19137 ;; All the other keys
19139 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
19140 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
19141 (if (boundp 'narrow-map)
19142 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
19143 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
19144 (if (boundp 'narrow-map)
19145 (org-defkey narrow-map "b" 'org-narrow-to-block)
19146 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
19147 (if (boundp 'narrow-map)
19148 (org-defkey narrow-map "e" 'org-narrow-to-element)
19149 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
19150 (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
19151 (org-defkey org-mode-map "\M-}" 'org-forward-element)
19152 (org-defkey org-mode-map "\M-{" 'org-backward-element)
19153 (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
19154 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
19155 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
19156 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
19157 (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
19158 (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
19159 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
19160 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
19161 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
19162 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
19163 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
19164 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
19165 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
19166 (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
19167 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
19168 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
19169 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
19170 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
19171 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
19172 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
19173 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
19174 (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
19175 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
19176 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
19177 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
19178 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
19179 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
19180 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
19181 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
19182 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
19183 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
19184 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
19185 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
19186 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
19187 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
19188 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
19189 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
19190 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
19191 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
19192 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
19193 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
19194 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
19195 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
19196 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
19197 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
19198 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
19199 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
19200 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
19201 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
19202 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
19203 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
19204 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
19205 (org-defkey org-mode-map "\C-c^" 'org-sort)
19206 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
19207 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
19208 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
19209 (org-defkey org-mode-map [remap open-line] 'org-open-line)
19210 (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
19211 (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
19212 (org-defkey org-mode-map "\C-m" 'org-return)
19213 (org-defkey org-mode-map "\C-j" 'org-return-indent)
19214 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
19215 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
19216 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
19217 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
19218 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
19219 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
19220 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
19221 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
19222 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
19223 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
19224 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
19225 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
19226 (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
19227 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
19228 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
19229 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
19230 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
19231 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
19232 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
19233 (org-defkey org-mode-map "\M-h" 'org-mark-element)
19234 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
19235 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
19237 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
19238 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
19239 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
19241 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
19242 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
19243 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
19244 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
19245 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
19246 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
19247 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
19248 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
19249 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
19250 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
19251 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
19252 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
19253 (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
19254 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
19255 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
19256 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
19257 (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
19258 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
19259 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
19260 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
19261 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
19262 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
19263 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
19265 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
19266 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
19267 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
19268 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
19269 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
19271 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
19273 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
19275 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
19276 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
19278 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
19281 (when (featurep 'xemacs)
19282 (org-defkey org-mode-map 'button3 'popup-mode-menu))
19285 (defconst org-speed-commands-default
19287 ("Outline Navigation")
19288 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
19289 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
19290 ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
19291 ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
19292 ("F" . org-next-block)
19293 ("B" . org-previous-block)
19294 ("u" . (org-speed-move-safe 'outline-up-heading))
19296 ("g" . (org-refile t))
19297 ("Outline Visibility")
19299 ("C" . org-shifttab)
19300 (" " . org-display-outline-path)
19301 ("s" . org-narrow-to-subtree)
19302 ("=" . org-columns)
19303 ("Outline Structure Editing")
19304 ("U" . org-shiftmetaup)
19305 ("D" . org-shiftmetadown)
19306 ("r" . org-metaright)
19307 ("l" . org-metaleft)
19308 ("R" . org-shiftmetaright)
19309 ("L" . org-shiftmetaleft)
19310 ("i" . (progn (forward-char 1) (call-interactively
19311 'org-insert-heading-respect-content)))
19314 ("a" . org-archive-subtree-default-with-confirmation)
19315 ("@" . org-mark-subtree)
19316 ("#" . org-toggle-comment)
19318 ("I" . org-clock-in)
19319 ("O" . org-clock-out)
19320 ("Meta Data Editing")
19322 ("," . (org-priority))
19323 ("0" . (org-priority ?\ ))
19324 ("1" . (org-priority ?A))
19325 ("2" . (org-priority ?B))
19326 ("3" . (org-priority ?C))
19327 (":" . org-set-tags-command)
19328 ("e" . org-set-effort)
19329 ("E" . org-inc-effort)
19330 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
19331 (org-entry-put (point) "APPT_WARNTIME" m)))
19332 ("Agenda Views etc")
19334 ("/" . org-sparse-tree)
19336 ("o" . org-open-at-point)
19337 ("?" . org-speed-command-help)
19338 ("<" . (org-agenda-set-restriction-lock 'subtree))
19339 (">" . (org-agenda-remove-restriction-lock))
19341 "The default speed commands.")
19343 (defun org-print-speed-command (e)
19344 (if (> (length (car e)) 1)
19349 (princ (make-string (length (car e)) ?-))
19353 (if (symbolp (cdr e))
19354 (princ (symbol-name (cdr e)))
19358 (defun org-speed-command-help ()
19359 "Show the available speed commands."
19361 (if (not org-use-speed-commands)
19362 (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
19363 (with-output-to-temp-buffer "*Help*"
19364 (princ "User-defined Speed commands\n===========================\n")
19365 (mapc 'org-print-speed-command org-speed-commands-user)
19367 (princ "Built-in Speed commands\n=======================\n")
19368 (mapc 'org-print-speed-command org-speed-commands-default))
19369 (with-current-buffer "*Help*"
19370 (setq truncate-lines t))))
19372 (defun org-speed-move-safe (cmd)
19373 "Execute CMD, but make sure that the cursor always ends up in a headline.
19374 If not, return to the original position and throw an error."
19376 (let ((pos (point)))
19377 (call-interactively cmd)
19378 (unless (and (bolp) (org-at-heading-p))
19380 (error "Boundary reached while executing %s" cmd))))
19382 (defvar org-self-insert-command-undo-counter 0)
19384 (defvar org-table-auto-blank-field) ; defined in org-table.el
19385 (defvar org-speed-command nil)
19387 (define-obsolete-function-alias
19388 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
19390 (defun org-speed-command-activate (keys)
19391 "Hook for activating single-letter speed commands.
19392 `org-speed-commands-default' specifies a minimal command set.
19393 Use `org-speed-commands-user' for further customization."
19394 (when (or (and (bolp) (looking-at org-outline-regexp))
19395 (and (functionp org-use-speed-commands)
19396 (funcall org-use-speed-commands)))
19397 (cdr (assoc keys (append org-speed-commands-user
19398 org-speed-commands-default)))))
19400 (define-obsolete-function-alias
19401 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
19403 (defun org-babel-speed-command-activate (keys)
19404 "Hook for activating single-letter code block commands."
19405 (when (and (bolp) (looking-at org-babel-src-block-regexp))
19406 (cdr (assoc keys org-babel-key-bindings))))
19408 (defcustom org-speed-command-hook
19409 '(org-speed-command-default-hook org-babel-speed-command-hook)
19410 "Hook for activating speed commands at strategic locations.
19411 Hook functions are called in sequence until a valid handler is
19414 Each hook takes a single argument, a user-pressed command key
19415 which is also a `self-insert-command' from the global map.
19417 Within the hook, examine the cursor position and the command key
19418 and return nil or a valid handler as appropriate. Handler could
19419 be one of an interactive command, a function, or a form.
19421 Set `org-use-speed-commands' to non-nil value to enable this
19422 hook. The default setting is `org-speed-command-activate'."
19423 :group 'org-structure
19427 (defun org-self-insert-command (N)
19428 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
19429 If the cursor is in a table looking at whitespace, the whitespace is
19430 overwritten, and the table is not marked as requiring realignment."
19432 (org-check-before-invisible-edit 'insert)
19434 ((and org-use-speed-commands
19435 (setq org-speed-command
19436 (run-hook-with-args-until-success
19437 'org-speed-command-hook (this-command-keys))))
19439 ((commandp org-speed-command)
19440 (setq this-command org-speed-command)
19441 (call-interactively org-speed-command))
19442 ((functionp org-speed-command)
19443 (funcall org-speed-command))
19444 ((and org-speed-command (listp org-speed-command))
19445 (eval org-speed-command))
19446 (t (let (org-use-speed-commands)
19447 (call-interactively 'org-self-insert-command)))))
19451 ;; check if we blank the field, and if that triggers align
19452 (and (featurep 'org-table) org-table-auto-blank-field
19453 (member last-command
19454 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
19455 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
19456 ;; got extra space, this field does not determine column width
19457 (let (org-table-may-need-update) (org-table-blank-field))
19458 ;; no extra space, this field may determine column width
19459 (org-table-blank-field)))
19462 (looking-at "[^|\n]* |"))
19463 (let (org-table-may-need-update)
19464 (goto-char (1- (match-end 0)))
19465 (backward-delete-char 1)
19466 (goto-char (match-beginning 0))
19467 (self-insert-command N)))
19469 (setq org-table-may-need-update t)
19470 (self-insert-command N)
19471 (org-fix-tags-on-the-fly)
19472 (if org-self-insert-cluster-for-undo
19473 (if (not (eq last-command 'org-self-insert-command))
19474 (setq org-self-insert-command-undo-counter 1)
19475 (if (>= org-self-insert-command-undo-counter 20)
19476 (setq org-self-insert-command-undo-counter 1)
19477 (and (> org-self-insert-command-undo-counter 0)
19478 buffer-undo-list (listp buffer-undo-list)
19479 (not (cadr buffer-undo-list)) ; remove nil entry
19480 (setcdr buffer-undo-list (cddr buffer-undo-list)))
19481 (setq org-self-insert-command-undo-counter
19482 (1+ org-self-insert-command-undo-counter))))))))
19484 (defun org-check-before-invisible-edit (kind)
19485 "Check is editing if kind KIND would be dangerous with invisible text around.
19486 The detailed reaction depends on the user option `org-catch-invisible-edits'."
19487 ;; First, try to get out of here as quickly as possible, to reduce overhead
19488 (if (and org-catch-invisible-edits
19489 (or (not (boundp 'visible-mode)) (not visible-mode))
19490 (or (get-char-property (point) 'invisible)
19491 (get-char-property (max (point-min) (1- (point))) 'invisible)))
19492 ;; OK, we need to take a closer look
19493 (let* ((invisible-at-point (get-char-property (point) 'invisible))
19494 (invisible-before-point (if (bobp) nil (get-char-property
19495 (1- (point)) 'invisible)))
19496 (border-and-ok-direction
19498 ;; Check if we are acting predictably before invisible text
19499 (and invisible-at-point (not invisible-before-point)
19500 (memq kind '(insert delete-backward)))
19501 ;; Check if we are acting predictably after invisible text
19502 ;; This works not well, and I have turned it off. It seems
19503 ;; better to always show and stop after invisible text.
19504 ;; (and (not invisible-at-point) invisible-before-point
19505 ;; (memq kind '(insert delete)))
19507 (when (or (memq invisible-at-point '(outline org-hide-block t))
19508 (memq invisible-before-point '(outline org-hide-block t)))
19509 (if (eq org-catch-invisible-edits 'error)
19510 (user-error "Editing in invisible areas is prohibited, make them visible first"))
19511 (if (and org-custom-properties-overlays
19512 (y-or-n-p "Display invisible properties in this buffer? "))
19513 (org-toggle-custom-properties-visibility)
19514 ;; Make the area visible
19516 (if invisible-before-point
19517 (goto-char (previous-single-char-property-change
19518 (point) 'invisible)))
19521 ((eq org-catch-invisible-edits 'show)
19522 ;; That's it, we do the edit after showing
19524 "Unfolding invisible region around point before editing")
19526 ((and (eq org-catch-invisible-edits 'smart)
19527 border-and-ok-direction)
19528 (message "Unfolding invisible region around point before editing"))
19530 ;; Don't do the edit, make the user repeat it in full visibility
19531 (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
19533 (defun org-fix-tags-on-the-fly ()
19534 (when (and (equal (char-after (point-at-bol)) ?*)
19535 (org-at-heading-p))
19536 (org-align-tags-here org-tags-column)))
19538 (defun org-delete-backward-char (N)
19539 "Like `delete-backward-char', insert whitespace at field end in tables.
19540 When deleting backwards, in tables this function will insert whitespace in
19541 front of the next \"|\" separator, to keep the table aligned. The table will
19542 still be marked for re-alignment if the field did fill the entire column,
19543 because, in this case the deletion might narrow the column."
19546 (org-check-before-invisible-edit 'delete-backward)
19547 (if (and (org-table-p)
19549 (string-match "|" (buffer-substring (point-at-bol) (point)))
19550 (looking-at ".*?|"))
19551 (let ((pos (point))
19552 (noalign (looking-at "[^|\n\r]* |"))
19553 (c org-table-may-need-update))
19554 (backward-delete-char N)
19555 (if (not overwrite-mode)
19557 (skip-chars-forward "^|")
19559 (goto-char (1- pos))))
19560 ;; noalign: if there were two spaces at the end, this field
19561 ;; does not determine the width of the column.
19562 (if noalign (setq org-table-may-need-update c)))
19563 (backward-delete-char N)
19564 (org-fix-tags-on-the-fly))))
19566 (defun org-delete-char (N)
19567 "Like `delete-char', but insert whitespace at field end in tables.
19568 When deleting characters, in tables this function will insert whitespace in
19569 front of the next \"|\" separator, to keep the table aligned. The table will
19570 still be marked for re-alignment if the field did fill the entire column,
19571 because, in this case the deletion might narrow the column."
19574 (org-check-before-invisible-edit 'delete)
19575 (if (and (org-table-p)
19577 (not (= (char-after) ?|))
19579 (if (looking-at ".*?|")
19580 (let ((pos (point))
19581 (noalign (looking-at "[^|\n\r]* |"))
19582 (c org-table-may-need-update))
19584 (concat (substring (match-string 0) 1 -1) " |") nil t)
19586 ;; noalign: if there were two spaces at the end, this field
19587 ;; does not determine the width of the column.
19588 (if noalign (setq org-table-may-need-update c)))
19591 (org-fix-tags-on-the-fly))))
19593 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
19594 (put 'org-self-insert-command 'delete-selection
19596 (not (run-hook-with-args-until-success
19597 'self-insert-uses-region-functions))))
19598 (put 'orgtbl-self-insert-command 'delete-selection
19600 (not (run-hook-with-args-until-success
19601 'self-insert-uses-region-functions))))
19602 (put 'org-delete-char 'delete-selection 'supersede)
19603 (put 'org-delete-backward-char 'delete-selection 'supersede)
19604 (put 'org-yank 'delete-selection 'yank)
19606 ;; Make `flyspell-mode' delay after some commands
19607 (put 'org-self-insert-command 'flyspell-delayed t)
19608 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
19609 (put 'org-delete-char 'flyspell-delayed t)
19610 (put 'org-delete-backward-char 'flyspell-delayed t)
19612 ;; Make pabbrev-mode expand after org-mode commands
19613 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
19614 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
19616 (defun org-remap (map &rest commands)
19617 "In MAP, remap the functions given in COMMANDS.
19618 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
19621 (setq old (pop commands) new (pop commands))
19622 (if (fboundp 'command-remapping)
19623 (org-defkey map (vector 'remap old) new)
19624 (substitute-key-definition old new map global-map)))))
19626 (defun org-transpose-words ()
19627 "Transpose words for Org.
19628 This uses the `org-mode-transpose-word-syntax-table' syntax
19629 table, which interprets characters in `org-emphasis-alist' as
19630 word constituents."
19632 (with-syntax-table org-mode-transpose-word-syntax-table
19633 (call-interactively 'transpose-words)))
19634 (org-remap org-mode-map 'transpose-words 'org-transpose-words)
19636 (when (eq org-enable-table-editor 'optimized)
19637 ;; If the user wants maximum table support, we need to hijack
19638 ;; some standard editing functions
19639 (org-remap org-mode-map
19640 'self-insert-command 'org-self-insert-command
19641 'delete-char 'org-delete-char
19642 'delete-backward-char 'org-delete-backward-char)
19643 (org-defkey org-mode-map "|" 'org-force-self-insert))
19645 (defvar org-ctrl-c-ctrl-c-hook nil
19646 "Hook for functions attaching themselves to `C-c C-c'.
19648 This can be used to add additional functionality to the C-c C-c
19649 key which executes context-dependent commands. This hook is run
19650 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
19651 run after the last test.
19653 Each function will be called with no arguments. The function
19654 must check if the context is appropriate for it to act. If yes,
19655 it should do its thing and then return a non-nil value. If the
19656 context is wrong, just do nothing and return nil.")
19658 (defvar org-ctrl-c-ctrl-c-final-hook nil
19659 "Hook for functions attaching themselves to `C-c C-c'.
19661 This can be used to add additional functionality to the C-c C-c
19662 key which executes context-dependent commands. This hook is run
19663 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
19664 before the first test.
19666 Each function will be called with no arguments. The function
19667 must check if the context is appropriate for it to act. If yes,
19668 it should do its thing and then return a non-nil value. If the
19669 context is wrong, just do nothing and return nil.")
19671 (defvar org-tab-first-hook nil
19672 "Hook for functions to attach themselves to TAB.
19673 See `org-ctrl-c-ctrl-c-hook' for more information.
19674 This hook runs as the first action when TAB is pressed, even before
19675 `org-cycle' messes around with the `outline-regexp' to cater for
19676 inline tasks and plain list item folding.
19677 If any function in this hook returns t, any other actions that
19678 would have been caused by TAB (such as table field motion or visibility
19679 cycling) will not occur.")
19681 (defvar org-tab-after-check-for-table-hook nil
19682 "Hook for functions to attach themselves to TAB.
19683 See `org-ctrl-c-ctrl-c-hook' for more information.
19684 This hook runs after it has been established that the cursor is not in a
19685 table, but before checking if the cursor is in a headline or if global cycling
19687 If any function in this hook returns t, not other actions like visibility
19688 cycling will be done.")
19690 (defvar org-tab-after-check-for-cycling-hook nil
19691 "Hook for functions to attach themselves to TAB.
19692 See `org-ctrl-c-ctrl-c-hook' for more information.
19693 This hook runs after it has been established that not table field motion and
19694 not visibility should be done because of current context. This is probably
19695 the place where a package like yasnippets can hook in.")
19697 (defvar org-tab-before-tab-emulation-hook nil
19698 "Hook for functions to attach themselves to TAB.
19699 See `org-ctrl-c-ctrl-c-hook' for more information.
19700 This hook runs after every other options for TAB have been exhausted, but
19701 before indentation and \t insertion takes place.")
19703 (defvar org-metaleft-hook nil
19704 "Hook for functions attaching themselves to `M-left'.
19705 See `org-ctrl-c-ctrl-c-hook' for more information.")
19706 (defvar org-metaright-hook nil
19707 "Hook for functions attaching themselves to `M-right'.
19708 See `org-ctrl-c-ctrl-c-hook' for more information.")
19709 (defvar org-metaup-hook nil
19710 "Hook for functions attaching themselves to `M-up'.
19711 See `org-ctrl-c-ctrl-c-hook' for more information.")
19712 (defvar org-metadown-hook nil
19713 "Hook for functions attaching themselves to `M-down'.
19714 See `org-ctrl-c-ctrl-c-hook' for more information.")
19715 (defvar org-shiftmetaleft-hook nil
19716 "Hook for functions attaching themselves to `M-S-left'.
19717 See `org-ctrl-c-ctrl-c-hook' for more information.")
19718 (defvar org-shiftmetaright-hook nil
19719 "Hook for functions attaching themselves to `M-S-right'.
19720 See `org-ctrl-c-ctrl-c-hook' for more information.")
19721 (defvar org-shiftmetaup-hook nil
19722 "Hook for functions attaching themselves to `M-S-up'.
19723 See `org-ctrl-c-ctrl-c-hook' for more information.")
19724 (defvar org-shiftmetadown-hook nil
19725 "Hook for functions attaching themselves to `M-S-down'.
19726 See `org-ctrl-c-ctrl-c-hook' for more information.")
19727 (defvar org-metareturn-hook nil
19728 "Hook for functions attaching themselves to `M-RET'.
19729 See `org-ctrl-c-ctrl-c-hook' for more information.")
19730 (defvar org-shiftup-hook nil
19731 "Hook for functions attaching themselves to `S-up'.
19732 See `org-ctrl-c-ctrl-c-hook' for more information.")
19733 (defvar org-shiftup-final-hook nil
19734 "Hook for functions attaching themselves to `S-up'.
19735 This one runs after all other options except shift-select have been excluded.
19736 See `org-ctrl-c-ctrl-c-hook' for more information.")
19737 (defvar org-shiftdown-hook nil
19738 "Hook for functions attaching themselves to `S-down'.
19739 See `org-ctrl-c-ctrl-c-hook' for more information.")
19740 (defvar org-shiftdown-final-hook nil
19741 "Hook for functions attaching themselves to `S-down'.
19742 This one runs after all other options except shift-select have been excluded.
19743 See `org-ctrl-c-ctrl-c-hook' for more information.")
19744 (defvar org-shiftleft-hook nil
19745 "Hook for functions attaching themselves to `S-left'.
19746 See `org-ctrl-c-ctrl-c-hook' for more information.")
19747 (defvar org-shiftleft-final-hook nil
19748 "Hook for functions attaching themselves to `S-left'.
19749 This one runs after all other options except shift-select have been excluded.
19750 See `org-ctrl-c-ctrl-c-hook' for more information.")
19751 (defvar org-shiftright-hook nil
19752 "Hook for functions attaching themselves to `S-right'.
19753 See `org-ctrl-c-ctrl-c-hook' for more information.")
19754 (defvar org-shiftright-final-hook nil
19755 "Hook for functions attaching themselves to `S-right'.
19756 This one runs after all other options except shift-select have been excluded.
19757 See `org-ctrl-c-ctrl-c-hook' for more information.")
19759 (defun org-modifier-cursor-error ()
19760 "Throw an error, a modified cursor command was applied in wrong context."
19761 (user-error "This command is active in special context like tables, headlines or items"))
19763 (defun org-shiftselect-error ()
19764 "Throw an error because Shift-Cursor command was applied in wrong context."
19765 (if (and (boundp 'shift-select-mode) shift-select-mode)
19766 (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
19767 (user-error "This command works only in special context like headlines or timestamps")))
19769 (defun org-call-for-shift-select (cmd)
19770 (let ((this-command-keys-shift-translated t))
19771 (call-interactively cmd)))
19773 (defun org-shifttab (&optional arg)
19774 "Global visibility cycling or move to previous table field.
19775 Call `org-table-previous-field' within a table.
19776 When ARG is nil, cycle globally through visibility states.
19777 When ARG is a numeric prefix, show contents of this level."
19780 ((org-at-table-p) (call-interactively 'org-table-previous-field))
19782 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
19783 (message "Content view to level: %d" arg)
19784 (org-content (prefix-numeric-value arg2))
19785 (org-cycle-show-empty-lines t)
19786 (setq org-cycle-global-status 'overview)))
19787 (t (call-interactively 'org-global-cycle))))
19789 (defun org-shiftmetaleft ()
19790 "Promote subtree or delete table column.
19791 Calls `org-promote-subtree', `org-outdent-item-tree', or
19792 `org-table-delete-column', depending on context. See the
19793 individual commands for more information."
19796 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
19797 ((org-at-table-p) (call-interactively 'org-table-delete-column))
19798 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
19799 ((if (not (org-region-active-p)) (org-at-item-p)
19800 (save-excursion (goto-char (region-beginning))
19802 (call-interactively 'org-outdent-item-tree))
19803 (t (org-modifier-cursor-error))))
19805 (defun org-shiftmetaright ()
19806 "Demote subtree or insert table column.
19807 Calls `org-demote-subtree', `org-indent-item-tree', or
19808 `org-table-insert-column', depending on context. See the
19809 individual commands for more information."
19812 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
19813 ((org-at-table-p) (call-interactively 'org-table-insert-column))
19814 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
19815 ((if (not (org-region-active-p)) (org-at-item-p)
19816 (save-excursion (goto-char (region-beginning))
19818 (call-interactively 'org-indent-item-tree))
19819 (t (org-modifier-cursor-error))))
19821 (defun org-shiftmetaup (&optional arg)
19822 "Move subtree up or kill table row.
19823 Calls `org-move-subtree-up' or `org-table-kill-row' or
19824 `org-move-item-up' or `org-timestamp-up', depending on context.
19825 See the individual commands for more information."
19828 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
19829 ((org-at-table-p) (call-interactively 'org-table-kill-row))
19830 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
19831 ((org-at-item-p) (call-interactively 'org-move-item-up))
19832 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
19833 (call-interactively 'org-timestamp-up)))
19834 (t (call-interactively 'org-drag-line-backward))))
19836 (defun org-shiftmetadown (&optional arg)
19837 "Move subtree down or insert table row.
19838 Calls `org-move-subtree-down' or `org-table-insert-row' or
19839 `org-move-item-down' or `org-timestamp-up', depending on context.
19840 See the individual commands for more information."
19843 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
19844 ((org-at-table-p) (call-interactively 'org-table-insert-row))
19845 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
19846 ((org-at-item-p) (call-interactively 'org-move-item-down))
19847 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
19848 (call-interactively 'org-timestamp-down)))
19849 (t (call-interactively 'org-drag-line-forward))))
19851 (defsubst org-hidden-tree-error ()
19853 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
19855 (defun org-metaleft (&optional arg)
19856 "Promote heading or move table column to left.
19857 Calls `org-do-promote' or `org-table-move-column', depending on context.
19858 With no specific context, calls the Emacs default `backward-word'.
19859 See the individual commands for more information."
19862 ((run-hook-with-args-until-success 'org-metaleft-hook))
19863 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
19864 ((org-with-limited-levels
19865 (or (org-at-heading-p)
19866 (and (org-region-active-p)
19868 (goto-char (region-beginning))
19869 (org-at-heading-p)))))
19870 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
19871 (call-interactively 'org-do-promote))
19872 ;; At an inline task.
19873 ((org-at-heading-p)
19874 (call-interactively 'org-inlinetask-promote))
19875 ((or (org-at-item-p)
19876 (and (org-region-active-p)
19878 (goto-char (region-beginning))
19880 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
19881 (call-interactively 'org-outdent-item))
19882 (t (call-interactively 'backward-word))))
19884 (defun org-metaright (&optional arg)
19885 "Demote a subtree, a list item or move table column to right.
19886 In front of a drawer or a block keyword, indent it correctly.
19887 With no specific context, calls the Emacs default `forward-word'.
19888 See the individual commands for more information."
19891 ((run-hook-with-args-until-success 'org-metaright-hook))
19892 ((org-at-table-p) (call-interactively 'org-table-move-column))
19893 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
19894 ((org-at-block-p) (call-interactively 'org-indent-block))
19895 ((org-with-limited-levels
19896 (or (org-at-heading-p)
19897 (and (org-region-active-p)
19899 (goto-char (region-beginning))
19900 (org-at-heading-p)))))
19901 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
19902 (call-interactively 'org-do-demote))
19903 ;; At an inline task.
19904 ((org-at-heading-p)
19905 (call-interactively 'org-inlinetask-demote))
19906 ((or (org-at-item-p)
19907 (and (org-region-active-p)
19909 (goto-char (region-beginning))
19911 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
19912 (call-interactively 'org-indent-item))
19913 (t (call-interactively 'forward-word))))
19915 (defun org-check-for-hidden (what)
19916 "Check if there are hidden headlines/items in the current visual line.
19917 WHAT can be either `headlines' or `items'. If the current line is
19918 an outline or item heading and it has a folded subtree below it,
19919 this function returns t, nil otherwise."
19921 ((eq what 'headlines) org-outline-regexp-bol)
19922 ((eq what 'items) (org-item-beginning-re))
19923 (t (error "This should not happen"))))
19927 (unless (org-region-active-p)
19928 (setq beg (point-at-bol))
19929 (beginning-of-line 2)
19930 (while (and (not (eobp)) ;; this is like `next-line'
19931 (get-char-property (1- (point)) 'invisible))
19932 (beginning-of-line 2))
19935 (goto-char (point-at-eol))
19936 (setq end (max end (point)))
19937 (while (re-search-forward re end t)
19938 (if (get-char-property (match-beginning 0) 'invisible)
19942 (defun org-metaup (&optional arg)
19943 "Move subtree up or move table row up.
19944 Calls `org-move-subtree-up' or `org-table-move-row' or
19945 `org-move-item-up', depending on context. See the individual commands
19946 for more information."
19949 ((run-hook-with-args-until-success 'org-metaup-hook))
19950 ((org-region-active-p)
19951 (let* ((a (min (region-beginning) (region-end)))
19952 (b (1- (max (region-beginning) (region-end))))
19953 (c (save-excursion (goto-char a)
19954 (move-beginning-of-line 0)))
19955 (d (save-excursion (goto-char a)
19956 (move-end-of-line 0) (point))))
19957 (transpose-regions a b c d)
19959 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
19960 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
19961 ((org-at-item-p) (call-interactively 'org-move-item-up))
19962 (t (org-drag-element-backward))))
19964 (defun org-metadown (&optional arg)
19965 "Move subtree down or move table row down.
19966 Calls `org-move-subtree-down' or `org-table-move-row' or
19967 `org-move-item-down', depending on context. See the individual
19968 commands for more information."
19971 ((run-hook-with-args-until-success 'org-metadown-hook))
19972 ((org-region-active-p)
19973 (let* ((a (min (region-beginning) (region-end)))
19974 (b (max (region-beginning) (region-end)))
19975 (c (save-excursion (goto-char b)
19976 (move-beginning-of-line 1)))
19977 (d (save-excursion (goto-char b)
19978 (move-end-of-line 1) (1+ (point)))))
19979 (transpose-regions a b c d)
19981 ((org-at-table-p) (call-interactively 'org-table-move-row))
19982 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
19983 ((org-at-item-p) (call-interactively 'org-move-item-down))
19984 (t (org-drag-element-forward))))
19986 (defun org-shiftup (&optional arg)
19987 "Increase item in timestamp or increase priority of current headline.
19988 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
19989 depending on context. See the individual commands for more information."
19992 ((run-hook-with-args-until-success 'org-shiftup-hook))
19993 ((and org-support-shift-select (org-region-active-p))
19994 (org-call-for-shift-select 'previous-line))
19995 ((org-at-timestamp-p t)
19996 (call-interactively (if org-edit-timestamp-down-means-later
19997 'org-timestamp-down 'org-timestamp-up)))
19998 ((and (not (eq org-support-shift-select 'always))
19999 org-enable-priority-commands
20000 (org-at-heading-p))
20001 (call-interactively 'org-priority-up))
20002 ((and (not org-support-shift-select) (org-at-item-p))
20003 (call-interactively 'org-previous-item))
20004 ((org-clocktable-try-shift 'up arg))
20005 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
20006 (org-support-shift-select
20007 (org-call-for-shift-select 'previous-line))
20008 (t (org-shiftselect-error))))
20010 (defun org-shiftdown (&optional arg)
20011 "Decrease item in timestamp or decrease priority of current headline.
20012 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
20013 depending on context. See the individual commands for more information."
20016 ((run-hook-with-args-until-success 'org-shiftdown-hook))
20017 ((and org-support-shift-select (org-region-active-p))
20018 (org-call-for-shift-select 'next-line))
20019 ((org-at-timestamp-p t)
20020 (call-interactively (if org-edit-timestamp-down-means-later
20021 'org-timestamp-up 'org-timestamp-down)))
20022 ((and (not (eq org-support-shift-select 'always))
20023 org-enable-priority-commands
20024 (org-at-heading-p))
20025 (call-interactively 'org-priority-down))
20026 ((and (not org-support-shift-select) (org-at-item-p))
20027 (call-interactively 'org-next-item))
20028 ((org-clocktable-try-shift 'down arg))
20029 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
20030 (org-support-shift-select
20031 (org-call-for-shift-select 'next-line))
20032 (t (org-shiftselect-error))))
20034 (defun org-shiftright (&optional arg)
20035 "Cycle the thing at point or in the current line, depending on context.
20036 Depending on context, this does one of the following:
20038 - switch a timestamp at point one day into the future
20039 - on a headline, switch to the next TODO keyword.
20040 - on an item, switch entire list to the next bullet type
20041 - on a property line, switch to the next allowed value
20042 - on a clocktable definition line, move time block into the future"
20045 ((run-hook-with-args-until-success 'org-shiftright-hook))
20046 ((and org-support-shift-select (org-region-active-p))
20047 (org-call-for-shift-select 'forward-char))
20048 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
20049 ((and (not (eq org-support-shift-select 'always))
20050 (org-at-heading-p))
20051 (let ((org-inhibit-logging
20052 (not org-treat-S-cursor-todo-selection-as-state-change))
20053 (org-inhibit-blocking
20054 (not org-treat-S-cursor-todo-selection-as-state-change)))
20055 (org-call-with-arg 'org-todo 'right)))
20056 ((or (and org-support-shift-select
20057 (not (eq org-support-shift-select 'always))
20058 (org-at-item-bullet-p))
20059 (and (not org-support-shift-select) (org-at-item-p)))
20060 (org-call-with-arg 'org-cycle-list-bullet nil))
20061 ((and (not (eq org-support-shift-select 'always))
20062 (org-at-property-p))
20063 (call-interactively 'org-property-next-allowed-value))
20064 ((org-clocktable-try-shift 'right arg))
20065 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
20066 (org-support-shift-select
20067 (org-call-for-shift-select 'forward-char))
20068 (t (org-shiftselect-error))))
20070 (defun org-shiftleft (&optional arg)
20071 "Cycle the thing at point or in the current line, depending on context.
20072 Depending on context, this does one of the following:
20074 - switch a timestamp at point one day into the past
20075 - on a headline, switch to the previous TODO keyword.
20076 - on an item, switch entire list to the previous bullet type
20077 - on a property line, switch to the previous allowed value
20078 - on a clocktable definition line, move time block into the past"
20081 ((run-hook-with-args-until-success 'org-shiftleft-hook))
20082 ((and org-support-shift-select (org-region-active-p))
20083 (org-call-for-shift-select 'backward-char))
20084 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
20085 ((and (not (eq org-support-shift-select 'always))
20086 (org-at-heading-p))
20087 (let ((org-inhibit-logging
20088 (not org-treat-S-cursor-todo-selection-as-state-change))
20089 (org-inhibit-blocking
20090 (not org-treat-S-cursor-todo-selection-as-state-change)))
20091 (org-call-with-arg 'org-todo 'left)))
20092 ((or (and org-support-shift-select
20093 (not (eq org-support-shift-select 'always))
20094 (org-at-item-bullet-p))
20095 (and (not org-support-shift-select) (org-at-item-p)))
20096 (org-call-with-arg 'org-cycle-list-bullet 'previous))
20097 ((and (not (eq org-support-shift-select 'always))
20098 (org-at-property-p))
20099 (call-interactively 'org-property-previous-allowed-value))
20100 ((org-clocktable-try-shift 'left arg))
20101 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
20102 (org-support-shift-select
20103 (org-call-for-shift-select 'backward-char))
20104 (t (org-shiftselect-error))))
20106 (defun org-shiftcontrolright ()
20107 "Switch to next TODO set."
20110 ((and org-support-shift-select (org-region-active-p))
20111 (org-call-for-shift-select 'forward-word))
20112 ((and (not (eq org-support-shift-select 'always))
20113 (org-at-heading-p))
20114 (org-call-with-arg 'org-todo 'nextset))
20115 (org-support-shift-select
20116 (org-call-for-shift-select 'forward-word))
20117 (t (org-shiftselect-error))))
20119 (defun org-shiftcontrolleft ()
20120 "Switch to previous TODO set."
20123 ((and org-support-shift-select (org-region-active-p))
20124 (org-call-for-shift-select 'backward-word))
20125 ((and (not (eq org-support-shift-select 'always))
20126 (org-at-heading-p))
20127 (org-call-with-arg 'org-todo 'previousset))
20128 (org-support-shift-select
20129 (org-call-for-shift-select 'backward-word))
20130 (t (org-shiftselect-error))))
20132 (defun org-shiftcontrolup (&optional n)
20133 "Change timestamps synchronously up in CLOCK log lines.
20134 Optional argument N tells to change by that many units."
20136 (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
20137 (let (org-support-shift-select)
20138 (org-clock-timestamps-up n))
20139 (user-error "Not at a clock log")))
20141 (defun org-shiftcontroldown (&optional n)
20142 "Change timestamps synchronously down in CLOCK log lines.
20143 Optional argument N tells to change by that many units."
20145 (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
20146 (let (org-support-shift-select)
20147 (org-clock-timestamps-down n))
20148 (user-error "Not at a clock log")))
20150 (defun org-ctrl-c-ret ()
20151 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
20154 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
20155 (t (call-interactively 'org-insert-heading))))
20157 (defun org-find-visible ()
20159 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
20160 (get-char-property s 'invisible)))
20162 (defun org-find-invisible ()
20164 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
20165 (not (get-char-property s 'invisible))))
20168 (defun org-copy-visible (beg end)
20169 "Copy the visible parts of the region."
20174 (narrow-to-region beg end)
20175 (setq s (goto-char (point-min)))
20176 (while (not (= (point) (point-max)))
20177 (goto-char (org-find-invisible))
20178 (push (buffer-substring s (point)) snippets)
20179 (setq s (goto-char (org-find-visible))))))
20180 (kill-new (apply 'concat (nreverse snippets)))))
20182 (defun org-copy-special ()
20183 "Copy region in table or copy current subtree.
20184 Calls `org-table-copy' or `org-copy-subtree', depending on context.
20185 See the individual commands for more information."
20187 (call-interactively
20188 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
20190 (defun org-cut-special ()
20191 "Cut region in table or cut current subtree.
20192 Calls `org-table-copy' or `org-cut-subtree', depending on context.
20193 See the individual commands for more information."
20195 (call-interactively
20196 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
20198 (defun org-paste-special (arg)
20199 "Paste rectangular region into table, or past subtree relative to level.
20200 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
20201 See the individual commands for more information."
20203 (if (org-at-table-p)
20204 (org-table-paste-rectangle)
20205 (org-paste-subtree arg)))
20207 (defsubst org-in-fixed-width-region-p ()
20208 "Is point in a fixed-width region?"
20210 (eq 'fixed-width (org-element-type (org-element-at-point)))))
20212 (defun org-edit-special (&optional arg)
20213 "Call a special editor for the element at point.
20214 When at a table, call the formula editor with `org-table-edit-formulas'.
20215 When in a source code block, call `org-edit-src-code'.
20216 When in a fixed-width region, call `org-edit-fixed-width-region'.
20217 When at an #+INCLUDE keyword, visit the included file.
20218 On a link, call `ffap' to visit the link at point.
20219 Otherwise, return a user error."
20221 (let ((element (org-element-at-point)))
20222 (assert (not buffer-read-only) nil
20223 "Buffer is read-only: %s" (buffer-name))
20224 (case (org-element-type element)
20226 (if (not arg) (org-edit-src-code)
20227 (let* ((info (org-babel-get-src-block-info))
20228 (lang (nth 0 info))
20229 (params (nth 2 info))
20230 (session (cdr (assq :session params))))
20231 (if (not session) (org-edit-src-code)
20232 ;; At a src-block with a session and function called with
20233 ;; an ARG: switch to the buffer related to the inferior
20236 (funcall (intern (concat "org-babel-prep-session:" lang))
20237 session params))))))
20239 (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
20241 (org-remove-double-quotes
20242 (car (org-split-string (org-element-property :value element)))))
20243 (user-error "No special environment to edit here")))
20245 (if (eq (org-element-property :type element) 'table.el)
20246 (org-edit-src-code)
20247 (call-interactively 'org-table-edit-formulas)))
20248 ;; Only Org tables contain `table-row' type elements.
20249 (table-row (call-interactively 'org-table-edit-formulas))
20250 ((example-block export-block) (org-edit-src-code))
20251 (fixed-width (org-edit-fixed-width-region))
20253 ;; No notable element at point. Though, we may be at a link,
20254 ;; which is an object. Thus, scan deeper.
20255 (if (eq (org-element-type (org-element-context element)) 'link)
20256 (call-interactively 'ffap)
20257 (user-error "No special environment to edit here"))))))
20259 (defvar org-table-coordinate-overlays) ; defined in org-table.el
20260 (defun org-ctrl-c-ctrl-c (&optional arg)
20261 "Set tags in headline, or update according to changed information at point.
20263 This command does many different things, depending on context:
20265 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
20266 this is what we do.
20268 - If the cursor is on a statistics cookie, update it.
20270 - If the cursor is in a headline, prompt for tags and insert them
20271 into the current line, aligned to `org-tags-column'. When called
20272 with prefix arg, realign all tags in the current buffer.
20274 - If the cursor is in one of the special #+KEYWORD lines, this
20275 triggers scanning the buffer for these lines and updating the
20278 - If the cursor is inside a table, realign the table. This command
20279 works even if the automatic table editor has been turned off.
20281 - If the cursor is on a #+TBLFM line, re-apply the formulas to
20284 - If the cursor is at a footnote reference or definition, jump to
20285 the corresponding definition or references, respectively.
20287 - If the cursor is a the beginning of a dynamic block, update it.
20289 - If the current buffer is a capture buffer, close note and file it.
20291 - If the cursor is on a <<<target>>>, update radio targets and
20292 corresponding links in this buffer.
20294 - If the cursor is on a numbered item in a plain list, renumber the
20297 - If the cursor is on a checkbox, toggle it.
20299 - If the cursor is on a code block, evaluate it. The variable
20300 `org-confirm-babel-evaluate' can be used to control prompting
20301 before code block evaluation, by default every code block
20302 evaluation requires confirmation. Code block evaluation can be
20303 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
20306 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
20307 org-occur-highlights
20308 org-latex-fragment-image-overlays)
20309 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
20310 (org-remove-occur-highlights)
20311 (org-remove-latex-fragment-image-overlays)
20312 (message "Temporary highlights/overlays removed from current buffer"))
20313 ((and (local-variable-p 'org-finish-function (current-buffer))
20314 (fboundp org-finish-function))
20315 (funcall org-finish-function))
20316 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
20318 (let* ((context (org-element-context)) (type (org-element-type context)))
20319 ;; Test if point is within a blank line.
20320 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
20321 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
20322 (user-error "C-c C-c can do nothing useful at this location"))
20324 ;; When at a link, act according to the parent instead.
20325 (link (setq context (org-element-property :parent context))
20326 (setq type (org-element-type context)))
20327 ;; Unsupported object types: refer to the first supported
20328 ;; element or object containing it.
20329 ((bold code entity export-snippet inline-babel-call inline-src-block
20330 italic latex-fragment line-break macro strike-through subscript
20331 superscript underline verbatim)
20332 (while (and (setq context (org-element-property :parent context))
20333 (not (memq (setq type (org-element-type context))
20334 '(radio-target paragraph verse-block
20336 ;; For convenience: at the first line of a paragraph on the
20337 ;; same line as an item, apply function on that item instead.
20338 (when (eq type 'paragraph)
20339 (let ((parent (org-element-property :parent context)))
20340 (when (and (eq (org-element-type parent) 'item)
20341 (= (point-at-bol) (org-element-property :begin parent)))
20342 (setq context parent type 'item))))
20343 ;; Act according to type of element or object at point.
20345 (clock (org-clock-update-time-maybe))
20348 (goto-char (org-element-property :post-affiliated context))
20349 (org-update-dblock)))
20350 (footnote-definition
20351 (goto-char (org-element-property :post-affiliated context))
20352 (call-interactively 'org-footnote-action))
20353 (footnote-reference (call-interactively 'org-footnote-action))
20354 ((headline inlinetask)
20355 (save-excursion (goto-char (org-element-property :begin context))
20356 (call-interactively 'org-set-tags)))
20358 ;; At an item: a double C-u set checkbox to "[-]"
20359 ;; unconditionally, whereas a single one will toggle its
20360 ;; presence. Without a universal argument, if the item
20361 ;; has a checkbox, toggle it. Otherwise repair the list.
20362 (let* ((box (org-element-property :checkbox context))
20363 (struct (org-element-property :structure context))
20364 (old-struct (copy-tree struct))
20365 (parents (org-list-parents-alist struct))
20366 (prevs (org-list-prevs-alist struct))
20367 (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
20368 (org-list-set-checkbox
20369 (org-element-property :begin context) struct
20370 (cond ((equal arg '(16)) "[-]")
20371 ((and (not box) (equal arg '(4))) "[ ]")
20372 ((or (not box) (equal arg '(4))) nil)
20373 ((eq box 'on) "[ ]")
20375 ;; Mimic `org-list-write-struct' but with grabbing
20376 ;; a return value from `org-list-struct-fix-box'.
20377 (org-list-struct-fix-ind struct parents 2)
20378 (org-list-struct-fix-item-end struct)
20379 (org-list-struct-fix-bul struct prevs)
20380 (org-list-struct-fix-ind struct parents)
20382 (org-list-struct-fix-box struct parents prevs orderedp)))
20383 (if (and box (equal struct old-struct))
20384 (if (equal arg '(16))
20385 (message "Checkboxes already reset")
20386 (user-error "Cannot toggle this checkbox: %s"
20388 "all subitems checked"
20389 "unchecked subitems")))
20390 (org-list-struct-apply-struct struct old-struct)
20391 (org-update-checkbox-count-maybe))
20393 (message "Checkboxes were removed due to empty box at line %d"
20394 (org-current-line block-item))))))
20396 (let ((org-inhibit-startup-visibility-stuff t)
20397 (org-startup-align-all-tables nil))
20398 (when (boundp 'org-table-coordinate-overlays)
20399 (mapc 'delete-overlay org-table-coordinate-overlays)
20400 (setq org-table-coordinate-overlays nil))
20401 (org-save-outline-visibility 'use-markers (org-mode-restart)))
20402 (message "Local setup has been refreshed"))
20404 ;; At a plain list, with a double C-u argument, set
20405 ;; checkboxes of each item to "[-]", whereas a single one
20406 ;; will toggle their presence according to the state of the
20407 ;; first item in the list. Without an argument, repair the
20409 (let* ((begin (org-element-property :contents-begin context))
20410 (beginm (move-marker (make-marker) begin))
20411 (struct (org-element-property :structure context))
20412 (old-struct (copy-tree struct))
20413 (first-box (save-excursion
20415 (looking-at org-list-full-item-re)
20416 (match-string-no-properties 3)))
20417 (new-box (cond ((equal arg '(16)) "[-]")
20418 ((equal arg '(4)) (unless first-box "[ ]"))
20419 ((equal first-box "[X]") "[ ]")
20423 (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
20424 (org-list-get-all-items
20425 begin struct (org-list-prevs-alist struct))))
20426 ((and first-box (eq (point) begin))
20427 ;; For convenience, when point is at bol on the first
20428 ;; item of the list and no argument is provided, simply
20429 ;; toggle checkbox of that item, if any.
20430 (org-list-set-checkbox begin struct new-box)))
20431 (org-list-write-struct
20432 struct (org-list-parents-alist struct) old-struct)
20433 (org-update-checkbox-count-maybe)
20434 (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
20435 ((property-drawer node-property)
20436 (call-interactively 'org-property-action))
20437 ((radio-target target)
20438 (call-interactively 'org-update-radio-target-regexp))
20440 (call-interactively 'org-update-statistics-cookies))
20441 ((table table-cell table-row)
20442 ;; At a table, recalculate every field and align it. Also
20443 ;; send the table if necessary. If the table has
20444 ;; a `table.el' type, just give up. At a table row or
20445 ;; cell, maybe recalculate line but always align table.
20446 (if (eq (org-element-property :type context) 'table.el)
20447 (message "%s" "Use C-c ' to edit table.el tables")
20448 (let ((org-enable-table-editor t))
20449 (if (or (eq type 'table)
20450 ;; Check if point is at a TBLFM line.
20451 (and (eq type 'table-row)
20452 (= (point) (org-element-property :end context))))
20454 (if (org-at-TBLFM-p)
20455 (progn (require 'org-table)
20456 (org-table-calc-current-TBLFM))
20457 (goto-char (org-element-property :contents-begin context))
20458 (org-call-with-arg 'org-table-recalculate (or arg t))
20459 (orgtbl-send-table 'maybe)))
20460 (org-table-maybe-eval-formula)
20461 (cond (arg (call-interactively 'org-table-recalculate))
20462 ((org-table-maybe-recalculate-line))
20463 (t (org-table-align)))))))
20464 (timestamp (org-timestamp-change 0 'day))
20466 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
20468 "C-c C-c can do nothing useful at this location")))))))))
20470 (defun org-mode-restart ()
20472 (let ((indent-status (org-bound-and-true-p org-indent-mode)))
20473 (funcall major-mode)
20474 (hack-local-variables)
20475 (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
20476 (org-indent-mode -1)))
20477 (message "%s restarted" major-mode))
20479 (defun org-kill-note-or-show-branches ()
20480 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
20482 (if (not org-finish-function)
20485 (call-interactively 'show-branches))
20486 (let ((org-note-abort t))
20487 (funcall org-finish-function))))
20489 (defun org-open-line (n)
20490 "Insert a new row in tables, call `open-line' elsewhere.
20491 If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
20494 ((not org-special-ctrl-o)
20497 (org-table-insert-row))
20501 (defun org-return (&optional indent)
20502 "Goto next table row or insert a newline.
20503 Calls `org-table-next-row' or `newline', depending on context.
20504 See the individual commands for more information."
20508 ((or (bobp) (org-in-src-block-p))
20509 (if indent (newline-and-indent) (newline)))
20511 (org-table-justify-field-maybe)
20512 (call-interactively 'org-table-next-row))
20513 ;; when `newline-and-indent' is called within a list, make sure
20514 ;; text moved stays inside the item.
20515 ((and (org-in-item-p) indent)
20516 (if (and (org-at-item-p) (>= (point) (match-end 0)))
20518 (save-match-data (newline))
20519 (org-indent-line-to (length (match-string 0))))
20520 (let ((ind (org-get-indentation)))
20522 (if (org-looking-back org-list-end-re)
20524 (org-indent-line-to ind)))))
20525 ((and org-return-follows-link
20526 (org-at-timestamp-p t)
20527 (not (eq org-ts-what 'after)))
20528 (org-follow-timestamp-link))
20529 ((and org-return-follows-link
20530 (let ((tprop (get-text-property (point) 'face)))
20531 (or (eq tprop 'org-link)
20532 (and (listp tprop) (memq 'org-link tprop)))))
20533 (call-interactively 'org-open-at-point))
20534 ((and (org-at-heading-p)
20536 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
20540 (t (if indent (newline-and-indent) (newline))))))
20542 (defun org-return-indent ()
20543 "Goto next table row or insert a newline and indent.
20544 Calls `org-table-next-row' or `newline-and-indent', depending on
20545 context. See the individual commands for more information."
20549 (defun org-ctrl-c-star ()
20550 "Compute table, or change heading status of lines.
20551 Calls `org-table-recalculate' or `org-toggle-heading',
20552 depending on context."
20556 (call-interactively 'org-table-recalculate))
20558 ;; Convert all lines in region to list items
20559 (call-interactively 'org-toggle-heading))))
20561 (defun org-ctrl-c-minus ()
20562 "Insert separator line in table or modify bullet status of line.
20563 Also turns a plain line or a region of lines into list items.
20564 Calls `org-table-insert-hline', `org-toggle-item', or
20565 `org-cycle-list-bullet', depending on context."
20569 (call-interactively 'org-table-insert-hline))
20570 ((org-region-active-p)
20571 (call-interactively 'org-toggle-item))
20573 (call-interactively 'org-cycle-list-bullet))
20575 (call-interactively 'org-toggle-item))))
20577 (defun org-toggle-item (arg)
20578 "Convert headings or normal lines to items, items to normal lines.
20579 If there is no active region, only the current line is considered.
20581 If the first non blank line in the region is a headline, convert
20582 all headlines to items, shifting text accordingly.
20584 If it is an item, convert all items to normal lines.
20586 If it is normal text, change region into a list of items.
20587 With a prefix argument ARG, change the region in a single item."
20591 ;; Shift text in current section to IND, from point to END.
20592 ;; The function leaves point to END line.
20594 (let ((min-i 1000) (end (copy-marker end)))
20595 ;; First determine the minimum indentation (MIN-I) of
20599 (while (< (point) end)
20600 (let ((i (org-get-indentation)))
20602 ;; Skip blank lines and inline tasks.
20603 ((looking-at "^[ \t]*$"))
20604 ((looking-at org-outline-regexp-bol))
20605 ;; We can't find less than 0 indentation.
20606 ((zerop i) (throw 'exit (setq min-i 0)))
20607 ((< i min-i) (setq min-i i))))
20609 ;; Then indent each line so that a line indented to
20610 ;; MIN-I becomes indented to IND. Ignore blank lines
20611 ;; and inline tasks in the process.
20612 (let ((delta (- ind min-i)))
20613 (while (< (point) end)
20614 (unless (or (looking-at "^[ \t]*$")
20615 (looking-at org-outline-regexp-bol))
20616 (org-indent-line-to (+ (org-get-indentation) delta)))
20617 (forward-line)))))))
20620 ;; Return beginning of first non-blank line, starting from
20625 (skip-chars-forward " \r\t\n")
20628 ;; Determine boundaries of changes.
20629 (if (org-region-active-p)
20630 (setq beg (funcall skip-blanks (region-beginning))
20631 end (copy-marker (region-end)))
20632 (setq beg (funcall skip-blanks (point-at-bol))
20633 end (copy-marker (point-at-eol))))
20634 ;; Depending on the starting line, choose an action on the text
20635 ;; between BEG and END.
20636 (org-with-limited-levels
20640 ;; Case 1. Start at an item: de-itemize. Note that it only
20641 ;; happens when a region is active: `org-ctrl-c-minus'
20642 ;; would call `org-cycle-list-bullet' otherwise.
20644 (while (< (point) end)
20645 (when (org-at-item-p)
20646 (skip-chars-forward " \t")
20647 (delete-region (point) (match-end 0)))
20649 ;; Case 2. Start at an heading: convert to items.
20650 ((org-at-heading-p)
20651 (let* ((bul (org-list-bullet-string "-"))
20652 (bul-len (length bul))
20653 ;; Indentation of the first heading. It should be
20654 ;; relative to the indentation of its parent, if any.
20655 (start-ind (save-excursion
20657 ((not org-adapt-indentation) 0)
20658 ((not (outline-previous-heading)) 0)
20659 (t (length (match-string 0))))))
20660 ;; Level of first heading. Further headings will be
20661 ;; compared to it to determine hierarchy in the list.
20662 (ref-level (org-reduced-level (org-outline-level))))
20663 (while (< (point) end)
20664 (let* ((level (org-reduced-level (org-outline-level)))
20665 (delta (max 0 (- level ref-level))))
20666 ;; If current headline is less indented than the first
20667 ;; one, set it as reference, in order to preserve
20669 (when (< level ref-level) (setq ref-level level))
20670 (replace-match bul t t)
20671 (org-indent-line-to (+ start-ind (* delta bul-len)))
20672 ;; Ensure all text down to END (or SECTION-END) belongs
20673 ;; to the newly created item.
20674 (let ((section-end (save-excursion
20675 (or (outline-next-heading) (point)))))
20677 (funcall shift-text
20678 (+ start-ind (* (1+ delta) bul-len))
20679 (min end section-end)))))))
20680 ;; Case 3. Normal line with ARG: make the first line of region
20681 ;; an item, and shift indentation of others lines to
20682 ;; set them as item's body.
20683 (arg (let* ((bul (org-list-bullet-string "-"))
20684 (bul-len (length bul))
20685 (ref-ind (org-get-indentation)))
20686 (skip-chars-forward " \t")
20689 (while (< (point) end)
20690 ;; Ensure that lines less indented than first one
20691 ;; still get included in item body.
20692 (funcall shift-text
20693 (+ ref-ind bul-len)
20694 (min end (save-excursion (or (outline-next-heading)
20697 ;; Case 4. Normal line without ARG: turn each non-item line
20700 (while (< (point) end)
20701 (unless (or (org-at-heading-p) (org-at-item-p))
20702 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
20704 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
20705 (forward-line))))))))
20707 (defun org-toggle-heading (&optional nstars)
20708 "Convert headings to normal text, or items or text to headings.
20709 If there is no active region, only convert the current line.
20711 With a \\[universal-argument] prefix, convert the whole list at
20712 point into heading.
20716 - If the first non blank line is a headline, remove the stars
20717 from all headlines in the region.
20719 - If it is a normal line, turn each and every normal line (i.e.,
20720 not an heading or an item) in the region into headings. If you
20721 want to convert only the first line of this region, use one
20722 universal prefix argument.
20724 - If it is a plain list item, turn all plain list items into headings.
20726 When converting a line into a heading, the number of stars is chosen
20727 such that the lines become children of the current entry. However,
20728 when a numeric prefix argument is given, its value determines the
20729 number of stars to add."
20733 ;; Return beginning of first non-blank line, starting from
20738 (while (org-at-comment-p) (forward-line))
20739 (skip-chars-forward " \r\t\n")
20742 ;; Determine boundaries of changes. If a universal prefix has
20743 ;; been given, put the list in a region. If region ends at a bol,
20744 ;; do not consider the last line to be in the region.
20746 (when (and current-prefix-arg (org-at-item-p))
20747 (if (listp current-prefix-arg) (setq current-prefix-arg 1))
20748 (org-mark-element))
20750 (if (org-region-active-p)
20751 (setq beg (funcall skip-blanks (region-beginning))
20752 end (copy-marker (save-excursion
20753 (goto-char (region-end))
20754 (if (bolp) (point) (point-at-eol)))))
20755 (setq beg (funcall skip-blanks (point-at-bol))
20756 end (copy-marker (point-at-eol))))
20757 ;; Ensure inline tasks don't count as headings.
20758 (org-with-limited-levels
20762 ;; Case 1. Started at an heading: de-star headings.
20763 ((org-at-heading-p)
20764 (while (< (point) end)
20765 (when (org-at-heading-p t)
20766 (looking-at org-outline-regexp) (replace-match "")
20769 ;; Case 2. Started at an item: change items into headlines.
20770 ;; One star will be added by `org-list-to-subtree'.
20772 (let* ((stars (make-string
20773 ;; subtract the star that will be added again by
20774 ;; `org-list-to-subtree'
20775 (if (numberp nstars) (1- nstars)
20776 (or (org-current-level) 0))
20779 (cond (nstars "") ; stars from prefix only
20780 ((equal stars "") "") ; before first heading
20781 (org-odd-levels-only "*") ; inside heading, odd
20782 (t "")))) ; inside heading, oddeven
20783 (while (< (point) end)
20784 (when (org-at-item-p)
20785 ;; Pay attention to cases when region ends before list.
20786 (let* ((struct (org-list-struct))
20787 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
20789 (narrow-to-region (point) list-end)
20791 (org-list-to-subtree
20792 (org-list-parse-list t)
20793 `(:istart (concat ',stars ',add-stars (funcall get-stars depth))
20794 :icount (concat ',stars ',add-stars (funcall get-stars depth)))))))
20797 ;; Case 3. Started at normal text: make every line an heading,
20798 ;; skipping headlines and items.
20801 (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
20803 (cond (nstars "") ; stars from prefix only
20804 ((equal stars "") "*") ; before first heading
20805 (org-odd-levels-only "**") ; inside heading, odd
20806 (t "*"))) ; inside heading, oddeven
20807 (rpl (concat stars add-stars " "))
20808 (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
20809 (while (< (point) (if (equal nstars '(4)) lend end))
20810 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
20811 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
20812 (replace-match (concat rpl (match-string 2))) (setq toggled t))
20813 (forward-line)))))))
20814 (unless toggled (message "Cannot toggle heading from here"))))
20816 (defun org-meta-return (&optional _arg)
20817 "Insert a new heading or wrap a region in a table.
20818 Calls `org-insert-heading' or `org-table-wrap-region', depending
20819 on context. See the individual commands for more information."
20821 (org-check-before-invisible-edit 'insert)
20822 (or (run-hook-with-args-until-success 'org-metareturn-hook)
20823 (let* ((element (org-element-at-point))
20824 (type (org-element-type element)))
20825 (when (eq type 'table-row)
20826 (setq element (org-element-property :parent element))
20827 (setq type 'table))
20828 (if (and (eq type 'table)
20829 (eq (org-element-property :type element) 'org)
20830 (>= (point) (org-element-property :contents-begin element))
20831 (< (point) (org-element-property :contents-end element)))
20832 (call-interactively 'org-table-wrap-region)
20833 (call-interactively 'org-insert-heading)))))
20837 (defsubst org-in-subtree-not-table-p ()
20838 "Are we in a subtree and not in a table?"
20839 (and (not (org-before-first-heading-p))
20840 (not (org-at-table-p))))
20842 ;; Define the Org-mode menus
20843 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
20845 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
20846 ["Next Field" org-cycle (org-at-table-p)]
20847 ["Previous Field" org-shifttab (org-at-table-p)]
20848 ["Next Row" org-return (org-at-table-p)]
20850 ["Blank Field" org-table-blank-field (org-at-table-p)]
20851 ["Edit Field" org-table-edit-field (org-at-table-p)]
20852 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
20855 ["Move Column Left" org-metaleft (org-at-table-p)]
20856 ["Move Column Right" org-metaright (org-at-table-p)]
20857 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
20858 ["Insert Column" org-shiftmetaright (org-at-table-p)])
20860 ["Move Row Up" org-metaup (org-at-table-p)]
20861 ["Move Row Down" org-metadown (org-at-table-p)]
20862 ["Delete Row" org-shiftmetaup (org-at-table-p)]
20863 ["Insert Row" org-shiftmetadown (org-at-table-p)]
20864 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
20866 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
20868 ["Copy Rectangle" org-copy-special (org-at-table-p)]
20869 ["Cut Rectangle" org-cut-special (org-at-table-p)]
20870 ["Paste Rectangle" org-paste-special (org-at-table-p)]
20871 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
20874 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
20875 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
20876 ["Edit Formulas" org-edit-special (org-at-table-p)]
20878 ["Recalculate line" org-table-recalculate (org-at-table-p)]
20879 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
20880 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
20882 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
20884 ["Sum Column/Rectangle" org-table-sum
20885 (or (org-at-table-p) (org-region-active-p))]
20886 ["Which Column?" org-table-current-column (org-at-table-p)])
20888 org-table-toggle-formula-debugger
20889 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
20890 ["Show Col/Row Numbers"
20891 org-table-toggle-coordinate-overlays
20893 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
20895 ["Create" org-table-create (and (not (org-at-table-p))
20896 org-enable-table-editor)]
20897 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
20898 ["Import from File" org-table-import (not (org-at-table-p))]
20899 ["Export to File" org-table-export (org-at-table-p)]
20901 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
20903 (easy-menu-define org-org-menu org-mode-map "Org menu"
20906 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
20907 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
20908 ["Sparse Tree..." org-sparse-tree t]
20909 ["Reveal Context" org-reveal t]
20910 ["Show All" show-all t]
20912 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
20914 ["New Heading" org-insert-heading t]
20915 ("Navigate Headings"
20916 ["Up" outline-up-heading t]
20917 ["Next" outline-next-visible-heading t]
20918 ["Previous" outline-previous-visible-heading t]
20919 ["Next Same Level" outline-forward-same-level t]
20920 ["Previous Same Level" outline-backward-same-level t]
20922 ["Jump" org-goto t])
20924 ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
20926 ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
20927 ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
20929 ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
20930 ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
20931 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
20933 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
20935 ["Copy visible text" org-copy-visible t]
20937 ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
20938 ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
20939 ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
20940 ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
20942 ["Sort Region/Children" org-sort t]
20944 ["Convert to odd levels" org-convert-to-odd-levels t]
20945 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
20947 ["Emphasis..." org-emphasize t]
20948 ["Edit Source Example" org-edit-special t]
20950 ["Footnote new/jump" org-footnote-action t]
20951 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
20953 ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
20955 ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
20956 ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
20957 ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
20961 ["Store Link (Global)" org-store-link t]
20962 ["Find existing link to here" org-occur-link-in-agenda-files t]
20963 ["Insert Link" org-insert-link t]
20964 ["Follow Link" org-open-at-point t]
20966 ["Next link" org-next-link t]
20967 ["Previous link" org-previous-link t]
20969 ["Descriptive Links"
20970 org-toggle-link-display
20972 :selected org-descriptive-links
20975 org-toggle-link-display
20977 :selected (not org-descriptive-links)])
20980 ["TODO/DONE/-" org-todo t]
20982 ["Next keyword" org-shiftright (org-at-heading-p)]
20983 ["Previous keyword" org-shiftleft (org-at-heading-p)]
20984 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
20985 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
20986 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
20987 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
20988 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
20990 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
20991 :selected org-enforce-todo-dependencies :style toggle :active t]
20992 "Settings for tree at point"
20993 ["Do Children sequentially" org-toggle-ordered-property :style radio
20994 :selected (org-entry-get nil "ORDERED")
20995 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
20996 ["Do Children parallel" org-toggle-ordered-property :style radio
20997 :selected (not (org-entry-get nil "ORDERED"))
20998 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
21000 ["Set Priority" org-priority t]
21001 ["Priority Up" org-shiftup t]
21002 ["Priority Down" org-shiftdown t]
21004 ["Get news from all feeds" org-feed-update-all t]
21005 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
21006 ["Customize feeds" (customize-variable 'org-feed-alist) t])
21007 ("TAGS and Properties"
21008 ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
21009 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
21011 ["Set property" org-set-property (not (org-before-first-heading-p))]
21012 ["Column view of properties" org-columns t]
21013 ["Insert Column View DBlock" org-insert-columns-dblock t])
21014 ("Dates and Scheduling"
21015 ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
21016 ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
21018 ["1 Day Later" org-shiftright (org-at-timestamp-p)]
21019 ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
21020 ["1 ... Later" org-shiftup (org-at-timestamp-p)]
21021 ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
21022 ["Compute Time Range" org-evaluate-time-range t]
21023 ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
21024 ["Deadline" org-deadline (not (org-before-first-heading-p))]
21026 ["Custom time format" org-toggle-time-stamp-overlays
21027 :style radio :selected org-display-custom-times]
21029 ["Goto Calendar" org-goto-calendar t]
21030 ["Date from Calendar" org-date-from-calendar t]
21032 ["Start/Restart Timer" org-timer-start t]
21033 ["Pause/Continue Timer" org-timer-pause-or-continue t]
21034 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
21035 ["Insert Timer String" org-timer t]
21036 ["Insert Timer Item" org-timer-item t])
21038 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
21039 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
21040 ["Clock out" org-clock-out t]
21041 ["Clock cancel" org-clock-cancel t]
21043 ["Mark as default task" org-clock-mark-default-task t]
21044 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
21045 ["Goto running clock" org-clock-goto t]
21047 ["Display times" org-clock-display t]
21048 ["Create clock table" org-clock-report t]
21050 ["Record DONE time"
21051 (progn (setq org-log-done (not org-log-done))
21052 (message "Switching to %s will %s record a timestamp"
21053 (car org-done-keywords)
21054 (if org-log-done "automatically" "not")))
21055 :style toggle :selected org-log-done])
21057 ["Agenda Command..." org-agenda t]
21058 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
21059 ("File List for Agenda")
21060 ("Special views current file"
21061 ["TODO Tree" org-show-todo-tree t]
21062 ["Check Deadlines" org-check-deadlines t]
21063 ["Timeline" org-timeline t]
21064 ["Tags/Property tree" org-match-sparse-tree t])
21066 ["Export/Publish..." org-export-dispatch t]
21068 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
21069 :selected org-cdlatex-mode]
21070 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
21071 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
21072 ["Modify math symbol" org-cdlatex-math-modify
21073 (org-inside-LaTeX-fragment-p)]
21074 ["Insert citation" org-reftex-citation t]
21076 ["Template for BEAMER" (org-beamer-insert-options-template) t])
21079 ["Push Files and Views" org-mobile-push t]
21080 ["Get Captured and Flagged" org-mobile-pull t]
21081 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
21083 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
21086 ["Show Version" org-version t]
21087 ["Info Documentation" org-info t])
21089 ["Browse Org Group" org-customize t]
21091 ["Expand This Menu" org-create-customize-menu
21092 (fboundp 'customize-menu-create)])
21093 ["Send bug report" org-submit-bug-report t]
21096 ["Refresh setup current buffer" org-mode-restart t]
21097 ["Reload Org (after update)" org-reload t]
21098 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
21101 (defun org-info (&optional node)
21102 "Read documentation for Org-mode in the info system.
21103 With optional NODE, go directly to that node."
21105 (info (format "(org)%s" (or node ""))))
21108 (defun org-submit-bug-report ()
21109 "Submit a bug report on Org-mode via mail.
21111 Don't hesitate to report any problems or inaccurate documentation.
21113 If you don't have setup sending mail from (X)Emacs, please copy the
21114 output buffer into your mail program, as it gives us important
21115 information about your Org-mode version and configuration."
21117 (require 'reporter)
21118 (defvar reporter-prompt-for-summary-p)
21119 (org-load-modules-maybe)
21120 (org-require-autoloaded-modules)
21121 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
21122 (reporter-submit-bug-report
21123 "emacs-orgmode@gnu.org"
21124 (org-version nil 'full)
21126 (save-window-excursion
21127 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
21128 (delete-other-windows)
21130 (insert "You are about to submit a bug report to the Org-mode mailing list.
21132 We would like to add your full Org-mode and Outline configuration to the
21133 bug report. This greatly simplifies the work of the maintainer and
21134 other experts on the mailing list.
21136 HOWEVER, some variables you have customized may contain private
21137 information. The names of customers, colleagues, or friends, might
21138 appear in the form of file names, tags, todo states, or search strings.
21139 If you answer yes to the prompt, you might want to check and remove
21140 such private information before sending the email.")
21141 (add-text-properties (point-min) (point-max) '(face org-warning))
21142 (when (yes-or-no-p "Include your Org-mode configuration ")
21146 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
21147 (or (and (symbol-value v)
21148 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
21150 (get v 'custom-type) (get v 'standard-value)
21151 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
21153 (kill-buffer (get-buffer "*Warn about privacy*"))
21156 "Remember to cover the basics, that is, what you expected to happen and
21157 what in fact did happen. You don't know how to make a good report? See
21159 http://orgmode.org/manual/Feedback.html#Feedback
21161 Your bug report will be posted to the Org-mode mailing list.
21162 ------------------------------------------------------------------------")
21164 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
21165 (replace-match "\\1Bug: \\3 [\\2]")))))
21168 (defun org-install-agenda-files-menu ()
21169 (let ((bl (buffer-list)))
21172 (set-buffer (pop bl))
21173 (if (derived-mode-p 'org-mode) (setq bl nil)))
21174 (when (derived-mode-p 'org-mode)
21176 '("Org") "File List for Agenda"
21179 ["Edit File List" (org-edit-agenda-file-list) t]
21180 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
21181 ["Remove Current File from List" org-remove-file t]
21182 ["Cycle through agenda files" org-cycle-agenda-files t]
21183 ["Occur in all agenda files" org-occur-in-agenda-files t]
21185 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
21189 (defun org-require-autoloaded-modules ()
21192 '(org-agenda org-archive org-attach org-clock org-colview org-id
21193 org-table org-timer)))
21196 (defun org-reload (&optional uncompiled)
21197 "Reload all org lisp files.
21198 With prefix arg UNCOMPILED, load the uncompiled versions."
21200 (require 'loadhist)
21201 (let* ((org-dir (org-find-library-dir "org"))
21202 (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
21203 (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
21204 (remove-re (mapconcat 'identity
21205 (mapcar (lambda (f) (concat "^" f "$"))
21206 (list (if (featurep 'xemacs)
21208 "org-colview-xemacs")
21209 "org" "org-loaddefs" "org-version"))
21211 (feats (delete-dups
21212 (mapcar 'file-name-sans-extension
21213 (mapcar 'file-name-nondirectory
21215 (mapcar 'feature-file
21222 (if (and (string-match feature-re f)
21223 (not (string-match remove-re f)))
21227 (list "org-version" "org")))
21228 (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
21229 (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
21230 load-uncore load-misses)
21233 (mapcar (lambda (f)
21234 (or (org-load-noerror-mustsuffix (concat org-dir f))
21235 (and (string= org-dir contrib-dir)
21236 (org-load-noerror-mustsuffix (concat contrib-dir f)))
21237 (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
21238 (add-to-list 'load-uncore f 'append)
21243 (message "The following feature%s found in load-path, please check if that's correct:\n%s"
21244 (if (> (length load-uncore) 1) "s were" " was") load-uncore))
21246 (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
21247 (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
21248 (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
21251 (defun org-customize ()
21252 "Call the customize function with org as argument."
21254 (org-load-modules-maybe)
21255 (org-require-autoloaded-modules)
21256 (customize-browse 'org))
21258 (defun org-create-customize-menu ()
21259 "Create a full customization menu for Org-mode, insert it into the menu."
21261 (org-load-modules-maybe)
21262 (org-require-autoloaded-modules)
21263 (if (fboundp 'customize-menu-create)
21266 '("Org") "Customize"
21267 `(["Browse Org group" org-customize t]
21269 ,(customize-menu-create 'org)
21270 ["Set" Custom-set t]
21271 ["Save" Custom-save t]
21272 ["Reset to Current" Custom-reset-current t]
21273 ["Reset to Saved" Custom-reset-saved t]
21274 ["Reset to Standard Settings" Custom-reset-standard t]))
21275 (message "\"Org\"-menu now contains full customization menu"))
21276 (error "Cannot expand menu (outdated version of cus-edit.el)")))
21278 ;;;; Miscellaneous stuff
21280 ;;; Generally useful functions
21282 (defun org-get-at-bol (property)
21283 "Get text property PROPERTY at beginning of line."
21284 (get-text-property (point-at-bol) property))
21286 (defun org-find-text-property-in-string (prop s)
21287 "Return the first non-nil value of property PROP in string S."
21288 (or (get-text-property 0 prop s)
21289 (get-text-property (or (next-single-property-change 0 prop s) 0)
21292 (defun org-display-warning (message) ;; Copied from Emacs-Muse
21293 "Display the given MESSAGE as a warning."
21294 (if (fboundp 'display-warning)
21295 (display-warning 'org message
21296 (if (featurep 'xemacs) 'warning :warning))
21297 (let ((buf (get-buffer-create "*Org warnings*")))
21298 (with-current-buffer buf
21299 (goto-char (point-max))
21300 (insert "Warning (Org): " message)
21303 (display-buffer buf)
21306 (defun org-eval (form)
21307 "Eval FORM and return result."
21308 (condition-case error
21310 (error (format "%%![Error: %s]" error))))
21312 (defun org-in-clocktable-p ()
21313 "Check if the cursor is in a clocktable."
21314 (let ((pos (point)) start)
21317 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
21318 (setq start (match-beginning 0))
21319 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
21320 (>= (match-end 0) pos)
21323 (defun org-in-commented-line ()
21324 "Is point in a line starting with `#'?"
21325 (equal (char-after (point-at-bol)) ?#))
21327 (defun org-in-indented-comment-line ()
21328 "Is point in a line starting with `#' after some white space?"
21331 (goto-char (point-at-bol))
21332 (looking-at "[ \t]*#"))))
21334 (defun org-in-verbatim-emphasis ()
21336 (and (org-in-regexp org-emph-re 2)
21337 (>= (point) (match-beginning 3))
21338 (<= (point) (match-end 4))
21339 (member (match-string 3) '("=" "~")))))
21341 (defun org-goto-marker-or-bmk (marker &optional bookmark)
21342 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
21343 (if (and marker (marker-buffer marker)
21344 (buffer-live-p (marker-buffer marker)))
21346 (org-pop-to-buffer-same-window (marker-buffer marker))
21347 (if (or (> marker (point-max)) (< marker (point-min)))
21350 (org-show-context 'org-goto))
21352 (bookmark-jump bookmark)
21353 (error "Cannot find location"))))
21355 (defun org-quote-csv-field (s)
21356 "Quote field for inclusion in CSV material."
21357 (if (string-match "[\",]" s)
21358 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
21361 (defun org-force-self-insert (N)
21362 "Needed to enforce self-insert under remapping."
21364 (self-insert-command N))
21366 (defun org-string-width (s)
21367 "Compute width of string, ignoring invisible characters.
21368 This ignores character with invisibility property `org-link', and also
21369 characters with property `org-cwidth', because these will become invisible
21370 upon the next fontification round."
21372 (when (or (eq t buffer-invisibility-spec)
21373 (assq 'org-link buffer-invisibility-spec))
21374 (while (setq b (text-property-any 0 (length s)
21375 'invisible 'org-link s))
21376 (setq s (concat (substring s 0 b)
21377 (substring s (or (next-single-property-change
21380 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
21381 (setq s (concat (substring s 0 b)
21382 (substring s (or (next-single-property-change
21385 (setq l (string-width s) b -1)
21386 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
21387 (setq l (- l (get-text-property b 'org-dwidth-n s))))
21390 (defun org-shorten-string (s maxlength)
21391 "Shorten string S so tht it is no longer than MAXLENGTH characters.
21392 If the string is shorter or has length MAXLENGTH, just return the
21393 original string. If it is longer, the functions finds a space in the
21394 string, breaks this string off at that locations and adds three dots
21395 as ellipsis. Including the ellipsis, the string will not be longer
21396 than MAXLENGTH. If finding a good breaking point in the string does
21397 not work, the string is just chopped off in the middle of a word
21399 (if (<= (length s) maxlength)
21401 (let* ((n (max (- maxlength 4) 1))
21402 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
21403 (if (string-match re s)
21404 (concat (match-string 1 s) "...")
21405 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
21407 (defun org-get-indentation (&optional line)
21408 "Get the indentation of the current line, interpreting tabs.
21409 When LINE is given, assume it represents a line and compute its indentation."
21411 (if (string-match "^ *" (org-remove-tabs line))
21414 (beginning-of-line 1)
21415 (skip-chars-forward " \t")
21416 (current-column))))
21418 (defun org-get-string-indentation (s)
21419 "What indentation has S due to SPACE and TAB at the beginning of the string?"
21420 (let ((n -1) (i 0) (w tab-width) c)
21422 (while (< (setq n (1+ n)) (length s))
21423 (setq c (aref s n))
21424 (cond ((= c ?\ ) (setq i (1+ i)))
21425 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
21426 (t (throw 'exit t)))))
21429 (defun org-remove-tabs (s &optional width)
21430 "Replace tabulators in S with spaces.
21431 Assumes that s is a single line, starting in column 0."
21432 (setq width (or width tab-width))
21433 (while (string-match "\t" s)
21434 (setq s (replace-match
21436 (- (* width (/ (+ (match-beginning 0) width) width))
21437 (match-beginning 0)) ?\ )
21441 (defun org-fix-indentation (line ind)
21442 "Fix indentation in LINE.
21443 IND is a cons cell with target and minimum indentation.
21444 If the current indentation in LINE is smaller than the minimum,
21445 leave it alone. If it is larger than ind, set it to the target."
21446 (let* ((l (org-remove-tabs line))
21447 (i (org-get-indentation l))
21448 (i1 (car ind)) (i2 (cdr ind)))
21449 (if (>= i i2) (setq l (substring line i2)))
21451 (concat (make-string i1 ?\ ) l)
21454 (defun org-remove-indentation (code &optional n)
21455 "Remove the maximum common indentation from the lines in CODE.
21456 N may optionally be the number of spaces to remove."
21459 (org-do-remove-indentation n)
21462 (defun org-do-remove-indentation (&optional n)
21463 "Remove the maximum common indentation from the buffer."
21464 (untabify (point-min) (point-max))
21465 (let ((min 10000) re)
21468 (goto-char (point-min))
21469 (while (re-search-forward "^ *[^ \n]" nil t)
21470 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
21471 (unless (or (= min 0) (= min 10000))
21472 (setq re (format "^ \\{%d\\}" min))
21473 (goto-char (point-min))
21474 (while (re-search-forward re nil t)
21479 (defun org-fill-template (template alist)
21480 "Find each %key of ALIST in TEMPLATE and replace it."
21481 (let ((case-fold-search nil))
21482 (dolist (entry (sort (copy-sequence alist)
21483 (lambda (a b) (< (length (car a)) (length (car b))))))
21485 (replace-regexp-in-string
21486 (concat "%" (regexp-quote (car entry)))
21487 (or (cdr entry) "") template t t)))
21490 (defun org-base-buffer (buffer)
21491 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
21494 (or (buffer-base-buffer buffer)
21497 (defun org-trim (s)
21498 "Remove whitespace at beginning and end of string."
21499 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
21500 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
21503 (defun org-wrap (string &optional width lines)
21504 "Wrap string to either a number of lines, or a width in characters.
21505 If WIDTH is non-nil, the string is wrapped to that width, however many lines
21506 that costs. If there is a word longer than WIDTH, the text is actually
21507 wrapped to the length of that word.
21508 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
21509 many lines, whatever width that takes.
21510 The return value is a list of lines, without newlines at the end."
21511 (let* ((words (org-split-string string "[ \t\n]+"))
21512 (maxword (apply 'max (mapcar 'org-string-width words)))
21515 (org-do-wrap words (max maxword width)))
21518 (setq ll (org-do-wrap words maxword))
21519 (if (<= (length ll) lines)
21522 (while (> (length ll) lines)
21524 (setq ll (org-do-wrap words w)))
21526 (t (error "Cannot wrap this")))))
21528 (defun org-do-wrap (words width)
21529 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
21532 (setq line (pop words))
21533 (while (and words (< (+ (length line) (length (car words))) width))
21534 (setq line (concat line " " (pop words))))
21535 (setq lines (push line lines)))
21538 (defun org-split-string (string &optional separators)
21539 "Splits STRING into substrings at SEPARATORS.
21540 No empty strings are returned if there are matches at the beginning
21541 and end of string."
21542 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
21546 (while (and (string-match rexp string
21548 (= start (match-beginning 0))
21549 (< start (length string)))
21551 (< (match-beginning 0) (length string)))
21553 (or (eq (match-beginning 0) 0)
21554 (and (eq (match-beginning 0) (match-end 0))
21555 (eq (match-beginning 0) start))
21557 (cons (substring string start (match-beginning 0))
21559 (setq start (match-end 0)))
21560 (or (eq start (length string))
21562 (cons (substring string start)
21566 (defun org-quote-vert (s)
21567 "Replace \"|\" with \"\\vert\"."
21568 (while (string-match "|" s)
21569 (setq s (replace-match "\\vert" t t s)))
21572 (defun org-uuidgen-p (s)
21573 "Is S an ID created by UUIDGEN?"
21574 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
21576 (defun org-in-src-block-p (&optional inside)
21577 "Whether point is in a code source block.
21578 When INSIDE is non-nil, don't consider we are within a src block
21579 when point is at #+BEGIN_SRC or #+END_SRC."
21580 (let ((case-fold-search t) ov)
21581 (or (and (setq ov (overlays-at (point)))
21582 (memq 'org-block-background
21583 (overlay-properties (car ov))))
21587 (beginning-of-line)
21588 (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
21590 (defun org-context ()
21591 "Return a list of contexts of the current cursor position.
21592 If several contexts apply, all are returned.
21593 Each context entry is a list with a symbol naming the context, and
21594 two positions indicating start and end of the context. Possible
21597 :headline anywhere in a headline
21598 :headline-stars on the leading stars in a headline
21599 :todo-keyword on a TODO keyword (including DONE) in a headline
21600 :tags on the TAGS in a headline
21601 :priority on the priority cookie in a headline
21602 :item on the first line of a plain list item
21603 :item-bullet on the bullet/number of a plain list item
21604 :checkbox on the checkbox in a plain list item
21605 :table in an org-mode table
21606 :table-special on a special filed in a table
21607 :table-table in a table.el table
21608 :clocktable in a clocktable
21609 :src-block in a source block
21610 :link on a hyperlink
21611 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
21612 :target on a <<target>>
21613 :radio-target on a <<<radio-target>>>
21614 :latex-fragment on a LaTeX fragment
21615 :latex-preview on a LaTeX fragment with overlaid preview image
21617 This function expects the position to be visible because it uses font-lock
21618 faces as a help to recognize the following contexts: :table-special, :link,
21620 (let* ((f (get-text-property (point) 'face))
21621 (faces (if (listp f) f (list f)))
21622 (case-fold-search t)
21623 (p (point)) clist o)
21624 ;; First the large context
21626 ((org-at-heading-p t)
21627 (push (list :headline (point-at-bol) (point-at-eol)) clist)
21629 (beginning-of-line 1)
21630 (looking-at org-todo-line-tags-regexp))
21631 (push (org-point-in-group p 1 :headline-stars) clist)
21632 (push (org-point-in-group p 2 :todo-keyword) clist)
21633 (push (org-point-in-group p 4 :tags) clist))
21635 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
21636 (if (looking-at "\\[#[A-Z0-9]\\]")
21637 (push (org-point-in-group p 0 :priority) clist)))
21640 (push (org-point-in-group p 2 :item-bullet) clist)
21641 (push (list :item (point-at-bol)
21642 (save-excursion (org-end-of-item) (point)))
21644 (and (org-at-item-checkbox-p)
21645 (push (org-point-in-group p 0 :checkbox) clist)))
21648 (push (list :table (org-table-begin) (org-table-end)) clist)
21649 (if (memq 'org-formula faces)
21650 (push (list :table-special
21651 (previous-single-property-change p 'face)
21652 (next-single-property-change p 'face)) clist)))
21653 ((org-at-table-p 'any)
21654 (push (list :table-table) clist)))
21657 (let ((case-fold-search t))
21658 ;; New the "medium" contexts: clocktables, source blocks
21659 (cond ((org-in-clocktable-p)
21660 (push (list :clocktable
21661 (and (or (looking-at "#\\+BEGIN: clocktable")
21662 (search-backward "#+BEGIN: clocktable" nil t))
21663 (match-beginning 0))
21664 (and (re-search-forward "#\\+END:?" nil t)
21665 (match-end 0))) clist))
21666 ((org-in-src-block-p)
21667 (push (list :src-block
21668 (and (or (looking-at "#\\+BEGIN_SRC")
21669 (search-backward "#+BEGIN_SRC" nil t))
21670 (match-beginning 0))
21671 (and (search-forward "#+END_SRC" nil t)
21672 (match-beginning 0))) clist))))
21675 ;; Now the small context
21677 ((org-at-timestamp-p)
21678 (push (org-point-in-group p 0 :timestamp) clist))
21679 ((memq 'org-link faces)
21681 (previous-single-property-change p 'face)
21682 (next-single-property-change p 'face)) clist))
21683 ((memq 'org-special-keyword faces)
21684 (push (list :keyword
21685 (previous-single-property-change p 'face)
21686 (next-single-property-change p 'face)) clist))
21688 (push (org-point-in-group p 0 :target) clist)
21689 (goto-char (1- (match-beginning 0)))
21690 (if (looking-at org-radio-target-regexp)
21691 (push (org-point-in-group p 0 :radio-target) clist))
21693 ((setq o (car (delq nil
21696 (if (memq x org-latex-fragment-image-overlays) x))
21697 (overlays-at (point))))))
21698 (push (list :latex-fragment
21699 (overlay-start o) (overlay-end o)) clist)
21700 (push (list :latex-preview
21701 (overlay-start o) (overlay-end o)) clist))
21702 ((org-inside-LaTeX-fragment-p)
21703 ;; FIXME: positions wrong.
21704 (push (list :latex-fragment (point) (point)) clist)))
21706 (setq clist (nreverse (delq nil clist)))
21709 ;; FIXME: Compare with at-regexp-p Do we need both?
21710 (defun org-in-regexp (re &optional nlines visually)
21711 "Check if point is inside a match of regexp.
21712 Normally only the current line is checked, but you can include NLINES extra
21713 lines both before and after point into the search.
21714 If VISUALLY is set, require that the cursor is not after the match but
21715 really on, so that the block visually is on the match."
21717 (let ((pos (point))
21718 (eol (point-at-eol (+ 1 (or nlines 0))))
21719 (inc (if visually 1 0)))
21721 (beginning-of-line (- 1 (or nlines 0)))
21722 (while (re-search-forward re eol t)
21723 (if (and (<= (match-beginning 0) pos)
21724 (>= (+ inc (match-end 0)) pos))
21725 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
21727 (defun org-at-regexp-p (regexp)
21728 "Is point inside a match of REGEXP in the current line?"
21731 (let ((pos (point)) (end (point-at-eol)))
21732 (beginning-of-line 1)
21733 (while (re-search-forward regexp end t)
21734 (if (and (<= (match-beginning 0) pos)
21735 (>= (match-end 0) pos))
21739 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
21740 "Non-nil when point is between matches of START-RE and END-RE.
21742 Also return a non-nil value when point is on one of the matches.
21744 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
21745 buffer positions. Default values are the positions of headlines
21746 surrounding the point.
21748 The functions returns a cons cell whose car (resp. cdr) is the
21749 position before START-RE (resp. after END-RE)."
21751 (let ((pos (point))
21752 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
21753 (limit-down (or lim-down (save-excursion (outline-next-heading))))
21756 ;; Point is on a block when on START-RE or if START-RE can be
21757 ;; found before it...
21758 (and (or (org-at-regexp-p start-re)
21759 (re-search-backward start-re limit-up t))
21760 (setq beg (match-beginning 0))
21761 ;; ... and END-RE after it...
21762 (goto-char (match-end 0))
21763 (re-search-forward end-re limit-down t)
21764 (> (setq end (match-end 0)) pos)
21765 ;; ... without another START-RE in-between.
21766 (goto-char (match-beginning 0))
21767 (not (re-search-backward start-re (1+ beg) t))
21769 (cons beg end))))))
21771 (defun org-in-block-p (names)
21772 "Non-nil when point belongs to a block whose name belongs to NAMES.
21774 NAMES is a list of strings containing names of blocks.
21776 Return first block name matched, or nil. Beware that in case of
21777 nested blocks, the returned name may not belong to the closest
21781 (let ((case-fold-search t)
21782 (lim-up (save-excursion (outline-previous-heading)))
21783 (lim-down (save-excursion (outline-next-heading))))
21784 (mapc (lambda (name)
21785 (let ((n (regexp-quote name)))
21786 (when (org-between-regexps-p
21787 (concat "^[ \t]*#\\+begin_" n)
21788 (concat "^[ \t]*#\\+end_" n)
21794 (defun org-in-drawer-p ()
21795 "Is point within a drawer?"
21797 (let ((case-fold-search t)
21798 (lim-up (save-excursion (outline-previous-heading)))
21799 (lim-down (save-excursion (outline-next-heading))))
21800 (org-between-regexps-p
21801 (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
21803 lim-up lim-down))))
21805 (defun org-occur-in-agenda-files (regexp &optional _nlines)
21806 "Call `multi-occur' with buffers for all agenda files."
21807 (interactive "sOrg-files matching: ")
21808 (let* ((files (org-agenda-files))
21809 (tnames (mapcar #'file-truename files))
21810 (extra org-agenda-text-search-extra-files))
21811 (when (eq (car extra) 'agenda-archives)
21812 (setq extra (cdr extra))
21813 (setq files (org-add-archive-files files)))
21815 (unless (member (file-truename f) tnames)
21816 (unless (member f files) (setq files (append files (list f))))
21817 (setq tnames (append tnames (list (file-truename f))))))
21819 (mapcar (lambda (x)
21820 (with-current-buffer
21821 ;; FIXME: Why not just (find-file-noselect x)?
21822 ;; Is it to avoid the "revert buffer" prompt?
21823 (or (get-file-buffer x) (find-file-noselect x))
21829 (if (boundp 'occur-mode-find-occurrence-hook)
21831 (add-hook 'occur-mode-find-occurrence-hook
21833 (when (derived-mode-p 'org-mode)
21836 (defadvice occur-mode-goto-occurrence
21837 (after org-occur-reveal activate)
21838 (and (derived-mode-p 'org-mode) (org-reveal)))
21839 (defadvice occur-mode-goto-occurrence-other-window
21840 (after org-occur-reveal activate)
21841 (and (derived-mode-p 'org-mode) (org-reveal)))
21842 (defadvice occur-mode-display-occurrence
21843 (after org-occur-reveal activate)
21844 (when (derived-mode-p 'org-mode)
21845 (let ((pos (occur-mode-find-occurrence)))
21846 (with-current-buffer (marker-buffer pos)
21851 (defun org-occur-link-in-agenda-files ()
21852 "Create a link and search for it in the agendas.
21853 The link is not stored in `org-stored-links', it is just created
21854 for the search purpose."
21856 (let ((link (condition-case nil
21857 (org-store-link nil)
21858 (error "Unable to create a link to here"))))
21859 (org-occur-in-agenda-files (regexp-quote link))))
21861 (defun org-reverse-string (string)
21862 "Return the reverse of STRING."
21863 (apply 'string (reverse (string-to-list string))))
21865 ;; defsubst org-uniquify must be defined before first use
21867 (defun org-uniquify-alist (alist)
21868 "Merge elements of ALIST with the same key.
21870 For example, in this alist:
21872 \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
21873 => \\='((a 1 3) (b 2))
21875 merge (a 1) and (a 3) into (a 1 3).
21877 The function returns the new ALIST."
21882 (if (not (assoc (car e) rtn))
21884 (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
21885 (setq rtn (assq-delete-all (car e) rtn))
21890 (defun org-delete-all (elts list)
21891 "Remove all elements in ELTS from LIST."
21893 (setq list (delete (pop elts) list)))
21896 (defun org-count (cl-item cl-seq)
21897 "Count the number of occurrences of ITEM in SEQ.
21898 Taken from `count' in cl-seq.el with all keyword arguments removed."
21899 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
21900 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
21901 (while (< cl-start cl-end)
21902 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
21903 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
21904 (setq cl-start (1+ cl-start)))
21907 (defun org-remove-if (predicate seq)
21908 "Remove everything from SEQ that fulfills PREDICATE."
21912 (if (not (funcall predicate e)) (push e res)))
21915 (defun org-remove-if-not (predicate seq)
21916 "Remove everything from SEQ that does not fulfill PREDICATE."
21920 (if (funcall predicate e) (push e res)))
21923 (defun org-reduce (cl-func cl-seq &rest cl-keys)
21924 "Reduce two-argument FUNCTION across SEQ.
21925 Taken from `reduce' in cl-seq.el with all keyword arguments but
21926 \":initial-value\" removed."
21927 (let ((cl-accum (cond ((memq :initial-value cl-keys)
21928 (cadr (memq :initial-value cl-keys)))
21929 (cl-seq (pop cl-seq))
21930 (t (funcall cl-func)))))
21932 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
21935 (defun org-every (pred seq)
21936 "Return true if PREDICATE is true of every element of SEQ.
21937 Adapted from `every' in cl.el."
21939 (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
21942 (defun org-some (pred seq)
21943 "Return true if PREDICATE is true of any element of SEQ.
21944 Adapted from `some' in cl.el."
21946 (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
21949 (defun org-back-over-empty-lines ()
21950 "Move backwards over whitespace, to the beginning of the first empty line.
21951 Returns the number of empty lines passed."
21952 (let ((pos (point)))
21953 (if (cdr (assoc 'heading org-blank-before-new-entry))
21954 (skip-chars-backward " \t\n\r")
21956 (forward-line -1)))
21957 (beginning-of-line 2)
21958 (goto-char (min (point) pos))
21959 (count-lines (point) pos)))
21961 (defun org-skip-whitespace ()
21962 (skip-chars-forward " \t\n\r"))
21964 (defun org-point-in-group (point group &optional context)
21965 "Check if POINT is in match-group GROUP.
21966 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
21967 match. If the match group does not exist or point is not inside it,
21969 (and (match-beginning group)
21970 (>= point (match-beginning group))
21971 (<= point (match-end group))
21973 (list context (match-beginning group) (match-end group))
21976 (defun org-switch-to-buffer-other-window (&rest args)
21977 "Switch to buffer in a second window on the current frame.
21978 In particular, do not allow pop-up frames.
21979 Returns the newly created buffer."
21981 (apply 'switch-to-buffer-other-window args)))
21983 (defun org-combine-plists (&rest plists)
21984 "Create a single property list from all plists in PLISTS.
21985 The process starts by copying the first list, and then setting properties
21986 from the other lists. Settings in the last list are the most significant
21987 ones and overrule settings in the other lists."
21988 (let ((rtn (copy-sequence (pop plists)))
21991 (setq ls (pop plists))
21993 (setq p (pop ls) v (pop ls))
21994 (setq rtn (plist-put rtn p v))))
21997 (defun org-replace-escapes (string table)
21998 "Replace %-escapes in STRING with values in TABLE.
21999 TABLE is an association list with keys like \"%a\" and string values.
22000 The sequences in STRING may contain normal field width and padding information,
22001 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
22002 so values can contain further %-escapes if they are define later in TABLE."
22003 (let ((tbl (copy-alist table))
22004 (case-fold-search nil)
22008 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
22009 (when (and (cdr e) (string-match re (cdr e)))
22010 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
22012 (add-text-properties 0 3 (list 'sref sref) safe)
22013 (setcdr e (replace-match safe t t (cdr e)))))
22014 (while (string-match re string)
22015 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
22017 (setq string (replace-match rpl t t string))))
22018 (while (setq pchg (next-property-change pchg string))
22019 (let ((sref (get-text-property pchg 'sref string)))
22020 (when (and sref (string-match "SREF" string pchg))
22021 (setq string (replace-match sref t t string)))))
22024 (defun org-sublist (list start end)
22025 "Return a section of LIST, from START to END.
22026 Counting starts at 1."
22027 (let (rtn (c start))
22028 (setq list (nthcdr (1- start) list))
22029 (while (and list (<= c end))
22030 (push (pop list) rtn)
22034 (defun org-find-base-buffer-visiting (file)
22035 "Like `find-buffer-visiting' but always return the base buffer and
22036 not an indirect buffer."
22037 (let ((buf (or (get-file-buffer file)
22038 (find-buffer-visiting file))))
22040 (or (buffer-base-buffer buf) buf)
22043 (defun org-image-file-name-regexp (&optional extensions)
22044 "Return regexp matching the file names of images.
22045 If EXTENSIONS is given, only match these."
22046 (if (and (not extensions) (fboundp 'image-file-name-regexp))
22047 (image-file-name-regexp)
22048 (let ((image-file-name-extensions
22050 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
22051 "xbm" "xpm" "pbm" "pgm" "ppm"))))
22053 (regexp-opt (nconc (mapcar 'upcase
22054 image-file-name-extensions)
22055 image-file-name-extensions)
22059 (defun org-file-image-p (file &optional extensions)
22060 "Return non-nil if FILE is an image."
22062 (string-match (org-image-file-name-regexp extensions) file)))
22064 (defun org-get-cursor-date (&optional with-time)
22065 "Return the date at cursor in as a time.
22066 This works in the calendar and in the agenda, anywhere else it just
22067 returns the current time.
22068 If WITH-TIME is non-nil, returns the time of the event at point (in
22069 the agenda) or the current time of the day."
22070 (let (date day defd tp hod mod)
22072 (setq tp (get-text-property (point) 'time))
22073 (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
22074 (setq hod (string-to-number (match-string 1 tp))
22075 mod (string-to-number (match-string 2 tp))))
22076 (or tp (let ((now (decode-time)))
22077 (setq hod (nth 2 now)
22078 mod (nth 1 now)))))
22080 ((eq major-mode 'calendar-mode)
22081 (setq date (calendar-cursor-to-date)
22082 defd (encode-time 0 (or mod 0) (or hod 0)
22083 (nth 1 date) (nth 0 date) (nth 2 date))))
22084 ((eq major-mode 'org-agenda-mode)
22085 (setq day (get-text-property (point) 'day))
22087 (setq date (calendar-gregorian-from-absolute day)
22088 defd (encode-time 0 (or mod 0) (or hod 0)
22089 (nth 1 date) (nth 0 date) (nth 2 date))))))
22090 (or defd (current-time))))
22092 (defun org-mark-subtree (&optional up)
22093 "Mark the current subtree.
22094 This puts point at the start of the current subtree, and mark at
22095 the end. If a numeric prefix UP is given, move up into the
22096 hierarchy of headlines by UP levels before marking the subtree."
22098 (org-with-limited-levels
22099 (cond ((org-at-heading-p) (beginning-of-line))
22100 ((org-before-first-heading-p) (user-error "Not in a subtree"))
22101 (t (outline-previous-visible-heading 1))))
22102 (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
22103 (if (org-called-interactively-p 'any)
22104 (call-interactively 'org-mark-element)
22105 (org-mark-element)))
22110 (defun org-indent-line ()
22111 "Indent line depending on context."
22113 (let* ((pos (point))
22114 (itemp (org-at-item-p))
22115 (case-fold-search t)
22116 (org-drawer-regexp (or org-drawer-regexp "\000"))
22117 (inline-task-p (and (featurep 'org-inlinetask)
22118 (org-inlinetask-in-task-p)))
22119 (inline-re (and inline-task-p
22120 (org-inlinetask-outline-regexp)))
22122 (if (and orgstruct-is-++ (eq pos (point)))
22123 (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
22124 (indent-according-to-mode))
22125 (beginning-of-line 1)
22128 ((looking-at org-outline-regexp) (setq column 0))
22129 ;; Footnote definition
22130 ((looking-at org-footnote-definition-re) (setq column 0))
22131 ;; Literal examples
22132 ((looking-at "[ \t]*:\\( \\|$\\)")
22133 (setq column (org-get-indentation))) ; do nothing
22135 ((ignore-errors (goto-char (org-in-item-p)))
22136 (setq column (if itemp
22137 (org-get-indentation)
22138 (org-list-item-body-column (point))))
22141 ((and (looking-at "[ \t]*:END:")
22142 (save-excursion (re-search-backward org-drawer-regexp nil t)))
22144 (goto-char (1- (match-beginning 1)))
22145 (setq column (current-column))))
22147 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
22149 (re-search-backward
22150 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
22151 (setq column (org-get-indentation (match-string 0))))
22152 ((and (not (looking-at "[ \t]*#\\+begin_"))
22153 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
22155 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
22157 (cond ((equal (downcase (match-string 1)) "src")
22158 ;; src blocks: let `org-edit-src-exit' handle them
22159 (org-get-indentation))
22160 ((equal (downcase (match-string 1)) "example")
22161 (max (org-get-indentation)
22162 (org-get-indentation (match-string 0))))
22164 (org-get-indentation (match-string 0))))))
22165 ;; This line has nothing special, look at the previous relevant
22166 ;; line to compute indentation
22168 (beginning-of-line 0)
22169 (while (and (not (bobp))
22170 (not (looking-at org-table-line-regexp))
22171 (not (looking-at org-drawer-regexp))
22172 ;; When point started in an inline task, do not move
22173 ;; above task starting line.
22174 (not (and inline-task-p (looking-at inline-re)))
22175 ;; Skip drawers, blocks, empty lines, verbatim,
22176 ;; comments, tables, footnotes definitions, lists,
22178 (or (and (looking-at "[ \t]*:END:")
22179 (re-search-backward org-drawer-regexp nil t))
22180 (and (looking-at "[ \t]*#\\+end_")
22181 (re-search-backward "[ \t]*#\\+begin_"nil t))
22182 (looking-at "[ \t]*[\n:#|]")
22183 (looking-at org-footnote-definition-re)
22184 (and (not inline-task-p)
22185 (featurep 'org-inlinetask)
22186 (org-inlinetask-in-task-p)
22187 (or (org-inlinetask-goto-beginning) t))))
22188 (beginning-of-line 0))
22190 ;; There was a list item above.
22191 ((ignore-errors (goto-char (org-in-item-p)))
22192 (goto-char (org-list-get-top-point (org-list-struct)))
22193 (setq column (org-get-indentation)))
22194 ;; There was an heading above.
22195 ((looking-at "\\*+[ \t]+")
22196 (if (not org-adapt-indentation)
22198 (goto-char (match-end 0))
22199 (setq column (current-column))))
22200 ;; A drawer had started and is unfinished
22201 ((looking-at org-drawer-regexp)
22202 (goto-char (1- (match-beginning 1)))
22203 (setq column (current-column)))
22204 ;; Else, nothing noticeable found: get indentation and go on.
22205 (t (setq column (org-get-indentation))))))
22206 ;; Now apply indentation and move cursor accordingly
22208 (if (<= (current-column) (current-indentation))
22209 (org-indent-line-to column)
22210 (save-excursion (org-indent-line-to column)))
22211 ;; Special polishing for properties, see `org-property-format'
22212 (setq column (current-column))
22213 (beginning-of-line 1)
22214 (if (looking-at org-property-re)
22215 (replace-match (concat (match-string 4)
22216 (format org-property-format
22217 (match-string 1) (match-string 3)))
22219 (org-move-to-column column))))
22221 (defun org-indent-drawer ()
22222 "Indent the drawer at point."
22225 (e (and (save-excursion (re-search-forward ":END:" nil t))
22230 (when (overlays-at (point))
22231 (member 'invisible (overlay-properties
22232 (car (overlays-at (point)))))))))
22233 (when folded (org-cycle))
22234 (indent-for-tab-command)
22235 (while (and (move-beginning-of-line 2) (< (point) e))
22236 (indent-for-tab-command))
22238 (when folded (org-cycle)))
22239 (message "Drawer at point indented"))
22241 (defun org-indent-block ()
22242 "Indent the block at point."
22245 (case-fold-search t)
22246 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
22251 (when (overlays-at (point))
22252 (member 'invisible (overlay-properties
22253 (car (overlays-at (point)))))))))
22254 (when folded (org-cycle))
22255 (indent-for-tab-command)
22256 (while (and (move-beginning-of-line 2) (< (point) e))
22257 (indent-for-tab-command))
22259 (when folded (org-cycle)))
22260 (message "Block at point indented"))
22262 (defun org-indent-region (start end)
22266 (let ((line-end (org-current-line end)))
22268 (while (< (org-current-line) line-end)
22269 (cond ((org-in-src-block-p t) (org-src-native-tab-command-maybe))
22270 (t (call-interactively 'org-indent-line)))
22271 (move-beginning-of-line 2)))))
22276 ;; We use our own fill-paragraph and auto-fill functions.
22278 ;; `org-fill-paragraph' relies on adaptive filling and context
22279 ;; checking. Appropriate `fill-prefix' is computed with
22280 ;; `org-adaptive-fill-function'.
22282 ;; `org-auto-fill-function' takes care of auto-filling. It calls
22283 ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
22284 ;; `org-adaptive-fill-function' value. Internally,
22285 ;; `org-comment-line-break-function' breaks the line.
22287 ;; `org-setup-filling' installs filling and auto-filling related
22288 ;; variables during `org-mode' initialization.
22290 (defvar org-element-paragraph-separate) ; org-element.el
22291 (defun org-setup-filling ()
22292 (require 'org-element)
22293 ;; Prevent auto-fill from inserting unwanted new items.
22294 (when (boundp 'fill-nobreak-predicate)
22296 'fill-nobreak-predicate
22298 (append fill-nobreak-predicate
22299 '(org-fill-line-break-nobreak-p
22300 org-fill-paragraph-with-timestamp-nobreak-p)))))
22301 (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
22302 (org-set-local 'paragraph-start paragraph-ending)
22303 (org-set-local 'paragraph-separate paragraph-ending))
22304 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
22305 (org-set-local 'auto-fill-inhibit-regexp nil)
22306 (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
22307 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
22308 (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
22310 (defun org-fill-line-break-nobreak-p ()
22311 "Non-nil when a new line at point would create an Org line break."
22313 (skip-chars-backward "[ \t]")
22314 (skip-chars-backward "\\\\")
22315 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
22317 (defun org-fill-paragraph-with-timestamp-nobreak-p ()
22318 "Non-nil when a new line at point would split a timestamp."
22319 (and (org-at-timestamp-p t)
22320 (not (looking-at org-ts-regexp-both))))
22322 (declare-function message-in-body-p "message" ())
22323 (defvar orgtbl-line-start-regexp) ; From org-table.el
22324 (defun org-adaptive-fill-function ()
22325 "Compute a fill prefix for the current line.
22326 Return fill prefix, as a string, or nil if current line isn't
22327 meant to be filled. For convenience, if `adaptive-fill-regexp'
22328 matches in paragraphs or comments, use it."
22330 (when (derived-mode-p 'message-mode)
22332 (beginning-of-line)
22333 (cond ((or (not (message-in-body-p))
22334 (looking-at orgtbl-line-start-regexp))
22336 ((looking-at message-cite-prefix-regexp)
22337 (throw 'exit (match-string-no-properties 0)))
22338 ((looking-at org-outline-regexp)
22339 (throw 'exit (make-string (length (match-string 0)) ? ))))))
22340 (org-with-wide-buffer
22341 (let* ((p (line-beginning-position))
22342 (element (save-excursion
22343 (beginning-of-line)
22344 (or (ignore-errors (org-element-at-point))
22345 (user-error "An element cannot be parsed line %d"
22346 (line-number-at-pos (point))))))
22347 (type (org-element-type element))
22348 (post-affiliated (org-element-property :post-affiliated element)))
22349 (unless (and post-affiliated (< p post-affiliated))
22353 (beginning-of-line)
22354 (looking-at "[ \t]*")
22355 (concat (match-string 0) "# ")))
22356 (footnote-definition "")
22358 (make-string (org-list-item-body-column
22359 (or post-affiliated
22360 (org-element-property :begin element)))
22363 ;; Fill prefix is usually the same as the current line,
22364 ;; unless the paragraph is at the beginning of an item.
22365 (let ((parent (org-element-property :parent element)))
22367 (beginning-of-line)
22368 (cond ((eq (org-element-type parent) 'item)
22369 (make-string (org-list-item-body-column
22370 (org-element-property :begin parent))
22372 ((and adaptive-fill-regexp
22374 ;; `adaptive-fill-function' to let
22375 ;; `fill-context-prefix' handle
22376 ;; `adaptive-fill-regexp' variable.
22377 (let (adaptive-fill-function)
22378 (fill-context-prefix
22380 (org-element-property :end element)))))
22381 ((looking-at "[ \t]+") (match-string 0))
22384 ;; Only fill contents if P is within block boundaries.
22385 (let* ((cbeg (save-excursion (goto-char post-affiliated)
22388 (cend (save-excursion
22389 (goto-char (org-element-property :end element))
22390 (skip-chars-backward " \r\t\n")
22391 (line-beginning-position))))
22392 (when (and (>= p cbeg) (< p cend))
22393 (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
22397 (declare-function message-goto-body "message" ())
22398 (defvar message-cite-prefix-regexp) ; From message.el
22399 (defun org-fill-paragraph (&optional justify)
22400 "Fill element at point, when applicable.
22402 This function only applies to comment blocks, comments, example
22403 blocks and paragraphs. Also, as a special case, re-align table
22404 when point is at one.
22406 If JUSTIFY is non-nil (interactively, with prefix argument),
22407 justify as well. If `sentence-end-double-space' is non-nil, then
22408 period followed by one space does not end a sentence, so don't
22409 break a line there. The variable `fill-column' controls the
22412 For convenience, when point is at a plain list, an item or
22413 a footnote definition, try to fill the first paragraph within."
22415 (if (and (derived-mode-p 'message-mode)
22416 (or (not (message-in-body-p))
22417 (save-excursion (move-beginning-of-line 1)
22418 (looking-at message-cite-prefix-regexp))))
22419 ;; First ensure filling is correct in message-mode.
22420 (let ((fill-paragraph-function
22421 (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
22422 (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
22423 (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
22424 (paragraph-separate
22425 (cadadr (assoc 'paragraph-separate org-fb-vars))))
22426 (fill-paragraph nil))
22427 (with-syntax-table org-mode-transpose-word-syntax-table
22428 ;; Move to end of line in order to get the first paragraph
22429 ;; within a plain list or a footnote definition.
22430 (let ((element (save-excursion
22432 (or (ignore-errors (org-element-at-point))
22433 (user-error "An element cannot be parsed line %d"
22434 (line-number-at-pos (point)))))))
22435 ;; First check if point is in a blank line at the beginning of
22436 ;; the buffer. In that case, ignore filling.
22437 (case (org-element-type element)
22438 ;; Use major mode filling function is src blocks.
22439 (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
22440 ;; Align Org tables, leave table.el tables as-is.
22441 (table-row (org-table-align) t)
22443 (when (eq (org-element-property :type element) 'org)
22445 (goto-char (org-element-property :post-affiliated element))
22446 (org-table-align)))
22449 ;; Paragraphs may contain `line-break' type objects.
22450 (let ((beg (max (point-min)
22451 (org-element-property :contents-begin element)))
22452 (end (min (point-max)
22453 (org-element-property :contents-end element))))
22454 ;; Do nothing if point is at an affiliated keyword.
22455 (if (< (line-end-position) beg) t
22456 (when (derived-mode-p 'message-mode)
22457 ;; In `message-mode', do not fill following citation
22458 ;; in current paragraph nor text before message body.
22459 (let ((body-start (save-excursion (message-goto-body))))
22460 (when body-start (setq beg (max body-start beg))))
22461 (when (save-excursion
22463 (concat "^" message-cite-prefix-regexp) end t))
22464 (setq end (match-beginning 0))))
22465 ;; Fill paragraph, taking line breaks into account.
22466 ;; For that, slice the paragraph using line breaks as
22467 ;; separators, and fill the parts in reverse order to
22468 ;; avoid messing with markers.
22473 (fill-region-as-paragraph pos (point) justify)
22475 ;; Find the list of ending positions for line breaks
22476 ;; in the current paragraph. Add paragraph
22477 ;; beginning to include first slice.
22481 (org-element--parse-objects
22482 beg end nil (org-element-restriction 'paragraph))
22484 (lambda (lb) (org-element-property :end lb)))))))
22486 ;; Contents of `comment-block' type elements should be
22487 ;; filled as plain text, but only if point is within block
22490 (let* ((case-fold-search t)
22491 (beg (save-excursion
22492 (goto-char (org-element-property :begin element))
22493 (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
22496 (end (save-excursion
22497 (goto-char (org-element-property :end element))
22498 (re-search-backward "^[ \t]*#\\+end_comment" nil t)
22499 (line-beginning-position))))
22500 (if (or (< (point) beg) (> (point) end)) t
22501 (fill-region-as-paragraph
22502 (save-excursion (end-of-line)
22503 (re-search-backward "^[ \t]*$" beg 'move)
22504 (line-beginning-position))
22505 (save-excursion (beginning-of-line)
22506 (re-search-forward "^[ \t]*$" end 'move)
22507 (line-beginning-position))
22511 (let ((begin (org-element-property :post-affiliated element))
22512 (end (org-element-property :end element)))
22513 (when (and (>= (point) begin) (<= (point) end))
22514 (let ((begin (save-excursion
22516 (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
22517 (progn (forward-line) (point))
22519 (end (save-excursion
22521 (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
22522 (1- (line-beginning-position))
22523 (skip-chars-backward " \r\t\n")
22524 (line-end-position)))))
22525 ;; Do not fill comments when at a blank line.
22526 (when (> end begin)
22529 (beginning-of-line)
22530 (looking-at "[ \t]*#")
22531 (let ((comment-prefix (match-string 0)))
22532 (goto-char (match-end 0))
22533 (if (looking-at adaptive-fill-regexp)
22534 (concat comment-prefix (match-string 0))
22535 (concat comment-prefix " "))))))
22537 (fill-region-as-paragraph begin end justify))))))
22539 ;; Ignore every other element.
22542 (defun org-auto-fill-function ()
22543 "Auto-fill function."
22544 ;; Check if auto-filling is meaningful.
22545 (let ((fc (current-fill-column)))
22546 (when (and fc (> (current-column) fc))
22547 (let* ((fill-prefix (org-adaptive-fill-function))
22548 ;; Enforce empty fill prefix, if required. Otherwise, it
22549 ;; will be computed again.
22550 (adaptive-fill-mode (not (equal fill-prefix ""))))
22551 (when fill-prefix (do-auto-fill))))))
22553 (defun org-comment-line-break-function (&optional soft)
22554 "Break line at point and indent, continuing comment if within one.
22555 The inserted newline is marked hard if variable
22556 `use-hard-newlines' is true, unless optional argument SOFT is
22558 (if soft (insert-and-inherit ?\n) (newline 1))
22559 (save-excursion (forward-char -1) (delete-horizontal-space))
22560 (delete-horizontal-space)
22561 (indent-to-left-margin)
22562 (insert-before-markers-and-inherit fill-prefix))
22567 ;; Org comments syntax is quite complex. It requires the entire line
22568 ;; to be just a comment. Also, even with the right syntax at the
22569 ;; beginning of line, some some elements (i.e. verse-block or
22570 ;; example-block) don't accept comments. Usual Emacs comment commands
22571 ;; cannot cope with those requirements. Therefore, Org replaces them.
22573 ;; Org still relies on `comment-dwim', but cannot trust
22574 ;; `comment-only-p'. So, `comment-region-function' and
22575 ;; `uncomment-region-function' both point
22576 ;; to`org-comment-or-uncomment-region'. Eventually,
22577 ;; `org-insert-comment' takes care of insertion of comments at the
22578 ;; beginning of line.
22580 ;; `org-setup-comments-handling' install comments related variables
22581 ;; during `org-mode' initialization.
22583 (defun org-setup-comments-handling ()
22585 (org-set-local 'comment-use-syntax nil)
22586 (org-set-local 'comment-start "# ")
22587 (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
22588 (org-set-local 'comment-insert-comment-function 'org-insert-comment)
22589 (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
22590 (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
22592 (defun org-insert-comment ()
22593 "Insert an empty comment above current line.
22594 If the line is empty, insert comment at its beginning."
22595 (beginning-of-line)
22596 (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
22600 (defvar comment-empty-lines) ; From newcomment.el.
22601 (defun org-comment-or-uncomment-region (beg end &rest _)
22602 "Comment or uncomment each non-blank line in the region.
22603 Uncomment each non-blank line between BEG and END if it only
22604 contains commented lines. Otherwise, comment them."
22607 (narrow-to-region (save-excursion (goto-char beg)
22608 (skip-chars-forward " \r\t\n" end)
22609 (line-beginning-position))
22610 (save-excursion (goto-char end)
22611 (skip-chars-backward " \r\t\n" beg)
22612 (line-end-position)))
22614 ;; UNCOMMENTP is non-nil when every non blank line between
22615 ;; BEG and END is a comment.
22617 (goto-char (point-min))
22618 (while (and (not (eobp))
22619 (let ((element (org-element-at-point)))
22620 (and (eq (org-element-type element) 'comment)
22621 (goto-char (min (point-max)
22622 (org-element-property
22623 :end element)))))))
22626 ;; Only blank lines and comments in region: uncomment it.
22628 (goto-char (point-min))
22629 (while (not (eobp))
22630 (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
22631 (replace-match "" nil nil nil 1))
22633 ;; Comment each line in region.
22634 (let ((min-indent (point-max)))
22635 ;; First find the minimum indentation across all lines.
22637 (goto-char (point-min))
22638 (while (and (not (eobp)) (not (zerop min-indent)))
22639 (unless (looking-at "[ \t]*$")
22640 (setq min-indent (min min-indent (current-indentation))))
22642 ;; Then loop over all lines.
22644 (goto-char (point-min))
22645 (while (not (eobp))
22646 (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
22647 ;; Don't get fooled by invisible text (e.g. link path)
22648 ;; when moving to column MIN-INDENT.
22649 (let ((buffer-invisibility-spec nil))
22650 (org-move-to-column min-indent t))
22651 (insert comment-start))
22652 (forward-line))))))))
22657 ;; This section contains tools to operate on timestamp objects, as
22658 ;; returned by, e.g. `org-element-context'.
22660 (defun org-timestamp-has-time-p (timestamp)
22661 "Non-nil when TIMESTAMP has a time specified."
22662 (org-element-property :hour-start timestamp))
22664 (defun org-timestamp-format (timestamp format &optional end zone)
22665 "Format a TIMESTAMP element into a string.
22667 FORMAT is a format specifier to be passed to
22668 `format-time-string'.
22670 When optional argument END is non-nil, use end of date-range or
22671 time-range, if possible.
22673 The optional ZONE is omitted or nil for Emacs local time, t for
22674 Universal Time, `wall' for system wall clock time, or a string as
22675 in the TZ environment variable. It can also be a list (as from
22676 `current-time-zone') or an integer (as from `decode-time')
22677 applied without consideration for daylight saving time."
22678 (format-time-string
22680 (apply 'encode-time
22683 (lambda (prop) (or (org-element-property prop timestamp) 0))
22684 (if end '(:minute-end :hour-end :day-end :month-end :year-end)
22685 '(:minute-start :hour-start :day-start :month-start
22689 (defun org-timestamp-split-range (timestamp &optional end)
22690 "Extract a timestamp object from a date or time range.
22692 TIMESTAMP is a timestamp object. END, when non-nil, means extract
22693 the end of the range. Otherwise, extract its start.
22695 Return a new timestamp object sharing the same parent as
22697 (let ((type (org-element-property :type timestamp)))
22698 (if (memq type '(active inactive diary)) timestamp
22699 (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
22701 (org-element-put-property
22702 split-ts :type (if (eq type 'active-range) 'active 'inactive))
22703 ;; Copy start properties over end properties if END is
22704 ;; non-nil. Otherwise, copy end properties over `start' ones.
22705 (let ((p-alist '((:minute-start . :minute-end)
22706 (:hour-start . :hour-end)
22707 (:day-start . :day-end)
22708 (:month-start . :month-end)
22709 (:year-start . :year-end))))
22710 (dolist (p-cell p-alist)
22711 (org-element-put-property
22713 (funcall (if end 'car 'cdr) p-cell)
22714 (org-element-property
22715 (funcall (if end 'cdr 'car) p-cell) split-ts)))
22716 ;; Eventually refresh `:raw-value'.
22717 (org-element-put-property split-ts :raw-value nil)
22718 (org-element-put-property
22719 split-ts :raw-value (org-element-interpret-data split-ts)))))))
22721 (defun org-timestamp-translate (timestamp &optional boundary)
22722 "Apply `org-translate-time' on a TIMESTAMP object.
22723 When optional argument BOUNDARY is non-nil, it is either the
22724 symbol `start' or `end'. In this case, only translate the
22725 starting or ending part of TIMESTAMP if it is a date or time
22726 range. Otherwise, translate both parts."
22727 (if (and (not boundary)
22728 (memq (org-element-property :type timestamp)
22729 '(active-range inactive-range)))
22731 (org-translate-time
22732 (org-element-property :raw-value
22733 (org-timestamp-split-range timestamp)))
22735 (org-translate-time
22736 (org-element-property :raw-value
22737 (org-timestamp-split-range timestamp t))))
22738 (org-translate-time
22739 (org-element-property
22741 (if (not boundary) timestamp
22742 (org-timestamp-split-range timestamp (eq boundary 'end)))))))
22748 (defun org-toggle-fixed-width-section (arg)
22749 "Toggle the fixed-width export.
22750 If there is no active region, the QUOTE keyword at the current headline is
22751 inserted or removed. When present, it causes the text between this headline
22752 and the next to be exported as fixed-width text, and unmodified.
22753 If there is an active region, this command adds or removes a colon as the
22754 first character of this line. If the first character of a line is a colon,
22755 this line is also exported in fixed-width font."
22758 (regionp (org-region-active-p))
22759 (beg (if regionp (region-beginning) (point)))
22760 (end (if regionp (region-end)))
22761 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
22762 (case-fold-search nil)
22763 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
22768 (setq cc (current-column))
22769 (beginning-of-line 1)
22770 (setq off (looking-at re))
22771 (while (> nlines 0)
22772 (setq nlines (1- nlines))
22773 (beginning-of-line 1)
22776 (org-move-to-column cc t)
22779 ((and off (looking-at re))
22780 (replace-match "" t t nil 1))
22781 ((not off) (org-move-to-column cc t) (insert ": ")))
22784 (org-back-to-heading)
22786 ((looking-at (format org-heading-keyword-regexp-format
22788 (goto-char (match-end 1))
22789 (looking-at (concat " +" org-quote-string))
22790 (replace-match "" t t)
22791 (when (eolp) (insert " ")))
22792 ((looking-at org-outline-regexp)
22793 (goto-char (match-end 0))
22794 (insert org-quote-string " ")))))))
22796 (defvar reftex-docstruct-symbol)
22797 (defvar reftex-cite-format)
22800 (defun org-reftex-citation ()
22801 "Use reftex-citation to insert a citation into the buffer.
22802 This looks for a line like
22804 #+BIBLIOGRAPHY: foo plain option:-d
22806 and derives from it that foo.bib is the bibliography file relevant
22807 for this document. It then installs the necessary environment for RefTeX
22808 to work in this buffer and calls `reftex-citation' to insert a citation
22811 Export of such citations to both LaTeX and HTML is handled by the contributed
22812 package ox-bibtex by Taru Karttunen."
22814 (let ((reftex-docstruct-symbol 'org--rds)
22815 (reftex-cite-format "\\cite{%l}")
22820 (let ((case-fold-search t)
22821 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
22822 (if (not (save-excursion
22823 (or (re-search-forward re nil t)
22824 (re-search-backward re nil t))))
22825 (error "No bibliography defined in file")
22826 (setq bib (concat (match-string 1) ".bib")
22827 org--rds (list (list 'bib bib)))))))
22828 (call-interactively 'reftex-citation)))
22830 ;;;; Functions extending outline functionality
22832 (defun org-beginning-of-line (&optional arg)
22833 "Go to the beginning of the current line. If that is invisible, continue
22834 to a visible line beginning. This makes the function of C-a more intuitive.
22835 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
22836 first attempt, and only move to after the tags when the cursor is already
22837 beyond the end of the headline."
22839 (let ((pos (point))
22840 (special (if (consp org-special-ctrl-a/e)
22841 (car org-special-ctrl-a/e)
22842 org-special-ctrl-a/e))
22843 deactivate-mark refpos)
22844 (if (org-bound-and-true-p visual-line-mode)
22845 (beginning-of-visual-line 1)
22846 (beginning-of-line 1))
22847 (if (and arg (fboundp 'move-beginning-of-line))
22848 (call-interactively 'move-beginning-of-line)
22852 (if (org-truely-invisible-p)
22853 (while (and (not (bobp)) (org-truely-invisible-p))
22855 (beginning-of-line 1))
22856 (forward-char 1))))
22859 ((and (looking-at org-complex-heading-regexp)
22860 (= (char-after (match-end 1)) ?\ ))
22861 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
22865 (cond ((> pos refpos) refpos)
22866 ((= pos (point)) refpos)
22868 (cond ((> pos (point)) (point))
22869 ((not (eq last-command this-command)) (point))
22872 ;; Being at an item and not looking at an the item means point
22873 ;; was previously moved to beginning of a visual line, which
22874 ;; doesn't contain the item. Therefore, do nothing special,
22876 (when (looking-at org-list-full-item-re)
22877 ;; Set special position at first white space character after
22878 ;; bullet, and check-box, if any.
22879 (let ((after-bullet
22880 (let ((box (match-end 3)))
22881 (if (not box) (match-end 1)
22882 (let ((after (char-after box)))
22883 (if (and after (= after ? )) (1+ box) box))))))
22884 ;; Special case: Move point to special position when
22885 ;; currently after it or at beginning of line.
22887 (when (or (> pos after-bullet) (= (point) pos))
22888 (goto-char after-bullet))
22889 ;; Reversed case: Move point to special position when
22890 ;; point was already at beginning of line and command is
22892 (when (and (= (point) pos) (eq last-command this-command))
22893 (goto-char after-bullet))))))))
22895 (and (featurep 'xemacs) (setq zmacs-region-stays t))))
22896 (setq disable-point-adjustment
22897 (or (not (invisible-p (point)))
22898 (not (invisible-p (max (point-min) (1- (point))))))))
22900 (defun org-end-of-line (&optional arg)
22901 "Go to the end of the line.
22902 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
22903 tags on the first attempt, and only move to after the tags when
22904 the cursor is already beyond the end of the headline."
22906 (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
22907 org-special-ctrl-a/e))
22908 (move-fun (cond ((org-bound-and-true-p visual-line-mode)
22909 'end-of-visual-line)
22910 ((fboundp 'move-end-of-line) 'move-end-of-line)
22913 (if (or (not special) arg) (call-interactively move-fun)
22914 (let* ((element (save-excursion (beginning-of-line)
22915 (org-element-at-point)))
22916 (type (org-element-type element)))
22918 ((memq type '(headline inlinetask))
22919 (let ((pos (point)))
22920 (beginning-of-line 1)
22921 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
22923 (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
22924 (goto-char (match-beginning 1))
22925 (goto-char (match-end 0)))
22926 (if (or (< pos (match-end 0))
22927 (not (eq this-command last-command)))
22928 (goto-char (match-end 0))
22929 (goto-char (match-beginning 1))))
22930 (call-interactively move-fun))))
22931 ((org-element-property :hiddenp element)
22932 ;; If element is hidden, `move-end-of-line' would put point
22933 ;; after it. Use `end-of-line' to stay on current line.
22934 (call-interactively 'end-of-line))
22935 (t (call-interactively move-fun)))))
22936 (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
22937 (setq disable-point-adjustment
22938 (or (not (invisible-p (point)))
22939 (not (invisible-p (max (point-min) (1- (point))))))))
22941 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
22942 (define-key org-mode-map "\C-e" 'org-end-of-line)
22944 (defun org-backward-sentence (&optional _arg)
22945 "Go to beginning of sentence, or beginning of table field.
22946 This will call `backward-sentence' or `org-table-beginning-of-field',
22947 depending on context."
22950 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
22951 (t (call-interactively 'backward-sentence))))
22953 (defun org-forward-sentence (&optional _arg)
22954 "Go to end of sentence, or end of table field.
22955 This will call `forward-sentence' or `org-table-end-of-field',
22956 depending on context."
22959 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
22960 (t (call-interactively 'forward-sentence))))
22962 (define-key org-mode-map "\M-a" 'org-backward-sentence)
22963 (define-key org-mode-map "\M-e" 'org-forward-sentence)
22965 (defun org-kill-line (&optional _arg)
22966 "Kill line, to tags or end of line."
22969 ((or (not org-special-ctrl-k)
22971 (not (org-at-heading-p)))
22972 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
22973 org-ctrl-k-protect-subtree)
22974 (if (or (eq org-ctrl-k-protect-subtree 'error)
22975 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
22976 (user-error "C-k aborted as it would kill a hidden subtree")))
22977 (call-interactively
22978 (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
22979 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
22980 (kill-region (point) (match-beginning 1))
22981 (org-set-tags nil t))
22982 (t (kill-region (point) (point-at-eol)))))
22984 (define-key org-mode-map "\C-k" 'org-kill-line)
22986 (defun org-yank (&optional arg)
22987 "Yank. If the kill is a subtree, treat it specially.
22988 This command will look at the current kill and check if is a single
22989 subtree, or a series of subtrees[1]. If it passes the test, and if the
22990 cursor is at the beginning of a line or after the stars of a currently
22991 empty headline, then the yank is handled specially. How exactly depends
22992 on the value of the following variables, both set by default.
22994 org-yank-folded-subtrees
22995 When set, the subtree(s) will be folded after insertion, but only
22996 if doing so would now swallow text after the yanked text.
22998 org-yank-adjusted-subtrees
22999 When set, the subtree will be promoted or demoted in order to
23000 fit into the local outline tree structure, which means that the level
23001 will be adjusted so that it becomes the smaller one of the two
23002 *visible* surrounding headings.
23004 Any prefix to this command will cause `yank' to be called directly with
23005 no special treatment. In particular, a simple \\[universal-argument] prefix \
23007 plainly yank the text as it is.
23009 [1] The test checks if the first non-white line is a heading
23010 and if there are no other headings with fewer stars."
23012 (org-yank-generic 'yank arg))
23014 (defun org-yank-generic (command arg)
23015 "Perform some yank-like command.
23017 This function implements the behavior described in the `org-yank'
23018 documentation. However, it has been generalized to work for any
23019 interactive command with similar behavior."
23021 ;; pretend to be command COMMAND
23022 (setq this-command command)
23025 (call-interactively command)
23027 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
23028 (and (org-kill-is-subtree-p)
23030 (and (looking-at "[ \t]*$")
23033 (buffer-substring (point-at-bol) (point)))))))
23036 ((and subtreep org-yank-folded-subtrees)
23037 (let ((beg (point))
23039 (if (and subtreep org-yank-adjusted-subtrees)
23040 (org-paste-subtree nil nil 'for-yank)
23041 (call-interactively command))
23045 (when (and (bolp) subtreep
23046 (not (setq swallowp
23047 (org-yank-folding-would-swallow-text beg end))))
23048 (org-with-limited-levels
23049 (or (looking-at org-outline-regexp)
23050 (re-search-forward org-outline-regexp-bol end t))
23051 (while (and (< (point) end) (looking-at org-outline-regexp))
23053 (org-cycle-show-empty-lines 'folded)
23054 (condition-case nil
23055 (outline-forward-same-level 1)
23056 (error (goto-char end))))))
23059 "Inserted text not folded because that would swallow text"))
23062 (skip-chars-forward " \t\n\r")
23063 (beginning-of-line 1)
23064 (push-mark beg 'nomsg)))
23065 ((and subtreep org-yank-adjusted-subtrees)
23066 (let ((beg (point-at-bol)))
23067 (org-paste-subtree nil nil 'for-yank)
23068 (push-mark beg 'nomsg)))
23070 (call-interactively command))))))
23072 (defun org-yank-folding-would-swallow-text (beg end)
23073 "Would hide-subtree at BEG swallow any text after END?"
23075 (org-with-limited-levels
23078 (when (or (looking-at org-outline-regexp)
23079 (re-search-forward org-outline-regexp-bol end t))
23080 (setq level (org-outline-level)))
23082 (skip-chars-forward " \t\r\n\v\f")
23084 (and (bolp) (looking-at org-outline-regexp)
23085 (<= (org-outline-level) level)))
23086 nil ; Nothing would be swallowed
23087 t))))) ; something would swallow
23089 (define-key org-mode-map "\C-y" 'org-yank)
23091 (defun org-truely-invisible-p ()
23092 "Check if point is at a character currently not visible.
23093 This version does not only check the character property, but also
23095 ;; Early versions of noutline don't have `outline-invisible-p'.
23096 (if (org-bound-and-true-p visible-mode)
23098 (outline-invisible-p)))
23100 (defun org-invisible-p2 ()
23101 "Check if point is at a character currently not visible."
23103 (if (and (eolp) (not (bobp))) (backward-char 1))
23104 ;; Early versions of noutline don't have `outline-invisible-p'.
23105 (outline-invisible-p)))
23107 (defun org-back-to-heading (&optional invisible-ok)
23108 "Call `outline-back-to-heading', but provide a better error message."
23109 (condition-case nil
23110 (outline-back-to-heading invisible-ok)
23111 (error (error "Before first headline at position %d in buffer %s"
23112 (point) (current-buffer)))))
23114 (defun org-before-first-heading-p ()
23115 "Before first heading?"
23118 (null (re-search-backward org-outline-regexp-bol nil t))))
23120 (defun org-at-heading-p (&optional ignored)
23121 (outline-on-heading-p t))
23122 ;; Compatibility alias with Org versions < 7.8.03
23123 (defalias 'org-on-heading-p 'org-at-heading-p)
23125 (defun org-at-comment-p nil
23126 "Is cursor in a line starting with a # character?"
23128 (beginning-of-line)
23129 (looking-at "^#")))
23131 (defun org-at-drawer-p nil
23132 "Is cursor at a drawer keyword?"
23134 (move-beginning-of-line 1)
23135 (looking-at org-drawer-regexp)))
23137 (defun org-at-block-p nil
23138 "Is cursor at a block keyword?"
23140 (move-beginning-of-line 1)
23141 (looking-at org-block-regexp)))
23143 (defun org-point-at-end-of-empty-headline ()
23144 "If point is at the end of an empty headline, return t, else nil.
23145 If the heading only contains a TODO keyword, it is still still considered
23147 (and (looking-at "[ \t]*$")
23148 (when org-todo-line-regexp
23150 (beginning-of-line 1)
23151 (let ((case-fold-search nil))
23152 (looking-at org-todo-line-regexp)
23153 (string= (match-string 3) ""))))))
23155 (defun org-at-heading-or-item-p ()
23156 (or (org-at-heading-p) (org-at-item-p)))
23158 (defun org-at-target-p ()
23159 (or (org-in-regexp org-radio-target-regexp)
23160 (org-in-regexp org-target-regexp)))
23161 ;; Compatibility alias with Org versions < 7.8.03
23162 (defalias 'org-on-target-p 'org-at-target-p)
23164 (defun org-up-heading-all (arg)
23165 "Move to the heading line of which the present line is a subheading.
23166 This function considers both visible and invisible heading lines.
23167 With argument, move up ARG levels."
23168 (if (fboundp 'outline-up-heading-all)
23169 (outline-up-heading-all arg) ; emacs 21 version of outline.el
23170 (outline-up-heading arg t))) ; emacs 22 version of outline.el
23172 (defun org-up-heading-safe ()
23173 "Move to the heading line of which the present line is a subheading.
23174 This version will not throw an error. It will return the level of the
23175 headline found, or nil if no higher level is found.
23177 Also, this function will be a lot faster than `outline-up-heading',
23178 because it relies on stars being the outline starters. This can really
23179 make a significant difference in outlines with very many siblings."
23180 (let (start-level re)
23181 (org-back-to-heading t)
23182 (setq start-level (funcall outline-level))
23183 (if (equal start-level 1)
23185 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
23186 (if (re-search-backward re nil t)
23187 (funcall outline-level)))))
23189 (defun org-first-sibling-p ()
23190 "Is this heading the first child of its parents?"
23192 (let ((re org-outline-regexp-bol)
23194 (unless (org-at-heading-p t)
23195 (user-error "Not at a heading"))
23196 (setq level (funcall outline-level))
23198 (if (not (re-search-backward re nil t))
23200 (setq l (funcall outline-level))
23203 (defun org-goto-sibling (&optional previous)
23204 "Goto the next sibling, even if it is invisible.
23205 When PREVIOUS is set, go to the previous sibling instead. Returns t
23206 when a sibling was found. When none is found, return nil and don't
23208 (let ((fun (if previous 're-search-backward 're-search-forward))
23210 (re org-outline-regexp-bol)
23212 (when (condition-case nil (org-back-to-heading t) (error nil))
23213 (setq level (funcall outline-level))
23215 (or previous (forward-char 1))
23216 (while (funcall fun re nil t)
23217 (setq l (funcall outline-level))
23218 (when (< l level) (goto-char pos) (throw 'exit nil))
23219 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
23223 (defun org-show-siblings ()
23224 "Show all siblings of the current headline."
23226 (while (org-goto-sibling) (org-flag-heading nil)))
23228 (while (org-goto-sibling 'previous)
23229 (org-flag-heading nil))))
23231 (defun org-goto-first-child ()
23232 "Goto the first child, even if it is invisible.
23233 Return t when a child was found. Otherwise don't move point and
23235 (let (level (pos (point)) (re org-outline-regexp-bol))
23236 (when (condition-case nil (org-back-to-heading t) (error nil))
23237 (setq level (outline-level))
23239 (if (and (re-search-forward re nil t) (> (outline-level) level))
23240 (progn (goto-char (match-beginning 0)) t)
23241 (goto-char pos) nil))))
23243 (defun org-show-hidden-entry ()
23244 "Show an entry where even the heading is hidden."
23248 (defun org-flag-heading (flag &optional entry)
23249 "Flag the current heading. FLAG non-nil means make invisible.
23250 When ENTRY is non-nil, show the entire entry."
23252 (org-back-to-heading t)
23253 ;; Check if we should show the entire entry
23258 (and (outline-next-heading)
23259 (org-flag-heading nil))))
23260 (outline-flag-region (max (point-min) (1- (point)))
23261 (save-excursion (outline-end-of-heading) (point))
23264 (defun org-get-next-sibling ()
23265 "Move to next heading of the same level, and return point.
23266 If there is no such heading, return nil.
23267 This is like outline-next-sibling, but invisible headings are ok."
23268 (let ((level (funcall outline-level)))
23269 (outline-next-heading)
23270 (while (and (not (eobp)) (> (funcall outline-level) level))
23271 (outline-next-heading))
23272 (if (or (eobp) (< (funcall outline-level) level))
23276 (defun org-get-last-sibling ()
23277 "Move to previous heading of the same level, and return point.
23278 If there is no such heading, return nil."
23279 (let ((opoint (point))
23280 (level (funcall outline-level)))
23281 (outline-previous-heading)
23282 (when (and (/= (point) opoint) (outline-on-heading-p t))
23283 (while (and (> (funcall outline-level) level)
23285 (outline-previous-heading))
23286 (if (< (funcall outline-level) level)
23290 (defun org-end-of-subtree (&optional invisible-ok to-heading)
23291 "Goto to the end of a subtree."
23292 ;; This contains an exact copy of the original function, but it uses
23293 ;; `org-back-to-heading', to make it work also in invisible
23294 ;; trees. And is uses an invisible-ok argument.
23295 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
23296 ;; Furthermore, when used inside Org, finding the end of a large subtree
23297 ;; with many children and grandchildren etc, this can be much faster
23298 ;; than the outline version.
23299 (org-back-to-heading invisible-ok)
23301 (level (funcall outline-level)))
23302 (if (and (derived-mode-p 'org-mode) (< level 1000))
23303 ;; A true heading (not a plain list item), in Org-mode
23304 ;; This means we can easily find the end by looking
23305 ;; only for the right number of stars. Using a regexp to do
23306 ;; this is so much faster than using a Lisp loop.
23307 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
23309 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
23310 ;; something else, do it the slow way
23311 (while (and (not (eobp))
23312 (or first (> (funcall outline-level) level)))
23314 (outline-next-heading)))
23316 (if (memq (preceding-char) '(?\n ?\^M))
23318 ;; Go to end of line before heading
23320 (if (memq (preceding-char) '(?\n ?\^M))
23321 ;; leave blank line before heading
23322 (forward-char -1))))))
23325 (defun org-end-of-meta-data-and-drawers ()
23326 "Jump to the first text after meta data and drawers in the current entry.
23327 This will move over empty lines, lines with planning time stamps,
23328 clocking lines, and drawers."
23329 (org-back-to-heading t)
23330 (let ((end (save-excursion (outline-next-heading) (point)))
23331 (re (concat "\\(" org-drawer-regexp "\\)"
23332 "\\|" "[ \t]*" org-keyword-time-regexp)))
23334 (while (re-search-forward re end t)
23335 (if (not (match-end 1))
23336 ;; empty or planning line
23338 ;; a drawer, find the end
23339 (re-search-forward "^[ \t]*:END:" end 'move)
23341 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
23344 (defun org-forward-heading-same-level (arg &optional invisible-ok)
23345 "Move forward to the ARG'th subheading at same level as this one.
23346 Stop at the first and last subheadings of a superior heading.
23347 Normally this only looks at visible headings, but when INVISIBLE-OK is
23348 non-nil it will also look at invisible ones."
23350 (if (not (ignore-errors (org-back-to-heading invisible-ok)))
23351 (if (and arg (< arg 0))
23352 (goto-char (point-min))
23353 (outline-next-heading))
23355 (let ((level (- (match-end 0) (match-beginning 0) 1))
23356 (f (if (and arg (< arg 0))
23357 're-search-backward
23358 're-search-forward))
23359 (count (if arg (abs arg) 1))
23361 (while (and (prog1 (> count 0)
23362 (forward-char (if (and arg (< arg 0)) -1 1)))
23363 (funcall f org-outline-regexp-bol nil 'move))
23364 (let ((l (- (match-end 0) (match-beginning 0) 1)))
23365 (cond ((< l level) (setq count 0))
23369 (goto-char (line-beginning-position))
23370 (not (outline-invisible-p)))))
23371 (setq count (1- count))
23373 (setq result (point)))))))
23374 (goto-char result))
23375 (beginning-of-line 1)))
23377 (defun org-backward-heading-same-level (arg &optional invisible-ok)
23378 "Move backward to the ARG'th subheading at same level as this one.
23379 Stop at the first and last subheadings of a superior heading."
23381 (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
23383 (defun org-next-block (arg &optional backward block-regexp)
23384 "Jump to the next block.
23385 With a prefix argument ARG, jump forward ARG many source blocks.
23386 When BACKWARD is non-nil, jump to the previous block.
23387 When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
23389 (let ((re (or block-regexp org-block-regexp))
23390 (re-search-fn (or (and backward 're-search-backward)
23391 're-search-forward)))
23392 (if (looking-at re) (forward-char 1))
23393 (condition-case nil
23394 (funcall re-search-fn re nil nil arg)
23395 (error (error "No %s code blocks" (if backward "previous" "further" ))))
23396 (goto-char (match-beginning 0)) (org-show-context)))
23398 (defun org-previous-block (arg &optional block-regexp)
23399 "Jump to the previous block.
23400 With a prefix argument ARG, jump backward ARG many source blocks.
23401 When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
23403 (org-next-block arg t block-regexp))
23405 (defun org-forward-paragraph ()
23406 "Move forward to beginning of next paragraph or equivalent.
23408 The function moves point to the beginning of the next visible
23409 structural element, which can be a paragraph, a table, a list
23410 item, etc. It also provides some special moves for convenience:
23412 - On an affiliated keyword, jump to the beginning of the
23414 - On an item or a footnote definition, move to the second
23415 element inside, if any.
23416 - On a table or a property drawer, jump after it.
23417 - On a verse or source block, stop after blank lines."
23419 (when (eobp) (user-error "Cannot move further down"))
23420 (let* ((deactivate-mark nil)
23421 (element (org-element-at-point))
23422 (type (org-element-type element))
23423 (post-affiliated (org-element-property :post-affiliated element))
23424 (contents-begin (org-element-property :contents-begin element))
23425 (contents-end (org-element-property :contents-end element))
23426 (end (let ((end (org-element-property :end element)) (parent element))
23427 (while (and (setq parent (org-element-property :parent parent))
23428 (= (org-element-property :contents-end parent) end))
23429 (setq end (org-element-property :end parent)))
23431 (cond ((not element)
23432 (skip-chars-forward " \r\t\n")
23433 (or (eobp) (beginning-of-line)))
23434 ;; On affiliated keywords, move to element's beginning.
23435 ((and post-affiliated (< (point) post-affiliated))
23436 (goto-char post-affiliated))
23437 ;; At a table row, move to the end of the table. Similarly,
23438 ;; at a node property, move to the end of the property
23440 ((memq type '(node-property table-row))
23441 (goto-char (org-element-property
23442 :end (org-element-property :parent element))))
23443 ((memq type '(property-drawer table)) (goto-char end))
23444 ;; Consider blank lines as separators in verse and source
23445 ;; blocks to ease editing.
23446 ((memq type '(src-block verse-block))
23447 (when (eq type 'src-block)
23449 (save-excursion (goto-char end)
23450 (skip-chars-backward " \r\t\n")
23451 (line-beginning-position))))
23452 (beginning-of-line)
23453 (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
23454 (if (not (re-search-forward "^[ \t]*$" contents-end t))
23456 (skip-chars-forward " \r\t\n")
23457 (if (= (point) contents-end) (goto-char end)
23458 (beginning-of-line))))
23459 ;; With no contents, just skip element.
23460 ((not contents-begin) (goto-char end))
23461 ;; If contents are invisible, skip the element altogether.
23462 ((outline-invisible-p (line-end-position))
23465 (org-with-limited-levels (outline-next-visible-heading 1)))
23466 ;; At a plain list, make sure we move to the next item
23467 ;; instead of skipping the whole list.
23468 (plain-list (forward-char)
23469 (org-forward-paragraph))
23470 (otherwise (goto-char end))))
23471 ((>= (point) contents-end) (goto-char end))
23472 ((>= (point) contents-begin)
23473 ;; This can only happen on paragraphs and plain lists.
23475 (paragraph (goto-char end))
23476 ;; At a plain list, try to move to second element in
23477 ;; first item, if possible.
23478 (plain-list (end-of-line)
23479 (org-forward-paragraph))))
23480 ;; When contents start on the middle of a line (e.g. in
23481 ;; items and footnote definitions), try to reach first
23482 ;; element starting after current line.
23483 ((> (line-end-position) contents-begin)
23485 (org-forward-paragraph))
23486 (t (goto-char contents-begin)))))
23488 (defun org-backward-paragraph ()
23489 "Move backward to start of previous paragraph or equivalent.
23491 The function moves point to the beginning of the current
23492 structural element, which can be a paragraph, a table, a list
23493 item, etc., or to the beginning of the previous visible one if
23494 point is already there. It also provides some special moves for
23497 - On an affiliated keyword, jump to the first one.
23498 - On a table or a property drawer, move to its beginning.
23499 - On a verse or source block, stop before blank lines."
23501 (when (bobp) (user-error "Cannot move further up"))
23502 (let* ((deactivate-mark nil)
23503 (element (org-element-at-point))
23504 (type (org-element-type element))
23505 (contents-begin (org-element-property :contents-begin element))
23506 (contents-end (org-element-property :contents-end element))
23507 (post-affiliated (org-element-property :post-affiliated element))
23508 (begin (org-element-property :begin element)))
23510 ((not element) (goto-char (point-min)))
23513 (org-backward-paragraph))
23514 ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
23515 ((memq type '(node-property table-row))
23516 (goto-char (org-element-property
23517 :post-affiliated (org-element-property :parent element))))
23518 ((memq type '(property-drawer table)) (goto-char begin))
23519 ((memq type '(src-block verse-block))
23520 (when (eq type 'src-block)
23521 (setq contents-begin
23522 (save-excursion (goto-char begin) (forward-line) (point))))
23523 (if (= (point) contents-begin) (goto-char post-affiliated)
23524 ;; Inside a verse block, see blank lines as paragraph
23526 (let ((origin (point)))
23527 (skip-chars-backward " \r\t\n" contents-begin)
23528 (when (re-search-backward "^[ \t]*$" contents-begin 'move)
23529 (skip-chars-forward " \r\t\n" origin)
23530 (if (= (point) origin) (goto-char contents-begin)
23531 (beginning-of-line))))))
23532 ((not contents-begin) (goto-char (or post-affiliated begin)))
23533 ((eq type 'paragraph)
23534 (goto-char contents-begin)
23535 ;; When at first paragraph in an item or a footnote definition,
23536 ;; move directly to beginning of line.
23537 (let ((parent-contents
23538 (org-element-property
23539 :contents-begin (org-element-property :parent element))))
23540 (when (and parent-contents (= parent-contents contents-begin))
23541 (beginning-of-line))))
23542 ;; At the end of a greater element, move to the beginning of the
23543 ;; last element within.
23544 ((>= (point) contents-end)
23545 (goto-char (1- contents-end))
23546 (org-backward-paragraph))
23547 (t (goto-char (or post-affiliated begin))))
23548 ;; Ensure we never leave point invisible.
23549 (when (outline-invisible-p (point)) (beginning-of-visual-line))))
23551 (defun org-forward-element ()
23552 "Move forward by one element.
23553 Move to the next element at the same level, when possible."
23555 (cond ((eobp) (user-error "Cannot move further down"))
23556 ((org-with-limited-levels (org-at-heading-p))
23557 (let ((origin (point)))
23558 (goto-char (org-end-of-subtree nil t))
23559 (unless (org-with-limited-levels (org-at-heading-p))
23561 (user-error "Cannot move further down"))))
23563 (let* ((elem (org-element-at-point))
23564 (end (org-element-property :end elem))
23565 (parent (org-element-property :parent elem)))
23566 (cond ((and parent (= (org-element-property :contents-end parent) end))
23567 (goto-char (org-element-property :end parent)))
23568 ((integer-or-marker-p end) (goto-char end))
23569 (t (message "No element at point")))))))
23571 (defun org-backward-element ()
23572 "Move backward by one element.
23573 Move to the previous element at the same level, when possible."
23575 (cond ((bobp) (user-error "Cannot move further up"))
23576 ((org-with-limited-levels (org-at-heading-p))
23577 ;; At a headline, move to the previous one, if any, or stay
23579 (let ((origin (point)))
23580 (org-with-limited-levels (org-backward-heading-same-level 1))
23581 ;; When current headline has no sibling above, move to its
23583 (when (= (point) origin)
23584 (or (org-with-limited-levels (org-up-heading-safe))
23585 (progn (goto-char origin)
23586 (user-error "Cannot move further up"))))))
23588 (let* ((trail (org-element-at-point 'keep-trail))
23590 (prev-elem (nth 1 trail))
23591 (beg (org-element-property :begin elem)))
23593 ;; Move to beginning of current element if point isn't
23595 ((null beg) (message "No element at point"))
23596 ((/= (point) beg) (goto-char beg))
23597 (prev-elem (goto-char (org-element-property :begin prev-elem)))
23598 ((org-before-first-heading-p) (goto-char (point-min)))
23599 (t (org-back-to-heading)))))))
23601 (defun org-up-element ()
23602 "Move to upper element."
23604 (if (org-with-limited-levels (org-at-heading-p))
23605 (unless (org-up-heading-safe) (user-error "No surrounding element"))
23606 (let* ((elem (org-element-at-point))
23607 (parent (org-element-property :parent elem)))
23608 (if parent (goto-char (org-element-property :begin parent))
23609 (if (org-with-limited-levels (org-before-first-heading-p))
23610 (user-error "No surrounding element")
23611 (org-with-limited-levels (org-back-to-heading)))))))
23613 (defvar org-element-greater-elements)
23614 (defun org-down-element ()
23615 "Move to inner element."
23617 (let ((element (org-element-at-point)))
23619 ((memq (org-element-type element) '(plain-list table))
23620 (goto-char (org-element-property :contents-begin element))
23622 ((memq (org-element-type element) org-element-greater-elements)
23623 ;; If contents are hidden, first disclose them.
23624 (when (org-element-property :hiddenp element) (org-cycle))
23625 (goto-char (or (org-element-property :contents-begin element)
23626 (user-error "No content for this element"))))
23627 (t (user-error "No inner element")))))
23629 (defun org-drag-element-backward ()
23630 "Move backward element at point."
23632 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
23633 (let* ((trail (org-element-at-point 'keep-trail))
23635 (prev-elem (nth 1 trail)))
23636 ;; Error out if no previous element or previous element is
23637 ;; a parent of the current one.
23638 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
23639 (user-error "Cannot drag element backward")
23640 (let ((pos (point)))
23641 (org-element-swap-A-B prev-elem elem)
23642 (goto-char (+ (org-element-property :begin prev-elem)
23643 (- pos (org-element-property :begin elem)))))))))
23645 (defun org-drag-element-forward ()
23646 "Move forward element at point."
23648 (let* ((pos (point))
23649 (elem (org-element-at-point)))
23650 (when (= (point-max) (org-element-property :end elem))
23651 (user-error "Cannot drag element forward"))
23652 (goto-char (org-element-property :end elem))
23653 (let ((next-elem (org-element-at-point)))
23654 (when (or (org-element-nested-p elem next-elem)
23655 (and (eq (org-element-type next-elem) 'headline)
23656 (not (eq (org-element-type elem) 'headline))))
23658 (user-error "Cannot drag element forward"))
23659 ;; Compute new position of point: it's shifted by NEXT-ELEM
23660 ;; body's length (without final blanks) and by the length of
23661 ;; blanks between ELEM and NEXT-ELEM.
23662 (let ((size-next (- (save-excursion
23663 (goto-char (org-element-property :end next-elem))
23664 (skip-chars-backward " \r\t\n")
23666 ;; Small correction if buffer doesn't end
23667 ;; with a newline character.
23668 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
23669 (org-element-property :begin next-elem)))
23670 (size-blank (- (org-element-property :end elem)
23672 (goto-char (org-element-property :end elem))
23673 (skip-chars-backward " \r\t\n")
23676 (org-element-swap-A-B elem next-elem)
23677 (goto-char (+ pos size-next size-blank))))))
23679 (defun org-drag-line-forward (arg)
23680 "Drag the line at point ARG lines forward."
23682 (dotimes (n (abs arg))
23683 (let ((c (current-column)))
23686 (beginning-of-line 2)
23687 (transpose-lines 1)
23688 (beginning-of-line 0))
23689 (transpose-lines 1)
23690 (beginning-of-line -1))
23691 (org-move-to-column c))))
23693 (defun org-drag-line-backward (arg)
23694 "Drag the line at point ARG lines backward."
23696 (org-drag-line-forward (- arg)))
23698 (defun org-mark-element ()
23699 "Put point at beginning of this element, mark at end.
23701 Interactively, if this command is repeated or (in Transient Mark
23702 mode) if the mark is active, it marks the next element after the
23703 ones already marked."
23705 (let (deactivate-mark)
23706 (if (and (org-called-interactively-p 'any)
23707 (or (and (eq last-command this-command) (mark t))
23708 (and transient-mark-mode mark-active)))
23712 (goto-char (org-element-property :end (org-element-at-point)))))
23713 (let ((element (org-element-at-point)))
23715 (push-mark (org-element-property :end element) t t)
23716 (goto-char (org-element-property :begin element))))))
23718 (defun org-narrow-to-element ()
23719 "Narrow buffer to current element."
23721 (let ((elem (org-element-at-point)))
23723 ((eq (car elem) 'headline)
23725 (org-element-property :begin elem)
23726 (org-element-property :end elem)))
23727 ((memq (car elem) org-element-greater-elements)
23729 (org-element-property :contents-begin elem)
23730 (org-element-property :contents-end elem)))
23733 (org-element-property :begin elem)
23734 (org-element-property :end elem))))))
23736 (defun org-transpose-element ()
23737 "Transpose current and previous elements, keeping blank lines between.
23738 Point is moved after both elements."
23740 (org-skip-whitespace)
23741 (let ((end (org-element-property :end (org-element-at-point))))
23742 (org-drag-element-backward)
23745 (defun org-unindent-buffer ()
23746 "Un-indent the visible part of the buffer.
23747 Relative indentation (between items, inside blocks, etc.) isn't
23750 (unless (eq major-mode 'org-mode)
23751 (user-error "Cannot un-indent a buffer not in Org mode"))
23752 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
23753 unindent-tree ; For byte-compiler.
23759 (if (memq (org-element-type element) '(headline section))
23760 (funcall unindent-tree (org-element-contents element))
23764 (org-element-property :begin element)
23765 (org-element-property :end element))
23766 (org-do-remove-indentation)))))
23767 (reverse contents))))))
23768 (funcall unindent-tree (org-element-contents parse-tree))))
23770 (defun org-show-subtree ()
23771 "Show everything after this heading at deeper levels."
23773 (outline-flag-region
23776 (org-end-of-subtree t t))
23779 (defun org-show-entry ()
23780 "Show the body directly following this heading.
23781 Show the heading too, if it is currently invisible."
23784 (condition-case nil
23786 (org-back-to-heading t)
23787 (outline-flag-region
23788 (max (point-min) (1- (point)))
23790 (if (re-search-forward
23791 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
23792 (match-beginning 1)
23795 (org-cycle-hide-drawers 'children))
23798 (defun org-make-options-regexp (kwds &optional extra)
23799 "Make a regular expression for keyword lines."
23802 (mapconcat 'regexp-quote kwds "\\|")
23803 (if extra (concat "\\|" extra))
23804 "\\):[ \t]*\\(.*\\)"))
23806 ;; Make isearch reveal the necessary context
23807 (defun org-isearch-end ()
23808 "Reveal context after isearch exits."
23809 (when isearch-success ; only if search was successful
23810 (if (featurep 'xemacs)
23811 ;; Under XEmacs, the hook is run in the correct place,
23812 ;; we directly show the context.
23813 (org-show-context 'isearch)
23814 ;; In Emacs the hook runs *before* restoring the overlays.
23815 ;; So we have to use a one-time post-command-hook to do this.
23816 ;; (Emacs 22 has a special variable, see function `org-mode')
23817 (unless (and (boundp 'isearch-mode-end-hook-quit)
23818 isearch-mode-end-hook-quit)
23819 ;; Only when the isearch was not quitted.
23820 (org-add-hook 'post-command-hook 'org-isearch-post-command
23821 'append 'local)))))
23823 (defun org-isearch-post-command ()
23824 "Remove self from hook, and show context."
23825 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
23826 (org-show-context 'isearch))
23829 ;;;; Integration with and fixes for other packages
23833 (defvar org-imenu-markers nil
23834 "All markers currently used by Imenu.")
23835 (make-variable-buffer-local 'org-imenu-markers)
23837 (defun org-imenu-new-marker (&optional pos)
23838 "Return a new marker for use by Imenu, and remember the marker."
23839 (let ((m (make-marker)))
23840 (move-marker m (or pos (point)))
23841 (push m org-imenu-markers)
23844 (defun org-imenu-get-tree ()
23845 "Produce the index for Imenu."
23846 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
23847 (setq org-imenu-markers nil)
23848 (let* ((n org-imenu-depth)
23849 (re (concat "^" (org-get-limited-outline-regexp)))
23850 (subs (make-vector (1+ n) nil))
23852 m level head0 head)
23856 (goto-char (point-max))
23857 (while (re-search-backward re nil t)
23858 (setq level (org-reduced-level (funcall outline-level)))
23859 (when (and (<= level n)
23860 (looking-at org-complex-heading-regexp)
23861 (setq head0 (org-match-string-no-properties 4)))
23862 (setq head (org-link-display-format head0)
23863 m (org-imenu-new-marker))
23864 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
23865 (if (>= level last-level)
23866 (push (cons head m) (aref subs level))
23867 (push (cons head (aref subs (1+ level))) (aref subs level))
23868 (loop for i from (1+ level) to n do (aset subs i nil)))
23869 (setq last-level level)))))
23872 (eval-after-load "imenu"
23874 (add-hook 'imenu-after-jump-hook
23876 (if (derived-mode-p 'org-mode)
23877 (org-show-context 'org-goto))))))
23879 (defun org-link-display-format (link)
23880 "Replace a link with its the description.
23881 If there is no description, use the link target."
23883 (if (string-match org-bracket-link-analytic-regexp link)
23884 (replace-match (if (match-end 5)
23885 (match-string 5 link)
23886 (concat (match-string 1 link)
23887 (match-string 3 link)))
23891 (defun org-toggle-link-display ()
23892 "Toggle the literal or descriptive display of links."
23894 (if org-descriptive-links
23895 (progn (org-remove-from-invisibility-spec '(org-link))
23896 (org-restart-font-lock)
23897 (setq org-descriptive-links nil))
23898 (progn (add-to-invisibility-spec '(org-link))
23899 (org-restart-font-lock)
23900 (setq org-descriptive-links t))))
23902 ;; Speedbar support
23904 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
23905 "Overlay marking the agenda restriction line in speedbar.")
23906 (overlay-put org-speedbar-restriction-lock-overlay
23907 'face 'org-agenda-restriction-lock)
23908 (overlay-put org-speedbar-restriction-lock-overlay
23909 'help-echo "Agendas are currently limited to this item.")
23910 (org-detach-overlay org-speedbar-restriction-lock-overlay)
23912 (defun org-speedbar-set-agenda-restriction ()
23913 "Restrict future agenda commands to the location at point in speedbar.
23914 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
23916 (require 'org-agenda)
23917 (let (p m tp np dir txt)
23919 ((setq p (text-property-any (point-at-bol) (point-at-eol)
23921 (setq m (get-text-property p 'org-imenu-marker))
23922 (with-current-buffer (marker-buffer m)
23924 (org-agenda-set-restriction-lock 'subtree)))
23925 ((setq p (text-property-any (point-at-bol) (point-at-eol)
23926 'speedbar-function 'speedbar-find-file))
23927 (setq tp (previous-single-property-change
23928 (1+ p) 'speedbar-function)
23929 np (next-single-property-change
23930 tp 'speedbar-function)
23931 dir (speedbar-line-directory)
23932 txt (buffer-substring-no-properties (or tp (point-min))
23933 (or np (point-max))))
23934 (with-current-buffer (find-file-noselect
23935 (let ((default-directory dir))
23936 (expand-file-name txt)))
23937 (unless (derived-mode-p 'org-mode)
23938 (user-error "Cannot restrict to non-Org-mode file"))
23939 (org-agenda-set-restriction-lock 'file)))
23940 (t (user-error "Don't know how to restrict Org-mode's agenda")))
23941 (move-overlay org-speedbar-restriction-lock-overlay
23942 (point-at-bol) (point-at-eol))
23943 (setq current-prefix-arg nil)
23944 (org-agenda-maybe-redo)))
23946 (defvar speedbar-file-key-map)
23947 (declare-function speedbar-add-supported-extension "speedbar" (extension))
23948 (eval-after-load "speedbar"
23950 (speedbar-add-supported-extension ".org")
23951 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
23952 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
23953 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
23954 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
23955 (add-hook 'speedbar-visiting-tag-hook
23956 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
23958 ;;; Fixes and Hacks for problems with other packages
23960 ;; Make flyspell not check words in links, to not mess up our keymap
23961 (defvar org-element-affiliated-keywords) ; From org-element.el
23962 (defvar org-element-block-name-alist) ; From org-element.el
23963 (defun org-mode-flyspell-verify ()
23964 "Don't let flyspell put overlays at active buttons, or on
23965 {todo,all-time,additional-option-like}-keywords."
23966 (require 'org-element) ; For `org-element-affiliated-keywords'
23967 (let ((pos (max (1- (point)) (point-min)))
23968 (word (thing-at-point 'word)))
23969 (and (not (get-text-property pos 'keymap))
23970 (not (get-text-property pos 'org-no-flyspell))
23971 (not (member word org-todo-keywords-1))
23972 (not (member word org-all-time-keywords))
23973 (not (member word org-options-keywords))
23974 (not (member word (mapcar 'car org-startup-options)))
23975 (not (member-ignore-case word org-element-affiliated-keywords))
23976 (not (member-ignore-case word (org-get-export-keywords)))
23977 (not (member-ignore-case
23978 word (mapcar 'car org-element-block-name-alist)))
23979 (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
23980 (not (org-in-src-block-p)))))
23982 (defun org-remove-flyspell-overlays-in (beg end)
23983 "Remove flyspell overlays in region."
23984 (and (org-bound-and-true-p flyspell-mode)
23985 (fboundp 'flyspell-delete-region-overlays)
23986 (flyspell-delete-region-overlays beg end))
23987 (add-text-properties beg end '(org-no-flyspell t)))
23989 ;; Make `bookmark-jump' shows the jump location if it was hidden.
23990 (eval-after-load "bookmark"
23991 '(if (boundp 'bookmark-after-jump-hook)
23992 ;; We can use the hook
23993 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
23994 ;; Hook not available, use advice
23995 (defadvice bookmark-jump (after org-make-visible activate)
23996 "Make the position visible."
23997 (org-bookmark-jump-unhide))))
23999 ;; Make sure saveplace shows the location if it was hidden
24000 (eval-after-load "saveplace"
24001 '(defadvice save-place-find-file-hook (after org-make-visible activate)
24002 "Make the position visible."
24003 (org-bookmark-jump-unhide)))
24005 ;; Make sure ecb shows the location if it was hidden
24006 (eval-after-load "ecb"
24007 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
24008 "Make hierarchy visible when jumping into location from ECB tree buffer."
24009 (if (derived-mode-p 'org-mode)
24010 (org-show-context))))
24012 (defun org-bookmark-jump-unhide ()
24013 "Unhide the current position, to show the bookmark location."
24014 (and (derived-mode-p 'org-mode)
24015 (or (outline-invisible-p)
24016 (save-excursion (goto-char (max (point-min) (1- (point))))
24017 (outline-invisible-p)))
24018 (org-show-context 'bookmark-jump)))
24020 ;; Make session.el ignore our circular variable
24021 (defvar session-globals-exclude)
24022 (eval-after-load "session"
24023 '(add-to-list 'session-globals-exclude 'org-mark-ring))
24029 (run-hooks 'org-load-hook)
24031 ;;; org.el ends here