1 ;;; org.el --- Outline-based notes management and organizer
3 ;; Carstens outline-mode for keeping track of everything.
4 ;; Copyright (C) 2004-2014 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 (or (equal this-command
'eval-buffer
)
82 (load (concat (file-name-directory load-file-name
)
86 (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
88 (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
94 (declare-function cdlatex-environment
"ext:cdlatex" (environment item
))
96 ;; `org-outline-regexp' ought to be a defconst but is let-bound in
97 ;; some places -- e.g. see the macro `org-with-limited-levels'.
99 ;; In Org buffers, the value of `outline-regexp' is that of
100 ;; `org-outline-regexp'. The only function still directly relying on
101 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
102 ;; job when `orgstruct-mode' is active.
103 (defvar org-outline-regexp
"\\*+ "
104 "Regexp to match Org headlines.")
106 (defvar org-outline-regexp-bol
"^\\*+ "
107 "Regexp to match Org headlines.
108 This is similar to `org-outline-regexp' but additionally makes
109 sure that we are at the beginning of the line.")
111 (defvar org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
112 "Matches a headline, putting stars and text into groups.
113 Stars are put in group 1 and the trimmed body in group 2.")
115 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
116 (unless (boundp 'calendar-view-holidays-initially-flag
)
117 (org-defvaralias 'calendar-view-holidays-initially-flag
118 'view-calendar-holidays-initially
))
119 (unless (boundp 'calendar-view-diary-initially-flag
)
120 (org-defvaralias 'calendar-view-diary-initially-flag
121 'view-diary-entries-initially
))
122 (unless (boundp 'diary-fancy-buffer
)
123 (org-defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
))
125 (declare-function org-add-archive-files
"org-archive" (files))
127 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
128 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
129 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
130 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
131 (declare-function org-clock-get-last-clock-out-time
"org-clock" ())
132 (declare-function org-clock-timestamps-up
"org-clock" (&optional n
))
133 (declare-function org-clock-timestamps-down
"org-clock" (&optional n
))
134 (declare-function org-clock-remove-overlays
"org-clock" (&optional beg end noremove
))
135 (declare-function org-clock-sum
"org-clock" (&optional tstart tend headline-filter propname
))
136 (declare-function org-clock-sum-current-item
"org-clock" (&optional tstart
))
137 (declare-function org-clock-update-time-maybe
"org-clock" ())
138 (declare-function org-clocktable-shift
"org-clock" (dir n
))
140 (declare-function org-babel-tangle-file
"ob-tangle" (file &optional target-file lang
))
141 (declare-function org-babel-do-in-edit-buffer
"ob-core" (&rest body
))
142 (declare-function orgtbl-mode
"org-table" (&optional arg
))
143 (declare-function org-clock-out
"org-clock" (&optional switch-to-state fail-quietly at-time
))
144 (declare-function org-beamer-mode
"ox-beamer" ())
145 (declare-function org-table-blank-field
"org-table" ())
146 (declare-function org-table-edit-field
"org-table" (arg))
147 (declare-function org-table-insert-row
"org-table" (&optional arg
))
148 (declare-function org-table-justify-field-maybe
"org-table" (&optional new
))
149 (declare-function org-table-set-constants
"org-table" ())
150 (declare-function org-table-calc-current-TBLFM
"org-table" (&optional arg
))
151 (declare-function org-id-get-create
"org-id" (&optional force
))
152 (declare-function org-add-archive-files
"org-archive" (files))
153 (declare-function org-id-find-id-file
"org-id" (id))
154 (declare-function org-tags-view
"org-agenda" (&optional todo-only match
))
155 (declare-function org-agenda-list
"org-agenda" (&optional arg start-day span
))
156 (declare-function org-agenda-redo
"org-agenda" (&optional all
))
157 (declare-function org-table-align
"org-table" ())
158 (declare-function org-table-begin
"org-table" (&optional table-type
))
159 (declare-function org-table-blank-field
"org-table" ())
160 (declare-function org-table-end
"org-table" (&optional table-type
))
161 (declare-function org-table-end-of-field
"org-table" (&optional n
))
162 (declare-function org-table-insert-row
"org-table" (&optional arg
))
163 (declare-function org-table-paste-rectangle
"org-table" ())
164 (declare-function org-table-maybe-eval-formula
"org-table" ())
165 (declare-function org-table-maybe-recalculate-line
"org-table" ())
167 (declare-function org-element-at-point
"org-element" ())
168 (declare-function org-element-cache-reset
"org-element" (&optional all
))
169 (declare-function org-element-cache-refresh
"org-element" (pos))
170 (declare-function org-element-contents
"org-element" (element))
171 (declare-function org-element-context
"org-element" (&optional element
))
172 (declare-function org-element-interpret-data
"org-element"
173 (data &optional parent
))
174 (declare-function org-element-nested-p
"org-element" (elem-a elem-b
))
175 (declare-function org-element-parse-buffer
"org-element"
176 (&optional granularity visible-only
))
177 (declare-function org-element-property
"org-element" (property element
))
178 (declare-function org-element-put-property
"org-element"
179 (element property value
))
180 (declare-function org-element-swap-A-B
"org-element" (elem-a elem-b
))
181 (declare-function org-element-parse-buffer
"org-element"
182 (&optional granularity visible-only
))
183 (declare-function org-element-type
"org-element" (element))
185 (defsubst org-uniquify
(list)
186 "Non-destructively remove duplicate elements from LIST."
187 (let ((res (copy-sequence list
))) (delete-dups res
)))
189 (defsubst org-get-at-bol
(property)
190 "Get text property PROPERTY at the beginning of line."
191 (get-text-property (point-at-bol) property
))
193 (defsubst org-trim
(s)
194 "Remove whitespace at the beginning and the end of string S."
195 (replace-regexp-in-string
197 (replace-regexp-in-string "[ \t\n\r]+\\'" "" s
)))
199 ;; load languages based on value of `org-babel-load-languages'
200 (defvar org-babel-load-languages
)
203 (defun org-babel-do-load-languages (sym value
)
204 "Load the languages defined in `org-babel-load-languages'."
205 (set-default sym value
)
207 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
210 (require (intern (concat "ob-" lang
))))
212 (funcall 'fmakunbound
213 (intern (concat "org-babel-execute:" lang
)))
214 (funcall 'fmakunbound
215 (intern (concat "org-babel-expand-body:" lang
)))))))
216 org-babel-load-languages
))
218 (declare-function org-babel-tangle-file
"ob-tangle" (file &optional target-file lang
))
220 (defun org-babel-load-file (file &optional compile
)
221 "Load Emacs Lisp source code blocks in the Org-mode FILE.
222 This function exports the source code using `org-babel-tangle'
223 and then loads the resulting file using `load-file'. With prefix
224 arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
225 file to byte-code before it is loaded."
226 (interactive "fFile to load: \nP")
227 (let* ((age (lambda (file)
229 (time-subtract (current-time)
230 (nth 5 (or (file-attributes (file-truename file
))
231 (file-attributes file
)))))))
232 (base-name (file-name-sans-extension file
))
233 (exported-file (concat base-name
".el")))
234 ;; tangle if the org-mode file is newer than the elisp file
235 (unless (and (file-exists-p exported-file
)
236 (> (funcall age file
) (funcall age exported-file
)))
238 (car (org-babel-tangle-file file exported-file
"emacs-lisp"))))
241 (progn (byte-compile-file exported-file
'load
)
242 "Compiled and loaded")
243 (progn (load-file exported-file
) "Loaded"))
246 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
247 "Languages which can be evaluated in Org-mode buffers.
248 This list can be used to load support for any of the languages
249 below, note that each language will depend on a different set of
250 system executables and/or Emacs modes. When a language is
251 \"loaded\", then code blocks in that language can be evaluated
252 with `org-babel-execute-src-block' bound by default to C-c
253 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
254 be set to remove code block evaluation from the C-c C-c
255 keybinding. By default only Emacs Lisp (which has no
256 requirements) is loaded."
258 :set
'org-babel-do-load-languages
260 :type
'(alist :tag
"Babel Languages"
263 (const :tag
"Awk" awk
)
266 (const :tag
"Asymptote" asymptote
)
267 (const :tag
"Calc" calc
)
268 (const :tag
"Clojure" clojure
)
269 (const :tag
"CSS" css
)
270 (const :tag
"Ditaa" ditaa
)
271 (const :tag
"Dot" dot
)
272 (const :tag
"Emacs Lisp" emacs-lisp
)
273 (const :tag
"Forth" forth
)
274 (const :tag
"Fortran" fortran
)
275 (const :tag
"Gnuplot" gnuplot
)
276 (const :tag
"Haskell" haskell
)
279 (const :tag
"Java" java
)
280 (const :tag
"Javascript" js
)
281 (const :tag
"LaTeX" latex
)
282 (const :tag
"Ledger" ledger
)
283 (const :tag
"Lilypond" lilypond
)
284 (const :tag
"Lisp" lisp
)
285 (const :tag
"Makefile" makefile
)
286 (const :tag
"Maxima" maxima
)
287 (const :tag
"Matlab" matlab
)
288 (const :tag
"Mscgen" mscgen
)
289 (const :tag
"Ocaml" ocaml
)
290 (const :tag
"Octave" octave
)
291 (const :tag
"Org" org
)
292 (const :tag
"Perl" perl
)
293 (const :tag
"Pico Lisp" picolisp
)
294 (const :tag
"PlantUML" plantuml
)
295 (const :tag
"Python" python
)
296 (const :tag
"Ruby" ruby
)
297 (const :tag
"Sass" sass
)
298 (const :tag
"Scala" scala
)
299 (const :tag
"Scheme" scheme
)
300 (const :tag
"Screen" screen
)
301 (const :tag
"Shell Script" shell
)
302 (const :tag
"Shen" shen
)
303 (const :tag
"Sql" sql
)
304 (const :tag
"Sqlite" sqlite
)
305 (const :tag
"ebnf2ps" ebnf2ps
))
306 :value-type
(boolean :tag
"Activate" :value t
)))
308 ;;;; Customization variables
309 (defcustom org-clone-delete-id nil
310 "Remove ID property of clones of a subtree.
311 When non-nil, clones of a subtree don't inherit the ID property.
312 Otherwise they inherit the ID property with a new unique
322 (defun org-version (&optional here full message
)
323 "Show the org-mode version.
324 Interactively, or when MESSAGE is non-nil, show it in echo area.
325 With prefix argument, or when HERE is non-nil, insert it at point.
326 In non-interactive uses, a reduced version string is output unless
328 (interactive (list current-prefix-arg t
(not current-prefix-arg
)))
329 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
330 (save-load-suffixes (when (boundp 'load-suffixes
) load-suffixes
))
331 (load-suffixes (list ".el"))
332 (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
334 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
335 (org-load-noerror-mustsuffix (concat org-dir
"org-version"))))
336 (load-suffixes save-load-suffixes
)
337 (org-version (org-release))
338 (git-version (org-git-version))
339 (version (format "Org-mode version %s (%s @ %s)"
343 (if (string= org-dir org-install-dir
)
345 (concat "mixed installation! " org-install-dir
" and " org-dir
))
346 "org-loaddefs.el can not be found!")))
347 (version1 (if full version org-version
)))
348 (when here
(insert version1
))
349 (when message
(message "%s" version1
))
352 (defconst org-version
(org-version))
359 (defconst org-block-regexp
360 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
361 "Regular expression for hiding blocks.")
363 (defconst org-dblock-start-re
364 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
365 "Matches the start line of a dynamic block, with parameters.")
367 (defconst org-dblock-end-re
"^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
368 "Matches the end of a dynamic block.")
370 ;;;; Clock and Planning
372 (defconst org-clock-string
"CLOCK:"
373 "String used as prefix for timestamps clocking work hours on an item.")
375 (defconst org-closed-string
"CLOSED:"
376 "String used as the prefix for timestamps logging closing a TODO entry.")
378 (defconst org-deadline-string
"DEADLINE:"
379 "String to mark deadline entries.
380 A deadline is this string, followed by a time stamp. Should be a word,
381 terminated by a colon. You can insert a schedule keyword and
382 a timestamp with \\[org-deadline].")
384 (defconst org-scheduled-string
"SCHEDULED:"
385 "String to mark scheduled TODO entries.
386 A schedule is this string, followed by a time stamp. Should be a word,
387 terminated by a colon. You can insert a schedule keyword and
388 a timestamp with \\[org-schedule].")
390 (defconst org-planning-or-clock-line-re
393 (list org-clock-string org-closed-string org-deadline-string
394 org-scheduled-string
)
396 "Matches a line with planning or clock info.
397 Matched keyword is in group 1.")
401 (defconst org-drawer-regexp
"^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
402 "Matches first or last line of a hidden block.
403 Group 1 contains drawer's name or \"END\".")
405 (defconst org-property-start-re
"^[ \t]*:PROPERTIES:[ \t]*$"
406 "Regular expression matching the first line of a property drawer.")
408 (defconst org-property-end-re
"^[ \t]*:END:[ \t]*$"
409 "Regular expression matching the last line of a property drawer.")
411 (defconst org-clock-drawer-start-re
"^[ \t]*:CLOCK:[ \t]*$"
412 "Regular expression matching the first line of a clock drawer.")
414 (defconst org-clock-drawer-end-re
"^[ \t]*:END:[ \t]*$"
415 "Regular expression matching the last line of a clock drawer.")
417 (defconst org-property-drawer-re
418 (concat "\\(" org-property-start-re
"\\)[^\000]*?\\("
419 org-property-end-re
"\\)\n?")
420 "Matches an entire property drawer.")
422 (defconst org-clock-drawer-re
423 (concat "\\(" org-clock-drawer-start-re
"\\)[^\000]*?\\("
424 org-clock-drawer-end-re
"\\)\n?")
425 "Matches an entire clock drawer.")
429 (defconst org-heading-keyword-regexp-format
430 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
431 "Printf format for a regexp matching a headline with some keyword.
432 This regexp will match the headline of any node which has the
433 exact keyword that is put into the format. The keyword isn't in
434 any group by default, but the stars and the body are.")
436 (defconst org-heading-keyword-maybe-regexp-format
437 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
438 "Printf format for a regexp matching a headline, possibly with some keyword.
439 This regexp can match any headline with the specified keyword, or
440 without a keyword. The keyword isn't in any group by default,
441 but the stars and the body are.")
443 (defconst org-archive-tag
"ARCHIVE"
444 "The tag that marks a subtree as archived.
445 An archived subtree does not open during visibility cycling, and does
446 not contribute to the agenda listings.")
448 (defconst org-comment-string
"COMMENT"
449 "Entries starting with this keyword will never be exported.
450 An entry can be toggled between COMMENT and normal with
451 \\[org-toggle-comment].")
454 ;;;; LaTeX Environments and Fragments
456 (defconst org-latex-regexps
457 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
458 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
459 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
460 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
461 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
462 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
463 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil
)
464 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil
))
465 "Regular expressions for matching embedded LaTeX.")
469 (defconst org-effort-property
"Effort"
470 "The property that is being used to keep track of effort estimates.
471 Effort estimates given in this property need to have the format H:MM.")
475 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
476 "Detect an org-type or table-type table.")
478 (defconst org-table-line-regexp
"^[ \t]*|"
479 "Detect an org-type table line.")
481 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
482 "Detect an org-type table line.")
484 (defconst org-table-hline-regexp
"^[ \t]*|-"
485 "Detect an org-type table hline.")
487 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
488 "Detect a table-type table hline.")
490 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
491 "Detect the first line outside a table when searching from within it.
492 This works for both table types.")
494 (defconst org-TBLFM-regexp
"^[ \t]*#\\+TBLFM: "
495 "Detect a #+TBLFM line.")
499 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
500 "Regular expression for fast time stamp matching.")
502 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
503 "Regular expression for fast time stamp matching.")
505 (defconst org-ts-regexp0
506 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
507 "Regular expression matching time strings for analysis.
508 This one does not require the space after the date, so it can be used
509 on a string that terminates immediately after the date.")
511 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
512 "Regular expression matching time strings for analysis.")
514 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
515 "Regular expression matching time stamps, with groups.")
517 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
518 "Regular expression matching time stamps (also [..]), with groups.")
520 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
521 "Regular expression matching a time stamp range.")
523 (defconst org-tr-regexp-both
524 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
525 "Regular expression matching a time stamp range.")
527 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
528 org-ts-regexp
"\\)?")
529 "Regular expression matching a time stamp or time stamp range.")
531 (defconst org-tsr-regexp-both
532 (concat org-ts-regexp-both
"\\(--?-?"
533 org-ts-regexp-both
"\\)?")
534 "Regular expression matching a time stamp or time stamp range.
535 The time stamps may be either active or inactive.")
537 (defconst org-repeat-re
538 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
539 "Regular expression for specifying repeated events.
540 After a match, group 1 contains the repeat expression.")
542 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
543 "Formats for `format-time-string' which are used for time stamps.")
546 ;;; The custom variables
549 "Outline-based notes management and organizer."
554 (defcustom org-mode-hook nil
555 "Mode hook for Org-mode, run after the mode was turned on."
559 (defcustom org-load-hook nil
560 "Hook that is run after org.el has been loaded."
564 (defcustom org-log-buffer-setup-hook nil
565 "Hook that is run after an Org log buffer is created."
570 (defvar org-modules
) ; defined below
571 (defvar org-modules-loaded nil
572 "Have the modules been loaded already?")
574 (defun org-load-modules-maybe (&optional force
)
575 "Load all extensions listed in `org-modules'."
576 (when (or force
(not org-modules-loaded
))
578 (condition-case nil
(require ext
)
579 (error (message "Problems while trying to load feature `%s'" ext
))))
581 (setq org-modules-loaded t
)))
583 (defun org-set-modules (var value
)
584 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
586 (when (featurep 'org
)
587 (org-load-modules-maybe 'force
)
588 (org-element-cache-reset 'all
)))
590 (defcustom org-modules
'(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail
)
591 "Modules that should always be loaded together with org.el.
593 If a description starts with <C>, the file is not part of Emacs
594 and loading it will require that you have downloaded and properly
595 installed the Org mode distribution.
597 You can also use this system to load external packages (i.e. neither Org
598 core modules, nor modules from the CONTRIB directory). Just add symbols
599 to the end of the list. If the package is called org-xyz.el, then you need
600 to add the symbol `xyz', and the package must have a call to:
604 For export specific modules, see also `org-export-backends'."
606 :set
'org-set-modules
608 :package-version
'(Org .
"8.0")
611 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
612 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
613 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
614 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
615 (const :tag
" docview: Links to doc-view buffers" org-docview
)
616 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
617 (const :tag
" habit: Track your consistency with habits" org-habit
)
618 (const :tag
" id: Global IDs for identifying entries" org-id
)
619 (const :tag
" info: Links to Info nodes" org-info
)
620 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
621 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
622 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
623 (const :tag
" mouse: Additional mouse support" org-mouse
)
624 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
625 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
626 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
628 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
629 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
630 (const :tag
"C bullets: Add overlays to headlines stars" org-bullets
)
631 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
632 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
633 (const :tag
"C collector: Collect properties into tables" org-collector
)
634 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
635 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
636 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
637 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
638 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
639 (const :tag
"C eval: Include command output as text" org-eval
)
640 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
641 (const :tag
"C favtable: Lookup table of favorite references and links" org-favtable
)
642 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
643 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
644 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
645 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
646 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
647 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
648 (const :tag
"C mac-link: Grab links and url from various mac Applications" org-mac-link
)
649 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
650 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
651 (const :tag
"C mew: Links to Mew folders/messages" org-mew
)
652 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
653 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
654 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
655 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
656 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
657 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
658 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
659 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
660 (const :tag
"C track: Keep up with Org-mode development" org-track
)
661 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
662 (const :tag
"C vm: Links to VM folders/messages" org-vm
)
663 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
664 (const :tag
"C wl: Links to Wanderlust folders/messages" org-wl
)
665 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
667 (defvar org-export--registered-backends
) ; From ox.el.
668 (declare-function org-export-derived-backend-p
"ox" (backend &rest backends
))
669 (declare-function org-export-backend-name
"ox" (backend))
670 (defcustom org-export-backends
'(ascii html icalendar latex
)
671 "List of export back-ends that should be always available.
673 If a description starts with <C>, the file is not part of Emacs
674 and loading it will require that you have downloaded and properly
675 installed the Org mode distribution.
677 Unlike to `org-modules', libraries in this list will not be
678 loaded along with Org, but only once the export framework is
681 This variable needs to be set before org.el is loaded. If you
682 need to make a change while Emacs is running, use the customize
683 interface or run the following code, where VAL stands for the new
684 value of the variable, after updating it:
687 \(setq org-export--registered-backends
690 \(let ((name (org-export-backend-name backend)))
694 \(and (org-export-derived-backend-p b name)
695 \(throw 'parentp t)))))))
696 org-export--registered-backends))
697 \(let ((new-list (mapcar 'org-export-backend-name
698 org-export--registered-backends)))
699 \(dolist (backend val)
701 \((not (load (format \"ox-%s\" backend) t t))
702 \(message \"Problems while trying to load export back-end `%s'\"
704 \((not (memq backend new-list)) (push backend new-list))))
705 \(set-default 'org-export-backends new-list)))
707 Adding a back-end to this list will also pull the back-end it
712 :package-version
'(Org .
"8.0")
713 :initialize
'custom-initialize-set
714 :set
(lambda (var val
)
715 (if (not (featurep 'ox
)) (set-default var val
)
716 ;; Any back-end not required anymore (not present in VAL and not
717 ;; a parent of any back-end in the new value) is removed from the
718 ;; list of registered back-ends.
719 (setq org-export--registered-backends
722 (let ((name (org-export-backend-name backend
)))
726 (and (org-export-derived-backend-p b name
)
727 (throw 'parentp t
)))))))
728 org-export--registered-backends
))
729 ;; Now build NEW-LIST of both new back-ends and required
731 (let ((new-list (mapcar 'org-export-backend-name
732 org-export--registered-backends
)))
733 (dolist (backend val
)
735 ((not (load (format "ox-%s" backend
) t t
))
736 (message "Problems while trying to load export back-end `%s'"
738 ((not (memq backend new-list
)) (push backend new-list
))))
739 ;; Set VAR to that list with fixed dependencies.
740 (set-default var new-list
))))
741 :type
'(set :greedy t
742 (const :tag
" ascii Export buffer to ASCII format" ascii
)
743 (const :tag
" beamer Export buffer to Beamer presentation" beamer
)
744 (const :tag
" html Export buffer to HTML format" html
)
745 (const :tag
" icalendar Export buffer to iCalendar format" icalendar
)
746 (const :tag
" latex Export buffer to LaTeX format" latex
)
747 (const :tag
" man Export buffer to MAN format" man
)
748 (const :tag
" md Export buffer to Markdown format" md
)
749 (const :tag
" odt Export buffer to ODT format" odt
)
750 (const :tag
" org Export buffer to Org format" org
)
751 (const :tag
" texinfo Export buffer to Texinfo format" texinfo
)
752 (const :tag
"C confluence Export buffer to Confluence Wiki format" confluence
)
753 (const :tag
"C deck Export buffer to deck.js presentations" deck
)
754 (const :tag
"C freemind Export buffer to Freemind mindmap format" freemind
)
755 (const :tag
"C groff Export buffer to Groff format" groff
)
756 (const :tag
"C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter
)
757 (const :tag
"C RSS 2.0 Export buffer to RSS 2.0 format" rss
)
758 (const :tag
"C s5 Export buffer to s5 presentations" s5
)
759 (const :tag
"C taskjuggler Export buffer to TaskJuggler format" taskjuggler
)))
764 (condition-case nil
(require (intern (format "ox-%s" backend
)))
765 (error (message "Problems while trying to load export back-end `%s'"
767 org-export-backends
))
769 (defcustom org-support-shift-select nil
770 "Non-nil means make shift-cursor commands select text when possible.
772 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
773 start selecting a region, or enlarge regions started in this way.
774 In Org-mode, in special contexts, these same keys are used for
775 other purposes, important enough to compete with shift selection.
776 Org tries to balance these needs by supporting `shift-select-mode'
777 outside these special contexts, under control of this variable.
779 The default of this variable is nil, to avoid confusing behavior. Shifted
780 cursor keys will then execute Org commands in the following contexts:
781 - on a headline, changing TODO state (left/right) and priority (up/down)
782 - on a time stamp, changing the time
783 - in a plain list item, changing the bullet type
784 - in a property definition line, switching between allowed values
785 - in the BEGIN line of a clock table (changing the time block).
786 Outside these contexts, the commands will throw an error.
788 When this variable is t and the cursor is not in a special
789 context, Org-mode will support shift-selection for making and
790 enlarging regions. To make this more effective, the bullet
791 cycling will no longer happen anywhere in an item line, but only
792 if the cursor is exactly on the bullet.
794 If you set this variable to the symbol `always', then the keys
795 will not be special in headlines, property lines, and item lines,
796 to make shift selection work there as well. If this is what you
797 want, you can use the following alternative commands: `C-c C-t'
798 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
799 can be used to switch TODO sets, `C-c -' to cycle item bullet
800 types, and properties can be edited by hand or in column view.
802 However, when the cursor is on a timestamp, shift-cursor commands
803 will still edit the time stamp - this is just too good to give up.
805 XEmacs user should have this variable set to nil, because
806 `shift-select-mode' is in Emacs 23 or later only."
809 (const :tag
"Never" nil
)
810 (const :tag
"When outside special context" t
)
811 (const :tag
"Everywhere except timestamps" always
)))
813 (defcustom org-loop-over-headlines-in-active-region nil
814 "Shall some commands act upon headlines in the active region?
816 When set to `t', some commands will be performed in all headlines
817 within the active region.
819 When set to `start-level', some commands will be performed in all
820 headlines within the active region, provided that these headlines
821 are of the same level than the first one.
823 When set to a string, those commands will be performed on the
824 matching headlines within the active region. Such string must be
825 a tags/property/todo match as it is used in the agenda tags view.
827 The list of commands is: `org-schedule', `org-deadline',
828 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
829 `org-archive-to-archive-sibling'. The archiving commands skip
830 already archived entries."
831 :type
'(choice (const :tag
"Don't loop" nil
)
832 (const :tag
"All headlines in active region" t
)
833 (const :tag
"In active region, headlines at the same level than the first one" start-level
)
834 (string :tag
"Tags/Property/Todo matcher"))
839 (defgroup org-startup nil
840 "Options concerning startup of Org-mode."
844 (defcustom org-startup-folded t
845 "Non-nil means entering Org-mode will switch to OVERVIEW.
846 This can also be configured on a per-file basis by adding one of
847 the following lines anywhere in the buffer:
849 #+STARTUP: fold (or `overview', this is equivalent)
850 #+STARTUP: nofold (or `showall', this is equivalent)
852 #+STARTUP: showeverything
854 By default, this option is ignored when Org opens agenda files
855 for the first time. If you want the agenda to honor the startup
856 option, set `org-agenda-inhibit-startup' to nil."
859 (const :tag
"nofold: show all" nil
)
860 (const :tag
"fold: overview" t
)
861 (const :tag
"content: all headlines" content
)
862 (const :tag
"show everything, even drawers" showeverything
)))
864 (defcustom org-startup-truncated t
865 "Non-nil means entering Org-mode will set `truncate-lines'.
866 This is useful since some lines containing links can be very long and
867 uninteresting. Also tables look terrible when wrapped."
871 (defcustom org-startup-indented nil
872 "Non-nil means turn on `org-indent-mode' on startup.
873 This can also be configured on a per-file basis by adding one of
874 the following lines anywhere in the buffer:
878 :group
'org-structure
880 (const :tag
"Not" nil
)
881 (const :tag
"Globally (slow on startup in large files)" t
)))
883 (defcustom org-use-sub-superscripts t
884 "Non-nil means interpret \"_\" and \"^\" for display.
886 If you want to control how Org exports those characters, see
887 `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
888 used to be an alias for `org-export-with-sub-superscripts' in
889 Org <8.0, it is not anymore.
891 When this option is turned on, you can use TeX-like syntax for
892 sub- and superscripts within the buffer. Several characters after
893 \"_\" or \"^\" will be considered as a single item - so grouping
894 with {} is normally not needed. For example, the following things
895 will be parsed as single sub- or superscripts:
897 10^24 or 10^tau several digits will be considered 1 item.
898 10^-12 or 10^-tau a leading sign with digits or a word
899 x^2-y^3 will be read as x^2 - y^3, because items are
900 terminated by almost any nonword/nondigit char.
901 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
903 Still, ambiguity is possible. So when in doubt, use {} to enclose
904 the sub/superscript. If you set this variable to the symbol `{}',
905 the braces are *required* in order to trigger interpretations as
906 sub/superscript. This can be helpful in documents that need \"_\"
907 frequently in plain text."
910 :package-version
'(Org .
"8.0")
912 (const :tag
"Always interpret" t
)
913 (const :tag
"Only with braces" {})
914 (const :tag
"Never interpret" nil
)))
916 (defcustom org-startup-with-beamer-mode nil
917 "Non-nil means turn on `org-beamer-mode' on startup.
918 This can also be configured on a per-file basis by adding one of
919 the following lines anywhere in the buffer:
926 (defcustom org-startup-align-all-tables nil
927 "Non-nil means align all tables when visiting a file.
928 This is useful when the column width in tables is forced with <N> cookies
929 in table fields. Such tables will look correct only after the first re-align.
930 This can also be configured on a per-file basis by adding one of
931 the following lines anywhere in the buffer:
937 (defcustom org-startup-with-inline-images nil
938 "Non-nil means show inline images when loading a new Org file.
939 This can also be configured on a per-file basis by adding one of
940 the following lines anywhere in the buffer:
941 #+STARTUP: inlineimages
942 #+STARTUP: noinlineimages"
947 (defcustom org-startup-with-latex-preview nil
948 "Non-nil means preview LaTeX fragments when loading a new Org file.
950 This can also be configured on a per-file basis by adding one of
951 the following lines anywhere in the buffer:
952 #+STARTUP: latexpreview
953 #+STARTUP: nolatexpreview"
956 :package-version
'(Org .
"8.0")
959 (defcustom org-insert-mode-line-in-empty-file nil
960 "Non-nil means insert the first line setting Org-mode in empty files.
961 When the function `org-mode' is called interactively in an empty file, this
962 normally means that the file name does not automatically trigger Org-mode.
963 To ensure that the file will always be in Org-mode in the future, a
964 line enforcing Org-mode will be inserted into the buffer, if this option
969 (defcustom org-replace-disputed-keys nil
970 "Non-nil means use alternative key bindings for some keys.
971 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
972 These keys are also used by other packages like shift-selection-mode'
973 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
974 If you want to use Org-mode together with one of these other modes,
975 or more generally if you would like to move some Org-mode commands to
976 other keys, set this variable and configure the keys with the variable
979 This option is only relevant at load-time of Org-mode, and must be set
980 *before* org.el is loaded. Changing it requires a restart of Emacs to
985 (defcustom org-use-extra-keys nil
986 "Non-nil means use extra key sequence definitions for certain commands.
987 This happens automatically if you run XEmacs or if `window-system'
988 is nil. This variable lets you do the same manually. You must
989 set it before loading org.
991 Example: on Carbon Emacs 22 running graphically, with an external
992 keyboard on a Powerbook, the default way of setting M-left might
993 not work for either Alt or ESC. Setting this variable will make
998 (org-defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
)
1000 (defcustom org-disputed-keys
1001 '(([(shift up
)] .
[(meta p
)])
1002 ([(shift down
)] .
[(meta n
)])
1003 ([(shift left
)] .
[(meta -
)])
1004 ([(shift right
)] .
[(meta +)])
1005 ([(control shift right
)] .
[(meta shift
+)])
1006 ([(control shift left
)] .
[(meta shift -
)]))
1007 "Keys for which Org-mode and other modes compete.
1008 This is an alist, cars are the default keys, second element specifies
1009 the alternative to use when `org-replace-disputed-keys' is t.
1011 Keys can be specified in any syntax supported by `define-key'.
1012 The value of this option takes effect only at Org-mode's startup,
1013 therefore you'll have to restart Emacs to apply it after changing."
1017 (defun org-key (key)
1018 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
1019 Or return the original if not disputed.
1020 Also apply the translations defined in `org-xemacs-key-equivalents'."
1021 (when org-replace-disputed-keys
1022 (let* ((nkey (key-description key
))
1023 (x (org-find-if (lambda (x)
1024 (equal (key-description (car x
)) nkey
))
1025 org-disputed-keys
)))
1026 (setq key
(if x
(cdr x
) key
))))
1027 (when (featurep 'xemacs
)
1028 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
1031 (defun org-find-if (predicate seq
)
1034 (if (funcall predicate
(car seq
))
1035 (throw 'exit
(car seq
))
1038 (defun org-defkey (keymap key def
)
1039 "Define a key, possibly translated, as returned by `org-key'."
1040 (define-key keymap
(org-key key
) def
))
1042 (defcustom org-ellipsis nil
1043 "The ellipsis to use in the Org-mode outline.
1044 When nil, just use the standard three dots.
1045 When a string, use that string instead.
1046 When a face, use the standard 3 dots, but with the specified face.
1047 The change affects only Org-mode (which will then use its own display table).
1048 Changing this requires executing \\[org-mode] in a buffer to become
1051 :type
'(choice (const :tag
"Default" nil
)
1052 (face :tag
"Face" :value org-warning
)
1053 (string :tag
"String" :value
"...#")))
1055 (defvar org-display-table nil
1056 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
1058 (defgroup org-keywords nil
1059 "Keywords in Org-mode."
1063 (defcustom org-closed-keep-when-no-todo nil
1064 "Remove CLOSED: time-stamp when switching back to a non-todo state?"
1066 :group
'org-keywords
1068 :package-version
'(Org .
"8.0")
1071 (defgroup org-structure nil
1072 "Options concerning the general structure of Org-mode files."
1073 :tag
"Org Structure"
1076 (defgroup org-reveal-location nil
1077 "Options about how to make context of a location visible."
1078 :tag
"Org Reveal Location"
1079 :group
'org-structure
)
1081 (defconst org-context-choice
1083 (const :tag
"Always" t
)
1084 (const :tag
"Never" nil
)
1085 (repeat :greedy t
:tag
"Individual contexts"
1087 (choice :tag
"Context"
1094 (const bookmark-jump
)
1098 "Contexts for the reveal options.")
1100 (defcustom org-show-hierarchy-above
'((default . t
))
1101 "Non-nil means show full hierarchy when revealing a location.
1102 Org-mode often shows locations in an org-mode file which might have
1103 been invisible before. When this is set, the hierarchy of headings
1104 above the exposed location is shown.
1105 Turning this off for example for sparse trees makes them very compact.
1106 Instead of t, this can also be an alist specifying this option for different
1107 contexts. Valid contexts are
1108 agenda when exposing an entry from the agenda
1109 org-goto when using the command `org-goto' on key C-c C-j
1110 occur-tree when using the command `org-occur' on key C-c /
1111 tags-tree when constructing a sparse tree based on tags matches
1112 link-search when exposing search matches associated with a link
1113 mark-goto when exposing the jump goal of a mark
1114 bookmark-jump when exposing a bookmark location
1115 isearch when exiting from an incremental search
1116 default default for all contexts not set explicitly"
1117 :group
'org-reveal-location
1118 :type org-context-choice
)
1120 (defcustom org-show-following-heading
'((default . nil
))
1121 "Non-nil means show following heading when revealing a location.
1122 Org-mode often shows locations in an org-mode file which might have
1123 been invisible before. When this is set, the heading following the
1125 Turning this off for example for sparse trees makes them very compact,
1126 but makes it harder to edit the location of the match. In such a case,
1127 use the command \\[org-reveal] to show more context.
1128 Instead of t, this can also be an alist specifying this option for different
1129 contexts. See `org-show-hierarchy-above' for valid contexts."
1130 :group
'org-reveal-location
1131 :type org-context-choice
)
1133 (defcustom org-show-siblings
'((default . nil
) (isearch t
) (bookmark-jump t
))
1134 "Non-nil means show all sibling heading when revealing a location.
1135 Org-mode often shows locations in an org-mode file which might have
1136 been invisible before. When this is set, the sibling of the current entry
1137 heading are all made visible. If `org-show-hierarchy-above' is t,
1138 the same happens on each level of the hierarchy above the current entry.
1140 By default this is on for the isearch context, off for all other contexts.
1141 Turning this off for example for sparse trees makes them very compact,
1142 but makes it harder to edit the location of the match. In such a case,
1143 use the command \\[org-reveal] to show more context.
1144 Instead of t, this can also be an alist specifying this option for different
1145 contexts. See `org-show-hierarchy-above' for valid contexts."
1146 :group
'org-reveal-location
1147 :type org-context-choice
1149 :package-version
'(Org .
"8.0"))
1151 (defcustom org-show-entry-below
'((default . nil
))
1152 "Non-nil means show the entry below a headline when revealing a location.
1153 Org-mode often shows locations in an org-mode file which might have
1154 been invisible before. When this is set, the text below the headline that is
1155 exposed is also shown.
1157 By default this is off for all contexts.
1158 Instead of t, this can also be an alist specifying this option for different
1159 contexts. See `org-show-hierarchy-above' for valid contexts."
1160 :group
'org-reveal-location
1161 :type org-context-choice
)
1163 (defcustom org-indirect-buffer-display
'other-window
1164 "How should indirect tree buffers be displayed?
1165 This applies to indirect buffers created with the commands
1166 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
1168 current-window Display in the current window
1169 other-window Just display in another window.
1170 dedicated-frame Create one new frame, and re-use it each time.
1171 new-frame Make a new frame each time. Note that in this case
1172 previously-made indirect buffers are kept, and you need to
1173 kill these buffers yourself."
1174 :group
'org-structure
1175 :group
'org-agenda-windows
1177 (const :tag
"In current window" current-window
)
1178 (const :tag
"In current frame, other window" other-window
)
1179 (const :tag
"Each time a new frame" new-frame
)
1180 (const :tag
"One dedicated frame" dedicated-frame
)))
1182 (defcustom org-use-speed-commands nil
1183 "Non-nil means activate single letter commands at beginning of a headline.
1184 This may also be a function to test for appropriate locations where speed
1185 commands should be active.
1187 For example, to activate speed commands when the point is on any
1188 star at the beginning of the headline, you can do this:
1190 (setq org-use-speed-commands
1191 (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
1192 :group
'org-structure
1194 (const :tag
"Never" nil
)
1195 (const :tag
"At beginning of headline stars" t
)
1198 (defcustom org-speed-commands-user nil
1199 "Alist of additional speed commands.
1200 This list will be checked before `org-speed-commands-default'
1201 when the variable `org-use-speed-commands' is non-nil
1202 and when the cursor is at the beginning of a headline.
1203 The car if each entry is a string with a single letter, which must
1204 be assigned to `self-insert-command' in the global map.
1205 The cdr is either a command to be called interactively, a function
1206 to be called, or a form to be evaluated.
1207 An entry that is just a list with a single string will be interpreted
1208 as a descriptive headline that will be added when listing the speed
1209 commands in the Help buffer using the `?' speed command."
1210 :group
'org-structure
1211 :type
'(repeat :value
("k" . ignore
)
1212 (choice :value
("k" . ignore
)
1213 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
1214 (cons :tag
"Letter and Command"
1215 (string :tag
"Command letter")
1220 (defcustom org-bookmark-names-plist
1221 '(:last-capture
"org-capture-last-stored"
1222 :last-refile
"org-refile-last-stored"
1223 :last-capture-marker
"org-capture-last-stored-marker")
1224 "Names for bookmarks automatically set by some Org commands.
1225 This can provide strings as names for a number of bookmarks Org sets
1226 automatically. The following keys are currently implemented:
1228 :last-capture-marker
1230 When a key does not show up in the property list, the corresponding bookmark
1232 :group
'org-structure
1235 (defgroup org-cycle nil
1236 "Options concerning visibility cycling in Org-mode."
1238 :group
'org-structure
)
1240 (defcustom org-cycle-skip-children-state-if-no-children t
1241 "Non-nil means skip CHILDREN state in entries that don't have any."
1245 (defcustom org-cycle-max-level nil
1246 "Maximum level which should still be subject to visibility cycling.
1247 Levels higher than this will, for cycling, be treated as text, not a headline.
1248 When `org-odd-levels-only' is set, a value of N in this variable actually
1249 means 2N-1 stars as the limiting headline.
1250 When nil, cycle all levels.
1251 Note that the limiting level of cycling is also influenced by
1252 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
1253 `org-inlinetask-min-level' is, cycling will be limited to levels one less
1257 (const :tag
"No limit" nil
)
1258 (integer :tag
"Maximum level")))
1260 (defcustom org-hide-block-startup nil
1261 "Non-nil means entering Org-mode will fold all blocks.
1262 This can also be set in on a per-file basis with
1264 #+STARTUP: hideblocks
1265 #+STARTUP: showblocks"
1270 (defcustom org-cycle-global-at-bob nil
1271 "Cycle globally if cursor is at beginning of buffer and not at a headline.
1272 This makes it possible to do global cycling without having to use S-TAB or
1273 \\[universal-argument] TAB. For this special case to work, the first line
1274 of the buffer must not be a headline -- it may be empty or some other text.
1275 When used in this way, `org-cycle-hook' is disabled temporarily to make
1276 sure the cursor stays at the beginning of the buffer. When this option is
1277 nil, don't do anything special at the beginning of the buffer."
1281 (defcustom org-cycle-level-after-item
/entry-creation t
1282 "Non-nil means cycle entry level or item indentation in new empty entries.
1284 When the cursor is at the end of an empty headline, i.e., with only stars
1285 and maybe a TODO keyword, TAB will then switch the entry to become a child,
1286 and then all possible ancestor states, before returning to the original state.
1287 This makes data entry extremely fast: M-RET to create a new headline,
1288 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
1290 When the cursor is at the end of an empty plain list item, one TAB will
1291 make it a subitem, two or more tabs will back up to make this an item
1292 higher up in the item hierarchy."
1296 (defcustom org-cycle-emulate-tab t
1297 "Where should `org-cycle' emulate TAB.
1299 white Only in completely white lines
1300 whitestart Only at the beginning of lines, before the first non-white char
1301 t Everywhere except in headlines
1302 exc-hl-bol Everywhere except at the start of a headline
1303 If TAB is used in a place where it does not emulate TAB, the current subtree
1304 visibility is cycled."
1306 :type
'(choice (const :tag
"Never" nil
)
1307 (const :tag
"Only in completely white lines" white
)
1308 (const :tag
"Before first char in a line" whitestart
)
1309 (const :tag
"Everywhere except in headlines" t
)
1310 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)))
1312 (defcustom org-cycle-separator-lines
2
1313 "Number of empty lines needed to keep an empty line between collapsed trees.
1314 If you leave an empty line between the end of a subtree and the following
1315 headline, this empty line is hidden when the subtree is folded.
1316 Org-mode will leave (exactly) one empty line visible if the number of
1317 empty lines is equal or larger to the number given in this variable.
1318 So the default 2 means at least 2 empty lines after the end of a subtree
1319 are needed to produce free space between a collapsed subtree and the
1322 If the number is negative, and the number of empty lines is at least -N,
1323 all empty lines are shown.
1325 Special case: when 0, never leave empty lines in collapsed view."
1328 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
1330 (defcustom org-pre-cycle-hook nil
1331 "Hook that is run before visibility cycling is happening.
1332 The function(s) in this hook must accept a single argument which indicates
1333 the new state that will be set right after running this hook. The
1334 argument is a symbol. Before a global state change, it can have the values
1335 `overview', `content', or `all'. Before a local state change, it can have
1336 the values `folded', `children', or `subtree'."
1340 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
1341 org-cycle-hide-drawers
1342 org-cycle-hide-inline-tasks
1343 org-cycle-show-empty-lines
1344 org-optimize-window-after-visibility-change
)
1345 "Hook that is run after `org-cycle' has changed the buffer visibility.
1346 The function(s) in this hook must accept a single argument which indicates
1347 the new state that was set by the most recent `org-cycle' command. The
1348 argument is a symbol. After a global state change, it can have the values
1349 `overview', `contents', or `all'. After a local state change, it can have
1350 the values `folded', `children', or `subtree'."
1354 (defgroup org-edit-structure nil
1355 "Options concerning structure editing in Org-mode."
1356 :tag
"Org Edit Structure"
1357 :group
'org-structure
)
1359 (defcustom org-odd-levels-only nil
1360 "Non-nil means skip even levels and only use odd levels for the outline.
1361 This has the effect that two stars are being added/taken away in
1362 promotion/demotion commands. It also influences how levels are
1363 handled by the exporters.
1364 Changing it requires restart of `font-lock-mode' to become effective
1365 for fontification also in regions already fontified.
1366 You may also set this on a per-file basis by adding one of the following
1367 lines to the buffer:
1371 :group
'org-edit-structure
1372 :group
'org-appearance
1375 (defcustom org-adapt-indentation t
1376 "Non-nil means adapt indentation to outline node level.
1378 When this variable is set, Org assumes that you write outlines by
1379 indenting text in each node to align with the headline (after the stars).
1380 The following issues are influenced by this variable:
1382 - When this is set and the *entire* text in an entry is indented, the
1383 indentation is increased by one space in a demotion command, and
1384 decreased by one in a promotion command. If any line in the entry
1385 body starts with text at column 0, indentation is not changed at all.
1387 - Property drawers and planning information is inserted indented when
1388 this variable s set. When nil, they will not be indented.
1390 - TAB indents a line relative to context. The lines below a headline
1391 will be indented when this variable is set.
1393 Note that this is all about true indentation, by adding and removing
1394 space characters. See also `org-indent.el' which does level-dependent
1395 indentation in a virtual way, i.e. at display time in Emacs."
1396 :group
'org-edit-structure
1399 (defcustom org-special-ctrl-a
/e nil
1400 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1402 When t, `C-a' will bring back the cursor to the beginning of the
1403 headline text, i.e. after the stars and after a possible TODO
1404 keyword. In an item, this will be the position after bullet and
1405 check-box, if any. When the cursor is already at that position,
1406 another `C-a' will bring it to the beginning of the line.
1408 `C-e' will jump to the end of the headline, ignoring the presence
1409 of tags in the headline. A second `C-e' will then jump to the
1410 true end of the line, after any tags. This also means that, when
1411 this variable is non-nil, `C-e' also will never jump beyond the
1412 end of the heading of a folded section, i.e. not after the
1415 When set to the symbol `reversed', the first `C-a' or `C-e' works
1416 normally, going to the true line boundary first. Only a directly
1417 following, identical keypress will bring the cursor to the
1420 This may also be a cons cell where the behavior for `C-a' and
1421 `C-e' is set separately."
1422 :group
'org-edit-structure
1424 (const :tag
"off" nil
)
1425 (const :tag
"on: after stars/bullet and before tags first" t
)
1426 (const :tag
"reversed: true line boundary first" reversed
)
1427 (cons :tag
"Set C-a and C-e separately"
1428 (choice :tag
"Special C-a"
1429 (const :tag
"off" nil
)
1430 (const :tag
"on: after stars/bullet first" t
)
1431 (const :tag
"reversed: before stars/bullet first" reversed
))
1432 (choice :tag
"Special C-e"
1433 (const :tag
"off" nil
)
1434 (const :tag
"on: before tags first" t
)
1435 (const :tag
"reversed: after tags first" reversed
)))))
1436 (org-defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
)
1438 (defcustom org-special-ctrl-k nil
1439 "Non-nil means `C-k' will behave specially in headlines.
1440 When nil, `C-k' will call the default `kill-line' command.
1441 When t, the following will happen while the cursor is in the headline:
1443 - When the cursor is at the beginning of a headline, kill the entire
1444 line and possible the folded subtree below the line.
1445 - When in the middle of the headline text, kill the headline up to the tags.
1446 - When after the headline text, kill the tags."
1447 :group
'org-edit-structure
1450 (defcustom org-ctrl-k-protect-subtree nil
1451 "Non-nil means, do not delete a hidden subtree with C-k.
1452 When set to the symbol `error', simply throw an error when C-k is
1453 used to kill (part-of) a headline that has hidden text behind it.
1454 Any other non-nil value will result in a query to the user, if it is
1455 OK to kill that hidden subtree. When nil, kill without remorse."
1456 :group
'org-edit-structure
1459 (const :tag
"Do not protect hidden subtrees" nil
)
1460 (const :tag
"Protect hidden subtrees with a security query" t
)
1461 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1463 (defcustom org-special-ctrl-o t
1464 "Non-nil means, make `C-o' insert a row in tables."
1465 :group
'org-edit-structure
1468 (defcustom org-catch-invisible-edits nil
1469 "Check if in invisible region before inserting or deleting a character.
1472 nil Do not check, so just do invisible edits.
1473 error Throw an error and do nothing.
1474 show Make point visible, and do the requested edit.
1475 show-and-error Make point visible, then throw an error and abort the edit.
1476 smart Make point visible, and do insertion/deletion if it is
1477 adjacent to visible text and the change feels predictable.
1478 Never delete a previously invisible character or add in the
1479 middle or right after an invisible region. Basically, this
1480 allows insertion and backward-delete right before ellipses.
1481 FIXME: maybe in this case we should not even show?"
1482 :group
'org-edit-structure
1485 (const :tag
"Do not check" nil
)
1486 (const :tag
"Throw error when trying to edit" error
)
1487 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1488 (const :tag
"Show invisible part and do the edit" show
)
1489 (const :tag
"Be smart and do the right thing" smart
)))
1491 (defcustom org-yank-folded-subtrees t
1492 "Non-nil means when yanking subtrees, fold them.
1493 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1494 it starts with a heading and all other headings in it are either children
1495 or siblings, then fold all the subtrees. However, do this only if no
1496 text after the yank would be swallowed into a folded tree by this action."
1497 :group
'org-edit-structure
1500 (defcustom org-yank-adjusted-subtrees nil
1501 "Non-nil means when yanking subtrees, adjust the level.
1502 With this setting, `org-paste-subtree' is used to insert the subtree, see
1503 this function for details."
1504 :group
'org-edit-structure
1507 (defcustom org-M-RET-may-split-line
'((default . t
))
1508 "Non-nil means M-RET will split the line at the cursor position.
1509 When nil, it will go to the end of the line before making a
1511 You may also set this option in a different way for different
1512 contexts. Valid contexts are:
1514 headline when creating a new headline
1515 item when creating a new item
1516 table in a table field
1517 default the value to be used for all contexts not explicitly
1519 :group
'org-structure
1522 (const :tag
"Always" t
)
1523 (const :tag
"Never" nil
)
1524 (repeat :greedy t
:tag
"Individual contexts"
1526 (choice :tag
"Context"
1534 (defcustom org-insert-heading-respect-content nil
1535 "Non-nil means insert new headings after the current subtree.
1536 When nil, the new heading is created directly after the current line.
1537 The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
1538 this variable on for the duration of the command."
1539 :group
'org-structure
1542 (defcustom org-blank-before-new-entry
'((heading . auto
)
1543 (plain-list-item . auto
))
1544 "Should `org-insert-heading' leave a blank line before new heading/item?
1545 The value is an alist, with `heading' and `plain-list-item' as CAR,
1546 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1547 which case Org will look at the surrounding headings/items and try to
1548 make an intelligent decision whether to insert a blank line or not.
1550 For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
1551 the setting here is ignored and no empty line is inserted to avoid breaking
1552 the list structure."
1553 :group
'org-edit-structure
1555 (cons (const heading
)
1556 (choice (const :tag
"Never" nil
)
1557 (const :tag
"Always" t
)
1558 (const :tag
"Auto" auto
)))
1559 (cons (const plain-list-item
)
1560 (choice (const :tag
"Never" nil
)
1561 (const :tag
"Always" t
)
1562 (const :tag
"Auto" auto
)))))
1564 (defcustom org-insert-heading-hook nil
1565 "Hook being run after inserting a new heading."
1566 :group
'org-edit-structure
1569 (defcustom org-enable-fixed-width-editor t
1570 "Non-nil means lines starting with \":\" are treated as fixed-width.
1571 This currently only means they are never auto-wrapped.
1572 When nil, such lines will be treated like ordinary lines."
1573 :group
'org-edit-structure
1576 (defcustom org-goto-auto-isearch t
1577 "Non-nil means typing characters in `org-goto' starts incremental search.
1578 When nil, you can use these keybindings to navigate the buffer:
1580 q Quit the org-goto interface
1581 n Go to the next visible heading
1582 p Go to the previous visible heading
1583 f Go one heading forward on same level
1584 b Go one heading backward on same level
1585 u Go one heading up"
1586 :group
'org-edit-structure
1589 (defgroup org-sparse-trees nil
1590 "Options concerning sparse trees in Org-mode."
1591 :tag
"Org Sparse Trees"
1592 :group
'org-structure
)
1594 (defcustom org-highlight-sparse-tree-matches t
1595 "Non-nil means highlight all matches that define a sparse tree.
1596 The highlights will automatically disappear the next time the buffer is
1597 changed by an edit command."
1598 :group
'org-sparse-trees
1601 (defcustom org-remove-highlights-with-change t
1602 "Non-nil means any change to the buffer will remove temporary highlights.
1603 Such highlights are created by `org-occur' and `org-clock-display'.
1604 When nil, `C-c C-c' needs to be used to get rid of the highlights.
1605 The highlights created by `org-toggle-latex-fragment' always need
1606 `C-c C-x C-l' to be removed."
1607 :group
'org-sparse-trees
1612 (defcustom org-occur-hook
'(org-first-headline-recenter)
1613 "Hook that is run after `org-occur' has constructed a sparse tree.
1614 This can be used to recenter the window to show as much of the structure
1616 :group
'org-sparse-trees
1619 (defgroup org-imenu-and-speedbar nil
1620 "Options concerning imenu and speedbar in Org-mode."
1621 :tag
"Org Imenu and Speedbar"
1622 :group
'org-structure
)
1624 (defcustom org-imenu-depth
2
1625 "The maximum level for Imenu access to Org-mode headlines.
1626 This also applied for speedbar access."
1627 :group
'org-imenu-and-speedbar
1630 (defgroup org-table nil
1631 "Options concerning tables in Org-mode."
1635 (defcustom org-enable-table-editor
'optimized
1636 "Non-nil means lines starting with \"|\" are handled by the table editor.
1637 When nil, such lines will be treated like ordinary lines.
1639 When equal to the symbol `optimized', the table editor will be optimized to
1641 - Automatic overwrite mode in front of whitespace in table fields.
1642 This makes the structure of the table stay in tact as long as the edited
1643 field does not exceed the column width.
1644 - Minimize the number of realigns. Normally, the table is aligned each time
1645 TAB or RET are pressed to move to another field. With optimization this
1646 happens only if changes to a field might have changed the column width.
1647 Optimization requires replacing the functions `self-insert-command',
1648 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1649 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1650 very good at guessing when a re-align will be necessary, but you can always
1651 force one with \\[org-ctrl-c-ctrl-c].
1653 If you would like to use the optimized version in Org-mode, but the
1654 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1656 This variable can be used to turn on and off the table editor during a session,
1657 but in order to toggle optimization, a restart is required.
1659 See also the variable `org-table-auto-blank-field'."
1662 (const :tag
"off" nil
)
1664 (const :tag
"on, optimized" optimized
)))
1666 (defcustom org-self-insert-cluster-for-undo
(or (featurep 'xemacs
)
1667 (version<= emacs-version
"24.1"))
1668 "Non-nil means cluster self-insert commands for undo when possible.
1669 If this is set, then, like in the Emacs command loop, 20 consecutive
1670 characters will be undone together.
1671 This is configurable, because there is some impact on typing performance."
1675 (defcustom org-table-tab-recognizes-table.el t
1676 "Non-nil means TAB will automatically notice a table.el table.
1677 When it sees such a table, it moves point into it and - if necessary -
1678 calls `table-recognize-table'."
1679 :group
'org-table-editing
1682 (defgroup org-link nil
1683 "Options concerning links in Org-mode."
1687 (defvar org-link-abbrev-alist-local nil
1688 "Buffer-local version of `org-link-abbrev-alist', which see.
1689 The value of this is taken from the #+LINK lines.")
1690 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1692 (defcustom org-link-abbrev-alist nil
1693 "Alist of link abbreviations.
1694 The car of each element is a string, to be replaced at the start of a link.
1695 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1696 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1698 [[linkkey:tag][description]]
1700 The 'linkkey' must be a word word, starting with a letter, followed
1701 by letters, numbers, '-' or '_'.
1703 If REPLACE is a string, the tag will simply be appended to create the link.
1704 If the string contains \"%s\", the tag will be inserted there. If the string
1705 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1706 at that point (see the function `url-hexify-string'). If the string contains
1707 the specifier \"%(my-function)\", then the custom function `my-function' will
1708 be invoked: this function takes the tag as its only argument and must return
1711 REPLACE may also be a function that will be called with the tag as the
1712 only argument to create the link, which should be returned as a string.
1714 See the manual for examples."
1718 (string :tag
"Protocol")
1720 (string :tag
"Format")
1723 (defcustom org-descriptive-links t
1724 "Non-nil means Org will display descriptive links.
1725 E.g. [[http://orgmode.org][Org website]] will be displayed as
1726 \"Org Website\", hiding the link itself and just displaying its
1727 description. When set to `nil', Org will display the full links
1730 You can interactively set the value of this variable by calling
1731 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1735 (defcustom org-link-file-path-type
'adaptive
1736 "How the path name in file links should be stored.
1739 relative Relative to the current directory, i.e. the directory of the file
1740 into which the link is being inserted.
1741 absolute Absolute path, if possible with ~ for home directory.
1742 noabbrev Absolute path, no abbreviation of home directory.
1743 adaptive Use relative path for files in the current directory and sub-
1744 directories of it. For other files, use an absolute path."
1752 (defvaralias 'org-activate-links
'org-highlight-links
)
1753 (defcustom org-highlight-links
'(bracket angle plain radio tag date footnote
)
1754 "Types of links that should be highlighted in Org-mode files.
1756 This is a list of symbols, each one of them leading to the
1757 highlighting of a certain link type.
1759 You can still open links that are not highlighted.
1761 In principle, it does not hurt to turn on highlighting for all
1762 link types. There may be a small gain when turning off unused
1763 link types. The types are:
1765 bracket The recommended [[link][description]] or [[link]] links with hiding.
1766 angle Links in angular brackets that may contain whitespace like
1767 <bbdb:Carsten Dominik>.
1768 plain Plain links in normal text, no whitespace, like http://google.com.
1769 radio Text that is matched by a radio target, see manual for details.
1770 tag Tag settings in a headline (link to tag search).
1771 date Time stamps (link to calendar).
1772 footnote Footnote labels.
1774 If you set this variable during an Emacs session, use `org-mode-restart'
1775 in the Org buffer so that the change takes effect."
1777 :group
'org-appearance
1778 :type
'(set :greedy t
1779 (const :tag
"Double bracket links" bracket
)
1780 (const :tag
"Angular bracket links" angle
)
1781 (const :tag
"Plain text links" plain
)
1782 (const :tag
"Radio target matches" radio
)
1783 (const :tag
"Tags" tag
)
1784 (const :tag
"Timestamps" date
)
1785 (const :tag
"Footnotes" footnote
)))
1787 (defcustom org-make-link-description-function nil
1788 "Function to use for generating link descriptions from links.
1789 When nil, the link location will be used. This function must take
1790 two parameters: the first one is the link, the second one is the
1791 description generated by `org-insert-link'. The function should
1792 return the description to use."
1794 :type
'(choice (const nil
) (function)))
1796 (defgroup org-link-store nil
1797 "Options concerning storing links in Org-mode."
1798 :tag
"Org Store Link"
1801 (defcustom org-url-hexify-p t
1802 "When non-nil, hexify URL when creating a link."
1805 :group
'org-link-store
)
1807 (defcustom org-email-link-description-format
"Email %c: %.30s"
1808 "Format of the description part of a link to an email or usenet message.
1809 The following %-escapes will be replaced by corresponding information:
1811 %F full \"From\" field
1812 %f name, taken from \"From\" field, address if no name
1813 %T full \"To\" field
1814 %t first name in \"To\" field, address if no name
1815 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1816 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1821 You may use normal field width specification between the % and the letter.
1822 This is for example useful to limit the length of the subject.
1824 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1825 :group
'org-link-store
1828 (defcustom org-from-is-user-regexp
1830 (when (and user-mail-address
(not (string= user-mail-address
"")))
1831 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1832 (when (and user-full-name
(not (string= user-full-name
"")))
1833 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1834 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1835 "Regexp matched against the \"From:\" header of an email or usenet message.
1836 It should match if the message is from the user him/herself."
1837 :group
'org-link-store
1840 (defcustom org-context-in-file-links t
1841 "Non-nil means file links from `org-store-link' contain context.
1842 A search string will be added to the file name with :: as separator and
1843 used to find the context when the link is activated by the command
1844 `org-open-at-point'. When this option is t, the entire active region
1845 will be placed in the search string of the file link. If set to a
1846 positive integer, only the first n lines of context will be stored.
1848 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1849 negates this setting for the duration of the command."
1850 :group
'org-link-store
1851 :type
'(choice boolean integer
))
1853 (defcustom org-keep-stored-link-after-insertion nil
1854 "Non-nil means keep link in list for entire session.
1856 The command `org-store-link' adds a link pointing to the current
1857 location to an internal list. These links accumulate during a session.
1858 The command `org-insert-link' can be used to insert links into any
1859 Org-mode file (offering completion for all stored links). When this
1860 option is nil, every link which has been inserted once using \\[org-insert-link]
1861 will be removed from the list, to make completing the unused links
1863 :group
'org-link-store
1866 (defgroup org-link-follow nil
1867 "Options concerning following links in Org-mode."
1868 :tag
"Org Follow Link"
1871 (defcustom org-link-translation-function nil
1872 "Function to translate links with different syntax to Org syntax.
1873 This can be used to translate links created for example by the Planner
1874 or emacs-wiki packages to Org syntax.
1875 The function must accept two parameters, a TYPE containing the link
1876 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1877 which is everything after the link protocol. It should return a cons
1878 with possibly modified values of type and path.
1879 Org contains a function for this, so if you set this variable to
1880 `org-translate-link-from-planner', you should be able follow many
1881 links created by planner."
1882 :group
'org-link-follow
1883 :type
'(choice (const nil
) (function)))
1885 (defcustom org-follow-link-hook nil
1886 "Hook that is run after a link has been followed."
1887 :group
'org-link-follow
1890 (defcustom org-tab-follows-link nil
1891 "Non-nil means on links TAB will follow the link.
1892 Needs to be set before org.el is loaded.
1893 This really should not be used, it does not make sense, and the
1894 implementation is bad."
1895 :group
'org-link-follow
1898 (defcustom org-return-follows-link nil
1899 "Non-nil means on links RET will follow the link.
1900 In tables, the special behavior of RET has precedence."
1901 :group
'org-link-follow
1904 (defcustom org-mouse-1-follows-link
1905 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1906 "Non-nil means mouse-1 on a link will follow the link.
1907 A longer mouse click will still set point. Does not work on XEmacs.
1908 Needs to be set before org.el is loaded."
1909 :group
'org-link-follow
1911 :package-version
'(Org .
"8.3")
1913 (const :tag
"A double click follows the link" double
)
1914 (const :tag
"Unconditionally follow the link with mouse-1" t
)
1915 (integer :tag
"mouse-1 click does not follow the link if longer than N ms" 450)))
1917 (defcustom org-mark-ring-length
4
1918 "Number of different positions to be recorded in the ring.
1919 Changing this requires a restart of Emacs to work correctly."
1920 :group
'org-link-follow
1923 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1924 "Non-nil means internal links in Org files must exactly match a headline.
1925 When nil, the link search tries to match a phrase with all words
1926 in the search text."
1927 :group
'org-link-follow
1930 (const :tag
"Use fuzzy text search" nil
)
1931 (const :tag
"Match only exact headline" t
)
1932 (const :tag
"Match exact headline or query to create it"
1935 (defcustom org-link-frame-setup
1936 '((vm . vm-visit-folder-other-frame
)
1937 (vm-imap . vm-visit-imap-folder-other-frame
)
1938 (gnus . org-gnus-no-new-news
)
1939 (file . find-file-other-window
)
1940 (wl . wl-other-frame
))
1941 "Setup the frame configuration for following links.
1942 When following a link with Emacs, it may often be useful to display
1943 this link in another window or frame. This variable can be used to
1944 set this up for the different types of links.
1947 `vm-visit-folder-other-window'
1948 `vm-visit-folder-other-frame'
1949 For Gnus, use any of
1952 `org-gnus-no-new-news'
1953 For FILE, use any of
1955 `find-file-other-window'
1956 `find-file-other-frame'
1957 For Wanderlust use any of
1960 For the calendar, use the variable `calendar-setup'.
1961 For BBDB, it is currently only possible to display the matches in
1963 :group
'org-link-follow
1967 (const vm-visit-folder
)
1968 (const vm-visit-folder-other-window
)
1969 (const vm-visit-folder-other-frame
)))
1970 (cons (const vm-imap
)
1972 (const vm-visit-imap-folder
)
1973 (const vm-visit-imap-folder-other-window
)
1974 (const vm-visit-imap-folder-other-frame
)))
1978 (const gnus-other-frame
)
1979 (const org-gnus-no-new-news
)))
1983 (const find-file-other-window
)
1984 (const find-file-other-frame
)))
1988 (const wl-other-frame
)))))
1990 (defcustom org-display-internal-link-with-indirect-buffer nil
1991 "Non-nil means use indirect buffer to display infile links.
1992 Activating internal links (from one location in a file to another location
1993 in the same file) normally just jumps to the location. When the link is
1994 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1996 another window. When this option is set, the other window actually displays
1997 an indirect buffer clone of the current buffer, to avoid any visibility
1998 changes to the current buffer."
1999 :group
'org-link-follow
2002 (defcustom org-open-non-existing-files nil
2003 "Non-nil means `org-open-file' will open non-existing files.
2004 When nil, an error will be generated.
2005 This variable applies only to external applications because they
2006 might choke on non-existing files. If the link is to a file that
2007 will be opened in Emacs, the variable is ignored."
2008 :group
'org-link-follow
2011 (defcustom org-open-directory-means-index-dot-org nil
2012 "Non-nil means a link to a directory really means to index.org.
2013 When nil, following a directory link will run dired or open a finder/explorer
2014 window on that directory."
2015 :group
'org-link-follow
2018 (defcustom org-confirm-shell-link-function
'yes-or-no-p
2019 "Non-nil means ask for confirmation before executing shell links.
2020 Shell links can be dangerous: just think about a link
2022 [[shell:rm -rf ~/*][Google Search]]
2024 This link would show up in your Org-mode document as \"Google Search\",
2025 but really it would remove your entire home directory.
2026 Therefore we advise against setting this variable to nil.
2027 Just change it to `y-or-n-p' if you want to confirm with a
2028 single keystroke rather than having to type \"yes\"."
2029 :group
'org-link-follow
2031 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
2032 (const :tag
"with y-or-n (faster)" y-or-n-p
)
2033 (const :tag
"no confirmation (dangerous)" nil
)))
2034 (put 'org-confirm-shell-link-function
2035 'safe-local-variable
2036 (lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
2038 (defcustom org-confirm-shell-link-not-regexp
""
2039 "A regexp to skip confirmation for shell links."
2040 :group
'org-link-follow
2044 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
2045 "Non-nil means ask for confirmation before executing Emacs Lisp links.
2046 Elisp links can be dangerous: just think about a link
2048 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
2050 This link would show up in your Org-mode document as \"Google Search\",
2051 but really it would remove your entire home directory.
2052 Therefore we advise against setting this variable to nil.
2053 Just change it to `y-or-n-p' if you want to confirm with a
2054 single keystroke rather than having to type \"yes\"."
2055 :group
'org-link-follow
2057 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
2058 (const :tag
"with y-or-n (faster)" y-or-n-p
)
2059 (const :tag
"no confirmation (dangerous)" nil
)))
2060 (put 'org-confirm-shell-link-function
2061 'safe-local-variable
2062 (lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
2064 (defcustom org-confirm-elisp-link-not-regexp
""
2065 "A regexp to skip confirmation for Elisp links."
2066 :group
'org-link-follow
2070 (defconst org-file-apps-defaults-gnu
2074 "Default file applications on a UNIX or GNU/Linux system.
2075 See `org-file-apps'.")
2077 (defconst org-file-apps-defaults-macosx
2080 (system .
"open %s")
2082 ("eps.gz" .
"gv %s")
2084 ("fig" .
"xfig %s"))
2085 "Default file applications on a MacOS X system.
2086 The system \"open\" is known as a default, but we use X11 applications
2087 for some files for which the OS does not have a good default.
2088 See `org-file-apps'.")
2090 (defconst org-file-apps-defaults-windowsnt
2094 (list (if (featurep 'xemacs
)
2095 'mswindows-shell-execute
2099 (list (if (featurep 'xemacs
)
2100 'mswindows-shell-execute
2103 "Default file applications on a Windows NT system.
2104 The system \"open\" is used for most files.
2105 See `org-file-apps'.")
2107 (defcustom org-file-apps
2108 '((auto-mode . emacs
)
2109 ("\\.mm\\'" . default
)
2110 ("\\.x?html?\\'" . default
)
2111 ("\\.pdf\\'" . default
))
2112 "External applications for opening `file:path' items in a document.
2113 Org-mode uses system defaults for different file types, but
2114 you can use this variable to set the application for a given file
2115 extension. The entries in this list are cons cells where the car identifies
2116 files and the cdr the corresponding command. Possible values for the
2118 \"string\" A string as a file identifier can be interpreted in different
2119 ways, depending on its contents:
2121 - Alphanumeric characters only:
2122 Match links with this file extension.
2123 Example: (\"pdf\" . \"evince %s\")
2124 to open PDFs with evince.
2126 - Regular expression: Match links where the
2127 filename matches the regexp. If you want to
2128 use groups here, use shy groups.
2130 Example: (\"\\.x?html\\'\" . \"firefox %s\")
2131 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
2132 to open *.html and *.xhtml with firefox.
2134 - Regular expression which contains (non-shy) groups:
2135 Match links where the whole link, including \"::\", and
2136 anything after that, matches the regexp.
2137 In a custom command string, %1, %2, etc. are replaced with
2138 the parts of the link that were matched by the groups.
2139 For backwards compatibility, if a command string is given
2140 that does not use any of the group matches, this case is
2141 handled identically to the second one (i.e. match against
2143 In a custom lisp form, you can access the group matches with
2144 (match-string n link).
2146 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
2147 to open [[file:document.pdf::5]] with evince at page 5.
2149 `directory' Matches a directory
2150 `remote' Matches a remote file, accessible through tramp or efs.
2151 Remote files most likely should be visited through Emacs
2152 because external applications cannot handle such paths.
2153 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
2154 so all files Emacs knows how to handle. Using this with
2155 command `emacs' will open most files in Emacs. Beware that this
2156 will also open html files inside Emacs, unless you add
2157 (\"html\" . default) to the list as well.
2158 t Default for files not matched by any of the other options.
2159 `system' The system command to open files, like `open' on Windows
2160 and Mac OS X, and mailcap under GNU/Linux. This is the command
2161 that will be selected if you call `C-c C-o' with a double
2162 \\[universal-argument] \\[universal-argument] prefix.
2164 Possible values for the command are:
2165 `emacs' The file will be visited by the current Emacs process.
2166 `default' Use the default application for this file type, which is the
2167 association for t in the list, most likely in the system-specific
2169 This can be used to overrule an unwanted setting in the
2170 system-specific variable.
2171 `system' Use the system command for opening files, like \"open\".
2172 This command is specified by the entry whose car is `system'.
2173 Most likely, the system-specific version of this variable
2174 does define this command, but you can overrule/replace it
2176 string A command to be executed by a shell; %s will be replaced
2177 by the path to the file.
2178 sexp A Lisp form which will be evaluated. The file path will
2179 be available in the Lisp variable `file'.
2180 For more examples, see the system specific constants
2181 `org-file-apps-defaults-macosx'
2182 `org-file-apps-defaults-windowsnt'
2183 `org-file-apps-defaults-gnu'."
2184 :group
'org-link-follow
2186 (cons (choice :value
""
2187 (string :tag
"Extension")
2188 (const :tag
"System command to open files" system
)
2189 (const :tag
"Default for unrecognized files" t
)
2190 (const :tag
"Remote file" remote
)
2191 (const :tag
"Links to a directory" directory
)
2192 (const :tag
"Any files that have Emacs modes"
2195 (const :tag
"Visit with Emacs" emacs
)
2196 (const :tag
"Use default" default
)
2197 (const :tag
"Use the system command" system
)
2198 (string :tag
"Command")
2199 (sexp :tag
"Lisp form")))))
2201 (defcustom org-doi-server-url
"http://dx.doi.org/"
2202 "The URL of the DOI server."
2205 :group
'org-link-follow
)
2207 (defgroup org-refile nil
2208 "Options concerning refiling entries in Org-mode."
2212 (defcustom org-directory
"~/org"
2213 "Directory with org files.
2214 This is just a default location to look for Org files. There is no need
2215 at all to put your files into this directory. It is only used in the
2216 following situations:
2218 1. When a capture template specifies a target file that is not an
2219 absolute path. The path will then be interpreted relative to
2221 2. When a capture note is filed away in an interactive way (when exiting the
2222 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
2223 with `org-directory' as the default path."
2228 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
2229 "Default target for storing notes.
2230 Used as a fall back file for org-capture.el, for templates that
2231 do not specify a target file."
2235 (const :tag
"Default from remember-data-file" nil
)
2238 (defcustom org-goto-interface
'outline
2239 "The default interface to be used for `org-goto'.
2241 outline The interface shows an outline of the relevant file
2242 and the correct heading is found by moving through
2243 the outline or by searching with incremental search.
2244 outline-path-completion Headlines in the current buffer are offered via
2245 completion. This is the interface also used by
2246 the refile command."
2249 (const :tag
"Outline" outline
)
2250 (const :tag
"Outline-path-completion" outline-path-completion
)))
2252 (defcustom org-goto-max-level
5
2253 "Maximum target level when running `org-goto' with refile interface."
2257 (defcustom org-reverse-note-order nil
2258 "Non-nil means store new notes at the beginning of a file or entry.
2259 When nil, new notes will be filed to the end of a file or entry.
2260 This can also be a list with cons cells of regular expressions that
2261 are matched against file names, and values."
2265 (const :tag
"Reverse always" t
)
2266 (const :tag
"Reverse never" nil
)
2267 (repeat :tag
"By file name regexp"
2268 (cons regexp boolean
))))
2270 (defcustom org-log-refile nil
2271 "Information to record when a task is refiled.
2273 Possible values are:
2275 nil Don't add anything
2276 time Add a time stamp to the task
2277 note Prompt for a note and add it with template `org-log-note-headings'
2279 This option can also be set with on a per-file-basis with
2281 #+STARTUP: nologrefile
2282 #+STARTUP: logrefile
2283 #+STARTUP: lognoterefile
2285 You can have local logging settings for a subtree by setting the LOGGING
2286 property to one or more of these keywords.
2288 When bulk-refiling from the agenda, the value `note' is forbidden and
2289 will temporarily be changed to `time'."
2291 :group
'org-progress
2294 (const :tag
"No logging" nil
)
2295 (const :tag
"Record timestamp" time
)
2296 (const :tag
"Record timestamp with note." note
)))
2298 (defcustom org-refile-targets nil
2299 "Targets for refiling entries with \\[org-refile].
2300 This is a list of cons cells. Each cell contains:
2301 - a specification of the files to be considered, either a list of files,
2302 or a symbol whose function or variable value will be used to retrieve
2303 a file name or a list of file names. If you use `org-agenda-files' for
2304 that, all agenda files will be scanned for targets. Nil means consider
2305 headings in the current buffer.
2306 - A specification of how to find candidate refile targets. This may be
2308 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
2309 This tag has to be present in all target headlines, inheritance will
2311 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
2313 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
2314 headlines that are refiling targets.
2315 - a cons cell (:level . N). Any headline of level N is considered a target.
2316 Note that, when `org-odd-levels-only' is set, level corresponds to
2317 order in hierarchy, not to the number of stars.
2318 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
2319 Note that, when `org-odd-levels-only' is set, level corresponds to
2320 order in hierarchy, not to the number of stars.
2322 Each element of this list generates a set of possible targets.
2323 The union of these sets is presented (with completion) to
2324 the user by `org-refile'.
2326 You can set the variable `org-refile-target-verify-function' to a function
2327 to verify each headline found by the simple criteria above.
2329 When this variable is nil, all top-level headlines in the current buffer
2330 are used, equivalent to the value `((nil . (:level . 1))'."
2334 (choice :value org-agenda-files
2335 (const :tag
"All agenda files" org-agenda-files
)
2336 (const :tag
"Current buffer" nil
)
2337 (function) (variable) (file))
2338 (choice :tag
"Identify target headline by"
2339 (cons :tag
"Specific tag" (const :value
:tag
) (string))
2340 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
2341 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
2342 (cons :tag
"Level number" (const :value
:level
) (integer))
2343 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
2345 (defcustom org-refile-target-verify-function nil
2346 "Function to verify if the headline at point should be a refile target.
2347 The function will be called without arguments, with point at the
2348 beginning of the headline. It should return t and leave point
2349 where it is if the headline is a valid target for refiling.
2351 If the target should not be selected, the function must return nil.
2352 In addition to this, it may move point to a place from where the search
2353 should be continued. For example, the function may decide that the entire
2354 subtree of the current entry should be excluded and move point to the end
2361 (defcustom org-refile-use-cache nil
2362 "Non-nil means cache refile targets to speed up the process.
2363 The cache for a particular file will be updated automatically when
2364 the buffer has been killed, or when any of the marker used for flagging
2365 refile targets no longer points at a live buffer.
2366 If you have added new entries to a buffer that might themselves be targets,
2367 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2368 find that easier, `C-u C-u C-u C-c C-w'."
2373 (defcustom org-refile-use-outline-path nil
2374 "Non-nil means provide refile targets as paths.
2375 So a level 3 headline will be available as level1/level2/level3.
2377 When the value is `file', also include the file name (without directory)
2378 into the path. In this case, you can also stop the completion after
2379 the file name, to get entries inserted as top level in the file.
2381 When `full-file-path', include the full file path."
2384 (const :tag
"Not" nil
)
2385 (const :tag
"Yes" t
)
2386 (const :tag
"Start with file name" file
)
2387 (const :tag
"Start with full file path" full-file-path
)))
2389 (defcustom org-outline-path-complete-in-steps t
2390 "Non-nil means complete the outline path in hierarchical steps.
2391 When Org-mode uses the refile interface to select an outline path
2392 \(see variable `org-refile-use-outline-path'), the completion of
2393 the path can be done is a single go, or if can be done in steps down
2394 the headline hierarchy. Going in steps is probably the best if you
2395 do not use a special completion package like `ido' or `icicles'.
2396 However, when using these packages, going in one step can be very
2397 fast, while still showing the whole path to the entry."
2401 (defcustom org-refile-allow-creating-parent-nodes nil
2402 "Non-nil means allow to create new nodes as refile targets.
2403 New nodes are then created by adding \"/new node name\" to the completion
2404 of an existing node. When the value of this variable is `confirm',
2405 new node creation must be confirmed by the user (recommended).
2406 When nil, the completion must match an existing entry.
2408 Note that, if the new heading is not seen by the criteria
2409 listed in `org-refile-targets', multiple instances of the same
2410 heading would be created by trying again to file under the new
2414 (const :tag
"Never" nil
)
2415 (const :tag
"Always" t
)
2416 (const :tag
"Prompt for confirmation" confirm
)))
2418 (defcustom org-refile-active-region-within-subtree nil
2419 "Non-nil means also refile active region within a subtree.
2421 By default `org-refile' doesn't allow refiling regions if they
2422 don't contain a set of subtrees, but it might be convenient to
2423 do so sometimes: in that case, the first line of the region is
2424 converted to a headline before refiling."
2429 (defgroup org-todo nil
2430 "Options concerning TODO items in Org-mode."
2434 (defgroup org-progress nil
2435 "Options concerning Progress logging in Org-mode."
2439 (defvar org-todo-interpretation-widgets
2440 '((:tag
"Sequence (cycling hits every state)" sequence
)
2441 (:tag
"Type (cycling directly to DONE)" type
))
2442 "The available interpretation symbols for customizing `org-todo-keywords'.
2443 Interested libraries should add to this list.")
2445 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2446 "List of TODO entry keyword sequences and their interpretation.
2447 \\<org-mode-map>This is a list of sequences.
2449 Each sequence starts with a symbol, either `sequence' or `type',
2450 indicating if the keywords should be interpreted as a sequence of
2451 action steps, or as different types of TODO items. The first
2452 keywords are states requiring action - these states will select a headline
2453 for inclusion into the global TODO list Org-mode produces. If one of
2454 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2455 signify that no further action is necessary. If \"|\" is not found,
2456 the last keyword is treated as the only DONE state of the sequence.
2458 The command \\[org-todo] cycles an entry through these states, and one
2459 additional state where no keyword is present. For details about this
2460 cycling, see the manual.
2462 TODO keywords and interpretation can also be set on a per-file basis with
2463 the special #+SEQ_TODO and #+TYP_TODO lines.
2465 Each keyword can optionally specify a character for fast state selection
2466 \(in combination with the variable `org-use-fast-todo-selection')
2467 and specifiers for state change logging, using the same syntax that
2468 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2469 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2470 indicates to record a time stamp each time this state is selected.
2472 Each keyword may also specify if a timestamp or a note should be
2473 recorded when entering or leaving the state, by adding additional
2474 characters in the parenthesis after the keyword. This looks like this:
2475 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2476 record only the time of the state change. With X and Y being either
2477 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2478 Y when leaving the state if and only if the *target* state does not
2479 define X. You may omit any of the fast-selection key or X or /Y,
2480 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2482 For backward compatibility, this variable may also be just a list
2483 of keywords. In this case the interpretation (sequence or type) will be
2484 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2486 :group
'org-keywords
2488 (repeat :tag
"Old syntax, just keywords"
2489 (string :tag
"Keyword"))
2490 (repeat :tag
"New syntax"
2493 :tag
"Interpretation"
2494 ;;Quick and dirty way to see
2495 ;;`org-todo-interpretations'. This takes the
2496 ;;place of item arguments
2504 org-todo-interpretation-widgets
))
2507 (string :tag
"Keyword"))))))
2509 (defvar org-todo-keywords-1 nil
2510 "All TODO and DONE keywords active in a buffer.")
2511 (make-variable-buffer-local 'org-todo-keywords-1
)
2512 (defvar org-todo-keywords-for-agenda nil
)
2513 (defvar org-done-keywords-for-agenda nil
)
2514 (defvar org-todo-keyword-alist-for-agenda nil
)
2515 (defvar org-tag-alist-for-agenda nil
2516 "Alist of all tags from all agenda files.")
2517 (defvar org-tag-groups-alist-for-agenda nil
2518 "Alist of all groups tags from all current agenda files.")
2519 (defvar org-tag-groups-alist nil
)
2520 (make-variable-buffer-local 'org-tag-groups-alist
)
2521 (defvar org-agenda-contributing-files nil
)
2522 (defvar org-not-done-keywords nil
)
2523 (make-variable-buffer-local 'org-not-done-keywords
)
2524 (defvar org-done-keywords nil
)
2525 (make-variable-buffer-local 'org-done-keywords
)
2526 (defvar org-todo-heads nil
)
2527 (make-variable-buffer-local 'org-todo-heads
)
2528 (defvar org-todo-sets nil
)
2529 (make-variable-buffer-local 'org-todo-sets
)
2530 (defvar org-todo-log-states nil
)
2531 (make-variable-buffer-local 'org-todo-log-states
)
2532 (defvar org-todo-kwd-alist nil
)
2533 (make-variable-buffer-local 'org-todo-kwd-alist
)
2534 (defvar org-todo-key-alist nil
)
2535 (make-variable-buffer-local 'org-todo-key-alist
)
2536 (defvar org-todo-key-trigger nil
)
2537 (make-variable-buffer-local 'org-todo-key-trigger
)
2539 (defcustom org-todo-interpretation
'sequence
2540 "Controls how TODO keywords are interpreted.
2541 This variable is in principle obsolete and is only used for
2542 backward compatibility, if the interpretation of todo keywords is
2543 not given already in `org-todo-keywords'. See that variable for
2546 :group
'org-keywords
2547 :type
'(choice (const sequence
)
2550 (defcustom org-use-fast-todo-selection t
2551 "Non-nil means use the fast todo selection scheme with C-c C-t.
2552 This variable describes if and under what circumstances the cycling
2553 mechanism for TODO keywords will be replaced by a single-key, direct
2556 When nil, fast selection is never used.
2558 When the symbol `prefix', it will be used when `org-todo' is called
2559 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2560 `C-u t' in an agenda buffer.
2562 When t, fast selection is used by default. In this case, the prefix
2563 argument forces cycling instead.
2565 In all cases, the special interface is only used if access keys have
2566 actually been assigned by the user, i.e. if keywords in the configuration
2567 are followed by a letter in parenthesis, like TODO(t)."
2570 (const :tag
"Never" nil
)
2571 (const :tag
"By default" t
)
2572 (const :tag
"Only with C-u C-c C-t" prefix
)))
2574 (defcustom org-provide-todo-statistics t
2575 "Non-nil means update todo statistics after insert and toggle.
2576 ALL-HEADLINES means update todo statistics by including headlines
2577 with no TODO keyword as well, counting them as not done.
2578 A list of TODO keywords means the same, but skip keywords that are
2580 When set to a list of two lists, the first list contains keywords
2581 to consider as TODO keywords, the second list contains keywords
2582 to consider as DONE keywords.
2584 When this is set, todo statistics is updated in the parent of the
2585 current entry each time a todo state is changed."
2588 (const :tag
"Yes, only for TODO entries" t
)
2589 (const :tag
"Yes, including all entries" all-headlines
)
2590 (repeat :tag
"Yes, for TODOs in this list"
2591 (string :tag
"TODO keyword"))
2592 (list :tag
"Yes, for TODOs and DONEs in these lists"
2593 (repeat (string :tag
"TODO keyword"))
2594 (repeat (string :tag
"DONE keyword")))
2595 (other :tag
"No TODO statistics" nil
)))
2597 (defcustom org-hierarchical-todo-statistics t
2598 "Non-nil means TODO statistics covers just direct children.
2599 When nil, all entries in the subtree are considered.
2600 This has only an effect if `org-provide-todo-statistics' is set.
2601 To set this to nil for only a single subtree, use a COOKIE_DATA
2602 property and include the word \"recursive\" into the value."
2606 (defcustom org-after-todo-state-change-hook nil
2607 "Hook which is run after the state of a TODO item was changed.
2608 The new state (a string with a TODO keyword, or nil) is available in the
2609 Lisp variable `org-state'."
2613 (defvar org-blocker-hook nil
2614 "Hook for functions that are allowed to block a state change.
2616 Functions in this hook should not modify the buffer.
2617 Each function gets as its single argument a property list,
2618 see `org-trigger-hook' for more information about this list.
2620 If any of the functions in this hook returns nil, the state change
2623 (defvar org-trigger-hook nil
2624 "Hook for functions that are triggered by a state change.
2626 Each function gets as its single argument a property list with at
2627 least the following elements:
2629 (:type type-of-change :position pos-at-entry-start
2630 :from old-state :to new-state)
2632 Depending on the type, more properties may be present.
2634 This mechanism is currently implemented for:
2638 :type todo-state-change
2639 :from previous state (keyword as a string), or nil, or a symbol
2640 'todo' or 'done', to indicate the general type of state.
2641 :to new state, like in :from")
2643 (defcustom org-enforce-todo-dependencies nil
2644 "Non-nil means undone TODO entries will block switching the parent to DONE.
2645 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2646 be blocked if any prior sibling is not yet done.
2647 Finally, if the parent is blocked because of ordered siblings of its own,
2648 the child will also be blocked."
2649 :set
(lambda (var val
)
2652 (add-hook 'org-blocker-hook
2653 'org-block-todo-from-children-or-siblings-or-parent
)
2654 (remove-hook 'org-blocker-hook
2655 'org-block-todo-from-children-or-siblings-or-parent
)))
2659 (defcustom org-enforce-todo-checkbox-dependencies nil
2660 "Non-nil means unchecked boxes will block switching the parent to DONE.
2661 When this is nil, checkboxes have no influence on switching TODO states.
2662 When non-nil, you first need to check off all check boxes before the TODO
2663 entry can be switched to DONE.
2664 This variable needs to be set before org.el is loaded, and you need to
2665 restart Emacs after a change to make the change effective. The only way
2666 to change is while Emacs is running is through the customize interface."
2667 :set
(lambda (var val
)
2670 (add-hook 'org-blocker-hook
2671 'org-block-todo-from-checkboxes
)
2672 (remove-hook 'org-blocker-hook
2673 'org-block-todo-from-checkboxes
)))
2677 (defcustom org-treat-insert-todo-heading-as-state-change nil
2678 "Non-nil means inserting a TODO heading is treated as state change.
2679 So when the command \\[org-insert-todo-heading] is used, state change
2680 logging will apply if appropriate. When nil, the new TODO item will
2681 be inserted directly, and no logging will take place."
2685 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2686 "Non-nil means switching TODO states with S-cursor counts as state change.
2687 This is the default behavior. However, setting this to nil allows a
2688 convenient way to select a TODO state and bypass any logging associated
2693 (defcustom org-todo-state-tags-triggers nil
2694 "Tag changes that should be triggered by TODO state changes.
2695 This is a list. Each entry is
2697 (state-change (tag . flag) .......)
2699 State-change can be a string with a state, and empty string to indicate the
2700 state that has no TODO keyword, or it can be one of the symbols `todo'
2701 or `done', meaning any not-done or done state, respectively."
2705 (cons (choice :tag
"When changing to"
2706 (const :tag
"Not-done state" todo
)
2707 (const :tag
"Done state" done
)
2708 (string :tag
"State"))
2710 (cons :tag
"Tag action"
2712 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2714 (defcustom org-log-done nil
2715 "Information to record when a task moves to the DONE state.
2717 Possible values are:
2719 nil Don't add anything, just change the keyword
2720 time Add a time stamp to the task
2721 note Prompt for a note and add it with template `org-log-note-headings'
2723 This option can also be set with on a per-file-basis with
2725 #+STARTUP: nologdone
2727 #+STARTUP: lognotedone
2729 You can have local logging settings for a subtree by setting the LOGGING
2730 property to one or more of these keywords."
2732 :group
'org-progress
2734 (const :tag
"No logging" nil
)
2735 (const :tag
"Record CLOSED timestamp" time
)
2736 (const :tag
"Record CLOSED timestamp with note." note
)))
2738 ;; Normalize old uses of org-log-done.
2740 ((eq org-log-done t
) (setq org-log-done
'time
))
2741 ((and (listp org-log-done
) (memq 'done org-log-done
))
2742 (setq org-log-done
'note
)))
2744 (defcustom org-log-reschedule nil
2745 "Information to record when the scheduling date of a tasks is modified.
2747 Possible values are:
2749 nil Don't add anything, just change the date
2750 time Add a time stamp to the task
2751 note Prompt for a note and add it with template `org-log-note-headings'
2753 This option can also be set with on a per-file-basis with
2755 #+STARTUP: nologreschedule
2756 #+STARTUP: logreschedule
2757 #+STARTUP: lognotereschedule"
2759 :group
'org-progress
2761 (const :tag
"No logging" nil
)
2762 (const :tag
"Record timestamp" time
)
2763 (const :tag
"Record timestamp with note." note
)))
2765 (defcustom org-log-redeadline nil
2766 "Information to record when the deadline date of a tasks is modified.
2768 Possible values are:
2770 nil Don't add anything, just change the date
2771 time Add a time stamp to the task
2772 note Prompt for a note and add it with template `org-log-note-headings'
2774 This option can also be set with on a per-file-basis with
2776 #+STARTUP: nologredeadline
2777 #+STARTUP: logredeadline
2778 #+STARTUP: lognoteredeadline
2780 You can have local logging settings for a subtree by setting the LOGGING
2781 property to one or more of these keywords."
2783 :group
'org-progress
2785 (const :tag
"No logging" nil
)
2786 (const :tag
"Record timestamp" time
)
2787 (const :tag
"Record timestamp with note." note
)))
2789 (defcustom org-log-note-clock-out nil
2790 "Non-nil means record a note when clocking out of an item.
2791 This can also be configured on a per-file basis by adding one of
2792 the following lines anywhere in the buffer:
2794 #+STARTUP: lognoteclock-out
2795 #+STARTUP: nolognoteclock-out"
2797 :group
'org-progress
2800 (defcustom org-log-done-with-time t
2801 "Non-nil means the CLOSED time stamp will contain date and time.
2802 When nil, only the date will be recorded."
2803 :group
'org-progress
2806 (defcustom org-log-note-headings
2807 '((done .
"CLOSING NOTE %t")
2808 (state .
"State %-12s from %-12S %t")
2809 (note .
"Note taken on %t")
2810 (reschedule .
"Rescheduled from %S on %t")
2811 (delschedule .
"Not scheduled, was %S on %t")
2812 (redeadline .
"New deadline from %S on %t")
2813 (deldeadline .
"Removed deadline, was %S on %t")
2814 (refile .
"Refiled on %t")
2816 "Headings for notes added to entries.
2817 The value is an alist, with the car being a symbol indicating the note
2818 context, and the cdr is the heading to be used. The heading may also be the
2820 %t in the heading will be replaced by a time stamp.
2821 %T will be an active time stamp instead the default inactive one
2822 %d will be replaced by a short-format time stamp.
2823 %D will be replaced by an active short-format time stamp.
2824 %s will be replaced by the new TODO state, in double quotes.
2825 %S will be replaced by the old TODO state, in double quotes.
2826 %u will be replaced by the user name.
2827 %U will be replaced by the full user name.
2829 In fact, it is not a good idea to change the `state' entry, because
2830 agenda log mode depends on the format of these entries."
2832 :group
'org-progress
2833 :type
'(list :greedy t
2834 (cons (const :tag
"Heading when closing an item" done
) string
)
2836 "Heading when changing todo state (todo sequence only)"
2838 (cons (const :tag
"Heading when just taking a note" note
) string
)
2839 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2840 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2841 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2842 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2843 (cons (const :tag
"Heading when refiling" refile
) string
)
2844 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
2846 (unless (assq 'note org-log-note-headings
)
2847 (push '(note .
"%t") org-log-note-headings
))
2849 (defcustom org-log-into-drawer nil
2850 "Non-nil means insert state change notes and time stamps into a drawer.
2851 When nil, state changes notes will be inserted after the headline and
2852 any scheduling and clock lines, but not inside a drawer.
2854 The value of this variable should be the name of the drawer to use.
2855 LOGBOOK is proposed as the default drawer for this purpose, you can
2856 also set this to a string to define the drawer of your choice.
2858 A value of t is also allowed, representing \"LOGBOOK\".
2860 A value of t or nil can also be set with on a per-file-basis with
2862 #+STARTUP: logdrawer
2863 #+STARTUP: nologdrawer
2865 If this variable is set, `org-log-state-notes-insert-after-drawers'
2868 You can set the property LOG_INTO_DRAWER to overrule this setting for
2871 :group
'org-progress
2873 (const :tag
"Not into a drawer" nil
)
2874 (const :tag
"LOGBOOK" t
)
2875 (string :tag
"Other")))
2877 (org-defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
)
2879 (defun org-log-into-drawer ()
2880 "Return the value of `org-log-into-drawer', but let properties overrule.
2881 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2882 used instead of the default value."
2883 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit t
)))
2885 ((not p
) org-log-into-drawer
)
2886 ((equal p
"nil") nil
)
2887 ((equal p
"t") "LOGBOOK")
2890 (defcustom org-log-state-notes-insert-after-drawers nil
2891 "Non-nil means insert state change notes after any drawers in entry.
2892 Only the drawers that *immediately* follow the headline and the
2893 deadline/scheduled line are skipped.
2894 When nil, insert notes right after the heading and perhaps the line
2895 with deadline/scheduling if present.
2897 This variable will have no effect if `org-log-into-drawer' is
2900 :group
'org-progress
2903 (defcustom org-log-states-order-reversed t
2904 "Non-nil means the latest state note will be directly after heading.
2905 When nil, the state change notes will be ordered according to time.
2907 This option can also be set with on a per-file-basis with
2909 #+STARTUP: logstatesreversed
2910 #+STARTUP: nologstatesreversed"
2912 :group
'org-progress
2915 (defcustom org-todo-repeat-to-state nil
2916 "The TODO state to which a repeater should return the repeating task.
2917 By default this is the first task in a TODO sequence, or the previous state
2918 in a TODO_TYP set. But you can specify another task here.
2919 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2922 :type
'(choice (const :tag
"Head of sequence" nil
)
2923 (string :tag
"Specific state")))
2925 (defcustom org-log-repeat
'time
2926 "Non-nil means record moving through the DONE state when triggering repeat.
2927 An auto-repeating task is immediately switched back to TODO when
2928 marked DONE. If you are not logging state changes (by adding \"@\"
2929 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2930 record a closing note, there will be no record of the task moving
2931 through DONE. This variable forces taking a note anyway.
2933 nil Don't force a record
2934 time Record a time stamp
2935 note Prompt for a note and add it with template `org-log-note-headings'
2937 This option can also be set with on a per-file-basis with
2939 #+STARTUP: nologrepeat
2940 #+STARTUP: logrepeat
2941 #+STARTUP: lognoterepeat
2943 You can have local logging settings for a subtree by setting the LOGGING
2944 property to one or more of these keywords."
2946 :group
'org-progress
2948 (const :tag
"Don't force a record" nil
)
2949 (const :tag
"Force recording the DONE state" time
)
2950 (const :tag
"Force recording a note with the DONE state" note
)))
2953 (defgroup org-priorities nil
2954 "Priorities in Org-mode."
2955 :tag
"Org Priorities"
2958 (defcustom org-enable-priority-commands t
2959 "Non-nil means priority commands are active.
2960 When nil, these commands will be disabled, so that you never accidentally
2962 :group
'org-priorities
2965 (defcustom org-highest-priority ?A
2966 "The highest priority of TODO items. A character like ?A, ?B etc.
2967 Must have a smaller ASCII number than `org-lowest-priority'."
2968 :group
'org-priorities
2971 (defcustom org-lowest-priority ?C
2972 "The lowest priority of TODO items. A character like ?A, ?B etc.
2973 Must have a larger ASCII number than `org-highest-priority'."
2974 :group
'org-priorities
2977 (defcustom org-default-priority ?B
2978 "The default priority of TODO items.
2979 This is the priority an item gets if no explicit priority is given.
2980 When starting to cycle on an empty priority the first step in the cycle
2981 depends on `org-priority-start-cycle-with-default'. The resulting first
2982 step priority must not exceed the range from `org-highest-priority' to
2983 `org-lowest-priority' which means that `org-default-priority' has to be
2984 in this range exclusive or inclusive the range boundaries. Else the
2985 first step refuses to set the default and the second will fall back
2986 to (depending on the command used) the highest or lowest priority."
2987 :group
'org-priorities
2990 (defcustom org-priority-start-cycle-with-default t
2991 "Non-nil means start with default priority when starting to cycle.
2992 When this is nil, the first step in the cycle will be (depending on the
2993 command used) one higher or lower than the default priority.
2994 See also `org-default-priority'."
2995 :group
'org-priorities
2998 (defcustom org-get-priority-function nil
2999 "Function to extract the priority from a string.
3000 The string is normally the headline. If this is nil Org computes the
3001 priority from the priority cookie like [#A] in the headline. It returns
3002 an integer, increasing by 1000 for each priority level.
3003 The user can set a different function here, which should take a string
3004 as an argument and return the numeric priority."
3005 :group
'org-priorities
3011 (defgroup org-time nil
3012 "Options concerning time stamps and deadlines in Org-mode."
3016 (defcustom org-insert-labeled-timestamps-at-point nil
3017 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
3018 When nil, these labeled time stamps are forces into the second line of an
3019 entry, just after the headline. When scheduling from the global TODO list,
3020 the time stamp will always be forced into the second line."
3024 (defcustom org-time-stamp-rounding-minutes
'(0 5)
3025 "Number of minutes to round time stamps to.
3026 These are two values, the first applies when first creating a time stamp.
3027 The second applies when changing it with the commands `S-up' and `S-down'.
3028 When changing the time stamp, this means that it will change in steps
3029 of N minutes, as given by the second value.
3031 When a setting is 0 or 1, insert the time unmodified. Useful rounding
3032 numbers should be factors of 60, so for example 5, 10, 15.
3034 When this is larger than 1, you can still force an exact time stamp by using
3035 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
3036 and by using a prefix arg to `S-up/down' to specify the exact number
3037 of minutes to shift."
3039 :get
(lambda (var) ; Make sure both elements are there
3040 (if (integerp (default-value var
))
3041 (list (default-value var
) 5)
3042 (default-value var
)))
3044 (integer :tag
"when inserting times")
3045 (integer :tag
"when modifying times")))
3047 ;; Normalize old customizations of this variable.
3048 (when (integerp org-time-stamp-rounding-minutes
)
3049 (setq org-time-stamp-rounding-minutes
3050 (list org-time-stamp-rounding-minutes
3051 org-time-stamp-rounding-minutes
)))
3053 (defcustom org-display-custom-times nil
3054 "Non-nil means overlay custom formats over all time stamps.
3055 The formats are defined through the variable `org-time-stamp-custom-formats'.
3056 To turn this on on a per-file basis, insert anywhere in the file:
3057 #+STARTUP: customtime"
3061 (make-variable-buffer-local 'org-display-custom-times
)
3063 (defcustom org-time-stamp-custom-formats
3064 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
3065 "Custom formats for time stamps. See `format-time-string' for the syntax.
3066 These are overlaid over the default ISO format if the variable
3067 `org-display-custom-times' is set. Time like %H:%M should be at the
3068 end of the second format. The custom formats are also honored by export
3069 commands, if custom time display is turned on at the time of export."
3073 (defun org-time-stamp-format (&optional long inactive
)
3074 "Get the right format for a time string."
3075 (let ((f (if long
(cdr org-time-stamp-formats
)
3076 (car org-time-stamp-formats
))))
3078 (concat "[" (substring f
1 -
1) "]")
3081 (defcustom org-time-clocksum-format
3082 '(:days
"%dd " :hours
"%d" :require-hours t
:minutes
":%02d" :require-minutes t
)
3083 "The format string used when creating CLOCKSUM lines.
3084 This is also used when Org mode generates a time duration.
3086 The value can be a single format string containing two
3087 %-sequences, which will be filled with the number of hours and
3088 minutes in that order.
3090 Alternatively, the value can be a plist associating any of the
3091 keys :years, :months, :weeks, :days, :hours or :minutes with
3092 format strings. The time duration is formatted using only the
3093 time components that are needed and concatenating the results.
3094 If a time unit in absent, it falls back to the next smallest
3097 The keys :require-years, :require-months, :require-days,
3098 :require-weeks, :require-hours, :require-minutes are also
3099 meaningful. A non-nil value for these keys indicates that the
3100 corresponding time component should always be included, even if
3106 \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
3109 means durations longer than a day will be expressed in days,
3110 hours and minutes, and durations less than a day will always be
3111 expressed in hours and minutes (even for durations less than an
3116 \(:days \"%dd\" :minutes \"%dm\")
3118 means durations longer than a day will be expressed in days and
3119 minutes, and durations less than a day will be expressed entirely
3120 in minutes (even for durations longer than an hour)."
3124 :package-version
'(Org .
"8.0")
3125 :type
'(choice (string :tag
"Format string")
3127 (group :inline t
(const :tag
"Years" :years
)
3128 (string :tag
"Format string"))
3130 (const :tag
"Always show years" :require-years
)
3132 (group :inline t
(const :tag
"Months" :months
)
3133 (string :tag
"Format string"))
3135 (const :tag
"Always show months" :require-months
)
3137 (group :inline t
(const :tag
"Weeks" :weeks
)
3138 (string :tag
"Format string"))
3140 (const :tag
"Always show weeks" :require-weeks
)
3142 (group :inline t
(const :tag
"Days" :days
)
3143 (string :tag
"Format string"))
3145 (const :tag
"Always show days" :require-days
)
3147 (group :inline t
(const :tag
"Hours" :hours
)
3148 (string :tag
"Format string"))
3150 (const :tag
"Always show hours" :require-hours
)
3152 (group :inline t
(const :tag
"Minutes" :minutes
)
3153 (string :tag
"Format string"))
3155 (const :tag
"Always show minutes" :require-minutes
)
3158 (defcustom org-time-clocksum-use-fractional nil
3159 "When non-nil, \\[org-clock-display] uses fractional times.
3160 See `org-time-clocksum-format' for more on time clock formats."
3166 (defcustom org-time-clocksum-use-effort-durations nil
3167 "When non-nil, \\[org-clock-display] uses effort durations.
3168 E.g. by default, one day is considered to be a 8 hours effort,
3169 so a task that has been clocked for 16 hours will be displayed
3170 as during 2 days in the clock display or in the clocktable.
3172 See `org-effort-durations' on how to set effort durations
3173 and `org-time-clocksum-format' for more on time clock formats."
3177 :package-version
'(Org .
"8.0")
3180 (defcustom org-time-clocksum-fractional-format
"%.2f"
3181 "The format string used when creating CLOCKSUM lines,
3182 or when Org mode generates a time duration, if
3183 `org-time-clocksum-use-fractional' is enabled.
3185 The value can be a single format string containing one
3186 %-sequence, which will be filled with the number of hours as
3189 Alternatively, the value can be a plist associating any of the
3190 keys :years, :months, :weeks, :days, :hours or :minutes with
3191 a format string. The time duration is formatted using the
3192 largest time unit which gives a non-zero integer part. If all
3193 specified formats have zero integer part, the smallest time unit
3196 :type
'(choice (string :tag
"Format string")
3197 (set (group :inline t
(const :tag
"Years" :years
)
3198 (string :tag
"Format string"))
3199 (group :inline t
(const :tag
"Months" :months
)
3200 (string :tag
"Format string"))
3201 (group :inline t
(const :tag
"Weeks" :weeks
)
3202 (string :tag
"Format string"))
3203 (group :inline t
(const :tag
"Days" :days
)
3204 (string :tag
"Format string"))
3205 (group :inline t
(const :tag
"Hours" :hours
)
3206 (string :tag
"Format string"))
3207 (group :inline t
(const :tag
"Minutes" :minutes
)
3208 (string :tag
"Format string")))))
3210 (defcustom org-deadline-warning-days
14
3211 "Number of days before expiration during which a deadline becomes active.
3212 This variable governs the display in sparse trees and in the agenda.
3213 When 0 or negative, it means use this number (the absolute value of it)
3214 even if a deadline has a different individual lead time specified.
3216 Custom commands can set this variable in the options section."
3218 :group
'org-agenda-daily
/weekly
3221 (defcustom org-scheduled-delay-days
0
3222 "Number of days before a scheduled item becomes active.
3223 This variable governs the display in sparse trees and in the agenda.
3224 The default value (i.e. 0) means: don't delay scheduled item.
3225 When negative, it means use this number (the absolute value of it)
3226 even if a scheduled item has a different individual delay time
3229 Custom commands can set this variable in the options section."
3231 :group
'org-agenda-daily
/weekly
3233 :package-version
'(Org .
"8.0")
3236 (defcustom org-read-date-prefer-future t
3237 "Non-nil means assume future for incomplete date input from user.
3238 This affects the following situations:
3239 1. The user gives a month but not a year.
3240 For example, if it is April and you enter \"feb 2\", this will be read
3241 as Feb 2, *next* year. \"May 5\", however, will be this year.
3242 2. The user gives a day, but no month.
3243 For example, if today is the 15th, and you enter \"3\", Org-mode will
3244 read this as the third of *next* month. However, if you enter \"17\",
3245 it will be considered as *this* month.
3247 If you set this variable to the symbol `time', then also the following
3250 3. If the user gives a time.
3251 If the time is before now, it will be interpreted as tomorrow.
3253 Currently none of this works for ISO week specifications.
3255 When this option is nil, the current day, month and year will always be
3258 See also `org-agenda-jump-prefer-future'."
3261 (const :tag
"Never" nil
)
3262 (const :tag
"Check month and day" t
)
3263 (const :tag
"Check month, day, and time" time
)))
3265 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
3266 "Should the agenda jump command prefer the future for incomplete dates?
3267 The default is to do the same as configured in `org-read-date-prefer-future'.
3268 But you can also set a deviating value here.
3269 This may t or nil, or the symbol `org-read-date-prefer-future'."
3274 (const :tag
"Use org-read-date-prefer-future"
3275 org-read-date-prefer-future
)
3276 (const :tag
"Never" nil
)
3277 (const :tag
"Always" t
)))
3279 (defcustom org-read-date-force-compatible-dates t
3280 "Should date/time prompt force dates that are guaranteed to work in Emacs?
3282 Depending on the system Emacs is running on, certain dates cannot
3283 be represented with the type used internally to represent time.
3284 Dates between 1970-1-1 and 2038-1-1 can always be represented
3285 correctly. Some systems allow for earlier dates, some for later,
3286 some for both. One way to find out it to insert any date into an
3287 Org buffer, putting the cursor on the year and hitting S-up and
3288 S-down to test the range.
3290 When this variable is set to t, the date/time prompt will not let
3291 you specify dates outside the 1970-2037 range, so it is certain that
3292 these dates will work in whatever version of Emacs you are
3293 running, and also that you can move a file from one Emacs implementation
3294 to another. WHenever Org is forcing the year for you, it will display
3297 When this variable is nil, Org will check if the date is
3298 representable in the specific Emacs implementation you are using.
3299 If not, it will force a year, usually the current year, and beep
3300 to remind you. Currently this setting is not recommended because
3301 the likelihood that you will open your Org files in an Emacs that
3302 has limited date range is not negligible.
3304 A workaround for this problem is to use diary sexp dates for time
3305 stamps outside of this range."
3310 (defcustom org-read-date-display-live t
3311 "Non-nil means display current interpretation of date prompt live.
3312 This display will be in an overlay, in the minibuffer."
3316 (defcustom org-read-date-popup-calendar t
3317 "Non-nil means pop up a calendar when prompting for a date.
3318 In the calendar, the date can be selected with mouse-1. However, the
3319 minibuffer will also be active, and you can simply enter the date as well.
3320 When nil, only the minibuffer will be available."
3323 (org-defvaralias 'org-popup-calendar-for-date-prompt
3324 'org-read-date-popup-calendar
)
3326 (make-obsolete-variable
3327 'org-read-date-minibuffer-setup-hook
3328 "Set `org-read-date-minibuffer-local-map' instead." "24.4")
3329 (defcustom org-read-date-minibuffer-setup-hook nil
3330 "Hook to be used to set up keys for the date/time interface.
3331 Add key definitions to `minibuffer-local-map', which will be a
3334 WARNING: This option is obsolete, you should use
3335 `org-read-date-minibuffer-local-map' to set up keys."
3339 (defcustom org-extend-today-until
0
3340 "The hour when your day really ends. Must be an integer.
3341 This has influence for the following applications:
3342 - When switching the agenda to \"today\". It it is still earlier than
3343 the time given here, the day recognized as TODAY is actually yesterday.
3344 - When a date is read from the user and it is still before the time given
3345 here, the current date and time will be assumed to be yesterday, 23:59.
3346 Also, timestamps inserted in capture templates follow this rule.
3348 IMPORTANT: This is a feature whose implementation is and likely will
3349 remain incomplete. Really, it is only here because past midnight seems to
3350 be the favorite working time of John Wiegley :-)"
3354 (defcustom org-use-effective-time nil
3355 "If non-nil, consider `org-extend-today-until' when creating timestamps.
3356 For example, if `org-extend-today-until' is 8, and it's 4am, then the
3357 \"effective time\" of any timestamps between midnight and 8am will be
3358 23:59 of the previous day."
3363 (defcustom org-use-last-clock-out-time-as-effective-time nil
3364 "When non-nil, use the last clock out time for `org-todo'.
3365 Note that this option has precedence over the combined use of
3366 `org-use-effective-time' and `org-extend-today-until'."
3369 :package-version
'(Org .
"8.0")
3372 (defcustom org-edit-timestamp-down-means-later nil
3373 "Non-nil means S-down will increase the time in a time stamp.
3374 When nil, S-up will increase."
3378 (defcustom org-calendar-follow-timestamp-change t
3379 "Non-nil means make the calendar window follow timestamp changes.
3380 When a timestamp is modified and the calendar window is visible, it will be
3381 moved to the new date."
3385 (defgroup org-tags nil
3386 "Options concerning tags in Org-mode."
3390 (defcustom org-tag-alist nil
3391 "List of tags allowed in Org-mode files.
3392 When this list is nil, Org-mode will base TAG input on what is already in the
3394 The value of this variable is an alist, the car of each entry must be a
3395 keyword as a string, the cdr may be a character that is used to select
3396 that tag through the fast-tag-selection interface.
3397 See the manual for details."
3401 (cons (string :tag
"Tag name")
3402 (character :tag
"Access char"))
3403 (list :tag
"Start radio group"
3405 (option (string :tag
"Group description")))
3406 (list :tag
"Group tags delimiter"
3408 (list :tag
"End radio group"
3410 (option (string :tag
"Group description")))
3411 (const :tag
"New line" (:newline
)))))
3413 (defcustom org-tag-persistent-alist nil
3414 "List of tags that will always appear in all Org-mode files.
3415 This is in addition to any in buffer settings or customizations
3417 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
3418 The value of this variable is an alist, the car of each entry must be a
3419 keyword as a string, the cdr may be a character that is used to select
3420 that tag through the fast-tag-selection interface.
3421 See the manual for details.
3422 To disable these tags on a per-file basis, insert anywhere in the file:
3427 (cons (string :tag
"Tag name")
3428 (character :tag
"Access char"))
3429 (const :tag
"Start radio group" (:startgroup
))
3430 (const :tag
"Group tags delimiter" (:grouptags
))
3431 (const :tag
"End radio group" (:endgroup
))
3432 (const :tag
"New line" (:newline
)))))
3434 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
3435 "If non-nil, always offer completion for all tags of all agenda files.
3436 Instead of customizing this variable directly, you might want to
3437 set it locally for capture buffers, because there no list of
3438 tags in that file can be created dynamically (there are none).
3440 (add-hook 'org-capture-mode-hook
3442 (set (make-local-variable
3443 'org-complete-tags-always-offer-all-agenda-tags)
3449 (defvar org-file-tags nil
3450 "List of tags that can be inherited by all entries in the file.
3451 The tags will be inherited if the variable `org-use-tag-inheritance'
3452 says they should be.
3453 This variable is populated from #+FILETAGS lines.")
3455 (defcustom org-use-fast-tag-selection
'auto
3456 "Non-nil means use fast tag selection scheme.
3457 This is a special interface to select and deselect tags with single keys.
3458 When nil, fast selection is never used.
3459 When the symbol `auto', fast selection is used if and only if selection
3460 characters for tags have been configured, either through the variable
3461 `org-tag-alist' or through a #+TAGS line in the buffer.
3462 When t, fast selection is always used and selection keys are assigned
3463 automatically if necessary."
3466 (const :tag
"Always" t
)
3467 (const :tag
"Never" nil
)
3468 (const :tag
"When selection characters are configured" auto
)))
3470 (defcustom org-fast-tag-selection-single-key nil
3471 "Non-nil means fast tag selection exits after first change.
3472 When nil, you have to press RET to exit it.
3473 During fast tag selection, you can toggle this flag with `C-c'.
3474 This variable can also have the value `expert'. In this case, the window
3475 displaying the tags menu is not even shown, until you press C-c again."
3478 (const :tag
"No" nil
)
3479 (const :tag
"Yes" t
)
3480 (const :tag
"Expert" expert
)))
3482 (defvar org-fast-tag-selection-include-todo nil
3483 "Non-nil means fast tags selection interface will also offer TODO states.
3484 This is an undocumented feature, you should not rely on it.")
3486 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
3487 "The column to which tags should be indented in a headline.
3488 If this number is positive, it specifies the column. If it is negative,
3489 it means that the tags should be flushright to that column. For example,
3490 -80 works well for a normal 80 character screen.
3491 When 0, place tags directly after headline text, with only one space in
3496 (defcustom org-auto-align-tags t
3497 "Non-nil keeps tags aligned when modifying headlines.
3498 Some operations (i.e. demoting) change the length of a headline and
3499 therefore shift the tags around. With this option turned on, after
3500 each such operation the tags are again aligned to `org-tags-column'."
3504 (defcustom org-use-tag-inheritance t
3505 "Non-nil means tags in levels apply also for sublevels.
3506 When nil, only the tags directly given in a specific line apply there.
3507 This may also be a list of tags that should be inherited, or a regexp that
3508 matches tags that should be inherited. Additional control is possible
3509 with the variable `org-tags-exclude-from-inheritance' which gives an
3510 explicit list of tags to be excluded from inheritance, even if the value of
3511 `org-use-tag-inheritance' would select it for inheritance.
3513 If this option is t, a match early-on in a tree can lead to a large
3514 number of matches in the subtree when constructing the agenda or creating
3515 a sparse tree. If you only want to see the first match in a tree during
3516 a search, check out the variable `org-tags-match-list-sublevels'."
3519 (const :tag
"Not" nil
)
3520 (const :tag
"Always" t
)
3521 (repeat :tag
"Specific tags" (string :tag
"Tag"))
3522 (regexp :tag
"Tags matched by regexp")))
3524 (defcustom org-tags-exclude-from-inheritance nil
3525 "List of tags that should never be inherited.
3526 This is a way to exclude a few tags from inheritance. For way to do
3527 the opposite, to actively allow inheritance for selected tags,
3528 see the variable `org-use-tag-inheritance'."
3530 :type
'(repeat (string :tag
"Tag")))
3532 (defun org-tag-inherit-p (tag)
3533 "Check if TAG is one that should be inherited."
3535 ((member tag org-tags-exclude-from-inheritance
) nil
)
3536 ((eq org-use-tag-inheritance t
) t
)
3537 ((not org-use-tag-inheritance
) nil
)
3538 ((stringp org-use-tag-inheritance
)
3539 (string-match org-use-tag-inheritance tag
))
3540 ((listp org-use-tag-inheritance
)
3541 (member tag org-use-tag-inheritance
))
3542 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3544 (defcustom org-tags-match-list-sublevels t
3545 "Non-nil means list also sublevels of headlines matching a search.
3546 This variable applies to tags/property searches, and also to stuck
3547 projects because this search is based on a tags match as well.
3549 When set to the symbol `indented', sublevels are indented with
3552 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3553 the sublevels of a headline matching a tag search often also match
3554 the same search. Listing all of them can create very long lists.
3555 Setting this variable to nil causes subtrees of a match to be skipped.
3557 This variable is semi-obsolete and probably should always be true. It
3558 is better to limit inheritance to certain tags using the variables
3559 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3562 (const :tag
"No, don't list them" nil
)
3563 (const :tag
"Yes, do list them" t
)
3564 (const :tag
"List them, indented with leading dots" indented
)))
3566 (defcustom org-tags-sort-function nil
3567 "When set, tags are sorted using this function as a comparator."
3570 (const :tag
"No sorting" nil
)
3571 (const :tag
"Alphabetical" string
<)
3572 (const :tag
"Reverse alphabetical" string
>)
3573 (function :tag
"Custom function" nil
)))
3575 (defvar org-tags-history nil
3576 "History of minibuffer reads for tags.")
3577 (defvar org-last-tags-completion-table nil
3578 "The last used completion table for tags.")
3579 (defvar org-after-tags-change-hook nil
3580 "Hook that is run after the tags in a line have changed.")
3582 (defgroup org-properties nil
3583 "Options concerning properties in Org-mode."
3584 :tag
"Org Properties"
3587 (defcustom org-property-format
"%-10s %s"
3588 "How property key/value pairs should be formatted by `indent-line'.
3589 When `indent-line' hits a property definition, it will format the line
3590 according to this format, mainly to make sure that the values are
3591 lined-up with respect to each other."
3592 :group
'org-properties
3595 (defcustom org-properties-postprocess-alist nil
3596 "Alist of properties and functions to adjust inserted values.
3597 Elements of this alist must be of the form
3599 ([string] [function])
3601 where [string] must be a property name and [function] must be a
3602 lambda expression: this lambda expression must take one argument,
3603 the value to adjust, and return the new value as a string.
3605 For example, this element will allow the property \"Remaining\"
3606 to be updated wrt the relation between the \"Effort\" property
3607 and the clock summary:
3609 ((\"Remaining\" (lambda(value)
3610 (let ((clocksum (org-clock-sum-current-item))
3611 (effort (org-duration-string-to-minutes
3612 (org-entry-get (point) \"Effort\"))))
3613 (org-minutes-to-clocksum-string (- effort clocksum))))))"
3614 :group
'org-properties
3616 :type
'(alist :key-type
(string :tag
"Property")
3617 :value-type
(function :tag
"Function")))
3619 (defcustom org-use-property-inheritance nil
3620 "Non-nil means properties apply also for sublevels.
3622 This setting is chiefly used during property searches. Turning it on can
3623 cause significant overhead when doing a search, which is why it is not
3626 When nil, only the properties directly given in the current entry count.
3627 When t, every property is inherited. The value may also be a list of
3628 properties that should have inheritance, or a regular expression matching
3629 properties that should be inherited.
3631 However, note that some special properties use inheritance under special
3632 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3633 and the properties ending in \"_ALL\" when they are used as descriptor
3634 for valid values of a property.
3636 Note for programmers:
3637 When querying an entry with `org-entry-get', you can control if inheritance
3638 should be used. By default, `org-entry-get' looks only at the local
3639 properties. You can request inheritance by setting the inherit argument
3640 to t (to force inheritance) or to `selective' (to respect the setting
3642 :group
'org-properties
3644 (const :tag
"Not" nil
)
3645 (const :tag
"Always" t
)
3646 (repeat :tag
"Specific properties" (string :tag
"Property"))
3647 (regexp :tag
"Properties matched by regexp")))
3649 (defun org-property-inherit-p (property)
3650 "Check if PROPERTY is one that should be inherited."
3652 ((eq org-use-property-inheritance t
) t
)
3653 ((not org-use-property-inheritance
) nil
)
3654 ((stringp org-use-property-inheritance
)
3655 (string-match org-use-property-inheritance property
))
3656 ((listp org-use-property-inheritance
)
3657 (member property org-use-property-inheritance
))
3658 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3660 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3661 "The default column format, if no other format has been defined.
3662 This variable can be set on the per-file basis by inserting a line
3664 #+COLUMNS: %25ITEM ....."
3665 :group
'org-properties
3668 (defcustom org-columns-ellipses
".."
3669 "The ellipses to be used when a field in column view is truncated.
3670 When this is the empty string, as many characters as possible are shown,
3671 but then there will be no visual indication that the field has been truncated.
3672 When this is a string of length N, the last N characters of a truncated
3673 field are replaced by this string. If the column is narrower than the
3674 ellipses string, only part of the ellipses string will be shown."
3675 :group
'org-properties
3678 (defcustom org-columns-modify-value-for-display-function nil
3679 "Function that modifies values for display in column view.
3680 For example, it can be used to cut out a certain part from a time stamp.
3681 The function must take 2 arguments:
3683 column-title The title of the column (*not* the property name)
3684 value The value that should be modified.
3686 The function should return the value that should be displayed,
3687 or nil if the normal value should be used."
3688 :group
'org-properties
3689 :type
'(choice (const nil
) (function)))
3691 (defconst org-global-properties-fixed
3692 '(("VISIBILITY_ALL" .
"folded children content all")
3693 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3694 "List of property/value pairs that can be inherited by any entry.
3696 These are fixed values, for the preset properties. The user variable
3697 that can be used to add to this list is `org-global-properties'.
3699 The entries in this list are cons cells where the car is a property
3700 name and cdr is a string with the value. If the value represents
3701 multiple items like an \"_ALL\" property, separate the items by
3704 (defcustom org-global-properties nil
3705 "List of property/value pairs that can be inherited by any entry.
3707 This list will be combined with the constant `org-global-properties-fixed'.
3709 The entries in this list are cons cells where the car is a property
3710 name and cdr is a string with the value.
3712 You can set buffer-local values for the same purpose in the variable
3713 `org-file-properties' this by adding lines like
3715 #+PROPERTY: NAME VALUE"
3716 :group
'org-properties
3718 (cons (string :tag
"Property")
3719 (string :tag
"Value"))))
3721 (defvar org-file-properties nil
3722 "List of property/value pairs that can be inherited by any entry.
3723 Valid for the current buffer.
3724 This variable is populated from #+PROPERTY lines.")
3725 (make-variable-buffer-local 'org-file-properties
)
3727 (defgroup org-agenda nil
3728 "Options concerning agenda views in Org-mode."
3732 (defvar org-category nil
3733 "Variable used by org files to set a category for agenda display.
3734 Such files should use a file variable to set it, for example
3736 # -*- mode: org; org-category: \"ELisp\"
3738 or contain a special line
3742 If the file does not specify a category, then file's base name
3744 (make-variable-buffer-local 'org-category
)
3745 (put 'org-category
'safe-local-variable
(lambda (x) (or (symbolp x
) (stringp x
))))
3747 (defcustom org-agenda-files nil
3748 "The files to be used for agenda display.
3749 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3750 \\[org-remove-file]. You can also use customize to edit the list.
3752 If an entry is a directory, all files in that directory that are matched by
3753 `org-agenda-file-regexp' will be part of the file list.
3755 If the value of the variable is not a list but a single file name, then
3756 the list of agenda files is actually stored and maintained in that file, one
3757 agenda file per line. In this file paths can be given relative to
3758 `org-directory'. Tilde expansion and environment variable substitution
3762 (repeat :tag
"List of files and directories" file
)
3763 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3765 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3766 "Regular expression to match files for `org-agenda-files'.
3767 If any element in the list in that variable contains a directory instead
3768 of a normal file, all files in that directory that are matched by this
3769 regular expression will be included."
3773 (defcustom org-agenda-text-search-extra-files nil
3774 "List of extra files to be searched by text search commands.
3775 These files will be searched in addition to the agenda files by the
3776 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3777 Note that these files will only be searched for text search commands,
3778 not for the other agenda views like todo lists, tag searches or the weekly
3779 agenda. This variable is intended to list notes and possibly archive files
3780 that should also be searched by these two commands.
3781 In fact, if the first element in the list is the symbol `agenda-archives',
3782 then all archive files of all agenda files will be added to the search
3785 :type
'(set :greedy t
3786 (const :tag
"Agenda Archives" agenda-archives
)
3787 (repeat :inline t
(file))))
3789 (org-defvaralias 'org-agenda-multi-occur-extra-files
3790 'org-agenda-text-search-extra-files
)
3792 (defcustom org-agenda-skip-unavailable-files nil
3793 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3794 A nil value means to remove them, after a query, from the list."
3798 (defcustom org-calendar-to-agenda-key
[?c
]
3799 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3800 The command `org-calendar-goto-agenda' will be bound to this key. The
3801 default is the character `c' because then `c' can be used to switch back and
3802 forth between agenda and calendar."
3806 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3807 "The key to be installed in `calendar-mode-map' for adding diary entries.
3808 This option is irrelevant until `org-agenda-diary-file' has been configured
3809 to point to an Org-mode file. When that is the case, the command
3810 `org-agenda-diary-entry' will be bound to the key given here, by default
3811 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3812 if you want to continue doing this, you need to change this to a different
3817 (defcustom org-agenda-diary-file
'diary-file
3818 "File to which to add new entries with the `i' key in agenda and calendar.
3819 When this is the symbol `diary-file', the functionality in the Emacs
3820 calendar will be used to add entries to the `diary-file'. But when this
3821 points to a file, `org-agenda-diary-entry' will be used instead."
3824 (const :tag
"The standard Emacs diary file" diary-file
)
3825 (file :tag
"Special Org file diary entries")))
3827 (eval-after-load "calendar"
3829 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3830 'org-calendar-goto-agenda
)
3831 (add-hook 'calendar-mode-hook
3833 (unless (eq org-agenda-diary-file
'diary-file
)
3834 (define-key calendar-mode-map
3835 org-calendar-insert-diary-entry-key
3836 'org-agenda-diary-entry
))))))
3838 (defgroup org-latex nil
3839 "Options for embedding LaTeX code into Org-mode."
3843 (defcustom org-format-latex-options
3844 '(:foreground default
:background default
:scale
1.0
3845 :html-foreground
"Black" :html-background
"Transparent"
3846 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3847 "Options for creating images from LaTeX fragments.
3848 This is a property list with the following properties:
3849 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3850 `default' means use the foreground of the default face.
3851 `auto' means use the foreground from the text face.
3852 :background the background color, or \"Transparent\".
3853 `default' means use the background of the default face.
3854 `auto' means use the background from the text face.
3855 :scale a scaling factor for the size of the images, to get more pixels
3856 :html-foreground, :html-background, :html-scale
3857 the same numbers for HTML export.
3858 :matchers a list indicating which matchers should be used to
3859 find LaTeX fragments. Valid members of this list are:
3860 \"begin\" find environments
3861 \"$1\" find single characters surrounded by $.$
3862 \"$\" find math expressions surrounded by $...$
3863 \"$$\" find math expressions surrounded by $$....$$
3864 \"\\(\" find math expressions surrounded by \\(...\\)
3865 \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
3869 (defcustom org-format-latex-signal-error t
3870 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3871 When nil, just push out a message."
3876 (defcustom org-latex-to-mathml-jar-file nil
3877 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3878 Use this to specify additional executable file say a jar file.
3880 When using MathToWeb as the converter, specify the full-path to
3881 your mathtoweb.jar file."
3885 (const :tag
"None" nil
)
3886 (file :tag
"JAR file" :must-match t
)))
3888 (defcustom org-latex-to-mathml-convert-command nil
3889 "Command to convert LaTeX fragments to MathML.
3890 Replace format-specifiers in the command as noted below and use
3891 `shell-command' to convert LaTeX to MathML.
3892 %j: Executable file in fully expanded form as specified by
3893 `org-latex-to-mathml-jar-file'.
3894 %I: Input LaTeX file in fully expanded form
3895 %o: Output MathML file
3896 This command is used by `org-create-math-formula'.
3898 When using MathToWeb as the converter, set this to
3899 \"java -jar %j -unicode -force -df %o %I\"."
3903 (const :tag
"None" nil
)
3904 (string :tag
"\nShell command")))
3906 (defcustom org-latex-create-formula-image-program
'dvipng
3907 "Program to convert LaTeX fragments with.
3909 dvipng Process the LaTeX fragments to dvi file, then convert
3910 dvi files to png files using dvipng.
3911 This will also include processing of non-math environments.
3912 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3913 to convert pdf files to png files"
3917 (const :tag
"dvipng" dvipng
)
3918 (const :tag
"imagemagick" imagemagick
)))
3920 (defcustom org-latex-preview-ltxpng-directory
"ltxpng/"
3921 "Path to store latex preview images.
3922 A relative path here creates many directories relative to the
3923 processed org files paths. An absolute path puts all preview
3924 images at the same place."
3929 (defun org-format-latex-mathml-available-p ()
3930 "Return t if `org-latex-to-mathml-convert-command' is usable."
3932 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3933 org-latex-to-mathml-convert-command
)
3934 (let ((executable (car (split-string
3935 org-latex-to-mathml-convert-command
))))
3936 (when (executable-find executable
)
3938 "%j" org-latex-to-mathml-convert-command
)
3939 (file-readable-p org-latex-to-mathml-jar-file
)
3942 (defcustom org-format-latex-header
"\\documentclass{article}
3943 \\usepackage[usenames]{color}
3946 \\pagestyle{empty} % do not remove
3947 % The settings below are copied from fullpage.sty
3948 \\setlength{\\textwidth}{\\paperwidth}
3949 \\addtolength{\\textwidth}{-3cm}
3950 \\setlength{\\oddsidemargin}{1.5cm}
3951 \\addtolength{\\oddsidemargin}{-2.54cm}
3952 \\setlength{\\evensidemargin}{\\oddsidemargin}
3953 \\setlength{\\textheight}{\\paperheight}
3954 \\addtolength{\\textheight}{-\\headheight}
3955 \\addtolength{\\textheight}{-\\headsep}
3956 \\addtolength{\\textheight}{-\\footskip}
3957 \\addtolength{\\textheight}{-3cm}
3958 \\setlength{\\topmargin}{1.5cm}
3959 \\addtolength{\\topmargin}{-2.54cm}"
3960 "The document header used for processing LaTeX fragments.
3961 It is imperative that this header make sure that no page number
3962 appears on the page. The package defined in the variables
3963 `org-latex-default-packages-alist' and `org-latex-packages-alist'
3964 will either replace the placeholder \"[PACKAGES]\" in this
3965 header, or they will be appended."
3969 (defun org-set-packages-alist (var val
)
3970 "Set the packages alist and make sure it has 3 elements per entry."
3971 (set var
(mapcar (lambda (x)
3972 (if (and (consp x
) (= (length x
) 2))
3973 (list (car x
) (nth 1 x
) t
)
3977 (defun org-get-packages-alist (var)
3978 "Get the packages alist and make sure it has 3 elements per entry."
3980 (if (and (consp x
) (= (length x
) 2))
3981 (list (car x
) (nth 1 x
) t
)
3983 (default-value var
)))
3985 (defcustom org-latex-default-packages-alist
3986 '(("AUTO" "inputenc" t
)
3990 ("" "longtable" nil
)
3994 ("normalem" "ulem" t
)
4002 "Alist of default packages to be inserted in the header.
4004 Change this only if one of the packages here causes an
4005 incompatibility with another package you are using.
4007 The packages in this list are needed by one part or another of
4008 Org mode to function properly:
4010 - inputenc, fontenc: for basic font and character selection
4011 - fixltx2e: Important patches of LaTeX itself
4012 - graphicx: for including images
4013 - longtable: For multipage tables
4014 - float, wrapfig: for figure placement
4015 - rotating: for sideways figures and tables
4016 - ulem: for underline and strike-through
4017 - amsmath: for subscript and superscript and math environments
4018 - textcomp, marvosymb, wasysym, amssymb: for various symbols used
4019 for interpreting the entities in `org-entities'. You can skip
4020 some of these packages if you don't use any of their symbols.
4021 - hyperref: for cross references
4023 Therefore you should not modify this variable unless you know
4024 what you are doing. The one reason to change it anyway is that
4025 you might be loading some other package that conflicts with one
4026 of the default packages. Each element is either a cell or
4029 A cell is of the format:
4031 \( \"options\" \"package\" SNIPPET-FLAG).
4033 If SNIPPET-FLAG is non-nil, the package also needs to be included
4034 when compiling LaTeX snippets into images for inclusion into
4037 A string will be inserted as-is in the header of the document."
4039 :group
'org-export-latex
4040 :set
'org-set-packages-alist
4041 :get
'org-get-packages-alist
4045 (list :tag
"options/package pair"
4046 (string :tag
"options")
4047 (string :tag
"package")
4048 (boolean :tag
"Snippet"))
4049 (string :tag
"A line of LaTeX"))))
4051 (defcustom org-latex-packages-alist nil
4052 "Alist of packages to be inserted in every LaTeX header.
4054 These will be inserted after `org-latex-default-packages-alist'.
4055 Each element is either a cell or a string.
4057 A cell is of the format:
4059 \(\"options\" \"package\" SNIPPET-FLAG)
4061 SNIPPET-FLAG, when non-nil, indicates that this package is also
4062 needed when turning LaTeX snippets into images for inclusion into
4065 A string will be inserted as-is in the header of the document.
4067 Make sure that you only list packages here which:
4069 - you want in every file;
4070 - do not conflict with the setup in `org-format-latex-header';
4071 - do not conflict with the default packages in
4072 `org-latex-default-packages-alist'."
4074 :group
'org-export-latex
4075 :set
'org-set-packages-alist
4076 :get
'org-get-packages-alist
4079 (list :tag
"options/package pair"
4080 (string :tag
"options")
4081 (string :tag
"package")
4082 (boolean :tag
"Snippet"))
4083 (string :tag
"A line of LaTeX"))))
4085 (defgroup org-appearance nil
4086 "Settings for Org-mode appearance."
4087 :tag
"Org Appearance"
4090 (defcustom org-level-color-stars-only nil
4091 "Non-nil means fontify only the stars in each headline.
4092 When nil, the entire headline is fontified.
4093 Changing it requires restart of `font-lock-mode' to become effective
4094 also in regions already fontified."
4095 :group
'org-appearance
4098 (defcustom org-hide-leading-stars nil
4099 "Non-nil means hide the first N-1 stars in a headline.
4100 This works by using the face `org-hide' for these stars. This
4101 face is white for a light background, and black for a dark
4102 background. You may have to customize the face `org-hide' to
4104 Changing it requires restart of `font-lock-mode' to become effective
4105 also in regions already fontified.
4106 You may also set this on a per-file basis by adding one of the following
4107 lines to the buffer:
4109 #+STARTUP: hidestars
4110 #+STARTUP: showstars"
4111 :group
'org-appearance
4114 (defcustom org-hidden-keywords nil
4115 "List of symbols corresponding to keywords to be hidden the org buffer.
4116 For example, a value '(title) for this list will make the document's title
4117 appear in the buffer without the initial #+TITLE: keyword."
4118 :group
'org-appearance
4120 :type
'(set (const :tag
"#+AUTHOR" author
)
4121 (const :tag
"#+DATE" date
)
4122 (const :tag
"#+EMAIL" email
)
4123 (const :tag
"#+TITLE" title
)))
4125 (defcustom org-custom-properties nil
4126 "List of properties (as strings) with a special meaning.
4127 The default use of these custom properties is to let the user
4128 hide them with `org-toggle-custom-properties-visibility'."
4129 :group
'org-properties
4130 :group
'org-appearance
4132 :type
'(repeat (string :tag
"Property Name")))
4134 (defcustom org-fontify-done-headline nil
4135 "Non-nil means change the face of a headline if it is marked DONE.
4136 Normally, only the TODO/DONE keyword indicates the state of a headline.
4137 When this is non-nil, the headline after the keyword is set to the
4138 `org-headline-done' as an additional indication."
4139 :group
'org-appearance
4142 (defcustom org-fontify-emphasized-text t
4143 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
4144 Changing this variable requires a restart of Emacs to take effect."
4145 :group
'org-appearance
4148 (defcustom org-fontify-whole-heading-line nil
4149 "Non-nil means fontify the whole line for headings.
4150 This is useful when setting a background color for the
4152 :group
'org-appearance
4155 (defcustom org-highlight-latex-and-related nil
4156 "Non-nil means highlight LaTeX related syntax in the buffer.
4157 When non nil, the value should be a list containing any of the
4159 `latex' Highlight LaTeX snippets and environments.
4160 `script' Highlight subscript and superscript.
4161 `entities' Highlight entities."
4162 :group
'org-appearance
4164 :package-version
'(Org .
"8.0")
4166 (const :tag
"No highlighting" nil
)
4167 (set :greedy t
:tag
"Highlight"
4168 (const :tag
"LaTeX snippets and environments" latex
)
4169 (const :tag
"Subscript and superscript" script
)
4170 (const :tag
"Entities" entities
))))
4172 (defcustom org-hide-emphasis-markers nil
4173 "Non-nil mean font-lock should hide the emphasis marker characters."
4174 :group
'org-appearance
4177 (defcustom org-hide-macro-markers nil
4178 "Non-nil mean font-lock should hide the brackets marking macro calls."
4179 :group
'org-appearance
4182 (defcustom org-pretty-entities nil
4183 "Non-nil means show entities as UTF8 characters.
4184 When nil, the \\name form remains in the buffer."
4185 :group
'org-appearance
4189 (defcustom org-pretty-entities-include-sub-superscripts t
4190 "Non-nil means, pretty entity display includes formatting sub/superscripts."
4191 :group
'org-appearance
4195 (defvar org-emph-re nil
4196 "Regular expression for matching emphasis.
4197 After a match, the match groups contain these elements:
4198 0 The match of the full regular expression, including the characters
4199 before and after the proper match
4200 1 The character before the proper match, or empty at beginning of line
4201 2 The proper match, including the leading and trailing markers
4202 3 The leading marker like * or /, indicating the type of highlighting
4203 4 The text between the emphasis markers, not including the markers
4204 5 The character after the match, empty at the end of a line")
4205 (defvar org-verbatim-re nil
4206 "Regular expression for matching verbatim text.")
4207 (defvar org-emphasis-regexp-components
) ; defined just below
4208 (defvar org-emphasis-alist
) ; defined just below
4209 (defun org-set-emph-re (var val
)
4210 "Set variable and compute the emphasis regular expression."
4212 (when (and (boundp 'org-emphasis-alist
)
4213 (boundp 'org-emphasis-regexp-components
)
4214 org-emphasis-alist org-emphasis-regexp-components
)
4215 (let* ((e org-emphasis-regexp-components
)
4221 (body1 (concat body
"*?"))
4222 (markers (mapconcat 'car org-emphasis-alist
""))
4223 (vmarkers (mapconcat
4224 (lambda (x) (if (eq (nth 2 x
) 'verbatim
) (car x
) ""))
4225 org-emphasis-alist
"")))
4226 ;; make sure special characters appear at the right position in the class
4227 (if (string-match "\\^" markers
)
4228 (setq markers
(concat (replace-match "" t t markers
) "^")))
4229 (if (string-match "-" markers
)
4230 (setq markers
(concat (replace-match "" t t markers
) "-")))
4231 (if (string-match "\\^" vmarkers
)
4232 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
4233 (if (string-match "-" vmarkers
)
4234 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
4236 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
4237 (int-to-string nl
) "\\}")))
4240 (concat "\\([" pre
"]\\|^\\)"
4242 "\\([" markers
"]\\)"
4250 "\\([" post
"]\\|$\\)"))
4251 (setq org-verbatim-re
4252 (concat "\\([" pre
"]\\|^\\)"
4254 "\\([" vmarkers
"]\\)"
4262 "\\([" post
"]\\|$\\)")))))
4264 ;; This used to be a defcustom (Org <8.0) but allowing the users to
4265 ;; set this option proved cumbersome. See this message/thread:
4266 ;; http://article.gmane.org/gmane.emacs.orgmode/68681
4267 (defvar org-emphasis-regexp-components
4268 '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
4269 "Components used to build the regular expression for emphasis.
4270 This is a list with five entries. Terminology: In an emphasis string
4271 like \" *strong word* \", we call the initial space PREMATCH, the final
4272 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
4273 and \"trong wor\" is the body. The different components in this variable
4274 specify what is allowed/forbidden in each part:
4276 pre Chars allowed as prematch. Beginning of line will be allowed too.
4277 post Chars allowed as postmatch. End of line will be allowed too.
4278 border The chars *forbidden* as border characters.
4279 body-regexp A regexp like \".\" to match a body character. Don't use
4280 non-shy groups here, and don't allow newline here.
4281 newline The maximum number of newlines allowed in an emphasis exp.
4283 You need to reload Org or to restart Emacs after customizing this.")
4285 (defcustom org-emphasis-alist
4289 ("=" org-verbatim verbatim
)
4290 ("~" org-code verbatim
)
4291 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))))
4292 "Alist of characters and faces to emphasize text.
4293 Text starting and ending with a special character will be emphasized,
4294 for example *bold*, _underlined_ and /italic/. This variable sets the
4295 marker characters and the face to be used by font-lock for highlighting
4296 in Org-mode Emacs buffers.
4298 You need to reload Org or to restart Emacs after customizing this."
4299 :group
'org-appearance
4300 :set
'org-set-emph-re
4302 :package-version
'(Org .
"8.0")
4305 (string :tag
"Marker character")
4307 (face :tag
"Font-lock-face")
4308 (plist :tag
"Face property list"))
4309 (option (const verbatim
)))))
4311 (defvar org-protecting-blocks
4312 '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
4313 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
4314 This is needed for font-lock setup.")
4316 ;;; Miscellaneous options
4318 (defgroup org-completion nil
4319 "Completion in Org-mode."
4320 :tag
"Org Completion"
4323 (defcustom org-completion-use-ido nil
4324 "Non-nil means use ido completion wherever possible.
4325 Note that `ido-mode' must be active for this variable to be relevant.
4326 If you decide to turn this variable on, you might well want to turn off
4327 `org-outline-path-complete-in-steps'.
4328 See also `org-completion-use-iswitchb'."
4329 :group
'org-completion
4332 (defcustom org-completion-use-iswitchb nil
4333 "Non-nil means use iswitchb completion wherever possible.
4334 Note that `iswitchb-mode' must be active for this variable to be relevant.
4335 If you decide to turn this variable on, you might well want to turn off
4336 `org-outline-path-complete-in-steps'.
4337 Note that this variable has only an effect if `org-completion-use-ido' is nil."
4338 :group
'org-completion
4341 (defcustom org-completion-fallback-command
'hippie-expand
4342 "The expansion command called by \\[pcomplete] in normal context.
4343 Normal means, no org-mode-specific context."
4344 :group
'org-completion
4347 ;;; Functions and variables from their packages
4348 ;; Declared here to avoid compiler warnings
4351 (defvar outline-mode-menu-heading
)
4352 (defvar outline-mode-menu-show
)
4353 (defvar outline-mode-menu-hide
)
4354 (defvar zmacs-regions
) ; XEmacs regions
4357 (defvar mark-active
)
4360 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
4361 (declare-function calendar-forward-day
"cal-move" (arg))
4362 (declare-function calendar-goto-date
"cal-move" (date))
4363 (declare-function calendar-goto-today
"cal-move" ())
4364 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
4365 (defvar calc-embedded-close-formula
)
4366 (defvar calc-embedded-open-formula
)
4367 (declare-function cdlatex-tab
"ext:cdlatex" ())
4368 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
4369 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
4370 (defvar font-lock-unfontify-region-function
)
4371 (declare-function iswitchb-read-buffer
"iswitchb"
4372 (prompt &optional default require-match start matches-set
))
4373 (defvar iswitchb-temp-buflist
)
4374 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
4375 (defvar org-agenda-tags-todo-honor-ignore-options
)
4376 (declare-function org-agenda-skip
"org-agenda" ())
4378 org-agenda-format-item
"org-agenda"
4379 (extra txt
&optional level category tags dotime noprefix remove-re habitp
))
4380 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
4381 (declare-function org-agenda-change-all-lines
"org-agenda"
4382 (newhead hdmarker
&optional fixface just-this
))
4383 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
4384 (declare-function org-agenda-maybe-redo
"org-agenda" ())
4385 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
4387 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
4388 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4389 "org-agenda" (&optional end
))
4390 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
4391 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
4392 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
4393 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
4394 (declare-function org-indent-mode
"org-indent" (&optional arg
))
4395 (declare-function parse-time-string
"parse-time" (string))
4396 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
4397 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
4398 (defvar remember-data-file
)
4399 (defvar texmathp-why
)
4400 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
4401 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
4402 (declare-function calc-eval
"calc" (str &optional separator
&rest args
))
4405 (defun turn-on-orgtbl ()
4406 "Unconditionally turn on `orgtbl-mode'."
4407 (require 'org-table
)
4410 (defun org-at-table-p (&optional table-type
)
4411 "Return t if the cursor is inside an org-type table.
4412 If TABLE-TYPE is non-nil, also check for table.el-type tables."
4413 (if org-enable-table-editor
4415 (beginning-of-line 1)
4416 (looking-at (if table-type org-table-any-line-regexp
4417 org-table-line-regexp
)))
4419 (defsubst org-table-p
() (org-at-table-p))
4421 (defun org-at-table.el-p
()
4422 "Return t if and only if we are at a table.el table."
4423 (and (org-at-table-p 'any
)
4425 (goto-char (org-table-begin 'any
))
4426 (looking-at org-table1-hline-regexp
))))
4428 (defun org-table-recognize-table.el
()
4429 "If there is a table.el table nearby, recognize it and move into it."
4430 (if org-table-tab-recognizes-table.el
4431 (if (org-at-table.el-p
)
4433 (beginning-of-line 1)
4434 (if (looking-at org-table-dataline-regexp
)
4436 (if (looking-at org-table1-hline-regexp
)
4438 (beginning-of-line 2)
4439 (if (looking-at org-table-any-border-regexp
)
4440 (beginning-of-line -
1)))))
4441 (if (re-search-forward "|" (org-table-end t
) t
)
4444 (if (table--at-cell-p (point))
4446 (message "recognizing table.el table...")
4447 (table-recognize-table)
4448 (message "recognizing table.el table...done")))
4449 (error "This should not happen"))
4454 (defun org-at-table-hline-p ()
4455 "Return t if the cursor is inside a hline in a table."
4456 (if org-enable-table-editor
4458 (beginning-of-line 1)
4459 (looking-at org-table-hline-regexp
))
4462 (defun org-table-map-tables (function &optional quietly
)
4463 "Apply FUNCTION to the start of all tables in the buffer."
4467 (goto-char (point-min))
4468 (while (re-search-forward org-table-any-line-regexp nil t
)
4470 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
4471 (beginning-of-line 1)
4472 (when (and (looking-at org-table-line-regexp
)
4473 ;; Exclude tables in src/example/verbatim/clocktable blocks
4474 (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
4475 (save-excursion (funcall function
))
4476 (or (looking-at org-table-line-regexp
)
4478 (re-search-forward org-table-any-border-regexp nil
1))))
4479 (unless quietly
(message "Mapping tables: done")))
4481 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock" (beg end
))
4482 (declare-function org-clock-update-mode-line
"org-clock" ())
4483 (declare-function org-resolve-clocks
"org-clock"
4484 (&optional also-non-dangling-p prompt last-valid
))
4486 (defun org-at-TBLFM-p (&optional pos
)
4487 "Return t when point (or POS) is in #+TBLFM line."
4490 (goto-char (or pos
(point)))
4491 (beginning-of-line 1)
4492 (looking-at org-TBLFM-regexp
)))
4494 (defvar org-clock-start-time
)
4495 (defvar org-clock-marker
(make-marker)
4496 "Marker recording the last clock-in.")
4497 (defvar org-clock-hd-marker
(make-marker)
4498 "Marker recording the last clock-in, but the headline position.")
4499 (defvar org-clock-heading
""
4500 "The heading of the current clock entry.")
4501 (defun org-clock-is-active ()
4502 "Return the buffer where the clock is currently running.
4503 Return nil if no clock is running."
4504 (marker-buffer org-clock-marker
))
4506 (defun org-check-running-clock ()
4507 "Check if the current buffer contains the running clock.
4508 If yes, offer to stop it and to save the buffer with the changes."
4509 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4510 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4513 (when (y-or-n-p "Save changed buffer?")
4516 (defun org-clocktable-try-shift (dir n
)
4517 "Check if this line starts a clock table, if yes, shift the time block."
4518 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4519 (org-clocktable-shift dir n
)))
4522 (defun org-clock-persistence-insinuate ()
4523 "Set up hooks for clock persistence."
4524 (require 'org-clock
)
4525 (add-hook 'org-mode-hook
'org-clock-load
)
4526 (add-hook 'kill-emacs-hook
'org-clock-save
))
4528 (defgroup org-archive nil
4529 "Options concerning archiving in Org-mode."
4531 :group
'org-structure
)
4533 (defcustom org-archive-location
"%s_archive::"
4534 "The location where subtrees should be archived.
4536 The value of this variable is a string, consisting of two parts,
4537 separated by a double-colon. The first part is a filename and
4538 the second part is a headline.
4540 When the filename is omitted, archiving happens in the same file.
4541 %s in the filename will be replaced by the current file
4542 name (without the directory part). Archiving to a different file
4543 is useful to keep archived entries from contributing to the
4546 The archived entries will be filed as subtrees of the specified
4547 headline. When the headline is omitted, the subtrees are simply
4548 filed away at the end of the file, as top-level entries. Also in
4549 the heading you can use %s to represent the file name, this can be
4550 useful when using the same archive for a number of different files.
4552 Here are a few examples:
4554 If the current file is Projects.org, archive in file
4555 Projects.org_archive, as top-level trees. This is the default.
4557 \"::* Archived Tasks\"
4558 Archive in the current file, under the top-level headline
4559 \"* Archived Tasks\".
4561 \"~/org/archive.org::\"
4562 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4564 \"~/org/archive.org::* From %s\"
4565 Archive in file ~/org/archive.org (absolute path), under headlines
4566 \"From FILENAME\" where file name is the current file name.
4568 \"~/org/datetree.org::datetree/* Finished Tasks\"
4569 The \"datetree/\" string is special, signifying to archive
4570 items to the datetree. Items are placed in either the CLOSED
4571 date of the item, or the current date if there is no CLOSED date.
4572 The heading will be a subentry to the current date. There doesn't
4573 need to be a heading, but there always needs to be a slash after
4574 datetree. For example, to store archived items directly in the
4575 datetree, use \"~/org/datetree.org::datetree/\".
4577 \"basement::** Finished Tasks\"
4578 Archive in file ./basement (relative path), as level 3 trees
4579 below the level 2 heading \"** Finished Tasks\".
4581 You may set this option on a per-file basis by adding to the buffer a
4584 #+ARCHIVE: basement::** Finished Tasks
4586 You may also define it locally for a subtree by setting an ARCHIVE property
4587 in the entry. If such a property is found in an entry, or anywhere up
4588 the hierarchy, it will be used."
4592 (defcustom org-agenda-skip-archived-trees t
4593 "Non-nil means the agenda will skip any items located in archived trees.
4594 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4595 variable is no longer recommended, you should leave it at the value t.
4596 Instead, use the key `v' to cycle the archives-mode in the agenda."
4598 :group
'org-agenda-skip
4601 (defcustom org-columns-skip-archived-trees t
4602 "Non-nil means ignore archived trees when creating column view."
4604 :group
'org-properties
4607 (defcustom org-cycle-open-archived-trees nil
4608 "Non-nil means `org-cycle' will open archived trees.
4609 An archived tree is a tree marked with the tag ARCHIVE.
4610 When nil, archived trees will stay folded. You can still open them with
4611 normal outline commands like `show-all', but not with the cycling commands."
4616 (defcustom org-sparse-tree-open-archived-trees nil
4617 "Non-nil means sparse tree construction shows matches in archived trees.
4618 When nil, matches in these trees are highlighted, but the trees are kept in
4621 :group
'org-sparse-trees
4624 (defcustom org-sparse-tree-default-date-type
'scheduled-or-deadline
4625 "The default date type when building a sparse tree.
4626 When this is nil, a date is a scheduled or a deadline timestamp.
4627 Otherwise, these types are allowed:
4630 active: only active timestamps (<...>)
4631 inactive: only inactive timestamps (<...)
4632 scheduled: only scheduled timestamps
4633 deadline: only deadline timestamps"
4634 :type
'(choice (const :tag
"Scheduled or deadline" scheduled-or-deadline
)
4635 (const :tag
"All timestamps" all
)
4636 (const :tag
"Only active timestamps" active
)
4637 (const :tag
"Only inactive timestamps" inactive
)
4638 (const :tag
"Only scheduled timestamps" scheduled
)
4639 (const :tag
"Only deadline timestamps" deadline
)
4640 (const :tag
"Only closed timestamps" closed
))
4642 :group
'org-sparse-trees
)
4644 (defun org-cycle-hide-archived-subtrees (state)
4645 "Re-hide all archived subtrees after a visibility state change."
4646 (when (and (not org-cycle-open-archived-trees
)
4647 (not (memq state
'(overview folded
))))
4649 (let* ((globalp (memq state
'(contents all
)))
4650 (beg (if globalp
(point-min) (point)))
4651 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4652 (org-hide-archived-subtrees beg end
)
4654 (if (looking-at (concat ".*:" org-archive-tag
":"))
4655 (message "%s" (substitute-command-keys
4656 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4658 (defun org-force-cycle-archived ()
4659 "Cycle subtree even if it is archived."
4661 (setq this-command
'org-cycle
)
4662 (let ((org-cycle-open-archived-trees t
))
4663 (call-interactively 'org-cycle
)))
4665 (defun org-hide-archived-subtrees (beg end
)
4666 "Re-hide all archived subtrees after a visibility state change."
4668 (let* ((re (concat ":" org-archive-tag
":")))
4670 (while (re-search-forward re end t
)
4671 (when (org-at-heading-p)
4672 (org-flag-subtree t
)
4673 (org-end-of-subtree t
))))))
4675 (declare-function outline-end-of-heading
"outline" ())
4676 (declare-function outline-flag-region
"outline" (from to flag
))
4677 (defun org-flag-subtree (flag)
4679 (org-back-to-heading t
)
4680 (outline-end-of-heading)
4681 (outline-flag-region (point)
4682 (progn (org-end-of-subtree t
) (point))
4685 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4687 ;; Declare Column View Code
4689 (declare-function org-columns-number-to-string
"org-colview" (n fmt
&optional printf
))
4690 (declare-function org-columns-get-format-and-top-level
"org-colview" ())
4691 (declare-function org-columns-compute
"org-colview" (property))
4695 (declare-function org-id-store-link
"org-id")
4696 (declare-function org-id-locations-load
"org-id")
4697 (declare-function org-id-locations-save
"org-id")
4698 (defvar org-id-track-globally
)
4700 ;;; Variables for pre-computed regular expressions, all buffer local
4702 (defvar org-todo-regexp nil
4703 "Matches any of the TODO state keywords.")
4704 (make-variable-buffer-local 'org-todo-regexp
)
4705 (defvar org-not-done-regexp nil
4706 "Matches any of the TODO state keywords except the last one.")
4707 (make-variable-buffer-local 'org-not-done-regexp
)
4708 (defvar org-not-done-heading-regexp nil
4709 "Matches a TODO headline that is not done.")
4710 (make-variable-buffer-local 'org-not-done-regexp
)
4711 (defvar org-todo-line-regexp nil
4712 "Matches a headline and puts TODO state into group 2 if present.")
4713 (make-variable-buffer-local 'org-todo-line-regexp
)
4714 (defvar org-complex-heading-regexp nil
4715 "Matches a headline and puts everything into groups:
4717 group 2: The todo keyword, maybe
4718 group 3: Priority cookie
4719 group 4: True headline
4721 (make-variable-buffer-local 'org-complex-heading-regexp
)
4722 (defvar org-complex-heading-regexp-format nil
4723 "Printf format to make regexp to match an exact headline.
4724 This regexp will match the headline of any node which has the
4725 exact headline text that is put into the format, but may have any
4726 TODO state, priority and tags.")
4727 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4728 (defvar org-todo-line-tags-regexp nil
4729 "Matches a headline and puts TODO state into group 2 if present.
4730 Also put tags into group 4 if tags are present.")
4731 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4732 (defvar org-ds-keyword-length
12
4733 "Maximum length of the DEADLINE and SCHEDULED keywords.")
4734 (make-variable-buffer-local 'org-ds-keyword-length
)
4735 (defvar org-deadline-regexp nil
4736 "Matches the DEADLINE keyword.")
4737 (make-variable-buffer-local 'org-deadline-regexp
)
4738 (defvar org-deadline-time-regexp nil
4739 "Matches the DEADLINE keyword together with a time stamp.")
4740 (make-variable-buffer-local 'org-deadline-time-regexp
)
4741 (defvar org-deadline-time-hour-regexp nil
4742 "Matches the DEADLINE keyword together with a time-and-hour stamp.")
4743 (make-variable-buffer-local 'org-deadline-time-hour-regexp
)
4744 (defvar org-deadline-line-regexp nil
4745 "Matches the DEADLINE keyword and the rest of the line.")
4746 (make-variable-buffer-local 'org-deadline-line-regexp
)
4747 (defvar org-scheduled-regexp nil
4748 "Matches the SCHEDULED keyword.")
4749 (make-variable-buffer-local 'org-scheduled-regexp
)
4750 (defvar org-scheduled-time-regexp nil
4751 "Matches the SCHEDULED keyword together with a time stamp.")
4752 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4753 (defvar org-scheduled-time-hour-regexp nil
4754 "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
4755 (make-variable-buffer-local 'org-scheduled-time-hour-regexp
)
4756 (defvar org-closed-time-regexp nil
4757 "Matches the CLOSED keyword together with a time stamp.")
4758 (make-variable-buffer-local 'org-closed-time-regexp
)
4760 (defvar org-keyword-time-regexp nil
4761 "Matches any of the 4 keywords, together with the time stamp.")
4762 (make-variable-buffer-local 'org-keyword-time-regexp
)
4763 (defvar org-keyword-time-not-clock-regexp nil
4764 "Matches any of the 3 keywords, together with the time stamp.")
4765 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4766 (defvar org-maybe-keyword-time-regexp nil
4767 "Matches a timestamp, possibly preceded by a keyword.")
4768 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4769 (defvar org-all-time-keywords nil
4770 "List of time keywords.")
4771 (make-variable-buffer-local 'org-all-time-keywords
)
4773 (defconst org-plain-time-of-day-regexp
4776 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4779 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4781 "Regular expression to match a plain time or time range.
4782 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4783 groups carry important information:
4785 1 the first time, range or not
4786 8 the second time, if it is a range.")
4788 (defconst org-plain-time-extension-regexp
4791 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4792 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4793 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4794 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4795 groups carry important information:
4798 9 minutes of duration")
4800 (defconst org-stamp-time-of-day-regexp
4802 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4803 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4805 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4806 "Regular expression to match a timestamp time or time range.
4807 After a match, the following groups carry important information:
4809 1 date plus weekday, for back referencing to make sure both times are on the same day
4810 2 the first time, range or not
4811 4 the second time, if it is a range.")
4813 (defconst org-startup-options
4814 '(("fold" org-startup-folded t
)
4815 ("overview" org-startup-folded t
)
4816 ("nofold" org-startup-folded nil
)
4817 ("showall" org-startup-folded nil
)
4818 ("showeverything" org-startup-folded showeverything
)
4819 ("content" org-startup-folded content
)
4820 ("indent" org-startup-indented t
)
4821 ("noindent" org-startup-indented nil
)
4822 ("hidestars" org-hide-leading-stars t
)
4823 ("showstars" org-hide-leading-stars nil
)
4824 ("odd" org-odd-levels-only t
)
4825 ("oddeven" org-odd-levels-only nil
)
4826 ("align" org-startup-align-all-tables t
)
4827 ("noalign" org-startup-align-all-tables nil
)
4828 ("inlineimages" org-startup-with-inline-images t
)
4829 ("noinlineimages" org-startup-with-inline-images nil
)
4830 ("latexpreview" org-startup-with-latex-preview t
)
4831 ("nolatexpreview" org-startup-with-latex-preview nil
)
4832 ("customtime" org-display-custom-times t
)
4833 ("logdone" org-log-done time
)
4834 ("lognotedone" org-log-done note
)
4835 ("nologdone" org-log-done nil
)
4836 ("lognoteclock-out" org-log-note-clock-out t
)
4837 ("nolognoteclock-out" org-log-note-clock-out nil
)
4838 ("logrepeat" org-log-repeat state
)
4839 ("lognoterepeat" org-log-repeat note
)
4840 ("logdrawer" org-log-into-drawer t
)
4841 ("nologdrawer" org-log-into-drawer nil
)
4842 ("logstatesreversed" org-log-states-order-reversed t
)
4843 ("nologstatesreversed" org-log-states-order-reversed nil
)
4844 ("nologrepeat" org-log-repeat nil
)
4845 ("logreschedule" org-log-reschedule time
)
4846 ("lognotereschedule" org-log-reschedule note
)
4847 ("nologreschedule" org-log-reschedule nil
)
4848 ("logredeadline" org-log-redeadline time
)
4849 ("lognoteredeadline" org-log-redeadline note
)
4850 ("nologredeadline" org-log-redeadline nil
)
4851 ("logrefile" org-log-refile time
)
4852 ("lognoterefile" org-log-refile note
)
4853 ("nologrefile" org-log-refile nil
)
4854 ("fninline" org-footnote-define-inline t
)
4855 ("nofninline" org-footnote-define-inline nil
)
4856 ("fnlocal" org-footnote-section nil
)
4857 ("fnauto" org-footnote-auto-label t
)
4858 ("fnprompt" org-footnote-auto-label nil
)
4859 ("fnconfirm" org-footnote-auto-label confirm
)
4860 ("fnplain" org-footnote-auto-label plain
)
4861 ("fnadjust" org-footnote-auto-adjust t
)
4862 ("nofnadjust" org-footnote-auto-adjust nil
)
4863 ("constcgs" constants-unit-system cgs
)
4864 ("constSI" constants-unit-system SI
)
4865 ("noptag" org-tag-persistent-alist nil
)
4866 ("hideblocks" org-hide-block-startup t
)
4867 ("nohideblocks" org-hide-block-startup nil
)
4868 ("beamer" org-startup-with-beamer-mode t
)
4869 ("entitiespretty" org-pretty-entities t
)
4870 ("entitiesplain" org-pretty-entities nil
))
4871 "Variable associated with STARTUP options for org-mode.
4872 Each element is a list of three items: the startup options (as written
4873 in the #+STARTUP line), the corresponding variable, and the value to set
4874 this variable to if the option is found. An optional forth element PUSH
4875 means to push this value onto the list in the variable.")
4877 (defun org-update-property-plist (key val props
)
4878 "Update PROPS with KEY and VAL."
4879 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4880 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4881 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4882 (previous (cdr (assoc key props
))))
4884 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4885 (cons (cons key val
) remainder
))))
4887 (defcustom org-group-tags t
4888 "When non-nil (the default), use group tags.
4889 This can be turned on/off through `org-toggle-tags-groups'."
4894 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4896 (defun org-toggle-tags-groups ()
4897 "Toggle support for group tags.
4898 Support for group tags is controlled by the option
4899 `org-group-tags', which is non-nil by default."
4901 (setq org-group-tags
(not org-group-tags
))
4902 (cond ((and (derived-mode-p 'org-agenda-mode
)
4905 ((derived-mode-p 'org-mode
)
4906 (let ((org-inhibit-startup t
)) (org-mode))))
4907 (message "Groups tags support has been turned %s"
4908 (if org-group-tags
"on" "off")))
4910 (defun org-set-regexps-and-options-for-tags ()
4911 "Precompute variables used for tags."
4912 (when (derived-mode-p 'org-mode
)
4913 (org-set-local 'org-file-tags nil
)
4914 (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
4917 tags ftags key value
)
4921 (goto-char (point-min))
4922 (while (re-search-forward re nil t
)
4923 (setq key
(upcase (org-match-string-no-properties 1))
4924 value
(org-match-string-no-properties 2))
4925 (if (stringp value
) (setq value
(org-trim value
)))
4928 (setq tags
(append tags
(if tags
'("\\n") nil
)
4929 (org-split-string value splitre
))))
4930 ((equal key
"FILETAGS")
4931 (when (string-match "\\S-" value
)
4936 (mapcar (lambda (x) (org-split-string x
":"))
4937 (org-split-string value
)))))))))))
4938 ;; Process the file tags.
4939 (and ftags
(org-set-local 'org-file-tags
4940 (mapcar 'org-add-prop-inherited ftags
)))
4941 (org-set-local 'org-tag-groups-alist nil
)
4942 ;; Process the tags.
4943 (when (and (not tags
) org-tag-alist
)
4946 (lambda (tg) (cond ((eq (car tg
) :startgroup
) "{")
4947 ((eq (car tg
) :endgroup
) "}")
4948 ((eq (car tg
) :grouptags
) ":")
4949 ((eq (car tg
) :newline
) "\n")
4951 (if (characterp (cdr tg
))
4952 (format "(%s)" (char-to-string (cdr tg
))) "")))))
4955 (while (setq e
(pop tags
))
4958 (progn (push '(:startgroup
) tgs
)
4959 (when (equal (nth 1 tags
) ":")
4960 (push (list (replace-regexp-in-string
4961 "(.+)$" "" (nth 0 tags
)))
4962 org-tag-groups-alist
)
4964 ((equal e
":") (push '(:grouptags
) tgs
))
4965 ((equal e
"}") (push '(:endgroup
) tgs
) (if g
(setq g nil
)))
4966 ((equal e
"\\n") (push '(:newline
) tgs
))
4967 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4968 (push (cons (match-string 1 e
)
4969 (string-to-char (match-string 2 e
))) tgs
)
4971 (setcar org-tag-groups-alist
4972 (append (car org-tag-groups-alist
)
4973 (list (match-string 1 e
)))))
4974 (if g
(setq g
(1+ g
))))
4975 (t (push (list e
) tgs
)
4977 (setcar org-tag-groups-alist
4978 (append (car org-tag-groups-alist
) (list e
))))
4979 (if g
(setq g
(1+ g
))))))
4980 (org-set-local 'org-tag-alist nil
)
4981 (while (setq e
(pop tgs
))
4982 (or (and (stringp (car e
))
4983 (assoc (car e
) org-tag-alist
))
4984 (push e org-tag-alist
)))
4985 ;; Return a list with tag variables
4986 (list org-file-tags org-tag-alist org-tag-groups-alist
)))))
4988 (defvar org-ota nil
)
4989 (defun org-set-regexps-and-options ()
4990 "Precompute regular expressions used in the current buffer."
4991 (when (derived-mode-p 'org-mode
)
4992 (org-set-local 'org-todo-kwd-alist nil
)
4993 (org-set-local 'org-todo-key-alist nil
)
4994 (org-set-local 'org-todo-key-trigger nil
)
4995 (org-set-local 'org-todo-keywords-1 nil
)
4996 (org-set-local 'org-done-keywords nil
)
4997 (org-set-local 'org-todo-heads nil
)
4998 (org-set-local 'org-todo-sets nil
)
4999 (org-set-local 'org-todo-log-states nil
)
5000 (org-set-local 'org-file-properties nil
)
5001 (let ((re (org-make-options-regexp
5002 '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
5003 "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
5004 "SETUPFILE" "OPTIONS")
5005 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
5007 (scripts org-use-sub-superscripts
)
5008 kwds kws0 kwsa key log value cat arch const links hw dws
5009 tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
5014 (goto-char (point-min))
5021 (insert ext-setup-or-nil
)
5022 (let ((major-mode 'org-mode
) org-ota
)
5023 (setq ret
(save-match-data
5024 (org-set-regexps-and-options-for-tags)))))
5025 ;; Append setupfile tags to existing tags
5028 (delq nil
(append org-file-tags
(nth 0 ret
)))
5030 (delq nil
(append org-tag-alist
(nth 1 ret
)))
5031 org-tag-groups-alist
5032 (delq nil
(append org-tag-groups-alist
(nth 2 ret
))))))
5033 (and ext-setup-or-nil
5034 (string-match re ext-setup-or-nil start
)
5035 (setq start
(match-end 0)))
5036 (and (setq ext-setup-or-nil nil start
0)
5037 (re-search-forward re nil t
)))
5038 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
5039 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
5040 (if (stringp value
) (setq value
(org-trim value
)))
5042 ((equal key
"CATEGORY")
5044 ((member key
'("SEQ_TODO" "TODO"))
5045 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
5046 ((equal key
"TYP_TODO")
5047 (push (cons 'type
(org-split-string value splitre
)) kwds
))
5048 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
5049 ;; general TODO-like setup
5050 (push (cons (intern (downcase (match-string 1 key
)))
5051 (org-split-string value splitre
)) kwds
))
5052 ((equal key
"COLUMNS")
5053 (org-set-local 'org-columns-default-format value
))
5055 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
5056 (push (cons (match-string 1 value
)
5057 (org-trim (match-string 2 value
)))
5059 ((equal key
"PRIORITIES")
5060 (setq prio
(org-split-string value
" +")))
5061 ((equal key
"PROPERTY")
5062 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
5063 (setq props
(org-update-property-plist (match-string 1 value
)
5064 (match-string 2 value
)
5066 ((equal key
"CONSTANTS")
5067 (org-table-set-constants))
5068 ((equal key
"STARTUP")
5069 (let ((opts (org-split-string value splitre
))
5071 (while (setq l
(pop opts
))
5072 (when (setq l
(assoc l org-startup-options
))
5073 (setq var
(nth 1 l
) val
(nth 2 l
))
5075 (set (make-local-variable var
) val
)
5076 (if (not (listp (symbol-value var
)))
5077 (set (make-local-variable var
) nil
))
5078 (set (make-local-variable var
) (symbol-value var
))
5079 (add-to-list var val
))))))
5080 ((equal key
"ARCHIVE")
5082 (remove-text-properties 0 (length arch
)
5083 '(face t fontified t
) arch
))
5084 ((equal key
"OPTIONS")
5085 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
5086 (setq scripts
(read (match-string 2 value
)))))
5087 ((and (equal key
"SETUPFILE")
5088 ;; Prevent checking in Gnus messages
5089 (not buffer-read-only
))
5090 (setq setup-contents
(org-file-contents
5092 (org-remove-double-quotes value
))
5094 (if (not ext-setup-or-nil
)
5095 (setq ext-setup-or-nil setup-contents start
0)
5096 (setq ext-setup-or-nil
5097 (concat (substring ext-setup-or-nil
0 start
)
5098 "\n" setup-contents
"\n"
5099 (substring ext-setup-or-nil start
)))))))
5100 ;; search for property blocks
5101 (goto-char (point-min))
5102 (while (re-search-forward org-block-regexp nil t
)
5103 (when (equal "PROPERTY" (upcase (match-string 1)))
5104 (setq value
(replace-regexp-in-string
5105 "[\n\r]" " " (match-string 4)))
5106 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
5107 (setq props
(org-update-property-plist (match-string 1 value
)
5108 (match-string 2 value
)
5110 (org-set-local 'org-use-sub-superscripts scripts
)
5112 (org-set-local 'org-category
(intern cat
))
5113 (push (cons "CATEGORY" cat
) props
))
5115 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
5116 (setq prio
(mapcar 'string-to-char prio
))
5117 (org-set-local 'org-highest-priority
(nth 0 prio
))
5118 (org-set-local 'org-lowest-priority
(nth 1 prio
))
5119 (org-set-local 'org-default-priority
(nth 2 prio
)))
5120 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
5121 (and arch
(org-set-local 'org-archive-location arch
))
5122 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
5123 ;; Process the TODO keywords
5125 ;; Use the global values as if they had been given locally.
5126 (setq kwds
(default-value 'org-todo-keywords
))
5127 (if (stringp (car kwds
))
5128 (setq kwds
(list (cons org-todo-interpretation
5129 (default-value 'org-todo-keywords
)))))
5130 (setq kwds
(reverse kwds
)))
5131 (setq kwds
(nreverse kwds
))
5133 (while (setq kws
(pop kwds
))
5135 (run-hook-with-args-until-success
5136 'org-todo-setup-filter-hook kws
)
5138 (setq inter
(pop kws
) sep
(member "|" kws
)
5139 kws0
(delete "|" (copy-sequence kws
))
5144 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
5146 (setq kw
(match-string 1 x
)
5147 key
(and (match-end 2) (match-string 2 x
))
5148 log
(org-extract-log-state-settings x
))
5149 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
5150 (and log
(push log org-todo-log-states
))
5152 (error "Invalid TODO keyword %s" x
)))
5154 kwsa
(if kwsa
(append '((:startgroup
))
5158 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
5159 tail
(list inter hw
(car dws
) (org-last dws
))))
5160 (add-to-list 'org-todo-heads hw
'append
)
5161 (push kws1 org-todo-sets
)
5162 (setq org-done-keywords
(append org-done-keywords dws nil
))
5163 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
5164 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
5165 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
5166 (setq org-todo-sets
(nreverse org-todo-sets
)
5167 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
5168 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
5169 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
5170 ;; Compute the regular expressions and other local variables.
5171 ;; Using `org-outline-regexp-bol' would complicate them much,
5172 ;; because of the fixed white space at the end of that string.
5173 (if (not org-done-keywords
)
5174 (setq org-done-keywords
(and org-todo-keywords-1
5175 (list (org-last org-todo-keywords-1
)))))
5176 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
5177 (length org-scheduled-string
)
5178 (length org-clock-string
)
5179 (length org-closed-string
)))
5180 org-not-done-keywords
5181 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
5184 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
5188 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
5190 org-not-done-heading-regexp
5191 (format org-heading-keyword-regexp-format org-not-done-regexp
)
5192 org-todo-line-regexp
5193 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
5194 org-complex-heading-regexp
5195 (concat "^\\(\\*+\\)"
5196 "\\(?: +" org-todo-regexp
"\\)?"
5197 "\\(?: +\\(\\[#.\\]\\)\\)?"
5198 "\\(?: +\\(.*?\\)\\)??"
5199 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
5201 org-complex-heading-regexp-format
5202 (concat "^\\(\\*+\\)"
5203 "\\(?: +" org-todo-regexp
"\\)?"
5204 "\\(?: +\\(\\[#.\\]\\)\\)?"
5206 ;; Stats cookies can be stuck to body.
5207 "\\(?:\\[[0-9%%/]+\\] *\\)*"
5209 "\\(?: *\\[[0-9%%/]+\\]\\)*"
5211 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
5213 org-todo-line-tags-regexp
5214 (concat "^\\(\\*+\\)"
5215 "\\(?: +" org-todo-regexp
"\\)?"
5216 "\\(?: +\\(.*?\\)\\)??"
5217 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
5219 org-deadline-regexp
(concat "\\<" org-deadline-string
)
5220 org-deadline-time-regexp
5221 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
5222 org-deadline-time-hour-regexp
5223 (concat "\\<" org-deadline-string
5224 " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
5225 org-deadline-line-regexp
5226 (concat "\\<\\(" org-deadline-string
"\\).*")
5227 org-scheduled-regexp
5228 (concat "\\<" org-scheduled-string
)
5229 org-scheduled-time-regexp
5230 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
5231 org-scheduled-time-hour-regexp
5232 (concat "\\<" org-scheduled-string
5233 " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
5234 org-closed-time-regexp
5235 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
5236 org-keyword-time-regexp
5237 (concat "\\<\\(" org-scheduled-string
5238 "\\|" org-deadline-string
5239 "\\|" org-closed-string
5240 "\\|" org-clock-string
"\\)"
5241 " *[[<]\\([^]>]+\\)[]>]")
5242 org-keyword-time-not-clock-regexp
5243 (concat "\\<\\(" org-scheduled-string
5244 "\\|" org-deadline-string
5245 "\\|" org-closed-string
5247 " *[[<]\\([^]>]+\\)[]>]")
5248 org-maybe-keyword-time-regexp
5249 (concat "\\(\\<\\(" org-scheduled-string
5250 "\\|" org-deadline-string
5251 "\\|" org-closed-string
5252 "\\|" org-clock-string
"\\)\\)?"
5253 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
5254 org-all-time-keywords
5255 (mapcar (lambda (w) (substring w
0 -
1))
5256 (list org-scheduled-string org-deadline-string
5257 org-clock-string org-closed-string
)))
5259 (org-compute-latex-and-related-regexp))))
5261 (defun org-file-contents (file &optional noerror
)
5262 "Return the contents of FILE, as a string."
5263 (if (and file
(file-readable-p file
))
5265 (insert-file-contents file
)
5267 (funcall (if noerror
'message
'error
)
5268 "Cannot read file \"%s\"%s"
5270 (let ((from (buffer-file-name (buffer-base-buffer))))
5271 (if from
(concat " (referenced in file \"" from
"\")") "")))))
5273 (defun org-extract-log-state-settings (x)
5274 "Extract the log state setting from a TODO keyword string.
5275 This will extract info from a string like \"WAIT(w@/!)\"."
5276 (let (kw key log1 log2
)
5277 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
5278 (setq kw
(match-string 1 x
)
5279 key
(and (match-end 2) (match-string 2 x
))
5280 log1
(and (match-end 3) (match-string 3 x
))
5281 log2
(and (match-end 4) (match-string 4 x
)))
5284 (and log1
(if (equal log1
"!") 'time
'note
))
5285 (and log2
(if (equal log2
"!") 'time
'note
)))))))
5287 (defun org-remove-keyword-keys (list)
5288 "Remove a pair of parenthesis at the end of each string in LIST."
5290 (if (string-match "(.*)$" x
)
5291 (substring x
0 (match-beginning 0))
5295 (defun org-assign-fast-keys (alist)
5296 "Assign fast keys to a keyword-key alist.
5297 Respect keys that are already there."
5298 (let (new e
(alt ?
0))
5299 (while (setq e
(pop alist
))
5300 (if (or (memq (car e
) '(:newline
:grouptags
:endgroup
:startgroup
))
5301 (cdr e
)) ;; Key already assigned.
5303 (let ((clist (string-to-list (downcase (car e
))))
5304 (used (append new alist
)))
5305 (when (= (car clist
) ?
@)
5307 (while (and clist
(rassoc (car clist
) used
))
5310 (while (rassoc alt used
)
5312 (push (cons (car e
) (or (car clist
) alt
)) new
))))
5315 ;;; Some variables used in various places
5317 (defvar org-window-configuration nil
5318 "Used in various places to store a window configuration.")
5319 (defvar org-selected-window nil
5320 "Used in various places to store a window configuration.")
5321 (defvar org-finish-function nil
5322 "Function to be called when `C-c C-c' is used.
5323 This is for getting out of special buffers like capture.")
5326 ;; FIXME: Occasionally check by commenting these, to make sure
5327 ;; no other functions uses these, forgetting to let-bind them.
5328 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
5329 (defvar org-last-state
)
5330 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
5332 ;; Defined somewhere in this file, but used before definition.
5333 (defvar org-entities
) ;; defined in org-entities.el
5334 (defvar org-struct-menu
)
5335 (defvar org-org-menu
)
5336 (defvar org-tbl-menu
)
5338 ;;;; Define the Org-mode
5340 ;; We use a before-change function to check if a table might need
5342 (defvar org-table-may-need-update t
5343 "Indicates that a table might need an update.
5344 This variable is set by `org-before-change-function'.
5345 `org-table-align' sets it back to nil.")
5346 (defun org-before-change-function (beg end
)
5347 "Every change indicates that a table might need an update."
5348 (setq org-table-may-need-update t
))
5349 (defvar org-mode-map
)
5350 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
5351 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
5352 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
5353 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
5354 (defvar org-table-buffer-is-an nil
)
5356 (defvar bidi-paragraph-direction
)
5357 (defvar buffer-face-mode-face
)
5360 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
5361 (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"))
5362 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
5364 ;; Other stuff we need.
5365 (require 'time-date
)
5366 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
5370 ;; (require 'org-macs) moved higher up in the file before it is first used
5371 (require 'org-entities
)
5372 ;; (require 'org-compat) moved higher up in the file before it is first used
5373 (require 'org-faces
)
5375 (require 'org-pcomplete
)
5377 (require 'org-footnote
)
5378 (require 'org-macro
)
5384 (define-derived-mode org-mode outline-mode
"Org"
5385 "Outline-based notes management and organizer, alias
5386 \"Carsten's outline-mode for keeping track of everything.\"
5388 Org-mode develops organizational tasks around a NOTES file which
5389 contains information about projects as plain text. Org-mode is
5390 implemented on top of outline-mode, which is ideal to keep the content
5391 of large files well structured. It supports ToDo items, deadlines and
5392 time stamps, which magically appear in the diary listing of the Emacs
5393 calendar. Tables are easily created with a built-in table editor.
5394 Plain text URL-like links connect to websites, emails (VM), Usenet
5395 messages (Gnus), BBDB entries, and any files related to the project.
5396 For printing and sharing of notes, an Org-mode file (or a part of it)
5397 can be exported as a structured ASCII or HTML file.
5399 The following commands are available:
5403 ;; Get rid of Outline menus, they are not needed
5404 ;; Need to do this here because define-derived-mode sets up
5405 ;; the keymap so late. Still, it is a waste to call this each time
5406 ;; we switch another buffer into org-mode.
5407 (if (featurep 'xemacs
)
5408 (when (boundp 'outline-mode-menu-heading
)
5409 ;; Assume this is Greg's port, it uses easymenu
5410 (easy-menu-remove outline-mode-menu-heading
)
5411 (easy-menu-remove outline-mode-menu-show
)
5412 (easy-menu-remove outline-mode-menu-hide
))
5413 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
5414 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
5415 (define-key org-mode-map
[menu-bar show
] 'undefined
))
5417 (org-load-modules-maybe)
5418 (easy-menu-add org-org-menu
)
5419 (easy-menu-add org-tbl-menu
)
5420 (org-install-agenda-files-menu)
5421 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
5422 (add-to-invisibility-spec '(org-cwidth))
5423 (add-to-invisibility-spec '(org-hide-block . t
))
5424 (when (featurep 'xemacs
)
5425 (org-set-local 'line-move-ignore-invisible t
))
5426 (org-set-local 'outline-regexp org-outline-regexp
)
5427 (org-set-local 'outline-level
'org-outline-level
)
5428 (setq bidi-paragraph-direction
'left-to-right
)
5429 (when (and org-ellipsis
5430 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
5431 (fboundp 'make-glyph-code
))
5432 (unless org-display-table
5433 (setq org-display-table
(make-display-table)))
5434 (set-display-table-slot
5437 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
5439 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
5440 (setq buffer-display-table org-display-table
))
5441 (org-set-regexps-and-options-for-tags)
5442 (org-set-regexps-and-options)
5443 (org-set-font-lock-defaults)
5444 (when (and org-tag-faces
(not org-tags-special-faces-re
))
5445 ;; tag faces set outside customize.... force initialization.
5446 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
5448 (org-set-local 'calc-embedded-open-mode
"# ")
5449 ;; Modify a few syntax entries
5450 (modify-syntax-entry ?
@ "w")
5451 (modify-syntax-entry ?
\" "\"")
5452 (modify-syntax-entry ?
\\ "_")
5453 (modify-syntax-entry ?~
"_")
5454 (if org-startup-truncated
(setq truncate-lines t
))
5455 (when org-startup-indented
(require 'org-indent
) (org-indent-mode 1))
5456 (org-set-local 'font-lock-unfontify-region-function
5457 'org-unfontify-region
)
5458 ;; Activate before-change-function
5459 (org-set-local 'org-table-may-need-update t
)
5460 (org-add-hook 'before-change-functions
'org-before-change-function nil
5462 ;; Check for running clock before killing a buffer
5463 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5464 ;; Initialize macros templates.
5465 (org-macro-initialize-templates)
5466 ;; Initialize radio targets.
5467 (org-update-radio-target-regexp)
5469 (org-set-local 'indent-line-function
'org-indent-line
)
5470 (org-set-local 'indent-region-function
'org-indent-region
)
5471 ;; Filling and auto-filling.
5474 (org-setup-comments-handling)
5475 ;; Initialize cache.
5476 (org-element-cache-reset)
5477 ;; Beginning/end of defun
5478 (org-set-local 'beginning-of-defun-function
'org-backward-element
)
5479 (org-set-local 'end-of-defun-function
5481 (if (not (org-at-heading-p))
5482 (org-forward-element)
5483 (org-forward-element)
5484 (forward-char -
1))))
5485 ;; Next error for sparse trees
5486 (org-set-local 'next-error-function
'org-occur-next-match
)
5487 ;; Make sure dependence stuff works reliably, even for users who set it
5489 (if org-enforce-todo-dependencies
5490 (add-hook 'org-blocker-hook
5491 'org-block-todo-from-children-or-siblings-or-parent
)
5492 (remove-hook 'org-blocker-hook
5493 'org-block-todo-from-children-or-siblings-or-parent
))
5494 (if org-enforce-todo-checkbox-dependencies
5495 (add-hook 'org-blocker-hook
5496 'org-block-todo-from-checkboxes
)
5497 (remove-hook 'org-blocker-hook
5498 'org-block-todo-from-checkboxes
))
5500 ;; Align options lines
5502 'align-mode-rules-list
5503 '((org-in-buffer-settings
5504 (regexp .
"^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5505 (modes .
'(org-mode)))))
5508 (org-set-local 'imenu-create-index-function
5509 'org-imenu-get-tree
)
5511 ;; Make isearch reveal context
5512 (if (or (featurep 'xemacs
)
5513 (not (boundp 'outline-isearch-open-invisible-function
)))
5514 ;; Emacs 21 and XEmacs make use of the hook
5515 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5516 ;; Emacs 22 deals with this through a special variable
5517 (org-set-local 'outline-isearch-open-invisible-function
5518 (lambda (&rest ignore
) (org-show-context 'isearch
))))
5520 ;; Setup the pcomplete hooks
5521 (set (make-local-variable 'pcomplete-command-completion-function
)
5522 'org-pcomplete-initial
)
5523 (set (make-local-variable 'pcomplete-command-name-function
)
5524 'org-command-at-point
)
5525 (set (make-local-variable 'pcomplete-default-completion-function
)
5527 (set (make-local-variable 'pcomplete-parse-arguments-function
)
5528 'org-parse-arguments
)
5529 (set (make-local-variable 'pcomplete-termination-string
) "")
5530 (when (>= emacs-major-version
23)
5531 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
5533 ;; If empty file that did not turn on org-mode automatically, make it to.
5534 (if (and org-insert-mode-line-in-empty-file
5535 (org-called-interactively-p 'any
)
5536 (= (point-min) (point-max)))
5537 (insert "# -*- mode: org -*-\n\n"))
5538 (unless org-inhibit-startup
5540 (and org-startup-with-beamer-mode
(org-beamer-mode))
5541 (when org-startup-align-all-tables
5542 (org-table-map-tables 'org-table-align
'quietly
))
5543 (when org-startup-with-inline-images
5544 (org-display-inline-images))
5545 (when org-startup-with-latex-preview
5546 (org-toggle-latex-fragment))
5547 (unless org-inhibit-startup-visibility-stuff
5548 (org-set-startup-visibility))
5549 (org-refresh-effort-properties)))
5550 ;; Try to set org-hide correctly
5551 (let ((foreground (org-find-invisible-foreground)))
5553 (set-face-foreground 'org-hide foreground
))))
5555 ;; Update `customize-package-emacs-version-alist'
5556 (add-to-list 'customize-package-emacs-version-alist
5557 '(Org ("6.21b" .
"23.1") ("6.33x" .
"23.2")
5558 ("7.8.11" .
"24.1") ("7.9.4" .
"24.3")
5559 ("8.2.6" .
"24.4")))
5561 (defvar org-mode-transpose-word-syntax-table
5562 (let ((st (make-syntax-table)))
5563 (mapc (lambda(c) (modify-syntax-entry
5564 (string-to-char (car c
)) "w p" st
))
5568 (when (fboundp 'abbrev-table-put
)
5569 (abbrev-table-put org-mode-abbrev-table
5570 :parents
(list text-mode-abbrev-table
)))
5572 (defun org-find-invisible-foreground ()
5573 (let ((candidates (remove
5576 (list (face-background 'default
)
5577 (face-background 'org-default
))
5580 (when (boundp alist
)
5581 (cdr (assoc 'background-color
(symbol-value alist
)))))
5582 '(default-frame-alist initial-frame-alist window-system-default-frame-alist
))
5583 (list (face-foreground 'org-hide
))))))
5584 (car (remove nil candidates
))))
5586 (defun org-current-time (&optional rounding-minutes past
)
5587 "Current time, possibly rounded to ROUNDING-MINUTES.
5588 When ROUNDING-MINUTES is not an integer, fall back on the car of
5589 `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
5590 the rounding returns a past time."
5591 (let ((r (or (and (integerp rounding-minutes
) rounding-minutes
)
5592 (car org-time-stamp-rounding-minutes
)))
5593 (time (decode-time)) res
)
5598 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5600 (if (and past
(< (org-float-time (time-subtract (current-time) res
)) 0))
5601 (seconds-to-time (- (org-float-time res
) (* r
60)))
5605 "Return today date, considering `org-extend-today-until'."
5607 (time-subtract (current-time)
5608 (list 0 (* 3600 org-extend-today-until
) 0))))
5610 ;;;; Font-Lock stuff, including the activators
5612 (defvar org-mouse-map
(make-sparse-keymap))
5613 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5614 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5615 (when org-mouse-1-follows-link
5616 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5617 (when org-tab-follows-link
5618 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5619 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5621 (require 'font-lock
)
5623 (defconst org-non-link-chars
"]\t\n\r<>")
5624 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "file+emacs"
5625 "file+sys" "news" "shell" "elisp" "doi" "message"
5627 (defvar org-link-types-re nil
5628 "Matches a link that has a url-like prefix like \"http:\"")
5629 (defvar org-link-re-with-space nil
5630 "Matches a link with spaces, optional angular brackets around it.")
5631 (defvar org-link-re-with-space2 nil
5632 "Matches a link with spaces, optional angular brackets around it.")
5633 (defvar org-link-re-with-space3 nil
5634 "Matches a link with spaces, only for internal part in bracket links.")
5635 (defvar org-angle-link-re nil
5636 "Matches link with angular brackets, spaces are allowed.")
5637 (defvar org-plain-link-re nil
5638 "Matches plain link, without spaces.")
5639 (defvar org-bracket-link-regexp nil
5640 "Matches a link in double brackets.")
5641 (defvar org-bracket-link-analytic-regexp nil
5642 "Regular expression used to analyze links.
5643 Here is what the match groups contain after a match:
5649 (defvar org-bracket-link-analytic-regexp
++ nil
5650 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5651 (defvar org-any-link-re nil
5652 "Regular expression matching any link.")
5654 (defconst org-match-sexp-depth
3
5655 "Number of stacked braces for sub/superscript matching.")
5657 (defun org-create-multibrace-regexp (left right n
)
5658 "Create a regular expression which will match a balanced sexp.
5659 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5660 as single character strings.
5661 The regexp returned will match the entire expression including the
5662 delimiters. It will also define a single group which contains the
5663 match except for the outermost delimiters. The maximum depth of
5664 stacked delimiters is N. Escaping delimiters is not possible."
5665 (let* ((nothing (concat "[^" left right
"]*?"))
5668 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5671 re
(concat re or next
)
5672 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5673 (concat left
"\\(" re
"\\)" right
)))
5675 (defconst org-match-substring-regexp
5677 "\\(\\S-\\)\\([_^]\\)\\("
5678 "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5680 "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5682 "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
5683 "The regular expression matching a sub- or superscript.")
5685 (defconst org-match-substring-with-braces-regexp
5687 "\\(\\S-\\)\\([_^]\\)"
5688 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)")
5689 "The regular expression matching a sub- or superscript, forcing braces.")
5691 (defun org-make-link-regexps ()
5692 "Update the link regular expressions.
5693 This should be called after the variable `org-link-types' has changed."
5694 (let ((types-re (regexp-opt org-link-types t
)))
5695 (setq org-link-types-re
5696 (concat "\\`" types-re
":")
5697 org-link-re-with-space
5698 (concat "<?" types-re
":"
5699 "\\([^" org-non-link-chars
" ]"
5700 "[^" org-non-link-chars
"]*"
5701 "[^" org-non-link-chars
" ]\\)>?")
5702 org-link-re-with-space2
5703 (concat "<?" types-re
":"
5704 "\\([^" org-non-link-chars
" ]"
5706 "[^" org-non-link-chars
" ]\\)>?")
5707 org-link-re-with-space3
5708 (concat "<?" types-re
":"
5709 "\\([^" org-non-link-chars
" ]"
5712 (concat "<" types-re
":"
5713 "\\([^" org-non-link-chars
" ]"
5714 "[^" org-non-link-chars
"]*"
5719 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5720 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5721 org-bracket-link-regexp
5722 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5723 org-bracket-link-analytic-regexp
5726 "\\(" types-re
":\\)?"
5729 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5731 org-bracket-link-analytic-regexp
++
5734 "\\(" (regexp-opt (cons "coderef" org-link-types
) t
) ":\\)?"
5737 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5740 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5741 org-angle-link-re
"\\)\\|\\("
5742 org-plain-link-re
"\\)"))))
5744 (org-make-link-regexps)
5746 (defvar org-emph-face nil
)
5748 (defun org-do-emphasis-faces (limit)
5749 "Run through the buffer and emphasize strings."
5751 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5752 (let* ((border (char-after (match-beginning 3)))
5753 (bre (regexp-quote (char-to-string border
))))
5754 (if (and (not (= border
(char-after (match-beginning 4))))
5755 (not (save-match-data
5756 (string-match (concat bre
".*" bre
)
5757 (replace-regexp-in-string
5759 (substring (match-string 2) 1 -
1))))))
5762 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5763 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5767 (org-remove-flyspell-overlays-in
5768 (match-beginning 0) (match-end 0)))
5769 (add-text-properties (match-beginning 2) (match-end 2)
5770 '(font-lock-multiline t org-emphasis t
))
5771 (when org-hide-emphasis-markers
5772 (add-text-properties (match-end 4) (match-beginning 5)
5773 '(invisible org-link
))
5774 (add-text-properties (match-beginning 3) (match-end 3)
5775 '(invisible org-link
))))))
5776 (goto-char (1+ (match-beginning 0))))
5779 (defun org-emphasize (&optional char
)
5780 "Insert or change an emphasis, i.e. a font like bold or italic.
5781 If there is an active region, change that region to a new emphasis.
5782 If there is no region, just insert the marker characters and position
5783 the cursor between them.
5784 CHAR should be the marker character. If it is a space, it means to
5785 remove the emphasis of the selected region.
5786 If CHAR is not given (for example in an interactive call) it will be
5789 (let ((erc org-emphasis-regexp-components
)
5791 (string "") beg end move c s
)
5792 (if (org-region-active-p)
5793 (setq beg
(region-beginning) end
(region-end)
5794 string
(buffer-substring beg end
))
5798 (message "Emphasis marker or tag: [%s]"
5799 (mapconcat (lambda(e) (car e
)) org-emphasis-alist
""))
5800 (setq char
(read-char-exclusive)))
5801 (if (equal char ?\
)
5802 (setq s
"" move nil
)
5803 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5804 (user-error "No such emphasis marker: \"%c\"" char
))
5805 (setq s
(char-to-string char
)))
5806 (while (and (> (length string
) 1)
5807 (equal (substring string
0 1) (substring string -
1))
5808 (assoc (substring string
0 1) org-emphasis-alist
))
5809 (setq string
(substring string
1 -
1)))
5810 (setq string
(concat s string s
))
5811 (if beg
(delete-region beg end
))
5813 (string-match (concat "[" (nth 0 erc
) "\n]")
5814 (char-to-string (char-before (point)))))
5817 (string-match (concat "[" (nth 1 erc
) "\n]")
5818 (char-to-string (char-after (point)))))
5819 (insert " ") (backward-char 1))
5821 (and move
(backward-char 1))))
5823 (defconst org-nonsticky-props
5824 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link
))
5826 (defsubst org-rear-nonsticky-at
(pos)
5827 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5829 (defun org-activate-plain-links (limit)
5830 "Add link properties for plain links."
5832 (when (and (re-search-forward (concat org-plain-link-re
) limit t
)
5833 (not (member 'org-tag
5834 (get-text-property (1- (match-beginning 0)) 'face
)))
5835 (not (org-in-src-block-p)))
5836 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5837 (setq f
(get-text-property (match-beginning 0) 'face
))
5838 (setq hl
(org-match-string-no-properties 0))
5839 (if (or (eq f
'org-tag
)
5840 (and (listp f
) (memq 'org-tag f
)))
5842 (add-text-properties (match-beginning 0) (match-end 0)
5843 (list 'mouse-face
'highlight
5845 'htmlize-link
`(:uri
,hl
)
5846 'keymap org-mouse-map
))
5847 (org-rear-nonsticky-at (match-end 0)))
5850 (defun org-activate-code (limit)
5851 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5853 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5854 (remove-text-properties (match-beginning 0) (match-end 0)
5855 '(display t invisible t intangible t
))
5858 (defcustom org-src-fontify-natively t
5859 "When non-nil, fontify code in code blocks."
5862 :package-version
'(Org .
"8.3")
5863 :group
'org-appearance
5866 (defcustom org-allow-promoting-top-level-subtree nil
5867 "When non-nil, allow promoting a top level subtree.
5868 The leading star of the top level headline will be replaced
5872 :group
'org-appearance
)
5874 (defun org-fontify-meta-lines-and-blocks (limit)
5876 (org-fontify-meta-lines-and-blocks-1 limit
)
5877 (error (message "org-mode fontification error"))))
5879 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5880 "Fontify #+ lines and blocks."
5881 (let ((case-fold-search t
))
5882 (if (re-search-forward
5883 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5885 (let ((beg (match-beginning 0))
5886 (block-start (match-end 0))
5888 (lang (match-string 7))
5889 (beg1 (line-beginning-position 2))
5890 (dc1 (downcase (match-string 2)))
5891 (dc3 (downcase (match-string 3)))
5892 end end1 quoting block-type ovl
)
5894 ((member dc1
'("+html:" "+ascii:" "+latex:"))
5895 ;; a single line of backend-specific content
5896 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5897 (remove-text-properties (match-beginning 0) (match-end 0)
5898 '(display t invisible t intangible t
))
5899 (add-text-properties (match-beginning 1) (match-end 3)
5900 '(font-lock-fontified t face org-meta-line
))
5901 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5902 '(font-lock-fontified t face org-block
))
5903 ; for backend-specific code
5905 ((and (match-end 4) (equal dc3
"+begin"))
5907 (setq block-type
(downcase (match-string 5))
5908 quoting
(member block-type org-protecting-blocks
))
5909 (when (re-search-forward
5910 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5911 nil t
) ;; on purpose, we look further than LIMIT
5912 (setq end
(min (point-max) (match-end 0))
5913 end1
(min (point-max) (1- (match-beginning 0))))
5914 (setq block-end
(match-beginning 0))
5916 (org-remove-flyspell-overlays-in beg1 end1
)
5917 (remove-text-properties beg end
5918 '(display t invisible t intangible t
)))
5919 (add-text-properties
5920 beg end
'(font-lock-fontified t font-lock-multiline t
))
5921 (add-text-properties beg beg1
'(face org-meta-line
))
5922 (org-remove-flyspell-overlays-in beg beg1
)
5923 (add-text-properties ; For end_src
5924 end1
(min (point-max) (1+ end
)) '(face org-meta-line
))
5925 (org-remove-flyspell-overlays-in end1 end
)
5927 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5928 (org-src-font-lock-fontify-block lang block-start block-end
)
5929 (add-text-properties beg1 block-end
'(src-block t
)))
5931 (add-text-properties beg1
(min (point-max) (1+ end1
))
5932 '(face org-block
))) ; end of source block
5933 ((not org-fontify-quote-and-verse-blocks
))
5934 ((string= block-type
"quote")
5935 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5936 ((string= block-type
"verse")
5937 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5938 (add-text-properties beg beg1
'(face org-block-begin-line
))
5939 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5940 '(face org-block-end-line
))
5942 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5943 (org-remove-flyspell-overlays-in
5945 (if (equal "+title:" dc1
) (match-end 2) (match-end 0)))
5946 (add-text-properties
5948 (if (member (intern (substring dc1
1 -
1)) org-hidden-keywords
)
5949 '(font-lock-fontified t invisible t
)
5950 '(font-lock-fontified t face org-document-info-keyword
)))
5951 (add-text-properties
5952 (match-beginning 6) (min (point-max) (1+ (match-end 6)))
5953 (if (string-equal dc1
"+title:")
5954 '(font-lock-fontified t face org-document-title
)
5955 '(font-lock-fontified t face org-document-info
))))
5956 ((or (equal dc1
"+results")
5957 (member dc1
'("+begin:" "+end:" "+caption:" "+label:"
5958 "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
5959 "+call:" "+header:" "+headers:" "+name:"))
5960 (and (match-end 4) (equal dc3
"+attr")))
5961 (org-remove-flyspell-overlays-in
5963 (if (equal "+caption:" dc1
) (match-end 2) (match-end 0)))
5964 (add-text-properties
5966 '(font-lock-fontified t face org-meta-line
))
5968 ((member dc3
'(" " ""))
5969 (org-remove-flyspell-overlays-in beg
(match-end 0))
5970 (add-text-properties
5972 '(font-lock-fontified t face font-lock-comment-face
)))
5973 (t ;; just any other in-buffer setting, but not indented
5974 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5975 (add-text-properties
5977 '(font-lock-fontified t face org-meta-line
))
5980 (defun org-fontify-drawers (limit)
5982 (when (re-search-forward org-drawer-regexp limit t
)
5983 (add-text-properties
5984 (match-beginning 0) (match-end 0)
5985 '(font-lock-fontified t face org-special-keyword
))
5986 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5989 (defun org-fontify-macros (limit)
5991 (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t
)
5992 (add-text-properties
5993 (match-beginning 0) (match-end 0)
5994 '(font-lock-fontified t face org-macro
))
5995 (when org-hide-macro-markers
5996 (add-text-properties (match-end 2) (match-beginning 2)
5998 (add-text-properties (match-beginning 1) (match-end 1)
6000 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
6003 (defun org-activate-angle-links (limit)
6004 "Add text properties for angle links."
6005 (if (and (re-search-forward org-angle-link-re limit t
)
6006 (not (org-in-src-block-p)))
6008 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
6009 (add-text-properties (match-beginning 0) (match-end 0)
6010 (list 'mouse-face
'highlight
6011 'keymap org-mouse-map
))
6012 (org-rear-nonsticky-at (match-end 0))
6015 (defun org-activate-footnote-links (limit)
6016 "Add text properties for footnotes."
6017 (let ((fn (org-footnote-next-reference-or-definition limit
)))
6019 (let* ((beg (nth 1 fn
))
6022 (referencep (/= (line-beginning-position) beg
)))
6023 (when (and referencep
(nth 3 fn
))
6026 (search-forward (or label
"fn:"))
6027 (org-remove-flyspell-overlays-in beg
(match-end 0))))
6028 (add-text-properties beg end
6029 (list 'mouse-face
'highlight
6030 'keymap org-mouse-map
6032 (if referencep
"Footnote reference"
6033 "Footnote definition")
6034 'font-lock-fontified t
6035 'font-lock-multiline t
6036 'face
'org-footnote
))))))
6038 (defun org-activate-bracket-links (limit)
6039 "Add text properties for bracketed links."
6040 (if (and (re-search-forward org-bracket-link-regexp limit t
)
6041 (not (org-in-src-block-p)))
6042 (let* ((hl (org-match-string-no-properties 1))
6043 (help (concat "LINK: " (save-match-data (org-link-unescape hl
))))
6044 (ip (org-maybe-intangible
6045 (list 'invisible
'org-link
6046 'keymap org-mouse-map
'mouse-face
'highlight
6047 'font-lock-multiline t
'help-echo help
6048 'htmlize-link
`(:uri
,hl
))))
6049 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
6050 'font-lock-multiline t
'help-echo help
6051 'htmlize-link
`(:uri
,hl
))))
6052 ;; We need to remove the invisible property here. Table narrowing
6053 ;; may have made some of this invisible.
6054 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
6055 (remove-text-properties (match-beginning 0) (match-end 0)
6059 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
6060 (org-rear-nonsticky-at (match-beginning 3))
6061 (add-text-properties (match-beginning 3) (match-end 3) vp
)
6062 (org-rear-nonsticky-at (match-end 3))
6063 (add-text-properties (match-end 3) (match-end 0) ip
)
6064 (org-rear-nonsticky-at (match-end 0)))
6065 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
6066 (org-rear-nonsticky-at (match-beginning 1))
6067 (add-text-properties (match-beginning 1) (match-end 1) vp
)
6068 (org-rear-nonsticky-at (match-end 1))
6069 (add-text-properties (match-end 1) (match-end 0) ip
)
6070 (org-rear-nonsticky-at (match-end 0)))
6073 (defun org-activate-dates (limit)
6074 "Add text properties for dates."
6075 (if (and (re-search-forward org-tsr-regexp-both limit t
)
6076 (not (equal (char-before (match-beginning 0)) 91)))
6078 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
6079 (add-text-properties (match-beginning 0) (match-end 0)
6080 (list 'mouse-face
'highlight
6081 'keymap org-mouse-map
))
6082 (org-rear-nonsticky-at (match-end 0))
6083 (when org-display-custom-times
6085 (org-display-custom-time (match-beginning 3) (match-end 3)))
6086 (org-display-custom-time (match-beginning 1) (match-end 1)))
6089 (defvar org-target-link-regexp nil
6090 "Regular expression matching radio targets in plain text.")
6091 (make-variable-buffer-local 'org-target-link-regexp
)
6093 (defconst org-target-regexp
(let ((border "[^<>\n\r \t]"))
6094 (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
6095 border border border
))
6096 "Regular expression matching a link target.")
6098 (defconst org-radio-target-regexp
(format "<%s>" org-target-regexp
)
6099 "Regular expression matching a radio target.")
6101 (defconst org-any-target-regexp
6102 (format "%s\\|%s" org-radio-target-regexp org-target-regexp
)
6103 "Regular expression matching any target.")
6105 (defun org-activate-target-links (limit)
6106 "Add text properties for target matches."
6107 (when org-target-link-regexp
6108 (let ((case-fold-search t
))
6109 (if (re-search-forward org-target-link-regexp limit t
)
6111 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
6112 (add-text-properties (match-beginning 1) (match-end 1)
6113 (list 'mouse-face
'highlight
6114 'keymap org-mouse-map
6115 'help-echo
"Radio target link"
6116 'org-linked-text t
))
6117 (org-rear-nonsticky-at (match-end 1))
6120 (defun org-update-radio-target-regexp ()
6121 "Find all radio targets in this file and update the regular expression.
6122 Also refresh fontification if needed."
6124 (let ((old-regexp org-target-link-regexp
)
6125 (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
6126 (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
6128 (org-with-wide-buffer
6129 (goto-char (point-min))
6131 (while (re-search-forward org-radio-target-regexp nil t
)
6132 ;; Make sure point is really within the object.
6134 (let ((obj (org-element-context)))
6135 (when (eq (org-element-type obj
) 'radio-target
)
6136 (add-to-list 'rtn
(org-element-property :value obj
)))))
6138 (setq org-target-link-regexp
6143 (replace-regexp-in-string
6144 " +" "\\s-+" (regexp-quote x
) t t
))
6148 (unless (equal old-regexp org-target-link-regexp
)
6150 (let ((regexp (cond ((not old-regexp
) org-target-link-regexp
)
6151 ((not org-target-link-regexp
) old-regexp
)
6156 (substring re
(length before-re
)
6157 (- (length after-re
))))
6158 (list old-regexp org-target-link-regexp
)
6161 (org-with-wide-buffer
6162 (goto-char (point-min))
6163 (while (re-search-forward regexp nil t
)
6164 (org-element-cache-refresh (match-beginning 1)))))
6165 ;; Re fontify buffer.
6166 (when (memq 'radio org-highlight-links
)
6167 (org-restart-font-lock)))))
6169 (defun org-hide-wide-columns (limit)
6171 (setq s
(text-property-any (point) (or limit
(point-max))
6174 (setq e
(next-single-property-change s
'org-cwidth
))
6175 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
6179 (defvar org-latex-and-related-regexp nil
6180 "Regular expression for highlighting LaTeX, entities and sub/superscript.")
6182 (defun org-compute-latex-and-related-regexp ()
6183 "Compute regular expression for LaTeX, entities and sub/superscript.
6184 Result depends on variable `org-highlight-latex-and-related'."
6186 'org-latex-and-related-regexp
6188 (cond ((not (memq 'script org-highlight-latex-and-related
)) nil
)
6189 ((eq org-use-sub-superscripts
'{})
6190 (list org-match-substring-with-braces-regexp
))
6191 (org-use-sub-superscripts (list org-match-substring-regexp
))))
6193 (when (memq 'latex org-highlight-latex-and-related
)
6194 (let ((matchers (plist-get org-format-latex-options
:matchers
)))
6197 (and (member (car x
) matchers
) (nth 1 x
)))
6198 org-latex-regexps
)))))
6200 (when (memq 'entities org-highlight-latex-and-related
)
6201 (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
6202 (mapconcat 'identity
(append re-latex re-entities re-sub
) "\\|"))))
6204 (defun org-do-latex-and-related (limit)
6205 "Highlight LaTeX snippets and environments, entities and sub/superscript.
6206 LIMIT bounds the search for syntax to highlight. Stop at first
6207 highlighted object, if any. Return t if some highlighting was
6208 done, nil otherwise."
6209 (when (org-string-nw-p org-latex-and-related-regexp
)
6211 (while (re-search-forward org-latex-and-related-regexp limit t
)
6212 (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
6214 '(org-code org-verbatim underline
))
6215 (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
6219 (font-lock-prepend-text-property
6220 (+ offset
(match-beginning 0)) (match-end 0)
6221 'face
'org-latex-and-related
)
6222 (add-text-properties (+ offset
(match-beginning 0)) (match-end 0)
6223 '(font-lock-multiline t
)))
6227 (defun org-restart-font-lock ()
6228 "Restart `font-lock-mode', to force refontification."
6229 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
6231 (font-lock-mode 1)))
6233 (defun org-activate-tags (limit)
6234 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
6236 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
6237 (add-text-properties (match-beginning 1) (match-end 1)
6238 (list 'mouse-face
'highlight
6239 'keymap org-mouse-map
))
6240 (org-rear-nonsticky-at (match-end 1))
6243 (defun org-outline-level ()
6244 "Compute the outline level of the heading at point.
6245 If this is called at a normal headline, the level is the number of stars.
6246 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
6248 (if (not (ignore-errors (org-back-to-heading t
)))
6250 (looking-at org-outline-regexp
)
6251 (1- (- (match-end 0) (match-beginning 0))))))
6253 (defvar org-font-lock-keywords nil
)
6255 (defsubst org-re-property
(property &optional literal allow-null
)
6256 "Return a regexp matching a PROPERTY line.
6257 Match group 3 will be set to the value if it exists."
6258 (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
6259 (if literal property
(regexp-quote property
))
6260 "\\):\\)[ \t]+\\(?3:[^ \t\r\n]"
6262 ".*?\\)\\(?5:[ \t]*\\)$"))
6264 (defconst org-property-re
6265 (org-re-property ".*?" 'literal t
)
6266 "Regular expression matching a property line.
6267 There are four matching groups:
6268 1: :PROPKEY: including the leading and trailing colon,
6269 2: PROPKEY without the leading and trailing colon,
6270 3: PROPVAL without leading or trailing spaces,
6271 4: the indentation of the current line,
6272 5: trailing whitespace.")
6274 (defvar org-font-lock-hook nil
6275 "Functions to be called for special font lock stuff.")
6277 (defvar org-font-lock-set-keywords-hook nil
6278 "Functions that can manipulate `org-font-lock-extra-keywords'.
6279 This is called after `org-font-lock-extra-keywords' is defined, but before
6280 it is installed to be used by font lock. This can be useful if something
6281 needs to be inserted at a specific position in the font-lock sequence.")
6283 (defun org-font-lock-hook (limit)
6284 "Run `org-font-lock-hook' within LIMIT."
6285 (run-hook-with-args 'org-font-lock-hook limit
))
6287 (defun org-set-font-lock-defaults ()
6288 "Set font lock defaults for the current buffer."
6289 (let* ((em org-fontify-emphasized-text
)
6290 (lk org-highlight-links
)
6291 (org-font-lock-extra-keywords
6294 '(org-font-lock-hook)
6296 `(,(if org-fontify-whole-heading-line
6297 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
6298 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
6299 (1 (org-get-level-face 1))
6300 (2 (org-get-level-face 2))
6301 (3 (org-get-level-face 3)))
6303 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
6306 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
6307 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
6308 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
6309 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
6311 '(org-fontify-drawers)
6313 (list org-property-re
6314 '(1 'org-special-keyword t
)
6315 '(3 'org-property-value t
))
6317 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
6318 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
6319 (if (memq 'plain lk
) '(org-activate-plain-links (0 'org-link t
)))
6320 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
6321 (if (memq 'radio lk
) '(org-activate-target-links (1 'org-link t
)))
6322 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
6323 (if (memq 'footnote lk
) '(org-activate-footnote-links))
6325 (list org-any-target-regexp
'(0 'org-target t
))
6327 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
6329 '(org-fontify-macros)
6330 '(org-hide-wide-columns (0 nil append
))
6332 (list (format org-heading-keyword-regexp-format
6334 '(2 (org-get-todo-face 2) t
))
6336 (if org-fontify-done-headline
6337 (list (format org-heading-keyword-regexp-format
6340 (mapconcat 'regexp-quote org-done-keywords
"\\|")
6342 '(2 'org-headline-done t
))
6345 '(org-font-lock-add-priority-faces)
6347 '(org-font-lock-add-tag-faces)
6349 (if (and org-group-tags org-tag-groups-alist
)
6350 (list (concat org-outline-regexp-bol
".+\\(:"
6351 (regexp-opt (mapcar 'car org-tag-groups-alist
))
6353 '(1 'org-tag-group prepend
)))
6355 (list (concat "\\<" org-comment-string
) '(0 'org-special-keyword t
))
6356 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
6357 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
6358 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
6359 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
6362 (if (featurep 'xemacs
)
6363 '(org-do-emphasis-faces (0 nil append
))
6364 '(org-do-emphasis-faces)))
6366 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
6367 1 'org-checkbox prepend
)
6368 (if (cdr (assq 'checkbox org-list-automatic-rules
))
6369 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
6370 (0 (org-get-checkbox-statistics-face) t
)))
6371 ;; Description list items
6372 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
6373 1 'org-list-dt prepend
)
6374 ;; ARCHIVEd headings
6376 org-outline-regexp-bol
6377 "\\(.*:" org-archive-tag
":.*\\)")
6378 '(1 'org-archived prepend
))
6380 '(org-do-latex-and-related)
6381 '(org-fontify-entities)
6382 '(org-raise-scripts)
6384 '(org-activate-code (1 'org-code t
))
6387 "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
6390 '(9 'org-special-keyword t
))
6391 ;; Blocks and meta lines
6392 '(org-fontify-meta-lines-and-blocks))))
6393 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
6394 (run-hooks 'org-font-lock-set-keywords-hook
)
6395 ;; Now set the full font-lock-keywords
6396 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
6397 (org-set-local 'font-lock-defaults
6398 '(org-font-lock-keywords t nil nil backward-paragraph
))
6399 (kill-local-variable 'font-lock-keywords
) nil
))
6401 (defun org-toggle-pretty-entities ()
6402 "Toggle the composition display of entities as UTF8 characters."
6404 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
6405 (org-restart-font-lock)
6406 (if org-pretty-entities
6407 (message "Entities are now displayed as UTF8 characters")
6410 (org-decompose-region (point-min) (point-max))
6411 (message "Entities are now displayed as plain text"))))
6413 (defvar org-custom-properties-overlays nil
6414 "List of overlays used for custom properties.")
6415 (make-variable-buffer-local 'org-custom-properties-overlays
)
6417 (defun org-toggle-custom-properties-visibility ()
6418 "Display or hide properties in `org-custom-properties'."
6420 (if org-custom-properties-overlays
6421 (progn (mapc 'delete-overlay org-custom-properties-overlays
)
6422 (setq org-custom-properties-overlays nil
))
6423 (unless (not org-custom-properties
)
6427 (goto-char (point-min))
6428 (while (re-search-forward org-property-re nil t
)
6430 (when (equal p
(substring (match-string 1) 1 -
1))
6431 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
6432 (overlay-put o
'invisible t
)
6433 (overlay-put o
'org-custom-property t
)
6434 (push o org-custom-properties-overlays
))))
6435 org-custom-properties
)))))))
6437 (defun org-fontify-entities (limit)
6438 "Find an entity to fontify."
6440 (when org-pretty-entities
6442 (while (re-search-forward
6443 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
6445 (if (and (not (org-at-comment-p))
6446 (setq ee
(org-entity-get (match-string 1)))
6447 (= (length (nth 6 ee
)) 1))
6449 ((end (if (equal (match-string 2) "{}")
6452 (add-text-properties
6453 (match-beginning 0) end
6454 (list 'font-lock-fontified t
))
6455 (compose-region (match-beginning 0) end
6461 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
6462 "Fontify string S like in Org-mode."
6465 (let ((org-odd-levels-only odd-levels
))
6473 (defun org-get-level-face (n)
6474 "Get the right face for match N in font-lock matching of headlines."
6475 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
6476 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
6477 (if org-cycle-level-faces
6478 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
6479 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
6481 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
6483 (t (if org-level-color-stars-only nil org-f
))))
6486 (defun org-get-todo-face (kwd)
6487 "Get the right face for a TODO keyword KWD.
6488 If KWD is a number, get the corresponding match group."
6489 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6490 (or (org-face-from-face-or-color
6491 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
6492 (and (member kwd org-done-keywords
) 'org-done
)
6495 (defun org-face-from-face-or-color (context inherit face-or-color
)
6496 "Create a face list that inherits INHERIT, but sets the foreground color.
6497 When FACE-OR-COLOR is not a string, just return it."
6498 (if (stringp face-or-color
)
6499 (list :inherit inherit
6500 (cdr (assoc context org-faces-easy-properties
))
6504 (defun org-font-lock-add-tag-faces (limit)
6505 "Add the special tag faces."
6506 (when (and org-tag-faces org-tags-special-faces-re
)
6507 (while (re-search-forward org-tags-special-faces-re limit t
)
6508 (add-text-properties (match-beginning 1) (match-end 1)
6509 (list 'face
(org-get-tag-face 1)
6510 'font-lock-fontified t
))
6511 (backward-char 1))))
6513 (defun org-font-lock-add-priority-faces (limit)
6514 "Add the special priority faces."
6515 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
6516 (when (save-match-data (org-at-heading-p))
6517 (add-text-properties
6518 (match-beginning 0) (match-end 0)
6519 (list 'face
(or (org-face-from-face-or-color
6520 'priority
'org-priority
6521 (cdr (assoc (char-after (match-beginning 1))
6522 org-priority-faces
)))
6524 'font-lock-fontified t
)))))
6526 (defun org-get-tag-face (kwd)
6527 "Get the right face for a TODO keyword KWD.
6528 If KWD is a number, get the corresponding match group."
6529 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6530 (or (org-face-from-face-or-color
6531 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
6534 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
6535 "Remove fontification and activation overlays from links."
6536 (font-lock-default-unfontify-region beg end
)
6537 (let* ((buffer-undo-list t
)
6538 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
6539 (inhibit-modification-hooks t
)
6540 deactivate-mark buffer-file-name buffer-file-truename
)
6541 (org-decompose-region beg end
)
6542 (remove-text-properties beg end
6543 '(mouse-face t keymap t org-linked-text t
6544 invisible t intangible t
6546 (org-remove-font-lock-display-properties beg end
)))
6548 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
6549 ((raise 0.3) (height 0.7))
6552 "Display properties for showing superscripts and subscripts.")
6554 (defun org-remove-font-lock-display-properties (beg end
)
6555 "Remove specific display properties that have been added by font lock.
6556 The will remove the raise properties that are used to show superscripts
6560 (setq next
(next-single-property-change beg
'display nil end
)
6561 prop
(get-text-property beg
'display
))
6562 (if (member prop org-script-display
)
6563 (put-text-property beg next
'display nil
))
6566 (defun org-raise-scripts (limit)
6567 "Add raise properties to sub/superscripts."
6568 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6569 (if (re-search-forward
6570 (if (eq org-use-sub-superscripts t
)
6571 org-match-substring-regexp
6572 org-match-substring-with-braces-regexp
)
6574 (let* ((pos (point)) table-p comment-p
6575 (mpos (match-beginning 3))
6576 (emph-p (get-text-property mpos
'org-emphasis
))
6577 (link-p (get-text-property mpos
'mouse-face
))
6578 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6579 (goto-char (point-at-bol))
6580 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6581 comment-p
(org-looking-at-p "^[ \t]*#[ +]"))
6584 (if (member (char-after) '(?_ ?^
)) (goto-char (1- pos
)))
6585 (if (or comment-p emph-p link-p keyw-p
)
6587 (put-text-property (match-beginning 3) (match-end 0)
6589 (if (equal (char-after (match-beginning 2)) ?^
)
6590 (nth (if table-p
3 1) org-script-display
)
6591 (nth (if table-p
2 0) org-script-display
)))
6592 (add-text-properties (match-beginning 2) (match-end 2)
6594 'org-dwidth t
'org-dwidth-n
1))
6595 (if (and (eq (char-after (match-beginning 3)) ?
{)
6596 (eq (char-before (match-end 3)) ?
}))
6598 (add-text-properties
6599 (match-beginning 3) (1+ (match-beginning 3))
6600 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6601 (add-text-properties
6602 (1- (match-end 3)) (match-end 3)
6603 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6606 ;;;; Visibility cycling, including org-goto and indirect buffer
6610 (defvar org-cycle-global-status nil
)
6611 (make-variable-buffer-local 'org-cycle-global-status
)
6612 (put 'org-cycle-global-status
'org-state t
)
6613 (defvar org-cycle-subtree-status nil
)
6614 (make-variable-buffer-local 'org-cycle-subtree-status
)
6615 (put 'org-cycle-subtree-status
'org-state t
)
6617 (defvar org-inlinetask-min-level
)
6619 (defun org-unlogged-message (&rest args
)
6620 "Display a message, but avoid logging it in the *Messages* buffer."
6621 (let ((message-log-max nil
))
6622 (apply 'message args
)))
6625 (defun org-cycle (&optional arg
)
6626 "TAB-action and visibility cycling for Org-mode.
6628 This is the command invoked in Org-mode by the TAB key. Its main purpose
6629 is outline visibility cycling, but it also invokes other actions
6630 in special contexts.
6632 - When this function is called with a prefix argument, rotate the entire
6633 buffer through 3 states (global cycling)
6634 1. OVERVIEW: Show only top-level headlines.
6635 2. CONTENTS: Show all headlines of all levels, but no body text.
6636 3. SHOW ALL: Show everything.
6637 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6638 determined by the variable `org-startup-folded', and by any VISIBILITY
6639 properties in the buffer.
6640 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6641 including any drawers.
6643 - When inside a table, re-align the table and move to the next field.
6645 - When point is at the beginning of a headline, rotate the subtree started
6646 by this line through 3 different states (local cycling)
6647 1. FOLDED: Only the main headline is shown.
6648 2. CHILDREN: The main headline and the direct children are shown.
6649 From this state, you can move to one of the children
6650 and zoom in further.
6651 3. SUBTREE: Show the entire subtree, including body text.
6652 If there is no subtree, switch directly from CHILDREN to FOLDED.
6654 - When point is at the beginning of an empty headline and the variable
6655 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6656 of the headline by demoting and promoting it to likely levels. This
6657 speeds up creation document structure by pressing TAB once or several
6658 times right after creating a new headline.
6660 - When there is a numeric prefix, go up to a heading with level ARG, do
6661 a `show-subtree' and return to the previous cursor position. If ARG
6662 is negative, go up that many levels.
6664 - When point is not at the beginning of a headline, execute the global
6665 binding for TAB, which is re-indenting the line. See the option
6666 `org-cycle-emulate-tab' for details.
6668 - Special case: if point is at the beginning of the buffer and there is
6669 no headline in line 1, this function will act as if called with prefix arg
6670 (C-u TAB, same as S-TAB) also when called without prefix arg.
6671 But only if also the variable `org-cycle-global-at-bob' is t."
6673 (org-load-modules-maybe)
6674 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6675 (and org-cycle-level-after-item
/entry-creation
6676 (or (org-cycle-level)
6677 (org-cycle-item-indentation))))
6679 (or org-cycle-max-level
6680 (and (boundp 'org-inlinetask-min-level
)
6681 org-inlinetask-min-level
6682 (1- org-inlinetask-min-level
))))
6683 (nstars (and limit-level
6684 (if org-odd-levels-only
6685 (and limit-level
(1- (* limit-level
2)))
6688 (if (not (derived-mode-p 'org-mode
))
6690 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6691 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6692 (not (looking-at org-outline-regexp
))))
6695 (delq 'org-optimize-window-after-visibility-change
6696 (copy-sequence org-cycle-hook
))
6700 (if (or bob-special
(equal arg
'(4)))
6701 ;; special case: use global cycling
6707 (setq last-command
'dummy
)
6708 (org-set-startup-visibility)
6709 (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
6713 (org-unlogged-message "Entire buffer visible, including drawers"))
6715 ;; Try cdlatex TAB completion
6716 ((org-try-cdlatex-tab))
6718 ;; Table: enter it or move to the next field.
6719 ((org-at-table-p 'any
)
6720 (if (org-at-table.el-p
)
6721 (message "Use C-c ' to edit table.el tables")
6722 (if arg
(org-table-edit-field t
)
6723 (org-table-justify-field-maybe)
6724 (call-interactively 'org-table-next-field
))))
6726 ((run-hook-with-args-until-success
6727 'org-tab-after-check-for-table-hook
))
6729 ;; Global cycling: delegate to `org-cycle-internal-global'.
6730 ((eq arg t
) (org-cycle-internal-global))
6732 ;; Drawers: delegate to `org-flag-drawer'.
6734 (beginning-of-line 1)
6735 (looking-at org-drawer-regexp
))
6736 (org-flag-drawer ; toggle block visibility
6737 (not (get-char-property (match-end 0) 'invisible
))))
6739 ;; Show-subtree, ARG levels up from here.
6742 (org-back-to-heading)
6743 (outline-up-heading (if (< arg
0) (- arg
)
6744 (- (funcall outline-level
) arg
)))
6745 (org-show-subtree)))
6747 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6748 ((and (featurep 'org-inlinetask
)
6749 (org-inlinetask-at-task-p)
6750 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6751 (org-inlinetask-toggle-visibility))
6753 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6754 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6755 (save-excursion (beginning-of-line 1)
6756 (looking-at org-outline-regexp
)))
6757 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6758 (org-cycle-internal-local))
6760 ;; From there: TAB emulation and template completion.
6761 (buffer-read-only (org-back-to-heading))
6763 ((run-hook-with-args-until-success
6764 'org-tab-after-check-for-cycling-hook
))
6766 ((org-try-structure-completion))
6768 ((run-hook-with-args-until-success
6769 'org-tab-before-tab-emulation-hook
))
6771 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6773 (not (looking-at org-outline-regexp
))))
6774 (call-interactively (global-key-binding "\t")))
6776 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6777 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6778 (or (and (eq org-cycle-emulate-tab
'white
)
6779 (= (match-end 0) (point-at-eol)))
6780 (and (eq org-cycle-emulate-tab
'whitestart
)
6781 (>= (match-end 0) pos
))))
6783 (eq org-cycle-emulate-tab t
))
6784 (call-interactively (global-key-binding "\t")))
6787 (org-back-to-heading)
6790 (defun org-cycle-internal-global ()
6791 "Do the global cycling action."
6792 ;; Hack to avoid display of messages for .org attachments in Gnus
6793 (let ((ga (string-match "\\*fontification" (buffer-name))))
6795 ((and (eq last-command this-command
)
6796 (eq org-cycle-global-status
'overview
))
6797 ;; We just created the overview - now do table of contents
6798 ;; This can be slow in very large buffers, so indicate action
6799 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6800 (unless ga
(org-unlogged-message "CONTENTS..."))
6802 (unless ga
(org-unlogged-message "CONTENTS...done"))
6803 (setq org-cycle-global-status
'contents
)
6804 (run-hook-with-args 'org-cycle-hook
'contents
))
6806 ((and (eq last-command this-command
)
6807 (eq org-cycle-global-status
'contents
))
6808 ;; We just showed the table of contents - now show everything
6809 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6811 (unless ga
(org-unlogged-message "SHOW ALL"))
6812 (setq org-cycle-global-status
'all
)
6813 (run-hook-with-args 'org-cycle-hook
'all
))
6816 ;; Default action: go to overview
6817 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6819 (unless ga
(org-unlogged-message "OVERVIEW"))
6820 (setq org-cycle-global-status
'overview
)
6821 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6823 (defvar org-called-with-limited-levels nil
6824 "Non-nil when `org-with-limited-levels' is currently active.")
6826 (defun org-cycle-internal-local ()
6827 "Do the local cycling action."
6828 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6829 ;; First, determine end of headline (EOH), end of subtree or item
6830 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6835 (setq struct
(org-list-struct))
6836 (setq eoh
(point-at-eol))
6837 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6838 (setq has-children
(org-list-has-child-p (point) struct
)))
6839 (org-back-to-heading)
6840 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6841 (setq eos
(save-excursion (org-end-of-subtree t t
)
6842 (when (bolp) (backward-char)) (point)))
6845 (let ((level (funcall outline-level
)))
6846 (outline-next-heading)
6847 (and (org-at-heading-p t
)
6848 (> (funcall outline-level
) level
))))
6850 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6851 ;; Determine end invisible part of buffer (EOL)
6852 (beginning-of-line 2)
6853 ;; XEmacs doesn't have `next-single-char-property-change'
6854 (if (featurep 'xemacs
)
6855 (while (and (not (eobp)) ;; this is like `next-line'
6856 (get-char-property (1- (point)) 'invisible
))
6857 (beginning-of-line 2))
6858 (while (and (not (eobp)) ;; this is like `next-line'
6859 (get-char-property (1- (point)) 'invisible
))
6860 (goto-char (next-single-char-property-change (point) 'invisible
))
6861 (and (eolp) (beginning-of-line 2))))
6863 ;; Find out what to do next and set `this-command'
6866 ;; Nothing is hidden behind this heading
6867 (unless (org-before-first-heading-p)
6868 (run-hook-with-args 'org-pre-cycle-hook
'empty
))
6869 (org-unlogged-message "EMPTY ENTRY")
6870 (setq org-cycle-subtree-status nil
)
6873 (outline-next-heading)
6874 (if (outline-invisible-p) (org-flag-heading nil
))))
6875 ((and (or (>= eol eos
)
6876 (not (string-match "\\S-" (buffer-substring eol eos
))))
6878 (not (setq children-skipped
6879 org-cycle-skip-children-state-if-no-children
))))
6880 ;; Entire subtree is hidden in one line: children view
6881 (unless (org-before-first-heading-p)
6882 (run-hook-with-args 'org-pre-cycle-hook
'children
))
6884 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6886 (org-with-limited-levels (show-children))
6887 ;; FIXME: This slows down the func way too much.
6888 ;; How keep drawers hidden in subtree anyway?
6889 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6890 ;; (org-cycle-hide-drawers 'subtree))
6892 ;; Fold every list in subtree to top-level items.
6893 (when (eq org-cycle-include-plain-lists
'integrate
)
6895 (org-back-to-heading)
6896 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6897 (beginning-of-line 1)
6898 (let* ((struct (org-list-struct))
6899 (prevs (org-list-prevs-alist struct
))
6900 (end (org-list-get-bottom-point struct
)))
6901 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6902 (org-list-get-all-items (point) struct prevs
))
6903 (goto-char (if (< end eos
) end eos
)))))))
6904 (org-unlogged-message "CHILDREN")
6907 (outline-next-heading)
6908 (if (outline-invisible-p) (org-flag-heading nil
)))
6909 (setq org-cycle-subtree-status
'children
)
6910 (unless (org-before-first-heading-p)
6911 (run-hook-with-args 'org-cycle-hook
'children
)))
6912 ((or children-skipped
6913 (and (eq last-command this-command
)
6914 (eq org-cycle-subtree-status
'children
)))
6915 ;; We just showed the children, or no children are there,
6916 ;; now show everything.
6917 (unless (org-before-first-heading-p)
6918 (run-hook-with-args 'org-pre-cycle-hook
'subtree
))
6919 (outline-flag-region eoh eos nil
)
6920 (org-unlogged-message
6921 (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6922 (setq org-cycle-subtree-status
'subtree
)
6923 (unless (org-before-first-heading-p)
6924 (run-hook-with-args 'org-cycle-hook
'subtree
)))
6926 ;; Default action: hide the subtree.
6927 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6928 (outline-flag-region eoh eos t
)
6929 (org-unlogged-message "FOLDED")
6930 (setq org-cycle-subtree-status
'folded
)
6931 (unless (org-before-first-heading-p)
6932 (run-hook-with-args 'org-cycle-hook
'folded
))))))
6935 (defun org-global-cycle (&optional arg
)
6936 "Cycle the global visibility. For details see `org-cycle'.
6937 With \\[universal-argument] prefix arg, switch to startup visibility.
6938 With a numeric prefix, show all headlines up to that level."
6940 (let ((org-cycle-include-plain-lists
6941 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6945 (hide-sublevels arg
)
6946 (setq org-cycle-global-status
'contents
))
6948 (org-set-startup-visibility)
6949 (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
6951 (org-cycle '(4))))))
6953 (defun org-set-startup-visibility ()
6954 "Set the visibility required by startup options and properties."
6956 ((eq org-startup-folded t
)
6958 ((eq org-startup-folded
'content
)
6960 ((or (eq org-startup-folded
'showeverything
)
6961 (eq org-startup-folded nil
))
6963 (unless (eq org-startup-folded
'showeverything
)
6964 (if org-hide-block-startup
(org-hide-block-all))
6965 (org-set-visibility-according-to-property 'no-cleanup
)
6966 (org-cycle-hide-archived-subtrees 'all
)
6967 (org-cycle-hide-drawers 'all
)
6968 (org-cycle-show-empty-lines t
)))
6970 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6971 "Switch subtree visibilities according to :VISIBILITY: property."
6973 (let (org-show-entry-below state
)
6975 (goto-char (point-min))
6976 (while (re-search-forward
6977 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6979 (setq state
(match-string 1))
6981 (org-back-to-heading t
)
6985 ((equal state
'("fold" "folded"))
6987 ((equal state
"children")
6988 (org-show-hidden-entry)
6990 ((equal state
"content")
6993 (org-narrow-to-subtree)
6995 ((member state
'("all" "showall"))
6998 (org-cycle-hide-archived-subtrees 'all
)
6999 (org-cycle-hide-drawers 'all
)
7000 (org-cycle-show-empty-lines 'all
)))))
7002 ;; This function uses outline-regexp instead of the more fundamental
7003 ;; org-outline-regexp so that org-cycle-global works outside of Org
7004 ;; buffers, where outline-regexp is needed.
7005 (defun org-overview ()
7006 "Switch to overview mode, showing only top-level headlines.
7007 This shows all headlines with a level equal or greater than the level
7008 of the first headline in the buffer. This is important, because if the
7009 first headline is not level one, then (hide-sublevels 1) gives confusing
7015 (goto-char (point-min))
7016 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
7018 (goto-char (match-beginning 0))
7019 (funcall outline-level
))))))
7020 (and level
(hide-sublevels level
)))))
7022 (defun org-content (&optional arg
)
7023 "Show all headlines in the buffer, like a table of contents.
7024 With numerical argument N, show content up to level N."
7028 ;; Visit all headings and show their offspring
7029 (and (integerp arg
) (org-overview))
7030 (goto-char (point-max))
7032 (while (and (progn (condition-case nil
7033 (outline-previous-visible-heading 1)
7034 (error (goto-char (point-min))))
7036 (looking-at org-outline-regexp
))
7038 (show-children (1- arg
))
7040 (if (bobp) (throw 'exit nil
))))))
7042 (defun org-optimize-window-after-visibility-change (state)
7043 "Adjust the window after a change in outline visibility.
7044 This function is the default value of the hook `org-cycle-hook'."
7045 (when (get-buffer-window (current-buffer))
7047 ((eq state
'content
) nil
)
7048 ((eq state
'all
) nil
)
7049 ((eq state
'folded
) nil
)
7050 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
7051 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
7053 (defun org-remove-empty-overlays-at (pos)
7054 "Remove outline overlays that do not contain non-white stuff."
7057 (and (eq 'outline
(overlay-get o
'invisible
))
7058 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
7060 (delete-overlay o
)))
7063 (defun org-clean-visibility-after-subtree-move ()
7064 "Fix visibility issues after moving a subtree."
7065 ;; First, find a reasonable region to look at:
7066 ;; Start two siblings above, end three below
7067 (let* ((beg (save-excursion
7068 (and (org-get-last-sibling)
7069 (org-get-last-sibling))
7071 (end (save-excursion
7072 (and (org-get-next-sibling)
7073 (org-get-next-sibling)
7074 (org-get-next-sibling))
7075 (if (org-at-heading-p)
7078 (level (looking-at "\\*+"))
7079 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
7082 (narrow-to-region beg end
)
7084 ;; Properly fold already folded siblings
7085 (goto-char (point-min))
7086 (while (re-search-forward re nil t
)
7087 (if (and (not (outline-invisible-p))
7089 (goto-char (point-at-eol)) (outline-invisible-p)))
7091 (org-cycle-show-empty-lines 'overview
)
7092 (org-cycle-hide-drawers 'overview
)))))
7094 (defun org-cycle-show-empty-lines (state)
7095 "Show empty lines above all visible headlines.
7096 The region to be covered depends on STATE when called through
7097 `org-cycle-hook'. Lisp program can use t for STATE to get the
7098 entire buffer covered. Note that an empty line is only shown if there
7099 are at least `org-cycle-separator-lines' empty lines before the headline."
7100 (when (not (= org-cycle-separator-lines
0))
7102 (let* ((n (abs org-cycle-separator-lines
))
7104 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
7105 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
7106 (t (let ((ns (number-to-string (- n
2))))
7107 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
7108 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
7111 ((memq state
'(overview contents t
))
7112 (setq beg
(point-min) end
(point-max)))
7113 ((memq state
'(children folded
))
7114 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
7115 (beginning-of-line 2)
7119 (while (re-search-forward re end t
)
7120 (unless (get-char-property (match-end 1) 'invisible
)
7121 (setq e
(match-end 1))
7122 (if (< org-cycle-separator-lines
0)
7123 (setq b
(save-excursion
7124 (goto-char (match-beginning 0))
7125 (org-back-over-empty-lines)
7127 (goto-char (max (point-min) (1- (point))))
7131 (setq b
(match-beginning 1)))
7132 (outline-flag-region b e nil
)))))))
7133 ;; Never hide empty lines at the end of the file.
7135 (goto-char (point-max))
7136 (outline-previous-heading)
7137 (outline-end-of-heading)
7138 (if (and (looking-at "[ \t\n]+")
7139 (= (match-end 0) (point-max)))
7140 (outline-flag-region (point) (match-end 0) nil
))))
7142 (defun org-show-empty-lines-in-parent ()
7143 "Move to the parent and re-show empty lines before visible headlines."
7145 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
7146 (org-cycle-show-empty-lines context
))))
7148 (defun org-files-list ()
7149 "Return `org-agenda-files' list, plus all open org-mode files.
7150 This is useful for operations that need to scan all of a user's
7151 open and agenda-wise Org files."
7152 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
7153 (dolist (buf (buffer-list))
7154 (with-current-buffer buf
7155 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
7156 (let ((file (expand-file-name (buffer-file-name))))
7157 (unless (member file files
)
7158 (push file files
))))))
7161 (defsubst org-entry-beginning-position
()
7162 "Return the beginning position of the current entry."
7163 (save-excursion (outline-back-to-heading t
) (point)))
7165 (defsubst org-entry-end-position
()
7166 "Return the end position of the current entry."
7167 (save-excursion (outline-next-heading) (point)))
7169 (defun org-cycle-hide-drawers (state &optional exceptions
)
7170 "Re-hide all drawers after a visibility state change.
7171 When non-nil, optional argument EXCEPTIONS is a list of strings
7172 specifying which drawers should not be hidden."
7173 (when (and (derived-mode-p 'org-mode
)
7174 (not (memq state
'(overview folded contents
))))
7176 (let* ((globalp (memq state
'(contents all
)))
7177 (beg (if globalp
(point-min) (point)))
7178 (end (if globalp
(point-max)
7179 (if (eq state
'children
)
7180 (save-excursion (outline-next-heading) (point))
7181 (org-end-of-subtree t
)))))
7183 (while (re-search-forward org-drawer-regexp
(max end
(point)) t
)
7184 (unless (member-ignore-case (match-string 1) exceptions
)
7185 (let ((drawer (org-element-at-point)))
7186 (when (memq (org-element-type drawer
) '(drawer property-drawer
))
7187 (org-flag-drawer t drawer
)
7188 ;; Make sure to skip drawer entirely or we might flag
7189 ;; it another time when matching its ending line with
7190 ;; `org-drawer-regexp'.
7191 (goto-char (org-element-property :end drawer
))))))))))
7193 (defun org-cycle-hide-inline-tasks (state)
7194 "Re-hide inline tasks when switching to 'contents or 'children
7198 (when (org-bound-and-true-p org-inlinetask-min-level
)
7199 (hide-sublevels (1- org-inlinetask-min-level
))))
7201 (when (featurep 'org-inlinetask
)
7203 (while (and (outline-next-heading)
7204 (org-inlinetask-at-task-p))
7205 (org-inlinetask-toggle-visibility)
7206 (org-inlinetask-goto-end)))))))
7208 (defun org-flag-drawer (flag &optional element
)
7209 "When FLAG is non-nil, hide the drawer we are at.
7210 Otherwise make it visible. When optional argument ELEMENT is
7211 a parsed drawer, as returned by `org-element-at-point', hide or
7212 show that drawer instead."
7213 (when (save-excursion
7215 (org-looking-at-p org-drawer-regexp
))
7216 (let ((drawer (or element
(org-element-at-point))))
7217 (when (memq (org-element-type drawer
) '(drawer property-drawer
))
7218 (let ((post (org-element-property :post-affiliated drawer
)))
7220 (outline-flag-region
7221 (progn (goto-char post
) (line-end-position))
7222 (progn (goto-char (org-element-property :end drawer
))
7223 (skip-chars-backward " \r\t\n")
7224 (line-end-position))
7226 ;; When the drawer is hidden away, make sure point lies in
7227 ;; a visible part of the buffer.
7228 (when (and flag
(> (line-beginning-position) post
))
7229 (goto-char post
)))))))
7231 (defun org-subtree-end-visible-p ()
7232 "Is the end of the current subtree visible?"
7233 (pos-visible-in-window-p
7234 (save-excursion (org-end-of-subtree t
) (point))))
7236 (defun org-first-headline-recenter ()
7237 "Move cursor to the first headline and recenter the headline."
7238 (goto-char (point-min))
7239 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
7240 (set-window-start (selected-window) (point-at-bol))))
7242 ;;; Saving and restoring visibility
7244 (defun org-outline-overlay-data (&optional use-markers
)
7245 "Return a list of the locations of all outline overlays.
7246 These are overlays with the `invisible' property value `outline'.
7247 The return value is a list of cons cells, with start and stop
7248 positions for each overlay.
7249 If USE-MARKERS is set, return the positions as markers."
7256 (when (eq (overlay-get o
'invisible
) 'outline
)
7257 (setq beg
(overlay-start o
)
7258 end
(overlay-end o
))
7259 (and beg end
(> end beg
)
7261 (cons (copy-marker beg
)
7262 (copy-marker end t
))
7264 (overlays-in (point-min) (point-max))))))))
7266 (defun org-set-outline-overlay-data (data)
7267 "Create visibility overlays for all positions in DATA.
7268 DATA should have been made by `org-outline-overlay-data'."
7275 (outline-flag-region (car c
) (cdr c
) t
))
7278 ;;; Folding of blocks
7280 (defvar org-hide-block-overlays nil
7281 "Overlays hiding blocks.")
7282 (make-variable-buffer-local 'org-hide-block-overlays
)
7284 (defun org-block-map (function &optional start end
)
7285 "Call FUNCTION at the head of all source blocks in the current buffer.
7286 Optional arguments START and END can be used to limit the range."
7287 (let ((start (or start
(point-min)))
7288 (end (or end
(point-max))))
7291 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
7294 (goto-char (match-beginning 0))
7295 (funcall function
)))))))
7297 (defun org-hide-block-toggle-all ()
7298 "Toggle the visibility of all blocks in the current buffer."
7299 (org-block-map 'org-hide-block-toggle
))
7301 (defun org-hide-block-all ()
7302 "Fold all blocks in the current buffer."
7304 (org-show-block-all)
7305 (org-block-map 'org-hide-block-toggle-maybe
))
7307 (defun org-show-block-all ()
7308 "Unfold all blocks in the current buffer."
7310 (mapc 'delete-overlay org-hide-block-overlays
)
7311 (setq org-hide-block-overlays nil
))
7313 (defun org-hide-block-toggle-maybe ()
7314 "Toggle visibility of block at point."
7316 (let ((case-fold-search t
))
7318 (beginning-of-line 1)
7319 (looking-at org-block-regexp
))
7320 (progn (org-hide-block-toggle)
7321 t
) ;; to signal that we took action
7322 nil
))) ;; to signal that we did not
7324 (defun org-hide-block-toggle (&optional force
)
7325 "Toggle the visibility of the current block.
7326 When optional argument FORCE is `off', make block visible. If it
7327 is non-nil, hide it unconditionally."
7329 (let ((element (org-element-at-point)))
7330 (unless (memq (org-element-type element
)
7331 '(center-block comment-block example-block quote-block
7332 src-block verse-block
))
7333 (user-error "Not at a block"))
7334 (let* ((start (save-excursion
7335 (goto-char (org-element-property :post-affiliated element
))
7336 (line-end-position)))
7337 (end (save-excursion
7338 (goto-char (org-element-property :end element
))
7339 (skip-chars-backward " \r\t\n")
7340 (line-end-position)))
7341 (overlays (overlays-at start
)))
7343 ;; Do nothing when not before or at the block opening line or
7344 ;; at the block closing line.
7345 ((let ((eol (line-end-position)))
7346 (and (> eol start
) (/= eol end
))))
7347 ((and (not (eq force
'off
))
7348 (not (memq t
(mapcar
7350 (eq (overlay-get o
'invisible
) 'org-hide-block
))
7352 (let ((ov (make-overlay start end
)))
7353 (overlay-put ov
'invisible
'org-hide-block
)
7354 ;; Make the block accessible to `isearch'.
7356 ov
'isearch-open-invisible
7358 (when (memq ov org-hide-block-overlays
)
7359 (setq org-hide-block-overlays
(delq ov org-hide-block-overlays
)))
7360 (when (eq (overlay-get ov
'invisible
) 'org-hide-block
)
7361 (delete-overlay ov
))))
7362 (push ov org-hide-block-overlays
)
7363 ;; When the block is hidden away, make sure point is left in
7364 ;; a visible part of the buffer.
7365 (when (> (line-beginning-position) start
)
7367 (beginning-of-line))))
7368 ((or (not force
) (eq force
'off
))
7369 (dolist (ov overlays
)
7370 (when (memq ov org-hide-block-overlays
)
7371 (setq org-hide-block-overlays
(delq ov org-hide-block-overlays
)))
7372 (when (eq (overlay-get ov
'invisible
) 'org-hide-block
)
7373 (delete-overlay ov
))))))))
7375 ;; org-tab-after-check-for-cycling-hook
7376 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
7377 ;; Remove overlays when changing major mode
7378 (add-hook 'org-mode-hook
7379 (lambda () (org-add-hook 'change-major-mode-hook
7380 'org-show-block-all
'append
'local
)))
7384 (defvar org-goto-window-configuration nil
)
7385 (defvar org-goto-marker nil
)
7386 (defvar org-goto-map
)
7387 (defun org-goto-map ()
7388 "Set the keymap `org-goto'."
7390 (let ((map (make-sparse-keymap)))
7391 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
7392 mouse-drag-region universal-argument org-occur
))
7394 (while (setq cmd
(pop cmds
))
7395 (substitute-key-definition cmd cmd map global-map
)))
7396 (suppress-keymap map
)
7397 (org-defkey map
"\C-m" 'org-goto-ret
)
7398 (org-defkey map
[(return)] 'org-goto-ret
)
7399 (org-defkey map
[(left)] 'org-goto-left
)
7400 (org-defkey map
[(right)] 'org-goto-right
)
7401 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
7402 (org-defkey map
"\C-i" 'org-cycle
)
7403 (org-defkey map
[(tab)] 'org-cycle
)
7404 (org-defkey map
[(down)] 'outline-next-visible-heading
)
7405 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
7406 (if org-goto-auto-isearch
7407 (if (fboundp 'define-key-after
)
7408 (define-key-after map
[t] 'org-goto-local-auto-isearch)
7410 (org-defkey map "q" 'org-goto-quit)
7411 (org-defkey map "n" 'outline-next-visible-heading)
7412 (org-defkey map "p" 'outline-previous-visible-heading)
7413 (org-defkey map "f" 'outline-forward-same-level)
7414 (org-defkey map "b" 'outline-backward-same-level)
7415 (org-defkey map "u" 'outline-up-heading))
7416 (org-defkey map "/" 'org-occur)
7417 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
7418 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
7419 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
7420 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
7421 (org-defkey map "\C-c\C-u" 'outline-up-heading)
7424 (defconst org-goto-help
7425 "Browse buffer copy, to find location or copy text.%s
7426 RET=jump to location C-g=quit and return to previous location
7427 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
7429 (defvar org-goto-start-pos) ; dynamically scoped parameter
7431 (defun org-goto (&optional alternative-interface)
7432 "Look up a different location in the current file, keeping current visibility.
7434 When you want look-up or go to a different location in a
7435 document, the fastest way is often to fold the entire buffer and
7436 then dive into the tree. This method has the disadvantage, that
7437 the previous location will be folded, which may not be what you
7440 This command works around this by showing a copy of the current
7441 buffer in an indirect buffer, in overview mode. You can dive
7442 into the tree in that copy, use org-occur and incremental search
7443 to find a location. When pressing RET or `Q', the command
7444 returns to the original buffer in which the visibility is still
7445 unchanged. After RET it will also jump to the location selected
7446 in the indirect buffer and expose the headline hierarchy above.
7448 With a prefix argument, use the alternative interface: e.g. if
7449 `org-goto-interface' is 'outline use 'outline-path-completion."
7452 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
7453 (org-refile-use-outline-path t)
7454 (org-refile-target-verify-function nil)
7456 (if (not alternative-interface)
7458 (if (eq org-goto-interface 'outline)
7459 'outline-path-completion
7461 (org-goto-start-pos (point))
7463 (if (eq interface 'outline)
7464 (car (org-get-location (current-buffer) org-goto-help))
7465 (let ((pa (org-refile-get-location "Goto" nil nil t)))
7466 (org-refile-check-position pa)
7470 (org-mark-ring-push org-goto-start-pos)
7471 (goto-char selected-point)
7472 (if (or (outline-invisible-p) (org-invisible-p2))
7473 (org-show-context 'org-goto)))
7476 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
7477 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
7478 (defvar org-goto-local-auto-isearch-map) ; defined below
7480 (defun org-get-location (buf help)
7481 "Let the user select a location in the Org-mode buffer BUF.
7482 This function uses a recursive edit. It returns the selected position
7485 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
7486 (isearch-hide-immediately nil)
7487 (isearch-search-fun-function
7488 (lambda () 'org-goto-local-search-headings))
7489 (org-goto-selected-point org-goto-exit-command))
7491 (save-window-excursion
7492 (delete-other-windows)
7493 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
7494 (org-pop-to-buffer-same-window
7496 (make-indirect-buffer (current-buffer) "*org-goto*")
7497 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
7498 (with-output-to-temp-buffer "*Org Help*"
7499 (princ (format help (if org-goto-auto-isearch
7500 " Just type for auto-isearch."
7501 " n/p/f/b/u to navigate, q to quit."))))
7502 (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
7503 (setq buffer-read-only nil)
7504 (let ((org-startup-truncated t)
7505 (org-startup-folded nil)
7506 (org-startup-align-all-tables nil))
7509 (setq buffer-read-only t)
7510 (if (and (boundp 'org-goto-start-pos)
7511 (integer-or-marker-p org-goto-start-pos))
7512 (let ((org-show-hierarchy-above t)
7513 (org-show-siblings t)
7514 (org-show-following-heading t))
7515 (goto-char org-goto-start-pos)
7516 (and (outline-invisible-p) (org-show-context)))
7517 (goto-char (point-min)))
7518 (let (org-special-ctrl-a/e) (org-beginning-of-line))
7519 (message "Select location and press RET")
7520 (use-local-map org-goto-map)
7522 (kill-buffer "*org-goto*")
7523 (cons org-goto-selected-point org-goto-exit-command))))
7525 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
7526 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
7527 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
7528 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
7530 (defun org-goto-local-search-headings (string bound noerror)
7531 "Search and make sure that any matches are in headlines."
7533 (while (if isearch-forward
7534 (search-forward string bound noerror)
7535 (search-backward string bound noerror))
7536 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
7537 (and (member :headline context)
7538 (not (member :tags context))))
7539 (throw 'return (point))))))
7541 (defun org-goto-local-auto-isearch ()
7544 (goto-char (point-min))
7545 (let ((keys (this-command-keys)))
7546 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
7548 (isearch-process-search-char (string-to-char keys)))))
7550 (defun org-goto-ret (&optional arg)
7551 "Finish `org-goto' by going to the new location."
7553 (setq org-goto-selected-point (point)
7554 org-goto-exit-command 'return)
7557 (defun org-goto-left ()
7558 "Finish `org-goto' by going to the new location."
7560 (if (org-at-heading-p)
7562 (beginning-of-line 1)
7563 (setq org-goto-selected-point (point)
7564 org-goto-exit-command 'left)
7566 (user-error "Not on a heading")))
7568 (defun org-goto-right ()
7569 "Finish `org-goto' by going to the new location."
7571 (if (org-at-heading-p)
7573 (setq org-goto-selected-point (point)
7574 org-goto-exit-command 'right)
7576 (user-error "Not on a heading")))
7578 (defun org-goto-quit ()
7579 "Finish `org-goto' without cursor motion."
7581 (setq org-goto-selected-point nil)
7582 (setq org-goto-exit-command 'quit)
7585 ;;; Indirect buffer display of subtrees
7587 (defvar org-indirect-dedicated-frame nil
7588 "This is the frame being used for indirect tree display.")
7589 (defvar org-last-indirect-buffer nil)
7591 (defun org-tree-to-indirect-buffer (&optional arg)
7592 "Create indirect buffer and narrow it to current subtree.
7593 With a numerical prefix ARG, go up to this level and then take that tree.
7594 If ARG is negative, go up that many levels.
7596 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7597 indirect buffer previously made with this command, to avoid proliferation of
7598 indirect buffers. However, when you call the command with a \
7599 \\[universal-argument] prefix, or
7600 when `org-indirect-buffer-display' is `new-frame', the last buffer
7601 is kept so that you can work with several indirect buffers at the same time.
7602 If `org-indirect-buffer-display' is `dedicated-frame', the \
7603 \\[universal-argument] prefix also
7604 requests that a new frame be made for the new buffer, so that the dedicated
7605 frame is not changed."
7607 (let ((cbuf (current-buffer))
7608 (cwin (selected-window))
7610 beg end level heading ibuf)
7612 (org-back-to-heading t)
7614 (setq level (org-outline-level))
7615 (if (< arg 0) (setq arg (+ level arg)))
7616 (while (> (setq level (org-outline-level)) arg)
7617 (org-up-heading-safe)))
7619 heading (org-get-heading))
7620 (org-end-of-subtree t t)
7621 (if (org-at-heading-p) (backward-char 1))
7623 (if (and (buffer-live-p org-last-indirect-buffer)
7624 (not (eq org-indirect-buffer-display 'new-frame))
7626 (kill-buffer org-last-indirect-buffer))
7627 (setq ibuf (org-get-indirect-buffer cbuf heading)
7628 org-last-indirect-buffer ibuf)
7630 ((or (eq org-indirect-buffer-display 'new-frame)
7631 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7632 (select-frame (make-frame))
7633 (delete-other-windows)
7634 (org-pop-to-buffer-same-window ibuf)
7635 (org-set-frame-title heading))
7636 ((eq org-indirect-buffer-display 'dedicated-frame)
7638 (select-frame (or (and org-indirect-dedicated-frame
7639 (frame-live-p org-indirect-dedicated-frame)
7640 org-indirect-dedicated-frame)
7641 (setq org-indirect-dedicated-frame (make-frame)))))
7642 (delete-other-windows)
7643 (org-pop-to-buffer-same-window ibuf)
7644 (org-set-frame-title (concat "Indirect: " heading)))
7645 ((eq org-indirect-buffer-display 'current-window)
7646 (org-pop-to-buffer-same-window ibuf))
7647 ((eq org-indirect-buffer-display 'other-window)
7648 (pop-to-buffer ibuf))
7649 (t (error "Invalid value")))
7650 (if (featurep 'xemacs)
7651 (save-excursion (org-mode) (turn-on-font-lock)))
7652 (narrow-to-region beg end)
7655 (run-hook-with-args 'org-cycle-hook 'all)
7656 (and (window-live-p cwin) (select-window cwin))))
7658 (defun org-get-indirect-buffer (&optional buffer heading)
7659 (setq buffer (or buffer (current-buffer)))
7660 (let ((n 1) (base (buffer-name buffer)) bname)
7661 (while (buffer-live-p
7665 (if heading (concat heading "-" (number-to-string n))
7666 (number-to-string n))))))
7669 (make-indirect-buffer buffer bname 'clone)
7670 (error (make-indirect-buffer buffer bname)))))
7672 (defun org-set-frame-title (title)
7673 "Set the title of the current frame to the string TITLE."
7674 ;; FIXME: how to name a single frame in XEmacs???
7675 (unless (featurep 'xemacs)
7676 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7678 ;;;; Structure editing
7680 ;;; Inserting headlines
7682 (defun org-previous-line-empty-p (&optional next)
7683 "Is the previous line a blank line?
7684 When NEXT is non-nil, check the next line instead."
7687 (or (beginning-of-line (if next 2 0)) t)
7689 (looking-at "[ \t]*$")))))
7691 (defun org-insert-heading (&optional arg invisible-ok)
7692 "Insert a new heading or an item with the same depth at point.
7694 If point is at the beginning of a heading or a list item, insert
7695 a new heading or a new item above the current one. If point is
7696 at the beginning of a normal line, turn the line into a heading.
7698 If point is in the middle of a headline or a list item, split the
7699 headline or the item and create a new headline/item with the text
7700 in the current line after point \(see `org-M-RET-may-split-line'
7701 on how to modify this behavior).
7703 With one universal prefix argument, set the user option
7704 `org-insert-heading-respect-content' to t for the duration of
7705 the command. This modifies the behavior described above in this
7706 ways: on list items and at the beginning of normal lines, force
7707 the insertion of a heading after the current subtree.
7709 With two universal prefix arguments, insert the heading at the
7710 end of the grandparent subtree. For example, if point is within
7711 a 2nd-level heading, then it will insert a 2nd-level heading at
7712 the end of the 1st-level parent heading.
7714 If point is at the beginning of a headline, insert a sibling
7715 before the current headline. If point is not at the beginning,
7716 split the line and create a new headline with the text in the
7717 current line after point \(see `org-M-RET-may-split-line' on how
7718 to modify this behavior).
7720 If point is at the beginning of a normal line, turn this line
7723 When INVISIBLE-OK is set, stop at invisible headlines when going
7724 back. This is important for non-interactive uses of the
7727 (if (org-called-interactively-p 'any) (org-reveal))
7728 (let ((itemp (org-in-item-p))
7729 (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
7730 (respect-content (or org-insert-heading-respect-content
7732 (initial-content "")
7733 (adjust-empty-lines t))
7737 ((or (= (buffer-size) 0)
7738 (and (not (save-excursion
7739 (and (ignore-errors (org-back-to-heading invisible-ok))
7740 (org-at-heading-p))))
7741 (or arg (not itemp))))
7742 ;; At beginning of buffer or so high up that only a heading
7744 (cond ((and (bolp) (not respect-content)) (insert "* "))
7745 ((not respect-content)
7746 (unless may-split (end-of-line))
7748 ((re-search-forward org-outline-regexp-bol nil t)
7752 (t (goto-char (point-max))
7754 (run-hooks 'org-insert-heading-hook))
7756 ((and itemp (not (member arg '((4) (16)))))
7761 ;; Maybe move at the end of the subtree
7762 (when (equal arg '(16))
7763 (org-up-heading-safe)
7764 (org-end-of-subtree t))
7769 (on-heading (org-at-heading-p))
7770 (empty-line-p (if on-heading
7771 (org-previous-line-empty-p)
7772 ;; We will decide later
7774 ;; Get a level string to fall back on
7776 (if (org-before-first-heading-p) "*"
7778 (org-back-to-heading t)
7779 (if (org-previous-line-empty-p) (setq empty-line-p t))
7780 (looking-at org-outline-regexp)
7781 (make-string (1- (length (match-string 0))) ?*))))
7786 (org-back-to-heading invisible-ok)
7787 (when (and (not on-heading)
7788 (featurep 'org-inlinetask)
7789 (integerp org-inlinetask-min-level)
7790 (>= (length (match-string 0))
7791 org-inlinetask-min-level))
7792 ;; Find a heading level before the inline task
7793 (while (and (setq level (org-up-heading-safe))
7794 (>= level org-inlinetask-min-level)))
7795 (if (org-at-heading-p)
7796 (org-back-to-heading invisible-ok)
7797 (error "This should not happen")))
7798 (unless (and (save-excursion
7800 (org-backward-heading-same-level
7802 (= (point) (match-beginning 0)))
7803 (not (org-previous-line-empty-p t)))
7804 (setq empty-line-p (or empty-line-p
7805 (org-previous-line-empty-p))))
7807 (error (or fix-level "* ")))))
7808 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7809 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7810 pos hide-previous previous-pos)
7812 ;; If we insert after content, move there and clean up whitespace
7813 (when (and respect-content
7814 (not (org-looking-at-p org-outline-regexp-bol)))
7815 (if (not (org-before-first-heading-p))
7816 (org-end-of-subtree nil t)
7817 (re-search-forward org-outline-regexp-bol)
7818 (beginning-of-line 0))
7819 (skip-chars-backward " \r\n")
7820 (and (not (looking-back "^\\*+"))
7821 (looking-at "[ \t]+") (replace-match ""))
7822 (unless (eobp) (forward-char 1))
7823 (when (looking-at "^\\*")
7824 (unless (bobp) (backward-char 1))
7827 ;; If we are splitting, grab the text that should be moved to the new headline
7829 (if (org-on-heading-p)
7830 ;; This is a heading, we split intelligently (keeping tags)
7831 (let ((pos (point)))
7832 (goto-char (point-at-bol))
7833 (unless (looking-at org-complex-heading-regexp)
7834 (error "This should not happen"))
7835 (when (and (match-beginning 4)
7836 (> pos (match-beginning 4))
7837 (< pos (match-end 4)))
7838 (setq initial-content (buffer-substring pos (match-end 4)))
7840 (delete-region (point) (match-end 4))
7841 (if (looking-at "[ \t]*$")
7843 (insert (make-string (length initial-content) ?\ )))
7844 (setq initial-content (org-trim initial-content)))
7847 (setq initial-content
7848 (org-trim (buffer-substring (point) (point-at-eol))))
7849 (delete-region (point) (point-at-eol))))
7851 ;; If we are at the beginning of the line, insert before it. Else after
7853 ((and (bolp) (looking-at "[ \t]*$")))
7854 ((and (bolp) (not (looking-at "[ \t]*$")))
7857 (goto-char (point-at-eol))
7860 ;; Insert the new heading
7863 (insert initial-content)
7864 (when adjust-empty-lines
7866 (and blank (not (org-previous-line-empty-p))))
7867 (org-N-empty-lines-before-current (if blank 1 0))))
7868 (run-hooks 'org-insert-heading-hook)))))))
7870 (defun org-N-empty-lines-before-current (N)
7871 "Make the number of empty lines before current exactly N.
7872 So this will delete or add empty lines."
7874 (goto-char (point-at-bol))
7875 (if (looking-back "\\s-+" nil 'greedy)
7877 (or (bobp) (insert "\n"))
7882 (defun org-get-heading (&optional no-tags no-todo)
7883 "Return the heading of the current entry, without the stars.
7884 When NO-TAGS is non-nil, don't include tags.
7885 When NO-TODO is non-nil, don't include TODO keywords."
7887 (org-back-to-heading t)
7889 ((and no-tags no-todo)
7890 (looking-at org-complex-heading-regexp)
7893 (looking-at (concat org-outline-regexp
7895 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7898 (looking-at org-todo-line-regexp)
7900 (t (looking-at org-heading-regexp)
7901 (match-string 2)))))
7903 (defvar orgstruct-mode) ; defined below
7905 (defun org-heading-components ()
7906 "Return the components of the current heading.
7907 This is a list with the following elements:
7908 - the level as an integer
7909 - the reduced level, different if `org-odd-levels-only' is set.
7910 - the TODO keyword, or nil
7911 - the priority character, like ?A, or nil if no priority is given
7912 - the headline text itself, or the tags string if no headline text
7913 - the tags string, or nil."
7915 (org-back-to-heading t)
7916 (if (let (case-fold-search)
7920 org-complex-heading-regexp)))
7922 (list (length (match-string 1))
7923 (org-reduced-level (length (match-string 1)))
7928 (list (length (match-string 1))
7929 (org-reduced-level (length (match-string 1)))
7930 (org-match-string-no-properties 2)
7931 (and (match-end 3) (aref (match-string 3) 2))
7932 (org-match-string-no-properties 4)
7933 (org-match-string-no-properties 5))))))
7935 (defun org-get-entry ()
7936 "Get the entry text, after heading, entire subtree."
7938 (org-back-to-heading t)
7939 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7941 (defun org-insert-heading-after-current ()
7942 "Insert a new heading with same level as current, after current subtree."
7944 (org-back-to-heading)
7945 (org-insert-heading)
7946 (org-move-subtree-down)
7949 (defun org-insert-heading-respect-content (&optional invisible-ok)
7950 "Insert heading with `org-insert-heading-respect-content' set to t."
7952 (org-insert-heading '(4) invisible-ok))
7954 (defun org-insert-todo-heading-respect-content (&optional force-state)
7955 "Insert TODO heading with `org-insert-heading-respect-content' set to t."
7957 (org-insert-todo-heading force-state '(4)))
7959 (defun org-insert-todo-heading (arg &optional force-heading)
7960 "Insert a new heading with the same level and TODO state as current heading.
7961 If the heading has no TODO state, or if the state is DONE, use the first
7962 state (TODO by default). Also with one prefix arg, force first state. With
7963 two prefix args, force inserting at the end of the parent subtree."
7965 (when (or force-heading (not (org-insert-item 'checkbox)))
7966 (org-insert-heading (or (and (equal arg '(16)) '(16))
7969 (org-back-to-heading)
7970 (outline-previous-heading)
7971 (looking-at org-todo-line-regexp))
7974 (if (or (equal arg '(4))
7975 (not (match-beginning 2))
7976 (member (match-string 2) org-done-keywords))
7977 (car org-todo-keywords-1)
7981 (run-hook-with-args-until-success
7982 'org-todo-get-default-hook new-mark-x nil)
7984 (beginning-of-line 1)
7985 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7986 (if org-treat-insert-todo-heading-as-state-change
7988 (insert new-mark " "))))
7989 (when org-provide-todo-statistics
7990 (org-update-parent-todo-statistics))))
7992 (defun org-insert-subheading (arg)
7993 "Insert a new subheading and demote it.
7994 Works for outline headings and for plain lists alike."
7996 (org-insert-heading arg)
7998 ((org-at-heading-p) (org-do-demote))
7999 ((org-at-item-p) (org-indent-item))))
8001 (defun org-insert-todo-subheading (arg)
8002 "Insert a new subheading with TODO keyword or checkbox and demote it.
8003 Works for outline headings and for plain lists alike."
8005 (org-insert-todo-heading arg)
8007 ((org-at-heading-p) (org-do-demote))
8008 ((org-at-item-p) (org-indent-item))))
8010 ;;; Promotion and Demotion
8012 (defvar org-after-demote-entry-hook nil
8013 "Hook run after an entry has been demoted.
8014 The cursor will be at the beginning of the entry.
8015 When a subtree is being demoted, the hook will be called for each node.")
8017 (defvar org-after-promote-entry-hook nil
8018 "Hook run after an entry has been promoted.
8019 The cursor will be at the beginning of the entry.
8020 When a subtree is being promoted, the hook will be called for each node.")
8022 (defun org-promote-subtree ()
8023 "Promote the entire subtree.
8024 See also `org-promote'."
8027 (org-with-limited-levels (org-map-tree 'org-promote)))
8028 (org-fix-position-after-promote))
8030 (defun org-demote-subtree ()
8031 "Demote the entire subtree. See `org-demote'.
8032 See also `org-promote'."
8035 (org-with-limited-levels (org-map-tree 'org-demote)))
8036 (org-fix-position-after-promote))
8039 (defun org-do-promote ()
8040 "Promote the current heading higher up the tree.
8041 If the region is active in `transient-mark-mode', promote all headings
8045 (if (org-region-active-p)
8046 (org-map-region 'org-promote (region-beginning) (region-end))
8048 (org-fix-position-after-promote))
8050 (defun org-do-demote ()
8051 "Demote the current heading lower down the tree.
8052 If the region is active in `transient-mark-mode', demote all headings
8056 (if (org-region-active-p)
8057 (org-map-region 'org-demote (region-beginning) (region-end))
8059 (org-fix-position-after-promote))
8061 (defun org-fix-position-after-promote ()
8062 "Make sure that after pro/demotion cursor position is right."
8063 (let ((pos (point)))
8064 (when (save-excursion
8065 (beginning-of-line 1)
8066 (looking-at org-todo-line-regexp)
8067 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
8068 (cond ((eobp) (insert " "))
8069 ((eolp) (insert " "))
8070 ((equal (char-after) ?\ ) (forward-char 1))))))
8072 (defun org-current-level ()
8073 "Return the level of the current entry, or nil if before the first headline.
8074 The level is the number of stars at the beginning of the headline."
8076 (org-with-limited-levels
8077 (if (ignore-errors (org-back-to-heading t))
8078 (funcall outline-level)))))
8080 (defun org-get-previous-line-level ()
8081 "Return the outline depth of the last headline before the current line.
8082 Returns 0 for the first headline in the buffer, and nil if before the
8084 (and (org-current-level)
8085 (or (and (/= (line-beginning-position) (point-min))
8086 (save-excursion (beginning-of-line 0) (org-current-level)))
8089 (defun org-reduced-level (l)
8090 "Compute the effective level of a heading.
8091 This takes into account the setting of `org-odd-levels-only'."
8094 (org-odd-levels-only (1+ (floor (/ l 2))))
8097 (defun org-level-increment ()
8098 "Return the number of stars that will be added or removed at a
8099 time to headlines when structure editing, based on the value of
8100 `org-odd-levels-only'."
8101 (if org-odd-levels-only 2 1))
8103 (defun org-get-valid-level (level &optional change)
8104 "Rectify a level change under the influence of `org-odd-levels-only'
8105 LEVEL is a current level, CHANGE is by how much the level should be
8106 modified. Even if CHANGE is nil, LEVEL may be returned modified because
8107 even level numbers will become the next higher odd number."
8108 (if org-odd-levels-only
8109 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
8110 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
8111 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
8112 (max 1 (+ level (or change 0)))))
8114 (if (boundp 'define-obsolete-function-alias)
8115 (if (or (featurep 'xemacs) (< emacs-major-version 23))
8116 (define-obsolete-function-alias 'org-get-legal-level
8117 'org-get-valid-level)
8118 (define-obsolete-function-alias 'org-get-legal-level
8119 'org-get-valid-level "23.1")))
8121 (defun org-promote ()
8122 "Promote the current heading higher up the tree.
8123 If the region is active in `transient-mark-mode', promote all headings
8125 (org-back-to-heading t)
8126 (let* ((level (save-match-data (funcall outline-level)))
8127 (after-change-functions (remove 'flyspell-after-change-function
8128 after-change-functions))
8129 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
8130 (diff (abs (- level (length up-head) -1))))
8131 (cond ((and (= level 1) org-called-with-limited-levels
8132 org-allow-promoting-top-level-subtree)
8133 (replace-match "# " nil t))
8135 (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
8136 (t (replace-match up-head nil t)))
8137 ;; Fixup tag positioning
8139 (and org-auto-align-tags (org-set-tags nil 'ignore-column))
8140 (if org-adapt-indentation (org-fixup-indentation (- diff))))
8141 (run-hooks 'org-after-promote-entry-hook)))
8143 (defun org-demote ()
8144 "Demote the current heading lower down the tree.
8145 If the region is active in `transient-mark-mode', demote all headings
8147 (org-back-to-heading t)
8148 (let* ((level (save-match-data (funcall outline-level)))
8149 (after-change-functions (remove 'flyspell-after-change-function
8150 after-change-functions))
8151 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
8152 (diff (abs (- level (length down-head) -1))))
8153 (replace-match down-head nil t)
8154 ;; Fixup tag positioning
8155 (and org-auto-align-tags (org-set-tags nil 'ignore-column))
8156 (if org-adapt-indentation (org-fixup-indentation diff))
8157 (run-hooks 'org-after-demote-entry-hook)))
8159 (defun org-cycle-level ()
8160 "Cycle the level of an empty headline through possible states.
8161 This goes first to child, then to parent, level, then up the hierarchy.
8162 After top level, it switches back to sibling level."
8164 (let ((org-adapt-indentation nil))
8165 (when (org-point-at-end-of-empty-headline)
8166 (setq this-command 'org-cycle-level) ; Only needed for caching
8167 (let ((cur-level (org-current-level))
8168 (prev-level (org-get-previous-line-level)))
8170 ;; If first headline in file, promote to top-level.
8172 (loop repeat (/ (- cur-level 1) (org-level-increment))
8173 do (org-do-promote)))
8174 ;; If same level as prev, demote one.
8175 ((= prev-level cur-level)
8177 ;; If parent is top-level, promote to top level if not already.
8179 (loop repeat (/ (- cur-level 1) (org-level-increment))
8180 do (org-do-promote)))
8181 ;; If top-level, return to prev-level.
8183 (loop repeat (/ (- prev-level 1) (org-level-increment))
8184 do (org-do-demote)))
8185 ;; If less than prev-level, promote one.
8186 ((< cur-level prev-level)
8188 ;; If deeper than prev-level, promote until higher than
8190 ((> cur-level prev-level)
8191 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
8192 do (org-do-promote))))
8195 (defun org-map-tree (fun)
8196 "Call FUN for every heading underneath the current one."
8197 (org-back-to-heading)
8198 (let ((level (funcall outline-level)))
8202 (outline-next-heading)
8203 (> (funcall outline-level) level))
8207 (defun org-map-region (fun beg end)
8208 "Call FUN for every heading between BEG and END."
8209 (let ((org-ignore-region t))
8211 (setq end (copy-marker end))
8213 (if (and (re-search-forward org-outline-regexp-bol nil t)
8217 (outline-next-heading)
8222 (defvar org-property-end-re) ; silence byte-compiler
8223 (defun org-fixup-indentation (diff)
8224 "Change the indentation in the current entry by DIFF.
8225 However, if any line in the current entry has no indentation, or if it
8226 would end up with no indentation after the change, nothing at all is done."
8228 (let ((end (save-excursion (outline-next-heading)
8230 (prohibit (if (> diff 0)
8232 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
8234 (unless (save-excursion (end-of-line 1)
8235 (re-search-forward prohibit end t))
8236 (while (and (< (point) end)
8237 (re-search-forward "^[ \t]+" end t))
8238 (goto-char (match-end 0))
8239 (setq col (current-column))
8240 (if (< diff 0) (replace-match ""))
8241 (org-indent-to-column (+ diff col))))
8242 (move-marker end nil))))
8244 (defun org-convert-to-odd-levels ()
8245 "Convert an org-mode file with all levels allowed to one with odd levels.
8246 This will leave level 1 alone, convert level 2 to level 3, level 3 to
8249 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
8250 (let ((outline-level 'org-outline-level)
8251 (org-odd-levels-only nil) n)
8253 (goto-char (point-min))
8254 (while (re-search-forward "^\\*\\*+ " nil t)
8255 (setq n (- (length (match-string 0)) 2))
8256 (while (>= (setq n (1- n)) 0)
8258 (end-of-line 1))))))
8260 (defun org-convert-to-oddeven-levels ()
8261 "Convert an org-mode file with only odd levels to one with odd/even levels.
8262 This promotes level 3 to level 2, level 5 to level 3 etc. If the
8263 file contains a section with an even level, conversion would
8264 destroy the structure of the file. An error is signaled in this
8267 (goto-char (point-min))
8268 ;; First check if there are no even levels
8269 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
8270 (org-show-context t)
8271 (error "Not all levels are odd in this file. Conversion not possible"))
8272 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
8273 (let ((outline-regexp org-outline-regexp)
8274 (outline-level 'org-outline-level)
8275 (org-odd-levels-only nil) n)
8277 (goto-char (point-min))
8278 (while (re-search-forward "^\\*\\*+ " nil t)
8279 (setq n (/ (1- (length (match-string 0))) 2))
8280 (while (>= (setq n (1- n)) 0)
8282 (end-of-line 1))))))
8284 (defun org-tr-level (n)
8285 "Make N odd if required."
8286 (if org-odd-levels-only (1+ (/ n 2)) n))
8288 ;;; Vertical tree motion, cutting and pasting of subtrees
8290 (defun org-move-subtree-up (&optional arg)
8291 "Move the current subtree up past ARG headlines of the same level."
8293 (org-move-subtree-down (- (prefix-numeric-value arg))))
8295 (defun org-move-subtree-down (&optional arg)
8296 "Move the current subtree down past ARG headlines of the same level."
8298 (setq arg (prefix-numeric-value arg))
8299 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
8300 'org-get-last-sibling))
8301 (ins-point (make-marker))
8303 (col (current-column))
8304 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
8306 (org-back-to-heading)
8309 (setq ne-beg (org-back-over-empty-lines))
8312 (save-excursion (outline-end-of-heading)
8313 (setq folded (outline-invisible-p)))
8314 (progn (org-end-of-subtree nil t)
8315 (unless (eobp) (backward-char))))
8316 (outline-next-heading)
8317 (setq ne-end (org-back-over-empty-lines))
8320 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
8321 ;; include less whitespace
8324 (forward-line (- ne-beg ne-end))
8325 (setq beg (point))))
8326 ;; Find insertion point, with error handling
8328 (or (and (funcall movfunc) (looking-at org-outline-regexp))
8329 (progn (goto-char beg0)
8330 (user-error "Cannot move past superior level or buffer limit")))
8331 (setq cnt (1- cnt)))
8333 ;; Moving forward - still need to move over subtree
8334 (progn (org-end-of-subtree t t)
8336 (org-back-over-empty-lines)
8337 (or (bolp) (newline)))))
8338 (setq ne-ins (org-back-over-empty-lines))
8339 (move-marker ins-point (point))
8340 (setq txt (buffer-substring beg end))
8341 (org-save-markers-in-region beg end)
8342 (delete-region beg end)
8343 (org-remove-empty-overlays-at beg)
8344 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
8345 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
8346 (and (not (bolp)) (looking-at "\n") (forward-char 1))
8347 (let ((bbb (point)))
8348 (insert-before-markers txt)
8349 (org-reinstall-markers-in-region bbb)
8350 (move-marker ins-point bbb))
8351 (or (bolp) (insert "\n"))
8352 (setq ins-end (point))
8353 (goto-char ins-point)
8354 (org-skip-whitespace)
8355 (when (and (< arg 0)
8356 (org-first-sibling-p)
8358 ;; Move whitespace back to beginning
8361 (let ((kill-whole-line t))
8362 (kill-line (- ne-ins ne-beg)) (point)))
8363 (insert (make-string (- ne-ins ne-beg) ?\n)))
8364 (move-marker ins-point nil)
8369 (org-cycle-hide-drawers 'children))
8370 (org-clean-visibility-after-subtree-move)
8371 ;; move back to the initial column we were at
8372 (move-to-column col)))
8374 (defvar org-subtree-clip ""
8375 "Clipboard for cut and paste of subtrees.
8376 This is actually only a copy of the kill, because we use the normal kill
8377 ring. We need it to check if the kill was created by `org-copy-subtree'.")
8379 (defvar org-subtree-clip-folded nil
8380 "Was the last copied subtree folded?
8381 This is used to fold the tree back after pasting.")
8383 (defun org-cut-subtree (&optional n)
8384 "Cut the current subtree into the clipboard.
8385 With prefix arg N, cut this many sequential subtrees.
8386 This is a short-hand for marking the subtree and then cutting it."
8388 (org-copy-subtree n 'cut))
8390 (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
8391 "Copy the current subtree it in the clipboard.
8392 With prefix arg N, copy this many sequential subtrees.
8393 This is a short-hand for marking the subtree and then copying it.
8394 If CUT is non-nil, actually cut the subtree.
8395 If FORCE-STORE-MARKERS is non-nil, store the relative locations
8396 of some markers in the region, even if CUT is non-nil. This is
8397 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
8399 (let (beg end folded (beg0 (point)))
8400 (if (org-called-interactively-p 'any)
8401 (org-back-to-heading nil) ; take what looks like a subtree
8402 (org-back-to-heading t)) ; take what is really there
8404 (skip-chars-forward " \t\r\n")
8407 (outline-next-heading)
8408 (save-excursion (outline-end-of-heading)
8409 (setq folded (outline-invisible-p)))
8410 (ignore-errors (org-forward-heading-same-level (1- n) t))
8411 (org-end-of-subtree t t)))
8415 (setq org-subtree-clip-folded folded)
8416 (when (or cut force-store-markers)
8417 (org-save-markers-in-region beg end))
8418 (if cut (kill-region beg end) (copy-region-as-kill beg end))
8419 (setq org-subtree-clip (current-kill 0))
8420 (message "%s: Subtree(s) with %d characters"
8421 (if cut "Cut" "Copied")
8422 (length org-subtree-clip)))))
8424 (defun org-paste-subtree (&optional level tree for-yank remove)
8425 "Paste the clipboard as a subtree, with modification of headline level.
8426 The entire subtree is promoted or demoted in order to match a new headline
8429 If the cursor is at the beginning of a headline, the same level as
8430 that headline is used to paste the tree.
8432 If not, the new level is derived from the *visible* headings
8433 before and after the insertion point, and taken to be the inferior headline
8434 level of the two. So if the previous visible heading is level 3 and the
8435 next is level 4 (or vice versa), level 4 will be used for insertion.
8436 This makes sure that the subtree remains an independent subtree and does
8437 not swallow low level entries.
8439 You can also force a different level, either by using a numeric prefix
8440 argument, or by inserting the heading marker by hand. For example, if the
8441 cursor is after \"*****\", then the tree will be shifted to level 5.
8443 If optional TREE is given, use this text instead of the kill ring.
8445 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
8446 move back over whitespace before inserting, and move point to the end of
8447 the inserted text when done.
8449 When REMOVE is non-nil, remove the subtree from the clipboard."
8451 (setq tree (or tree (and kill-ring (current-kill 0))))
8452 (unless (org-kill-is-subtree-p tree)
8454 (substitute-command-keys
8455 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
8456 (org-with-limited-levels
8457 (let* ((visp (not (outline-invisible-p)))
8459 (^re_ "\\(\\*+\\)[ \t]*")
8460 (old-level (if (string-match org-outline-regexp-bol txt)
8461 (- (match-end 0) (match-beginning 0) 1)
8463 (force-level (cond (level (prefix-numeric-value level))
8464 ((and (looking-at "[ \t]*$")
8466 "^\\*+$" (buffer-substring
8467 (point-at-bol) (point))))
8468 (- (match-end 0) (match-beginning 0)))
8470 (looking-at org-outline-regexp))
8471 (- (match-end 0) (point) 1))))
8472 (previous-level (save-excursion
8475 (outline-previous-visible-heading 1)
8476 (if (looking-at ^re_)
8477 (- (match-end 0) (match-beginning 0) 1)
8480 (next-level (save-excursion
8483 (or (looking-at org-outline-regexp)
8484 (outline-next-visible-heading 1))
8485 (if (looking-at ^re_)
8486 (- (match-end 0) (match-beginning 0) 1)
8489 (new-level (or force-level (max previous-level next-level)))
8490 (shift (if (or (= old-level -1)
8492 (= old-level new-level))
8494 (- new-level old-level)))
8495 (delta (if (> shift 0) -1 1))
8496 (func (if (> shift 0) 'org-demote 'org-promote))
8497 (org-odd-levels-only nil)
8499 ;; Remove the forced level indicator
8501 (delete-region (point-at-bol) (point)))
8503 (beginning-of-line (if (bolp) 1 2))
8505 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
8506 (insert-before-markers txt)
8507 (unless (string-match "\n\\'" txt) (insert "\n"))
8508 (setq newend (point))
8509 (org-reinstall-markers-in-region beg)
8512 (skip-chars-forward " \t\n\r")
8514 (if (and (outline-invisible-p) visp)
8515 (save-excursion (outline-show-heading)))
8516 ;; Shift if necessary
8519 (narrow-to-region beg end)
8520 (while (not (= shift 0))
8521 (org-map-region func (point-min) (point-max))
8522 (setq shift (+ delta shift)))
8523 (goto-char (point-min))
8524 (setq newend (point-max))))
8525 (when (or (org-called-interactively-p 'interactive) for-yank)
8526 (message "Clipboard pasted as level %d subtree" new-level))
8527 (if (and (not for-yank) ; in this case, org-yank will decide about folding
8529 (eq org-subtree-clip (current-kill 0))
8530 org-subtree-clip-folded)
8531 ;; The tree was folded before it was killed/copied
8533 (and for-yank (goto-char newend))
8534 (and remove (setq kill-ring (cdr kill-ring))))))
8536 (defun org-kill-is-subtree-p (&optional txt)
8537 "Check if the current kill is an outline subtree, or a set of trees.
8538 Returns nil if kill does not start with a headline, or if the first
8539 headline level is not the largest headline level in the tree.
8540 So this will actually accept several entries of equal levels as well,
8541 which is OK for `org-paste-subtree'.
8542 If optional TXT is given, check this string instead of the current kill."
8543 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
8544 (re (org-get-limited-outline-regexp))
8545 (^re (concat "^" re))
8546 (start-level (and kill
8548 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
8550 (- (match-end 2) (match-beginning 2) 1)))
8551 (start (1+ (or (match-beginning 2) -1))))
8552 (if (not start-level)
8554 nil) ;; does not even start with a heading
8556 (while (setq start (string-match ^re kill (1+ start)))
8557 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
8561 (defvar org-markers-to-move nil
8562 "Markers that should be moved with a cut-and-paste operation.
8563 Those markers are stored together with their positions relative to
8564 the start of the region.")
8566 (defun org-save-markers-in-region (beg end)
8567 "Check markers in region.
8568 If these markers are between BEG and END, record their position relative
8569 to BEG, so that after moving the block of text, we can put the markers back
8571 This function gets called just before an entry or tree gets cut from the
8572 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
8573 called immediately, to move the markers with the entries."
8574 (setq org-markers-to-move nil)
8575 (when (featurep 'org-clock)
8576 (org-clock-save-markers-for-cut-and-paste beg end))
8577 (when (featurep 'org-agenda)
8578 (org-agenda-save-markers-for-cut-and-paste beg end)))
8580 (defun org-check-and-save-marker (marker beg end)
8581 "Check if MARKER is between BEG and END.
8582 If yes, remember the marker and the distance to BEG."
8583 (when (and (marker-buffer marker)
8584 (equal (marker-buffer marker) (current-buffer)))
8585 (if (and (>= marker beg) (< marker end))
8586 (push (cons marker (- marker beg)) org-markers-to-move))))
8588 (defun org-reinstall-markers-in-region (beg)
8589 "Move all remembered markers to their position relative to BEG."
8591 (move-marker (car x) (+ beg (cdr x))))
8592 org-markers-to-move)
8593 (setq org-markers-to-move nil))
8595 (defun org-narrow-to-subtree ()
8596 "Narrow buffer to the current subtree."
8600 (org-with-limited-levels
8602 (progn (org-back-to-heading t) (point))
8603 (progn (org-end-of-subtree t t)
8604 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
8607 (defun org-narrow-to-block ()
8608 "Narrow buffer to the current block."
8610 (let* ((case-fold-search t)
8611 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
8612 "^[ \t]*#\\+end_.*")))
8614 (narrow-to-region (car blockp) (cdr blockp))
8615 (user-error "Not in a block"))))
8618 (defvar org-property-drawer-re))
8620 (defvar org-property-start-re) ;; defined below
8621 (defun org-clone-subtree-with-time-shift (n &optional shift)
8622 "Clone the task (subtree) at point N times.
8623 The clones will be inserted as siblings.
8625 In interactive use, the user will be prompted for the number of
8626 clones to be produced. If the entry has a timestamp, the user
8627 will also be prompted for a time shift, which may be a repeater
8628 as used in time stamps, for example `+3d'. To disable this,
8629 you can call the function with a universal prefix argument.
8631 When a valid repeater is given and the entry contains any time
8632 stamps, the clones will become a sequence in time, with time
8633 stamps in the subtree shifted for each clone produced. If SHIFT
8634 is nil or the empty string, time stamps will be left alone. The
8635 ID property of the original subtree is removed.
8637 If the original subtree did contain time stamps with a repeater,
8638 the following will happen:
8639 - the repeater will be removed in each clone
8640 - an additional clone will be produced, with the current, unshifted
8641 date(s) in the entry.
8642 - the original entry will be placed *after* all the clones, with
8644 - the start days in the repeater in the original entry will be shifted
8645 to past the last clone.
8646 In this way you can spell out a number of instances of a repeating task,
8647 and still retain the repeater to cover future instances of the task."
8648 (interactive "nNumber of clones to produce: ")
8651 (if (and (not (equal current-prefix-arg '(4)))
8653 (re-search-forward org-ts-regexp-both
8655 (org-end-of-subtree t)
8657 (read-from-minibuffer
8658 "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
8659 ""))) ;; No time shift
8661 (drawer-re org-drawer-regexp)
8662 beg end template task idprop
8663 shift-n shift-what doshift nmin nmax)
8664 (if (not (and (integerp n) (> n 0)))
8665 (user-error "Invalid number of replications %s" n))
8666 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
8667 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
8669 (user-error "Invalid shift specification %s" shift))
8671 (setq shift-n (string-to-number (match-string 1 shift))
8672 shift-what (cdr (assoc (match-string 2 shift)
8673 '(("d" . day) ("w" . week)
8674 ("m" . month) ("y" . year))))))
8675 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
8676 (setq nmin 1 nmax n)
8677 (org-back-to-heading t)
8679 (setq idprop (org-entry-get nil "ID"))
8680 (org-end-of-subtree t t)
8681 (or (bolp) (insert "\n"))
8683 (setq template (buffer-substring beg end))
8685 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
8686 (delete-region beg end)
8688 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
8690 (loop for n from nmin to nmax do
8695 (goto-char (point-min))
8697 (and idprop (if org-clone-delete-id
8698 (org-entry-delete nil "ID")
8699 (org-id-get-create t)))
8701 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
8703 (goto-char (point-min))
8704 (while (re-search-forward drawer-re nil t)
8705 (org-remove-empty-drawer-at (point))))
8706 (goto-char (point-min))
8708 (while (re-search-forward org-ts-regexp-both nil t)
8709 (org-timestamp-change (* n shift-n) shift-what))
8710 (unless (= n n-no-remove)
8711 (goto-char (point-min))
8712 (while (re-search-forward org-ts-regexp nil t)
8714 (goto-char (match-beginning 0))
8715 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
8716 (delete-region (match-beginning 1) (match-end 1)))))))
8717 (setq task (buffer-string)))
8723 (defun org-sort (with-case)
8724 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8725 Optional argument WITH-CASE means sort case-sensitively."
8728 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8729 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8731 (org-call-with-arg 'org-sort-entries with-case))))
8733 (defun org-sort-remove-invisible (s)
8734 "Remove invisible links from string S."
8735 (remove-text-properties 0 (length s) org-rm-props s)
8736 (while (string-match org-bracket-link-regexp s)
8737 (setq s (replace-match (if (match-end 2)
8739 (match-string 1 s)) t t s)))
8740 (let ((st (format " %s " s)))
8741 (while (string-match org-emph-re st)
8742 (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
8743 (setq s (substring st 1 -1)))
8746 (defvar org-priority-regexp) ; defined later in the file
8748 (defvar org-after-sorting-entries-or-items-hook nil
8749 "Hook that is run after a bunch of entries or items have been sorted.
8750 When children are sorted, the cursor is in the parent line when this
8751 hook gets called. When a region or a plain list is sorted, the cursor
8752 will be in the first entry of the sorted region/list.")
8754 (defun org-sort-entries
8755 (&optional with-case sorting-type getkey-func compare-func property)
8756 "Sort entries on a certain level of an outline tree.
8757 If there is an active region, the entries in the region are sorted.
8758 Else, if the cursor is before the first entry, sort the top-level items.
8759 Else, the children of the entry at point are sorted.
8761 Sorting can be alphabetically, numerically, by date/time as given by
8762 a time stamp, by a property, by priority order, or by a custom function.
8764 The command prompts for the sorting type unless it has been given to the
8765 function through the SORTING-TYPE argument, which needs to be a character,
8766 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
8767 the precise meaning of each character:
8769 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8770 c By creation time, which is assumed to be the first inactive time stamp
8771 at the beginning of a line.
8772 d By deadline date/time.
8774 n Numerically, by converting the beginning of the entry/item to a number.
8775 o By order of TODO keywords.
8776 p By priority according to the cookie.
8777 r By the value of a property.
8778 s By scheduled date/time.
8779 t By date/time, either the first active time stamp in the entry, or, if
8780 none exist, by the first inactive one.
8782 Capital letters will reverse the sort order.
8784 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8785 called with point at the beginning of the record. It must return either
8786 a string or a number that should serve as the sorting key for that record.
8788 Comparing entries ignores case by default. However, with an optional argument
8789 WITH-CASE, the sorting considers case as well.
8791 Sorting is done against the visible part of the headlines, it ignores hidden
8794 When sorting is done, call `org-after-sorting-entries-or-items-hook'."
8796 (let ((case-func (if with-case 'identity 'downcase))
8798 ;; The clock marker is lost when using `sort-subr', let's
8799 ;; store the clocking string.
8800 (when (equal (marker-buffer org-clock-marker) (current-buffer))
8802 (goto-char org-clock-marker)
8803 (looking-back "^.*") (match-string-no-properties 0))))
8804 start beg end stars re re2
8806 ;; Find beginning and end of region to sort
8808 ((org-region-active-p)
8809 ;; we will sort the region
8810 (setq end (region-end)
8812 (goto-char (region-beginning))
8813 (if (not (org-at-heading-p)) (outline-next-heading))
8814 (setq start (point)))
8815 ((or (org-at-heading-p)
8816 (ignore-errors (progn (org-back-to-heading) t)))
8817 ;; we will sort the children of the current headline
8818 (org-back-to-heading)
8820 end (progn (org-end-of-subtree t t)
8821 (or (bolp) (insert "\n"))
8822 (when (>= (org-back-over-empty-lines) 1)
8828 (outline-next-heading))
8830 ;; we will sort the top-level entries in this file
8831 (goto-char (point-min))
8832 (or (org-at-heading-p) (outline-next-heading))
8833 (setq start (point))
8834 (goto-char (point-max))
8835 (beginning-of-line 1)
8836 (when (looking-at ".*?\\S-")
8837 ;; File ends in a non-white line
8840 (setq end (point-max))
8841 (setq what "top-level")
8846 (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
8848 (looking-at "\\(\\*+\\)")
8849 (setq stars (match-string 1)
8850 re (concat "^" (regexp-quote stars) " +")
8851 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8852 txt (buffer-substring beg end))
8853 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8854 (if (and (not (equal stars "*")) (string-match re2 txt))
8855 (user-error "Region to sort contains a level above the first entry"))
8857 (unless sorting-type
8859 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8860 [t]ime
[s]cheduled [d]eadline [c]reated cloc[k]ing
8861 A/N/P/R/O/F/T/S/D/C/K means reversed:"
8863 (setq sorting-type (read-char-exclusive))
8866 (and (= (downcase sorting-type) ?f)
8868 (org-icompleting-read "Sort using function: "
8869 obarray 'fboundp t nil nil))
8870 (setq getkey-func (intern getkey-func))))
8872 (and (= (downcase sorting-type) ?r)
8875 (org-icompleting-read "Property: "
8876 (mapcar 'list (org-buffer-property-keys t))
8879 (when (member sorting-type '(?k ?K)) (org-clock-sum))
8880 (message "Sorting entries...")
8883 (narrow-to-region start end)
8884 (let ((dcst (downcase sorting-type))
8885 (case-fold-search nil)
8886 (now (current-time)))
8888 (/= dcst sorting-type)
8889 ;; This function moves to the beginning character of the "record" to
8892 (if (re-search-forward re nil t)
8893 (goto-char (match-beginning 0))
8894 (goto-char (point-max))))
8895 ;; This function moves to the last character of the "record" being
8900 (outline-forward-same-level 1)
8902 (goto-char (point-max))))))
8903 ;; This function returns the value that gets sorted against.
8907 (if (looking-at org-complex-heading-regexp)
8908 (string-to-number (org-sort-remove-invisible (match-string 4)))
8911 (if (looking-at org-complex-heading-regexp)
8912 (funcall case-func (org-sort-remove-invisible (match-string 4)))
8915 (or (get-text-property (point) :org-clock-minutes) 0))
8917 (let ((end (save-excursion (outline-next-heading) (point))))
8918 (if (or (re-search-forward org-ts-regexp end t)
8919 (re-search-forward org-ts-regexp-both end t))
8920 (org-time-string-to-seconds (match-string 0))
8921 (org-float-time now))))
8923 (let ((end (save-excursion (outline-next-heading) (point))))
8924 (if (re-search-forward
8925 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8927 (org-time-string-to-seconds (match-string 0))
8928 (org-float-time now))))
8930 (let ((end (save-excursion (outline-next-heading) (point))))
8931 (if (re-search-forward org-scheduled-time-regexp end t)
8932 (org-time-string-to-seconds (match-string 1))
8933 (org-float-time now))))
8935 (let ((end (save-excursion (outline-next-heading) (point))))
8936 (if (re-search-forward org-deadline-time-regexp end t)
8937 (org-time-string-to-seconds (match-string 1))
8938 (org-float-time now))))
8940 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8941 (string-to-char (match-string 2))
8942 org-default-priority))
8944 (or (org-entry-get nil property) ""))
8946 (if (looking-at org-complex-heading-regexp)
8947 (let* ((m (match-string 2))
8948 (s (if (member m org-done-keywords) '- '+)))
8949 (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
8953 (setq tmp (funcall getkey-func))
8954 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8956 (error "Invalid key function `%s'" getkey-func)))
8957 (t (error "Invalid sorting type `%c'" sorting-type))))
8960 ((= dcst ?a) 'string<)
8961 ((= dcst ?f) compare-func)
8962 ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
8963 (run-hooks 'org-after-sorting-entries-or-items-hook)
8964 ;; Reset the clock marker if needed
8968 (search-forward cmstr nil t)
8969 (move-marker org-clock-marker (point))))
8970 (message "Sorting entries...done")))
8972 (defun org-do-sort (table what &optional with-case sorting-type)
8973 "Sort TABLE of WHAT according to SORTING-TYPE.
8974 The user will be prompted for the SORTING-TYPE if the call to this
8975 function does not specify it.
8976 WHAT is only for the prompt, to indicate what is being sorted.
8977 The sorting key will be extracted from the car of the elements of
8979 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8980 (unless sorting-type
8982 "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
8984 (setq sorting-type (read-char-exclusive)))
8985 (let ((dcst (downcase sorting-type))
8986 extractfun comparefun)
8987 ;; Define the appropriate functions
8990 (setq extractfun 'string-to-number
8991 comparefun (if (= dcst sorting-type) '< '>)))
8993 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8994 (lambda(x) (downcase (org-sort-remove-invisible x))))
8995 comparefun (if (= dcst sorting-type)
8997 (lambda (a b) (and (not (string< a b))
8998 (not (string= a b)))))))
9002 (cond ((or (string-match org-ts-regexp x)
9003 (string-match org-ts-regexp-both x))
9005 (org-time-string-to-time (match-string 0 x))))
9006 ((string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" x)
9007 (org-hh:mm-string-to-minutes x))
9009 comparefun (if (= dcst sorting-type) '< '>)))
9010 (t (error "Invalid sorting type `%c'" sorting-type)))
9012 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
9014 (lambda (a b) (funcall comparefun (car a) (car b))))))
9017 ;;; The orgstruct minor mode
9019 ;; Define a minor mode which can be used in other modes in order to
9020 ;; integrate the org-mode structure editing commands.
9022 ;; This is really a hack, because the org-mode structure commands use
9023 ;; keys which normally belong to the major mode. Here is how it
9024 ;; works: The minor mode defines all the keys necessary to operate the
9025 ;; structure commands, but wraps the commands into a function which
9026 ;; tests if the cursor is currently at a headline or a plain list
9027 ;; item. If that is the case, the structure command is used,
9028 ;; temporarily setting many Org-mode variables like regular
9029 ;; expressions for filling etc. However, when any of those keys is
9030 ;; used at a different location, function uses `key-binding' to look
9031 ;; up if the key has an associated command in another currently active
9032 ;; keymap (minor modes, major mode, global), and executes that
9033 ;; command. There might be problems if any of the keys is otherwise
9034 ;; used as a prefix key.
9036 (defcustom orgstruct-heading-prefix-regexp ""
9037 "Regexp that matches the custom prefix of Org headlines in
9038 orgstruct(++)-mode."
9041 :package-version '(Org . "8.3")
9043 ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
9045 (defcustom orgstruct-setup-hook nil
9046 "Hook run after orgstruct-mode-map is filled."
9049 :package-version '(Org . "8.0")
9052 (defvar orgstruct-initialized nil)
9054 (defvar org-local-vars nil
9055 "List of local variables, for use by `orgstruct-mode'.")
9058 (define-minor-mode orgstruct-mode
9059 "Toggle the minor mode `orgstruct-mode'.
9060 This mode is for using Org-mode structure commands in other
9061 modes. The following keys behave as if Org-mode were active, if
9062 the cursor is on a headline, or on a plain list item (both as
9063 defined by Org-mode)."
9064 nil " OrgStruct" (make-sparse-keymap)
9065 (funcall (if orgstruct-mode
9066 'add-to-invisibility-spec
9067 'remove-from-invisibility-spec)
9069 (when orgstruct-mode
9070 (org-load-modules-maybe)
9071 (unless orgstruct-initialized
9073 (setq orgstruct-initialized t))))
9076 (defun turn-on-orgstruct ()
9077 "Unconditionally turn on `orgstruct-mode'."
9080 (defvar org-fb-vars nil)
9081 (make-variable-buffer-local 'org-fb-vars)
9082 (defun orgstruct++-mode (&optional arg)
9083 "Toggle `orgstruct-mode', the enhanced version of it.
9084 In addition to setting orgstruct-mode, this also exports all
9085 indentation and autofilling variables from org-mode into the
9086 buffer. It will also recognize item context in multiline items."
9088 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
9090 (progn (orgstruct-mode -1)
9092 (org-set-local (car v)
9093 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
9096 (setq org-fb-vars nil)
9097 (unless org-local-vars
9098 (setq org-local-vars (org-get-local-variables)))
9103 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
9104 (symbol-name (car x)))
9105 (setq var (car x) val (nth 1 x))
9106 (push (list var `(quote ,(eval var))) org-fb-vars)
9107 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
9109 (org-set-local 'orgstruct-is-++ t))))
9111 (defvar orgstruct-is-++ nil
9112 "Is `orgstruct-mode' in ++ version in the current-buffer?")
9113 (make-variable-buffer-local 'orgstruct-is-++)
9116 (defun turn-on-orgstruct++ ()
9117 "Unconditionally turn on `orgstruct++-mode'."
9118 (orgstruct++-mode 1))
9120 (defun orgstruct-error ()
9121 "Error when there is no default binding for a structure key."
9123 (funcall (if (fboundp 'user-error)
9126 "This key has no function outside structure elements"))
9128 (defun orgstruct-setup ()
9129 "Setup orgstruct keymap."
9130 (dolist (cell '((org-demote . t)
9134 (org-shiftmetaleft . t)
9135 (org-shiftmetaright . t)
9136 org-backward-element
9137 org-backward-heading-same-level
9142 org-forward-heading-same-level
9144 org-insert-heading-respect-content
9145 org-kill-note-or-show-branches
9150 org-narrow-to-subtree
9165 outline-next-visible-heading
9166 outline-previous-visible-heading
9170 (let ((f (or (car-safe cell) cell))
9171 (disable-when-heading-prefix (cdr-safe cell)))
9173 (let ((new-bindings))
9174 (dolist (binding (nconc (where-is-internal f org-mode-map)
9175 (where-is-internal f outline-mode-map)))
9176 (push binding new-bindings)
9177 ;; TODO use local-function-key-map
9178 (dolist (rep '(("<tab>" . "TAB")
9179 ("<return>" . "RET")
9180 ("<escape>" . "ESC")
9181 ("<delete>" . "DEL")))
9182 (setq binding (read-kbd-macro
9183 (let ((case-fold-search))
9184 (replace-regexp-in-string
9185 (regexp-quote (cdr rep))
9187 (key-description binding)))))
9188 (pushnew binding new-bindings :test 'equal)))
9189 (dolist (binding new-bindings)
9190 (let ((key (lookup-key orgstruct-mode-map binding)))
9191 (when (or (not key) (numberp key))
9193 (org-defkey orgstruct-mode-map
9195 (orgstruct-make-binding
9196 f binding disable-when-heading-prefix))))))))))
9197 (run-hooks 'orgstruct-setup-hook))
9199 (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
9200 "Create a function for binding in the structure minor mode.
9201 FUN is the command to call inside a table. KEY is the key that
9202 should be checked in for a command to execute outside of tables.
9203 Non-nil `disable-when-heading-prefix' means to disable the command
9204 if `orgstruct-heading-prefix-regexp' is not empty."
9205 (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
9208 (while (fboundp (intern nname))
9209 (setq nname (format "%s-%d" name (setq i (1+ i)))))
9210 (setq name (intern nname)))
9212 (let ((bindings '((org-heading-regexp
9214 orgstruct-heading-prefix-regexp
9215 "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
9217 (concat orgstruct-heading-prefix-regexp "\\*+ "))
9218 (org-outline-regexp-bol
9219 (concat "^" org-outline-regexp))
9220 (outline-regexp org-outline-regexp)
9221 (outline-heading-end-regexp "\n")
9222 (outline-level 'org-outline-level)
9223 (outline-heading-alist))))
9225 ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
9226 "Outside of structure, run the binding of `"
9227 (key-description key) "'."
9228 (when disable-when-heading-prefix
9230 "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
9231 "back to the default binding due to limitations of Org's implementation of\n"
9232 "`" (symbol-name fun) "'.")))
9235 ,(and disable-when-heading-prefix
9236 '(not (string= orgstruct-heading-prefix-regexp ""))))
9241 (org-context-p 'headline 'item
9243 '(org-insert-heading
9244 org-insert-heading-respect-content
9246 '(when orgstruct-is-++
9249 (let* ((orgstruct-mode)
9251 (loop with key = ,key
9254 ("<\\([^>]*\\)tab>" . "\\1TAB")
9255 ("<\\([^>]*\\)return>" . "\\1RET")
9256 ("<\\([^>]*\\)escape>" . "\\1ESC")
9257 ("<\\([^>]*\\)delete>" . "\\1DEL"))
9260 (setq key (read-kbd-macro
9261 (let ((case-fold-search))
9262 (replace-regexp-in-string
9265 (key-description key))))))
9266 thereis (key-binding key))))
9267 (if (keymapp binding)
9268 (org-set-transient-map binding)
9269 (let ((func (or binding
9271 'orgstruct-error))))
9273 (call-interactively func)))))
9274 (org-run-like-in-org-mode
9278 (call-interactively ',fun)))))))))
9281 (defun org-contextualize-keys (alist contexts)
9282 "Return valid elements in ALIST depending on CONTEXTS.
9284 `org-agenda-custom-commands' or `org-capture-templates' are the
9285 values used for ALIST, and `org-agenda-custom-commands-contexts'
9286 or `org-capture-templates-contexts' are the associated contexts
9289 ;; normalize contexts
9291 (lambda(c) (cond ((listp (cadr c))
9292 (list (car c) (car c) (cadr c)))
9293 ((string= "" (cadr c))
9294 (list (car c) (car c) (caddr c)))
9297 ;; loop over all commands or templates
9298 (while (setq c (pop a))
9301 ((not (assoc (car c) contexts))
9303 ((and (assoc (car c) contexts)
9304 (setq vrules (org-contextualize-validate-key
9307 (when (not (equal (car vr) (cadr vr)))
9308 (setq repl vr))) vrules)
9309 (if (not repl) (push c r)
9310 (push (cadr repl) s)
9313 (cdr (or (assoc (cadr repl) alist)
9314 (error "Undefined key `%s' as contextual replacement for `%s'"
9315 (cadr repl) (car c)))))
9317 ;; Return limited ALIST, possibly with keys modified, and deduplicated
9322 (let ((tpl (car x)))
9326 (equal y tpl)) s))) x)))
9329 (defun org-contextualize-validate-key (key contexts)
9330 "Check CONTEXTS for agenda or capture KEY."
9332 (while (setq r (pop contexts))
9336 (and (equal key (car r))
9337 (if (functionp rr) (funcall rr)
9338 (or (and (eq (car rr) 'in-file)
9340 (string-match (cdr rr) (buffer-file-name)))
9341 (and (eq (car rr) 'in-mode)
9342 (string-match (cdr rr) (symbol-name major-mode)))
9343 (and (eq (car rr) 'in-buffer)
9344 (string-match (cdr rr) (buffer-name)))
9345 (when (and (eq (car rr) 'not-in-file)
9347 (not (string-match (cdr rr) (buffer-file-name))))
9348 (when (eq (car rr) 'not-in-mode)
9349 (not (string-match (cdr rr) (symbol-name major-mode))))
9350 (when (eq (car rr) 'not-in-buffer)
9351 (not (string-match (cdr rr) (buffer-name)))))))
9354 (delete-dups (delq nil res))))
9356 (defun org-context-p (&rest contexts)
9357 "Check if local context is any of CONTEXTS.
9358 Possible values in the list of contexts are `table', `headline', and `item'."
9359 (let ((pos (point)))
9360 (goto-char (point-at-bol))
9361 (prog1 (or (and (memq 'table contexts)
9362 (looking-at "[ \t]*|"))
9363 (and (memq 'headline contexts)
9364 (looking-at org-outline-regexp))
9365 (and (memq 'item contexts)
9366 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
9367 (and (memq 'item-body contexts)
9371 (defun org-get-local-variables ()
9372 "Return a list of all local variables in an Org mode buffer."
9374 (with-current-buffer (get-buffer-create "*Org tmp*")
9377 (setq varlist (buffer-local-variables)))
9378 (kill-buffer "*Org tmp*")
9385 (list (car x) (cdr x))))
9386 (if (and (not (get (car x) 'org-state))
9388 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
9389 (symbol-name (car x))))
9393 (defun org-clone-local-variables (from-buffer &optional regexp)
9394 "Clone local variables from FROM-BUFFER.
9395 Optional argument REGEXP selects variables to clone."
9398 (and (symbolp (car pair))
9400 (string-match regexp (symbol-name (car pair))))
9401 (set (make-local-variable (car pair))
9403 (buffer-local-variables from-buffer)))
9406 (defun org-run-like-in-org-mode (cmd)
9407 "Run a command, pretending that the current buffer is in Org-mode.
9408 This will temporarily bind local variables that are typically bound in
9409 Org-mode to the values they have in Org-mode, and then interactively
9411 (org-load-modules-maybe)
9412 (unless org-local-vars
9413 (setq org-local-vars (org-get-local-variables)))
9415 (dolist (var org-local-vars)
9416 (when (or (not (boundp (car var)))
9417 (eq (symbol-value (car var))
9418 (default-value (car var))))
9419 (push (list (car var) `(quote ,(cadr var))) binds)))
9421 (call-interactively (quote ,cmd))))))
9423 (defun org-get-category (&optional pos force-refresh)
9424 "Get the category applying to position POS."
9426 (if force-refresh (org-refresh-category-properties))
9427 (let ((pos (or pos (point))))
9428 (or (get-text-property pos 'org-category)
9429 (progn (org-refresh-category-properties)
9430 (get-text-property pos 'org-category))))))
9432 ;;; Refresh properties
9434 (defun org-refresh-properties (dprop tprop)
9435 "Refresh buffer text properties.
9436 DPROP is the drawer property and TPROP is either the
9437 corresponding text property to set, or an alist with each element
9438 being a text property (as a symbol) and a function to apply to
9439 the value of the drawer property."
9440 (let ((case-fold-search t)
9441 (inhibit-read-only t))
9442 (org-with-silent-modifications
9446 (goto-char (point-min))
9447 (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
9448 (org-refresh-property tprop (org-match-string-no-properties 1))))))))
9450 (defun org-refresh-property (tprop p)
9451 "Refresh the buffer text property TPROP from the drawer property P.
9452 The refresh happens only for the current tree (not subtree)."
9454 (org-back-to-heading t)
9455 ;; tprop is a text property symbol
9458 (point) (or (outline-next-heading) (point-max)) tprop p)
9459 ;; tprop is an alist with (properties . function) elements
9463 (point-at-bol) (or (outline-next-heading) (point-max))
9465 (funcall (cdr al) p))))
9468 (defun org-refresh-category-properties ()
9469 "Refresh category text properties in the buffer."
9470 (let ((case-fold-search t)
9471 (inhibit-read-only t)
9473 ((null org-category)
9474 (if buffer-file-name
9475 (file-name-sans-extension
9476 (file-name-nondirectory buffer-file-name))
9478 ((symbolp org-category) (symbol-name org-category))
9480 beg end cat pos optionp)
9481 (org-with-silent-modifications
9485 (goto-char (point-min))
9486 (put-text-property (point) (point-max) 'org-category def-cat)
9487 (while (re-search-forward
9488 "^[ \t]*\\(\\(?:#\\+\\|:\\)CATEGORY:\\)\\(.*\\)" nil t)
9489 (setq pos (match-end 0)
9490 optionp (equal (char-after (match-beginning 0)) ?#)
9491 cat (org-trim (match-string 2)))
9493 (setq beg (point-at-bol) end (point-max))
9494 (org-back-to-heading t)
9495 (setq beg (point) end (org-end-of-subtree t t)))
9496 (put-text-property beg end 'org-category cat)
9497 (goto-char pos)))))))
9499 (defun org-refresh-stats-properties ()
9500 "Refresh stats text properties in the buffer."
9502 (org-with-silent-modifications
9506 (goto-char (point-min))
9507 (while (re-search-forward
9508 (concat org-outline-regexp-bol ".*"
9509 "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
9511 (setq stats (cond ((equal (match-string 3) "0") 0)
9513 (/ (* (string-to-number (match-string 2)) 100)
9514 (string-to-number (match-string 3))))
9515 (t (string-to-number (match-string 1)))))
9516 (org-back-to-heading t)
9517 (put-text-property (point) (progn (org-end-of-subtree t t) (point))
9518 'org-stats stats)))))))
9520 (defun org-refresh-effort-properties ()
9521 "Refresh effort properties"
9522 (org-refresh-properties
9524 '((effort . identity)
9525 (effort-minutes . org-duration-string-to-minutes))))
9529 ;;; Link abbreviations
9531 (defun org-link-expand-abbrev (link)
9532 "Apply replacements as defined in `org-link-abbrev-alist'."
9533 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
9534 (let* ((key (match-string 1 link))
9535 (as (or (assoc key org-link-abbrev-alist-local)
9536 (assoc key org-link-abbrev-alist)))
9537 (tag (and (match-end 2) (match-string 3 link)))
9543 ((symbolp rpl) (funcall rpl tag))
9544 ((string-match "%(\\([^)]+\\))" rpl)
9547 (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
9548 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
9549 ((string-match "%h" rpl)
9550 (replace-match (url-hexify-string (or tag "")) t t rpl))
9551 (t (concat rpl tag)))))
9554 ;;; Storing and inserting links
9556 (defvar org-insert-link-history nil
9557 "Minibuffer history for links inserted with `org-insert-link'.")
9559 (defvar org-stored-links nil
9560 "Contains the links stored with `org-store-link'.")
9562 (defvar org-store-link-plist nil
9563 "Plist with info about the most recently link created with `org-store-link'.")
9565 (defvar org-link-protocols nil
9566 "Link protocols added to Org-mode using `org-add-link-type'.")
9568 (defvar org-store-link-functions nil
9569 "List of functions that are called to create and store a link.
9570 Each function will be called in turn until one returns a non-nil
9571 value. Each function should check if it is responsible for creating
9572 this link (for example by looking at the major mode).
9573 If not, it must exit and return nil.
9574 If yes, it should return a non-nil value after a calling
9575 `org-store-link-props' with a list of properties and values.
9576 Special properties are:
9578 :type The link prefix, like \"http\". This must be given.
9579 :link The link, like \"http://www.astro.uva.nl/~dominik\".
9580 This is obligatory as well.
9581 :description Optional default description for the second pair
9582 of brackets in an Org-mode link. The user can still change
9583 this when inserting this link into an Org-mode buffer.
9585 In addition to these, any additional properties can be specified
9586 and then used in capture templates.")
9588 (defun org-add-link-type (type &optional follow export)
9589 "Add TYPE to the list of `org-link-types'.
9590 Re-compute all regular expressions depending on `org-link-types'
9592 FOLLOW and EXPORT are two functions.
9594 FOLLOW should take the link path as the single argument and do whatever
9595 is necessary to follow the link, for example find a file or display
9598 EXPORT should format the link path for export to one of the export formats.
9599 It should be a function accepting three arguments:
9601 path the path of the link, the text after the prefix (like \"http:\")
9602 desc the description of the link, if any, or a description added by
9603 org-export-normalize-links if there is none
9604 format the export format, a symbol like `html' or `latex' or `ascii'..
9606 The function may use the FORMAT information to return different values
9607 depending on the format. The return value will be put literally into
9608 the exported file. If the return value is nil, this means Org should
9609 do what it normally does with links which do not have EXPORT defined.
9611 Org-mode has a built-in default for exporting links. If you are happy with
9612 this default, there is no need to define an export function for the link
9613 type. For a simple example of an export function, see `org-bbdb.el'."
9614 (add-to-list 'org-link-types type t)
9615 (org-make-link-regexps)
9616 (if (assoc type org-link-protocols)
9617 (setcdr (assoc type org-link-protocols) (list follow export))
9618 (push (list type follow export) org-link-protocols)))
9620 (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
9621 (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
9624 (defun org-store-link (arg)
9625 "\\<org-mode-map>Store an org-link to the current location.
9626 This link is added to `org-stored-links' and can later be inserted
9627 into an org-buffer with \\[org-insert-link].
9629 For some link types, a prefix arg is interpreted.
9630 For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
9631 For file links, arg negates `org-context-in-file-links'.
9633 A double prefix arg force skipping storing functions that are not
9636 A triple prefix arg force storing a link for each line in the
9639 (org-load-modules-maybe)
9640 (if (and (equal arg '(64)) (org-region-active-p))
9642 (let ((end (region-end)))
9643 (goto-char (region-beginning))
9645 (while (< (point-at-eol) end)
9646 (move-end-of-line 1) (activate-mark)
9647 (let (current-prefix-arg)
9648 (call-interactively 'org-store-link))
9649 (move-beginning-of-line 2)
9650 (set-mark (point)))))
9651 (org-with-limited-levels
9652 (setq org-store-link-plist nil)
9653 (let (link cpltxt desc description search
9654 txt custom-id agenda-link sfuns sfunsn)
9657 ;; Store a link using an external link type
9658 ((and (not (equal arg '(16)))
9661 nil (mapcar (lambda (f)
9662 (let (fs) (if (funcall f) (push f fs))))
9663 org-store-link-functions))
9664 sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
9665 (or (and (cdr sfuns)
9668 "Which function for creating the link? "
9669 sfunsn nil t (car sfunsn)))))
9670 (funcall (caar sfuns)))
9671 (setq link (plist-get org-store-link-plist :link)
9672 desc (or (plist-get org-store-link-plist
9673 :description) link))))
9675 ;; Store a link from a source code buffer
9676 ((org-src-edit-buffer-p)
9678 (while (or (not label)
9682 (goto-char (point-min))
9684 (regexp-quote (format org-coderef-label-format label))
9686 (when label (message "Label exists already") (sit-for 2))
9687 (setq label (read-string "Code line label: " label)))
9689 (setq link (format org-coderef-label-format label))
9690 (setq gc (- 79 (length link)))
9691 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
9693 (setq link (concat "(" label ")") desc nil)))
9695 ;; We are in the agenda, link to referenced location
9696 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
9697 (let ((m (or (get-text-property (point) 'org-hd-marker)
9698 (get-text-property (point) 'org-marker))))
9700 (org-with-point-at m
9702 (if (org-called-interactively-p 'any)
9703 (call-interactively 'org-store-link)
9704 (org-store-link nil)))))))
9706 ((eq major-mode 'calendar-mode)
9707 (let ((cd (calendar-cursor-to-date)))
9710 (car org-time-stamp-formats)
9712 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
9714 (org-store-link-props :type "calendar" :date cd)))
9716 ((eq major-mode 'help-mode)
9717 (setq link (concat "help:" (save-excursion
9718 (goto-char (point-min))
9719 (looking-at "^[^ ]+")
9721 (org-store-link-props :type "help"))
9723 ((eq major-mode 'w3-mode)
9724 (setq cpltxt (if (and (buffer-name)
9725 (not (string-match "Untitled" (buffer-name))))
9728 link (url-view-url t))
9729 (org-store-link-props :type "w3" :url (url-view-url t)))
9731 ((eq major-mode 'image-mode)
9732 (setq cpltxt (concat "file:"
9733 (abbreviate-file-name buffer-file-name))
9735 (org-store-link-props :type "image" :file buffer-file-name))
9737 ;; In dired, store a link to the file of the current line
9738 ((derived-mode-p 'dired-mode)
9739 (let ((file (dired-get-filename nil t)))
9741 (abbreviate-file-name
9742 (expand-file-name (dired-get-filename nil t)))
9743 ;; otherwise, no file so use current directory.
9745 (setq cpltxt (concat "file:" file)
9748 ((setq search (run-hook-with-args-until-success
9749 'org-create-file-search-functions))
9750 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
9752 (setq cpltxt (or description link)))
9754 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
9755 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
9757 ;; Store a link using the target at point
9758 ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
9761 (abbreviate-file-name
9762 (buffer-file-name (buffer-base-buffer)))
9763 "::" (match-string 1))
9765 ((and (featurep 'org-id)
9766 (or (eq org-id-link-to-org-use-id t)
9767 (and (org-called-interactively-p 'any)
9768 (or (eq org-id-link-to-org-use-id 'create-if-interactive)
9769 (and (eq org-id-link-to-org-use-id
9770 'create-if-interactive-and-no-custom-id)
9772 (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
9773 ;; Store a link using the ID at point
9774 (setq link (condition-case nil
9775 (prog1 (org-id-store-link)
9776 (setq desc (or (plist-get org-store-link-plist
9780 ;; Probably before first headline, link only to file
9782 (abbreviate-file-name
9783 (buffer-file-name (buffer-base-buffer))))))))
9785 ;; Just link to current headline
9786 (setq cpltxt (concat "file:"
9787 (abbreviate-file-name
9788 (buffer-file-name (buffer-base-buffer)))))
9789 ;; Add a context search string
9790 (when (org-xor org-context-in-file-links arg)
9791 (let* ((ee (org-element-at-point))
9792 (et (org-element-type ee))
9793 (ev (plist-get (cadr ee) :value))
9794 (ek (plist-get (cadr ee) :key))
9795 (eok (and (stringp ek) (string-match "name" ek))))
9797 ((org-at-heading-p) nil)
9798 ((and (eq et 'keyword) eok) ev)
9799 ((org-region-active-p)
9800 (buffer-substring (region-beginning) (region-end)))))
9801 (when (or (null txt) (string-match "\\S-" txt))
9805 (org-make-org-heading-search-string txt)
9807 desc (or (and (eq et 'keyword) eok ev)
9808 (nth 4 (ignore-errors (org-heading-components)))
9810 (if (string-match "::\\'" cpltxt)
9811 (setq cpltxt (substring cpltxt 0 -2)))
9812 (setq link cpltxt))))
9814 ((buffer-file-name (buffer-base-buffer))
9815 ;; Just link to this file here.
9816 (setq cpltxt (concat "file:"
9817 (abbreviate-file-name
9818 (buffer-file-name (buffer-base-buffer)))))
9819 ;; Add a context string.
9820 (when (org-xor org-context-in-file-links arg)
9821 (setq txt (if (org-region-active-p)
9822 (buffer-substring (region-beginning) (region-end))
9823 (buffer-substring (point-at-bol) (point-at-eol))))
9824 ;; Only use search option if there is some text.
9825 (when (string-match "\\S-" txt)
9827 (concat cpltxt "::" (org-make-org-heading-search-string txt))
9831 ((org-called-interactively-p 'interactive)
9832 (user-error "No method for storing a link from this buffer"))
9834 (t (setq link nil)))
9836 ;; We're done setting link and desc, clean up
9837 (if (consp link) (setq cpltxt (car link) link (cdr link)))
9838 (setq link (or link cpltxt)
9839 desc (or desc cpltxt))
9840 (cond ((equal desc "NONE") (setq desc nil))
9841 ((and desc (string-match org-bracket-link-analytic-regexp desc))
9842 (let ((d0 (match-string 3 desc))
9843 (p0 (match-string 5 desc)))
9845 (replace-regexp-in-string
9846 org-bracket-link-regexp
9848 (if (equal (length (match-string 0 desc))
9849 (length desc)) "*" "")) desc)))))
9852 (if (not (and (or (org-called-interactively-p 'any)
9853 executing-kbd-macro) link))
9854 (or agenda-link (and link (org-make-link-string link desc)))
9855 (push (list link desc) org-stored-links)
9856 (message "Stored: %s" (or desc link))
9858 (setq link (concat "file:" (abbreviate-file-name
9859 (buffer-file-name)) "::#" custom-id))
9860 (push (list link desc) org-stored-links))
9861 (car org-stored-links))))))
9863 (defun org-store-link-props (&rest plist)
9864 "Store link properties, extract names and addresses."
9866 (when (setq x (plist-get plist :from))
9867 (setq adr (mail-extract-address-components x))
9868 (setq plist (plist-put plist :fromname (car adr)))
9869 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
9870 (when (setq x (plist-get plist :to))
9871 (setq adr (mail-extract-address-components x))
9872 (setq plist (plist-put plist :toname (car adr)))
9873 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
9874 (let ((from (plist-get plist :from))
9875 (to (plist-get plist :to)))
9876 (when (and from to org-from-is-user-regexp)
9878 (plist-put plist :fromto
9879 (if (string-match org-from-is-user-regexp from)
9881 (concat "from %f"))))))
9882 (setq org-store-link-plist plist))
9884 (defun org-add-link-props (&rest plist)
9885 "Add these properties to the link property list."
9888 (setq key (pop plist) value (pop plist))
9889 (setq org-store-link-plist
9890 (plist-put org-store-link-plist key value)))))
9892 (defun org-email-link-description (&optional fmt)
9893 "Return the description part of an email link.
9894 This takes information from `org-store-link-plist' and formats it
9895 according to FMT (default from `org-email-link-description-format')."
9896 (setq fmt (or fmt org-email-link-description-format))
9897 (let* ((p org-store-link-plist)
9898 (to (plist-get p :toaddress))
9899 (from (plist-get p :fromaddress))
9902 (cons "%c" (plist-get p :fromto))
9903 (cons "%F" (plist-get p :from))
9904 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
9905 (cons "%T" (plist-get p :to))
9906 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
9907 (cons "%s" (plist-get p :subject))
9908 (cons "%d" (plist-get p :date))
9909 (cons "%m" (plist-get p :message-id)))))
9910 (when (string-match "%c" fmt)
9911 ;; Check if the user wrote this message
9912 (if (and org-from-is-user-regexp from to
9913 (save-match-data (string-match org-from-is-user-regexp from)))
9914 (setq fmt (replace-match "to %t" t t fmt))
9915 (setq fmt (replace-match "from %f" t t fmt))))
9916 (org-replace-escapes fmt table)))
9918 (defun org-make-org-heading-search-string (&optional string)
9919 "Make search string for the current headline or STRING."
9921 (and (derived-mode-p 'org-mode)
9923 (org-back-to-heading t)
9924 (org-element-property :raw-value (org-element-at-point))))))
9925 (lines org-context-in-file-links))
9926 (or string (setq s (concat "*" s))) ; Add * for headlines
9927 (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
9928 (when (and string (integerp lines) (> lines 0))
9929 (let ((slines (org-split-string s "\n")))
9930 (when (< lines (length slines))
9933 (reverse (nthcdr (- (length slines) lines)
9934 (reverse slines))) "\n")))))
9935 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
9937 (defun org-make-link-string (link &optional description)
9938 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9939 (unless (string-match "\\S-" link)
9940 (error "Empty link"))
9941 (when (and description
9942 (stringp description)
9943 (not (string-match "\\S-" description)))
9944 (setq description nil))
9945 (when (stringp description)
9946 ;; Remove brackets from the description, they are fatal.
9947 (while (string-match "\\[" description)
9948 (setq description (replace-match "{" t t description)))
9949 (while (string-match "\\]" description)
9950 (setq description (replace-match "}" t t description))))
9951 (when (equal link description)
9952 ;; No description needed, it is identical
9953 (setq description nil))
9954 (when (and (not description)
9955 (not (string-match (org-image-file-name-regexp) link))
9956 (not (equal link (org-link-escape link))))
9957 (setq description (org-extract-attributes link)))
9959 (cond ((string-match (org-image-file-name-regexp) link) link)
9960 ((string-match org-link-types-re link)
9961 (concat (match-string 1 link)
9962 (org-link-escape (substring link (match-end 1)))))
9963 (t (org-link-escape link))))
9964 (concat "[[" link "]"
9965 (if description (concat "[" description "]") "")
9968 (defconst org-link-escape-chars
9971 "List of characters that should be escaped in a link when stored to Org.
9972 This is the list that is used for internal purposes.")
9974 (defconst org-link-escape-chars-browser
9977 "List of characters to be escaped before handing over to the browser.
9978 If you consider using this constant then you probably want to use
9979 the function `org-link-escape-browser' instead. See there why
9980 this constant is a candidate to be removed once Org drops support
9981 for Emacs 24.1 and 24.2.")
9983 (defun org-link-escape (text &optional table merge)
9984 "Return percent escaped representation of TEXT.
9985 TEXT is a string with the text to escape.
9986 Optional argument TABLE is a list with characters that should be
9987 escaped. When nil, `org-link-escape-chars' is used.
9988 If optional argument MERGE is set, merge TABLE into
9989 `org-link-escape-chars'."
9990 ;; Don't escape chars in internal links
9991 (if (string-match "^\\*[[:alnum:]]+" text)
9995 (mapc (lambda (defchr)
9996 (unless (member defchr table)
9997 (setq table (cons defchr table))))
9998 org-link-escape-chars))
10000 (setq table org-link-escape-chars)))
10003 (if (or (member char table)
10004 (and (or (< char 32) (= char ?\%) (> char 126))
10006 (mapconcat (lambda (sequence-element)
10007 (format "%%%.2X" sequence-element))
10008 (or (encode-coding-char char 'utf-8)
10009 (error "Unable to percent escape character: %s"
10010 (char-to-string char))) "")
10011 (char-to-string char))) text "")))
10013 (defun org-link-escape-browser (text)
10014 "Escape some characters before handing over to the browser.
10015 This function is a candidate to be removed together with the
10016 constant `org-link-escape-chars-browser' once Org drops support
10017 for Emacs 24.1 and 24.2. All calls to this function will have to
10018 be replaced with `url-encode-url' which is available since Emacs
10020 ;; Example with the Org link
10021 ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
10022 ;; to open the browser with +subject:"Release 8.2" filled into the
10023 ;; query field: In this case the variable TEXT contains the
10024 ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
10025 ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
10026 ;; `org-link-escape' with `org-link-escape-chars-browser' converts
10027 ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
10028 (if (fboundp 'url-encode-url)
10029 (url-encode-url text)
10030 (if (org-string-match-p
10031 (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
10033 (org-link-escape text org-link-escape-chars-browser)
10036 (defun org-link-unescape (str)
10037 "Unhex hexified Unicode strings as returned from the JavaScript function
10038 encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
10039 (unless (and (null str) (string= "" str))
10040 (let ((pos 0) (case-fold-search t) unhexed)
10041 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
10042 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
10043 (setq str (replace-match unhexed t t str))
10044 (setq pos (+ pos (length unhexed))))))
10047 (defun org-link-unescape-compound (hex)
10048 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
10049 Note: this function also decodes single byte encodings like
10050 `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
10052 (let* ((bytes (cdr (split-string hex "%")))
10057 (let* ((val (string-to-number (pop bytes) 16))
10061 ((>= val 252) (cons 6 252))
10062 ((>= val 248) (cons 5 248))
10063 ((>= val 240) (cons 4 240))
10064 ((>= val 224) (cons 3 224))
10065 ((>= val 192) (cons 2 192))
10068 (if (>= val 192) (setq eat (car shift-xor)))
10069 (setq val (logxor val (cdr shift-xor)))
10070 (setq sum (+ (lsh sum (car shift-xor)) val))
10071 (if (> eat 0) (setq eat (- eat 1)))
10073 ((= 0 eat) ;multi byte
10074 (setq ret (concat ret (org-char-to-string sum)))
10076 ((not bytes) ; single byte(s)
10077 (setq ret (org-link-unescape-single-byte-sequence hex))))))
10080 (defun org-link-unescape-single-byte-sequence (hex)
10081 "Unhexify hex-encoded single byte character sequences."
10082 (mapconcat (lambda (byte)
10083 (char-to-string (string-to-number byte 16)))
10084 (cdr (split-string hex "%")) ""))
10086 (defun org-xor (a b)
10090 (defun org-fixup-message-id-for-http (s)
10091 "Replace special characters in a message id, so it can be used in an http query."
10092 (when (string-match "%" s)
10093 (setq s (mapconcat (lambda (c)
10096 (char-to-string c)))
10098 (while (string-match "<" s)
10099 (setq s (replace-match "%3C" t t s)))
10100 (while (string-match ">" s)
10101 (setq s (replace-match "%3E" t t s)))
10102 (while (string-match "@" s)
10103 (setq s (replace-match "%40" t t s)))
10106 (defun org-link-prettify (link)
10107 "Return a human-readable representation of LINK.
10108 The car of LINK must be a raw link.
10109 The cdr of LINK must be either a link description or nil."
10110 (let ((desc (or (cadr link) "<no description>")))
10111 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
10112 "<" (car link) ">")))
10115 (defun org-insert-link-global ()
10116 "Insert a link like Org-mode does.
10117 This command can be called in any mode to insert a link in Org-mode syntax."
10119 (org-load-modules-maybe)
10120 (org-run-like-in-org-mode 'org-insert-link))
10122 (defun org-insert-all-links (arg &optional pre post)
10123 "Insert all links in `org-stored-links'.
10124 When a universal prefix, do not delete the links from `org-stored-links'.
10125 When `ARG' is a number, insert the last N link(s).
10126 `PRE' and `POST' are optional arguments to define a string to
10127 prepend or to append."
10129 (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
10130 (links (copy-seq org-stored-links))
10132 (po (or post "\n"))
10134 (if (null org-stored-links)
10135 (message "No link to insert")
10136 (while (and (or (listp arg) (>= arg cnt))
10137 (setq l (if (listp arg)
10139 (pop org-stored-links))))
10140 (setq cnt (1+ cnt))
10142 (org-insert-link nil (car l) (or (cadr l) "<no description>"))
10145 (defun org-insert-last-stored-link (arg)
10146 "Insert the last link stored in `org-stored-links'."
10148 (org-insert-all-links arg "" "\n"))
10150 (defun org-link-fontify-links-to-this-file ()
10151 "Fontify links to the current file in `org-stored-links'."
10152 (let ((f (buffer-file-name)) a b)
10153 (setq a (mapcar (lambda(l)
10154 (let ((ll (car l)))
10155 (when (and (string-match "^file:\\(.+\\)::" ll)
10156 (equal f (expand-file-name (match-string 1 ll))))
10159 (when (featurep 'org-id)
10160 (setq b (mapcar (lambda(l)
10161 (let ((ll (car l)))
10162 (when (and (string-match "^id:\\(.+\\)$" ll)
10163 (equal f (expand-file-name
10164 (or (org-id-find-id-file
10165 (match-string 1 ll)) ""))))
10167 org-stored-links)))
10169 (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
10170 (delq nil (append a b)))))
10172 (defvar org-link-links-in-this-file nil)
10173 (defun org-insert-link (&optional complete-file link-location default-description)
10174 "Insert a link. At the prompt, enter the link.
10176 Completion can be used to insert any of the link protocol prefixes like
10177 http or ftp in use.
10179 The history can be used to select a link previously stored with
10180 `org-store-link'. When the empty string is entered (i.e. if you just
10181 press RET at the prompt), the link defaults to the most recently
10182 stored link. As SPC triggers completion in the minibuffer, you need to
10183 use M-SPC or C-q SPC to force the insertion of a space character.
10185 You will also be prompted for a description, and if one is given, it will
10186 be displayed in the buffer instead of the link.
10188 If there is already a link at point, this command will allow you to edit link
10189 and description parts.
10191 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
10192 be selected using completion. The path to the file will be relative to the
10193 current directory if the file is in the current directory or a subdirectory.
10194 Otherwise, the link will be the absolute path as completed in the minibuffer
10195 \(i.e. normally ~/path/to/file). You can configure this behavior using the
10196 option `org-link-file-path-type'.
10198 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
10199 the current directory or below.
10201 With three \\[universal-argument] prefixes, negate the meaning of
10202 `org-keep-stored-link-after-insertion'.
10204 If `org-make-link-description-function' is non-nil, this function will be
10205 called with the link target, and the result will be the default
10208 If the LINK-LOCATION parameter is non-nil, this value will be
10209 used as the link location instead of reading one interactively.
10211 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
10212 be used as the default description."
10214 (let* ((wcf (current-window-configuration))
10215 (origbuf (current-buffer))
10216 (region (if (org-region-active-p)
10217 (buffer-substring (region-beginning) (region-end))))
10218 (remove (and region (list (region-beginning) (region-end))))
10220 tmphist ; byte-compile incorrectly complains about this
10221 (link link-location)
10222 (abbrevs org-link-abbrev-alist-local)
10223 entry file all-prefixes auto-desc)
10225 (link-location) ; specified by arg, just use it.
10226 ((org-in-regexp org-bracket-link-regexp 1)
10227 ;; We do have a link at point, and we are going to edit it.
10228 (setq remove (list (match-beginning 0) (match-end 0)))
10229 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
10230 (setq link (read-string "Link: "
10232 (org-match-string-no-properties 1)))))
10233 ((or (org-in-regexp org-angle-link-re)
10234 (org-in-regexp org-plain-link-re))
10235 ;; Convert to bracket link
10236 (setq remove (list (match-beginning 0) (match-end 0))
10237 link (read-string "Link: "
10238 (org-remove-angle-brackets (match-string 0)))))
10239 ((member complete-file '((4) (16)))
10240 ;; Completing read for file names.
10241 (setq link (org-file-complete-link complete-file)))
10243 ;; Read link, with completion for stored links.
10244 (org-link-fontify-links-to-this-file)
10245 (org-switch-to-buffer-other-window "*Org Links*")
10246 (with-current-buffer "*Org Links*"
10248 (insert "Insert a link.
10249 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
10250 (when org-stored-links
10251 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
10252 (insert (mapconcat 'org-link-prettify
10253 (reverse org-stored-links) "\n")))
10254 (goto-char (point-min)))
10255 (let ((cw (selected-window)))
10256 (select-window (get-buffer-window "*Org Links*" 'visible))
10257 (with-current-buffer "*Org Links*" (setq truncate-lines t))
10258 (unless (pos-visible-in-window-p (point-max))
10259 (org-fit-window-to-buffer))
10260 (and (window-live-p cw) (select-window cw)))
10261 ;; Fake a link history, containing the stored links.
10262 (setq tmphist (append (mapcar 'car org-stored-links)
10263 org-insert-link-history))
10264 (setq all-prefixes (append (mapcar 'car abbrevs)
10265 (mapcar 'car org-link-abbrev-alist)
10270 (org-completing-read
10273 (mapcar (lambda (x) (concat x ":"))
10275 (mapcar 'car org-stored-links))
10278 (caar org-stored-links)))
10279 (if (not (string-match "\\S-" link))
10280 (user-error "No link selected"))
10282 (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
10284 (if (or (member link all-prefixes)
10285 (and (equal ":" (substring link -1))
10286 (member (substring link 0 -1) all-prefixes)
10287 (setq link (substring link 0 -1))))
10288 (setq link (with-current-buffer origbuf
10289 (org-link-try-special-completion link)))))
10290 (set-window-configuration wcf)
10291 (kill-buffer "*Org Links*"))
10292 (setq entry (assoc link org-stored-links))
10293 (or entry (push link org-insert-link-history))
10294 (setq desc (or desc (nth 1 entry)))))
10296 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
10297 (not org-keep-stored-link-after-insertion))
10298 (setq org-stored-links (delq (assoc link org-stored-links)
10299 org-stored-links)))
10301 (if (and (string-match org-plain-link-re link)
10302 (not (string-match org-ts-regexp link)))
10303 ;; URL-like link, normalize the use of angular brackets.
10304 (setq link (org-remove-angle-brackets link)))
10306 ;; Check if we are linking to the current file with a search
10307 ;; option If yes, simplify the link by using only the search
10309 (when (and buffer-file-name
10310 (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
10311 (let* ((path (match-string 1 link))
10312 (case-fold-search nil)
10313 (search (match-string 2 link)))
10315 (if (equal (file-truename buffer-file-name) (file-truename path))
10316 ;; We are linking to this same file, with a search option
10317 (setq link search)))))
10319 ;; Check if we can/should use a relative path. If yes, simplify the link
10320 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
10321 (let* ((type (match-string 1 link))
10322 (path (match-string 2 link))
10324 (case-fold-search nil))
10326 ((or (eq org-link-file-path-type 'absolute)
10327 (equal complete-file '(16)))
10328 (setq path (abbreviate-file-name (expand-file-name path))))
10329 ((eq org-link-file-path-type 'noabbrev)
10330 (setq path (expand-file-name path)))
10331 ((eq org-link-file-path-type 'relative)
10332 (setq path (file-relative-name path)))
10335 (if (string-match (concat "^" (regexp-quote
10337 (file-name-as-directory
10338 default-directory))))
10339 (expand-file-name path))
10340 ;; We are linking a file with relative path name.
10341 (setq path (substring (expand-file-name path)
10343 (setq path (abbreviate-file-name (expand-file-name path)))))))
10344 (setq link (concat type path))
10345 (if (equal desc origpath)
10346 (setq desc path))))
10348 (if org-make-link-description-function
10350 (or (condition-case nil
10351 (funcall org-make-link-description-function link desc)
10352 (error (progn (message "Can't get link description from `%s'"
10353 (symbol-name org-make-link-description-function))
10355 (read-string "Description: " default-description)))
10356 (if default-description (setq desc default-description)
10357 (setq desc (or (and auto-desc desc)
10358 (read-string "Description: " desc)))))
10360 (unless (string-match "\\S-" desc) (setq desc nil))
10361 (if remove (apply 'delete-region remove))
10362 (insert (org-make-link-string link desc))))
10364 (defun org-link-try-special-completion (type)
10365 "If there is completion support for link type TYPE, offer it."
10366 (let ((fun (intern (concat "org-" type "-complete-link"))))
10367 (if (functionp fun)
10369 (read-string "Link (no completion support): " (concat type ":")))))
10371 (defun org-file-complete-link (&optional arg)
10372 "Create a file link using completion."
10374 (setq file (org-iread-file-name "File: "))
10375 (let ((pwd (file-name-as-directory (expand-file-name ".")))
10376 (pwd1 (file-name-as-directory (abbreviate-file-name
10377 (expand-file-name ".")))))
10382 (abbreviate-file-name (expand-file-name file)))))
10383 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
10384 (setq link (concat "file:" (match-string 1 file))))
10385 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
10386 (expand-file-name file))
10388 "file:" (match-string 1 (expand-file-name file)))))
10389 (t (setq link (concat "file:" file)))))
10392 (defun org-iread-file-name (&rest args)
10393 "Read-file-name using `ido-mode' speedup if available.
10394 ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
10395 See `read-file-name' for a description of parameters."
10396 (org-without-partial-completion
10397 (if (and org-completion-use-ido
10398 (fboundp 'ido-read-file-name)
10399 (boundp 'ido-mode) ido-mode
10400 (listp (second args)))
10401 (let ((ido-enter-matching-directory nil))
10402 (apply 'ido-read-file-name args))
10403 (apply 'read-file-name args))))
10405 (defun org-completing-read (&rest args)
10406 "Completing-read with SPACE being a normal character."
10407 (let ((enable-recursive-minibuffers t)
10408 (minibuffer-local-completion-map
10409 (copy-keymap minibuffer-local-completion-map)))
10410 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
10411 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
10412 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
10413 (apply 'org-icompleting-read args)))
10415 (defun org-completing-read-no-i (&rest args)
10416 (let (org-completion-use-ido org-completion-use-iswitchb)
10417 (apply 'org-completing-read args)))
10419 (defun org-iswitchb-completing-read (prompt choices &rest args)
10420 "Use iswitch as a completing-read replacement to choose from choices.
10421 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
10423 (let* ((iswitchb-use-virtual-buffers nil)
10424 (iswitchb-make-buflist-hook
10426 (setq iswitchb-temp-buflist choices))))
10427 (iswitchb-read-buffer prompt)))
10429 (defun org-icompleting-read (&rest args)
10430 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
10431 (org-without-partial-completion
10432 (if (and org-completion-use-ido
10433 (fboundp 'ido-completing-read)
10434 (boundp 'ido-mode) ido-mode
10435 (listp (second args)))
10436 (let ((ido-enter-matching-directory nil))
10437 (apply 'ido-completing-read (concat (car args))
10438 (if (consp (car (nth 1 args)))
10439 (mapcar 'car (nth 1 args))
10442 (if (and org-completion-use-iswitchb
10443 (boundp 'iswitchb-mode) iswitchb-mode
10444 (listp (second args)))
10445 (apply 'org-iswitchb-completing-read (concat (car args))
10446 (if (consp (car (nth 1 args)))
10447 (mapcar 'car (nth 1 args))
10450 (apply 'completing-read args)))))
10452 (defun org-extract-attributes (s)
10453 "Extract the attributes cookie from a string and set as text property."
10454 (let (a attr (start 0) key value)
10456 (when (string-match "{{\\([^}]+\\)}}$" s)
10457 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
10458 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
10459 (setq key (match-string 1 a) value (match-string 2 a)
10460 start (match-end 0)
10461 attr (plist-put attr (intern key) value))))
10462 (org-add-props s nil 'org-attr attr))
10465 ;;; Opening/following a link
10467 (defvar org-link-search-failed nil)
10469 (defvar org-open-link-functions nil
10470 "Hook for functions finding a plain text link.
10471 These functions must take a single argument, the link content.
10472 They will be called for links that look like [[link text][description]]
10473 when LINK TEXT does not have a protocol like \"http:\" and does not look
10474 like a filename (e.g. \"./blue.png\").
10476 These functions will be called *before* Org attempts to resolve the
10477 link by doing text searches in the current buffer - so if you want a
10478 link \"[[target]]\" to still find \"<<target>>\", your function should
10479 handle this as a special case.
10481 When the function does handle the link, it must return a non-nil value.
10482 If it decides that it is not responsible for this link, it must return
10483 nil to indicate that that Org-mode can continue with other options
10484 like exact and fuzzy text search.")
10486 (defun org-next-link (&optional search-backward)
10487 "Move forward to the next link.
10488 If the link is in hidden text, expose it."
10490 (when (and org-link-search-failed (eq this-command last-command))
10491 (goto-char (point-min))
10492 (message "Link search wrapped back to beginning of buffer"))
10493 (setq org-link-search-failed nil)
10494 (let* ((pos (point))
10496 (a (assoc :link ct))
10497 (srch-fun (if search-backward 're-search-backward 're-search-forward)))
10498 (cond (a (goto-char (nth (if search-backward 1 2) a)))
10499 ((looking-at org-any-link-re)
10500 ;; Don't stay stuck at link without an org-link face
10501 (forward-char (if search-backward -1 1))))
10502 (if (funcall srch-fun org-any-link-re nil t)
10504 (goto-char (match-beginning 0))
10505 (if (outline-invisible-p) (org-show-context)))
10507 (setq org-link-search-failed t)
10508 (message "No further link found"))))
10510 (defun org-previous-link ()
10511 "Move backward to the previous link.
10512 If the link is in hidden text, expose it."
10514 (funcall 'org-next-link t))
10516 (defun org-translate-link (s)
10517 "Translate a link string if a translation function has been defined."
10518 (if (and org-link-translation-function
10519 (fboundp org-link-translation-function)
10520 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
10522 (setq s (funcall org-link-translation-function
10523 (match-string 1 s) (match-string 2 s)))
10524 (concat (car s) ":" (cdr s)))
10527 (defun org-translate-link-from-planner (type path)
10528 "Translate a link from Emacs Planner syntax so that Org can follow it.
10529 This is still an experimental function, your mileage may vary."
10531 ((member type '("http" "https" "news" "ftp"))
10532 ;; standard Internet links are the same.
10534 ((and (equal type "irc") (string-match "^//" path))
10535 ;; Planner has two / at the beginning of an irc link, we have 1.
10536 ;; We should have zero, actually....
10537 (setq path (substring path 1)))
10538 ((and (equal type "lisp") (string-match "^/" path))
10539 ;; Planner has a slash, we do not.
10540 (setq type "elisp" path (substring path 1)))
10541 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
10542 ;; A typical message link. Planner has the id after the final slash,
10543 ;; we separate it with a hash mark
10544 (setq path (concat (match-string 1 path) "#"
10545 (org-remove-angle-brackets (match-string 2 path))))))
10548 (defun org-find-file-at-mouse (ev)
10549 "Open file link or URL at mouse."
10551 (mouse-set-point ev)
10552 (org-open-at-point 'in-emacs))
10554 (defun org-open-at-mouse (ev)
10555 "Open file link or URL at mouse.
10556 See the docstring of `org-open-file' for details."
10558 (mouse-set-point ev)
10559 (if (eq major-mode 'org-agenda-mode)
10560 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
10561 (org-open-at-point))
10563 (defvar org-window-config-before-follow-link nil
10564 "The window configuration before following a link.
10565 This is saved in case the need arises to restore it.")
10567 (defvar org-open-link-marker (make-marker)
10568 "Marker pointing to the location where `org-open-at-point' was called.")
10571 (defun org-open-at-point-global ()
10572 "Follow a link like Org-mode does.
10573 This command can be called in any mode to follow a link that has
10576 (org-run-like-in-org-mode 'org-open-at-point))
10579 (defun org-open-link-from-string (s &optional arg reference-buffer)
10580 "Open a link in the string S, as if it was in Org-mode."
10581 (interactive "sLink: \nP")
10582 (let ((reference-buffer (or reference-buffer (current-buffer))))
10584 (let ((org-inhibit-startup (not reference-buffer)))
10587 (goto-char (point-min))
10588 (when reference-buffer
10589 (setq org-link-abbrev-alist-local
10590 (with-current-buffer reference-buffer
10591 org-link-abbrev-alist-local)))
10592 (org-open-at-point arg reference-buffer)))))
10594 (defvar org-open-at-point-functions nil
10595 "Hook that is run when following a link at point.
10597 Functions in this hook must return t if they identify and follow
10598 a link at point. If they don't find anything interesting at point,
10599 they must return nil.")
10601 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
10602 (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
10603 (defun org-open-at-point (&optional arg reference-buffer)
10604 "Open link, timestamp, footnote or tags at point.
10606 When point is on a link, follow it. Normally, files will be
10607 opened by an appropriate application. If the optional prefix
10608 argument ARG is non-nil, Emacs will visit the file. With
10609 a double prefix argument, try to open outside of Emacs, in the
10610 application the system uses for this file type.
10612 When point is on a timestamp, open the agenda at the day
10615 When point is a footnote definition, move to the first reference
10616 found. If it is on a reference, move to the associated
10619 When point is on a headline, display a list of every link in the
10620 entry, so it is possible to pick one, or all, of them. If point
10621 is on a tag, call `org-tags-view' instead.
10623 When optional argument REFERENCE-BUFFER is non-nil, it should
10624 specify a buffer from where the link search should happen. This
10625 is used internally by `org-open-link-from-string'.
10627 On top of syntactically correct links, this function will open
10628 the link at point in comments or comment blocks and the first
10629 link in a property drawer line."
10631 ;; On a code block, open block's results.
10632 (unless (call-interactively 'org-babel-open-src-block-result)
10633 (org-load-modules-maybe)
10634 (move-marker org-open-link-marker (point))
10635 (setq org-window-config-before-follow-link (current-window-configuration))
10636 (org-remove-occur-highlights nil nil t)
10637 (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
10638 (let* ((context (org-element-context)) type value)
10639 ;; On an unsupported type, check if point is contained within
10641 (while (and (not (memq (setq type (org-element-type context))
10642 '(comment comment-block
10643 headline inlinetask link
10644 footnote-definition footnote-reference
10645 node-property timestamp)))
10646 (setq context (org-element-property :parent context))))
10647 (setq value (org-element-property :value context))
10649 ;; Blank lines at the beginning of buffer: bail out.
10650 ((not context) (user-error "No link found"))
10651 ;; Exception n°1: links in property drawers
10652 ((eq type 'node-property)
10653 (org-open-link-from-string
10654 (and (string-match org-any-link-re value)
10655 (match-string-no-properties 0 value))))
10656 ;; Exception n°2: links in comments.
10657 ((memq type '(comment comment-block))
10659 (skip-chars-forward "\\S-" (point-at-eol))
10660 (let ((string-rear (replace-regexp-in-string
10661 "^[ \t]*# [ \t]*" ""
10662 (buffer-substring (point) (line-beginning-position))))
10663 (string-front (buffer-substring (point) (line-end-position))))
10665 (let ((org-inhibit-startup t)) (org-mode))
10667 (goto-char (point-min))
10668 (when (and (search-forward string-rear nil t)
10669 (search-forward string-front (line-end-position) t))
10670 (goto-char (match-beginning 0))
10671 (org-open-at-point)
10672 (when (string= string-rear "") (forward-char)))))))
10673 ;; On a headline or an inlinetask, but not on a timestamp,
10674 ;; a link, a footnote reference or on tags.
10675 ((and (memq type '(headline inlinetask))
10677 (progn (save-excursion (beginning-of-line)
10678 (looking-at org-complex-heading-regexp))
10679 (or (not (match-beginning 5))
10680 (< (point) (match-beginning 5)))))
10681 (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
10683 (links-end (cdr data)))
10685 (dolist (link (if (stringp links) (list links) links))
10686 (search-forward link nil links-end)
10687 (goto-char (match-beginning 0))
10688 (org-open-at-point))
10689 (require 'org-attach)
10690 (org-attach-reveal 'if-exists))))
10691 ;; Do nothing on white spaces after an object, unless point
10692 ;; is right after it.
10695 (goto-char (org-element-property :end context))
10696 (skip-chars-backward " \t")
10698 (user-error "No link found"))
10699 ((eq type 'timestamp) (org-follow-timestamp-link))
10700 ;; On tags within a headline or an inlinetask.
10701 ((and (memq type '(headline inlinetask))
10702 (progn (save-excursion (beginning-of-line)
10703 (looking-at org-complex-heading-regexp))
10704 (and (match-beginning 5)
10705 (>= (point) (match-beginning 5)))))
10706 (org-tags-view arg (substring (match-string 5) 0 -1)))
10708 (let ((type (org-element-property :type context))
10709 (path (org-link-unescape (org-element-property :path context))))
10710 ;; Switch back to REFERENCE-BUFFER needed when called in
10711 ;; a temporary buffer through `org-open-link-from-string'.
10712 (with-current-buffer (or reference-buffer (current-buffer))
10714 ((equal type "file")
10715 (if (string-match "[*?{]" (file-name-nondirectory path))
10717 ;; Look into `org-link-protocols' in order to find
10718 ;; a DEDICATED-FUNCTION to open file. The function
10719 ;; will be applied on raw link instead of parsed
10720 ;; link due to the limitation in `org-add-link-type'
10721 ;; ("open" function called with a single argument).
10722 ;; If no such function is found, fallback to
10723 ;; `org-open-file'.
10725 ;; Note : "file+emacs" and "file+sys" types are
10726 ;; hard-coded in order to escape the previous
10728 (let* ((option (org-element-property :search-option context))
10729 (app (org-element-property :application context))
10730 (dedicated-function
10731 (nth 1 (assoc app org-link-protocols))))
10732 (if dedicated-function
10733 (funcall dedicated-function
10735 (and option (concat "::" option))))
10736 (apply 'org-open-file
10739 ((equal app "emacs") 'emacs)
10740 ((equal app "sys") 'system))
10741 (cond ((not option) nil)
10742 ((org-string-match-p "\\`[0-9]+\\'" option)
10743 (list (string-to-number option)))
10745 (org-link-unescape option)))))))))
10746 ((assoc type org-link-protocols)
10747 (funcall (nth 1 (assoc type org-link-protocols)) path))
10748 ((equal type "help")
10749 (let ((f-or-v (intern path)))
10750 (cond ((fboundp f-or-v) (describe-function f-or-v))
10751 ((boundp f-or-v) (describe-variable f-or-v))
10752 (t (error "Not a known function or variable")))))
10753 ((member type '("http" "https" "ftp" "mailto" "news"))
10754 (browse-url (org-link-escape-browser (concat type ":" path))))
10755 ((equal type "doi")
10757 (org-link-escape-browser (concat org-doi-server-url path))))
10758 ((equal type "message") (browse-url (concat type ":" path)))
10759 ((equal type "shell")
10760 (let ((buf (generate-new-buffer "*Org Shell Output*"))
10762 (if (or (and (org-string-nw-p
10763 org-confirm-shell-link-not-regexp)
10765 org-confirm-shell-link-not-regexp cmd))
10766 (not org-confirm-shell-link-function)
10767 (funcall org-confirm-shell-link-function
10768 (format "Execute \"%s\" in shell? "
10769 (org-add-props cmd nil
10770 'face 'org-warning))))
10772 (message "Executing %s" cmd)
10773 (shell-command cmd buf)
10774 (when (featurep 'midnight)
10775 (setq clean-buffer-list-kill-buffer-names
10777 clean-buffer-list-kill-buffer-names))))
10778 (user-error "Abort"))))
10779 ((equal type "elisp")
10781 (if (or (and (org-string-nw-p
10782 org-confirm-elisp-link-not-regexp)
10783 (org-string-match-p
10784 org-confirm-elisp-link-not-regexp cmd))
10785 (not org-confirm-elisp-link-function)
10786 (funcall org-confirm-elisp-link-function
10787 (format "Execute \"%s\" as elisp? "
10788 (org-add-props cmd nil
10789 'face 'org-warning))))
10790 (message "%s => %s" cmd
10791 (if (eq (string-to-char cmd) ?\()
10793 (call-interactively (read cmd))))
10794 (user-error "Abort"))))
10797 (funcall (nth 1 (assoc "id" org-link-protocols)) path))
10798 ((member type '("coderef" "custom-id" "fuzzy" "radio"))
10799 (unless (run-hook-with-args-until-success
10800 'org-open-link-functions path)
10801 (if (not arg) (org-mark-ring-push)
10802 (switch-to-buffer-other-window
10803 (org-get-buffer-for-internal-link (current-buffer))))
10804 (let ((cmd `(org-link-search
10805 ,(if (member type '("custom-id" "coderef"))
10806 (org-element-property :raw-link context)
10808 ,(cond ((equal arg '(4)) 'occur)
10809 ((equal arg '(16)) 'org-occur))
10810 ,(org-element-property :begin context))))
10811 (condition-case nil
10812 (let ((org-link-search-inhibit-query t))
10814 (error (progn (widen) (eval cmd)))))))
10815 (t (browse-url-at-point))))))
10816 ;; On a footnote reference or at a footnote definition's label.
10817 ((or (eq type 'footnote-reference)
10818 (and (eq type 'footnote-definition)
10820 ;; Do not validate action when point is on the
10821 ;; spaces right after the footnote label, in
10822 ;; order to be on par with behaviour on links.
10823 (skip-chars-forward " \t")
10825 (org-element-property :contents-begin context)))
10826 (if begin (< (point) begin)
10827 (= (org-element-property :post-affiliated context)
10828 (line-beginning-position)))))))
10829 (org-footnote-action))
10830 (t (user-error "No link found")))))
10831 (move-marker org-open-link-marker nil)
10832 (run-hook-with-args 'org-follow-link-hook)))
10834 (defun org-offer-links-in-entry (buffer marker &optional nth zero)
10835 "Offer links in the current entry and return the selected link.
10836 If there is only one link, return it.
10837 If NTH is an integer, return the NTH link found.
10838 If ZERO is a string, check also this string for a link, and if
10839 there is one, return it."
10840 (with-current-buffer buffer
10846 (in-emacs (if (integerp nth) nil nth))
10847 have-zero end links link c)
10848 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
10849 (push (match-string 0 zero) links)
10850 (setq cnt (1- cnt) have-zero t))
10852 (org-back-to-heading t)
10853 (setq end (save-excursion (outline-next-heading) (point)))
10854 (while (re-search-forward org-any-link-re end t)
10855 (push (match-string 0) links))
10856 (setq links (org-uniquify (reverse links))))
10859 (message "No links"))
10860 ((equal (length links) 1)
10861 (setq link (car links)))
10862 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
10863 (setq link (nth (if have-zero nth (1- nth)) links)))
10864 (t ; we have to select a link
10866 (save-window-excursion
10867 (delete-other-windows)
10868 (with-output-to-temp-buffer "*Select Link*"
10870 (if (not (string-match org-bracket-link-regexp l))
10871 (princ (format "[%c] %s\n" (incf cnt)
10872 (org-remove-angle-brackets l)))
10874 (princ (format "[%c] %s (%s)\n" (incf cnt)
10875 (match-string 3 l) (match-string 1 l)))
10876 (princ (format "[%c] %s\n" (incf cnt)
10877 (match-string 1 l))))))
10879 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
10880 (message "Select link to open, RET to open all:")
10881 (setq c (read-char-exclusive))
10882 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
10883 (when (equal c ?q) (user-error "Abort"))
10884 (if (equal c ?\C-m)
10886 (setq nth (- c ?0))
10887 (if have-zero (setq nth (1+ nth)))
10888 (unless (and (integerp nth) (>= (length links) nth))
10889 (user-error "Invalid link selection"))
10890 (setq link (nth (1- nth) links)))))
10891 (cons link end))))))
10893 ;; TODO: These functions are deprecated since `org-open-at-point'
10894 ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
10895 (defun org-open-file-with-system (path)
10896 "Open file at PATH using the system way of opening it."
10897 (org-open-file path 'system))
10898 (defun org-open-file-with-emacs (path)
10899 "Open file at PATH in Emacs."
10900 (org-open-file path 'emacs))
10905 (defvar org-create-file-search-functions nil
10906 "List of functions to construct the right search string for a file link.
10907 These functions are called in turn with point at the location to
10908 which the link should point.
10910 A function in the hook should first test if it would like to
10911 handle this file type, for example by checking the `major-mode'
10912 or the file extension. If it decides not to handle this file, it
10913 should just return nil to give other functions a chance. If it
10914 does handle the file, it must return the search string to be used
10915 when following the link. The search string will be part of the
10916 file link, given after a double colon, and `org-open-at-point'
10917 will automatically search for it. If special measures must be
10918 taken to make the search successful, another function should be
10919 added to the companion hook `org-execute-file-search-functions',
10922 A function in this hook may also use `setq' to set the variable
10923 `description' to provide a suggestion for the descriptive text to
10924 be used for this link when it gets inserted into an Org-mode
10925 buffer with \\[org-insert-link].")
10927 (defvar org-execute-file-search-functions nil
10928 "List of functions to execute a file search triggered by a link.
10930 Functions added to this hook must accept a single argument, the
10931 search string that was part of the file link, the part after the
10932 double colon. The function must first check if it would like to
10933 handle this search, for example by checking the `major-mode' or
10934 the file extension. If it decides not to handle this search, it
10935 should just return nil to give other functions a chance. If it
10936 does handle the search, it must return a non-nil value to keep
10937 other functions from trying.
10939 Each function can access the current prefix argument through the
10940 variable `current-prefix-arg'. Note that a single prefix is used
10941 to force opening a link in Emacs, so it may be good to only use a
10942 numeric or double prefix to guide the search function.
10944 In case this is needed, a function in this hook can also restore
10945 the window configuration before `org-open-at-point' was called using:
10947 (set-window-configuration org-window-config-before-follow-link)")
10949 (defun org-link-search (s &optional type avoid-pos stealth)
10950 "Search for a link search option.
10951 If S is surrounded by forward slashes, it is interpreted as a
10952 regular expression. In org-mode files, this will create an `org-occur'
10953 sparse tree. In ordinary files, `occur' will be used to list matches.
10954 If the current buffer is in `dired-mode', grep will be used to search
10955 in all files. If AVOID-POS is given, ignore matches near that position.
10957 When optional argument STEALTH is non-nil, do not modify
10958 visibility around point, thus ignoring
10959 `org-show-hierarchy-above', `org-show-following-heading' and
10960 `org-show-siblings' variables."
10961 (let ((case-fold-search t)
10962 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
10963 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
10964 (append '(("") (" ") ("\t") ("\n"))
10965 org-emphasis-alist)
10968 (pre nil) (post nil)
10969 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
10971 ;; First check if there are any special search functions
10972 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
10973 ;; Now try the builtin stuff
10974 ((and (equal (string-to-char s0) ?#)
10977 (goto-char (point-min))
10980 (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
10981 (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
10982 (setq type 'dedicated
10983 pos (match-beginning 0))))
10984 ;; There is an exact target for this
10986 (org-back-to-heading t)))
10988 (goto-char (point-min))
10991 (concat "<<" (regexp-quote s0) ">>") nil t)
10992 (setq type 'dedicated
10993 pos (match-beginning 0))))
10994 ;; There is an exact target for this
10997 (goto-char (point-min))
11000 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
11001 (setq type 'dedicated pos (match-beginning 0))))
11002 ;; Found an element with a matching #+name affiliated keyword.
11004 ((and (string-match "^(\\(.*\\))$" s0)
11006 (goto-char (point-min))
11009 (concat "[^[]" (regexp-quote
11010 (format org-coderef-label-format
11011 (match-string 1 s0))))
11013 (setq type 'dedicated
11014 pos (1+ (match-beginning 0))))))
11015 ;; There is a coderef target for this
11017 ((string-match "^/\\(.*\\)/$" s)
11018 ;; A regular expression
11020 ((derived-mode-p 'org-mode)
11021 (org-occur (match-string 1 s)))
11022 (t (org-do-occur (match-string 1 s)))))
11023 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
11024 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
11025 (goto-char (point-min))
11027 ((let (case-fold-search)
11028 (re-search-forward (format org-complex-heading-regexp-format
11031 ;; OK, found a match
11032 (setq type 'dedicated)
11033 (goto-char (match-beginning 0)))
11034 ((and (not org-link-search-inhibit-query)
11035 (eq org-link-search-must-match-exact-headline 'query-to-create)
11036 (y-or-n-p "No match - create this as a new heading? "))
11037 (goto-char (point-max))
11038 (or (bolp) (newline))
11039 (insert "* " s "\n")
11040 (beginning-of-line 0))
11043 (error "No match"))))
11045 ;; A normal search string
11046 (when (equal (string-to-char s) ?*)
11047 ;; Anchor on headlines, post may include tags.
11048 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
11049 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
11050 s (substring s 1)))
11051 (remove-text-properties
11053 '(face nil mouse-face nil keymap nil fontified nil) s)
11054 ;; Make a series of regular expressions to find a match
11055 (setq words (org-split-string s "[ \n\r\t]+")
11057 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
11058 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
11060 re2a_ (concat "\\(" (mapconcat 'downcase words
11061 "[ \t\r\n]+") "\\)[ \t\r\n]")
11062 re2a (concat "[ \t\r\n]" re2a_)
11063 re4_ (concat "\\(" (mapconcat 'downcase words
11064 "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
11065 re4 (concat "[^a-zA-Z_]" re4_)
11067 re1 (concat pre re2 post)
11068 re3 (concat pre (if pre re4_ re4) post)
11069 re5 (concat pre ".*" re4)
11070 re2 (concat pre re2)
11071 re2a (concat pre (if pre re2a_ re2a))
11072 re4 (concat pre (if pre re4_ re4))
11073 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
11074 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
11077 ((eq type 'org-occur) (org-occur reall))
11078 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
11079 (t (goto-char (point-min))
11081 (if (or (and (org-search-not-self 1 re0 nil t)
11082 (setq type 'dedicated))
11083 (org-search-not-self 1 re1 nil t)
11084 (org-search-not-self 1 re2 nil t)
11085 (org-search-not-self 1 re2a nil t)
11086 (org-search-not-self 1 re3 nil t)
11087 (org-search-not-self 1 re4 nil t)
11088 (org-search-not-self 1 re5 nil t))
11089 (goto-char (match-beginning 1))
11091 (error "No match"))))))
11092 (and (derived-mode-p 'org-mode)
11094 (org-show-context 'link-search))
11097 (defun org-search-not-self (group &rest args)
11098 "Execute `re-search-forward', but only accept matches that do not
11099 enclose the position of `org-open-link-marker'."
11100 (let ((m org-open-link-marker))
11102 (while (apply 're-search-forward args)
11103 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
11104 (goto-char (match-end group))
11105 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
11106 (> (match-beginning 0) (marker-position m))
11107 (< (match-end 0) (marker-position m)))
11109 (or (not (org-in-regexp
11110 org-bracket-link-analytic-regexp 1))
11111 (not (match-end 4)) ; no description
11112 (and (<= (match-beginning 4) (point))
11113 (>= (match-end 4) (point))))))
11114 (throw 'exit (point))))))))
11116 (defun org-get-buffer-for-internal-link (buffer)
11117 "Return a buffer to be used for displaying the link target of internal links."
11119 ((not org-display-internal-link-with-indirect-buffer)
11121 ((string-match "(Clone)$" (buffer-name buffer))
11122 (message "Buffer is already a clone, not making another one")
11123 ;; we also do not modify visibility in this case
11125 (t ; make a new indirect buffer for displaying the link
11126 (let* ((bn (buffer-name buffer))
11127 (ibn (concat bn "(Clone)"))
11128 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
11129 (with-current-buffer ib (org-overview))
11132 (defun org-do-occur (regexp &optional cleanup)
11133 "Call the Emacs command `occur'.
11134 If CLEANUP is non-nil, remove the printout of the regular expression
11135 in the *Occur* buffer. This is useful if the regex is long and not useful
11139 (let ((cwin (selected-window)) win beg end)
11140 (when (setq win (get-buffer-window "*Occur*"))
11141 (select-window win))
11142 (goto-char (point-min))
11143 (when (re-search-forward "match[a-z]+" nil t)
11144 (setq beg (match-end 0))
11145 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
11146 (setq end (1- (match-beginning 0)))))
11147 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
11148 (goto-char (point-min))
11149 (select-window cwin))))
11151 ;;; The mark ring for links jumps
11153 (defvar org-mark-ring nil
11154 "Mark ring for positions before jumps in Org-mode.")
11155 (defvar org-mark-ring-last-goto nil
11156 "Last position in the mark ring used to go back.")
11157 ;; Fill and close the ring
11158 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
11159 (loop for i from 1 to org-mark-ring-length do
11160 (push (make-marker) org-mark-ring))
11161 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
11164 (defun org-mark-ring-push (&optional pos buffer)
11165 "Put the current position or POS into the mark ring and rotate it."
11167 (setq pos (or pos (point)))
11168 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
11169 (move-marker (car org-mark-ring)
11171 (or buffer (current-buffer)))
11173 (substitute-command-keys
11174 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
11176 (defun org-mark-ring-goto (&optional n)
11177 "Jump to the previous position in the mark ring.
11178 With prefix arg N, jump back that many stored positions. When
11179 called several times in succession, walk through the entire ring.
11180 Org-mode commands jumping to a different position in the current file,
11181 or to another Org-mode file, automatically push the old position
11185 (if (eq last-command this-command)
11186 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
11187 (setq p org-mark-ring))
11188 (setq org-mark-ring-last-goto p)
11190 (org-pop-to-buffer-same-window (marker-buffer m))
11192 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
11194 (defun org-remove-angle-brackets (s)
11195 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
11196 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
11198 (defun org-add-angle-brackets (s)
11199 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
11200 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
11202 (defun org-remove-double-quotes (s)
11203 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
11204 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
11207 ;;; Following specific links
11209 (defun org-follow-timestamp-link ()
11210 "Open an agenda view for the time-stamp date/range at point."
11212 ((org-at-date-range-p t)
11213 (let ((org-agenda-start-on-weekday)
11214 (t1 (match-string 1))
11215 (t2 (match-string 2)) tt1 tt2)
11216 (setq tt1 (time-to-days (org-time-string-to-time t1))
11217 tt2 (time-to-days (org-time-string-to-time t2)))
11218 (let ((org-agenda-buffer-tmp-name
11219 (format "*Org Agenda(a:%s)"
11220 (concat (substring t1 0 10) "--" (substring t2 0 10)))))
11221 (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
11222 ((org-at-timestamp-p t)
11223 (let ((org-agenda-buffer-tmp-name
11224 (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
11225 (org-agenda-list nil (time-to-days (org-time-string-to-time
11226 (substring (match-string 1) 0 10)))
11228 (t (error "This should not happen"))))
11231 ;;; Following file links
11232 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
11233 (declare-function mailcap-extension-to-mime "mailcap" (extn))
11234 (declare-function mailcap-mime-info
11235 "mailcap" (string &optional request no-decode))
11236 (defvar org-wait nil)
11237 (defun org-open-file (path &optional in-emacs line search)
11238 "Open the file at PATH.
11239 First, this expands any special file name abbreviations. Then the
11240 configuration variable `org-file-apps' is checked if it contains an
11241 entry for this file type, and if yes, the corresponding command is launched.
11243 If no application is found, Emacs simply visits the file.
11245 With optional prefix argument IN-EMACS, Emacs will visit the file.
11246 With a double \\[universal-argument] \\[universal-argument] \
11247 prefix arg, Org tries to avoid opening in Emacs
11248 and to use an external application to visit the file.
11250 Optional LINE specifies a line to go to, optional SEARCH a string
11251 to search for. If LINE or SEARCH is given, the file will be
11252 opened in Emacs, unless an entry from org-file-apps that makes
11253 use of groups in a regexp matches.
11255 If you want to change the way frames are used when following a
11256 link, please customize `org-link-frame-setup'.
11258 If the file does not exist, an error is thrown."
11259 (let* ((file (if (equal path "")
11261 (substitute-in-file-name (expand-file-name path))))
11262 (file-apps (append org-file-apps (org-default-apps)))
11263 (apps (org-remove-if
11264 'org-file-apps-entry-match-against-dlink-p file-apps))
11265 (apps-dlink (org-remove-if-not
11266 'org-file-apps-entry-match-against-dlink-p file-apps))
11267 (remp (and (assq 'remote apps) (org-file-remote-p file)))
11268 (dirp (if remp nil (file-directory-p file)))
11269 (file (if (and dirp org-open-directory-means-index-dot-org)
11270 (concat (file-name-as-directory file) "index.org")
11272 (a-m-a-p (assq 'auto-mode apps))
11273 (dfile (downcase file))
11274 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
11275 (link (cond ((and (eq line nil)
11279 (concat file "::" (number-to-string line)))
11281 (concat file "::" search))))
11282 (dlink (downcase link))
11283 (old-buffer (current-buffer))
11285 (old-mode major-mode)
11286 ext cmd link-match-data)
11287 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
11288 (setq ext (match-string 1 dfile))
11289 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
11290 (setq ext (match-string 1 dfile))))
11292 ((member in-emacs '((16) system))
11293 (setq cmd (cdr (assoc 'system apps))))
11294 (in-emacs (setq cmd 'emacs))
11296 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
11297 (and dirp (cdr (assoc 'directory apps)))
11298 ; first, try matching against apps-dlink
11299 ; if we get a match here, store the match data for later
11300 (let ((match (assoc-default dlink apps-dlink
11303 (progn (setq link-match-data (match-data))
11305 (progn (setq in-emacs (or in-emacs line search))
11306 nil))) ; if we have no match in apps-dlink,
11307 ; always open the file in emacs if line or search
11308 ; is given (for backwards compatibility)
11309 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
11311 (cdr (assoc ext apps))
11312 (cdr (assoc t apps))))))
11313 (when (eq cmd 'system)
11314 (setq cmd (cdr (assoc 'system apps))))
11315 (when (eq cmd 'default)
11316 (setq cmd (cdr (assoc t apps))))
11317 (when (eq cmd 'mailcap)
11319 (mailcap-parse-mailcaps)
11320 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
11321 (command (mailcap-mime-info mime-type)))
11322 (if (stringp command)
11324 (setq cmd 'emacs))))
11325 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
11326 (not (file-exists-p file))
11327 (not org-open-non-existing-files))
11328 (user-error "No such file: %s" file))
11330 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
11331 ;; Remove quotes around the file name - we'll use shell-quote-argument.
11332 (while (string-match "['\"]%s['\"]" cmd)
11333 (setq cmd (replace-match "%s" t t cmd)))
11334 (while (string-match "%s" cmd)
11335 (setq cmd (replace-match
11337 (shell-quote-argument
11338 (convert-standard-filename file)))
11341 ;; Replace "%1", "%2" etc. in command with group matches from regex
11343 (let ((match-index 1)
11344 (number-of-groups (- (/ (length link-match-data) 2) 1)))
11345 (set-match-data link-match-data)
11346 (while (<= match-index number-of-groups)
11347 (let ((regex (concat "%" (number-to-string match-index)))
11348 (replace-with (match-string match-index dlink)))
11349 (while (string-match regex cmd)
11350 (setq cmd (replace-match replace-with t t cmd))))
11351 (setq match-index (+ match-index 1)))))
11353 (save-window-excursion
11354 (message "Running %s...done" cmd)
11355 (start-process-shell-command cmd nil cmd)
11356 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
11359 (funcall (cdr (assq 'file org-link-frame-setup)) file)
11361 (if line (progn (org-goto-line line)
11362 (if (derived-mode-p 'org-mode)
11364 (if search (org-link-search search))))
11366 (let ((file (convert-standard-filename file)))
11368 (set-match-data link-match-data)
11370 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
11371 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
11372 (or (not (equal old-buffer (current-buffer)))
11373 (not (equal old-pos (point))))
11374 (org-mark-ring-push old-pos old-buffer))))
11376 (defun org-file-apps-entry-match-against-dlink-p (entry)
11377 "This function returns non-nil if `entry' uses a regular
11378 expression which should be matched against the whole link by
11381 It assumes that is the case when the entry uses a regular
11382 expression which has at least one grouping construct and the
11383 action is either a lisp form or a command string containing
11384 '%1', i.e. using at least one subexpression match as a
11386 (let ((selector (car entry))
11387 (action (cdr entry)))
11388 (if (stringp selector)
11389 (and (> (regexp-opt-depth selector) 0)
11390 (or (and (stringp action)
11391 (string-match "%[0-9]" action))
11395 (defun org-default-apps ()
11396 "Return the default applications for this operating system."
11398 ((eq system-type 'darwin)
11399 org-file-apps-defaults-macosx)
11400 ((eq system-type 'windows-nt)
11401 org-file-apps-defaults-windowsnt)
11402 (t org-file-apps-defaults-gnu)))
11404 (defun org-apps-regexp-alist (list &optional add-auto-mode)
11405 "Convert extensions to regular expressions in the cars of LIST.
11406 Also, weed out any non-string entries, because the return value is used
11407 only for regexp matching.
11408 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
11409 point to the symbol `emacs', indicating that the file should
11410 be opened in Emacs."
11413 (mapcar (lambda (x)
11414 (if (not (stringp (car x)))
11416 (if (string-match "\\W" (car x))
11418 (cons (concat "\\." (car x) "\\'") (cdr x)))))
11421 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
11423 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
11424 (defun org-file-remote-p (file)
11425 "Test whether FILE specifies a location on a remote system.
11426 Return non-nil if the location is indeed remote.
11428 For example, the filename \"/user@host:/foo\" specifies a location
11429 on the system \"/user@host:\"."
11430 (cond ((fboundp 'file-remote-p)
11431 (file-remote-p file))
11432 ((fboundp 'tramp-handle-file-remote-p)
11433 (tramp-handle-file-remote-p file))
11434 ((and (boundp 'ange-ftp-name-format)
11435 (string-match (car ange-ftp-name-format) file))
11441 (defun org-get-org-file ()
11442 "Read a filename, with default directory `org-directory'."
11443 (let ((default (or org-default-notes-file remember-data-file)))
11444 (read-file-name (format "File name [%s]: " default)
11445 (file-name-as-directory org-directory)
11448 (defun org-notes-order-reversed-p ()
11449 "Check if the current file should receive notes in reversed order."
11451 ((not org-reverse-note-order) nil)
11452 ((eq t org-reverse-note-order) t)
11453 ((not (listp org-reverse-note-order)) nil)
11455 (let ((all org-reverse-note-order)
11457 (while (setq entry (pop all))
11458 (if (string-match (car entry) buffer-file-name)
11459 (throw 'exit (cdr entry))))
11462 (defvar org-refile-target-table nil
11463 "The list of refile targets, created by `org-refile'.")
11465 (defvar org-agenda-new-buffers nil
11466 "Buffers created to visit agenda files.")
11468 (defvar org-refile-cache nil
11469 "Cache for refile targets.")
11471 (defvar org-refile-markers nil
11472 "All the markers used for caching refile locations.")
11474 (defun org-refile-marker (pos)
11475 "Get a new refile marker, but only if caching is in use."
11476 (if (not org-refile-use-cache)
11478 (let ((m (make-marker)))
11479 (move-marker m pos)
11480 (push m org-refile-markers)
11483 (defun org-refile-cache-clear ()
11484 "Clear the refile cache and disable all the markers."
11485 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
11486 (setq org-refile-markers nil)
11487 (setq org-refile-cache nil)
11488 (message "Refile cache has been cleared"))
11490 (defun org-refile-cache-check-set (set)
11491 "Check if all the markers in the cache still have live buffers."
11494 (while (and set (setq marker (nth 3 (pop set))))
11495 ;; If `org-refile-use-outline-path' is 'file, marker may be nil
11496 (when (and marker (null (marker-buffer marker)))
11497 (message "Please regenerate the refile cache with `C-0 C-c C-w'")
11499 (throw 'exit nil)))
11502 (defun org-refile-cache-put (set &rest identifiers)
11503 "Push the refile targets SET into the cache, under IDENTIFIERS."
11504 (let* ((key (sha1 (prin1-to-string identifiers)))
11505 (entry (assoc key org-refile-cache)))
11508 (push (cons key set) org-refile-cache))))
11510 (defun org-refile-cache-get (&rest identifiers)
11511 "Retrieve the cached value for refile targets given by IDENTIFIERS."
11513 ((not org-refile-cache) nil)
11514 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
11516 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
11517 org-refile-cache))))
11518 (and set (org-refile-cache-check-set set) set)))))
11520 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
11521 "Produce a table with refile targets."
11522 (let ((case-fold-search nil)
11523 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
11524 (entries (or org-refile-targets '((nil . (:level . 1)))))
11525 targets tgs txt re files f desc descre fast-path-p level pos0)
11526 (message "Getting targets...")
11527 (with-current-buffer (or default-buffer (current-buffer))
11528 (while (setq entry (pop entries))
11529 (setq files (car entry) desc (cdr entry))
11530 (setq fast-path-p nil)
11532 ((null files) (setq files (list (current-buffer))))
11533 ((eq files 'org-agenda-files)
11534 (setq files (org-agenda-files 'unrestricted)))
11535 ((and (symbolp files) (fboundp files))
11536 (setq files (funcall files)))
11537 ((and (symbolp files) (boundp files))
11538 (setq files (symbol-value files))))
11539 (if (stringp files) (setq files (list files)))
11541 ((eq (car desc) :tag)
11542 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
11543 ((eq (car desc) :todo)
11544 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
11545 ((eq (car desc) :regexp)
11546 (setq descre (cdr desc)))
11547 ((eq (car desc) :level)
11548 (setq descre (concat "^\\*\\{" (number-to-string
11549 (if org-odd-levels-only
11550 (1- (* 2 (cdr desc)))
11553 ((eq (car desc) :maxlevel)
11554 (setq fast-path-p t)
11555 (setq descre (concat "^\\*\\{1," (number-to-string
11556 (if org-odd-levels-only
11557 (1- (* 2 (cdr desc)))
11560 (t (error "Bad refiling target description %s" desc)))
11561 (while (setq f (pop files))
11562 (with-current-buffer
11563 (if (bufferp f) f (org-get-agenda-file-buffer f))
11565 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
11567 (if (bufferp f) (setq f (buffer-file-name
11568 (buffer-base-buffer f))))
11569 (setq f (and f (expand-file-name f)))
11570 (if (eq org-refile-use-outline-path 'file)
11571 (push (list (file-name-nondirectory f) f nil nil) tgs))
11575 (goto-char (point-min))
11576 (while (re-search-forward descre nil t)
11577 (goto-char (setq pos0 (point-at-bol)))
11579 (when org-refile-target-verify-function
11581 (or (funcall org-refile-target-verify-function)
11583 (when (and (looking-at org-complex-heading-regexp)
11584 (not (member (match-string 4) excluded-entries))
11586 (setq level (org-reduced-level
11587 (- (match-end 1) (match-beginning 1)))
11588 txt (org-link-display-format (match-string 4))
11589 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
11590 re (format org-complex-heading-regexp-format
11591 (regexp-quote (match-string 4))))
11592 (when org-refile-use-outline-path
11593 (setq txt (mapconcat
11596 (if (eq org-refile-use-outline-path
11598 (list (file-name-nondirectory
11600 (buffer-base-buffer))))
11601 (if (eq org-refile-use-outline-path
11603 (list (buffer-file-name
11604 (buffer-base-buffer)))))
11605 (org-get-outline-path fast-path-p
11609 (push (list txt f re (org-refile-marker (point)))
11611 (when (= (point) pos0)
11612 ;; verification function has not moved point
11613 (goto-char (point-at-eol))))))))
11614 (when org-refile-use-cache
11615 (org-refile-cache-put tgs (buffer-file-name) descre))
11616 (setq targets (append tgs targets))))))
11617 (message "Getting targets...done")
11618 (nreverse targets)))
11620 (defun org-protect-slash (s)
11621 (while (string-match "/" s)
11622 (setq s (replace-match "\\" t t s)))
11625 (defvar org-olpa (make-vector 20 nil))
11627 (defun org-get-outline-path (&optional fastp level heading)
11628 "Return the outline path to the current entry, as a list.
11630 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
11631 routine which makes outline path derivations for an entire file,
11632 avoiding backtracing. Refile target collection makes use of that."
11636 (error "Outline path failure, more than 19 levels"))
11637 (loop for i from level upto 19 do
11638 (aset org-olpa i nil))
11640 (delq nil (append org-olpa nil))
11641 (aset org-olpa level heading)))
11642 (let (rtn case-fold-search)
11646 (while (org-up-heading-safe)
11647 (when (looking-at org-complex-heading-regexp)
11649 (replace-regexp-in-string
11650 ;; Remove statistical/checkboxes cookies
11651 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
11652 (org-match-string-no-properties 4)))
11656 (defun org-format-outline-path (path &optional width prefix separator)
11657 "Format the outline path PATH for display.
11658 WIDTH is the maximum number of characters that is available.
11659 PREFIX is a prefix to be included in the returned string,
11660 such as the file name.
11661 SEPARATOR is inserted between the different parts of the path,
11662 the default is \"/\"."
11663 (setq width (or width 79))
11664 (if prefix (setq width (- width (length prefix))))
11667 (let* ((nsteps (length path))
11668 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
11669 (maxwidth (if (<= total-width width)
11670 10000 ;; everything fits
11671 ;; we need to shorten the level headings
11672 (/ (- width nsteps) nsteps)))
11673 (org-odd-levels-only nil)
11675 (total (1+ (length prefix))))
11676 (setq maxwidth (max maxwidth 10))
11678 (if prefix (or separator "/"))
11682 (if (and (= n nsteps) (< maxwidth 10000))
11683 (setq maxwidth (- total-width total)))
11684 (if (< (length h) maxwidth)
11685 (progn (setq total (+ total (length h) 1)) h)
11686 (setq h (substring h 0 (- maxwidth 2))
11687 total (+ total maxwidth 1))
11688 (if (string-match "[ \t]+\\'" h)
11689 (setq h (substring h 0 (match-beginning 0))))
11690 (setq h (concat h "..")))
11691 (org-add-props h nil 'face
11692 (nth (% (1- n) org-n-level-faces)
11695 path (or separator "/"))))))
11697 (defun org-display-outline-path (&optional file current separator just-return-string)
11698 "Display the current outline path in the echo area.
11700 If FILE is non-nil, prepend the output with the file name.
11701 If CURRENT is non-nil, append the current heading to the output.
11702 SEPARATOR is passed through to `org-format-outline-path'. It separates
11703 the different parts of the path and defaults to \"/\".
11704 If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
11706 (let* (case-fold-search
11707 (bfn (buffer-file-name (buffer-base-buffer)))
11708 (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
11710 (if current (setq path (append path
11712 (org-back-to-heading t)
11713 (if (looking-at org-complex-heading-regexp)
11714 (list (match-string 4)))))))
11716 (org-format-outline-path
11719 (and file bfn (concat (file-name-nondirectory bfn) separator))
11721 (if just-return-string
11722 (org-no-properties res)
11723 (org-unlogged-message "%s" res))))
11725 (defvar org-refile-history nil
11726 "History for refiling operations.")
11728 (defvar org-after-refile-insert-hook nil
11729 "Hook run after `org-refile' has inserted its stuff at the new location.
11730 Note that this is still *before* the stuff will be removed from
11731 the *old* location.")
11733 (defvar org-capture-last-stored-marker)
11734 (defvar org-refile-keep nil
11735 "Non-nil means `org-refile' will copy instead of refile.")
11738 "Like `org-refile', but copy."
11740 (let ((org-refile-keep t))
11741 (funcall 'org-refile nil nil nil "Copy")))
11743 (defun org-refile (&optional arg default-buffer rfloc msg)
11744 "Move the entry or entries at point to another heading.
11745 The list of target headings is compiled using the information in
11746 `org-refile-targets', which see.
11748 At the target location, the entry is filed as a subitem of the
11749 target heading. Depending on `org-reverse-note-order', the new
11750 subitem will either be the first or the last subitem.
11752 If there is an active region, all entries in that region will be
11753 refiled. However, the region must fulfill the requirement that
11754 the first heading sets the top-level of the moved text.
11756 With prefix arg ARG, the command will only visit the target
11757 location and not actually move anything.
11759 With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
11760 refiling operation has put the subtree.
11762 With a numeric prefix argument of `2', refile to the running clock.
11764 With a numeric prefix argument of `3', emulate `org-refile-keep'
11765 being set to `t' and copy to the target location, don't move it.
11766 Beware that keeping refiled entries may result in duplicated ID
11769 RFLOC can be a refile location obtained in a different way.
11771 MSG is a string to replace \"Refile\" in the default prompt with
11772 another verb. E.g. `org-copy' sets this parameter to \"Copy\".
11774 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
11776 If you are using target caching (see `org-refile-use-cache'), you
11777 have to clear the target cache in order to find new targets.
11778 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
11779 prefix argument (`C-u C-u C-u C-c C-w')."
11781 (if (member arg '(0 (64)))
11782 (org-refile-cache-clear)
11783 (let* ((actionmsg (cond (msg msg)
11784 ((equal arg 3) "Refile (and keep)")
11786 (cbuf (current-buffer))
11787 (regionp (org-region-active-p))
11788 (region-start (and regionp (region-beginning)))
11789 (region-end (and regionp (region-end)))
11790 (filename (buffer-file-name (buffer-base-buffer cbuf)))
11791 (org-refile-keep (if (equal arg 3) t org-refile-keep))
11792 pos it nbuf file re level reversed)
11793 (setq last-command nil)
11795 (goto-char region-start)
11796 (or (bolp) (goto-char (point-at-bol)))
11797 (setq region-start (point))
11798 (unless (or (org-kill-is-subtree-p
11799 (buffer-substring region-start region-end))
11800 (prog1 org-refile-active-region-within-subtree
11801 (let ((s (point-at-eol)))
11802 (org-toggle-heading)
11803 (setq region-end (+ (- (point-at-eol) s) region-end)))))
11804 (user-error "The region is not a (sequence of) subtree(s)")))
11805 (if (equal arg '(16))
11806 (org-refile-goto-last-stored)
11809 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
11811 (setq it (list (or org-clock-heading "running clock")
11813 (marker-buffer org-clock-hd-marker))
11815 (marker-position org-clock-hd-marker)))
11818 (let (heading-text)
11820 (unless (and arg (listp arg))
11821 (org-back-to-heading t)
11823 (replace-regexp-in-string
11824 org-bracket-link-regexp
11826 (nth 4 (org-heading-components)))))
11827 (org-refile-get-location
11828 (cond ((and arg (listp arg)) "Goto")
11829 (regionp (concat actionmsg " region to"))
11830 (t (concat actionmsg " subtree \""
11831 heading-text "\" to")))
11833 (and (not (equal '(4) arg))
11834 org-refile-allow-creating-parent-nodes)
11836 (setq file (nth 1 it)
11841 (equal (buffer-file-name) file)
11843 (and (>= pos region-start)
11844 (<= pos region-end))
11845 (and (>= pos (point))
11846 (< pos (save-excursion
11847 (org-end-of-subtree t t))))))
11848 (error "Cannot refile to position inside the tree or region"))
11849 (setq nbuf (or (find-buffer-visiting file)
11850 (find-file-noselect file)))
11851 (if (and arg (not (equal arg 3)))
11853 (org-pop-to-buffer-same-window nbuf)
11855 (org-show-context 'org-goto))
11858 (org-kill-new (buffer-substring region-start region-end))
11859 (org-save-markers-in-region region-start region-end))
11860 (org-copy-subtree 1 nil t))
11861 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
11862 (find-file-noselect file)))
11863 (setq reversed (org-notes-order-reversed-p))
11870 (looking-at org-outline-regexp)
11871 (setq level (org-get-valid-level (funcall outline-level) 1))
11874 (or (outline-next-heading) (point-max))
11875 (or (save-excursion (org-get-next-sibling))
11876 (org-end-of-subtree t t)
11880 (goto-char (point-max))
11881 (goto-char (point-min))
11882 (or (outline-next-heading) (goto-char (point-max)))))
11883 (if (not (bolp)) (newline))
11884 (org-paste-subtree level nil nil t)
11885 (when org-log-refile
11886 (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
11887 (unless (eq org-log-refile 'note)
11888 (save-excursion (org-add-log-note))))
11889 (and org-auto-align-tags
11890 (let ((org-loop-over-headlines-in-active-region nil))
11891 (org-set-tags nil t)))
11892 (let ((bookmark-name (plist-get org-bookmark-names-plist
11894 (when bookmark-name
11895 (with-demoted-errors
11896 (bookmark-set bookmark-name))))
11897 ;; If we are refiling for capture, make sure that the
11898 ;; last-capture pointers point here
11899 (when (org-bound-and-true-p org-refile-for-capture)
11900 (let ((bookmark-name (plist-get org-bookmark-names-plist
11901 :last-capture-marker)))
11902 (when bookmark-name
11903 (with-demoted-errors
11904 (bookmark-set bookmark-name))))
11905 (move-marker org-capture-last-stored-marker (point)))
11906 (if (fboundp 'deactivate-mark) (deactivate-mark))
11907 (run-hooks 'org-after-refile-insert-hook))))
11908 (unless org-refile-keep
11910 (delete-region (point) (+ (point) (- region-end region-start)))
11912 (and (org-back-to-heading t) (point))
11913 (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
11914 (when (featurep 'org-inlinetask)
11915 (org-inlinetask-remove-END-maybe))
11916 (setq org-markers-to-move nil)
11917 (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
11919 (defun org-refile-goto-last-stored ()
11920 "Go to the location where the last refile was stored."
11922 (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
11923 (message "This is the location of the last refile"))
11925 (defun org-refile--get-location (refloc tbl)
11926 "When user refile to REFLOC, find the associated target in TBL.
11927 Also check `org-refile-target-table'."
11931 (lambda (r) (or (assoc r tbl)
11932 (assoc r org-refile-target-table)))
11933 (list (replace-regexp-in-string "/$" "" refloc)
11934 (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
11936 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
11938 "Prompt the user for a refile location, using PROMPT.
11939 PROMPT should not be suffixed with a colon and a space, because
11940 this function appends the default value from
11941 `org-refile-history' automatically, if that is not empty.
11942 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
11943 this is used for the GOTO interface."
11944 (let ((org-refile-targets org-refile-targets)
11945 (org-refile-use-outline-path org-refile-use-outline-path)
11947 (when (and (derived-mode-p 'org-mode)
11948 (not org-refile-use-cache)
11952 (setq excluded-entries
11953 (append excluded-entries (list (org-get-heading t t)))))))
11954 (setq org-refile-target-table
11955 (org-refile-get-targets default-buffer excluded-entries)))
11956 (unless org-refile-target-table
11957 (user-error "No refile targets"))
11958 (let* ((cbuf (current-buffer))
11959 (partial-completion-mode nil)
11960 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
11961 (cfunc (if (and org-refile-use-outline-path
11962 org-outline-path-complete-in-steps)
11963 'org-olpath-completing-read
11964 'org-icompleting-read))
11965 (extra (if org-refile-use-outline-path "/" ""))
11966 (cbnex (concat (buffer-name) extra))
11967 (filename (and cfn (expand-file-name cfn)))
11970 (if (and (not (member org-refile-use-outline-path
11971 '(file full-file-path)))
11972 (not (equal filename (nth 1 x))))
11973 (cons (concat (car x) extra " ("
11974 (file-name-nondirectory (nth 1 x)) ")")
11976 (cons (concat (car x) extra) (cdr x))))
11977 org-refile-target-table))
11978 (completion-ignore-case t)
11980 (prompt (concat prompt
11981 (or (and (car org-refile-history)
11982 (concat " (default " (car org-refile-history) ")"))
11983 (and (assoc cbnex tbl) (setq cdef cbnex)
11984 (concat " (default " cbnex ")"))) ": "))
11985 pa answ parent-target child parent old-hist)
11986 (setq old-hist org-refile-history)
11987 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
11988 nil 'org-refile-history (or cdef (car org-refile-history))))
11989 (if (setq pa (org-refile--get-location answ tbl))
11991 (org-refile-check-position pa)
11992 (when (or (not org-refile-history)
11993 (not (eq old-hist org-refile-history))
11994 (not (equal (car pa) (car org-refile-history))))
11995 (setq org-refile-history
11996 (cons (car pa) (if (assoc (car org-refile-history) tbl)
11998 (cdr org-refile-history))))
11999 (if (equal (car org-refile-history) (nth 1 org-refile-history))
12000 (pop org-refile-history)))
12002 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
12004 (setq parent (match-string 1 answ)
12005 child (match-string 2 answ))
12006 (setq parent-target (org-refile--get-location parent tbl))
12007 (when (and parent-target
12008 (or (eq new-nodes t)
12009 (and (eq new-nodes 'confirm)
12010 (y-or-n-p (format "Create new node \"%s\"? "
12012 (org-refile-new-child parent-target child)))
12013 (user-error "Invalid target location")))))
12015 (declare-function org-string-nw-p "org-macs" (s))
12016 (defun org-refile-check-position (refile-pointer)
12017 "Check if the refile pointer matches the headline to which it points."
12018 (let* ((file (nth 1 refile-pointer))
12019 (re (nth 2 refile-pointer))
12020 (pos (nth 3 refile-pointer))
12022 (if (and (not (markerp pos)) (not file))
12023 (user-error "Please indicate a target file in the refile path")
12024 (when (org-string-nw-p re)
12025 (setq buffer (if (markerp pos)
12026 (marker-buffer pos)
12027 (or (find-buffer-visiting file)
12028 (find-file-noselect file))))
12029 (with-current-buffer buffer
12034 (beginning-of-line 1)
12035 (unless (org-looking-at-p re)
12036 (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
12038 (defun org-refile-new-child (parent-target child)
12039 "Use refile target PARENT-TARGET to add new CHILD below it."
12040 (unless parent-target
12041 (error "Cannot find parent for new node"))
12042 (let ((file (nth 1 parent-target))
12043 (pos (nth 3 parent-target))
12045 (with-current-buffer (or (find-buffer-visiting file)
12046 (find-file-noselect file))
12052 (goto-char (point-max))
12053 (if (not (bolp)) (newline)))
12054 (when (looking-at org-outline-regexp)
12055 (setq level (funcall outline-level))
12056 (org-end-of-subtree t t))
12057 (org-back-over-empty-lines)
12058 (insert "\n" (make-string
12059 (if pos (org-get-valid-level level 1) 1) ?*)
12061 (beginning-of-line 0)
12062 (list (concat (car parent-target) "/" child) file "" (point)))))))
12064 (defun org-olpath-completing-read (prompt collection &rest args)
12065 "Read an outline path like a file name."
12066 (let ((thetable collection)
12067 (org-completion-use-ido nil) ; does not work with ido.
12068 (org-completion-use-iswitchb nil)) ; or iswitchb
12070 'org-icompleting-read prompt
12071 (lambda (string predicate &optional flag)
12072 (let (rtn r f (l (length string)))
12076 (try-completion string thetable))
12079 (setq rtn (all-completions string thetable predicate))
12082 (setq r (substring x l))
12083 (if (string-match " ([^)]*)$" x)
12084 (setq f (match-string 0 x))
12086 (if (string-match "/" r)
12087 (concat string (substring r 0 (match-end 0)) f)
12092 (assoc string thetable)))))
12095 ;;;; Dynamic blocks
12097 (defun org-find-dblock (name)
12098 "Find the first dynamic block with name NAME in the buffer.
12099 If not found, stay at current position and return nil."
12100 (let ((case-fold-search t) pos)
12102 (goto-char (point-min))
12103 (setq pos (and (re-search-forward
12104 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
12105 (match-beginning 0))))
12106 (if pos (goto-char pos))
12109 (defun org-create-dblock (plist)
12110 "Create a dynamic block section, with parameters taken from PLIST.
12111 PLIST must contain a :name entry which is used as the name of the block."
12112 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
12115 (let ((col (current-column))
12116 (name (plist-get plist :name)))
12117 (insert "#+BEGIN: " name)
12119 (if (eq (car plist) :name)
12120 (setq plist (cddr plist))
12121 (insert " " (prin1-to-string (pop plist)))))
12122 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
12123 (beginning-of-line -2)))
12125 (defun org-prepare-dblock ()
12126 "Prepare dynamic block for refresh.
12127 This empties the block, puts the cursor at the insert position and returns
12128 the property list including an extra property :name with the block name."
12129 (unless (looking-at org-dblock-start-re)
12130 (user-error "Not at a dynamic block"))
12131 (let* ((begdel (1+ (match-end 0)))
12132 (name (org-no-properties (match-string 1)))
12133 (params (append (list :name name)
12134 (read (concat "(" (match-string 3) ")")))))
12136 (beginning-of-line 1)
12137 (skip-chars-forward " \t")
12138 (setq params (plist-put params :indentation-column (current-column))))
12139 (unless (re-search-forward org-dblock-end-re nil t)
12140 (error "Dynamic block not terminated"))
12143 (list :content (buffer-substring
12144 begdel (match-beginning 0)))))
12145 (delete-region begdel (match-beginning 0))
12150 (defun org-map-dblocks (&optional command)
12151 "Apply COMMAND to all dynamic blocks in the current buffer.
12152 If COMMAND is not given, use `org-update-dblock'."
12153 (let ((cmd (or command 'org-update-dblock)))
12155 (goto-char (point-min))
12156 (while (re-search-forward org-dblock-start-re nil t)
12157 (goto-char (match-beginning 0))
12159 (condition-case nil
12161 (error (message "Error during update of dynamic block"))))
12162 (unless (re-search-forward org-dblock-end-re nil t)
12163 (error "Dynamic block not terminated"))))))
12165 (defun org-dblock-update (&optional arg)
12166 "User command for updating dynamic blocks.
12167 Update the dynamic block at point. With prefix ARG, update all dynamic
12168 blocks in the buffer."
12171 (org-update-all-dblocks)
12172 (or (looking-at org-dblock-start-re)
12173 (org-beginning-of-dblock))
12174 (org-update-dblock)))
12176 (defun org-update-dblock ()
12177 "Update the dynamic block at point.
12178 This means to empty the block, parse for parameters and then call
12179 the correct writing function."
12182 (let* ((win (selected-window))
12184 (line (org-current-line))
12185 (params (org-prepare-dblock))
12186 (name (plist-get params :name))
12187 (indent (plist-get params :indentation-column))
12188 (cmd (intern (concat "org-dblock-write:" name))))
12189 (message "Updating dynamic block `%s' at line %d..." name line)
12190 (funcall cmd params)
12191 (message "Updating dynamic block `%s' at line %d...done" name line)
12193 (when (and indent (> indent 0))
12194 (setq indent (make-string indent ?\ ))
12196 (select-window win)
12197 (org-beginning-of-dblock)
12199 (while (not (looking-at org-dblock-end-re))
12201 (beginning-of-line 2))
12202 (when (looking-at org-dblock-end-re)
12203 (and (looking-at "[ \t]+")
12204 (replace-match ""))
12205 (insert indent)))))))
12207 (defun org-beginning-of-dblock ()
12208 "Find the beginning of the dynamic block at point.
12209 Error if there is no such block at point."
12210 (let ((pos (point))
12213 (if (and (re-search-backward org-dblock-start-re nil t)
12214 (setq beg (match-beginning 0))
12215 (re-search-forward org-dblock-end-re nil t)
12216 (> (match-end 0) pos))
12219 (error "Not in a dynamic block"))))
12221 (defun org-update-all-dblocks ()
12222 "Update all dynamic blocks in the buffer.
12223 This function can be used in a hook."
12225 (when (derived-mode-p 'org-mode)
12226 (org-map-dblocks 'org-update-dblock)))
12231 (declare-function org-export-backend-name "org-export" (cl-x))
12232 (declare-function org-export-backend-options "org-export" (cl-x))
12233 (defun org-get-export-keywords ()
12234 "Return a list of all currently understood export keywords.
12235 Export keywords include options, block names, attributes and
12236 keywords relative to each registered export back-end."
12239 (org-bound-and-true-p org-export--registered-backends)
12240 (delq nil keywords))
12241 ;; Back-end name (for keywords, like #+LATEX:)
12242 (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
12243 (dolist (option-entry (org-export-backend-options backend))
12244 ;; Back-end options.
12245 (push (nth 1 option-entry) keywords)))))
12247 (defconst org-options-keywords
12248 '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
12249 "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
12250 "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
12251 "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
12252 "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
12254 (defcustom org-structure-template-alist
12255 '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
12256 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
12257 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
12258 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
12259 ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
12260 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
12261 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
12263 ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
12265 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
12268 ("I" "#+INCLUDE: %file ?"))
12269 "Structure completion elements.
12270 This is a list of abbreviation keys and values. The value gets inserted
12271 if you type `<' followed by the key and then press the completion key,
12272 usually `TAB'. %file will be replaced by a file name after prompting
12273 for the file using completion. The cursor will be placed at the position
12274 of the `?` in the template.
12275 There are two templates for each key, the first uses the original Org syntax,
12276 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
12277 the default when the /org-mtags.el/ module has been loaded. See also the
12278 variable `org-mtags-prefer-muse-templates'."
12279 :group 'org-completion
12282 (string :tag "Key")
12283 (string :tag "Template")))
12285 :package-version '(Org . "8.3"))
12287 (defun org-try-structure-completion ()
12288 "Try to complete a structure template before point.
12289 This looks for strings like \"<e\" on an otherwise empty line and
12291 (let ((l (buffer-substring (point-at-bol) (point)))
12293 (when (and (looking-at "[ \t]*$")
12294 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
12295 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
12296 (org-complete-expand-structure-template (+ -1 (point-at-bol)
12297 (match-beginning 1)) a)
12300 (defun org-complete-expand-structure-template (start cell)
12301 "Expand a structure template."
12302 (let ((rpl (nth 1 cell))
12304 (delete-region start (point))
12305 (when (string-match "\\`[ \t]*#\\+" rpl)
12308 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
12309 (setq ind (buffer-substring (point-at-bol) (point))))
12311 (setq start (point))
12312 (if (string-match "%file" rpl)
12313 (setq rpl (replace-match
12317 (abbreviate-file-name (read-file-name "Include file: ")))
12320 (setq rpl (mapconcat 'identity (split-string rpl "\n")
12321 (concat "\n" ind)))
12323 (if (re-search-backward "\\?" start t) (delete-char 1))))
12325 ;;;; TODO, DEADLINE, Comments
12327 (defun org-toggle-comment ()
12328 "Change the COMMENT state of an entry."
12331 (org-back-to-heading)
12332 (looking-at org-complex-heading-regexp)
12333 (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
12334 (skip-chars-forward " \t")
12335 (unless (memq (char-before) '(?\s ?\t)) (insert " "))
12336 (if (org-in-commented-heading-p t)
12337 (delete-region (point)
12338 (progn (search-forward " " (line-end-position) 'move)
12339 (skip-chars-forward " \t")
12341 (insert org-comment-string)
12342 (unless (eolp) (insert " ")))))
12344 (defvar org-last-todo-state-is-todo nil
12345 "This is non-nil when the last TODO state change led to a TODO state.
12346 If the last change removed the TODO tag or switched to DONE, then
12349 (defvar org-setting-tags nil) ; dynamically skipped
12351 (defvar org-todo-setup-filter-hook nil
12352 "Hook for functions that pre-filter todo specs.
12353 Each function takes a todo spec and returns either nil or the spec
12354 transformed into canonical form." )
12356 (defvar org-todo-get-default-hook nil
12357 "Hook for functions that get a default item for todo.
12358 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
12359 nil or a string to be used for the todo mark." )
12361 (defvar org-agenda-headline-snapshot-before-repeat)
12363 (defun org-current-effective-time ()
12364 "Return current time adjusted for `org-extend-today-until' variable."
12365 (let* ((ct (org-current-time))
12366 (dct (decode-time ct))
12369 (org-use-last-clock-out-time-as-effective-time
12370 (or (org-clock-get-last-clock-out-time) ct))
12371 ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
12372 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
12376 (defun org-todo-yesterday (&optional arg)
12377 "Like `org-todo' but the time of change will be 23:59 of yesterday."
12379 (if (eq major-mode 'org-agenda-mode)
12380 (apply 'org-agenda-todo-yesterday arg)
12381 (let* ((hour (third (decode-time
12382 (org-current-time))))
12383 (org-extend-today-until (1+ hour)))
12386 (defvar org-block-entry-blocking ""
12387 "First entry preventing the TODO state change.")
12389 (defun org-cancel-repeater ()
12390 "Cancel a repeater by setting its numeric value to zero."
12393 (org-back-to-heading t)
12394 (let ((bound1 (point))
12395 (bound0 (save-excursion (outline-next-heading) (point))))
12396 (when (re-search-forward
12397 (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12398 org-deadline-time-regexp "\\)\\|\\("
12399 org-ts-regexp "\\)")
12401 (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
12402 (replace-match "0" t nil nil 1))))))
12404 (defun org-todo (&optional arg)
12405 "Change the TODO state of an item.
12406 The state of an item is given by a keyword at the start of the heading,
12408 *** TODO Write paper
12411 The different keywords are specified in the variable `org-todo-keywords'.
12412 By default the available states are \"TODO\" and \"DONE\".
12413 So for this example: when the item starts with TODO, it is changed to DONE.
12414 When it starts with DONE, the DONE is removed. And when neither TODO nor
12415 DONE are present, add TODO at the beginning of the heading.
12417 With \\[universal-argument] prefix arg, use completion to determine the new \
12419 With numeric prefix arg, switch to that state.
12420 With a double \\[universal-argument] prefix, switch to the next set of TODO \
12421 keywords (nextset).
12422 With a triple \\[universal-argument] prefix, circumvent any state blocking.
12423 With a numeric prefix arg of 0, inhibit note taking for the change.
12424 With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
12426 When called through ELisp, arg is also interpreted in the following way:
12427 'none -> empty state
12428 \"\"(empty string) -> switch to empty state
12429 'done -> switch to DONE
12430 'nextset -> switch to the next set of keywords
12431 'previousset -> switch to the previous set of keywords
12432 \"WAITING\" -> switch to the specified keyword, but only if it
12433 really is a member of `org-todo-keywords'."
12435 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12436 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12437 'region-start-level 'region))
12438 org-loop-over-headlines-in-active-region)
12441 org-loop-over-headlines-in-active-region
12442 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12443 (if (equal arg '(16)) (setq arg 'nextset))
12444 (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
12445 (let ((org-blocker-hook org-blocker-hook)
12448 (when (equal arg '(64))
12449 (setq arg nil org-blocker-hook nil))
12450 (when (and org-blocker-hook
12451 (or org-inhibit-blocking
12452 (org-entry-get nil "NOBLOCKING")))
12453 (setq org-blocker-hook nil))
12456 (org-back-to-heading t)
12457 (when (org-in-commented-heading-p t)
12458 (org-toggle-comment)
12460 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
12461 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
12462 (looking-at "\\(?: *\\|[ \t]*$\\)"))
12463 (let* ((match-data (match-data))
12464 (startpos (point-at-bol))
12465 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
12466 (org-log-done org-log-done)
12467 (org-log-repeat org-log-repeat)
12468 (org-todo-log-states org-todo-log-states)
12469 (org-inhibit-logging
12471 (progn (setq arg nil) 'note) org-inhibit-logging))
12472 (this (match-string 1))
12473 (hl-pos (match-beginning 0))
12474 (head (org-get-todo-sequence-head this))
12475 (ass (assoc head org-todo-kwd-alist))
12476 (interpret (nth 1 ass))
12477 (done-word (nth 3 ass))
12478 (final-done-word (nth 4 ass))
12479 (org-last-state (or this ""))
12480 (completion-ignore-case t)
12481 (member (member this org-todo-keywords-1))
12482 (tail (cdr member))
12484 ((and org-todo-key-trigger
12485 (or (and (equal arg '(4))
12486 (eq org-use-fast-todo-selection 'prefix))
12487 (and (not arg) org-use-fast-todo-selection
12488 (not (eq org-use-fast-todo-selection
12490 ;; Use fast selection
12491 (org-fast-todo-selection))
12492 ((and (equal arg '(4))
12493 (or (not org-use-fast-todo-selection)
12494 (not org-todo-key-trigger)))
12495 ;; Read a state with completion
12496 (org-icompleting-read
12497 "State: " (mapcar 'list org-todo-keywords-1)
12501 (if tail (car tail) nil)
12502 (car org-todo-keywords-1)))
12504 (if (equal member org-todo-keywords-1)
12507 (nth (- (length org-todo-keywords-1)
12509 org-todo-keywords-1)
12510 (org-last org-todo-keywords-1))))
12511 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
12512 (setq arg nil))) ; hack to fall back to cycling
12514 ;; user or caller requests a specific state
12516 ((equal arg "") nil)
12517 ((eq arg 'none) nil)
12518 ((eq arg 'done) (or done-word (car org-done-keywords)))
12520 (or (car (cdr (member head org-todo-heads)))
12521 (car org-todo-heads)))
12522 ((eq arg 'previousset)
12523 (let ((org-todo-heads (reverse org-todo-heads)))
12524 (or (car (cdr (member head org-todo-heads)))
12525 (car org-todo-heads))))
12526 ((car (member arg org-todo-keywords-1)))
12528 (user-error "State `%s' not valid in this file" arg))
12529 ((nth (1- (prefix-numeric-value arg))
12530 org-todo-keywords-1))))
12531 ((null member) (or head (car org-todo-keywords-1)))
12532 ((equal this final-done-word) nil) ;; -> make empty
12533 ((null tail) nil) ;; -> first entry
12534 ((memq interpret '(type priority))
12535 (if (eq this-command last-command)
12537 (if (> (length tail) 0)
12538 (or done-word (car org-done-keywords))
12543 (run-hook-with-args-until-success
12544 'org-todo-get-default-hook org-state org-last-state)
12546 (next (if org-state (concat " " org-state " ") " "))
12547 (change-plist (list :type 'todo-state-change :from this :to org-state
12548 :position startpos))
12550 (when org-blocker-hook
12551 (setq org-last-todo-state-is-todo
12552 (not (member this org-done-keywords)))
12553 (unless (save-excursion
12555 (org-with-wide-buffer
12556 (run-hook-with-args-until-failure
12557 'org-blocker-hook change-plist))))
12558 (if (org-called-interactively-p 'interactive)
12559 (user-error "TODO state change from %s to %s blocked (by \"%s\")"
12560 this org-state org-block-entry-blocking)
12562 (message "TODO state change from %s to %s blocked (by \"%s\")"
12563 this org-state org-block-entry-blocking)
12564 (throw 'exit nil))))
12565 (store-match-data match-data)
12566 (replace-match next t t)
12567 (cond ((equal this org-state)
12568 (message "TODO state was already %s" (org-trim next)))
12569 ((pos-visible-in-window-p hl-pos)
12570 (message "TODO state changed to %s" (org-trim next))))
12572 (setq head (org-get-todo-sequence-head org-state)
12573 ass (assoc head org-todo-kwd-alist)
12574 interpret (nth 1 ass)
12575 done-word (nth 3 ass)
12576 final-done-word (nth 4 ass)))
12577 (when (memq arg '(nextset previousset))
12578 (message "Keyword-Set %d/%d: %s"
12579 (- (length org-todo-sets) -1
12580 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
12581 (length org-todo-sets)
12582 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
12583 (setq org-last-todo-state-is-todo
12584 (not (member org-state org-done-keywords)))
12585 (setq now-done-p (and (member org-state org-done-keywords)
12586 (not (member this org-done-keywords))))
12587 (and logging (org-local-logging logging))
12588 (when (and (or org-todo-log-states org-log-done)
12589 (not (eq org-inhibit-logging t))
12590 (not (memq arg '(nextset previousset))))
12591 ;; we need to look at recording a time and note
12592 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
12593 (nth 2 (assoc this org-todo-log-states))))
12594 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
12595 (setq dolog 'time))
12596 (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
12598 (member org-state org-not-done-keywords)
12599 (not (member this org-not-done-keywords))))
12600 ;; This is now a todo state and was not one before
12601 ;; If there was a CLOSED time stamp, get rid of it.
12602 (org-add-planning-info nil nil 'closed))
12603 (when (and now-done-p org-log-done)
12604 ;; It is now done, and it was not done before
12605 (org-add-planning-info 'closed (org-current-effective-time))
12606 (if (and (not dolog) (eq 'note org-log-done))
12607 (org-add-log-setup 'done org-state this 'findpos 'note)))
12608 (when (and org-state dolog)
12609 ;; This is a non-nil state, and we need to log it
12610 (org-add-log-setup 'state org-state this 'findpos dolog)))
12611 ;; Fixup tag positioning
12612 (org-todo-trigger-tag-changes org-state)
12613 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
12614 (when org-provide-todo-statistics
12615 (org-update-parent-todo-statistics))
12616 (run-hooks 'org-after-todo-state-change-hook)
12617 (if (and arg (not (member org-state org-done-keywords)))
12618 (setq head (org-get-todo-sequence-head org-state)))
12619 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
12620 ;; Do we need to trigger a repeat?
12622 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
12623 ;; This is for the agenda, take a snapshot of the headline.
12625 (setq org-agenda-headline-snapshot-before-repeat
12626 (org-get-heading))))
12627 (org-auto-repeat-maybe org-state))
12628 ;; Fixup cursor location if close to the keyword
12629 (if (and (outline-on-heading-p)
12631 (save-excursion (beginning-of-line 1)
12632 (looking-at org-todo-line-regexp))
12633 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
12635 (goto-char (or (match-end 2) (match-end 1)))
12636 (and (looking-at " ") (just-one-space))))
12637 (when org-trigger-hook
12639 (run-hook-with-args 'org-trigger-hook change-plist)))
12640 (when commentp (org-toggle-comment))))))))
12642 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
12643 "Block turning an entry into a TODO, using the hierarchy.
12644 This checks whether the current task should be blocked from state
12645 changes. Such blocking occurs when:
12647 1. The task has children which are not all in a completed state.
12649 2. A task has a parent with the property :ORDERED:, and there
12650 are siblings prior to the current task with incomplete
12653 3. The parent of the task is blocked because it has siblings that should
12654 be done first, or is child of a block grandparent TODO entry."
12656 (if (not org-enforce-todo-dependencies)
12657 t ; if locally turned off don't block
12659 ;; If this is not a todo state change, or if this entry is already DONE,
12661 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
12662 (member (plist-get change-plist :from)
12663 (cons 'done org-done-keywords))
12664 (member (plist-get change-plist :to)
12665 (cons 'todo org-not-done-keywords))
12666 (not (plist-get change-plist :to)))
12667 (throw 'dont-block t))
12668 ;; If this task has children, and any are undone, it's blocked
12670 (org-back-to-heading t)
12671 (let ((this-level (funcall outline-level)))
12672 (outline-next-heading)
12673 (let ((child-level (funcall outline-level)))
12674 (while (and (not (eobp))
12675 (> child-level this-level))
12676 ;; this todo has children, check whether they are all
12678 (if (and (not (org-entry-is-done-p))
12679 (org-entry-is-todo-p))
12680 (progn (setq org-block-entry-blocking (org-get-heading))
12681 (throw 'dont-block nil)))
12682 (outline-next-heading)
12683 (setq child-level (funcall outline-level))))))
12684 ;; Otherwise, if the task's parent has the :ORDERED: property, and
12685 ;; any previous siblings are undone, it's blocked
12687 (org-back-to-heading t)
12688 (let* ((pos (point))
12689 (parent-pos (and (org-up-heading-safe) (point))))
12690 (if (not parent-pos) (throw 'dont-block t)) ; no parent
12691 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12693 (re-search-forward org-not-done-heading-regexp pos t))
12694 (setq org-block-entry-blocking (match-string 0))
12695 (throw 'dont-block nil)) ; block, there is an older sibling not done.
12696 ;; Search further up the hierarchy, to see if an ancestor is blocked
12698 (goto-char parent-pos)
12699 (if (not (looking-at org-not-done-heading-regexp))
12700 (throw 'dont-block t)) ; do not block, parent is not a TODO
12702 (setq parent-pos (and (org-up-heading-safe) (point)))
12703 (if (not parent-pos) (throw 'dont-block t)) ; no parent
12704 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12706 (re-search-forward org-not-done-heading-regexp pos t)
12707 (setq org-block-entry-blocking (org-get-heading)))
12708 (throw 'dont-block nil)))))))) ; block, older sibling not done.
12710 (defcustom org-track-ordered-property-with-tag nil
12711 "Should the ORDERED property also be shown as a tag?
12712 The ORDERED property decides if an entry should require subtasks to be
12713 completed in sequence. Since a property is not very visible, setting
12714 this option means that toggling the ORDERED property with the command
12715 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
12716 not relevant for the behavior, but it makes things more visible.
12718 Note that toggling the tag with tags commands will not change the property
12719 and therefore not influence behavior!
12721 This can be t, meaning the tag ORDERED should be used, It can also be a
12722 string to select a different tag for this task."
12725 (const :tag "No tracking" nil)
12726 (const :tag "Track with ORDERED tag" t)
12727 (string :tag "Use other tag")))
12729 (defun org-toggle-ordered-property ()
12730 "Toggle the ORDERED property of the current entry.
12731 For better visibility, you can track the value of this property with a tag.
12732 See variable `org-track-ordered-property-with-tag'."
12734 (let* ((t1 org-track-ordered-property-with-tag)
12735 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
12737 (org-back-to-heading)
12738 (if (org-entry-get nil "ORDERED")
12740 (org-delete-property "ORDERED")
12741 (and tag (org-toggle-tag tag 'off))
12742 (message "Subtasks can be completed in arbitrary order"))
12743 (org-entry-put nil "ORDERED" "t")
12744 (and tag (org-toggle-tag tag 'on))
12745 (message "Subtasks must be completed in sequence")))))
12747 (defvar org-blocked-by-checkboxes) ; dynamically scoped
12748 (defun org-block-todo-from-checkboxes (change-plist)
12749 "Block turning an entry into a TODO, using checkboxes.
12750 This checks whether the current task should be blocked from state
12751 changes because there are unchecked boxes in this entry."
12752 (if (not org-enforce-todo-checkbox-dependencies)
12753 t ; if locally turned off don't block
12755 ;; If this is not a todo state change, or if this entry is already DONE,
12757 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
12758 (member (plist-get change-plist :from)
12759 (cons 'done org-done-keywords))
12760 (member (plist-get change-plist :to)
12761 (cons 'todo org-not-done-keywords))
12762 (not (plist-get change-plist :to)))
12763 (throw 'dont-block t))
12764 ;; If this task has checkboxes that are not checked, it's blocked
12766 (org-back-to-heading t)
12767 (let ((beg (point)) end)
12768 (outline-next-heading)
12771 (if (org-list-search-forward
12772 (concat (org-item-beginning-re)
12773 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
12777 (if (boundp 'org-blocked-by-checkboxes)
12778 (setq org-blocked-by-checkboxes t))
12779 (throw 'dont-block nil)))))
12780 t))) ; do not block
12782 (defun org-entry-blocked-p ()
12783 "Is the current entry blocked?"
12784 (org-with-silent-modifications
12785 (if (org-entry-get nil "NOBLOCKING")
12786 nil ;; Never block this entry
12787 (not (run-hook-with-args-until-failure
12789 (list :type 'todo-state-change
12794 (defun org-update-statistics-cookies (all)
12795 "Update the statistics cookie, either from TODO or from checkboxes.
12796 This should be called with the cursor in a line with a statistics cookie."
12800 (org-update-checkbox-count 'all)
12801 (org-map-entries 'org-update-parent-todo-statistics))
12802 (if (not (org-at-heading-p))
12803 (org-update-checkbox-count)
12804 (let ((pos (point-marker))
12806 (ignore-errors (org-back-to-heading t))
12807 (if (not (org-at-heading-p))
12808 (org-update-checkbox-count)
12809 (setq l1 (org-outline-level))
12810 (setq end (save-excursion
12811 (outline-next-heading)
12812 (if (org-at-heading-p) (setq l2 (org-outline-level)))
12814 (if (and (save-excursion
12816 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
12817 (not (save-excursion (re-search-forward
12818 ":COOKIE_DATA:.*\\<todo\\>" end t))))
12819 (org-update-checkbox-count)
12820 (if (and l2 (> l2 l1))
12823 (org-update-parent-todo-statistics))
12825 (beginning-of-line 1)
12826 (while (re-search-forward
12827 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
12829 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
12831 (move-marker pos nil)))))
12833 (defvar org-entry-property-inherited-from) ;; defined below
12834 (defun org-update-parent-todo-statistics ()
12835 "Update any statistics cookie in the parent of the current headline.
12836 When `org-hierarchical-todo-statistics' is nil, statistics will cover
12837 the entire subtree and this will travel up the hierarchy and update
12838 statistics everywhere."
12839 (let* ((prop (save-excursion (org-up-heading-safe)
12840 (org-entry-get nil "COOKIE_DATA" 'inherit)))
12841 (recursive (or (not org-hierarchical-todo-statistics)
12842 (and prop (string-match "\\<recursive\\>" prop))))
12843 (lim (or (and prop (marker-position org-entry-property-inherited-from))
12846 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
12847 level ltoggle l1 new ndel
12848 (cnt-all 0) (cnt-done 0) is-percent kwd
12849 checkbox-beg ov ovs ove cookie-present)
12852 (beginning-of-line 1)
12853 (setq ltoggle (funcall outline-level))
12854 ;; Three situations are to consider:
12856 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
12857 ;; to the top-level ancestor on the headline;
12859 ;; 2. If parent has "recursive" property, repeat up to the
12860 ;; headline setting that property, taking inheritance into
12863 ;; 3. Else, move up to direct parent and proceed only once.
12864 (while (and (setq level (org-up-heading-safe))
12865 (or recursive first)
12867 (setq first nil cookie-present nil)
12871 (downcase (or (org-entry-get nil "COOKIE_DATA")
12874 (while (re-search-forward box-re (point-at-eol) t)
12875 (setq cnt-all 0 cnt-done 0 cookie-present t)
12876 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
12878 (unless (outline-next-heading) (throw 'exit nil))
12879 (while (and (looking-at org-complex-heading-regexp)
12880 (> (setq l1 (length (match-string 1))) level))
12881 (setq kwd (and (or recursive (= l1 ltoggle))
12883 (if (or (eq org-provide-todo-statistics 'all-headlines)
12884 (and (eq org-provide-todo-statistics t)
12885 (or (member kwd org-done-keywords)))
12886 (and (listp org-provide-todo-statistics)
12887 (stringp (car org-provide-todo-statistics))
12888 (or (member kwd org-provide-todo-statistics)
12889 (member kwd org-done-keywords)))
12890 (and (listp org-provide-todo-statistics)
12891 (listp (car org-provide-todo-statistics))
12892 (or (member kwd (car org-provide-todo-statistics))
12893 (and (member kwd org-done-keywords)
12894 (member kwd (cadr org-provide-todo-statistics))))))
12895 (setq cnt-all (1+ cnt-all))
12896 (if (eq org-provide-todo-statistics t)
12897 (and kwd (setq cnt-all (1+ cnt-all)))))
12898 (when (or (and (member org-provide-todo-statistics '(t all-headlines))
12899 (member kwd org-done-keywords))
12900 (and (listp org-provide-todo-statistics)
12901 (listp (car org-provide-todo-statistics))
12902 (member kwd org-done-keywords)
12903 (member kwd (cadr org-provide-todo-statistics)))
12904 (and (listp org-provide-todo-statistics)
12905 (stringp (car org-provide-todo-statistics))
12906 (member kwd org-done-keywords)))
12907 (setq cnt-done (1+ cnt-done)))
12908 (outline-next-heading)))
12911 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
12912 (format "[%d/%d]" cnt-done cnt-all))
12913 ndel (- (match-end 0) checkbox-beg))
12914 ;; handle overlays when updating cookie from column view
12915 (when (setq ov (car (overlays-at checkbox-beg)))
12916 (setq ovs (overlay-start ov) ove (overlay-end ov))
12917 (delete-overlay ov))
12918 (goto-char checkbox-beg)
12920 (delete-region (point) (+ (point) ndel))
12921 (when org-auto-align-tags (org-fix-tags-on-the-fly))
12922 (when ov (move-overlay ov ovs ove)))
12923 (when cookie-present
12924 (run-hook-with-args 'org-after-todo-statistics-hook
12925 cnt-done (- cnt-all cnt-done))))))
12926 (run-hooks 'org-todo-statistics-hook)))
12928 (defvar org-after-todo-statistics-hook nil
12929 "Hook that is called after a TODO statistics cookie has been updated.
12930 Each function is called with two arguments: the number of not-done entries
12931 and the number of done entries.
12933 For example, the following function, when added to this hook, will switch
12934 an entry to DONE when all children are done, and back to TODO when new
12935 entries are set to a TODO status. Note that this hook is only called
12936 when there is a statistics cookie in the headline!
12938 (defun org-summary-todo (n-done n-not-done)
12939 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
12940 (let (org-log-done org-log-states) ; turn off logging
12941 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
12944 (defvar org-todo-statistics-hook nil
12945 "Hook that is run whenever Org thinks TODO statistics should be updated.
12946 This hook runs even if there is no statistics cookie present, in which case
12947 `org-after-todo-statistics-hook' would not run.")
12949 (defun org-todo-trigger-tag-changes (state)
12950 "Apply the changes defined in `org-todo-state-tags-triggers'."
12951 (let ((l org-todo-state-tags-triggers)
12953 (when (or (not state) (equal state ""))
12954 (setq changes (append changes (cdr (assoc "" l)))))
12955 (when (and (stringp state) (> (length state) 0))
12956 (setq changes (append changes (cdr (assoc state l)))))
12957 (when (member state org-not-done-keywords)
12958 (setq changes (append changes (cdr (assoc 'todo l)))))
12959 (when (member state org-done-keywords)
12960 (setq changes (append changes (cdr (assoc 'done l)))))
12961 (dolist (c changes)
12962 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
12964 (defun org-local-logging (value)
12965 "Get logging settings from a property VALUE."
12967 ;; directly set the variables, they are already local.
12968 (setq org-log-done nil
12970 org-todo-log-states nil)
12971 (setq words (org-split-string value))
12972 (while (setq w (pop words))
12974 ((setq a (assoc w org-startup-options))
12975 (and (member (nth 1 a) '(org-log-done org-log-repeat))
12976 (set (nth 1 a) (nth 2 a))))
12977 ((setq a (org-extract-log-state-settings w))
12978 (and (member (car a) org-todo-keywords-1)
12979 (push a org-todo-log-states)))))))
12981 (defun org-get-todo-sequence-head (kwd)
12982 "Return the head of the TODO sequence to which KWD belongs.
12983 If KWD is not set, check if there is a text property remembering the
12988 (or (get-text-property (point-at-bol) 'org-todo-head)
12990 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
12991 nil (point-at-eol)))
12992 (get-text-property p 'org-todo-head))))
12993 ((not (member kwd org-todo-keywords-1))
12994 (car org-todo-keywords-1))
12995 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
12997 (defun org-fast-todo-selection ()
12998 "Fast TODO keyword selection with single keys.
12999 Returns the new TODO keyword, or nil if no state change should occur."
13000 (let* ((fulltable org-todo-key-alist)
13001 (done-keywords org-done-keywords) ;; needed for the faces.
13002 (maxlen (apply 'max (mapcar
13004 (if (stringp (car x)) (string-width (car x)) 0))
13007 (fwidth (+ maxlen 3 1 3))
13008 (ncol (/ (- (window-width) 4) fwidth))
13012 (save-window-excursion
13014 (set-buffer (get-buffer-create " *Org todo*"))
13015 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
13017 (org-set-local 'org-done-keywords done-keywords)
13018 (setq tbl fulltable cnt 0)
13019 (while (setq e (pop tbl))
13021 ((equal e '(:startgroup))
13022 (push '() groups) (setq ingroup t)
13023 (when (not (= cnt 0))
13027 ((equal e '(:endgroup))
13028 (setq ingroup nil cnt 0)
13030 ((equal e '(:newline))
13031 (when (not (= cnt 0))
13035 (while (equal (car tbl) '(:newline))
13037 (setq tbl (cdr tbl)))))
13039 (setq tg (car e) c (cdr e))
13040 (if ingroup (push tg (car groups)))
13041 (setq tg (org-add-props tg nil 'face
13042 (org-get-todo-face tg)))
13043 (if (and (= cnt 0) (not ingroup)) (insert " "))
13044 (insert "[" c "] " tg (make-string
13045 (- fwidth 4 (length tg)) ?\ ))
13046 (when (= (setq cnt (1+ cnt)) ncol)
13048 (if ingroup (insert " "))
13051 (goto-char (point-min))
13052 (if (not expert) (org-fit-window-to-buffer))
13053 (message "[a-z..]:Set [SPC]:clear")
13054 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13057 (and (= c ?q) (not (rassoc c fulltable))))
13058 (setq quit-flag t))
13060 ((setq e (rassoc c fulltable) tg (car e))
13062 (t (setq quit-flag t)))))))
13064 (defun org-entry-is-todo-p ()
13065 (member (org-get-todo-state) org-not-done-keywords))
13067 (defun org-entry-is-done-p ()
13068 (member (org-get-todo-state) org-done-keywords))
13070 (defun org-get-todo-state ()
13071 "Return the TODO keyword of the current subtree."
13073 (org-back-to-heading t)
13074 (and (looking-at org-todo-line-regexp)
13076 (match-string 2))))
13078 (defun org-at-date-range-p (&optional inactive-ok)
13079 "Is the cursor inside a date range?"
13083 (let ((pos (point)))
13084 (skip-chars-backward "^[<\r\n")
13085 (skip-chars-backward "<[")
13086 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
13087 (>= (match-end 0) pos)
13089 (skip-chars-backward "^<[\r\n")
13090 (skip-chars-backward "<[")
13091 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
13092 (>= (match-end 0) pos)
13096 (defun org-get-repeat (&optional tagline)
13097 "Check if there is a deadline/schedule with repeater in this entry."
13100 (org-back-to-heading t)
13101 (and (re-search-forward (if tagline
13102 (concat tagline "\\s-*" org-repeat-re)
13104 (org-entry-end-position) t)
13105 (match-string-no-properties 1)))))
13107 (defvar org-last-changed-timestamp)
13108 (defvar org-last-inserted-timestamp)
13109 (defvar org-log-post-message)
13110 (defvar org-log-note-purpose)
13111 (defvar org-log-note-how nil)
13112 (defvar org-log-note-extra)
13113 (defun org-auto-repeat-maybe (done-word)
13114 "Check if the current headline contains a repeated deadline/schedule.
13115 If yes, set TODO state back to what it was and change the base date
13116 of repeating deadline/scheduled time stamps to new date.
13117 This function is run automatically after each state change to a DONE state."
13118 ;; last-state is dynamically scoped into this function
13119 (let* ((repeat (org-get-repeat))
13120 (aa (assoc org-last-state org-todo-kwd-alist))
13121 (interpret (nth 1 aa))
13123 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
13124 (msg "Entry repeats: ")
13126 (org-todo-log-states nil)
13127 re type n what ts time to-state)
13128 (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
13129 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
13130 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
13131 org-todo-repeat-to-state))
13132 (unless (and to-state (member to-state org-todo-keywords-1))
13133 (setq to-state (if (eq interpret 'type) org-last-state head)))
13134 (org-todo to-state)
13135 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
13136 (org-entry-put nil "LAST_REPEAT" (format-time-string
13137 (org-time-stamp-format t t))))
13138 (when org-log-repeat
13139 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
13140 (memq 'org-add-log-note post-command-hook))
13141 ;; OK, we are already setup for some record
13142 (if (eq org-log-repeat 'note)
13143 ;; make sure we take a note, not only a time stamp
13144 (setq org-log-note-how 'note))
13145 ;; Set up for taking a record
13146 (org-add-log-setup 'state (or done-word (car org-done-keywords))
13148 'findpos org-log-repeat)))
13149 (org-back-to-heading t)
13150 (org-add-planning-info nil nil 'closed)
13151 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
13152 org-deadline-time-regexp "\\)\\|\\("
13153 org-ts-regexp "\\)"))
13154 (while (re-search-forward
13155 re (save-excursion (outline-next-heading) (point)) t)
13156 (setq type (if (match-end 1) org-scheduled-string
13157 (if (match-end 3) org-deadline-string "Plain:"))
13158 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
13159 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
13160 (setq n (string-to-number (match-string 2 ts))
13161 what (match-string 3 ts))
13162 (if (equal what "w") (setq n (* n 7) what "d"))
13163 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
13164 (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
13165 ;; Preparation, see if we need to modify the start date for the change
13166 (when (match-end 1)
13167 (setq time (save-match-data (org-time-string-to-time ts)))
13169 ((equal (match-string 1 ts) ".")
13170 ;; Shift starting date to today
13171 (org-timestamp-change
13172 (- (org-today) (time-to-days time))
13174 ((equal (match-string 1 ts) "+")
13175 (let ((nshiftmax 10) (nshift 0))
13176 (while (or (= nshift 0)
13177 (<= (time-to-days time)
13178 (time-to-days (current-time))))
13179 (when (= (incf nshift) nshiftmax)
13180 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
13181 (user-error "Abort")))
13182 (org-timestamp-change n (cdr (assoc what whata)))
13183 (org-at-timestamp-p t)
13184 (setq ts (match-string 1))
13185 (setq time (save-match-data (org-time-string-to-time ts)))))
13186 (org-timestamp-change (- n) (cdr (assoc what whata)))
13187 ;; rematch, so that we have everything in place for the real shift
13188 (org-at-timestamp-p t)
13189 (setq ts (match-string 1))
13190 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
13191 (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
13192 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
13193 (setq org-log-post-message msg)
13194 (message "%s" msg))))
13196 (defun org-show-todo-tree (arg)
13197 "Make a compact tree which shows all headlines marked with TODO.
13198 The tree will show the lines where the regexp matches, and all higher
13199 headlines above the match.
13200 With a \\[universal-argument] prefix, prompt for a regexp to match.
13201 With a numeric prefix N, construct a sparse tree for the Nth element
13202 of `org-todo-keywords-1'."
13204 (let ((case-fold-search nil)
13206 (cond ((null arg) org-not-done-regexp)
13208 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
13209 (mapcar 'list org-todo-keywords-1))))
13211 (mapconcat 'identity (org-split-string kwd "|") "\\|")
13213 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
13214 (regexp-quote (nth (1- (prefix-numeric-value arg))
13215 org-todo-keywords-1)))
13216 (t (user-error "Invalid prefix argument: %s" arg)))))
13217 (message "%d TODO entries found"
13218 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
13220 (defun org-deadline (arg &optional time)
13221 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
13222 With one universal prefix argument, remove any deadline from the item.
13223 With two universal prefix arguments, prompt for a warning delay.
13224 With argument TIME, set the deadline at the corresponding date. TIME
13225 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
13227 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13228 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13229 'region-start-level 'region))
13230 org-loop-over-headlines-in-active-region)
13232 `(org-deadline ',arg ,time)
13233 org-loop-over-headlines-in-active-region
13234 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13235 (let* ((old-date (org-entry-get nil "DEADLINE"))
13236 (old-date-time (if old-date (org-time-string-to-time old-date)))
13237 (repeater (and old-date
13239 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
13241 (match-string 1 old-date))))
13244 (when (and old-date org-log-redeadline)
13245 (org-add-log-setup 'deldeadline nil old-date 'findpos
13246 org-log-redeadline))
13247 (org-remove-timestamp-with-keyword org-deadline-string)
13248 (message "Item no longer has a deadline."))
13251 (org-back-to-heading t)
13252 (if (re-search-forward
13253 org-deadline-time-regexp
13254 (save-excursion (outline-next-heading) (point)) t)
13255 (let* ((rpl0 (match-string 1))
13256 (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
13258 (concat org-deadline-string
13264 (org-read-date nil t nil "Warn starting from" old-date-time)))
13265 (time-to-days old-date-time))))
13267 (user-error "No deadline information to update"))))
13269 (org-add-planning-info 'deadline time 'closed)
13270 (when (and old-date org-log-redeadline
13271 (not (equal old-date
13272 (substring org-last-inserted-timestamp 1 -1))))
13273 (org-add-log-setup 'redeadline nil old-date 'findpos
13274 org-log-redeadline))
13277 (org-back-to-heading t)
13278 (when (re-search-forward (concat org-deadline-string " "
13279 org-last-inserted-timestamp)
13281 (outline-next-heading) (point)) t)
13282 (goto-char (1- (match-end 0)))
13283 (insert " " repeater)
13284 (setq org-last-inserted-timestamp
13285 (concat (substring org-last-inserted-timestamp 0 -1)
13287 (substring org-last-inserted-timestamp -1))))))
13288 (message "Deadline on %s" org-last-inserted-timestamp))))))
13290 (defun org-schedule (arg &optional time)
13291 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
13292 With one universal prefix argument, remove any scheduling date from the item.
13293 With two universal prefix arguments, prompt for a delay cookie.
13294 With argument TIME, scheduled at the corresponding date. TIME can
13295 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
13297 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13298 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13299 'region-start-level 'region))
13300 org-loop-over-headlines-in-active-region)
13302 `(org-schedule ',arg ,time)
13303 org-loop-over-headlines-in-active-region
13304 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13305 (let* ((old-date (org-entry-get nil "SCHEDULED"))
13306 (old-date-time (if old-date (org-time-string-to-time old-date)))
13307 (repeater (and old-date
13309 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
13311 (match-string 1 old-date))))
13315 (when (and old-date org-log-reschedule)
13316 (org-add-log-setup 'delschedule nil old-date 'findpos
13317 org-log-reschedule))
13318 (org-remove-timestamp-with-keyword org-scheduled-string)
13319 (message "Item is no longer scheduled.")))
13322 (org-back-to-heading t)
13323 (if (re-search-forward
13324 org-scheduled-time-regexp
13325 (save-excursion (outline-next-heading) (point)) t)
13326 (let* ((rpl0 (match-string 1))
13327 (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
13329 (concat org-scheduled-string
13335 (org-read-date nil t nil "Delay until" old-date-time)))
13336 (time-to-days old-date-time))))
13338 (user-error "No scheduled information to update"))))
13340 (org-add-planning-info 'scheduled time 'closed)
13341 (when (and old-date org-log-reschedule
13342 (not (equal old-date
13343 (substring org-last-inserted-timestamp 1 -1))))
13344 (org-add-log-setup 'reschedule nil old-date 'findpos
13345 org-log-reschedule))
13348 (org-back-to-heading t)
13349 (when (re-search-forward (concat org-scheduled-string " "
13350 org-last-inserted-timestamp)
13352 (outline-next-heading) (point)) t)
13353 (goto-char (1- (match-end 0)))
13354 (insert " " repeater)
13355 (setq org-last-inserted-timestamp
13356 (concat (substring org-last-inserted-timestamp 0 -1)
13358 (substring org-last-inserted-timestamp -1))))))
13359 (message "Scheduled to %s" org-last-inserted-timestamp))))))
13361 (defun org-get-scheduled-time (pom &optional inherit)
13362 "Get the scheduled time as a time tuple, of a format suitable
13363 for calling org-schedule with, or if there is no scheduling,
13365 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
13367 (apply 'encode-time (org-parse-time-string time)))))
13369 (defun org-get-deadline-time (pom &optional inherit)
13370 "Get the deadline as a time tuple, of a format suitable for
13371 calling org-deadline with, or if there is no scheduling, returns
13373 (let ((time (org-entry-get pom "DEADLINE" inherit)))
13375 (apply 'encode-time (org-parse-time-string time)))))
13377 (defun org-remove-timestamp-with-keyword (keyword)
13378 "Remove all time stamps with KEYWORD in the current entry."
13379 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
13382 (org-back-to-heading t)
13384 (outline-next-heading)
13385 (while (re-search-backward re beg t)
13387 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
13388 (equal (char-before) ?\ ))
13389 (backward-delete-char 1)
13390 (if (string-match "^[ \t]*$" (buffer-substring
13391 (point-at-bol) (point-at-eol)))
13392 (delete-region (point-at-bol)
13393 (min (point-max) (1+ (point-at-eol))))))))))
13395 (defvar org-time-was-given) ; dynamically scoped parameter
13396 (defvar org-end-time-was-given) ; dynamically scoped parameter
13398 (defun org-add-planning-info (what &optional time &rest remove)
13399 "Insert new timestamp with keyword in the line directly after the headline.
13400 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
13401 If non is given, the user is prompted for a date.
13402 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
13405 (let (org-time-was-given org-end-time-was-given ts
13406 end default-time default-input)
13409 (when (and (memq what '(scheduled deadline))
13411 (and (stringp time)
13412 (string-match "^[-+]+[0-9]" time))))
13413 ;; Try to get a default date/time from existing timestamp
13415 (org-back-to-heading t)
13416 (setq end (save-excursion (outline-next-heading) (point)))
13417 (when (re-search-forward (if (eq what 'scheduled)
13418 org-scheduled-time-regexp
13419 org-deadline-time-regexp)
13421 (setq ts (match-string 1)
13423 (apply 'encode-time (org-parse-time-string ts))
13424 default-input (and ts (org-get-compact-tod ts))))))
13428 ;; This is a string (relative or absolute), set proper date
13429 (apply 'encode-time
13430 (org-read-date-analyze
13431 time default-time (decode-time default-time)))
13432 ;; If necessary, get the time from the user
13433 (or time (org-read-date nil 'to-time nil nil
13434 default-time default-input)))))
13436 (when (and org-insert-labeled-timestamps-at-point
13437 (member what '(scheduled deadline)))
13439 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
13440 (org-insert-time-stamp time org-time-was-given
13441 nil nil nil (list org-end-time-was-given))
13445 (let (col list elt ts buffer-invisibility-spec)
13446 (org-back-to-heading t)
13447 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
13448 (goto-char (match-end 1))
13449 (setq col (current-column))
13450 (goto-char (match-end 0))
13451 (if (eobp) (insert "\n") (forward-char 1))
13452 (when (and (not what)
13455 org-keyword-time-not-clock-regexp))))
13456 ;; Nothing to add, nothing to remove...... :-)
13458 (if (and (not (looking-at org-outline-regexp))
13459 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
13461 (not (equal (match-string 1) org-clock-string)))
13462 (narrow-to-region (match-beginning 0) (match-end 0))
13463 (insert-before-markers "\n")
13465 (narrow-to-region (point) (point))
13466 (and org-adapt-indentation (org-indent-to-column col)))
13467 ;; Check if we have to remove something.
13468 (setq list (cons what remove))
13470 (setq elt (pop list))
13471 (when (or (and (eq elt 'scheduled)
13472 (re-search-forward org-scheduled-time-regexp nil t))
13473 (and (eq elt 'deadline)
13474 (re-search-forward org-deadline-time-regexp nil t))
13475 (and (eq elt 'closed)
13476 (re-search-forward org-closed-time-regexp nil t)))
13478 (if (looking-at "--+<[^>]+>") (replace-match ""))))
13479 (and (looking-at "[ \t]+") (replace-match ""))
13480 (and org-adapt-indentation (bolp) (org-indent-to-column col))
13483 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
13484 (cond ((eq what 'scheduled) org-scheduled-string)
13485 ((eq what 'deadline) org-deadline-string)
13486 ((eq what 'closed) org-closed-string))
13488 (setq ts (org-insert-time-stamp
13490 (or org-time-was-given
13491 (and (eq what 'closed) org-log-done-with-time))
13493 nil nil (list org-end-time-was-given)))
13495 (if (not (or (bolp) (eq (char-before) ?\ )
13496 (memq (char-after) '(32 10))
13499 (goto-char (point-min))
13501 (if (and (looking-at "[ \t]*\n")
13502 (equal (char-before) ?\n))
13503 (delete-region (1- (point)) (point-at-eol)))
13506 (defvar org-log-note-marker (make-marker))
13507 (defvar org-log-note-purpose nil)
13508 (defvar org-log-note-state nil)
13509 (defvar org-log-note-previous-state nil)
13510 (defvar org-log-note-extra nil)
13511 (defvar org-log-note-window-configuration nil)
13512 (defvar org-log-note-return-to (make-marker))
13513 (defvar org-log-note-effective-time nil
13514 "Remembered current time so that dynamically scoped
13515 `org-extend-today-until' affects tha timestamps in state change
13518 (defvar org-log-post-message nil
13519 "Message to be displayed after a log note has been stored.
13520 The auto-repeater uses this.")
13522 (defun org-add-note ()
13523 "Add a note to the current entry.
13524 This is done in the same way as adding a state change note."
13526 (org-add-log-setup 'note nil nil 'findpos nil))
13528 (defvar org-property-end-re)
13529 (defun org-add-log-setup (&optional purpose state prev-state
13531 "Set up the post command hook to take a note.
13532 If this is about to TODO state change, the new state is expected in STATE.
13533 When FINDPOS is non-nil, find the correct position for the note in
13534 the current entry. If not, assume that it can be inserted at point.
13535 HOW is an indicator what kind of note should be created.
13536 EXTRA is additional text that will be inserted into the notes buffer."
13537 (let* ((org-log-into-drawer (org-log-into-drawer))
13538 (drawer (cond ((stringp org-log-into-drawer)
13539 org-log-into-drawer)
13540 (org-log-into-drawer "LOGBOOK"))))
13544 (org-back-to-heading t)
13545 (narrow-to-region (point) (save-excursion
13546 (outline-next-heading) (point)))
13547 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
13548 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
13550 (goto-char (match-end 0))
13553 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
13556 (goto-char (match-end 0))
13557 (or org-log-states-order-reversed
13558 (and (re-search-forward org-property-end-re nil t)
13559 (goto-char (1- (match-beginning 0))))))
13560 (insert "\n:" drawer ":\n:END:")
13561 (beginning-of-line 0)
13563 (beginning-of-line 2)
13566 ((and org-log-state-notes-insert-after-drawers
13568 (forward-line) (looking-at org-drawer-regexp)))
13570 (while (looking-at org-drawer-regexp)
13571 (goto-char (match-end 0))
13572 (re-search-forward org-property-end-re (point-max) t)
13574 (forward-line -1)))
13575 (unless org-log-states-order-reversed
13576 (and (= (char-after) ?\n) (forward-char 1))
13577 (org-skip-over-state-notes)
13578 (skip-chars-backward " \t\n\r")))
13579 (move-marker org-log-note-marker (point))
13580 (setq org-log-note-purpose purpose
13581 org-log-note-state state
13582 org-log-note-previous-state prev-state
13583 org-log-note-how how
13584 org-log-note-extra extra
13585 org-log-note-effective-time (org-current-effective-time))
13586 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
13588 (defun org-skip-over-state-notes ()
13589 "Skip past the list of State notes in an entry."
13590 (if (looking-at "\n[ \t]*- State") (forward-char 1))
13591 (when (ignore-errors (goto-char (org-in-item-p)))
13592 (let* ((struct (org-list-struct))
13593 (prevs (org-list-prevs-alist struct)))
13594 (while (looking-at "[ \t]*- State")
13595 (goto-char (or (org-list-get-next-item (point) struct prevs)
13596 (org-list-get-item-end (point) struct)))))))
13598 (defun org-add-log-note (&optional purpose)
13599 "Pop up a window for taking a note, and add this note later at point."
13600 (remove-hook 'post-command-hook 'org-add-log-note)
13601 (setq org-log-note-window-configuration (current-window-configuration))
13602 (delete-other-windows)
13603 (move-marker org-log-note-return-to (point))
13604 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
13605 (goto-char org-log-note-marker)
13606 (org-switch-to-buffer-other-window "*Org Note*")
13608 (if (memq org-log-note-how '(time state))
13609 (let (current-prefix-arg) (org-store-log-note))
13610 (let ((org-inhibit-startup t)) (org-mode))
13611 (insert (format "# Insert note for %s.
13612 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
13614 ((eq org-log-note-purpose 'clock-out) "stopped clock")
13615 ((eq org-log-note-purpose 'done) "closed todo item")
13616 ((eq org-log-note-purpose 'state)
13617 (format "state change from \"%s\" to \"%s\""
13618 (or org-log-note-previous-state "")
13619 (or org-log-note-state "")))
13620 ((eq org-log-note-purpose 'reschedule)
13622 ((eq org-log-note-purpose 'delschedule)
13623 "no longer scheduled")
13624 ((eq org-log-note-purpose 'redeadline)
13625 "changing deadline")
13626 ((eq org-log-note-purpose 'deldeadline)
13627 "removing deadline")
13628 ((eq org-log-note-purpose 'refile)
13630 ((eq org-log-note-purpose 'note)
13632 (t (error "This should not happen")))))
13633 (if org-log-note-extra (insert org-log-note-extra))
13634 (org-set-local 'org-finish-function 'org-store-log-note)
13635 (run-hooks 'org-log-buffer-setup-hook)))
13637 (defvar org-note-abort nil) ; dynamically scoped
13638 (defun org-store-log-note ()
13639 "Finish taking a log note, and insert it to where it belongs."
13640 (let ((txt (buffer-string)))
13641 (kill-buffer (current-buffer))
13642 (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
13644 (while (string-match "\\`# .*\n[ \t\n]*" txt)
13645 (setq txt (replace-match "" t t txt)))
13646 (if (string-match "\\s-+\\'" txt)
13647 (setq txt (replace-match "" t t txt)))
13648 (setq lines (org-split-string txt "\n"))
13649 (when (and note (string-match "\\S-" note))
13651 (org-replace-escapes
13653 (list (cons "%u" (user-login-name))
13654 (cons "%U" user-full-name)
13655 (cons "%t" (format-time-string
13656 (org-time-stamp-format 'long 'inactive)
13657 org-log-note-effective-time))
13658 (cons "%T" (format-time-string
13659 (org-time-stamp-format 'long nil)
13660 org-log-note-effective-time))
13661 (cons "%d" (format-time-string
13662 (org-time-stamp-format nil 'inactive)
13663 org-log-note-effective-time))
13664 (cons "%D" (format-time-string
13665 (org-time-stamp-format nil nil)
13666 org-log-note-effective-time))
13667 (cons "%s" (if org-log-note-state
13668 (concat "\"" org-log-note-state "\"")
13670 (cons "%S" (if org-log-note-previous-state
13671 (concat "\"" org-log-note-previous-state "\"")
13673 (if lines (setq note (concat note " \\\\")))
13675 (when (or current-prefix-arg org-note-abort)
13676 (when org-log-into-drawer
13677 (org-remove-empty-drawer-at org-log-note-marker))
13680 (with-current-buffer (marker-buffer org-log-note-marker)
13682 (goto-char org-log-note-marker)
13683 (move-marker org-log-note-marker nil)
13685 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
13686 (setq ind (save-excursion
13687 (if (ignore-errors (goto-char (org-in-item-p)))
13688 (let ((struct (org-list-struct)))
13690 (org-list-get-top-point struct) struct))
13691 (skip-chars-backward " \r\t\n")
13693 ((and (org-at-heading-p)
13694 org-adapt-indentation)
13695 (1+ (org-current-level)))
13696 ((org-at-heading-p) 0)
13697 (t (org-get-indentation))))))
13698 (setq bul (org-list-bullet-string "-"))
13699 (org-indent-line-to ind)
13700 (insert bul (pop lines))
13701 (let ((ind-body (+ (length bul) ind)))
13704 (org-indent-line-to ind-body)
13705 (insert (pop lines))))
13706 (message "Note stored")
13707 (org-back-to-heading t)
13708 (org-cycle-hide-drawers 'children))
13709 ;; Fix `buffer-undo-list' when `org-store-log-note' is called
13710 ;; from within `org-add-log-note' because `buffer-undo-list'
13711 ;; is then modified outside of `org-with-remote-undo'.
13712 (when (eq this-command 'org-agenda-todo)
13713 (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
13714 ;; Don't add undo information when called from `org-agenda-todo'
13715 (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
13716 (set-window-configuration org-log-note-window-configuration)
13717 (with-current-buffer (marker-buffer org-log-note-return-to)
13718 (goto-char org-log-note-return-to))
13719 (move-marker org-log-note-return-to nil)
13720 (and org-log-post-message (message "%s" org-log-post-message))))
13722 (defun org-remove-empty-drawer-at (pos)
13723 "Remove an empty drawer at position POS.
13724 POS may also be a marker."
13725 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
13726 (org-with-wide-buffer
13728 (let ((drawer (org-element-at-point)))
13729 (when (and (memq (org-element-type drawer) '(drawer property-drawer))
13730 (not (org-element-property :contents-begin drawer)))
13731 (delete-region (org-element-property :begin drawer)
13732 (progn (goto-char (org-element-property :end drawer))
13733 (skip-chars-backward " \r\t\n")
13737 (defvar org-ts-type nil)
13738 (defun org-sparse-tree (&optional arg type)
13739 "Create a sparse tree, prompt for the details.
13740 This command can create sparse trees. You first need to select the type
13741 of match used to create the tree:
13743 t Show all TODO entries.
13744 T Show entries with a specific TODO keyword.
13745 m Show entries selected by a tags/property match.
13746 p Enter a property name and its value (both with completion on existing
13747 names/values) and show entries with that property.
13748 r Show entries matching a regular expression (`/' can be used as well).
13749 b Show deadlines and scheduled items before a date.
13750 a Show deadlines and scheduled items after a date.
13751 d Show deadlines due within `org-deadline-warning-days'.
13752 D Show deadlines and scheduled items between a date range."
13754 (setq type (or type org-sparse-tree-default-date-type))
13755 (setq org-ts-type type)
13756 (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
13757 [d]eadlines [b]efore-date [a]fter-date [D]ates range
13758 [c]ycle through date types: %s"
13760 (all "all timestamps")
13761 (scheduled "only scheduled")
13762 (deadline "only deadline")
13763 (active "only active timestamps")
13764 (inactive "only inactive timestamps")
13765 (scheduled-or-deadline "scheduled/deadline")
13766 (closed "with a closed time-stamp")
13767 (otherwise "scheduled/deadline")))
13768 (let ((answer (read-char-exclusive)))
13773 (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
13774 inactive closed)))))
13775 (?d (call-interactively 'org-check-deadlines))
13776 (?b (call-interactively 'org-check-before-date))
13777 (?a (call-interactively 'org-check-after-date))
13778 (?D (call-interactively 'org-check-dates-range))
13779 (?t (call-interactively 'org-show-todo-tree))
13780 (?T (org-show-todo-tree '(4)))
13781 (?m (call-interactively 'org-match-sparse-tree))
13783 (let* ((kwd (org-icompleting-read
13784 "Property: " (mapcar 'list (org-buffer-property-keys))))
13785 (value (org-icompleting-read
13786 "Value: " (mapcar 'list (org-property-values kwd)))))
13787 (unless (string-match "\\`{.*}\\'" value)
13788 (setq value (concat "\"" value "\"")))
13789 (org-match-sparse-tree arg (concat kwd "=" value))))
13790 ((?r ?R ?/) (call-interactively 'org-occur))
13791 (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
13793 (defvar org-occur-highlights nil
13794 "List of overlays used for occur matches.")
13795 (make-variable-buffer-local 'org-occur-highlights)
13796 (defvar org-occur-parameters nil
13797 "Parameters of the active org-occur calls.
13798 This is a list, each call to org-occur pushes as cons cell,
13799 containing the regular expression and the callback, onto the list.
13800 The list can contain several entries if `org-occur' has been called
13801 several time with the KEEP-PREVIOUS argument. Otherwise, this list
13802 will only contain one set of parameters. When the highlights are
13803 removed (for example with `C-c C-c', or with the next edit (depending
13804 on `org-remove-highlights-with-change'), this variable is emptied
13806 (make-variable-buffer-local 'org-occur-parameters)
13808 (defun org-occur (regexp &optional keep-previous callback)
13809 "Make a compact tree which shows all matches of REGEXP.
13810 The tree will show the lines where the regexp matches, and all higher
13811 headlines above the match. It will also show the heading after the match,
13812 to make sure editing the matching entry is easy.
13813 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
13814 call to `org-occur' will be kept, to allow stacking of calls to this
13816 If CALLBACK is non-nil, it is a function which is called to confirm
13817 that the match should indeed be shown."
13818 (interactive "sRegexp: \nP")
13819 (when (equal regexp "")
13820 (user-error "Regexp cannot be empty"))
13821 (unless keep-previous
13822 (org-remove-occur-highlights nil nil t))
13823 (push (cons regexp callback) org-occur-parameters)
13826 (goto-char (point-min))
13827 (if (or (not keep-previous) ; do not want to keep
13828 (not org-occur-highlights)) ; no previous matches
13831 (while (re-search-forward regexp nil t)
13832 (when (or (not callback)
13833 (save-match-data (funcall callback)))
13834 (setq cnt (1+ cnt))
13835 (when org-highlight-sparse-tree-matches
13836 (org-highlight-new-match (match-beginning 0) (match-end 0)))
13837 (org-show-context 'occur-tree))))
13838 (when org-remove-highlights-with-change
13839 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
13841 (unless org-sparse-tree-open-archived-trees
13842 (org-hide-archived-subtrees (point-min) (point-max)))
13843 (run-hooks 'org-occur-hook)
13844 (if (org-called-interactively-p 'interactive)
13845 (message "%d match(es) for regexp %s" cnt regexp))
13848 (defun org-occur-next-match (&optional n reset)
13849 "Function for `next-error-function' to find sparse tree matches.
13850 N is the number of matches to move, when negative move backwards.
13851 RESET is entirely ignored - this function always goes back to the
13852 starting point when no match is found."
13853 (let* ((limit (if (< n 0) (point-min) (point-max)))
13854 (search-func (if (< n 0)
13855 'previous-single-char-property-change
13856 'next-single-char-property-change))
13861 (while (setq p1 (funcall search-func (point) 'org-type))
13862 (when (equal p1 limit)
13864 (user-error "No more matches"))
13865 (when (equal (get-char-property p1 'org-type) 'org-occur)
13869 (throw 'exit (point))))
13872 (user-error "No more matches"))))
13874 (defun org-show-context (&optional key)
13875 "Make sure point and context are visible.
13876 How much context is shown depends upon the variables
13877 `org-show-hierarchy-above', `org-show-following-heading',
13878 `org-show-entry-below' and `org-show-siblings'."
13879 (let ((heading-p (org-at-heading-p t))
13880 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
13881 (following-p (org-get-alist-option org-show-following-heading key))
13882 (entry-p (org-get-alist-option org-show-entry-below key))
13883 (siblings-p (org-get-alist-option org-show-siblings key)))
13884 ;; Show heading or entry text
13885 (if (and heading-p (not entry-p))
13886 (org-flag-heading nil) ; only show the heading
13887 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
13888 (org-show-hidden-entry))) ; show entire entry
13890 ;; Show next sibling, or heading below text
13892 (and (if heading-p (org-goto-sibling) (outline-next-heading))
13893 (org-flag-heading nil))))
13894 (when siblings-p (org-show-siblings))
13896 ;; show all higher headings, possibly with siblings
13898 (while (and (ignore-errors (progn (org-up-heading-all 1) t))
13900 (org-flag-heading nil)
13901 (when siblings-p (org-show-siblings)))))))
13903 (defvar org-reveal-start-hook nil
13904 "Hook run before revealing a location.")
13906 (defun org-reveal (&optional siblings)
13907 "Show current entry, hierarchy above it, and the following headline.
13908 This can be used to show a consistent set of context around locations
13909 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
13910 not t for the search context.
13912 With optional argument SIBLINGS, on each level of the hierarchy all
13913 siblings are shown. This repairs the tree structure to what it would
13914 look like when opened with hierarchical calls to `org-cycle'.
13915 With double optional argument \\[universal-argument] \\[universal-argument], \
13916 go to the parent and show the
13919 (run-hooks 'org-reveal-start-hook)
13920 (let ((org-show-hierarchy-above t)
13921 (org-show-following-heading t)
13922 (org-show-siblings (if siblings t org-show-siblings)))
13923 (org-show-context nil))
13924 (when (equal siblings '(16))
13926 (when (org-up-heading-safe)
13928 (run-hook-with-args 'org-cycle-hook 'subtree)))))
13930 (defun org-highlight-new-match (beg end)
13931 "Highlight from BEG to END and mark the highlight is an occur headline."
13932 (let ((ov (make-overlay beg end)))
13933 (overlay-put ov 'face 'secondary-selection)
13934 (overlay-put ov 'org-type 'org-occur)
13935 (push ov org-occur-highlights)))
13937 (defun org-remove-occur-highlights (&optional beg end noremove)
13938 "Remove the occur highlights from the buffer.
13939 BEG and END are ignored. If NOREMOVE is nil, remove this function
13940 from the `before-change-functions' in the current buffer."
13942 (unless org-inhibit-highlight-removal
13943 (mapc 'delete-overlay org-occur-highlights)
13944 (setq org-occur-highlights nil)
13945 (setq org-occur-parameters nil)
13947 (remove-hook 'before-change-functions
13948 'org-remove-occur-highlights 'local))))
13952 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
13953 "Regular expression matching the priority indicator.")
13955 (defvar org-remove-priority-next-time nil)
13957 (defun org-priority-up ()
13958 "Increase the priority of the current item."
13960 (org-priority 'up))
13962 (defun org-priority-down ()
13963 "Decrease the priority of the current item."
13965 (org-priority 'down))
13967 (defun org-priority (&optional action show)
13968 "Change the priority of an item.
13969 ACTION can be `set', `up', `down', or a character."
13971 (if (equal action '(4))
13972 (org-show-priority)
13973 (unless org-enable-priority-commands
13974 (user-error "Priority commands are disabled"))
13975 (setq action (or action 'set))
13976 (let (current new news have remove)
13978 (org-back-to-heading t)
13979 (if (looking-at org-priority-regexp)
13980 (setq current (string-to-char (match-string 2))
13983 ((eq action 'remove)
13984 (setq remove t new ?\ ))
13985 ((or (eq action 'set)
13986 (if (featurep 'xemacs) (characterp action) (integerp action)))
13987 (if (not (eq action 'set))
13989 (message "Priority %c-%c, SPC to remove: "
13990 org-highest-priority org-lowest-priority)
13992 (setq new (read-char-exclusive))))
13993 (if (and (= (upcase org-highest-priority) org-highest-priority)
13994 (= (upcase org-lowest-priority) org-lowest-priority))
13995 (setq new (upcase new)))
13996 (cond ((equal new ?\ ) (setq remove t))
13997 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
13998 (user-error "Priority must be between `%c' and `%c'"
13999 org-highest-priority org-lowest-priority))))
14002 (1- current) ; normal cycling
14003 ;; last priority was empty
14004 (if (eq last-command this-command)
14005 org-lowest-priority ; wrap around empty to lowest
14007 (if org-priority-start-cycle-with-default
14008 org-default-priority
14009 (1- org-default-priority))))))
14012 (1+ current) ; normal cycling
14013 ;; last priority was empty
14014 (if (eq last-command this-command)
14015 org-highest-priority ; wrap around empty to highest
14017 (if org-priority-start-cycle-with-default
14018 org-default-priority
14019 (1+ org-default-priority))))))
14020 (t (user-error "Invalid action")))
14021 (if (or (< (upcase new) org-highest-priority)
14022 (> (upcase new) org-lowest-priority))
14023 (if (and (memq action '(up down))
14024 (not have) (not (eq last-command this-command)))
14025 ;; `new' is from default priority
14027 "The default can not be set, see `org-default-priority' why")
14028 ;; normal cycling: `new' is beyond highest/lowest priority
14029 ;; and is wrapped around to the empty priority
14031 (setq news (format "%c" new))
14034 (replace-match "" t t nil 1)
14035 (replace-match news t t nil 2))
14037 (user-error "No priority cookie found in line")
14038 (let ((case-fold-search nil))
14039 (looking-at org-todo-line-regexp))
14042 (goto-char (match-end 2))
14043 (insert " [#" news "]"))
14044 (goto-char (match-beginning 3))
14045 (insert "[#" news "] "))))
14046 (org-set-tags nil 'align))
14048 (message "Priority removed")
14049 (message "Priority of current item set to %s" news)))))
14051 (defun org-show-priority ()
14052 "Show the priority of the current item.
14053 This priority is composed of the main priority given with the [#A] cookies,
14054 and by additional input from the age of a schedules or deadline entry."
14056 (let ((pri (if (eq major-mode 'org-agenda-mode)
14057 (org-get-at-bol 'priority)
14060 (beginning-of-line)
14061 (and (looking-at org-heading-regexp)
14062 (org-get-priority (match-string 0))))))))
14063 (message "Priority is %d" (if pri pri -1000))))
14065 (defun org-get-priority (s)
14066 "Find priority cookie and return priority."
14068 (if (functionp org-get-priority-function)
14069 (funcall org-get-priority-function)
14070 (if (not (string-match org-priority-regexp s))
14071 (* 1000 (- org-lowest-priority org-default-priority))
14072 (* 1000 (- org-lowest-priority
14073 (string-to-char (match-string 2 s))))))))
14077 (defvar org-agenda-archives-mode)
14078 (defvar org-map-continue-from nil
14079 "Position from where mapping should continue.
14080 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
14082 (defvar org-scanner-tags nil
14083 "The current tag list while the tags scanner is running.")
14084 (defvar org-trust-scanner-tags nil
14085 "Should `org-get-tags-at' use the tags for the scanner.
14086 This is for internal dynamical scoping only.
14087 When this is non-nil, the function `org-get-tags-at' will return the value
14088 of `org-scanner-tags' instead of building the list by itself. This
14089 can lead to large speed-ups when the tags scanner is used in a file with
14090 many entries, and when the list of tags is retrieved, for example to
14091 obtain a list of properties. Building the tags list for each entry in such
14092 a file becomes an N^2 operation - but with this variable set, it scales
14095 (defun org-scan-tags (action matcher todo-only &optional start-level)
14096 "Scan headline tags with inheritance and produce output ACTION.
14098 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
14099 or `agenda' to produce an entry list for an agenda view. It can also be
14100 a Lisp form or a function that should be called at each matched headline, in
14101 this case the return value is a list of all return values from these calls.
14103 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
14104 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
14105 only lines with a not-done TODO keyword are included in the output.
14106 This should be the same variable that was scoped into
14107 and set by `org-make-tags-matcher' when it constructed MATCHER.
14109 START-LEVEL can be a string with asterisks, reducing the scope to
14110 headlines matching this string."
14111 (require 'org-agenda)
14112 (let* ((re (concat "^"
14114 ;; Get the correct level to match
14115 (concat "\\*\\{" (number-to-string start-level) "\\} ")
14116 org-outline-regexp)
14118 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
14119 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
14120 (props (list 'face 'default
14121 'done-face 'org-agenda-done
14122 'undone-face 'default
14123 'mouse-face 'highlight
14124 'org-not-done-regexp org-not-done-regexp
14125 'org-todo-regexp org-todo-regexp
14126 'org-complex-heading-regexp org-complex-heading-regexp
14128 (format "mouse-2 or RET jump to org file %s"
14129 (abbreviate-file-name
14130 (or (buffer-file-name (buffer-base-buffer))
14131 (buffer-name (buffer-base-buffer)))))))
14132 (org-map-continue-from nil)
14133 lspos tags tags-list
14134 (tags-alist (list (cons 0 org-file-tags)))
14135 (llast 0) rtn rtn1 level category i txt
14136 todo marker entry priority)
14137 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
14138 (setq action (list 'lambda nil action)))
14140 (goto-char (point-min))
14141 (when (eq action 'sparse-tree)
14143 (org-remove-occur-highlights))
14144 (while (let (case-fold-search)
14145 (re-search-forward re nil t))
14146 (setq org-map-continue-from nil)
14148 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
14149 tags (if (match-end 4) (org-match-string-no-properties 4)))
14150 (goto-char (setq lspos (match-beginning 0)))
14151 (setq level (org-reduced-level (org-outline-level))
14152 category (org-get-category))
14153 (setq i llast llast level)
14154 ;; remove tag lists from same and sublevels
14155 (while (>= i level)
14156 (when (setq entry (assoc i tags-alist))
14157 (setq tags-alist (delete entry tags-alist)))
14159 ;; add the next tags
14161 (setq tags (org-split-string tags ":")
14163 (cons (cons level tags) tags-alist)))
14164 ;; compile tags for current headline
14166 (if org-use-tag-inheritance
14167 (apply 'append (mapcar 'cdr (reverse tags-alist)))
14169 org-scanner-tags tags-list)
14170 (when org-use-tag-inheritance
14171 (setcdr (car tags-alist)
14172 (mapcar (lambda (x)
14173 (setq x (copy-sequence x))
14174 (org-add-prop-inherited x))
14175 (cdar tags-alist))))
14176 (when (and tags org-use-tag-inheritance
14177 (or (not (eq t org-use-tag-inheritance))
14178 org-tags-exclude-from-inheritance))
14179 ;; selective inheritance, remove uninherited ones
14180 (setcdr (car tags-alist)
14181 (org-remove-uninherited-tags (cdar tags-alist))))
14184 ;; eval matcher only when the todo condition is OK
14185 (and (or (not todo-only) (member todo org-not-done-keywords))
14186 (let ((case-fold-search t) (org-trust-scanner-tags t))
14189 ;; Call the skipper, but return t if it does not skip,
14190 ;; so that the `and' form continues evaluating
14192 (unless (eq action 'sparse-tree) (org-agenda-skip))
14195 ;; Check if timestamps are deselecting this entry
14196 (or (not todo-only)
14197 (and (member todo org-not-done-keywords)
14198 (or (not org-agenda-tags-todo-honor-ignore-options)
14199 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
14201 ;; select this headline
14203 ((eq action 'sparse-tree)
14204 (and org-highlight-sparse-tree-matches
14205 (org-get-heading) (match-end 0)
14206 (org-highlight-new-match
14207 (match-beginning 1) (match-end 1)))
14208 (org-show-context 'tags-tree))
14209 ((eq action 'agenda)
14210 (setq txt (org-agenda-format-item
14213 (if (eq org-tags-match-list-sublevels 'indented)
14214 (make-string (1- level) ?.) "")
14218 priority (org-get-priority txt))
14220 (setq marker (org-agenda-new-marker))
14221 (org-add-props txt props
14222 'org-marker marker 'org-hd-marker marker 'org-category category
14224 'priority priority 'type "tagsmatch")
14226 ((functionp action)
14227 (setq org-map-continue-from nil)
14229 (setq rtn1 (funcall action))
14231 (t (user-error "Invalid action")))
14233 ;; if we are to skip sublevels, jump to end of subtree
14234 (unless org-tags-match-list-sublevels
14235 (org-end-of-subtree t)
14236 (backward-char 1))))
14237 ;; Get the correct position from where to continue
14238 (if org-map-continue-from
14239 (goto-char org-map-continue-from)
14240 (and (= (point) lspos) (end-of-line 1)))))
14241 (when (and (eq action 'sparse-tree)
14242 (not org-sparse-tree-open-archived-trees))
14243 (org-hide-archived-subtrees (point-min) (point-max)))
14246 (defun org-remove-uninherited-tags (tags)
14247 "Remove all tags that are not inherited from the list TAGS."
14249 ((eq org-use-tag-inheritance t)
14250 (if org-tags-exclude-from-inheritance
14251 (org-delete-all org-tags-exclude-from-inheritance tags)
14253 ((not org-use-tag-inheritance) nil)
14254 ((stringp org-use-tag-inheritance)
14257 (if (and (string-match org-use-tag-inheritance x)
14258 (not (member x org-tags-exclude-from-inheritance)))
14261 ((listp org-use-tag-inheritance)
14264 (if (member x org-use-tag-inheritance) x nil))
14267 (defun org-match-sparse-tree (&optional todo-only match)
14268 "Create a sparse tree according to tags string MATCH.
14269 MATCH can contain positive and negative selection of tags, like
14270 \"+WORK+URGENT-WITHBOSS\".
14271 If optional argument TODO-ONLY is non-nil, only select lines that are
14274 (org-agenda-prepare-buffers (list (current-buffer)))
14275 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
14277 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
14279 (defvar org-cached-props nil)
14280 (defun org-cached-entry-get (pom property)
14281 (if (or (eq t org-use-property-inheritance)
14282 (and (stringp org-use-property-inheritance)
14283 (string-match org-use-property-inheritance property))
14284 (and (listp org-use-property-inheritance)
14285 (member property org-use-property-inheritance)))
14286 ;; Caching is not possible, check it directly
14287 (org-entry-get pom property 'inherit)
14288 ;; Get all properties, so that we can do complicated checks easily
14289 (cdr (assoc property (or org-cached-props
14290 (setq org-cached-props
14291 (org-entry-properties pom)))))))
14293 (defun org-global-tags-completion-table (&optional files)
14294 "Return the list of all tags in all agenda buffer/files.
14295 Optional FILES argument is a list of files which can be used
14296 instead of the agenda files."
14303 (set-buffer (find-file-noselect file))
14304 (append (org-get-buffer-tags)
14305 (mapcar (lambda (x) (if (stringp (car-safe x))
14306 (list (car-safe x)) nil))
14308 (if (and files (car files))
14310 (org-agenda-files))))))))
14312 (defun org-make-tags-matcher (match)
14313 "Create the TAGS/TODO matcher form for the selection string MATCH.
14315 The variable `todo-only' is scoped dynamically into this function.
14316 It will be set to t if the matcher restricts matching to TODO entries,
14317 otherwise will not be touched.
14319 Returns a cons of the selection string MATCH and the constructed
14320 lisp form implementing the matcher. The matcher is to be evaluated
14321 at an Org entry, with point on the headline, and returns t if the
14322 entry matches the selection string MATCH. The returned lisp form
14323 references two variables with information about the entry, which
14324 must be bound around the form's evaluation: todo, the TODO keyword
14325 at the entry (or nil of none); and tags-list, the list of all tags
14326 at the entry including inherited ones. Additionally, the category
14327 of the entry (if any) must be specified as the text property
14328 'org-category on the headline.
14330 See also `org-scan-tags'.
14332 (declare (special todo-only))
14333 (unless (boundp 'todo-only)
14334 (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
14336 ;; Get a new match request, with completion against the global
14337 ;; tags table and the local tags in current buffer
14338 (let ((org-last-tags-completion-table
14340 (delq nil (append (org-get-buffer-tags)
14341 (org-global-tags-completion-table))))))
14342 (setq match (org-completing-read-no-i
14343 "Match: " 'org-tags-completion-function nil nil nil
14344 'org-tags-history))))
14346 ;; Parse the string and create a lisp form
14347 (let ((match0 match)
14348 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
14350 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
14351 orterms term orlist re-p str-p level-p level-op time-p
14352 prop-p pn pv po gv rest (start 0) (ss 0))
14353 ;; Expand group tags
14354 (setq match (org-tags-expand match))
14356 ;; Check if there is a TODO part of this match, which would be the
14357 ;; part after a "/". TO make sure that this slash is not part of
14358 ;; a property value to be matched against, we also check that there
14359 ;; is no " after that slash.
14360 ;; First, find the last slash
14361 (while (string-match "/+" match ss)
14362 (setq start (match-beginning 0) ss (match-end 0)))
14363 (if (and (string-match "/+" match start)
14364 (not (save-match-data (string-match "\"" match start))))
14365 ;; match contains also a todo-matching request
14367 (setq tagsmatch (substring match 0 (match-beginning 0))
14368 todomatch (substring match (match-end 0)))
14369 (if (string-match "^!" todomatch)
14370 (setq todo-only t todomatch (substring todomatch 1)))
14371 (if (string-match "^\\s-*$" todomatch)
14372 (setq todomatch nil)))
14373 ;; only matching tags
14374 (setq tagsmatch match todomatch nil))
14376 ;; Make the tags matcher
14377 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
14378 (setq tagsmatcher t)
14379 (setq orterms (org-split-string tagsmatch "|") orlist nil)
14380 (while (setq term (pop orterms))
14381 (while (and (equal (substring term -1) "\\") orterms)
14382 (setq term (concat term "|" (pop orterms)))) ; repair bad split
14383 (while (string-match re term)
14384 (setq rest (substring term (match-end 0))
14385 minus (and (match-end 1)
14386 (equal (match-string 1 term) "-"))
14387 tag (save-match-data (replace-regexp-in-string
14389 (match-string 2 term)))
14390 re-p (equal (string-to-char tag) ?{)
14391 level-p (match-end 4)
14392 prop-p (match-end 5)
14394 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
14396 (setq level-op (org-op-to-function (match-string 3 term)))
14397 `(,level-op level ,(string-to-number
14398 (match-string 4 term))))
14400 (setq pn (match-string 5 term)
14401 po (match-string 6 term)
14402 pv (match-string 7 term)
14403 re-p (equal (string-to-char pv) ?{)
14404 str-p (equal (string-to-char pv) ?\")
14405 time-p (save-match-data
14406 (string-match "^\"[[<].*[]>]\"$" pv))
14407 pv (if (or re-p str-p) (substring pv 1 -1) pv))
14408 (if time-p (setq pv (org-matcher-time pv)))
14409 (setq po (org-op-to-function po (if time-p 'time str-p)))
14411 ((equal pn "CATEGORY")
14412 (setq gv '(get-text-property (point) 'org-category)))
14416 (setq gv `(org-cached-entry-get nil ,pn))))
14419 `(not (string-match ,pv (or ,gv "")))
14420 `(string-match ,pv (or ,gv "")))
14422 `(,po (or ,gv "") ,pv)
14423 `(,po (string-to-number (or ,gv ""))
14424 ,(string-to-number pv) ))))
14425 (t `(member ,tag tags-list)))
14426 mm (if minus (list 'not mm) mm)
14428 (push mm tagsmatcher))
14429 (push (if (> (length tagsmatcher) 1)
14430 (cons 'and tagsmatcher)
14433 (setq tagsmatcher nil))
14434 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
14436 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
14437 ;; Make the todo matcher
14438 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
14439 (setq todomatcher t)
14440 (setq orterms (org-split-string todomatch "|") orlist nil)
14441 (while (setq term (pop orterms))
14442 (while (string-match re term)
14443 (setq minus (and (match-end 1)
14444 (equal (match-string 1 term) "-"))
14445 kwd (match-string 2 term)
14446 re-p (equal (string-to-char kwd) ?{)
14447 term (substring term (match-end 0))
14449 `(string-match ,(substring kwd 1 -1) todo)
14450 (list 'equal 'todo kwd))
14451 mm (if minus (list 'not mm) mm))
14452 (push mm todomatcher))
14453 (push (if (> (length todomatcher) 1)
14454 (cons 'and todomatcher)
14457 (setq todomatcher nil))
14458 (setq todomatcher (if (> (length orlist) 1)
14459 (cons 'or orlist) (car orlist))))
14461 ;; Return the string and lisp forms of the matcher
14462 (setq matcher (if todomatcher
14463 (list 'and tagsmatcher todomatcher)
14466 (setq matcher (list 'and '(member todo org-not-done-keywords)
14468 (cons match0 matcher)))
14470 (defun org-tags-expand (match &optional single-as-list downcased)
14471 "Expand group tags in MATCH.
14473 This replaces every group tag in MATCH with a regexp tag search.
14474 For example, a group tag \"Work\" defined as { Work : Lab Conf }
14475 will be replaced like this:
14477 Work => {\\(?:Work\\|Lab\\|Conf\\)}
14478 +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
14479 -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
14481 Replacing by a regexp preserves the structure of the match.
14482 E.g., this expansion
14484 Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
14486 will match anything tagged with \"Lab\" and \"Home\", or tagged
14487 with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
14489 When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
14490 assumed to be a single group tag, and the function will return
14491 the list of tags in this group.
14493 When DOWNCASE is non-nil, expand downcased TAGS."
14495 (let* ((case-fold-search t)
14496 (stable org-mode-syntax-table)
14497 (tal (or org-tag-groups-alist-for-agenda
14498 org-tag-groups-alist))
14500 (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
14501 (tml (mapcar 'car tal))
14502 (rtnmatch match) rpl)
14503 ;; @ and _ are allowed as word-components in tags
14504 (modify-syntax-entry ?@ "w" stable)
14505 (modify-syntax-entry ?_ "w" stable)
14507 (with-syntax-table stable
14509 (concat "\\(?1:[+-]?\\)\\(?2:\\<"
14510 (regexp-opt tml) "\\>\\)") rtnmatch)))
14511 (let* ((dir (match-string 1 rtnmatch))
14512 (tag (match-string 2 rtnmatch))
14513 (tag (if downcased (downcase tag) tag)))
14514 (setq tml (delete tag tml))
14515 (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
14516 (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
14517 (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
14518 (if (stringp rpl) (org-add-props rpl '(grouptag t)))
14519 (setq rtnmatch (replace-match rpl t t rtnmatch)))))
14521 (or (reverse rpl) (list rtnmatch))
14523 (if single-as-list (list (if downcased (downcase match) match))
14526 (defun org-op-to-function (op &optional stringp)
14527 "Turn an operator into the appropriate function."
14530 ((equal op "<" ) '(< string< org-time<))
14531 ((equal op ">" ) '(> org-string> org-time>))
14532 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
14533 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
14534 ((member op '("=" "==")) '(= string= org-time=))
14535 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
14536 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
14538 (defun org<> (a b) (not (= a b)))
14539 (defun org-string<= (a b) (or (string= a b) (string< a b)))
14540 (defun org-string>= (a b) (not (string< a b)))
14541 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
14542 (defun org-string<> (a b) (not (string= a b)))
14543 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
14544 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
14545 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
14546 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
14547 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
14548 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
14550 "Convert S to a floating point time.
14551 If S is already a number, just return it. If it is a string, parse
14552 it as a time string and apply `float-time' to it. If S is nil, just return 0."
14556 (condition-case nil
14557 (float-time (apply 'encode-time (org-parse-time-string s)))
14561 (defun org-time-today ()
14562 "Time in seconds today at 0:00.
14563 Returns the float number of seconds since the beginning of the
14564 epoch to the beginning of today (00:00)."
14565 (float-time (apply 'encode-time
14566 (append '(0 0 0) (nthcdr 3 (decode-time))))))
14568 (defun org-matcher-time (s)
14569 "Interpret a time comparison value."
14572 ((string= s "<now>") (float-time))
14573 ((string= s "<today>") (org-time-today))
14574 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
14575 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
14576 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
14577 (+ (org-time-today)
14578 (* (string-to-number (match-string 1 s))
14579 (cdr (assoc (match-string 2 s)
14580 '(("d" . 86400.0) ("w" . 604800.0)
14581 ("m" . 2678400.0) ("y" . 31557600.0)))))))
14584 (defun org-match-any-p (re list)
14585 "Does re match any element of list?"
14586 (setq list (mapcar (lambda (x) (string-match re x)) list))
14589 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
14590 (defvar org-tags-overlay (make-overlay 1 1))
14591 (org-detach-overlay org-tags-overlay)
14593 (defun org-get-local-tags-at (&optional pos)
14594 "Get a list of tags defined in the current headline."
14595 (org-get-tags-at pos 'local))
14597 (defun org-get-local-tags ()
14598 "Get a list of tags defined in the current headline."
14599 (org-get-tags-at nil 'local))
14601 (defun org-get-tags-at (&optional pos local)
14602 "Get a list of all headline tags applicable at POS.
14603 POS defaults to point. If tags are inherited, the list contains
14604 the targets in the same sequence as the headlines appear, i.e.
14605 the tags of the current headline come last.
14606 When LOCAL is non-nil, only return tags from the current headline,
14607 ignore inherited ones."
14609 (if (and org-trust-scanner-tags
14610 (or (not pos) (equal pos (point)))
14613 (let (tags ltags lastpos parent)
14617 (goto-char (or pos (point)))
14620 (condition-case nil
14622 (org-back-to-heading t)
14623 (while (not (equal lastpos (point)))
14624 (setq lastpos (point))
14626 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
14627 (setq ltags (org-split-string
14628 (org-match-string-no-properties 1) ":"))
14630 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
14633 (org-remove-uninherited-tags ltags)
14636 (or org-use-tag-inheritance (throw 'done t))
14637 (if local (throw 'done t))
14638 (or (org-up-heading-safe) (error nil))
14643 (reverse (delete-dups
14645 (org-remove-uninherited-tags
14646 org-file-tags) tags)))))))))
14648 (defun org-add-prop-inherited (s)
14649 (add-text-properties 0 (length s) '(inherited t) s)
14652 (defun org-toggle-tag (tag &optional onoff)
14653 "Toggle the tag TAG for the current line.
14654 If ONOFF is `on' or `off', don't toggle but set to this state."
14657 (org-back-to-heading t)
14658 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
14661 (setq current (match-string 1))
14662 (replace-match ""))
14664 (setq current (nreverse (org-split-string current ":")))
14668 (or (member tag current) (push tag current)))
14670 (or (not (member tag current)) (setq current (delete tag current))))
14671 (t (if (member tag current)
14672 (setq current (delete tag current))
14674 (push tag current))))
14678 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
14679 (org-set-tags nil t))
14680 (delete-horizontal-space))
14681 (run-hooks 'org-after-tags-change-hook))
14684 (defun org-align-tags-here (to-col)
14685 ;; Assumes that this is a headline
14686 "Align tags on the current headline to TO-COL."
14687 (let ((pos (point)) (col (current-column)) ncol tags-l p)
14688 (beginning-of-line 1)
14689 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14690 (< pos (match-beginning 2)))
14692 (setq tags-l (- (match-end 2) (match-beginning 2)))
14693 (goto-char (match-beginning 1))
14695 (delete-region (point) (1+ (match-beginning 2)))
14696 (setq ncol (max (current-column)
14700 (- (abs to-col) tags-l))))
14702 (insert (make-string (- ncol (current-column)) ?\ ))
14703 (setq ncol (current-column))
14704 (when indent-tabs-mode (tabify p (point-at-eol)))
14705 (org-move-to-column (min ncol col)))
14708 (defun org-set-tags-command (&optional arg just-align)
14709 "Call the set-tags command for the current entry."
14711 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
14712 (org-set-tags arg just-align)
14714 (unless (and (org-region-active-p)
14715 org-loop-over-headlines-in-active-region)
14716 (org-back-to-heading t))
14717 (org-set-tags arg just-align))))
14719 (defun org-set-tags-to (data)
14720 "Set the tags of the current entry to DATA, replacing the current tags.
14721 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
14722 If DATA is nil or the empty string, any tags will be removed."
14723 (interactive "sTags: ")
14727 ((equal data "") "")
14729 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
14732 (concat ":" (mapconcat 'identity data ":") ":"))))
14735 (org-back-to-heading t)
14736 (when (looking-at org-complex-heading-regexp)
14739 (goto-char (match-beginning 5))
14741 (delete-region (point) (point-at-eol))
14742 (org-set-tags nil 'align))
14743 (goto-char (point-at-eol))
14745 (org-set-tags nil 'align)))
14746 (beginning-of-line 1)
14747 (if (looking-at ".*?\\([ \t]+\\)$")
14748 (delete-region (match-beginning 1) (match-end 1))))))
14750 (defun org-align-all-tags ()
14751 "Align the tags i all headings."
14754 (or (ignore-errors (org-back-to-heading t))
14755 (outline-next-heading))
14756 (if (org-at-heading-p)
14758 (message "No headings"))))
14760 (defvar org-indent-indentation-per-level)
14761 (defun org-set-tags (&optional arg just-align)
14762 "Set the tags for the current headline.
14763 With prefix ARG, realign all tags in headings in the current buffer.
14764 When JUST-ALIGN is non-nil, only align tags."
14766 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
14767 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
14768 'region-start-level 'region))
14769 org-loop-over-headlines-in-active-region)
14771 ;; We don't use ARG and JUST-ALIGN here because these args
14772 ;; are not useful when looping over headlines.
14774 org-loop-over-headlines-in-active-region
14775 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
14776 (let* ((re org-outline-regexp-bol)
14777 (current (unless arg (org-get-tags-string)))
14778 (col (current-column))
14779 (org-setting-tags t)
14780 table current-tags inherited-tags ; computed below when needed
14781 tags p0 c0 c1 rpl di tc level)
14784 (goto-char (point-min))
14785 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
14786 (while (re-search-forward re nil t)
14787 (org-set-tags nil t)
14789 (message "All tags realigned to column %d" org-tags-column))
14791 (setq tags current)
14792 ;; Get a new set of tags from the user
14794 (setq table (append org-tag-persistent-alist
14795 (or org-tag-alist (org-get-buffer-tags))
14797 org-complete-tags-always-offer-all-agenda-tags
14798 (org-global-tags-completion-table
14799 (org-agenda-files))))
14800 org-last-tags-completion-table table
14801 current-tags (org-split-string current ":")
14802 inherited-tags (nreverse
14803 (nthcdr (length current-tags)
14804 (nreverse (org-get-tags-at))))
14806 (if (or (eq t org-use-fast-tag-selection)
14807 (and org-use-fast-tag-selection
14808 (delq nil (mapcar 'cdr table))))
14809 (org-fast-tag-selection
14810 current-tags inherited-tags table
14811 (if org-fast-tag-selection-include-todo
14812 org-todo-key-alist))
14813 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
14815 (org-icompleting-read "Tags: "
14816 'org-tags-completion-function
14817 nil nil current 'org-tags-history))))))
14818 (while (string-match "[-+&]+" tags)
14819 ;; No boolean logic, just a list
14820 (setq tags (replace-match ":" t t tags))))
14822 (setq tags (replace-regexp-in-string "[,]" ":" tags))
14824 (if org-tags-sort-function
14825 (setq tags (mapconcat 'identity
14826 (sort (org-split-string
14827 tags (org-re "[^[:alnum:]_@#%]+"))
14828 org-tags-sort-function) ":")))
14830 (if (string-match "\\`[\t ]*\\'" tags)
14832 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
14833 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
14835 ;; Insert new tags at the correct column
14836 (beginning-of-line 1)
14837 (setq level (or (and (looking-at org-outline-regexp)
14838 (- (match-end 0) (point) 1))
14841 ((and (equal current "") (equal tags "")))
14842 ((re-search-forward
14843 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
14845 (if (equal tags "")
14847 (goto-char (match-beginning 0))
14848 (setq c0 (current-column)
14849 ;; compute offset for the case of org-indent-mode active
14850 di (if (org-bound-and-true-p org-indent-mode)
14851 (* (1- org-indent-indentation-per-level) (1- level))
14853 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
14854 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
14855 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
14856 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
14857 (replace-match rpl t t)
14858 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
14860 (t (error "Tags alignment failed")))
14861 (org-move-to-column col)
14863 (run-hooks 'org-after-tags-change-hook))))))
14865 (defun org-change-tag-in-region (beg end tag off)
14866 "Add or remove TAG for each entry in the region.
14867 This works in the agenda, and also in an org-mode buffer."
14869 (list (region-beginning) (region-end)
14870 (let ((org-last-tags-completion-table
14871 (if (derived-mode-p 'org-mode)
14873 (delq nil (append (org-get-buffer-tags)
14874 (org-global-tags-completion-table))))
14875 (org-global-tags-completion-table))))
14876 (org-icompleting-read
14877 "Tag: " 'org-tags-completion-function nil nil nil
14878 'org-tags-history))
14880 (message "[s]et or
[r]emove? ")
14881 (equal (read-char-exclusive) ?r))))
14882 (if (fboundp 'deactivate-mark) (deactivate-mark))
14883 (let ((agendap (equal major-mode 'org-agenda-mode))
14884 l1 l2 m buf pos newhead (cnt 0))
14886 (setq l2 (1- (org-current-line)))
14888 (setq l1 (org-current-line))
14889 (loop for l from l1 to l2 do
14891 (setq m (get-text-property (point) 'org-hd-marker))
14892 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
14894 (setq buf (if agendap (marker-buffer m) (current-buffer))
14895 pos (if agendap m (point)))
14896 (with-current-buffer buf
14900 (setq cnt (1+ cnt))
14901 (org-toggle-tag tag (if off 'off 'on))
14902 (setq newhead (org-get-heading)))))
14903 (and agendap (org-agenda-change-all-lines newhead m))))
14904 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
14906 (defun org-tags-completion-function (string predicate &optional flag)
14907 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
14908 (confirm (lambda (x) (stringp (car x)))))
14909 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
14910 (setq s1 (match-string 1 string)
14911 s2 (match-string 2 string))
14912 (setq s1 "" s2 string))
14916 (setq rtn (try-completion s2 ctable confirm))
14919 (concat s1 s2 (substring rtn (length s2))
14920 (if (and org-add-colon-after-tag-completion
14921 (assoc rtn ctable))
14926 (all-completions s2 ctable confirm))
14929 (assoc s2 ctable)))))
14931 (defun org-fast-tag-insert (kwd tags face &optional end)
14932 "Insert KDW, and the TAGS, the latter with face FACE.
14934 (insert (format "%-12s" (concat kwd ":"))
14935 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
14938 (defun org-fast-tag-show-exit (flag)
14941 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
14942 (replace-match ""))
14945 (org-move-to-column (- (window-width) 19) t)
14946 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
14948 (defun org-set-current-tags-overlay (current prefix)
14949 "Add an overlay to CURRENT tag with PREFIX."
14950 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
14951 (if (featurep 'xemacs)
14952 (org-overlay-display org-tags-overlay (concat prefix s)
14953 'secondary-selection)
14954 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
14955 (org-overlay-display org-tags-overlay (concat prefix s)))))
14957 (defvar org-last-tag-selection-key nil)
14958 (defun org-fast-tag-selection (current inherited table &optional todo-table)
14959 "Fast tag selection with single keys.
14960 CURRENT is the current list of tags in the headline, INHERITED is the
14961 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
14962 possibly with grouping information. TODO-TABLE is a similar table with
14963 TODO keywords, should these have keys assigned to them.
14964 If the keys are nil, a-z are automatically assigned.
14965 Returns the new tags string, or nil to not change the current settings."
14966 (let* ((fulltable (append table todo-table))
14967 (maxlen (apply 'max (mapcar
14969 (if (stringp (car x)) (string-width (car x)) 0))
14971 (buf (current-buffer))
14972 (expert (eq org-fast-tag-selection-single-key 'expert))
14974 (fwidth (+ maxlen 3 1 3))
14975 (ncol (/ (- (window-width) 4) fwidth))
14978 tg cnt e c char c1 c2 ntable tbl rtn
14979 ov-start ov-end ov-prefix
14980 (exit-after-next org-fast-tag-selection-single-key)
14981 (done-keywords org-done-keywords)
14984 (beginning-of-line 1)
14986 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14987 (setq ov-start (match-beginning 1)
14988 ov-end (match-end 1)
14990 (setq ov-start (1- (point-at-eol))
14991 ov-end (1+ ov-start))
14992 (skip-chars-forward "^\n\r")
14995 (buffer-substring (1- (point)) (point))
14996 (if (> (current-column) org-tags-column)
14998 (make-string (- org-tags-column (current-column)) ?\ ))))))
14999 (move-overlay org-tags-overlay ov-start ov-end)
15000 (save-window-excursion
15002 (set-buffer (get-buffer-create " *Org tags*"))
15003 (delete-other-windows)
15004 (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
15005 (org-switch-to-buffer-other-window " *Org tags*"))
15007 (org-set-local 'org-done-keywords done-keywords)
15008 (org-fast-tag-insert "Inherited" inherited i-face "\n")
15009 (org-fast-tag-insert "Current" current c-face "\n\n")
15010 (org-fast-tag-show-exit exit-after-next)
15011 (org-set-current-tags-overlay current ov-prefix)
15012 (setq tbl fulltable char ?a cnt 0)
15013 (while (setq e (pop tbl))
15015 ((equal (car e) :startgroup)
15016 (push '() groups) (setq ingroup t)
15017 (when (not (= cnt 0))
15020 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
15021 ((equal (car e) :endgroup)
15022 (setq ingroup nil cnt 0)
15023 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
15024 ((equal e '(:newline))
15025 (when (not (= cnt 0))
15029 (while (equal (car tbl) '(:newline))
15031 (setq tbl (cdr tbl)))))
15032 ((equal e '(:grouptags)) nil)
15034 (setq tg (copy-sequence (car e)) c2 nil)
15037 ;; automatically assign a character.
15038 (setq c1 (string-to-char
15039 (downcase (substring
15040 tg (if (= (string-to-char tg) ?@) 1 0)))))
15041 (if (or (rassoc c1 ntable) (rassoc c1 table))
15042 (while (or (rassoc char ntable) (rassoc char table))
15043 (setq char (1+ char)))
15045 (setq c (or c2 char)))
15046 (if ingroup (push tg (car groups)))
15047 (setq tg (org-add-props tg nil 'face
15049 ((not (assoc tg table))
15050 (org-get-todo-face tg))
15051 ((member tg current) c-face)
15052 ((member tg inherited) i-face))))
15053 (if (equal (caar tbl) :grouptags)
15054 (org-add-props tg nil 'face 'org-tag-group))
15055 (if (and (= cnt 0) (not ingroup)) (insert " "))
15056 (insert "[" c "] " tg (make-string
15057 (- fwidth 4 (length tg)) ?\ ))
15058 (push (cons tg c) ntable)
15059 (when (= (setq cnt (1+ cnt)) ncol)
15061 (if ingroup (insert " "))
15063 (setq ntable (nreverse ntable))
15065 (goto-char (point-min))
15066 (if (not expert) (org-fit-window-to-buffer))
15070 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
15071 (if (not groups) "no " "")
15072 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
15073 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
15074 (setq org-last-tag-selection-key c)
15076 ((= c ?\r) (throw 'exit t))
15078 (setq groups (not groups))
15079 (goto-char (point-min))
15080 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
15083 (org-fast-tag-show-exit
15084 (setq exit-after-next (not exit-after-next)))
15086 (delete-other-windows)
15087 (set-window-buffer (split-window-vertically) " *Org tags*")
15088 (org-switch-to-buffer-other-window " *Org tags*")
15089 (org-fit-window-to-buffer)))
15091 (and (= c ?q) (not (rassoc c ntable))))
15092 (org-detach-overlay org-tags-overlay)
15093 (setq quit-flag t))
15096 (if exit-after-next (setq exit-after-next 'now)))
15098 (condition-case nil
15099 (setq tg (org-icompleting-read
15102 (with-current-buffer buf
15103 (org-get-buffer-tags)))))
15104 (quit (setq tg "")))
15105 (when (string-match "\\S-" tg)
15106 (add-to-list 'buffer-tags (list tg))
15107 (if (member tg current)
15108 (setq current (delete tg current))
15109 (push tg current)))
15110 (if exit-after-next (setq exit-after-next 'now)))
15111 ((setq e (rassoc c todo-table) tg (car e))
15112 (with-current-buffer buf
15113 (save-excursion (org-todo tg)))
15114 (if exit-after-next (setq exit-after-next 'now)))
15115 ((setq e (rassoc c ntable) tg (car e))
15116 (if (member tg current)
15117 (setq current (delete tg current))
15118 (loop for g in groups do
15121 (setq current (delete x current)))
15124 (if exit-after-next (setq exit-after-next 'now))))
15126 ;; Create a sorted list
15130 (assoc b (cdr (memq (assoc a ntable) ntable))))))
15131 (if (eq exit-after-next 'now) (throw 'exit t))
15132 (goto-char (point-min))
15133 (beginning-of-line 2)
15134 (delete-region (point) (point-at-eol))
15135 (org-fast-tag-insert "Current" current c-face)
15136 (org-set-current-tags-overlay current ov-prefix)
15137 (while (re-search-forward
15138 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
15139 (setq tg (match-string 1))
15140 (add-text-properties
15141 (match-beginning 1) (match-end 1)
15144 ((member tg current) c-face)
15145 ((member tg inherited) i-face)
15146 (t (get-text-property (match-beginning 1) 'face))))))
15147 (goto-char (point-min)))))
15148 (org-detach-overlay org-tags-overlay)
15150 (mapconcat 'identity current ":")
15153 (defun org-get-tags-string ()
15154 "Get the TAGS string in the current headline."
15155 (unless (org-at-heading-p t)
15156 (user-error "Not on a heading"))
15158 (beginning-of-line 1)
15159 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
15160 (org-match-string-no-properties 1)
15163 (defun org-get-tags ()
15164 "Get the list of tags specified in the current headline."
15165 (org-split-string (org-get-tags-string) ":"))
15167 (defun org-get-buffer-tags ()
15168 "Get a table of all tags used in the buffer, for completion."
15171 (goto-char (point-min))
15172 (while (re-search-forward
15173 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
15174 (when (equal (char-after (point-at-bol 0)) ?*)
15175 (mapc (lambda (x) (add-to-list 'tags x))
15176 (org-split-string (org-match-string-no-properties 1) ":")))))
15177 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
15178 (mapcar 'list tags)))
15180 ;;;; The mapping API
15182 (defun org-map-entries (func &optional match scope &rest skip)
15183 "Call FUNC at each headline selected by MATCH in SCOPE.
15185 FUNC is a function or a lisp form. The function will be called without
15186 arguments, with the cursor positioned at the beginning of the headline.
15187 The return values of all calls to the function will be collected and
15188 returned as a list.
15190 The call to FUNC will be wrapped into a save-excursion form, so FUNC
15191 does not need to preserve point. After evaluation, the cursor will be
15192 moved to the end of the line (presumably of the headline of the
15193 processed entry) and search continues from there. Under some
15194 circumstances, this may not produce the wanted results. For example,
15195 if you have removed (e.g. archived) the current (sub)tree it could
15196 mean that the next entry will be skipped entirely. In such cases, you
15197 can specify the position from where search should continue by making
15198 FUNC set the variable `org-map-continue-from' to the desired buffer
15201 MATCH is a tags/property/todo match as it is used in the agenda tags view.
15202 Only headlines that are matched by this query will be considered during
15203 the iteration. When MATCH is nil or t, all headlines will be
15204 visited by the iteration.
15206 SCOPE determines the scope of this command. It can be any of:
15208 nil The current buffer, respecting the restriction if any
15209 tree The subtree started with the entry at point
15210 region The entries within the active region, if any
15212 The entries within the active region, but only those at
15213 the same level than the first one.
15214 file The current buffer, without restriction
15216 The current buffer, and any archives associated with it
15217 agenda All agenda files
15218 agenda-with-archives
15219 All agenda files with any archive files associated with them
15221 If this is a list, all files in the list will be scanned
15223 The remaining args are treated as settings for the skipping facilities of
15224 the scanner. The following items can be given here:
15226 archive skip trees with the archive tag
15227 comment skip trees with the COMMENT keyword
15228 function or Emacs Lisp form:
15229 will be used as value for `org-agenda-skip-function', so
15230 whenever the function returns a position, FUNC will not be
15231 called for that entry and search will continue from the
15234 If your function needs to retrieve the tags including inherited tags
15235 at the *current* entry, you can use the value of the variable
15236 `org-scanner-tags' which will be much faster than getting the value
15237 with `org-get-tags-at'. If your function gets properties with
15238 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
15239 to t around the call to `org-entry-properties' to get the same speedup.
15240 Note that if your function moves around to retrieve tags and properties at
15241 a *different* entry, you cannot use these techniques."
15242 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
15243 (not (org-region-active-p)))
15244 (let* ((org-agenda-archives-mode nil) ; just to make sure
15245 (org-agenda-skip-archived-trees (memq 'archive skip))
15246 (org-agenda-skip-comment-trees (memq 'comment skip))
15247 (org-agenda-skip-function
15248 (car (org-delete-all '(comment archive) skip)))
15249 (org-tags-match-list-sublevels t)
15250 (start-level (eq scope 'region-start-level))
15252 org-todo-keywords-for-agenda
15253 org-done-keywords-for-agenda
15254 org-todo-keyword-alist-for-agenda
15255 org-tag-alist-for-agenda
15259 ((eq match t) (setq matcher t))
15260 ((eq match nil) (setq matcher t))
15261 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
15265 (cond ((eq scope 'tree)
15266 (org-back-to-heading t)
15267 (org-narrow-to-subtree)
15269 ((and (or (eq scope 'region) (eq scope 'region-start-level))
15270 (org-region-active-p))
15271 ;; If needed, set start-level to a string like "2"
15274 (goto-char (region-beginning))
15275 (unless (org-at-heading-p) (outline-next-heading))
15276 (setq start-level (org-current-level))))
15277 (narrow-to-region (region-beginning)
15279 (goto-char (region-end))
15280 (unless (and (bolp) (org-at-heading-p))
15281 (outline-next-heading))
15287 (org-agenda-prepare-buffers
15288 (list (buffer-file-name (current-buffer))))
15289 (setq res (org-scan-tags func matcher todo-only start-level)))
15290 ;; Get the right scope
15292 ((and scope (listp scope) (symbolp (car scope)))
15293 (setq scope (eval scope)))
15294 ((eq scope 'agenda)
15295 (setq scope (org-agenda-files t)))
15296 ((eq scope 'agenda-with-archives)
15297 (setq scope (org-agenda-files t))
15298 (setq scope (org-add-archive-files scope)))
15300 (setq scope (list (buffer-file-name))))
15301 ((eq scope 'file-with-archives)
15302 (setq scope (org-add-archive-files (list (buffer-file-name))))))
15303 (org-agenda-prepare-buffers scope)
15304 (while (setq file (pop scope))
15305 (with-current-buffer (org-find-base-buffer-visiting file)
15309 (goto-char (point-min))
15310 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
15315 ;;; Setting and retrieving properties
15317 (defconst org-special-properties
15318 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
15319 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
15320 "The special properties valid in Org-mode.
15322 These are properties that are not defined in the property drawer,
15323 but in some other way.")
15325 (defconst org-default-properties
15326 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
15327 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
15328 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
15329 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
15330 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
15331 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
15332 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
15333 "Some properties that are used by Org-mode for various purposes.
15334 Being in this list makes sure that they are offered for completion.")
15336 (defun org-property-action ()
15337 "Do an action on properties."
15340 (org-at-property-p)
15341 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
15342 (setq c (read-char-exclusive))
15345 (call-interactively 'org-set-property))
15347 (call-interactively 'org-delete-property))
15349 (call-interactively 'org-delete-property-globally))
15351 (call-interactively 'org-compute-property-at-point))
15352 (t (user-error "No such property action %c" c)))))
15354 (defun org-inc-effort ()
15355 "Increment the value of the effort property in the current entry."
15357 (org-set-effort nil t))
15359 (defvar org-clock-effort) ;; Defined in org-clock.el
15360 (defvar org-clock-current-task) ;; Defined in org-clock.el
15361 (defun org-set-effort (&optional value increment)
15362 "Set the effort property of the current entry.
15363 With numerical prefix arg, use the nth allowed value, 0 stands for the
15364 10th allowed value.
15366 When INCREMENT is non-nil, set the property to the next allowed value."
15368 (if (equal value 0) (setq value 10))
15369 (let* ((completion-ignore-case t)
15370 (prop org-effort-property)
15371 (cur (org-entry-get nil prop))
15372 (allowed (org-property-get-allowed-values nil prop 'table))
15373 (existing (mapcar 'list (org-property-values prop)))
15374 (heading (nth 4 (org-heading-components)))
15377 ((stringp value) value)
15378 ((and allowed (integerp value))
15379 (or (car (nth (1- value) allowed))
15380 (car (org-last allowed))))
15381 ((and allowed increment)
15382 (or (caadr (member (list cur) allowed))
15383 (user-error "Allowed effort values are not set")))
15385 (message "Select 1-9,0, [RET%s]: %s"
15386 (if cur (concat "=" cur) "")
15387 (mapconcat 'car allowed " "))
15388 (setq rpl (read-char-exclusive))
15389 (if (equal rpl ?\r)
15391 (setq rpl (- rpl ?0))
15392 (if (equal rpl 0) (setq rpl 10))
15393 (if (and (> rpl 0) (<= rpl (length allowed)))
15394 (car (nth (1- rpl) allowed))
15395 (org-completing-read "Effort: " allowed nil))))
15397 (let (org-completion-use-ido org-completion-use-iswitchb)
15398 (org-completing-read
15399 (concat "Effort " (if (and cur (string-match "\\S-" cur))
15400 (concat "[" cur "]") "")
15402 existing nil nil "" nil cur))))))
15403 (unless (equal (org-entry-get nil prop) val)
15404 (org-entry-put nil prop val))
15405 (org-refresh-property
15406 '((effort . identity)
15407 (effort-minutes . org-duration-string-to-minutes))
15409 (when (string= heading org-clock-current-task)
15410 (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
15411 (org-clock-update-mode-line))
15412 (message "%s is now %s" prop val)))
15414 (defun org-at-property-p ()
15415 "Non-nil when point is inside a property drawer.
15416 See `org-property-re' for match data, if applicable."
15417 (when (eq (org-element-type (org-element-at-point)) 'node-property)
15419 (beginning-of-line)
15420 (looking-at org-property-re))))
15422 (defun org-get-property-block (&optional beg end force)
15423 "Return the (beg . end) range of the body of the property drawer.
15424 BEG and END are the beginning and end of the current subtree, or of
15425 the part before the first headline. If they are not given, they will
15426 be found. If the drawer does not exist and FORCE is non-nil, create
15430 (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
15431 (progn (org-back-to-heading t) (point))))
15432 (end (or end (and (not (outline-next-heading)) (point-max))
15435 (if (re-search-forward org-property-start-re end t)
15436 (setq beg (1+ (match-end 0)))
15439 (org-insert-property-drawer)
15440 (setq end (progn (outline-next-heading) (point))))
15443 (if (re-search-forward org-property-start-re end t)
15444 (setq beg (1+ (match-end 0)))))
15445 (if (re-search-forward org-property-end-re end t)
15446 (setq end (match-beginning 0))
15447 (or force (throw 'exit nil))
15451 (insert ":END:\n"))
15454 (defun org-entry-properties (&optional pom which specific)
15455 "Get all properties of the entry at point-or-marker POM.
15456 This includes the TODO keyword, the tags, time strings for deadline,
15457 scheduled, and clocking, and any additional properties defined in the
15458 entry. The return value is an alist, keys may occur multiple times
15459 if the property key was used several times.
15460 POM may also be nil, in which case the current entry is used.
15461 If WHICH is nil or `all', get all properties. If WHICH is
15462 `special' or `standard', only get that subclass. If WHICH
15463 is a string only get exactly this property. SPECIFIC can be a string, the
15464 specific property we are interested in. Specifying it can speed
15465 things up because then unnecessary parsing is avoided."
15466 (setq which (or which 'all))
15467 (org-with-wide-buffer
15468 (org-with-point-at pom
15469 (let ((clockstr (substring org-clock-string 0 -1))
15470 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
15471 (case-fold-search nil)
15472 beg end range props sum-props key key1 value string clocksum clocksumt)
15473 (when (and (derived-mode-p 'org-mode)
15474 (ignore-errors (org-back-to-heading t)))
15476 (setq sum-props (get-text-property (point) 'org-summaries))
15477 (setq clocksum (get-text-property (point) :org-clock-minutes)
15478 clocksumt (get-text-property (point) :org-clock-minutes-today))
15479 (outline-next-heading)
15481 (when (memq which '(all special))
15482 ;; Get the special properties, like TODO and tags
15484 (when (and (or (not specific) (string= specific "TODO"))
15485 (looking-at org-todo-line-regexp) (match-end 2))
15486 (push (cons "TODO" (org-match-string-no-properties 2)) props))
15487 (when (and (or (not specific) (string= specific "PRIORITY"))
15488 (looking-at org-priority-regexp))
15489 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
15490 (when (or (not specific) (string= specific "FILE"))
15491 (push (cons "FILE" buffer-file-name) props))
15492 (when (and (or (not specific) (string= specific "TAGS"))
15493 (setq value (org-get-tags-string))
15494 (string-match "\\S-" value))
15495 (push (cons "TAGS" value) props))
15496 (when (and (or (not specific) (string= specific "ALLTAGS"))
15497 (setq value (org-get-tags-at)))
15498 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
15501 (when (or (not specific) (string= specific "BLOCKED"))
15502 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
15503 (when (or (not specific)
15505 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
15506 "TIMESTAMP" "TIMESTAMP_IA")))
15508 (while (and (re-search-forward org-maybe-keyword-time-regexp end t)
15509 (not (text-property-any 0 (length (match-string 0))
15510 'face 'font-lock-comment-face
15511 (match-string 0))))
15512 (setq key (if (match-end 1)
15513 (substring (org-match-string-no-properties 1)
15515 string (if (equal key clockstr)
15517 (buffer-substring-no-properties
15518 (match-beginning 3) (goto-char
15520 (substring (org-match-string-no-properties 3)
15522 ;; Get the correct property name from the key. This is
15523 ;; necessary if the user has configured time keywords.
15524 (setq key1 (concat key ":"))
15528 (if (= (char-after (match-beginning 3)) ?\[)
15529 "TIMESTAMP_IA" "TIMESTAMP")))
15530 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
15531 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
15532 ((equal key1 org-closed-string) (setq key "CLOSED"))
15533 ((equal key1 org-clock-string) (setq key "CLOCK")))
15534 (if (and specific (equal key specific) (not (equal key "CLOCK")))
15536 (push (cons key string) props)
15537 ;; no need to search further if match is found
15539 (when (or (equal key "CLOCK") (not (assoc key props)))
15540 (push (cons key string) props)))))))
15542 (when (memq which '(all standard))
15543 ;; Get the standard properties, like :PROP: ...
15544 (setq range (org-get-property-block beg end))
15546 (goto-char (car range))
15547 (while (re-search-forward org-property-re
15549 (setq key (org-match-string-no-properties 2)
15550 value (org-trim (or (org-match-string-no-properties 3) "")))
15551 (unless (member key excluded)
15552 (push (cons key (or value "")) props)))))
15554 (push (cons "CLOCKSUM"
15555 (org-columns-number-to-string (/ (float clocksum) 60.)
15559 (push (cons "CLOCKSUM_T"
15560 (org-columns-number-to-string (/ (float clocksumt) 60.)
15563 (unless (assoc "CATEGORY" props)
15564 (push (cons "CATEGORY" (org-get-category)) props))
15565 (append sum-props (nreverse props)))))))
15567 (defun org-entry-get (pom property &optional inherit literal-nil)
15568 "Get value of PROPERTY for entry or content at point-or-marker POM.
15569 If INHERIT is non-nil and the entry does not have the property,
15570 then also check higher levels of the hierarchy.
15571 If INHERIT is the symbol `selective', use inheritance only if the setting
15572 in `org-use-property-inheritance' selects PROPERTY for inheritance.
15573 If the property is present but empty, the return value is the empty string.
15574 If the property is not present at all, nil is returned.
15576 Return the value as a string.
15578 If LITERAL-NIL is set, return the string value \"nil\" as a string,
15579 do not interpret it as the list atom nil. This is used for inheritance
15580 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
15581 (org-with-point-at pom
15582 (if (and inherit (if (eq inherit 'selective)
15583 (org-property-inherit-p property)
15585 (org-entry-get-with-inheritance property literal-nil)
15586 (if (member property org-special-properties)
15587 ;; We need a special property. Use `org-entry-properties'
15588 ;; to retrieve it, but specify the wanted property
15589 (cdr (assoc property (org-entry-properties nil 'special property)))
15590 (org-with-wide-buffer
15591 (let ((range (org-get-property-block)))
15592 (when (and range (not (eq (car range) (cdr range)))
15594 (goto-char (car range))
15596 (concat (org-re-property property) "\\|"
15597 (org-re-property (concat property "+")))
15600 (list (or (assoc property org-file-properties)
15601 (assoc property org-global-properties)
15602 (assoc property org-global-properties-fixed))))
15604 (when (re-search-forward
15605 (org-re-property key) (cdr range) t)
15607 (org-update-property-plist
15610 (org-match-string-no-properties 3) "")
15613 (goto-char (car range))
15614 (funcall ap property)
15615 (goto-char (car range))
15616 (while (funcall ap (concat property "+")))
15617 (setq val (cdr (assoc property props)))
15618 (when val (if literal-nil val (org-not-nil val)))))))))))
15620 (defun org-property-or-variable-value (var &optional inherit)
15621 "Check if there is a property fixing the value of VAR.
15622 If yes, return this value. If not, return the current value of the variable."
15623 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
15624 (if (and prop (stringp prop) (string-match "\\S-" prop))
15626 (symbol-value var))))
15628 (defun org-entry-delete (pom property)
15629 "Delete the property PROPERTY from entry at point-or-marker POM."
15630 (unless (member property org-special-properties)
15631 (org-with-point-at pom
15632 (let ((range (org-get-property-block)))
15634 (goto-char (car range))
15636 (org-re-property property nil t)
15639 (delete-region (match-beginning 0) (1+ (point-at-eol)))
15640 (org-remove-empty-drawer-at (car range))
15644 ;; Multi-values properties are properties that contain multiple values
15645 ;; These values are assumed to be single words, separated by whitespace.
15646 (defun org-entry-add-to-multivalued-property (pom property value)
15647 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
15648 (let* ((old (org-entry-get pom property))
15649 (values (and old (org-split-string old "[ \t]"))))
15650 (setq value (org-entry-protect-space value))
15651 (unless (member value values)
15652 (setq values (append values (list value)))
15653 (org-entry-put pom property
15654 (mapconcat 'identity values " ")))))
15656 (defun org-entry-remove-from-multivalued-property (pom property value)
15657 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
15658 (let* ((old (org-entry-get pom property))
15659 (values (and old (org-split-string old "[ \t]"))))
15660 (setq value (org-entry-protect-space value))
15661 (when (member value values)
15662 (setq values (delete value values))
15663 (org-entry-put pom property
15664 (mapconcat 'identity values " ")))))
15666 (defun org-entry-member-in-multivalued-property (pom property value)
15667 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
15668 (let* ((old (org-entry-get pom property))
15669 (values (and old (org-split-string old "[ \t]"))))
15670 (setq value (org-entry-protect-space value))
15671 (member value values)))
15673 (defun org-entry-get-multivalued-property (pom property)
15674 "Return a list of values in a multivalued property."
15675 (let* ((value (org-entry-get pom property))
15676 (values (and value (org-split-string value "[ \t]"))))
15677 (mapcar 'org-entry-restore-space values)))
15679 (defun org-entry-put-multivalued-property (pom property &rest values)
15680 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
15681 VALUES should be a list of strings. Spaces will be protected."
15682 (org-entry-put pom property
15683 (mapconcat 'org-entry-protect-space values " "))
15684 (let* ((value (org-entry-get pom property))
15685 (values (and value (org-split-string value "[ \t]"))))
15686 (mapcar 'org-entry-restore-space values)))
15688 (defun org-entry-protect-space (s)
15689 "Protect spaces and newline in string S."
15690 (while (string-match " " s)
15691 (setq s (replace-match "%20" t t s)))
15692 (while (string-match "\n" s)
15693 (setq s (replace-match "%0A" t t s)))
15696 (defun org-entry-restore-space (s)
15697 "Restore spaces and newline in string S."
15698 (while (string-match "%20" s)
15699 (setq s (replace-match " " t t s)))
15700 (while (string-match "%0A" s)
15701 (setq s (replace-match "\n" t t s)))
15704 (defvar org-entry-property-inherited-from (make-marker)
15705 "Marker pointing to the entry from where a property was inherited.
15706 Each call to `org-entry-get-with-inheritance' will set this marker to the
15707 location of the entry where the inheritance search matched. If there was
15708 no match, the marker will point nowhere.
15709 Note that also `org-entry-get' calls this function, if the INHERIT flag
15712 (defun org-entry-get-with-inheritance (property &optional literal-nil)
15713 "Get PROPERTY of entry or content at point, search higher levels if needed.
15714 The search will stop at the first ancestor which has the property defined.
15715 If the value found is \"nil\", return nil to show that the property
15716 should be considered as undefined (this is the meaning of nil here).
15717 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
15718 (move-marker org-entry-property-inherited-from nil)
15725 (when (setq tmp (org-entry-get nil property nil literal-nil))
15726 (or (ignore-errors (org-back-to-heading t))
15727 (goto-char (point-min)))
15728 (move-marker org-entry-property-inherited-from (point))
15730 (or (ignore-errors (org-up-heading-safe))
15731 (throw 'ex nil))))))
15733 (cdr (assoc property org-file-properties))
15734 (cdr (assoc property org-global-properties))
15735 (cdr (assoc property org-global-properties-fixed))))
15736 (if literal-nil tmp (org-not-nil tmp))))
15738 (defvar org-property-changed-functions nil
15739 "Hook called when the value of a property has changed.
15740 Each hook function should accept two arguments, the name of the property
15741 and the new value.")
15743 (defun org-entry-put (pom property value)
15744 "Set PROPERTY to VALUE for entry at point-or-marker POM.
15745 If the value is `nil', it is converted to the empty string.
15746 If it is not a string, an error is raised."
15747 (cond ((null value) (setq value ""))
15748 ((not (stringp value))
15749 (error "Properties values should be strings.")))
15750 (org-with-point-at pom
15751 (org-back-to-heading t)
15752 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
15755 ((equal property "TODO")
15756 (when (and (string-match "\\S-" value)
15757 (not (member value org-todo-keywords-1)))
15758 (user-error "\"%s\" is not a valid TODO state" value))
15759 (if (or (not value)
15760 (not (string-match "\\S-" value)))
15761 (setq value 'none))
15763 (org-set-tags nil 'align))
15764 ((equal property "PRIORITY")
15765 (org-priority (if (and value (string-match "\\S-" value))
15766 (string-to-char value) ?\ ))
15767 (org-set-tags nil 'align))
15768 ((equal property "CLOCKSUM")
15769 (if (not (re-search-forward
15770 (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
15771 (error "Cannot find a clock log")
15772 (goto-char (- (match-end 1) 2))
15774 ((eq value 'earlier) (org-timestamp-down))
15775 ((eq value 'later) (org-timestamp-up)))
15776 (org-clock-sum-current-item)))
15777 ((equal property "SCHEDULED")
15778 (if (re-search-forward org-scheduled-time-regexp end t)
15780 ((eq value 'earlier) (org-timestamp-change -1 'day))
15781 ((eq value 'later) (org-timestamp-change 1 'day))
15782 (t (call-interactively 'org-schedule)))
15783 (call-interactively 'org-schedule)))
15784 ((equal property "DEADLINE")
15785 (if (re-search-forward org-deadline-time-regexp end t)
15787 ((eq value 'earlier) (org-timestamp-change -1 'day))
15788 ((eq value 'later) (org-timestamp-change 1 'day))
15789 (t (call-interactively 'org-deadline)))
15790 (call-interactively 'org-deadline)))
15791 ((member property org-special-properties)
15792 (error "The %s property can not yet be set with `org-entry-put'"
15794 (t ; a non-special property
15795 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
15796 (setq range (org-get-property-block beg end 'force))
15797 (goto-char (car range))
15798 (if (re-search-forward
15799 (org-re-property property nil t) (cdr range) t)
15801 (delete-region (match-beginning 0) (match-end 0))
15802 (goto-char (match-beginning 0)))
15803 (goto-char (cdr range))
15807 (insert ":" property ":")
15808 (and value (insert " " value))
15809 (org-indent-line)))))
15810 (run-hook-with-args 'org-property-changed-functions property value)))
15812 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
15813 "Get all property keys in the current buffer.
15814 With INCLUDE-SPECIALS, also list the special properties that reflect things
15815 like tags and TODO state.
15816 With INCLUDE-DEFAULTS, also include properties that has special meaning
15817 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
15819 With INCLUDE-COLUMNS, also include property names given in COLUMN
15820 formats in the current buffer."
15821 (let (rtn range cfmt s p)
15825 (goto-char (point-min))
15826 (while (re-search-forward org-property-start-re nil t)
15828 (setq range (or (org-get-property-block)
15830 (format "Malformed drawer at %d, repair?" (point)))
15831 (org-get-property-block nil nil t)
15832 (throw 'cont nil))))
15833 (goto-char (car range))
15834 (while (re-search-forward org-property-re
15836 (add-to-list 'rtn (org-match-string-no-properties 2)))
15837 (outline-next-heading)))))
15839 (when include-specials
15840 (setq rtn (append org-special-properties rtn)))
15842 (when include-defaults
15843 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
15844 (add-to-list 'rtn org-effort-property))
15846 (when include-columns
15850 (goto-char (point-min))
15851 (while (re-search-forward
15852 "^[ \t]*\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
15854 (setq cfmt (match-string 2) s 0)
15855 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
15857 (setq s (match-end 0)
15858 p (match-string 1 cfmt))
15859 (unless (or (equal p "ITEM")
15860 (member p org-special-properties))
15861 (add-to-list 'rtn (match-string 1 cfmt))))))))
15863 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
15865 (defun org-property-values (key)
15866 "Return a list of all values of property KEY in the current buffer."
15870 (goto-char (point-min))
15871 (let ((re (org-re-property key))
15873 (while (re-search-forward re nil t)
15874 (add-to-list 'values (org-trim (match-string 3))))
15875 (delete "" values)))))
15877 (defun org-insert-property-drawer ()
15878 "Insert a property drawer into the current entry."
15879 (org-back-to-heading t)
15880 (looking-at org-outline-regexp)
15881 (let ((indent (if org-adapt-indentation
15882 (- (match-end 0) (match-beginning 0))
15885 (re (concat "^[ \t]*" org-keyword-time-regexp))
15887 (outline-next-heading)
15890 (while (re-search-forward re end t))
15891 (setq hiddenp (outline-invisible-p))
15893 (and (equal (char-after) ?\n) (forward-char 1))
15894 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
15895 (if (member (match-string 1) '("CLOCK:" ":END:"))
15896 ;; just skip this line
15897 (beginning-of-line 2)
15898 ;; Drawer start, find the end
15899 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
15900 (beginning-of-line 1)))
15901 (org-skip-over-state-notes)
15902 (skip-chars-backward " \t\n\r")
15903 (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
15905 (goto-char (point-at-eol))
15906 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
15907 (beginning-of-line 0)
15908 (org-indent-to-column indent)
15909 (beginning-of-line 2)
15910 (org-indent-to-column indent)
15911 (beginning-of-line 0)
15914 (org-back-to-heading t)
15916 (org-flag-drawer t))))
15918 (defun org-insert-drawer (&optional arg drawer)
15919 "Insert a drawer at point.
15921 Optional argument DRAWER, when non-nil, is a string representing
15922 drawer's name. Otherwise, the user is prompted for a name.
15924 If a region is active, insert the drawer around that region
15927 Point is left between drawer's boundaries."
15929 (let* ((drawer (if arg "PROPERTIES"
15930 (or drawer (read-from-minibuffer "Drawer: ")))))
15932 ;; With C-u, fall back on `org-insert-property-drawer'
15933 (arg (org-insert-property-drawer))
15935 ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
15936 (user-error "Invalid drawer name"))
15937 ;; With an active region, insert a drawer at point.
15938 ((not (org-region-active-p))
15940 (unless (bolp) (insert "\n"))
15941 (insert (format ":%s:\n\n:END:\n" drawer))
15942 (forward-line -2)))
15943 ;; Otherwise, insert the drawer at point
15945 (let ((rbeg (region-beginning))
15946 (rend (copy-marker (region-end))))
15950 (beginning-of-line)
15951 (when (save-excursion
15952 (re-search-forward org-outline-regexp-bol rend t))
15953 (user-error "Drawers cannot contain headlines"))
15954 ;; Position point at the beginning of the first
15955 ;; non-blank line in region. Insert drawer's opening
15956 ;; there, then indent it.
15957 (org-skip-whitespace)
15958 (beginning-of-line)
15959 (insert ":" drawer ":\n")
15961 (indent-for-tab-command)
15962 ;; Move point to the beginning of the first blank line
15963 ;; after the last non-blank line in region. Insert
15964 ;; drawer's closing, then indent it.
15966 (skip-chars-backward " \r\t\n")
15968 (deactivate-mark t)
15969 (indent-for-tab-command)
15970 (unless (eolp) (insert "\n")))
15971 ;; Clear marker, whatever the outcome of insertion is.
15972 (set-marker rend nil)))))))
15974 (defvar org-property-set-functions-alist nil
15975 "Property set function alist.
15976 Each entry should have the following format:
15978 (PROPERTY . READ-FUNCTION)
15980 The read function will be called with the same argument as
15981 `org-completing-read'.")
15983 (defun org-set-property-function (property)
15984 "Get the function that should be used to set PROPERTY.
15985 This is computed according to `org-property-set-functions-alist'."
15986 (or (cdr (assoc property org-property-set-functions-alist))
15987 'org-completing-read))
15989 (defun org-read-property-value (property)
15990 "Read PROPERTY value from user."
15991 (let* ((completion-ignore-case t)
15992 (allowed (org-property-get-allowed-values nil property 'table))
15993 (cur (org-entry-get nil property))
15994 (prompt (concat property " value"
15995 (if (and cur (string-match "\\S-" cur))
15996 (concat " [" cur "]") "") ": "))
15997 (set-function (org-set-property-function property))
15999 (funcall set-function prompt allowed nil
16000 (not (get-text-property 0 'org-unrestricted
16002 (let (org-completion-use-ido org-completion-use-iswitchb)
16003 (funcall set-function prompt
16004 (mapcar 'list (org-property-values property))
16005 nil nil "" nil cur)))))
16008 (defvar org-last-set-property nil)
16009 (defvar org-last-set-property-value nil)
16010 (defun org-read-property-name ()
16011 "Read a property name."
16012 (let* ((completion-ignore-case t)
16013 (keys (org-buffer-property-keys nil t t))
16014 (default-prop (or (save-excursion
16016 (beginning-of-line)
16017 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
16018 (null (string= (match-string 1) "END"))
16019 (match-string 1))))
16020 org-last-set-property))
16021 (property (org-icompleting-read
16023 (if default-prop (concat " [" default-prop "]") "")
16025 (mapcar 'list keys)
16028 (if (member property keys)
16030 (or (cdr (assoc (downcase property)
16031 (mapcar (lambda (x) (cons (downcase x) x))
16035 (defun org-set-property-and-value (use-last)
16036 "Allow to set [PROPERTY]: [value] direction from prompt.
16037 When use-default, don't even ask, just use the last
16038 \"[PROPERTY]: [value]\" string from the history."
16040 (let* ((completion-ignore-case t)
16041 (pv (or (and use-last org-last-set-property-value)
16042 (org-completing-read
16043 "Enter a \"[Property]: [value]\" pair: "
16044 nil nil nil nil nil
16045 org-last-set-property-value)))
16047 (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
16048 (setq prop (match-string 1 pv)
16049 val (match-string 2 pv))
16050 (org-set-property prop val))))
16052 (defun org-set-property (property value)
16053 "In the current entry, set PROPERTY to VALUE.
16054 When called interactively, this will prompt for a property name, offering
16055 completion on existing and default properties. And then it will prompt
16056 for a value, offering completion either on allowed values (via an inherited
16057 xxx_ALL property) or on existing values in other instances of this property
16058 in the current file."
16059 (interactive (list nil nil))
16060 (let* ((property (or property (org-read-property-name)))
16061 (value (or value (org-read-property-value property)))
16062 (fn (cdr (assoc property org-properties-postprocess-alist))))
16063 (setq org-last-set-property property)
16064 (setq org-last-set-property-value (concat property ": " value))
16065 ;; Possibly postprocess the inserted value:
16066 (when fn (setq value (funcall fn value)))
16067 (unless (equal (org-entry-get nil property) value)
16068 (org-entry-put nil property value))))
16070 (defun org-delete-property (property)
16071 "In the current entry, delete PROPERTY."
16073 (let* ((completion-ignore-case t)
16074 (cat (org-entry-get (point) "CATEGORY"))
16075 (props0 (org-entry-properties nil 'standard))
16076 (props (if cat props0
16077 (delete `("CATEGORY" . ,(org-get-category)) props0)))
16078 (prop (if (< 1 (length props))
16079 (org-icompleting-read "Property: " props nil t)
16083 (message "No property to delete in this entry")
16084 (org-entry-delete nil property)
16085 (message "Property \"%s\" deleted" property)))
16087 (defun org-delete-property-globally (property)
16088 "Remove PROPERTY globally, from all entries."
16090 (let* ((completion-ignore-case t)
16091 (prop (org-icompleting-read
16092 "Globally remove property: "
16093 (mapcar 'list (org-buffer-property-keys)))))
16098 (goto-char (point-min))
16100 (while (re-search-forward
16101 (org-re-property property)
16103 (setq cnt (1+ cnt))
16104 (delete-region (match-beginning 0) (1+ (point-at-eol))))
16105 (message "Property \"%s\" removed from %d entries" property cnt)))))
16107 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
16109 (defun org-compute-property-at-point ()
16110 "Compute the property at point.
16111 This looks for an enclosing column format, extracts the operator and
16112 then applies it to the property in the column format's scope."
16114 (unless (org-at-property-p)
16115 (user-error "Not at a property"))
16116 (let ((prop (org-match-string-no-properties 2)))
16117 (org-columns-get-format-and-top-level)
16118 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
16119 (user-error "No operator defined for property %s" prop))
16120 (org-columns-compute prop)))
16122 (defvar org-property-allowed-value-functions nil
16123 "Hook for functions supplying allowed values for a specific property.
16124 The functions must take a single argument, the name of the property, and
16125 return a flat list of allowed values. If \":ETC\" is one of
16126 the values, this means that these values are intended as defaults for
16127 completion, but that other values should be allowed too.
16128 The functions must return nil if they are not responsible for this
16131 (defun org-property-get-allowed-values (pom property &optional table)
16132 "Get allowed values for the property PROPERTY.
16133 When TABLE is non-nil, return an alist that can directly be used for
16137 ((equal property "TODO")
16138 (setq vals (org-with-point-at pom
16139 (append org-todo-keywords-1 '("")))))
16140 ((equal property "PRIORITY")
16141 (let ((n org-lowest-priority))
16142 (while (>= n org-highest-priority)
16143 (push (char-to-string n) vals)
16145 ((member property org-special-properties))
16146 ((setq vals (run-hook-with-args-until-success
16147 'org-property-allowed-value-functions property)))
16149 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
16150 (when (and vals (string-match "\\S-" vals))
16151 (setq vals (car (read-from-string (concat "(" vals ")"))))
16152 (setq vals (mapcar (lambda (x)
16153 (cond ((stringp x) x)
16154 ((numberp x) (number-to-string x))
16155 ((symbolp x) (symbol-name x))
16158 (when (member ":ETC" vals)
16159 (setq vals (remove ":ETC" vals))
16160 (org-add-props (car vals) '(org-unrestricted t)))
16161 (if table (mapcar 'list vals) vals)))
16163 (defun org-property-previous-allowed-value (&optional previous)
16164 "Switch to the next allowed value for this property."
16166 (org-property-next-allowed-value t))
16168 (defun org-property-next-allowed-value (&optional previous)
16169 "Switch to the next allowed value for this property."
16171 (unless (org-at-property-p)
16172 (user-error "Not at a property"))
16173 (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
16174 (key (match-string 2))
16175 (value (match-string 3))
16176 (allowed (or (org-property-get-allowed-values (point) key)
16177 (and (member value '("[ ]" "[-]" "[X]"))
16179 (heading (save-match-data (nth 4 (org-heading-components))))
16182 (user-error "Allowed values for this property have not been defined"))
16183 (if previous (setq allowed (reverse allowed)))
16184 (if (member value allowed)
16185 (setq nval (car (cdr (member value allowed)))))
16186 (setq nval (or nval (car allowed)))
16187 (if (equal nval value)
16188 (user-error "Only one allowed value for this property"))
16189 (org-at-property-p)
16190 (replace-match (concat " :" key ": " nval) t t)
16192 (beginning-of-line 1)
16193 (skip-chars-forward " \t")
16194 (when (equal prop org-effort-property)
16195 (org-refresh-property
16196 '((effort . identity)
16197 (effort-minutes . org-duration-string-to-minutes))
16199 (when (string= org-clock-current-task heading)
16200 (setq org-clock-effort nval)
16201 (org-clock-update-mode-line)))
16202 (run-hook-with-args 'org-property-changed-functions key nval)))
16204 (defun org-find-olp (path &optional this-buffer)
16205 "Return a marker pointing to the entry at outline path OLP.
16206 If anything goes wrong, throw an error.
16207 You can wrap this call to catch the error like this:
16209 (condition-case msg
16210 (org-mobile-locate-entry (match-string 4))
16211 (error (nth 1 msg)))
16213 The return value will then be either a string with the error message,
16214 or a marker if everything is OK.
16216 If THIS-BUFFER is set, the outline path does not contain a file,
16218 (let* ((file (if this-buffer buffer-file-name (pop path)))
16219 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
16223 limit re end found pos heading cnt flevel)
16224 (unless buffer (error "File not found :%s" file))
16225 (with-current-buffer buffer
16229 (setq limit (point-max))
16230 (goto-char (point-min))
16231 (while (setq heading (pop path))
16232 (setq re (format org-complex-heading-regexp-format
16233 (regexp-quote heading)))
16234 (setq cnt 0 pos (point))
16235 (while (re-search-forward re end t)
16236 (setq level (- (match-end 1) (match-beginning 1)))
16237 (if (and (>= level lmin) (<= level lmax))
16238 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
16239 (when (= cnt 0) (error "Heading not found on level %d: %s"
16241 (when (> cnt 1) (error "Heading not unique on level %d: %s"
16244 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
16245 (setq end (save-excursion (org-end-of-subtree t t))))
16246 (when (org-at-heading-p)
16247 (point-marker)))))))
16249 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
16250 "Find node HEADING in BUFFER.
16251 Return a marker to the heading if it was found, or nil if not.
16252 If POS-ONLY is set, return just the position instead of a marker.
16254 The heading text must match exact, but it may have a TODO keyword,
16255 a priority cookie and tags in the standard locations."
16256 (with-current-buffer (or buffer (current-buffer))
16260 (goto-char (point-min))
16261 (let (case-fold-search)
16262 (if (re-search-forward
16263 (format org-complex-heading-regexp-format
16264 (regexp-quote heading)) nil t)
16266 (match-beginning 0)
16267 (move-marker (make-marker) (match-beginning 0)))))))))
16269 (defun org-find-exact-heading-in-directory (heading &optional dir)
16270 "Find Org node headline HEADING in all .org files in directory DIR.
16271 When the target headline is found, return a marker to this location."
16272 (let ((files (directory-files (or dir default-directory)
16273 t "\\`[^.#].*\\.org\\'"))
16274 file visiting m buffer)
16276 (while (setq file (pop files))
16277 (message "trying %s" file)
16278 (setq visiting (org-find-base-buffer-visiting file))
16279 (setq buffer (or visiting (find-file-noselect file)))
16280 (setq m (org-find-exact-headline-in-buffer
16282 (when (and (not m) (not visiting)) (kill-buffer buffer))
16283 (and m (throw 'found m))))))
16285 (defun org-find-entry-with-id (ident)
16286 "Locate the entry that contains the ID property with exact value IDENT.
16287 IDENT can be a string, a symbol or a number, this function will search for
16288 the string representation of it.
16289 Return the position where this entry starts, or nil if there is no such entry."
16290 (interactive "sID: ")
16292 ((stringp ident) ident)
16293 ((symbol-name ident) (symbol-name ident))
16294 ((numberp ident) (number-to-string ident))
16295 (t (error "IDENT %s must be a string, symbol or number" ident))))
16296 (case-fold-search nil))
16300 (goto-char (point-min))
16301 (when (re-search-forward
16302 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
16304 (org-back-to-heading t)
16309 (defvar org-last-changed-timestamp nil)
16310 (defvar org-last-inserted-timestamp nil
16311 "The last time stamp inserted with `org-insert-time-stamp'.")
16312 (defvar org-ts-what) ; dynamically scoped parameter
16314 (defun org-time-stamp (arg &optional inactive)
16315 "Prompt for a date/time and insert a time stamp.
16316 If the user specifies a time like HH:MM or if this command is
16317 called with at least one prefix argument, the time stamp contains
16318 the date and the time. Otherwise, only the date is be included.
16320 All parts of a date not specified by the user is filled in from
16321 the current date/time. So if you just press return without
16322 typing anything, the time stamp will represent the current
16325 If there is already a timestamp at the cursor, it will be
16328 With two universal prefix arguments, insert an active timestamp
16329 with the current time without prompting the user.
16331 When called from lisp, the timestamp is inactive if INACTIVE is
16336 ;; Default time is either today, or, when entering a range,
16337 ;; the range start.
16338 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
16340 (re-search-backward
16341 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
16342 (- (point) 20) t)))
16343 (apply 'encode-time (org-parse-time-string (match-string 1)))
16345 (default-input (and ts (org-get-compact-tod ts)))
16346 (repeater (save-excursion
16348 (beginning-of-line)
16349 (when (re-search-forward
16350 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
16351 (save-excursion (progn (end-of-line) (point))) t)
16352 (match-string 0)))))
16353 org-time-was-given org-end-time-was-given time)
16355 ((and (org-at-timestamp-p t)
16356 (memq last-command '(org-time-stamp org-time-stamp-inactive))
16357 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
16359 (setq time (let ((this-command this-command))
16360 (org-read-date arg 'totime nil nil
16361 default-time default-input inactive)))
16362 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
16363 ((org-at-timestamp-p t)
16364 (setq time (let ((this-command this-command))
16365 (org-read-date arg 'totime nil nil default-time default-input inactive)))
16366 (when (org-at-timestamp-p t) ; just to get the match data
16367 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
16369 (setq org-last-changed-timestamp
16370 (org-insert-time-stamp
16371 time (or org-time-was-given arg)
16372 inactive nil nil (list org-end-time-was-given)))
16373 (when repeater (goto-char (1- (point))) (insert " " repeater)
16374 (setq org-last-changed-timestamp
16375 (concat (substring org-last-inserted-timestamp 0 -1)
16376 " " repeater ">"))))
16377 (message "Timestamp updated"))
16379 (org-insert-time-stamp (current-time) t inactive))
16381 (setq time (let ((this-command this-command))
16382 (org-read-date arg 'totime nil nil default-time default-input inactive)))
16383 (org-insert-time-stamp time (or org-time-was-given arg) inactive
16384 nil nil (list org-end-time-was-given))))))
16386 ;; FIXME: can we use this for something else, like computing time differences?
16387 (defun org-get-compact-tod (s)
16388 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
16389 (let* ((t1 (match-string 1 s))
16390 (h1 (string-to-number (match-string 2 s)))
16391 (m1 (string-to-number (match-string 3 s)))
16392 (t2 (and (match-end 4) (match-string 5 s)))
16393 (h2 (and t2 (string-to-number (match-string 6 s))))
16394 (m2 (and t2 (string-to-number (match-string 7 s))))
16398 (setq dh (- h2 h1) dm (- m2 m1))
16399 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
16400 (concat t1 "+" (number-to-string dh)
16401 (and (/= 0 dm) (format ":%02d" dm)))))))
16403 (defun org-time-stamp-inactive (&optional arg)
16404 "Insert an inactive time stamp.
16405 An inactive time stamp is enclosed in square brackets instead of angle
16406 brackets. It is inactive in the sense that it does not trigger agenda entries,
16407 does not link to the calendar and cannot be changed with the S-cursor keys.
16408 So these are more for recording a certain time/date."
16410 (org-time-stamp arg 'inactive))
16412 (defvar org-date-ovl (make-overlay 1 1))
16413 (overlay-put org-date-ovl 'face 'org-date-selected)
16414 (org-detach-overlay org-date-ovl)
16416 (defvar org-ans1) ; dynamically scoped parameter
16417 (defvar org-ans2) ; dynamically scoped parameter
16419 (defvar org-plain-time-of-day-regexp) ; defined below
16421 (defvar org-overriding-default-time nil) ; dynamically scoped
16422 (defvar org-read-date-overlay nil)
16423 (defvar org-dcst nil) ; dynamically scoped
16424 (defvar org-read-date-history nil)
16425 (defvar org-read-date-final-answer nil)
16426 (defvar org-read-date-analyze-futurep nil)
16427 (defvar org-read-date-analyze-forced-year nil)
16428 (defvar org-read-date-inactive)
16430 (defvar org-read-date-minibuffer-local-map
16431 (let* ((org-replace-disputed-keys nil)
16432 (map (make-sparse-keymap)))
16433 (set-keymap-parent map minibuffer-local-map)
16434 (org-defkey map (kbd ".")
16435 (lambda () (interactive)
16436 ;; Are we at the beginning of the prompt?
16437 (if (looking-back "^[^:]+: ")
16438 (org-eval-in-calendar '(calendar-goto-today))
16440 (org-defkey map (kbd "C-.")
16441 (lambda () (interactive)
16442 (org-eval-in-calendar '(calendar-goto-today))))
16443 (org-defkey map [(meta shift left)]
16444 (lambda () (interactive)
16445 (org-eval-in-calendar '(calendar-backward-month 1))))
16446 (org-defkey map [(meta shift right)]
16447 (lambda () (interactive)
16448 (org-eval-in-calendar '(calendar-forward-month 1))))
16449 (org-defkey map [(meta shift up)]
16450 (lambda () (interactive)
16451 (org-eval-in-calendar '(calendar-backward-year 1))))
16452 (org-defkey map [(meta shift down)]
16453 (lambda () (interactive)
16454 (org-eval-in-calendar '(calendar-forward-year 1))))
16455 (org-defkey map [?\e (shift left)]
16456 (lambda () (interactive)
16457 (org-eval-in-calendar '(calendar-backward-month 1))))
16458 (org-defkey map [?\e (shift right)]
16459 (lambda () (interactive)
16460 (org-eval-in-calendar '(calendar-forward-month 1))))
16461 (org-defkey map [?\e (shift up)]
16462 (lambda () (interactive)
16463 (org-eval-in-calendar '(calendar-backward-year 1))))
16464 (org-defkey map [?\e (shift down)]
16465 (lambda () (interactive)
16466 (org-eval-in-calendar '(calendar-forward-year 1))))
16467 (org-defkey map [(shift up)]
16468 (lambda () (interactive)
16469 (org-eval-in-calendar '(calendar-backward-week 1))))
16470 (org-defkey map [(shift down)]
16471 (lambda () (interactive)
16472 (org-eval-in-calendar '(calendar-forward-week 1))))
16473 (org-defkey map [(shift left)]
16474 (lambda () (interactive)
16475 (org-eval-in-calendar '(calendar-backward-day 1))))
16476 (org-defkey map [(shift right)]
16477 (lambda () (interactive)
16478 (org-eval-in-calendar '(calendar-forward-day 1))))
16479 (org-defkey map "!"
16480 (lambda () (interactive)
16481 (org-eval-in-calendar '(diary-view-entries))
16483 (org-defkey map ">"
16484 (lambda () (interactive)
16485 (org-eval-in-calendar '(scroll-calendar-left 1))))
16486 (org-defkey map "<"
16487 (lambda () (interactive)
16488 (org-eval-in-calendar '(scroll-calendar-right 1))))
16489 (org-defkey map "\C-v"
16490 (lambda () (interactive)
16491 (org-eval-in-calendar
16492 '(calendar-scroll-left-three-months 1))))
16493 (org-defkey map "\M-v"
16494 (lambda () (interactive)
16495 (org-eval-in-calendar
16496 '(calendar-scroll-right-three-months 1))))
16498 "Keymap for minibuffer commands when using `org-read-date'.")
16501 (defvar org-defdecode)
16502 (defvar org-with-time)
16504 (defun org-read-date (&optional org-with-time to-time from-string prompt
16505 default-time default-input inactive)
16506 "Read a date, possibly a time, and make things smooth for the user.
16507 The prompt will suggest to enter an ISO date, but you can also enter anything
16508 which will at least partially be understood by `parse-time-string'.
16509 Unrecognized parts of the date will default to the current day, month, year,
16510 hour and minute. If this command is called to replace a timestamp at point,
16511 or to enter the second timestamp of a range, the default time is taken
16512 from the existing stamp. Furthermore, the command prefers the future,
16513 so if you are giving a date where the year is not given, and the day-month
16514 combination is already past in the current year, it will assume you
16515 mean next year. For details, see the manual. A few examples:
16517 3-2-5 --> 2003-02-05
16518 feb 15 --> currentyear-02-15
16519 2/15 --> currentyear-02-15
16520 sep 12 9 --> 2009-09-12
16521 12:45 --> today 12:45
16522 22 sept 0:34 --> currentyear-09-22 0:34
16523 12 --> currentyear-currentmonth-12
16524 Fri --> nearest Friday after today
16525 -Tue --> last Tuesday
16528 Furthermore you can specify a relative date by giving, as the *first* thing
16529 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
16530 change in days weeks, months, years.
16531 With a single plus or minus, the date is relative to today. With a double
16532 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
16533 +4d --> four days from today
16534 +4 --> same as above
16535 +2w --> two weeks from today
16536 ++5 --> five days from default date
16538 The function understands only English month and weekday abbreviations.
16540 While prompting, a calendar is popped up - you can also select the
16541 date with the mouse (button 1). The calendar shows a period of three
16542 months. To scroll it to other months, use the keys `>' and `<'.
16543 If you don't like the calendar, turn it off with
16544 \(setq org-read-date-popup-calendar nil)
16546 With optional argument TO-TIME, the date will immediately be converted
16547 to an internal time.
16548 With an optional argument ORG-WITH-TIME, the prompt will suggest to
16549 also insert a time. Note that when ORG-WITH-TIME is not set, you can
16550 still enter a time, and this function will inform the calling routine
16551 about this change. The calling routine may then choose to change the
16552 format used to insert the time stamp into the buffer to include the time.
16553 With optional argument FROM-STRING, read from this string instead from
16554 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
16555 the time/date that is used for everything that is not specified by the
16557 (require 'parse-time)
16558 (let* ((org-time-stamp-rounding-minutes
16559 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
16560 (org-dcst org-display-custom-times)
16561 (ct (org-current-time))
16562 (org-def (or org-overriding-default-time default-time ct))
16563 (org-defdecode (decode-time org-def))
16565 (when (< (nth 2 org-defdecode) org-extend-today-until)
16566 (setcar (nthcdr 2 org-defdecode) -1)
16567 (setcar (nthcdr 1 org-defdecode) 59)
16568 (setq org-def (apply 'encode-time org-defdecode)
16569 org-defdecode (decode-time org-def)))))
16570 (mouse-autoselect-window nil) ; Don't let the mouse jump
16571 (calendar-frame-setup nil)
16572 (calendar-setup nil)
16573 (calendar-move-hook nil)
16574 (calendar-view-diary-initially-flag nil)
16575 (calendar-view-holidays-initially-flag nil)
16576 (timestr (format-time-string
16577 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
16578 (prompt (concat (if prompt (concat prompt " ") "")
16579 (format "Date+time [%s]: " timestr)))
16580 ans (org-ans0 "") org-ans1 org-ans2 final)
16583 (from-string (setq ans from-string))
16584 (org-read-date-popup-calendar
16586 (save-window-excursion
16588 (org-eval-in-calendar '(setq cursor-type nil) t)
16591 (calendar-forward-day (- (time-to-days org-def)
16592 (calendar-absolute-from-gregorian
16593 (calendar-current-date))))
16594 (org-eval-in-calendar nil t)
16595 (let* ((old-map (current-local-map))
16596 (map (copy-keymap calendar-mode-map))
16597 (minibuffer-local-map
16598 (copy-keymap org-read-date-minibuffer-local-map)))
16599 (org-defkey map (kbd "RET") 'org-calendar-select)
16600 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
16601 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
16604 (use-local-map map)
16605 (setq org-read-date-inactive inactive)
16606 (add-hook 'post-command-hook 'org-read-date-display)
16607 (setq org-ans0 (read-string prompt default-input
16608 'org-read-date-history nil))
16609 ;; org-ans0: from prompt
16610 ;; org-ans1: from mouse click
16611 ;; org-ans2: from calendar motion
16612 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
16613 (remove-hook 'post-command-hook 'org-read-date-display)
16614 (use-local-map old-map)
16615 (when org-read-date-overlay
16616 (delete-overlay org-read-date-overlay)
16617 (setq org-read-date-overlay nil)))))
16618 (bury-buffer "*Calendar*")))))
16620 (t ; Naked prompt only
16622 (setq ans (read-string prompt default-input
16623 'org-read-date-history timestr))
16624 (when org-read-date-overlay
16625 (delete-overlay org-read-date-overlay)
16626 (setq org-read-date-overlay nil)))))
16628 (setq final (org-read-date-analyze ans org-def org-defdecode))
16630 (when org-read-date-analyze-forced-year
16631 (message "Year was forced into %s"
16632 (if org-read-date-force-compatible-dates
16633 "compatible range (1970-2037)"
16634 "range representable on this machine"))
16637 ;; One round trip to get rid of 34th of August and stuff like that....
16638 (setq final (decode-time (apply 'encode-time final)))
16640 (setq org-read-date-final-answer ans)
16643 (apply 'encode-time final)
16644 (if (and (boundp 'org-time-was-given) org-time-was-given)
16645 (format "%04d-%02d-%02d %02d:%02d"
16646 (nth 5 final) (nth 4 final) (nth 3 final)
16647 (nth 2 final) (nth 1 final))
16648 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
16650 (defun org-read-date-display ()
16651 "Display the current date prompt interpretation in the minibuffer."
16652 (when org-read-date-display-live
16653 (when org-read-date-overlay
16654 (delete-overlay org-read-date-overlay))
16655 (when (minibufferp (current-buffer))
16658 (while (not (equal (buffer-substring
16659 (max (point-min) (- (point) 4)) (point))
16662 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
16663 " " (or org-ans1 org-ans2)))
16664 (org-end-time-was-given nil)
16665 (f (org-read-date-analyze ans org-def org-defdecode))
16667 org-time-stamp-custom-formats
16668 org-time-stamp-formats))
16669 (fmt (if (or org-with-time
16670 (and (boundp 'org-time-was-given) org-time-was-given))
16673 (txt (format-time-string fmt (apply 'encode-time f)))
16674 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
16675 (txt (concat "=> " txt)))
16676 (when (and org-end-time-was-given
16677 (string-match org-plain-time-of-day-regexp txt))
16678 (setq txt (concat (substring txt 0 (match-end 0)) "-"
16679 org-end-time-was-given
16680 (substring txt (match-end 0)))))
16681 (when org-read-date-analyze-futurep
16682 (setq txt (concat txt " (=>F)")))
16683 (setq org-read-date-overlay
16684 (make-overlay (1- (point-at-eol)) (point-at-eol)))
16685 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
16687 (defun org-read-date-analyze (ans org-def org-defdecode)
16688 "Analyze the combined answer of the date prompt."
16689 ;; FIXME: cleanup and comment
16690 (let ((nowdecode (decode-time (current-time)))
16691 delta deltan deltaw deltadef year month day
16692 hour minute second wday pm h2 m2 tl wday1
16693 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
16694 (setq org-read-date-analyze-futurep nil
16695 org-read-date-analyze-forced-year nil)
16696 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
16699 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
16700 (setq ans (replace-match "" t t ans)
16702 deltaw (nth 1 delta)
16703 deltadef (nth 2 delta)))
16705 ;; Check if there is an iso week date in there. If yes, store the
16706 ;; info and postpone interpreting it until the rest of the parsing
16708 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
16709 (setq iso-year (if (match-end 1)
16710 (org-small-year-to-year
16711 (string-to-number (match-string 1 ans))))
16712 iso-weekday (if (match-end 3)
16713 (string-to-number (match-string 3 ans)))
16714 iso-week (string-to-number (match-string 2 ans)))
16715 (setq ans (replace-match "" t t ans)))
16717 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
16718 (when (string-match
16719 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
16720 (setq year (if (match-end 2)
16721 (string-to-number (match-string 2 ans))
16722 (progn (setq kill-year t)
16723 (string-to-number (format-time-string "%Y"))))
16724 month (string-to-number (match-string 3 ans))
16725 day (string-to-number (match-string 4 ans)))
16726 (if (< year 100) (setq year (+ 2000 year)))
16727 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
16730 ;; Help matching dotted european dates
16731 (when (string-match
16732 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
16733 (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
16735 (string-to-number (format-time-string "%Y")))
16736 day (string-to-number (match-string 1 ans))
16737 month (string-to-number (match-string 2 ans))
16738 ans (replace-match (format "%04d-%02d-%02d" year month day)
16741 ;; Help matching american dates, like 5/30 or 5/30/7
16742 (when (string-match
16743 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
16744 (setq year (if (match-end 4)
16745 (string-to-number (match-string 4 ans))
16746 (progn (setq kill-year t)
16747 (string-to-number (format-time-string "%Y"))))
16748 month (string-to-number (match-string 1 ans))
16749 day (string-to-number (match-string 2 ans)))
16750 (if (< year 100) (setq year (+ 2000 year)))
16751 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
16753 ;; Help matching am/pm times, because `parse-time-string' does not do that.
16754 ;; If there is a time with am/pm, and *no* time without it, we convert
16755 ;; so that matching will be successful.
16756 (loop for i from 1 to 2 do ; twice, for end time as well
16757 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
16758 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
16759 (setq hour (string-to-number (match-string 1 ans))
16760 minute (if (match-end 3)
16761 (string-to-number (match-string 3 ans))
16764 (string-to-char (downcase (match-string 4 ans)))))
16765 (if (and (= hour 12) (not pm))
16767 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
16768 (setq ans (replace-match (format "%02d:%02d" hour minute)
16771 ;; Check if a time range is given as a duration
16772 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
16773 (setq hour (string-to-number (match-string 1 ans))
16774 h2 (+ hour (string-to-number (match-string 3 ans)))
16775 minute (string-to-number (match-string 2 ans))
16776 m2 (+ minute (if (match-end 5) (string-to-number
16777 (match-string 5 ans))0)))
16778 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
16779 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
16782 ;; Check if there is a time range
16783 (when (boundp 'org-end-time-was-given)
16784 (setq org-time-was-given nil)
16785 (when (and (string-match org-plain-time-of-day-regexp ans)
16787 (setq org-end-time-was-given (match-string 8 ans))
16788 (setq ans (concat (substring ans 0 (match-beginning 7))
16789 (substring ans (match-end 7))))))
16791 (setq tl (parse-time-string ans)
16792 day (or (nth 3 tl) (nth 3 org-defdecode))
16793 month (or (nth 4 tl)
16794 (if (and org-read-date-prefer-future
16795 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
16796 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
16797 (nth 4 org-defdecode)))
16798 year (or (and (not kill-year) (nth 5 tl))
16799 (if (and org-read-date-prefer-future
16800 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
16801 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
16802 (nth 5 org-defdecode)))
16803 hour (or (nth 2 tl) (nth 2 org-defdecode))
16804 minute (or (nth 1 tl) (nth 1 org-defdecode))
16805 second (or (nth 0 tl) 0)
16808 (when (and (eq org-read-date-prefer-future 'time)
16809 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
16810 (equal day (nth 3 nowdecode))
16811 (equal month (nth 4 nowdecode))
16812 (equal year (nth 5 nowdecode))
16814 (or (< (nth 2 tl) (nth 2 nowdecode))
16815 (and (= (nth 2 tl) (nth 2 nowdecode))
16817 (< (nth 1 tl) (nth 1 nowdecode)))))
16821 ;; Special date definitions below
16824 ;; There was an iso week
16827 (setq year (or iso-year year)
16828 day (or iso-weekday wday 1)
16829 wday nil ; to make sure that the trigger below does not match
16830 iso-date (calendar-gregorian-from-absolute
16831 (calendar-absolute-from-iso
16832 (list iso-week day year))))
16833 ; FIXME: Should we also push ISO weeks into the future?
16834 ; (when (and org-read-date-prefer-future
16836 ; (< (calendar-absolute-from-gregorian iso-date)
16837 ; (time-to-days (current-time))))
16838 ; (setq year (1+ year)
16839 ; iso-date (calendar-gregorian-from-absolute
16840 ; (calendar-absolute-from-iso
16841 ; (list iso-week day year)))))
16842 (setq month (car iso-date)
16843 year (nth 2 iso-date)
16844 day (nth 1 iso-date)))
16848 (let ((now (decode-time (current-time))))
16849 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
16850 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
16851 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
16852 ((equal deltaw "m") (setq month (+ month deltan)))
16853 ((equal deltaw "y") (setq year (+ year deltan)))))
16854 ((and wday (not (nth 3 tl)))
16855 ;; Weekday was given, but no day, so pick that day in the week
16856 ;; on or after the derived date.
16857 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
16858 (unless (equal wday wday1)
16859 (setq day (+ day (% (- wday wday1 -7) 7))))))
16860 (if (and (boundp 'org-time-was-given)
16862 (setq org-time-was-given t))
16863 (if (< year 100) (setq year (+ 2000 year)))
16864 ;; Check of the date is representable
16865 (if org-read-date-force-compatible-dates
16868 (setq year 1970 org-read-date-analyze-forced-year t))
16870 (setq year 2037 org-read-date-analyze-forced-year t)))
16871 (condition-case nil
16872 (ignore (encode-time second minute hour day month year))
16874 (setq year (nth 5 org-defdecode))
16875 (setq org-read-date-analyze-forced-year t))))
16876 (setq org-read-date-analyze-futurep futurep)
16877 (list second minute hour day month year)))
16879 (defvar parse-time-weekdays)
16880 (defun org-read-date-get-relative (s today default)
16881 "Check string S for special relative date string.
16882 TODAY and DEFAULT are internal times, for today and for a default.
16883 Return shift list (N what def-flag)
16884 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
16885 N is the number of WHATs to shift.
16886 DEF-FLAG is t when a double ++ or -- indicates shift relative to
16887 the DEFAULT date rather than TODAY."
16888 (require 'parse-time)
16892 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
16894 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
16895 "\\([ \t]\\|$\\)") s)
16896 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
16897 (let* ((dir (if (> (match-end 1) (match-beginning 1))
16898 (string-to-char (substring (match-string 1 s) -1))
16900 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
16901 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
16902 (what (if (match-end 3) (match-string 3 s) "d"))
16903 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
16904 (date (if rel default today))
16905 (wday (nth 6 (decode-time date)))
16909 (setq delta (mod (+ 7 (- wday1 wday)) 7))
16910 (if (= delta 0) (setq delta 7))
16913 (setq delta (- delta 7))
16914 (if (= delta 0) (setq delta -7))))
16915 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
16916 (list delta "d" rel))
16917 (list (* n (if (= dir ?-) -1 1)) what rel)))))
16919 (defun org-order-calendar-date-args (arg1 arg2 arg3)
16920 "Turn a user-specified date into the internal representation.
16921 The internal representation needed by the calendar is (month day year).
16922 This is a wrapper to handle the brain-dead convention in calendar that
16923 user function argument order change dependent on argument order."
16924 (if (boundp 'calendar-date-style)
16926 ((eq calendar-date-style 'american)
16927 (list arg1 arg2 arg3))
16928 ((eq calendar-date-style 'european)
16929 (list arg2 arg1 arg3))
16930 ((eq calendar-date-style 'iso)
16931 (list arg2 arg3 arg1)))
16932 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
16933 (if (org-bound-and-true-p european-calendar-style)
16934 (list arg2 arg1 arg3)
16935 (list arg1 arg2 arg3)))))
16937 (defun org-eval-in-calendar (form &optional keepdate)
16938 "Eval FORM in the calendar window and return to current window.
16939 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
16940 otherwise stick to the current value of `org-ans2'."
16941 (let ((sf (selected-frame))
16942 (sw (selected-window)))
16943 (select-window (get-buffer-window "*Calendar*" t))
16945 (when (and (not keepdate) (calendar-cursor-to-date))
16946 (let* ((date (calendar-cursor-to-date))
16947 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16948 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
16949 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
16951 (org-select-frame-set-input-focus sf)))
16953 (defun org-calendar-select ()
16954 "Return to `org-read-date' with the date currently selected.
16955 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16957 (when (calendar-cursor-to-date)
16958 (let* ((date (calendar-cursor-to-date))
16959 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16960 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
16961 (if (active-minibuffer-window) (exit-minibuffer))))
16963 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
16964 "Insert a date stamp for the date given by the internal TIME.
16965 WITH-HM means use the stamp format that includes the time of the day.
16966 INACTIVE means use square brackets instead of angular ones, so that the
16967 stamp will not contribute to the agenda.
16968 PRE and POST are optional strings to be inserted before and after the
16970 The command returns the inserted time stamp."
16971 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
16973 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
16974 (insert-before-markers (or pre ""))
16975 (when (listp extra)
16976 (setq extra (car extra))
16977 (if (and (stringp extra)
16978 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
16979 (setq extra (format "-%02d:%02d"
16980 (string-to-number (match-string 1 extra))
16981 (string-to-number (match-string 2 extra))))
16984 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
16985 (insert-before-markers (setq stamp (format-time-string fmt time)))
16986 (insert-before-markers (or post ""))
16987 (setq org-last-inserted-timestamp stamp)))
16989 (defun org-toggle-time-stamp-overlays ()
16990 "Toggle the use of custom time stamp formats."
16992 (setq org-display-custom-times (not org-display-custom-times))
16993 (unless org-display-custom-times
16994 (let ((p (point-min)) (bmp (buffer-modified-p)))
16995 (while (setq p (next-single-property-change p 'display))
16996 (if (and (get-text-property p 'display)
16997 (eq (get-text-property p 'face) 'org-date))
16998 (remove-text-properties
16999 p (setq p (next-single-property-change p 'display))
17001 (set-buffer-modified-p bmp)))
17002 (if (featurep 'xemacs)
17003 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
17004 (org-restart-font-lock)
17005 (setq org-table-may-need-update t)
17006 (if org-display-custom-times
17007 (message "Time stamps are overlaid with custom format")
17008 (message "Time stamp overlays removed")))
17010 (defun org-display-custom-time (beg end)
17011 "Overlay modified time stamp format over timestamp between BEG and END."
17012 (let* ((ts (buffer-substring beg end))
17013 t1 w1 with-hm tf time str w2 (off 0))
17015 (setq t1 (org-parse-time-string ts t))
17016 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
17017 (setq off (- (match-end 0) (match-beginning 0)))))
17018 (setq end (- end off))
17019 (setq w1 (- end beg)
17020 with-hm (and (nth 1 t1) (nth 2 t1))
17021 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
17022 time (org-fix-decoded-time t1)
17024 (format-time-string
17025 (substring tf 1 -1) (apply 'encode-time time))
17026 nil 'mouse-face 'highlight)
17028 (if (not (= w2 w1))
17029 (add-text-properties (1+ beg) (+ 2 beg)
17030 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
17031 (if (featurep 'xemacs)
17033 (put-text-property beg end 'invisible t)
17034 (put-text-property beg end 'end-glyph (make-glyph str)))
17035 (put-text-property beg end 'display str))))
17037 (defun org-translate-time (string)
17038 "Translate all timestamps in STRING to custom format.
17039 But do this only if the variable `org-display-custom-times' is set."
17040 (when org-display-custom-times
17043 (re org-ts-regexp-both)
17044 t1 with-hm inactive tf time str beg end)
17045 (while (setq start (string-match re string start))
17046 (setq beg (match-beginning 0)
17048 t1 (save-match-data
17049 (org-parse-time-string (substring string beg end) t))
17050 with-hm (and (nth 1 t1) (nth 2 t1))
17051 inactive (equal (substring string beg (1+ beg)) "[")
17052 tf (funcall (if with-hm 'cdr 'car)
17053 org-time-stamp-custom-formats)
17054 time (org-fix-decoded-time t1)
17055 str (format-time-string
17057 (if inactive "[" "<") (substring tf 1 -1)
17058 (if inactive "]" ">"))
17059 (apply 'encode-time time))
17060 string (replace-match str t t string)
17061 start (+ start (length str)))))))
17064 (defun org-fix-decoded-time (time)
17065 "Set 0 instead of nil for the first 6 elements of time.
17066 Don't touch the rest."
17068 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
17070 (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
17072 (defun org-time-stamp-to-now (timestamp-string &optional seconds)
17073 "Difference between TIMESTAMP-STRING and now in days.
17074 If SECONDS is non-nil, return the difference in seconds."
17075 (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
17076 (- (funcall fdiff (org-time-string-to-time timestamp-string))
17077 (funcall fdiff (current-time)))))
17079 (defun org-deadline-close (timestamp-string &optional ndays)
17080 "Is the time in TIMESTAMP-STRING close to the current date?"
17081 (setq ndays (or ndays (org-get-wdays timestamp-string)))
17082 (and (< (org-time-stamp-to-now timestamp-string) ndays)
17083 (not (org-entry-is-done-p))))
17085 (defun org-get-wdays (ts &optional delay zero-delay)
17086 "Get the deadline lead time appropriate for timestring TS.
17087 When DELAY is non-nil, get the delay time for scheduled items
17088 instead of the deadline lead time. When ZERO-DELAY is non-nil
17089 and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
17090 don't try to find the delay cookie in the scheduled timestamp."
17091 (let ((tv (if delay org-scheduled-delay-days
17092 org-deadline-warning-days)))
17094 ((or (and delay (< tv 0))
17095 (and delay zero-delay (<= tv 0))
17096 (and (not delay) (<= tv 0)))
17097 ;; Enforce this value no matter what
17099 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
17100 ;; lead time is specified.
17101 (floor (* (string-to-number (match-string 1 ts))
17102 (cdr (assoc (match-string 2 ts)
17103 '(("d" . 1) ("w" . 7)
17104 ("m" . 30.4) ("y" . 365.25)
17105 ("h" . 0.041667)))))))
17106 ;; go for the default.
17109 (defun org-calendar-select-mouse (ev)
17110 "Return to `org-read-date' with the date currently selected.
17111 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
17113 (mouse-set-point ev)
17114 (when (calendar-cursor-to-date)
17115 (let* ((date (calendar-cursor-to-date))
17116 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
17117 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
17118 (if (active-minibuffer-window) (exit-minibuffer))))
17120 (defun org-check-deadlines (ndays)
17121 "Check if there are any deadlines due or past due.
17122 A deadline is considered due if it happens within `org-deadline-warning-days'
17123 days from today's date. If the deadline appears in an entry marked DONE,
17124 it is not shown. The prefix arg NDAYS can be used to test that many
17125 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
17127 (let* ((org-warn-days
17129 ((equal ndays '(4)) 100000)
17130 (ndays (prefix-numeric-value ndays))
17131 (t (abs org-deadline-warning-days))))
17132 (case-fold-search nil)
17133 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
17135 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
17137 (message "%d deadlines past-due or due within %d days"
17138 (org-occur regexp nil callback)
17141 (defsubst org-re-timestamp (type)
17142 "Return a regexp for timestamp TYPE.
17143 Allowed values for TYPE are:
17145 all: all timestamps
17146 active: only active timestamps (<...>)
17147 inactive: only inactive timestamps ([...])
17148 scheduled: only scheduled timestamps
17149 deadline: only deadline timestamps
17150 closed: only closed time-stamps
17152 When TYPE is nil, fall back on returning a regexp that matches
17153 both scheduled and deadline timestamps."
17154 (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9>
\n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
17155 ((eq type 'active) org-ts-regexp)
17156 ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]")
17157 ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
17158 ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
17159 ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]"))
17160 ((eq type 'scheduled-or-deadline)
17161 (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
17163 (defun org-check-before-date (date)
17164 "Check if there are deadlines or scheduled entries before DATE."
17165 (interactive (list (org-read-date)))
17166 (let ((case-fold-search nil)
17167 (regexp (org-re-timestamp org-ts-type))
17169 (lambda () (time-less-p
17170 (org-time-string-to-time (match-string 1))
17171 (org-time-string-to-time date)))))
17172 (message "%d entries before %s"
17173 (org-occur regexp nil callback) date)))
17175 (defun org-check-after-date (date)
17176 "Check if there are deadlines or scheduled entries after DATE."
17177 (interactive (list (org-read-date)))
17178 (let ((case-fold-search nil)
17179 (regexp (org-re-timestamp org-ts-type))
17183 (org-time-string-to-time (match-string 1))
17184 (org-time-string-to-time date))))))
17185 (message "%d entries after %s"
17186 (org-occur regexp nil callback) date)))
17188 (defun org-check-dates-range (start-date end-date)
17189 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
17190 (interactive (list (org-read-date nil nil nil "Range starts")
17191 (org-read-date nil nil nil "Range end")))
17192 (let ((case-fold-search nil)
17193 (regexp (org-re-timestamp org-ts-type))
17196 (let ((match (match-string 1)))
17199 (org-time-string-to-time match)
17200 (org-time-string-to-time start-date)))
17202 (org-time-string-to-time match)
17203 (org-time-string-to-time end-date)))))))
17204 (message "%d entries between %s and %s"
17205 (org-occur regexp nil callback) start-date end-date)))
17207 (defun org-evaluate-time-range (&optional to-buffer)
17208 "Evaluate a time range by computing the difference between start and end.
17209 Normally the result is just printed in the echo area, but with prefix arg
17210 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
17211 If the time range is actually in a table, the result is inserted into the
17213 For time difference computation, a year is assumed to be exactly 365
17214 days in order to avoid rounding problems."
17217 (org-clock-update-time-maybe)
17219 (unless (org-at-date-range-p t)
17220 (goto-char (point-at-bol))
17221 (re-search-forward org-tr-regexp-both (point-at-eol) t))
17222 (if (not (org-at-date-range-p t))
17223 (user-error "Not at a time-stamp range, and none found in current line")))
17224 (let* ((ts1 (match-string 1))
17225 (ts2 (match-string 2))
17226 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
17227 (match-end (match-end 0))
17228 (time1 (org-time-string-to-time ts1))
17229 (time2 (org-time-string-to-time ts2))
17230 (t1 (org-float-time time1))
17231 (t2 (org-float-time time2))
17232 (diff (abs (- t2 t1)))
17233 (negative (< (- t2 t1) 0))
17234 ;; (ys (floor (* 365 24 60 60)))
17237 (fy "%dy %dd %02d:%02d")
17239 (fd "%dd %02d:%02d")
17244 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
17246 d (floor (/ diff ds)) diff (mod diff ds)
17247 h (floor (/ diff hs)) diff (mod diff hs)
17248 m (floor (/ diff 60)))
17249 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
17251 d (floor (+ (/ diff ds) 0.5))
17253 (if (not to-buffer)
17254 (message "%s" (org-make-tdiff-string y d h m))
17255 (if (org-at-table-p)
17257 (goto-char match-end)
17259 (and (looking-at " *|") (goto-char (match-end 0))))
17260 (goto-char match-end))
17262 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
17263 (replace-match ""))
17264 (if negative (insert " -"))
17265 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
17266 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
17267 (insert " " (format fh h m))))
17268 (if align (org-table-align))
17269 (message "Time difference inserted")))))
17271 (defun org-make-tdiff-string (y d h m)
17274 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
17276 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
17278 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
17280 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
17282 (apply 'format fmt (nreverse l))))
17284 (defun org-time-string-to-time (s &optional buffer pos)
17285 "Convert a timestamp string into internal time."
17286 (condition-case errdata
17287 (apply 'encode-time (org-parse-time-string s))
17288 (error (error "Bad timestamp `%s'%s\nError was: %s"
17289 s (if (not (and buffer pos))
17291 (format " at %d in buffer `%s'" pos buffer))
17294 (defun org-time-string-to-seconds (s)
17295 "Convert a timestamp string to a number of seconds."
17296 (org-float-time (org-time-string-to-time s)))
17298 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
17299 "Convert a time stamp to an absolute day number.
17300 If there is a specifier for a cyclic time stamp, get the closest
17302 PREFER and SHOW-ALL are passed through to `org-closest-date'.
17303 The variable `date' is bound by the calendar when this is called."
17305 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
17306 (if (org-diary-sexp-entry (match-string 1 s) "" date)
17309 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
17310 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
17311 (time-to-days (current-time))) (match-string 0 s)
17314 (condition-case errdata
17315 (apply 'encode-time (org-parse-time-string s))
17316 (error (error "Bad timestamp `%s'%s\nError was: %s"
17317 s (if (not (and buffer pos))
17319 (format " at %d in buffer `%s'" pos buffer))
17320 (cdr errdata))))))))
17322 (defun org-days-to-iso-week (days)
17323 "Return the iso week number."
17325 (car (calendar-iso-from-absolute days)))
17327 (defun org-small-year-to-year (year)
17328 "Convert 2-digit years into 4-digit years.
17329 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
17330 The year 2000 cannot be abbreviated. Any year larger than 99
17331 is returned unchanged."
17333 (setq year (+ 2000 year))
17335 (setq year (+ 1900 year))))
17338 (defun org-time-from-absolute (d)
17339 "Return the time corresponding to date D.
17340 D may be an absolute day number, or a calendar-type list (month day year)."
17341 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
17342 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
17344 (defun org-calendar-holiday ()
17345 "List of holidays, for Diary display in Org-mode."
17346 (require 'holidays)
17348 (if (fboundp 'calendar-check-holidays)
17349 'calendar-check-holidays 'check-calendar-holidays) date)))
17350 (if hl (mapconcat 'identity hl "; "))))
17352 (defun org-diary-sexp-entry (sexp entry date)
17353 "Process a SEXP diary ENTRY for DATE."
17354 (require 'diary-lib)
17355 (let ((result (if calendar-debug-sexp
17356 (let ((stack-trace-on-error t))
17357 (eval (car (read-from-string sexp))))
17358 (condition-case nil
17359 (eval (car (read-from-string sexp)))
17362 (message "Bad sexp at line %d in %s: %s"
17364 (buffer-file-name) sexp)
17366 (cond ((stringp result) (split-string result "; "))
17367 ((and (consp result)
17368 (not (consp (cdr result)))
17369 (stringp (cdr result))) (cdr result))
17370 ((and (consp result)
17371 (stringp (car result))) result)
17374 (defun org-diary-to-ical-string (frombuf)
17375 "Get iCalendar entries from diary entries in buffer FROMBUF.
17376 This uses the icalendar.el library."
17377 (let* ((tmpdir (if (featurep 'xemacs)
17379 temporary-file-directory))
17380 (tmpfile (make-temp-name
17381 (expand-file-name "orgics" tmpdir)))
17383 (with-current-buffer frombuf
17384 (icalendar-export-region (point-min) (point-max) tmpfile)
17385 (setq buf (find-buffer-visiting tmpfile))
17387 (goto-char (point-min))
17388 (if (re-search-forward "^BEGIN:VEVENT" nil t)
17389 (setq b (match-beginning 0)))
17390 (goto-char (point-max))
17391 (if (re-search-backward "^END:VEVENT" nil t)
17392 (setq e (match-end 0)))
17393 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
17395 (delete-file tmpfile)
17398 (defun org-closest-date (start current change prefer show-all)
17399 "Find the date closest to CURRENT that is consistent with START and CHANGE.
17400 When PREFER is `past', return a date that is either CURRENT or past.
17401 When PREFER is `future', return a date that is either CURRENT or future.
17402 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
17403 ;; Make the proper lists from the dates
17405 (let ((a1 '(("h" . hour)
17410 (shour (nth 2 (org-parse-time-string start)))
17411 dn dw sday cday n1 n2 n0
17412 d m y y1 y2 date1 date2 nmonths nm ny m2)
17414 (setq start (org-date-to-gregorian start)
17415 current (org-date-to-gregorian
17418 (time-to-days (current-time))))
17419 sday (calendar-absolute-from-gregorian start)
17420 cday (calendar-absolute-from-gregorian current))
17422 (if (<= cday sday) (throw 'exit sday))
17424 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
17425 (setq dn (string-to-number (match-string 1 change))
17426 dw (cdr (assoc (match-string 2 change) a1)))
17427 (user-error "Invalid change specifier: %s" change))
17428 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
17431 (let ((missing-hours
17432 (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
17434 (setq n1 (if (zerop missing-hours) cday
17435 (- cday (1+ (floor (/ missing-hours 24)))))
17436 n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
17438 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
17441 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
17442 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
17443 (setq date1 (list m d y1)
17444 n1 (calendar-absolute-from-gregorian date1)
17445 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
17446 n2 (calendar-absolute-from-gregorian date2)))
17448 ;; approx number of month between the two dates
17449 (setq nmonths (floor (/ (- cday sday) 30.436875)))
17450 ;; How often does dn fit in there?
17451 (setq d (nth 1 start) m (car start) y (nth 2 start)
17452 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
17454 ny (floor (/ m 12))
17457 (while (> m 12) (setq m (- m 12) y (1+ y)))
17458 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
17459 (setq m2 (+ m dn) y2 y)
17460 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
17461 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
17462 (while (<= n2 cday)
17463 (setq n1 n2 m m2 y y2)
17464 (setq m2 (+ m dn) y2 y)
17465 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
17466 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
17467 ;; Make sure n1 is the earlier date
17468 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
17471 ((eq prefer 'past) (if (= cday n2) n2 n1))
17472 ((eq prefer 'future) (if (= cday n1) n1 n2))
17473 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
17475 ((eq prefer 'past) (if (= cday n2) n2 n1))
17476 ((eq prefer 'future) (if (= cday n1) n1 n2))
17477 (t (if (= cday n1) n1 n2)))))))
17479 (defun org-date-to-gregorian (date)
17480 "Turn any specification of DATE into a Gregorian date for the calendar."
17481 (cond ((integerp date) (calendar-gregorian-from-absolute date))
17482 ((and (listp date) (= (length date) 3)) date)
17484 (setq date (org-parse-time-string date))
17485 (list (nth 4 date) (nth 3 date) (nth 5 date)))
17487 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
17489 (defun org-parse-time-string (s &optional nodefault)
17490 "Parse the standard Org-mode time string.
17491 This should be a lot faster than the normal `parse-time-string'.
17492 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
17493 hour and minute fields will be nil if not given."
17494 (cond ((string-match org-ts-regexp0 s)
17496 (if (or (match-beginning 8) (not nodefault))
17497 (string-to-number (or (match-string 8 s) "0")))
17498 (if (or (match-beginning 7) (not nodefault))
17499 (string-to-number (or (match-string 7 s) "0")))
17500 (string-to-number (match-string 4 s))
17501 (string-to-number (match-string 3 s))
17502 (string-to-number (match-string 2 s))
17504 ((string-match "^<[^>]+>$" s)
17505 (decode-time (seconds-to-time (org-matcher-time s))))
17506 (t (error "Not a standard Org-mode time string: %s" s))))
17508 (defun org-timestamp-up (&optional arg)
17509 "Increase the date item at the cursor by one.
17510 If the cursor is on the year, change the year. If it is on the month,
17511 the day or the time, change that.
17512 With prefix ARG, change by that many units."
17514 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
17516 (defun org-timestamp-down (&optional arg)
17517 "Decrease the date item at the cursor by one.
17518 If the cursor is on the year, change the year. If it is on the month,
17519 the day or the time, change that.
17520 With prefix ARG, change by that many units."
17522 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
17524 (defun org-timestamp-up-day (&optional arg)
17525 "Increase the date in the time stamp by one day.
17526 With prefix ARG, change that many days."
17528 (if (and (not (org-at-timestamp-p t))
17529 (org-at-heading-p))
17531 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
17533 (defun org-timestamp-down-day (&optional arg)
17534 "Decrease the date in the time stamp by one day.
17535 With prefix ARG, change that many days."
17537 (if (and (not (org-at-timestamp-p t))
17538 (org-at-heading-p))
17540 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
17542 (defun org-at-timestamp-p (&optional inactive-ok)
17543 "Determine if the cursor is in or at a timestamp."
17545 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
17547 (ans (or (looking-at tsr)
17549 (skip-chars-backward "^[<\n\r\t")
17550 (if (> (point) (point-min)) (backward-char 1))
17551 (and (looking-at tsr)
17552 (> (- (match-end 0) pos) -1))))))
17554 (boundp 'org-ts-what)
17557 ((= pos (match-beginning 0)) 'bracket)
17558 ;; Point is considered to be "on the bracket" whether
17559 ;; it's really on it or right after it.
17560 ((= pos (1- (match-end 0))) 'bracket)
17561 ((= pos (match-end 0)) 'after)
17562 ((org-pos-in-match-range pos 2) 'year)
17563 ((org-pos-in-match-range pos 3) 'month)
17564 ((org-pos-in-match-range pos 7) 'hour)
17565 ((org-pos-in-match-range pos 8) 'minute)
17566 ((or (org-pos-in-match-range pos 4)
17567 (org-pos-in-match-range pos 5)) 'day)
17568 ((and (> pos (or (match-end 8) (match-end 5)))
17569 (< pos (match-end 0)))
17570 (- pos (or (match-end 8) (match-end 5))))
17574 (defun org-toggle-timestamp-type ()
17575 "Toggle the type (<active> or [inactive]) of a time stamp."
17577 (when (org-at-timestamp-p t)
17578 (let ((beg (match-beginning 0)) (end (match-end 0))
17579 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
17582 (while (re-search-forward "[][<>]" end t)
17583 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
17585 (message "Timestamp is now %sactive"
17586 (if (equal (char-after beg) ?<) "" "in")))))
17588 (defun org-at-clock-log-p nil
17589 "Is the cursor on the clock log line?"
17591 (move-beginning-of-line 1)
17592 (looking-at "^[ \t]*CLOCK:")))
17594 (defvar org-clock-history) ; defined in org-clock.el
17595 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
17596 (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
17597 "Change the date in the time stamp at point.
17598 The date will be changed by N times WHAT. WHAT can be `day', `month',
17599 `year', `minute', `second'. If WHAT is not given, the cursor position
17600 in the timestamp determines what will be changed.
17601 When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
17602 (let ((origin (point)) origin-cat
17604 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
17607 ts time time0 fixnext clrgx)
17608 (if (not (org-at-timestamp-p t))
17609 (user-error "Not at a timestamp"))
17610 (if (and (not what) (eq org-ts-what 'bracket))
17611 (org-toggle-timestamp-type)
17612 ;; Point isn't on brackets. Remember the part of the time-stamp
17613 ;; the point was in. Indeed, size of time-stamps may change,
17614 ;; but point must be kept in the same category nonetheless.
17615 (setq origin-cat org-ts-what)
17616 (if (and (not what) (not (eq org-ts-what 'day))
17617 org-display-custom-times
17618 (get-text-property (point) 'display)
17619 (not (get-text-property (1- (point)) 'display)))
17620 (setq org-ts-what 'day))
17621 (setq org-ts-what (or what org-ts-what)
17622 inactive (= (char-after (match-beginning 0)) ?\[)
17623 ts (match-string 0))
17625 (when (string-match
17626 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
17628 (setq extra (match-string 1 ts))
17629 (if suppress-tmp-delay
17630 (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
17631 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
17633 (setq time0 (org-parse-time-string ts))
17635 (eq org-ts-what 'minute)
17636 (not current-prefix-arg))
17637 ;; This looks like s-up and s-down. Change by one rounding step.
17638 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
17639 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
17640 (setcar (cdr time0) (+ (nth 1 time0)
17641 (if (> n 0) (- rem) (- dm rem))))))
17643 (encode-time (or (car time0) 0)
17644 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
17645 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
17646 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
17647 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
17648 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
17650 (when (and (member org-ts-what '(hour minute))
17652 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
17653 (setq extra (org-modify-ts-extra
17655 (if (eq org-ts-what 'hour) 2 5)
17657 (when (integerp org-ts-what)
17658 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
17659 (if (eq what 'calendar)
17660 (let ((cal-date (org-get-date-from-calendar)))
17661 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
17662 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
17663 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
17664 (setcar time0 (or (car time0) 0))
17665 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
17666 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
17667 (setq time (apply 'encode-time time0))))
17668 ;; Insert the new time-stamp, and ensure point stays in the same
17669 ;; category as before (i.e. not after the last position in that
17671 (let ((pos (point)))
17672 ;; Stay before inserted string. `save-excursion' is of no use.
17673 (setq org-last-changed-timestamp
17674 (org-insert-time-stamp time with-hm inactive nil nil extra))
17677 (looking-at org-ts-regexp3)
17679 ;; `day' category ends before `hour' if any, or at
17680 ;; the end of the day name.
17681 ((eq origin-cat 'day)
17682 (min (or (match-beginning 7) (1- (match-end 5))) origin))
17683 ((eq origin-cat 'hour) (min (match-end 7) origin))
17684 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
17685 ((integerp origin-cat) (min (1- (match-end 0)) origin))
17686 ;; `year' and `month' have both fixed size: point
17687 ;; couldn't have moved into another part.
17689 ;; Update clock if on a CLOCK line.
17690 (org-clock-update-time-maybe)
17691 ;; Maybe adjust the closest clock in `org-clock-history'
17692 (when org-clock-adjust-closest
17693 (if (not (and (org-at-clock-log-p)
17694 (< 1 (length (delq nil (mapcar 'marker-position
17695 org-clock-history))))))
17696 (message "No clock to adjust")
17697 (cond ((save-excursion ; fix previous clock?
17698 (re-search-backward org-ts-regexp0 nil t)
17699 (org-looking-back (concat org-clock-string " \\[")))
17700 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
17701 ((save-excursion ; fix next clock?
17702 (re-search-backward org-ts-regexp0 nil t)
17703 (looking-at (concat org-ts-regexp0 "\\] =>")))
17704 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
17705 (save-window-excursion
17706 ;; Find closest clock to point, adjust the previous/next one in history
17707 (let* ((p (save-excursion (org-back-to-heading t)))
17708 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
17710 (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
17711 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
17713 (message "No clock to adjust")
17715 (org-goto-marker-or-bmk clfixpos)
17717 (when (re-search-forward clrgx nil t)
17718 (goto-char (match-beginning 1))
17719 (let (org-clock-adjust-closest)
17720 (org-timestamp-change n org-ts-what updown))
17721 (message "Clock adjusted in %s for heading: %s"
17722 (file-name-nondirectory (buffer-file-name))
17723 (org-get-heading t t)))))))))
17724 ;; Try to recenter the calendar window, if any.
17725 (if (and org-calendar-follow-timestamp-change
17726 (get-buffer-window "*Calendar*" t)
17727 (memq org-ts-what '(day month year)))
17728 (org-recenter-calendar (time-to-days time))))))
17730 (defun org-modify-ts-extra (s pos n dm)
17731 "Change the different parts of the lead-time and repeat fields in timestamp."
17732 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
17734 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
17736 ((or (org-pos-in-match-range pos 2)
17737 (org-pos-in-match-range pos 3))
17738 (setq m (string-to-number (match-string 3 s))
17739 h (string-to-number (match-string 2 s)))
17740 (if (org-pos-in-match-range pos 2)
17742 (setq n (* dm (org-no-warnings (signum n))))
17743 (when (not (= 0 (setq rem (% m dm))))
17744 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
17746 (if (< m 0) (setq m (+ m 60) h (1- h)))
17747 (if (> m 59) (setq m (- m 60) h (1+ h)))
17748 (setq h (min 24 (max 0 h)))
17749 (setq ng 1 new (format "-%02d:%02d" h m)))
17750 ((org-pos-in-match-range pos 6)
17751 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
17752 ((org-pos-in-match-range pos 5)
17753 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
17755 ((org-pos-in-match-range pos 9)
17756 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
17757 ((org-pos-in-match-range pos 8)
17758 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
17762 (substring s 0 (match-beginning ng))
17764 (substring s (match-end ng))))))
17767 (defun org-recenter-calendar (date)
17768 "If the calendar is visible, recenter it to DATE."
17769 (let ((cwin (get-buffer-window "*Calendar*" t)))
17771 (let ((calendar-move-hook nil))
17772 (with-selected-window cwin
17773 (calendar-goto-date (if (listp date) date
17774 (calendar-gregorian-from-absolute date))))))))
17776 (defun org-goto-calendar (&optional arg)
17777 "Go to the Emacs calendar at the current date.
17778 If there is a time stamp in the current line, go to that date.
17779 A prefix ARG can be used to force the current date."
17781 (let ((tsr org-ts-regexp) diff
17782 (calendar-move-hook nil)
17783 (calendar-view-holidays-initially-flag nil)
17784 (calendar-view-diary-initially-flag nil))
17785 (if (or (org-at-timestamp-p)
17787 (beginning-of-line 1)
17788 (looking-at (concat ".*" tsr))))
17789 (let ((d1 (time-to-days (current-time)))
17791 (org-time-string-to-time (match-string 1)))))
17792 (setq diff (- d2 d1))))
17794 (calendar-goto-today)
17795 (if (and diff (not arg)) (calendar-forward-day diff))))
17797 (defun org-get-date-from-calendar ()
17798 "Return a list (month day year) of date at point in calendar."
17799 (with-current-buffer "*Calendar*"
17801 (calendar-cursor-to-date))))
17803 (defun org-date-from-calendar ()
17804 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
17805 If there is already a time stamp at the cursor position, update it."
17807 (if (org-at-timestamp-p t)
17808 (org-timestamp-change 0 'calendar)
17809 (let ((cal-date (org-get-date-from-calendar)))
17810 (org-insert-time-stamp
17811 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
17813 (defcustom org-effort-durations
17816 ("w" . ,(* 60 8 5))
17817 ("m" . ,(* 60 8 5 4))
17818 ("y" . ,(* 60 8 5 40)))
17819 "Conversion factor to minutes for an effort modifier.
17821 Each entry has the form (MODIFIER . MINUTES).
17823 In an effort string, a number followed by MODIFIER is multiplied
17824 by the specified number of MINUTES to obtain an effort in
17827 For example, if the value of this variable is ((\"hours\" . 60)), then an
17828 effort string \"2hours\" is equivalent to 120 minutes."
17831 :type '(alist :key-type (string :tag "Modifier")
17832 :value-type (number :tag "Minutes")))
17834 (defun org-minutes-to-clocksum-string (m)
17835 "Format number of minutes as a clocksum string.
17836 The format is determined by `org-time-clocksum-format',
17837 `org-time-clocksum-use-fractional' and
17838 `org-time-clocksum-fractional-format' and
17839 `org-time-clocksum-use-effort-durations'."
17840 (let ((clocksum "")
17841 (m (round m)) ; Don't allow fractions of minutes
17843 (setq h (if org-time-clocksum-use-effort-durations
17844 (cdr (assoc "h" org-effort-durations)) 60)
17845 d (if org-time-clocksum-use-effort-durations
17846 (/ (cdr (assoc "d" org-effort-durations)) h) 24)
17847 w (if org-time-clocksum-use-effort-durations
17848 (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
17849 mo (if org-time-clocksum-use-effort-durations
17850 (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
17851 y (if org-time-clocksum-use-effort-durations
17852 (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
17853 ;; fractional format
17854 (if org-time-clocksum-use-fractional
17856 ;; single format string
17857 ((stringp org-time-clocksum-fractional-format)
17858 (format org-time-clocksum-fractional-format (/ m (float h))))
17859 ;; choice of fractional formats for different time units
17860 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
17861 (> (/ (truncate m) (* y d h)) 0))
17862 (format fmt (/ m (* y d (float h)))))
17863 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
17864 (> (/ (truncate m) (* mo d h)) 0))
17865 (format fmt (/ m (* mo d (float h)))))
17866 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
17867 (> (/ (truncate m) (* w d h)) 0))
17868 (format fmt (/ m (* w d (float h)))))
17869 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
17870 (> (/ (truncate m) (* d h)) 0))
17871 (format fmt (/ m (* d (float h)))))
17872 ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
17873 (> (/ (truncate m) h) 0))
17874 (format fmt (/ m (float h))))
17875 ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
17877 ;; fall back to smallest time unit with a format
17878 ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
17879 (format fmt (/ m (float h))))
17880 ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
17881 (format fmt (/ m (* d (float h)))))
17882 ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
17883 (format fmt (/ m (* w d (float h)))))
17884 ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
17885 (format fmt (/ m (* mo d (float h)))))
17886 ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
17887 (format fmt (/ m (* y d (float h))))))
17888 ;; standard (non-fractional) format, with single format string
17889 (if (stringp org-time-clocksum-format)
17890 (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
17891 ;; separate formats components
17892 (and (setq fmt (plist-get org-time-clocksum-format :years))
17893 (or (> (setq n (/ (truncate m) (* y d h))) 0)
17894 (plist-get org-time-clocksum-format :require-years))
17895 (setq clocksum (concat clocksum (format fmt n))
17896 m (- m (* n y d h))))
17897 (and (setq fmt (plist-get org-time-clocksum-format :months))
17898 (or (> (setq n (/ (truncate m) (* mo d h))) 0)
17899 (plist-get org-time-clocksum-format :require-months))
17900 (setq clocksum (concat clocksum (format fmt n))
17901 m (- m (* n mo d h))))
17902 (and (setq fmt (plist-get org-time-clocksum-format :weeks))
17903 (or (> (setq n (/ (truncate m) (* w d h))) 0)
17904 (plist-get org-time-clocksum-format :require-weeks))
17905 (setq clocksum (concat clocksum (format fmt n))
17906 m (- m (* n w d h))))
17907 (and (setq fmt (plist-get org-time-clocksum-format :days))
17908 (or (> (setq n (/ (truncate m) (* d h))) 0)
17909 (plist-get org-time-clocksum-format :require-days))
17910 (setq clocksum (concat clocksum (format fmt n))
17911 m (- m (* n d h))))
17912 (and (setq fmt (plist-get org-time-clocksum-format :hours))
17913 (or (> (setq n (/ (truncate m) h)) 0)
17914 (plist-get org-time-clocksum-format :require-hours))
17915 (setq clocksum (concat clocksum (format fmt n))
17917 (and (setq fmt (plist-get org-time-clocksum-format :minutes))
17918 (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
17919 (setq clocksum (concat clocksum (format fmt m))))
17920 ;; return formatted time duration
17923 (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
17924 (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
17925 "Org mode version 8.0")
17927 (defun org-hours-to-clocksum-string (n)
17928 (org-minutes-to-clocksum-string (* n 60)))
17930 (defun org-hh:mm-string-to-minutes (s)
17931 "Convert a string H:MM to a number of minutes.
17932 If the string is just a number, interpret it as minutes.
17933 In fact, the first hh:mm or number in the string will be taken,
17934 there can be extra stuff in the string.
17935 If no number is found, the return value is 0."
17938 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
17939 (+ (* (string-to-number (match-string 1 s)) 60)
17940 (string-to-number (match-string 2 s))))
17941 ((string-match "\\([0-9]+\\)" s)
17942 (string-to-number (match-string 1 s)))
17945 (defcustom org-image-actual-width t
17946 "Should we use the actual width of images when inlining them?
17948 When set to `t', always use the image width.
17950 When set to a number, use imagemagick (when available) to set
17951 the image's width to this value.
17953 When set to a number in a list, try to get the width from any
17954 #+ATTR.* keyword if it matches a width specification like
17956 #+ATTR_HTML: :width 300px
17958 and fall back on that number if none is found.
17960 When set to nil, try to get the width from an #+ATTR.* keyword
17961 and fall back on the original width if none is found.
17963 This requires Emacs >= 24.1, build with imagemagick support."
17964 :group 'org-appearance
17966 :package-version '(Org . "8.0")
17968 (const :tag "Use the image width" t)
17969 (integer :tag "Use a number of pixels")
17970 (list :tag "Use #+ATTR* or a number of pixels" (integer))
17971 (const :tag "Use #+ATTR* or don't resize" nil)))
17973 (defcustom org-agenda-inhibit-startup nil
17974 "Inhibit startup when preparing agenda buffers.
17975 When this variable is `t', the initialization of the Org agenda
17976 buffers is inhibited: e.g. the visibility state is not set, the
17977 tables are not re-aligned, etc."
17980 :group 'org-agenda)
17982 (define-obsolete-variable-alias
17983 'org-agenda-ignore-drawer-properties
17984 'org-agenda-ignore-properties "24.5")
17986 (defcustom org-agenda-ignore-properties nil
17987 "Avoid updating text properties when building the agenda.
17988 Properties are used to prepare buffers for effort estimates,
17989 appointments, statistics and subtree-local categories.
17990 If you don't use these in the agenda, you can add them to this
17991 list and agenda building will be a bit faster.
17992 The value is a list, with zero or more of the symbols `effort', `appt',
17993 `stats' or `category'."
17994 :type '(set :greedy t
18000 :package-version '(Org . "8.3")
18001 :group 'org-agenda)
18003 (defun org-duration-string-to-minutes (s &optional output-to-string)
18004 "Convert a duration string S to minutes.
18006 A bare number is interpreted as minutes, modifiers can be set by
18007 customizing `org-effort-durations' (which see).
18009 Entries containing a colon are interpreted as H:MM by
18010 `org-hh:mm-string-to-minutes'."
18012 (re (concat "\\([0-9.]+\\) *\\("
18013 (regexp-opt (mapcar 'car org-effort-durations))
18015 (while (string-match re s)
18016 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
18017 (string-to-number (match-string 1 s))))
18018 (setq s (replace-match "" nil t s)))
18019 (setq result (floor result))
18020 (incf result (org-hh:mm-string-to-minutes s))
18021 (if output-to-string (number-to-string result) result)))
18025 (defun org-save-all-org-buffers ()
18026 "Save all Org-mode buffers without user confirmation."
18028 (message "Saving all Org-mode buffers...")
18029 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
18030 (when (featurep 'org-id) (org-id-locations-save))
18031 (message "Saving all Org-mode buffers... done"))
18033 (defun org-revert-all-org-buffers ()
18034 "Revert all Org-mode buffers.
18035 Prompt for confirmation when there are unsaved changes.
18036 Be sure you know what you are doing before letting this function
18037 overwrite your changes.
18039 This function is useful in a setup where one tracks org files
18040 with a version control system, to revert on one machine after pulling
18041 changes from another. I believe the procedure must be like this:
18043 1. M-x org-save-all-org-buffers
18044 2. Pull changes from the other machine, resolve conflicts
18045 3. M-x org-revert-all-org-buffers"
18047 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
18048 (user-error "Abort"))
18050 (save-window-excursion
18053 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
18054 (with-current-buffer b buffer-file-name))
18055 (org-pop-to-buffer-same-window b)
18056 (revert-buffer t 'no-confirm)))
18058 (when (and (featurep 'org-id) org-id-track-globally)
18059 (org-id-locations-load)))))
18064 (defun org-switchb (&optional arg)
18065 "Switch between Org buffers.
18066 With one prefix argument, restrict available buffers to files.
18067 With two prefix arguments, restrict available buffers to agenda files.
18069 Defaults to `iswitchb' for buffer name completion.
18070 Set `org-completion-use-ido' to make it use ido instead."
18072 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
18073 ((equal arg '(16)) (org-buffer-list 'agenda))
18074 (t (org-buffer-list))))
18075 (org-completion-use-iswitchb org-completion-use-iswitchb)
18076 (org-completion-use-ido org-completion-use-ido))
18077 (unless (or org-completion-use-ido org-completion-use-iswitchb)
18078 (setq org-completion-use-iswitchb t))
18079 (org-pop-to-buffer-same-window
18080 (org-icompleting-read "Org buffer: "
18081 (mapcar 'list (mapcar 'buffer-name blist))
18084 ;;; Define some older names previously used for this functionality
18086 (defalias 'org-ido-switchb 'org-switchb)
18088 (defalias 'org-iswitchb 'org-switchb)
18090 (defun org-buffer-list (&optional predicate exclude-tmp)
18091 "Return a list of Org buffers.
18092 PREDICATE can be `export', `files' or `agenda'.
18094 export restrict the list to Export buffers.
18095 files restrict the list to buffers visiting Org files.
18096 agenda restrict the list to buffers visiting agenda files.
18098 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
18100 (agenda-files (and (eq predicate 'agenda)
18101 (mapcar 'file-truename (org-agenda-files t))))
18104 ((eq predicate 'files)
18105 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
18106 ((eq predicate 'export)
18107 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
18108 ((eq predicate 'agenda)
18110 (with-current-buffer b
18111 (and (derived-mode-p 'org-mode)
18112 (setq bfn (buffer-file-name b))
18113 (member (file-truename bfn) agenda-files)))))
18114 (t (lambda (b) (with-current-buffer b
18115 (or (derived-mode-p 'org-mode)
18116 (string-match "\*Org .*Export"
18117 (buffer-name b)))))))))
18121 (if (and (funcall filter b)
18122 (or (not exclude-tmp)
18123 (not (string-match "tmp" (buffer-name b)))))
18128 (defun org-agenda-files (&optional unrestricted archives)
18129 "Get the list of agenda files.
18130 Optional UNRESTRICTED means return the full list even if a restriction
18131 is currently in place.
18132 When ARCHIVES is t, include all archive files that are really being
18133 used by the agenda files. If ARCHIVE is `ifmode', do this only if
18134 `org-agenda-archives-mode' is t."
18137 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
18138 ((stringp org-agenda-files) (org-read-agenda-file-list))
18139 ((listp org-agenda-files) org-agenda-files)
18140 (t (error "Invalid value of `org-agenda-files'")))))
18141 (setq files (apply 'append
18142 (mapcar (lambda (f)
18143 (if (file-directory-p f)
18145 f t org-agenda-file-regexp)
18148 (when org-agenda-skip-unavailable-files
18149 (setq files (delq nil
18152 (and (file-readable-p file) file)))
18154 (when (or (eq archives t)
18155 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
18156 (setq files (org-add-archive-files files)))
18159 (defun org-agenda-file-p (&optional file)
18160 "Return non-nil, if FILE is an agenda file.
18161 If FILE is omitted, use the file associated with the current
18163 (member (or file (buffer-file-name))
18164 (org-agenda-files t)))
18166 (defun org-edit-agenda-file-list ()
18167 "Edit the list of agenda files.
18168 Depending on setup, this either uses customize to edit the variable
18169 `org-agenda-files', or it visits the file that is holding the list. In the
18170 latter case, the buffer is set up in a way that saving it automatically kills
18171 the buffer and restores the previous window configuration."
18173 (if (stringp org-agenda-files)
18174 (let ((cw (current-window-configuration)))
18175 (find-file org-agenda-files)
18176 (org-set-local 'org-window-configuration cw)
18177 (org-add-hook 'after-save-hook
18179 (set-window-configuration
18180 (prog1 org-window-configuration
18181 (kill-buffer (current-buffer))))
18182 (org-install-agenda-files-menu)
18183 (message "New agenda file list installed"))
18185 (message "%s" (substitute-command-keys
18186 "Edit list and finish with \\[save-buffer]")))
18187 (customize-variable 'org-agenda-files)))
18189 (defun org-store-new-agenda-file-list (list)
18190 "Set new value for the agenda file list and save it correctly."
18191 (if (stringp org-agenda-files)
18192 (let ((fe (org-read-agenda-file-list t)) b u)
18193 (while (setq b (find-buffer-visiting org-agenda-files))
18195 (with-temp-file org-agenda-files
18198 (lambda (f) ;; Keep un-expanded entries.
18199 (if (setq u (assoc f fe))
18204 (let ((org-mode-hook nil) (org-inhibit-startup t)
18205 (org-insert-mode-line-in-empty-file nil))
18206 (setq org-agenda-files list)
18207 (customize-save-variable 'org-agenda-files org-agenda-files))))
18209 (defun org-read-agenda-file-list (&optional pair-with-expansion)
18210 "Read the list of agenda files from a file.
18211 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
18212 filenames, used by `org-store-new-agenda-file-list' to write back
18213 un-expanded file names."
18214 (when (file-directory-p org-agenda-files)
18215 (error "`org-agenda-files' cannot be a single directory"))
18216 (when (stringp org-agenda-files)
18218 (insert-file-contents org-agenda-files)
18221 (let ((e (expand-file-name (substitute-in-file-name f)
18223 (if pair-with-expansion
18226 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
18229 (defun org-cycle-agenda-files ()
18230 "Cycle through the files in `org-agenda-files'.
18231 If the current buffer visits an agenda file, find the next one in the list.
18232 If the current buffer does not, find the first agenda file."
18234 (let* ((fs (org-agenda-files t))
18235 (files (append fs (list (car fs))))
18236 (tcf (if buffer-file-name (file-truename buffer-file-name)))
18238 (unless files (user-error "No agenda files"))
18240 (while (setq file (pop files))
18241 (if (equal (file-truename file) tcf)
18243 (find-file (car files))
18245 (find-file (car fs)))
18246 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
18248 (defun org-agenda-file-to-front (&optional to-end)
18249 "Move/add the current file to the top of the agenda file list.
18250 If the file is not present in the list, it is added to the front. If it is
18251 present, it is moved there. With optional argument TO-END, add/move to the
18254 (let ((org-agenda-skip-unavailable-files nil)
18255 (file-alist (mapcar (lambda (x)
18256 (cons (file-truename x) x))
18257 (org-agenda-files t)))
18258 (ctf (file-truename
18259 (or buffer-file-name
18260 (user-error "Please save the current buffer to a file"))))
18262 (setq x (assoc ctf file-alist) had x)
18264 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
18266 (setq file-alist (append (delq x file-alist) (list x)))
18267 (setq file-alist (cons x (delq x file-alist))))
18268 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
18269 (org-install-agenda-files-menu)
18270 (message "File %s to %s of agenda file list"
18271 (if had "moved" "added") (if to-end "end" "front"))))
18273 (defun org-remove-file (&optional file)
18274 "Remove current file from the list of files in variable `org-agenda-files'.
18275 These are the files which are being checked for agenda entries.
18276 Optional argument FILE means use this file instead of the current."
18278 (let* ((org-agenda-skip-unavailable-files nil)
18279 (file (or file buffer-file-name
18280 (user-error "Current buffer does not visit a file")))
18281 (true-file (file-truename file))
18282 (afile (abbreviate-file-name file))
18283 (files (delq nil (mapcar
18285 (if (equal true-file
18288 (org-agenda-files t)))))
18289 (if (not (= (length files) (length (org-agenda-files t))))
18291 (org-store-new-agenda-file-list files)
18292 (org-install-agenda-files-menu)
18293 (message "Removed file: %s" afile))
18294 (message "File was not in list: %s (not removed)" afile))))
18296 (defun org-file-menu-entry (file)
18297 (vector file (list 'find-file file) t))
18299 (defun org-check-agenda-file (file)
18300 "Make sure FILE exists. If not, ask user what to do."
18301 (when (not (file-exists-p file))
18302 (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
18303 (abbreviate-file-name file))
18304 (let ((r (downcase (read-char-exclusive))))
18307 (org-remove-file file)
18308 (throw 'nextfile t))
18309 (t (user-error "Abort"))))))
18311 (defun org-get-agenda-file-buffer (file)
18312 "Get an agenda buffer visiting FILE.
18313 If the buffer needs to be created, add it to the list of buffers
18314 which might be released later."
18315 (let ((buf (org-find-base-buffer-visiting file)))
18317 buf ; just return it
18318 ;; Make a new buffer and remember it
18319 (setq buf (find-file-noselect file))
18320 (if buf (push buf org-agenda-new-buffers))
18323 (defun org-release-buffers (blist)
18324 "Release all buffers in list, asking the user for confirmation when needed.
18325 When a buffer is unmodified, it is just killed. When modified, it is saved
18326 \(if the user agrees) and then killed."
18328 (while (setq buf (pop blist))
18329 (setq file (buffer-file-name buf))
18330 (when (and (buffer-modified-p buf)
18332 (y-or-n-p (format "Save file %s? " file)))
18333 (with-current-buffer buf (save-buffer)))
18334 (kill-buffer buf))))
18336 (defun org-agenda-prepare-buffers (files)
18337 "Create buffers for all agenda files, protect archived trees and comments."
18339 (let ((pa '(:org-archived t))
18340 (pc '(:org-comment t))
18341 (pall '(:org-archived t :org-comment t))
18342 (inhibit-read-only t)
18343 (org-inhibit-startup org-agenda-inhibit-startup)
18344 (rea (concat ":" org-archive-tag ":"))
18346 (setq org-tag-alist-for-agenda nil
18347 org-tag-groups-alist-for-agenda nil)
18350 (while (setq file (pop files))
18354 (org-check-agenda-file file)
18355 (set-buffer (org-get-agenda-file-buffer file)))
18357 (org-set-regexps-and-options-for-tags)
18359 (goto-char (point-min))
18360 (let ((case-fold-search t))
18361 (when (search-forward "#+setupfile" nil t)
18362 ;; Don't set all regexps and options systematically as
18363 ;; this is only run for setting agenda tags from setup
18365 (org-set-regexps-and-options)))
18366 (or (memq 'category org-agenda-ignore-properties)
18367 (org-refresh-category-properties))
18368 (or (memq 'stats org-agenda-ignore-properties)
18369 (org-refresh-stats-properties))
18370 (or (memq 'effort org-agenda-ignore-properties)
18371 (org-refresh-effort-properties))
18372 (or (memq 'appt org-agenda-ignore-properties)
18373 (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
18374 (setq org-todo-keywords-for-agenda
18375 (append org-todo-keywords-for-agenda org-todo-keywords-1))
18376 (setq org-done-keywords-for-agenda
18377 (append org-done-keywords-for-agenda org-done-keywords))
18378 (setq org-todo-keyword-alist-for-agenda
18379 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
18380 (setq org-tag-alist-for-agenda
18382 (append org-tag-alist-for-agenda
18384 org-tag-persistent-alist)))
18386 (setq org-tag-groups-alist-for-agenda
18387 (org-uniquify-alist
18388 (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
18389 (org-with-silent-modifications
18391 (remove-text-properties (point-min) (point-max) pall)
18392 (when org-agenda-skip-archived-trees
18393 (goto-char (point-min))
18394 (while (re-search-forward rea nil t)
18395 (if (org-at-heading-p t)
18396 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
18397 (goto-char (point-min))
18398 (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
18399 (while (re-search-forward re nil t)
18400 (when (save-match-data (org-in-commented-heading-p t))
18401 (add-text-properties
18402 (match-beginning 0) (org-end-of-subtree t) pc)))))
18403 (goto-char pos)))))
18404 (setq org-todo-keywords-for-agenda
18405 (org-uniquify org-todo-keywords-for-agenda))
18406 (setq org-todo-keyword-alist-for-agenda
18407 (org-uniquify org-todo-keyword-alist-for-agenda))))
18410 ;;;; CDLaTeX minor mode
18412 (defvar org-cdlatex-mode-map (make-sparse-keymap)
18413 "Keymap for the minor `org-cdlatex-mode'.")
18415 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
18416 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
18417 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
18418 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
18419 (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
18421 (defvar org-cdlatex-texmathp-advice-is-done nil
18422 "Flag remembering if we have applied the advice to texmathp already.")
18424 (define-minor-mode org-cdlatex-mode
18425 "Toggle the minor `org-cdlatex-mode'.
18426 This mode supports entering LaTeX environment and math in LaTeX fragments
18428 \\{org-cdlatex-mode-map}"
18430 (when org-cdlatex-mode
18432 (run-hooks 'cdlatex-mode-hook)
18433 (cdlatex-compute-tables))
18434 (unless org-cdlatex-texmathp-advice-is-done
18435 (setq org-cdlatex-texmathp-advice-is-done t)
18436 (defadvice texmathp (around org-math-always-on activate)
18437 "Always return t in org-mode buffers.
18438 This is because we want to insert math symbols without dollars even outside
18439 the LaTeX math segments. If Orgmode thinks that point is actually inside
18440 an embedded LaTeX fragment, let texmathp do its job.
18441 \\[org-cdlatex-mode-map]"
18445 ((not (derived-mode-p 'org-mode)) ad-do-it)
18446 ((eq this-command 'cdlatex-math-symbol)
18447 (setq ad-return-value t
18448 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
18450 (let ((p (org-inside-LaTeX-fragment-p)))
18451 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
18452 (setq ad-return-value t
18453 texmathp-why '("Org-mode embedded math" . 0))
18454 (if p ad-do-it)))))))))
18456 (defun turn-on-org-cdlatex ()
18457 "Unconditionally turn on `org-cdlatex-mode'."
18458 (org-cdlatex-mode 1))
18460 (defun org-try-cdlatex-tab ()
18461 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
18462 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
18463 - inside a LaTeX fragment, or
18464 - after the first word in a line, where an abbreviation expansion could
18465 insert a LaTeX environment."
18466 (when org-cdlatex-mode
18468 ;; Before any word on the line: No expansion possible.
18469 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
18470 ;; Just after first word on the line: Expand it. Make sure it
18471 ;; cannot happen on headlines, though.
18473 (skip-chars-backward "a-zA-Z0-9*")
18474 (skip-chars-backward " \t")
18475 (and (bolp) (not (org-at-heading-p))))
18477 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
18479 (defun org-cdlatex-underscore-caret (&optional arg)
18480 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
18481 Revert to the normal definition outside of these fragments."
18483 (if (org-inside-LaTeX-fragment-p)
18484 (call-interactively 'cdlatex-sub-superscript)
18485 (let (org-cdlatex-mode)
18486 (call-interactively (key-binding (vector last-input-event))))))
18488 (defun org-cdlatex-math-modify (&optional arg)
18489 "Execute `cdlatex-math-modify' in LaTeX fragments.
18490 Revert to the normal definition outside of these fragments."
18492 (if (org-inside-LaTeX-fragment-p)
18493 (call-interactively 'cdlatex-math-modify)
18494 (let (org-cdlatex-mode)
18495 (call-interactively (key-binding (vector last-input-event))))))
18497 (defun org-cdlatex-environment-indent (&optional environment item)
18498 "Execute `cdlatex-environment' and indent the inserted environment."
18500 (cdlatex-environment environment item)
18501 (let ((element (org-element-at-point)))
18502 (org-indent-region (org-element-property :begin element)
18503 (org-element-property :end element))))
18506 ;;;; LaTeX fragments
18508 (defun org-inside-LaTeX-fragment-p ()
18509 "Test if point is inside a LaTeX fragment.
18510 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
18511 sequence appearing also before point.
18512 Even though the matchers for math are configurable, this function assumes
18513 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
18514 delimiters are skipped when they have been removed by customization.
18515 The return value is nil, or a cons cell with the delimiter and the
18516 position of this delimiter.
18518 This function does a reasonably good job, but can locally be fooled by
18519 for example currency specifications. For example it will assume being in
18520 inline math after \"$22.34\". The LaTeX fragment formatter will only format
18521 fragments that are properly closed, but during editing, we have to live
18522 with the uncertainty caused by missing closing delimiters. This function
18523 looks only before point, not after."
18525 (let ((pos (point))
18526 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
18528 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
18530 dd-on str (start 0) m re)
18533 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
18534 re (nth 1 (assoc "$" org-latex-regexps)))
18535 (while (string-match re str start)
18537 ((= (match-end 0) (length str))
18538 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
18539 ((= (match-end 0) (- (length str) 5))
18541 (t (setq start (match-end 0))))))
18542 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
18544 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
18545 (and (match-beginning 2) (throw 'exit nil))
18547 (while (re-search-backward "\\$\\$" lim t)
18548 (setq dd-on (not dd-on)))
18550 (if dd-on (cons "$$" m))))))
18552 (defun org-inside-latex-macro-p ()
18553 "Is point inside a LaTeX macro or its arguments?"
18556 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
18558 (defvar org-latex-fragment-image-overlays nil
18559 "List of overlays carrying the images of latex fragments.")
18560 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
18562 (defun org-remove-latex-fragment-image-overlays ()
18563 "Remove all overlays with LaTeX fragment images in current buffer."
18564 (mapc 'delete-overlay org-latex-fragment-image-overlays)
18565 (setq org-latex-fragment-image-overlays nil))
18567 (define-obsolete-function-alias
18568 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
18569 (defun org-toggle-latex-fragment (&optional subtree)
18570 "Preview the LaTeX fragment at point, or all locally or globally.
18571 If the cursor is in a LaTeX fragment, create the image and overlay
18572 it over the source code. If there is no fragment at point, display
18573 all fragments in the current text, from one headline to the next. With
18574 prefix SUBTREE, display all fragments in the current subtree. With a
18575 double prefix arg \\[universal-argument] \\[universal-argument], or when \
18576 the cursor is before the first headline,
18577 display all fragments in the buffer.
18578 The images can be removed again with \\[org-toggle-latex-fragment]."
18580 (unless buffer-file-name
18581 (user-error "Can't preview LaTeX fragment in a non-file buffer"))
18582 (if org-latex-fragment-image-overlays
18583 (progn (org-remove-latex-fragment-image-overlays)
18584 (message "LaTeX fragments images removed"))
18585 (when (display-graphic-p)
18586 (org-remove-latex-fragment-image-overlays)
18589 (let (beg end at msg)
18591 ((or (equal subtree '(16))
18592 (not (save-excursion
18593 (re-search-backward org-outline-regexp-bol nil t))))
18594 (setq beg (point-min) end (point-max)
18595 msg "Creating images for buffer...%s"))
18596 ((equal subtree '(4))
18597 (org-back-to-heading)
18598 (setq beg (point) end (org-end-of-subtree t)
18599 msg "Creating images for subtree...%s"))
18601 (if (setq at (org-inside-LaTeX-fragment-p))
18602 (goto-char (max (point-min) (- (cdr at) 2)))
18603 (org-back-to-heading))
18604 (setq beg (point) end (progn (outline-next-heading) (point))
18605 msg (if at "Creating image...%s"
18606 "Creating images for entry...%s"))))
18608 (narrow-to-region beg end)
18611 (concat org-latex-preview-ltxpng-directory
18612 (file-name-sans-extension
18613 (file-name-nondirectory buffer-file-name)))
18614 default-directory 'overlays msg at 'forbuffer
18615 org-latex-create-formula-image-program)
18616 (message msg "done. Use `C-c C-x C-l' to remove images.")))))))
18618 (defun org-format-latex (prefix &optional dir overlays msg at
18619 forbuffer processing-type)
18620 "Replace LaTeX fragments with links to an image, and produce images.
18621 Some of the options can be changed using the variable
18622 `org-format-latex-options'."
18623 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
18624 (let* ((prefixnodir (file-name-nondirectory prefix))
18625 (absprefix (expand-file-name prefix dir))
18626 (todir (file-name-directory absprefix))
18627 (opt org-format-latex-options)
18628 (optnew org-format-latex-options)
18629 (matchers (plist-get opt :matchers))
18630 (re-list org-latex-regexps)
18631 (cnt 0) txt hash link beg end re e checkdir
18633 m n block-type block linkfile movefile ov)
18634 ;; Check the different regular expressions
18635 (while (setq e (pop re-list))
18636 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
18637 block (if block-type "\n\n" ""))
18638 (when (member m matchers)
18639 (goto-char (point-min))
18640 (while (re-search-forward re nil t)
18641 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
18643 (not (eq (get-char-property (match-beginning n)
18645 'org-latex-overlay))))
18647 ((eq processing-type 'verbatim))
18648 ((eq processing-type 'mathjax)
18649 ;; Prepare for MathJax processing.
18650 (setq string (match-string n))
18651 (when (member m '("$" "$1"))
18653 (delete-region (match-beginning n) (match-end n))
18654 (goto-char (match-beginning n))
18655 (insert (concat "\\(" (substring string 1 -1) "\\)")))))
18656 ((or (eq processing-type 'dvipng)
18657 (eq processing-type 'imagemagick))
18658 ;; Process to an image.
18659 (setq txt (match-string n)
18660 beg (match-beginning n) end (match-end n)
18662 (let ((face (face-at-point))
18663 (fg (plist-get opt :foreground))
18664 (bg (plist-get opt :background))
18665 ;; Ensure full list is printed.
18666 print-length print-level)
18668 ;; Get the colors from the face at point.
18670 (when (eq fg 'auto)
18671 (setq fg (face-attribute face :foreground nil 'default)))
18672 (when (eq bg 'auto)
18673 (setq bg (face-attribute face :background nil 'default)))
18674 (setq optnew (copy-sequence opt))
18675 (plist-put optnew :foreground fg)
18676 (plist-put optnew :background bg))
18677 (setq hash (sha1 (prin1-to-string
18678 (list org-format-latex-header
18679 org-latex-default-packages-alist
18680 org-latex-packages-alist
18681 org-format-latex-options
18682 forbuffer txt fg bg)))
18683 linkfile (format "%s_%s.png" prefix hash)
18684 movefile (format "%s_%s.png" absprefix hash)))
18685 (setq link (concat block "[[file:" linkfile "]]" block))
18686 (if msg (message msg cnt))
18688 (unless checkdir ; Ensure the directory exists.
18690 (or (file-directory-p todir) (make-directory todir t)))
18691 (unless (file-exists-p movefile)
18692 (org-create-formula-image
18693 txt movefile optnew forbuffer processing-type))
18697 (if (eq (overlay-get o 'org-overlay-type)
18698 'org-latex-overlay)
18699 (delete-overlay o)))
18700 (overlays-in beg end))
18701 (setq ov (make-overlay beg end))
18702 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
18703 (if (featurep 'xemacs)
18705 (overlay-put ov 'invisible t)
18708 (make-glyph (vector 'png :file movefile))))
18711 (list 'image :type 'png :file movefile :ascent 'center)))
18712 (push ov org-latex-fragment-image-overlays)
18714 (delete-region beg end)
18715 (insert (org-add-props link
18716 (list 'org-latex-src
18717 (replace-regexp-in-string
18719 'org-latex-src-embed-type
18720 (if block-type 'paragraph 'character))))))
18721 ((eq processing-type 'mathml)
18722 ;; Process to MathML
18723 (unless (save-match-data (org-format-latex-mathml-available-p))
18724 (user-error "LaTeX to MathML converter not configured"))
18725 (setq txt (match-string n)
18726 beg (match-beginning n) end (match-end n)
18728 (if msg (message msg cnt))
18730 (delete-region beg end)
18731 (insert (org-format-latex-as-mathml
18732 txt block-type prefix dir)))
18734 (error "Unknown conversion type %s for LaTeX fragments"
18735 processing-type)))))))))
18737 (defun org-create-math-formula (latex-frag &optional mathml-file)
18738 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
18739 Use `org-latex-to-mathml-convert-command'. If the conversion is
18740 sucessful, return the portion between \"<math...> </math>\"
18741 elements otherwise return nil. When MATHML-FILE is specified,
18742 write the results in to that file. When invoked as an
18743 interactive command, prompt for LATEX-FRAG, with initial value
18744 set to the current active region and echo the results for user
18746 (interactive (list (let ((frag (when (org-region-active-p)
18747 (buffer-substring-no-properties
18748 (region-beginning) (region-end)))))
18749 (read-string "LaTeX Fragment: " frag nil frag))))
18750 (unless latex-frag (user-error "Invalid LaTeX fragment"))
18751 (let* ((tmp-in-file (file-relative-name
18752 (make-temp-name (expand-file-name "ltxmathml-in"))))
18753 (ignore (write-region latex-frag nil tmp-in-file))
18754 (tmp-out-file (file-relative-name
18755 (make-temp-name (expand-file-name "ltxmathml-out"))))
18757 org-latex-to-mathml-convert-command
18758 `((?j . ,(shell-quote-argument
18759 (expand-file-name org-latex-to-mathml-jar-file)))
18760 (?I . ,(shell-quote-argument tmp-in-file))
18761 (?o . ,(shell-quote-argument tmp-out-file)))))
18762 mathml shell-command-output)
18763 (when (org-called-interactively-p 'any)
18764 (unless (org-format-latex-mathml-available-p)
18765 (user-error "LaTeX to MathML converter not configured")))
18766 (message "Running %s" cmd)
18767 (setq shell-command-output (shell-command-to-string cmd))
18769 (when (file-readable-p tmp-out-file)
18770 (with-current-buffer (find-file-noselect tmp-out-file t)
18771 (goto-char (point-min))
18772 (when (re-search-forward
18775 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
18777 (regexp-quote "</math>")) nil t)
18778 (prog1 (match-string 0) (kill-buffer))))))
18782 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
18784 (write-region mathml nil mathml-file))
18785 (when (org-called-interactively-p 'any)
18787 ((message "LaTeX to MathML conversion failed")
18788 (message shell-command-output)))
18789 (delete-file tmp-in-file)
18790 (when (file-exists-p tmp-out-file)
18791 (delete-file tmp-out-file))
18794 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
18795 prefix &optional dir)
18796 "Use `org-create-math-formula' but check local cache first."
18797 (let* ((absprefix (expand-file-name prefix dir))
18798 (print-length nil) (print-level nil)
18799 (formula-id (concat
18804 org-latex-to-mathml-convert-command)))))
18805 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
18806 (formula-cache-dir (file-name-directory formula-cache)))
18808 (unless (file-directory-p formula-cache-dir)
18809 (make-directory formula-cache-dir t))
18811 (unless (file-exists-p formula-cache)
18812 (org-create-math-formula latex-frag formula-cache))
18814 (if (file-exists-p formula-cache)
18815 ;; Successful conversion. Return the link to MathML file.
18817 (format "[[file:%s]]" (file-relative-name formula-cache dir))
18818 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
18819 'org-latex-src-embed-type (if latex-frag-type
18820 'paragraph 'character)))
18821 ;; Failed conversion. Return the LaTeX fragment verbatim
18824 (defun org-create-formula-image (string tofile options buffer &optional type)
18825 "Create an image from LaTeX source using dvipng or convert.
18826 This function calls either `org-create-formula-image-with-dvipng'
18827 or `org-create-formula-image-with-imagemagick' depending on the
18828 value of `org-latex-create-formula-image-program' or on the value
18829 of the optional TYPE variable.
18831 Note: ultimately these two function should be combined as they
18832 share a good deal of logic."
18833 (org-check-external-command
18834 "latex" "needed to convert LaTeX fragments to images")
18836 (case (or type org-latex-create-formula-image-program)
18838 (org-check-external-command
18839 "dvipng" "needed to convert LaTeX fragments to images")
18840 'org-create-formula-image-with-dvipng)
18842 (org-check-external-command
18843 "convert" "you need to install imagemagick")
18844 'org-create-formula-image-with-imagemagick)
18846 "Invalid value of `org-latex-create-formula-image-program'")))
18847 string tofile options buffer))
18849 (declare-function org-export-get-backend "ox" (name))
18850 (declare-function org-export--get-global-options "ox" (&optional backend))
18851 (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
18852 (declare-function org-latex-guess-inputenc "ox-latex" (header))
18853 (declare-function org-latex-guess-babel-language "ox-latex" (header info))
18854 (defun org-create-formula--latex-header ()
18855 "Return LaTeX header appropriate for previewing a LaTeX snippet."
18856 (let ((info (org-combine-plists (org-export--get-global-options
18857 (org-export-get-backend 'latex))
18858 (org-export--get-inbuffer-options
18859 (org-export-get-backend 'latex)))))
18860 (org-latex-guess-babel-language
18861 (org-latex-guess-inputenc
18862 (org-splice-latex-header
18863 org-format-latex-header
18864 org-latex-default-packages-alist
18865 org-latex-packages-alist t
18866 (plist-get info :latex-header)))
18869 ;; This function borrows from Ganesh Swami's latex2png.el
18870 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
18871 "This calls dvipng."
18872 (require 'ox-latex)
18873 (let* ((tmpdir (if (featurep 'xemacs)
18875 temporary-file-directory))
18876 (texfilebase (make-temp-name
18877 (expand-file-name "orgtex" tmpdir)))
18878 (texfile (concat texfilebase ".tex"))
18879 (dvifile (concat texfilebase ".dvi"))
18880 (pngfile (concat texfilebase ".png"))
18881 (fnh (if (featurep 'xemacs)
18882 (font-height (face-font 'default))
18883 (face-attribute 'default :height nil)))
18884 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
18885 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
18886 (fg (or (plist-get options (if buffer :foreground :html-foreground))
18888 (bg (or (plist-get options (if buffer :background :html-background))
18890 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
18891 (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
18892 (if (eq bg 'default) (setq bg (org-dvipng-color :background))
18893 (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
18894 (let ((latex-header (org-create-formula--latex-header)))
18895 (with-temp-file texfile
18896 (insert latex-header)
18897 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
18898 (let ((dir default-directory))
18901 (call-process "latex" nil nil nil texfile))
18903 (if (not (file-exists-p dvifile))
18904 (progn (message "Failed to create dvi file from %s" texfile) nil)
18906 (if (featurep 'xemacs)
18907 (call-process "dvipng" nil nil nil
18912 (call-process "dvipng" nil nil nil
18915 ;;"-x" scale "-y" scale
18919 (if (not (file-exists-p pngfile))
18920 (if org-format-latex-signal-error
18921 (error "Failed to create png file from %s" texfile)
18922 (message "Failed to create png file from %s" texfile)
18924 ;; Use the requested file name and clean up
18925 (copy-file pngfile tofile 'replace)
18926 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
18927 (if (file-exists-p (concat texfilebase e))
18928 (delete-file (concat texfilebase e))))
18931 (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
18932 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
18933 "This calls convert, which is included into imagemagick."
18934 (require 'ox-latex)
18935 (let* ((tmpdir (if (featurep 'xemacs)
18937 temporary-file-directory))
18938 (texfilebase (make-temp-name
18939 (expand-file-name "orgtex" tmpdir)))
18940 (texfile (concat texfilebase ".tex"))
18941 (pdffile (concat texfilebase ".pdf"))
18942 (pngfile (concat texfilebase ".png"))
18943 (fnh (if (featurep 'xemacs)
18944 (font-height (face-font 'default))
18945 (face-attribute 'default :height nil)))
18946 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
18947 (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
18948 (fg (or (plist-get options (if buffer :foreground :html-foreground))
18950 (bg (or (plist-get options (if buffer :background :html-background))
18952 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
18953 (setq fg (org-latex-color-format fg)))
18954 (if (eq bg 'default) (setq bg (org-latex-color :background))
18955 (setq bg (org-latex-color-format
18956 (if (string= bg "Transparent") "white" bg))))
18957 (let ((latex-header (org-create-formula--latex-header)))
18958 (with-temp-file texfile
18959 (insert latex-header)
18960 (insert "\n\\begin{document}\n"
18961 "\\definecolor{fg}{rgb}{" fg "}\n"
18962 "\\definecolor{bg}{rgb}{" bg "}\n"
18963 "\n\\pagecolor{bg}\n"
18967 "\n\\end{document}\n")))
18968 (org-latex-compile texfile t)
18969 (if (not (file-exists-p pdffile))
18970 (progn (message "Failed to create pdf file from %s" texfile) nil)
18972 (if (featurep 'xemacs)
18973 (call-process "convert" nil nil nil
18979 ;; "-sharpen" "0x1.0"
18981 (call-process "convert" nil nil nil
18987 ;; "-sharpen" "0x1.0"
18989 (if (not (file-exists-p pngfile))
18990 (if org-format-latex-signal-error
18991 (error "Failed to create png file from %s" texfile)
18992 (message "Failed to create png file from %s" texfile)
18994 ;; Use the requested file name and clean up
18995 (copy-file pngfile tofile 'replace)
18996 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
18997 (if (file-exists-p (concat texfilebase e))
18998 (delete-file (concat texfilebase e))))
19001 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
19002 "Fill a LaTeX header template TPL.
19003 In the template, the following place holders will be recognized:
19005 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
19006 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
19007 [PACKAGES] \\usepackage statements for PKG
19008 [NO-PACKAGES] do not include PKG
19009 [EXTRA] the string EXTRA
19010 [NO-EXTRA] do not include EXTRA
19012 For backward compatibility, if both the positive and the negative place
19013 holder is missing, the positive one (without the \"NO-\") will be
19014 assumed to be present at the end of the template.
19015 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
19017 SNIPPETS-P indicates if this is run to create snippet images for HTML."
19018 (let (rpl (end ""))
19019 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
19020 (setq rpl (if (or (match-end 1) (not def-pkg))
19021 "" (org-latex-packages-to-string def-pkg snippets-p t))
19022 tpl (replace-match rpl t t tpl))
19023 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
19025 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
19026 (setq rpl (if (or (match-end 1) (not pkg))
19027 "" (org-latex-packages-to-string pkg snippets-p t))
19028 tpl (replace-match rpl t t tpl))
19031 (org-latex-packages-to-string pkg snippets-p)))))
19033 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
19034 (setq rpl (if (or (match-end 1) (not extra))
19035 "" (concat extra "\n"))
19036 tpl (replace-match rpl t t tpl))
19037 (if (and extra (string-match "\\S-" extra))
19038 (setq end (concat end "\n" extra))))
19040 (if (string-match "\\S-" end)
19041 (concat tpl "\n" end)
19044 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
19045 "Turn an alist of packages into a string with the \\usepackage macros."
19046 (setq pkg (mapconcat (lambda(p)
19049 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
19050 (format "%% Package %s omitted" (cadr p)))
19051 ((equal "" (car p))
19052 (format "\\usepackage{%s}" (cadr p)))
19054 (format "\\usepackage[%s]{%s}"
19055 (car p) (cadr p)))))
19058 (if newline (concat pkg "\n") pkg))
19060 (defun org-dvipng-color (attr)
19061 "Return a RGB color specification for dvipng."
19062 (apply 'format "rgb %s %s %s"
19063 (mapcar 'org-normalize-color
19064 (if (featurep 'xemacs)
19065 (color-rgb-components
19066 (face-property 'default
19067 (cond ((eq attr :foreground) 'foreground)
19068 ((eq attr :background) 'background))))
19069 (color-values (face-attribute 'default attr nil))))))
19071 (defun org-dvipng-color-format (color-name)
19072 "Convert COLOR-NAME to a RGB color value for dvipng."
19073 (apply 'format "rgb %s %s %s"
19074 (mapcar 'org-normalize-color
19075 (color-values color-name))))
19077 (defun org-latex-color (attr)
19078 "Return a RGB color for the LaTeX color package."
19079 (apply 'format "%s,%s,%s"
19080 (mapcar 'org-normalize-color
19081 (if (featurep 'xemacs)
19082 (color-rgb-components
19083 (face-property 'default
19084 (cond ((eq attr :foreground) 'foreground)
19085 ((eq attr :background) 'background))))
19086 (color-values (face-attribute 'default attr nil))))))
19088 (defun org-latex-color-format (color-name)
19089 "Convert COLOR-NAME to a RGB color value."
19090 (apply 'format "%s,%s,%s"
19091 (mapcar 'org-normalize-color
19092 (color-values color-name))))
19094 (defun org-normalize-color (value)
19095 "Return string to be used as color value for an RGB component."
19096 (format "%g" (/ value 65535.0)))
19102 (defvar org-inline-image-overlays nil)
19103 (make-variable-buffer-local 'org-inline-image-overlays)
19105 (defun org-toggle-inline-images (&optional include-linked)
19106 "Toggle the display of inline images.
19107 INCLUDE-LINKED is passed to `org-display-inline-images'."
19109 (if org-inline-image-overlays
19111 (org-remove-inline-images)
19112 (message "Inline image display turned off"))
19113 (org-display-inline-images include-linked)
19114 (if (and (org-called-interactively-p)
19115 org-inline-image-overlays)
19116 (message "%d images displayed inline"
19117 (length org-inline-image-overlays))
19118 (message "No images to display inline"))))
19120 (defun org-redisplay-inline-images ()
19121 "Refresh the display of inline images."
19123 (if (not org-inline-image-overlays)
19124 (org-toggle-inline-images)
19125 (org-toggle-inline-images)
19126 (org-toggle-inline-images)))
19128 (defun org-display-inline-images (&optional include-linked refresh beg end)
19129 "Display inline images.
19131 An inline image is a link which follows either of these
19134 1. Its path is a file with an extension matching return value
19135 from `image-file-name-regexp' and it has no contents.
19137 2. Its description consists in a single link of the previous
19140 When optional argument INCLUDE-LINKED is non-nil, also links with
19141 a text description part will be inlined. This can be nice for
19142 a quick look at those images, but it does not reflect what
19143 exported files will look like.
19145 When optional argument REFRESH is non-nil, refresh existing
19146 images between BEG and END. This will create new image displays
19147 only if necessary. BEG and END default to the buffer
19150 (when (display-graphic-p)
19152 (org-remove-inline-images)
19153 (when (fboundp 'clear-image-cache) (clear-image-cache)))
19154 (org-with-wide-buffer
19155 (goto-char (or beg (point-min)))
19156 (let ((case-fold-search t)
19157 (file-extension-re (org-image-file-name-regexp)))
19158 (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
19159 (let ((link (save-match-data (org-element-context))))
19160 ;; Check if we're at an inline image.
19161 (when (and (equal (org-element-property :type link) "file")
19163 (not (org-element-property :contents-begin link)))
19164 (let ((parent (org-element-property :parent link)))
19165 (or (not (eq (org-element-type parent) 'link))
19166 (not (cdr (org-element-contents parent)))))
19167 (org-string-match-p file-extension-re
19168 (org-element-property :path link)))
19169 (let ((file (expand-file-name (org-element-property :path link))))
19170 (when (file-exists-p file)
19172 ;; Apply `org-image-actual-width' specifications.
19174 ((not (image-type-available-p 'imagemagick)) nil)
19175 ((eq org-image-actual-width t) nil)
19176 ((listp org-image-actual-width)
19178 ;; First try to find a width among
19179 ;; attributes associated to the paragraph
19180 ;; containing link.
19183 (while (and (setq e (org-element-property
19185 (not (eq (org-element-type e)
19190 (goto-char (org-element-property :begin paragraph))
19193 "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
19194 (org-element-property
19195 :post-affiliated paragraph)
19197 (string-to-number (match-string 1))))))
19198 ;; Otherwise, fall-back to provided number.
19199 (car org-image-actual-width)))
19200 ((numberp org-image-actual-width)
19201 org-image-actual-width)))
19202 (old (get-char-property-and-overlay
19203 (org-element-property :begin link)
19204 'org-image-overlay)))
19205 (if (and (car-safe old) refresh)
19206 (image-refresh (overlay-get (cdr old) 'display))
19207 (let ((image (create-image file
19208 (and width 'imagemagick)
19213 ;; If inline image is the description
19214 ;; of another link, be sure to
19215 ;; consider the latter as the one to
19216 ;; apply the overlay on.
19218 (org-element-property :parent link)))
19219 (if (eq (org-element-type parent) 'link)
19223 (org-element-property :begin link)
19226 (org-element-property :end link))
19227 (skip-chars-backward " \t")
19229 (overlay-put ov 'display image)
19230 (overlay-put ov 'face 'default)
19231 (overlay-put ov 'org-image-overlay t)
19233 ov 'modification-hooks
19234 (list 'org-display-inline-remove-overlay))
19235 (push ov org-inline-image-overlays)))))))))))))))
19237 (define-obsolete-function-alias
19238 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
19240 (defun org-display-inline-remove-overlay (ov after beg end &optional len)
19241 "Remove inline-display overlay if a corresponding region is modified."
19242 (let ((inhibit-modification-hooks t))
19243 (when (and ov after)
19244 (delete ov org-inline-image-overlays)
19245 (delete-overlay ov))))
19247 (defun org-remove-inline-images ()
19248 "Remove inline display of images."
19250 (mapc 'delete-overlay org-inline-image-overlays)
19251 (setq org-inline-image-overlays nil))
19255 ;; Outline functions from `outline-mode-prefix-map'
19256 ;; that can be remapped in Org:
19257 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
19258 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
19259 (define-key org-mode-map [remap outline-forward-same-level]
19260 'org-forward-heading-same-level)
19261 (define-key org-mode-map [remap outline-backward-same-level]
19262 'org-backward-heading-same-level)
19263 (define-key org-mode-map [remap show-branches]
19264 'org-kill-note-or-show-branches)
19265 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
19266 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
19267 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
19269 ;; Outline functions from `outline-mode-prefix-map' that can not
19270 ;; be remapped in Org:
19272 ;; - the column "key binding" shows whether the Outline function is still
19273 ;; available in Org mode on the same key that it has been bound to in
19275 ;; - "overridden": key used for a different functionality in Org mode
19276 ;; - else: key still bound to the same Outline function in Org mode
19278 ;; | Outline function | key binding | Org replacement |
19279 ;; |------------------------------------+-------------+-----------------------|
19280 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
19281 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
19282 ;; | `outline-up-heading' | `C-c C-u' | still same function |
19283 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
19284 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
19285 ;; | `show-entry' | overridden | no replacement |
19286 ;; | `show-children' | `C-c C-i' | visibility cycling |
19287 ;; | `show-branches' | `C-c C-k' | still same function |
19288 ;; | `show-subtree' | overridden | visibility cycling |
19289 ;; | `show-all' | overridden | no replacement |
19290 ;; | `hide-subtree' | overridden | visibility cycling |
19291 ;; | `hide-body' | overridden | no replacement |
19292 ;; | `hide-entry' | overridden | visibility cycling |
19293 ;; | `hide-leaves' | overridden | no replacement |
19294 ;; | `hide-sublevels' | overridden | no replacement |
19295 ;; | `hide-other' | overridden | no replacement |
19297 ;; Make `C-c C-x' a prefix key
19298 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
19300 ;; TAB key with modifiers
19301 (org-defkey org-mode-map "\C-i" 'org-cycle)
19302 (org-defkey org-mode-map [(tab)] 'org-cycle)
19303 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
19304 (org-defkey org-mode-map "\M-\t" 'pcomplete)
19305 ;; The following line is necessary under Suse GNU/Linux
19306 (unless (featurep 'xemacs)
19307 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
19308 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
19309 (define-key org-mode-map [backtab] 'org-shifttab)
19311 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
19312 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
19313 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
19315 ;; Cursor keys with modifiers
19316 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
19317 (org-defkey org-mode-map [(meta right)] 'org-metaright)
19318 (org-defkey org-mode-map [(meta up)] 'org-metaup)
19319 (org-defkey org-mode-map [(meta down)] 'org-metadown)
19321 (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
19322 (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
19323 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
19324 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
19325 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
19326 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
19328 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
19329 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
19330 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
19331 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
19333 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
19334 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
19335 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
19336 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
19339 (define-key org-mode-map org-babel-key-prefix org-babel-map)
19340 (mapc (lambda (pair)
19341 (define-key org-babel-map (car pair) (cdr pair)))
19342 org-babel-key-bindings)
19344 ;;; Extra keys for tty access.
19345 ;; We only set them when really needed because otherwise the
19346 ;; menus don't show the simple keys
19348 (when (or org-use-extra-keys
19349 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
19350 (not window-system))
19351 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
19352 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
19353 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
19354 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
19355 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
19356 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
19357 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
19358 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
19359 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
19360 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
19361 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
19362 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
19363 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
19364 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
19365 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
19366 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
19367 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
19368 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
19369 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
19370 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
19371 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
19372 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
19373 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
19374 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
19375 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
19376 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
19377 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
19378 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
19380 ;; All the other keys
19382 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
19383 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
19384 (if (boundp 'narrow-map)
19385 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
19386 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
19387 (if (boundp 'narrow-map)
19388 (org-defkey narrow-map "b" 'org-narrow-to-block)
19389 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
19390 (if (boundp 'narrow-map)
19391 (org-defkey narrow-map "e" 'org-narrow-to-element)
19392 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
19393 (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
19394 (org-defkey org-mode-map "\M-}" 'org-forward-element)
19395 (org-defkey org-mode-map "\M-{" 'org-backward-element)
19396 (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
19397 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
19398 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
19399 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
19400 (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
19401 (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
19402 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
19403 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
19404 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
19405 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
19406 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
19407 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
19408 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
19409 (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
19410 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
19411 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
19412 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
19413 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
19414 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
19415 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
19416 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
19417 (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
19418 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
19419 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
19420 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
19421 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
19422 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
19423 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
19424 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
19425 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
19426 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
19427 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
19428 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
19429 (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
19430 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
19431 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
19432 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
19433 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
19434 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
19435 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
19436 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
19437 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
19438 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
19439 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
19440 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
19441 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
19442 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
19443 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
19444 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
19445 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
19446 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
19447 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
19448 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
19449 (org-defkey org-mode-map "\C-c^" 'org-sort)
19450 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
19451 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
19452 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
19453 (org-defkey org-mode-map [remap open-line] 'org-open-line)
19454 (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
19455 (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
19456 (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
19457 (org-defkey org-mode-map "\C-m" 'org-return)
19458 (org-defkey org-mode-map "\C-j" 'org-return-indent)
19459 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
19460 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
19461 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
19462 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
19463 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
19464 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
19465 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
19466 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
19467 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
19468 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
19469 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
19470 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
19471 (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
19472 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
19473 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
19474 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
19475 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
19476 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
19477 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
19478 (org-defkey org-mode-map "\M-h" 'org-mark-element)
19479 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
19480 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
19482 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
19483 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
19484 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
19486 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
19487 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
19488 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
19489 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
19490 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
19491 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
19492 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
19493 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
19494 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
19495 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
19496 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
19497 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
19498 (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
19499 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
19500 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
19501 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
19502 (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
19503 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
19504 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
19505 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
19506 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
19507 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
19508 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
19510 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
19511 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
19512 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
19513 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
19514 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
19516 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
19518 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
19520 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
19521 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
19523 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
19526 (when (featurep 'xemacs)
19527 (org-defkey org-mode-map 'button3 'popup-mode-menu))
19530 (defconst org-speed-commands-default
19532 ("Outline Navigation")
19533 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
19534 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
19535 ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
19536 ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
19537 ("F" . org-next-block)
19538 ("B" . org-previous-block)
19539 ("u" . (org-speed-move-safe 'outline-up-heading))
19541 ("g" . (org-refile t))
19542 ("Outline Visibility")
19544 ("C" . org-shifttab)
19545 (" " . org-display-outline-path)
19546 ("s" . org-narrow-to-subtree)
19547 ("=" . org-columns)
19548 ("Outline Structure Editing")
19550 ("D" . org-metadown)
19551 ("r" . org-metaright)
19552 ("l" . org-metaleft)
19553 ("R" . org-shiftmetaright)
19554 ("L" . org-shiftmetaleft)
19555 ("i" . (progn (forward-char 1) (call-interactively
19556 'org-insert-heading-respect-content)))
19559 ("a" . org-archive-subtree-default-with-confirmation)
19560 ("@" . org-mark-subtree)
19561 ("#" . org-toggle-comment)
19563 ("I" . org-clock-in)
19564 ("O" . org-clock-out)
19565 ("Meta Data Editing")
19567 ("," . (org-priority))
19568 ("0" . (org-priority ?\ ))
19569 ("1" . (org-priority ?A))
19570 ("2" . (org-priority ?B))
19571 ("3" . (org-priority ?C))
19572 (":" . org-set-tags-command)
19573 ("e" . org-set-effort)
19574 ("E" . org-inc-effort)
19575 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
19576 (org-entry-put (point) "APPT_WARNTIME" m)))
19577 ("Agenda Views etc")
19579 ("/" . org-sparse-tree)
19581 ("o" . org-open-at-point)
19582 ("?" . org-speed-command-help)
19583 ("<" . (org-agenda-set-restriction-lock 'subtree))
19584 (">" . (org-agenda-remove-restriction-lock))
19586 "The default speed commands.")
19588 (defun org-print-speed-command (e)
19589 (if (> (length (car e)) 1)
19594 (princ (make-string (length (car e)) ?-))
19598 (if (symbolp (cdr e))
19599 (princ (symbol-name (cdr e)))
19603 (defun org-speed-command-help ()
19604 "Show the available speed commands."
19606 (if (not org-use-speed-commands)
19607 (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
19608 (with-output-to-temp-buffer "*Help*"
19609 (princ "User-defined Speed commands\n===========================\n")
19610 (mapc 'org-print-speed-command org-speed-commands-user)
19612 (princ "Built-in Speed commands\n=======================\n")
19613 (mapc 'org-print-speed-command org-speed-commands-default))
19614 (with-current-buffer "*Help*"
19615 (setq truncate-lines t))))
19617 (defun org-speed-move-safe (cmd)
19618 "Execute CMD, but make sure that the cursor always ends up in a headline.
19619 If not, return to the original position and throw an error."
19621 (let ((pos (point)))
19622 (call-interactively cmd)
19623 (unless (and (bolp) (org-at-heading-p))
19625 (error "Boundary reached while executing %s" cmd))))
19627 (defvar org-self-insert-command-undo-counter 0)
19629 (defvar org-table-auto-blank-field) ; defined in org-table.el
19630 (defvar org-speed-command nil)
19632 (define-obsolete-function-alias
19633 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
19635 (defun org-speed-command-activate (keys)
19636 "Hook for activating single-letter speed commands.
19637 `org-speed-commands-default' specifies a minimal command set.
19638 Use `org-speed-commands-user' for further customization."
19639 (when (or (and (bolp) (looking-at org-outline-regexp))
19640 (and (functionp org-use-speed-commands)
19641 (funcall org-use-speed-commands)))
19642 (cdr (assoc keys (append org-speed-commands-user
19643 org-speed-commands-default)))))
19645 (define-obsolete-function-alias
19646 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
19648 (defun org-babel-speed-command-activate (keys)
19649 "Hook for activating single-letter code block commands."
19650 (when (and (bolp) (looking-at org-babel-src-block-regexp))
19651 (cdr (assoc keys org-babel-key-bindings))))
19653 (defcustom org-speed-command-hook
19654 '(org-speed-command-default-hook org-babel-speed-command-hook)
19655 "Hook for activating speed commands at strategic locations.
19656 Hook functions are called in sequence until a valid handler is
19659 Each hook takes a single argument, a user-pressed command key
19660 which is also a `self-insert-command' from the global map.
19662 Within the hook, examine the cursor position and the command key
19663 and return nil or a valid handler as appropriate. Handler could
19664 be one of an interactive command, a function, or a form.
19666 Set `org-use-speed-commands' to non-nil value to enable this
19667 hook. The default setting is `org-speed-command-activate'."
19668 :group 'org-structure
19672 (defun org-self-insert-command (N)
19673 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
19674 If the cursor is in a table looking at whitespace, the whitespace is
19675 overwritten, and the table is not marked as requiring realignment."
19677 (org-check-before-invisible-edit 'insert)
19679 ((and org-use-speed-commands
19680 (setq org-speed-command
19681 (run-hook-with-args-until-success
19682 'org-speed-command-hook (this-command-keys))))
19684 ((commandp org-speed-command)
19685 (setq this-command org-speed-command)
19686 (call-interactively org-speed-command))
19687 ((functionp org-speed-command)
19688 (funcall org-speed-command))
19689 ((and org-speed-command (listp org-speed-command))
19690 (eval org-speed-command))
19691 (t (let (org-use-speed-commands)
19692 (call-interactively 'org-self-insert-command)))))
19696 ;; check if we blank the field, and if that triggers align
19697 (and (featurep 'org-table) org-table-auto-blank-field
19698 (member last-command
19699 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas-expand))
19700 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
19701 ;; got extra space, this field does not determine column width
19702 (let (org-table-may-need-update) (org-table-blank-field))
19703 ;; no extra space, this field may determine column width
19704 (org-table-blank-field)))
19707 (looking-at "[^|\n]* |"))
19708 (let (org-table-may-need-update)
19709 (goto-char (1- (match-end 0)))
19710 (backward-delete-char 1)
19711 (goto-char (match-beginning 0))
19712 (self-insert-command N)))
19714 (setq org-table-may-need-update t)
19715 (self-insert-command N)
19716 (org-fix-tags-on-the-fly)
19717 (if org-self-insert-cluster-for-undo
19718 (if (not (eq last-command 'org-self-insert-command))
19719 (setq org-self-insert-command-undo-counter 1)
19720 (if (>= org-self-insert-command-undo-counter 20)
19721 (setq org-self-insert-command-undo-counter 1)
19722 (and (> org-self-insert-command-undo-counter 0)
19723 buffer-undo-list (listp buffer-undo-list)
19724 (not (cadr buffer-undo-list)) ; remove nil entry
19725 (setcdr buffer-undo-list (cddr buffer-undo-list)))
19726 (setq org-self-insert-command-undo-counter
19727 (1+ org-self-insert-command-undo-counter))))))))
19729 (defun org-check-before-invisible-edit (kind)
19730 "Check is editing if kind KIND would be dangerous with invisible text around.
19731 The detailed reaction depends on the user option `org-catch-invisible-edits'."
19732 ;; First, try to get out of here as quickly as possible, to reduce overhead
19733 (if (and org-catch-invisible-edits
19734 (or (not (boundp 'visible-mode)) (not visible-mode))
19735 (or (get-char-property (point) 'invisible)
19736 (get-char-property (max (point-min) (1- (point))) 'invisible)))
19737 ;; OK, we need to take a closer look
19738 (let* ((invisible-at-point (get-char-property (point) 'invisible))
19739 (invisible-before-point (if (bobp) nil (get-char-property
19740 (1- (point)) 'invisible)))
19741 (border-and-ok-direction
19743 ;; Check if we are acting predictably before invisible text
19744 (and invisible-at-point (not invisible-before-point)
19745 (memq kind '(insert delete-backward)))
19746 ;; Check if we are acting predictably after invisible text
19747 ;; This works not well, and I have turned it off. It seems
19748 ;; better to always show and stop after invisible text.
19749 ;; (and (not invisible-at-point) invisible-before-point
19750 ;; (memq kind '(insert delete)))
19752 (when (or (memq invisible-at-point '(outline org-hide-block t))
19753 (memq invisible-before-point '(outline org-hide-block t)))
19754 (if (eq org-catch-invisible-edits 'error)
19755 (user-error "Editing in invisible areas is prohibited, make them visible first"))
19756 (if (and org-custom-properties-overlays
19757 (y-or-n-p "Display invisible properties in this buffer? "))
19758 (org-toggle-custom-properties-visibility)
19759 ;; Make the area visible
19761 (if invisible-before-point
19762 (goto-char (previous-single-char-property-change
19763 (point) 'invisible)))
19766 ((eq org-catch-invisible-edits 'show)
19767 ;; That's it, we do the edit after showing
19769 "Unfolding invisible region around point before editing")
19771 ((and (eq org-catch-invisible-edits 'smart)
19772 border-and-ok-direction)
19773 (message "Unfolding invisible region around point before editing"))
19775 ;; Don't do the edit, make the user repeat it in full visibility
19776 (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
19778 (defun org-fix-tags-on-the-fly ()
19779 (when (and (equal (char-after (point-at-bol)) ?*)
19780 (org-at-heading-p))
19781 (org-align-tags-here org-tags-column)))
19783 (defun org-delete-backward-char (N)
19784 "Like `delete-backward-char', insert whitespace at field end in tables.
19785 When deleting backwards, in tables this function will insert whitespace in
19786 front of the next \"|\" separator, to keep the table aligned. The table will
19787 still be marked for re-alignment if the field did fill the entire column,
19788 because, in this case the deletion might narrow the column."
19791 (org-check-before-invisible-edit 'delete-backward)
19792 (if (and (org-table-p)
19794 (string-match "|" (buffer-substring (point-at-bol) (point)))
19795 (looking-at ".*?|"))
19796 (let ((pos (point))
19797 (noalign (looking-at "[^|\n\r]* |"))
19798 (c org-table-may-need-update))
19799 (backward-delete-char N)
19800 (if (not overwrite-mode)
19802 (skip-chars-forward "^|")
19804 (goto-char (1- pos))))
19805 ;; noalign: if there were two spaces at the end, this field
19806 ;; does not determine the width of the column.
19807 (if noalign (setq org-table-may-need-update c)))
19808 (backward-delete-char N)
19809 (org-fix-tags-on-the-fly))))
19811 (defun org-delete-char (N)
19812 "Like `delete-char', but insert whitespace at field end in tables.
19813 When deleting characters, in tables this function will insert whitespace in
19814 front of the next \"|\" separator, to keep the table aligned. The table will
19815 still be marked for re-alignment if the field did fill the entire column,
19816 because, in this case the deletion might narrow the column."
19819 (org-check-before-invisible-edit 'delete)
19820 (if (and (org-table-p)
19822 (not (= (char-after) ?|))
19824 (if (looking-at ".*?|")
19825 (let ((pos (point))
19826 (noalign (looking-at "[^|\n\r]* |"))
19827 (c org-table-may-need-update))
19829 (concat (substring (match-string 0) 1 -1) " |") nil t)
19831 ;; noalign: if there were two spaces at the end, this field
19832 ;; does not determine the width of the column.
19833 (if noalign (setq org-table-may-need-update c)))
19836 (org-fix-tags-on-the-fly))))
19838 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
19839 (put 'org-self-insert-command 'delete-selection
19841 (not (run-hook-with-args-until-success
19842 'self-insert-uses-region-functions))))
19843 (put 'orgtbl-self-insert-command 'delete-selection
19845 (not (run-hook-with-args-until-success
19846 'self-insert-uses-region-functions))))
19847 (put 'org-delete-char 'delete-selection 'supersede)
19848 (put 'org-delete-backward-char 'delete-selection 'supersede)
19849 (put 'org-yank 'delete-selection 'yank)
19851 ;; Make `flyspell-mode' delay after some commands
19852 (put 'org-self-insert-command 'flyspell-delayed t)
19853 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
19854 (put 'org-delete-char 'flyspell-delayed t)
19855 (put 'org-delete-backward-char 'flyspell-delayed t)
19857 ;; Make pabbrev-mode expand after org-mode commands
19858 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
19859 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
19861 (defun org-remap (map &rest commands)
19862 "In MAP, remap the functions given in COMMANDS.
19863 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
19866 (setq old (pop commands) new (pop commands))
19867 (if (fboundp 'command-remapping)
19868 (org-defkey map (vector 'remap old) new)
19869 (substitute-key-definition old new map global-map)))))
19871 (defun org-transpose-words ()
19872 "Transpose words for Org.
19873 This uses the `org-mode-transpose-word-syntax-table' syntax
19874 table, which interprets characters in `org-emphasis-alist' as
19875 word constituents."
19877 (with-syntax-table org-mode-transpose-word-syntax-table
19878 (call-interactively 'transpose-words)))
19879 (org-remap org-mode-map 'transpose-words 'org-transpose-words)
19881 (when (eq org-enable-table-editor 'optimized)
19882 ;; If the user wants maximum table support, we need to hijack
19883 ;; some standard editing functions
19884 (org-remap org-mode-map
19885 'self-insert-command 'org-self-insert-command
19886 'delete-char 'org-delete-char
19887 'delete-backward-char 'org-delete-backward-char)
19888 (org-defkey org-mode-map "|" 'org-force-self-insert))
19890 (defvar org-ctrl-c-ctrl-c-hook nil
19891 "Hook for functions attaching themselves to `C-c C-c'.
19893 This can be used to add additional functionality to the C-c C-c
19894 key which executes context-dependent commands. This hook is run
19895 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
19896 run after the last test.
19898 Each function will be called with no arguments. The function
19899 must check if the context is appropriate for it to act. If yes,
19900 it should do its thing and then return a non-nil value. If the
19901 context is wrong, just do nothing and return nil.")
19903 (defvar org-ctrl-c-ctrl-c-final-hook nil
19904 "Hook for functions attaching themselves to `C-c C-c'.
19906 This can be used to add additional functionality to the C-c C-c
19907 key which executes context-dependent commands. This hook is run
19908 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
19909 before the first test.
19911 Each function will be called with no arguments. The function
19912 must check if the context is appropriate for it to act. If yes,
19913 it should do its thing and then return a non-nil value. If the
19914 context is wrong, just do nothing and return nil.")
19916 (defvar org-tab-first-hook nil
19917 "Hook for functions to attach themselves to TAB.
19918 See `org-ctrl-c-ctrl-c-hook' for more information.
19919 This hook runs as the first action when TAB is pressed, even before
19920 `org-cycle' messes around with the `outline-regexp' to cater for
19921 inline tasks and plain list item folding.
19922 If any function in this hook returns t, any other actions that
19923 would have been caused by TAB (such as table field motion or visibility
19924 cycling) will not occur.")
19926 (defvar org-tab-after-check-for-table-hook nil
19927 "Hook for functions to attach themselves to TAB.
19928 See `org-ctrl-c-ctrl-c-hook' for more information.
19929 This hook runs after it has been established that the cursor is not in a
19930 table, but before checking if the cursor is in a headline or if global cycling
19932 If any function in this hook returns t, not other actions like visibility
19933 cycling will be done.")
19935 (defvar org-tab-after-check-for-cycling-hook nil
19936 "Hook for functions to attach themselves to TAB.
19937 See `org-ctrl-c-ctrl-c-hook' for more information.
19938 This hook runs after it has been established that not table field motion and
19939 not visibility should be done because of current context. This is probably
19940 the place where a package like yasnippets can hook in.")
19942 (defvar org-tab-before-tab-emulation-hook nil
19943 "Hook for functions to attach themselves to TAB.
19944 See `org-ctrl-c-ctrl-c-hook' for more information.
19945 This hook runs after every other options for TAB have been exhausted, but
19946 before indentation and \t insertion takes place.")
19948 (defvar org-metaleft-hook nil
19949 "Hook for functions attaching themselves to `M-left'.
19950 See `org-ctrl-c-ctrl-c-hook' for more information.")
19951 (defvar org-metaright-hook nil
19952 "Hook for functions attaching themselves to `M-right'.
19953 See `org-ctrl-c-ctrl-c-hook' for more information.")
19954 (defvar org-metaup-hook nil
19955 "Hook for functions attaching themselves to `M-up'.
19956 See `org-ctrl-c-ctrl-c-hook' for more information.")
19957 (defvar org-metadown-hook nil
19958 "Hook for functions attaching themselves to `M-down'.
19959 See `org-ctrl-c-ctrl-c-hook' for more information.")
19960 (defvar org-shiftmetaleft-hook nil
19961 "Hook for functions attaching themselves to `M-S-left'.
19962 See `org-ctrl-c-ctrl-c-hook' for more information.")
19963 (defvar org-shiftmetaright-hook nil
19964 "Hook for functions attaching themselves to `M-S-right'.
19965 See `org-ctrl-c-ctrl-c-hook' for more information.")
19966 (defvar org-shiftmetaup-hook nil
19967 "Hook for functions attaching themselves to `M-S-up'.
19968 See `org-ctrl-c-ctrl-c-hook' for more information.")
19969 (defvar org-shiftmetadown-hook nil
19970 "Hook for functions attaching themselves to `M-S-down'.
19971 See `org-ctrl-c-ctrl-c-hook' for more information.")
19972 (defvar org-metareturn-hook nil
19973 "Hook for functions attaching themselves to `M-RET'.
19974 See `org-ctrl-c-ctrl-c-hook' for more information.")
19975 (defvar org-shiftup-hook nil
19976 "Hook for functions attaching themselves to `S-up'.
19977 See `org-ctrl-c-ctrl-c-hook' for more information.")
19978 (defvar org-shiftup-final-hook nil
19979 "Hook for functions attaching themselves to `S-up'.
19980 This one runs after all other options except shift-select have been excluded.
19981 See `org-ctrl-c-ctrl-c-hook' for more information.")
19982 (defvar org-shiftdown-hook nil
19983 "Hook for functions attaching themselves to `S-down'.
19984 See `org-ctrl-c-ctrl-c-hook' for more information.")
19985 (defvar org-shiftdown-final-hook nil
19986 "Hook for functions attaching themselves to `S-down'.
19987 This one runs after all other options except shift-select have been excluded.
19988 See `org-ctrl-c-ctrl-c-hook' for more information.")
19989 (defvar org-shiftleft-hook nil
19990 "Hook for functions attaching themselves to `S-left'.
19991 See `org-ctrl-c-ctrl-c-hook' for more information.")
19992 (defvar org-shiftleft-final-hook nil
19993 "Hook for functions attaching themselves to `S-left'.
19994 This one runs after all other options except shift-select have been excluded.
19995 See `org-ctrl-c-ctrl-c-hook' for more information.")
19996 (defvar org-shiftright-hook nil
19997 "Hook for functions attaching themselves to `S-right'.
19998 See `org-ctrl-c-ctrl-c-hook' for more information.")
19999 (defvar org-shiftright-final-hook nil
20000 "Hook for functions attaching themselves to `S-right'.
20001 This one runs after all other options except shift-select have been excluded.
20002 See `org-ctrl-c-ctrl-c-hook' for more information.")
20004 (defun org-modifier-cursor-error ()
20005 "Throw an error, a modified cursor command was applied in wrong context."
20006 (user-error "This command is active in special context like tables, headlines or items"))
20008 (defun org-shiftselect-error ()
20009 "Throw an error because Shift-Cursor command was applied in wrong context."
20010 (if (and (boundp 'shift-select-mode) shift-select-mode)
20011 (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
20012 (user-error "This command works only in special context like headlines or timestamps")))
20014 (defun org-call-for-shift-select (cmd)
20015 (let ((this-command-keys-shift-translated t))
20016 (call-interactively cmd)))
20018 (defun org-shifttab (&optional arg)
20019 "Global visibility cycling or move to previous table field.
20020 Call `org-table-previous-field' within a table.
20021 When ARG is nil, cycle globally through visibility states.
20022 When ARG is a numeric prefix, show contents of this level."
20025 ((org-at-table-p) (call-interactively 'org-table-previous-field))
20027 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
20028 (message "Content view to level: %d" arg)
20029 (org-content (prefix-numeric-value arg2))
20030 (org-cycle-show-empty-lines t)
20031 (setq org-cycle-global-status 'overview)))
20032 (t (call-interactively 'org-global-cycle))))
20034 (defun org-shiftmetaleft ()
20035 "Promote subtree or delete table column.
20036 Calls `org-promote-subtree', `org-outdent-item-tree', or
20037 `org-table-delete-column', depending on context. See the
20038 individual commands for more information."
20041 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
20042 ((org-at-table-p) (call-interactively 'org-table-delete-column))
20043 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
20044 ((if (not (org-region-active-p)) (org-at-item-p)
20045 (save-excursion (goto-char (region-beginning))
20047 (call-interactively 'org-outdent-item-tree))
20048 (t (org-modifier-cursor-error))))
20050 (defun org-shiftmetaright ()
20051 "Demote subtree or insert table column.
20052 Calls `org-demote-subtree', `org-indent-item-tree', or
20053 `org-table-insert-column', depending on context. See the
20054 individual commands for more information."
20057 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
20058 ((org-at-table-p) (call-interactively 'org-table-insert-column))
20059 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
20060 ((if (not (org-region-active-p)) (org-at-item-p)
20061 (save-excursion (goto-char (region-beginning))
20063 (call-interactively 'org-indent-item-tree))
20064 (t (org-modifier-cursor-error))))
20066 (defun org-shiftmetaup (&optional arg)
20067 "Drag the line at point up.
20068 In a table, kill the current row.
20069 On a clock timestamp, update the value of the timestamp like `S-<up>'
20070 but also adjust the previous clocked item in the clock history.
20071 Everywhere else, drag the line at point up."
20074 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
20075 ((org-at-table-p) (call-interactively 'org-table-kill-row))
20076 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
20077 (call-interactively 'org-timestamp-up)))
20078 (t (call-interactively 'org-drag-line-backward))))
20080 (defun org-shiftmetadown (&optional arg)
20081 "Drag the line at point down.
20082 In a table, insert an empty row at the current line.
20083 On a clock timestamp, update the value of the timestamp like `S-<down>'
20084 but also adjust the previous clocked item in the clock history.
20085 Everywhere else, drag the line at point down."
20088 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
20089 ((org-at-table-p) (call-interactively 'org-table-insert-row))
20090 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
20091 (call-interactively 'org-timestamp-down)))
20092 (t (call-interactively 'org-drag-line-forward))))
20094 (defsubst org-hidden-tree-error ()
20096 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
20098 (defun org-metaleft (&optional arg)
20099 "Promote heading or move table column to left.
20100 Calls `org-do-promote' or `org-table-move-column', depending on context.
20101 With no specific context, calls the Emacs default `backward-word'.
20102 See the individual commands for more information."
20105 ((run-hook-with-args-until-success 'org-metaleft-hook))
20106 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
20107 ((org-with-limited-levels
20108 (or (org-at-heading-p)
20109 (and (org-region-active-p)
20111 (goto-char (region-beginning))
20112 (org-at-heading-p)))))
20113 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
20114 (call-interactively 'org-do-promote))
20115 ;; At an inline task.
20116 ((org-at-heading-p)
20117 (call-interactively 'org-inlinetask-promote))
20118 ((or (org-at-item-p)
20119 (and (org-region-active-p)
20121 (goto-char (region-beginning))
20123 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
20124 (call-interactively 'org-outdent-item))
20125 (t (call-interactively 'backward-word))))
20127 (defun org-metaright (&optional arg)
20128 "Demote a subtree, a list item or move table column to right.
20129 In front of a drawer or a block keyword, indent it correctly.
20130 With no specific context, calls the Emacs default `forward-word'.
20131 See the individual commands for more information."
20134 ((run-hook-with-args-until-success 'org-metaright-hook))
20135 ((org-at-table-p) (call-interactively 'org-table-move-column))
20136 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
20137 ((org-at-block-p) (call-interactively 'org-indent-block))
20138 ((org-with-limited-levels
20139 (or (org-at-heading-p)
20140 (and (org-region-active-p)
20142 (goto-char (region-beginning))
20143 (org-at-heading-p)))))
20144 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
20145 (call-interactively 'org-do-demote))
20146 ;; At an inline task.
20147 ((org-at-heading-p)
20148 (call-interactively 'org-inlinetask-demote))
20149 ((or (org-at-item-p)
20150 (and (org-region-active-p)
20152 (goto-char (region-beginning))
20154 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
20155 (call-interactively 'org-indent-item))
20156 (t (call-interactively 'forward-word))))
20158 (defun org-check-for-hidden (what)
20159 "Check if there are hidden headlines/items in the current visual line.
20160 WHAT can be either `headlines' or `items'. If the current line is
20161 an outline or item heading and it has a folded subtree below it,
20162 this function returns t, nil otherwise."
20164 ((eq what 'headlines) org-outline-regexp-bol)
20165 ((eq what 'items) (org-item-beginning-re))
20166 (t (error "This should not happen"))))
20170 (unless (org-region-active-p)
20171 (setq beg (point-at-bol))
20172 (beginning-of-line 2)
20173 (while (and (not (eobp)) ;; this is like `next-line'
20174 (get-char-property (1- (point)) 'invisible))
20175 (beginning-of-line 2))
20178 (goto-char (point-at-eol))
20179 (setq end (max end (point)))
20180 (while (re-search-forward re end t)
20181 (if (get-char-property (match-beginning 0) 'invisible)
20185 (defun org-metaup (&optional arg)
20186 "Move subtree up or move table row up.
20187 Calls `org-move-subtree-up' or `org-table-move-row' or
20188 `org-move-item-up', depending on context. See the individual commands
20189 for more information."
20192 ((run-hook-with-args-until-success 'org-metaup-hook))
20193 ((org-region-active-p)
20194 (let* ((a (min (region-beginning) (region-end)))
20195 (b (1- (max (region-beginning) (region-end))))
20196 (c (save-excursion (goto-char a)
20197 (move-beginning-of-line 0)))
20198 (d (save-excursion (goto-char a)
20199 (move-end-of-line 0) (point))))
20200 (transpose-regions a b c d)
20202 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
20203 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
20204 ((org-at-item-p) (call-interactively 'org-move-item-up))
20205 (t (org-drag-element-backward))))
20207 (defun org-metadown (&optional arg)
20208 "Move subtree down or move table row down.
20209 Calls `org-move-subtree-down' or `org-table-move-row' or
20210 `org-move-item-down', depending on context. See the individual
20211 commands for more information."
20214 ((run-hook-with-args-until-success 'org-metadown-hook))
20215 ((org-region-active-p)
20216 (let* ((a (min (region-beginning) (region-end)))
20217 (b (max (region-beginning) (region-end)))
20218 (c (save-excursion (goto-char b)
20219 (move-beginning-of-line 1)))
20220 (d (save-excursion (goto-char b)
20221 (move-end-of-line 1) (1+ (point)))))
20222 (transpose-regions a b c d)
20224 ((org-at-table-p) (call-interactively 'org-table-move-row))
20225 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
20226 ((org-at-item-p) (call-interactively 'org-move-item-down))
20227 (t (org-drag-element-forward))))
20229 (defun org-shiftup (&optional arg)
20230 "Increase item in timestamp or increase priority of current headline.
20231 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
20232 depending on context. See the individual commands for more information."
20235 ((run-hook-with-args-until-success 'org-shiftup-hook))
20236 ((and org-support-shift-select (org-region-active-p))
20237 (org-call-for-shift-select 'previous-line))
20238 ((org-at-timestamp-p t)
20239 (call-interactively (if org-edit-timestamp-down-means-later
20240 'org-timestamp-down 'org-timestamp-up)))
20241 ((and (not (eq org-support-shift-select 'always))
20242 org-enable-priority-commands
20243 (org-at-heading-p))
20244 (call-interactively 'org-priority-up))
20245 ((and (not org-support-shift-select) (org-at-item-p))
20246 (call-interactively 'org-previous-item))
20247 ((org-clocktable-try-shift 'up arg))
20248 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
20249 (org-support-shift-select
20250 (org-call-for-shift-select 'previous-line))
20251 (t (org-shiftselect-error))))
20253 (defun org-shiftdown (&optional arg)
20254 "Decrease item in timestamp or decrease priority of current headline.
20255 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
20256 depending on context. See the individual commands for more information."
20259 ((run-hook-with-args-until-success 'org-shiftdown-hook))
20260 ((and org-support-shift-select (org-region-active-p))
20261 (org-call-for-shift-select 'next-line))
20262 ((org-at-timestamp-p t)
20263 (call-interactively (if org-edit-timestamp-down-means-later
20264 'org-timestamp-up 'org-timestamp-down)))
20265 ((and (not (eq org-support-shift-select 'always))
20266 org-enable-priority-commands
20267 (org-at-heading-p))
20268 (call-interactively 'org-priority-down))
20269 ((and (not org-support-shift-select) (org-at-item-p))
20270 (call-interactively 'org-next-item))
20271 ((org-clocktable-try-shift 'down arg))
20272 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
20273 (org-support-shift-select
20274 (org-call-for-shift-select 'next-line))
20275 (t (org-shiftselect-error))))
20277 (defun org-shiftright (&optional arg)
20278 "Cycle the thing at point or in the current line, depending on context.
20279 Depending on context, this does one of the following:
20281 - switch a timestamp at point one day into the future
20282 - on a headline, switch to the next TODO keyword.
20283 - on an item, switch entire list to the next bullet type
20284 - on a property line, switch to the next allowed value
20285 - on a clocktable definition line, move time block into the future"
20288 ((run-hook-with-args-until-success 'org-shiftright-hook))
20289 ((and org-support-shift-select (org-region-active-p))
20290 (org-call-for-shift-select 'forward-char))
20291 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
20292 ((and (not (eq org-support-shift-select 'always))
20293 (org-at-heading-p))
20294 (let ((org-inhibit-logging
20295 (not org-treat-S-cursor-todo-selection-as-state-change))
20296 (org-inhibit-blocking
20297 (not org-treat-S-cursor-todo-selection-as-state-change)))
20298 (org-call-with-arg 'org-todo 'right)))
20299 ((or (and org-support-shift-select
20300 (not (eq org-support-shift-select 'always))
20301 (org-at-item-bullet-p))
20302 (and (not org-support-shift-select) (org-at-item-p)))
20303 (org-call-with-arg 'org-cycle-list-bullet nil))
20304 ((and (not (eq org-support-shift-select 'always))
20305 (org-at-property-p))
20306 (call-interactively 'org-property-next-allowed-value))
20307 ((org-clocktable-try-shift 'right arg))
20308 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
20309 (org-support-shift-select
20310 (org-call-for-shift-select 'forward-char))
20311 (t (org-shiftselect-error))))
20313 (defun org-shiftleft (&optional arg)
20314 "Cycle the thing at point or in the current line, depending on context.
20315 Depending on context, this does one of the following:
20317 - switch a timestamp at point one day into the past
20318 - on a headline, switch to the previous TODO keyword.
20319 - on an item, switch entire list to the previous bullet type
20320 - on a property line, switch to the previous allowed value
20321 - on a clocktable definition line, move time block into the past"
20324 ((run-hook-with-args-until-success 'org-shiftleft-hook))
20325 ((and org-support-shift-select (org-region-active-p))
20326 (org-call-for-shift-select 'backward-char))
20327 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
20328 ((and (not (eq org-support-shift-select 'always))
20329 (org-at-heading-p))
20330 (let ((org-inhibit-logging
20331 (not org-treat-S-cursor-todo-selection-as-state-change))
20332 (org-inhibit-blocking
20333 (not org-treat-S-cursor-todo-selection-as-state-change)))
20334 (org-call-with-arg 'org-todo 'left)))
20335 ((or (and org-support-shift-select
20336 (not (eq org-support-shift-select 'always))
20337 (org-at-item-bullet-p))
20338 (and (not org-support-shift-select) (org-at-item-p)))
20339 (org-call-with-arg 'org-cycle-list-bullet 'previous))
20340 ((and (not (eq org-support-shift-select 'always))
20341 (org-at-property-p))
20342 (call-interactively 'org-property-previous-allowed-value))
20343 ((org-clocktable-try-shift 'left arg))
20344 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
20345 (org-support-shift-select
20346 (org-call-for-shift-select 'backward-char))
20347 (t (org-shiftselect-error))))
20349 (defun org-shiftcontrolright ()
20350 "Switch to next TODO set."
20353 ((and org-support-shift-select (org-region-active-p))
20354 (org-call-for-shift-select 'forward-word))
20355 ((and (not (eq org-support-shift-select 'always))
20356 (org-at-heading-p))
20357 (org-call-with-arg 'org-todo 'nextset))
20358 (org-support-shift-select
20359 (org-call-for-shift-select 'forward-word))
20360 (t (org-shiftselect-error))))
20362 (defun org-shiftcontrolleft ()
20363 "Switch to previous TODO set."
20366 ((and org-support-shift-select (org-region-active-p))
20367 (org-call-for-shift-select 'backward-word))
20368 ((and (not (eq org-support-shift-select 'always))
20369 (org-at-heading-p))
20370 (org-call-with-arg 'org-todo 'previousset))
20371 (org-support-shift-select
20372 (org-call-for-shift-select 'backward-word))
20373 (t (org-shiftselect-error))))
20375 (defun org-shiftcontrolup (&optional n)
20376 "Change timestamps synchronously up in CLOCK log lines.
20377 Optional argument N tells to change by that many units."
20379 (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
20380 (let (org-support-shift-select)
20381 (org-clock-timestamps-up n))
20382 (user-error "Not at a clock log")))
20384 (defun org-shiftcontroldown (&optional n)
20385 "Change timestamps synchronously down in CLOCK log lines.
20386 Optional argument N tells to change by that many units."
20388 (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
20389 (let (org-support-shift-select)
20390 (org-clock-timestamps-down n))
20391 (user-error "Not at a clock log")))
20393 (defun org-increase-number-at-point (&optional inc)
20394 "Increment the number at point.
20395 With an optional prefix numeric argument INC, increment using
20396 this numeric value."
20398 (if (not (number-at-point))
20399 (user-error "Not on a number")
20400 (unless inc (setq inc 1))
20401 (let ((pos (point))
20402 (beg (skip-chars-backward "-+^/*0-9eE."))
20403 (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
20404 (setq nap (buffer-substring-no-properties
20405 (+ pos beg) (+ pos beg end)))
20406 (delete-region (+ pos beg) (+ pos beg end))
20407 (insert (calc-eval (concat (number-to-string inc) "+" nap))))
20408 (when (org-at-table-p)
20410 (org-table-end-of-field 1))))
20412 (defun org-decrease-number-at-point (&optional inc)
20413 "Decrement the number at point.
20414 With an optional prefix numeric argument INC, decrement using
20415 this numeric value."
20417 (org-increase-number-at-point (- inc)))
20419 (defun org-ctrl-c-ret ()
20420 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
20423 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
20424 (t (call-interactively 'org-insert-heading))))
20426 (defun org-find-visible ()
20428 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
20429 (get-char-property s 'invisible)))
20431 (defun org-find-invisible ()
20433 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
20434 (not (get-char-property s 'invisible))))
20437 (defun org-copy-visible (beg end)
20438 "Copy the visible parts of the region."
20443 (narrow-to-region beg end)
20444 (setq s (goto-char (point-min)))
20445 (while (not (= (point) (point-max)))
20446 (goto-char (org-find-invisible))
20447 (push (buffer-substring s (point)) snippets)
20448 (setq s (goto-char (org-find-visible))))))
20449 (kill-new (apply 'concat (nreverse snippets)))))
20451 (defun org-copy-special ()
20452 "Copy region in table or copy current subtree.
20453 Calls `org-table-copy' or `org-copy-subtree', depending on context.
20454 See the individual commands for more information."
20456 (call-interactively
20457 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
20459 (defun org-cut-special ()
20460 "Cut region in table or cut current subtree.
20461 Calls `org-table-copy' or `org-cut-subtree', depending on context.
20462 See the individual commands for more information."
20464 (call-interactively
20465 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
20467 (defun org-paste-special (arg)
20468 "Paste rectangular region into table, or past subtree relative to level.
20469 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
20470 See the individual commands for more information."
20472 (if (org-at-table-p)
20473 (org-table-paste-rectangle)
20474 (org-paste-subtree arg)))
20476 (defsubst org-in-fixed-width-region-p ()
20477 "Is point in a fixed-width region?"
20479 (eq 'fixed-width (org-element-type (org-element-at-point)))))
20481 (defun org-edit-special (&optional arg)
20482 "Call a special editor for the element at point.
20483 When at a table, call the formula editor with `org-table-edit-formulas'.
20484 When in a source code block, call `org-edit-src-code'.
20485 When in a fixed-width region, call `org-edit-fixed-width-region'.
20486 When at an #+INCLUDE keyword, visit the included file.
20487 On a link, call `ffap' to visit the link at point.
20488 Otherwise, return a user error."
20490 (let ((element (org-element-at-point)))
20491 (assert (not buffer-read-only) nil
20492 "Buffer is read-only: %s" (buffer-name))
20493 (case (org-element-type element)
20495 (if (not arg) (org-edit-src-code)
20496 (let* ((info (org-babel-get-src-block-info))
20497 (lang (nth 0 info))
20498 (params (nth 2 info))
20499 (session (cdr (assq :session params))))
20500 (if (not session) (org-edit-src-code)
20501 ;; At a src-block with a session and function called with
20502 ;; an ARG: switch to the buffer related to the inferior
20505 (funcall (intern (concat "org-babel-prep-session:" lang))
20506 session params))))))
20508 (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
20509 (find-file-other-window
20510 (org-remove-double-quotes
20511 (car (org-split-string (org-element-property :value element)))))
20512 (user-error "No special environment to edit here")))
20514 (if (eq (org-element-property :type element) 'table.el)
20515 (org-edit-src-code)
20516 (call-interactively 'org-table-edit-formulas)))
20517 ;; Only Org tables contain `table-row' type elements.
20518 (table-row (call-interactively 'org-table-edit-formulas))
20519 ((example-block export-block) (org-edit-src-code))
20520 (fixed-width (org-edit-fixed-width-region))
20522 ;; No notable element at point. Though, we may be at a link,
20523 ;; which is an object. Thus, scan deeper.
20524 (if (eq (org-element-type (org-element-context element)) 'link)
20525 (call-interactively 'ffap)
20526 (user-error "No special environment to edit here"))))))
20528 (defvar org-table-coordinate-overlays) ; defined in org-table.el
20529 (defun org-ctrl-c-ctrl-c (&optional arg)
20530 "Set tags in headline, or update according to changed information at point.
20532 This command does many different things, depending on context:
20534 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
20535 this is what we do.
20537 - If the cursor is on a statistics cookie, update it.
20539 - If the cursor is in a headline, prompt for tags and insert them
20540 into the current line, aligned to `org-tags-column'. When called
20541 with prefix arg, realign all tags in the current buffer.
20543 - If the cursor is in one of the special #+KEYWORD lines, this
20544 triggers scanning the buffer for these lines and updating the
20547 - If the cursor is inside a table, realign the table. This command
20548 works even if the automatic table editor has been turned off.
20550 - If the cursor is on a #+TBLFM line, re-apply the formulas to
20553 - If the cursor is at a footnote reference or definition, jump to
20554 the corresponding definition or references, respectively.
20556 - If the cursor is a the beginning of a dynamic block, update it.
20558 - If the current buffer is a capture buffer, close note and file it.
20560 - If the cursor is on a <<<target>>>, update radio targets and
20561 corresponding links in this buffer.
20563 - If the cursor is on a numbered item in a plain list, renumber the
20566 - If the cursor is on a checkbox, toggle it.
20568 - If the cursor is on a code block, evaluate it. The variable
20569 `org-confirm-babel-evaluate' can be used to control prompting
20570 before code block evaluation, by default every code block
20571 evaluation requires confirmation. Code block evaluation can be
20572 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
20575 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
20576 org-occur-highlights)
20577 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
20578 (org-remove-occur-highlights)
20579 (message "Temporary highlights/overlays removed from current buffer"))
20580 ((and (local-variable-p 'org-finish-function (current-buffer))
20581 (fboundp org-finish-function))
20582 (funcall org-finish-function))
20583 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
20585 (let* ((context (org-element-context)) (type (org-element-type context)))
20586 ;; Test if point is within a blank line.
20587 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
20588 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
20589 (user-error "C-c C-c can do nothing useful at this location"))
20591 ;; When at a link, act according to the parent instead.
20592 (link (setq context (org-element-property :parent context))
20593 (setq type (org-element-type context)))
20594 ;; Unsupported object types: refer to the first supported
20595 ;; element or object containing it.
20596 ((bold code entity export-snippet inline-babel-call inline-src-block
20597 italic latex-fragment line-break macro strike-through subscript
20598 superscript underline verbatim)
20599 (while (and (setq context (org-element-property :parent context))
20600 (not (memq (setq type (org-element-type context))
20601 '(radio-target paragraph verse-block
20603 ;; For convenience: at the first line of a paragraph on the
20604 ;; same line as an item, apply function on that item instead.
20605 (when (eq type 'paragraph)
20606 (let ((parent (org-element-property :parent context)))
20607 (when (and (eq (org-element-type parent) 'item)
20608 (= (point-at-bol) (org-element-property :begin parent)))
20609 (setq context parent type 'item))))
20610 ;; Act according to type of element or object at point.
20612 (clock (org-clock-update-time-maybe))
20615 (goto-char (org-element-property :post-affiliated context))
20616 (org-update-dblock)))
20617 (footnote-definition
20618 (goto-char (org-element-property :post-affiliated context))
20619 (call-interactively 'org-footnote-action))
20620 (footnote-reference (call-interactively 'org-footnote-action))
20621 ((headline inlinetask)
20622 (save-excursion (goto-char (org-element-property :begin context))
20623 (call-interactively 'org-set-tags)))
20625 ;; At an item: a double C-u set checkbox to "[-]"
20626 ;; unconditionally, whereas a single one will toggle its
20627 ;; presence. Without an universal argument, if the item
20628 ;; has a checkbox, toggle it. Otherwise repair the list.
20629 (let* ((box (org-element-property :checkbox context))
20630 (struct (org-element-property :structure context))
20631 (old-struct (copy-tree struct))
20632 (parents (org-list-parents-alist struct))
20633 (prevs (org-list-prevs-alist struct))
20634 (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
20635 (org-list-set-checkbox
20636 (org-element-property :begin context) struct
20637 (cond ((equal arg '(16)) "[-]")
20638 ((and (not box) (equal arg '(4))) "[ ]")
20639 ((or (not box) (equal arg '(4))) nil)
20640 ((eq box 'on) "[ ]")
20642 ;; Mimic `org-list-write-struct' but with grabbing
20643 ;; a return value from `org-list-struct-fix-box'.
20644 (org-list-struct-fix-ind struct parents 2)
20645 (org-list-struct-fix-item-end struct)
20646 (org-list-struct-fix-bul struct prevs)
20647 (org-list-struct-fix-ind struct parents)
20649 (org-list-struct-fix-box struct parents prevs orderedp)))
20650 (if (and box (equal struct old-struct))
20651 (if (equal arg '(16))
20652 (message "Checkboxes already reset")
20653 (user-error "Cannot toggle this checkbox: %s"
20655 "all subitems checked"
20656 "unchecked subitems")))
20657 (org-list-struct-apply-struct struct old-struct)
20658 (org-update-checkbox-count-maybe))
20660 (message "Checkboxes were removed due to empty box at line %d"
20661 (org-current-line block-item))))))
20663 (let ((org-inhibit-startup-visibility-stuff t)
20664 (org-startup-align-all-tables nil))
20665 (when (boundp 'org-table-coordinate-overlays)
20666 (mapc 'delete-overlay org-table-coordinate-overlays)
20667 (setq org-table-coordinate-overlays nil))
20668 (org-save-outline-visibility 'use-markers (org-mode-restart)))
20669 (message "Local setup has been refreshed"))
20671 ;; At a plain list, with a double C-u argument, set
20672 ;; checkboxes of each item to "[-]", whereas a single one
20673 ;; will toggle their presence according to the state of the
20674 ;; first item in the list. Without an argument, repair the
20676 (let* ((begin (org-element-property :contents-begin context))
20677 (beginm (move-marker (make-marker) begin))
20678 (struct (org-element-property :structure context))
20679 (old-struct (copy-tree struct))
20680 (first-box (save-excursion
20682 (looking-at org-list-full-item-re)
20683 (match-string-no-properties 3)))
20684 (new-box (cond ((equal arg '(16)) "[-]")
20685 ((equal arg '(4)) (unless first-box "[ ]"))
20686 ((equal first-box "[X]") "[ ]")
20690 (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
20691 (org-list-get-all-items
20692 begin struct (org-list-prevs-alist struct))))
20693 ((and first-box (eq (point) begin))
20694 ;; For convenience, when point is at bol on the first
20695 ;; item of the list and no argument is provided, simply
20696 ;; toggle checkbox of that item, if any.
20697 (org-list-set-checkbox begin struct new-box)))
20698 (org-list-write-struct
20699 struct (org-list-parents-alist struct) old-struct)
20700 (org-update-checkbox-count-maybe)
20701 (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
20702 ((property-drawer node-property)
20703 (call-interactively 'org-property-action))
20704 ((radio-target target)
20705 (call-interactively 'org-update-radio-target-regexp))
20707 (call-interactively 'org-update-statistics-cookies))
20708 ((table table-cell table-row)
20709 ;; At a table, recalculate every field and align it. Also
20710 ;; send the table if necessary. If the table has
20711 ;; a `table.el' type, just give up. At a table row or
20712 ;; cell, maybe recalculate line but always align table.
20713 (if (eq (org-element-property :type context) 'table.el)
20714 (message "Use C-c ' to edit table.el tables")
20715 (let ((org-enable-table-editor t))
20716 (if (or (eq type 'table)
20717 ;; Check if point is at a TBLFM line.
20718 (and (eq type 'table-row)
20719 (= (point) (org-element-property :end context))))
20721 (if (org-at-TBLFM-p)
20722 (progn (require 'org-table)
20723 (org-table-calc-current-TBLFM))
20724 (goto-char (org-element-property :contents-begin context))
20725 (org-call-with-arg 'org-table-recalculate (or arg t))
20726 (orgtbl-send-table 'maybe)))
20727 (org-table-maybe-eval-formula)
20728 (cond (arg (call-interactively 'org-table-recalculate))
20729 ((org-table-maybe-recalculate-line))
20730 (t (org-table-align)))))))
20731 (timestamp (org-timestamp-change 0 'day))
20733 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
20735 "C-c C-c can do nothing useful at this location")))))))))
20737 (defun org-mode-restart ()
20739 (let ((indent-status (org-bound-and-true-p org-indent-mode)))
20740 (funcall major-mode)
20741 (hack-local-variables)
20742 (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
20743 (org-indent-mode -1)))
20744 (message "%s restarted" major-mode))
20746 (defun org-kill-note-or-show-branches ()
20747 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
20749 (if (not org-finish-function)
20752 (call-interactively 'show-branches))
20753 (let ((org-note-abort t))
20754 (funcall org-finish-function))))
20756 (defun org-open-line (n)
20757 "Insert a new row in tables, call `open-line' elsewhere.
20758 If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
20761 ((not org-special-ctrl-o)
20764 (org-table-insert-row))
20768 (defun org-return (&optional indent)
20769 "Goto next table row or insert a newline.
20770 Calls `org-table-next-row' or `newline', depending on context.
20771 See the individual commands for more information."
20775 ((or (bobp) (org-in-src-block-p))
20776 (if indent (newline-and-indent) (newline)))
20778 (org-table-justify-field-maybe)
20779 (call-interactively 'org-table-next-row))
20780 ;; when `newline-and-indent' is called within a list, make sure
20781 ;; text moved stays inside the item.
20782 ((and (org-in-item-p) indent)
20783 (if (and (org-at-item-p) (>= (point) (match-end 0)))
20785 (save-match-data (newline))
20786 (org-indent-line-to (length (match-string 0))))
20787 (let ((ind (org-get-indentation)))
20789 (if (org-looking-back org-list-end-re)
20791 (org-indent-line-to ind)))))
20792 ((and org-return-follows-link
20793 (org-at-timestamp-p t)
20794 (not (eq org-ts-what 'after)))
20795 (org-follow-timestamp-link))
20796 ((and org-return-follows-link
20797 (let ((tprop (get-text-property (point) 'face)))
20798 (or (eq tprop 'org-link)
20799 (and (listp tprop) (memq 'org-link tprop)))))
20800 (call-interactively 'org-open-at-point))
20801 ((and (org-at-heading-p)
20803 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
20807 (t (if indent (newline-and-indent) (newline))))))
20809 (defun org-return-indent ()
20810 "Goto next table row or insert a newline and indent.
20811 Calls `org-table-next-row' or `newline-and-indent', depending on
20812 context. See the individual commands for more information."
20816 (defun org-ctrl-c-star ()
20817 "Compute table, or change heading status of lines.
20818 Calls `org-table-recalculate' or `org-toggle-heading',
20819 depending on context."
20823 (call-interactively 'org-table-recalculate))
20825 ;; Convert all lines in region to list items
20826 (call-interactively 'org-toggle-heading))))
20828 (defun org-ctrl-c-minus ()
20829 "Insert separator line in table or modify bullet status of line.
20830 Also turns a plain line or a region of lines into list items.
20831 Calls `org-table-insert-hline', `org-toggle-item', or
20832 `org-cycle-list-bullet', depending on context."
20836 (call-interactively 'org-table-insert-hline))
20837 ((org-region-active-p)
20838 (call-interactively 'org-toggle-item))
20840 (call-interactively 'org-cycle-list-bullet))
20842 (call-interactively 'org-toggle-item))))
20844 (defun org-toggle-item (arg)
20845 "Convert headings or normal lines to items, items to normal lines.
20846 If there is no active region, only the current line is considered.
20848 If the first non blank line in the region is a headline, convert
20849 all headlines to items, shifting text accordingly.
20851 If it is an item, convert all items to normal lines.
20853 If it is normal text, change region into a list of items.
20854 With a prefix argument ARG, change the region in a single item."
20858 ;; Shift text in current section to IND, from point to END.
20859 ;; The function leaves point to END line.
20861 (let ((min-i 1000) (end (copy-marker end)))
20862 ;; First determine the minimum indentation (MIN-I) of
20866 (while (< (point) end)
20867 (let ((i (org-get-indentation)))
20869 ;; Skip blank lines and inline tasks.
20870 ((looking-at "^[ \t]*$"))
20871 ((looking-at org-outline-regexp-bol))
20872 ;; We can't find less than 0 indentation.
20873 ((zerop i) (throw 'exit (setq min-i 0)))
20874 ((< i min-i) (setq min-i i))))
20876 ;; Then indent each line so that a line indented to
20877 ;; MIN-I becomes indented to IND. Ignore blank lines
20878 ;; and inline tasks in the process.
20879 (let ((delta (- ind min-i)))
20880 (while (< (point) end)
20881 (unless (or (looking-at "^[ \t]*$")
20882 (looking-at org-outline-regexp-bol))
20883 (org-indent-line-to (+ (org-get-indentation) delta)))
20884 (forward-line)))))))
20887 ;; Return beginning of first non-blank line, starting from
20892 (skip-chars-forward " \r\t\n")
20895 ;; Determine boundaries of changes.
20896 (if (org-region-active-p)
20897 (setq beg (funcall skip-blanks (region-beginning))
20898 end (copy-marker (region-end)))
20899 (setq beg (funcall skip-blanks (point-at-bol))
20900 end (copy-marker (point-at-eol))))
20901 ;; Depending on the starting line, choose an action on the text
20902 ;; between BEG and END.
20903 (org-with-limited-levels
20907 ;; Case 1. Start at an item: de-itemize. Note that it only
20908 ;; happens when a region is active: `org-ctrl-c-minus'
20909 ;; would call `org-cycle-list-bullet' otherwise.
20911 (while (< (point) end)
20912 (when (org-at-item-p)
20913 (skip-chars-forward " \t")
20914 (delete-region (point) (match-end 0)))
20916 ;; Case 2. Start at an heading: convert to items.
20917 ((org-at-heading-p)
20918 (let* ((bul (org-list-bullet-string "-"))
20919 (bul-len (length bul))
20920 (done (org-entry-is-done-p))
20921 (todo (org-entry-is-todo-p))
20922 ;; Indentation of the first heading. It should be
20923 ;; relative to the indentation of its parent, if any.
20924 (start-ind (save-excursion
20926 ((not org-adapt-indentation) 0)
20927 ((not (outline-previous-heading)) 0)
20928 (t (length (match-string 0))))))
20929 ;; Level of first heading. Further headings will be
20930 ;; compared to it to determine hierarchy in the list.
20931 (ref-level (org-reduced-level (org-outline-level))))
20932 (when (or done todo) (org-todo ""))
20933 (while (< (point) end)
20934 (let* ((level (org-reduced-level (org-outline-level)))
20935 (delta (max 0 (- level ref-level))))
20936 ;; If current headline is less indented than the first
20937 ;; one, set it as reference, in order to preserve
20939 (when (< level ref-level) (setq ref-level level))
20940 (replace-match bul t t)
20941 (org-indent-line-to (+ start-ind (* delta bul-len)))
20942 (when (or done todo)
20943 (let* ((struct (org-list-struct))
20944 (old (copy-tree struct)))
20945 (org-list-set-checkbox (line-beginning-position)
20947 (if done "[X]" "[ ]"))
20948 (org-list-write-struct struct
20949 (org-list-parents-alist struct)
20951 ;; Ensure all text down to END (or SECTION-END) belongs
20952 ;; to the newly created item.
20953 (let ((section-end (save-excursion
20954 (or (outline-next-heading) (point)))))
20956 (funcall shift-text
20957 (+ start-ind (* (1+ delta) bul-len))
20958 (min end section-end)))))))
20959 ;; Case 3. Normal line with ARG: make the first line of region
20960 ;; an item, and shift indentation of others lines to
20961 ;; set them as item's body.
20962 (arg (let* ((bul (org-list-bullet-string "-"))
20963 (bul-len (length bul))
20964 (ref-ind (org-get-indentation)))
20965 (skip-chars-forward " \t")
20968 (while (< (point) end)
20969 ;; Ensure that lines less indented than first one
20970 ;; still get included in item body.
20971 (funcall shift-text
20972 (+ ref-ind bul-len)
20973 (min end (save-excursion (or (outline-next-heading)
20976 ;; Case 4. Normal line without ARG: turn each non-item line
20979 (while (< (point) end)
20980 (unless (or (org-at-heading-p) (org-at-item-p))
20981 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
20983 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
20984 (forward-line))))))))
20986 (defun org-toggle-heading (&optional nstars)
20987 "Convert headings to normal text, or items or text to headings.
20988 If there is no active region, only convert the current line.
20990 With a \\[universal-argument] prefix, convert the whole list at
20991 point into heading.
20995 - If the first non blank line is a headline, remove the stars
20996 from all headlines in the region.
20998 - If it is a normal line, turn each and every normal line (i.e.,
20999 not an heading or an item) in the region into headings. If you
21000 want to convert only the first line of this region, use one
21001 universal prefix argument.
21003 - If it is a plain list item, turn all plain list items into headings.
21005 When converting a line into a heading, the number of stars is chosen
21006 such that the lines become children of the current entry. However,
21007 when a numeric prefix argument is given, its value determines the
21008 number of stars to add."
21012 ;; Return beginning of first non-blank line, starting from
21017 (while (org-at-comment-p) (forward-line))
21018 (skip-chars-forward " \r\t\n")
21021 ;; Determine boundaries of changes. If a universal prefix has
21022 ;; been given, put the list in a region. If region ends at a bol,
21023 ;; do not consider the last line to be in the region.
21025 (when (and current-prefix-arg (org-at-item-p))
21026 (if (listp current-prefix-arg) (setq current-prefix-arg 1))
21027 (org-mark-element))
21029 (if (org-region-active-p)
21030 (setq beg (funcall skip-blanks (region-beginning))
21031 end (copy-marker (save-excursion
21032 (goto-char (region-end))
21033 (if (bolp) (point) (point-at-eol)))))
21034 (setq beg (funcall skip-blanks (point-at-bol))
21035 end (copy-marker (point-at-eol))))
21036 ;; Ensure inline tasks don't count as headings.
21037 (org-with-limited-levels
21041 ;; Case 1. Started at an heading: de-star headings.
21042 ((org-at-heading-p)
21043 (while (< (point) end)
21044 (when (org-at-heading-p t)
21045 (looking-at org-outline-regexp) (replace-match "")
21048 ;; Case 2. Started at an item: change items into headlines.
21049 ;; One star will be added by `org-list-to-subtree'.
21051 (let* ((stars (make-string
21052 ;; subtract the star that will be added again by
21053 ;; `org-list-to-subtree'
21054 (if (numberp nstars) (1- nstars)
21055 (or (org-current-level) 0))
21058 (cond (nstars "") ; stars from prefix only
21059 ((equal stars "") "") ; before first heading
21060 (org-odd-levels-only "*") ; inside heading, odd
21061 (t "")))) ; inside heading, oddeven
21062 (while (< (point) end)
21063 (when (org-at-item-p)
21064 ;; Pay attention to cases when region ends before list.
21065 (let* ((struct (org-list-struct))
21066 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
21068 (narrow-to-region (point) list-end)
21070 (org-list-to-subtree
21071 (org-list-parse-list t)
21072 '(:istart (concat stars add-stars (funcall get-stars depth))
21073 :icount (concat stars add-stars (funcall get-stars depth)))))))
21076 ;; Case 3. Started at normal text: make every line an heading,
21077 ;; skipping headlines and items.
21080 (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
21082 (cond (nstars "") ; stars from prefix only
21083 ((equal stars "") "*") ; before first heading
21084 (org-odd-levels-only "**") ; inside heading, odd
21085 (t "*"))) ; inside heading, oddeven
21086 (rpl (concat stars add-stars " "))
21087 (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
21088 (while (< (point) (if (equal nstars '(4)) lend end))
21089 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
21090 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
21091 (replace-match (concat rpl (match-string 2))) (setq toggled t))
21092 (forward-line)))))))
21093 (unless toggled (message "Cannot toggle heading from here"))))
21095 (defun org-meta-return (&optional arg)
21096 "Insert a new heading or wrap a region in a table.
21097 Calls `org-insert-heading' or `org-table-wrap-region', depending
21098 on context. See the individual commands for more information."
21100 (org-check-before-invisible-edit 'insert)
21101 (or (run-hook-with-args-until-success 'org-metareturn-hook)
21102 (let* ((element (org-element-at-point))
21103 (type (org-element-type element)))
21104 (when (eq type 'table-row)
21105 (setq element (org-element-property :parent element))
21106 (setq type 'table))
21107 (if (and (eq type 'table)
21108 (eq (org-element-property :type element) 'org)
21109 (>= (point) (org-element-property :contents-begin element))
21110 (< (point) (org-element-property :contents-end element)))
21111 (call-interactively 'org-table-wrap-region)
21112 (call-interactively 'org-insert-heading)))))
21116 (defsubst org-in-subtree-not-table-p ()
21117 "Are we in a subtree and not in a table?"
21118 (and (not (org-before-first-heading-p))
21119 (not (org-at-table-p))))
21121 ;; Define the Org-mode menus
21122 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
21124 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
21125 ["Next Field" org-cycle (org-at-table-p)]
21126 ["Previous Field" org-shifttab (org-at-table-p)]
21127 ["Next Row" org-return (org-at-table-p)]
21129 ["Blank Field" org-table-blank-field (org-at-table-p)]
21130 ["Edit Field" org-table-edit-field (org-at-table-p)]
21131 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
21134 ["Move Column Left" org-metaleft (org-at-table-p)]
21135 ["Move Column Right" org-metaright (org-at-table-p)]
21136 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
21137 ["Insert Column" org-shiftmetaright (org-at-table-p)])
21139 ["Move Row Up" org-metaup (org-at-table-p)]
21140 ["Move Row Down" org-metadown (org-at-table-p)]
21141 ["Delete Row" org-shiftmetaup (org-at-table-p)]
21142 ["Insert Row" org-shiftmetadown (org-at-table-p)]
21143 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
21145 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
21147 ["Copy Rectangle" org-copy-special (org-at-table-p)]
21148 ["Cut Rectangle" org-cut-special (org-at-table-p)]
21149 ["Paste Rectangle" org-paste-special (org-at-table-p)]
21150 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
21153 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
21154 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
21155 ["Edit Formulas" org-edit-special (org-at-table-p)]
21157 ["Recalculate line" org-table-recalculate (org-at-table-p)]
21158 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
21159 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
21161 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
21163 ["Sum Column/Rectangle" org-table-sum
21164 (or (org-at-table-p) (org-region-active-p))]
21165 ["Which Column?" org-table-current-column (org-at-table-p)])
21167 org-table-toggle-formula-debugger
21168 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
21169 ["Show Col/Row Numbers"
21170 org-table-toggle-coordinate-overlays
21172 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
21174 ["Create" org-table-create (and (not (org-at-table-p))
21175 org-enable-table-editor)]
21176 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
21177 ["Import from File" org-table-import (not (org-at-table-p))]
21178 ["Export to File" org-table-export (org-at-table-p)]
21180 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
21182 (easy-menu-define org-org-menu org-mode-map "Org menu"
21185 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
21186 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
21187 ["Sparse Tree..." org-sparse-tree t]
21188 ["Reveal Context" org-reveal t]
21189 ["Show All" show-all t]
21191 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
21193 ["New Heading" org-insert-heading t]
21194 ("Navigate Headings"
21195 ["Up" outline-up-heading t]
21196 ["Next" outline-next-visible-heading t]
21197 ["Previous" outline-previous-visible-heading t]
21198 ["Next Same Level" outline-forward-same-level t]
21199 ["Previous Same Level" outline-backward-same-level t]
21201 ["Jump" org-goto t])
21203 ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
21205 ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
21206 ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
21208 ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
21209 ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
21210 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
21212 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
21214 ["Copy visible text" org-copy-visible t]
21216 ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
21217 ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
21218 ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
21219 ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
21221 ["Sort Region/Children" org-sort t]
21223 ["Convert to odd levels" org-convert-to-odd-levels t]
21224 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
21226 ["Emphasis..." org-emphasize t]
21227 ["Edit Source Example" org-edit-special t]
21229 ["Footnote new/jump" org-footnote-action t]
21230 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
21232 ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
21234 ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
21235 ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
21236 ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
21240 ["Store Link (Global)" org-store-link t]
21241 ["Find existing link to here" org-occur-link-in-agenda-files t]
21242 ["Insert Link" org-insert-link t]
21243 ["Follow Link" org-open-at-point t]
21245 ["Next link" org-next-link t]
21246 ["Previous link" org-previous-link t]
21248 ["Descriptive Links"
21249 org-toggle-link-display
21251 :selected org-descriptive-links
21254 org-toggle-link-display
21256 :selected (not org-descriptive-links)])
21259 ["TODO/DONE/-" org-todo t]
21261 ["Next keyword" org-shiftright (org-at-heading-p)]
21262 ["Previous keyword" org-shiftleft (org-at-heading-p)]
21263 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
21264 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
21265 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
21266 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
21267 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
21269 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
21270 :selected org-enforce-todo-dependencies :style toggle :active t]
21271 "Settings for tree at point"
21272 ["Do Children sequentially" org-toggle-ordered-property :style radio
21273 :selected (org-entry-get nil "ORDERED")
21274 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
21275 ["Do Children parallel" org-toggle-ordered-property :style radio
21276 :selected (not (org-entry-get nil "ORDERED"))
21277 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
21279 ["Set Priority" org-priority t]
21280 ["Priority Up" org-shiftup t]
21281 ["Priority Down" org-shiftdown t]
21283 ["Get news from all feeds" org-feed-update-all t]
21284 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
21285 ["Customize feeds" (customize-variable 'org-feed-alist) t])
21286 ("TAGS and Properties"
21287 ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
21288 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
21290 ["Set property" org-set-property (not (org-before-first-heading-p))]
21291 ["Column view of properties" org-columns t]
21292 ["Insert Column View DBlock" org-insert-columns-dblock t])
21293 ("Dates and Scheduling"
21294 ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
21295 ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
21297 ["1 Day Later" org-shiftright (org-at-timestamp-p)]
21298 ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
21299 ["1 ... Later" org-shiftup (org-at-timestamp-p)]
21300 ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
21301 ["Compute Time Range" org-evaluate-time-range t]
21302 ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
21303 ["Deadline" org-deadline (not (org-before-first-heading-p))]
21305 ["Custom time format" org-toggle-time-stamp-overlays
21306 :style radio :selected org-display-custom-times]
21308 ["Goto Calendar" org-goto-calendar t]
21309 ["Date from Calendar" org-date-from-calendar t]
21311 ["Start/Restart Timer" org-timer-start t]
21312 ["Pause/Continue Timer" org-timer-pause-or-continue t]
21313 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
21314 ["Insert Timer String" org-timer t]
21315 ["Insert Timer Item" org-timer-item t])
21317 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
21318 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
21319 ["Clock out" org-clock-out t]
21320 ["Clock cancel" org-clock-cancel t]
21322 ["Mark as default task" org-clock-mark-default-task t]
21323 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
21324 ["Goto running clock" org-clock-goto t]
21326 ["Display times" org-clock-display t]
21327 ["Create clock table" org-clock-report t]
21329 ["Record DONE time"
21330 (progn (setq org-log-done (not org-log-done))
21331 (message "Switching to %s will %s record a timestamp"
21332 (car org-done-keywords)
21333 (if org-log-done "automatically" "not")))
21334 :style toggle :selected org-log-done])
21336 ["Agenda Command..." org-agenda t]
21337 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
21338 ("File List for Agenda")
21339 ("Special views current file"
21340 ["TODO Tree" org-show-todo-tree t]
21341 ["Check Deadlines" org-check-deadlines t]
21342 ["Timeline" org-timeline t]
21343 ["Tags/Property tree" org-match-sparse-tree t])
21345 ["Export/Publish..." org-export-dispatch t]
21347 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
21348 :selected org-cdlatex-mode]
21349 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
21350 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
21351 ["Modify math symbol" org-cdlatex-math-modify
21352 (org-inside-LaTeX-fragment-p)]
21353 ["Insert citation" org-reftex-citation t]
21355 ["Template for BEAMER" (org-beamer-insert-options-template) t])
21358 ["Push Files and Views" org-mobile-push t]
21359 ["Get Captured and Flagged" org-mobile-pull t]
21360 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
21362 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
21365 ["Show Version" org-version t]
21366 ["Info Documentation" org-info t])
21368 ["Browse Org Group" org-customize t]
21370 ["Expand This Menu" org-create-customize-menu
21371 (fboundp 'customize-menu-create)])
21372 ["Send bug report" org-submit-bug-report t]
21375 ["Refresh setup current buffer" org-mode-restart t]
21376 ["Reload Org (after update)" org-reload t]
21377 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
21380 (defun org-info (&optional node)
21381 "Read documentation for Org-mode in the info system.
21382 With optional NODE, go directly to that node."
21384 (info (format "(org)%s" (or node ""))))
21387 (defun org-submit-bug-report ()
21388 "Submit a bug report on Org-mode via mail.
21390 Don't hesitate to report any problems or inaccurate documentation.
21392 If you don't have setup sending mail from (X)Emacs, please copy the
21393 output buffer into your mail program, as it gives us important
21394 information about your Org-mode version and configuration."
21396 (require 'reporter)
21397 (org-load-modules-maybe)
21398 (org-require-autoloaded-modules)
21399 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
21400 (reporter-submit-bug-report
21401 "emacs-orgmode@gnu.org"
21402 (org-version nil 'full)
21404 (save-window-excursion
21405 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
21406 (delete-other-windows)
21408 (insert "You are about to submit a bug report to the Org-mode mailing list.
21410 We would like to add your full Org-mode and Outline configuration to the
21411 bug report. This greatly simplifies the work of the maintainer and
21412 other experts on the mailing list.
21414 HOWEVER, some variables you have customized may contain private
21415 information. The names of customers, colleagues, or friends, might
21416 appear in the form of file names, tags, todo states, or search strings.
21417 If you answer yes to the prompt, you might want to check and remove
21418 such private information before sending the email.")
21419 (add-text-properties (point-min) (point-max) '(face org-warning))
21420 (when (yes-or-no-p "Include your Org-mode configuration ")
21424 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
21425 (or (and (symbol-value v)
21426 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
21428 (get v 'custom-type) (get v 'standard-value)
21429 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
21431 (kill-buffer (get-buffer "*Warn about privacy*"))
21434 "Remember to cover the basics, that is, what you expected to happen and
21435 what in fact did happen. You don't know how to make a good report? See
21437 http://orgmode.org/manual/Feedback.html#Feedback
21439 Your bug report will be posted to the Org-mode mailing list.
21440 ------------------------------------------------------------------------")
21442 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
21443 (replace-match "\\1Bug: \\3 [\\2]")))))
21446 (defun org-install-agenda-files-menu ()
21447 (let ((bl (buffer-list)))
21450 (set-buffer (pop bl))
21451 (if (derived-mode-p 'org-mode) (setq bl nil)))
21452 (when (derived-mode-p 'org-mode)
21454 '("Org") "File List for Agenda"
21457 ["Edit File List" (org-edit-agenda-file-list) t]
21458 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
21459 ["Remove Current File from List" org-remove-file t]
21460 ["Cycle through agenda files" org-cycle-agenda-files t]
21461 ["Occur in all agenda files" org-occur-in-agenda-files t]
21463 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
21467 (defun org-require-autoloaded-modules ()
21470 '(org-agenda org-archive org-attach org-clock org-colview org-id
21471 org-table org-timer)))
21474 (defun org-reload (&optional uncompiled)
21475 "Reload all org lisp files.
21476 With prefix arg UNCOMPILED, load the uncompiled versions."
21478 (require 'loadhist)
21479 (let* ((org-dir (org-find-library-dir "org"))
21480 (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
21481 (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
21482 (remove-re (mapconcat 'identity
21483 (mapcar (lambda (f) (concat "^" f "$"))
21484 (list (if (featurep 'xemacs)
21486 "org-colview-xemacs")
21487 "org" "org-loaddefs" "org-version"))
21489 (feats (delete-dups
21490 (mapcar 'file-name-sans-extension
21491 (mapcar 'file-name-nondirectory
21493 (mapcar 'feature-file
21500 (if (and (string-match feature-re f)
21501 (not (string-match remove-re f)))
21505 (list "org-version" "org")))
21506 (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
21507 (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
21508 load-uncore load-misses)
21511 (mapcar (lambda (f)
21512 (or (org-load-noerror-mustsuffix (concat org-dir f))
21513 (and (string= org-dir contrib-dir)
21514 (org-load-noerror-mustsuffix (concat contrib-dir f)))
21515 (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
21516 (add-to-list 'load-uncore f 'append)
21521 (message "The following feature%s found in load-path, please check if that's correct:\n%s"
21522 (if (> (length load-uncore) 1) "s were" " was") load-uncore))
21524 (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
21525 (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
21526 (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
21529 (defun org-customize ()
21530 "Call the customize function with org as argument."
21532 (org-load-modules-maybe)
21533 (org-require-autoloaded-modules)
21534 (customize-browse 'org))
21536 (defun org-create-customize-menu ()
21537 "Create a full customization menu for Org-mode, insert it into the menu."
21539 (org-load-modules-maybe)
21540 (org-require-autoloaded-modules)
21541 (if (fboundp 'customize-menu-create)
21544 '("Org") "Customize"
21545 `(["Browse Org group" org-customize t]
21547 ,(customize-menu-create 'org)
21548 ["Set" Custom-set t]
21549 ["Save" Custom-save t]
21550 ["Reset to Current" Custom-reset-current t]
21551 ["Reset to Saved" Custom-reset-saved t]
21552 ["Reset to Standard Settings" Custom-reset-standard t]))
21553 (message "\"Org\"-menu now contains full customization menu"))
21554 (error "Cannot expand menu (outdated version of cus-edit.el)")))
21556 ;;;; Miscellaneous stuff
21558 ;;; Generally useful functions
21560 (defsubst org-get-at-eol (property n)
21561 "Get text property PROPERTY at the end of line less N characters."
21562 (get-text-property (- (point-at-eol) n) property))
21564 (defun org-find-text-property-in-string (prop s)
21565 "Return the first non-nil value of property PROP in string S."
21566 (or (get-text-property 0 prop s)
21567 (get-text-property (or (next-single-property-change 0 prop s) 0)
21570 (defun org-display-warning (message) ;; Copied from Emacs-Muse
21571 "Display the given MESSAGE as a warning."
21572 (if (fboundp 'display-warning)
21573 (display-warning 'org message
21574 (if (featurep 'xemacs) 'warning :warning))
21575 (let ((buf (get-buffer-create "*Org warnings*")))
21576 (with-current-buffer buf
21577 (goto-char (point-max))
21578 (insert "Warning (Org): " message)
21581 (display-buffer buf)
21584 (defun org-eval (form)
21585 "Eval FORM and return result."
21586 (condition-case error
21588 (error (format "%%![Error: %s]" error))))
21590 (defun org-in-clocktable-p ()
21591 "Check if the cursor is in a clocktable."
21592 (let ((pos (point)) start)
21595 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
21596 (setq start (match-beginning 0))
21597 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
21598 (>= (match-end 0) pos)
21601 (defun org-in-verbatim-emphasis ()
21603 (and (org-in-regexp org-emph-re 2)
21604 (>= (point) (match-beginning 3))
21605 (<= (point) (match-end 4))
21606 (member (match-string 3) '("=" "~")))))
21608 (defun org-goto-marker-or-bmk (marker &optional bookmark)
21609 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
21610 (if (and marker (marker-buffer marker)
21611 (buffer-live-p (marker-buffer marker)))
21613 (org-pop-to-buffer-same-window (marker-buffer marker))
21614 (if (or (> marker (point-max)) (< marker (point-min)))
21617 (org-show-context 'org-goto))
21619 (bookmark-jump bookmark)
21620 (error "Cannot find location"))))
21622 (defun org-quote-csv-field (s)
21623 "Quote field for inclusion in CSV material."
21624 (if (string-match "[\",]" s)
21625 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
21628 (defun org-force-self-insert (N)
21629 "Needed to enforce self-insert under remapping."
21631 (self-insert-command N))
21633 (defun org-string-width (s)
21634 "Compute width of string, ignoring invisible characters.
21635 This ignores character with invisibility property `org-link', and also
21636 characters with property `org-cwidth', because these will become invisible
21637 upon the next fontification round."
21639 (when (or (eq t buffer-invisibility-spec)
21640 (assq 'org-link buffer-invisibility-spec))
21641 (while (setq b (text-property-any 0 (length s)
21642 'invisible 'org-link s))
21643 (setq s (concat (substring s 0 b)
21644 (substring s (or (next-single-property-change
21645 b 'invisible s) (length s)))))))
21646 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
21647 (setq s (concat (substring s 0 b)
21648 (substring s (or (next-single-property-change
21649 b 'org-cwidth s) (length s))))))
21650 (setq l (string-width s) b -1)
21651 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
21652 (setq l (- l (get-text-property b 'org-dwidth-n s))))
21655 (defun org-shorten-string (s maxlength)
21656 "Shorten string S so tht it is no longer than MAXLENGTH characters.
21657 If the string is shorter or has length MAXLENGTH, just return the
21658 original string. If it is longer, the functions finds a space in the
21659 string, breaks this string off at that locations and adds three dots
21660 as ellipsis. Including the ellipsis, the string will not be longer
21661 than MAXLENGTH. If finding a good breaking point in the string does
21662 not work, the string is just chopped off in the middle of a word
21664 (if (<= (length s) maxlength)
21666 (let* ((n (max (- maxlength 4) 1))
21667 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
21668 (if (string-match re s)
21669 (concat (match-string 1 s) "...")
21670 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
21672 (defun org-get-indentation (&optional line)
21673 "Get the indentation of the current line, interpreting tabs.
21674 When LINE is given, assume it represents a line and compute its indentation."
21676 (if (string-match "^ *" (org-remove-tabs line))
21679 (beginning-of-line 1)
21680 (skip-chars-forward " \t")
21681 (current-column))))
21683 (defun org-get-string-indentation (s)
21684 "What indentation has S due to SPACE and TAB at the beginning of the string?"
21685 (let ((n -1) (i 0) (w tab-width) c)
21687 (while (< (setq n (1+ n)) (length s))
21688 (setq c (aref s n))
21689 (cond ((= c ?\ ) (setq i (1+ i)))
21690 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
21691 (t (throw 'exit t)))))
21694 (defun org-remove-tabs (s &optional width)
21695 "Replace tabulators in S with spaces.
21696 Assumes that s is a single line, starting in column 0."
21697 (setq width (or width tab-width))
21698 (while (string-match "\t" s)
21699 (setq s (replace-match
21701 (- (* width (/ (+ (match-beginning 0) width) width))
21702 (match-beginning 0)) ?\ )
21706 (defun org-fix-indentation (line ind)
21707 "Fix indentation in LINE.
21708 IND is a cons cell with target and minimum indentation.
21709 If the current indentation in LINE is smaller than the minimum,
21710 leave it alone. If it is larger than ind, set it to the target."
21711 (let* ((l (org-remove-tabs line))
21712 (i (org-get-indentation l))
21713 (i1 (car ind)) (i2 (cdr ind)))
21714 (if (>= i i2) (setq l (substring line i2)))
21716 (concat (make-string i1 ?\ ) l)
21719 (defun org-remove-indentation (code &optional n)
21720 "Remove the maximum common indentation from the lines in CODE.
21721 N may optionally be the number of spaces to remove."
21724 (org-do-remove-indentation n)
21727 (defun org-do-remove-indentation (&optional n)
21728 "Remove the maximum common indentation from the buffer."
21729 (untabify (point-min) (point-max))
21730 (let ((min 10000) re)
21733 (goto-char (point-min))
21734 (while (re-search-forward "^ *[^ \n]" nil t)
21735 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
21736 (unless (or (= min 0) (= min 10000))
21737 (setq re (format "^ \\{%d\\}" min))
21738 (goto-char (point-min))
21739 (while (re-search-forward re nil t)
21744 (defun org-fill-template (template alist)
21745 "Find each %key of ALIST in TEMPLATE and replace it."
21746 (let ((case-fold-search nil)
21748 (setq alist (sort (copy-sequence alist)
21749 (lambda (a b) (< (length (car a)) (length (car b))))))
21750 (while (setq entry (pop alist))
21752 (replace-regexp-in-string
21753 (concat "%" (regexp-quote (car entry)))
21754 (or (cdr entry) "") template t t)))
21757 (defun org-base-buffer (buffer)
21758 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
21761 (or (buffer-base-buffer buffer)
21764 (defun org-wrap (string &optional width lines)
21765 "Wrap string to either a number of lines, or a width in characters.
21766 If WIDTH is non-nil, the string is wrapped to that width, however many lines
21767 that costs. If there is a word longer than WIDTH, the text is actually
21768 wrapped to the length of that word.
21769 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
21770 many lines, whatever width that takes.
21771 The return value is a list of lines, without newlines at the end."
21772 (let* ((words (org-split-string string "[ \t\n]+"))
21773 (maxword (apply 'max (mapcar 'org-string-width words)))
21776 (org-do-wrap words (max maxword width)))
21779 (setq ll (org-do-wrap words maxword))
21780 (if (<= (length ll) lines)
21783 (while (> (length ll) lines)
21785 (setq ll (org-do-wrap words w)))
21787 (t (error "Cannot wrap this")))))
21789 (defun org-do-wrap (words width)
21790 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
21793 (setq line (pop words))
21794 (while (and words (< (+ (length line) (length (car words))) width))
21795 (setq line (concat line " " (pop words))))
21796 (setq lines (push line lines)))
21799 (defun org-split-string (string &optional separators)
21800 "Splits STRING into substrings at SEPARATORS.
21801 No empty strings are returned if there are matches at the beginning
21802 and end of string."
21803 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
21807 (while (and (string-match rexp string
21809 (= start (match-beginning 0))
21810 (< start (length string)))
21812 (< (match-beginning 0) (length string)))
21814 (or (eq (match-beginning 0) 0)
21815 (and (eq (match-beginning 0) (match-end 0))
21816 (eq (match-beginning 0) start))
21818 (cons (substring string start (match-beginning 0))
21820 (setq start (match-end 0)))
21821 (or (eq start (length string))
21823 (cons (substring string start)
21827 (defun org-quote-vert (s)
21828 "Replace \"|\" with \"\\vert\"."
21829 (while (string-match "|" s)
21830 (setq s (replace-match "\\vert" t t s)))
21833 (defun org-uuidgen-p (s)
21834 "Is S an ID created by UUIDGEN?"
21835 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
21837 (defun org-in-src-block-p (&optional inside)
21838 "Whether point is in a code source block.
21839 When INSIDE is non-nil, don't consider we are within a src block
21840 when point is at #+BEGIN_SRC or #+END_SRC."
21841 (let ((case-fold-search t) ov)
21842 (or (and (eq (get-char-property (point) 'src-block) t))
21846 (beginning-of-line)
21847 (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
21849 (defun org-context ()
21850 "Return a list of contexts of the current cursor position.
21851 If several contexts apply, all are returned.
21852 Each context entry is a list with a symbol naming the context, and
21853 two positions indicating start and end of the context. Possible
21856 :headline anywhere in a headline
21857 :headline-stars on the leading stars in a headline
21858 :todo-keyword on a TODO keyword (including DONE) in a headline
21859 :tags on the TAGS in a headline
21860 :priority on the priority cookie in a headline
21861 :item on the first line of a plain list item
21862 :item-bullet on the bullet/number of a plain list item
21863 :checkbox on the checkbox in a plain list item
21864 :table in an org-mode table
21865 :table-special on a special filed in a table
21866 :table-table in a table.el table
21867 :clocktable in a clocktable
21868 :src-block in a source block
21869 :link on a hyperlink
21870 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
21871 :target on a <<target>>
21872 :radio-target on a <<<radio-target>>>
21873 :latex-fragment on a LaTeX fragment
21874 :latex-preview on a LaTeX fragment with overlaid preview image
21876 This function expects the position to be visible because it uses font-lock
21877 faces as a help to recognize the following contexts: :table-special, :link,
21879 (let* ((f (get-text-property (point) 'face))
21880 (faces (if (listp f) f (list f)))
21881 (case-fold-search t)
21882 (p (point)) clist o)
21883 ;; First the large context
21885 ((org-at-heading-p t)
21886 (push (list :headline (point-at-bol) (point-at-eol)) clist)
21888 (beginning-of-line 1)
21889 (looking-at org-todo-line-tags-regexp))
21890 (push (org-point-in-group p 1 :headline-stars) clist)
21891 (push (org-point-in-group p 2 :todo-keyword) clist)
21892 (push (org-point-in-group p 4 :tags) clist))
21894 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
21895 (if (looking-at "\\[#[A-Z0-9]\\]")
21896 (push (org-point-in-group p 0 :priority) clist)))
21899 (push (org-point-in-group p 2 :item-bullet) clist)
21900 (push (list :item (point-at-bol)
21901 (save-excursion (org-end-of-item) (point)))
21903 (and (org-at-item-checkbox-p)
21904 (push (org-point-in-group p 0 :checkbox) clist)))
21907 (push (list :table (org-table-begin) (org-table-end)) clist)
21908 (if (memq 'org-formula faces)
21909 (push (list :table-special
21910 (previous-single-property-change p 'face)
21911 (next-single-property-change p 'face)) clist)))
21912 ((org-at-table-p 'any)
21913 (push (list :table-table) clist)))
21916 (let ((case-fold-search t))
21917 ;; New the "medium" contexts: clocktables, source blocks
21918 (cond ((org-in-clocktable-p)
21919 (push (list :clocktable
21920 (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
21921 (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
21922 (match-beginning 1))
21923 (and (re-search-forward "[ \t]*#\\+END:?" nil t)
21924 (match-end 0))) clist))
21925 ((org-in-src-block-p)
21926 (push (list :src-block
21927 (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
21928 (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
21929 (match-beginning 1))
21930 (and (search-forward "#+END_SRC" nil t)
21931 (match-beginning 0))) clist))))
21934 ;; Now the small context
21936 ((org-at-timestamp-p)
21937 (push (org-point-in-group p 0 :timestamp) clist))
21938 ((memq 'org-link faces)
21940 (previous-single-property-change p 'face)
21941 (next-single-property-change p 'face)) clist))
21942 ((memq 'org-special-keyword faces)
21943 (push (list :keyword
21944 (previous-single-property-change p 'face)
21945 (next-single-property-change p 'face)) clist))
21947 (push (org-point-in-group p 0 :target) clist)
21948 (goto-char (1- (match-beginning 0)))
21949 (if (looking-at org-radio-target-regexp)
21950 (push (org-point-in-group p 0 :radio-target) clist))
21952 ((setq o (car (delq nil
21955 (if (memq x org-latex-fragment-image-overlays) x))
21956 (overlays-at (point))))))
21957 (push (list :latex-fragment
21958 (overlay-start o) (overlay-end o)) clist)
21959 (push (list :latex-preview
21960 (overlay-start o) (overlay-end o)) clist))
21961 ((org-inside-LaTeX-fragment-p)
21962 ;; FIXME: positions wrong.
21963 (push (list :latex-fragment (point) (point)) clist)))
21965 (setq clist (nreverse (delq nil clist)))
21968 ;; FIXME: Compare with at-regexp-p Do we need both?
21969 (defun org-in-regexp (re &optional nlines visually)
21970 "Check if point is inside a match of regexp.
21971 Normally only the current line is checked, but you can include NLINES extra
21972 lines both before and after point into the search.
21973 If VISUALLY is set, require that the cursor is not after the match but
21974 really on, so that the block visually is on the match."
21976 (let ((pos (point))
21977 (eol (point-at-eol (+ 1 (or nlines 0))))
21978 (inc (if visually 1 0)))
21980 (beginning-of-line (- 1 (or nlines 0)))
21981 (while (re-search-forward re eol t)
21982 (if (and (<= (match-beginning 0) pos)
21983 (>= (+ inc (match-end 0)) pos))
21984 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
21986 (defun org-at-regexp-p (regexp)
21987 "Is point inside a match of REGEXP in the current line?"
21990 (let ((pos (point)) (end (point-at-eol)))
21991 (beginning-of-line 1)
21992 (while (re-search-forward regexp end t)
21993 (if (and (<= (match-beginning 0) pos)
21994 (>= (match-end 0) pos))
21998 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
21999 "Non-nil when point is between matches of START-RE and END-RE.
22001 Also return a non-nil value when point is on one of the matches.
22003 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
22004 buffer positions. Default values are the positions of headlines
22005 surrounding the point.
22007 The functions returns a cons cell whose car (resp. cdr) is the
22008 position before START-RE (resp. after END-RE)."
22010 (let ((pos (point))
22011 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
22012 (limit-down (or lim-down (save-excursion (outline-next-heading))))
22015 ;; Point is on a block when on START-RE or if START-RE can be
22016 ;; found before it...
22017 (and (or (org-at-regexp-p start-re)
22018 (re-search-backward start-re limit-up t))
22019 (setq beg (match-beginning 0))
22020 ;; ... and END-RE after it...
22021 (goto-char (match-end 0))
22022 (re-search-forward end-re limit-down t)
22023 (> (setq end (match-end 0)) pos)
22024 ;; ... without another START-RE in-between.
22025 (goto-char (match-beginning 0))
22026 (not (re-search-backward start-re (1+ beg) t))
22028 (cons beg end))))))
22030 (defun org-in-block-p (names)
22031 "Non-nil when point belongs to a block whose name belongs to NAMES.
22033 NAMES is a list of strings containing names of blocks.
22035 Return first block name matched, or nil. Beware that in case of
22036 nested blocks, the returned name may not belong to the closest
22040 (let ((case-fold-search t)
22041 (lim-up (save-excursion (outline-previous-heading)))
22042 (lim-down (save-excursion (outline-next-heading))))
22043 (mapc (lambda (name)
22044 (let ((n (regexp-quote name)))
22045 (when (org-between-regexps-p
22046 (concat "^[ \t]*#\\+begin_" n)
22047 (concat "^[ \t]*#\\+end_" n)
22053 (defun org-in-drawer-p ()
22054 "Non-nil if point is within a drawer.
22055 If point is within a drawer, return it, as parsed data."
22056 (let ((element (save-match-data (org-element-at-point))))
22057 (while (and element (not (memq (org-element-type element)
22058 '(drawer property-drawer))))
22059 (setq element (org-element-property :parent element)))
22062 (defun org-occur-in-agenda-files (regexp &optional nlines)
22063 "Call `multi-occur' with buffers for all agenda files."
22064 (interactive "sOrg-files matching: \np")
22065 (let* ((files (org-agenda-files))
22066 (tnames (mapcar 'file-truename files))
22067 (extra org-agenda-text-search-extra-files)
22069 (when (eq (car extra) 'agenda-archives)
22070 (setq extra (cdr extra))
22071 (setq files (org-add-archive-files files)))
22072 (while (setq f (pop extra))
22073 (unless (member (file-truename f) tnames)
22074 (add-to-list 'files f 'append)
22075 (add-to-list 'tnames (file-truename f) 'append)))
22077 (mapcar (lambda (x)
22078 (with-current-buffer
22079 (or (get-file-buffer x) (find-file-noselect x))
22085 (if (boundp 'occur-mode-find-occurrence-hook)
22087 (add-hook 'occur-mode-find-occurrence-hook
22089 (when (derived-mode-p 'org-mode)
22092 (defadvice occur-mode-goto-occurrence
22093 (after org-occur-reveal activate)
22094 (and (derived-mode-p 'org-mode) (org-reveal)))
22095 (defadvice occur-mode-goto-occurrence-other-window
22096 (after org-occur-reveal activate)
22097 (and (derived-mode-p 'org-mode) (org-reveal)))
22098 (defadvice occur-mode-display-occurrence
22099 (after org-occur-reveal activate)
22100 (when (derived-mode-p 'org-mode)
22101 (let ((pos (occur-mode-find-occurrence)))
22102 (with-current-buffer (marker-buffer pos)
22107 (defun org-occur-link-in-agenda-files ()
22108 "Create a link and search for it in the agendas.
22109 The link is not stored in `org-stored-links', it is just created
22110 for the search purpose."
22112 (let ((link (condition-case nil
22113 (org-store-link nil)
22114 (error "Unable to create a link to here"))))
22115 (org-occur-in-agenda-files (regexp-quote link))))
22117 (defun org-reverse-string (string)
22118 "Return the reverse of STRING."
22119 (apply 'string (reverse (string-to-list string))))
22121 ;; defsubst org-uniquify must be defined before first use
22123 (defun org-uniquify-alist (alist)
22124 "Merge elements of ALIST with the same key.
22126 For example, in this alist:
22128 \(org-uniquify-alist '((a 1) (b 2) (a 3)))
22129 => '((a 1 3) (b 2))
22131 merge (a 1) and (a 3) into (a 1 3).
22133 The function returns the new ALIST."
22138 (if (not (assoc (car e) rtn))
22140 (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
22141 (setq rtn (assq-delete-all (car e) rtn))
22146 (defun org-delete-all (elts list)
22147 "Remove all elements in ELTS from LIST."
22149 (setq list (delete (pop elts) list)))
22152 (defun org-count (cl-item cl-seq)
22153 "Count the number of occurrences of ITEM in SEQ.
22154 Taken from `count' in cl-seq.el with all keyword arguments removed."
22155 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
22156 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
22157 (while (< cl-start cl-end)
22158 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
22159 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
22160 (setq cl-start (1+ cl-start)))
22163 (defun org-remove-if (predicate seq)
22164 "Remove everything from SEQ that fulfills PREDICATE."
22168 (if (not (funcall predicate e)) (push e res)))
22171 (defun org-remove-if-not (predicate seq)
22172 "Remove everything from SEQ that does not fulfill PREDICATE."
22176 (if (funcall predicate e) (push e res)))
22179 (defun org-reduce (cl-func cl-seq &rest cl-keys)
22180 "Reduce two-argument FUNCTION across SEQ.
22181 Taken from `reduce' in cl-seq.el with all keyword arguments but
22182 \":initial-value\" removed."
22183 (let ((cl-accum (cond ((memq :initial-value cl-keys)
22184 (cadr (memq :initial-value cl-keys)))
22185 (cl-seq (pop cl-seq))
22186 (t (funcall cl-func)))))
22188 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
22191 (defun org-every (pred seq)
22192 "Return true if PREDICATE is true of every element of SEQ.
22193 Adapted from `every' in cl.el."
22195 (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
22198 (defun org-some (pred seq)
22199 "Return true if PREDICATE is true of any element of SEQ.
22200 Adapted from `some' in cl.el."
22202 (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
22205 (defun org-back-over-empty-lines ()
22206 "Move backwards over whitespace, to the beginning of the first empty line.
22207 Returns the number of empty lines passed."
22208 (let ((pos (point)))
22209 (if (cdr (assoc 'heading org-blank-before-new-entry))
22210 (skip-chars-backward " \t\n\r")
22212 (forward-line -1)))
22213 (beginning-of-line 2)
22214 (goto-char (min (point) pos))
22215 (count-lines (point) pos)))
22217 (defun org-skip-whitespace ()
22218 (skip-chars-forward " \t\n\r"))
22220 (defun org-point-in-group (point group &optional context)
22221 "Check if POINT is in match-group GROUP.
22222 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
22223 match. If the match group does not exist or point is not inside it,
22225 (and (match-beginning group)
22226 (>= point (match-beginning group))
22227 (<= point (match-end group))
22229 (list context (match-beginning group) (match-end group))
22232 (defun org-switch-to-buffer-other-window (&rest args)
22233 "Switch to buffer in a second window on the current frame.
22234 In particular, do not allow pop-up frames.
22235 Returns the newly created buffer."
22237 (apply 'switch-to-buffer-other-window args)))
22239 (defun org-combine-plists (&rest plists)
22240 "Create a single property list from all plists in PLISTS.
22241 The process starts by copying the first list, and then setting properties
22242 from the other lists. Settings in the last list are the most significant
22243 ones and overrule settings in the other lists."
22244 (let ((rtn (copy-sequence (pop plists)))
22247 (setq ls (pop plists))
22249 (setq p (pop ls) v (pop ls))
22250 (setq rtn (plist-put rtn p v))))
22253 (defun org-replace-escapes (string table)
22254 "Replace %-escapes in STRING with values in TABLE.
22255 TABLE is an association list with keys like \"%a\" and string values.
22256 The sequences in STRING may contain normal field width and padding information,
22257 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
22258 so values can contain further %-escapes if they are define later in TABLE."
22259 (let ((tbl (copy-alist table))
22260 (case-fold-search nil)
22263 (while (setq e (pop tbl))
22264 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
22265 (when (and (cdr e) (string-match re (cdr e)))
22266 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
22268 (add-text-properties 0 3 (list 'sref sref) safe)
22269 (setcdr e (replace-match safe t t (cdr e)))))
22270 (while (string-match re string)
22271 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
22273 (setq string (replace-match rpl t t string))))
22274 (while (setq pchg (next-property-change pchg string))
22275 (let ((sref (get-text-property pchg 'sref string)))
22276 (when (and sref (string-match "SREF" string pchg))
22277 (setq string (replace-match sref t t string)))))
22280 (defun org-sublist (list start end)
22281 "Return a section of LIST, from START to END.
22282 Counting starts at 1."
22283 (let (rtn (c start))
22284 (setq list (nthcdr (1- start) list))
22285 (while (and list (<= c end))
22286 (push (pop list) rtn)
22290 (defun org-find-base-buffer-visiting (file)
22291 "Like `find-buffer-visiting' but always return the base buffer and
22292 not an indirect buffer."
22293 (let ((buf (or (get-file-buffer file)
22294 (find-buffer-visiting file))))
22296 (or (buffer-base-buffer buf) buf)
22299 (defun org-image-file-name-regexp (&optional extensions)
22300 "Return regexp matching the file names of images.
22301 If EXTENSIONS is given, only match these."
22302 (if (and (not extensions) (fboundp 'image-file-name-regexp))
22303 (image-file-name-regexp)
22304 (let ((image-file-name-extensions
22306 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
22307 "xbm" "xpm" "pbm" "pgm" "ppm"))))
22309 (regexp-opt (nconc (mapcar 'upcase
22310 image-file-name-extensions)
22311 image-file-name-extensions)
22315 (defun org-file-image-p (file &optional extensions)
22316 "Return non-nil if FILE is an image."
22318 (string-match (org-image-file-name-regexp extensions) file)))
22320 (defun org-get-cursor-date (&optional with-time)
22321 "Return the date at cursor in as a time.
22322 This works in the calendar and in the agenda, anywhere else it just
22323 returns the current time.
22324 If WITH-TIME is non-nil, returns the time of the event at point (in
22325 the agenda) or the current time of the day."
22326 (let (date day defd tp tm hod mod)
22328 (setq tp (get-text-property (point) 'time))
22329 (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
22330 (setq hod (string-to-number (match-string 1 tp))
22331 mod (string-to-number (match-string 2 tp))))
22332 (or tp (setq hod (nth 2 (decode-time (current-time)))
22333 mod (nth 1 (decode-time (current-time))))))
22335 ((eq major-mode 'calendar-mode)
22336 (setq date (calendar-cursor-to-date)
22337 defd (encode-time 0 (or mod 0) (or hod 0)
22338 (nth 1 date) (nth 0 date) (nth 2 date))))
22339 ((eq major-mode 'org-agenda-mode)
22340 (setq day (get-text-property (point) 'day))
22342 (setq date (calendar-gregorian-from-absolute day)
22343 defd (encode-time 0 (or mod 0) (or hod 0)
22344 (nth 1 date) (nth 0 date) (nth 2 date))))))
22345 (or defd (current-time))))
22347 (defun org-mark-subtree (&optional up)
22348 "Mark the current subtree.
22349 This puts point at the start of the current subtree, and mark at
22350 the end. If a numeric prefix UP is given, move up into the
22351 hierarchy of headlines by UP levels before marking the subtree."
22353 (org-with-limited-levels
22354 (cond ((org-at-heading-p) (beginning-of-line))
22355 ((org-before-first-heading-p) (user-error "Not in a subtree"))
22356 (t (outline-previous-visible-heading 1))))
22357 (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
22358 (if (org-called-interactively-p 'any)
22359 (call-interactively 'org-mark-element)
22360 (org-mark-element)))
22365 (defun org--get-expected-indentation (element contentsp)
22366 "Expected indentation column for current line, according to ELEMENT.
22367 ELEMENT is an element containing point. CONTENTSP is non-nil
22368 when indentation is to be computed according to contents of
22370 (let ((type (org-element-type element))
22371 (start (org-element-property :begin element)))
22372 (org-with-wide-buffer
22376 (footnote-definition 0)
22377 ((headline inlinetask nil)
22378 (if (not org-adapt-indentation) 0
22379 (let ((level (org-current-level)))
22380 (if level (1+ level) 0))))
22382 (org-list-item-body-column
22383 (org-element-property :post-affiliated element)))
22386 (org-get-indentation))))
22387 ((memq type '(headline inlinetask nil))
22388 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
22389 (org--get-expected-indentation element t)
22391 ((eq type 'footnote-definition) 0)
22392 ;; First paragraph of a footnote definition or an item.
22393 ;; Indent like parent.
22394 ((< (line-beginning-position) start)
22395 (org--get-expected-indentation
22396 (org-element-property :parent element) t))
22397 ;; At first line: indent according to previous sibling, if any,
22398 ;; ignoring footnote definitions and inline tasks, or parent's
22400 ((= (line-beginning-position) start)
22403 (if (= (point-min) start) (throw 'exit 0)
22404 (goto-char (1- start))
22405 (let ((previous (org-element-at-point)))
22406 (while (let ((parent (org-element-property :parent previous)))
22408 (setq previous parent)
22409 (<= (org-element-property :end parent) start))))
22410 (cond ((or (not previous)
22411 (> (org-element-property :end previous) start))
22412 (throw 'exit (org--get-expected-indentation previous t)))
22413 ((memq (org-element-type previous)
22414 '(footnote-definition inlinetask))
22415 (setq start (org-element-property :begin previous)))
22416 (t (goto-char (org-element-property :begin previous))
22417 (throw 'exit (org-get-indentation)))))))))
22418 ;; Otherwise, move to the first non-blank line above.
22420 (beginning-of-line)
22421 (let ((pos (point)))
22422 (skip-chars-backward " \r\t\n")
22424 ;; Two blank lines end a footnote definition or a plain
22425 ;; list. When we indent an empty line after them, the
22426 ;; containing list or footnote definition is over, so it
22427 ;; qualifies as a previous sibling. Therefore, we indent
22428 ;; like its first line.
22429 ((and (memq type '(footnote-definition plain-list))
22430 (> (count-lines (point) pos) 2))
22432 (org-get-indentation))
22433 ;; Line above is the first one of a paragraph at the
22434 ;; beginning of an item or a footnote definition. Indent
22436 ((< (line-beginning-position) start)
22437 (org--get-expected-indentation
22438 (org-element-property :parent element) t))
22439 ;; POS is after contents in a greater element. Indent like
22440 ;; the beginning of the element.
22442 ;; As a special case, if point is at the end of a footnote
22443 ;; definition or an item, indent like the very last element
22445 ((let ((cend (org-element-property :contents-end element)))
22446 (and cend (<= cend pos)))
22447 (if (memq type '(footnote-definition item plain-list))
22448 (org--get-expected-indentation (org-element-at-point) nil)
22450 (org-get-indentation)))
22451 ;; In any other case, indent like the current line.
22452 (t (org-get-indentation)))))))))
22454 (defun org--align-node-property ()
22455 "Align node property at point.
22456 Alignment is done according to `org-property-format', which see."
22457 (when (save-excursion
22458 (beginning-of-line)
22459 (looking-at org-property-re))
22461 (concat (match-string 4)
22462 (format org-property-format (match-string 1) (match-string 3)))
22465 (defun org-indent-line ()
22466 "Indent line depending on context.
22468 Indentation is done according to the following rules:
22470 - Footnote definitions, headlines and inline tasks have to
22473 - On the very first line of an element, consider, in order, the
22474 next rules until one matches:
22476 1. If there's a sibling element before, ignoring footnote
22477 definitions and inline tasks, indent like its first line.
22479 2. If element has a parent, indent like its contents. More
22480 precisely, if parent is an item, indent after the
22481 description part, if any, or the bullet (see
22482 ``org-list-description-max-indent'). Else, indent like
22483 parent's first line.
22485 3. Otherwise, indent relatively to current level, if
22486 `org-adapt-indentation' is non-nil, or to left margin.
22488 - On a blank line at the end of a plain list, an item, or
22489 a footnote definition, indent like the very last element
22492 - In the code part of a source block, use language major mode
22493 to indent current line if `org-src-tab-acts-natively' is
22494 non-nil. If it is nil, do nothing.
22496 - Otherwise, indent like the first non-blank line above.
22498 The function doesn't indent an item as it could break the whole
22499 list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
22500 \\[org-shiftmetaright].
22502 Also align node properties according to `org-property-format'."
22506 (let ((indent-line-function
22507 (cadadr (assq 'indent-line-function org-fb-vars))))
22508 (indent-according-to-mode)))
22509 ((org-at-heading-p) 'noindent)
22511 (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
22512 (type (org-element-type element)))
22513 (cond ((and (memq type '(plain-list item))
22514 (= (line-beginning-position)
22515 (org-element-property :post-affiliated element)))
22517 ((and (eq type 'src-block)
22518 org-src-tab-acts-natively
22519 (> (line-beginning-position)
22520 (org-element-property :post-affiliated element))
22521 (< (line-beginning-position)
22522 (org-with-wide-buffer
22523 (goto-char (org-element-property :end element))
22524 (skip-chars-backward " \r\t\n")
22525 (line-beginning-position))))
22526 (let ((org-src-strip-leading-and-trailing-blank-lines nil))
22527 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB"))))
22529 (let ((column (org--get-expected-indentation element nil)))
22530 ;; Preserve current column.
22531 (if (<= (current-column) (current-indentation))
22532 (org-indent-line-to column)
22533 (save-excursion (org-indent-line-to column))))
22534 ;; Align node property. Also preserve current column.
22535 (when (eq type 'node-property)
22536 (let ((column (current-column)))
22537 (org--align-node-property)
22538 (org-move-to-column column)))))))))
22540 (defun org-indent-region (start end)
22541 "Indent each non-blank line in the region.
22542 Called from a program, START and END specify the region to
22543 indent. The function will not indent contents of example blocks,
22544 verse blocks and export blocks as leading white spaces are
22545 assumed to be significant there."
22549 (beginning-of-line)
22552 ;; Set IND as indentation for all lines between point and
22553 ;; POS or END, whichever comes first. Blank lines are
22554 ;; ignored. Leave point after POS once done.
22555 (let ((limit (copy-marker (min end pos))))
22556 (while (< (point) limit)
22557 (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
22559 (set-marker limit nil))))
22560 (end (copy-marker end)))
22561 (while (< (point) end)
22562 (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
22563 (let* ((element (org-element-at-point))
22564 (type (org-element-type element))
22565 (element-end (copy-marker (org-element-property :end element)))
22566 (ind (org--get-expected-indentation element nil)))
22567 (if (or (memq type '(paragraph table table-row))
22568 (not (or (org-element-property :contents-begin element)
22570 '(example-block export-block src-block)))))
22571 ;; Elements here are indented as a single block. Also
22572 ;; align node properties.
22574 (when (eq type 'node-property)
22575 (org--align-node-property)
22576 (beginning-of-line))
22577 (funcall indent-to ind element-end))
22578 ;; Elements in this category consist of three parts:
22579 ;; before the contents, the contents, and after the
22580 ;; contents. The contents are treated specially,
22581 ;; according to the element type, or not indented at
22582 ;; all. Other parts are indented as a single block.
22583 (let* ((post (copy-marker
22584 (org-element-property :post-affiliated element)))
22588 ((not (org-element-property :contents-begin element))
22589 ;; Fake contents for source blocks.
22590 (org-with-wide-buffer
22594 ((memq type '(footnote-definition item plain-list))
22595 ;; Contents in these elements could start on
22596 ;; the same line as the beginning of the
22597 ;; element. Make sure we start indenting
22598 ;; from the second line.
22599 (org-with-wide-buffer
22603 (t (org-element-property :contents-begin element)))))
22605 (or (org-element-property :contents-end element)
22606 ;; Fake contents for source blocks.
22607 (org-with-wide-buffer
22608 (goto-char element-end)
22609 (skip-chars-backward " \r\t\n")
22610 (line-beginning-position))))))
22611 (funcall indent-to ind cbeg)
22612 (when (< (point) end)
22614 ((example-block export-block verse-block))
22616 ;; In a source block, indent source code according
22617 ;; to language major mode, but only if
22618 ;; `org-src-tab-acts-natively' is non-nil.
22619 (when (and (< (point) end) org-src-tab-acts-natively)
22621 (let (org-src-strip-leading-and-trailing-blank-lines
22622 ;; Region boundaries in edit buffer.
22623 (start (1+ (- (point) cbeg)))
22624 (end (- (min cend end) cbeg)))
22625 (org-babel-do-in-edit-buffer
22626 (indent-region start end))))))
22627 (t (org-indent-region (point) (min cend end))))
22628 (goto-char (min cend end))
22629 (when (< (point) end) (funcall indent-to ind element-end)))
22630 (set-marker post nil)
22631 (set-marker cbeg nil)
22632 (set-marker cend nil)))
22633 (set-marker element-end nil))))
22634 (set-marker end nil))))
22636 (defun org-indent-drawer ()
22637 "Indent the drawer at point."
22639 (unless (save-excursion
22640 (beginning-of-line)
22641 (org-looking-at-p org-drawer-regexp))
22642 (user-error "Not at a drawer"))
22643 (let ((element (org-element-at-point)))
22644 (unless (memq (org-element-type element) '(drawer property-drawer))
22645 (user-error "Not at a drawer"))
22646 (org-with-wide-buffer
22647 (org-indent-region (org-element-property :begin element)
22648 (org-element-property :end element))))
22649 (message "Drawer at point indented"))
22651 (defun org-indent-block ()
22652 "Indent the block at point."
22654 (unless (save-excursion
22655 (beginning-of-line)
22656 (let ((case-fold-search t))
22657 (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
22658 (user-error "Not at a block"))
22659 (let ((element (org-element-at-point)))
22660 (unless (memq (org-element-type element)
22661 '(comment-block center-block example-block export-block
22662 quote-block special-block src-block
22664 (user-error "Not at a block"))
22665 (org-with-wide-buffer
22666 (org-indent-region (org-element-property :begin element)
22667 (org-element-property :end element))))
22668 (message "Block at point indented"))
22673 ;; We use our own fill-paragraph and auto-fill functions.
22675 ;; `org-fill-paragraph' relies on adaptive filling and context
22676 ;; checking. Appropriate `fill-prefix' is computed with
22677 ;; `org-adaptive-fill-function'.
22679 ;; `org-auto-fill-function' takes care of auto-filling. It calls
22680 ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
22681 ;; `org-adaptive-fill-function' value. Internally,
22682 ;; `org-comment-line-break-function' breaks the line.
22684 ;; `org-setup-filling' installs filling and auto-filling related
22685 ;; variables during `org-mode' initialization.
22687 (defvar org-element-paragraph-separate) ; org-element.el
22688 (defun org-setup-filling ()
22689 (require 'org-element)
22690 ;; Prevent auto-fill from inserting unwanted new items.
22691 (when (boundp 'fill-nobreak-predicate)
22693 'fill-nobreak-predicate
22695 (append fill-nobreak-predicate
22696 '(org-fill-line-break-nobreak-p
22697 org-fill-paragraph-with-timestamp-nobreak-p)))))
22698 (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
22699 (org-set-local 'paragraph-start paragraph-ending)
22700 (org-set-local 'paragraph-separate paragraph-ending))
22701 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
22702 (org-set-local 'auto-fill-inhibit-regexp nil)
22703 (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
22704 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
22705 (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
22707 (defun org-fill-line-break-nobreak-p ()
22708 "Non-nil when a new line at point would create an Org line break."
22710 (skip-chars-backward "[ \t]")
22711 (skip-chars-backward "\\\\")
22712 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
22714 (defun org-fill-paragraph-with-timestamp-nobreak-p ()
22715 "Non-nil when a new line at point would split a timestamp."
22716 (and (org-at-timestamp-p t)
22717 (not (looking-at org-ts-regexp-both))))
22719 (declare-function message-in-body-p "message" ())
22720 (defvar orgtbl-line-start-regexp) ; From org-table.el
22721 (defun org-adaptive-fill-function ()
22722 "Compute a fill prefix for the current line.
22723 Return fill prefix, as a string, or nil if current line isn't
22724 meant to be filled. For convenience, if `adaptive-fill-regexp'
22725 matches in paragraphs or comments, use it."
22727 (when (derived-mode-p 'message-mode)
22729 (beginning-of-line)
22730 (cond ((or (not (message-in-body-p))
22731 (looking-at orgtbl-line-start-regexp))
22733 ((looking-at message-cite-prefix-regexp)
22734 (throw 'exit (match-string-no-properties 0)))
22735 ((looking-at org-outline-regexp)
22736 (throw 'exit (make-string (length (match-string 0)) ?\s))))))
22737 (org-with-wide-buffer
22738 (unless (org-at-heading-p)
22739 (let* ((p (line-beginning-position))
22740 (element (save-excursion
22741 (beginning-of-line)
22742 (org-element-at-point)))
22743 (type (org-element-type element))
22744 (post-affiliated (org-element-property :post-affiliated element)))
22745 (unless (< p post-affiliated)
22749 (beginning-of-line)
22750 (looking-at "[ \t]*")
22751 (concat (match-string 0) "# ")))
22752 (footnote-definition "")
22754 (make-string (org-list-item-body-column post-affiliated) ?\s))
22756 ;; Fill prefix is usually the same as the current line,
22757 ;; unless the paragraph is at the beginning of an item.
22758 (let ((parent (org-element-property :parent element)))
22760 (beginning-of-line)
22761 (cond ((eq (org-element-type parent) 'item)
22762 (make-string (org-list-item-body-column
22763 (org-element-property :begin parent))
22765 ((and adaptive-fill-regexp
22767 ;; `adaptive-fill-function' to let
22768 ;; `fill-context-prefix' handle
22769 ;; `adaptive-fill-regexp' variable.
22770 (let (adaptive-fill-function)
22771 (fill-context-prefix
22773 (org-element-property :end element)))))
22774 ((looking-at "[ \t]+") (match-string 0))
22777 ;; Only fill contents if P is within block boundaries.
22778 (let* ((cbeg (save-excursion (goto-char post-affiliated)
22781 (cend (save-excursion
22782 (goto-char (org-element-property :end element))
22783 (skip-chars-backward " \r\t\n")
22784 (line-beginning-position))))
22785 (when (and (>= p cbeg) (< p cend))
22786 (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
22790 (declare-function message-goto-body "message" ())
22791 (defvar message-cite-prefix-regexp) ; From message.el
22792 (defun org-fill-paragraph (&optional justify)
22793 "Fill element at point, when applicable.
22795 This function only applies to comment blocks, comments, example
22796 blocks and paragraphs. Also, as a special case, re-align table
22797 when point is at one.
22799 If JUSTIFY is non-nil (interactively, with prefix argument),
22800 justify as well. If `sentence-end-double-space' is non-nil, then
22801 period followed by one space does not end a sentence, so don't
22802 break a line there. The variable `fill-column' controls the
22805 For convenience, when point is at a plain list, an item or
22806 a footnote definition, try to fill the first paragraph within."
22808 (if (and (derived-mode-p 'message-mode)
22809 (or (not (message-in-body-p))
22810 (save-excursion (move-beginning-of-line 1)
22811 (looking-at message-cite-prefix-regexp))))
22812 ;; First ensure filling is correct in message-mode.
22813 (let ((fill-paragraph-function
22814 (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
22815 (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
22816 (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
22817 (paragraph-separate
22818 (cadadr (assoc 'paragraph-separate org-fb-vars))))
22819 (fill-paragraph nil))
22820 (with-syntax-table org-mode-transpose-word-syntax-table
22821 ;; Move to end of line in order to get the first paragraph
22822 ;; within a plain list or a footnote definition.
22823 (let ((element (save-excursion
22825 (or (ignore-errors (org-element-at-point))
22826 (user-error "An element cannot be parsed line %d"
22827 (line-number-at-pos (point)))))))
22828 ;; First check if point is in a blank line at the beginning of
22829 ;; the buffer. In that case, ignore filling.
22830 (case (org-element-type element)
22831 ;; Use major mode filling function is src blocks.
22832 (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
22833 ;; Align Org tables, leave table.el tables as-is.
22834 (table-row (org-table-align) t)
22836 (when (eq (org-element-property :type element) 'org)
22838 (goto-char (org-element-property :post-affiliated element))
22839 (org-table-align)))
22842 ;; Paragraphs may contain `line-break' type objects.
22843 (let ((beg (max (point-min)
22844 (org-element-property :contents-begin element)))
22845 (end (min (point-max)
22846 (org-element-property :contents-end element))))
22847 ;; Do nothing if point is at an affiliated keyword.
22848 (if (< (line-end-position) beg) t
22849 (when (derived-mode-p 'message-mode)
22850 ;; In `message-mode', do not fill following citation
22851 ;; in current paragraph nor text before message body.
22852 (let ((body-start (save-excursion (message-goto-body))))
22853 (when body-start (setq beg (max body-start beg))))
22854 (when (save-excursion
22856 (concat "^" message-cite-prefix-regexp) end t))
22857 (setq end (match-beginning 0))))
22858 ;; Fill paragraph, taking line breaks into account.
22861 (let ((cuts (list beg)))
22862 (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
22863 (when (eq 'line-break
22865 (save-excursion (backward-char)
22866 (org-element-context))))
22867 (push (point) cuts)))
22868 (dolist (c (delq end cuts))
22869 (fill-region-as-paragraph c end justify)
22872 ;; Contents of `comment-block' type elements should be
22873 ;; filled as plain text, but only if point is within block
22876 (let* ((case-fold-search t)
22877 (beg (save-excursion
22878 (goto-char (org-element-property :begin element))
22879 (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
22882 (end (save-excursion
22883 (goto-char (org-element-property :end element))
22884 (re-search-backward "^[ \t]*#\\+end_comment" nil t)
22885 (line-beginning-position))))
22886 (if (or (< (point) beg) (> (point) end)) t
22887 (fill-region-as-paragraph
22888 (save-excursion (end-of-line)
22889 (re-search-backward "^[ \t]*$" beg 'move)
22890 (line-beginning-position))
22891 (save-excursion (beginning-of-line)
22892 (re-search-forward "^[ \t]*$" end 'move)
22893 (line-beginning-position))
22897 (let ((begin (org-element-property :post-affiliated element))
22898 (end (org-element-property :end element)))
22899 (when (and (>= (point) begin) (<= (point) end))
22900 (let ((begin (save-excursion
22902 (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
22903 (progn (forward-line) (point))
22905 (end (save-excursion
22907 (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
22908 (1- (line-beginning-position))
22909 (skip-chars-backward " \r\t\n")
22910 (line-end-position)))))
22911 ;; Do not fill comments when at a blank line.
22912 (when (> end begin)
22915 (beginning-of-line)
22916 (looking-at "[ \t]*#")
22917 (let ((comment-prefix (match-string 0)))
22918 (goto-char (match-end 0))
22919 (if (looking-at adaptive-fill-regexp)
22920 (concat comment-prefix (match-string 0))
22921 (concat comment-prefix " "))))))
22923 (fill-region-as-paragraph begin end justify))))))
22925 ;; Ignore every other element.
22928 (defun org-auto-fill-function ()
22929 "Auto-fill function."
22930 ;; Check if auto-filling is meaningful.
22931 (let ((fc (current-fill-column)))
22932 (when (and fc (> (current-column) fc))
22933 (let* ((fill-prefix (org-adaptive-fill-function))
22934 ;; Enforce empty fill prefix, if required. Otherwise, it
22935 ;; will be computed again.
22936 (adaptive-fill-mode (not (equal fill-prefix ""))))
22937 (when fill-prefix (do-auto-fill))))))
22939 (defun org-comment-line-break-function (&optional soft)
22940 "Break line at point and indent, continuing comment if within one.
22941 The inserted newline is marked hard if variable
22942 `use-hard-newlines' is true, unless optional argument SOFT is
22944 (if soft (insert-and-inherit ?\n) (newline 1))
22945 (save-excursion (forward-char -1) (delete-horizontal-space))
22946 (delete-horizontal-space)
22947 (indent-to-left-margin)
22948 (insert-before-markers-and-inherit fill-prefix))
22951 ;;; Fixed Width Areas
22953 (defun org-toggle-fixed-width ()
22954 "Toggle fixed-width markup.
22956 Add or remove fixed-width markup on current line, whenever it
22957 makes sense. Return an error otherwise.
22959 If a region is active and if it contains only fixed-width areas
22960 or blank lines, remove all fixed-width markup in it. If the
22961 region contains anything else, convert all non-fixed-width lines
22962 to fixed-width ones.
22964 Blank lines at the end of the region are ignored unless the
22965 region only contains such lines."
22967 (if (not (org-region-active-p))
22970 ;; Remove fixed width marker only in a fixed-with element.
22972 ;; Add fixed width maker in paragraphs, in blank lines after
22973 ;; elements or at the beginning of a headline or an inlinetask,
22974 ;; and before any one-line elements (e.g., a clock).
22976 (beginning-of-line)
22977 (let* ((element (org-element-at-point))
22978 (type (org-element-type element)))
22980 ((and (eq type 'fixed-width)
22981 (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
22983 "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
22984 ((and (memq type '(babel-call clock comment diary-sexp headline
22985 horizontal-rule keyword paragraph
22987 (<= (org-element-property :post-affiliated element) (point)))
22988 (skip-chars-forward " \t")
22990 ((and (org-looking-at-p "[ \t]*$")
22991 (or (eq type 'inlinetask)
22993 (skip-chars-forward " \r\t\n")
22994 (<= (org-element-property :end element) (point)))))
22995 (delete-region (point) (line-end-position))
22998 (t (user-error "Cannot insert a fixed-width line here")))))
23000 (let* ((begin (save-excursion
23001 (goto-char (region-beginning))
23002 (line-beginning-position)))
23005 (goto-char (region-end))
23006 (unless (eolp) (beginning-of-line))
23007 (if (save-excursion (re-search-backward "\\S-" begin t))
23008 (progn (skip-chars-backward " \r\t\n") (point))
23014 (skip-chars-forward " \r\t\n")
23015 (when (eobp) (throw 'not-all-p nil))
23016 (while (< (point) end)
23017 (let ((element (org-element-at-point)))
23018 (if (eq (org-element-type element) 'fixed-width)
23019 (goto-char (org-element-property :end element))
23020 (throw 'not-all-p nil))))
23022 (if all-fixed-width-p
23025 (while (< (point) end)
23026 (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
23029 (if (= (line-end-position) (match-end 0)) 0 1)))
23031 (let ((min-ind (point-max)))
23032 ;; Find minimum indentation across all lines.
23035 (if (not (save-excursion (re-search-forward "\\S-" end t)))
23038 (while (< (point) end)
23039 (unless (org-looking-at-p "[ \t]*$")
23040 (let ((ind (org-get-indentation)))
23041 (setq min-ind (min min-ind ind))
23042 (when (zerop ind) (throw 'zerop t))))
23044 ;; Loop over all lines and add fixed-width markup everywhere
23045 ;; but in fixed-width lines.
23048 (while (< (point) end)
23050 ((org-at-heading-p)
23053 (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
23056 ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
23057 (let* ((element (org-element-at-point))
23058 (element-end (org-element-property :end element)))
23059 (if (eq (org-element-type element) 'fixed-width)
23060 (progn (goto-char element-end)
23061 (skip-chars-backward " \r\t\n")
23063 (let ((limit (min end element-end)))
23064 (while (< (point) limit)
23065 (org-move-to-column min-ind t)
23067 (forward-line))))))
23069 (org-move-to-column min-ind t)
23071 (forward-line)))))))
23072 (set-marker end nil))))
23077 ;; Org comments syntax is quite complex. It requires the entire line
23078 ;; to be just a comment. Also, even with the right syntax at the
23079 ;; beginning of line, some some elements (i.e. verse-block or
23080 ;; example-block) don't accept comments. Usual Emacs comment commands
23081 ;; cannot cope with those requirements. Therefore, Org replaces them.
23083 ;; Org still relies on `comment-dwim', but cannot trust
23084 ;; `comment-only-p'. So, `comment-region-function' and
23085 ;; `uncomment-region-function' both point
23086 ;; to`org-comment-or-uncomment-region'. Eventually,
23087 ;; `org-insert-comment' takes care of insertion of comments at the
23088 ;; beginning of line.
23090 ;; `org-setup-comments-handling' install comments related variables
23091 ;; during `org-mode' initialization.
23093 (defun org-setup-comments-handling ()
23095 (org-set-local 'comment-use-syntax nil)
23096 (org-set-local 'comment-start "# ")
23097 (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
23098 (org-set-local 'comment-insert-comment-function 'org-insert-comment)
23099 (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
23100 (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
23102 (defun org-insert-comment ()
23103 "Insert an empty comment above current line.
23104 If the line is empty, insert comment at its beginning. When
23105 point is within a source block, comment according to the related
23107 (if (let ((element (org-element-at-point)))
23108 (and (eq (org-element-type element) 'src-block)
23110 (goto-char (org-element-property :post-affiliated element))
23111 (line-end-position))
23114 (goto-char (org-element-property :end element))
23115 (skip-chars-backward " \r\t\n")
23116 (line-beginning-position))
23118 (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
23119 (beginning-of-line)
23120 (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
23125 (defvar comment-empty-lines) ; From newcomment.el.
23126 (defun org-comment-or-uncomment-region (beg end &rest ignore)
23127 "Comment or uncomment each non-blank line in the region.
23128 Uncomment each non-blank line between BEG and END if it only
23129 contains commented lines. Otherwise, comment them. If region is
23130 strictly within a source block, use appropriate comment syntax."
23131 (if (let ((element (org-element-at-point)))
23132 (and (eq (org-element-type element) 'src-block)
23134 (goto-char (org-element-property :post-affiliated element))
23135 (line-end-position))
23137 (>= (save-excursion
23138 (goto-char (org-element-property :end element))
23139 (skip-chars-backward " \r\t\n")
23140 (line-beginning-position))
23142 (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
23145 (narrow-to-region (save-excursion (goto-char beg)
23146 (skip-chars-forward " \r\t\n" end)
23147 (line-beginning-position))
23148 (save-excursion (goto-char end)
23149 (skip-chars-backward " \r\t\n" beg)
23150 (line-end-position)))
23152 ;; UNCOMMENTP is non-nil when every non blank line between
23153 ;; BEG and END is a comment.
23155 (goto-char (point-min))
23156 (while (and (not (eobp))
23157 (let ((element (org-element-at-point)))
23158 (and (eq (org-element-type element) 'comment)
23159 (goto-char (min (point-max)
23160 (org-element-property
23161 :end element)))))))
23164 ;; Only blank lines and comments in region: uncomment it.
23166 (goto-char (point-min))
23167 (while (not (eobp))
23168 (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
23169 (replace-match "" nil nil nil 1))
23171 ;; Comment each line in region.
23172 (let ((min-indent (point-max)))
23173 ;; First find the minimum indentation across all lines.
23175 (goto-char (point-min))
23176 (while (and (not (eobp)) (not (zerop min-indent)))
23177 (unless (looking-at "[ \t]*$")
23178 (setq min-indent (min min-indent (current-indentation))))
23180 ;; Then loop over all lines.
23182 (goto-char (point-min))
23183 (while (not (eobp))
23184 (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
23185 ;; Don't get fooled by invisible text (e.g. link path)
23186 ;; when moving to column MIN-INDENT.
23187 (let ((buffer-invisibility-spec nil))
23188 (org-move-to-column min-indent t))
23189 (insert comment-start))
23190 (forward-line)))))))))
23192 (defun org-comment-dwim (arg)
23193 "Call `comment-dwim' within a source edit buffer if needed."
23195 (if (org-in-src-block-p)
23196 (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
23197 (call-interactively 'comment-dwim)))
23202 ;; This section contains tools to operate on timestamp objects, as
23203 ;; returned by, e.g. `org-element-context'.
23205 (defun org-timestamp-has-time-p (timestamp)
23206 "Non-nil when TIMESTAMP has a time specified."
23207 (org-element-property :hour-start timestamp))
23209 (defun org-timestamp-format (timestamp format &optional end utc)
23210 "Format a TIMESTAMP element into a string.
23212 FORMAT is a format specifier to be passed to
23213 `format-time-string'.
23215 When optional argument END is non-nil, use end of date-range or
23216 time-range, if possible.
23218 When optional argument UTC is non-nil, time will be expressed as
23220 (format-time-string
23222 (apply 'encode-time
23225 (lambda (prop) (or (org-element-property prop timestamp) 0))
23226 (if end '(:minute-end :hour-end :day-end :month-end :year-end)
23227 '(:minute-start :hour-start :day-start :month-start
23231 (defun org-timestamp-split-range (timestamp &optional end)
23232 "Extract a timestamp object from a date or time range.
23234 TIMESTAMP is a timestamp object. END, when non-nil, means extract
23235 the end of the range. Otherwise, extract its start.
23237 Return a new timestamp object sharing the same parent as
23239 (let ((type (org-element-property :type timestamp)))
23240 (if (memq type '(active inactive diary)) timestamp
23241 (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
23243 (org-element-put-property
23244 split-ts :type (if (eq type 'active-range) 'active 'inactive))
23245 ;; Copy start properties over end properties if END is
23246 ;; non-nil. Otherwise, copy end properties over `start' ones.
23247 (let ((p-alist '((:minute-start . :minute-end)
23248 (:hour-start . :hour-end)
23249 (:day-start . :day-end)
23250 (:month-start . :month-end)
23251 (:year-start . :year-end))))
23252 (dolist (p-cell p-alist)
23253 (org-element-put-property
23255 (funcall (if end 'car 'cdr) p-cell)
23256 (org-element-property
23257 (funcall (if end 'cdr 'car) p-cell) split-ts)))
23258 ;; Eventually refresh `:raw-value'.
23259 (org-element-put-property split-ts :raw-value nil)
23260 (org-element-put-property
23261 split-ts :raw-value (org-element-interpret-data split-ts)))))))
23263 (defun org-timestamp-translate (timestamp &optional boundary)
23264 "Apply `org-translate-time' on a TIMESTAMP object.
23265 When optional argument BOUNDARY is non-nil, it is either the
23266 symbol `start' or `end'. In this case, only translate the
23267 starting or ending part of TIMESTAMP if it is a date or time
23268 range. Otherwise, translate both parts."
23269 (if (and (not boundary)
23270 (memq (org-element-property :type timestamp)
23271 '(active-range inactive-range)))
23273 (org-translate-time
23274 (org-element-property :raw-value
23275 (org-timestamp-split-range timestamp)))
23277 (org-translate-time
23278 (org-element-property :raw-value
23279 (org-timestamp-split-range timestamp t))))
23280 (org-translate-time
23281 (org-element-property
23283 (if (not boundary) timestamp
23284 (org-timestamp-split-range timestamp (eq boundary 'end)))))))
23290 (defun org-reftex-citation ()
23291 "Use reftex-citation to insert a citation into the buffer.
23292 This looks for a line like
23294 #+BIBLIOGRAPHY: foo plain option:-d
23296 and derives from it that foo.bib is the bibliography file relevant
23297 for this document. It then installs the necessary environment for RefTeX
23298 to work in this buffer and calls `reftex-citation' to insert a citation
23301 Export of such citations to both LaTeX and HTML is handled by the contributed
23302 package ox-bibtex by Taru Karttunen."
23304 (let ((reftex-docstruct-symbol 'rds)
23305 (reftex-cite-format "\\cite{%l}")
23310 (let ((case-fold-search t)
23311 (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
23312 (if (not (save-excursion
23313 (or (re-search-forward re nil t)
23314 (re-search-backward re nil t))))
23315 (user-error "No bibliography defined in file")
23316 (setq bib (concat (match-string 1) ".bib")
23317 rds (list (list 'bib bib)))))))
23318 (call-interactively 'reftex-citation)))
23320 ;;;; Functions extending outline functionality
23322 (defun org-beginning-of-line (&optional arg)
23323 "Go to the beginning of the current line. If that is invisible, continue
23324 to a visible line beginning. This makes the function of C-a more intuitive.
23325 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
23326 first attempt, and only move to after the tags when the cursor is already
23327 beyond the end of the headline."
23329 (let ((pos (point))
23330 (special (if (consp org-special-ctrl-a/e)
23331 (car org-special-ctrl-a/e)
23332 org-special-ctrl-a/e))
23333 deactivate-mark refpos)
23334 (if (org-bound-and-true-p visual-line-mode)
23335 (beginning-of-visual-line 1)
23336 (beginning-of-line 1))
23337 (if (and arg (fboundp 'move-beginning-of-line))
23338 (call-interactively 'move-beginning-of-line)
23342 (if (org-truely-invisible-p)
23343 (while (and (not (bobp)) (org-truely-invisible-p))
23345 (beginning-of-line 1))
23346 (forward-char 1))))
23349 ((and (looking-at org-complex-heading-regexp)
23350 (= (char-after (match-end 1)) ?\ ))
23351 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
23355 (cond ((> pos refpos) refpos)
23356 ((= pos (point)) refpos)
23358 (cond ((> pos (point)) (point))
23359 ((not (eq last-command this-command)) (point))
23362 ;; Being at an item and not looking at an the item means point
23363 ;; was previously moved to beginning of a visual line, which
23364 ;; doesn't contain the item. Therefore, do nothing special,
23366 (when (looking-at org-list-full-item-re)
23367 ;; Set special position at first white space character after
23368 ;; bullet, and check-box, if any.
23369 (let ((after-bullet
23370 (let ((box (match-end 3)))
23371 (if (not box) (match-end 1)
23372 (let ((after (char-after box)))
23373 (if (and after (= after ? )) (1+ box) box))))))
23374 ;; Special case: Move point to special position when
23375 ;; currently after it or at beginning of line.
23377 (when (or (> pos after-bullet) (= (point) pos))
23378 (goto-char after-bullet))
23379 ;; Reversed case: Move point to special position when
23380 ;; point was already at beginning of line and command is
23382 (when (and (= (point) pos) (eq last-command this-command))
23383 (goto-char after-bullet))))))))
23385 (and (featurep 'xemacs) (setq zmacs-region-stays t))))
23386 (setq disable-point-adjustment
23387 (or (not (invisible-p (point)))
23388 (not (invisible-p (max (point-min) (1- (point))))))))
23390 (defun org-end-of-line (&optional arg)
23391 "Go to the end of the line.
23392 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
23393 tags on the first attempt, and only move to after the tags when
23394 the cursor is already beyond the end of the headline."
23396 (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
23397 org-special-ctrl-a/e))
23398 (move-fun (cond ((org-bound-and-true-p visual-line-mode)
23399 'end-of-visual-line)
23400 ((fboundp 'move-end-of-line) 'move-end-of-line)
23403 (if (or (not special) arg) (call-interactively move-fun)
23404 (let* ((element (save-excursion (beginning-of-line)
23405 (org-element-at-point)))
23406 (type (org-element-type element)))
23408 ((memq type '(headline inlinetask))
23409 (let ((pos (point)))
23410 (beginning-of-line 1)
23411 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
23413 (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
23414 (goto-char (match-beginning 1))
23415 (goto-char (match-end 0)))
23416 (if (or (< pos (match-end 0))
23417 (not (eq this-command last-command)))
23418 (goto-char (match-end 0))
23419 (goto-char (match-beginning 1))))
23420 (call-interactively move-fun))))
23421 ((outline-invisible-p (line-end-position))
23422 ;; If element is hidden, `move-end-of-line' would put point
23423 ;; after it. Use `end-of-line' to stay on current line.
23424 (call-interactively 'end-of-line))
23425 (t (call-interactively move-fun)))))
23426 (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
23427 (setq disable-point-adjustment
23428 (or (not (invisible-p (point)))
23429 (not (invisible-p (max (point-min) (1- (point))))))))
23431 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
23432 (define-key org-mode-map "\C-e" 'org-end-of-line)
23434 (defun org-backward-sentence (&optional arg)
23435 "Go to beginning of sentence, or beginning of table field.
23436 This will call `backward-sentence' or `org-table-beginning-of-field',
23437 depending on context."
23440 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
23441 (t (call-interactively 'backward-sentence))))
23443 (defun org-forward-sentence (&optional arg)
23444 "Go to end of sentence, or end of table field.
23445 This will call `forward-sentence' or `org-table-end-of-field',
23446 depending on context."
23449 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
23450 (t (call-interactively 'forward-sentence))))
23452 (define-key org-mode-map "\M-a" 'org-backward-sentence)
23453 (define-key org-mode-map "\M-e" 'org-forward-sentence)
23455 (defun org-kill-line (&optional arg)
23456 "Kill line, to tags or end of line."
23459 ((or (not org-special-ctrl-k)
23461 (not (org-at-heading-p)))
23462 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
23463 org-ctrl-k-protect-subtree)
23464 (if (or (eq org-ctrl-k-protect-subtree 'error)
23465 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
23466 (user-error "C-k aborted as it would kill a hidden subtree")))
23467 (call-interactively
23468 (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
23469 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
23470 (kill-region (point) (match-beginning 1))
23471 (org-set-tags nil t))
23472 (t (kill-region (point) (point-at-eol)))))
23474 (define-key org-mode-map "\C-k" 'org-kill-line)
23476 (defun org-yank (&optional arg)
23477 "Yank. If the kill is a subtree, treat it specially.
23478 This command will look at the current kill and check if is a single
23479 subtree, or a series of subtrees[1]. If it passes the test, and if the
23480 cursor is at the beginning of a line or after the stars of a currently
23481 empty headline, then the yank is handled specially. How exactly depends
23482 on the value of the following variables, both set by default.
23484 `org-yank-folded-subtrees'
23485 When set, the subtree(s) will be folded after insertion, but only
23486 if doing so would now swallow text after the yanked text.
23488 `org-yank-adjusted-subtrees'
23489 When set, the subtree will be promoted or demoted in order to
23490 fit into the local outline tree structure, which means that the
23491 level will be adjusted so that it becomes the smaller one of the
23492 two *visible* surrounding headings.
23494 Any prefix to this command will cause `yank' to be called directly with
23495 no special treatment. In particular, a simple \\[universal-argument] prefix \
23497 plainly yank the text as it is.
23499 \[1] The test checks if the first non-white line is a heading
23500 and if there are no other headings with fewer stars."
23502 (org-yank-generic 'yank arg))
23504 (defun org-yank-generic (command arg)
23505 "Perform some yank-like command.
23507 This function implements the behavior described in the `org-yank'
23508 documentation. However, it has been generalized to work for any
23509 interactive command with similar behavior."
23511 ;; pretend to be command COMMAND
23512 (setq this-command command)
23515 (call-interactively command)
23517 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
23518 (and (org-kill-is-subtree-p)
23520 (and (looking-at "[ \t]*$")
23523 (buffer-substring (point-at-bol) (point)))))))
23526 ((and subtreep org-yank-folded-subtrees)
23527 (let ((beg (point))
23529 (if (and subtreep org-yank-adjusted-subtrees)
23530 (org-paste-subtree nil nil 'for-yank)
23531 (call-interactively command))
23535 (when (and (bolp) subtreep
23536 (not (setq swallowp
23537 (org-yank-folding-would-swallow-text beg end))))
23538 (org-with-limited-levels
23539 (or (looking-at org-outline-regexp)
23540 (re-search-forward org-outline-regexp-bol end t))
23541 (while (and (< (point) end) (looking-at org-outline-regexp))
23543 (org-cycle-show-empty-lines 'folded)
23544 (condition-case nil
23545 (outline-forward-same-level 1)
23546 (error (goto-char end))))))
23549 "Inserted text not folded because that would swallow text"))
23552 (skip-chars-forward " \t\n\r")
23553 (beginning-of-line 1)
23554 (push-mark beg 'nomsg)))
23555 ((and subtreep org-yank-adjusted-subtrees)
23556 (let ((beg (point-at-bol)))
23557 (org-paste-subtree nil nil 'for-yank)
23558 (push-mark beg 'nomsg)))
23560 (call-interactively command))))))
23562 (defun org-yank-folding-would-swallow-text (beg end)
23563 "Would hide-subtree at BEG swallow any text after END?"
23565 (org-with-limited-levels
23568 (when (or (looking-at org-outline-regexp)
23569 (re-search-forward org-outline-regexp-bol end t))
23570 (setq level (org-outline-level)))
23572 (skip-chars-forward " \t\r\n\v\f")
23574 (and (bolp) (looking-at org-outline-regexp)
23575 (<= (org-outline-level) level)))
23576 nil ; Nothing would be swallowed
23577 t))))) ; something would swallow
23579 (define-key org-mode-map "\C-y" 'org-yank)
23581 (defun org-truely-invisible-p ()
23582 "Check if point is at a character currently not visible.
23583 This version does not only check the character property, but also
23585 ;; Early versions of noutline don't have `outline-invisible-p'.
23586 (if (org-bound-and-true-p visible-mode)
23588 (outline-invisible-p)))
23590 (defun org-invisible-p2 ()
23591 "Check if point is at a character currently not visible."
23593 (if (and (eolp) (not (bobp))) (backward-char 1))
23594 ;; Early versions of noutline don't have `outline-invisible-p'.
23595 (outline-invisible-p)))
23597 (defun org-back-to-heading (&optional invisible-ok)
23598 "Call `outline-back-to-heading', but provide a better error message."
23599 (condition-case nil
23600 (outline-back-to-heading invisible-ok)
23601 (error (error "Before first headline at position %d in buffer %s"
23602 (point) (current-buffer)))))
23604 (defun org-before-first-heading-p ()
23605 "Before first heading?"
23608 (null (re-search-backward org-outline-regexp-bol nil t))))
23610 (defun org-at-heading-p (&optional ignored)
23611 (outline-on-heading-p t))
23612 ;; Compatibility alias with Org versions < 7.8.03
23613 (defalias 'org-on-heading-p 'org-at-heading-p)
23615 (defun org-in-commented-heading-p (&optional no-inheritance)
23616 "Non-nil if point is under a commented heading.
23617 This function also checks ancestors of the current headline,
23618 unless optional argument NO-INHERITANCE is non-nil."
23620 ((org-before-first-heading-p) nil)
23621 ((let ((headline (nth 4 (org-heading-components))))
23623 (let ((case-fold-search nil))
23624 (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
23626 (no-inheritance nil)
23628 (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
23630 (defun org-at-comment-p nil
23631 "Is cursor in a commented line?"
23634 (beginning-of-line)
23635 (looking-at "^[ \t]*# "))))
23637 (defun org-at-drawer-p nil
23638 "Is cursor at a drawer keyword?"
23640 (move-beginning-of-line 1)
23641 (looking-at org-drawer-regexp)))
23643 (defun org-at-block-p nil
23644 "Is cursor at a block keyword?"
23646 (move-beginning-of-line 1)
23647 (looking-at org-block-regexp)))
23649 (defun org-point-at-end-of-empty-headline ()
23650 "If point is at the end of an empty headline, return t, else nil.
23651 If the heading only contains a TODO keyword, it is still still considered
23653 (and (looking-at "[ \t]*$")
23654 (when org-todo-line-regexp
23656 (beginning-of-line 1)
23657 (let ((case-fold-search nil))
23658 (looking-at org-todo-line-regexp)
23659 (string= (match-string 3) ""))))))
23661 (defun org-at-heading-or-item-p ()
23662 (or (org-at-heading-p) (org-at-item-p)))
23664 (defun org-at-target-p ()
23665 (or (org-in-regexp org-radio-target-regexp)
23666 (org-in-regexp org-target-regexp)))
23667 ;; Compatibility alias with Org versions < 7.8.03
23668 (defalias 'org-on-target-p 'org-at-target-p)
23670 (defun org-up-heading-all (arg)
23671 "Move to the heading line of which the present line is a subheading.
23672 This function considers both visible and invisible heading lines.
23673 With argument, move up ARG levels."
23674 (if (fboundp 'outline-up-heading-all)
23675 (outline-up-heading-all arg) ; emacs 21 version of outline.el
23676 (outline-up-heading arg t))) ; emacs 22 version of outline.el
23678 (defun org-up-heading-safe ()
23679 "Move to the heading line of which the present line is a subheading.
23680 This version will not throw an error. It will return the level of the
23681 headline found, or nil if no higher level is found.
23683 Also, this function will be a lot faster than `outline-up-heading',
23684 because it relies on stars being the outline starters. This can really
23685 make a significant difference in outlines with very many siblings."
23686 (let (start-level re)
23687 (org-back-to-heading t)
23688 (setq start-level (funcall outline-level))
23689 (if (equal start-level 1)
23691 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
23692 (if (re-search-backward re nil t)
23693 (funcall outline-level)))))
23695 (defun org-first-sibling-p ()
23696 "Is this heading the first child of its parents?"
23698 (let ((re org-outline-regexp-bol)
23700 (unless (org-at-heading-p t)
23701 (user-error "Not at a heading"))
23702 (setq level (funcall outline-level))
23704 (if (not (re-search-backward re nil t))
23706 (setq l (funcall outline-level))
23709 (defun org-goto-sibling (&optional previous)
23710 "Goto the next sibling, even if it is invisible.
23711 When PREVIOUS is set, go to the previous sibling instead. Returns t
23712 when a sibling was found. When none is found, return nil and don't
23714 (let ((fun (if previous 're-search-backward 're-search-forward))
23716 (re org-outline-regexp-bol)
23718 (when (ignore-errors (org-back-to-heading t))
23719 (setq level (funcall outline-level))
23721 (or previous (forward-char 1))
23722 (while (funcall fun re nil t)
23723 (setq l (funcall outline-level))
23724 (when (< l level) (goto-char pos) (throw 'exit nil))
23725 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
23729 (defun org-show-siblings ()
23730 "Show all siblings of the current headline."
23732 (while (org-goto-sibling) (org-flag-heading nil)))
23734 (while (org-goto-sibling 'previous)
23735 (org-flag-heading nil))))
23737 (defun org-goto-first-child ()
23738 "Goto the first child, even if it is invisible.
23739 Return t when a child was found. Otherwise don't move point and
23741 (let (level (pos (point)) (re org-outline-regexp-bol))
23742 (when (ignore-errors (org-back-to-heading t))
23743 (setq level (outline-level))
23745 (if (and (re-search-forward re nil t) (> (outline-level) level))
23746 (progn (goto-char (match-beginning 0)) t)
23747 (goto-char pos) nil))))
23749 (defun org-show-hidden-entry ()
23750 "Show an entry where even the heading is hidden."
23754 (defun org-flag-heading (flag &optional entry)
23755 "Flag the current heading. FLAG non-nil means make invisible.
23756 When ENTRY is non-nil, show the entire entry."
23758 (org-back-to-heading t)
23759 ;; Check if we should show the entire entry
23764 (and (outline-next-heading)
23765 (org-flag-heading nil))))
23766 (outline-flag-region (max (point-min) (1- (point)))
23767 (save-excursion (outline-end-of-heading) (point))
23770 (defun org-get-next-sibling ()
23771 "Move to next heading of the same level, and return point.
23772 If there is no such heading, return nil.
23773 This is like outline-next-sibling, but invisible headings are ok."
23774 (let ((level (funcall outline-level)))
23775 (outline-next-heading)
23776 (while (and (not (eobp)) (> (funcall outline-level) level))
23777 (outline-next-heading))
23778 (if (or (eobp) (< (funcall outline-level) level))
23782 (defun org-get-last-sibling ()
23783 "Move to previous heading of the same level, and return point.
23784 If there is no such heading, return nil."
23785 (let ((opoint (point))
23786 (level (funcall outline-level)))
23787 (outline-previous-heading)
23788 (when (and (/= (point) opoint) (outline-on-heading-p t))
23789 (while (and (> (funcall outline-level) level)
23791 (outline-previous-heading))
23792 (if (< (funcall outline-level) level)
23796 (defun org-end-of-subtree (&optional invisible-ok to-heading)
23797 "Goto to the end of a subtree."
23798 ;; This contains an exact copy of the original function, but it uses
23799 ;; `org-back-to-heading', to make it work also in invisible
23800 ;; trees. And is uses an invisible-ok argument.
23801 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
23802 ;; Furthermore, when used inside Org, finding the end of a large subtree
23803 ;; with many children and grandchildren etc, this can be much faster
23804 ;; than the outline version.
23805 (org-back-to-heading invisible-ok)
23807 (level (funcall outline-level)))
23808 (if (and (derived-mode-p 'org-mode) (< level 1000))
23809 ;; A true heading (not a plain list item), in Org-mode
23810 ;; This means we can easily find the end by looking
23811 ;; only for the right number of stars. Using a regexp to do
23812 ;; this is so much faster than using a Lisp loop.
23813 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
23815 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
23816 ;; something else, do it the slow way
23817 (while (and (not (eobp))
23818 (or first (> (funcall outline-level) level)))
23820 (outline-next-heading)))
23822 (if (memq (preceding-char) '(?\n ?\^M))
23824 ;; Go to end of line before heading
23826 (if (memq (preceding-char) '(?\n ?\^M))
23827 ;; leave blank line before heading
23828 (forward-char -1))))))
23831 (defun org-end-of-meta-data-and-drawers ()
23832 "Jump to the first text after meta data and drawers in the current entry.
23833 This will move over empty lines, lines with planning time stamps,
23834 clocking lines, and drawers."
23835 (org-back-to-heading t)
23836 (let ((end (save-excursion (outline-next-heading) (point)))
23837 (re (concat "\\(" org-drawer-regexp "\\)"
23838 "\\|" "[ \t]*" org-keyword-time-regexp)))
23840 (while (re-search-forward re end t)
23841 (if (not (match-end 1))
23842 ;; empty or planning line
23844 ;; a drawer, find the end
23845 (re-search-forward "^[ \t]*:END:" end 'move)
23847 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
23850 (defun org-forward-heading-same-level (arg &optional invisible-ok)
23851 "Move forward to the ARG'th subheading at same level as this one.
23852 Stop at the first and last subheadings of a superior heading.
23853 Normally this only looks at visible headings, but when INVISIBLE-OK is
23854 non-nil it will also look at invisible ones."
23856 (if (not (ignore-errors (org-back-to-heading invisible-ok)))
23857 (if (and arg (< arg 0))
23858 (goto-char (point-min))
23859 (outline-next-heading))
23861 (let ((level (- (match-end 0) (match-beginning 0) 1))
23862 (f (if (and arg (< arg 0))
23863 're-search-backward
23864 're-search-forward))
23865 (count (if arg (abs arg) 1))
23867 (while (and (prog1 (> count 0)
23868 (forward-char (if (and arg (< arg 0)) -1 1)))
23869 (funcall f org-outline-regexp-bol nil 'move))
23870 (let ((l (- (match-end 0) (match-beginning 0) 1)))
23871 (cond ((< l level) (setq count 0))
23875 (goto-char (line-beginning-position))
23876 (not (outline-invisible-p)))))
23877 (setq count (1- count))
23879 (setq result (point)))))))
23880 (goto-char result))
23881 (beginning-of-line 1)))
23883 (defun org-backward-heading-same-level (arg &optional invisible-ok)
23884 "Move backward to the ARG'th subheading at same level as this one.
23885 Stop at the first and last subheadings of a superior heading."
23887 (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
23889 (defun org-next-block (arg &optional backward block-regexp)
23890 "Jump to the next block.
23891 With a prefix argument ARG, jump forward ARG many source blocks.
23892 When BACKWARD is non-nil, jump to the previous block.
23893 When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
23895 (let ((re (or block-regexp org-block-regexp))
23896 (re-search-fn (or (and backward 're-search-backward)
23897 're-search-forward)))
23898 (if (looking-at re) (forward-char 1))
23899 (condition-case nil
23900 (funcall re-search-fn re nil nil arg)
23901 (error (user-error "No %s code blocks"
23902 (if backward "previous" "further" ))))
23903 (goto-char (match-beginning 0)) (org-show-context)))
23905 (defun org-previous-block (arg &optional block-regexp)
23906 "Jump to the previous block.
23907 With a prefix argument ARG, jump backward ARG many source blocks.
23908 When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
23910 (org-next-block arg t block-regexp))
23912 (defun org-forward-paragraph ()
23913 "Move forward to beginning of next paragraph or equivalent.
23915 The function moves point to the beginning of the next visible
23916 structural element, which can be a paragraph, a table, a list
23917 item, etc. It also provides some special moves for convenience:
23919 - On an affiliated keyword, jump to the beginning of the
23921 - On an item or a footnote definition, move to the second
23922 element inside, if any.
23923 - On a table or a property drawer, jump after it.
23924 - On a verse or source block, stop after blank lines."
23926 (when (eobp) (user-error "Cannot move further down"))
23927 (let* ((deactivate-mark nil)
23928 (element (org-element-at-point))
23929 (type (org-element-type element))
23930 (post-affiliated (org-element-property :post-affiliated element))
23931 (contents-begin (org-element-property :contents-begin element))
23932 (contents-end (org-element-property :contents-end element))
23933 (end (let ((end (org-element-property :end element)) (parent element))
23934 (while (and (setq parent (org-element-property :parent parent))
23935 (= (org-element-property :contents-end parent) end))
23936 (setq end (org-element-property :end parent)))
23938 (cond ((not element)
23939 (skip-chars-forward " \r\t\n")
23940 (or (eobp) (beginning-of-line)))
23941 ;; On affiliated keywords, move to element's beginning.
23942 ((< (point) post-affiliated)
23943 (goto-char post-affiliated))
23944 ;; At a table row, move to the end of the table. Similarly,
23945 ;; at a node property, move to the end of the property
23947 ((memq type '(node-property table-row))
23948 (goto-char (org-element-property
23949 :end (org-element-property :parent element))))
23950 ((memq type '(property-drawer table)) (goto-char end))
23951 ;; Consider blank lines as separators in verse and source
23952 ;; blocks to ease editing.
23953 ((memq type '(src-block verse-block))
23954 (when (eq type 'src-block)
23956 (save-excursion (goto-char end)
23957 (skip-chars-backward " \r\t\n")
23958 (line-beginning-position))))
23959 (beginning-of-line)
23960 (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
23961 (if (not (re-search-forward "^[ \t]*$" contents-end t))
23963 (skip-chars-forward " \r\t\n")
23964 (if (= (point) contents-end) (goto-char end)
23965 (beginning-of-line))))
23966 ;; With no contents, just skip element.
23967 ((not contents-begin) (goto-char end))
23968 ;; If contents are invisible, skip the element altogether.
23969 ((outline-invisible-p (line-end-position))
23972 (org-with-limited-levels (outline-next-visible-heading 1)))
23973 ;; At a plain list, make sure we move to the next item
23974 ;; instead of skipping the whole list.
23975 (plain-list (forward-char)
23976 (org-forward-paragraph))
23977 (otherwise (goto-char end))))
23978 ((>= (point) contents-end) (goto-char end))
23979 ((>= (point) contents-begin)
23980 ;; This can only happen on paragraphs and plain lists.
23982 (paragraph (goto-char end))
23983 ;; At a plain list, try to move to second element in
23984 ;; first item, if possible.
23985 (plain-list (end-of-line)
23986 (org-forward-paragraph))))
23987 ;; When contents start on the middle of a line (e.g. in
23988 ;; items and footnote definitions), try to reach first
23989 ;; element starting after current line.
23990 ((> (line-end-position) contents-begin)
23992 (org-forward-paragraph))
23993 (t (goto-char contents-begin)))))
23995 (defun org-backward-paragraph ()
23996 "Move backward to start of previous paragraph or equivalent.
23998 The function moves point to the beginning of the current
23999 structural element, which can be a paragraph, a table, a list
24000 item, etc., or to the beginning of the previous visible one if
24001 point is already there. It also provides some special moves for
24004 - On an affiliated keyword, jump to the first one.
24005 - On a table or a property drawer, move to its beginning.
24006 - On a verse or source block, stop before blank lines."
24008 (when (bobp) (user-error "Cannot move further up"))
24009 (let* ((deactivate-mark nil)
24010 (element (org-element-at-point))
24011 (type (org-element-type element))
24012 (contents-begin (org-element-property :contents-begin element))
24013 (contents-end (org-element-property :contents-end element))
24014 (post-affiliated (org-element-property :post-affiliated element))
24015 (begin (org-element-property :begin element)))
24017 ((not element) (goto-char (point-min)))
24020 (org-backward-paragraph))
24021 ((<= (point) post-affiliated) (goto-char begin))
24022 ((memq type '(node-property table-row))
24023 (goto-char (org-element-property
24024 :post-affiliated (org-element-property :parent element))))
24025 ((memq type '(property-drawer table)) (goto-char begin))
24026 ((memq type '(src-block verse-block))
24027 (when (eq type 'src-block)
24028 (setq contents-begin
24029 (save-excursion (goto-char begin) (forward-line) (point))))
24030 (if (= (point) contents-begin) (goto-char post-affiliated)
24031 ;; Inside a verse block, see blank lines as paragraph
24033 (let ((origin (point)))
24034 (skip-chars-backward " \r\t\n" contents-begin)
24035 (when (re-search-backward "^[ \t]*$" contents-begin 'move)
24036 (skip-chars-forward " \r\t\n" origin)
24037 (if (= (point) origin) (goto-char contents-begin)
24038 (beginning-of-line))))))
24039 ((not contents-begin) (goto-char (or post-affiliated begin)))
24040 ((eq type 'paragraph)
24041 (goto-char contents-begin)
24042 ;; When at first paragraph in an item or a footnote definition,
24043 ;; move directly to beginning of line.
24044 (let ((parent-contents
24045 (org-element-property
24046 :contents-begin (org-element-property :parent element))))
24047 (when (and parent-contents (= parent-contents contents-begin))
24048 (beginning-of-line))))
24049 ;; At the end of a greater element, move to the beginning of the
24050 ;; last element within.
24051 ((>= (point) contents-end)
24052 (goto-char (1- contents-end))
24053 (org-backward-paragraph))
24054 (t (goto-char (or post-affiliated begin))))
24055 ;; Ensure we never leave point invisible.
24056 (when (outline-invisible-p (point)) (beginning-of-visual-line))))
24058 (defun org-forward-element ()
24059 "Move forward by one element.
24060 Move to the next element at the same level, when possible."
24062 (cond ((eobp) (user-error "Cannot move further down"))
24063 ((org-with-limited-levels (org-at-heading-p))
24064 (let ((origin (point)))
24065 (goto-char (org-end-of-subtree nil t))
24066 (unless (org-with-limited-levels (org-at-heading-p))
24068 (user-error "Cannot move further down"))))
24070 (let* ((elem (org-element-at-point))
24071 (end (org-element-property :end elem))
24072 (parent (org-element-property :parent elem)))
24073 (cond ((and parent (= (org-element-property :contents-end parent) end))
24074 (goto-char (org-element-property :end parent)))
24075 ((integer-or-marker-p end) (goto-char end))
24076 (t (message "No element at point")))))))
24078 (defun org-backward-element ()
24079 "Move backward by one element.
24080 Move to the previous element at the same level, when possible."
24082 (cond ((bobp) (user-error "Cannot move further up"))
24083 ((org-with-limited-levels (org-at-heading-p))
24084 ;; At a headline, move to the previous one, if any, or stay
24086 (let ((origin (point)))
24087 (org-with-limited-levels (org-backward-heading-same-level 1))
24088 ;; When current headline has no sibling above, move to its
24090 (when (= (point) origin)
24091 (or (org-with-limited-levels (org-up-heading-safe))
24092 (progn (goto-char origin)
24093 (user-error "Cannot move further up"))))))
24095 (let* ((elem (org-element-at-point))
24096 (beg (org-element-property :begin elem)))
24098 ;; Move to beginning of current element if point isn't
24100 ((null beg) (message "No element at point"))
24101 ((/= (point) beg) (goto-char beg))
24103 (skip-chars-backward " \r\t\n")
24105 (let ((prev (org-element-at-point)))
24106 (goto-char (org-element-property :begin prev))
24107 (while (and (setq prev (org-element-property :parent prev))
24108 (<= (org-element-property :end prev) beg))
24109 (goto-char (org-element-property :begin prev)))))))))))
24111 (defun org-up-element ()
24112 "Move to upper element."
24114 (if (org-with-limited-levels (org-at-heading-p))
24115 (unless (org-up-heading-safe) (user-error "No surrounding element"))
24116 (let* ((elem (org-element-at-point))
24117 (parent (org-element-property :parent elem)))
24118 (if parent (goto-char (org-element-property :begin parent))
24119 (if (org-with-limited-levels (org-before-first-heading-p))
24120 (user-error "No surrounding element")
24121 (org-with-limited-levels (org-back-to-heading)))))))
24123 (defvar org-element-greater-elements)
24124 (defun org-down-element ()
24125 "Move to inner element."
24127 (let ((element (org-element-at-point)))
24129 ((memq (org-element-type element) '(plain-list table))
24130 (goto-char (org-element-property :contents-begin element))
24132 ((memq (org-element-type element) org-element-greater-elements)
24133 ;; If contents are hidden, first disclose them.
24134 (when (outline-invisible-p (line-end-position)) (org-cycle))
24135 (goto-char (or (org-element-property :contents-begin element)
24136 (user-error "No content for this element"))))
24137 (t (user-error "No inner element")))))
24139 (defun org-drag-element-backward ()
24140 "Move backward element at point."
24142 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
24143 (let* ((elem (org-element-at-point))
24146 (goto-char (org-element-property :begin elem))
24147 (skip-chars-backward " \r\t\n")
24149 (let* ((beg (org-element-property :begin elem))
24150 (prev (org-element-at-point))
24152 (while (and (setq up (org-element-property :parent up))
24153 (<= (org-element-property :end up) beg))
24156 ;; Error out if no previous element or previous element is
24157 ;; a parent of the current one.
24158 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
24159 (user-error "Cannot drag element backward")
24160 (let ((pos (point)))
24161 (org-element-swap-A-B prev-elem elem)
24162 (goto-char (+ (org-element-property :begin prev-elem)
24163 (- pos (org-element-property :begin elem)))))))))
24165 (defun org-drag-element-forward ()
24166 "Move forward element at point."
24168 (let* ((pos (point))
24169 (elem (org-element-at-point)))
24170 (when (= (point-max) (org-element-property :end elem))
24171 (user-error "Cannot drag element forward"))
24172 (goto-char (org-element-property :end elem))
24173 (let ((next-elem (org-element-at-point)))
24174 (when (or (org-element-nested-p elem next-elem)
24175 (and (eq (org-element-type next-elem) 'headline)
24176 (not (eq (org-element-type elem) 'headline))))
24178 (user-error "Cannot drag element forward"))
24179 ;; Compute new position of point: it's shifted by NEXT-ELEM
24180 ;; body's length (without final blanks) and by the length of
24181 ;; blanks between ELEM and NEXT-ELEM.
24182 (let ((size-next (- (save-excursion
24183 (goto-char (org-element-property :end next-elem))
24184 (skip-chars-backward " \r\t\n")
24186 ;; Small correction if buffer doesn't end
24187 ;; with a newline character.
24188 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
24189 (org-element-property :begin next-elem)))
24190 (size-blank (- (org-element-property :end elem)
24192 (goto-char (org-element-property :end elem))
24193 (skip-chars-backward " \r\t\n")
24196 (org-element-swap-A-B elem next-elem)
24197 (goto-char (+ pos size-next size-blank))))))
24199 (defun org-drag-line-forward (arg)
24200 "Drag the line at point ARG lines forward."
24202 (dotimes (n (abs arg))
24203 (let ((c (current-column)))
24206 (beginning-of-line 2)
24207 (transpose-lines 1)
24208 (beginning-of-line 0))
24209 (transpose-lines 1)
24210 (beginning-of-line -1))
24211 (org-move-to-column c))))
24213 (defun org-drag-line-backward (arg)
24214 "Drag the line at point ARG lines backward."
24216 (org-drag-line-forward (- arg)))
24218 (defun org-mark-element ()
24219 "Put point at beginning of this element, mark at end.
24221 Interactively, if this command is repeated or (in Transient Mark
24222 mode) if the mark is active, it marks the next element after the
24223 ones already marked."
24225 (let (deactivate-mark)
24226 (if (and (org-called-interactively-p 'any)
24227 (or (and (eq last-command this-command) (mark t))
24228 (and transient-mark-mode mark-active)))
24232 (goto-char (org-element-property :end (org-element-at-point)))))
24233 (let ((element (org-element-at-point)))
24235 (push-mark (org-element-property :end element) t t)
24236 (goto-char (org-element-property :begin element))))))
24238 (defun org-narrow-to-element ()
24239 "Narrow buffer to current element."
24241 (let ((elem (org-element-at-point)))
24243 ((eq (car elem) 'headline)
24245 (org-element-property :begin elem)
24246 (org-element-property :end elem)))
24247 ((memq (car elem) org-element-greater-elements)
24249 (org-element-property :contents-begin elem)
24250 (org-element-property :contents-end elem)))
24253 (org-element-property :begin elem)
24254 (org-element-property :end elem))))))
24256 (defun org-transpose-element ()
24257 "Transpose current and previous elements, keeping blank lines between.
24258 Point is moved after both elements."
24260 (org-skip-whitespace)
24261 (let ((end (org-element-property :end (org-element-at-point))))
24262 (org-drag-element-backward)
24265 (defun org-unindent-buffer ()
24266 "Un-indent the visible part of the buffer.
24267 Relative indentation (between items, inside blocks, etc.) isn't
24270 (unless (eq major-mode 'org-mode)
24271 (user-error "Cannot un-indent a buffer not in Org mode"))
24272 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
24273 unindent-tree ; For byte-compiler.
24279 (if (memq (org-element-type element) '(headline section))
24280 (funcall unindent-tree (org-element-contents element))
24284 (org-element-property :begin element)
24285 (org-element-property :end element))
24286 (org-do-remove-indentation)))))
24287 (reverse contents))))))
24288 (funcall unindent-tree (org-element-contents parse-tree))))
24290 (defun org-show-subtree ()
24291 "Show everything after this heading at deeper levels."
24293 (outline-flag-region
24296 (org-end-of-subtree t t))
24299 (defun org-show-entry ()
24300 "Show the body directly following this heading.
24301 Show the heading too, if it is currently invisible."
24305 (org-back-to-heading t)
24306 (outline-flag-region
24307 (max (point-min) (1- (point)))
24309 (if (re-search-forward
24310 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
24311 (match-beginning 1)
24314 (org-cycle-hide-drawers 'children))))
24316 (defun org-make-options-regexp (kwds &optional extra)
24317 "Make a regular expression for keyword lines."
24320 (mapconcat 'regexp-quote kwds "\\|")
24321 (if extra (concat "\\|" extra))
24322 "\\):[ \t]*\\(.*\\)"))
24324 ;; Make isearch reveal the necessary context
24325 (defun org-isearch-end ()
24326 "Reveal context after isearch exits."
24327 (when isearch-success ; only if search was successful
24328 (if (featurep 'xemacs)
24329 ;; Under XEmacs, the hook is run in the correct place,
24330 ;; we directly show the context.
24331 (org-show-context 'isearch)
24332 ;; In Emacs the hook runs *before* restoring the overlays.
24333 ;; So we have to use a one-time post-command-hook to do this.
24334 ;; (Emacs 22 has a special variable, see function `org-mode')
24335 (unless (and (boundp 'isearch-mode-end-hook-quit)
24336 isearch-mode-end-hook-quit)
24337 ;; Only when the isearch was not quitted.
24338 (org-add-hook 'post-command-hook 'org-isearch-post-command
24339 'append 'local)))))
24341 (defun org-isearch-post-command ()
24342 "Remove self from hook, and show context."
24343 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
24344 (org-show-context 'isearch))
24347 ;;;; Integration with and fixes for other packages
24351 (defvar org-imenu-markers nil
24352 "All markers currently used by Imenu.")
24353 (make-variable-buffer-local 'org-imenu-markers)
24355 (defun org-imenu-new-marker (&optional pos)
24356 "Return a new marker for use by Imenu, and remember the marker."
24357 (let ((m (make-marker)))
24358 (move-marker m (or pos (point)))
24359 (push m org-imenu-markers)
24362 (defun org-imenu-get-tree ()
24363 "Produce the index for Imenu."
24364 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
24365 (setq org-imenu-markers nil)
24366 (let* ((n org-imenu-depth)
24367 (re (concat "^" (org-get-limited-outline-regexp)))
24368 (subs (make-vector (1+ n) nil))
24370 m level head0 head)
24374 (goto-char (point-max))
24375 (while (re-search-backward re nil t)
24376 (setq level (org-reduced-level (funcall outline-level)))
24377 (when (and (<= level n)
24378 (looking-at org-complex-heading-regexp)
24379 (setq head0 (org-match-string-no-properties 4)))
24380 (setq head (org-link-display-format head0)
24381 m (org-imenu-new-marker))
24382 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
24383 (if (>= level last-level)
24384 (push (cons head m) (aref subs level))
24385 (push (cons head (aref subs (1+ level))) (aref subs level))
24386 (loop for i from (1+ level) to n do (aset subs i nil)))
24387 (setq last-level level)))))
24390 (eval-after-load "imenu"
24392 (add-hook 'imenu-after-jump-hook
24394 (if (derived-mode-p 'org-mode)
24395 (org-show-context 'org-goto))))))
24397 (defun org-link-display-format (link)
24398 "Replace a link with its the description.
24399 If there is no description, use the link target."
24401 (if (string-match org-bracket-link-analytic-regexp link)
24402 (replace-match (if (match-end 5)
24403 (match-string 5 link)
24404 (concat (match-string 1 link)
24405 (match-string 3 link)))
24409 (defun org-toggle-link-display ()
24410 "Toggle the literal or descriptive display of links."
24412 (if org-descriptive-links
24413 (progn (org-remove-from-invisibility-spec '(org-link))
24414 (org-restart-font-lock)
24415 (setq org-descriptive-links nil))
24416 (progn (add-to-invisibility-spec '(org-link))
24417 (org-restart-font-lock)
24418 (setq org-descriptive-links t))))
24420 ;; Speedbar support
24422 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
24423 "Overlay marking the agenda restriction line in speedbar.")
24424 (overlay-put org-speedbar-restriction-lock-overlay
24425 'face 'org-agenda-restriction-lock)
24426 (overlay-put org-speedbar-restriction-lock-overlay
24427 'help-echo "Agendas are currently limited to this item.")
24428 (org-detach-overlay org-speedbar-restriction-lock-overlay)
24430 (defun org-speedbar-set-agenda-restriction ()
24431 "Restrict future agenda commands to the location at point in speedbar.
24432 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
24434 (require 'org-agenda)
24435 (let (p m tp np dir txt)
24437 ((setq p (text-property-any (point-at-bol) (point-at-eol)
24439 (setq m (get-text-property p 'org-imenu-marker))
24440 (with-current-buffer (marker-buffer m)
24442 (org-agenda-set-restriction-lock 'subtree)))
24443 ((setq p (text-property-any (point-at-bol) (point-at-eol)
24444 'speedbar-function 'speedbar-find-file))
24445 (setq tp (previous-single-property-change
24446 (1+ p) 'speedbar-function)
24447 np (next-single-property-change
24448 tp 'speedbar-function)
24449 dir (speedbar-line-directory)
24450 txt (buffer-substring-no-properties (or tp (point-min))
24451 (or np (point-max))))
24452 (with-current-buffer (find-file-noselect
24453 (let ((default-directory dir))
24454 (expand-file-name txt)))
24455 (unless (derived-mode-p 'org-mode)
24456 (user-error "Cannot restrict to non-Org-mode file"))
24457 (org-agenda-set-restriction-lock 'file)))
24458 (t (user-error "Don't know how to restrict Org-mode's agenda")))
24459 (move-overlay org-speedbar-restriction-lock-overlay
24460 (point-at-bol) (point-at-eol))
24461 (setq current-prefix-arg nil)
24462 (org-agenda-maybe-redo)))
24464 (defvar speedbar-file-key-map)
24465 (declare-function speedbar-add-supported-extension "speedbar" (extension))
24466 (eval-after-load "speedbar"
24468 (speedbar-add-supported-extension ".org")
24469 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
24470 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
24471 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
24472 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
24473 (add-hook 'speedbar-visiting-tag-hook
24474 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
24476 ;;; Fixes and Hacks for problems with other packages
24478 (defun org-mode-flyspell-verify ()
24479 "Function used for `flyspell-generic-check-word-predicate'."
24480 (if (org-at-heading-p)
24481 ;; At a headline or an inlinetask, check title only. This is
24482 ;; faster than relying on `org-element-at-point'.
24483 (and (save-excursion (beginning-of-line)
24484 (and (let ((case-fold-search t))
24485 (not (looking-at "\\*+ END[ \t]*$")))
24486 (looking-at org-complex-heading-regexp)))
24487 (match-beginning 4)
24488 (>= (point) (match-beginning 4))
24489 (or (not (match-beginning 5))
24490 (< (point) (match-beginning 5))))
24491 (let* ((element (org-element-at-point))
24492 (post-affiliated (org-element-property :post-affiliated element))
24495 ;; Non-nil if checks can be done for object at point.
24497 (let ((object (save-excursion
24498 (when (org-looking-at-p "\\>") (backward-char))
24499 (org-element-context element))))
24500 (case (org-element-type object)
24501 ;; Prevent checks in links due to keybinding conflict
24503 ((code entity export-snippet inline-babel-call
24504 inline-src-block line-break latex-fragment link macro
24505 statistics-cookie target timestamp verbatim)
24507 (footnote-reference
24508 ;; Only in inline footnotes, within the definition.
24509 (and (eq (org-element-property :type object) 'inline)
24511 (goto-char (org-element-property :begin object))
24512 (search-forward ":" nil t 2))
24514 (otherwise t)))))))
24516 ;; Ignore checks in all affiliated keywords but captions.
24517 ((< (point) post-affiliated)
24518 (and (save-excursion
24519 (beginning-of-line)
24520 (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
24521 (> (point) (match-end 0))
24522 (funcall object-check)))
24523 ;; Ignore checks in LOGBOOK (or equivalent) drawer.
24524 ((and org-log-into-drawer
24525 (let ((log (or (org-string-nw-p org-log-into-drawer) "LOGBOOK"))
24527 (while (and parent (not (eq (org-element-type parent) 'drawer)))
24528 (setq parent (org-element-property :parent parent)))
24530 (eq (compare-strings
24532 (org-element-property :drawer-name parent) nil nil t)
24536 (case (org-element-type element)
24537 ((comment quote-section) t)
24539 ;; Allow checks between block markers, not on them.
24540 (and (> (line-beginning-position) post-affiliated)
24543 (skip-chars-forward " \r\t\n")
24544 (< (point) (org-element-property :end element)))))
24545 ;; Arbitrary list of keywords where checks are meaningful.
24546 ;; Make sure point is on the value part of the element.
24548 (and (member (org-element-property :key element)
24549 '("DESCRIPTION" "TITLE"))
24551 (beginning-of-line) (search-forward ":") (point))
24553 ;; Check is globally allowed in paragraphs verse blocks and
24554 ;; table rows (after affiliated keywords) but some objects
24555 ;; must not be affected.
24556 ((paragraph table-row verse-block)
24557 (and (>= (point) (org-element-property :contents-begin element))
24558 (< (point) (org-element-property :contents-end element))
24559 (funcall object-check)))))))))
24560 (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
24562 (defun org-remove-flyspell-overlays-in (beg end)
24563 "Remove flyspell overlays in region."
24564 (and (org-bound-and-true-p flyspell-mode)
24565 (fboundp 'flyspell-delete-region-overlays)
24566 (flyspell-delete-region-overlays beg end)))
24568 (defvar flyspell-delayed-commands)
24569 (eval-after-load "flyspell"
24570 '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
24572 ;; Make `bookmark-jump' shows the jump location if it was hidden.
24573 (eval-after-load "bookmark"
24574 '(if (boundp 'bookmark-after-jump-hook)
24575 ;; We can use the hook
24576 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
24577 ;; Hook not available, use advice
24578 (defadvice bookmark-jump (after org-make-visible activate)
24579 "Make the position visible."
24580 (org-bookmark-jump-unhide))))
24582 ;; Make sure saveplace shows the location if it was hidden
24583 (eval-after-load "saveplace"
24584 '(defadvice save-place-find-file-hook (after org-make-visible activate)
24585 "Make the position visible."
24586 (org-bookmark-jump-unhide)))
24588 ;; Make sure ecb shows the location if it was hidden
24589 (eval-after-load "ecb"
24590 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
24591 "Make hierarchy visible when jumping into location from ECB tree buffer."
24592 (if (derived-mode-p 'org-mode)
24593 (org-show-context))))
24595 (defun org-bookmark-jump-unhide ()
24596 "Unhide the current position, to show the bookmark location."
24597 (and (derived-mode-p 'org-mode)
24598 (or (outline-invisible-p)
24599 (save-excursion (goto-char (max (point-min) (1- (point))))
24600 (outline-invisible-p)))
24601 (org-show-context 'bookmark-jump)))
24603 (defun org-mark-jump-unhide ()
24604 "Make the point visible with `org-show-context' after jumping to the mark."
24605 (when (and (derived-mode-p 'org-mode)
24606 (outline-invisible-p))
24607 (org-show-context 'mark-goto)))
24609 (eval-after-load "simple"
24610 '(defadvice pop-to-mark-command (after org-make-visible activate)
24611 "Make the point visible with `org-show-context'."
24612 (org-mark-jump-unhide)))
24614 (eval-after-load "simple"
24615 '(defadvice exchange-point-and-mark (after org-make-visible activate)
24616 "Make the point visible with `org-show-context'."
24617 (org-mark-jump-unhide)))
24619 (eval-after-load "simple"
24620 '(defadvice pop-global-mark (after org-make-visible activate)
24621 "Make the point visible with `org-show-context'."
24622 (org-mark-jump-unhide)))
24624 ;; Make session.el ignore our circular variable
24625 (defvar session-globals-exclude)
24626 (eval-after-load "session"
24627 '(add-to-list 'session-globals-exclude 'org-mark-ring))
24633 (run-hooks 'org-load-hook)
24635 ;;; org.el ends here