1 ;;; org.el --- Outline-based notes management and organizer -*- lexical-binding: t; -*-
3 ;; Carstens outline-mode for keeping track of everything.
4 ;; Copyright (C) 2004-2018 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: https://orgmode.org
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
29 ;; Org is a mode for keeping notes, maintaining ToDo lists, and doing
30 ;; project planning with a fast and effective plain-text system.
32 ;; Org mode develops organizational tasks around NOTES files that
33 ;; contain information about projects as plain text. Org mode is
34 ;; implemented on top of outline-mode, which makes it possible to keep
35 ;; the content of large files well structured. Visibility cycling and
36 ;; structure editing help to work with the tree. Tables are easily
37 ;; created with a built-in table editor. Org mode supports ToDo
38 ;; items, deadlines, time stamps, and scheduling. It dynamically
39 ;; compiles entries into an agenda that utilizes and smoothly
40 ;; integrates much of the Emacs calendar and diary. Plain text
41 ;; URL-like links connect to websites, emails, Usenet messages, BBDB
42 ;; entries, and any files related to the projects. For printing and
43 ;; sharing of notes, an Org file can be exported as a structured ASCII
44 ;; file, as HTML, or (todo and agenda items only) as an iCalendar
45 ;; file. It can also serve as a publishing tool for a set of linked
48 ;; Installation and Activation
49 ;; ---------------------------
50 ;; See the corresponding sections in the manual at
52 ;; https://orgmode.org/org.html#Installation
56 ;; The documentation of Org mode can be found in the TeXInfo file. The
57 ;; distribution also contains a PDF version of it. At the homepage of
58 ;; Org mode, you can read the same text online as HTML. There is also an
59 ;; excellent reference card made by Philip Rooke. This card can be found
60 ;; in the doc/ directory.
62 ;; A list of recent changes can be found at
63 ;; https://orgmode.org/Changes.html
67 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
68 (defvar-local org-table-formula-constants-local nil
69 "Local version of `org-table-formula-constants'.")
70 (defvar org-inlinetask-min-level
)
72 ;;;; Require other packages
76 (eval-when-compile (require 'gnus-sum
))
80 (require 'format-spec
)
82 (or (eq this-command
'eval-buffer
)
84 (load (concat (file-name-directory load-file-name
)
88 (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
90 (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
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'.
98 (defvar org-outline-regexp
"\\*+ "
99 "Regexp to match Org headlines.")
101 (defvar org-outline-regexp-bol
"^\\*+ "
102 "Regexp to match Org headlines.
103 This is similar to `org-outline-regexp' but additionally makes
104 sure that we are at the beginning of the line.")
106 (defvar org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
107 "Matches a headline, putting stars and text into groups.
108 Stars are put in group 1 and the trimmed body in group 2.")
110 (declare-function calendar-check-holidays
"holidays" (date))
111 (declare-function cdlatex-environment
"ext:cdlatex" (environment item
))
112 (declare-function cdlatex-math-symbol
"ext:cdlatex")
113 (declare-function isearch-no-upper-case-p
"isearch" (string regexp-flag
))
114 (declare-function org-add-archive-files
"org-archive" (files))
115 (declare-function org-agenda-entry-get-agenda-timestamp
"org-agenda" (pom))
116 (declare-function org-agenda-list
"org-agenda" (&optional arg start-day span with-hour
))
117 (declare-function org-agenda-redo
"org-agenda" (&optional all
))
118 (declare-function org-babel-do-in-edit-buffer
"ob-core" (&rest body
) t
)
119 (declare-function org-babel-tangle-file
"ob-tangle" (file &optional target-file lang
))
120 (declare-function org-beamer-mode
"ox-beamer" (&optional prefix
) t
)
121 (declare-function org-clock-get-last-clock-out-time
"org-clock" ())
122 (declare-function org-clock-out
"org-clock" (&optional switch-to-state fail-quietly at-time
))
123 (declare-function org-clock-remove-overlays
"org-clock" (&optional beg end noremove
))
124 (declare-function org-clock-sum
"org-clock" (&optional tstart tend headline-filter propname
))
125 (declare-function org-clock-sum-current-item
"org-clock" (&optional tstart
))
126 (declare-function org-clock-timestamps-down
"org-clock" (&optional n
))
127 (declare-function org-clock-timestamps-up
"org-clock" (&optional n
))
128 (declare-function org-clock-update-time-maybe
"org-clock" ())
129 (declare-function org-clocking-buffer
"org-clock" ())
130 (declare-function org-clocktable-shift
"org-clock" (dir n
))
132 org-duration-from-minutes
"org-duration" (minutes &optional fmt canonical
))
133 (declare-function org-element-at-point
"org-element" ())
134 (declare-function org-element-cache-refresh
"org-element" (pos))
135 (declare-function org-element-cache-reset
"org-element" (&optional all
))
136 (declare-function org-element-contents
"org-element" (element))
137 (declare-function org-element-context
"org-element" (&optional element
))
138 (declare-function org-element-copy
"org-element" (datum))
139 (declare-function org-element-interpret-data
"org-element" (data))
140 (declare-function org-element-lineage
"org-element" (blob &optional types with-self
))
141 (declare-function org-element-link-parser
"org-element" ())
142 (declare-function org-element-nested-p
"org-element" (elem-a elem-b
))
143 (declare-function org-element-parse-buffer
"org-element" (&optional granularity visible-only
))
144 (declare-function org-element-property
"org-element" (property element
))
145 (declare-function org-element-put-property
"org-element" (element property value
))
146 (declare-function org-element-swap-A-B
"org-element" (elem-a elem-b
))
147 (declare-function org-element-type
"org-element" (element))
148 (declare-function org-element-update-syntax
"org-element" ())
149 (declare-function org-id-find-id-file
"org-id" (id))
150 (declare-function org-id-get-create
"org-id" (&optional force
))
151 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
152 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
153 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
154 (declare-function org-plot
/gnuplot
"org-plot" (&optional params
))
155 (declare-function org-table-align
"org-table" ())
156 (declare-function org-table-begin
"org-table" (&optional table-type
))
157 (declare-function org-table-beginning-of-field
"org-table" (&optional n
))
158 (declare-function org-table-blank-field
"org-table" ())
159 (declare-function org-table-calc-current-TBLFM
"org-table" (&optional arg
))
160 (declare-function org-table-copy-region
"org-table" (beg end
&optional cut
))
161 (declare-function org-table-cut-region
"org-table" (beg end
))
162 (declare-function org-table-edit-field
"org-table" (arg))
163 (declare-function org-table-end
"org-table" (&optional table-type
))
164 (declare-function org-table-end-of-field
"org-table" (&optional n
))
165 (declare-function org-table-insert-row
"org-table" (&optional arg
))
166 (declare-function org-table-justify-field-maybe
"org-table" (&optional new
))
167 (declare-function org-table-maybe-eval-formula
"org-table" ())
168 (declare-function org-table-maybe-recalculate-line
"org-table" ())
169 (declare-function org-table-next-row
"org-table" ())
170 (declare-function org-table-paste-rectangle
"org-table" ())
171 (declare-function org-table-recalculate
"org-table" (&optional all noalign
))
173 org-table-sort-lines
"org-table"
174 (&optional with-case sorting-type getkey-func compare-func interactive?
))
175 (declare-function org-table-shrink
"org-table" (&optional begin end
))
176 (declare-function org-table-toggle-column-width
"org-table" (&optional arg
))
177 (declare-function org-table-wrap-region
"org-table" (arg))
178 (declare-function org-tags-view
"org-agenda" (&optional todo-only match
))
179 (declare-function orgtbl-ascii-plot
"org-table" (&optional ask
))
180 (declare-function orgtbl-mode
"org-table" (&optional arg
))
181 (declare-function org-export-get-backend
"ox" (name))
182 (declare-function org-export-get-environment
"ox" (&optional backend subtreep ext-plist
))
183 (declare-function org-latex-make-preamble
"ox-latex" (info &optional template snippet?
))
185 (defvar ffap-url-regexp
)
186 (defvar org-element-paragraph-separate
)
188 ;; load languages based on value of `org-babel-load-languages'
189 (defvar org-babel-load-languages
)
192 (defun org-babel-do-load-languages (sym value
)
193 "Load the languages defined in `org-babel-load-languages'."
194 (set-default sym value
)
195 (dolist (pair org-babel-load-languages
)
196 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
198 (require (intern (concat "ob-" lang
)))
199 (funcall 'fmakunbound
200 (intern (concat "org-babel-execute:" lang
)))
201 (funcall 'fmakunbound
202 (intern (concat "org-babel-expand-body:" lang
)))))))
204 (declare-function org-babel-tangle-file
"ob-tangle" (file &optional target-file lang
))
206 (defun org-babel-load-file (file &optional compile
)
207 "Load Emacs Lisp source code blocks in the Org FILE.
208 This function exports the source code using `org-babel-tangle'
209 and then loads the resulting file using `load-file'. With prefix
210 arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
211 file to byte-code before it is loaded."
212 (interactive "fFile to load: \nP")
213 (let* ((age (lambda (file)
215 (time-subtract (current-time)
216 (nth 5 (or (file-attributes (file-truename file
))
217 (file-attributes file
)))))))
218 (base-name (file-name-sans-extension file
))
219 (exported-file (concat base-name
".el")))
220 ;; tangle if the Org file is newer than the elisp file
221 (unless (and (file-exists-p exported-file
)
222 (> (funcall age file
) (funcall age exported-file
)))
223 ;; Tangle-file traversal returns reversed list of tangled files
224 ;; and we want to evaluate the first target.
226 (car (last (org-babel-tangle-file file exported-file
"emacs-lisp")))))
229 (progn (byte-compile-file exported-file
'load
)
230 "Compiled and loaded")
231 (progn (load-file exported-file
) "Loaded"))
234 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
235 "Languages which can be evaluated in Org buffers.
236 This list can be used to load support for any of the languages
237 below, note that each language will depend on a different set of
238 system executables and/or Emacs modes. When a language is
239 \"loaded\", then code blocks in that language can be evaluated
240 with `org-babel-execute-src-block' bound by default to C-c
241 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
242 be set to remove code block evaluation from the C-c C-c
243 keybinding. By default only Emacs Lisp (which has no
244 requirements) is loaded."
246 :set
'org-babel-do-load-languages
248 :type
'(alist :tag
"Babel Languages"
251 (const :tag
"Awk" awk
)
254 (const :tag
"Asymptote" asymptote
)
255 (const :tag
"Calc" calc
)
256 (const :tag
"Clojure" clojure
)
257 (const :tag
"CSS" css
)
258 (const :tag
"Ditaa" ditaa
)
259 (const :tag
"Dot" dot
)
260 (const :tag
"Ebnf2ps" ebnf2ps
)
261 (const :tag
"Emacs Lisp" emacs-lisp
)
262 (const :tag
"Forth" forth
)
263 (const :tag
"Fortran" fortran
)
264 (const :tag
"Gnuplot" gnuplot
)
265 (const :tag
"Haskell" haskell
)
266 (const :tag
"hledger" hledger
)
269 (const :tag
"Java" java
)
270 (const :tag
"Javascript" js
)
271 (const :tag
"LaTeX" latex
)
272 (const :tag
"Ledger" ledger
)
273 (const :tag
"Lilypond" lilypond
)
274 (const :tag
"Lisp" lisp
)
275 (const :tag
"Makefile" makefile
)
276 (const :tag
"Maxima" maxima
)
277 (const :tag
"Matlab" matlab
)
278 (const :tag
"Mscgen" mscgen
)
279 (const :tag
"Ocaml" ocaml
)
280 (const :tag
"Octave" octave
)
281 (const :tag
"Org" org
)
282 (const :tag
"Perl" perl
)
283 (const :tag
"Pico Lisp" picolisp
)
284 (const :tag
"PlantUML" plantuml
)
285 (const :tag
"Python" python
)
286 (const :tag
"Ruby" ruby
)
287 (const :tag
"Sass" sass
)
288 (const :tag
"Scala" scala
)
289 (const :tag
"Scheme" scheme
)
290 (const :tag
"Screen" screen
)
291 (const :tag
"Shell Script" shell
)
292 (const :tag
"Shen" shen
)
293 (const :tag
"Sql" sql
)
294 (const :tag
"Sqlite" sqlite
)
295 (const :tag
"Stan" stan
)
296 (const :tag
"Vala" vala
))
297 :value-type
(boolean :tag
"Activate" :value t
)))
299 ;;;; Customization variables
300 (defcustom org-clone-delete-id nil
301 "Remove ID property of clones of a subtree.
302 When non-nil, clones of a subtree don't inherit the ID property.
303 Otherwise they inherit the ID property with a new unique
313 (defun org-version (&optional here full message
)
314 "Show the Org version.
315 Interactively, or when MESSAGE is non-nil, show it in echo area.
316 With prefix argument, or when HERE is non-nil, insert it at point.
317 In non-interactive uses, a reduced version string is output unless
319 (interactive (list current-prefix-arg t
(not current-prefix-arg
)))
320 (let ((org-dir (ignore-errors (org-find-library-dir "org")))
321 (save-load-suffixes (when (boundp 'load-suffixes
) load-suffixes
))
322 (load-suffixes (list ".el"))
324 (ignore-errors (org-find-library-dir "org-loaddefs"))))
325 (unless (and (fboundp 'org-release
) (fboundp 'org-git-version
))
326 (org-load-noerror-mustsuffix (concat org-dir
"org-version")))
327 (let* ((load-suffixes save-load-suffixes
)
328 (release (org-release))
329 (git-version (org-git-version))
330 (version (format "Org mode version %s (%s @ %s)"
334 (if (string= org-dir org-install-dir
)
336 (concat "mixed installation! "
340 "org-loaddefs.el can not be found!")))
341 (version1 (if full version release
)))
342 (when here
(insert version1
))
343 (when message
(message "%s" version1
))
346 (defconst org-version
(org-version))
353 (defconst org-block-regexp
354 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
355 "Regular expression for hiding blocks.")
357 (defconst org-dblock-start-re
358 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
359 "Matches the start line of a dynamic block, with parameters.")
361 (defconst org-dblock-end-re
"^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
362 "Matches the end of a dynamic block.")
364 ;;;; Clock and Planning
366 (defconst org-clock-string
"CLOCK:"
367 "String used as prefix for timestamps clocking work hours on an item.")
369 (defvar org-closed-string
"CLOSED:"
370 "String used as the prefix for timestamps logging closing a TODO entry.")
372 (defvar org-deadline-string
"DEADLINE:"
373 "String to mark deadline entries.
375 A deadline is this string, followed by a time stamp. It must be
376 a word, terminated by a colon. You can insert a schedule keyword
377 and a timestamp with `\\[org-deadline]'.")
379 (defvar org-scheduled-string
"SCHEDULED:"
380 "String to mark scheduled TODO entries.
382 A schedule is this string, followed by a time stamp. It must be
383 a word, terminated by a colon. You can insert a schedule keyword
384 and a timestamp with `\\[org-schedule]'.")
386 (defconst org-ds-keyword-length
390 (list org-deadline-string org-scheduled-string
391 org-clock-string org-closed-string
))))
392 "Maximum length of the DEADLINE and SCHEDULED keywords.")
394 (defconst org-planning-line-re
397 (list org-closed-string org-deadline-string org-scheduled-string
)
399 "Matches a line with planning info.
400 Matched keyword is in group 1.")
402 (defconst org-clock-line-re
403 (concat "^[ \t]*" org-clock-string
)
404 "Matches a line with clock info.")
406 (defconst org-deadline-regexp
(concat "\\<" org-deadline-string
)
407 "Matches the DEADLINE keyword.")
409 (defconst org-deadline-time-regexp
410 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
411 "Matches the DEADLINE keyword together with a time stamp.")
413 (defconst org-deadline-time-hour-regexp
414 (concat "\\<" org-deadline-string
415 " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
416 "Matches the DEADLINE keyword together with a time-and-hour stamp.")
418 (defconst org-deadline-line-regexp
419 (concat "\\<\\(" org-deadline-string
"\\).*")
420 "Matches the DEADLINE keyword and the rest of the line.")
422 (defconst org-scheduled-regexp
(concat "\\<" org-scheduled-string
)
423 "Matches the SCHEDULED keyword.")
425 (defconst org-scheduled-time-regexp
426 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
427 "Matches the SCHEDULED keyword together with a time stamp.")
429 (defconst org-scheduled-time-hour-regexp
430 (concat "\\<" org-scheduled-string
431 " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
432 "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
434 (defconst org-closed-time-regexp
435 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
436 "Matches the CLOSED keyword together with a time stamp.")
438 (defconst org-keyword-time-regexp
441 (list org-scheduled-string org-deadline-string org-closed-string
444 " *[[<]\\([^]>]+\\)[]>]")
445 "Matches any of the 4 keywords, together with the time stamp.")
447 (defconst org-keyword-time-not-clock-regexp
451 (list org-scheduled-string org-deadline-string org-closed-string
) t
)
452 " *[[<]\\([^]>]+\\)[]>]")
453 "Matches any of the 3 keywords, together with the time stamp.")
455 (defconst org-maybe-keyword-time-regexp
458 (list org-scheduled-string org-deadline-string org-closed-string
462 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
465 "Matches a timestamp, possibly preceded by a keyword.")
467 (defconst org-all-time-keywords
468 (mapcar (lambda (w) (substring w
0 -
1))
469 (list org-scheduled-string org-deadline-string
470 org-clock-string org-closed-string
))
471 "List of time keywords.")
475 (defconst org-drawer-regexp
"^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
476 "Matches first or last line of a hidden block.
477 Group 1 contains drawer's name or \"END\".")
479 (defconst org-property-start-re
"^[ \t]*:PROPERTIES:[ \t]*$"
480 "Regular expression matching the first line of a property drawer.")
482 (defconst org-property-end-re
"^[ \t]*:END:[ \t]*$"
483 "Regular expression matching the last line of a property drawer.")
485 (defconst org-clock-drawer-start-re
"^[ \t]*:CLOCK:[ \t]*$"
486 "Regular expression matching the first line of a clock drawer.")
488 (defconst org-clock-drawer-end-re
"^[ \t]*:END:[ \t]*$"
489 "Regular expression matching the last line of a clock drawer.")
491 (defconst org-property-drawer-re
492 (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
493 "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
494 "[ \t]*:END:[ \t]*$")
495 "Matches an entire property drawer.")
497 (defconst org-clock-drawer-re
498 (concat "\\(" org-clock-drawer-start-re
"\\)[^\000]*?\\("
499 org-clock-drawer-end-re
"\\)\n?")
500 "Matches an entire clock drawer.")
504 (defconst org-heading-keyword-regexp-format
505 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
506 "Printf format for a regexp matching a headline with some keyword.
507 This regexp will match the headline of any node which has the
508 exact keyword that is put into the format. The keyword isn't in
509 any group by default, but the stars and the body are.")
511 (defconst org-heading-keyword-maybe-regexp-format
512 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
513 "Printf format for a regexp matching a headline, possibly with some keyword.
514 This regexp can match any headline with the specified keyword, or
515 without a keyword. The keyword isn't in any group by default,
516 but the stars and the body are.")
518 (defconst org-archive-tag
"ARCHIVE"
519 "The tag that marks a subtree as archived.
520 An archived subtree does not open during visibility cycling, and does
521 not contribute to the agenda listings.")
524 (defconst org-comment-string
"COMMENT"
525 "Entries starting with this keyword will never be exported.
527 An entry can be toggled between COMMENT and normal with
528 `\\[org-toggle-comment]'."))
531 ;;;; LaTeX Environments and Fragments
533 (defconst org-latex-regexps
534 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
535 ;; ("$" "\\([ \t(]\\|^\\)\\(\\(\\([$]\\)\\([^ \t\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \t\n,.$]\\)\\4\\)\\)\\([ \t.,?;:'\")]\\|$\\)" 2 nil)
536 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
537 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \t\r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|'\\|$\\)" 2 nil
)
538 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \t\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \t\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|'\\|$\\)" 2 nil
)
539 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
540 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil
)
541 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil
))
542 "Regular expressions for matching embedded LaTeX.")
546 (defconst org-effort-property
"Effort"
547 "The property that is being used to keep track of effort estimates.
548 Effort estimates given in this property need to have the format H:MM.")
552 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
553 "Detect an org-type or table-type table.")
555 (defconst org-table-line-regexp
"^[ \t]*|"
556 "Detect an org-type table line.")
558 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
559 "Detect an org-type table line.")
561 (defconst org-table-hline-regexp
"^[ \t]*|-"
562 "Detect an org-type table hline.")
564 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
565 "Detect a table-type table hline.")
567 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
568 "Detect the first line outside a table when searching from within it.
569 This works for both table types.")
571 (defconst org-TBLFM-regexp
"^[ \t]*#\\+TBLFM: "
572 "Detect a #+TBLFM line.")
576 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
577 "Regular expression for fast time stamp matching.")
579 (defconst org-ts-regexp-inactive
580 "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
581 "Regular expression for fast inactive time stamp matching.")
583 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
584 "Regular expression for fast time stamp matching.")
586 (defconst org-ts-regexp0
587 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
588 "Regular expression matching time strings for analysis.
589 This one does not require the space after the date, so it can be used
590 on a string that terminates immediately after the date.")
592 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
593 "Regular expression matching time strings for analysis.")
595 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
596 "Regular expression matching time stamps, with groups.")
598 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
599 "Regular expression matching time stamps (also [..]), with groups.")
601 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
602 "Regular expression matching a time stamp range.")
604 (defconst org-tr-regexp-both
605 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
606 "Regular expression matching a time stamp range.")
608 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
609 org-ts-regexp
"\\)?")
610 "Regular expression matching a time stamp or time stamp range.")
612 (defconst org-tsr-regexp-both
613 (concat org-ts-regexp-both
"\\(--?-?"
614 org-ts-regexp-both
"\\)?")
615 "Regular expression matching a time stamp or time stamp range.
616 The time stamps may be either active or inactive.")
618 (defconst org-repeat-re
619 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
620 "Regular expression for specifying repeated events.
621 After a match, group 1 contains the repeat expression.")
623 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
624 "Formats for `format-time-string' which are used for time stamps.")
627 ;;; The custom variables
630 "Outline-based notes management and organizer."
635 (defcustom org-mode-hook nil
636 "Mode hook for Org mode, run after the mode was turned on."
640 (defcustom org-load-hook nil
641 "Hook that is run after org.el has been loaded."
645 (defcustom org-log-buffer-setup-hook nil
646 "Hook that is run after an Org log buffer is created."
651 (defvar org-modules
) ; defined below
652 (defvar org-modules-loaded nil
653 "Have the modules been loaded already?")
655 (defun org-load-modules-maybe (&optional force
)
656 "Load all extensions listed in `org-modules'."
657 (when (or force
(not org-modules-loaded
))
658 (dolist (ext org-modules
)
659 (condition-case nil
(require ext
)
660 (error (message "Problems while trying to load feature `%s'" ext
))))
661 (setq org-modules-loaded t
)))
663 (defun org-set-modules (var value
)
664 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
666 (when (featurep 'org
)
667 (org-load-modules-maybe 'force
)
668 (org-element-cache-reset 'all
)))
670 (defcustom org-modules
'(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail
)
671 "Modules that should always be loaded together with org.el.
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 You can also use this system to load external packages (i.e. neither Org
678 core modules, nor modules from the CONTRIB directory). Just add symbols
679 to the end of the list. If the package is called org-xyz.el, then you need
680 to add the symbol `xyz', and the package must have a call to:
682 (provide \\='org-xyz)
684 For export specific modules, see also `org-export-backends'."
686 :set
'org-set-modules
688 :package-version
'(Org .
"8.0")
691 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
692 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
693 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
694 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
695 (const :tag
" docview: Links to doc-view buffers" org-docview
)
696 (const :tag
" eww: Store link to url of eww" org-eww
)
697 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
698 (const :tag
" habit: Track your consistency with habits" org-habit
)
699 (const :tag
" id: Global IDs for identifying entries" org-id
)
700 (const :tag
" info: Links to Info nodes" org-info
)
701 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
702 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
703 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
704 (const :tag
" mouse: Additional mouse support" org-mouse
)
705 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
706 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
707 (const :tag
" tempo: Fast completion for structures" org-tempo
)
708 (const :tag
" w3m: Special cut/paste from w3m to Org mode." org-w3m
)
710 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
711 (const :tag
"C bookmark: Org links to bookmarks" org-bookmark
)
712 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
713 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
714 (const :tag
"C collector: Collect properties into tables" org-collector
)
715 (const :tag
"C depend: TODO dependencies for Org mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
716 (const :tag
"C drill: Flashcards and spaced repetition for Org mode" org-drill
)
717 (const :tag
"C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol
)
718 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
719 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
720 (const :tag
"C eval: Include command output as text" org-eval
)
721 (const :tag
"C expiry: Expiry mechanism for Org entries" org-expiry
)
722 (const :tag
"C favtable: Lookup table of favorite references and links" org-favtable
)
723 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
724 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
725 (const :tag
"C invoice: Help manage client invoices in Org mode" org-invoice
)
726 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
727 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
728 (const :tag
"C mac-link: Grab links and url from various mac Applications" org-mac-link
)
729 (const :tag
"C mairix: Hook mairix search into Org for different MUAs" org-mairix
)
730 (const :tag
"C man: Support for links to manpages in Org mode" org-man
)
731 (const :tag
"C mew: Links to Mew folders/messages" org-mew
)
732 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
733 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
734 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
735 (const :tag
"C registry: A registry for Org links" org-registry
)
736 (const :tag
"C screen: Visit screen sessions through Org links" org-screen
)
737 (const :tag
"C secretary: Team management with Org" org-secretary
)
738 (const :tag
"C sqlinsert: Convert Org tables to SQL insertions" orgtbl-sqlinsert
)
739 (const :tag
"C toc: Table of contents for Org buffer" org-toc
)
740 (const :tag
"C track: Keep up with Org mode development" org-track
)
741 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
742 (const :tag
"C vm: Links to VM folders/messages" org-vm
)
743 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
744 (const :tag
"C wl: Links to Wanderlust folders/messages" org-wl
)
745 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
747 (defvar org-export-registered-backends
) ; From ox.el.
748 (declare-function org-export-derived-backend-p
"ox" (backend &rest backends
))
749 (declare-function org-export-backend-name
"ox" (backend) t
)
750 (defcustom org-export-backends
'(ascii html icalendar latex odt
)
751 "List of export back-ends that should be always available.
753 If a description starts with <C>, the file is not part of Emacs
754 and loading it will require that you have downloaded and properly
755 installed the Org mode distribution.
757 Unlike to `org-modules', libraries in this list will not be
758 loaded along with Org, but only once the export framework is
761 This variable needs to be set before org.el is loaded. If you
762 need to make a change while Emacs is running, use the customize
763 interface or run the following code, where VAL stands for the new
764 value of the variable, after updating it:
767 (setq org-export-registered-backends
770 (let ((name (org-export-backend-name backend)))
774 (and (org-export-derived-backend-p b name)
775 (throw \\='parentp t)))))))
776 org-export-registered-backends))
777 (let ((new-list (mapcar #\\='org-export-backend-name
778 org-export-registered-backends)))
779 (dolist (backend val)
781 ((not (load (format \"ox-%s\" backend) t t))
782 (message \"Problems while trying to load export back-end \\=`%s\\='\"
784 ((not (memq backend new-list)) (push backend new-list))))
785 (set-default \\='org-export-backends new-list)))
787 Adding a back-end to this list will also pull the back-end it
792 :package-version
'(Org .
"9.0")
793 :initialize
'custom-initialize-set
794 :set
(lambda (var val
)
795 (if (not (featurep 'ox
)) (set-default var val
)
796 ;; Any back-end not required anymore (not present in VAL and not
797 ;; a parent of any back-end in the new value) is removed from the
798 ;; list of registered back-ends.
799 (setq org-export-registered-backends
802 (let ((name (org-export-backend-name backend
)))
806 (and (org-export-derived-backend-p b name
)
807 (throw 'parentp t
)))))))
808 org-export-registered-backends
))
809 ;; Now build NEW-LIST of both new back-ends and required
811 (let ((new-list (mapcar #'org-export-backend-name
812 org-export-registered-backends
)))
813 (dolist (backend val
)
815 ((not (load (format "ox-%s" backend
) t t
))
816 (message "Problems while trying to load export back-end `%s'"
818 ((not (memq backend new-list
)) (push backend new-list
))))
819 ;; Set VAR to that list with fixed dependencies.
820 (set-default var new-list
))))
821 :type
'(set :greedy t
822 (const :tag
" ascii Export buffer to ASCII format" ascii
)
823 (const :tag
" beamer Export buffer to Beamer presentation" beamer
)
824 (const :tag
" html Export buffer to HTML format" html
)
825 (const :tag
" icalendar Export buffer to iCalendar format" icalendar
)
826 (const :tag
" latex Export buffer to LaTeX format" latex
)
827 (const :tag
" man Export buffer to MAN format" man
)
828 (const :tag
" md Export buffer to Markdown format" md
)
829 (const :tag
" odt Export buffer to ODT format" odt
)
830 (const :tag
" org Export buffer to Org format" org
)
831 (const :tag
" texinfo Export buffer to Texinfo format" texinfo
)
832 (const :tag
"C confluence Export buffer to Confluence Wiki format" confluence
)
833 (const :tag
"C deck Export buffer to deck.js presentations" deck
)
834 (const :tag
"C freemind Export buffer to Freemind mindmap format" freemind
)
835 (const :tag
"C groff Export buffer to Groff format" groff
)
836 (const :tag
"C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter
)
837 (const :tag
"C RSS 2.0 Export buffer to RSS 2.0 format" rss
)
838 (const :tag
"C s5 Export buffer to s5 presentations" s5
)
839 (const :tag
"C taskjuggler Export buffer to TaskJuggler format" taskjuggler
)))
842 '(dolist (backend org-export-backends
)
843 (condition-case nil
(require (intern (format "ox-%s" backend
)))
844 (error (message "Problems while trying to load export back-end `%s'"
847 (defcustom org-support-shift-select nil
848 "Non-nil means make shift-cursor commands select text when possible.
851 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
852 start selecting a region, or enlarge regions started in this way.
853 In Org mode, in special contexts, these same keys are used for
854 other purposes, important enough to compete with shift selection.
855 Org tries to balance these needs by supporting `shift-select-mode'
856 outside these special contexts, under control of this variable.
858 The default of this variable is nil, to avoid confusing behavior. Shifted
859 cursor keys will then execute Org commands in the following contexts:
860 - on a headline, changing TODO state (left/right) and priority (up/down)
861 - on a time stamp, changing the time
862 - in a plain list item, changing the bullet type
863 - in a property definition line, switching between allowed values
864 - in the BEGIN line of a clock table (changing the time block).
865 Outside these contexts, the commands will throw an error.
867 When this variable is t and the cursor is not in a special
868 context, Org mode will support shift-selection for making and
869 enlarging regions. To make this more effective, the bullet
870 cycling will no longer happen anywhere in an item line, but only
871 if the cursor is exactly on the bullet.
873 If you set this variable to the symbol `always', then the keys
874 will not be special in headlines, property lines, and item lines,
875 to make shift selection work there as well. If this is what you
876 want, you can use the following alternative commands:
877 `\\[org-todo]' and `\\[org-priority]' \
878 to change TODO state and priority,
879 `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
880 can be used to switch TODO sets,
881 `\\[org-ctrl-c-minus]' to cycle item bullet types,
882 and properties can be edited by hand or in column view.
884 However, when the cursor is on a timestamp, shift-cursor commands
885 will still edit the time stamp - this is just too good to give up."
888 (const :tag
"Never" nil
)
889 (const :tag
"When outside special context" t
)
890 (const :tag
"Everywhere except timestamps" always
)))
892 (defcustom org-loop-over-headlines-in-active-region nil
893 "Shall some commands act upon headlines in the active region?
895 When set to t, some commands will be performed in all headlines
896 within the active region.
898 When set to `start-level', some commands will be performed in all
899 headlines within the active region, provided that these headlines
900 are of the same level than the first one.
902 When set to a string, those commands will be performed on the
903 matching headlines within the active region. Such string must be
904 a tags/property/todo match as it is used in the agenda tags view.
906 The list of commands is: `org-schedule', `org-deadline',
907 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
908 `org-archive-to-archive-sibling'. The archiving commands skip
909 already archived entries."
910 :type
'(choice (const :tag
"Don't loop" nil
)
911 (const :tag
"All headlines in active region" t
)
912 (const :tag
"In active region, headlines at the same level than the first one" start-level
)
913 (string :tag
"Tags/Property/Todo matcher"))
918 (defcustom org-startup-folded t
919 "Non-nil means entering Org mode will switch to OVERVIEW.
921 This can also be configured on a per-file basis by adding one of
922 the following lines anywhere in the buffer:
924 #+STARTUP: fold (or `overview', this is equivalent)
925 #+STARTUP: nofold (or `showall', this is equivalent)
927 #+STARTUP: showeverything
929 Set `org-agenda-inhibit-startup' to a non-nil value if you want
930 to ignore this option when Org opens agenda files for the first
934 (const :tag
"nofold: show all" nil
)
935 (const :tag
"fold: overview" t
)
936 (const :tag
"content: all headlines" content
)
937 (const :tag
"show everything, even drawers" showeverything
)))
939 (defcustom org-startup-truncated t
940 "Non-nil means entering Org mode will set `truncate-lines'.
941 This is useful since some lines containing links can be very long and
942 uninteresting. Also tables look terrible when wrapped.
944 The variable `org-startup-truncated' allows to configure
945 truncation for Org mode different to the other modes that use the
946 variable `truncate-lines' and as a shortcut instead of putting
947 the variable `truncate-lines' into the `org-mode-hook'. If one
948 wants to configure truncation for Org mode not statically but
949 dynamically e. g. in a hook like `ediff-prepare-buffer-hook' then
950 the variable `truncate-lines' has to be used because in such a
951 case it is too late to set the variable `org-startup-truncated'."
955 (defcustom org-startup-indented nil
956 "Non-nil means turn on `org-indent-mode' on startup.
957 This can also be configured on a per-file basis by adding one of
958 the following lines anywhere in the buffer:
962 :group
'org-structure
964 (const :tag
"Not" nil
)
965 (const :tag
"Globally (slow on startup in large files)" t
)))
967 (defcustom org-use-sub-superscripts t
968 "Non-nil means interpret \"_\" and \"^\" for display.
970 If you want to control how Org exports those characters, see
971 `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
972 used to be an alias for `org-export-with-sub-superscripts' in
973 Org <8.0, it is not anymore.
975 When this option is turned on, you can use TeX-like syntax for
976 sub- and superscripts within the buffer. Several characters after
977 \"_\" or \"^\" will be considered as a single item - so grouping
978 with {} is normally not needed. For example, the following things
979 will be parsed as single sub- or superscripts:
981 10^24 or 10^tau several digits will be considered 1 item.
982 10^-12 or 10^-tau a leading sign with digits or a word
983 x^2-y^3 will be read as x^2 - y^3, because items are
984 terminated by almost any nonword/nondigit char.
985 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
987 Still, ambiguity is possible. So when in doubt, use {} to enclose
988 the sub/superscript. If you set this variable to the symbol `{}',
989 the braces are *required* in order to trigger interpretations as
990 sub/superscript. This can be helpful in documents that need \"_\"
991 frequently in plain text."
994 :package-version
'(Org .
"8.0")
996 (const :tag
"Always interpret" t
)
997 (const :tag
"Only with braces" {})
998 (const :tag
"Never interpret" nil
)))
1000 (defcustom org-startup-with-beamer-mode nil
1001 "Non-nil means turn on `org-beamer-mode' on startup.
1002 This can also be configured on a per-file basis by adding one of
1003 the following lines anywhere in the buffer:
1010 (defcustom org-startup-align-all-tables nil
1011 "Non-nil means align all tables when visiting a file.
1012 This can also be configured on a per-file basis by adding one of
1013 the following lines anywhere in the buffer:
1019 (defcustom org-startup-shrink-all-tables nil
1020 "Non-nil means shrink all table columns with a width cookie.
1021 This can also be configured on a per-file basis by adding one of
1022 the following lines anywhere in the buffer:
1027 :package-version
'(Org .
"9.2")
1030 (defcustom org-startup-with-inline-images nil
1031 "Non-nil means show inline images when loading a new Org file.
1032 This can also be configured on a per-file basis by adding one of
1033 the following lines anywhere in the buffer:
1034 #+STARTUP: inlineimages
1035 #+STARTUP: noinlineimages"
1040 (defcustom org-startup-with-latex-preview nil
1041 "Non-nil means preview LaTeX fragments when loading a new Org file.
1043 This can also be configured on a per-file basis by adding one of
1044 the following lines anywhere in the buffer:
1045 #+STARTUP: latexpreview
1046 #+STARTUP: nolatexpreview"
1049 :package-version
'(Org .
"8.0")
1052 (defcustom org-insert-mode-line-in-empty-file nil
1053 "Non-nil means insert the first line setting Org mode in empty files.
1054 When the function `org-mode' is called interactively in an empty file, this
1055 normally means that the file name does not automatically trigger Org mode.
1056 To ensure that the file will always be in Org mode in the future, a
1057 line enforcing Org mode will be inserted into the buffer, if this option
1062 (defcustom org-replace-disputed-keys nil
1063 "Non-nil means use alternative key bindings for some keys.
1065 Org mode uses S-<cursor> keys for changing timestamps and priorities.
1066 These keys are also used by other packages like Shift Select mode,
1067 CUA mode or Windmove. If you want to use Org mode together with
1068 one of these other modes, or more generally if you would like to
1069 move some Org mode commands to other keys, set this variable and
1070 configure the keys with the variable `org-disputed-keys'.
1072 This option is only relevant at load-time of Org mode, and must be set
1073 *before* org.el is loaded. Changing it requires a restart of Emacs to
1078 (defcustom org-use-extra-keys nil
1079 "Non-nil means use extra key sequence definitions for certain commands.
1080 This happens automatically if `window-system' is nil. This
1081 variable lets you do the same manually. You must set it before
1086 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
)
1088 (defcustom org-disputed-keys
1089 '(([(shift up
)] .
[(meta p
)])
1090 ([(shift down
)] .
[(meta n
)])
1091 ([(shift left
)] .
[(meta -
)])
1092 ([(shift right
)] .
[(meta +)])
1093 ([(control shift right
)] .
[(meta shift
+)])
1094 ([(control shift left
)] .
[(meta shift -
)]))
1095 "Keys for which Org mode and other modes compete.
1096 This is an alist, cars are the default keys, second element specifies
1097 the alternative to use when `org-replace-disputed-keys' is t.
1099 Keys can be specified in any syntax supported by `define-key'.
1100 The value of this option takes effect only at Org mode startup,
1101 therefore you'll have to restart Emacs to apply it after changing."
1105 (defun org-key (key)
1106 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
1107 Or return the original if not disputed."
1108 (when org-replace-disputed-keys
1109 (let* ((nkey (key-description key
))
1110 (x (cl-find-if (lambda (x) (equal (key-description (car x
)) nkey
))
1111 org-disputed-keys
)))
1112 (setq key
(if x
(cdr x
) key
))))
1115 (defun org-defkey (keymap key def
)
1116 "Define a key, possibly translated, as returned by `org-key'."
1117 (define-key keymap
(org-key key
) def
))
1119 (defcustom org-ellipsis nil
1120 "The ellipsis to use in the Org mode outline.
1122 When nil, just use the standard three dots. When a non-empty string,
1123 use that string instead.
1125 The change affects only Org mode (which will then use its own display table).
1126 Changing this requires executing `\\[org-mode]' in a buffer to become
1129 :type
'(choice (const :tag
"Default" nil
)
1130 (string :tag
"String" :value
"...#"))
1131 :safe
(lambda (v) (and (string-or-null-p v
) (not (equal "" v
)))))
1133 (defvar org-display-table nil
1134 "The display table for Org mode, in case `org-ellipsis' is non-nil.")
1136 (defgroup org-keywords nil
1137 "Keywords in Org mode."
1141 (defcustom org-closed-keep-when-no-todo nil
1142 "Remove CLOSED: time-stamp when switching back to a non-todo state?"
1144 :group
'org-keywords
1146 :package-version
'(Org .
"8.0")
1149 (defgroup org-structure nil
1150 "Options concerning the general structure of Org files."
1151 :tag
"Org Structure"
1154 (defgroup org-reveal-location nil
1155 "Options about how to make context of a location visible."
1156 :tag
"Org Reveal Location"
1157 :group
'org-structure
)
1159 (defcustom org-show-context-detail
'((agenda . local
)
1160 (bookmark-jump . lineage
)
1162 (default . ancestors
))
1163 "Alist between context and visibility span when revealing a location.
1165 \\<org-mode-map>Some actions may move point into invisible
1166 locations. As a consequence, Org always expose a neighborhood
1167 around point. How much is shown depends on the initial action,
1168 or context. Valid contexts are
1170 agenda when exposing an entry from the agenda
1171 org-goto when using the command `org-goto' (`\\[org-goto]')
1172 occur-tree when using the command `org-occur' (`\\[org-sparse-tree] /')
1173 tags-tree when constructing a sparse tree based on tags matches
1174 link-search when exposing search matches associated with a link
1175 mark-goto when exposing the jump goal of a mark
1176 bookmark-jump when exposing a bookmark location
1177 isearch when exiting from an incremental search
1178 default default for all contexts not set explicitly
1180 Allowed visibility spans are
1182 minimal show current headline; if point is not on headline,
1185 local show current headline, entry and next headline
1187 ancestors show current headline and its direct ancestors; if
1188 point is not on headline, also show entry
1190 lineage show current headline, its direct ancestors and all
1191 their children; if point is not on headline, also show
1192 entry and first child
1194 tree show current headline, its direct ancestors and all
1195 their children; if point is not on headline, also show
1196 entry and all children
1198 canonical show current headline, its direct ancestors along with
1199 their entries and children; if point is not located on
1200 the headline, also show current entry and all children
1202 As special cases, a nil or t value means show all contexts in
1203 `minimal' or `canonical' view, respectively.
1205 Some views can make displayed information very compact, but also
1206 make it harder to edit the location of the match. In such
1207 a case, use the command `org-reveal' (`\\[org-reveal]') to show
1209 :group
'org-reveal-location
1211 :package-version
'(Org .
"9.0")
1213 (const :tag
"Canonical" t
)
1214 (const :tag
"Minimal" nil
)
1215 (repeat :greedy t
:tag
"Individual contexts"
1217 (choice :tag
"Context"
1224 (const bookmark-jump
)
1227 (choice :tag
"Detail level"
1233 (const canonical
))))))
1235 (defcustom org-indirect-buffer-display
'other-window
1236 "How should indirect tree buffers be displayed?
1238 This applies to indirect buffers created with the commands
1239 `org-tree-to-indirect-buffer' and `org-agenda-tree-to-indirect-buffer'.
1242 current-window Display in the current window
1243 other-window Just display in another window.
1244 dedicated-frame Create one new frame, and re-use it each time.
1245 new-frame Make a new frame each time. Note that in this case
1246 previously-made indirect buffers are kept, and you need to
1247 kill these buffers yourself."
1248 :group
'org-structure
1249 :group
'org-agenda-windows
1251 (const :tag
"In current window" current-window
)
1252 (const :tag
"In current frame, other window" other-window
)
1253 (const :tag
"Each time a new frame" new-frame
)
1254 (const :tag
"One dedicated frame" dedicated-frame
)))
1256 (defcustom org-use-speed-commands nil
1257 "Non-nil means activate single letter commands at beginning of a headline.
1258 This may also be a function to test for appropriate locations where speed
1259 commands should be active.
1261 For example, to activate speed commands when the point is on any
1262 star at the beginning of the headline, you can do this:
1264 (setq org-use-speed-commands
1265 (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
1266 :group
'org-structure
1268 (const :tag
"Never" nil
)
1269 (const :tag
"At beginning of headline stars" t
)
1272 (defcustom org-speed-commands-user nil
1273 "Alist of additional speed commands.
1274 This list will be checked before `org-speed-commands-default'
1275 when the variable `org-use-speed-commands' is non-nil
1276 and when the cursor is at the beginning of a headline.
1277 The car of each entry is a string with a single letter, which must
1278 be assigned to `self-insert-command' in the global map.
1279 The cdr is either a command to be called interactively, a function
1280 to be called, or a form to be evaluated.
1281 An entry that is just a list with a single string will be interpreted
1282 as a descriptive headline that will be added when listing the speed
1283 commands in the Help buffer using the `?' speed command."
1284 :group
'org-structure
1285 :type
'(repeat :value
("k" . ignore
)
1286 (choice :value
("k" . ignore
)
1287 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
1288 (cons :tag
"Letter and Command"
1289 (string :tag
"Command letter")
1294 (defcustom org-bookmark-names-plist
1295 '(:last-capture
"org-capture-last-stored"
1296 :last-refile
"org-refile-last-stored"
1297 :last-capture-marker
"org-capture-last-stored-marker")
1298 "Names for bookmarks automatically set by some Org commands.
1299 This can provide strings as names for a number of bookmarks Org sets
1300 automatically. The following keys are currently implemented:
1302 :last-capture-marker
1304 When a key does not show up in the property list, the corresponding bookmark
1306 :group
'org-structure
1309 (defgroup org-cycle nil
1310 "Options concerning visibility cycling in Org mode."
1312 :group
'org-structure
)
1314 (defcustom org-cycle-skip-children-state-if-no-children t
1315 "Non-nil means skip CHILDREN state in entries that don't have any."
1319 (defcustom org-cycle-max-level nil
1320 "Maximum level which should still be subject to visibility cycling.
1321 Levels higher than this will, for cycling, be treated as text, not a headline.
1322 When `org-odd-levels-only' is set, a value of N in this variable actually
1323 means 2N-1 stars as the limiting headline.
1324 When nil, cycle all levels.
1325 Note that the limiting level of cycling is also influenced by
1326 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
1327 `org-inlinetask-min-level' is, cycling will be limited to levels one less
1331 (const :tag
"No limit" nil
)
1332 (integer :tag
"Maximum level")))
1334 (defcustom org-hide-block-startup nil
1335 "Non-nil means entering Org mode will fold all blocks.
1336 This can also be set in on a per-file basis with
1338 #+STARTUP: hideblocks
1339 #+STARTUP: showblocks"
1344 (defcustom org-cycle-global-at-bob nil
1345 "Cycle globally if cursor is at beginning of buffer and not at a headline.
1347 This makes it possible to do global cycling without having to use `S-TAB'
1348 or `\\[universal-argument] TAB'. For this special case to work, the first \
1350 must not be a headline -- it may be empty or some other text.
1352 When used in this way, `org-cycle-hook' is disabled temporarily to make
1353 sure the cursor stays at the beginning of the buffer.
1355 When this option is nil, don't do anything special at the beginning of
1360 (defcustom org-cycle-level-after-item
/entry-creation t
1361 "Non-nil means cycle entry level or item indentation in new empty entries.
1363 When the cursor is at the end of an empty headline, i.e., with only stars
1364 and maybe a TODO keyword, TAB will then switch the entry to become a child,
1365 and then all possible ancestor states, before returning to the original state.
1366 This makes data entry extremely fast: M-RET to create a new headline,
1367 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
1369 When the cursor is at the end of an empty plain list item, one TAB will
1370 make it a subitem, two or more tabs will back up to make this an item
1371 higher up in the item hierarchy."
1375 (defcustom org-cycle-emulate-tab t
1376 "Where should `org-cycle' emulate TAB.
1378 white Only in completely white lines
1379 whitestart Only at the beginning of lines, before the first non-white char
1380 t Everywhere except in headlines
1381 exc-hl-bol Everywhere except at the start of a headline
1382 If TAB is used in a place where it does not emulate TAB, the current subtree
1383 visibility is cycled."
1385 :type
'(choice (const :tag
"Never" nil
)
1386 (const :tag
"Only in completely white lines" white
)
1387 (const :tag
"Before first char in a line" whitestart
)
1388 (const :tag
"Everywhere except in headlines" t
)
1389 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)))
1391 (defcustom org-cycle-separator-lines
2
1392 "Number of empty lines needed to keep an empty line between collapsed trees.
1393 If you leave an empty line between the end of a subtree and the following
1394 headline, this empty line is hidden when the subtree is folded.
1395 Org mode will leave (exactly) one empty line visible if the number of
1396 empty lines is equal or larger to the number given in this variable.
1397 So the default 2 means at least 2 empty lines after the end of a subtree
1398 are needed to produce free space between a collapsed subtree and the
1401 If the number is negative, and the number of empty lines is at least -N,
1402 all empty lines are shown.
1404 Special case: when 0, never leave empty lines in collapsed view."
1407 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
1409 (defcustom org-pre-cycle-hook nil
1410 "Hook that is run before visibility cycling is happening.
1411 The function(s) in this hook must accept a single argument which indicates
1412 the new state that will be set right after running this hook. The
1413 argument is a symbol. Before a global state change, it can have the values
1414 `overview', `content', or `all'. Before a local state change, it can have
1415 the values `folded', `children', or `subtree'."
1419 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
1420 org-cycle-show-empty-lines
1421 org-optimize-window-after-visibility-change
)
1422 "Hook that is run after `org-cycle' has changed the buffer visibility.
1423 The function(s) in this hook must accept a single argument which indicates
1424 the new state that was set by the most recent `org-cycle' command. The
1425 argument is a symbol. After a global state change, it can have the values
1426 `overview', `contents', or `all'. After a local state change, it can have
1427 the values `folded', `children', or `subtree'."
1431 :package-version
'(Org .
"8.3"))
1433 (defgroup org-edit-structure nil
1434 "Options concerning structure editing in Org mode."
1435 :tag
"Org Edit Structure"
1436 :group
'org-structure
)
1438 (defcustom org-odd-levels-only nil
1439 "Non-nil means skip even levels and only use odd levels for the outline.
1440 This has the effect that two stars are being added/taken away in
1441 promotion/demotion commands. It also influences how levels are
1442 handled by the exporters.
1443 Changing it requires restart of `font-lock-mode' to become effective
1444 for fontification also in regions already fontified.
1445 You may also set this on a per-file basis by adding one of the following
1446 lines to the buffer:
1450 :group
'org-edit-structure
1451 :group
'org-appearance
1454 (defcustom org-adapt-indentation t
1455 "Non-nil means adapt indentation to outline node level.
1457 When this variable is set, Org assumes that you write outlines by
1458 indenting text in each node to align with the headline (after the
1459 stars). The following issues are influenced by this variable:
1461 - The indentation is increased by one space in a demotion
1462 command, and decreased by one in a promotion command. However,
1463 in the latter case, if shifting some line in the entry body
1464 would alter document structure (e.g., insert a new headline),
1465 indentation is not changed at all.
1467 - Property drawers and planning information is inserted indented
1468 when this variable is set. When nil, they will not be indented.
1470 - TAB indents a line relative to current level. The lines below
1471 a headline will be indented when this variable is set.
1473 Note that this is all about true indentation, by adding and
1474 removing space characters. See also \"org-indent.el\" which does
1475 level-dependent indentation in a virtual way, i.e. at display
1477 :group
'org-edit-structure
1481 (defcustom org-special-ctrl-a
/e nil
1482 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1484 When t, `C-a' will bring back the cursor to the beginning of the
1485 headline text, i.e. after the stars and after a possible TODO
1486 keyword. In an item, this will be the position after bullet and
1487 check-box, if any. When the cursor is already at that position,
1488 another `C-a' will bring it to the beginning of the line.
1490 `C-e' will jump to the end of the headline, ignoring the presence
1491 of tags in the headline. A second `C-e' will then jump to the
1492 true end of the line, after any tags. This also means that, when
1493 this variable is non-nil, `C-e' also will never jump beyond the
1494 end of the heading of a folded section, i.e. not after the
1497 When set to the symbol `reversed', the first `C-a' or `C-e' works
1498 normally, going to the true line boundary first. Only a directly
1499 following, identical keypress will bring the cursor to the
1502 This may also be a cons cell where the behavior for `C-a' and
1503 `C-e' is set separately."
1504 :group
'org-edit-structure
1506 (const :tag
"off" nil
)
1507 (const :tag
"on: after stars/bullet and before tags first" t
)
1508 (const :tag
"reversed: true line boundary first" reversed
)
1509 (cons :tag
"Set C-a and C-e separately"
1510 (choice :tag
"Special C-a"
1511 (const :tag
"off" nil
)
1512 (const :tag
"on: after stars/bullet first" t
)
1513 (const :tag
"reversed: before stars/bullet first" reversed
))
1514 (choice :tag
"Special C-e"
1515 (const :tag
"off" nil
)
1516 (const :tag
"on: before tags first" t
)
1517 (const :tag
"reversed: after tags first" reversed
)))))
1518 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
)
1520 (defcustom org-special-ctrl-k nil
1521 "Non-nil means `C-k' will behave specially in headlines.
1522 When nil, `C-k' will call the default `kill-line' command.
1523 When t, the following will happen while the cursor is in the headline:
1525 - When the cursor is at the beginning of a headline, kill the entire
1526 line and possible the folded subtree below the line.
1527 - When in the middle of the headline text, kill the headline up to the tags.
1528 - When after the headline text, kill the tags."
1529 :group
'org-edit-structure
1532 (defcustom org-ctrl-k-protect-subtree nil
1533 "Non-nil means, do not delete a hidden subtree with C-k.
1534 When set to the symbol `error', simply throw an error when C-k is
1535 used to kill (part-of) a headline that has hidden text behind it.
1536 Any other non-nil value will result in a query to the user, if it is
1537 OK to kill that hidden subtree. When nil, kill without remorse."
1538 :group
'org-edit-structure
1541 (const :tag
"Do not protect hidden subtrees" nil
)
1542 (const :tag
"Protect hidden subtrees with a security query" t
)
1543 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1545 (defcustom org-special-ctrl-o t
1546 "Non-nil means, make `C-o' insert a row in tables."
1547 :group
'org-edit-structure
1550 (defcustom org-catch-invisible-edits nil
1551 "Check if in invisible region before inserting or deleting a character.
1554 nil Do not check, so just do invisible edits.
1555 error Throw an error and do nothing.
1556 show Make point visible, and do the requested edit.
1557 show-and-error Make point visible, then throw an error and abort the edit.
1558 smart Make point visible, and do insertion/deletion if it is
1559 adjacent to visible text and the change feels predictable.
1560 Never delete a previously invisible character or add in the
1561 middle or right after an invisible region. Basically, this
1562 allows insertion and backward-delete right before ellipses.
1563 FIXME: maybe in this case we should not even show?"
1564 :group
'org-edit-structure
1567 (const :tag
"Do not check" nil
)
1568 (const :tag
"Throw error when trying to edit" error
)
1569 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1570 (const :tag
"Show invisible part and do the edit" show
)
1571 (const :tag
"Be smart and do the right thing" smart
)))
1573 (defcustom org-yank-folded-subtrees t
1574 "Non-nil means when yanking subtrees, fold them.
1575 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1576 it starts with a heading and all other headings in it are either children
1577 or siblings, then fold all the subtrees. However, do this only if no
1578 text after the yank would be swallowed into a folded tree by this action."
1579 :group
'org-edit-structure
1582 (defcustom org-yank-adjusted-subtrees nil
1583 "Non-nil means when yanking subtrees, adjust the level.
1584 With this setting, `org-paste-subtree' is used to insert the subtree, see
1585 this function for details."
1586 :group
'org-edit-structure
1589 (defcustom org-M-RET-may-split-line
'((default . t
))
1590 "Non-nil means M-RET will split the line at the cursor position.
1591 When nil, it will go to the end of the line before making a
1593 You may also set this option in a different way for different
1594 contexts. Valid contexts are:
1596 headline when creating a new headline
1597 item when creating a new item
1598 table in a table field
1599 default the value to be used for all contexts not explicitly
1601 :group
'org-structure
1604 (const :tag
"Always" t
)
1605 (const :tag
"Never" nil
)
1606 (repeat :greedy t
:tag
"Individual contexts"
1608 (choice :tag
"Context"
1616 (defcustom org-insert-heading-respect-content nil
1617 "Non-nil means insert new headings after the current subtree.
1619 When nil, the new heading is created directly after the current line.
1620 The commands `\\[org-insert-heading-respect-content]' and \
1621 `\\[org-insert-todo-heading-respect-content]' turn this variable on
1622 for the duration of the command."
1623 :group
'org-structure
1626 (defcustom org-blank-before-new-entry
'((heading . auto
)
1627 (plain-list-item . auto
))
1628 "Should `org-insert-heading' leave a blank line before new heading/item?
1629 The value is an alist, with `heading' and `plain-list-item' as CAR,
1630 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1631 which case Org will look at the surrounding headings/items and try to
1632 make an intelligent decision whether to insert a blank line or not."
1633 :group
'org-edit-structure
1635 (cons (const heading
)
1636 (choice (const :tag
"Never" nil
)
1637 (const :tag
"Always" t
)
1638 (const :tag
"Auto" auto
)))
1639 (cons (const plain-list-item
)
1640 (choice (const :tag
"Never" nil
)
1641 (const :tag
"Always" t
)
1642 (const :tag
"Auto" auto
)))))
1644 (defcustom org-insert-heading-hook nil
1645 "Hook being run after inserting a new heading."
1646 :group
'org-edit-structure
1649 (defcustom org-enable-fixed-width-editor t
1650 "Non-nil means lines starting with \":\" are treated as fixed-width.
1651 This currently only means they are never auto-wrapped.
1652 When nil, such lines will be treated like ordinary lines."
1653 :group
'org-edit-structure
1656 (defgroup org-sparse-trees nil
1657 "Options concerning sparse trees in Org mode."
1658 :tag
"Org Sparse Trees"
1659 :group
'org-structure
)
1661 (defcustom org-highlight-sparse-tree-matches t
1662 "Non-nil means highlight all matches that define a sparse tree.
1663 The highlights will automatically disappear the next time the buffer is
1664 changed by an edit command."
1665 :group
'org-sparse-trees
1668 (defcustom org-remove-highlights-with-change t
1669 "Non-nil means any change to the buffer will remove temporary highlights.
1671 Such highlights are created by `org-occur' and `org-clock-display'.
1672 When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
1673 to get rid of the highlights.
1674 The highlights created by `org-toggle-latex-fragment' always need
1675 `\\[org-toggle-latex-fragment]' to be removed."
1676 :group
'org-sparse-trees
1680 (defcustom org-occur-case-fold-search t
1681 "Non-nil means `org-occur' should be case-insensitive.
1682 If set to `smart' the search will be case-insensitive only if it
1683 doesn't specify any upper case character."
1684 :group
'org-sparse-trees
1687 (const :tag
"Case-sensitive" nil
)
1688 (const :tag
"Case-insensitive" t
)
1689 (const :tag
"Case-insensitive for lower case searches only" smart
)))
1691 (defcustom org-occur-hook
'(org-first-headline-recenter)
1692 "Hook that is run after `org-occur' has constructed a sparse tree.
1693 This can be used to recenter the window to show as much of the structure
1695 :group
'org-sparse-trees
1698 (defgroup org-table nil
1699 "Options concerning tables in Org mode."
1703 (defcustom org-self-insert-cluster-for-undo nil
1704 "Non-nil means cluster self-insert commands for undo when possible.
1705 If this is set, then, like in the Emacs command loop, 20 consecutive
1706 characters will be undone together.
1707 This is configurable, because there is some impact on typing performance."
1711 (defcustom org-table-tab-recognizes-table.el t
1712 "Non-nil means TAB will automatically notice a table.el table.
1713 When it sees such a table, it moves point into it and - if necessary -
1714 calls `table-recognize-table'."
1715 :group
'org-table-editing
1718 (defgroup org-link nil
1719 "Options concerning links in Org mode."
1723 (defvar-local org-link-abbrev-alist-local nil
1724 "Buffer-local version of `org-link-abbrev-alist', which see.
1725 The value of this is taken from the #+LINK lines.")
1727 (defcustom org-link-parameters
1728 '(("doi" :follow org--open-doi-link
)
1729 ("elisp" :follow org--open-elisp-link
)
1730 ("file" :complete org-file-complete-link
)
1731 ("ftp" :follow
(lambda (path) (browse-url (concat "ftp:" path
))))
1732 ("help" :follow org--open-help-link
)
1733 ("http" :follow
(lambda (path) (browse-url (concat "http:" path
))))
1734 ("https" :follow
(lambda (path) (browse-url (concat "https:" path
))))
1735 ("mailto" :follow
(lambda (path) (browse-url (concat "mailto:" path
))))
1736 ("news" :follow
(lambda (path) (browse-url (concat "news:" path
))))
1737 ("shell" :follow org--open-shell-link
))
1738 "An alist of properties that defines all the links in Org mode.
1739 The key in each association is a string of the link type.
1740 Subsequent optional elements make up a p-list of link properties.
1742 :follow - A function that takes the link path as an argument.
1744 :export - A function that takes the link path, description and
1745 export-backend as arguments.
1747 :store - A function responsible for storing the link. See the
1748 function `org-store-link-functions'.
1750 :complete - A function that inserts a link with completion. The
1751 function takes one optional prefix arg.
1753 :face - A face for the link, or a function that returns a face.
1754 The function takes one argument which is the link path. The
1755 default face is `org-link'.
1757 :mouse-face - The mouse-face. The default is `highlight'.
1759 :display - `full' will not fold the link in descriptive
1760 display. Default is `org-link'.
1762 :help-echo - A string or function that takes (window object position)
1763 as arguments and returns a string.
1765 :keymap - A keymap that is active on the link. The default is
1768 :htmlize-link - A function for the htmlize-link. Defaults
1769 to (list :uri \"type:path\")
1771 :activate-func - A function to run at the end of font-lock
1772 activation. The function must accept (link-start link-end path bracketp)
1775 :type
'(alist :tag
"Link display parameters"
1778 :package-version
'(Org .
"9.1"))
1780 (defun org-link-get-parameter (type key
)
1781 "Get TYPE link property for KEY.
1782 TYPE is a string and KEY is a plist keyword."
1784 (cdr (assoc type org-link-parameters
))
1787 (defun org-link-set-parameters (type &rest parameters
)
1788 "Set link TYPE properties to PARAMETERS.
1789 PARAMETERS should be :key val pairs."
1790 (let ((data (assoc type org-link-parameters
)))
1791 (if data
(setcdr data
(org-combine-plists (cdr data
) parameters
))
1792 (push (cons type parameters
) org-link-parameters
)
1793 (org-make-link-regexps)
1794 (org-element-update-syntax))))
1796 (defun org-link-types ()
1797 "Return a list of known link types."
1798 (mapcar #'car org-link-parameters
))
1800 (defcustom org-link-abbrev-alist nil
1801 "Alist of link abbreviations.
1802 The car of each element is a string, to be replaced at the start of a link.
1803 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1804 links in Org buffers can have an optional tag after a double colon, e.g.,
1806 [[linkkey:tag][description]]
1808 The `linkkey' must be a single word, starting with a letter, followed
1809 by letters, numbers, `-' or `_'.
1811 If REPLACE is a string, the tag will simply be appended to create the link.
1812 If the string contains \"%s\", the tag will be inserted there. If the string
1813 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1814 at that point (see the function `url-hexify-string'). If the string contains
1815 the specifier \"%(my-function)\", then the custom function `my-function' will
1816 be invoked: this function takes the tag as its only argument and must return
1819 REPLACE may also be a function that will be called with the tag as the
1820 only argument to create the link, which should be returned as a string.
1822 See the manual for examples."
1826 (string :tag
"Protocol")
1828 (string :tag
"Format")
1831 (defcustom org-descriptive-links t
1832 "Non-nil means Org will display descriptive links.
1833 E.g. [[https://orgmode.org][Org website]] will be displayed as
1834 \"Org Website\", hiding the link itself and just displaying its
1835 description. When set to nil, Org will display the full links
1838 You can interactively set the value of this variable by calling
1839 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1843 (defcustom org-link-file-path-type
'adaptive
1844 "How the path name in file links should be stored.
1847 relative Relative to the current directory, i.e. the directory of the file
1848 into which the link is being inserted.
1849 absolute Absolute path, if possible with ~ for home directory.
1850 noabbrev Absolute path, no abbreviation of home directory.
1851 adaptive Use relative path for files in the current directory and sub-
1852 directories of it. For other files, use an absolute path."
1860 (defvaralias 'org-activate-links
'org-highlight-links
)
1861 (defcustom org-highlight-links
'(bracket angle plain radio tag date footnote
)
1862 "Types of links that should be highlighted in Org files.
1864 This is a list of symbols, each one of them leading to the
1865 highlighting of a certain link type.
1867 You can still open links that are not highlighted.
1869 In principle, it does not hurt to turn on highlighting for all
1870 link types. There may be a small gain when turning off unused
1871 link types. The types are:
1873 bracket The recommended [[link][description]] or [[link]] links with hiding.
1874 angle Links in angular brackets that may contain whitespace like
1875 <bbdb:Carsten Dominik>.
1876 plain Plain links in normal text, no whitespace, like http://google.com.
1877 radio Text that is matched by a radio target, see manual for details.
1878 tag Tag settings in a headline (link to tag search).
1879 date Time stamps (link to calendar).
1880 footnote Footnote labels.
1882 If you set this variable during an Emacs session, use `org-mode-restart'
1883 in the Org buffer so that the change takes effect."
1885 :group
'org-appearance
1886 :type
'(set :greedy t
1887 (const :tag
"Double bracket links" bracket
)
1888 (const :tag
"Angular bracket links" angle
)
1889 (const :tag
"Plain text links" plain
)
1890 (const :tag
"Radio target matches" radio
)
1891 (const :tag
"Tags" tag
)
1892 (const :tag
"Timestamps" date
)
1893 (const :tag
"Footnotes" footnote
)))
1895 (defcustom org-make-link-description-function nil
1896 "Function to use for generating link descriptions from links.
1897 This function must take two parameters: the first one is the
1898 link, the second one is the description generated by
1899 `org-insert-link'. The function should return the description to
1902 :type
'(choice (const nil
) (function)))
1904 (defgroup org-link-store nil
1905 "Options concerning storing links in Org mode."
1906 :tag
"Org Store Link"
1909 (defcustom org-url-hexify-p t
1910 "When non-nil, hexify URL when creating a link."
1913 :group
'org-link-store
)
1915 (defcustom org-email-link-description-format
"Email %c: %.30s"
1916 "Format of the description part of a link to an email or usenet message.
1917 The following %-escapes will be replaced by corresponding information:
1919 %F full \"From\" field
1920 %f name, taken from \"From\" field, address if no name
1921 %T full \"To\" field
1922 %t first name in \"To\" field, address if no name
1923 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1924 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1929 You may use normal field width specification between the % and the letter.
1930 This is for example useful to limit the length of the subject.
1932 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1933 :group
'org-link-store
1936 (defcustom org-from-is-user-regexp
1938 (when (and user-mail-address
(not (string= user-mail-address
"")))
1939 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1940 (when (and user-full-name
(not (string= user-full-name
"")))
1941 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1942 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1943 "Regexp matched against the \"From:\" header of an email or usenet message.
1944 It should match if the message is from the user him/herself."
1945 :group
'org-link-store
1948 (defcustom org-context-in-file-links t
1949 "Non-nil means file links from `org-store-link' contain context.
1951 A search string will be added to the file name with :: as separator
1952 and used to find the context when the link is activated by the command
1953 `org-open-at-point'. When this option is t, the entire active region
1954 will be placed in the search string of the file link. If set to a
1955 positive integer, only the first n lines of context will be stored.
1957 Using a prefix arg to the command `org-store-link' (`\\[universal-argument] \
1958 \\[org-store-link]')
1959 negates this setting for the duration of the command."
1960 :group
'org-link-store
1961 :type
'(choice boolean integer
))
1963 (defcustom org-keep-stored-link-after-insertion nil
1964 "Non-nil means keep link in list for entire session.
1966 The command `org-store-link' adds a link pointing to the current
1967 location to an internal list. These links accumulate during a session.
1968 The command `org-insert-link' can be used to insert links into any
1969 Org file (offering completion for all stored links).
1971 When this option is nil, every link which has been inserted once using
1972 `\\[org-insert-link]' will be removed from the list, to make completing the \
1974 links more efficient."
1975 :group
'org-link-store
1978 (defgroup org-link-follow nil
1979 "Options concerning following links in Org mode."
1980 :tag
"Org Follow Link"
1983 (defcustom org-link-translation-function nil
1984 "Function to translate links with different syntax to Org syntax.
1985 This can be used to translate links created for example by the Planner
1986 or emacs-wiki packages to Org syntax.
1987 The function must accept two parameters, a TYPE containing the link
1988 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1989 which is everything after the link protocol. It should return a cons
1990 with possibly modified values of type and path.
1991 Org contains a function for this, so if you set this variable to
1992 `org-translate-link-from-planner', you should be able follow many
1993 links created by planner."
1994 :group
'org-link-follow
1995 :type
'(choice (const nil
) (function)))
1997 (defcustom org-follow-link-hook nil
1998 "Hook that is run after a link has been followed."
1999 :group
'org-link-follow
2002 (defcustom org-tab-follows-link nil
2003 "Non-nil means on links TAB will follow the link.
2004 Needs to be set before org.el is loaded.
2005 This really should not be used, it does not make sense, and the
2006 implementation is bad."
2007 :group
'org-link-follow
2010 (defcustom org-return-follows-link nil
2011 "Non-nil means on links RET will follow the link.
2012 In tables, the special behavior of RET has precedence."
2013 :group
'org-link-follow
2016 (defcustom org-mouse-1-follows-link
2017 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
2018 "Non-nil means mouse-1 on a link will follow the link.
2019 A longer mouse click will still set point. Needs to be set
2020 before org.el is loaded."
2021 :group
'org-link-follow
2023 :package-version
'(Org .
"8.3")
2025 (const :tag
"A double click follows the link" double
)
2026 (const :tag
"Unconditionally follow the link with mouse-1" t
)
2027 (integer :tag
"mouse-1 click does not follow the link if longer than N ms" 450)))
2029 (defcustom org-mark-ring-length
4
2030 "Number of different positions to be recorded in the ring.
2031 Changing this requires a restart of Emacs to work correctly."
2032 :group
'org-link-follow
2035 (defcustom org-link-search-must-match-exact-headline
'query-to-create
2036 "Non-nil means internal fuzzy links can only match headlines.
2038 When nil, the a fuzzy link may point to a target or a named
2039 construct in the document. When set to the special value
2040 `query-to-create', offer to create a new headline when none
2043 Spaces and statistics cookies are ignored during heading searches."
2044 :group
'org-link-follow
2047 (const :tag
"Use fuzzy text search" nil
)
2048 (const :tag
"Match only exact headline" t
)
2049 (const :tag
"Match exact headline or query to create it"
2053 (defcustom org-link-frame-setup
2054 '((vm . vm-visit-folder-other-frame
)
2055 (vm-imap . vm-visit-imap-folder-other-frame
)
2056 (gnus . org-gnus-no-new-news
)
2057 (file . find-file-other-window
)
2058 (wl . wl-other-frame
))
2059 "Setup the frame configuration for following links.
2060 When following a link with Emacs, it may often be useful to display
2061 this link in another window or frame. This variable can be used to
2062 set this up for the different types of links.
2065 `vm-visit-folder-other-window'
2066 `vm-visit-folder-other-frame'
2067 For Gnus, use any of
2070 `org-gnus-no-new-news'
2071 For FILE, use any of
2073 `find-file-other-window'
2074 `find-file-other-frame'
2075 For Wanderlust use any of
2078 For the calendar, use the variable `calendar-setup'.
2079 For BBDB, it is currently only possible to display the matches in
2081 :group
'org-link-follow
2085 (const vm-visit-folder
)
2086 (const vm-visit-folder-other-window
)
2087 (const vm-visit-folder-other-frame
)))
2088 (cons (const vm-imap
)
2090 (const vm-visit-imap-folder
)
2091 (const vm-visit-imap-folder-other-window
)
2092 (const vm-visit-imap-folder-other-frame
)))
2096 (const gnus-other-frame
)
2097 (const org-gnus-no-new-news
)))
2101 (const find-file-other-window
)
2102 (const find-file-other-frame
)))
2106 (const wl-other-frame
)))))
2108 (defcustom org-display-internal-link-with-indirect-buffer nil
2109 "Non-nil means use indirect buffer to display infile links.
2110 Activating internal links (from one location in a file to another location
2111 in the same file) normally just jumps to the location. When the link is
2112 activated with a `\\[universal-argument]' prefix (or with mouse-3), the link \
2114 another window. When this option is set, the other window actually displays
2115 an indirect buffer clone of the current buffer, to avoid any visibility
2116 changes to the current buffer."
2117 :group
'org-link-follow
2120 (defcustom org-open-non-existing-files nil
2121 "Non-nil means `org-open-file' will open non-existing files.
2122 When nil, an error will be generated.
2123 This variable applies only to external applications because they
2124 might choke on non-existing files. If the link is to a file that
2125 will be opened in Emacs, the variable is ignored."
2126 :group
'org-link-follow
2129 (defcustom org-open-directory-means-index-dot-org nil
2130 "Non-nil means a link to a directory really means to index.org.
2131 When nil, following a directory link will run dired or open a finder/explorer
2132 window on that directory."
2133 :group
'org-link-follow
2136 (defcustom org-confirm-shell-link-function
'yes-or-no-p
2137 "Non-nil means ask for confirmation before executing shell links.
2138 Shell links can be dangerous: just think about a link
2140 [[shell:rm -rf ~/*][Google Search]]
2142 This link would show up in your Org document as \"Google Search\",
2143 but really it would remove your entire home directory.
2144 Therefore we advise against setting this variable to nil.
2145 Just change it to `y-or-n-p' if you want to confirm with a
2146 single keystroke rather than having to type \"yes\"."
2147 :group
'org-link-follow
2149 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
2150 (const :tag
"with y-or-n (faster)" y-or-n-p
)
2151 (const :tag
"no confirmation (dangerous)" nil
)))
2152 (put 'org-confirm-shell-link-function
2153 'safe-local-variable
2154 (lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
2156 (defcustom org-confirm-shell-link-not-regexp
""
2157 "A regexp to skip confirmation for shell links."
2158 :group
'org-link-follow
2162 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
2163 "Non-nil means ask for confirmation before executing Emacs Lisp links.
2164 Elisp links can be dangerous: just think about a link
2166 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
2168 This link would show up in your Org document as \"Google Search\",
2169 but really it would remove your entire home directory.
2170 Therefore we advise against setting this variable to nil.
2171 Just change it to `y-or-n-p' if you want to confirm with a
2172 single keystroke rather than having to type \"yes\"."
2173 :group
'org-link-follow
2175 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
2176 (const :tag
"with y-or-n (faster)" y-or-n-p
)
2177 (const :tag
"no confirmation (dangerous)" nil
)))
2178 (put 'org-confirm-shell-link-function
2179 'safe-local-variable
2180 (lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
2182 (defcustom org-confirm-elisp-link-not-regexp
""
2183 "A regexp to skip confirmation for Elisp links."
2184 :group
'org-link-follow
2188 (defconst org-file-apps-defaults-gnu
2192 "Default file applications on a UNIX or GNU/Linux system.
2193 See `org-file-apps'.")
2195 (defconst org-file-apps-defaults-macosx
2197 (system .
"open %s")
2199 ("eps.gz" .
"gv %s")
2203 "Default file applications on a macOS system.
2204 The system \"open\" is known as a default, but we use X11 applications
2205 for some files for which the OS does not have a good default.
2206 See `org-file-apps'.")
2208 (defconst org-file-apps-defaults-windowsnt
2209 (list '(remote . emacs
)
2210 (cons 'system
(lambda (file _path
)
2211 (with-no-warnings (w32-shell-execute "open" file
))))
2212 (cons t
(lambda (file _path
)
2213 (with-no-warnings (w32-shell-execute "open" file
)))))
2214 "Default file applications on a Windows NT system.
2215 The system \"open\" is used for most files.
2216 See `org-file-apps'.")
2218 (defcustom org-file-apps
2219 '((auto-mode . emacs
)
2220 ("\\.mm\\'" . default
)
2221 ("\\.x?html?\\'" . default
)
2222 ("\\.pdf\\'" . default
))
2223 "External applications for opening `file:path' items in a document.
2226 Org mode uses system defaults for different file types, but
2227 you can use this variable to set the application for a given file
2228 extension. The entries in this list are cons cells where the car identifies
2229 files and the cdr the corresponding command.
2231 Possible values for the file identifier are:
2233 \"string\" A string as a file identifier can be interpreted in different
2234 ways, depending on its contents:
2236 - Alphanumeric characters only:
2237 Match links with this file extension.
2238 Example: (\"pdf\" . \"evince %s\")
2239 to open PDFs with evince.
2241 - Regular expression: Match links where the
2242 filename matches the regexp. If you want to
2243 use groups here, use shy groups.
2245 Example: (\"\\\\.x?html\\\\\\='\" . \"firefox %s\")
2246 (\"\\\\(?:xhtml\\\\|html\\\\)\\\\\\='\" . \"firefox %s\")
2247 to open *.html and *.xhtml with firefox.
2249 - Regular expression which contains (non-shy) groups:
2250 Match links where the whole link, including \"::\", and
2251 anything after that, matches the regexp.
2252 In a custom command string, %1, %2, etc. are replaced with
2253 the parts of the link that were matched by the groups.
2254 For backwards compatibility, if a command string is given
2255 that does not use any of the group matches, this case is
2256 handled identically to the second one (i.e. match against
2258 In a custom function, you can access the group matches with
2259 (match-string n link).
2261 Example: (\"\\\\.pdf::\\\\(\\\\d+\\\\)\\\\\\='\" . \
2262 \"evince -p %1 %s\")
2263 to open [[file:document.pdf::5]] with evince at page 5.
2265 `directory' Matches a directory
2266 `remote' Matches a remote file, accessible through tramp or efs.
2267 Remote files most likely should be visited through Emacs
2268 because external applications cannot handle such paths.
2269 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
2270 so all files Emacs knows how to handle. Using this with
2271 command `emacs' will open most files in Emacs. Beware that this
2272 will also open html files inside Emacs, unless you add
2273 (\"html\" . default) to the list as well.
2274 `system' The system command to open files, like `open' on Windows
2275 and macOS, and mailcap under GNU/Linux. This is the command
2276 that will be selected if you call `org-open-at-point' with a
2277 double prefix argument (`\\[universal-argument] \
2278 \\[universal-argument] \\[org-open-at-point]').
2279 t Default for files not matched by any of the other options.
2281 Possible values for the command are:
2283 `emacs' The file will be visited by the current Emacs process.
2284 `default' Use the default application for this file type, which is the
2285 association for t in the list, most likely in the system-specific
2286 part. This can be used to overrule an unwanted setting in the
2287 system-specific variable.
2288 `system' Use the system command for opening files, like \"open\".
2289 This command is specified by the entry whose car is `system'.
2290 Most likely, the system-specific version of this variable
2291 does define this command, but you can overrule/replace it
2293 `mailcap' Use command specified in the mailcaps.
2294 string A command to be executed by a shell; %s will be replaced
2295 by the path to the file.
2296 function A Lisp function, which will be called with two arguments:
2297 the file path and the original link string, without the
2300 For more examples, see the system specific constants
2301 `org-file-apps-defaults-macosx'
2302 `org-file-apps-defaults-windowsnt'
2303 `org-file-apps-defaults-gnu'."
2304 :group
'org-link-follow
2306 (cons (choice :value
""
2307 (string :tag
"Extension")
2308 (const :tag
"System command to open files" system
)
2309 (const :tag
"Default for unrecognized files" t
)
2310 (const :tag
"Remote file" remote
)
2311 (const :tag
"Links to a directory" directory
)
2312 (const :tag
"Any files that have Emacs modes"
2315 (const :tag
"Visit with Emacs" emacs
)
2316 (const :tag
"Use default" default
)
2317 (const :tag
"Use the system command" system
)
2318 (string :tag
"Command")
2319 (function :tag
"Function")))))
2321 (defcustom org-doi-server-url
"http://dx.doi.org/"
2322 "The URL of the DOI server."
2325 :group
'org-link-follow
)
2327 (defgroup org-refile nil
2328 "Options concerning refiling entries in Org mode."
2332 (defcustom org-directory
"~/org"
2333 "Directory with Org files.
2334 This is just a default location to look for Org files. There is no need
2335 at all to put your files into this directory. It is used in the
2336 following situations:
2338 1. When a capture template specifies a target file that is not an
2339 absolute path. The path will then be interpreted relative to
2341 2. When the value of variable `org-agenda-files' is a single file, any
2342 relative paths in this file will be taken as relative to
2348 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
2349 "Default target for storing notes.
2350 Used as a fall back file for org-capture.el, for templates that
2351 do not specify a target file."
2356 (defcustom org-reverse-note-order nil
2357 "Non-nil means store new notes at the beginning of a file or entry.
2358 When nil, new notes will be filed to the end of a file or entry.
2359 This can also be a list with cons cells of regular expressions that
2360 are matched against file names, and values."
2364 (const :tag
"Reverse always" t
)
2365 (const :tag
"Reverse never" nil
)
2366 (repeat :tag
"By file name regexp"
2367 (cons regexp boolean
))))
2369 (defcustom org-log-refile nil
2370 "Information to record when a task is refiled.
2372 Possible values are:
2374 nil Don't add anything
2375 time Add a time stamp to the task
2376 note Prompt for a note and add it with template `org-log-note-headings'
2378 This option can also be set with on a per-file-basis with
2380 #+STARTUP: nologrefile
2381 #+STARTUP: logrefile
2382 #+STARTUP: lognoterefile
2384 You can have local logging settings for a subtree by setting the LOGGING
2385 property to one or more of these keywords.
2387 When bulk-refiling from the agenda, the value `note' is forbidden and
2388 will temporarily be changed to `time'."
2390 :group
'org-progress
2393 (const :tag
"No logging" nil
)
2394 (const :tag
"Record timestamp" time
)
2395 (const :tag
"Record timestamp with note." note
)))
2397 (defcustom org-refile-targets nil
2398 "Targets for refiling entries with `\\[org-refile]'.
2399 This is a list of cons cells. Each cell contains:
2400 - a specification of the files to be considered, either a list of files,
2401 or a symbol whose function or variable value will be used to retrieve
2402 a file name or a list of file names. If you use `org-agenda-files' for
2403 that, all agenda files will be scanned for targets. Nil means consider
2404 headings in the current buffer.
2405 - A specification of how to find candidate refile targets. This may be
2407 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
2408 This tag has to be present in all target headlines, inheritance will
2410 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
2412 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
2413 headlines that are refiling targets.
2414 - a cons cell (:level . N). Any headline of level N is considered a target.
2415 Note that, when `org-odd-levels-only' is set, level corresponds to
2416 order in hierarchy, not to the number of stars.
2417 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
2418 Note that, when `org-odd-levels-only' is set, level corresponds to
2419 order in hierarchy, not to the number of stars.
2421 Each element of this list generates a set of possible targets.
2422 The union of these sets is presented (with completion) to
2423 the user by `org-refile'.
2425 You can set the variable `org-refile-target-verify-function' to a function
2426 to verify each headline found by the simple criteria above.
2428 When this variable is nil, all top-level headlines in the current buffer
2429 are used, equivalent to the value `((nil . (:level . 1))'."
2433 (choice :value org-agenda-files
2434 (const :tag
"All agenda files" org-agenda-files
)
2435 (const :tag
"Current buffer" nil
)
2436 (function) (variable) (file))
2437 (choice :tag
"Identify target headline by"
2438 (cons :tag
"Specific tag" (const :value
:tag
) (string))
2439 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
2440 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
2441 (cons :tag
"Level number" (const :value
:level
) (integer))
2442 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
2444 (defcustom org-refile-target-verify-function nil
2445 "Function to verify if the headline at point should be a refile target.
2446 The function will be called without arguments, with point at the
2447 beginning of the headline. It should return t and leave point
2448 where it is if the headline is a valid target for refiling.
2450 If the target should not be selected, the function must return nil.
2451 In addition to this, it may move point to a place from where the search
2452 should be continued. For example, the function may decide that the entire
2453 subtree of the current entry should be excluded and move point to the end
2460 (defcustom org-refile-use-cache nil
2461 "Non-nil means cache refile targets to speed up the process.
2463 The cache for a particular file will be updated automatically when
2464 the buffer has been killed, or when any of the marker used for flagging
2465 refile targets no longer points at a live buffer.
2466 If you have added new entries to a buffer that might themselves be targets,
2467 you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
2468 if you find that easier, \
2469 `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
2475 (defcustom org-refile-use-outline-path nil
2476 "Non-nil means provide refile targets as paths.
2477 So a level 3 headline will be available as level1/level2/level3.
2479 When the value is `file', also include the file name (without directory)
2480 into the path. In this case, you can also stop the completion after
2481 the file name, to get entries inserted as top level in the file.
2483 When `full-file-path', include the full file path.
2485 When `buffer-name', use the buffer name."
2488 (const :tag
"Not" nil
)
2489 (const :tag
"Yes" t
)
2490 (const :tag
"Start with file name" file
)
2491 (const :tag
"Start with full file path" full-file-path
)
2492 (const :tag
"Start with buffer name" buffer-name
)))
2494 (defcustom org-outline-path-complete-in-steps t
2495 "Non-nil means complete the outline path in hierarchical steps.
2496 When Org uses the refile interface to select an outline path (see
2497 `org-refile-use-outline-path'), the completion of the path can be
2498 done in a single go, or it can be done in steps down the headline
2499 hierarchy. Going in steps is probably the best if you do not use
2500 a special completion package like `ido' or `icicles'. However,
2501 when using these packages, going in one step can be very fast,
2502 while still showing the whole path to the entry."
2506 (defcustom org-refile-allow-creating-parent-nodes nil
2507 "Non-nil means allow the creation of new nodes as refile targets.
2508 New nodes are then created by adding \"/new node name\" to the completion
2509 of an existing node. When the value of this variable is `confirm',
2510 new node creation must be confirmed by the user (recommended).
2511 When nil, the completion must match an existing entry.
2513 Note that, if the new heading is not seen by the criteria
2514 listed in `org-refile-targets', multiple instances of the same
2515 heading would be created by trying again to file under the new
2519 (const :tag
"Never" nil
)
2520 (const :tag
"Always" t
)
2521 (const :tag
"Prompt for confirmation" confirm
)))
2523 (defcustom org-refile-active-region-within-subtree nil
2524 "Non-nil means also refile active region within a subtree.
2526 By default `org-refile' doesn't allow refiling regions if they
2527 don't contain a set of subtrees, but it might be convenient to
2528 do so sometimes: in that case, the first line of the region is
2529 converted to a headline before refiling."
2534 (defgroup org-todo nil
2535 "Options concerning TODO items in Org mode."
2539 (defgroup org-progress nil
2540 "Options concerning Progress logging in Org mode."
2544 (defvar org-todo-interpretation-widgets
2545 '((:tag
"Sequence (cycling hits every state)" sequence
)
2546 (:tag
"Type (cycling directly to DONE)" type
))
2547 "The available interpretation symbols for customizing `org-todo-keywords'.
2548 Interested libraries should add to this list.")
2550 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2551 "List of TODO entry keyword sequences and their interpretation.
2552 \\<org-mode-map>This is a list of sequences.
2554 Each sequence starts with a symbol, either `sequence' or `type',
2555 indicating if the keywords should be interpreted as a sequence of
2556 action steps, or as different types of TODO items. The first
2557 keywords are states requiring action - these states will select a headline
2558 for inclusion into the global TODO list Org produces. If one of the
2559 \"keywords\" is the vertical bar, \"|\", the remaining keywords
2560 signify that no further action is necessary. If \"|\" is not found,
2561 the last keyword is treated as the only DONE state of the sequence.
2563 The command `\\[org-todo]' cycles an entry through these states, and one
2564 additional state where no keyword is present. For details about this
2565 cycling, see the manual.
2567 TODO keywords and interpretation can also be set on a per-file basis with
2568 the special #+SEQ_TODO and #+TYP_TODO lines.
2570 Each keyword can optionally specify a character for fast state selection
2571 \(in combination with the variable `org-use-fast-todo-selection')
2572 and specifiers for state change logging, using the same syntax that
2573 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2574 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2575 indicates to record a time stamp each time this state is selected.
2577 Each keyword may also specify if a timestamp or a note should be
2578 recorded when entering or leaving the state, by adding additional
2579 characters in the parenthesis after the keyword. This looks like this:
2580 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2581 record only the time of the state change. With X and Y being either
2582 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2583 Y when leaving the state if and only if the *target* state does not
2584 define X. You may omit any of the fast-selection key or X or /Y,
2585 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2587 For backward compatibility, this variable may also be just a list
2588 of keywords. In this case the interpretation (sequence or type) will be
2589 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2591 :group
'org-keywords
2593 (repeat :tag
"Old syntax, just keywords"
2594 (string :tag
"Keyword"))
2595 (repeat :tag
"New syntax"
2598 :tag
"Interpretation"
2599 ;;Quick and dirty way to see
2600 ;;`org-todo-interpretations'. This takes the
2601 ;;place of item arguments
2609 org-todo-interpretation-widgets
))
2612 (string :tag
"Keyword"))))))
2614 (defvar-local org-todo-keywords-1 nil
2615 "All TODO and DONE keywords active in a buffer.")
2616 (defvar org-todo-keywords-for-agenda nil
)
2617 (defvar org-done-keywords-for-agenda nil
)
2618 (defvar org-todo-keyword-alist-for-agenda nil
)
2619 (defvar org-tag-alist-for-agenda nil
2620 "Alist of all tags from all agenda files.")
2621 (defvar org-tag-groups-alist-for-agenda nil
2622 "Alist of all groups tags from all current agenda files.")
2623 (defvar-local org-tag-groups-alist nil
)
2624 (defvar org-agenda-contributing-files nil
)
2625 (defvar-local org-current-tag-alist nil
2626 "Alist of all tag groups in current buffer.
2627 This variable takes into consideration `org-tag-alist',
2628 `org-tag-persistent-alist' and TAGS keywords in the buffer.")
2629 (defvar-local org-not-done-keywords nil
)
2630 (defvar-local org-done-keywords nil
)
2631 (defvar-local org-todo-heads nil
)
2632 (defvar-local org-todo-sets nil
)
2633 (defvar-local org-todo-log-states nil
)
2634 (defvar-local org-todo-kwd-alist nil
)
2635 (defvar-local org-todo-key-alist nil
)
2636 (defvar-local org-todo-key-trigger nil
)
2638 (defcustom org-todo-interpretation
'sequence
2639 "Controls how TODO keywords are interpreted.
2640 This variable is in principle obsolete and is only used for
2641 backward compatibility, if the interpretation of todo keywords is
2642 not given already in `org-todo-keywords'. See that variable for
2645 :group
'org-keywords
2646 :type
'(choice (const sequence
)
2649 (defcustom org-use-fast-todo-selection t
2651 Non-nil means use the fast todo selection scheme with `\\[org-todo]'.
2652 This variable describes if and under what circumstances the cycling
2653 mechanism for TODO keywords will be replaced by a single-key, direct
2656 When nil, fast selection is never used.
2658 When the symbol `prefix', it will be used when `org-todo' is called
2659 with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
2660 in an Org buffer, and
2661 `\\[universal-argument] t' in an agenda buffer.
2663 When t, fast selection is used by default. In this case, the prefix
2664 argument forces cycling instead.
2666 In all cases, the special interface is only used if access keys have
2667 actually been assigned by the user, i.e. if keywords in the configuration
2668 are followed by a letter in parenthesis, like TODO(t)."
2671 (const :tag
"Never" nil
)
2672 (const :tag
"By default" t
)
2673 (const :tag
"Only with C-u C-c C-t" prefix
)))
2675 (defcustom org-provide-todo-statistics t
2676 "Non-nil means update todo statistics after insert and toggle.
2677 ALL-HEADLINES means update todo statistics by including headlines
2678 with no TODO keyword as well, counting them as not done.
2679 A list of TODO keywords means the same, but skip keywords that are
2681 When set to a list of two lists, the first list contains keywords
2682 to consider as TODO keywords, the second list contains keywords
2683 to consider as DONE keywords.
2685 When this is set, todo statistics is updated in the parent of the
2686 current entry each time a todo state is changed."
2689 (const :tag
"Yes, only for TODO entries" t
)
2690 (const :tag
"Yes, including all entries" all-headlines
)
2691 (repeat :tag
"Yes, for TODOs in this list"
2692 (string :tag
"TODO keyword"))
2693 (list :tag
"Yes, for TODOs and DONEs in these lists"
2694 (repeat (string :tag
"TODO keyword"))
2695 (repeat (string :tag
"DONE keyword")))
2696 (other :tag
"No TODO statistics" nil
)))
2698 (defcustom org-hierarchical-todo-statistics t
2699 "Non-nil means TODO statistics covers just direct children.
2700 When nil, all entries in the subtree are considered.
2701 This has only an effect if `org-provide-todo-statistics' is set.
2702 To set this to nil for only a single subtree, use a COOKIE_DATA
2703 property and include the word \"recursive\" into the value."
2707 (defcustom org-after-todo-state-change-hook nil
2708 "Hook which is run after the state of a TODO item was changed.
2709 The new state (a string with a TODO keyword, or nil) is available in the
2710 Lisp variable `org-state'."
2714 (defvar org-blocker-hook nil
2715 "Hook for functions that are allowed to block a state change.
2717 Functions in this hook should not modify the buffer.
2718 Each function gets as its single argument a property list,
2719 see `org-trigger-hook' for more information about this list.
2721 If any of the functions in this hook returns nil, the state change
2724 (defvar org-trigger-hook nil
2725 "Hook for functions that are triggered by a state change.
2727 Each function gets as its single argument a property list with at
2728 least the following elements:
2730 (:type type-of-change :position pos-at-entry-start
2731 :from old-state :to new-state)
2733 Depending on the type, more properties may be present.
2735 This mechanism is currently implemented for:
2739 :type todo-state-change
2740 :from previous state (keyword as a string), or nil, or a symbol
2741 `todo' or `done', to indicate the general type of state.
2742 :to new state, like in :from")
2744 (defcustom org-enforce-todo-dependencies nil
2745 "Non-nil means undone TODO entries will block switching the parent to DONE.
2746 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2747 be blocked if any prior sibling is not yet done.
2748 Finally, if the parent is blocked because of ordered siblings of its own,
2749 the child will also be blocked."
2750 :set
(lambda (var val
)
2753 (add-hook 'org-blocker-hook
2754 'org-block-todo-from-children-or-siblings-or-parent
)
2755 (remove-hook 'org-blocker-hook
2756 'org-block-todo-from-children-or-siblings-or-parent
)))
2760 (defcustom org-enforce-todo-checkbox-dependencies nil
2761 "Non-nil means unchecked boxes will block switching the parent to DONE.
2762 When this is nil, checkboxes have no influence on switching TODO states.
2763 When non-nil, you first need to check off all check boxes before the TODO
2764 entry can be switched to DONE.
2765 This variable needs to be set before org.el is loaded, and you need to
2766 restart Emacs after a change to make the change effective. The only way
2767 to change is while Emacs is running is through the customize interface."
2768 :set
(lambda (var val
)
2771 (add-hook 'org-blocker-hook
2772 'org-block-todo-from-checkboxes
)
2773 (remove-hook 'org-blocker-hook
2774 'org-block-todo-from-checkboxes
)))
2778 (defcustom org-treat-insert-todo-heading-as-state-change nil
2779 "Non-nil means inserting a TODO heading is treated as state change.
2780 So when the command `\\[org-insert-todo-heading]' is used, state change
2781 logging will apply if appropriate. When nil, the new TODO item will
2782 be inserted directly, and no logging will take place."
2786 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2787 "Non-nil means switching TODO states with S-cursor counts as state change.
2788 This is the default behavior. However, setting this to nil allows a
2789 convenient way to select a TODO state and bypass any logging associated
2794 (defcustom org-todo-state-tags-triggers nil
2795 "Tag changes that should be triggered by TODO state changes.
2796 This is a list. Each entry is
2798 (state-change (tag . flag) .......)
2800 State-change can be a string with a state, and empty string to indicate the
2801 state that has no TODO keyword, or it can be one of the symbols `todo'
2802 or `done', meaning any not-done or done state, respectively."
2806 (cons (choice :tag
"When changing to"
2807 (const :tag
"Not-done state" todo
)
2808 (const :tag
"Done state" done
)
2809 (string :tag
"State"))
2811 (cons :tag
"Tag action"
2813 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2815 (defcustom org-log-done nil
2816 "Information to record when a task moves to the DONE state.
2818 Possible values are:
2820 nil Don't add anything, just change the keyword
2821 time Add a time stamp to the task
2822 note Prompt for a note and add it with template `org-log-note-headings'
2824 This option can also be set with on a per-file-basis with
2826 #+STARTUP: nologdone
2828 #+STARTUP: lognotedone
2830 You can have local logging settings for a subtree by setting the LOGGING
2831 property to one or more of these keywords."
2833 :group
'org-progress
2835 (const :tag
"No logging" nil
)
2836 (const :tag
"Record CLOSED timestamp" time
)
2837 (const :tag
"Record CLOSED timestamp with note." note
)))
2839 ;; Normalize old uses of org-log-done.
2841 ((eq org-log-done t
) (setq org-log-done
'time
))
2842 ((and (listp org-log-done
) (memq 'done org-log-done
))
2843 (setq org-log-done
'note
)))
2845 (defcustom org-log-reschedule nil
2846 "Information to record when the scheduling date of a tasks is modified.
2848 Possible values are:
2850 nil Don't add anything, just change the date
2851 time Add a time stamp to the task
2852 note Prompt for a note and add it with template `org-log-note-headings'
2854 This option can also be set with on a per-file-basis with
2856 #+STARTUP: nologreschedule
2857 #+STARTUP: logreschedule
2858 #+STARTUP: lognotereschedule"
2860 :group
'org-progress
2862 (const :tag
"No logging" nil
)
2863 (const :tag
"Record timestamp" time
)
2864 (const :tag
"Record timestamp with note." note
)))
2866 (defcustom org-log-redeadline nil
2867 "Information to record when the deadline date of a tasks is modified.
2869 Possible values are:
2871 nil Don't add anything, just change the date
2872 time Add a time stamp to the task
2873 note Prompt for a note and add it with template `org-log-note-headings'
2875 This option can also be set with on a per-file-basis with
2877 #+STARTUP: nologredeadline
2878 #+STARTUP: logredeadline
2879 #+STARTUP: lognoteredeadline
2881 You can have local logging settings for a subtree by setting the LOGGING
2882 property to one or more of these keywords."
2884 :group
'org-progress
2886 (const :tag
"No logging" nil
)
2887 (const :tag
"Record timestamp" time
)
2888 (const :tag
"Record timestamp with note." note
)))
2890 (defcustom org-log-note-clock-out nil
2891 "Non-nil means record a note when clocking out of an item.
2892 This can also be configured on a per-file basis by adding one of
2893 the following lines anywhere in the buffer:
2895 #+STARTUP: lognoteclock-out
2896 #+STARTUP: nolognoteclock-out"
2898 :group
'org-progress
2901 (defcustom org-log-done-with-time t
2902 "Non-nil means the CLOSED time stamp will contain date and time.
2903 When nil, only the date will be recorded."
2904 :group
'org-progress
2907 (defcustom org-log-note-headings
2908 '((done .
"CLOSING NOTE %t")
2909 (state .
"State %-12s from %-12S %t")
2910 (note .
"Note taken on %t")
2911 (reschedule .
"Rescheduled from %S on %t")
2912 (delschedule .
"Not scheduled, was %S on %t")
2913 (redeadline .
"New deadline from %S on %t")
2914 (deldeadline .
"Removed deadline, was %S on %t")
2915 (refile .
"Refiled on %t")
2917 "Headings for notes added to entries.
2919 The value is an alist, with the car being a symbol indicating the
2920 note context, and the cdr is the heading to be used. The heading
2921 may also be the empty string. The following placeholders can be
2925 %T an active time stamp instead the default inactive one
2926 %d a short-format time stamp.
2927 %D an active short-format time stamp.
2928 %s the new TODO state or time stamp (inactive), in double quotes.
2929 %S the old TODO state or time stamp (inactive), in double quotes.
2933 In fact, it is not a good idea to change the `state' entry,
2934 because Agenda Log mode depends on the format of these entries."
2936 :group
'org-progress
2937 :type
'(list :greedy t
2938 (cons (const :tag
"Heading when closing an item" done
) string
)
2940 "Heading when changing todo state (todo sequence only)"
2942 (cons (const :tag
"Heading when just taking a note" note
) string
)
2943 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2944 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2945 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2946 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2947 (cons (const :tag
"Heading when refiling" refile
) string
)
2948 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
2950 (unless (assq 'note org-log-note-headings
)
2951 (push '(note .
"%t") org-log-note-headings
))
2953 (defcustom org-log-into-drawer nil
2954 "Non-nil means insert state change notes and time stamps into a drawer.
2955 When nil, state changes notes will be inserted after the headline and
2956 any scheduling and clock lines, but not inside a drawer.
2958 The value of this variable should be the name of the drawer to use.
2959 LOGBOOK is proposed as the default drawer for this purpose, you can
2960 also set this to a string to define the drawer of your choice.
2962 A value of t is also allowed, representing \"LOGBOOK\".
2964 A value of t or nil can also be set with on a per-file-basis with
2966 #+STARTUP: logdrawer
2967 #+STARTUP: nologdrawer
2969 If this variable is set, `org-log-state-notes-insert-after-drawers'
2972 You can set the property LOG_INTO_DRAWER to overrule this setting for
2975 Do not check directly this variable in a Lisp program. Call
2976 function `org-log-into-drawer' instead."
2978 :group
'org-progress
2980 (const :tag
"Not into a drawer" nil
)
2981 (const :tag
"LOGBOOK" t
)
2982 (string :tag
"Other")))
2984 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
)
2986 (defun org-log-into-drawer ()
2987 "Name of the log drawer, as a string, or nil.
2988 This is the value of `org-log-into-drawer'. However, if the
2989 current entry has or inherits a LOG_INTO_DRAWER property, it will
2990 be used instead of the default value."
2991 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit t
)))
2992 (cond ((equal p
"nil") nil
)
2993 ((equal p
"t") "LOGBOOK")
2996 ((stringp org-log-into-drawer
) org-log-into-drawer
)
2997 (org-log-into-drawer "LOGBOOK"))))
2999 (defcustom org-log-state-notes-insert-after-drawers nil
3000 "Non-nil means insert state change notes after any drawers in entry.
3001 Only the drawers that *immediately* follow the headline and the
3002 deadline/scheduled line are skipped.
3003 When nil, insert notes right after the heading and perhaps the line
3004 with deadline/scheduling if present.
3006 This variable will have no effect if `org-log-into-drawer' is
3009 :group
'org-progress
3012 (defcustom org-log-states-order-reversed t
3013 "Non-nil means the latest state note will be directly after heading.
3014 When nil, the state change notes will be ordered according to time.
3016 This option can also be set with on a per-file-basis with
3018 #+STARTUP: logstatesreversed
3019 #+STARTUP: nologstatesreversed"
3021 :group
'org-progress
3024 (defcustom org-todo-repeat-to-state nil
3025 "The TODO state to which a repeater should return the repeating task.
3026 By default this is the first task of a TODO sequence or the
3027 previous state of a TYPE_TODO set. But you can specify to use
3028 the previous state in a TODO sequence or a string.
3030 Alternatively, you can set the :REPEAT_TO_STATE: property of the
3031 entry, which has precedence over this option."
3034 :type
'(choice (const :tag
"Use the previous TODO state" t
)
3035 (const :tag
"Use the head of the TODO sequence" nil
)
3036 (string :tag
"Use a specific TODO state")))
3038 (defcustom org-log-repeat
'time
3039 "Non-nil means record moving through the DONE state when triggering repeat.
3040 An auto-repeating task is immediately switched back to TODO when
3041 marked DONE. If you are not logging state changes (by adding \"@\"
3042 or \"!\" to the TODO keyword definition), or set `org-log-done' to
3043 record a closing note, there will be no record of the task moving
3044 through DONE. This variable forces taking a note anyway.
3046 nil Don't force a record
3047 time Record a time stamp
3048 note Prompt for a note and add it with template `org-log-note-headings'
3050 This option can also be set with on a per-file-basis with
3052 #+STARTUP: nologrepeat
3053 #+STARTUP: logrepeat
3054 #+STARTUP: lognoterepeat
3056 You can have local logging settings for a subtree by setting the LOGGING
3057 property to one or more of these keywords."
3059 :group
'org-progress
3061 (const :tag
"Don't force a record" nil
)
3062 (const :tag
"Force recording the DONE state" time
)
3063 (const :tag
"Force recording a note with the DONE state" note
)))
3066 (defgroup org-priorities nil
3067 "Priorities in Org mode."
3068 :tag
"Org Priorities"
3071 (defcustom org-enable-priority-commands t
3072 "Non-nil means priority commands are active.
3073 When nil, these commands will be disabled, so that you never accidentally
3075 :group
'org-priorities
3078 (defcustom org-highest-priority ?A
3079 "The highest priority of TODO items. A character like ?A, ?B etc.
3080 Must have a smaller ASCII number than `org-lowest-priority'."
3081 :group
'org-priorities
3084 (defcustom org-lowest-priority ?C
3085 "The lowest priority of TODO items. A character like ?A, ?B etc.
3086 Must have a larger ASCII number than `org-highest-priority'."
3087 :group
'org-priorities
3090 (defcustom org-default-priority ?B
3091 "The default priority of TODO items.
3092 This is the priority an item gets if no explicit priority is given.
3093 When starting to cycle on an empty priority the first step in the cycle
3094 depends on `org-priority-start-cycle-with-default'. The resulting first
3095 step priority must not exceed the range from `org-highest-priority' to
3096 `org-lowest-priority' which means that `org-default-priority' has to be
3097 in this range exclusive or inclusive the range boundaries. Else the
3098 first step refuses to set the default and the second will fall back
3099 to (depending on the command used) the highest or lowest priority."
3100 :group
'org-priorities
3103 (defcustom org-priority-start-cycle-with-default t
3104 "Non-nil means start with default priority when starting to cycle.
3105 When this is nil, the first step in the cycle will be (depending on the
3106 command used) one higher or lower than the default priority.
3107 See also `org-default-priority'."
3108 :group
'org-priorities
3111 (defcustom org-get-priority-function nil
3112 "Function to extract the priority from a string.
3113 The string is normally the headline. If this is nil Org computes the
3114 priority from the priority cookie like [#A] in the headline. It returns
3115 an integer, increasing by 1000 for each priority level.
3116 The user can set a different function here, which should take a string
3117 as an argument and return the numeric priority."
3118 :group
'org-priorities
3124 (defgroup org-time nil
3125 "Options concerning time stamps and deadlines in Org mode."
3129 (defcustom org-time-stamp-rounding-minutes
'(0 5)
3130 "Number of minutes to round time stamps to.
3132 These are two values, the first applies when first creating a time stamp.
3133 The second applies when changing it with the commands `S-up' and `S-down'.
3134 When changing the time stamp, this means that it will change in steps
3135 of N minutes, as given by the second value.
3137 When a setting is 0 or 1, insert the time unmodified. Useful rounding
3138 numbers should be factors of 60, so for example 5, 10, 15.
3140 When this is larger than 1, you can still force an exact time stamp by using
3141 a double prefix argument to a time stamp command like \
3142 `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
3143 and by using a prefix arg to `S-up/down' to specify the exact number
3144 of minutes to shift."
3146 :get
(lambda (var) ; Make sure both elements are there
3147 (if (integerp (default-value var
))
3148 (list (default-value var
) 5)
3149 (default-value var
)))
3151 (integer :tag
"when inserting times")
3152 (integer :tag
"when modifying times")))
3154 ;; Normalize old customizations of this variable.
3155 (when (integerp org-time-stamp-rounding-minutes
)
3156 (setq org-time-stamp-rounding-minutes
3157 (list org-time-stamp-rounding-minutes
3158 org-time-stamp-rounding-minutes
)))
3160 (defcustom org-display-custom-times nil
3161 "Non-nil means overlay custom formats over all time stamps.
3162 The formats are defined through the variable `org-time-stamp-custom-formats'.
3163 To turn this on on a per-file basis, insert anywhere in the file:
3164 #+STARTUP: customtime"
3168 (make-variable-buffer-local 'org-display-custom-times
)
3170 (defcustom org-time-stamp-custom-formats
3171 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
3172 "Custom formats for time stamps. See `format-time-string' for the syntax.
3173 These are overlaid over the default ISO format if the variable
3174 `org-display-custom-times' is set. Time like %H:%M should be at the
3175 end of the second format. The custom formats are also honored by export
3176 commands, if custom time display is turned on at the time of export."
3180 (defun org-time-stamp-format (&optional long inactive
)
3181 "Get the right format for a time string."
3182 (let ((f (if long
(cdr org-time-stamp-formats
)
3183 (car org-time-stamp-formats
))))
3185 (concat "[" (substring f
1 -
1) "]")
3188 (defcustom org-deadline-warning-days
14
3189 "Number of days before expiration during which a deadline becomes active.
3190 This variable governs the display in sparse trees and in the agenda.
3191 When 0 or negative, it means use this number (the absolute value of it)
3192 even if a deadline has a different individual lead time specified.
3194 Custom commands can set this variable in the options section."
3196 :group
'org-agenda-daily
/weekly
3199 (defcustom org-scheduled-delay-days
0
3200 "Number of days before a scheduled item becomes active.
3201 This variable governs the display in sparse trees and in the agenda.
3202 The default value (i.e. 0) means: don't delay scheduled item.
3203 When negative, it means use this number (the absolute value of it)
3204 even if a scheduled item has a different individual delay time
3207 Custom commands can set this variable in the options section."
3209 :group
'org-agenda-daily
/weekly
3211 :package-version
'(Org .
"8.0")
3214 (defcustom org-read-date-prefer-future t
3215 "Non-nil means assume future for incomplete date input from user.
3216 This affects the following situations:
3217 1. The user gives a month but not a year.
3218 For example, if it is April and you enter \"feb 2\", this will be read
3219 as Feb 2, *next* year. \"May 5\", however, will be this year.
3220 2. The user gives a day, but no month.
3221 For example, if today is the 15th, and you enter \"3\", Org will read
3222 this as the third of *next* month. However, if you enter \"17\",
3223 it will be considered as *this* month.
3225 If you set this variable to the symbol `time', then also the following
3228 3. If the user gives a time.
3229 If the time is before now, it will be interpreted as tomorrow.
3231 Currently none of this works for ISO week specifications.
3233 When this option is nil, the current day, month and year will always be
3236 See also `org-agenda-jump-prefer-future'."
3239 (const :tag
"Never" nil
)
3240 (const :tag
"Check month and day" t
)
3241 (const :tag
"Check month, day, and time" time
)))
3243 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
3244 "Should the agenda jump command prefer the future for incomplete dates?
3245 The default is to do the same as configured in `org-read-date-prefer-future'.
3246 But you can also set a deviating value here.
3247 This may t or nil, or the symbol `org-read-date-prefer-future'."
3252 (const :tag
"Use org-read-date-prefer-future"
3253 org-read-date-prefer-future
)
3254 (const :tag
"Never" nil
)
3255 (const :tag
"Always" t
)))
3257 (defcustom org-read-date-force-compatible-dates t
3258 "Should date/time prompt force dates that are guaranteed to work in Emacs?
3260 Depending on the system Emacs is running on, certain dates cannot
3261 be represented with the type used internally to represent time.
3262 Dates between 1970-1-1 and 2038-1-1 can always be represented
3263 correctly. Some systems allow for earlier dates, some for later,
3264 some for both. One way to find out it to insert any date into an
3265 Org buffer, putting the cursor on the year and hitting S-up and
3266 S-down to test the range.
3268 When this variable is set to t, the date/time prompt will not let
3269 you specify dates outside the 1970-2037 range, so it is certain that
3270 these dates will work in whatever version of Emacs you are
3271 running, and also that you can move a file from one Emacs implementation
3272 to another. WHenever Org is forcing the year for you, it will display
3275 When this variable is nil, Org will check if the date is
3276 representable in the specific Emacs implementation you are using.
3277 If not, it will force a year, usually the current year, and beep
3278 to remind you. Currently this setting is not recommended because
3279 the likelihood that you will open your Org files in an Emacs that
3280 has limited date range is not negligible.
3282 A workaround for this problem is to use diary sexp dates for time
3283 stamps outside of this range."
3288 (defcustom org-read-date-display-live t
3289 "Non-nil means display current interpretation of date prompt live.
3290 This display will be in an overlay, in the minibuffer."
3294 (defcustom org-read-date-popup-calendar t
3295 "Non-nil means pop up a calendar when prompting for a date.
3296 In the calendar, the date can be selected with mouse-1. However, the
3297 minibuffer will also be active, and you can simply enter the date as well.
3298 When nil, only the minibuffer will be available."
3301 (defvaralias 'org-popup-calendar-for-date-prompt
3302 'org-read-date-popup-calendar
)
3304 (defcustom org-extend-today-until
0
3305 "The hour when your day really ends. Must be an integer.
3306 This has influence for the following applications:
3307 - When switching the agenda to \"today\". It it is still earlier than
3308 the time given here, the day recognized as TODAY is actually yesterday.
3309 - When a date is read from the user and it is still before the time given
3310 here, the current date and time will be assumed to be yesterday, 23:59.
3311 Also, timestamps inserted in capture templates follow this rule.
3313 IMPORTANT: This is a feature whose implementation is and likely will
3314 remain incomplete. Really, it is only here because past midnight seems to
3315 be the favorite working time of John Wiegley :-)"
3319 (defcustom org-use-effective-time nil
3320 "If non-nil, consider `org-extend-today-until' when creating timestamps.
3321 For example, if `org-extend-today-until' is 8, and it's 4am, then the
3322 \"effective time\" of any timestamps between midnight and 8am will be
3323 23:59 of the previous day."
3328 (defcustom org-use-last-clock-out-time-as-effective-time nil
3329 "When non-nil, use the last clock out time for `org-todo'.
3330 Note that this option has precedence over the combined use of
3331 `org-use-effective-time' and `org-extend-today-until'."
3334 :package-version
'(Org .
"8.0")
3337 (defcustom org-edit-timestamp-down-means-later nil
3338 "Non-nil means S-down will increase the time in a time stamp.
3339 When nil, S-up will increase."
3343 (defcustom org-calendar-follow-timestamp-change t
3344 "Non-nil means make the calendar window follow timestamp changes.
3345 When a timestamp is modified and the calendar window is visible, it will be
3346 moved to the new date."
3350 (defgroup org-tags nil
3351 "Options concerning tags in Org mode."
3355 (defcustom org-tag-alist nil
3356 "Default tags available in Org files.
3358 The value of this variable is an alist. Associations either:
3364 where TAG is a tag as a string, SELECT is character, used to
3365 select that tag through the fast tag selection interface, and
3366 SPECIAL is one of the following keywords: `:startgroup',
3367 `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
3368 `:newline'. These keywords are used to define a hierarchy of
3369 tags. See manual for details.
3371 When this variable is nil, Org mode bases tag input on what is
3372 already in the buffer. The value can be overridden locally by
3373 using a TAGS keyword, e.g.,
3377 See also `org-tag-persistent-alist' to sidestep this behavior."
3381 (cons :tag
"Tag with key"
3382 (string :tag
"Tag name")
3383 (character :tag
"Access char"))
3384 (list :tag
"Tag" (string :tag
"Tag name"))
3385 (const :tag
"Start radio group" (:startgroup
))
3386 (const :tag
"Start tag group, non distinct" (:startgrouptag
))
3387 (const :tag
"Group tags delimiter" (:grouptags
))
3388 (const :tag
"End radio group" (:endgroup
))
3389 (const :tag
"End tag group, non distinct" (:endgrouptag
))
3390 (const :tag
"New line" (:newline
)))))
3392 (defcustom org-tag-persistent-alist nil
3393 "Tags always available in Org files.
3395 The value of this variable is an alist. Associations either:
3401 where TAG is a tag as a string, SELECT is a character, used to
3402 select that tag through the fast tag selection interface, and
3403 SPECIAL is one of the following keywords: `:startgroup',
3404 `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
3405 `:newline'. These keywords are used to define a hierarchy of
3406 tags. See manual for details.
3408 Unlike to `org-tag-alist', tags defined in this variable do not
3409 depend on a local TAGS keyword. Instead, to disable these tags
3410 on a per-file basis, insert anywhere in the file:
3416 (cons :tag
"Tag with key"
3417 (string :tag
"Tag name")
3418 (character :tag
"Access char"))
3419 (list :tag
"Tag" (string :tag
"Tag name"))
3420 (const :tag
"Start radio group" (:startgroup
))
3421 (const :tag
"Start tag group, non distinct" (:startgrouptag
))
3422 (const :tag
"Group tags delimiter" (:grouptags
))
3423 (const :tag
"End radio group" (:endgroup
))
3424 (const :tag
"End tag group, non distinct" (:endgrouptag
))
3425 (const :tag
"New line" (:newline
)))))
3427 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
3428 "If non-nil, always offer completion for all tags of all agenda files.
3429 Instead of customizing this variable directly, you might want to
3430 set it locally for capture buffers, because there no list of
3431 tags in that file can be created dynamically (there are none).
3433 (add-hook \\='org-capture-mode-hook
3435 (setq-local org-complete-tags-always-offer-all-agenda-tags t)))"
3440 (defvar org-file-tags nil
3441 "List of tags that can be inherited by all entries in the file.
3442 The tags will be inherited if the variable `org-use-tag-inheritance'
3443 says they should be.
3444 This variable is populated from #+FILETAGS lines.")
3446 (defcustom org-use-fast-tag-selection
'auto
3447 "Non-nil means use fast tag selection scheme.
3448 This is a special interface to select and deselect tags with single keys.
3449 When nil, fast selection is never used.
3450 When the symbol `auto', fast selection is used if and only if selection
3451 characters for tags have been configured, either through the variable
3452 `org-tag-alist' or through a #+TAGS line in the buffer.
3453 When t, fast selection is always used and selection keys are assigned
3454 automatically if necessary."
3457 (const :tag
"Always" t
)
3458 (const :tag
"Never" nil
)
3459 (const :tag
"When selection characters are configured" auto
)))
3461 (defcustom org-fast-tag-selection-single-key nil
3462 "Non-nil means fast tag selection exits after first change.
3463 When nil, you have to press RET to exit it.
3464 During fast tag selection, you can toggle this flag with `C-c'.
3465 This variable can also have the value `expert'. In this case, the window
3466 displaying the tags menu is not even shown, until you press C-c again."
3469 (const :tag
"No" nil
)
3470 (const :tag
"Yes" t
)
3471 (const :tag
"Expert" expert
)))
3473 (defvar org-fast-tag-selection-include-todo nil
3474 "Non-nil means fast tags selection interface will also offer TODO states.
3475 This is an undocumented feature, you should not rely on it.")
3477 (defcustom org-tags-column -
77
3478 "The column to which tags should be indented in a headline.
3479 If this number is positive, it specifies the column. If it is negative,
3480 it means that the tags should be flushright to that column. For example,
3481 -80 works well for a normal 80 character screen.
3482 When 0, place tags directly after headline text, with only one space in
3487 (defcustom org-auto-align-tags t
3488 "Non-nil keeps tags aligned when modifying headlines.
3489 Some operations (i.e. demoting) change the length of a headline and
3490 therefore shift the tags around. With this option turned on, after
3491 each such operation the tags are again aligned to `org-tags-column'."
3495 (defcustom org-use-tag-inheritance t
3496 "Non-nil means tags in levels apply also for sublevels.
3497 When nil, only the tags directly given in a specific line apply there.
3498 This may also be a list of tags that should be inherited, or a regexp that
3499 matches tags that should be inherited. Additional control is possible
3500 with the variable `org-tags-exclude-from-inheritance' which gives an
3501 explicit list of tags to be excluded from inheritance, even if the value of
3502 `org-use-tag-inheritance' would select it for inheritance.
3504 If this option is t, a match early-on in a tree can lead to a large
3505 number of matches in the subtree when constructing the agenda or creating
3506 a sparse tree. If you only want to see the first match in a tree during
3507 a search, check out the variable `org-tags-match-list-sublevels'."
3510 (const :tag
"Not" nil
)
3511 (const :tag
"Always" t
)
3512 (repeat :tag
"Specific tags" (string :tag
"Tag"))
3513 (regexp :tag
"Tags matched by regexp")))
3515 (defcustom org-tags-exclude-from-inheritance nil
3516 "List of tags that should never be inherited.
3517 This is a way to exclude a few tags from inheritance. For way to do
3518 the opposite, to actively allow inheritance for selected tags,
3519 see the variable `org-use-tag-inheritance'."
3521 :type
'(repeat (string :tag
"Tag")))
3523 (defun org-tag-inherit-p (tag)
3524 "Check if TAG is one that should be inherited."
3526 ((member tag org-tags-exclude-from-inheritance
) nil
)
3527 ((eq org-use-tag-inheritance t
) t
)
3528 ((not org-use-tag-inheritance
) nil
)
3529 ((stringp org-use-tag-inheritance
)
3530 (string-match org-use-tag-inheritance tag
))
3531 ((listp org-use-tag-inheritance
)
3532 (member tag org-use-tag-inheritance
))
3533 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3535 (defcustom org-tags-match-list-sublevels t
3536 "Non-nil means list also sublevels of headlines matching a search.
3537 This variable applies to tags/property searches, and also to stuck
3538 projects because this search is based on a tags match as well.
3540 When set to the symbol `indented', sublevels are indented with
3543 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3544 the sublevels of a headline matching a tag search often also match
3545 the same search. Listing all of them can create very long lists.
3546 Setting this variable to nil causes subtrees of a match to be skipped.
3548 This variable is semi-obsolete and probably should always be true. It
3549 is better to limit inheritance to certain tags using the variables
3550 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3553 (const :tag
"No, don't list them" nil
)
3554 (const :tag
"Yes, do list them" t
)
3555 (const :tag
"List them, indented with leading dots" indented
)))
3557 (defcustom org-tags-sort-function nil
3558 "When set, tags are sorted using this function as a comparator."
3561 (const :tag
"No sorting" nil
)
3562 (const :tag
"Alphabetical" org-string-collate-lessp
)
3563 (const :tag
"Reverse alphabetical" org-string-collate-greaterp
)
3564 (function :tag
"Custom function" nil
)))
3566 (defvar org-tags-history nil
3567 "History of minibuffer reads for tags.")
3568 (defvar org-last-tags-completion-table nil
3569 "The last used completion table for tags.")
3570 (defvar org-after-tags-change-hook nil
3571 "Hook that is run after the tags in a line have changed.")
3573 (defgroup org-properties nil
3574 "Options concerning properties in Org mode."
3575 :tag
"Org Properties"
3578 (defcustom org-property-format
"%-10s %s"
3579 "How property key/value pairs should be formatted by `indent-line'.
3580 When `indent-line' hits a property definition, it will format the line
3581 according to this format, mainly to make sure that the values are
3582 lined-up with respect to each other."
3583 :group
'org-properties
3586 (defcustom org-properties-postprocess-alist nil
3587 "Alist of properties and functions to adjust inserted values.
3588 Elements of this alist must be of the form
3590 ([string] [function])
3592 where [string] must be a property name and [function] must be a
3593 lambda expression: this lambda expression must take one argument,
3594 the value to adjust, and return the new value as a string.
3596 For example, this element will allow the property \"Remaining\"
3597 to be updated wrt the relation between the \"Effort\" property
3598 and the clock summary:
3600 ((\"Remaining\" (lambda(value)
3601 (let ((clocksum (org-clock-sum-current-item))
3602 (effort (org-duration-to-minutes
3603 (org-entry-get (point) \"Effort\"))))
3604 (org-minutes-to-clocksum-string (- effort clocksum))))))"
3605 :group
'org-properties
3607 :type
'(alist :key-type
(string :tag
"Property")
3608 :value-type
(function :tag
"Function")))
3610 (defcustom org-use-property-inheritance nil
3611 "Non-nil means properties apply also for sublevels.
3613 This setting is chiefly used during property searches. Turning it on can
3614 cause significant overhead when doing a search, which is why it is not
3617 When nil, only the properties directly given in the current entry count.
3618 When t, every property is inherited. The value may also be a list of
3619 properties that should have inheritance, or a regular expression matching
3620 properties that should be inherited.
3622 However, note that some special properties use inheritance under special
3623 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3624 and the properties ending in \"_ALL\" when they are used as descriptor
3625 for valid values of a property.
3627 Note for programmers:
3628 When querying an entry with `org-entry-get', you can control if inheritance
3629 should be used. By default, `org-entry-get' looks only at the local
3630 properties. You can request inheritance by setting the inherit argument
3631 to t (to force inheritance) or to `selective' (to respect the setting
3633 :group
'org-properties
3635 (const :tag
"Not" nil
)
3636 (const :tag
"Always" t
)
3637 (repeat :tag
"Specific properties" (string :tag
"Property"))
3638 (regexp :tag
"Properties matched by regexp")))
3640 (defun org-property-inherit-p (property)
3641 "Return a non-nil value if PROPERTY should be inherited."
3643 ((eq org-use-property-inheritance t
) t
)
3644 ((not org-use-property-inheritance
) nil
)
3645 ((stringp org-use-property-inheritance
)
3646 (string-match org-use-property-inheritance property
))
3647 ((listp org-use-property-inheritance
)
3648 (member-ignore-case property org-use-property-inheritance
))
3649 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3651 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3652 "The default column format, if no other format has been defined.
3653 This variable can be set on the per-file basis by inserting a line
3655 #+COLUMNS: %25ITEM ....."
3656 :group
'org-properties
3659 (defcustom org-columns-ellipses
".."
3660 "The ellipses to be used when a field in column view is truncated.
3661 When this is the empty string, as many characters as possible are shown,
3662 but then there will be no visual indication that the field has been truncated.
3663 When this is a string of length N, the last N characters of a truncated
3664 field are replaced by this string. If the column is narrower than the
3665 ellipses string, only part of the ellipses string will be shown."
3666 :group
'org-properties
3669 (defconst org-global-properties-fixed
3670 '(("VISIBILITY_ALL" .
"folded children content all")
3671 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3672 "List of property/value pairs that can be inherited by any entry.
3674 These are fixed values, for the preset properties. The user variable
3675 that can be used to add to this list is `org-global-properties'.
3677 The entries in this list are cons cells where the car is a property
3678 name and cdr is a string with the value. If the value represents
3679 multiple items like an \"_ALL\" property, separate the items by
3682 (defcustom org-global-properties nil
3683 "List of property/value pairs that can be inherited by any entry.
3685 This list will be combined with the constant `org-global-properties-fixed'.
3687 The entries in this list are cons cells where the car is a property
3688 name and cdr is a string with the value.
3690 You can set buffer-local values for the same purpose in the variable
3691 `org-file-properties' this by adding lines like
3693 #+PROPERTY: NAME VALUE"
3694 :group
'org-properties
3696 (cons (string :tag
"Property")
3697 (string :tag
"Value"))))
3699 (defvar-local org-file-properties nil
3700 "List of property/value pairs that can be inherited by any entry.
3701 Valid for the current buffer.
3702 This variable is populated from #+PROPERTY lines.")
3704 (defgroup org-agenda nil
3705 "Options concerning agenda views in Org mode."
3709 (defvar-local org-category nil
3710 "Variable used by Org files to set a category for agenda display.
3711 Such files should use a file variable to set it, for example
3713 # -*- mode: org; org-category: \"ELisp\"
3715 or contain a special line
3719 If the file does not specify a category, then file's base name
3721 (put 'org-category
'safe-local-variable
(lambda (x) (or (symbolp x
) (stringp x
))))
3723 (defcustom org-agenda-files nil
3724 "The files to be used for agenda display.
3726 If an entry is a directory, all files in that directory that are matched
3727 by `org-agenda-file-regexp' will be part of the file list.
3729 If the value of the variable is not a list but a single file name, then
3730 the list of agenda files is actually stored and maintained in that file,
3731 one agenda file per line. In this file paths can be given relative to
3732 `org-directory'. Tilde expansion and environment variable substitution
3735 Entries may be added to this list with `\\[org-agenda-file-to-front]'
3736 and removed with `\\[org-remove-file]'."
3739 (repeat :tag
"List of files and directories" file
)
3740 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3742 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3743 "Regular expression to match files for `org-agenda-files'.
3744 If any element in the list in that variable contains a directory instead
3745 of a normal file, all files in that directory that are matched by this
3746 regular expression will be included."
3750 (defcustom org-agenda-text-search-extra-files nil
3751 "List of extra files to be searched by text search commands.
3752 These files will be searched in addition to the agenda files by the
3753 commands `org-search-view' (`\\[org-agenda] s') \
3754 and `org-occur-in-agenda-files'.
3755 Note that these files will only be searched for text search commands,
3756 not for the other agenda views like todo lists, tag searches or the weekly
3757 agenda. This variable is intended to list notes and possibly archive files
3758 that should also be searched by these two commands.
3759 In fact, if the first element in the list is the symbol `agenda-archives',
3760 then all archive files of all agenda files will be added to the search
3763 :type
'(set :greedy t
3764 (const :tag
"Agenda Archives" agenda-archives
)
3765 (repeat :inline t
(file))))
3767 (defvaralias 'org-agenda-multi-occur-extra-files
3768 'org-agenda-text-search-extra-files
)
3770 (defcustom org-agenda-skip-unavailable-files nil
3771 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3772 A nil value means to remove them, after a query, from the list."
3776 (defcustom org-agenda-diary-file
'diary-file
3777 "File to which to add new entries with the `i' key in agenda and calendar.
3778 When this is the symbol `diary-file', the functionality in the Emacs
3779 calendar will be used to add entries to the `diary-file'. But when this
3780 points to a file, `org-agenda-diary-entry' will be used instead."
3783 (const :tag
"The standard Emacs diary file" diary-file
)
3784 (file :tag
"Special Org file diary entries")))
3786 (defgroup org-latex nil
3787 "Options for embedding LaTeX code into Org mode."
3791 (defcustom org-format-latex-options
3792 '(:foreground default
:background default
:scale
1.0
3793 :html-foreground
"Black" :html-background
"Transparent"
3794 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3795 "Options for creating images from LaTeX fragments.
3796 This is a property list with the following properties:
3797 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3798 `default' means use the foreground of the default face.
3799 `auto' means use the foreground from the text face.
3800 :background the background color, or \"Transparent\".
3801 `default' means use the background of the default face.
3802 `auto' means use the background from the text face.
3803 :scale a scaling factor for the size of the images, to get more pixels
3804 :html-foreground, :html-background, :html-scale
3805 the same numbers for HTML export.
3806 :matchers a list indicating which matchers should be used to
3807 find LaTeX fragments. Valid members of this list are:
3808 \"begin\" find environments
3809 \"$1\" find single characters surrounded by $.$
3810 \"$\" find math expressions surrounded by $...$
3811 \"$$\" find math expressions surrounded by $$....$$
3812 \"\\(\" find math expressions surrounded by \\(...\\)
3813 \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
3817 (defcustom org-format-latex-signal-error t
3818 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3819 When nil, just push out a message."
3824 (defcustom org-latex-to-mathml-jar-file nil
3825 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3826 Use this to specify additional executable file say a jar file.
3828 When using MathToWeb as the converter, specify the full-path to
3829 your mathtoweb.jar file."
3833 (const :tag
"None" nil
)
3834 (file :tag
"JAR file" :must-match t
)))
3836 (defcustom org-latex-to-mathml-convert-command nil
3837 "Command to convert LaTeX fragments to MathML.
3838 Replace format-specifiers in the command as noted below and use
3839 `shell-command' to convert LaTeX to MathML.
3840 %j: Executable file in fully expanded form as specified by
3841 `org-latex-to-mathml-jar-file'.
3842 %I: Input LaTeX file in fully expanded form.
3843 %i: The latex fragment to be converted.
3844 %o: Output MathML file.
3846 This command is used by `org-create-math-formula'.
3848 When using MathToWeb as the converter, set this option to
3849 \"java -jar %j -unicode -force -df %o %I\".
3851 When using LaTeXML set this option to
3852 \"latexmlmath \"%i\" --presentationmathml=%o\"."
3856 (const :tag
"None" nil
)
3857 (string :tag
"\nShell command")))
3859 (defcustom org-preview-latex-default-process
'dvipng
3860 "The default process to convert LaTeX fragments to image files.
3861 All available processes and theirs documents can be found in
3862 `org-preview-latex-process-alist', which see."
3865 :package-version
'(Org .
"9.0")
3868 (defcustom org-preview-latex-process-alist
3870 :programs
("latex" "dvipng")
3871 :description
"dvi > png"
3872 :message
"you need to install the programs: latex and dvipng."
3873 :image-input-type
"dvi"
3874 :image-output-type
"png"
3875 :image-size-adjust
(1.0 .
1.0)
3876 :latex-compiler
("latex -interaction nonstopmode -output-directory %o %f")
3877 :image-converter
("dvipng -fg %F -bg %B -D %D -T tight -o %O %f"))
3879 :programs
("latex" "dvisvgm")
3880 :description
"dvi > svg"
3881 :message
"you need to install the programs: latex and dvisvgm."
3883 :image-input-type
"dvi"
3884 :image-output-type
"svg"
3885 :image-size-adjust
(1.7 .
1.5)
3886 :latex-compiler
("latex -interaction nonstopmode -output-directory %o %f")
3887 :image-converter
("dvisvgm %f -n -b min -c %S -o %O"))
3889 :programs
("latex" "convert")
3890 :description
"pdf > png"
3891 :message
"you need to install the programs: latex and imagemagick."
3893 :image-input-type
"pdf"
3894 :image-output-type
"png"
3895 :image-size-adjust
(1.0 .
1.0)
3896 :latex-compiler
("pdflatex -interaction nonstopmode -output-directory %o %f")
3898 ("convert -density %D -trim -antialias %f -quality 100 %O")))
3899 "Definitions of external processes for LaTeX previewing.
3900 Org mode can use some external commands to generate TeX snippet's images for
3901 previewing or inserting into HTML files, e.g., \"dvipng\". This variable tells
3902 `org-create-formula-image' how to call them.
3904 The value is an alist with the pattern (NAME . PROPERTIES). NAME is a symbol.
3905 PROPERTIES accepts the following attributes:
3907 :programs list of strings, required programs.
3908 :description string, describe the process.
3909 :message string, message it when required programs cannot be found.
3910 :image-input-type string, input file type of image converter (e.g., \"dvi\").
3911 :image-output-type string, output file type of image converter (e.g., \"png\").
3912 :use-xcolor boolean, when non-nil, LaTeX \"xcolor\" macro is used to
3913 deal with background and foreground color of image.
3914 Otherwise, dvipng style background and foreground color
3915 format are generated. You may then refer to them in
3916 command options with \"%F\" and \"%B\".
3917 :image-size-adjust cons of numbers, the car element is used to adjust LaTeX
3918 image size showed in buffer and the cdr element is for
3919 HTML file. This option is only useful for process
3920 developers, users should use variable
3921 `org-format-latex-options' instead.
3922 :post-clean list of strings, files matched are to be cleaned up once
3923 the image is generated. When nil, the files with \".dvi\",
3924 \".xdv\", \".pdf\", \".tex\", \".aux\", \".log\", \".svg\",
3925 \".png\", \".jpg\", \".jpeg\" or \".out\" extension will
3927 :latex-header list of strings, the LaTeX header of the snippet file.
3928 When nil, the fallback value is used instead, which is
3929 controlled by `org-format-latex-header',
3930 `org-latex-default-packages-alist' and
3931 `org-latex-packages-alist', which see.
3932 :latex-compiler list of LaTeX commands, as strings. Each of them is given
3933 to the shell. Place-holders \"%t\", \"%b\" and \"%o\" are
3934 replaced with values defined below.
3935 :image-converter list of image converter commands strings. Each of them is
3936 given to the shell and supports any of the following
3937 place-holders defined below.
3939 Place-holders used by `:image-converter' and `:latex-compiler':
3942 %b base name of input file
3943 %o base directory of input file
3944 %O absolute output file name
3946 Place-holders only used by `:image-converter':
3948 %F foreground of image
3949 %B background of image
3950 %D dpi, which is used to adjust image size by some processing commands.
3951 %S the image size scale ratio, which is used to adjust image size by some
3952 processing commands."
3955 :package-version
'(Org .
"9.0")
3956 :type
'(alist :tag
"LaTeX to image backends"
3957 :value-type
(plist)))
3959 (defcustom org-preview-latex-image-directory
"ltximg/"
3960 "Path to store latex preview images.
3961 A relative path here creates many directories relative to the
3962 processed Org files paths. An absolute path puts all preview
3963 images at the same place."
3966 :package-version
'(Org .
"9.0")
3969 (defun org-format-latex-mathml-available-p ()
3970 "Return t if `org-latex-to-mathml-convert-command' is usable."
3972 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3973 org-latex-to-mathml-convert-command
)
3974 (let ((executable (car (split-string
3975 org-latex-to-mathml-convert-command
))))
3976 (when (executable-find executable
)
3978 "%j" org-latex-to-mathml-convert-command
)
3979 (file-readable-p org-latex-to-mathml-jar-file
)
3982 (defcustom org-format-latex-header
"\\documentclass{article}
3983 \\usepackage[usenames]{color}
3986 \\pagestyle{empty} % do not remove
3987 % The settings below are copied from fullpage.sty
3988 \\setlength{\\textwidth}{\\paperwidth}
3989 \\addtolength{\\textwidth}{-3cm}
3990 \\setlength{\\oddsidemargin}{1.5cm}
3991 \\addtolength{\\oddsidemargin}{-2.54cm}
3992 \\setlength{\\evensidemargin}{\\oddsidemargin}
3993 \\setlength{\\textheight}{\\paperheight}
3994 \\addtolength{\\textheight}{-\\headheight}
3995 \\addtolength{\\textheight}{-\\headsep}
3996 \\addtolength{\\textheight}{-\\footskip}
3997 \\addtolength{\\textheight}{-3cm}
3998 \\setlength{\\topmargin}{1.5cm}
3999 \\addtolength{\\topmargin}{-2.54cm}"
4000 "The document header used for processing LaTeX fragments.
4001 It is imperative that this header make sure that no page number
4002 appears on the page. The package defined in the variables
4003 `org-latex-default-packages-alist' and `org-latex-packages-alist'
4004 will either replace the placeholder \"[PACKAGES]\" in this
4005 header, or they will be appended."
4009 (defun org-set-packages-alist (var val
)
4010 "Set the packages alist and make sure it has 3 elements per entry."
4011 (set var
(mapcar (lambda (x)
4012 (if (and (consp x
) (= (length x
) 2))
4013 (list (car x
) (nth 1 x
) t
)
4017 (defun org-get-packages-alist (var)
4018 "Get the packages alist and make sure it has 3 elements per entry."
4020 (if (and (consp x
) (= (length x
) 2))
4021 (list (car x
) (nth 1 x
) t
)
4023 (default-value var
)))
4025 (defcustom org-latex-default-packages-alist
4026 '(("AUTO" "inputenc" t
("pdflatex"))
4027 ("T1" "fontenc" t
("pdflatex"))
4030 ("" "longtable" nil
)
4033 ("normalem" "ulem" t
)
4038 ("" "hyperref" nil
))
4039 "Alist of default packages to be inserted in the header.
4041 Change this only if one of the packages here causes an
4042 incompatibility with another package you are using.
4044 The packages in this list are needed by one part or another of
4045 Org mode to function properly:
4047 - inputenc, fontenc: for basic font and character selection
4048 - graphicx: for including images
4049 - grffile: allow periods and spaces in graphics file names
4050 - longtable: For multipage tables
4051 - wrapfig: for figure placement
4052 - rotating: for sideways figures and tables
4053 - ulem: for underline and strike-through
4054 - amsmath: for subscript and superscript and math environments
4055 - textcomp, amssymb: for various symbols used
4056 for interpreting the entities in `org-entities'. You can skip
4057 some of these packages if you don't use any of their symbols.
4058 - capt-of: for captions outside of floats
4059 - hyperref: for cross references
4061 Therefore you should not modify this variable unless you know
4062 what you are doing. The one reason to change it anyway is that
4063 you might be loading some other package that conflicts with one
4064 of the default packages. Each element is either a cell or
4067 A cell is of the format
4069 (\"options\" \"package\" SNIPPET-FLAG COMPILERS)
4071 If SNIPPET-FLAG is non-nil, the package also needs to be included
4072 when compiling LaTeX snippets into images for inclusion into
4073 non-LaTeX output. COMPILERS is a list of compilers that should
4074 include the package, see `org-latex-compiler'. If the document
4075 compiler is not in the list, and the list is non-nil, the package
4076 will not be inserted in the final document.
4078 A string will be inserted as-is in the header of the document."
4080 :group
'org-export-latex
4081 :set
'org-set-packages-alist
4082 :get
'org-get-packages-alist
4084 :package-version
'(Org .
"8.3")
4087 (list :tag
"options/package pair"
4088 (string :tag
"options")
4089 (string :tag
"package")
4090 (boolean :tag
"Snippet")
4092 (const :tag
"For all compilers" nil
)
4093 (repeat :tag
"Allowed compiler" string
)))
4094 (string :tag
"A line of LaTeX"))))
4096 (defcustom org-latex-packages-alist nil
4097 "Alist of packages to be inserted in every LaTeX header.
4099 These will be inserted after `org-latex-default-packages-alist'.
4100 Each element is either a cell or a string.
4102 A cell is of the format:
4104 (\"options\" \"package\" SNIPPET-FLAG)
4106 SNIPPET-FLAG, when non-nil, indicates that this package is also
4107 needed when turning LaTeX snippets into images for inclusion into
4110 A string will be inserted as-is in the header of the document.
4112 Make sure that you only list packages here which:
4114 - you want in every file;
4115 - do not conflict with the setup in `org-format-latex-header';
4116 - do not conflict with the default packages in
4117 `org-latex-default-packages-alist'."
4119 :group
'org-export-latex
4120 :set
'org-set-packages-alist
4121 :get
'org-get-packages-alist
4124 (list :tag
"options/package pair"
4125 (string :tag
"options")
4126 (string :tag
"package")
4127 (boolean :tag
"Snippet"))
4128 (string :tag
"A line of LaTeX"))))
4130 (defgroup org-appearance nil
4131 "Settings for Org mode appearance."
4132 :tag
"Org Appearance"
4135 (defcustom org-level-color-stars-only nil
4136 "Non-nil means fontify only the stars in each headline.
4137 When nil, the entire headline is fontified.
4138 Changing it requires restart of `font-lock-mode' to become effective
4139 also in regions already fontified."
4140 :group
'org-appearance
4143 (defcustom org-hide-leading-stars nil
4144 "Non-nil means hide the first N-1 stars in a headline.
4145 This works by using the face `org-hide' for these stars. This
4146 face is white for a light background, and black for a dark
4147 background. You may have to customize the face `org-hide' to
4149 Changing it requires restart of `font-lock-mode' to become effective
4150 also in regions already fontified.
4151 You may also set this on a per-file basis by adding one of the following
4152 lines to the buffer:
4154 #+STARTUP: hidestars
4155 #+STARTUP: showstars"
4156 :group
'org-appearance
4159 (defcustom org-hidden-keywords nil
4160 "List of symbols corresponding to keywords to be hidden in the Org buffer.
4161 For example, a value \\='(title) for this list makes the document's title
4162 appear in the buffer without the initial \"#+TITLE:\" part."
4163 :group
'org-appearance
4165 :type
'(set (const :tag
"#+AUTHOR" author
)
4166 (const :tag
"#+DATE" date
)
4167 (const :tag
"#+EMAIL" email
)
4168 (const :tag
"#+TITLE" title
)))
4170 (defcustom org-custom-properties nil
4171 "List of properties (as strings) with a special meaning.
4172 The default use of these custom properties is to let the user
4173 hide them with `org-toggle-custom-properties-visibility'."
4174 :group
'org-properties
4175 :group
'org-appearance
4177 :type
'(repeat (string :tag
"Property Name")))
4179 (defcustom org-fontify-done-headline nil
4180 "Non-nil means change the face of a headline if it is marked DONE.
4181 Normally, only the TODO/DONE keyword indicates the state of a headline.
4182 When this is non-nil, the headline after the keyword is set to the
4183 `org-headline-done' as an additional indication."
4184 :group
'org-appearance
4187 (defcustom org-fontify-emphasized-text t
4188 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
4189 Changing this variable requires a restart of Emacs to take effect."
4190 :group
'org-appearance
4193 (defcustom org-fontify-whole-heading-line nil
4194 "Non-nil means fontify the whole line for headings.
4195 This is useful when setting a background color for the
4197 :group
'org-appearance
4200 (defcustom org-highlight-latex-and-related nil
4201 "Non-nil means highlight LaTeX related syntax in the buffer.
4202 When non nil, the value should be a list containing any of the
4204 `latex' Highlight LaTeX snippets and environments.
4205 `script' Highlight subscript and superscript.
4206 `entities' Highlight entities."
4207 :group
'org-appearance
4209 :package-version
'(Org .
"8.0")
4211 (const :tag
"No highlighting" nil
)
4212 (set :greedy t
:tag
"Highlight"
4213 (const :tag
"LaTeX snippets and environments" latex
)
4214 (const :tag
"Subscript and superscript" script
)
4215 (const :tag
"Entities" entities
))))
4217 (defcustom org-hide-emphasis-markers nil
4218 "Non-nil mean font-lock should hide the emphasis marker characters."
4219 :group
'org-appearance
4222 (defcustom org-hide-macro-markers nil
4223 "Non-nil mean font-lock should hide the brackets marking macro calls."
4224 :group
'org-appearance
4227 (defcustom org-pretty-entities nil
4228 "Non-nil means show entities as UTF8 characters.
4229 When nil, the \\name form remains in the buffer."
4230 :group
'org-appearance
4234 (defcustom org-pretty-entities-include-sub-superscripts t
4235 "Non-nil means, pretty entity display includes formatting sub/superscripts."
4236 :group
'org-appearance
4240 (defvar org-emph-re nil
4241 "Regular expression for matching emphasis.
4242 After a match, the match groups contain these elements:
4243 0 The match of the full regular expression, including the characters
4244 before and after the proper match
4245 1 The character before the proper match, or empty at beginning of line
4246 2 The proper match, including the leading and trailing markers
4247 3 The leading marker like * or /, indicating the type of highlighting
4248 4 The text between the emphasis markers, not including the markers
4249 5 The character after the match, empty at the end of a line")
4251 (defvar org-verbatim-re nil
4252 "Regular expression for matching verbatim text.")
4254 (defvar org-emphasis-regexp-components
) ; defined just below
4255 (defvar org-emphasis-alist
) ; defined just below
4256 (defun org-set-emph-re (var val
)
4257 "Set variable and compute the emphasis regular expression."
4259 (when (and (boundp 'org-emphasis-alist
)
4260 (boundp 'org-emphasis-regexp-components
)
4261 org-emphasis-alist org-emphasis-regexp-components
)
4263 ((`(,pre
,post
,border
,body
,nl
) org-emphasis-regexp-components
)
4264 (body (if (<= nl
0) body
4265 (format "%s*?\\(?:\n%s*?\\)\\{0,%d\\}" body body nl
)))
4267 (format (concat "\\([%s]\\|^\\)" ;before markers
4268 "\\(\\([%%s]\\)\\([^%s]\\|[^%s]%s[^%s]\\)\\3\\)"
4269 "\\([%s]\\|$\\)") ;after markers
4270 pre border border body border post
)))
4271 (setq org-emph-re
(format template
"*/_+"))
4272 (setq org-verbatim-re
(format template
"=~")))))
4274 ;; This used to be a defcustom (Org <8.0) but allowing the users to
4275 ;; set this option proved cumbersome. See this message/thread:
4276 ;; http://article.gmane.org/gmane.emacs.orgmode/68681
4277 (defvar org-emphasis-regexp-components
4278 '("-[:space:]('\"{" "-[:space:].,:!?;'\")}\\[" "[:space:]" "." 1)
4279 "Components used to build the regular expression for emphasis.
4280 This is a list with five entries. Terminology: In an emphasis string
4281 like \" *strong word* \", we call the initial space PREMATCH, the final
4282 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
4283 and \"trong wor\" is the body. The different components in this variable
4284 specify what is allowed/forbidden in each part:
4286 pre Chars allowed as prematch. Beginning of line will be allowed too.
4287 post Chars allowed as postmatch. End of line will be allowed too.
4288 border The chars *forbidden* as border characters.
4289 body-regexp A regexp like \".\" to match a body character. Don't use
4290 non-shy groups here, and don't allow newline here.
4291 newline The maximum number of newlines allowed in an emphasis exp.
4293 You need to reload Org or to restart Emacs after setting this.")
4295 (defcustom org-emphasis-alist
4299 ("=" org-verbatim verbatim
)
4300 ("~" org-code verbatim
)
4301 ("+" (:strike-through t
)))
4302 "Alist of characters and faces to emphasize text.
4303 Text starting and ending with a special character will be emphasized,
4304 for example *bold*, _underlined_ and /italic/. This variable sets the
4305 marker characters and the face to be used by font-lock for highlighting
4308 You need to reload Org or to restart Emacs after customizing this."
4309 :group
'org-appearance
4310 :set
'org-set-emph-re
4312 :package-version
'(Org .
"8.0")
4315 (string :tag
"Marker character")
4317 (face :tag
"Font-lock-face")
4318 (plist :tag
"Face property list"))
4319 (option (const verbatim
)))))
4321 (defvar org-protecting-blocks
'("src" "example" "export")
4322 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
4323 This is needed for font-lock setup.")
4325 ;;; Functions and variables from their packages
4326 ;; Declared here to avoid compiler warnings
4327 (defvar mark-active
)
4330 (declare-function calc-eval
"calc" (str &optional separator
&rest args
))
4331 (declare-function calendar-forward-day
"cal-move" (arg))
4332 (declare-function calendar-goto-date
"cal-move" (date))
4333 (declare-function calendar-goto-today
"cal-move" ())
4334 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
4335 (declare-function calendar-iso-to-absolute
"cal-iso" (date))
4336 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
4337 (declare-function cdlatex-tab
"ext:cdlatex" ())
4338 (declare-function dired-get-filename
4340 (&optional localp no-error-if-not-filep
))
4341 (declare-function iswitchb-read-buffer
4344 default require-match _predicate start matches-set
))
4345 (declare-function org-agenda-change-all-lines
4347 (newhead hdmarker
&optional fixface just-this
))
4348 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4351 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
4352 (declare-function org-agenda-format-item
4354 (extra txt
&optional level category tags dotime
4356 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
4357 (declare-function org-agenda-save-markers-for-cut-and-paste
4360 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
4361 (declare-function org-agenda-skip
"org-agenda" ())
4362 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
4363 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
4364 (declare-function org-indent-mode
"org-indent" (&optional arg
))
4365 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
4366 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
4367 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
4368 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
4369 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
4370 (declare-function parse-time-string
"parse-time" (string))
4372 (defvar align-mode-rules-list
)
4373 (defvar calc-embedded-close-formula
)
4374 (defvar calc-embedded-open-formula
)
4375 (defvar calc-embedded-open-mode
)
4376 (defvar font-lock-unfontify-region-function
)
4377 (defvar iswitchb-temp-buflist
)
4378 (defvar org-agenda-tags-todo-honor-ignore-options
)
4379 (defvar remember-data-file
)
4380 (defvar texmathp-why
)
4383 (defun turn-on-orgtbl ()
4384 "Unconditionally turn on `orgtbl-mode'."
4385 (require 'org-table
)
4388 (defun org-at-table-p (&optional table-type
)
4389 "Non-nil if the cursor is inside an Org table.
4390 If TABLE-TYPE is non-nil, also check for table.el-type tables."
4391 (and (org-match-line (if table-type
"[ \t]*[|+]" "[ \t]*|"))
4392 (or (not (derived-mode-p 'org-mode
))
4393 (let ((e (org-element-lineage (org-element-at-point) '(table) t
)))
4394 (and e
(or table-type
4395 (eq 'org
(org-element-property :type e
))))))))
4397 (defun org-at-table.el-p
()
4398 "Non-nil when point is at a table.el table."
4399 (and (org-match-line "[ \t]*[|+]")
4400 (let ((element (org-element-at-point)))
4401 (and (eq (org-element-type element
) 'table
)
4402 (eq (org-element-property :type element
) 'table.el
)))))
4404 (defun org-at-table-hline-p ()
4405 "Non-nil when point is inside a hline in a table.
4406 Assume point is already in a table."
4407 (org-match-line org-table-hline-regexp
))
4409 (defun org-table-map-tables (function &optional quietly
)
4410 "Apply FUNCTION to the start of all tables in the buffer."
4411 (org-with-wide-buffer
4412 (goto-char (point-min))
4413 (while (re-search-forward org-table-any-line-regexp nil t
)
4415 (message "Mapping tables: %d%%"
4416 (floor (* 100.0 (point)) (buffer-size))))
4417 (beginning-of-line 1)
4418 (when (and (looking-at org-table-line-regexp
)
4419 ;; Exclude tables in src/example/verbatim/clocktable blocks
4420 (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
4421 (save-excursion (funcall function
))
4422 (or (looking-at org-table-line-regexp
)
4424 (re-search-forward org-table-any-border-regexp nil
1)))
4425 (unless quietly
(message "Mapping tables: done")))
4427 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock" (beg end
))
4428 (declare-function org-clock-update-mode-line
"org-clock" ())
4429 (declare-function org-resolve-clocks
"org-clock"
4430 (&optional also-non-dangling-p prompt last-valid
))
4432 (defun org-at-TBLFM-p (&optional pos
)
4433 "Non-nil when point (or POS) is in #+TBLFM line."
4435 (goto-char (or pos
(point)))
4437 (and (let ((case-fold-search t
)) (looking-at org-TBLFM-regexp
))
4438 (eq (org-element-type (org-element-at-point)) 'table
))))
4440 (defvar org-clock-start-time
)
4441 (defvar org-clock-marker
(make-marker)
4442 "Marker recording the last clock-in.")
4443 (defvar org-clock-hd-marker
(make-marker)
4444 "Marker recording the last clock-in, but the headline position.")
4445 (defvar org-clock-heading
""
4446 "The heading of the current clock entry.")
4447 (defun org-clock-is-active ()
4448 "Return the buffer where the clock is currently running.
4449 Return nil if no clock is running."
4450 (marker-buffer org-clock-marker
))
4452 (defun org-check-running-clock ()
4453 "Check if the current buffer contains the running clock.
4454 If yes, offer to stop it and to save the buffer with the changes."
4455 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4456 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4459 (when (y-or-n-p "Save changed buffer?")
4462 (defun org-clocktable-try-shift (dir n
)
4463 "Check if this line starts a clock table, if yes, shift the time block."
4464 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4465 (org-clocktable-shift dir n
)))
4468 (defun org-clock-persistence-insinuate ()
4469 "Set up hooks for clock persistence."
4470 (require 'org-clock
)
4471 (add-hook 'org-mode-hook
'org-clock-load
)
4472 (add-hook 'kill-emacs-hook
'org-clock-save
))
4474 (defgroup org-archive nil
4475 "Options concerning archiving in Org mode."
4477 :group
'org-structure
)
4479 (defcustom org-archive-location
"%s_archive::"
4480 "The location where subtrees should be archived.
4482 The value of this variable is a string, consisting of two parts,
4483 separated by a double-colon. The first part is a filename and
4484 the second part is a headline.
4486 When the filename is omitted, archiving happens in the same file.
4487 %s in the filename will be replaced by the current file
4488 name (without the directory part). Archiving to a different file
4489 is useful to keep archived entries from contributing to the
4492 The archived entries will be filed as subtrees of the specified
4493 headline. When the headline is omitted, the subtrees are simply
4494 filed away at the end of the file, as top-level entries. Also in
4495 the heading you can use %s to represent the file name, this can be
4496 useful when using the same archive for a number of different files.
4498 Here are a few examples:
4500 If the current file is Projects.org, archive in file
4501 Projects.org_archive, as top-level trees. This is the default.
4503 \"::* Archived Tasks\"
4504 Archive in the current file, under the top-level headline
4505 \"* Archived Tasks\".
4507 \"~/org/archive.org::\"
4508 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4510 \"~/org/archive.org::* From %s\"
4511 Archive in file ~/org/archive.org (absolute path), under headlines
4512 \"From FILENAME\" where file name is the current file name.
4514 \"~/org/datetree.org::datetree/* Finished Tasks\"
4515 The \"datetree/\" string is special, signifying to archive
4516 items to the datetree. Items are placed in either the CLOSED
4517 date of the item, or the current date if there is no CLOSED date.
4518 The heading will be a subentry to the current date. There doesn't
4519 need to be a heading, but there always needs to be a slash after
4520 datetree. For example, to store archived items directly in the
4521 datetree, use \"~/org/datetree.org::datetree/\".
4523 \"basement::** Finished Tasks\"
4524 Archive in file ./basement (relative path), as level 3 trees
4525 below the level 2 heading \"** Finished Tasks\".
4527 You may set this option on a per-file basis by adding to the buffer a
4530 #+ARCHIVE: basement::** Finished Tasks
4532 You may also define it locally for a subtree by setting an ARCHIVE property
4533 in the entry. If such a property is found in an entry, or anywhere up
4534 the hierarchy, it will be used."
4538 (defcustom org-agenda-skip-archived-trees t
4539 "Non-nil means the agenda will skip any items located in archived trees.
4540 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4541 variable is no longer recommended, you should leave it at the value t.
4542 Instead, use the key `v' to cycle the archives-mode in the agenda."
4544 :group
'org-agenda-skip
4547 (defcustom org-columns-skip-archived-trees t
4548 "Non-nil means ignore archived trees when creating column view."
4550 :group
'org-properties
4553 (defcustom org-cycle-open-archived-trees nil
4554 "Non-nil means `org-cycle' will open archived trees.
4555 An archived tree is a tree marked with the tag ARCHIVE.
4556 When nil, archived trees will stay folded. You can still open them with
4557 normal outline commands like `show-all', but not with the cycling commands."
4562 (defcustom org-sparse-tree-open-archived-trees nil
4563 "Non-nil means sparse tree construction shows matches in archived trees.
4564 When nil, matches in these trees are highlighted, but the trees are kept in
4567 :group
'org-sparse-trees
4570 (defcustom org-sparse-tree-default-date-type nil
4571 "The default date type when building a sparse tree.
4572 When this is nil, a date is a scheduled or a deadline timestamp.
4573 Otherwise, these types are allowed:
4576 active: only active timestamps (<...>)
4577 inactive: only inactive timestamps ([...])
4578 scheduled: only scheduled timestamps
4579 deadline: only deadline timestamps"
4580 :type
'(choice (const :tag
"Scheduled or deadline" nil
)
4581 (const :tag
"All timestamps" all
)
4582 (const :tag
"Only active timestamps" active
)
4583 (const :tag
"Only inactive timestamps" inactive
)
4584 (const :tag
"Only scheduled timestamps" scheduled
)
4585 (const :tag
"Only deadline timestamps" deadline
)
4586 (const :tag
"Only closed timestamps" closed
))
4588 :package-version
'(Org .
"8.3")
4589 :group
'org-sparse-trees
)
4591 (defun org-cycle-hide-archived-subtrees (state)
4592 "Re-hide all archived subtrees after a visibility state change.
4593 STATE should be one of the symbols listed in the docstring of
4595 (when (and (not org-cycle-open-archived-trees
)
4596 (not (memq state
'(overview folded
))))
4598 (let* ((globalp (memq state
'(contents all
)))
4599 (beg (if globalp
(point-min) (point)))
4600 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4601 (org-hide-archived-subtrees beg end
)
4603 (when (looking-at-p (concat ".*:" org-archive-tag
":"))
4604 (message "%s" (substitute-command-keys
4605 "Subtree is archived and stays closed. Use \
4606 `\\[org-force-cycle-archived]' to cycle it anyway.")))))))
4608 (defun org-force-cycle-archived ()
4609 "Cycle subtree even if it is archived."
4611 (setq this-command
'org-cycle
)
4612 (let ((org-cycle-open-archived-trees t
))
4613 (call-interactively 'org-cycle
)))
4615 (defun org-hide-archived-subtrees (beg end
)
4616 "Re-hide all archived subtrees after a visibility state change."
4617 (org-with-wide-buffer
4618 (let ((case-fold-search nil
)
4619 (re (concat org-outline-regexp-bol
".*:" org-archive-tag
":")))
4621 ;; Include headline point is currently on.
4623 (while (and (< (point) end
) (re-search-forward re end t
))
4624 (when (member org-archive-tag
(org-get-tags))
4625 (org-flag-subtree t
)
4626 (org-end-of-subtree t
))))))
4628 (defun org-flag-subtree (flag)
4630 (org-back-to-heading t
)
4631 (org-flag-region (line-end-position)
4632 (progn (org-end-of-subtree t
) (point))
4636 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4638 ;; Declare Column View Code
4640 (declare-function org-columns-get-format-and-top-level
"org-colview" ())
4641 (declare-function org-columns-compute
"org-colview" (property))
4645 (declare-function org-id-store-link
"org-id")
4646 (declare-function org-id-locations-load
"org-id")
4647 (declare-function org-id-locations-save
"org-id")
4648 (defvar org-id-track-globally
)
4650 ;;; Variables for pre-computed regular expressions, all buffer local
4652 (defvar-local org-todo-regexp nil
4653 "Matches any of the TODO state keywords.
4654 Since TODO keywords are case-sensitive, `case-fold-search' is
4655 expected to be bound to nil when matching against this regexp.")
4657 (defvar-local org-not-done-regexp nil
4658 "Matches any of the TODO state keywords except the last one.
4659 Since TODO keywords are case-sensitive, `case-fold-search' is
4660 expected to be bound to nil when matching against this regexp.")
4662 (defvar-local org-not-done-heading-regexp nil
4663 "Matches a TODO headline that is not done.
4664 Since TODO keywords are case-sensitive, `case-fold-search' is
4665 expected to be bound to nil when matching against this regexp.")
4667 (defvar-local org-todo-line-regexp nil
4668 "Matches a headline and puts TODO state into group 2 if present.
4669 Since TODO keywords are case-sensitive, `case-fold-search' is
4670 expected to be bound to nil when matching against this regexp.")
4672 (defvar-local org-complex-heading-regexp nil
4673 "Matches a headline and puts everything into groups:
4676 group 2: The TODO keyword, maybe
4677 group 3: Priority cookie
4678 group 4: True headline
4681 Since TODO keywords are case-sensitive, `case-fold-search' is
4682 expected to be bound to nil when matching against this regexp.")
4684 (defvar-local org-complex-heading-regexp-format nil
4685 "Printf format to make regexp to match an exact headline.
4686 This regexp will match the headline of any node which has the
4687 exact headline text that is put into the format, but may have any
4688 TODO state, priority and tags.")
4690 (defvar-local org-todo-line-tags-regexp nil
4691 "Matches a headline and puts TODO state into group 2 if present.
4692 Also put tags into group 4 if tags are present.")
4694 (defconst org-plain-time-of-day-regexp
4697 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4700 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4702 "Regular expression to match a plain time or time range.
4703 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4704 groups carry important information:
4706 1 the first time, range or not
4707 8 the second time, if it is a range.")
4709 (defconst org-plain-time-extension-regexp
4712 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4713 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4714 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4715 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4716 groups carry important information:
4719 9 minutes of duration")
4721 (defconst org-stamp-time-of-day-regexp
4723 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4724 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4726 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4727 "Regular expression to match a timestamp time or time range.
4728 After a match, the following groups carry important information:
4730 1 date plus weekday, for back referencing to make sure both times are on the same day
4731 2 the first time, range or not
4732 4 the second time, if it is a range.")
4734 (defconst org-startup-options
4735 '(("fold" org-startup-folded t
)
4736 ("overview" org-startup-folded t
)
4737 ("nofold" org-startup-folded nil
)
4738 ("showall" org-startup-folded nil
)
4739 ("showeverything" org-startup-folded showeverything
)
4740 ("content" org-startup-folded content
)
4741 ("indent" org-startup-indented t
)
4742 ("noindent" org-startup-indented nil
)
4743 ("hidestars" org-hide-leading-stars t
)
4744 ("showstars" org-hide-leading-stars nil
)
4745 ("odd" org-odd-levels-only t
)
4746 ("oddeven" org-odd-levels-only nil
)
4747 ("align" org-startup-align-all-tables t
)
4748 ("noalign" org-startup-align-all-tables nil
)
4749 ("shrink" org-startup-shrink-all-tables t
)
4750 ("inlineimages" org-startup-with-inline-images t
)
4751 ("noinlineimages" org-startup-with-inline-images nil
)
4752 ("latexpreview" org-startup-with-latex-preview t
)
4753 ("nolatexpreview" org-startup-with-latex-preview nil
)
4754 ("customtime" org-display-custom-times t
)
4755 ("logdone" org-log-done time
)
4756 ("lognotedone" org-log-done note
)
4757 ("nologdone" org-log-done nil
)
4758 ("lognoteclock-out" org-log-note-clock-out t
)
4759 ("nolognoteclock-out" org-log-note-clock-out nil
)
4760 ("logrepeat" org-log-repeat state
)
4761 ("lognoterepeat" org-log-repeat note
)
4762 ("logdrawer" org-log-into-drawer t
)
4763 ("nologdrawer" org-log-into-drawer nil
)
4764 ("logstatesreversed" org-log-states-order-reversed t
)
4765 ("nologstatesreversed" org-log-states-order-reversed nil
)
4766 ("nologrepeat" org-log-repeat nil
)
4767 ("logreschedule" org-log-reschedule time
)
4768 ("lognotereschedule" org-log-reschedule note
)
4769 ("nologreschedule" org-log-reschedule nil
)
4770 ("logredeadline" org-log-redeadline time
)
4771 ("lognoteredeadline" org-log-redeadline note
)
4772 ("nologredeadline" org-log-redeadline nil
)
4773 ("logrefile" org-log-refile time
)
4774 ("lognoterefile" org-log-refile note
)
4775 ("nologrefile" org-log-refile nil
)
4776 ("fninline" org-footnote-define-inline t
)
4777 ("nofninline" org-footnote-define-inline nil
)
4778 ("fnlocal" org-footnote-section nil
)
4779 ("fnauto" org-footnote-auto-label t
)
4780 ("fnprompt" org-footnote-auto-label nil
)
4781 ("fnconfirm" org-footnote-auto-label confirm
)
4782 ("fnplain" org-footnote-auto-label plain
)
4783 ("fnadjust" org-footnote-auto-adjust t
)
4784 ("nofnadjust" org-footnote-auto-adjust nil
)
4785 ("constcgs" constants-unit-system cgs
)
4786 ("constSI" constants-unit-system SI
)
4787 ("noptag" org-tag-persistent-alist nil
)
4788 ("hideblocks" org-hide-block-startup t
)
4789 ("nohideblocks" org-hide-block-startup nil
)
4790 ("beamer" org-startup-with-beamer-mode t
)
4791 ("entitiespretty" org-pretty-entities t
)
4792 ("entitiesplain" org-pretty-entities nil
))
4793 "Variable associated with STARTUP options for Org.
4794 Each element is a list of three items: the startup options (as written
4795 in the #+STARTUP line), the corresponding variable, and the value to set
4796 this variable to if the option is found. An optional forth element PUSH
4797 means to push this value onto the list in the variable.")
4799 (defcustom org-group-tags t
4800 "When non-nil (the default), use group tags.
4801 This can be turned on/off through `org-toggle-tags-groups'."
4806 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4808 (defun org-toggle-tags-groups ()
4809 "Toggle support for group tags.
4810 Support for group tags is controlled by the option
4811 `org-group-tags', which is non-nil by default."
4813 (setq org-group-tags
(not org-group-tags
))
4814 (cond ((and (derived-mode-p 'org-agenda-mode
)
4817 ((derived-mode-p 'org-mode
)
4818 (let ((org-inhibit-startup t
)) (org-mode))))
4819 (message "Groups tags support has been turned %s"
4820 (if org-group-tags
"on" "off")))
4822 (defun org-tag-add-to-alist (alist1 alist2
)
4823 "Append ALIST1 elements to ALIST2 if they are not there yet."
4825 ((null alist2
) alist1
)
4826 ((null alist1
) alist2
)
4827 (t (let ((alist2-cars (mapcar (lambda (x) (car-safe x
)) alist2
))
4830 (unless (member (car-safe i
) alist2-cars
)
4832 (append to-add alist2
)))))
4834 (defun org-set-regexps-and-options (&optional tags-only
)
4835 "Precompute regular expressions used in the current buffer.
4836 When optional argument TAGS-ONLY is non-nil, only compute tags
4837 related expressions."
4838 (when (derived-mode-p 'org-mode
)
4839 (let ((alist (org--setup-collect-keywords
4840 (org-make-options-regexp
4841 (append '("FILETAGS" "TAGS" "SETUPFILE")
4842 (and (not tags-only
)
4843 '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
4844 "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
4845 "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
4846 ;; Startup options. Get this early since it does change
4847 ;; behavior for other options (e.g., tags).
4848 (let ((startup (cdr (assq 'startup alist
))))
4849 (dolist (option startup
)
4850 (let ((entry (assoc-string option org-startup-options t
)))
4852 (let ((var (nth 1 entry
))
4853 (val (nth 2 entry
)))
4854 (if (not (nth 3 entry
)) (set (make-local-variable var
) val
)
4855 (unless (listp (symbol-value var
))
4856 (set (make-local-variable var
) nil
))
4857 (add-to-list var val
)))))))
4858 (setq-local org-file-tags
4859 (mapcar #'org-add-prop-inherited
4860 (cdr (assq 'filetags alist
))))
4861 (setq org-current-tag-alist
4862 (org-tag-add-to-alist
4863 org-tag-persistent-alist
4864 (let ((tags (cdr (assq 'tags alist
))))
4865 (if tags
(org-tag-string-to-alist tags
)
4867 (setq org-tag-groups-alist
4868 (org-tag-alist-to-groups org-current-tag-alist
))
4871 (setq-local org-file-properties
(cdr (assq 'property alist
)))
4872 ;; Archive location.
4873 (let ((archive (cdr (assq 'archive alist
))))
4874 (when archive
(setq-local org-archive-location archive
)))
4876 (let ((cat (org-string-nw-p (cdr (assq 'category alist
)))))
4878 (setq-local org-category
(intern cat
))
4879 (setq-local org-file-properties
4880 (org--update-property-plist
4881 "CATEGORY" cat org-file-properties
))))
4883 (let ((column (cdr (assq 'columns alist
))))
4884 (when column
(setq-local org-columns-default-format column
)))
4886 (setq org-table-formula-constants-local
(cdr (assq 'constants alist
)))
4887 ;; Link abbreviations.
4888 (let ((links (cdr (assq 'link alist
))))
4889 (when links
(setq org-link-abbrev-alist-local
(nreverse links
))))
4891 (let ((priorities (cdr (assq 'priorities alist
))))
4893 (setq-local org-highest-priority
(nth 0 priorities
))
4894 (setq-local org-lowest-priority
(nth 1 priorities
))
4895 (setq-local org-default-priority
(nth 2 priorities
))))
4897 (let ((scripts (assq 'scripts alist
)))
4899 (setq-local org-use-sub-superscripts
(cdr scripts
))))
4901 (setq-local org-todo-kwd-alist nil
)
4902 (setq-local org-todo-key-alist nil
)
4903 (setq-local org-todo-key-trigger nil
)
4904 (setq-local org-todo-keywords-1 nil
)
4905 (setq-local org-done-keywords nil
)
4906 (setq-local org-todo-heads nil
)
4907 (setq-local org-todo-sets nil
)
4908 (setq-local org-todo-log-states nil
)
4909 (let ((todo-sequences
4910 (or (nreverse (cdr (assq 'todo alist
)))
4911 (let ((d (default-value 'org-todo-keywords
)))
4912 (if (not (stringp (car d
))) d
4913 ;; XXX: Backward compatibility code.
4914 (list (cons org-todo-interpretation d
)))))))
4915 (dolist (sequence todo-sequences
)
4916 (let* ((sequence (or (run-hook-with-args-until-success
4917 'org-todo-setup-filter-hook sequence
)
4919 (sequence-type (car sequence
))
4920 (keywords (cdr sequence
))
4921 (sep (member "|" keywords
))
4923 (dolist (k (remove "|" keywords
))
4924 (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
4926 (error "Invalid TODO keyword %s" k
))
4927 (let ((name (match-string 1 k
))
4928 (key (match-string 2 k
))
4929 (log (org-extract-log-state-settings k
)))
4931 (push (cons name
(and key
(string-to-char key
))) alist
)
4932 (when log
(push log org-todo-log-states
))))
4933 (let* ((names (nreverse names
))
4934 (done (if sep
(org-remove-keyword-keys (cdr sep
))
4937 (tail (list sequence-type head
(car done
) (org-last done
))))
4938 (add-to-list 'org-todo-heads head
'append
)
4939 (push names org-todo-sets
)
4940 (setq org-done-keywords
(append org-done-keywords done nil
))
4941 (setq org-todo-keywords-1
(append org-todo-keywords-1 names nil
))
4942 (setq org-todo-key-alist
4943 (append org-todo-key-alist
4945 (append '((:startgroup
))
4948 (dolist (k names
) (push (cons k tail
) org-todo-kwd-alist
))))))
4949 (setq org-todo-sets
(nreverse org-todo-sets
)
4950 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4951 org-todo-key-trigger
(delq nil
(mapcar #'cdr org-todo-key-alist
))
4952 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
))
4953 ;; Compute the regular expressions and other local variables.
4954 ;; Using `org-outline-regexp-bol' would complicate them much,
4955 ;; because of the fixed white space at the end of that string.
4956 (unless org-done-keywords
4957 (setq org-done-keywords
4958 (and org-todo-keywords-1
(last org-todo-keywords-1
))))
4959 (setq org-not-done-keywords
4960 (org-delete-all org-done-keywords
4961 (copy-sequence org-todo-keywords-1
))
4962 org-todo-regexp
(regexp-opt org-todo-keywords-1 t
)
4963 org-not-done-regexp
(regexp-opt org-not-done-keywords t
)
4964 org-not-done-heading-regexp
4965 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4966 org-todo-line-regexp
4967 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4968 org-complex-heading-regexp
4969 (concat "^\\(\\*+\\)"
4970 "\\(?: +" org-todo-regexp
"\\)?"
4971 "\\(?: +\\(\\[#.\\]\\)\\)?"
4972 "\\(?: +\\(.*?\\)\\)??"
4973 "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?"
4975 org-complex-heading-regexp-format
4976 (concat "^\\(\\*+\\)"
4977 "\\(?: +" org-todo-regexp
"\\)?"
4978 "\\(?: +\\(\\[#.\\]\\)\\)?"
4980 ;; Stats cookies can be stuck to body.
4981 "\\(?:\\[[0-9%%/]+\\] *\\)*"
4983 "\\(?: *\\[[0-9%%/]+\\]\\)*"
4985 "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?"
4987 org-todo-line-tags-regexp
4988 (concat "^\\(\\*+\\)"
4989 "\\(?: +" org-todo-regexp
"\\)?"
4990 "\\(?: +\\(.*?\\)\\)??"
4991 "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?"
4993 (org-compute-latex-and-related-regexp)))))
4995 (defun org--setup-collect-keywords (regexp &optional files alist
)
4996 "Return setup keywords values as an alist.
4998 REGEXP matches a subset of setup keywords. FILES is a list of
4999 file names already visited. It is used to avoid circular setup
5000 files. ALIST, when non-nil, is the alist computed so far.
5002 Return value contains the following keys: `archive', `category',
5003 `columns', `constants', `filetags', `link', `priorities',
5004 `property', `scripts', `startup', `tags' and `todo'."
5005 (org-with-wide-buffer
5006 (goto-char (point-min))
5007 (let ((case-fold-search t
))
5008 (while (re-search-forward regexp nil t
)
5009 (let ((element (org-element-at-point)))
5010 (when (eq (org-element-type element
) 'keyword
)
5011 (let ((key (org-element-property :key element
))
5012 (value (org-element-property :value element
)))
5014 ((equal key
"ARCHIVE")
5015 (when (org-string-nw-p value
)
5016 (push (cons 'archive value
) alist
)))
5017 ((equal key
"CATEGORY") (push (cons 'category value
) alist
))
5018 ((equal key
"COLUMNS") (push (cons 'columns value
) alist
))
5019 ((equal key
"CONSTANTS")
5020 (let* ((constants (assq 'constants alist
))
5021 (store (cdr constants
)))
5022 (dolist (pair (split-string value
))
5023 (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
5025 (let* ((name (match-string 1 pair
))
5026 (value (match-string 2 pair
))
5027 (old (assoc name store
)))
5028 (if old
(setcdr old value
)
5029 (push (cons name value
) store
)))))
5030 (if constants
(setcdr constants store
)
5031 (push (cons 'constants store
) alist
))))
5032 ((equal key
"FILETAGS")
5033 (when (org-string-nw-p value
)
5034 (let ((old (assq 'filetags alist
))
5036 (mapcar (lambda (x) (org-split-string x
":"))
5037 (split-string value
)))))
5038 (if old
(setcdr old
(append new
(cdr old
)))
5039 (push (cons 'filetags new
) alist
)))))
5041 (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
5042 (let ((links (assq 'link alist
))
5043 (pair (cons (match-string-no-properties 1 value
)
5044 (match-string-no-properties 2 value
))))
5045 (if links
(push pair
(cdr links
))
5046 (push (list 'link pair
) alist
)))))
5047 ((equal key
"OPTIONS")
5048 (when (and (org-string-nw-p value
)
5049 (string-match "\\^:\\(t\\|nil\\|{}\\)" value
))
5050 (push (cons 'scripts
(read (match-string 1 value
))) alist
)))
5051 ((equal key
"PRIORITIES")
5052 (push (cons 'priorities
5053 (let ((prio (split-string value
)))
5054 (if (< (length prio
) 3) '(?A ?C ?B
)
5055 (mapcar #'string-to-char prio
))))
5057 ((equal key
"PROPERTY")
5058 (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value
)
5059 (let* ((property (assq 'property alist
))
5060 (value (org--update-property-plist
5061 (match-string-no-properties 1 value
)
5062 (match-string-no-properties 2 value
)
5064 (if property
(setcdr property value
)
5065 (push (cons 'property value
) alist
)))))
5066 ((equal key
"STARTUP")
5067 (let ((startup (assq 'startup alist
)))
5070 (append (cdr startup
) (split-string value
)))
5071 (push (cons 'startup
(split-string value
)) alist
))))
5073 (let ((tag-cell (assq 'tags alist
)))
5075 (setcdr tag-cell
(concat (cdr tag-cell
) "\n" value
))
5076 (push (cons 'tags value
) alist
))))
5077 ((member key
'("TODO" "SEQ_TODO" "TYP_TODO"))
5078 (let ((todo (assq 'todo alist
))
5079 (value (cons (if (equal key
"TYP_TODO") 'type
'sequence
)
5080 (split-string value
))))
5081 (if todo
(push value
(cdr todo
))
5082 (push (list 'todo value
) alist
))))
5083 ((equal key
"SETUPFILE")
5084 (unless buffer-read-only
; Do not check in Gnus messages.
5085 (let ((f (and (org-string-nw-p value
)
5087 (org-unbracket-string "\"" "\"" value
)))))
5088 (when (and f
(file-readable-p f
) (not (member f files
)))
5090 (setq default-directory
(file-name-directory f
))
5091 (insert-file-contents f
)
5093 ;; Fake Org mode to benefit from cache
5094 ;; without recurring needlessly.
5095 (let ((major-mode 'org-mode
))
5096 (org--setup-collect-keywords
5097 regexp
(cons f files
) alist
)))))))))))))))
5100 (defun org-tag-string-to-alist (s)
5101 "Return tag alist associated to string S.
5102 S is a value for TAGS keyword or produced with
5103 `org-tag-alist-to-string'. Return value is an alist suitable for
5104 `org-tag-alist' or `org-tag-persistent-alist'."
5105 (let ((lines (mapcar #'split-string
(split-string s
"\n" t
)))
5106 (tag-re (concat "\\`\\([[:alnum:]_@#%]+"
5107 "\\|{.+?}\\)" ; regular expression
5108 "\\(?:(\\(.\\))\\)?\\'"))
5110 (dolist (tokens lines
(cdr (nreverse alist
)))
5111 (push '(:newline
) alist
)
5113 (let ((token (pop tokens
)))
5116 (push '(:startgroup
) alist
)
5117 (when (equal (nth 1 tokens
) ":") (setq group-flag t
)))
5119 (push '(:endgroup
) alist
)
5120 (setq group-flag nil
))
5122 (push '(:startgrouptag
) alist
)
5123 (when (equal (nth 1 tokens
) ":") (setq group-flag t
)))
5125 (push '(:endgrouptag
) alist
)
5126 (setq group-flag nil
))
5128 (push '(:grouptags
) alist
))
5129 ((guard (string-match tag-re token
))
5130 (let ((tag (match-string 1 token
))
5131 (key (and (match-beginning 2)
5132 (string-to-char (match-string 2 token
)))))
5133 ;; Push all tags in groups, no matter if they already
5134 ;; appear somewhere else in the list.
5135 (when (or group-flag
(not (assoc tag alist
)))
5136 (push (cons tag key
) alist
))))))))))
5138 (defun org-tag-alist-to-string (alist &optional skip-key
)
5139 "Return tag string associated to ALIST.
5141 ALIST is an alist, as defined in `org-tag-alist' or
5142 `org-tag-persistent-alist', or produced with
5143 `org-tag-string-to-alist'.
5145 Return value is a string suitable as a value for \"TAGS\"
5148 When optional argument SKIP-KEY is non-nil, skip selection keys
5150 (mapconcat (lambda (token)
5152 (`(:startgroup
) "{")
5154 (`(:startgrouptag
) "[")
5155 (`(:endgrouptag
) "]")
5159 (guard (not skip-key
))
5160 `(,(and tag
(pred stringp
)) .
,(and key
(pred characterp
))))
5161 (format "%s(%c)" tag key
))
5162 (`(,(and tag
(pred stringp
)) .
,_
) tag
)
5163 (_ (user-error "Invalid tag token: %S" token
))))
5167 (defun org-tag-alist-to-groups (alist)
5168 "Return group alist from tag ALIST.
5169 ALIST is an alist, as defined in `org-tag-alist' or
5170 `org-tag-persistent-alist', or produced with
5171 `org-tag-string-to-alist'. Return value is an alist following
5172 the pattern (GROUP-TAG TAGS) where GROUP-TAG is the tag, as
5173 a string, summarizing TAGS, as a list of strings."
5174 (let (groups group-status current-group
)
5175 (dolist (token alist
(nreverse groups
))
5177 (`(,(or :startgroup
:startgrouptag
)) (setq group-status t
))
5178 (`(,(or :endgroup
:endgrouptag
))
5179 (when (eq group-status
'append
)
5180 (push (nreverse current-group
) groups
))
5181 (setq group-status nil current-group nil
))
5182 (`(:grouptags
) (setq group-status
'append
))
5183 ((and `(,tag .
,_
) (guard group-status
))
5184 (if (eq group-status
'append
) (push tag current-group
)
5185 (setq current-group
(list tag
))))
5188 (defvar org--file-cache
(make-hash-table :test
#'equal
)
5189 "Hash table to store contents of files referenced via a URL.
5190 This is the cache of file URLs read using `org-file-contents'.")
5192 (defun org-reset-file-cache ()
5193 "Reset the cache of files downloaded by `org-file-contents'."
5194 (clrhash org--file-cache
))
5196 (defun org-file-url-p (file)
5197 "Non-nil if FILE is a URL."
5199 (string-match-p ffap-url-regexp file
))
5201 (defun org-file-contents (file &optional noerror nocache
)
5202 "Return the contents of FILE, as a string.
5204 FILE can be a file name or URL.
5206 If FILE is a URL, download the contents. If the URL contents are
5207 already cached in the `org--file-cache' hash table, the download step
5210 If NOERROR is non-nil, ignore the error when unable to read the FILE
5213 If NOCACHE is non-nil, do a fresh fetch of FILE even if cached version
5214 is available. This option applies only if FILE is a URL."
5215 (let* ((is-url (org-file-url-p file
))
5218 (gethash file org--file-cache
))))
5222 (with-current-buffer (url-retrieve-synchronously file
)
5223 (goto-char (point-min))
5224 ;; Move point to after the url-retrieve header.
5225 (search-forward "\n\n" nil
:move
)
5226 ;; Search for the success code only in the url-retrieve header.
5228 (re-search-backward "HTTP.*\\s-+200\\s-OK" nil
:noerror
))
5229 ;; Update the cache `org--file-cache' and return contents.
5231 (buffer-substring-no-properties (point) (point-max))
5233 (funcall (if noerror
#'message
#'user-error
)
5234 "Unable to fetch file from %S"
5240 (insert-file-contents file
)
5243 (funcall (if noerror
#'message
#'user-error
)
5244 "Unable to read file %S"
5247 (defun org-extract-log-state-settings (x)
5248 "Extract the log state setting from a TODO keyword string.
5249 This will extract info from a string like \"WAIT(w@/!)\"."
5250 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
5251 (let ((kw (match-string 1 x
))
5252 (log1 (and (match-end 3) (match-string 3 x
)))
5253 (log2 (and (match-end 4) (match-string 4 x
))))
5256 (and log1
(if (equal log1
"!") 'time
'note
))
5257 (and log2
(if (equal log2
"!") 'time
'note
)))))))
5259 (defun org-remove-keyword-keys (list)
5260 "Remove a pair of parenthesis at the end of each string in LIST."
5262 (if (string-match "(.*)$" x
)
5263 (substring x
0 (match-beginning 0))
5267 (defun org-assign-fast-keys (alist)
5268 "Assign fast keys to a keyword-key alist.
5269 Respect keys that are already there."
5270 (let (new e
(alt ?
0))
5271 (while (setq e
(pop alist
))
5272 (if (or (memq (car e
) '(:newline
:grouptags
:endgroup
:startgroup
))
5273 (cdr e
)) ;; Key already assigned.
5275 (let ((clist (string-to-list (downcase (car e
))))
5276 (used (append new alist
)))
5277 (when (= (car clist
) ?
@)
5279 (while (and clist
(rassoc (car clist
) used
))
5282 (while (rassoc alt used
)
5284 (push (cons (car e
) (or (car clist
) alt
)) new
))))
5287 ;;; Some variables used in various places
5289 (defvar org-window-configuration nil
5290 "Used in various places to store a window configuration.")
5291 (defvar org-selected-window nil
5292 "Used in various places to store a window configuration.")
5293 (defvar org-finish-function nil
5294 "Function to be called when `C-c C-c' is used.
5295 This is for getting out of special buffers like capture.")
5296 (defvar org-last-state
)
5298 ;; Defined somewhere in this file, but used before definition.
5299 (defvar org-entities
) ;; defined in org-entities.el
5300 (defvar org-struct-menu
)
5301 (defvar org-org-menu
)
5302 (defvar org-tbl-menu
)
5304 ;;;; Define the Org mode
5306 ;; We use a before-change function to check if a table might need
5308 (defvar org-table-may-need-update t
5309 "Indicates that a table might need an update.
5310 This variable is set by `org-before-change-function'.
5311 `org-table-align' sets it back to nil.")
5312 (defun org-before-change-function (_beg _end
)
5313 "Every change indicates that a table might need an update."
5314 (setq org-table-may-need-update t
))
5315 (defvar org-mode-map
)
5316 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
5317 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
5318 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
5319 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
5320 (defvar org-table-buffer-is-an nil
)
5322 (defvar bidi-paragraph-direction
)
5323 (defvar buffer-face-mode-face
)
5327 ;; Other stuff we need.
5328 (require 'time-date
)
5329 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
5331 (autoload 'easy-menu-add
"easymenu")
5334 ;; (require 'org-macs) moved higher up in the file before it is first used
5335 (require 'org-entities
)
5336 ;; (require 'org-compat) moved higher up in the file before it is first used
5337 (require 'org-faces
)
5339 (require 'org-pcomplete
)
5341 (require 'org-footnote
)
5342 (require 'org-macro
)
5348 (define-derived-mode org-mode outline-mode
"Org"
5349 "Outline-based notes management and organizer, alias
5350 \"Carsten's outline-mode for keeping track of everything.\"
5352 Org mode develops organizational tasks around a NOTES file which
5353 contains information about projects as plain text. Org mode is
5354 implemented on top of Outline mode, which is ideal to keep the content
5355 of large files well structured. It supports ToDo items, deadlines and
5356 time stamps, which magically appear in the diary listing of the Emacs
5357 calendar. Tables are easily created with a built-in table editor.
5358 Plain text URL-like links connect to websites, emails (VM), Usenet
5359 messages (Gnus), BBDB entries, and any files related to the project.
5360 For printing and sharing of notes, an Org file (or a part of it)
5361 can be exported as a structured ASCII or HTML file.
5363 The following commands are available:
5367 ;; Get rid of Outline menus, they are not needed
5368 ;; Need to do this here because define-derived-mode sets up
5369 ;; the keymap so late. Still, it is a waste to call this each time
5370 ;; we switch another buffer into Org mode.
5371 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
5372 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
5373 (define-key org-mode-map
[menu-bar show
] 'undefined
)
5375 (org-load-modules-maybe)
5376 (org-install-agenda-files-menu)
5377 (when org-descriptive-links
(add-to-invisibility-spec '(org-link)))
5378 (add-to-invisibility-spec '(org-hide-block . t
))
5379 (add-to-invisibility-spec '(org-hide-drawer . t
))
5380 (setq-local outline-regexp org-outline-regexp
)
5381 (setq-local outline-level
'org-outline-level
)
5382 (setq bidi-paragraph-direction
'left-to-right
)
5383 (when (and (stringp org-ellipsis
) (not (equal "" org-ellipsis
)))
5384 (unless org-display-table
5385 (setq org-display-table
(make-display-table)))
5386 (set-display-table-slot
5388 (vconcat (mapcar (lambda (c) (make-glyph-code c
'org-ellipsis
))
5390 (setq buffer-display-table org-display-table
))
5391 (org-set-regexps-and-options)
5392 (org-set-font-lock-defaults)
5393 (when (and org-tag-faces
(not org-tags-special-faces-re
))
5394 ;; tag faces set outside customize.... force initialization.
5395 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
5397 (setq-local calc-embedded-open-mode
"# ")
5398 ;; Modify a few syntax entries
5399 (modify-syntax-entry ?
@ "w")
5400 (modify-syntax-entry ?
\" "\"")
5401 (modify-syntax-entry ?
\\ "_")
5402 (modify-syntax-entry ?~
"_")
5403 (setq-local font-lock-unfontify-region-function
'org-unfontify-region
)
5404 ;; Activate before-change-function
5405 (setq-local org-table-may-need-update t
)
5406 (add-hook 'before-change-functions
'org-before-change-function nil
'local
)
5407 ;; Check for running clock before killing a buffer
5408 (add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5409 ;; Initialize macros templates.
5410 (org-macro-initialize-templates)
5411 ;; Initialize radio targets.
5412 (org-update-radio-target-regexp)
5414 (setq-local indent-line-function
'org-indent-line
)
5415 (setq-local indent-region-function
'org-indent-region
)
5416 ;; Filling and auto-filling.
5419 (org-setup-comments-handling)
5420 ;; Initialize cache.
5421 (org-element-cache-reset)
5422 ;; Beginning/end of defun
5423 (setq-local beginning-of-defun-function
'org-backward-element
)
5424 (setq-local end-of-defun-function
5426 (if (not (org-at-heading-p))
5427 (org-forward-element)
5428 (org-forward-element)
5429 (forward-char -
1))))
5430 ;; Next error for sparse trees
5431 (setq-local next-error-function
'org-occur-next-match
)
5432 ;; Make sure dependence stuff works reliably, even for users who set it
5434 (if org-enforce-todo-dependencies
5435 (add-hook 'org-blocker-hook
5436 'org-block-todo-from-children-or-siblings-or-parent
)
5437 (remove-hook 'org-blocker-hook
5438 'org-block-todo-from-children-or-siblings-or-parent
))
5439 (if org-enforce-todo-checkbox-dependencies
5440 (add-hook 'org-blocker-hook
5441 'org-block-todo-from-checkboxes
)
5442 (remove-hook 'org-blocker-hook
5443 'org-block-todo-from-checkboxes
))
5445 ;; Align options lines
5447 align-mode-rules-list
5448 '((org-in-buffer-settings
5449 (regexp .
"^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5450 (modes .
'(org-mode)))))
5453 (setq-local imenu-create-index-function
'org-imenu-get-tree
)
5455 ;; Make isearch reveal context
5456 (setq-local outline-isearch-open-invisible-function
5457 (lambda (&rest _
) (org-show-context 'isearch
)))
5459 ;; Setup the pcomplete hooks
5460 (setq-local pcomplete-command-completion-function
'org-pcomplete-initial
)
5461 (setq-local pcomplete-command-name-function
'org-command-at-point
)
5462 (setq-local pcomplete-default-completion-function
'ignore
)
5463 (setq-local pcomplete-parse-arguments-function
'org-parse-arguments
)
5464 (setq-local pcomplete-termination-string
"")
5465 (setq-local buffer-face-mode-face
'org-default
)
5467 ;; If empty file that did not turn on Org mode automatically, make
5469 (when (and org-insert-mode-line-in-empty-file
5470 (called-interactively-p 'any
)
5471 (= (point-min) (point-max)))
5472 (insert "# -*- mode: org -*-\n\n"))
5473 (unless org-inhibit-startup
5475 (when org-startup-with-beamer-mode
(org-beamer-mode))
5476 (when (or org-startup-align-all-tables org-startup-shrink-all-tables
)
5477 (org-table-map-tables
5478 (cond ((and org-startup-align-all-tables
5479 org-startup-shrink-all-tables
)
5480 (lambda () (org-table-align) (org-table-shrink)))
5481 (org-startup-align-all-tables #'org-table-align
)
5482 (t #'org-table-shrink
))
5484 (when org-startup-with-inline-images
(org-display-inline-images))
5485 (when org-startup-with-latex-preview
(org-toggle-latex-fragment '(16)))
5486 (unless org-inhibit-startup-visibility-stuff
(org-set-startup-visibility))
5487 (when org-startup-truncated
(setq truncate-lines t
))
5488 (when org-startup-indented
(require 'org-indent
) (org-indent-mode 1))
5489 (org-refresh-effort-properties)))
5490 ;; Try to set `org-hide' face correctly.
5491 (let ((foreground (org-find-invisible-foreground)))
5493 (set-face-foreground 'org-hide foreground
))))
5495 ;; Update `customize-package-emacs-version-alist'
5496 (add-to-list 'customize-package-emacs-version-alist
5497 '(Org ("8.0" .
"24.4")
5506 (defvar org-mode-transpose-word-syntax-table
5507 (let ((st (make-syntax-table text-mode-syntax-table
)))
5508 (dolist (c org-emphasis-alist st
)
5509 (modify-syntax-entry (string-to-char (car c
)) "w p" st
))))
5511 (when (fboundp 'abbrev-table-put
)
5512 (abbrev-table-put org-mode-abbrev-table
5513 :parents
(list text-mode-abbrev-table
)))
5515 (defun org-find-invisible-foreground ()
5516 (let ((candidates (remove
5519 (list (face-background 'default
)
5520 (face-background 'org-default
))
5523 (when (boundp alist
)
5524 (cdr (assq 'background-color
(symbol-value alist
)))))
5525 '(default-frame-alist initial-frame-alist window-system-default-frame-alist
))
5526 (list (face-foreground 'org-hide
))))))
5527 (car (remove nil candidates
))))
5529 (defun org-current-time (&optional rounding-minutes past
)
5530 "Current time, possibly rounded to ROUNDING-MINUTES.
5531 When ROUNDING-MINUTES is not an integer, fall back on the car of
5532 `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
5533 the rounding returns a past time."
5534 (let ((r (or (and (integerp rounding-minutes
) rounding-minutes
)
5535 (car org-time-stamp-rounding-minutes
)))
5536 (time (decode-time)) res
)
5541 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5543 (if (and past
(< (float-time (time-subtract (current-time) res
)) 0))
5544 (seconds-to-time (- (float-time res
) (* r
60)))
5548 "Return today date, considering `org-extend-today-until'."
5550 (time-subtract (current-time)
5551 (list 0 (* 3600 org-extend-today-until
) 0))))
5553 ;;;; Font-Lock stuff, including the activators
5555 (defvar org-mouse-map
(make-sparse-keymap))
5556 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5557 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5558 (when org-mouse-1-follows-link
5559 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5560 (when org-tab-follows-link
5561 (org-defkey org-mouse-map
(kbd "<tab>") #'org-open-at-point
)
5562 (org-defkey org-mouse-map
(kbd "TAB") #'org-open-at-point
))
5564 (require 'font-lock
)
5566 (defconst org-non-link-chars
"]\t\n\r<>")
5567 (defvar org-link-types-re nil
5568 "Matches a link that has a url-like prefix like \"http:\"")
5569 (defvar org-link-re-with-space nil
5570 "Matches a link with spaces, optional angular brackets around it.")
5571 (defvar org-link-re-with-space2 nil
5572 "Matches a link with spaces, optional angular brackets around it.")
5573 (defvar org-link-re-with-space3 nil
5574 "Matches a link with spaces, only for internal part in bracket links.")
5575 (defvar org-angle-link-re nil
5576 "Matches link with angular brackets, spaces are allowed.")
5577 (defvar org-plain-link-re nil
5578 "Matches plain link, without spaces.")
5579 (defvar org-bracket-link-regexp nil
5580 "Matches a link in double brackets.")
5581 (defvar org-bracket-link-analytic-regexp nil
5582 "Regular expression used to analyze links.
5583 Here is what the match groups contain after a match:
5589 (defvar org-bracket-link-analytic-regexp
++ nil
5590 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5591 (defvar org-any-link-re nil
5592 "Regular expression matching any link.")
5594 (defconst org-match-sexp-depth
3
5595 "Number of stacked braces for sub/superscript matching.")
5597 (defun org-create-multibrace-regexp (left right n
)
5598 "Create a regular expression which will match a balanced sexp.
5599 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5600 as single character strings.
5601 The regexp returned will match the entire expression including the
5602 delimiters. It will also define a single group which contains the
5603 match except for the outermost delimiters. The maximum depth of
5604 stacked delimiters is N. Escaping delimiters is not possible."
5605 (let* ((nothing (concat "[^" left right
"]*?"))
5608 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5611 re
(concat re or next
)
5612 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5613 (concat left
"\\(" re
"\\)" right
)))
5615 (defconst org-match-substring-regexp
5617 "\\(\\S-\\)\\([_^]\\)\\("
5618 "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5620 "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5622 "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
5623 "The regular expression matching a sub- or superscript.")
5625 (defconst org-match-substring-with-braces-regexp
5627 "\\(\\S-\\)\\([_^]\\)"
5628 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)")
5629 "The regular expression matching a sub- or superscript, forcing braces.")
5631 (defun org-make-link-regexps ()
5632 "Update the link regular expressions.
5633 This should be called after the variable `org-link-parameters' has changed."
5634 (let ((types-re (regexp-opt (org-link-types) t
)))
5635 (setq org-link-types-re
5636 (concat "\\`" types-re
":")
5637 org-link-re-with-space
5638 (concat "<?" types-re
":"
5639 "\\([^" org-non-link-chars
" ]"
5640 "[^" org-non-link-chars
"]*"
5641 "[^" org-non-link-chars
" ]\\)>?")
5642 org-link-re-with-space2
5643 (concat "<?" types-re
":"
5644 "\\([^" org-non-link-chars
" ]"
5646 "[^" org-non-link-chars
" ]\\)>?")
5647 org-link-re-with-space3
5648 (concat "<?" types-re
":"
5649 "\\([^" org-non-link-chars
" ]"
5652 (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
5657 "\\([^][ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)")
5658 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5659 org-bracket-link-regexp
5660 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5661 org-bracket-link-analytic-regexp
5664 "\\(" types-re
":\\)?"
5667 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5669 org-bracket-link-analytic-regexp
++
5672 "\\(" (regexp-opt (cons "coderef" (org-link-types)) t
) ":\\)?"
5675 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5678 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5679 org-angle-link-re
"\\)\\|\\("
5680 org-plain-link-re
"\\)"))))
5682 (org-make-link-regexps)
5684 (defvar org-emph-face nil
)
5686 (defun org-do-emphasis-faces (limit)
5687 "Run through the buffer and emphasize strings."
5688 (let ((quick-re (format "\\([%s]\\|^\\)\\([~=*/_+]\\)"
5689 (car org-emphasis-regexp-components
))))
5691 (while (re-search-forward quick-re limit t
)
5692 (let* ((marker (match-string 2))
5693 (verbatim?
(member marker
'("~" "="))))
5694 (when (save-excursion
5695 (goto-char (match-beginning 0))
5697 ;; Do not match table hlines.
5698 (not (and (equal marker
"+")
5700 "[ \t]*\\(|[-+]+|?\\|\\+[-+]+\\+\\)[ \t]*$")))
5701 ;; Do not match headline stars. Do not consider
5702 ;; stars of a headline as closing marker for bold
5704 (not (and (equal marker
"*")
5707 (skip-chars-backward "*")
5708 (looking-at-p org-outline-regexp-bol
))))
5709 ;; Match full emphasis markup regexp.
5710 (looking-at (if verbatim? org-verbatim-re org-emph-re
))
5711 ;; Do not span over paragraph boundaries.
5712 (not (string-match-p org-element-paragraph-separate
5714 ;; Do not span over cells in table rows.
5715 (not (and (save-match-data (org-match-line "[ \t]*|"))
5716 (string-match-p "|" (match-string 4))))))
5717 (pcase-let ((`(,_
,face
,_
) (assoc marker org-emphasis-alist
)))
5718 (font-lock-prepend-text-property
5719 (match-beginning 2) (match-end 2) 'face face
)
5721 (org-remove-flyspell-overlays-in
5722 (match-beginning 0) (match-end 0)))
5723 (add-text-properties (match-beginning 2) (match-end 2)
5724 '(font-lock-multiline t org-emphasis t
))
5725 (when org-hide-emphasis-markers
5726 (add-text-properties (match-end 4) (match-beginning 5)
5727 '(invisible org-link
))
5728 (add-text-properties (match-beginning 3) (match-end 3)
5729 '(invisible org-link
)))
5730 (throw :exit t
))))))))
5732 (defun org-emphasize (&optional char
)
5733 "Insert or change an emphasis, i.e. a font like bold or italic.
5734 If there is an active region, change that region to a new emphasis.
5735 If there is no region, just insert the marker characters and position
5736 the cursor between them.
5737 CHAR should be the marker character. If it is a space, it means to
5738 remove the emphasis of the selected region.
5739 If CHAR is not given (for example in an interactive call) it will be
5742 (let ((erc org-emphasis-regexp-components
)
5743 (string "") beg end move s
)
5744 (if (org-region-active-p)
5745 (setq beg
(region-beginning)
5747 string
(buffer-substring beg end
))
5751 (message "Emphasis marker or tag: [%s]"
5752 (mapconcat #'car org-emphasis-alist
""))
5753 (setq char
(read-char-exclusive)))
5754 (if (equal char ?\s
)
5757 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5758 (user-error "No such emphasis marker: \"%c\"" char
))
5759 (setq s
(char-to-string char
)))
5760 (while (and (> (length string
) 1)
5761 (equal (substring string
0 1) (substring string -
1))
5762 (assoc (substring string
0 1) org-emphasis-alist
))
5763 (setq string
(substring string
1 -
1)))
5764 (setq string
(concat s string s
))
5765 (when beg
(delete-region beg end
))
5767 (string-match (concat "[" (nth 0 erc
) "\n]")
5768 (char-to-string (char-before (point)))))
5771 (string-match (concat "[" (nth 1 erc
) "\n]")
5772 (char-to-string (char-after (point)))))
5773 (insert " ") (backward-char 1))
5775 (and move
(backward-char 1))))
5777 (defconst org-nonsticky-props
5778 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link
))
5780 (defsubst org-rear-nonsticky-at
(pos)
5781 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5783 (defun org-activate-links (limit)
5784 "Add link properties to links.
5785 This includes angle, plain, and bracket links."
5787 (while (re-search-forward org-any-link-re limit t
)
5788 (let* ((start (match-beginning 0))
5790 (style (cond ((eq ?
< (char-after start
)) 'angle
)
5791 ((eq ?\
[ (char-after (1+ start
))) 'bracket
)
5793 (when (and (memq style org-highlight-links
)
5794 ;; Do not confuse plain links with tags.
5795 (not (and (eq style
'plain
)
5796 (let ((face (get-text-property
5797 (max (1- start
) (point-min)) 'face
)))
5798 (if (consp face
) (memq 'org-tag face
)
5799 (eq 'org-tag face
))))))
5800 (let* ((link-object (save-excursion
5802 (save-match-data (org-element-link-parser))))
5803 (link (org-element-property :raw-link link-object
))
5804 (type (org-element-property :type link-object
))
5805 (path (org-element-property :path link-object
))
5806 (properties ;for link's visible part
5808 'face
(pcase (org-link-get-parameter type
:face
)
5809 ((and (pred functionp
) face
) (funcall face path
))
5810 ((and (pred facep
) face
) face
)
5811 ((and (pred consp
) face
) face
) ;anonymous
5813 'mouse-face
(or (org-link-get-parameter type
:mouse-face
)
5815 'keymap
(or (org-link-get-parameter type
:keymap
)
5817 'help-echo
(pcase (org-link-get-parameter type
:help-echo
)
5818 ((and (pred stringp
) echo
) echo
)
5819 ((and (pred functionp
) echo
) echo
)
5820 (_ (concat "LINK: " link
)))
5821 'htmlize-link
(pcase (org-link-get-parameter type
5823 ((and (pred functionp
) f
) (funcall f
))
5825 'font-lock-multiline t
)))
5826 (org-remove-flyspell-overlays-in start end
)
5827 (org-rear-nonsticky-at end
)
5828 (if (not (eq 'bracket style
))
5829 (add-text-properties start end properties
)
5830 ;; Handle invisible parts in bracket links.
5831 (remove-text-properties start end
'(invisible nil
))
5834 ,(or (org-link-get-parameter type
:display
)
5837 (visible-start (or (match-beginning 4) (match-beginning 2)))
5838 (visible-end (or (match-end 4) (match-end 2))))
5839 (add-text-properties start visible-start hidden
)
5840 (add-text-properties visible-start visible-end properties
)
5841 (add-text-properties visible-end end hidden
)
5842 (org-rear-nonsticky-at visible-start
)
5843 (org-rear-nonsticky-at visible-end
)))
5844 (let ((f (org-link-get-parameter type
:activate-func
)))
5846 (funcall f start end path
(eq style
'bracket
))))
5847 (throw :exit t
))))) ;signal success
5850 (defun org-activate-code (limit)
5851 (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5852 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5853 (remove-text-properties (match-beginning 0) (match-end 0)
5854 '(display t invisible t intangible t
))
5857 (defcustom org-src-fontify-natively t
5858 "When non-nil, fontify code in code blocks.
5859 See also the `org-block' face."
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 in %S at %d"
5879 (line-number-at-pos)))))
5881 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5882 "Fontify #+ lines and blocks."
5883 (let ((case-fold-search t
))
5884 (when (re-search-forward
5885 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5887 (let ((beg (match-beginning 0))
5888 (block-start (match-end 0))
5890 (lang (match-string 7))
5891 (beg1 (line-beginning-position 2))
5892 (dc1 (downcase (match-string 2)))
5893 (dc3 (downcase (match-string 3)))
5894 end end1 quoting block-type
)
5896 ((and (match-end 4) (equal dc3
"+begin"))
5898 (setq block-type
(downcase (match-string 5))
5899 quoting
(member block-type org-protecting-blocks
))
5900 (when (re-search-forward
5901 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5902 nil t
) ;; on purpose, we look further than LIMIT
5903 (setq end
(min (point-max) (match-end 0))
5904 end1
(min (point-max) (1- (match-beginning 0))))
5905 (setq block-end
(match-beginning 0))
5907 (org-remove-flyspell-overlays-in beg1 end1
)
5908 (remove-text-properties beg end
5909 '(display t invisible t intangible t
)))
5910 (add-text-properties
5911 beg end
'(font-lock-fontified t font-lock-multiline t
))
5912 (add-text-properties beg beg1
'(face org-meta-line
))
5913 (org-remove-flyspell-overlays-in beg beg1
)
5914 (add-text-properties ; For end_src
5915 end1
(min (point-max) (1+ end
)) '(face org-meta-line
))
5916 (org-remove-flyspell-overlays-in end1 end
)
5918 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5919 (org-src-font-lock-fontify-block lang block-start block-end
)
5920 (add-text-properties beg1 block-end
'(src-block t
)))
5922 (add-text-properties beg1
(min (point-max) (1+ end1
))
5926 (intern (format "org-block-%s" lang
))))
5927 (append (and (facep face-name
) (list face-name
))
5928 '(org-block))))))) ; end of source block
5929 ((not org-fontify-quote-and-verse-blocks
))
5930 ((string= block-type
"quote")
5931 (add-face-text-property
5932 beg1
(min (point-max) (1+ end1
)) 'org-quote t
))
5933 ((string= block-type
"verse")
5934 (add-face-text-property
5935 beg1
(min (point-max) (1+ end1
)) 'org-verse t
)))
5936 (add-text-properties beg beg1
'(face org-block-begin-line
))
5937 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5938 '(face org-block-end-line
))
5940 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5941 (org-remove-flyspell-overlays-in
5943 (if (equal "+title:" dc1
) (match-end 2) (match-end 0)))
5944 (add-text-properties
5946 (if (member (intern (substring dc1
1 -
1)) org-hidden-keywords
)
5947 '(font-lock-fontified t invisible t
)
5948 '(font-lock-fontified t face org-document-info-keyword
)))
5949 (add-text-properties
5950 (match-beginning 6) (min (point-max) (1+ (match-end 6)))
5951 (if (string-equal dc1
"+title:")
5952 '(font-lock-fontified t face org-document-title
)
5953 '(font-lock-fontified t face org-document-info
))))
5954 ((string-prefix-p "+caption" dc1
)
5955 (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
5956 (remove-text-properties (match-beginning 0) (match-end 0)
5957 '(display t invisible t intangible t
))
5958 ;; Handle short captions.
5961 (looking-at "\\([ \t]*#\\+caption\\(?:\\[.*\\]\\)?:\\)[ \t]*"))
5962 (add-text-properties (line-beginning-position) (match-end 1)
5963 '(font-lock-fontified t face org-meta-line
))
5964 (add-text-properties (match-end 0) (line-end-position)
5965 '(font-lock-fontified t face org-block
))
5967 ((member dc3
'(" " ""))
5968 (org-remove-flyspell-overlays-in beg
(match-end 0))
5969 (add-text-properties
5971 '(font-lock-fontified t face font-lock-comment-face
)))
5972 (t ;; just any other in-buffer setting, but not indented
5973 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5974 (remove-text-properties (match-beginning 0) (match-end 0)
5975 '(display t invisible t intangible t
))
5976 (add-text-properties beg
(match-end 0)
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 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)" limit t
)
5992 (let ((begin (match-beginning 0))
5993 (opening-end (match-beginning 1)))
5994 (when (and (re-search-forward "\n[ \t]*\n\\|\\(}}}\\)" limit t
)
5996 (let ((end (match-end 1))
5997 (closing-start (match-beginning 1)))
5998 (add-text-properties
6000 '(font-lock-multiline t font-lock-fontified t face org-macro
))
6001 (org-remove-flyspell-overlays-in begin end
)
6002 (when org-hide-macro-markers
6003 (add-text-properties begin opening-end
'(invisible t
))
6004 (add-text-properties closing-start end
'(invisible t
)))
6007 (defun org-activate-footnote-links (limit)
6008 "Add text properties for footnotes."
6009 (let ((fn (org-footnote-next-reference-or-definition limit
)))
6011 (let* ((beg (nth 1 fn
))
6014 (referencep (/= (line-beginning-position) beg
)))
6015 (when (and referencep
(nth 3 fn
))
6018 (search-forward (or label
"fn:"))
6019 (org-remove-flyspell-overlays-in beg
(match-end 0))))
6020 (add-text-properties beg end
6021 (list 'mouse-face
'highlight
6022 'keymap org-mouse-map
6024 (if referencep
"Footnote reference"
6025 "Footnote definition")
6026 'font-lock-fontified t
6027 'font-lock-multiline t
6028 'face
'org-footnote
))))))
6030 (defun org-activate-dates (limit)
6031 "Add text properties for dates."
6032 (when (and (re-search-forward org-tsr-regexp-both limit t
)
6033 (not (equal (char-before (match-beginning 0)) 91)))
6034 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
6035 (add-text-properties (match-beginning 0) (match-end 0)
6036 (list 'mouse-face
'highlight
6037 'keymap org-mouse-map
))
6038 (org-rear-nonsticky-at (match-end 0))
6039 (when org-display-custom-times
6040 ;; If it's a date range, activate custom time for second date.
6042 (org-display-custom-time (match-beginning 3) (match-end 3)))
6043 (org-display-custom-time (match-beginning 1) (match-end 1)))
6046 (defvar-local org-target-link-regexp nil
6047 "Regular expression matching radio targets in plain text.")
6049 (defconst org-target-regexp
(let ((border "[^<>\n\r \t]"))
6050 (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
6051 border border border
))
6052 "Regular expression matching a link target.")
6054 (defconst org-radio-target-regexp
(format "<%s>" org-target-regexp
)
6055 "Regular expression matching a radio target.")
6057 (defconst org-any-target-regexp
6058 (format "%s\\|%s" org-radio-target-regexp org-target-regexp
)
6059 "Regular expression matching any target.")
6061 (defun org-activate-target-links (limit)
6062 "Add text properties for target matches."
6063 (when org-target-link-regexp
6064 (let ((case-fold-search t
))
6065 (when (re-search-forward org-target-link-regexp limit t
)
6066 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
6067 (add-text-properties (match-beginning 1) (match-end 1)
6068 (list 'mouse-face
'highlight
6069 'keymap org-mouse-map
6070 'help-echo
"Radio target link"
6071 'org-linked-text t
))
6072 (org-rear-nonsticky-at (match-end 1))
6075 (defun org-update-radio-target-regexp ()
6076 "Find all radio targets in this file and update the regular expression.
6077 Also refresh fontification if needed."
6079 (let ((old-regexp org-target-link-regexp
)
6080 (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
6081 (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
6083 (org-with-wide-buffer
6084 (goto-char (point-min))
6086 (while (re-search-forward org-radio-target-regexp nil t
)
6087 ;; Make sure point is really within the object.
6089 (let ((obj (org-element-context)))
6090 (when (eq (org-element-type obj
) 'radio-target
)
6091 (cl-pushnew (org-element-property :value obj
) rtn
6094 (setq org-target-link-regexp
6099 (replace-regexp-in-string
6100 " +" "\\s-+" (regexp-quote x
) t t
))
6104 (unless (equal old-regexp org-target-link-regexp
)
6106 (let ((regexp (cond ((not old-regexp
) org-target-link-regexp
)
6107 ((not org-target-link-regexp
) old-regexp
)
6112 (substring re
(length before-re
)
6113 (- (length after-re
))))
6114 (list old-regexp org-target-link-regexp
)
6117 (org-with-wide-buffer
6118 (goto-char (point-min))
6119 (while (re-search-forward regexp nil t
)
6120 (org-element-cache-refresh (match-beginning 1)))))
6121 ;; Re fontify buffer.
6122 (when (memq 'radio org-highlight-links
)
6123 (org-restart-font-lock)))))
6125 (defvar org-latex-and-related-regexp nil
6126 "Regular expression for highlighting LaTeX, entities and sub/superscript.")
6128 (defun org-compute-latex-and-related-regexp ()
6129 "Compute regular expression for LaTeX, entities and sub/superscript.
6130 Result depends on variable `org-highlight-latex-and-related'."
6132 org-latex-and-related-regexp
6134 (cond ((not (memq 'script org-highlight-latex-and-related
)) nil
)
6135 ((eq org-use-sub-superscripts
'{})
6136 (list org-match-substring-with-braces-regexp
))
6137 (org-use-sub-superscripts (list org-match-substring-regexp
))))
6139 (when (memq 'latex org-highlight-latex-and-related
)
6140 (let ((matchers (plist-get org-format-latex-options
:matchers
)))
6143 (and (member (car x
) matchers
) (nth 1 x
)))
6144 org-latex-regexps
)))))
6146 (when (memq 'entities org-highlight-latex-and-related
)
6147 (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
6148 (mapconcat 'identity
(append re-latex re-entities re-sub
) "\\|"))))
6150 (defun org-do-latex-and-related (limit)
6151 "Highlight LaTeX snippets and environments, entities and sub/superscript.
6152 LIMIT bounds the search for syntax to highlight. Stop at first
6153 highlighted object, if any. Return t if some highlighting was
6154 done, nil otherwise."
6155 (when (org-string-nw-p org-latex-and-related-regexp
)
6157 (while (re-search-forward org-latex-and-related-regexp limit t
)
6161 (memq f
'(org-code org-verbatim underline org-special-keyword
)))
6163 (goto-char (1+ (match-beginning 0)))
6164 (face-at-point nil t
)))
6165 (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
6169 (font-lock-prepend-text-property
6170 (+ offset
(match-beginning 0)) (match-end 0)
6171 'face
'org-latex-and-related
)
6172 (add-text-properties (+ offset
(match-beginning 0)) (match-end 0)
6173 '(font-lock-multiline t
)))
6177 (defun org-restart-font-lock ()
6178 "Restart `font-lock-mode', to force refontification."
6179 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
6181 (font-lock-mode 1)))
6183 (defun org-activate-tags (limit)
6184 (when (re-search-forward
6185 "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" limit t
)
6186 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
6187 (add-text-properties (match-beginning 1) (match-end 1)
6188 (list 'mouse-face
'highlight
6189 'keymap org-mouse-map
))
6190 (org-rear-nonsticky-at (match-end 1))
6193 (defun org-outline-level ()
6194 "Compute the outline level of the heading at point.
6196 If this is called at a normal headline, the level is the number
6197 of stars. Use `org-reduced-level' to remove the effect of
6198 `org-odd-levels'. Unlike to `org-current-level', this function
6199 takes into consideration inlinetasks."
6200 (org-with-wide-buffer
6202 (if (re-search-backward org-outline-regexp-bol nil t
)
6203 (1- (- (match-end 0) (match-beginning 0)))
6206 (defvar org-font-lock-keywords nil
)
6208 (defsubst org-re-property
(property &optional literal allow-null value
)
6209 "Return a regexp matching a PROPERTY line.
6211 When optional argument LITERAL is non-nil, do not quote PROPERTY.
6212 This is useful when PROPERTY is a regexp. When ALLOW-NULL is
6213 non-nil, match properties even without a value.
6215 Match group 3 is set to the value when it exists. If there is no
6216 value and ALLOW-NULL is non-nil, it is set to the empty string.
6218 With optional argument VALUE, match only property lines with
6219 that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
6220 unless LITERAL is non-nil."
6223 (format "\\(?1::\\(?2:%s\\):\\)"
6224 (if literal property
(regexp-quote property
)))
6226 (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
6227 (if literal value
(regexp-quote value
))))
6229 "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
6231 "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
6233 (defconst org-property-re
6234 (org-re-property "\\S-+" 'literal t
)
6235 "Regular expression matching a property line.
6236 There are four matching groups:
6237 1: :PROPKEY: including the leading and trailing colon,
6238 2: PROPKEY without the leading and trailing colon,
6239 3: PROPVAL without leading or trailing spaces,
6240 4: the indentation of the current line,
6241 5: trailing whitespace.")
6243 (defvar org-font-lock-hook nil
6244 "Functions to be called for special font lock stuff.")
6246 (defvar org-font-lock-extra-keywords nil
) ;Dynamically scoped.
6248 (defvar org-font-lock-set-keywords-hook nil
6249 "Functions that can manipulate `org-font-lock-extra-keywords'.
6250 This is called after `org-font-lock-extra-keywords' is defined, but before
6251 it is installed to be used by font lock. This can be useful if something
6252 needs to be inserted at a specific position in the font-lock sequence.")
6254 (defun org-font-lock-hook (limit)
6255 "Run `org-font-lock-hook' within LIMIT."
6256 (run-hook-with-args 'org-font-lock-hook limit
))
6258 (defun org-set-font-lock-defaults ()
6259 "Set font lock defaults for the current buffer."
6260 (let* ((em org-fontify-emphasized-text
)
6261 (lk org-highlight-links
)
6262 (org-font-lock-extra-keywords
6265 '(org-font-lock-hook)
6267 `(,(if org-fontify-whole-heading-line
6268 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
6269 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
6270 (1 (org-get-level-face 1))
6271 (2 (org-get-level-face 2))
6272 (3 (org-get-level-face 3)))
6274 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
6277 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
6278 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
6279 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
6280 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
6282 '(org-fontify-drawers)
6284 (list org-property-re
6285 '(1 'org-special-keyword t
)
6286 '(3 'org-property-value t
))
6287 ;; Link related fontification.
6288 '(org-activate-links)
6289 (when (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
6290 (when (memq 'radio lk
) '(org-activate-target-links (1 'org-link t
)))
6291 (when (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
6292 (when (memq 'footnote lk
) '(org-activate-footnote-links))
6294 (list org-any-target-regexp
'(0 'org-target t
))
6296 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
6298 '(org-fontify-macros)
6300 (list (format org-heading-keyword-regexp-format
6302 '(2 (org-get-todo-face 2) t
))
6304 (if org-fontify-done-headline
6305 (list (format org-heading-keyword-regexp-format
6308 (mapconcat 'regexp-quote org-done-keywords
"\\|")
6310 '(2 'org-headline-done t
))
6313 '(org-font-lock-add-priority-faces)
6315 '(org-font-lock-add-tag-faces)
6317 (when (and org-group-tags org-tag-groups-alist
)
6318 (list (concat org-outline-regexp-bol
".+\\(:"
6319 (regexp-opt (mapcar 'car org-tag-groups-alist
))
6321 '(1 'org-tag-group prepend
)))
6323 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
6324 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
6325 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
6326 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
6328 (when em
'(org-do-emphasis-faces))
6330 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
6331 1 'org-checkbox prepend
)
6332 (when (cdr (assq 'checkbox org-list-automatic-rules
))
6333 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
6334 (0 (org-get-checkbox-statistics-face) t
)))
6335 ;; Description list items
6336 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
6337 1 'org-list-dt prepend
)
6338 ;; ARCHIVEd headings
6340 org-outline-regexp-bol
6341 "\\(.*:" org-archive-tag
":.*\\)")
6342 '(1 'org-archived prepend
))
6344 '(org-do-latex-and-related)
6345 '(org-fontify-entities)
6346 '(org-raise-scripts)
6348 '(org-activate-code (1 'org-code t
))
6351 "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
6354 '(9 'org-special-keyword t
))
6355 ;; Blocks and meta lines
6356 '(org-fontify-meta-lines-and-blocks))))
6357 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
6358 (run-hooks 'org-font-lock-set-keywords-hook
)
6359 ;; Now set the full font-lock-keywords
6360 (setq-local org-font-lock-keywords org-font-lock-extra-keywords
)
6361 (setq-local font-lock-defaults
6362 '(org-font-lock-keywords t nil nil backward-paragraph
))
6363 (kill-local-variable 'font-lock-keywords
)
6366 (defun org-toggle-pretty-entities ()
6367 "Toggle the composition display of entities as UTF8 characters."
6369 (setq-local org-pretty-entities
(not org-pretty-entities
))
6370 (org-restart-font-lock)
6371 (if org-pretty-entities
6372 (message "Entities are now displayed as UTF8 characters")
6375 (decompose-region (point-min) (point-max))
6376 (message "Entities are now displayed as plain text"))))
6378 (defvar-local org-custom-properties-overlays nil
6379 "List of overlays used for custom properties.")
6381 (defun org-toggle-custom-properties-visibility ()
6382 "Display or hide properties in `org-custom-properties'."
6384 (if org-custom-properties-overlays
6385 (progn (mapc #'delete-overlay org-custom-properties-overlays
)
6386 (setq org-custom-properties-overlays nil
))
6387 (when org-custom-properties
6388 (org-with-wide-buffer
6389 (goto-char (point-min))
6390 (let ((regexp (org-re-property (regexp-opt org-custom-properties
) t t
)))
6391 (while (re-search-forward regexp nil t
)
6392 (let ((end (cdr (save-match-data (org-get-property-block)))))
6393 (when (and end
(< (point) end
))
6394 ;; Hide first custom property in current drawer.
6395 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
6396 (overlay-put o
'invisible t
)
6397 (overlay-put o
'org-custom-property t
)
6398 (push o org-custom-properties-overlays
))
6399 ;; Hide additional custom properties in the same drawer.
6400 (while (re-search-forward regexp end t
)
6401 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
6402 (overlay-put o
'invisible t
)
6403 (overlay-put o
'org-custom-property t
)
6404 (push o org-custom-properties-overlays
)))))
6405 ;; Each entry is limited to a single property drawer.
6406 (outline-next-heading)))))))
6408 (defun org-fontify-entities (limit)
6409 "Find an entity to fontify."
6411 (when org-pretty-entities
6413 ;; "\_ "-family is left out on purpose. Only the first one,
6414 ;; i.e., "\_ ", could be fontified anyway, and it would be
6415 ;; confusing when adding a second white space character.
6416 (while (re-search-forward
6417 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
6419 (when (and (not (org-at-comment-p))
6420 (setq ee
(org-entity-get (match-string 1)))
6421 (= (length (nth 6 ee
)) 1))
6422 (let* ((end (if (equal (match-string 2) "{}")
6425 (add-text-properties
6426 (match-beginning 0) end
6427 (list 'font-lock-fontified t
))
6428 (compose-region (match-beginning 0) end
6434 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
6435 "Fontify string S like in Org mode."
6438 (let ((org-odd-levels-only odd-levels
))
6440 (org-font-lock-ensure)
6446 (defun org-get-level-face (n)
6447 "Get the right face for match N in font-lock matching of headlines."
6448 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
6449 (when org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
6450 (if org-cycle-level-faces
6451 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
6452 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
6454 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
6456 (t (unless org-level-color-stars-only org-f
))))
6458 (defun org-face-from-face-or-color (context inherit face-or-color
)
6459 "Create a face list that inherits INHERIT, but sets the foreground color.
6460 When FACE-OR-COLOR is not a string, just return it."
6461 (if (stringp face-or-color
)
6462 (list :inherit inherit
6463 (cdr (assoc context org-faces-easy-properties
))
6467 (defun org-get-todo-face (kwd)
6468 "Get the right face for a TODO keyword KWD.
6469 If KWD is a number, get the corresponding match group."
6470 (when (numberp kwd
) (setq kwd
(match-string kwd
)))
6471 (or (org-face-from-face-or-color
6472 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
6473 (and (member kwd org-done-keywords
) 'org-done
)
6476 (defun org-get-priority-face (priority)
6477 "Get the right face for PRIORITY.
6478 PRIORITY is a character."
6479 (or (org-face-from-face-or-color
6480 'priority
'org-priority
(cdr (assq priority org-priority-faces
)))
6483 (defun org-get-tag-face (tag)
6484 "Get the right face for TAG.
6485 If TAG is a number, get the corresponding match group."
6486 (let ((tag (if (wholenump tag
) (match-string tag
) tag
)))
6487 (or (org-face-from-face-or-color
6488 'tag
'org-tag
(cdr (assoc tag org-tag-faces
)))
6491 (defun org-font-lock-add-priority-faces (limit)
6492 "Add the special priority faces."
6493 (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t
)
6494 (add-text-properties
6495 (match-beginning 1) (match-end 1)
6496 (list 'face
(org-get-priority-face (string-to-char (match-string 2)))
6497 'font-lock-fontified t
))))
6499 (defun org-font-lock-add-tag-faces (limit)
6500 "Add the special tag faces."
6501 (when (and org-tag-faces org-tags-special-faces-re
)
6502 (while (re-search-forward org-tags-special-faces-re limit t
)
6503 (add-text-properties (match-beginning 1) (match-end 1)
6504 (list 'face
(org-get-tag-face 1)
6505 'font-lock-fontified t
))
6506 (backward-char 1))))
6508 (defun org-unfontify-region (beg end
&optional _maybe_loudly
)
6509 "Remove fontification and activation overlays from links."
6510 (font-lock-default-unfontify-region beg end
)
6511 (let* ((buffer-undo-list t
)
6512 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
6513 (inhibit-modification-hooks t
)
6514 deactivate-mark buffer-file-name buffer-file-truename
)
6515 (decompose-region beg end
)
6516 (remove-text-properties beg end
6517 '(mouse-face t keymap t org-linked-text t
6518 invisible t intangible t
6520 (org-remove-font-lock-display-properties beg end
)))
6522 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
6523 ((raise 0.3) (height 0.7))
6526 "Display properties for showing superscripts and subscripts.")
6528 (defun org-remove-font-lock-display-properties (beg end
)
6529 "Remove specific display properties that have been added by font lock.
6530 The will remove the raise properties that are used to show superscripts
6534 (setq next
(next-single-property-change beg
'display nil end
)
6535 prop
(get-text-property beg
'display
))
6536 (when (member prop org-script-display
)
6537 (put-text-property beg next
'display nil
))
6540 (defun org-raise-scripts (limit)
6541 "Add raise properties to sub/superscripts."
6542 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
6544 (if (eq org-use-sub-superscripts t
)
6545 org-match-substring-regexp
6546 org-match-substring-with-braces-regexp
)
6548 (let* ((pos (point)) table-p comment-p
6549 (mpos (match-beginning 3))
6550 (emph-p (get-text-property mpos
'org-emphasis
))
6551 (link-p (get-text-property mpos
'mouse-face
))
6552 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6553 (goto-char (point-at-bol))
6554 (setq table-p
(looking-at-p org-table-dataline-regexp
)
6555 comment-p
(looking-at-p "^[ \t]*#[ +]"))
6558 (when (member (char-after) '(?_ ?^
)) (goto-char (1- pos
)))
6559 (unless (or comment-p emph-p link-p keyw-p
)
6560 (put-text-property (match-beginning 3) (match-end 0)
6562 (if (equal (char-after (match-beginning 2)) ?^
)
6563 (nth (if table-p
3 1) org-script-display
)
6564 (nth (if table-p
2 0) org-script-display
)))
6565 (add-text-properties (match-beginning 2) (match-end 2)
6566 (list 'invisible t
))
6567 (when (and (eq (char-after (match-beginning 3)) ?
{)
6568 (eq (char-before (match-end 3)) ?
}))
6569 (add-text-properties (match-beginning 3) (1+ (match-beginning 3))
6570 (list 'invisible t
))
6571 (add-text-properties (1- (match-end 3)) (match-end 3)
6572 (list 'invisible t
))))
6575 (defun org-remove-empty-overlays-at (pos)
6576 "Remove outline overlays that do not contain non-white stuff."
6577 (dolist (o (overlays-at pos
))
6578 (and (eq 'outline
(overlay-get o
'invisible
))
6579 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6581 (delete-overlay o
))))
6583 (defun org-show-empty-lines-in-parent ()
6584 "Move to the parent and re-show empty lines before visible headlines."
6586 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6587 (org-cycle-show-empty-lines context
))))
6589 (defun org-files-list ()
6590 "Return `org-agenda-files' list, plus all open Org files.
6591 This is useful for operations that need to scan all of a user's
6592 open and agenda-wise Org files."
6593 (let ((files (mapcar #'expand-file-name
(org-agenda-files))))
6594 (dolist (buf (buffer-list))
6595 (with-current-buffer buf
6596 (when (and (derived-mode-p 'org-mode
) (buffer-file-name))
6597 (cl-pushnew (expand-file-name (buffer-file-name)) files
6601 (defsubst org-entry-beginning-position
()
6602 "Return the beginning position of the current entry."
6603 (save-excursion (org-back-to-heading t
) (point)))
6605 (defsubst org-entry-end-position
()
6606 "Return the end position of the current entry."
6607 (save-excursion (outline-next-heading) (point)))
6609 (defun org-subtree-end-visible-p ()
6610 "Is the end of the current subtree visible?"
6611 (pos-visible-in-window-p
6612 (save-excursion (org-end-of-subtree t
) (point))))
6614 (defun org-first-headline-recenter ()
6615 "Move cursor to the first headline and recenter the headline."
6616 (let ((window (get-buffer-window)))
6618 (goto-char (point-min))
6619 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6620 (set-window-start window
(line-beginning-position))))))
6623 ;;; Visibility (headlines, blocks, drawers)
6625 ;;;; Headlines visibility
6627 (defun org-show-entry ()
6628 "Show the body directly following this heading.
6629 Show the heading too, if it is currently invisible."
6633 (org-back-to-heading t
)
6635 (line-end-position 0)
6637 (if (re-search-forward
6638 (concat "[\r\n]\\(" org-outline-regexp
"\\)") nil t
)
6644 (defun org-show-children (&optional level
)
6645 "Show all direct subheadings of this heading.
6646 Prefix arg LEVEL is how many levels below the current level
6647 should be shown. Default is enough to cause the following
6651 (org-back-to-heading t
)
6652 (let* ((current-level (funcall outline-level
))
6653 (max-level (org-get-valid-level
6655 (if level
(prefix-numeric-value level
) 1)))
6656 (end (save-excursion (org-end-of-subtree t t
)))
6657 (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
6658 (past-first-child nil
)
6659 ;; Make sure to skip inlinetasks.
6660 (re (format regexp-fmt
6663 ((not (featurep 'org-inlinetask
)) "")
6664 (org-odd-levels-only (- (* 2 org-inlinetask-min-level
)
6666 (t (1- org-inlinetask-min-level
))))))
6667 ;; Display parent heading.
6668 (org-flag-heading nil
)
6670 ;; Display children. First child may be deeper than expected
6671 ;; MAX-LEVEL. Since we want to display it anyway, adjust
6672 ;; MAX-LEVEL accordingly.
6673 (while (re-search-forward re end t
)
6674 (unless past-first-child
6675 (setq re
(format regexp-fmt
6677 (max (funcall outline-level
) max-level
)))
6678 (setq past-first-child t
))
6679 (org-flag-heading nil
)))))
6681 (defun org-show-subtree ()
6682 "Show everything after this heading at deeper levels."
6685 (point) (save-excursion (org-end-of-subtree t t
)) nil
'outline
))
6687 ;;;; Blocks visibility
6689 (defun org-hide-block-toggle-maybe ()
6690 "Toggle visibility of block at point.
6691 Unlike to `org-hide-block-toggle', this function does not throw
6692 an error. Return a non-nil value when toggling is successful."
6694 (ignore-errors (org-hide-block-toggle)))
6696 (defun org-hide-block-toggle (&optional force
)
6697 "Toggle the visibility of the current block.
6698 When optional argument FORCE is `off', make block visible. If it
6699 is non-nil, hide it unconditionally. Throw an error when not at
6700 a block. Return a non-nil value when toggling is successful."
6702 (let ((element (org-element-at-point)))
6703 (unless (memq (org-element-type element
)
6704 '(center-block comment-block dynamic-block example-block
6705 export-block quote-block special-block
6706 src-block verse-block
))
6707 (user-error "Not at a block"))
6708 (let* ((post (org-element-property :post-affiliated element
))
6709 (start (save-excursion
6711 (line-end-position)))
6712 (end (save-excursion
6713 (goto-char (org-element-property :end element
))
6714 (skip-chars-backward " \t\n")
6715 (line-end-position))))
6716 ;; Do nothing when not before or at the block opening line or at
6717 ;; the block closing line.
6718 (unless (let ((eol (line-end-position))) (and (> eol start
) (/= eol end
)))
6719 (cond ((eq force
'off
)
6720 (org-flag-region start end nil
'org-hide-block
))
6722 (org-flag-region start end t
'org-hide-block
))
6723 ((eq (get-char-property start
'invisible
) 'org-hide-block
)
6724 (org-flag-region start end nil
'org-hide-block
))
6726 (org-flag-region start end t
'org-hide-block
)))
6727 ;; When the block is hidden away, make sure point is left in
6728 ;; a visible part of the buffer.
6729 (when (invisible-p (max (1- (point)) (point-min)))
6734 (defun org-hide-block-toggle-all ()
6735 "Toggle the visibility of all blocks in the current buffer."
6736 (org-block-map 'org-hide-block-toggle
))
6738 (defun org-hide-block-all ()
6739 "Fold all blocks in the current buffer."
6741 (org-show-all '(blocks))
6742 (org-block-map 'org-hide-block-toggle-maybe
))
6744 ;;;; Drawers visibility
6746 (defun org-cycle-hide-drawers (state &optional exceptions
)
6747 "Re-hide all drawers after a visibility state change.
6748 STATE should be one of the symbols listed in the docstring of
6749 `org-cycle-hook'. When non-nil, optional argument EXCEPTIONS is
6750 a list of strings specifying which drawers should not be hidden."
6751 (when (and (derived-mode-p 'org-mode
)
6752 (not (memq state
'(overview folded contents
))))
6754 (let* ((globalp (eq state
'all
))
6755 (beg (if globalp
(point-min) (point)))
6756 (end (if globalp
(point-max)
6757 (if (eq state
'children
)
6758 (save-excursion (outline-next-heading) (point))
6759 (org-end-of-subtree t
)))))
6761 (while (re-search-forward org-drawer-regexp
(max end
(point)) t
)
6762 (unless (member-ignore-case (match-string 1) exceptions
)
6763 (let ((drawer (org-element-at-point)))
6764 (when (memq (org-element-type drawer
) '(drawer property-drawer
))
6765 (org-flag-drawer t drawer
)
6766 ;; Make sure to skip drawer entirely or we might flag
6767 ;; it another time when matching its ending line with
6768 ;; `org-drawer-regexp'.
6769 (goto-char (org-element-property :end drawer
))))))))))
6771 (defun org-flag-drawer (flag &optional element
)
6772 "When FLAG is non-nil, hide the drawer we are at.
6773 Otherwise make it visible. When optional argument ELEMENT is
6774 a parsed drawer, as returned by `org-element-at-point', hide or
6775 show that drawer instead."
6776 (let ((drawer (or element
6777 (and (save-excursion
6779 (looking-at-p org-drawer-regexp
))
6780 (org-element-at-point)))))
6781 (when (memq (org-element-type drawer
) '(drawer property-drawer
))
6782 (let ((post (org-element-property :post-affiliated drawer
)))
6784 (save-excursion (goto-char post
) (line-end-position))
6785 (save-excursion (goto-char (org-element-property :end drawer
))
6786 (skip-chars-backward " \t\n")
6787 (line-end-position))
6788 flag
'org-hide-drawer
)
6789 ;; When the drawer is hidden away, make sure point lies in
6790 ;; a visible part of the buffer.
6791 (when (invisible-p (max (1- (point)) (point-min)))
6792 (goto-char post
))))))
6794 ;;;; Visibility cycling
6796 (defvar-local org-cycle-global-status nil
)
6797 (put 'org-cycle-global-status
'org-state t
)
6798 (defvar-local org-cycle-subtree-status nil
)
6799 (put 'org-cycle-subtree-status
'org-state t
)
6801 (defun org-show-all (&optional types
)
6802 "Show all contents in the visible part of the buffer.
6803 By default, the function expands headings, blocks and drawers.
6804 When optional argument TYPE is a list of symbols among `blocks',
6805 `drawers' and `headings', to only expand one specific type."
6806 (dolist (type (or types
'(blocks drawers headings
)))
6807 (org-flag-region (point-min) (point-max) nil
6809 (`blocks
'org-hide-block
)
6810 (`drawers
'org-hide-drawer
)
6811 (`headings
'outline
)
6812 (_ (error "Invalid type: %S" type
))))))
6815 (defun org-cycle (&optional arg
)
6816 "TAB-action and visibility cycling for Org mode.
6818 This is the command invoked in Org mode by the `TAB' key. Its main
6819 purpose is outline visibility cycling, but it also invokes other actions
6820 in special contexts.
6822 When this function is called with a `\\[universal-argument]' prefix, rotate \
6824 buffer through 3 states (global cycling)
6825 1. OVERVIEW: Show only top-level headlines.
6826 2. CONTENTS: Show all headlines of all levels, but no body text.
6827 3. SHOW ALL: Show everything.
6829 With a `\\[universal-argument] \\[universal-argument]' prefix argument, \
6830 switch to the startup visibility,
6831 determined by the variable `org-startup-folded', and by any VISIBILITY
6832 properties in the buffer.
6834 With a `\\[universal-argument] \\[universal-argument] \
6835 \\[universal-argument]' prefix argument, show the entire buffer, including
6838 When inside a table, re-align the table and move to the next field.
6840 When point is at the beginning of a headline, rotate the subtree started
6841 by this line through 3 different states (local cycling)
6842 1. FOLDED: Only the main headline is shown.
6843 2. CHILDREN: The main headline and the direct children are shown.
6844 From this state, you can move to one of the children
6845 and zoom in further.
6846 3. SUBTREE: Show the entire subtree, including body text.
6847 If there is no subtree, switch directly from CHILDREN to FOLDED.
6849 When point is at the beginning of an empty headline and the variable
6850 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6851 of the headline by demoting and promoting it to likely levels. This
6852 speeds up creation document structure by pressing `TAB' once or several
6853 times right after creating a new headline.
6855 When there is a numeric prefix, go up to a heading with level ARG, do
6856 a `show-subtree' and return to the previous cursor position. If ARG
6857 is negative, go up that many levels.
6859 When point is not at the beginning of a headline, execute the global
6860 binding for `TAB', which is re-indenting the line. See the option
6861 `org-cycle-emulate-tab' for details.
6863 As a special case, if point is at the beginning of the buffer and there is
6864 no headline in line 1, this function will act as if called with prefix arg
6865 \(`\\[universal-argument] TAB', same as `S-TAB') also when called without \
6866 prefix arg, but only
6867 if the variable `org-cycle-global-at-bob' is t."
6869 (org-load-modules-maybe)
6870 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6871 (and org-cycle-level-after-item
/entry-creation
6872 (or (org-cycle-level)
6873 (org-cycle-item-indentation))))
6875 (or org-cycle-max-level
6876 (and (boundp 'org-inlinetask-min-level
)
6877 org-inlinetask-min-level
6878 (1- org-inlinetask-min-level
))))
6879 (nstars (and limit-level
6880 (if org-odd-levels-only
6881 (and limit-level
(1- (* limit-level
2)))
6884 (if (not (derived-mode-p 'org-mode
))
6886 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6887 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6888 (not (looking-at org-outline-regexp
))))
6891 (delq 'org-optimize-window-after-visibility-change
6892 (copy-sequence org-cycle-hook
))
6899 (setq last-command
'dummy
)
6900 (org-set-startup-visibility)
6901 (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
6905 (org-unlogged-message "Entire buffer visible, including drawers"))
6907 ((equal arg
'(4)) (org-cycle-internal-global))
6909 ;; Try hiding block at point.
6910 ((org-hide-block-toggle-maybe))
6912 ;; Try cdlatex TAB completion
6913 ((org-try-cdlatex-tab))
6915 ;; Table: enter it or move to the next field.
6916 ((org-at-table-p 'any
)
6917 (if (org-at-table.el-p
)
6918 (message "%s" (substitute-command-keys "\\<org-mode-map>\
6919 Use `\\[org-edit-special]' to edit table.el tables"))
6920 (if arg
(org-table-edit-field t
)
6921 (org-table-justify-field-maybe)
6922 (call-interactively 'org-table-next-field
))))
6924 ((run-hook-with-args-until-success 'org-tab-after-check-for-table-hook
))
6926 ;; Global cycling: delegate to `org-cycle-internal-global'.
6927 (bob-special (org-cycle-internal-global))
6929 ;; Drawers: delegate to `org-flag-drawer'.
6931 (beginning-of-line 1)
6932 (looking-at org-drawer-regexp
))
6933 (org-flag-drawer ; toggle block visibility
6934 (not (get-char-property (match-end 0) 'invisible
))))
6936 ;; Show-subtree, ARG levels up from here.
6939 (org-back-to-heading)
6940 (outline-up-heading (if (< arg
0) (- arg
)
6941 (- (funcall outline-level
) arg
)))
6942 (org-show-subtree)))
6944 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6945 ((and (featurep 'org-inlinetask
)
6946 (org-inlinetask-at-task-p)
6947 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6948 (org-inlinetask-toggle-visibility))
6950 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6951 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6952 (save-excursion (move-beginning-of-line 1)
6953 (looking-at org-outline-regexp
)))
6954 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6955 (org-cycle-internal-local))
6957 ;; From there: TAB emulation and template completion.
6958 (buffer-read-only (org-back-to-heading))
6960 ((run-hook-with-args-until-success
6961 'org-tab-after-check-for-cycling-hook
))
6963 ((run-hook-with-args-until-success
6964 'org-tab-before-tab-emulation-hook
))
6966 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6968 (not (looking-at org-outline-regexp
))))
6969 (call-interactively (global-key-binding "\t")))
6971 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6972 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6973 (or (and (eq org-cycle-emulate-tab
'white
)
6974 (= (match-end 0) (point-at-eol)))
6975 (and (eq org-cycle-emulate-tab
'whitestart
)
6976 (>= (match-end 0) pos
))))
6978 (eq org-cycle-emulate-tab t
))
6979 (call-interactively (global-key-binding "\t")))
6982 (org-back-to-heading)
6985 (defun org-cycle-internal-global ()
6986 "Do the global cycling action."
6987 ;; Hack to avoid display of messages for .org attachments in Gnus
6988 (let ((ga (string-match "\\*fontification" (buffer-name))))
6990 ((and (eq last-command this-command
)
6991 (eq org-cycle-global-status
'overview
))
6992 ;; We just created the overview - now do table of contents
6993 ;; This can be slow in very large buffers, so indicate action
6994 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6995 (unless ga
(org-unlogged-message "CONTENTS..."))
6997 (unless ga
(org-unlogged-message "CONTENTS...done"))
6998 (setq org-cycle-global-status
'contents
)
6999 (run-hook-with-args 'org-cycle-hook
'contents
))
7001 ((and (eq last-command this-command
)
7002 (eq org-cycle-global-status
'contents
))
7003 ;; We just showed the table of contents - now show everything
7004 (run-hook-with-args 'org-pre-cycle-hook
'all
)
7005 (org-show-all '(headings blocks
))
7006 (unless ga
(org-unlogged-message "SHOW ALL"))
7007 (setq org-cycle-global-status
'all
)
7008 (run-hook-with-args 'org-cycle-hook
'all
))
7011 ;; Default action: go to overview
7012 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
7014 (unless ga
(org-unlogged-message "OVERVIEW"))
7015 (setq org-cycle-global-status
'overview
)
7016 (run-hook-with-args 'org-cycle-hook
'overview
)))))
7018 (defvar org-called-with-limited-levels nil
7019 "Non-nil when `org-with-limited-levels' is currently active.")
7021 (defun org-cycle-internal-local ()
7022 "Do the local cycling action."
7023 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
7024 ;; First, determine end of headline (EOH), end of subtree or item
7025 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
7030 (setq struct
(org-list-struct))
7031 (setq eoh
(point-at-eol))
7032 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
7033 (setq has-children
(org-list-has-child-p (point) struct
)))
7034 (org-back-to-heading)
7035 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
7036 (setq eos
(save-excursion (org-end-of-subtree t t
)
7037 (when (bolp) (backward-char)) (point)))
7040 (let ((level (funcall outline-level
)))
7041 (outline-next-heading)
7042 (and (org-at-heading-p t
)
7043 (> (funcall outline-level
) level
))))
7045 (org-list-search-forward (org-item-beginning-re) eos t
)))))
7046 ;; Determine end invisible part of buffer (EOL)
7047 (beginning-of-line 2)
7048 (while (and (not (eobp)) ;This is like `next-line'.
7049 (get-char-property (1- (point)) 'invisible
))
7050 (goto-char (next-single-char-property-change (point) 'invisible
))
7051 (and (eolp) (beginning-of-line 2)))
7053 ;; Find out what to do next and set `this-command'
7056 ;; Nothing is hidden behind this heading
7057 (unless (org-before-first-heading-p)
7058 (run-hook-with-args 'org-pre-cycle-hook
'empty
))
7059 (org-unlogged-message "EMPTY ENTRY")
7060 (setq org-cycle-subtree-status nil
)
7063 (outline-next-heading)
7064 (when (org-invisible-p) (org-flag-heading nil
))))
7065 ((and (or (>= eol eos
)
7066 (not (string-match "\\S-" (buffer-substring eol eos
))))
7068 (not (setq children-skipped
7069 org-cycle-skip-children-state-if-no-children
))))
7070 ;; Entire subtree is hidden in one line: children view
7071 (unless (org-before-first-heading-p)
7072 (run-hook-with-args 'org-pre-cycle-hook
'children
))
7074 (org-list-set-item-visibility (point-at-bol) struct
'children
)
7076 (org-with-limited-levels (org-show-children))
7077 (org-show-set-visibility 'canonical
)
7078 ;; Fold every list in subtree to top-level items.
7079 (when (eq org-cycle-include-plain-lists
'integrate
)
7081 (org-back-to-heading)
7082 (while (org-list-search-forward (org-item-beginning-re) eos t
)
7083 (beginning-of-line 1)
7084 (let* ((struct (org-list-struct))
7085 (prevs (org-list-prevs-alist struct
))
7086 (end (org-list-get-bottom-point struct
)))
7087 (dolist (e (org-list-get-all-items (point) struct prevs
))
7088 (org-list-set-item-visibility e struct
'folded
))
7089 (goto-char (if (< end eos
) end eos
)))))))
7090 (org-unlogged-message "CHILDREN")
7093 (outline-next-heading)
7094 (when (org-invisible-p) (org-flag-heading nil
)))
7095 (setq org-cycle-subtree-status
'children
)
7096 (unless (org-before-first-heading-p)
7097 (run-hook-with-args 'org-cycle-hook
'children
)))
7098 ((or children-skipped
7099 (and (eq last-command this-command
)
7100 (eq org-cycle-subtree-status
'children
)))
7101 ;; We just showed the children, or no children are there,
7102 ;; now show everything.
7103 (unless (org-before-first-heading-p)
7104 (run-hook-with-args 'org-pre-cycle-hook
'subtree
))
7105 (org-flag-region eoh eos nil
'outline
)
7106 (org-unlogged-message
7107 (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
7108 (setq org-cycle-subtree-status
'subtree
)
7109 (unless (org-before-first-heading-p)
7110 (run-hook-with-args 'org-cycle-hook
'subtree
)))
7112 ;; Default action: hide the subtree.
7113 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
7114 (org-flag-region eoh eos t
'outline
)
7115 (org-unlogged-message "FOLDED")
7116 (setq org-cycle-subtree-status
'folded
)
7117 (unless (org-before-first-heading-p)
7118 (run-hook-with-args 'org-cycle-hook
'folded
))))))
7121 (defun org-global-cycle (&optional arg
)
7122 "Cycle the global visibility. For details see `org-cycle'.
7123 With `\\[universal-argument]' prefix ARG, switch to startup visibility.
7124 With a numeric prefix, show all headlines up to that level."
7126 (let ((org-cycle-include-plain-lists
7127 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
7130 (org-show-all '(headings blocks
))
7131 (outline-hide-sublevels arg
)
7132 (setq org-cycle-global-status
'contents
))
7134 (org-set-startup-visibility)
7135 (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
7137 (org-cycle '(4))))))
7139 (defun org-set-startup-visibility ()
7140 "Set the visibility required by startup options and properties."
7142 ((eq org-startup-folded t
)
7144 ((eq org-startup-folded
'content
)
7146 ((or (eq org-startup-folded
'showeverything
)
7147 (eq org-startup-folded nil
))
7149 (unless (eq org-startup-folded
'showeverything
)
7150 (when org-hide-block-startup
(org-hide-block-all))
7151 (org-set-visibility-according-to-property)
7152 (org-cycle-hide-archived-subtrees 'all
)
7153 (org-cycle-hide-drawers 'all
)
7154 (org-cycle-show-empty-lines t
)))
7156 (defun org-set-visibility-according-to-property ()
7157 "Switch subtree visibilities according to :VISIBILITY: property."
7159 (org-with-wide-buffer
7160 (goto-char (point-min))
7161 (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t
)
7162 (if (not (org-at-property-p)) (outline-next-heading)
7163 (let ((state (match-string 3)))
7165 (org-back-to-heading t
)
7166 (outline-hide-subtree)
7169 ((equal state
"folded")
7170 (outline-hide-subtree))
7171 ((equal state
"children")
7172 (org-show-hidden-entry)
7173 (org-show-children))
7174 ((equal state
"content")
7177 (org-narrow-to-subtree)
7179 ((member state
'("all" "showall"))
7180 (outline-show-subtree)))))))))
7182 (defun org-overview ()
7183 "Switch to overview mode, showing only top-level headlines.
7184 This shows all headlines with a level equal or greater than the level
7185 of the first headline in the buffer. This is important, because if the
7186 first headline is not level one, then (hide-sublevels 1) gives confusing
7192 (goto-char (point-min))
7193 (when (re-search-forward org-outline-regexp-bol nil t
)
7194 (goto-char (match-beginning 0))
7195 (funcall outline-level
)))))
7196 (and level
(outline-hide-sublevels level
)))))
7198 (defun org-content (&optional arg
)
7199 "Show all headlines in the buffer, like a table of contents.
7200 With numerical argument N, show content up to level N."
7204 ;; Visit all headings and show their offspring
7205 (and (integerp arg
) (org-overview))
7206 (goto-char (point-max))
7208 (while (and (progn (condition-case nil
7209 (outline-previous-visible-heading 1)
7210 (error (goto-char (point-min))))
7212 (looking-at org-outline-regexp
))
7214 (org-show-children (1- arg
))
7215 (outline-show-branches))
7216 (when (bobp) (throw 'exit nil
))))))
7218 (defun org-optimize-window-after-visibility-change (state)
7219 "Adjust the window after a change in outline visibility.
7220 This function is the default value of the hook `org-cycle-hook'."
7221 (when (get-buffer-window (current-buffer))
7223 ((eq state
'content
) nil
)
7224 ((eq state
'all
) nil
)
7225 ((eq state
'folded
) nil
)
7226 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
7227 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
7229 (defun org-clean-visibility-after-subtree-move ()
7230 "Fix visibility issues after moving a subtree."
7231 ;; First, find a reasonable region to look at:
7232 ;; Start two siblings above, end three below
7233 (let* ((beg (save-excursion
7234 (and (org-get-last-sibling)
7235 (org-get-last-sibling))
7237 (end (save-excursion
7238 (and (org-get-next-sibling)
7239 (org-get-next-sibling)
7240 (org-get-next-sibling))
7241 (if (org-at-heading-p)
7244 (level (looking-at "\\*+"))
7245 (re (when level
(concat "^" (regexp-quote (match-string 0)) " "))))
7248 (narrow-to-region beg end
)
7250 ;; Properly fold already folded siblings
7251 (goto-char (point-min))
7252 (while (re-search-forward re nil t
)
7253 (when (and (not (org-invisible-p))
7254 (org-invisible-p (line-end-position)))
7255 (outline-hide-entry))))
7256 (org-cycle-hide-drawers 'all
)
7257 (org-cycle-show-empty-lines 'overview
)))))
7259 (defun org-cycle-show-empty-lines (state)
7260 "Show empty lines above all visible headlines.
7261 The region to be covered depends on STATE when called through
7262 `org-cycle-hook'. Lisp program can use t for STATE to get the
7263 entire buffer covered. Note that an empty line is only shown if there
7264 are at least `org-cycle-separator-lines' empty lines before the headline."
7265 (when (/= org-cycle-separator-lines
0)
7267 (let* ((n (abs org-cycle-separator-lines
))
7269 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
7270 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
7271 (t (let ((ns (number-to-string (- n
2))))
7272 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
7273 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
7276 ((memq state
'(overview contents t
))
7277 (setq beg
(point-min) end
(point-max)))
7278 ((memq state
'(children folded
))
7280 end
(progn (org-end-of-subtree t t
)
7281 (line-beginning-position 2)))))
7284 (while (re-search-forward re end t
)
7285 (unless (get-char-property (match-end 1) 'invisible
)
7286 (let ((e (match-end 1))
7287 (b (if (>= org-cycle-separator-lines
0)
7290 (goto-char (match-beginning 0))
7291 (skip-chars-backward " \t\n")
7292 (line-end-position)))))
7293 (org-flag-region b e nil
'outline
))))))))
7294 ;; Never hide empty lines at the end of the file.
7296 (goto-char (point-max))
7297 (outline-previous-heading)
7298 (outline-end-of-heading)
7299 (when (and (looking-at "[ \t\n]+")
7300 (= (match-end 0) (point-max)))
7301 (org-flag-region (point) (match-end 0) nil
'outline
))))
7303 ;;;; Reveal point location
7305 (defun org-show-context (&optional key
)
7306 "Make sure point and context are visible.
7307 Optional argument KEY, when non-nil, is a symbol. See
7308 `org-show-context-detail' for allowed values and how much is to
7310 (org-show-set-visibility
7311 (cond ((symbolp org-show-context-detail
) org-show-context-detail
)
7312 ((cdr (assq key org-show-context-detail
)))
7313 (t (cdr (assq 'default org-show-context-detail
))))))
7315 (defun org-show-set-visibility (detail)
7316 "Set visibility around point according to DETAIL.
7317 DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
7318 `tree', `canonical' or t. See `org-show-context-detail' for more
7320 ;; Show current heading and possibly its entry, following headline
7322 (if (and (org-at-heading-p) (not (eq detail
'local
)))
7323 (org-flag-heading nil
)
7325 ;; If point is hidden within a drawer or a block, make sure to
7327 (dolist (o (overlays-at (point)))
7328 (when (memq (overlay-get o
'invisible
)
7329 '(org-hide-block org-hide-drawer outline
))
7330 (delete-overlay o
)))
7331 (unless (org-before-first-heading-p)
7332 (org-with-limited-levels
7334 ((tree canonical t
) (org-show-children))
7335 ((nil minimal ancestors
))
7337 (outline-next-heading)
7338 (org-flag-heading nil
)))))))
7339 ;; Show all siblings.
7340 (when (eq detail
'lineage
) (org-show-siblings))
7341 ;; Show ancestors, possibly with their children.
7342 (when (memq detail
'(ancestors lineage tree canonical t
))
7344 (while (org-up-heading-safe)
7345 (org-flag-heading nil
)
7346 (when (memq detail
'(canonical t
)) (org-show-entry))
7347 (when (memq detail
'(tree canonical t
)) (org-show-children))))))
7349 (defvar org-reveal-start-hook nil
7350 "Hook run before revealing a location.")
7352 (defun org-reveal (&optional siblings
)
7353 "Show current entry, hierarchy above it, and the following headline.
7355 This can be used to show a consistent set of context around
7356 locations exposed with `org-show-context'.
7358 With optional argument SIBLINGS, on each level of the hierarchy all
7359 siblings are shown. This repairs the tree structure to what it would
7360 look like when opened with hierarchical calls to `org-cycle'.
7362 With a \\[universal-argument] \\[universal-argument] prefix, \
7363 go to the parent and show the entire tree."
7365 (run-hooks 'org-reveal-start-hook
)
7366 (cond ((equal siblings
'(4)) (org-show-set-visibility 'canonical
))
7367 ((equal siblings
'(16))
7369 (when (org-up-heading-safe)
7371 (run-hook-with-args 'org-cycle-hook
'subtree
))))
7372 (t (org-show-set-visibility 'lineage
))))
7375 ;;; Indirect buffer display of subtrees
7377 (defvar org-indirect-dedicated-frame nil
7378 "This is the frame being used for indirect tree display.")
7379 (defvar org-last-indirect-buffer nil
)
7381 (defun org-tree-to-indirect-buffer (&optional arg
)
7382 "Create indirect buffer and narrow it to current subtree.
7384 With a numerical prefix ARG, go up to this level and then take that tree.
7385 If ARG is negative, go up that many levels.
7387 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7388 indirect buffer previously made with this command, to avoid proliferation of
7389 indirect buffers. However, when you call the command with a \
7390 `\\[universal-argument]' prefix, or
7391 when `org-indirect-buffer-display' is `new-frame', the last buffer is kept
7392 so that you can work with several indirect buffers at the same time. If
7393 `org-indirect-buffer-display' is `dedicated-frame', the \
7394 `\\[universal-argument]' prefix also
7395 requests that a new frame be made for the new buffer, so that the dedicated
7396 frame is not changed."
7398 (let ((cbuf (current-buffer))
7399 (cwin (selected-window))
7401 beg end level heading ibuf
)
7403 (org-back-to-heading t
)
7405 (setq level
(org-outline-level))
7406 (when (< arg
0) (setq arg
(+ level arg
)))
7407 (while (> (setq level
(org-outline-level)) arg
)
7408 (org-up-heading-safe)))
7410 heading
(org-get-heading 'no-tags
))
7411 (org-end-of-subtree t t
)
7412 (when (org-at-heading-p) (backward-char 1))
7414 (when (and (buffer-live-p org-last-indirect-buffer
)
7415 (not (eq org-indirect-buffer-display
'new-frame
))
7417 (kill-buffer org-last-indirect-buffer
))
7418 (setq ibuf
(org-get-indirect-buffer cbuf heading
)
7419 org-last-indirect-buffer ibuf
)
7421 ((or (eq org-indirect-buffer-display
'new-frame
)
7422 (and arg
(eq org-indirect-buffer-display
'dedicated-frame
)))
7423 (select-frame (make-frame))
7424 (delete-other-windows)
7425 (pop-to-buffer-same-window ibuf
)
7426 (org-set-frame-title heading
))
7427 ((eq org-indirect-buffer-display
'dedicated-frame
)
7429 (select-frame (or (and org-indirect-dedicated-frame
7430 (frame-live-p org-indirect-dedicated-frame
)
7431 org-indirect-dedicated-frame
)
7432 (setq org-indirect-dedicated-frame
(make-frame)))))
7433 (delete-other-windows)
7434 (pop-to-buffer-same-window ibuf
)
7435 (org-set-frame-title (concat "Indirect: " heading
)))
7436 ((eq org-indirect-buffer-display
'current-window
)
7437 (pop-to-buffer-same-window ibuf
))
7438 ((eq org-indirect-buffer-display
'other-window
)
7439 (pop-to-buffer ibuf
))
7440 (t (error "Invalid value")))
7441 (narrow-to-region beg end
)
7442 (org-show-all '(headings blocks
))
7444 (run-hook-with-args 'org-cycle-hook
'all
)
7445 (and (window-live-p cwin
) (select-window cwin
))))
7447 (defun org-get-indirect-buffer (&optional buffer heading
)
7448 (setq buffer
(or buffer
(current-buffer)))
7449 (let ((n 1) (base (buffer-name buffer
)) bname
)
7450 (while (buffer-live-p
7454 (if heading
(concat heading
"-" (number-to-string n
))
7455 (number-to-string n
))))))
7458 (make-indirect-buffer buffer bname
'clone
)
7459 (error (make-indirect-buffer buffer bname
)))))
7461 (defun org-set-frame-title (title)
7462 "Set the title of the current frame to the string TITLE."
7463 (modify-frame-parameters (selected-frame) (list (cons 'name title
))))
7465 ;;;; Structure editing
7467 ;;; Inserting headlines
7469 (defun org--line-empty-p (n)
7470 "Is the Nth next line empty?
7472 Counts the current line as N = 1 and the previous line as N = 0;
7473 see `beginning-of-line'."
7476 (or (beginning-of-line n
) t
)
7478 (looking-at "[ \t]*$")))))
7480 (defun org-previous-line-empty-p ()
7481 "Is the previous line a blank line?
7482 When NEXT is non-nil, check the next line instead."
7483 (org--line-empty-p 0))
7485 (defun org-next-line-empty-p ()
7486 "Is the previous line a blank line?
7487 When NEXT is non-nil, check the next line instead."
7488 (org--line-empty-p 2))
7490 (defun org--blank-before-heading-p (&optional parent
)
7491 "Non-nil when an empty line should precede a new heading here.
7492 When optional argument PARENT is non-nil, consider parent
7493 headline instead of current one."
7494 (pcase (assq 'heading org-blank-before-new-entry
)
7497 (org-with-limited-levels
7498 (unless (and (org-before-first-heading-p)
7499 (not (outline-next-heading)))
7500 (org-back-to-heading t
)
7501 (when parent
(org-up-heading-safe))
7503 (org-previous-line-empty-p))
7504 ((outline-next-heading)
7505 (org-previous-line-empty-p))
7506 ;; Ignore trailing spaces on last buffer line.
7507 ((progn (skip-chars-backward " \t") (bolp))
7508 (org-previous-line-empty-p))
7510 (`(heading .
,value
) value
)
7513 (defun org-insert-heading (&optional arg invisible-ok top
)
7514 "Insert a new heading or an item with the same depth at point.
7516 If point is at the beginning of a heading, insert a new heading
7517 or a new headline above the current one. When at the beginning
7518 of a regular line of text, turn it into a heading.
7520 If point is in the middle of a line, split it and create a new
7521 headline with the text in the current line after point (see
7522 `org-M-RET-may-split-line' on how to modify this behavior). As
7523 a special case, on a headline, splitting can only happen on the
7524 title itself. E.g., this excludes breaking stars or tags.
7526 With a `\\[universal-argument]' prefix, set \
7527 `org-insert-heading-respect-content' to
7528 a non-nil value for the duration of the command. This forces the
7529 insertion of a heading after the current subtree, independently
7530 on the location of point.
7532 With a `\\[universal-argument] \\[universal-argument]' prefix, \
7533 insert the heading at the end of the tree
7534 above the current heading. For example, if point is within a
7535 2nd-level heading, then it will insert a 2nd-level heading at
7536 the end of the 1st-level parent subtree.
7538 When INVISIBLE-OK is set, stop at invisible headlines when going
7539 back. This is important for non-interactive uses of the
7542 When optional argument TOP is non-nil, insert a level 1 heading,
7545 (let* ((blank?
(org--blank-before-heading-p (equal arg
'(16))))
7546 (level (org-current-level))
7547 (stars (make-string (if (and level
(not top
)) level
1) ?
*)))
7549 ((or org-insert-heading-respect-content
7550 (member arg
'((4) (16)))
7551 (and (not invisible-ok
)
7552 (invisible-p (max (1- (point)) (point-min)))))
7553 ;; Position point at the location of insertion.
7554 (if (not level
) ;before first headline
7555 (org-with-limited-levels (outline-next-heading))
7556 ;; Make sure we end up on a visible headline if INVISIBLE-OK
7558 (org-with-limited-levels (org-back-to-heading invisible-ok
))
7559 (cond ((equal arg
'(16))
7560 (org-up-heading-safe)
7561 (org-end-of-subtree t t
))
7563 (org-end-of-subtree t t
))))
7564 (unless (bolp) (insert "\n")) ;ensure final newline
7565 (unless (and blank?
(org-previous-line-empty-p))
7566 (org-N-empty-lines-before-current (if blank?
1 0)))
7567 (insert stars
" \n")
7572 (when blank?
(save-excursion (insert "\n")))
7573 (save-excursion (insert stars
" \n"))
7574 (unless (and blank?
(org-previous-line-empty-p))
7575 (org-N-empty-lines-before-current (if blank?
1 0)))
7577 ((and (org-get-alist-option org-M-RET-may-split-line
'headline
)
7578 (org-match-line org-complex-heading-regexp
)
7579 (org-pos-in-match-range (point) 4))
7580 ;; Grab the text that should moved to the new headline.
7582 (let ((split (delete-and-extract-region (point) (match-end 4))))
7583 (if (looking-at "[ \t]*$") (replace-match "")
7584 (org-set-tags nil t
))
7586 (when blank?
(insert "\n"))
7587 (insert "\n" stars
" ")
7588 (when (org-string-nw-p split
) (insert split
))
7589 (when (eobp) (save-excursion (insert "\n")))))
7592 (when blank?
(insert "\n"))
7593 (insert "\n" stars
" ")
7594 (when (eobp) (save-excursion (insert "\n"))))))
7595 ;; On regular text, turn line into a headline or split, if
7599 (unless (and blank?
(org-previous-line-empty-p))
7600 (org-N-empty-lines-before-current (if blank?
1 0))))
7602 (unless (org-get-alist-option org-M-RET-may-split-line
'headline
)
7604 (insert "\n" stars
" ")
7605 (unless (and blank?
(org-previous-line-empty-p))
7606 (org-N-empty-lines-before-current (if blank?
1 0))))))
7607 (run-hooks 'org-insert-heading-hook
))
7609 (defun org-N-empty-lines-before-current (n)
7610 "Make the number of empty lines before current exactly N.
7611 So this will delete or add empty lines."
7612 (let ((column (current-column)))
7615 (let ((start (save-excursion
7616 (skip-chars-backward " \r\t\n")
7617 (line-end-position))))
7618 (delete-region start
(line-end-position 0))))
7619 (insert (make-string n ?
\n))
7620 (move-to-column column
)))
7622 (defun org-get-heading (&optional no-tags no-todo no-priority no-comment
)
7623 "Return the heading of the current entry, without the stars.
7624 When NO-TAGS is non-nil, don't include tags.
7625 When NO-TODO is non-nil, don't include TODO keywords.
7626 When NO-PRIORITY is non-nil, don't include priority cookie.
7627 When NO-COMMENT is non-nil, don't include COMMENT string."
7629 (org-back-to-heading t
)
7630 (let ((case-fold-search nil
))
7631 (looking-at org-complex-heading-regexp
)
7632 (let ((todo (and (not no-todo
) (match-string 2)))
7633 (priority (and (not no-priority
) (match-string 3)))
7634 (headline (pcase (match-string 4)
7636 ((and (guard no-comment
) h
)
7637 (replace-regexp-in-string
7639 (format "\\`%s[ \t]+" org-comment-string
))
7642 (tags (and (not no-tags
) (match-string 5))))
7643 (mapconcat #'identity
7644 (delq nil
(list todo priority headline tags
))
7647 (defun org-heading-components ()
7648 "Return the components of the current heading.
7649 This is a list with the following elements:
7650 - the level as an integer
7651 - the reduced level, different if `org-odd-levels-only' is set.
7652 - the TODO keyword, or nil
7653 - the priority character, like ?A, or nil if no priority is given
7654 - the headline text itself, or the tags string if no headline text
7655 - the tags string, or nil."
7657 (org-back-to-heading t
)
7658 (when (let (case-fold-search) (looking-at org-complex-heading-regexp
))
7659 (list (length (match-string 1))
7660 (org-reduced-level (length (match-string 1)))
7661 (match-string-no-properties 2)
7662 (and (match-end 3) (aref (match-string 3) 2))
7663 (match-string-no-properties 4)
7664 (match-string-no-properties 5)))))
7666 (defun org-get-entry ()
7667 "Get the entry text, after heading, entire subtree."
7669 (org-back-to-heading t
)
7670 (buffer-substring (point-at-bol 2) (org-end-of-subtree t
))))
7672 (defun org-edit-headline (&optional heading
)
7673 "Edit the current headline.
7674 Set it to HEADING when provided."
7676 (org-with-wide-buffer
7677 (org-back-to-heading t
)
7678 (let ((case-fold-search nil
))
7679 (when (looking-at org-complex-heading-regexp
)
7680 (let* ((old (match-string-no-properties 4))
7681 (new (save-match-data
7682 (org-trim (or heading
(read-string "Edit: " old
))))))
7683 (unless (equal old new
)
7684 (if old
(replace-match new t t nil
4)
7685 (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
7687 (org-set-tags nil t
)
7688 (when (looking-at "[ \t]*$") (replace-match ""))))))))
7690 (defun org-insert-heading-after-current ()
7691 "Insert a new heading with same level as current, after current subtree."
7693 (org-back-to-heading)
7694 (org-insert-heading)
7695 (org-move-subtree-down)
7698 (defun org-insert-heading-respect-content (&optional invisible-ok
)
7699 "Insert heading with `org-insert-heading-respect-content' set to t."
7701 (org-insert-heading '(4) invisible-ok
))
7703 (defun org-insert-todo-heading-respect-content (&optional force-state
)
7704 "Insert TODO heading with `org-insert-heading-respect-content' set to t."
7706 (org-insert-todo-heading force-state
'(4)))
7708 (defun org-insert-todo-heading (arg &optional force-heading
)
7709 "Insert a new heading with the same level and TODO state as current heading.
7711 If the heading has no TODO state, or if the state is DONE, use
7712 the first state (TODO by default). Also with one prefix arg,
7713 force first state. With two prefix args, force inserting at the
7714 end of the parent subtree.
7716 When called at a plain list item, insert a new item with an
7717 unchecked check box."
7719 (when (or force-heading
(not (org-insert-item 'checkbox
)))
7720 (org-insert-heading (or (and (equal arg
'(16)) '(16))
7723 (org-forward-heading-same-level -
1)
7724 (let ((case-fold-search nil
)) (looking-at org-todo-line-regexp
)))
7726 (if (or (equal arg
'(4))
7727 (not (match-beginning 2))
7728 (member (match-string 2) org-done-keywords
))
7729 (car org-todo-keywords-1
)
7733 (run-hook-with-args-until-success
7734 'org-todo-get-default-hook new-mark-x nil
)
7736 (beginning-of-line 1)
7737 (and (looking-at org-outline-regexp
) (goto-char (match-end 0))
7738 (if org-treat-insert-todo-heading-as-state-change
7740 (insert new-mark
" "))))
7741 (when org-provide-todo-statistics
7742 (org-update-parent-todo-statistics))))
7744 (defun org-insert-subheading (arg)
7745 "Insert a new subheading and demote it.
7746 Works for outline headings and for plain lists alike."
7748 (org-insert-heading arg
)
7750 ((org-at-heading-p) (org-do-demote))
7751 ((org-at-item-p) (org-indent-item))))
7753 (defun org-insert-todo-subheading (arg)
7754 "Insert a new subheading with TODO keyword or checkbox and demote it.
7755 Works for outline headings and for plain lists alike."
7757 (org-insert-todo-heading arg
)
7759 ((org-at-heading-p) (org-do-demote))
7760 ((org-at-item-p) (org-indent-item))))
7762 ;;; Promotion and Demotion
7764 (defvar org-after-demote-entry-hook nil
7765 "Hook run after an entry has been demoted.
7766 The cursor will be at the beginning of the entry.
7767 When a subtree is being demoted, the hook will be called for each node.")
7769 (defvar org-after-promote-entry-hook nil
7770 "Hook run after an entry has been promoted.
7771 The cursor will be at the beginning of the entry.
7772 When a subtree is being promoted, the hook will be called for each node.")
7774 (defun org-promote-subtree ()
7775 "Promote the entire subtree.
7776 See also `org-promote'."
7779 (org-with-limited-levels (org-map-tree 'org-promote
)))
7780 (org-fix-position-after-promote))
7782 (defun org-demote-subtree ()
7783 "Demote the entire subtree.
7784 See `org-demote' and `org-promote'."
7787 (org-with-limited-levels (org-map-tree 'org-demote
)))
7788 (org-fix-position-after-promote))
7790 (defun org-do-promote ()
7791 "Promote the current heading higher up the tree.
7792 If the region is active in `transient-mark-mode', promote all
7793 headings in the region."
7796 (if (org-region-active-p)
7797 (org-map-region 'org-promote
(region-beginning) (region-end))
7799 (org-fix-position-after-promote))
7801 (defun org-do-demote ()
7802 "Demote the current heading lower down the tree.
7803 If the region is active in `transient-mark-mode', demote all
7804 headings in the region."
7807 (if (org-region-active-p)
7808 (org-map-region 'org-demote
(region-beginning) (region-end))
7810 (org-fix-position-after-promote))
7812 (defun org-fix-position-after-promote ()
7813 "Fix cursor position and indentation after demoting/promoting."
7814 (let ((pos (point)))
7815 (when (save-excursion
7817 (let ((case-fold-search nil
)) (looking-at org-todo-line-regexp
))
7818 (or (eq pos
(match-end 1)) (eq pos
(match-end 2))))
7819 (cond ((eobp) (insert " "))
7820 ((eolp) (insert " "))
7821 ((equal (char-after) ?\s
) (forward-char 1))))))
7823 (defun org-current-level ()
7824 "Return the level of the current entry, or nil if before the first headline.
7825 The level is the number of stars at the beginning of the
7826 headline. Use `org-reduced-level' to remove the effect of
7827 `org-odd-levels'. Unlike to `org-outline-level', this function
7828 ignores inlinetasks."
7829 (let ((level (org-with-limited-levels (org-outline-level))))
7830 (and (> level
0) level
)))
7832 (defun org-get-previous-line-level ()
7833 "Return the outline depth of the last headline before the current line.
7834 Returns 0 for the first headline in the buffer, and nil if before the
7836 (and (org-current-level)
7837 (or (and (/= (line-beginning-position) (point-min))
7838 (save-excursion (beginning-of-line 0) (org-current-level)))
7841 (defun org-reduced-level (l)
7842 "Compute the effective level of a heading.
7843 This takes into account the setting of `org-odd-levels-only'."
7846 (org-odd-levels-only (1+ (floor (/ l
2))))
7849 (defun org-level-increment ()
7850 "Return the number of stars that will be added or removed at a
7851 time to headlines when structure editing, based on the value of
7852 `org-odd-levels-only'."
7853 (if org-odd-levels-only
2 1))
7855 (defun org-get-valid-level (level &optional change
)
7856 "Rectify a level change under the influence of `org-odd-levels-only'.
7857 LEVEL is a current level, CHANGE is by how much the level should
7858 be modified. Even if CHANGE is nil, LEVEL may be returned
7859 modified because even level numbers will become the next higher
7860 odd number. Returns values greater than 0."
7861 (if org-odd-levels-only
7862 (cond ((or (not change
) (= 0 change
)) (1+ (* 2 (/ level
2))))
7863 ((> change
0) (1+ (* 2 (/ (+ (1- level
) (* 2 change
)) 2))))
7864 ((< change
0) (max 1 (1+ (* 2 (/ (+ level
(* 2 change
)) 2))))))
7865 (max 1 (+ level
(or change
0)))))
7867 (defun org-promote ()
7868 "Promote the current heading higher up the tree."
7869 (org-with-wide-buffer
7870 (org-back-to-heading t
)
7871 (let* ((after-change-functions (remq 'flyspell-after-change-function
7872 after-change-functions
))
7873 (level (save-match-data (funcall outline-level
)))
7874 (up-head (concat (make-string (org-get-valid-level level -
1) ?
*) " "))
7875 (diff (abs (- level
(length up-head
) -
1))))
7877 ((and (= level
1) org-allow-promoting-top-level-subtree
)
7878 (replace-match "# " nil t
))
7880 (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
7881 (t (replace-match up-head nil t
)))
7883 (when org-auto-align-tags
(org-set-tags nil
'ignore-column
))
7884 (when org-adapt-indentation
(org-fixup-indentation (- diff
))))
7885 (run-hooks 'org-after-promote-entry-hook
))))
7887 (defun org-demote ()
7888 "Demote the current heading lower down the tree."
7889 (org-with-wide-buffer
7890 (org-back-to-heading t
)
7891 (let* ((after-change-functions (remq 'flyspell-after-change-function
7892 after-change-functions
))
7893 (level (save-match-data (funcall outline-level
)))
7894 (down-head (concat (make-string (org-get-valid-level level
1) ?
*) " "))
7895 (diff (abs (- level
(length down-head
) -
1))))
7896 (replace-match down-head nil t
)
7897 (when org-auto-align-tags
(org-set-tags nil
'ignore-column
))
7898 (when org-adapt-indentation
(org-fixup-indentation diff
))
7899 (run-hooks 'org-after-demote-entry-hook
))))
7901 (defun org-cycle-level ()
7902 "Cycle the level of an empty headline through possible states.
7903 This goes first to child, then to parent, level, then up the hierarchy.
7904 After top level, it switches back to sibling level."
7906 (let ((org-adapt-indentation nil
))
7907 (when (org-point-at-end-of-empty-headline)
7908 (setq this-command
'org-cycle-level
) ; Only needed for caching
7909 (let ((cur-level (org-current-level))
7910 (prev-level (org-get-previous-line-level)))
7912 ;; If first headline in file, promote to top-level.
7914 (cl-loop repeat
(/ (- cur-level
1) (org-level-increment))
7915 do
(org-do-promote)))
7916 ;; If same level as prev, demote one.
7917 ((= prev-level cur-level
)
7919 ;; If parent is top-level, promote to top level if not already.
7921 (cl-loop repeat
(/ (- cur-level
1) (org-level-increment))
7922 do
(org-do-promote)))
7923 ;; If top-level, return to prev-level.
7925 (cl-loop repeat
(/ (- prev-level
1) (org-level-increment))
7926 do
(org-do-demote)))
7927 ;; If less than prev-level, promote one.
7928 ((< cur-level prev-level
)
7930 ;; If deeper than prev-level, promote until higher than
7932 ((> cur-level prev-level
)
7933 (cl-loop repeat
(+ 1 (/ (- cur-level prev-level
) (org-level-increment)))
7934 do
(org-do-promote))))
7937 (defun org-map-tree (fun)
7938 "Call FUN for every heading underneath the current one."
7939 (org-back-to-heading t
)
7940 (let ((level (funcall outline-level
)))
7944 (outline-next-heading)
7945 (> (funcall outline-level
) level
))
7949 (defun org-map-region (fun beg end
)
7950 "Call FUN for every heading between BEG and END."
7951 (let ((org-ignore-region t
))
7953 (setq end
(copy-marker end
))
7955 (when (and (re-search-forward org-outline-regexp-bol nil t
)
7959 (outline-next-heading)
7964 (defun org-fixup-indentation (diff)
7965 "Change the indentation in the current entry by DIFF.
7967 DIFF is an integer. Indentation is done according to the
7970 - Planning information and property drawers are always indented
7971 according to the new level of the headline;
7973 - Footnote definitions and their contents are ignored;
7975 - Inlinetasks' boundaries are not shifted;
7977 - Empty lines are ignored;
7979 - Other lines' indentation are shifted by DIFF columns, unless
7980 it would introduce a structural change in the document, in
7981 which case no shifting is done at all.
7983 Assume point is at a heading or an inlinetask beginning."
7984 (org-with-wide-buffer
7985 (narrow-to-region (line-beginning-position)
7987 (if (org-with-limited-levels (org-at-heading-p))
7988 (org-with-limited-levels (outline-next-heading))
7989 (org-inlinetask-goto-end))
7992 ;; Indent properly planning info and property drawer.
7993 (when (looking-at-p org-planning-line-re
)
7996 (when (looking-at org-property-drawer-re
)
7997 (goto-char (match-end 0))
7999 (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
8001 (when (zerop diff
) (throw 'no-shift nil
))
8002 ;; If DIFF is negative, first check if a shift is possible at all
8003 ;; (e.g., it doesn't break structure). This can only happen if
8004 ;; some contents are not properly indented.
8005 (let ((case-fold-search t
))
8007 (let ((diff (- diff
))
8008 (forbidden-re (concat org-outline-regexp
8010 (substring org-footnote-definition-re
1))))
8014 ((looking-at-p "[ \t]*$") (forward-line))
8015 ((and (looking-at-p org-footnote-definition-re
)
8016 (let ((e (org-element-at-point)))
8017 (and (eq (org-element-type e
) 'footnote-definition
)
8018 (goto-char (org-element-property :end e
))))))
8019 ((looking-at-p org-outline-regexp
) (forward-line))
8020 ;; Give up if shifting would move before column 0 or
8021 ;; if it would introduce a headline or a footnote
8024 (skip-chars-forward " \t")
8025 (let ((ind (current-column)))
8026 (when (or (< ind diff
)
8027 (and (= ind diff
) (looking-at-p forbidden-re
)))
8028 (throw 'no-shift nil
)))
8029 ;; Ignore contents of example blocks and source
8030 ;; blocks if their indentation is meant to be
8031 ;; preserved. Jump to block's closing line.
8033 (or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
8034 (let ((e (org-element-at-point)))
8035 (and (memq (org-element-type e
)
8036 '(example-block src-block
))
8037 (or org-src-preserve-indentation
8038 (org-element-property :preserve-indent e
))
8039 (goto-char (org-element-property :end e
))
8040 (progn (skip-chars-backward " \r\t\n")
8043 (forward-line))))))))
8044 ;; Shift lines but footnote definitions, inlinetasks boundaries
8045 ;; by DIFF. Also skip contents of source or example blocks
8046 ;; when indentation is meant to be preserved.
8049 ((and (looking-at-p org-footnote-definition-re
)
8050 (let ((e (org-element-at-point)))
8051 (and (eq (org-element-type e
) 'footnote-definition
)
8052 (goto-char (org-element-property :end e
))))))
8053 ((looking-at-p org-outline-regexp
) (forward-line))
8054 ((looking-at-p "[ \t]*$") (forward-line))
8056 (indent-line-to (+ (org-get-indentation) diff
))
8058 (or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
8059 (let ((e (org-element-at-point)))
8060 (and (memq (org-element-type e
)
8061 '(example-block src-block
))
8062 (or org-src-preserve-indentation
8063 (org-element-property :preserve-indent e
))
8064 (goto-char (org-element-property :end e
))
8065 (progn (skip-chars-backward " \r\t\n")
8068 (forward-line)))))))))
8070 (defun org-convert-to-odd-levels ()
8071 "Convert an Org file with all levels allowed to one with odd levels.
8072 This will leave level 1 alone, convert level 2 to level 3, level 3 to
8075 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
8076 (let ((outline-level 'org-outline-level
)
8077 (org-odd-levels-only nil
) n
)
8079 (goto-char (point-min))
8080 (while (re-search-forward "^\\*\\*+ " nil t
)
8081 (setq n
(- (length (match-string 0)) 2))
8082 (while (>= (setq n
(1- n
)) 0)
8084 (end-of-line 1))))))
8086 (defun org-convert-to-oddeven-levels ()
8087 "Convert an Org file with only odd levels to one with odd/even levels.
8088 This promotes level 3 to level 2, level 5 to level 3 etc. If the
8089 file contains a section with an even level, conversion would
8090 destroy the structure of the file. An error is signaled in this
8093 (goto-char (point-min))
8094 ;; First check if there are no even levels
8095 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t
)
8096 (org-show-set-visibility 'canonical
)
8097 (error "Not all levels are odd in this file. Conversion not possible"))
8098 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
8099 (let ((outline-regexp org-outline-regexp
)
8100 (outline-level 'org-outline-level
)
8101 (org-odd-levels-only nil
) n
)
8103 (goto-char (point-min))
8104 (while (re-search-forward "^\\*\\*+ " nil t
)
8105 (setq n
(/ (1- (length (match-string 0))) 2))
8106 (while (>= (setq n
(1- n
)) 0)
8108 (end-of-line 1))))))
8110 (defun org-tr-level (n)
8111 "Make N odd if required."
8112 (if org-odd-levels-only
(1+ (/ n
2)) n
))
8114 ;;; Vertical tree motion, cutting and pasting of subtrees
8116 (defun org-move-subtree-up (&optional arg
)
8117 "Move the current subtree up past ARG headlines of the same level."
8119 (org-move-subtree-down (- (prefix-numeric-value arg
))))
8121 (defun org-move-subtree-down (&optional arg
)
8122 "Move the current subtree down past ARG headlines of the same level."
8124 (setq arg
(prefix-numeric-value arg
))
8125 (org-preserve-local-variables
8126 (let ((movfunc (if (> arg
0) 'org-get-next-sibling
8127 'org-get-last-sibling
))
8128 (ins-point (make-marker))
8130 (col (current-column))
8131 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end
)
8133 (org-back-to-heading)
8136 (setq ne-beg
(org-back-over-empty-lines))
8139 (save-excursion (outline-end-of-heading)
8140 (setq folded
(org-invisible-p)))
8141 (progn (org-end-of-subtree nil t
)
8142 (unless (eobp) (backward-char))))
8143 (outline-next-heading)
8144 (setq ne-end
(org-back-over-empty-lines))
8147 (when (and (> arg
0) (org-first-sibling-p) (< ne-end ne-beg
))
8148 ;; include less whitespace
8151 (forward-line (- ne-beg ne-end
))
8152 (setq beg
(point))))
8153 ;; Find insertion point, with error handling
8155 (unless (and (funcall movfunc
) (looking-at org-outline-regexp
))
8157 (user-error "Cannot move past superior level or buffer limit"))
8158 (setq cnt
(1- cnt
)))
8160 ;; Moving forward - still need to move over subtree
8161 (org-end-of-subtree t t
)
8163 (org-back-over-empty-lines)
8164 (or (bolp) (newline))))
8165 (setq ne-ins
(org-back-over-empty-lines))
8166 (move-marker ins-point
(point))
8167 (setq txt
(buffer-substring beg end
))
8168 (org-save-markers-in-region beg end
)
8169 (delete-region beg end
)
8170 (org-remove-empty-overlays-at beg
)
8171 (unless (= beg
(point-min)) (org-flag-region (1- beg
) beg nil
'outline
))
8172 (unless (bobp) (org-flag-region (1- (point)) (point) nil
'outline
))
8173 (and (not (bolp)) (looking-at "\n") (forward-char 1))
8174 (let ((bbb (point)))
8175 (insert-before-markers txt
)
8176 (org-reinstall-markers-in-region bbb
)
8177 (move-marker ins-point bbb
))
8178 (or (bolp) (insert "\n"))
8179 (setq ins-end
(point))
8180 (goto-char ins-point
)
8181 (org-skip-whitespace)
8182 (when (and (< arg
0)
8183 (org-first-sibling-p)
8185 ;; Move whitespace back to beginning
8188 (let ((kill-whole-line t
))
8189 (kill-line (- ne-ins ne-beg
)) (point)))
8190 (insert (make-string (- ne-ins ne-beg
) ?
\n)))
8191 (move-marker ins-point nil
)
8193 (outline-hide-subtree)
8195 (org-show-children))
8196 (org-clean-visibility-after-subtree-move)
8197 ;; move back to the initial column we were at
8198 (move-to-column col
))))
8200 (defvar org-subtree-clip
""
8201 "Clipboard for cut and paste of subtrees.
8202 This is actually only a copy of the kill, because we use the normal kill
8203 ring. We need it to check if the kill was created by `org-copy-subtree'.")
8205 (defvar org-subtree-clip-folded nil
8206 "Was the last copied subtree folded?
8207 This is used to fold the tree back after pasting.")
8209 (defun org-cut-subtree (&optional n
)
8210 "Cut the current subtree into the clipboard.
8211 With prefix arg N, cut this many sequential subtrees.
8212 This is a short-hand for marking the subtree and then cutting it."
8214 (org-copy-subtree n
'cut
))
8216 (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees
)
8217 "Copy the current subtree into the clipboard.
8218 With prefix arg N, copy this many sequential subtrees.
8219 This is a short-hand for marking the subtree and then copying it.
8220 If CUT is non-nil, actually cut the subtree.
8221 If FORCE-STORE-MARKERS is non-nil, store the relative locations
8222 of some markers in the region, even if CUT is non-nil. This is
8223 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
8225 (org-preserve-local-variables
8226 (let (beg end folded
(beg0 (point)))
8227 (if (called-interactively-p 'any
)
8228 (org-back-to-heading nil
) ; take what looks like a subtree
8229 (org-back-to-heading t
)) ; take what is really there
8231 (skip-chars-forward " \t\r\n")
8234 (outline-next-heading)
8235 (save-excursion (outline-end-of-heading)
8236 (setq folded
(org-invisible-p)))
8237 (ignore-errors (org-forward-heading-same-level (1- n
) t
))
8238 (org-end-of-subtree t t
)))
8239 ;; Include the end of an inlinetask
8240 (when (and (featurep 'org-inlinetask
)
8241 (looking-at-p (concat (org-inlinetask-outline-regexp)
8247 (setq org-subtree-clip-folded folded
)
8248 (when (or cut force-store-markers
)
8249 (org-save-markers-in-region beg end
))
8250 (if cut
(kill-region beg end
) (copy-region-as-kill beg end
))
8251 (setq org-subtree-clip
(current-kill 0))
8252 (message "%s: Subtree(s) with %d characters"
8253 (if cut
"Cut" "Copied")
8254 (length org-subtree-clip
))))))
8256 (defun org-paste-subtree (&optional level tree for-yank remove
)
8257 "Paste the clipboard as a subtree, with modification of headline level.
8259 The entire subtree is promoted or demoted in order to match a new headline
8262 If the cursor is at the beginning of a headline, the same level as
8263 that headline is used to paste the tree.
8265 If not, the new level is derived from the *visible* headings
8266 before and after the insertion point, and taken to be the inferior headline
8267 level of the two. So if the previous visible heading is level 3 and the
8268 next is level 4 (or vice versa), level 4 will be used for insertion.
8269 This makes sure that the subtree remains an independent subtree and does
8270 not swallow low level entries.
8272 You can also force a different level, either by using a numeric prefix
8273 argument, or by inserting the heading marker by hand. For example, if the
8274 cursor is after \"*****\", then the tree will be shifted to level 5.
8276 If optional TREE is given, use this text instead of the kill ring.
8278 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
8279 move back over whitespace before inserting, and move point to the end of
8280 the inserted text when done.
8282 When REMOVE is non-nil, remove the subtree from the clipboard."
8284 (setq tree
(or tree
(and kill-ring
(current-kill 0))))
8285 (unless (org-kill-is-subtree-p tree
)
8287 (substitute-command-keys
8288 "The kill is not a (set of) tree(s). Use `\\[yank]' to yank anyway")))
8289 (org-with-limited-levels
8290 (let* ((visp (not (org-invisible-p)))
8292 (old-level (if (string-match org-outline-regexp-bol txt
)
8293 (- (match-end 0) (match-beginning 0) 1)
8297 (level (prefix-numeric-value level
))
8298 ;; When point is right after the stars in an otherwise
8299 ;; empty headline, use stars as the forced level.
8300 ((and (looking-at-p "[ \t]*$")
8301 (string-match-p "^\\*+ *"
8302 (buffer-substring (line-beginning-position)
8304 (org-outline-level))
8305 ((looking-at-p org-outline-regexp-bol
) (org-outline-level))))
8308 (org-previous-visible-heading 1)
8309 (if (org-at-heading-p) (org-outline-level) 1)))
8312 (if (org-at-heading-p) (org-outline-level)
8313 (org-next-visible-heading 1)
8314 (if (org-at-heading-p) (org-outline-level) 1))))
8315 (new-level (or force-level
(max previous-level next-level
)))
8316 (shift (if (or (= old-level -
1)
8318 (= old-level new-level
))
8320 (- new-level old-level
)))
8321 (delta (if (> shift
0) -
1 1))
8322 (func (if (> shift
0) #'org-demote
#'org-promote
))
8323 (org-odd-levels-only nil
)
8325 ;; Remove the forced level indicator.
8326 (when (and force-level
(not level
))
8327 (delete-region (line-beginning-position) (point)))
8328 ;; Paste before the next visible heading or at end of buffer,
8329 ;; unless point is at the beginning of a headline.
8330 (unless (and (bolp) (org-at-heading-p))
8331 (org-next-visible-heading 1)
8332 (unless (bolp) (insert "\n")))
8334 (when (fboundp 'org-id-paste-tracker
) (org-id-paste-tracker txt
))
8335 (insert-before-markers txt
)
8336 (unless (string-suffix-p "\n" txt
) (insert "\n"))
8337 (setq newend
(point))
8338 (org-reinstall-markers-in-region beg
)
8341 (skip-chars-forward " \t\n\r")
8343 (when (and (org-invisible-p) visp
)
8344 (save-excursion (outline-show-heading)))
8345 ;; Shift if necessary.
8348 (narrow-to-region beg end
)
8349 (while (not (= shift
0))
8350 (org-map-region func
(point-min) (point-max))
8351 (setq shift
(+ delta shift
)))
8352 (goto-char (point-min))
8353 (setq newend
(point-max))))
8354 (when (or for-yank
(called-interactively-p 'interactive
))
8355 (message "Clipboard pasted as level %d subtree" new-level
))
8356 (when (and (not for-yank
) ; in this case, org-yank will decide about folding
8358 (equal org-subtree-clip
(current-kill 0))
8359 org-subtree-clip-folded
)
8360 ;; The tree was folded before it was killed/copied
8361 (outline-hide-subtree))
8362 (when for-yank
(goto-char newend
))
8363 (when remove
(pop kill-ring
)))))
8365 (defun org-kill-is-subtree-p (&optional txt
)
8366 "Check if the current kill is an outline subtree, or a set of trees.
8367 Returns nil if kill does not start with a headline, or if the first
8368 headline level is not the largest headline level in the tree.
8369 So this will actually accept several entries of equal levels as well,
8370 which is OK for `org-paste-subtree'.
8371 If optional TXT is given, check this string instead of the current kill."
8372 (let* ((kill (or txt
(and kill-ring
(current-kill 0)) ""))
8373 (re (org-get-limited-outline-regexp))
8374 (^re
(concat "^" re
))
8375 (start-level (and kill
8377 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re
"\\)")
8379 (- (match-end 2) (match-beginning 2) 1)))
8380 (start (1+ (or (match-beginning 2) -
1))))
8381 (if (not start-level
)
8383 nil
) ;; does not even start with a heading
8385 (while (setq start
(string-match ^re kill
(1+ start
)))
8386 (when (< (- (match-end 0) (match-beginning 0) 1) start-level
)
8390 (defvar org-markers-to-move nil
8391 "Markers that should be moved with a cut-and-paste operation.
8392 Those markers are stored together with their positions relative to
8393 the start of the region.")
8395 (defun org-save-markers-in-region (beg end
)
8396 "Check markers in region.
8397 If these markers are between BEG and END, record their position relative
8398 to BEG, so that after moving the block of text, we can put the markers back
8400 This function gets called just before an entry or tree gets cut from the
8401 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
8402 called immediately, to move the markers with the entries."
8403 (setq org-markers-to-move nil
)
8404 (when (featurep 'org-clock
)
8405 (org-clock-save-markers-for-cut-and-paste beg end
))
8406 (when (featurep 'org-agenda
)
8407 (org-agenda-save-markers-for-cut-and-paste beg end
)))
8409 (defun org-check-and-save-marker (marker beg end
)
8410 "Check if MARKER is between BEG and END.
8411 If yes, remember the marker and the distance to BEG."
8412 (when (and (marker-buffer marker
)
8413 (equal (marker-buffer marker
) (current-buffer))
8414 (>= marker beg
) (< marker end
))
8415 (push (cons marker
(- marker beg
)) org-markers-to-move
)))
8417 (defun org-reinstall-markers-in-region (beg)
8418 "Move all remembered markers to their position relative to BEG."
8419 (dolist (x org-markers-to-move
)
8420 (move-marker (car x
) (+ beg
(cdr x
))))
8421 (setq org-markers-to-move nil
))
8423 (defun org-narrow-to-subtree ()
8424 "Narrow buffer to the current subtree."
8428 (org-with-limited-levels
8430 (progn (org-back-to-heading t
) (point))
8431 (progn (org-end-of-subtree t t
)
8432 (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
8435 (defun org-narrow-to-block ()
8436 "Narrow buffer to the current block."
8438 (let* ((case-fold-search t
)
8439 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
8440 "^[ \t]*#\\+end_.*")))
8442 (narrow-to-region (car blockp
) (cdr blockp
))
8443 (user-error "Not in a block"))))
8445 (defun org-clone-subtree-with-time-shift (n &optional shift
)
8446 "Clone the task (subtree) at point N times.
8447 The clones will be inserted as siblings.
8449 In interactive use, the user will be prompted for the number of
8450 clones to be produced. If the entry has a timestamp, the user
8451 will also be prompted for a time shift, which may be a repeater
8452 as used in time stamps, for example `+3d'. To disable this,
8453 you can call the function with a universal prefix argument.
8455 When a valid repeater is given and the entry contains any time
8456 stamps, the clones will become a sequence in time, with time
8457 stamps in the subtree shifted for each clone produced. If SHIFT
8458 is nil or the empty string, time stamps will be left alone. The
8459 ID property of the original subtree is removed.
8461 In each clone, all the CLOCK entries will be removed. This
8462 prevents Org from considering that the clocked times overlap.
8464 If the original subtree did contain time stamps with a repeater,
8465 the following will happen:
8466 - the repeater will be removed in each clone
8467 - an additional clone will be produced, with the current, unshifted
8468 date(s) in the entry.
8469 - the original entry will be placed *after* all the clones, with
8471 - the start days in the repeater in the original entry will be shifted
8472 to past the last clone.
8473 In this way you can spell out a number of instances of a repeating task,
8474 and still retain the repeater to cover future instances of the task.
8476 As described above, N+1 clones are produced when the original
8477 subtree has a repeater. Setting N to 0, then, can be used to
8478 remove the repeater from a subtree and create a shifted clone
8479 with the original repeater."
8480 (interactive "nNumber of clones to produce: ")
8481 (unless (wholenump n
) (user-error "Invalid number of replications %s" n
))
8482 (when (org-before-first-heading-p) (user-error "No subtree to clone"))
8483 (let* ((beg (save-excursion (org-back-to-heading t
) (point)))
8484 (end-of-tree (save-excursion (org-end-of-subtree t t
) (point)))
8487 (if (and (not (equal current-prefix-arg
'(4)))
8490 (re-search-forward org-ts-regexp-both end-of-tree t
)))
8491 (read-from-minibuffer
8492 "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
8493 ""))) ;No time shift
8495 (and (org-string-nw-p shift
)
8496 (or (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
8498 (user-error "Invalid shift specification %s" shift
)))))
8499 (goto-char end-of-tree
)
8500 (unless (bolp) (insert "\n"))
8501 (let* ((end (point))
8502 (template (buffer-substring beg end
))
8503 (shift-n (and doshift
(string-to-number (match-string 1 shift
))))
8504 (shift-what (pcase (and doshift
(match-string 2 shift
))
8507 ("w" (setq shift-n
(* 7 shift-n
)) 'day
)
8510 (_ (error "Unsupported time unit"))))
8514 (idprop (org-entry-get nil
"ID")))
8516 (string-match-p "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>"
8518 (delete-region beg end
)
8521 (setq nmax
(1+ nmax
))
8522 (setq n-no-remove nmax
))
8524 (cl-loop for n from nmin to nmax do
8530 (goto-char (point-min))
8532 (and idprop
(if org-clone-delete-id
8533 (org-entry-delete nil
"ID")
8534 (org-id-get-create t
)))
8536 (while (re-search-forward org-clock-line-re nil t
)
8537 (delete-region (line-beginning-position)
8538 (line-beginning-position 2)))
8539 (goto-char (point-min))
8540 (while (re-search-forward org-drawer-regexp nil t
)
8541 (org-remove-empty-drawer-at (point))))
8542 (goto-char (point-min))
8544 (while (re-search-forward org-ts-regexp-both nil t
)
8545 (org-timestamp-change (* n shift-n
) shift-what
))
8546 (unless (= n n-no-remove
)
8547 (goto-char (point-min))
8548 (while (re-search-forward org-ts-regexp nil t
)
8550 (goto-char (match-beginning 0))
8551 (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
8552 (delete-region (match-beginning 1) (match-end 1)))))))
8558 (defun org-sort (&optional with-case
)
8559 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8560 Optional argument WITH-CASE means sort case-sensitively."
8563 (cond ((org-at-table-p) #'org-table-sort-lines
)
8564 ((org-at-item-p) #'org-sort-list
)
8565 (t #'org-sort-entries
))
8568 (defun org-sort-remove-invisible (s)
8569 "Remove invisible part of links and emphasis markers from string S."
8570 (remove-text-properties 0 (length s
) org-rm-props s
)
8571 (replace-regexp-in-string
8572 org-verbatim-re
(lambda (m) (format "%s " (match-string 4 m
)))
8573 (replace-regexp-in-string
8574 org-emph-re
(lambda (m) (format " %s " (match-string 4 m
)))
8575 (org-link-display-format s
)
8578 (defvar org-priority-regexp
) ; defined later in the file
8580 (defvar org-after-sorting-entries-or-items-hook nil
8581 "Hook that is run after a bunch of entries or items have been sorted.
8582 When children are sorted, the cursor is in the parent line when this
8583 hook gets called. When a region or a plain list is sorted, the cursor
8584 will be in the first entry of the sorted region/list.")
8586 (defun org-sort-entries
8587 (&optional with-case sorting-type getkey-func compare-func property
8589 "Sort entries on a certain level of an outline tree.
8590 If there is an active region, the entries in the region are sorted.
8591 Else, if the cursor is before the first entry, sort the top-level items.
8592 Else, the children of the entry at point are sorted.
8594 Sorting can be alphabetically, numerically, by date/time as given by
8595 a time stamp, by a property, by priority order, or by a custom function.
8597 The command prompts for the sorting type unless it has been given to the
8598 function through the SORTING-TYPE argument, which needs to be a character,
8599 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
8600 the precise meaning of each character:
8602 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8603 c By creation time, which is assumed to be the first inactive time stamp
8604 at the beginning of a line.
8605 d By deadline date/time.
8607 n Numerically, by converting the beginning of the entry/item to a number.
8608 o By order of TODO keywords.
8609 p By priority according to the cookie.
8610 r By the value of a property.
8611 s By scheduled date/time.
8612 t By date/time, either the first active time stamp in the entry, or, if
8613 none exist, by the first inactive one.
8615 Capital letters will reverse the sort order.
8617 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8618 called with point at the beginning of the record. It must return a
8619 value that is compatible with COMPARE-FUNC, the function used to
8622 Comparing entries ignores case by default. However, with an optional argument
8623 WITH-CASE, the sorting considers case as well.
8625 Sorting is done against the visible part of the headlines, it ignores hidden
8628 When sorting is done, call `org-after-sorting-entries-or-items-hook'.
8630 A non-nil value for INTERACTIVE? is used to signal that this
8631 function is being called interactively."
8632 (interactive (list current-prefix-arg nil nil nil nil t
))
8633 (let ((case-func (if with-case
'identity
'downcase
))
8634 start beg end stars re re2
8636 ;; Find beginning and end of region to sort
8638 ((org-region-active-p)
8639 ;; we will sort the region
8640 (setq end
(region-end)
8642 (goto-char (region-beginning))
8643 (unless (org-at-heading-p) (outline-next-heading))
8644 (setq start
(point)))
8645 ((or (org-at-heading-p)
8646 (ignore-errors (progn (org-back-to-heading) t
)))
8647 ;; we will sort the children of the current headline
8648 (org-back-to-heading)
8650 end
(progn (org-end-of-subtree t t
)
8651 (or (bolp) (insert "\n"))
8652 (when (>= (org-back-over-empty-lines) 1)
8657 (outline-show-subtree)
8658 (outline-next-heading))
8660 ;; we will sort the top-level entries in this file
8661 (goto-char (point-min))
8662 (or (org-at-heading-p) (outline-next-heading))
8663 (setq start
(point))
8664 (goto-char (point-max))
8665 (beginning-of-line 1)
8666 (when (looking-at ".*?\\S-")
8667 ;; File ends in a non-white line
8670 (setq end
(point-max))
8671 (setq what
"top-level")
8673 (org-show-all '(headings blocks
))))
8676 (when (>= beg end
) (goto-char start
) (user-error "Nothing to sort"))
8678 (looking-at "\\(\\*+\\)")
8679 (setq stars
(match-string 1)
8680 re
(concat "^" (regexp-quote stars
) " +")
8681 re2
(concat "^" (regexp-quote (substring stars
0 -
1)) "[ \t\n]")
8682 txt
(buffer-substring beg end
))
8683 (unless (equal (substring txt -
1) "\n") (setq txt
(concat txt
"\n")))
8684 (when (and (not (equal stars
"*")) (string-match re2 txt
))
8685 (user-error "Region to sort contains a level above the first entry"))
8687 (unless sorting-type
8689 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8690 [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
8691 A/N/P/R/O/F/T/S/D/C/K means reversed:"
8693 (setq sorting-type
(read-char-exclusive)))
8696 (and (= (downcase sorting-type
) ?f
)
8698 (or (and interactive?
8700 "Function for extracting keys: "))
8701 (error "Missing key extractor")))))
8703 (and (= (downcase sorting-type
) ?r
)
8706 (completing-read "Property: "
8707 (mapcar #'list
(org-buffer-property-keys t
))
8710 (when (member sorting-type
'(?k ?K
)) (org-clock-sum))
8711 (message "Sorting entries...")
8714 (narrow-to-region start end
)
8715 (let ((restore-clock?
8716 ;; The clock marker is lost when using `sort-subr'; mark
8717 ;; the clock with temporary `:org-clock-marker-backup'
8719 (when (and (eq (org-clock-is-active) (current-buffer))
8720 (<= start
(marker-position org-clock-marker
))
8721 (>= end
(marker-position org-clock-marker
)))
8722 (org-with-silent-modifications
8723 (put-text-property (1- org-clock-marker
) org-clock-marker
8724 :org-clock-marker-backup t
))
8726 (dcst (downcase sorting-type
))
8727 (case-fold-search nil
)
8728 (now (current-time)))
8729 (org-preserve-local-variables
8731 (/= dcst sorting-type
)
8732 ;; This function moves to the beginning character of the
8733 ;; "record" to be sorted.
8735 (if (re-search-forward re nil t
)
8736 (goto-char (match-beginning 0))
8737 (goto-char (point-max))))
8738 ;; This function moves to the last character of the "record" being
8743 (outline-forward-same-level 1)
8745 (goto-char (point-max))))))
8746 ;; This function returns the value that gets sorted against.
8751 (org-sort-remove-invisible (org-get-heading t t t t
))))
8754 (org-sort-remove-invisible (org-get-heading t t t t
))))
8756 (or (get-text-property (point) :org-clock-minutes
) 0))
8758 (let ((end (save-excursion (outline-next-heading) (point))))
8759 (if (or (re-search-forward org-ts-regexp end t
)
8760 (re-search-forward org-ts-regexp-both end t
))
8761 (org-time-string-to-seconds (match-string 0))
8764 (let ((end (save-excursion (outline-next-heading) (point))))
8765 (if (re-search-forward
8766 (concat "^[ \t]*\\[" org-ts-regexp1
"\\]")
8768 (org-time-string-to-seconds (match-string 0))
8771 (let ((end (save-excursion (outline-next-heading) (point))))
8772 (if (re-search-forward org-scheduled-time-regexp end t
)
8773 (org-time-string-to-seconds (match-string 1))
8776 (let ((end (save-excursion (outline-next-heading) (point))))
8777 (if (re-search-forward org-deadline-time-regexp end t
)
8778 (org-time-string-to-seconds (match-string 1))
8781 (if (re-search-forward org-priority-regexp
(point-at-eol) t
)
8782 (string-to-char (match-string 2))
8783 org-default-priority
))
8785 (or (org-entry-get nil property
) ""))
8787 (when (looking-at org-complex-heading-regexp
)
8788 (let* ((m (match-string 2))
8789 (s (if (member m org-done-keywords
) '-
'+)))
8790 (- 99 (funcall s
(length (member m org-todo-keywords-1
)))))))
8794 (setq tmp
(funcall getkey-func
))
8795 (when (stringp tmp
) (setq tmp
(funcall case-func tmp
)))
8797 (error "Invalid key function `%s'" getkey-func
)))
8798 (t (error "Invalid sorting type `%c'" sorting-type
))))
8801 ((= dcst ?a
) 'org-string-collate-lessp
)
8806 (concat "Function for comparing keys "
8807 "(empty for default `sort-subr' predicate): ")
8809 ((member dcst
'(?p ?t ?s ?d ?c ?k
)) '<))))
8810 (when restore-clock?
8811 (move-marker org-clock-marker
8812 (1+ (next-single-property-change
8813 start
:org-clock-marker-backup
)))
8814 (remove-text-properties (1- org-clock-marker
) org-clock-marker
8815 '(:org-clock-marker-backup t
)))))
8816 (run-hooks 'org-after-sorting-entries-or-items-hook
)
8817 (message "Sorting entries...done")))
8819 (defun org-contextualize-keys (alist contexts
)
8820 "Return valid elements in ALIST depending on CONTEXTS.
8822 `org-agenda-custom-commands' or `org-capture-templates' are the
8823 values used for ALIST, and `org-agenda-custom-commands-contexts'
8824 or `org-capture-templates-contexts' are the associated contexts
8827 ;; normalize contexts
8829 (lambda(c) (cond ((listp (cadr c
))
8830 (list (car c
) (car c
) (nth 1 c
)))
8831 ((string= "" (cadr c
))
8832 (list (car c
) (car c
) (nth 2 c
)))
8836 ;; loop over all commands or templates
8840 ((not (assoc (car c
) contexts
))
8842 ((and (assoc (car c
) contexts
)
8843 (setq vrules
(org-contextualize-validate-key
8846 (unless (equal (car vr
) (cadr vr
))
8849 (if (not repl
) (push c r
)
8850 (push (cadr repl
) s
)
8853 (cdr (or (assoc (cadr repl
) alist
)
8854 (error "Undefined key `%s' as contextual replacement for `%s'"
8855 (cadr repl
) (car c
)))))
8857 ;; Return limited ALIST, possibly with keys modified, and deduplicated
8862 (let ((tpl (car x
)))
8871 (defun org-contextualize-validate-key (key contexts
)
8872 "Check CONTEXTS for agenda or capture KEY."
8874 (dolist (r contexts
)
8875 (dolist (rr (car (last r
)))
8877 (and (equal key
(car r
))
8878 (if (functionp rr
) (funcall rr
)
8879 (or (and (eq (car rr
) 'in-file
)
8881 (string-match (cdr rr
) (buffer-file-name)))
8882 (and (eq (car rr
) 'in-mode
)
8883 (string-match (cdr rr
) (symbol-name major-mode
)))
8884 (and (eq (car rr
) 'in-buffer
)
8885 (string-match (cdr rr
) (buffer-name)))
8886 (when (and (eq (car rr
) 'not-in-file
)
8888 (not (string-match (cdr rr
) (buffer-file-name))))
8889 (when (eq (car rr
) 'not-in-mode
)
8890 (not (string-match (cdr rr
) (symbol-name major-mode
))))
8891 (when (eq (car rr
) 'not-in-buffer
)
8892 (not (string-match (cdr rr
) (buffer-name)))))))
8894 (delete-dups (delq nil res
))))
8896 ;; Defined to provide a value for defcustom, since there is no
8897 ;; string-collate-greaterp in Emacs.
8898 (defun org-string-collate-greaterp (s1 s2
)
8899 "Return non-nil if S1 is greater than S2 in collation order."
8900 (not (org-string-collate-lessp s1 s2
)))
8903 (defun org-run-like-in-org-mode (cmd)
8904 "Run a command, pretending that the current buffer is in Org mode.
8905 This will temporarily bind local variables that are typically bound in
8906 Org mode to the values they have in Org mode, and then interactively
8908 (org-load-modules-maybe)
8910 (dolist (var (org-get-local-variables))
8911 (when (or (not (boundp (car var
)))
8912 (eq (symbol-value (car var
))
8913 (default-value (car var
))))
8914 (push (list (car var
) `(quote ,(cadr var
))) binds
)))
8916 (call-interactively (quote ,cmd
))))))
8918 (defun org-get-category (&optional pos force-refresh
)
8919 "Get the category applying to position POS."
8921 (when force-refresh
(org-refresh-category-properties))
8922 (let ((pos (or pos
(point))))
8923 (or (get-text-property pos
'org-category
)
8924 (progn (org-refresh-category-properties)
8925 (get-text-property pos
'org-category
))))))
8927 ;;; Refresh properties
8929 (defun org-refresh-properties (dprop tprop
)
8930 "Refresh buffer text properties.
8931 DPROP is the drawer property and TPROP is either the
8932 corresponding text property to set, or an alist with each element
8933 being a text property (as a symbol) and a function to apply to
8934 the value of the drawer property."
8935 (let* ((case-fold-search t
)
8936 (inhibit-read-only t
)
8937 (inherit?
(org-property-inherit-p dprop
))
8938 (property-re (org-re-property (concat (regexp-quote dprop
) "\\+?") t
))
8939 (global (and inherit?
(org--property-global-value dprop nil
))))
8940 (org-with-silent-modifications
8941 (org-with-point-at 1
8942 ;; Set global values (e.g., values defined through
8943 ;; "#+PROPERTY:" keywords) to the whole buffer.
8944 (when global
(put-text-property (point-min) (point-max) tprop global
))
8945 ;; Set local values.
8946 (while (re-search-forward property-re nil t
)
8947 (when (org-at-property-p)
8948 (org-refresh-property tprop
(org-entry-get (point) dprop
) inherit?
))
8949 (outline-next-heading))))))
8951 (defun org-refresh-property (tprop p
&optional inherit
)
8952 "Refresh the buffer text property TPROP from the drawer property P.
8953 The refresh happens only for the current headline, or the whole
8954 sub-tree if optional argument INHERIT is non-nil."
8955 (unless (org-before-first-heading-p)
8957 (org-back-to-heading t
)
8958 (let ((start (point))
8959 (end (save-excursion
8960 (if inherit
(org-end-of-subtree t t
)
8961 (or (outline-next-heading) (point-max))))))
8963 ;; TPROP is a text property symbol.
8964 (put-text-property start end tprop p
)
8965 ;; TPROP is an alist with (property . function) elements.
8966 (pcase-dolist (`(,prop .
,f
) tprop
)
8967 (put-text-property start end prop
(funcall f p
))))))))
8969 (defun org-refresh-category-properties ()
8970 "Refresh category text properties in the buffer."
8971 (let ((case-fold-search t
)
8972 (inhibit-read-only t
)
8974 (cond ((null org-category
)
8975 (if buffer-file-name
8976 (file-name-sans-extension
8977 (file-name-nondirectory buffer-file-name
))
8979 ((symbolp org-category
) (symbol-name org-category
))
8981 (org-with-silent-modifications
8982 (org-with-wide-buffer
8983 ;; Set buffer-wide category. Search last #+CATEGORY keyword.
8984 ;; This is the default category for the buffer. If none is
8985 ;; found, fall-back to `org-category' or buffer file name.
8987 (point-min) (point-max)
8989 (catch 'buffer-category
8990 (goto-char (point-max))
8991 (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t
)
8992 (let ((element (org-element-at-point)))
8993 (when (eq (org-element-type element
) 'keyword
)
8994 (throw 'buffer-category
8995 (org-element-property :value element
)))))
8997 ;; Set sub-tree specific categories.
8998 (goto-char (point-min))
8999 (let ((regexp (org-re-property "CATEGORY")))
9000 (while (re-search-forward regexp nil t
)
9001 (let ((value (match-string-no-properties 3)))
9002 (when (org-at-property-p)
9004 (save-excursion (org-back-to-heading t
) (point))
9005 (save-excursion (org-end-of-subtree t t
) (point))
9009 (defun org-refresh-stats-properties ()
9010 "Refresh stats text properties in the buffer."
9011 (org-with-silent-modifications
9012 (org-with-point-at 1
9013 (let ((regexp (concat org-outline-regexp-bol
9014 ".*\\[\\([0-9]*\\)\\(?:%\\|/\\([0-9]*\\)\\)\\]")))
9015 (while (re-search-forward regexp nil t
)
9016 (let* ((numerator (string-to-number (match-string 1)))
9017 (denominator (and (match-end 2)
9018 (string-to-number (match-string 2))))
9019 (stats (cond ((not denominator
) numerator
) ;percent
9020 ((= denominator
0) 0)
9021 (t (/ (* numerator
100) denominator
)))))
9022 (put-text-property (point) (progn (org-end-of-subtree t t
) (point))
9023 'org-stats stats
)))))))
9025 (defun org-refresh-effort-properties ()
9026 "Refresh effort properties"
9027 (org-refresh-properties
9029 '((effort . identity
)
9030 (effort-minutes . org-duration-to-minutes
))))
9034 ;;; Link abbreviations
9036 (defun org-link-expand-abbrev (link)
9037 "Apply replacements as defined in `org-link-abbrev-alist'."
9038 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link
)
9039 (let* ((key (match-string 1 link
))
9040 (as (or (assoc key org-link-abbrev-alist-local
)
9041 (assoc key org-link-abbrev-alist
)))
9042 (tag (and (match-end 2) (match-string 3 link
)))
9048 ((symbolp rpl
) (funcall rpl tag
))
9049 ((string-match "%(\\([^)]+\\))" rpl
)
9052 (funcall (intern-soft (match-string 1 rpl
)) tag
)) t t rpl
))
9053 ((string-match "%s" rpl
) (replace-match (or tag
"") t t rpl
))
9054 ((string-match "%h" rpl
)
9055 (replace-match (url-hexify-string (or tag
"")) t t rpl
))
9056 (t (concat rpl tag
)))))
9059 ;;; Storing and inserting links
9061 (defvar org-insert-link-history nil
9062 "Minibuffer history for links inserted with `org-insert-link'.")
9064 (defvar org-stored-links nil
9065 "Contains the links stored with `org-store-link'.")
9067 (defvar org-store-link-plist nil
9068 "Plist with info about the most recently link created with `org-store-link'.")
9070 (defun org-store-link-functions ()
9071 "Return a list of functions that are called to create and store a link.
9072 The functions defined in the :store property of
9073 `org-link-parameters'.
9075 Each function will be called in turn until one returns a non-nil
9076 value. Each function should check if it is responsible for
9077 creating this link (for example by looking at the major mode).
9078 If not, it must exit and return nil. If yes, it should return
9079 a non-nil value after calling `org-store-link-props' with a list
9080 of properties and values. Special properties are:
9082 :type The link prefix, like \"http\". This must be given.
9083 :link The link, like \"http://www.astro.uva.nl/~dominik\".
9084 This is obligatory as well.
9085 :description Optional default description for the second pair
9086 of brackets in an Org mode link. The user can still change
9087 this when inserting this link into an Org mode buffer.
9089 In addition to these, any additional properties can be specified
9090 and then used in capture templates."
9091 (cl-loop for link in org-link-parameters
9093 do
(setq store-func
(org-link-get-parameter (car link
) :store
))
9095 collect store-func
))
9097 (defvar org-agenda-buffer-name
) ; Defined in org-agenda.el
9098 (defvar org-id-link-to-org-use-id
) ; Defined in org-id.el
9101 (defun org-store-link (arg &optional interactive?
)
9102 "Store an org-link to the current location.
9104 This link is added to `org-stored-links' and can later be inserted
9105 into an Org buffer with `org-insert-link' (`\\[org-insert-link]').
9107 For some link types, a `\\[universal-argument]' prefix ARG is interpreted. \
9109 `\\[universal-argument]' negates `org-context-in-file-links' for file links or
9110 `org-gnus-prefer-web-links' for links to Usenet articles.
9112 A `\\[universal-argument] \\[universal-argument]' prefix ARG forces \
9113 skipping storing functions that are not
9116 A `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
9117 prefix ARG forces storing a link for each line in the
9120 Assume the function is called interactively if INTERACTIVE? is
9122 (interactive "P\np")
9123 (org-load-modules-maybe)
9124 (if (and (equal arg
'(64)) (org-region-active-p))
9126 (let ((end (region-end)))
9127 (goto-char (region-beginning))
9129 (while (< (point-at-eol) end
)
9130 (move-end-of-line 1) (activate-mark)
9131 (let (current-prefix-arg)
9132 (call-interactively 'org-store-link
))
9133 (move-beginning-of-line 2)
9134 (set-mark (point)))))
9135 (setq org-store-link-plist nil
)
9136 (let (link cpltxt desc description search txt custom-id agenda-link
)
9138 ;; Store a link using an external link type, if any function is
9139 ;; available. If more than one can generate a link from current
9140 ;; location, ask which one to use.
9141 ((and (not (equal arg
'(16)))
9142 (let ((results-alist nil
))
9143 (dolist (f (org-store-link-functions))
9145 ;; XXX: return value is not link's plist, so we
9146 ;; store the new value before it is modified. It
9147 ;; would be cleaner to ask store link functions to
9148 ;; return the plist instead.
9149 (push (cons f
(copy-sequence org-store-link-plist
))
9151 (pcase results-alist
9153 (`((,_ .
,_
)) t
) ;single choice: nothing to do
9154 (`((,name .
,_
) .
,_
)
9155 ;; Reinstate link plist associated to the chosen
9157 (apply #'org-store-link-props
9160 "Which function for creating the link? "
9161 (mapcar #'car results-alist
)
9162 nil t
(symbol-name name
))
9165 (setq link
(plist-get org-store-link-plist
:link
))
9166 (setq desc
(or (plist-get org-store-link-plist
:description
)
9169 ;; Store a link from a source code buffer.
9170 ((org-src-edit-buffer-p)
9171 (let ((coderef-format (org-src-coderef-format)))
9172 (cond ((org-match-line (org-src-coderef-regexp coderef-format
))
9173 (setq link
(format "(%s)" (match-string-no-properties 3))))
9175 (let ((label (read-string "Code line label: ")))
9177 (setq link
(format coderef-format label
))
9178 (let ((gc (- 79 (length link
))))
9179 (if (< (current-column) gc
)
9180 (org-move-to-column gc t
)
9183 (setq link
(concat "(" label
")"))
9185 (t (setq link nil
)))))
9187 ;; We are in the agenda, link to referenced location
9188 ((equal (bound-and-true-p org-agenda-buffer-name
) (buffer-name))
9189 (let ((m (or (get-text-property (point) 'org-hd-marker
)
9190 (get-text-property (point) 'org-marker
))))
9192 (org-with-point-at m
9193 (setq agenda-link
(org-store-link nil interactive?
))))))
9195 ((eq major-mode
'calendar-mode
)
9196 (let ((cd (calendar-cursor-to-date)))
9199 (car org-time-stamp-formats
)
9201 (list 0 0 0 (nth 1 cd
) (nth 0 cd
) (nth 2 cd
)
9203 (org-store-link-props :type
"calendar" :date cd
)))
9205 ((eq major-mode
'help-mode
)
9206 (setq link
(concat "help:" (save-excursion
9207 (goto-char (point-min))
9208 (looking-at "^[^ ]+")
9210 (org-store-link-props :type
"help"))
9212 ((eq major-mode
'w3-mode
)
9213 (setq cpltxt
(if (and (buffer-name)
9214 (not (string-match "Untitled" (buffer-name))))
9217 link
(url-view-url t
))
9218 (org-store-link-props :type
"w3" :url
(url-view-url t
)))
9220 ((eq major-mode
'image-mode
)
9221 (setq cpltxt
(concat "file:"
9222 (abbreviate-file-name buffer-file-name
))
9224 (org-store-link-props :type
"image" :file buffer-file-name
))
9226 ;; In dired, store a link to the file of the current line
9227 ((derived-mode-p 'dired-mode
)
9228 (let ((file (dired-get-filename nil t
)))
9230 (abbreviate-file-name
9231 (expand-file-name (dired-get-filename nil t
)))
9232 ;; otherwise, no file so use current directory.
9234 (setq cpltxt
(concat "file:" file
)
9237 ((setq search
(run-hook-with-args-until-success
9238 'org-create-file-search-functions
))
9239 (setq link
(concat "file:" (abbreviate-file-name buffer-file-name
)
9241 (setq cpltxt
(or description link
)))
9243 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode
))
9244 (org-with-limited-levels
9245 (setq custom-id
(org-entry-get nil
"CUSTOM_ID"))
9247 ;; Store a link using the target at point
9248 ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
9251 (abbreviate-file-name
9252 (buffer-file-name (buffer-base-buffer)))
9253 "::" (match-string 1))
9255 ((and (featurep 'org-id
)
9256 (or (eq org-id-link-to-org-use-id t
)
9258 (or (eq org-id-link-to-org-use-id
'create-if-interactive
)
9259 (and (eq org-id-link-to-org-use-id
9260 'create-if-interactive-and-no-custom-id
)
9262 (and org-id-link-to-org-use-id
(org-entry-get nil
"ID"))))
9263 ;; Store a link using the ID at point
9264 (setq link
(condition-case nil
9265 (prog1 (org-id-store-link)
9266 (setq desc
(or (plist-get org-store-link-plist
9270 ;; Probably before first headline, link only to file
9272 (abbreviate-file-name
9273 (buffer-file-name (buffer-base-buffer))))))))
9275 ;; Just link to current headline
9276 (setq cpltxt
(concat "file:"
9277 (abbreviate-file-name
9278 (buffer-file-name (buffer-base-buffer)))))
9279 ;; Add a context search string
9280 (when (org-xor org-context-in-file-links
9282 (let* ((element (org-element-at-point))
9283 (name (org-element-property :name element
)))
9285 ((org-at-heading-p) nil
)
9287 ((org-region-active-p)
9288 (buffer-substring (region-beginning) (region-end)))))
9289 (when (or (null txt
) (string-match "\\S-" txt
))
9293 (org-make-org-heading-search-string txt
)
9296 (nth 4 (ignore-errors (org-heading-components)))
9298 (when (string-match "::\\'" cpltxt
)
9299 (setq cpltxt
(substring cpltxt
0 -
2)))
9300 (setq link cpltxt
)))))
9302 ((buffer-file-name (buffer-base-buffer))
9303 ;; Just link to this file here.
9304 (setq cpltxt
(concat "file:"
9305 (abbreviate-file-name
9306 (buffer-file-name (buffer-base-buffer)))))
9307 ;; Add a context string.
9308 (when (org-xor org-context-in-file-links
9310 (setq txt
(if (org-region-active-p)
9311 (buffer-substring (region-beginning) (region-end))
9312 (buffer-substring (point-at-bol) (point-at-eol))))
9313 ;; Only use search option if there is some text.
9314 (when (string-match "\\S-" txt
)
9316 (concat cpltxt
"::" (org-make-org-heading-search-string txt
))
9321 (user-error "No method for storing a link from this buffer"))
9323 (t (setq link nil
)))
9325 ;; We're done setting link and desc, clean up
9326 (when (consp link
) (setq cpltxt
(car link
) link
(cdr link
)))
9327 (setq link
(or link cpltxt
)
9328 desc
(or desc cpltxt
))
9330 ((equal desc
"NONE") (setq desc nil
))
9332 (replace-regexp-in-string
9333 org-bracket-link-analytic-regexp
9334 (lambda (m) (or (match-string 5 m
) (match-string 3 m
)))
9337 (if (not (and interactive? link
))
9338 (or agenda-link
(and link
(org-make-link-string link desc
)))
9339 (push (list link desc
) org-stored-links
)
9340 (message "Stored: %s" (or desc link
))
9342 (setq link
(concat "file:" (abbreviate-file-name
9343 (buffer-file-name)) "::#" custom-id
))
9344 (push (list link desc
) org-stored-links
))
9345 (car org-stored-links
)))))
9347 (defun org-store-link-props (&rest plist
)
9348 "Store link properties.
9349 The properties are pre-processed by extracting names, addresses
9351 (let ((x (plist-get plist
:from
)))
9353 (let ((adr (mail-extract-address-components x
)))
9354 (setq plist
(plist-put plist
:fromname
(car adr
)))
9355 (setq plist
(plist-put plist
:fromaddress
(nth 1 adr
))))))
9356 (let ((x (plist-get plist
:to
)))
9358 (let ((adr (mail-extract-address-components x
)))
9359 (setq plist
(plist-put plist
:toname
(car adr
)))
9360 (setq plist
(plist-put plist
:toaddress
(nth 1 adr
))))))
9361 (let ((x (ignore-errors (date-to-time (plist-get plist
:date
)))))
9363 (setq plist
(plist-put plist
:date-timestamp
9365 (org-time-stamp-format t
) x
)))
9366 (setq plist
(plist-put plist
:date-timestamp-inactive
9368 (org-time-stamp-format t t
) x
)))))
9369 (let ((from (plist-get plist
:from
))
9370 (to (plist-get plist
:to
)))
9371 (when (and from to org-from-is-user-regexp
)
9373 (plist-put plist
:fromto
9374 (if (string-match org-from-is-user-regexp from
)
9376 (concat "from %f"))))))
9377 (setq org-store-link-plist plist
))
9379 (defun org-add-link-props (&rest plist
)
9380 "Add these properties to the link property list."
9383 (setq key
(pop plist
) value
(pop plist
))
9384 (setq org-store-link-plist
9385 (plist-put org-store-link-plist key value
)))))
9387 (defun org-email-link-description (&optional fmt
)
9388 "Return the description part of an email link.
9389 This takes information from `org-store-link-plist' and formats it
9390 according to FMT (default from `org-email-link-description-format')."
9391 (setq fmt
(or fmt org-email-link-description-format
))
9392 (let* ((p org-store-link-plist
)
9393 (to (plist-get p
:toaddress
))
9394 (from (plist-get p
:fromaddress
))
9397 (cons "%c" (plist-get p
:fromto
))
9398 (cons "%F" (plist-get p
:from
))
9399 (cons "%f" (or (plist-get p
:fromname
) (plist-get p
:fromaddress
) "?"))
9400 (cons "%T" (plist-get p
:to
))
9401 (cons "%t" (or (plist-get p
:toname
) (plist-get p
:toaddress
) "?"))
9402 (cons "%s" (plist-get p
:subject
))
9403 (cons "%d" (plist-get p
:date
))
9404 (cons "%m" (plist-get p
:message-id
)))))
9405 (when (string-match "%c" fmt
)
9406 ;; Check if the user wrote this message
9407 (if (and org-from-is-user-regexp from to
9408 (save-match-data (string-match org-from-is-user-regexp from
)))
9409 (setq fmt
(replace-match "to %t" t t fmt
))
9410 (setq fmt
(replace-match "from %f" t t fmt
))))
9411 (org-replace-escapes fmt table
)))
9413 (defun org-make-org-heading-search-string (&optional string
)
9414 "Make search string for the current headline or STRING."
9416 (and (derived-mode-p 'org-mode
)
9418 (org-back-to-heading t
)
9419 (org-element-property :raw-value
(org-element-at-point))))))
9420 (lines org-context-in-file-links
))
9421 (unless string
(setq s
(concat "*" s
))) ;Add * for headlines
9422 (setq s
(replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s
))
9423 (when (and string
(integerp lines
) (> lines
0))
9424 (let ((slines (org-split-string s
"\n")))
9425 (when (< lines
(length slines
))
9428 (reverse (nthcdr (- (length slines
) lines
)
9429 (reverse slines
))) "\n")))))
9430 (mapconcat #'identity
(split-string s
) " ")))
9432 (defconst org-link-escape-chars
9435 "List of characters that should be escaped in a link when stored to Org.
9436 This is the list that is used for internal purposes.")
9438 (defun org-make-link-string (link &optional description
)
9439 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9440 (unless (org-string-nw-p link
) (error "Empty link"))
9441 (let ((uri (cond ((string-match org-link-types-re link
)
9442 (concat (match-string 1 link
)
9443 (org-link-escape (substring link
(match-end 1)))))
9444 ((or (file-name-absolute-p link
)
9445 (string-match-p "\\`\\.\\.?/" link
))
9446 (org-link-escape link
))
9447 ;; For readability, do not encode space characters
9449 (t (org-link-escape link
(remq ?\s org-link-escape-chars
)))))
9451 (and (org-string-nw-p description
)
9452 ;; Remove brackets from description, as they are fatal.
9453 (replace-regexp-in-string
9454 "[][]" (lambda (m) (if (equal "[" m
) "{" "}"))
9455 (org-trim description
)))))
9458 (if description
(format "[%s]" description
) ""))))
9460 (defun org-link-escape (text &optional table merge
)
9461 "Return percent escaped representation of TEXT.
9462 TEXT is a string with the text to escape.
9463 Optional argument TABLE is a list with characters that should be
9464 escaped. When nil, `org-link-escape-chars' is used.
9465 If optional argument MERGE is set, merge TABLE into
9466 `org-link-escape-chars'."
9467 (let ((characters-to-encode
9468 (cond ((null table
) org-link-escape-chars
)
9469 (merge (append org-link-escape-chars table
))
9473 (if (or (memq c characters-to-encode
)
9474 (and org-url-hexify-p
(or (< c
32) (> c
126))))
9475 (mapconcat (lambda (e) (format "%%%.2X" e
))
9476 (or (encode-coding-char c
'utf-8
)
9477 (error "Unable to percent escape character: %c" c
))
9479 (char-to-string c
)))
9482 (defun org-link-unescape (str)
9483 "Unhex hexified Unicode parts in string STR.
9484 E.g. `%C3%B6' becomes the german o-Umlaut. This is the
9485 reciprocal of `org-link-escape', which see."
9486 (if (org-string-nw-p str
)
9487 (replace-regexp-in-string
9488 "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t
)
9491 (defun org-link-unescape-compound (hex)
9492 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
9493 Note: this function also decodes single byte encodings like
9494 `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
9496 (let* ((bytes (cdr (split-string hex
"%")))
9501 (let* ((val (string-to-number (pop bytes
) 16))
9505 ((>= val
252) (cons 6 252))
9506 ((>= val
248) (cons 5 248))
9507 ((>= val
240) (cons 4 240))
9508 ((>= val
224) (cons 3 224))
9509 ((>= val
192) (cons 2 192))
9512 (when (>= val
192) (setq eat
(car shift-xor
)))
9513 (setq val
(logxor val
(cdr shift-xor
)))
9514 (setq sum
(+ (lsh sum
(car shift-xor
)) val
))
9515 (when (> eat
0) (setq eat
(- eat
1)))
9517 ((= 0 eat
) ;multi byte
9518 (setq ret
(concat ret
(char-to-string sum
)))
9520 ((not bytes
) ; single byte(s)
9521 (setq ret
(org-link-unescape-single-byte-sequence hex
))))))
9524 (defun org-link-unescape-single-byte-sequence (hex)
9525 "Unhexify hex-encoded single byte character sequences."
9526 (mapconcat (lambda (byte)
9527 (char-to-string (string-to-number byte
16)))
9528 (cdr (split-string hex
"%")) ""))
9530 (defun org-fixup-message-id-for-http (s)
9531 "Replace special characters in a message id, so it can be used in an http query."
9532 (when (string-match "%" s
)
9533 (setq s
(mapconcat (lambda (c)
9536 (char-to-string c
)))
9538 (while (string-match "<" s
)
9539 (setq s
(replace-match "%3C" t t s
)))
9540 (while (string-match ">" s
)
9541 (setq s
(replace-match "%3E" t t s
)))
9542 (while (string-match "@" s
)
9543 (setq s
(replace-match "%40" t t s
)))
9546 (defun org-link-prettify (link)
9547 "Return a human-readable representation of LINK.
9548 The car of LINK must be a raw link.
9549 The cdr of LINK must be either a link description or nil."
9550 (let ((desc (or (cadr link
) "<no description>")))
9551 (concat (format "%-45s" (substring desc
0 (min (length desc
) 40)))
9552 "<" (car link
) ">")))
9555 (defun org-insert-link-global ()
9556 "Insert a link like Org mode does.
9557 This command can be called in any mode to insert a link in Org syntax."
9559 (org-load-modules-maybe)
9560 (org-run-like-in-org-mode 'org-insert-link
))
9562 (defun org-insert-all-links (arg &optional pre post
)
9563 "Insert all links in `org-stored-links'.
9564 When a universal prefix, do not delete the links from `org-stored-links'.
9565 When `ARG' is a number, insert the last N link(s).
9566 `PRE' and `POST' are optional arguments to define a string to
9567 prepend or to append."
9569 (let ((org-keep-stored-link-after-insertion (equal arg
'(4)))
9570 (links (copy-sequence org-stored-links
))
9574 (if (null org-stored-links
)
9575 (message "No link to insert")
9576 (while (and (or (listp arg
) (>= arg cnt
))
9577 (setq l
(if (listp arg
)
9579 (pop org-stored-links
))))
9582 (org-insert-link nil
(car l
) (or (cadr l
) "<no description>"))
9585 (defun org-insert-last-stored-link (arg)
9586 "Insert the last link stored in `org-stored-links'."
9588 (org-insert-all-links arg
"" "\n"))
9590 (defun org-link-fontify-links-to-this-file ()
9591 "Fontify links to the current file in `org-stored-links'."
9592 (let ((f (buffer-file-name)) a b
)
9593 (setq a
(mapcar (lambda(l)
9595 (when (and (string-match "^file:\\(.+\\)::" ll
)
9596 (equal f
(expand-file-name (match-string 1 ll
))))
9599 (when (featurep 'org-id
)
9600 (setq b
(mapcar (lambda(l)
9602 (when (and (string-match "^id:\\(.+\\)$" ll
)
9603 (equal f
(expand-file-name
9604 (or (org-id-find-id-file
9605 (match-string 1 ll
)) ""))))
9609 (put-text-property 0 (length l
) 'face
'font-lock-comment-face l
))
9610 (delq nil
(append a b
)))))
9612 (defvar org--links-history nil
)
9613 (defun org-insert-link (&optional complete-file link-location default-description
)
9614 "Insert a link. At the prompt, enter the link.
9616 Completion can be used to insert any of the link protocol prefixes in use.
9618 The history can be used to select a link previously stored with
9619 `org-store-link'. When the empty string is entered (i.e. if you just
9620 press `RET' at the prompt), the link defaults to the most recently
9621 stored link. As `SPC' triggers completion in the minibuffer, you need to
9622 use `M-SPC' or `C-q SPC' to force the insertion of a space character.
9624 You will also be prompted for a description, and if one is given, it will
9625 be displayed in the buffer instead of the link.
9627 If there is already a link at point, this command will allow you to edit
9628 link and description parts.
9630 With a `\\[universal-argument]' prefix, prompts for a file to link to. The \
9632 selected using completion. The path to the file will be relative to the
9633 current directory if the file is in the current directory or a subdirectory.
9634 Otherwise, the link will be the absolute path as completed in the minibuffer
9635 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9636 option `org-link-file-path-type'.
9638 With a `\\[universal-argument] \\[universal-argument]' prefix, enforce an \
9639 absolute path even if the file is in
9640 the current directory or below.
9642 A `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
9643 prefix negates `org-keep-stored-link-after-insertion'.
9645 If the LINK-LOCATION parameter is non-nil, this value will be used as
9646 the link location instead of reading one interactively.
9648 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9649 be used as the default description. Otherwise, if
9650 `org-make-link-description-function' is non-nil, this function
9651 will be called with the link target, and the result will be the
9652 default link description. When called non-interactivily, don't
9653 allow to edit the default description."
9655 (let* ((wcf (current-window-configuration))
9656 (origbuf (current-buffer))
9657 (region (when (org-region-active-p)
9658 (buffer-substring (region-beginning) (region-end))))
9659 (remove (and region
(list (region-beginning) (region-end))))
9661 (link link-location
)
9662 (abbrevs org-link-abbrev-alist-local
)
9663 entry all-prefixes auto-desc
)
9665 (link-location) ; specified by arg, just use it.
9666 ((org-in-regexp org-bracket-link-regexp
1)
9667 ;; We do have a link at point, and we are going to edit it.
9668 (setq remove
(list (match-beginning 0) (match-end 0)))
9669 (setq desc
(when (match-end 3) (match-string-no-properties 3)))
9670 (setq link
(read-string "Link: "
9672 (match-string-no-properties 1)))))
9673 ((or (org-in-regexp org-angle-link-re
)
9674 (org-in-regexp org-plain-link-re
))
9675 ;; Convert to bracket link
9676 (setq remove
(list (match-beginning 0) (match-end 0))
9677 link
(read-string "Link: "
9678 (org-unbracket-string "<" ">" (match-string 0)))))
9679 ((member complete-file
'((4) (16)))
9680 ;; Completing read for file names.
9681 (setq link
(org-file-complete-link complete-file
)))
9683 ;; Read link, with completion for stored links.
9684 (org-link-fontify-links-to-this-file)
9685 (org-switch-to-buffer-other-window "*Org Links*")
9686 (with-current-buffer "*Org Links*"
9688 (insert "Insert a link.
9689 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9690 (when org-stored-links
9691 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9692 (insert (mapconcat 'org-link-prettify
9693 (reverse org-stored-links
) "\n")))
9694 (goto-char (point-min)))
9695 (let ((cw (selected-window)))
9696 (select-window (get-buffer-window "*Org Links*" 'visible
))
9697 (with-current-buffer "*Org Links*" (setq truncate-lines t
))
9698 (unless (pos-visible-in-window-p (point-max))
9699 (org-fit-window-to-buffer))
9700 (and (window-live-p cw
) (select-window cw
)))
9701 (setq all-prefixes
(append (mapcar 'car abbrevs
)
9702 (mapcar 'car org-link-abbrev-alist
)
9705 ;; Fake a link history, containing the stored links.
9706 (let ((org--links-history
9707 (append (mapcar #'car org-stored-links
)
9708 org-insert-link-history
)))
9710 (org-completing-read
9713 (mapcar (lambda (x) (concat x
":")) all-prefixes
)
9714 (mapcar #'car org-stored-links
))
9717 (caar org-stored-links
)))
9718 (unless (org-string-nw-p link
) (user-error "No link selected"))
9719 (dolist (l org-stored-links
)
9720 (when (equal link
(cadr l
))
9722 (setq auto-desc t
)))
9723 (when (or (member link all-prefixes
)
9724 (and (equal ":" (substring link -
1))
9725 (member (substring link
0 -
1) all-prefixes
)
9726 (setq link
(substring link
0 -
1))))
9727 (setq link
(with-current-buffer origbuf
9728 (org-link-try-special-completion link
)))))
9729 (set-window-configuration wcf
)
9730 (kill-buffer "*Org Links*"))
9731 (setq entry
(assoc link org-stored-links
))
9732 (or entry
(push link org-insert-link-history
))
9733 (setq desc
(or desc
(nth 1 entry
)))))
9735 (when (funcall (if (equal complete-file
'(64)) 'not
'identity
)
9736 (not org-keep-stored-link-after-insertion
))
9737 (setq org-stored-links
(delq (assoc link org-stored-links
)
9740 (when (and (string-match org-plain-link-re link
)
9741 (not (string-match org-ts-regexp link
)))
9742 ;; URL-like link, normalize the use of angular brackets.
9743 (setq link
(org-unbracket-string "<" ">" link
)))
9745 ;; Check if we are linking to the current file with a search
9746 ;; option If yes, simplify the link by using only the search
9748 (when (and buffer-file-name
9749 (let ((case-fold-search nil
))
9750 (string-match "\\`file:\\(.+?\\)::" link
)))
9751 (let ((path (match-string-no-properties 1 link
))
9752 (search (substring-no-properties link
(match-end 0))))
9754 (when (equal (file-truename buffer-file-name
) (file-truename path
))
9755 ;; We are linking to this same file, with a search option
9756 (setq link search
)))))
9758 ;; Check if we can/should use a relative path. If yes, simplify
9760 (let ((case-fold-search nil
))
9761 (when (string-match "\\`\\(file\\|docview\\):" link
)
9762 (let* ((type (match-string-no-properties 0 link
))
9763 (path-start (match-end 0))
9764 (search (and (string-match "::\\(.*\\)\\'" link
)
9765 (match-string 1 link
)))
9768 (substring-no-properties
9769 link path-start
(match-beginning 0))
9770 (substring-no-properties link
(match-end 0))))
9773 ((or (eq org-link-file-path-type
'absolute
)
9774 (equal complete-file
'(16)))
9775 (setq path
(abbreviate-file-name (expand-file-name path
))))
9776 ((eq org-link-file-path-type
'noabbrev
)
9777 (setq path
(expand-file-name path
)))
9778 ((eq org-link-file-path-type
'relative
)
9779 (setq path
(file-relative-name path
)))
9782 (if (string-match (concat "^" (regexp-quote
9784 (file-name-as-directory
9785 default-directory
))))
9786 (expand-file-name path
))
9787 ;; We are linking a file with relative path name.
9788 (setq path
(substring (expand-file-name path
)
9790 (setq path
(abbreviate-file-name (expand-file-name path
)))))))
9791 (setq link
(concat type path
(and search
(concat "::" search
))))
9792 (when (equal desc origpath
)
9793 (setq desc path
)))))
9796 (let ((initial-input
9798 (default-description)
9799 ((not org-make-link-description-function
) desc
)
9800 (t (condition-case nil
9801 (funcall org-make-link-description-function link desc
)
9803 (message "Can't get link description from `%s'"
9804 (symbol-name org-make-link-description-function
))
9807 (setq desc
(if (called-interactively-p 'any
)
9808 (read-string "Description: " initial-input
)
9811 (unless (string-match "\\S-" desc
) (setq desc nil
))
9812 (when remove
(apply 'delete-region remove
))
9813 (insert (org-make-link-string link desc
))
9814 ;; Redisplay so as the new link has proper invisible characters.
9817 (defun org-link-try-special-completion (type)
9818 "If there is completion support for link type TYPE, offer it."
9819 (let ((fun (org-link-get-parameter type
:complete
)))
9822 (read-string "Link (no completion support): " (concat type
":")))))
9824 (defun org-file-complete-link (&optional arg
)
9825 "Create a file link using completion."
9826 (let ((file (read-file-name "File: "))
9827 (pwd (file-name-as-directory (expand-file-name ".")))
9828 (pwd1 (file-name-as-directory (abbreviate-file-name
9829 (expand-file-name ".")))))
9830 (cond ((equal arg
'(16))
9832 (abbreviate-file-name (expand-file-name file
))))
9834 (concat "^" (regexp-quote pwd1
) "\\(.+\\)") file
)
9835 (concat "file:" (match-string 1 file
)))
9837 (concat "^" (regexp-quote pwd
) "\\(.+\\)")
9838 (expand-file-name file
))
9840 (match-string 1 (expand-file-name file
))))
9841 (t (concat "file:" file
)))))
9844 ;;; Opening/following a link
9846 (defvar org-link-search-failed nil
)
9848 (defvar org-open-link-functions nil
9849 "Hook for functions finding a plain text link.
9850 These functions must take a single argument, the link content.
9851 They will be called for links that look like [[link text][description]]
9852 when LINK TEXT does not have a protocol like \"http:\" and does not look
9853 like a filename (e.g. \"./blue.png\").
9855 These functions will be called *before* Org attempts to resolve the
9856 link by doing text searches in the current buffer - so if you want a
9857 link \"[[target]]\" to still find \"<<target>>\", your function should
9858 handle this as a special case.
9860 When the function does handle the link, it must return a non-nil value.
9861 If it decides that it is not responsible for this link, it must return
9862 nil to indicate that that Org can continue with other options like
9863 exact and fuzzy text search.")
9865 (defun org-next-link (&optional search-backward
)
9866 "Move forward to the next link.
9867 If the link is in hidden text, expose it."
9869 (when (and org-link-search-failed
(eq this-command last-command
))
9870 (goto-char (point-min))
9871 (message "Link search wrapped back to beginning of buffer"))
9872 (setq org-link-search-failed nil
)
9873 (let* ((pos (point))
9876 (srch-fun (if search-backward
're-search-backward
're-search-forward
)))
9877 (cond (a (goto-char (nth (if search-backward
1 2) a
)))
9878 ((looking-at org-any-link-re
)
9879 ;; Don't stay stuck at link without an org-link face
9880 (forward-char (if search-backward -
1 1))))
9881 (if (funcall srch-fun org-any-link-re nil t
)
9883 (goto-char (match-beginning 0))
9884 (when (org-invisible-p) (org-show-context)))
9886 (setq org-link-search-failed t
)
9887 (message "No further link found"))))
9889 (defun org-previous-link ()
9890 "Move backward to the previous link.
9891 If the link is in hidden text, expose it."
9893 (funcall 'org-next-link t
))
9895 (defun org-translate-link (s)
9896 "Translate a link string if a translation function has been defined."
9898 (insert (org-trim s
))
9899 (org-trim (org-element-interpret-data (org-element-context)))))
9901 (defun org-translate-link-from-planner (type path
)
9902 "Translate a link from Emacs Planner syntax so that Org can follow it.
9903 This is still an experimental function, your mileage may vary."
9905 ((member type
'("http" "https" "news" "ftp"))
9906 ;; standard Internet links are the same.
9908 ((and (equal type
"irc") (string-match "^//" path
))
9909 ;; Planner has two / at the beginning of an irc link, we have 1.
9910 ;; We should have zero, actually....
9911 (setq path
(substring path
1)))
9912 ((and (equal type
"lisp") (string-match "^/" path
))
9913 ;; Planner has a slash, we do not.
9914 (setq type
"elisp" path
(substring path
1)))
9915 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path
)
9916 ;; A typical message link. Planner has the id after the final slash,
9917 ;; we separate it with a hash mark
9918 (setq path
(concat (match-string 1 path
) "#"
9919 (org-unbracket-string "<" ">" (match-string 2 path
))))))
9922 (defun org-find-file-at-mouse (ev)
9923 "Open file link or URL at mouse."
9925 (mouse-set-point ev
)
9926 (org-open-at-point 'in-emacs
))
9928 (defun org-open-at-mouse (ev)
9929 "Open file link or URL at mouse.
9930 See the docstring of `org-open-file' for details."
9932 (mouse-set-point ev
)
9933 (when (eq major-mode
'org-agenda-mode
)
9934 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local
))
9935 (org-open-at-point))
9937 (defvar org-window-config-before-follow-link nil
9938 "The window configuration before following a link.
9939 This is saved in case the need arises to restore it.")
9942 (defun org-open-at-point-global ()
9943 "Follow a link or time-stamp like Org mode does.
9944 This command can be called in any mode to follow an external link
9945 or a time-stamp that has Org mode syntax. Its behavior is
9946 undefined when called on internal links (e.g., fuzzy links).
9947 Raise an error when there is nothing to follow. "
9949 (cond ((org-in-regexp org-any-link-re
)
9950 (org-open-link-from-string (match-string-no-properties 0)))
9951 ((or (org-in-regexp org-ts-regexp-both nil t
)
9952 (org-in-regexp org-tsr-regexp-both nil t
))
9953 (org-follow-timestamp-link))
9954 (t (user-error "No link found"))))
9957 (defun org-open-link-from-string (s &optional arg reference-buffer
)
9958 "Open a link in the string S, as if it was in Org mode."
9959 (interactive "sLink: \nP")
9960 (let ((reference-buffer (or reference-buffer
(current-buffer))))
9962 (let ((org-inhibit-startup (not reference-buffer
)))
9965 (goto-char (point-min))
9966 (when reference-buffer
9967 (setq org-link-abbrev-alist-local
9968 (with-current-buffer reference-buffer
9969 org-link-abbrev-alist-local
)))
9970 (org-open-at-point arg reference-buffer
)))))
9972 (defvar org-open-at-point-functions nil
9973 "Hook that is run when following a link at point.
9975 Functions in this hook must return t if they identify and follow
9976 a link at point. If they don't find anything interesting at point,
9977 they must return nil.")
9979 (defvar org-link-search-inhibit-query nil
)
9980 (defvar clean-buffer-list-kill-buffer-names
) ;Defined in midnight.el
9981 (defun org--open-doi-link (path)
9982 "Open a \"doi\" type link.
9983 PATH is a the path to search for, as a string."
9984 (browse-url (url-encode-url (concat org-doi-server-url path
))))
9986 (defun org--open-elisp-link (path)
9987 "Open a \"elisp\" type link.
9988 PATH is the sexp to evaluate, as a string."
9990 (if (or (and (org-string-nw-p
9991 org-confirm-elisp-link-not-regexp
)
9992 (string-match-p org-confirm-elisp-link-not-regexp cmd
))
9993 (not org-confirm-elisp-link-function
)
9994 (funcall org-confirm-elisp-link-function
9995 (format "Execute \"%s\" as elisp? "
9996 (org-add-props cmd nil
'face
'org-warning
))))
9997 (message "%s => %s" cmd
9998 (if (eq (string-to-char cmd
) ?\
()
10000 (call-interactively (read cmd
))))
10001 (user-error "Abort"))))
10003 (defun org--open-help-link (path)
10004 "Open a \"help\" type link.
10005 PATH is a symbol name, as a string."
10006 (pcase (intern path
)
10007 ((and (pred fboundp
) variable
) (describe-function variable
))
10008 ((and (pred boundp
) function
) (describe-variable function
))
10009 (name (user-error "Unknown function or variable: %s" name
))))
10011 (defun org--open-shell-link (path)
10012 "Open a \"shell\" type link.
10013 PATH is the command to execute, as a string."
10014 (let ((buf (generate-new-buffer "*Org Shell Output*"))
10016 (if (or (and (org-string-nw-p
10017 org-confirm-shell-link-not-regexp
)
10019 org-confirm-shell-link-not-regexp cmd
))
10020 (not org-confirm-shell-link-function
)
10021 (funcall org-confirm-shell-link-function
10022 (format "Execute \"%s\" in shell? "
10023 (org-add-props cmd nil
10024 'face
'org-warning
))))
10026 (message "Executing %s" cmd
)
10027 (shell-command cmd buf
)
10028 (when (featurep 'midnight
)
10029 (setq clean-buffer-list-kill-buffer-names
10030 (cons (buffer-name buf
)
10031 clean-buffer-list-kill-buffer-names
))))
10032 (user-error "Abort"))))
10034 (defun org-open-at-point (&optional arg reference-buffer
)
10035 "Open link, timestamp, footnote or tags at point.
10037 When point is on a link, follow it. Normally, files will be
10038 opened by an appropriate application. If the optional prefix
10039 argument ARG is non-nil, Emacs will visit the file. With
10040 a double prefix argument, try to open outside of Emacs, in the
10041 application the system uses for this file type.
10043 When point is on a timestamp, open the agenda at the day
10046 When point is a footnote definition, move to the first reference
10047 found. If it is on a reference, move to the associated
10050 When point is on a headline, display a list of every link in the
10051 entry, so it is possible to pick one, or all, of them. If point
10052 is on a tag, call `org-tags-view' instead.
10054 When optional argument REFERENCE-BUFFER is non-nil, it should
10055 specify a buffer from where the link search should happen. This
10056 is used internally by `org-open-link-from-string'.
10058 On top of syntactically correct links, this function will also
10059 try to open links and time-stamps in comments, example
10060 blocks... i.e., whenever point is on something looking like
10061 a timestamp or a link."
10063 ;; On a code block, open block's results.
10064 (unless (call-interactively 'org-babel-open-src-block-result
)
10065 (org-load-modules-maybe)
10066 (setq org-window-config-before-follow-link
(current-window-configuration))
10067 (org-remove-occur-highlights nil nil t
)
10068 (unless (run-hook-with-args-until-success 'org-open-at-point-functions
)
10070 ;; Only consider supported types, even if they are not
10071 ;; the closest one.
10072 (org-element-lineage
10073 (org-element-context)
10074 '(clock footnote-definition footnote-reference headline
10075 inlinetask link timestamp
)
10077 (type (org-element-type context
))
10078 (value (org-element-property :value context
)))
10080 ;; On a headline or an inlinetask, but not on a timestamp,
10081 ;; a link, a footnote reference.
10082 ((memq type
'(headline inlinetask
))
10083 (org-match-line org-complex-heading-regexp
)
10084 (if (and (match-beginning 5)
10085 (>= (point) (match-beginning 5))
10086 (< (point) (match-end 5)))
10088 (org-tags-view arg
(substring (match-string 5) 0 -
1))
10090 (pcase (org-offer-links-in-entry (current-buffer) (point) arg
)
10092 (require 'org-attach
)
10093 (org-attach-reveal 'if-exists
))
10094 (`(,links .
,links-end
)
10095 (dolist (link (if (stringp links
) (list links
) links
))
10096 (search-forward link nil links-end
)
10097 (goto-char (match-beginning 0))
10098 (org-open-at-point))))))
10099 ;; On a footnote reference or at definition's label.
10100 ((or (eq type
'footnote-reference
)
10101 (and (eq type
'footnote-definition
)
10103 ;; Do not validate action when point is on the
10104 ;; spaces right after the footnote label, in
10105 ;; order to be on par with behavior on links.
10106 (skip-chars-forward " \t")
10108 (org-element-property :contents-begin context
)))
10109 (if begin
(< (point) begin
)
10110 (= (org-element-property :post-affiliated context
)
10111 (line-beginning-position)))))))
10112 (org-footnote-action))
10113 ;; No valid context. Ignore catch-all types like `headline'.
10114 ;; If point is on something looking like a link or
10115 ;; a time-stamp, try opening it. It may be useful in
10116 ;; comments, example blocks...
10117 ((memq type
'(footnote-definition headline inlinetask nil
))
10118 (call-interactively #'org-open-at-point-global
))
10119 ;; On a clock line, make sure point is on the timestamp
10120 ;; before opening it.
10121 ((and (eq type
'clock
)
10123 (>= (point) (org-element-property :begin value
))
10124 (<= (point) (org-element-property :end value
)))
10125 (org-follow-timestamp-link))
10126 ;; Do nothing on white spaces after an object.
10129 (goto-char (org-element-property :end context
))
10130 (skip-chars-backward " \t")
10132 (user-error "No link found"))
10133 ((eq type
'timestamp
) (org-follow-timestamp-link))
10135 (let ((type (org-element-property :type context
))
10136 (path (org-link-unescape (org-element-property :path context
))))
10137 ;; Switch back to REFERENCE-BUFFER needed when called in
10138 ;; a temporary buffer through `org-open-link-from-string'.
10139 (with-current-buffer (or reference-buffer
(current-buffer))
10141 ((equal type
"file")
10142 (if (string-match "[*?{]" (file-name-nondirectory path
))
10144 ;; Look into `org-link-parameters' in order to find
10145 ;; a DEDICATED-FUNCTION to open file. The function
10146 ;; will be applied on raw link instead of parsed
10147 ;; link due to the limitation in `org-add-link-type'
10148 ;; ("open" function called with a single argument).
10149 ;; If no such function is found, fallback to
10150 ;; `org-open-file'.
10151 (let* ((option (org-element-property :search-option context
))
10152 (app (org-element-property :application context
))
10153 (dedicated-function
10154 (org-link-get-parameter
10155 (if app
(concat type
"+" app
) type
)
10157 (if dedicated-function
10158 (funcall dedicated-function
10160 (and option
(concat "::" option
))))
10161 (apply #'org-open-file
10164 ((equal app
"emacs") 'emacs
)
10165 ((equal app
"sys") 'system
))
10166 (cond ((not option
) nil
)
10167 ((string-match-p "\\`[0-9]+\\'" option
)
10168 (list (string-to-number option
)))
10170 (org-link-unescape option
)))))))))
10171 ((functionp (org-link-get-parameter type
:follow
))
10172 (funcall (org-link-get-parameter type
:follow
) path
))
10173 ((member type
'("coderef" "custom-id" "fuzzy" "radio"))
10174 (unless (run-hook-with-args-until-success
10175 'org-open-link-functions path
)
10176 (if (not arg
) (org-mark-ring-push)
10177 (switch-to-buffer-other-window
10178 (org-get-buffer-for-internal-link (current-buffer))))
10180 (org-with-wide-buffer
10181 (if (equal type
"radio")
10182 (org-search-radio-target
10183 (org-element-property :path context
))
10185 (if (member type
'("custom-id" "coderef"))
10186 (org-element-property :raw-link context
)
10188 ;; Prevent fuzzy links from matching
10190 (and (equal type
"fuzzy")
10191 (+ 2 (org-element-property :begin context
)))))
10193 (unless (and (<= (point-min) destination
)
10194 (>= (point-max) destination
))
10196 (goto-char destination
))))
10197 (t (browse-url-at-point))))))
10198 (t (user-error "No link found")))))
10199 (run-hook-with-args 'org-follow-link-hook
)))
10201 (defun org-offer-links-in-entry (buffer marker
&optional nth zero
)
10202 "Offer links in the current entry and return the selected link.
10203 If there is only one link, return it.
10204 If NTH is an integer, return the NTH link found.
10205 If ZERO is a string, check also this string for a link, and if
10206 there is one, return it."
10207 (with-current-buffer buffer
10208 (org-with-wide-buffer
10211 have-zero end links link c
)
10212 (when (and (stringp zero
) (string-match org-bracket-link-regexp zero
))
10213 (push (match-string 0 zero
) links
)
10214 (setq cnt
(1- cnt
) have-zero t
))
10216 (org-back-to-heading t
)
10217 (setq end
(save-excursion (outline-next-heading) (point)))
10218 (while (re-search-forward org-any-link-re end t
)
10219 (push (match-string 0) links
))
10220 (setq links
(org-uniquify (reverse links
))))
10223 (message "No links"))
10224 ((equal (length links
) 1)
10225 (setq link
(car links
)))
10226 ((and (integerp nth
) (>= (length links
) (if have-zero
(1+ nth
) nth
)))
10227 (setq link
(nth (if have-zero nth
(1- nth
)) links
)))
10228 (t ; we have to select a link
10230 (save-window-excursion
10231 (delete-other-windows)
10232 (with-output-to-temp-buffer "*Select Link*"
10235 ((not (string-match org-bracket-link-regexp l
))
10236 (princ (format "[%c] %s\n" (cl-incf cnt
)
10237 (org-unbracket-string "<" ">" l
))))
10239 (princ (format "[%c] %s (%s)\n" (cl-incf cnt
)
10240 (match-string 3 l
) (match-string 1 l
))))
10241 (t (princ (format "[%c] %s\n" (cl-incf cnt
)
10242 (match-string 1 l
)))))))
10243 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
10244 (message "Select link to open, RET to open all:")
10245 (setq c
(read-char-exclusive))
10246 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
10247 (when (equal c ?q
) (user-error "Abort"))
10248 (if (equal c ?\C-m
)
10250 (setq nth
(- c ?
0))
10251 (when have-zero
(setq nth
(1+ nth
)))
10252 (unless (and (integerp nth
) (>= (length links
) nth
))
10253 (user-error "Invalid link selection"))
10254 (setq link
(nth (1- nth
) links
)))))
10255 (cons link end
)))))
10257 ;; TODO: These functions are deprecated since `org-open-at-point'
10258 ;; hard-codes behavior for "file+emacs" and "file+sys" types.
10259 (defun org-open-file-with-system (path)
10260 "Open file at PATH using the system way of opening it."
10261 (org-open-file path
'system
))
10262 (defun org-open-file-with-emacs (path)
10263 "Open file at PATH in Emacs."
10264 (org-open-file path
'emacs
))
10269 (defvar org-create-file-search-functions nil
10270 "List of functions to construct the right search string for a file link.
10271 These functions are called in turn with point at the location to
10272 which the link should point.
10274 A function in the hook should first test if it would like to
10275 handle this file type, for example by checking the `major-mode'
10276 or the file extension. If it decides not to handle this file, it
10277 should just return nil to give other functions a chance. If it
10278 does handle the file, it must return the search string to be used
10279 when following the link. The search string will be part of the
10280 file link, given after a double colon, and `org-open-at-point'
10281 will automatically search for it. If special measures must be
10282 taken to make the search successful, another function should be
10283 added to the companion hook `org-execute-file-search-functions',
10286 A function in this hook may also use `setq' to set the variable
10287 `description' to provide a suggestion for the descriptive text to
10288 be used for this link when it gets inserted into an Org buffer
10289 with \\[org-insert-link].")
10291 (defvar org-execute-file-search-functions nil
10292 "List of functions to execute a file search triggered by a link.
10294 Functions added to this hook must accept a single argument, the
10295 search string that was part of the file link, the part after the
10296 double colon. The function must first check if it would like to
10297 handle this search, for example by checking the `major-mode' or
10298 the file extension. If it decides not to handle this search, it
10299 should just return nil to give other functions a chance. If it
10300 does handle the search, it must return a non-nil value to keep
10301 other functions from trying.
10303 Each function can access the current prefix argument through the
10304 variable `current-prefix-arg'. Note that a single prefix is used
10305 to force opening a link in Emacs, so it may be good to only use a
10306 numeric or double prefix to guide the search function.
10308 In case this is needed, a function in this hook can also restore
10309 the window configuration before `org-open-at-point' was called using:
10311 (set-window-configuration org-window-config-before-follow-link)")
10313 (defun org-search-radio-target (target)
10314 "Search a radio target matching TARGET in current buffer.
10315 White spaces are not significant."
10316 (let ((re (format "<<<%s>>>"
10317 (mapconcat #'regexp-quote
10318 (split-string target
)
10319 "[ \t]+\\(?:\n[ \t]*\\)?")))
10321 (goto-char (point-min))
10322 (catch :radio-match
10323 (while (re-search-forward re nil t
)
10325 (let ((object (org-element-context)))
10326 (when (eq (org-element-type object
) 'radio-target
)
10327 (goto-char (org-element-property :begin object
))
10328 (org-show-context 'link-search
)
10329 (throw :radio-match nil
))))
10331 (user-error "No match for radio target: %s" target
))))
10333 (defun org-link-search (s &optional avoid-pos stealth
)
10334 "Search for a search string S.
10336 If S starts with \"#\", it triggers a custom ID search.
10338 If S is enclosed within parenthesis, it initiates a coderef
10341 If S is surrounded by forward slashes, it is interpreted as
10342 a regular expression. In Org mode files, this will create an
10343 `org-occur' sparse tree. In ordinary files, `occur' will be used
10344 to list matches. If the current buffer is in `dired-mode', grep
10345 will be used to search in all files.
10347 When AVOID-POS is given, ignore matches near that position.
10349 When optional argument STEALTH is non-nil, do not modify
10350 visibility around point, thus ignoring `org-show-context-detail'
10353 Search is case-insensitive and ignores white spaces. Return type
10354 of matched result, which is either `dedicated' or `fuzzy'."
10355 (unless (org-string-nw-p s
) (error "Invalid search string \"%s\"" s
))
10356 (let* ((case-fold-search t
)
10358 (normalized (replace-regexp-in-string "\n[ \t]*" " " s
))
10359 (starred (eq (string-to-char normalized
) ?
*))
10360 (words (split-string (if starred
(substring s
1) s
)))
10361 (s-multi-re (mapconcat #'regexp-quote words
"\\(?:[ \t\n]+\\)"))
10362 (s-single-re (mapconcat #'regexp-quote words
"[ \t]+"))
10365 ;; Check if there are any special search functions.
10366 ((run-hook-with-args-until-success 'org-execute-file-search-functions s
))
10367 ((eq (string-to-char s
) ?
#)
10368 ;; Look for a custom ID S if S starts with "#".
10369 (let* ((id (substring normalized
1))
10370 (match (org-find-property "CUSTOM_ID" id
)))
10371 (if match
(progn (goto-char match
) (setf type
'dedicated
))
10372 (error "No match for custom ID: %s" id
))))
10373 ((string-match "\\`(\\(.*\\))\\'" normalized
)
10374 ;; Look for coderef targets if S is enclosed within parenthesis.
10375 (let ((coderef (match-string-no-properties 1 normalized
))
10376 (re (substring s-single-re
1 -
1)))
10377 (goto-char (point-min))
10378 (catch :coderef-match
10379 (while (re-search-forward re nil t
)
10380 (let ((element (org-element-at-point)))
10381 (when (and (memq (org-element-type element
)
10382 '(example-block src-block
))
10383 ;; Build proper regexp according to current
10384 ;; block's label format.
10387 (or (org-element-property :label-fmt element
)
10388 org-coderef-label-format
))))
10390 (beginning-of-line)
10391 (looking-at (format ".*?\\(%s\\)[ \t]*$"
10392 (format label-fmt coderef
))))))
10393 (setq type
'dedicated
)
10394 (goto-char (match-beginning 1))
10395 (throw :coderef-match nil
))))
10397 (error "No match for coderef: %s" coderef
))))
10398 ((string-match "\\`/\\(.*\\)/\\'" normalized
)
10399 ;; Look for a regular expression.
10400 (funcall (if (derived-mode-p 'org-mode
) #'org-occur
#'org-do-occur
)
10401 (match-string 1 s
)))
10402 ;; From here, we handle fuzzy links.
10404 ;; Look for targets, only if not in a headline search.
10405 ((and (not starred
)
10406 (let ((target (format "<<%s>>" s-multi-re
)))
10407 (catch :target-match
10408 (goto-char (point-min))
10409 (while (re-search-forward target nil t
)
10411 (let ((context (org-element-context)))
10412 (when (eq (org-element-type context
) 'target
)
10413 (setq type
'dedicated
)
10414 (goto-char (org-element-property :begin context
))
10415 (throw :target-match t
))))
10417 ;; Look for elements named after S, only if not in a headline
10419 ((and (not starred
)
10420 (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re
)))
10422 (goto-char (point-min))
10423 (while (re-search-forward name nil t
)
10424 (let ((element (org-element-at-point)))
10427 (org-element-property :name element
)))
10428 (setq type
'dedicated
)
10429 (beginning-of-line)
10430 (throw :name-match t
))))
10432 ;; Regular text search. Prefer headlines in Org mode buffers.
10433 ;; Ignore COMMENT keyword, TODO keywords, priority cookies,
10434 ;; statistics cookies and tags.
10435 ((and (derived-mode-p 'org-mode
)
10437 (format "%s.*\\(?:%s[ \t]\\)?.*%s"
10438 org-outline-regexp-bol
10440 (mapconcat #'regexp-quote words
".+")))
10441 (cookie-re "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]")
10442 (comment-re (eval-when-compile
10443 (format "\\`%s[ \t]+" org-comment-string
))))
10444 (goto-char (point-min))
10446 (while (re-search-forward title-re nil t
)
10449 (replace-regexp-in-string
10451 (replace-regexp-in-string
10452 comment-re
"" (org-get-heading t t t
)))))
10455 (beginning-of-line)
10456 (setq type
'dedicated
))
10457 ;; Offer to create non-existent headline depending on
10458 ;; `org-link-search-must-match-exact-headline'.
10459 ((and (derived-mode-p 'org-mode
)
10460 (not org-link-search-inhibit-query
)
10461 (eq org-link-search-must-match-exact-headline
'query-to-create
)
10462 (yes-or-no-p "No match - create this as a new heading? "))
10463 (goto-char (point-max))
10464 (unless (bolp) (newline))
10465 (org-insert-heading nil t t
)
10467 (beginning-of-line 0))
10468 ;; Only headlines are looked after. No need to process
10469 ;; further: throw an error.
10470 ((and (derived-mode-p 'org-mode
)
10471 (or starred org-link-search-must-match-exact-headline
))
10473 (error "No match for fuzzy expression: %s" normalized
))
10474 ;; Regular text search.
10475 ((catch :fuzzy-match
10476 (goto-char (point-min))
10477 (while (re-search-forward s-multi-re nil t
)
10478 ;; Skip match if it contains AVOID-POS or it is included in
10479 ;; a link with a description but outside the description.
10480 (unless (or (and avoid-pos
10481 (<= (match-beginning 0) avoid-pos
)
10482 (> (match-end 0) avoid-pos
))
10483 (and (save-match-data
10484 (org-in-regexp org-bracket-link-regexp
))
10485 (match-beginning 3)
10486 (or (> (match-beginning 3) (point))
10487 (<= (match-end 3) (point)))
10488 (org-element-lineage
10489 (save-match-data (org-element-context))
10491 (goto-char (match-beginning 0))
10493 (throw :fuzzy-match t
)))
10495 ;; All failed. Throw an error.
10496 (t (goto-char origin
)
10497 (error "No match for fuzzy expression: %s" normalized
)))
10498 ;; Disclose surroundings of match, if appropriate.
10499 (when (and (derived-mode-p 'org-mode
) (not stealth
))
10500 (org-show-context 'link-search
))
10503 (defun org-get-buffer-for-internal-link (buffer)
10504 "Return a buffer to be used for displaying the link target of internal links."
10506 ((not org-display-internal-link-with-indirect-buffer
)
10508 ((string-suffix-p "(Clone)" (buffer-name buffer
))
10509 (message "Buffer is already a clone, not making another one")
10510 ;; we also do not modify visibility in this case
10512 (t ; make a new indirect buffer for displaying the link
10513 (let* ((bn (buffer-name buffer
))
10514 (ibn (concat bn
"(Clone)"))
10515 (ib (or (get-buffer ibn
) (make-indirect-buffer buffer ibn
'clone
))))
10516 (with-current-buffer ib
(org-overview))
10519 (defun org-do-occur (regexp &optional cleanup
)
10520 "Call the Emacs command `occur'.
10521 If CLEANUP is non-nil, remove the printout of the regular expression
10522 in the *Occur* buffer. This is useful if the regex is long and not useful
10526 (let ((cwin (selected-window)) win beg end
)
10527 (when (setq win
(get-buffer-window "*Occur*"))
10528 (select-window win
))
10529 (goto-char (point-min))
10530 (when (re-search-forward "match[a-z]+" nil t
)
10531 (setq beg
(match-end 0))
10532 (when (re-search-forward "^[ \t]*[0-9]+" nil t
)
10533 (setq end
(1- (match-beginning 0)))))
10534 (and beg end
(let ((inhibit-read-only t
)) (delete-region beg end
)))
10535 (goto-char (point-min))
10536 (select-window cwin
))))
10541 (defvar org-mark-ring nil
10542 "Mark ring for positions before jumps in Org mode.")
10544 (defvar org-mark-ring-last-goto nil
10545 "Last position in the mark ring used to go back.")
10547 ;; Fill and close the ring
10548 (setq org-mark-ring nil
)
10549 (setq org-mark-ring-last-goto nil
) ;in case file is reloaded
10551 (dotimes (_ org-mark-ring-length
) (push (make-marker) org-mark-ring
))
10552 (setcdr (nthcdr (1- org-mark-ring-length
) org-mark-ring
)
10555 (defun org-mark-ring-push (&optional pos buffer
)
10556 "Put the current position into the mark ring and rotate it.
10557 Also push position into the Emacs mark ring. If optional
10558 argument POS and BUFFER are not nil, mark this location instead."
10560 (let ((pos (or pos
(point)))
10561 (buffer (or buffer
(current-buffer))))
10562 (with-current-buffer buffer
10563 (org-with-point-at pos
(push-mark nil t
)))
10564 (setq org-mark-ring
(nthcdr (1- org-mark-ring-length
) org-mark-ring
))
10565 (move-marker (car org-mark-ring
) pos buffer
))
10567 (substitute-command-keys
10568 "Position saved to mark ring, go back with `\\[org-mark-ring-goto]'.")))
10570 (defun org-mark-ring-goto (&optional n
)
10571 "Jump to the previous position in the mark ring.
10572 With prefix arg N, jump back that many stored positions. When
10573 called several times in succession, walk through the entire ring.
10574 Org mode commands jumping to a different position in the current file,
10575 or to another Org file, automatically push the old position onto the ring."
10578 (if (eq last-command this-command
)
10579 (setq p
(nthcdr n
(or org-mark-ring-last-goto org-mark-ring
)))
10580 (setq p org-mark-ring
))
10581 (setq org-mark-ring-last-goto p
)
10583 (pop-to-buffer-same-window (marker-buffer m
))
10585 (when (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto
))))
10587 (defun org-add-angle-brackets (s)
10588 (unless (equal (substring s
0 1) "<") (setq s
(concat "<" s
)))
10589 (unless (equal (substring s -
1) ">") (setq s
(concat s
">")))
10592 ;;; Following specific links
10594 (defvar org-agenda-buffer-tmp-name
)
10595 (defvar org-agenda-start-on-weekday
)
10596 (defun org-follow-timestamp-link ()
10597 "Open an agenda view for the time-stamp date/range at point."
10599 ((org-at-date-range-p t
)
10600 (let ((org-agenda-start-on-weekday)
10601 (t1 (match-string 1))
10602 (t2 (match-string 2)) tt1 tt2
)
10603 (setq tt1
(time-to-days (org-time-string-to-time t1
))
10604 tt2
(time-to-days (org-time-string-to-time t2
)))
10605 (let ((org-agenda-buffer-tmp-name
10606 (format "*Org Agenda(a:%s)"
10607 (concat (substring t1
0 10) "--" (substring t2
0 10)))))
10608 (org-agenda-list nil tt1
(1+ (- tt2 tt1
))))))
10609 ((org-at-timestamp-p 'lax
)
10610 (let ((org-agenda-buffer-tmp-name
10611 (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
10612 (org-agenda-list nil
(time-to-days (org-time-string-to-time
10613 (substring (match-string 1) 0 10)))
10615 (t (error "This should not happen"))))
10618 ;;; Following file links
10619 (declare-function mailcap-parse-mailcaps
"mailcap" (&optional path force
))
10620 (declare-function mailcap-extension-to-mime
"mailcap" (extn))
10621 (declare-function mailcap-mime-info
10622 "mailcap" (string &optional request no-decode
))
10623 (defvar org-wait nil
)
10624 (defun org-open-file (path &optional in-emacs line search
)
10625 "Open the file at PATH.
10626 First, this expands any special file name abbreviations. Then the
10627 configuration variable `org-file-apps' is checked if it contains an
10628 entry for this file type, and if yes, the corresponding command is launched.
10630 If no application is found, Emacs simply visits the file.
10632 With optional prefix argument IN-EMACS, Emacs will visit the file.
10633 With a double \\[universal-argument] \\[universal-argument] \
10634 prefix arg, Org tries to avoid opening in Emacs
10635 and to use an external application to visit the file.
10637 Optional LINE specifies a line to go to, optional SEARCH a string
10638 to search for. If LINE or SEARCH is given, the file will be
10639 opened in Emacs, unless an entry from org-file-apps that makes
10640 use of groups in a regexp matches.
10642 If you want to change the way frames are used when following a
10643 link, please customize `org-link-frame-setup'.
10645 If the file does not exist, an error is thrown."
10646 (let* ((file (if (equal path
"")
10648 (substitute-in-file-name (expand-file-name path
))))
10649 (file-apps (append org-file-apps
(org-default-apps)))
10650 (apps (cl-remove-if
10651 'org-file-apps-entry-match-against-dlink-p file-apps
))
10652 (apps-dlink (cl-remove-if-not
10653 'org-file-apps-entry-match-against-dlink-p file-apps
))
10654 (remp (and (assq 'remote apps
) (org-file-remote-p file
)))
10655 (dirp (unless remp
(file-directory-p file
)))
10656 (file (if (and dirp org-open-directory-means-index-dot-org
)
10657 (concat (file-name-as-directory file
) "index.org")
10659 (a-m-a-p (assq 'auto-mode apps
))
10660 (dfile (downcase file
))
10661 ;; Reconstruct the original link from the PATH, LINE and
10663 (link (cond (line (concat file
"::" (number-to-string line
)))
10664 (search (concat file
"::" search
))
10666 (dlink (downcase link
))
10668 (and (string-match "\\`.*?\\.\\([a-zA-Z0-9]+\\(\\.gz\\)?\\)\\'" dfile
)
10669 (match-string 1 dfile
)))
10670 (save-position-maybe
10671 (let ((old-buffer (current-buffer))
10673 (old-mode major-mode
))
10675 (and (derived-mode-p 'org-mode
)
10676 (eq old-mode
'org-mode
)
10677 (or (not (eq old-buffer
(current-buffer)))
10678 (not (eq old-pos
(point))))
10679 (org-mark-ring-push old-pos old-buffer
)))))
10680 cmd link-match-data
)
10682 ((member in-emacs
'((16) system
))
10683 (setq cmd
(cdr (assq 'system apps
))))
10684 (in-emacs (setq cmd
'emacs
))
10686 (setq cmd
(or (and remp
(cdr (assq 'remote apps
)))
10687 (and dirp
(cdr (assq 'directory apps
)))
10688 ;; First, try matching against apps-dlink if we
10689 ;; get a match here, store the match data for
10691 (let ((match (assoc-default dlink apps-dlink
10694 (progn (setq link-match-data
(match-data))
10696 (progn (setq in-emacs
(or in-emacs line search
))
10697 nil
))) ; if we have no match in apps-dlink,
10698 ; always open the file in emacs if line or search
10699 ; is given (for backwards compatibility)
10700 (assoc-default dfile
(org-apps-regexp-alist apps a-m-a-p
)
10702 (cdr (assoc ext apps
))
10703 (cdr (assq t apps
))))))
10704 (when (eq cmd
'system
)
10705 (setq cmd
(cdr (assq 'system apps
))))
10706 (when (eq cmd
'default
)
10707 (setq cmd
(cdr (assoc t apps
))))
10708 (when (eq cmd
'mailcap
)
10710 (mailcap-parse-mailcaps)
10711 (let* ((mime-type (mailcap-extension-to-mime (or ext
"")))
10712 (command (mailcap-mime-info mime-type
)))
10713 (if (stringp command
)
10715 (setq cmd
'emacs
))))
10716 (when (and (not (eq cmd
'emacs
)) ; Emacs has no problems with non-ex files
10717 (not (file-exists-p file
))
10718 (not org-open-non-existing-files
))
10719 (user-error "No such file: %s" file
))
10721 ((and (stringp cmd
) (not (string-match "^\\s-*$" cmd
)))
10722 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10723 (while (string-match "['\"]%s['\"]" cmd
)
10724 (setq cmd
(replace-match "%s" t t cmd
)))
10725 (setq cmd
(replace-regexp-in-string
10727 (shell-quote-argument (convert-standard-filename file
))
10731 ;; Replace "%1", "%2" etc. in command with group matches from regex
10733 (let ((match-index 1)
10734 (number-of-groups (- (/ (length link-match-data
) 2) 1)))
10735 (set-match-data link-match-data
)
10736 (while (<= match-index number-of-groups
)
10737 (let ((regex (concat "%" (number-to-string match-index
)))
10738 (replace-with (match-string match-index dlink
)))
10739 (while (string-match regex cmd
)
10740 (setq cmd
(replace-match replace-with t t cmd
))))
10741 (setq match-index
(+ match-index
1)))))
10743 (save-window-excursion
10744 (message "Running %s...done" cmd
)
10745 (start-process-shell-command cmd nil cmd
)
10746 (and (boundp 'org-wait
) (numberp org-wait
) (sit-for org-wait
))))
10749 (funcall (cdr (assq 'file org-link-frame-setup
)) file
)
10751 (cond (line (org-goto-line line
)
10752 (when (derived-mode-p 'org-mode
) (org-reveal)))
10753 (search (condition-case err
10754 (org-link-search search
)
10755 ;; Save position before error-ing out so user
10756 ;; can easily move back to the original buffer.
10757 (error (funcall save-position-maybe
)
10758 (error (nth 1 err
)))))))
10761 (set-match-data link-match-data
)
10762 (condition-case nil
10763 (funcall cmd file link
)
10764 ;; FIXME: Remove this check when most default installations
10765 ;; of Emacs have at least Org 9.0.
10766 ((debug wrong-number-of-arguments wrong-type-argument
10768 (user-error "Please see Org News for version 9.0 about \
10769 `org-file-apps'--Lisp error: %S" cmd
)))))
10771 ;; FIXME: Remove this check when most default installations of
10772 ;; Emacs have at least Org 9.0. Heads-up instead of silently
10773 ;; fall back to `org-link-frame-setup' for an old usage of
10774 ;; `org-file-apps' with sexp instead of a function for `cmd'.
10775 (user-error "Please see Org News for version 9.0 about \
10776 `org-file-apps'--Error: Deprecated usage of %S" cmd
))
10777 (t (funcall (cdr (assq 'file org-link-frame-setup
)) file
)))
10778 (funcall save-position-maybe
)))
10780 (defun org-file-apps-entry-match-against-dlink-p (entry)
10781 "This function returns non-nil if `entry' uses a regular
10782 expression which should be matched against the whole link by
10785 It assumes that is the case when the entry uses a regular
10786 expression which has at least one grouping construct and the
10787 action is either a lisp form or a command string containing
10788 `%1', i.e. using at least one subexpression match as a
10790 (let ((selector (car entry
))
10791 (action (cdr entry
)))
10792 (if (stringp selector
)
10793 (and (> (regexp-opt-depth selector
) 0)
10794 (or (and (stringp action
)
10795 (string-match "%[0-9]" action
))
10799 (defun org-default-apps ()
10800 "Return the default applications for this operating system."
10802 ((eq system-type
'darwin
)
10803 org-file-apps-defaults-macosx
)
10804 ((eq system-type
'windows-nt
)
10805 org-file-apps-defaults-windowsnt
)
10806 (t org-file-apps-defaults-gnu
)))
10808 (defun org-apps-regexp-alist (list &optional add-auto-mode
)
10809 "Convert extensions to regular expressions in the cars of LIST.
10810 Also, weed out any non-string entries, because the return value is used
10811 only for regexp matching.
10812 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10813 point to the symbol `emacs', indicating that the file should
10814 be opened in Emacs."
10817 (mapcar (lambda (x)
10818 (unless (not (stringp (car x
)))
10819 (if (string-match "\\W" (car x
))
10821 (cons (concat "\\." (car x
) "\\'") (cdr x
)))))
10823 (when add-auto-mode
10824 (mapcar (lambda (x) (cons (car x
) 'emacs
)) auto-mode-alist
))))
10826 (defvar ange-ftp-name-format
)
10827 (defun org-file-remote-p (file)
10828 "Test whether FILE specifies a location on a remote system.
10829 Return non-nil if the location is indeed remote.
10831 For example, the filename \"/user@host:/foo\" specifies a location
10832 on the system \"/user@host:\"."
10833 (cond ((fboundp 'file-remote-p
)
10834 (file-remote-p file
))
10835 ((fboundp 'tramp-handle-file-remote-p
)
10836 (tramp-handle-file-remote-p file
))
10837 ((and (boundp 'ange-ftp-name-format
)
10838 (string-match (car ange-ftp-name-format
) file
))
10844 (defun org-get-org-file ()
10845 "Read a filename, with default directory `org-directory'."
10846 (let ((default (or org-default-notes-file remember-data-file
)))
10847 (read-file-name (format "File name [%s]: " default
)
10848 (file-name-as-directory org-directory
)
10851 (defun org-notes-order-reversed-p ()
10852 "Check if the current file should receive notes in reversed order."
10854 ((not org-reverse-note-order
) nil
)
10855 ((eq t org-reverse-note-order
) t
)
10856 ((not (listp org-reverse-note-order
)) nil
)
10858 (dolist (entry org-reverse-note-order
)
10859 (when (string-match (car entry
) buffer-file-name
)
10860 (throw 'exit
(cdr entry
))))))))
10862 (defvar org-refile-target-table nil
10863 "The list of refile targets, created by `org-refile'.")
10865 (defvar org-agenda-new-buffers nil
10866 "Buffers created to visit agenda files.")
10868 (defvar org-refile-cache nil
10869 "Cache for refile targets.")
10871 (defvar org-refile-markers nil
10872 "All the markers used for caching refile locations.")
10874 (defun org-refile-marker (pos)
10875 "Get a new refile marker, but only if caching is in use."
10876 (if (not org-refile-use-cache
)
10878 (let ((m (make-marker)))
10879 (move-marker m pos
)
10880 (push m org-refile-markers
)
10883 (defun org-refile-cache-clear ()
10884 "Clear the refile cache and disable all the markers."
10885 (dolist (m org-refile-markers
) (move-marker m nil
))
10886 (setq org-refile-markers nil
)
10887 (setq org-refile-cache nil
)
10888 (message "Refile cache has been cleared"))
10890 (defun org-refile-cache-check-set (set)
10891 "Check if all the markers in the cache still have live buffers."
10894 (while (and set
(setq marker
(nth 3 (pop set
))))
10895 ;; If `org-refile-use-outline-path' is 'file, marker may be nil
10896 (when (and marker
(null (marker-buffer marker
)))
10897 (message "Please regenerate the refile cache with `C-0 C-c C-w'")
10899 (throw 'exit nil
)))
10902 (defun org-refile-cache-put (set &rest identifiers
)
10903 "Push the refile targets SET into the cache, under IDENTIFIERS."
10904 (let* ((key (sha1 (prin1-to-string identifiers
)))
10905 (entry (assoc key org-refile-cache
)))
10908 (push (cons key set
) org-refile-cache
))))
10910 (defun org-refile-cache-get (&rest identifiers
)
10911 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10913 ((not org-refile-cache
) nil
)
10914 ((not org-refile-use-cache
) (org-refile-cache-clear) nil
)
10916 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers
))
10917 org-refile-cache
))))
10918 (and set
(org-refile-cache-check-set set
) set
)))))
10920 (defvar org-outline-path-cache nil
10921 "Alist between buffer positions and outline paths.
10922 It value is an alist (POSITION . PATH) where POSITION is the
10923 buffer position at the beginning of an entry and PATH is a list
10924 of strings describing the outline path for that entry, in reverse
10927 (defun org-refile-get-targets (&optional default-buffer
)
10928 "Produce a table with refile targets."
10929 (let ((case-fold-search nil
)
10930 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10931 (entries (or org-refile-targets
'((nil .
(:level .
1)))))
10932 targets tgs files desc descre
)
10933 (message "Getting targets...")
10934 (with-current-buffer (or default-buffer
(current-buffer))
10935 (dolist (entry entries
)
10936 (setq files
(car entry
) desc
(cdr entry
))
10938 ((null files
) (setq files
(list (current-buffer))))
10939 ((eq files
'org-agenda-files
)
10940 (setq files
(org-agenda-files 'unrestricted
)))
10941 ((and (symbolp files
) (fboundp files
))
10942 (setq files
(funcall files
)))
10943 ((and (symbolp files
) (boundp files
))
10944 (setq files
(symbol-value files
))))
10945 (when (stringp files
) (setq files
(list files
)))
10947 ((eq (car desc
) :tag
)
10948 (setq descre
(concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc
)) ":")))
10949 ((eq (car desc
) :todo
)
10950 (setq descre
(concat "^\\*+[ \t]+" (regexp-quote (cdr desc
)) "[ \t]")))
10951 ((eq (car desc
) :regexp
)
10952 (setq descre
(cdr desc
)))
10953 ((eq (car desc
) :level
)
10954 (setq descre
(concat "^\\*\\{" (number-to-string
10955 (if org-odd-levels-only
10956 (1- (* 2 (cdr desc
)))
10959 ((eq (car desc
) :maxlevel
)
10960 (setq descre
(concat "^\\*\\{1," (number-to-string
10961 (if org-odd-levels-only
10962 (1- (* 2 (cdr desc
)))
10965 (t (error "Bad refiling target description %s" desc
)))
10967 (with-current-buffer (if (bufferp f
) f
(org-get-agenda-file-buffer f
))
10969 (setq tgs
(org-refile-cache-get (buffer-file-name) descre
))
10972 (setq f
(buffer-file-name (buffer-base-buffer f
))))
10973 (setq f
(and f
(expand-file-name f
)))
10974 (when (eq org-refile-use-outline-path
'file
)
10975 (push (list (file-name-nondirectory f
) f nil nil
) tgs
))
10976 (when (eq org-refile-use-outline-path
'buffer-name
)
10977 (push (list (buffer-name (buffer-base-buffer)) f nil nil
) tgs
))
10978 (when (eq org-refile-use-outline-path
'full-file-path
)
10979 (push (list (file-truename (buffer-file-name (buffer-base-buffer))) f nil nil
) tgs
))
10980 (org-with-wide-buffer
10981 (goto-char (point-min))
10982 (setq org-outline-path-cache nil
)
10983 (while (re-search-forward descre nil t
)
10984 (beginning-of-line)
10985 (let ((case-fold-search nil
))
10986 (looking-at org-complex-heading-regexp
))
10987 (let ((begin (point))
10988 (heading (match-string-no-properties 4)))
10990 org-refile-target-verify-function
10992 (funcall org-refile-target-verify-function
)))
10994 (let ((re (format org-complex-heading-regexp-format
10995 (regexp-quote heading
)))
10997 (if (not org-refile-use-outline-path
) heading
11001 (pcase org-refile-use-outline-path
11002 (`file
(list (file-name-nondirectory
11004 (buffer-base-buffer)))))
11006 (list (buffer-file-name
11007 (buffer-base-buffer))))
11010 (buffer-base-buffer))))
11012 (mapcar (lambda (s) (replace-regexp-in-string
11013 "/" "\\/" s nil t
))
11014 (org-get-outline-path t t
)))
11016 (push (list target f re
(org-refile-marker (point)))
11018 (when (= (point) begin
)
11019 ;; Verification function has not moved point.
11020 (end-of-line)))))))
11021 (when org-refile-use-cache
11022 (org-refile-cache-put tgs
(buffer-file-name) descre
))
11023 (setq targets
(append tgs targets
))))))
11024 (message "Getting targets...done")
11025 (delete-dups (nreverse targets
))))
11027 (defun org--get-outline-path-1 (&optional use-cache
)
11028 "Return outline path to current headline.
11030 Outline path is a list of strings, in reverse order. When
11031 optional argument USE-CACHE is non-nil, make use of a cache. See
11032 `org-get-outline-path' for details.
11034 Assume buffer is widened and point is on a headline."
11035 (or (and use-cache
(cdr (assq (point) org-outline-path-cache
)))
11037 (heading (let ((case-fold-search nil
))
11038 (looking-at org-complex-heading-regexp
)
11039 (if (not (match-end 4)) ""
11040 ;; Remove statistics cookies.
11042 (org-link-display-format
11043 (replace-regexp-in-string
11044 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
11045 (match-string-no-properties 4))))))))
11046 (if (org-up-heading-safe)
11047 (let ((path (cons heading
(org--get-outline-path-1 use-cache
))))
11049 (push (cons p path
) org-outline-path-cache
))
11051 ;; This is a new root node. Since we assume we are moving
11052 ;; forward, we can drop previous cache so as to limit number
11053 ;; of associations there.
11054 (let ((path (list heading
)))
11055 (when use-cache
(setq org-outline-path-cache
(list (cons p path
))))
11058 (defun org-get-outline-path (&optional with-self use-cache
)
11059 "Return the outline path to the current entry.
11061 An outline path is a list of ancestors for current headline, as
11062 a list of strings. Statistics cookies are removed and links are
11063 replaced with their description, if any, or their path otherwise.
11065 When optional argument WITH-SELF is non-nil, the path also
11066 includes the current headline.
11068 When optional argument USE-CACHE is non-nil, cache outline paths
11069 between calls to this function so as to avoid backtracking. This
11070 argument is useful when planning to find more than one outline
11071 path in the same document. In that case, there are two
11072 conditions to satisfy:
11073 - `org-outline-path-cache' is set to nil before starting the
11075 - outline paths are computed by increasing buffer positions."
11076 (org-with-wide-buffer
11077 (and (or (and with-self
(org-back-to-heading t
))
11078 (org-up-heading-safe))
11079 (reverse (org--get-outline-path-1 use-cache
)))))
11081 (defun org-format-outline-path (path &optional width prefix separator
)
11082 "Format the outline path PATH for display.
11083 WIDTH is the maximum number of characters that is available.
11084 PREFIX is a prefix to be included in the returned string,
11085 such as the file name.
11086 SEPARATOR is inserted between the different parts of the path,
11087 the default is \"/\"."
11088 (setq width
(or width
79))
11089 (setq path
(delq nil path
))
11090 (unless (> width
0)
11091 (user-error "Argument `width' must be positive"))
11092 (setq separator
(or separator
"/"))
11093 (let* ((org-odd-levels-only nil
)
11095 prefix
(and prefix path separator
)
11097 (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s
))
11098 (cl-loop for head in path
11100 collect
(org-add-props
11102 (nth (% n org-n-level-faces
) org-level-faces
)))
11104 (when (> (length fpath
) width
)
11106 ;; It's unlikely that `width' will be this small, but don't
11107 ;; waste characters by adding ".." if it is.
11108 (setq fpath
(substring fpath
0 width
))
11109 (setf (substring fpath
(- width
2)) "..")))
11112 (defun org-display-outline-path (&optional file current separator just-return-string
)
11113 "Display the current outline path in the echo area.
11115 If FILE is non-nil, prepend the output with the file name.
11116 If CURRENT is non-nil, append the current heading to the output.
11117 SEPARATOR is passed through to `org-format-outline-path'. It separates
11118 the different parts of the path and defaults to \"/\".
11119 If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
11121 (let* (case-fold-search
11122 (bfn (buffer-file-name (buffer-base-buffer)))
11123 (path (and (derived-mode-p 'org-mode
) (org-get-outline-path)))
11125 (when current
(setq path
(append path
11127 (org-back-to-heading t
)
11128 (when (looking-at org-complex-heading-regexp
)
11129 (list (match-string 4)))))))
11131 (org-format-outline-path
11134 (and file bfn
(concat (file-name-nondirectory bfn
) separator
))
11136 (if just-return-string
11137 (org-no-properties res
)
11138 (org-unlogged-message "%s" res
))))
11140 (defvar org-refile-history nil
11141 "History for refiling operations.")
11143 (defvar org-after-refile-insert-hook nil
11144 "Hook run after `org-refile' has inserted its stuff at the new location.
11145 Note that this is still *before* the stuff will be removed from
11146 the *old* location.")
11148 (defvar org-capture-last-stored-marker
)
11149 (defvar org-refile-keep nil
11150 "Non-nil means `org-refile' will copy instead of refile.")
11153 "Like `org-refile', but copy."
11155 (let ((org-refile-keep t
))
11156 (funcall 'org-refile nil nil nil
"Copy")))
11158 (defun org-refile (&optional arg default-buffer rfloc msg
)
11159 "Move the entry or entries at point to another heading.
11161 The list of target headings is compiled using the information in
11162 `org-refile-targets', which see.
11164 At the target location, the entry is filed as a subitem of the
11165 target heading. Depending on `org-reverse-note-order', the new
11166 subitem will either be the first or the last subitem.
11168 If there is an active region, all entries in that region will be
11169 refiled. However, the region must fulfill the requirement that
11170 the first heading sets the top-level of the moved text.
11172 With a `\\[universal-argument]' ARG, the command will only visit the target \
11174 and not actually move anything.
11176 With a prefix `\\[universal-argument] \\[universal-argument]', go to the \
11177 location where the last
11178 refiling operation has put the subtree.
11180 With a numeric prefix argument of `2', refile to the running clock.
11182 With a numeric prefix argument of `3', emulate `org-refile-keep'
11183 being set to t and copy to the target location, don't move it.
11184 Beware that keeping refiled entries may result in duplicated ID
11187 RFLOC can be a refile location obtained in a different way.
11189 MSG is a string to replace \"Refile\" in the default prompt with
11190 another verb. E.g. `org-copy' sets this parameter to \"Copy\".
11192 See also `org-refile-use-outline-path'.
11194 If you are using target caching (see `org-refile-use-cache'), you
11195 have to clear the target cache in order to find new targets.
11196 This can be done with a `0' prefix (`C-0 C-c C-w') or a triple
11197 prefix argument (`C-u C-u C-u C-c C-w')."
11199 (if (member arg
'(0 (64)))
11200 (org-refile-cache-clear)
11201 (let* ((actionmsg (cond (msg msg
)
11202 ((equal arg
3) "Refile (and keep)")
11204 (regionp (org-region-active-p))
11205 (region-start (and regionp
(region-beginning)))
11206 (region-end (and regionp
(region-end)))
11207 (org-refile-keep (if (equal arg
3) t org-refile-keep
))
11208 pos it nbuf file level reversed
)
11209 (setq last-command nil
)
11211 (goto-char region-start
)
11212 (or (bolp) (goto-char (point-at-bol)))
11213 (setq region-start
(point))
11214 (unless (or (org-kill-is-subtree-p
11215 (buffer-substring region-start region-end
))
11216 (prog1 org-refile-active-region-within-subtree
11217 (let ((s (point-at-eol)))
11218 (org-toggle-heading)
11219 (setq region-end
(+ (- (point-at-eol) s
) region-end
)))))
11220 (user-error "The region is not a (sequence of) subtree(s)")))
11221 (if (equal arg
'(16))
11222 (org-refile-goto-last-stored)
11225 org-clock-hd-marker
(marker-buffer org-clock-hd-marker
)
11227 (setq it
(list (or org-clock-heading
"running clock")
11229 (marker-buffer org-clock-hd-marker
))
11231 (marker-position org-clock-hd-marker
)))
11235 (let (heading-text)
11237 (unless (and arg
(listp arg
))
11238 (org-back-to-heading t
)
11240 (replace-regexp-in-string
11241 org-bracket-link-regexp
11243 (or (nth 4 (org-heading-components))
11245 (org-refile-get-location
11246 (cond ((and arg
(listp arg
)) "Goto")
11247 (regionp (concat actionmsg
" region to"))
11248 (t (concat actionmsg
" subtree \""
11249 heading-text
"\" to")))
11251 (and (not (equal '(4) arg
))
11252 org-refile-allow-creating-parent-nodes
)))))))
11253 (setq file
(nth 1 it
)
11255 (when (and (not arg
)
11257 (equal (buffer-file-name) file
)
11259 (and (>= pos region-start
)
11260 (<= pos region-end
))
11261 (and (>= pos
(point))
11262 (< pos
(save-excursion
11263 (org-end-of-subtree t t
))))))
11264 (error "Cannot refile to position inside the tree or region"))
11265 (setq nbuf
(or (find-buffer-visiting file
)
11266 (find-file-noselect file
)))
11267 (if (and arg
(not (equal arg
3)))
11269 (pop-to-buffer-same-window nbuf
)
11270 (goto-char (cond (pos)
11271 ((org-notes-order-reversed-p) (point-min))
11273 (org-show-context 'org-goto
))
11276 (org-kill-new (buffer-substring region-start region-end
))
11277 (org-save-markers-in-region region-start region-end
))
11278 (org-copy-subtree 1 nil t
))
11279 (with-current-buffer (setq nbuf
(or (find-buffer-visiting file
)
11280 (find-file-noselect file
)))
11281 (setq reversed
(org-notes-order-reversed-p))
11282 (org-with-wide-buffer
11286 (setq level
(org-get-valid-level (funcall outline-level
) 1))
11289 (or (outline-next-heading) (point-max))
11290 (or (save-excursion (org-get-next-sibling))
11291 (org-end-of-subtree t t
)
11295 (goto-char (point-max))
11296 (goto-char (point-min))
11297 (or (outline-next-heading) (goto-char (point-max)))))
11298 (unless (bolp) (newline))
11299 (org-paste-subtree level nil nil t
)
11300 (when org-log-refile
11301 (org-add-log-setup 'refile nil nil org-log-refile
)
11302 (unless (eq org-log-refile
'note
)
11303 (save-excursion (org-add-log-note))))
11304 (and org-auto-align-tags
11305 (let ((org-loop-over-headlines-in-active-region nil
))
11306 (org-set-tags nil t
)))
11307 (let ((bookmark-name (plist-get org-bookmark-names-plist
11309 (when bookmark-name
11310 (with-demoted-errors
11311 (bookmark-set bookmark-name
))))
11312 ;; If we are refiling for capture, make sure that the
11313 ;; last-capture pointers point here
11314 (when (bound-and-true-p org-capture-is-refiling
)
11315 (let ((bookmark-name (plist-get org-bookmark-names-plist
11316 :last-capture-marker
)))
11317 (when bookmark-name
11318 (with-demoted-errors
11319 (bookmark-set bookmark-name
))))
11320 (move-marker org-capture-last-stored-marker
(point)))
11321 (when (fboundp 'deactivate-mark
) (deactivate-mark))
11322 (run-hooks 'org-after-refile-insert-hook
)))
11323 (unless org-refile-keep
11325 (delete-region (point) (+ (point) (- region-end region-start
)))
11326 (org-preserve-local-variables
11328 (and (org-back-to-heading t
) (point))
11329 (min (1+ (buffer-size)) (org-end-of-subtree t t
) (point))))))
11330 (when (featurep 'org-inlinetask
)
11331 (org-inlinetask-remove-END-maybe))
11332 (setq org-markers-to-move nil
)
11333 (message (concat actionmsg
" to \"%s\" in file %s: done") (car it
) file
)))))))
11335 (defun org-refile-goto-last-stored ()
11336 "Go to the location where the last refile was stored."
11338 (bookmark-jump (plist-get org-bookmark-names-plist
:last-refile
))
11339 (message "This is the location of the last refile"))
11341 (defun org-refile--get-location (refloc tbl
)
11342 "When user refile to REFLOC, find the associated target in TBL.
11343 Also check `org-refile-target-table'."
11347 (lambda (r) (or (assoc r tbl
)
11348 (assoc r org-refile-target-table
)))
11349 (list (replace-regexp-in-string "/$" "" refloc
)
11350 (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc
))))))
11352 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
)
11353 "Prompt the user for a refile location, using PROMPT.
11354 PROMPT should not be suffixed with a colon and a space, because
11355 this function appends the default value from
11356 `org-refile-history' automatically, if that is not empty."
11357 (let ((org-refile-targets org-refile-targets
)
11358 (org-refile-use-outline-path org-refile-use-outline-path
))
11359 (setq org-refile-target-table
(org-refile-get-targets default-buffer
)))
11360 (unless org-refile-target-table
11361 (user-error "No refile targets"))
11362 (let* ((cbuf (current-buffer))
11363 (cfn (buffer-file-name (buffer-base-buffer cbuf
)))
11364 (cfunc (if (and org-refile-use-outline-path
11365 org-outline-path-complete-in-steps
)
11366 #'org-olpath-completing-read
11367 #'completing-read
))
11368 (extra (if org-refile-use-outline-path
"/" ""))
11369 (cbnex (concat (buffer-name) extra
))
11370 (filename (and cfn
(expand-file-name cfn
)))
11373 (if (and (not (member org-refile-use-outline-path
11374 '(file full-file-path
)))
11375 (not (equal filename
(nth 1 x
))))
11376 (cons (concat (car x
) extra
" ("
11377 (file-name-nondirectory (nth 1 x
)) ")")
11379 (cons (concat (car x
) extra
) (cdr x
))))
11380 org-refile-target-table
))
11381 (completion-ignore-case t
)
11383 (prompt (concat prompt
11384 (or (and (car org-refile-history
)
11385 (concat " (default " (car org-refile-history
) ")"))
11386 (and (assoc cbnex tbl
) (setq cdef cbnex
)
11387 (concat " (default " cbnex
")"))) ": "))
11388 pa answ parent-target child parent old-hist
)
11389 (setq old-hist org-refile-history
)
11390 (setq answ
(funcall cfunc prompt tbl nil
(not new-nodes
)
11391 nil
'org-refile-history
(or cdef
(car org-refile-history
))))
11392 (if (setq pa
(org-refile--get-location answ tbl
))
11394 (org-refile-check-position pa
)
11395 (when (or (not org-refile-history
)
11396 (not (eq old-hist org-refile-history
))
11397 (not (equal (car pa
) (car org-refile-history
))))
11398 (setq org-refile-history
11399 (cons (car pa
) (if (assoc (car org-refile-history
) tbl
)
11401 (cdr org-refile-history
))))
11402 (when (equal (car org-refile-history
) (nth 1 org-refile-history
))
11403 (pop org-refile-history
)))
11405 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ
)
11407 (setq parent
(match-string 1 answ
)
11408 child
(match-string 2 answ
))
11409 (setq parent-target
(org-refile--get-location parent tbl
))
11410 (when (and parent-target
11411 (or (eq new-nodes t
)
11412 (and (eq new-nodes
'confirm
)
11413 (y-or-n-p (format "Create new node \"%s\"? "
11415 (org-refile-new-child parent-target child
)))
11416 (user-error "Invalid target location")))))
11418 (declare-function org-string-nw-p
"org-macs" (s))
11419 (defun org-refile-check-position (refile-pointer)
11420 "Check if the refile pointer matches the headline to which it points."
11421 (let* ((file (nth 1 refile-pointer
))
11422 (re (nth 2 refile-pointer
))
11423 (pos (nth 3 refile-pointer
))
11425 (if (and (not (markerp pos
)) (not file
))
11426 (user-error "Please indicate a target file in the refile path")
11427 (when (org-string-nw-p re
)
11428 (setq buffer
(if (markerp pos
)
11429 (marker-buffer pos
)
11430 (or (find-buffer-visiting file
)
11431 (find-file-noselect file
))))
11432 (with-current-buffer buffer
11433 (org-with-wide-buffer
11435 (beginning-of-line 1)
11436 (unless (looking-at-p re
)
11437 (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
11439 (defun org-refile-new-child (parent-target child
)
11440 "Use refile target PARENT-TARGET to add new CHILD below it."
11441 (unless parent-target
11442 (error "Cannot find parent for new node"))
11443 (let ((file (nth 1 parent-target
))
11444 (pos (nth 3 parent-target
))
11446 (with-current-buffer (or (find-buffer-visiting file
)
11447 (find-file-noselect file
))
11448 (org-with-wide-buffer
11451 (goto-char (point-max))
11452 (unless (bolp) (newline)))
11453 (when (looking-at org-outline-regexp
)
11454 (setq level
(funcall outline-level
))
11455 (org-end-of-subtree t t
))
11456 (org-back-over-empty-lines)
11457 (insert "\n" (make-string
11458 (if pos
(org-get-valid-level level
1) 1) ?
*)
11460 (beginning-of-line 0)
11461 (list (concat (car parent-target
) "/" child
) file
"" (point))))))
11463 (defun org-olpath-completing-read (prompt collection
&rest args
)
11464 "Read an outline path like a file name."
11465 (let ((thetable collection
))
11466 (apply #'completing-read
11468 (lambda (string predicate
&optional flag
)
11470 ((eq flag nil
) (try-completion string thetable
))
11472 (let ((l (length string
)))
11473 (mapcar (lambda (x)
11474 (let ((r (substring x l
))
11475 (f (if (string-match " ([^)]*)$" x
)
11478 (if (string-match "/" r
)
11479 (concat string
(substring r
0 (match-end 0)) f
)
11481 (all-completions string thetable predicate
))))
11483 ((eq flag
'lambda
) (assoc string thetable
))))
11486 ;;;; Dynamic blocks
11488 (defun org-find-dblock (name)
11489 "Find the first dynamic block with name NAME in the buffer.
11490 If not found, stay at current position and return nil."
11491 (let ((case-fold-search t
) pos
)
11493 (goto-char (point-min))
11494 (setq pos
(and (re-search-forward
11495 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name
"\\>") nil t
)
11496 (match-beginning 0))))
11497 (when pos
(goto-char pos
))
11500 (defun org-create-dblock (plist)
11501 "Create a dynamic block section, with parameters taken from PLIST.
11502 PLIST must contain a :name entry which is used as the name of the block."
11503 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11506 (let ((col (current-column))
11507 (name (plist-get plist
:name
)))
11508 (insert "#+BEGIN: " name
)
11510 (if (eq (car plist
) :name
)
11511 (setq plist
(cddr plist
))
11512 (insert " " (prin1-to-string (pop plist
)))))
11513 (insert "\n\n" (make-string col ?\
) "#+END:\n")
11514 (beginning-of-line -
2)))
11516 (defun org-prepare-dblock ()
11517 "Prepare dynamic block for refresh.
11518 This empties the block, puts the cursor at the insert position and returns
11519 the property list including an extra property :name with the block name."
11520 (unless (looking-at org-dblock-start-re
)
11521 (user-error "Not at a dynamic block"))
11522 (let* ((begdel (1+ (match-end 0)))
11523 (name (org-no-properties (match-string 1)))
11524 (params (append (list :name name
)
11525 (read (concat "(" (match-string 3) ")")))))
11527 (beginning-of-line 1)
11528 (skip-chars-forward " \t")
11529 (setq params
(plist-put params
:indentation-column
(current-column))))
11530 (unless (re-search-forward org-dblock-end-re nil t
)
11531 (error "Dynamic block not terminated"))
11534 (list :content
(buffer-substring
11535 begdel
(match-beginning 0)))))
11536 (delete-region begdel
(match-beginning 0))
11541 (defun org-map-dblocks (&optional command
)
11542 "Apply COMMAND to all dynamic blocks in the current buffer.
11543 If COMMAND is not given, use `org-update-dblock'."
11544 (let ((cmd (or command
'org-update-dblock
)))
11546 (goto-char (point-min))
11547 (while (re-search-forward org-dblock-start-re nil t
)
11548 (goto-char (match-beginning 0))
11550 (condition-case nil
11552 (error (message "Error during update of dynamic block"))))
11553 (unless (re-search-forward org-dblock-end-re nil t
)
11554 (error "Dynamic block not terminated"))))))
11556 (defun org-dblock-update (&optional arg
)
11557 "User command for updating dynamic blocks.
11558 Update the dynamic block at point. With prefix ARG, update all dynamic
11559 blocks in the buffer."
11562 (org-update-all-dblocks)
11563 (or (looking-at org-dblock-start-re
)
11564 (org-beginning-of-dblock))
11565 (org-update-dblock)))
11567 (defun org-update-dblock ()
11568 "Update the dynamic block at point.
11569 This means to empty the block, parse for parameters and then call
11570 the correct writing function."
11573 (let* ((win (selected-window))
11575 (line (org-current-line))
11576 (params (org-prepare-dblock))
11577 (name (plist-get params
:name
))
11578 (indent (plist-get params
:indentation-column
))
11579 (cmd (intern (concat "org-dblock-write:" name
))))
11580 (message "Updating dynamic block `%s' at line %d..." name line
)
11581 (funcall cmd params
)
11582 (message "Updating dynamic block `%s' at line %d...done" name line
)
11584 (when (and indent
(> indent
0))
11585 (setq indent
(make-string indent ?\
))
11587 (select-window win
)
11588 (org-beginning-of-dblock)
11590 (while (not (looking-at org-dblock-end-re
))
11592 (beginning-of-line 2))
11593 (when (looking-at org-dblock-end-re
)
11594 (and (looking-at "[ \t]+")
11595 (replace-match ""))
11596 (insert indent
)))))))
11598 (defun org-beginning-of-dblock ()
11599 "Find the beginning of the dynamic block at point.
11600 Error if there is no such block at point."
11601 (let ((pos (point))
11604 (if (and (re-search-backward org-dblock-start-re nil t
)
11605 (setq beg
(match-beginning 0))
11606 (re-search-forward org-dblock-end-re nil t
)
11607 (> (match-end 0) pos
))
11610 (error "Not in a dynamic block"))))
11612 (defun org-update-all-dblocks ()
11613 "Update all dynamic blocks in the buffer.
11614 This function can be used in a hook."
11616 (when (derived-mode-p 'org-mode
)
11617 (org-map-dblocks 'org-update-dblock
)))
11622 (declare-function org-export-backend-options
"ox" (cl-x) t
)
11623 (defun org-get-export-keywords ()
11624 "Return a list of all currently understood export keywords.
11625 Export keywords include options, block names, attributes and
11626 keywords relative to each registered export back-end."
11629 (bound-and-true-p org-export-registered-backends
)
11630 (delq nil keywords
))
11631 ;; Back-end name (for keywords, like #+LATEX:)
11632 (push (upcase (symbol-name (org-export-backend-name backend
))) keywords
)
11633 (dolist (option-entry (org-export-backend-options backend
))
11634 ;; Back-end options.
11635 (push (nth 1 option-entry
) keywords
)))))
11637 (defconst org-options-keywords
11638 '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
11639 "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
11640 "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
11641 "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
11642 "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
11644 (defcustom org-structure-template-alist
11645 '((?a .
"export ascii")
11650 (?h .
"export html")
11651 (?l .
"export latex")
11655 "Structure completion elements.
11656 This is an alist of characters and values. When
11657 `org-insert-structure-template' is called, an additional key is
11658 read. The key is first looked up in this alist, and the
11659 corresponding structure is inserted, with \"#+BEGIN_\" and
11660 \"#+END_\" added automatically."
11661 :group
'org-edit-structure
11663 (cons (character :tag
"Key")
11664 (string :tag
"Template")))
11665 :package-version
'(Org .
"9.2"))
11667 (defun org-insert-structure-template (type)
11668 "Insert a block structure of the type #+begin_foo/#+end_foo.
11669 First read a character, which can be one of the keys in
11670 `org-structure-template-alist'. When it is <TAB>, prompt the
11671 user for a string to use. With an active region, wrap the region
11672 in the block. Otherwise, insert an empty block."
11675 (let* ((key (read-key "Key: "))
11677 (or (cdr (assq key org-structure-template-alist
))
11679 (read-string "Structure type: "))
11680 (user-error "`%c' has no structure definition" key
))))
11682 (let* ((region?
(use-region-p))
11683 (s (if region?
(region-beginning) (point)))
11684 (e (copy-marker (if region?
(region-end) (point)) t
))
11686 (when (string-match-p
11687 (concat "\\`" (regexp-opt '("example" "export" "src")))
11689 (org-escape-code-in-region s e
))
11691 (setq column
(current-indentation))
11692 (beginning-of-line)
11694 (insert (format "#+begin_%s%s\n" type
(if (string-equal "src" type
) " " "")))
11698 (skip-chars-backward " \n\t")
11703 (insert (format "#+end_%s\n"
11704 (car (split-string type
))))
11705 (when (or (not region?
)
11706 (string-match-p "src\\|\\`export\\'" type
))
11709 (set-marker e nil
)))
11711 ;;;; TODO, DEADLINE, Comments
11713 (defun org-toggle-comment ()
11714 "Change the COMMENT state of an entry."
11717 (org-back-to-heading)
11718 (let ((case-fold-search nil
))
11719 (looking-at org-complex-heading-regexp
))
11720 (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
11721 (skip-chars-forward " \t")
11722 (unless (memq (char-before) '(?\s ?
\t)) (insert " "))
11723 (if (org-in-commented-heading-p t
)
11724 (delete-region (point)
11725 (progn (search-forward " " (line-end-position) 'move
)
11726 (skip-chars-forward " \t")
11728 (insert org-comment-string
)
11729 (unless (eolp) (insert " ")))))
11731 (defvar org-last-todo-state-is-todo nil
11732 "This is non-nil when the last TODO state change led to a TODO state.
11733 If the last change removed the TODO tag or switched to DONE, then
11736 (defvar org-setting-tags nil
) ; dynamically scoped
11738 (defvar org-todo-setup-filter-hook nil
11739 "Hook for functions that pre-filter todo specs.
11740 Each function takes a todo spec and returns either nil or the spec
11741 transformed into canonical form." )
11743 (defvar org-todo-get-default-hook nil
11744 "Hook for functions that get a default item for todo.
11745 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11746 nil or a string to be used for the todo mark." )
11748 (defvar org-agenda-headline-snapshot-before-repeat
)
11750 (defun org-current-effective-time ()
11751 "Return current time adjusted for `org-extend-today-until' variable."
11752 (let* ((ct (org-current-time))
11753 (dct (decode-time ct
))
11756 (org-use-last-clock-out-time-as-effective-time
11757 (or (org-clock-get-last-clock-out-time) ct
))
11758 ((and org-use-effective-time
(< (nth 2 dct
) org-extend-today-until
))
11759 (encode-time 0 59 23 (1- (nth 3 dct
)) (nth 4 dct
) (nth 5 dct
)))
11763 (defun org-todo-yesterday (&optional arg
)
11764 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11766 (if (eq major-mode
'org-agenda-mode
)
11767 (apply 'org-agenda-todo-yesterday arg
)
11768 (let* ((org-use-effective-time t
)
11769 (hour (nth 2 (decode-time (org-current-time))))
11770 (org-extend-today-until (1+ hour
)))
11773 (defvar org-block-entry-blocking
""
11774 "First entry preventing the TODO state change.")
11776 (defun org-cancel-repeater ()
11777 "Cancel a repeater by setting its numeric value to zero."
11780 (org-back-to-heading t
)
11781 (let ((bound1 (point))
11782 (bound0 (save-excursion (outline-next-heading) (point))))
11783 (when (and (re-search-forward
11784 (concat "\\(" org-scheduled-time-regexp
"\\)\\|\\("
11785 org-deadline-time-regexp
"\\)\\|\\("
11786 org-ts-regexp
"\\)")
11788 (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
11790 (replace-match "0" t nil nil
1)))))
11793 (defvar org-blocked-by-checkboxes
)
11794 (defun org-todo (&optional arg
)
11795 "Change the TODO state of an item.
11797 The state of an item is given by a keyword at the start of the heading,
11799 *** TODO Write paper
11802 The different keywords are specified in the variable `org-todo-keywords'.
11803 By default the available states are \"TODO\" and \"DONE\". So, for this
11804 example: when the item starts with TODO, it is changed to DONE.
11805 When it starts with DONE, the DONE is removed. And when neither TODO nor
11806 DONE are present, add TODO at the beginning of the heading.
11808 With `\\[universal-argument]' prefix ARG, use completion to determine the new \
11810 With numeric prefix ARG, switch to that state.
11811 With a `\\[universal-argument] \\[universal-argument]' prefix, switch to the \
11813 keywords (nextset).
11814 With a `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
11815 prefix, circumvent any state blocking.
11816 With a numeric prefix arg of 0, inhibit note taking for the change.
11817 With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
11819 When called through ELisp, arg is also interpreted in the following way:
11820 `none' -> empty state
11821 \"\" -> switch to empty state
11822 `done' -> switch to DONE
11823 `nextset' -> switch to the next set of keywords
11824 `previousset' -> switch to the previous set of keywords
11825 \"WAITING\" -> switch to the specified keyword, but only if it
11826 really is a member of `org-todo-keywords'."
11828 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
11829 (let ((cl (if (eq org-loop-over-headlines-in-active-region
'start-level
)
11830 'region-start-level
'region
))
11831 org-loop-over-headlines-in-active-region
)
11834 org-loop-over-headlines-in-active-region
11835 cl
(when (org-invisible-p) (org-end-of-subtree nil t
))))
11836 (when (equal arg
'(16)) (setq arg
'nextset
))
11837 (when (equal arg -
1) (org-cancel-repeater) (setq arg nil
))
11838 (let ((org-blocker-hook org-blocker-hook
)
11841 (when (equal arg
'(64))
11842 (setq arg nil org-blocker-hook nil
))
11843 (when (and org-blocker-hook
11844 (or org-inhibit-blocking
11845 (org-entry-get nil
"NOBLOCKING")))
11846 (setq org-blocker-hook nil
))
11849 (org-back-to-heading t
)
11850 (when (org-in-commented-heading-p t
)
11851 (org-toggle-comment)
11853 (when (looking-at org-outline-regexp
) (goto-char (1- (match-end 0))))
11854 (or (looking-at (concat " +" org-todo-regexp
"\\( +\\|[ \t]*$\\)"))
11855 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11856 (let* ((match-data (match-data))
11857 (startpos (copy-marker (line-beginning-position)))
11858 (logging (save-match-data (org-entry-get nil
"LOGGING" t t
)))
11859 (org-log-done org-log-done
)
11860 (org-log-repeat org-log-repeat
)
11861 (org-todo-log-states org-todo-log-states
)
11862 (org-inhibit-logging
11864 (progn (setq arg nil
) 'note
) org-inhibit-logging
))
11865 (this (match-string 1))
11866 (hl-pos (match-beginning 0))
11867 (head (org-get-todo-sequence-head this
))
11868 (ass (assoc head org-todo-kwd-alist
))
11869 (interpret (nth 1 ass
))
11870 (done-word (nth 3 ass
))
11871 (final-done-word (nth 4 ass
))
11872 (org-last-state (or this
""))
11873 (completion-ignore-case t
)
11874 (member (member this org-todo-keywords-1
))
11875 (tail (cdr member
))
11877 ((and org-todo-key-trigger
11878 (or (and (equal arg
'(4))
11879 (eq org-use-fast-todo-selection
'prefix
))
11880 (and (not arg
) org-use-fast-todo-selection
11881 (not (eq org-use-fast-todo-selection
11883 ;; Use fast selection.
11884 (org-fast-todo-selection))
11885 ((and (equal arg
'(4))
11886 (or (not org-use-fast-todo-selection
)
11887 (not org-todo-key-trigger
)))
11888 ;; Read a state with completion.
11890 "State: " (mapcar #'list org-todo-keywords-1
)
11894 (if tail
(car tail
) nil
)
11895 (car org-todo-keywords-1
)))
11897 (unless (equal member org-todo-keywords-1
)
11899 (nth (- (length org-todo-keywords-1
)
11901 org-todo-keywords-1
)
11902 (org-last org-todo-keywords-1
))))
11903 ((and (eq org-use-fast-todo-selection t
) (equal arg
'(4))
11904 (setq arg nil
))) ;hack to fall back to cycling
11906 ;; User or caller requests a specific state.
11908 ((equal arg
"") nil
)
11909 ((eq arg
'none
) nil
)
11910 ((eq arg
'done
) (or done-word
(car org-done-keywords
)))
11912 (or (car (cdr (member head org-todo-heads
)))
11913 (car org-todo-heads
)))
11914 ((eq arg
'previousset
)
11915 (let ((org-todo-heads (reverse org-todo-heads
)))
11916 (or (car (cdr (member head org-todo-heads
)))
11917 (car org-todo-heads
))))
11918 ((car (member arg org-todo-keywords-1
)))
11920 (user-error "State `%s' not valid in this file" arg
))
11921 ((nth (1- (prefix-numeric-value arg
))
11922 org-todo-keywords-1
))))
11923 ((null member
) (or head
(car org-todo-keywords-1
)))
11924 ((equal this final-done-word
) nil
) ;-> make empty
11925 ((null tail
) nil
) ;-> first entry
11926 ((memq interpret
'(type priority
))
11927 (if (eq this-command last-command
)
11929 (if (> (length tail
) 0)
11930 (or done-word
(car org-done-keywords
))
11935 (run-hook-with-args-until-success
11936 'org-todo-get-default-hook org-state org-last-state
)
11938 (next (if org-state
(concat " " org-state
" ") " "))
11939 (change-plist (list :type
'todo-state-change
:from this
:to org-state
11940 :position startpos
))
11942 (when org-blocker-hook
11943 (let (org-blocked-by-checkboxes block-reason
)
11944 (setq org-last-todo-state-is-todo
11945 (not (member this org-done-keywords
)))
11946 (unless (save-excursion
11948 (org-with-wide-buffer
11949 (run-hook-with-args-until-failure
11950 'org-blocker-hook change-plist
))))
11951 (setq block-reason
(if org-blocked-by-checkboxes
11952 "contained checkboxes"
11953 (format "\"%s\"" org-block-entry-blocking
)))
11954 (if (called-interactively-p 'interactive
)
11955 (user-error "TODO state change from %s to %s blocked (by %s)"
11956 this org-state block-reason
)
11958 (message "TODO state change from %s to %s blocked (by %s)"
11959 this org-state block-reason
)
11960 (throw 'exit nil
)))))
11961 (store-match-data match-data
)
11962 (replace-match next t t
)
11963 (cond ((equal this org-state
)
11964 (message "TODO state was already %s" (org-trim next
)))
11965 ((not (pos-visible-in-window-p hl-pos
))
11966 (message "TODO state changed to %s" (org-trim next
))))
11968 (setq head
(org-get-todo-sequence-head org-state
)
11969 ass
(assoc head org-todo-kwd-alist
)
11970 interpret
(nth 1 ass
)
11971 done-word
(nth 3 ass
)
11972 final-done-word
(nth 4 ass
)))
11973 (when (memq arg
'(nextset previousset
))
11974 (message "Keyword-Set %d/%d: %s"
11975 (- (length org-todo-sets
) -
1
11976 (length (memq (assoc org-state org-todo-sets
) org-todo-sets
)))
11977 (length org-todo-sets
)
11978 (mapconcat 'identity
(assoc org-state org-todo-sets
) " ")))
11979 (setq org-last-todo-state-is-todo
11980 (not (member org-state org-done-keywords
)))
11981 (setq now-done-p
(and (member org-state org-done-keywords
)
11982 (not (member this org-done-keywords
))))
11983 (and logging
(org-local-logging logging
))
11984 (when (and (or org-todo-log-states org-log-done
)
11985 (not (eq org-inhibit-logging t
))
11986 (not (memq arg
'(nextset previousset
))))
11987 ;; We need to look at recording a time and note.
11988 (setq dolog
(or (nth 1 (assoc org-state org-todo-log-states
))
11989 (nth 2 (assoc this org-todo-log-states
))))
11990 (when (and (eq dolog
'note
) (eq org-inhibit-logging
'note
))
11991 (setq dolog
'time
))
11992 (when (or (and (not org-state
) (not org-closed-keep-when-no-todo
))
11994 (member org-state org-not-done-keywords
)
11995 (not (member this org-not-done-keywords
))))
11996 ;; This is now a todo state and was not one before
11997 ;; If there was a CLOSED time stamp, get rid of it.
11998 (org-add-planning-info nil nil
'closed
))
11999 (when (and now-done-p org-log-done
)
12000 ;; It is now done, and it was not done before.
12001 (org-add-planning-info 'closed
(org-current-effective-time))
12002 (when (and (not dolog
) (eq 'note org-log-done
))
12003 (org-add-log-setup 'done org-state this
'note
)))
12004 (when (and org-state dolog
)
12005 ;; This is a non-nil state, and we need to log it.
12006 (org-add-log-setup 'state org-state this dolog
)))
12007 ;; Fixup tag positioning.
12008 (org-todo-trigger-tag-changes org-state
)
12009 (and org-auto-align-tags
(not org-setting-tags
) (org-set-tags nil t
))
12010 (when org-provide-todo-statistics
12011 (org-update-parent-todo-statistics))
12012 (run-hooks 'org-after-todo-state-change-hook
)
12013 (when (and arg
(not (member org-state org-done-keywords
)))
12014 (setq head
(org-get-todo-sequence-head org-state
)))
12015 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head
)
12016 ;; Do we need to trigger a repeat?
12018 (when (boundp 'org-agenda-headline-snapshot-before-repeat
)
12019 ;; This is for the agenda, take a snapshot of the headline.
12021 (setq org-agenda-headline-snapshot-before-repeat
12022 (org-get-heading))))
12023 (org-auto-repeat-maybe org-state
))
12024 ;; Fixup cursor location if close to the keyword.
12025 (when (and (outline-on-heading-p)
12027 (save-excursion (beginning-of-line 1)
12028 (looking-at org-todo-line-regexp
))
12029 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
12030 (goto-char (or (match-end 2) (match-end 1)))
12031 (and (looking-at " ") (just-one-space)))
12032 (when org-trigger-hook
12034 (run-hook-with-args 'org-trigger-hook change-plist
)))
12035 (when commentp
(org-toggle-comment))))))))
12037 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
12038 "Block turning an entry into a TODO, using the hierarchy.
12039 This checks whether the current task should be blocked from state
12040 changes. Such blocking occurs when:
12042 1. The task has children which are not all in a completed state.
12044 2. A task has a parent with the property :ORDERED:, and there
12045 are siblings prior to the current task with incomplete
12048 3. The parent of the task is blocked because it has siblings that should
12049 be done first, or is child of a block grandparent TODO entry."
12051 (if (not org-enforce-todo-dependencies
)
12052 t
; if locally turned off don't block
12054 ;; If this is not a todo state change, or if this entry is already DONE,
12056 (when (or (not (eq (plist-get change-plist
:type
) 'todo-state-change
))
12057 (member (plist-get change-plist
:from
)
12058 (cons 'done org-done-keywords
))
12059 (member (plist-get change-plist
:to
)
12060 (cons 'todo org-not-done-keywords
))
12061 (not (plist-get change-plist
:to
)))
12062 (throw 'dont-block t
))
12063 ;; If this task has children, and any are undone, it's blocked
12065 (org-back-to-heading t
)
12066 (let ((this-level (funcall outline-level
)))
12067 (outline-next-heading)
12068 (let ((child-level (funcall outline-level
)))
12069 (while (and (not (eobp))
12070 (> child-level this-level
))
12071 ;; this todo has children, check whether they are all
12073 (when (and (not (org-entry-is-done-p))
12074 (org-entry-is-todo-p))
12075 (setq org-block-entry-blocking
(org-get-heading))
12076 (throw 'dont-block nil
))
12077 (outline-next-heading)
12078 (setq child-level
(funcall outline-level
))))))
12079 ;; Otherwise, if the task's parent has the :ORDERED: property, and
12080 ;; any previous siblings are undone, it's blocked
12082 (org-back-to-heading t
)
12083 (let* ((pos (point))
12084 (parent-pos (and (org-up-heading-safe) (point)))
12085 (case-fold-search nil
))
12086 (unless parent-pos
(throw 'dont-block t
)) ; no parent
12087 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12089 (re-search-forward org-not-done-heading-regexp pos t
))
12090 (setq org-block-entry-blocking
(match-string 0))
12091 (throw 'dont-block nil
)) ; block, there is an older sibling not done.
12092 ;; Search further up the hierarchy, to see if an ancestor is blocked
12094 (goto-char parent-pos
)
12095 (unless (looking-at org-not-done-heading-regexp
)
12096 (throw 'dont-block t
)) ; do not block, parent is not a TODO
12098 (setq parent-pos
(and (org-up-heading-safe) (point)))
12099 (unless parent-pos
(throw 'dont-block t
)) ; no parent
12100 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12102 (re-search-forward org-not-done-heading-regexp pos t
)
12103 (setq org-block-entry-blocking
(org-get-heading)))
12104 (throw 'dont-block nil
)))))))) ; block, older sibling not done.
12106 (defcustom org-track-ordered-property-with-tag nil
12107 "Should the ORDERED property also be shown as a tag?
12108 The ORDERED property decides if an entry should require subtasks to be
12109 completed in sequence. Since a property is not very visible, setting
12110 this option means that toggling the ORDERED property with the command
12111 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
12112 not relevant for the behavior, but it makes things more visible.
12114 Note that toggling the tag with tags commands will not change the property
12115 and therefore not influence behavior!
12117 This can be t, meaning the tag ORDERED should be used, It can also be a
12118 string to select a different tag for this task."
12121 (const :tag
"No tracking" nil
)
12122 (const :tag
"Track with ORDERED tag" t
)
12123 (string :tag
"Use other tag")))
12125 (defun org-toggle-ordered-property ()
12126 "Toggle the ORDERED property of the current entry.
12127 For better visibility, you can track the value of this property with a tag.
12128 See variable `org-track-ordered-property-with-tag'."
12130 (let* ((t1 org-track-ordered-property-with-tag
)
12131 (tag (and t1
(if (stringp t1
) t1
"ORDERED"))))
12133 (org-back-to-heading)
12134 (if (org-entry-get nil
"ORDERED")
12136 (org-delete-property "ORDERED")
12137 (and tag
(org-toggle-tag tag
'off
))
12138 (message "Subtasks can be completed in arbitrary order"))
12139 (org-entry-put nil
"ORDERED" "t")
12140 (and tag
(org-toggle-tag tag
'on
))
12141 (message "Subtasks must be completed in sequence")))))
12143 (defun org-block-todo-from-checkboxes (change-plist)
12144 "Block turning an entry into a TODO, using checkboxes.
12145 This checks whether the current task should be blocked from state
12146 changes because there are unchecked boxes in this entry."
12147 (if (not org-enforce-todo-checkbox-dependencies
)
12148 t
; if locally turned off don't block
12150 ;; If this is not a todo state change, or if this entry is already DONE,
12152 (when (or (not (eq (plist-get change-plist
:type
) 'todo-state-change
))
12153 (member (plist-get change-plist
:from
)
12154 (cons 'done org-done-keywords
))
12155 (member (plist-get change-plist
:to
)
12156 (cons 'todo org-not-done-keywords
))
12157 (not (plist-get change-plist
:to
)))
12158 (throw 'dont-block t
))
12159 ;; If this task has checkboxes that are not checked, it's blocked
12161 (org-back-to-heading t
)
12162 (let ((beg (point)) end
)
12163 (outline-next-heading)
12166 (when (org-list-search-forward
12167 (concat (org-item-beginning-re)
12168 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
12171 (when (boundp 'org-blocked-by-checkboxes
)
12172 (setq org-blocked-by-checkboxes t
))
12173 (throw 'dont-block nil
))))
12174 t
))) ; do not block
12176 (defun org-entry-blocked-p ()
12177 "Non-nil if entry at point is blocked."
12178 (and (not (org-entry-get nil
"NOBLOCKING"))
12179 (member (org-entry-get nil
"TODO") org-not-done-keywords
)
12180 (not (run-hook-with-args-until-failure
12182 (list :type
'todo-state-change
12187 (defun org-update-statistics-cookies (all)
12188 "Update the statistics cookie, either from TODO or from checkboxes.
12189 This should be called with the cursor in a line with a statistics
12190 cookie. When called with a \\[universal-argument] prefix, update
12191 all statistics cookies in the buffer."
12195 (org-update-checkbox-count 'all
)
12196 (org-map-entries 'org-update-parent-todo-statistics
))
12197 (if (not (org-at-heading-p))
12198 (org-update-checkbox-count)
12199 (let ((pos (point-marker))
12201 (ignore-errors (org-back-to-heading t
))
12202 (if (not (org-at-heading-p))
12203 (org-update-checkbox-count)
12204 (setq l1
(org-outline-level))
12205 (setq end
(save-excursion
12206 (outline-next-heading)
12207 (when (org-at-heading-p) (setq l2
(org-outline-level)))
12209 (if (and (save-excursion
12211 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t
))
12212 (not (save-excursion (re-search-forward
12213 ":COOKIE_DATA:.*\\<todo\\>" end t
))))
12214 (org-update-checkbox-count)
12215 (if (and l2
(> l2 l1
))
12218 (org-update-parent-todo-statistics))
12220 (beginning-of-line 1)
12221 (while (re-search-forward
12222 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
12224 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t
)))))
12226 (move-marker pos nil
)))))
12228 (defvar org-entry-property-inherited-from
) ;; defined below
12229 (defun org-update-parent-todo-statistics ()
12230 "Update any statistics cookie in the parent of the current headline.
12231 When `org-hierarchical-todo-statistics' is nil, statistics will cover
12232 the entire subtree and this will travel up the hierarchy and update
12233 statistics everywhere."
12234 (let* ((prop (save-excursion (org-up-heading-safe)
12235 (org-entry-get nil
"COOKIE_DATA" 'inherit
)))
12236 (recursive (or (not org-hierarchical-todo-statistics
)
12237 (and prop
(string-match "\\<recursive\\>" prop
))))
12238 (lim (or (and prop
(marker-position org-entry-property-inherited-from
))
12241 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
12242 level ltoggle l1 new ndel
12243 (cnt-all 0) (cnt-done 0) is-percent kwd
12244 checkbox-beg cookie-present
)
12247 (beginning-of-line 1)
12248 (setq ltoggle
(funcall outline-level
))
12249 ;; Three situations are to consider:
12251 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
12252 ;; to the top-level ancestor on the headline;
12254 ;; 2. If parent has "recursive" property, repeat up to the
12255 ;; headline setting that property, taking inheritance into
12258 ;; 3. Else, move up to direct parent and proceed only once.
12259 (while (and (setq level
(org-up-heading-safe))
12260 (or recursive first
)
12262 (setq first nil cookie-present nil
)
12266 (downcase (or (org-entry-get nil
"COOKIE_DATA")
12269 (while (re-search-forward box-re
(point-at-eol) t
)
12270 (setq cnt-all
0 cnt-done
0 cookie-present t
)
12271 (setq is-percent
(match-end 2) checkbox-beg
(match-beginning 0))
12273 (unless (outline-next-heading) (throw 'exit nil
))
12274 (while (and (looking-at org-complex-heading-regexp
)
12275 (> (setq l1
(length (match-string 1))) level
))
12276 (setq kwd
(and (or recursive
(= l1 ltoggle
))
12278 (if (or (eq org-provide-todo-statistics
'all-headlines
)
12279 (and (eq org-provide-todo-statistics t
)
12280 (or (member kwd org-done-keywords
)))
12281 (and (listp org-provide-todo-statistics
)
12282 (stringp (car org-provide-todo-statistics
))
12283 (or (member kwd org-provide-todo-statistics
)
12284 (member kwd org-done-keywords
)))
12285 (and (listp org-provide-todo-statistics
)
12286 (listp (car org-provide-todo-statistics
))
12287 (or (member kwd
(car org-provide-todo-statistics
))
12288 (and (member kwd org-done-keywords
)
12289 (member kwd
(cadr org-provide-todo-statistics
))))))
12290 (setq cnt-all
(1+ cnt-all
))
12291 (and (eq org-provide-todo-statistics t
)
12293 (setq cnt-all
(1+ cnt-all
))))
12294 (when (or (and (member org-provide-todo-statistics
'(t all-headlines
))
12295 (member kwd org-done-keywords
))
12296 (and (listp org-provide-todo-statistics
)
12297 (listp (car org-provide-todo-statistics
))
12298 (member kwd org-done-keywords
)
12299 (member kwd
(cadr org-provide-todo-statistics
)))
12300 (and (listp org-provide-todo-statistics
)
12301 (stringp (car org-provide-todo-statistics
))
12302 (member kwd org-done-keywords
)))
12303 (setq cnt-done
(1+ cnt-done
)))
12304 (outline-next-heading)))
12307 (format "[%d%%]" (floor (* 100.0 cnt-done
)
12309 (format "[%d/%d]" cnt-done cnt-all
))
12310 ndel
(- (match-end 0) checkbox-beg
))
12311 (goto-char checkbox-beg
)
12313 (delete-region (point) (+ (point) ndel
))
12314 (when org-auto-align-tags
(org-fix-tags-on-the-fly)))
12315 (when cookie-present
12316 (run-hook-with-args 'org-after-todo-statistics-hook
12317 cnt-done
(- cnt-all cnt-done
))))))
12318 (run-hooks 'org-todo-statistics-hook
)))
12320 (defvar org-after-todo-statistics-hook nil
12321 "Hook that is called after a TODO statistics cookie has been updated.
12322 Each function is called with two arguments: the number of not-done entries
12323 and the number of done entries.
12325 For example, the following function, when added to this hook, will switch
12326 an entry to DONE when all children are done, and back to TODO when new
12327 entries are set to a TODO status. Note that this hook is only called
12328 when there is a statistics cookie in the headline!
12330 (defun org-summary-todo (n-done n-not-done)
12331 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
12332 (let (org-log-done org-log-states) ; turn off logging
12333 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
12336 (defvar org-todo-statistics-hook nil
12337 "Hook that is run whenever Org thinks TODO statistics should be updated.
12338 This hook runs even if there is no statistics cookie present, in which case
12339 `org-after-todo-statistics-hook' would not run.")
12341 (defun org-todo-trigger-tag-changes (state)
12342 "Apply the changes defined in `org-todo-state-tags-triggers'."
12343 (let ((l org-todo-state-tags-triggers
)
12345 (when (or (not state
) (equal state
""))
12346 (setq changes
(append changes
(cdr (assoc "" l
)))))
12347 (when (and (stringp state
) (> (length state
) 0))
12348 (setq changes
(append changes
(cdr (assoc state l
)))))
12349 (when (member state org-not-done-keywords
)
12350 (setq changes
(append changes
(cdr (assq 'todo l
)))))
12351 (when (member state org-done-keywords
)
12352 (setq changes
(append changes
(cdr (assq 'done l
)))))
12353 (dolist (c changes
)
12354 (org-toggle-tag (car c
) (if (cdr c
) 'on
'off
)))))
12356 (defun org-local-logging (value)
12357 "Get logging settings from a property VALUE."
12358 ;; Directly set the variables, they are already local.
12359 (setq org-log-done nil
12361 org-todo-log-states nil
)
12362 (dolist (w (split-string value
))
12365 ((setq a
(assoc w org-startup-options
))
12366 (and (member (nth 1 a
) '(org-log-done org-log-repeat
))
12367 (set (nth 1 a
) (nth 2 a
))))
12368 ((setq a
(org-extract-log-state-settings w
))
12369 (and (member (car a
) org-todo-keywords-1
)
12370 (push a org-todo-log-states
)))))))
12372 (defun org-get-todo-sequence-head (kwd)
12373 "Return the head of the TODO sequence to which KWD belongs.
12374 If KWD is not set, check if there is a text property remembering the
12379 (or (get-text-property (point-at-bol) 'org-todo-head
)
12381 (setq p
(next-single-property-change (point-at-bol) 'org-todo-head
12382 nil
(point-at-eol)))
12383 (get-text-property p
'org-todo-head
))))
12384 ((not (member kwd org-todo-keywords-1
))
12385 (car org-todo-keywords-1
))
12386 (t (nth 2 (assoc kwd org-todo-kwd-alist
))))))
12388 (defun org-fast-todo-selection ()
12389 "Fast TODO keyword selection with single keys.
12390 Returns the new TODO keyword, or nil if no state change should occur."
12391 (let* ((fulltable org-todo-key-alist
)
12392 (done-keywords org-done-keywords
) ;; needed for the faces.
12393 (maxlen (apply 'max
(mapcar
12395 (if (stringp (car x
)) (string-width (car x
)) 0))
12398 (fwidth (+ maxlen
3 1 3))
12399 (ncol (/ (- (window-width) 4) fwidth
))
12403 (save-window-excursion
12405 (set-buffer (get-buffer-create " *Org todo*"))
12406 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
12408 (setq-local org-done-keywords done-keywords
)
12409 (setq tbl fulltable cnt
0)
12410 (while (setq e
(pop tbl
))
12412 ((equal e
'(:startgroup
))
12413 (push '() groups
) (setq ingroup t
)
12418 ((equal e
'(:endgroup
))
12419 (setq ingroup nil cnt
0)
12421 ((equal e
'(:newline
))
12426 (while (equal (car tbl
) '(:newline
))
12428 (setq tbl
(cdr tbl
)))))
12430 (setq tg
(car e
) c
(cdr e
))
12431 (when ingroup
(push tg
(car groups
)))
12432 (setq tg
(org-add-props tg nil
'face
12433 (org-get-todo-face tg
)))
12434 (when (and (= cnt
0) (not ingroup
)) (insert " "))
12435 (insert "[" c
"] " tg
(make-string
12436 (- fwidth
4 (length tg
)) ?\
))
12437 (when (= (setq cnt
(1+ cnt
)) ncol
)
12439 (when ingroup
(insert " "))
12442 (goto-char (point-min))
12443 (unless expert
(org-fit-window-to-buffer))
12444 (message "[a-z..]:Set [SPC]:clear")
12445 (setq c
(let ((inhibit-quit t
)) (read-char-exclusive)))
12448 (and (= c ?q
) (not (rassoc c fulltable
))))
12449 (setq quit-flag t
))
12451 ((setq e
(rassoc c fulltable
) tg
(car e
))
12453 (t (setq quit-flag t
)))))))
12455 (defun org-entry-is-todo-p ()
12456 (member (org-get-todo-state) org-not-done-keywords
))
12458 (defun org-entry-is-done-p ()
12459 (member (org-get-todo-state) org-done-keywords
))
12461 (defun org-get-todo-state ()
12462 "Return the TODO keyword of the current subtree."
12464 (org-back-to-heading t
)
12465 (and (let ((case-fold-search nil
)) (looking-at org-todo-line-regexp
))
12467 (match-string 2))))
12469 (defun org-at-date-range-p (&optional inactive-ok
)
12470 "Non-nil if point is inside a date range.
12472 When optional argument INACTIVE-OK is non-nil, also consider
12473 inactive time ranges.
12475 When this function returns a non-nil value, match data is set
12476 according to `org-tr-regexp-both' or `org-tr-regexp', depending
12481 (let ((pos (point)))
12482 (skip-chars-backward "^[<\r\n")
12483 (skip-chars-backward "<[")
12484 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp
))
12485 (>= (match-end 0) pos
)
12487 (skip-chars-backward "^<[\r\n")
12488 (skip-chars-backward "<[")
12489 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp
))
12490 (>= (match-end 0) pos
)
12494 (defun org-get-repeat (&optional timestamp
)
12495 "Check if there is a time-stamp with repeater in this entry.
12497 Return the repeater, as a string, or nil. Also return nil when
12498 this function is called before first heading.
12500 When optional argument TIMESTAMP is a string, extract the
12501 repeater from there instead."
12504 (and (string-match org-repeat-re timestamp
)
12505 (match-string-no-properties 1 timestamp
)))
12506 ((org-before-first-heading-p) nil
)
12509 (org-back-to-heading t
)
12510 (let ((end (org-entry-end-position)))
12512 (while (re-search-forward org-repeat-re end t
)
12513 (when (save-match-data (org-at-timestamp-p 'agenda
))
12514 (throw :repeat
(match-string-no-properties 1)))))))))))
12516 (defvar org-last-changed-timestamp
)
12517 (defvar org-last-inserted-timestamp
)
12518 (defvar org-log-post-message
)
12519 (defvar org-log-note-purpose
)
12520 (defvar org-log-note-how nil
)
12521 (defvar org-log-note-extra
)
12522 (defun org-auto-repeat-maybe (done-word)
12523 "Check if the current headline contains a repeated time-stamp.
12525 If yes, set TODO state back to what it was and change the base date
12526 of repeating deadline/scheduled time stamps to new date.
12528 This function is run automatically after each state change to a DONE state."
12529 (let* ((repeat (org-get-repeat))
12530 (aa (assoc org-last-state org-todo-kwd-alist
))
12531 (interpret (nth 1 aa
))
12533 (whata '(("h" . hour
) ("d" . day
) ("m" . month
) ("y" . year
)))
12534 (msg "Entry repeats: ")
12536 (org-todo-log-states nil
)
12537 (end (copy-marker (org-entry-end-position))))
12539 (when (and repeat
(not (zerop (string-to-number (substring repeat
1)))))
12540 (when (eq org-log-repeat t
) (setq org-log-repeat
'state
))
12541 (let ((to-state (or (org-entry-get nil
"REPEAT_TO_STATE" 'selective
)
12542 (and (stringp org-todo-repeat-to-state
)
12543 org-todo-repeat-to-state
)
12544 (and org-todo-repeat-to-state org-last-state
))))
12546 ((and to-state
(member to-state org-todo-keywords-1
))
12548 ((eq interpret
'type
) org-last-state
)
12551 (org-back-to-heading t
)
12552 (org-add-planning-info nil nil
'closed
)
12553 ;; When `org-log-repeat' is non-nil or entry contains
12554 ;; a clock, set LAST_REPEAT property.
12555 (when (or org-log-repeat
12558 (while (re-search-forward org-clock-line-re end t
)
12559 (when (org-at-clock-log-p) (throw :clock t
))))))
12560 (org-entry-put nil
"LAST_REPEAT" (format-time-string
12561 (org-time-stamp-format t t
)
12563 (when org-log-repeat
12564 (if (or (memq 'org-add-log-note
(default-value 'post-command-hook
))
12565 (memq 'org-add-log-note post-command-hook
))
12566 ;; We are already setup for some record.
12567 (when (eq org-log-repeat
'note
)
12568 ;; Make sure we take a note, not only a time stamp.
12569 (setq org-log-note-how
'note
))
12570 ;; Set up for taking a record.
12571 (org-add-log-setup 'state
12572 (or done-word
(car org-done-keywords
))
12575 (let ((planning-re (regexp-opt
12576 (list org-scheduled-string org-deadline-string
))))
12577 (while (re-search-forward org-ts-regexp end t
)
12578 (let* ((ts (match-string 0))
12579 (planning?
(org-at-planning-p))
12580 (type (if (not planning?
) "Plain:"
12582 (re-search-backward
12583 planning-re
(line-beginning-position) t
)
12584 (match-string 0)))))
12586 ;; Ignore fake time-stamps (e.g., within comments).
12587 ((not (org-at-timestamp-p 'agenda
)))
12588 ;; Time-stamps without a repeater are usually
12589 ;; skipped. However, a SCHEDULED time-stamp without
12590 ;; one is removed, as they are no longer relevant.
12591 ((not (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
12593 (when (equal type org-scheduled-string
)
12594 (org-remove-timestamp-with-keyword type
)))
12596 (let ((n (string-to-number (match-string 2 ts
)))
12597 (what (match-string 3 ts
)))
12598 (when (equal what
"w") (setq n
(* n
7) what
"d"))
12599 (when (and (equal what
"h")
12600 (not (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}"
12603 "Cannot repeat in Repeat in %d hour(s) because no hour \
12606 ;; Preparation, see if we need to modify the start
12607 ;; date for the change.
12608 (when (match-end 1)
12609 (let ((time (save-match-data
12610 (org-time-string-to-time ts
))))
12612 ((equal (match-string 1 ts
) ".")
12613 ;; Shift starting date to today
12614 (org-timestamp-change
12615 (- (org-today) (time-to-days time
))
12617 ((equal (match-string 1 ts
) "+")
12618 (let ((nshiftmax 10)
12620 (while (or (= nshift
0)
12621 (not (time-less-p (current-time) time
)))
12622 (when (= (cl-incf nshift
) nshiftmax
)
12624 (format "%d repeater intervals were not \
12625 enough to shift date past today. Continue? "
12627 (user-error "Abort")))
12628 (org-timestamp-change n
(cdr (assoc what whata
)))
12629 (org-in-regexp org-ts-regexp3
)
12630 (setq ts
(match-string 1))
12633 (org-time-string-to-time ts
)))))
12634 (org-timestamp-change (- n
) (cdr (assoc what whata
)))
12635 ;; Rematch, so that we have everything in place
12636 ;; for the real shift.
12637 (org-in-regexp org-ts-regexp3
)
12638 (setq ts
(match-string 1))
12639 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
12642 (org-timestamp-change n
(cdr (assoc what whata
)) nil t
))
12645 msg type
" " org-last-changed-timestamp
" "))))))))
12646 (setq org-log-post-message msg
)
12647 (message "%s" msg
))
12648 (set-marker end nil
))))
12650 (defun org-show-todo-tree (arg)
12651 "Make a compact tree which shows all headlines marked with TODO.
12652 The tree will show the lines where the regexp matches, and all higher
12653 headlines above the match.
12654 With a `\\[universal-argument]' prefix, prompt for a regexp to match.
12655 With a numeric prefix N, construct a sparse tree for the Nth element
12656 of `org-todo-keywords-1'."
12658 (let ((case-fold-search nil
)
12660 (cond ((null arg
) org-not-done-regexp
)
12663 (completing-read "Keyword (or KWD1|KWD2|...): "
12664 (mapcar #'list org-todo-keywords-1
))))
12666 (mapconcat 'identity
(org-split-string kwd
"|") "\\|")
12668 ((<= (prefix-numeric-value arg
) (length org-todo-keywords-1
))
12669 (regexp-quote (nth (1- (prefix-numeric-value arg
))
12670 org-todo-keywords-1
)))
12671 (t (user-error "Invalid prefix argument: %s" arg
)))))
12672 (message "%d TODO entries found"
12673 (org-occur (concat "^" org-outline-regexp
" *" kwd-re
)))))
12675 (defun org--deadline-or-schedule (arg type time
)
12676 "Insert DEADLINE or SCHEDULE information in current entry.
12677 TYPE is either `deadline' or `scheduled'. See `org-deadline' or
12678 `org-schedule' for information about ARG and TIME arguments."
12679 (let* ((deadline?
(eq type
'deadline
))
12680 (keyword (if deadline? org-deadline-string org-scheduled-string
))
12681 (log (if deadline? org-log-redeadline org-log-reschedule
))
12682 (old-date (org-entry-get nil
(if deadline?
"DEADLINE" "SCHEDULED")))
12683 (old-date-time (and old-date
(org-time-string-to-time old-date
)))
12684 ;; Save repeater cookie from either TIME or current scheduled
12685 ;; time stamp. We are going to insert it back at the end of
12687 (repeater (or (and (org-string-nw-p time
)
12688 ;; We use `org-repeat-re' because we need
12689 ;; to tell the difference between a real
12690 ;; repeater and a time delta, e.g. "+2d".
12691 (string-match org-repeat-re time
)
12692 (match-string 1 time
))
12693 (and (org-string-nw-p old-date
)
12694 (string-match "\\([.+-]+[0-9]+[hdwmy]\
12695 \\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\)"
12697 (match-string 1 old-date
)))))
12700 (when (and old-date log
)
12701 (org-add-log-setup (if deadline?
'deldeadline
'delschedule
)
12703 (org-remove-timestamp-with-keyword keyword
)
12704 (message (if deadline?
"Item no longer has a deadline."
12705 "Item is no longer scheduled.")))
12708 (org-back-to-heading t
)
12709 (let ((regexp (if deadline? org-deadline-time-regexp
12710 org-scheduled-time-regexp
)))
12711 (if (not (re-search-forward regexp
(line-end-position 2) t
))
12712 (user-error (if deadline?
"No deadline information to update"
12713 "No scheduled information to update"))
12714 (let* ((rpl0 (match-string 1))
12715 (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0
))
12716 (msg (if deadline?
"Warn starting from" "Delay until")))
12721 (abs (- (time-to-days
12724 nil t nil msg old-date-time
)))
12725 (time-to-days old-date-time
))))
12728 (org-add-planning-info type time
'closed
)
12729 (when (and old-date
12731 (not (equal old-date org-last-inserted-timestamp
)))
12732 (org-add-log-setup (if deadline?
'redeadline
'reschedule
)
12733 org-last-inserted-timestamp
12738 (org-back-to-heading t
)
12739 (when (re-search-forward
12740 (concat keyword
" " org-last-inserted-timestamp
)
12741 (line-end-position 2)
12743 (goto-char (1- (match-end 0)))
12744 (insert " " repeater
)
12745 (setq org-last-inserted-timestamp
12746 (concat (substring org-last-inserted-timestamp
0 -
1)
12748 (substring org-last-inserted-timestamp -
1))))))
12749 (message (if deadline?
"Deadline on %s" "Scheduled to %s")
12750 org-last-inserted-timestamp
)))))
12752 (defun org-deadline (arg &optional time
)
12753 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12754 With one universal prefix argument, remove any deadline from the item.
12755 With two universal prefix arguments, prompt for a warning delay.
12756 With argument TIME, set the deadline at the corresponding date. TIME
12757 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12759 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
12761 (lambda () (org--deadline-or-schedule arg
'deadline time
))
12763 (if (eq org-loop-over-headlines-in-active-region
'start-level
)
12764 'region-start-level
12766 (lambda () (when (org-invisible-p) (org-end-of-subtree nil t
))))
12767 (org--deadline-or-schedule arg
'deadline time
)))
12769 (defun org-schedule (arg &optional time
)
12770 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12771 With one universal prefix argument, remove any scheduling date from the item.
12772 With two universal prefix arguments, prompt for a delay cookie.
12773 With argument TIME, scheduled at the corresponding date. TIME can
12774 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12776 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
12778 (lambda () (org--deadline-or-schedule arg
'scheduled time
))
12780 (if (eq org-loop-over-headlines-in-active-region
'start-level
)
12781 'region-start-level
12783 (lambda () (when (org-invisible-p) (org-end-of-subtree nil t
))))
12784 (org--deadline-or-schedule arg
'scheduled time
)))
12786 (defun org-get-scheduled-time (pom &optional inherit
)
12787 "Get the scheduled time as a time tuple, of a format suitable
12788 for calling org-schedule with, or if there is no scheduling,
12790 (let ((time (org-entry-get pom
"SCHEDULED" inherit
)))
12792 (apply 'encode-time
(org-parse-time-string time
)))))
12794 (defun org-get-deadline-time (pom &optional inherit
)
12795 "Get the deadline as a time tuple, of a format suitable for
12796 calling org-deadline with, or if there is no scheduling, returns
12798 (let ((time (org-entry-get pom
"DEADLINE" inherit
)))
12800 (apply 'encode-time
(org-parse-time-string time
)))))
12802 (defun org-remove-timestamp-with-keyword (keyword)
12803 "Remove all time stamps with KEYWORD in the current entry."
12804 (let ((re (concat "\\<" (regexp-quote keyword
) " +<[^>\n]+>[ \t]*"))
12807 (org-back-to-heading t
)
12809 (outline-next-heading)
12810 (while (re-search-backward re beg t
)
12812 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12813 (equal (char-before) ?\
))
12814 (backward-delete-char 1)
12815 (when (string-match "^[ \t]*$" (buffer-substring
12816 (point-at-bol) (point-at-eol)))
12817 (delete-region (point-at-bol)
12818 (min (point-max) (1+ (point-at-eol))))))))))
12820 (defvar org-time-was-given
) ; dynamically scoped parameter
12821 (defvar org-end-time-was-given
) ; dynamically scoped parameter
12823 (defun org-at-planning-p ()
12824 "Non-nil when point is on a planning info line."
12825 ;; This is as accurate and faster than `org-element-at-point' since
12826 ;; planning info location is fixed in the section.
12827 (org-with-wide-buffer
12828 (beginning-of-line)
12829 (and (looking-at-p org-planning-line-re
)
12832 (if (and (featurep 'org-inlinetask
) (org-inlinetask-in-task-p))
12833 (org-back-to-heading t
)
12834 (org-with-limited-levels (org-back-to-heading t
)))
12835 (line-beginning-position 2))))))
12837 (defun org-add-planning-info (what &optional time
&rest remove
)
12838 "Insert new timestamp with keyword in the planning line.
12839 WHAT indicates what kind of time stamp to add. It is a symbol
12840 among `closed', `deadline', `scheduled' and nil. TIME indicates
12841 the time to use. If none is given, the user is prompted for
12842 a date. REMOVE indicates what kind of entries to remove. An old
12843 WHAT entry will also be removed."
12844 (let (org-time-was-given org-end-time-was-given default-time default-input
)
12846 (when (and (memq what
'(scheduled deadline
))
12848 (and (stringp time
)
12849 (string-match "^[-+]+[0-9]" time
))))
12850 ;; Try to get a default date/time from existing timestamp
12852 (org-back-to-heading t
)
12853 (let ((end (save-excursion (outline-next-heading) (point))) ts
)
12854 (when (re-search-forward (if (eq what
'scheduled
)
12855 org-scheduled-time-regexp
12856 org-deadline-time-regexp
)
12858 (setq ts
(match-string 1)
12859 default-time
(apply 'encode-time
(org-parse-time-string ts
))
12860 default-input
(and ts
(org-get-compact-tod ts
)))))))
12864 ;; This is a string (relative or absolute), set
12866 (apply #'encode-time
12867 (org-read-date-analyze
12868 time default-time
(decode-time default-time
)))
12869 ;; If necessary, get the time from the user
12870 (or time
(org-read-date nil
'to-time nil nil
12871 default-time default-input
)))))
12873 (org-with-wide-buffer
12874 (org-back-to-heading t
)
12876 (unless (bolp) (insert "\n"))
12877 (cond ((looking-at-p org-planning-line-re
)
12878 ;; Move to current indentation.
12879 (skip-chars-forward " \t")
12880 ;; Check if we have to remove something.
12881 (dolist (type (if what
(cons what remove
) remove
))
12883 (when (re-search-forward
12885 (closed org-closed-time-regexp
)
12886 (deadline org-deadline-time-regexp
)
12887 (scheduled org-scheduled-time-regexp
)
12889 (error "Invalid planning type: %s" type
)))
12890 (line-end-position) t
)
12891 ;; Delete until next keyword or end of line.
12893 (match-beginning 0)
12894 (if (re-search-forward org-keyword-time-not-clock-regexp
12895 (line-end-position)
12897 (match-beginning 0)
12898 (line-end-position))))))
12899 ;; If there is nothing more to add and no more keyword
12900 ;; is left, remove the line completely.
12901 (if (and (looking-at-p "[ \t]*$") (not what
))
12902 (delete-region (line-beginning-position)
12903 (line-beginning-position 2))
12904 ;; If we removed last keyword, do not leave trailing
12905 ;; white space at the end of line.
12909 (unless (= (skip-chars-backward " \t" p
) 0)
12910 (delete-region (point) (line-end-position)))))))
12911 ((not what
) (throw 'exit nil
)) ; Nothing to do.
12912 (t (insert-before-markers "\n")
12914 (when org-adapt-indentation
12915 (indent-to-column (1+ (org-outline-level))))))
12917 ;; Insert planning keyword.
12918 (insert (cl-case what
12919 (closed org-closed-string
)
12920 (deadline org-deadline-string
)
12921 (scheduled org-scheduled-string
)
12922 (otherwise (error "Invalid planning type: %s" what
)))
12924 ;; Insert associated timestamp.
12925 (let ((ts (org-insert-time-stamp
12927 (or org-time-was-given
12928 (and (eq what
'closed
) org-log-done-with-time
))
12930 nil nil
(list org-end-time-was-given
))))
12931 (unless (eolp) (insert " "))
12934 (defvar org-log-note-marker
(make-marker)
12935 "Marker pointing at the entry where the note is to be inserted.")
12936 (defvar org-log-note-purpose nil
)
12937 (defvar org-log-note-state nil
)
12938 (defvar org-log-note-previous-state nil
)
12939 (defvar org-log-note-extra nil
)
12940 (defvar org-log-note-window-configuration nil
)
12941 (defvar org-log-note-return-to
(make-marker))
12942 (defvar org-log-note-effective-time nil
12943 "Remembered current time so that dynamically scoped
12944 `org-extend-today-until' affects timestamps in state change log")
12946 (defvar org-log-post-message nil
12947 "Message to be displayed after a log note has been stored.
12948 The auto-repeater uses this.")
12950 (defun org-add-note ()
12951 "Add a note to the current entry.
12952 This is done in the same way as adding a state change note."
12954 (org-add-log-setup 'note
))
12956 (defun org-log-beginning (&optional create
)
12957 "Return expected start of log notes in current entry.
12958 When optional argument CREATE is non-nil, the function creates
12959 a drawer to store notes, if necessary. Returned position ignores
12961 (org-with-wide-buffer
12962 (let ((drawer (org-log-into-drawer)))
12965 (org-end-of-meta-data)
12966 (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer
) ":[ \t]*$"))
12967 (end (if (org-at-heading-p) (point)
12968 (save-excursion (outline-next-heading) (point))))
12969 (case-fold-search t
))
12971 ;; Try to find existing drawer.
12972 (while (re-search-forward regexp end t
)
12973 (let ((element (org-element-at-point)))
12974 (when (eq (org-element-type element
) 'drawer
)
12975 (let ((cend (org-element-property :contents-end element
)))
12976 (when (and (not org-log-states-order-reversed
) cend
)
12978 (throw 'exit nil
))))
12979 ;; No drawer found. Create one, if permitted.
12981 (unless (bolp) (insert "\n"))
12982 (let ((beg (point)))
12983 (insert ":" drawer
":\n:END:\n")
12984 (org-indent-region beg
(point))
12986 (line-end-position -
1) (1- (point)) t
'org-hide-drawer
))
12987 (end-of-line -
1)))))
12989 (org-end-of-meta-data org-log-state-notes-insert-after-drawers
)
12990 (skip-chars-forward " \t\n")
12991 (beginning-of-line)
12992 (unless org-log-states-order-reversed
12993 (org-skip-over-state-notes)
12994 (skip-chars-backward " \t\n")
12996 (if (bolp) (point) (line-beginning-position 2))))
12998 (defun org-add-log-setup (&optional purpose state prev-state how extra
)
12999 "Set up the post command hook to take a note.
13000 If this is about to TODO state change, the new state is expected in STATE.
13001 HOW is an indicator what kind of note should be created.
13002 EXTRA is additional text that will be inserted into the notes buffer."
13003 (move-marker org-log-note-marker
(point))
13004 (setq org-log-note-purpose purpose
13005 org-log-note-state state
13006 org-log-note-previous-state prev-state
13007 org-log-note-how how
13008 org-log-note-extra extra
13009 org-log-note-effective-time
(org-current-effective-time))
13010 (add-hook 'post-command-hook
'org-add-log-note
'append
))
13012 (defun org-skip-over-state-notes ()
13013 "Skip past the list of State notes in an entry."
13014 (when (ignore-errors (goto-char (org-in-item-p)))
13015 (let* ((struct (org-list-struct))
13016 (prevs (org-list-prevs-alist struct
))
13018 (concat "[ \t]*- +"
13019 (replace-regexp-in-string
13021 (org-replace-escapes
13022 (regexp-quote (cdr (assq 'state org-log-note-headings
)))
13023 `(("%d" .
,org-ts-regexp-inactive
)
13024 ("%D" .
,org-ts-regexp
)
13025 ("%s" .
"\"\\S-+\"")
13026 ("%S" .
"\"\\S-+\"")
13027 ("%t" .
,org-ts-regexp-inactive
)
13028 ("%T" .
,org-ts-regexp
)
13030 ("%U" .
".*?")))))))
13031 (while (looking-at-p regexp
)
13032 (goto-char (or (org-list-get-next-item (point) struct prevs
)
13033 (org-list-get-item-end (point) struct
)))))))
13035 (defun org-add-log-note (&optional _purpose
)
13036 "Pop up a window for taking a note, and add this note later."
13037 (remove-hook 'post-command-hook
'org-add-log-note
)
13038 (setq org-log-note-window-configuration
(current-window-configuration))
13039 (delete-other-windows)
13040 (move-marker org-log-note-return-to
(point))
13041 (pop-to-buffer-same-window (marker-buffer org-log-note-marker
))
13042 (goto-char org-log-note-marker
)
13043 (org-switch-to-buffer-other-window "*Org Note*")
13045 (if (memq org-log-note-how
'(time state
))
13046 (org-store-log-note)
13047 (let ((org-inhibit-startup t
)) (org-mode))
13048 (insert (format "# Insert note for %s.
13049 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
13051 ((eq org-log-note-purpose
'clock-out
) "stopped clock")
13052 ((eq org-log-note-purpose
'done
) "closed todo item")
13053 ((eq org-log-note-purpose
'state
)
13054 (format "state change from \"%s\" to \"%s\""
13055 (or org-log-note-previous-state
"")
13056 (or org-log-note-state
"")))
13057 ((eq org-log-note-purpose
'reschedule
)
13059 ((eq org-log-note-purpose
'delschedule
)
13060 "no longer scheduled")
13061 ((eq org-log-note-purpose
'redeadline
)
13062 "changing deadline")
13063 ((eq org-log-note-purpose
'deldeadline
)
13064 "removing deadline")
13065 ((eq org-log-note-purpose
'refile
)
13067 ((eq org-log-note-purpose
'note
)
13069 (t (error "This should not happen")))))
13070 (when org-log-note-extra
(insert org-log-note-extra
))
13071 (setq-local org-finish-function
'org-store-log-note
)
13072 (run-hooks 'org-log-buffer-setup-hook
)))
13074 (defvar org-note-abort nil
) ; dynamically scoped
13075 (defun org-store-log-note ()
13076 "Finish taking a log note, and insert it to where it belongs."
13077 (let ((txt (prog1 (buffer-string)
13079 (note (cdr (assq org-log-note-purpose org-log-note-headings
)))
13081 (while (string-match "\\`# .*\n[ \t\n]*" txt
)
13082 (setq txt
(replace-match "" t t txt
)))
13083 (when (string-match "\\s-+\\'" txt
)
13084 (setq txt
(replace-match "" t t txt
)))
13085 (setq lines
(and (not (equal "" txt
)) (org-split-string txt
"\n")))
13086 (when (org-string-nw-p note
)
13088 (org-replace-escapes
13090 (list (cons "%u" (user-login-name))
13091 (cons "%U" user-full-name
)
13092 (cons "%t" (format-time-string
13093 (org-time-stamp-format 'long
'inactive
)
13094 org-log-note-effective-time
))
13095 (cons "%T" (format-time-string
13096 (org-time-stamp-format 'long nil
)
13097 org-log-note-effective-time
))
13098 (cons "%d" (format-time-string
13099 (org-time-stamp-format nil
'inactive
)
13100 org-log-note-effective-time
))
13101 (cons "%D" (format-time-string
13102 (org-time-stamp-format nil nil
)
13103 org-log-note-effective-time
))
13105 ((not org-log-note-state
) "")
13106 ((string-match-p org-ts-regexp
13107 org-log-note-state
)
13109 (substring org-log-note-state
1 -
1)))
13110 (t (format "\"%s\"" org-log-note-state
))))
13113 ((not org-log-note-previous-state
) "")
13114 ((string-match-p org-ts-regexp
13115 org-log-note-previous-state
)
13118 org-log-note-previous-state
1 -
1)))
13119 (t (format "\"%s\""
13120 org-log-note-previous-state
)))))))
13121 (when lines
(setq note
(concat note
" \\\\")))
13123 (when (and lines
(not org-note-abort
))
13124 (with-current-buffer (marker-buffer org-log-note-marker
)
13125 (org-with-wide-buffer
13126 ;; Find location for the new note.
13127 (goto-char org-log-note-marker
)
13128 (set-marker org-log-note-marker nil
)
13129 ;; Note associated to a clock is to be located right after
13130 ;; the clock. Do not move point.
13131 (unless (eq org-log-note-purpose
'clock-out
)
13132 (goto-char (org-log-beginning t
)))
13133 ;; Make sure point is at the beginning of an empty line.
13134 (cond ((not (bolp)) (let ((inhibit-read-only t
)) (insert "\n")))
13135 ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
13136 ;; In an existing list, add a new item at the top level.
13137 ;; Otherwise, indent line like a regular one.
13138 (let ((itemp (org-in-item-p)))
13141 (let ((struct (save-excursion
13142 (goto-char itemp
) (org-list-struct))))
13143 (org-list-get-ind (org-list-get-top-point struct
) struct
)))
13144 (org-indent-line)))
13145 (insert (org-list-bullet-string "-") (pop lines
))
13146 (let ((ind (org-list-item-body-column (line-beginning-position))))
13147 (dolist (line lines
)
13149 (indent-line-to ind
)
13151 (message "Note stored")
13152 (org-back-to-heading t
))
13153 ;; Fix `buffer-undo-list' when `org-store-log-note' is called
13154 ;; from within `org-add-log-note' because `buffer-undo-list'
13155 ;; is then modified outside of `org-with-remote-undo'.
13156 (when (eq this-command
'org-agenda-todo
)
13157 (setcdr buffer-undo-list
(cddr buffer-undo-list
))))))
13158 ;; Don't add undo information when called from `org-agenda-todo'.
13159 (let ((buffer-undo-list (eq this-command
'org-agenda-todo
)))
13160 (set-window-configuration org-log-note-window-configuration
)
13161 (with-current-buffer (marker-buffer org-log-note-return-to
)
13162 (goto-char org-log-note-return-to
))
13163 (move-marker org-log-note-return-to nil
)
13164 (when org-log-post-message
(message "%s" org-log-post-message
))))
13166 (defun org-remove-empty-drawer-at (pos)
13167 "Remove an empty drawer at position POS.
13168 POS may also be a marker."
13169 (with-current-buffer (if (markerp pos
) (marker-buffer pos
) (current-buffer))
13170 (org-with-wide-buffer
13172 (let ((drawer (org-element-at-point)))
13173 (when (and (memq (org-element-type drawer
) '(drawer property-drawer
))
13174 (not (org-element-property :contents-begin drawer
)))
13175 (delete-region (org-element-property :begin drawer
)
13176 (progn (goto-char (org-element-property :end drawer
))
13177 (skip-chars-backward " \r\t\n")
13181 (defvar org-ts-type nil
)
13182 (defun org-sparse-tree (&optional arg type
)
13183 "Create a sparse tree, prompt for the details.
13184 This command can create sparse trees. You first need to select the type
13185 of match used to create the tree:
13187 t Show all TODO entries.
13188 T Show entries with a specific TODO keyword.
13189 m Show entries selected by a tags/property match.
13190 p Enter a property name and its value (both with completion on existing
13191 names/values) and show entries with that property.
13192 r Show entries matching a regular expression (`/' can be used as well).
13193 b Show deadlines and scheduled items before a date.
13194 a Show deadlines and scheduled items after a date.
13195 d Show deadlines due within `org-deadline-warning-days'.
13196 D Show deadlines and scheduled items between a date range."
13198 (setq type
(or type org-sparse-tree-default-date-type
))
13199 (setq org-ts-type type
)
13200 (message "Sparse tree: [r]egexp [t]odo [T]odo-kwd [m]atch [p]roperty
13201 \[d]eadlines [b]efore-date [a]fter-date [D]ates range
13202 \[c]ycle through date types: %s"
13204 (all "all timestamps")
13205 (scheduled "only scheduled")
13206 (deadline "only deadline")
13207 (active "only active timestamps")
13208 (inactive "only inactive timestamps")
13209 (closed "with a closed time-stamp")
13210 (otherwise "scheduled/deadline")))
13211 (let ((answer (read-char-exclusive)))
13217 (memq type
'(nil all scheduled deadline active inactive closed
)))))
13218 (?d
(call-interactively 'org-check-deadlines
))
13219 (?b
(call-interactively 'org-check-before-date
))
13220 (?a
(call-interactively 'org-check-after-date
))
13221 (?D
(call-interactively 'org-check-dates-range
))
13222 (?t
(call-interactively 'org-show-todo-tree
))
13223 (?T
(org-show-todo-tree '(4)))
13224 (?m
(call-interactively 'org-match-sparse-tree
))
13226 (let* ((kwd (completing-read
13227 "Property: " (mapcar #'list
(org-buffer-property-keys))))
13228 (value (completing-read
13229 "Value: " (mapcar #'list
(org-property-values kwd
)))))
13230 (unless (string-match "\\`{.*}\\'" value
)
13231 (setq value
(concat "\"" value
"\"")))
13232 (org-match-sparse-tree arg
(concat kwd
"=" value
))))
13233 ((?r ?R ?
/) (call-interactively 'org-occur
))
13234 (otherwise (user-error "No such sparse tree command \"%c\"" answer
)))))
13236 (defvar-local org-occur-highlights nil
13237 "List of overlays used for occur matches.")
13238 (defvar-local org-occur-parameters nil
13239 "Parameters of the active org-occur calls.
13240 This is a list, each call to org-occur pushes as cons cell,
13241 containing the regular expression and the callback, onto the list.
13242 The list can contain several entries if `org-occur' has been called
13243 several time with the KEEP-PREVIOUS argument. Otherwise, this list
13244 will only contain one set of parameters. When the highlights are
13245 removed (for example with `C-c C-c', or with the next edit (depending
13246 on `org-remove-highlights-with-change'), this variable is emptied
13249 (defun org-occur (regexp &optional keep-previous callback
)
13250 "Make a compact tree which shows all matches of REGEXP.
13252 The tree will show the lines where the regexp matches, and any other context
13253 defined in `org-show-context-detail', which see.
13255 When optional argument KEEP-PREVIOUS is non-nil, highlighting and exposing
13256 done by a previous call to `org-occur' will be kept, to allow stacking of
13257 calls to this command.
13259 Optional argument CALLBACK can be a function of no argument. In this case,
13260 it is called with point at the end of the match, match data being set
13261 accordingly. Current match is shown only if the return value is non-nil.
13262 The function must neither move point nor alter narrowing."
13263 (interactive "sRegexp: \nP")
13264 (when (equal regexp
"")
13265 (user-error "Regexp cannot be empty"))
13266 (unless keep-previous
13267 (org-remove-occur-highlights nil nil t
))
13268 (push (cons regexp callback
) org-occur-parameters
)
13271 (goto-char (point-min))
13272 (when (or (not keep-previous
) ; do not want to keep
13273 (not org-occur-highlights
)) ; no previous matches
13276 (let ((case-fold-search (if (eq org-occur-case-fold-search
'smart
)
13277 (isearch-no-upper-case-p regexp t
)
13278 org-occur-case-fold-search
)))
13279 (while (re-search-forward regexp nil t
)
13280 (when (or (not callback
)
13281 (save-match-data (funcall callback
)))
13282 (setq cnt
(1+ cnt
))
13283 (when org-highlight-sparse-tree-matches
13284 (org-highlight-new-match (match-beginning 0) (match-end 0)))
13285 (org-show-context 'occur-tree
)))))
13286 (when org-remove-highlights-with-change
13287 (add-hook 'before-change-functions
'org-remove-occur-highlights
13289 (unless org-sparse-tree-open-archived-trees
13290 (org-hide-archived-subtrees (point-min) (point-max)))
13291 (run-hooks 'org-occur-hook
)
13292 (when (called-interactively-p 'interactive
)
13293 (message "%d match(es) for regexp %s" cnt regexp
))
13296 (defun org-occur-next-match (&optional n _reset
)
13297 "Function for `next-error-function' to find sparse tree matches.
13298 N is the number of matches to move, when negative move backwards.
13299 This function always goes back to the starting point when no
13301 (let* ((limit (if (< n
0) (point-min) (point-max)))
13302 (search-func (if (< n
0)
13303 'previous-single-char-property-change
13304 'next-single-char-property-change
))
13309 (while (setq p1
(funcall search-func
(point) 'org-type
))
13310 (when (equal p1 limit
)
13312 (user-error "No more matches"))
13313 (when (equal (get-char-property p1
'org-type
) 'org-occur
)
13317 (throw 'exit
(point))))
13320 (user-error "No more matches"))))
13322 (defun org-highlight-new-match (beg end
)
13323 "Highlight from BEG to END and mark the highlight is an occur headline."
13324 (let ((ov (make-overlay beg end
)))
13325 (overlay-put ov
'face
'secondary-selection
)
13326 (overlay-put ov
'org-type
'org-occur
)
13327 (push ov org-occur-highlights
)))
13329 (defun org-remove-occur-highlights (&optional _beg _end noremove
)
13330 "Remove the occur highlights from the buffer.
13331 BEG and END are ignored. If NOREMOVE is nil, remove this function
13332 from the `before-change-functions' in the current buffer."
13334 (unless org-inhibit-highlight-removal
13335 (mapc #'delete-overlay org-occur-highlights
)
13336 (setq org-occur-highlights nil
)
13337 (setq org-occur-parameters nil
)
13339 (remove-hook 'before-change-functions
13340 'org-remove-occur-highlights
'local
))))
13344 (defvar org-priority-regexp
".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
13345 "Regular expression matching the priority indicator.")
13347 (defvar org-remove-priority-next-time nil
)
13349 (defun org-priority-up ()
13350 "Increase the priority of the current item."
13352 (org-priority 'up
))
13354 (defun org-priority-down ()
13355 "Decrease the priority of the current item."
13357 (org-priority 'down
))
13359 (defun org-priority (&optional action _show
)
13360 "Change the priority of an item.
13361 ACTION can be `set', `up', `down', or a character."
13363 (if (equal action
'(4))
13364 (org-show-priority)
13365 (unless org-enable-priority-commands
13366 (user-error "Priority commands are disabled"))
13367 (setq action
(or action
'set
))
13368 (let (current new news have remove
)
13370 (org-back-to-heading t
)
13371 (when (looking-at org-priority-regexp
)
13372 (setq current
(string-to-char (match-string 2))
13375 ((eq action
'remove
)
13376 (setq remove t new ?\
))
13377 ((or (eq action
'set
)
13379 (if (not (eq action
'set
))
13381 (message "Priority %c-%c, SPC to remove: "
13382 org-highest-priority org-lowest-priority
)
13384 (setq new
(read-char-exclusive))))
13385 (when (and (= (upcase org-highest-priority
) org-highest-priority
)
13386 (= (upcase org-lowest-priority
) org-lowest-priority
))
13387 (setq new
(upcase new
)))
13388 (cond ((equal new ?\
) (setq remove t
))
13389 ((or (< (upcase new
) org-highest-priority
) (> (upcase new
) org-lowest-priority
))
13390 (user-error "Priority must be between `%c' and `%c'"
13391 org-highest-priority org-lowest-priority
))))
13394 (1- current
) ; normal cycling
13395 ;; last priority was empty
13396 (if (eq last-command this-command
)
13397 org-lowest-priority
; wrap around empty to lowest
13399 (if org-priority-start-cycle-with-default
13400 org-default-priority
13401 (1- org-default-priority
))))))
13404 (1+ current
) ; normal cycling
13405 ;; last priority was empty
13406 (if (eq last-command this-command
)
13407 org-highest-priority
; wrap around empty to highest
13409 (if org-priority-start-cycle-with-default
13410 org-default-priority
13411 (1+ org-default-priority
))))))
13412 (t (user-error "Invalid action")))
13413 (when (or (< (upcase new
) org-highest-priority
)
13414 (> (upcase new
) org-lowest-priority
))
13415 (if (and (memq action
'(up down
))
13416 (not have
) (not (eq last-command this-command
)))
13417 ;; `new' is from default priority
13419 "The default can not be set, see `org-default-priority' why")
13420 ;; normal cycling: `new' is beyond highest/lowest priority
13421 ;; and is wrapped around to the empty priority
13423 (setq news
(format "%c" new
))
13426 (replace-match "" t t nil
1)
13427 (replace-match news t t nil
2))
13429 (user-error "No priority cookie found in line")
13430 (let ((case-fold-search nil
)) (looking-at org-todo-line-regexp
))
13433 (goto-char (match-end 2))
13434 (insert " [#" news
"]"))
13435 (goto-char (match-beginning 3))
13436 (insert "[#" news
"] "))))
13437 (org-set-tags nil
'align
))
13439 (message "Priority removed")
13440 (message "Priority of current item set to %s" news
)))))
13442 (defun org-show-priority ()
13443 "Show the priority of the current item.
13444 This priority is composed of the main priority given with the [#A] cookies,
13445 and by additional input from the age of a schedules or deadline entry."
13447 (let ((pri (if (eq major-mode
'org-agenda-mode
)
13448 (org-get-at-bol 'priority
)
13451 (beginning-of-line)
13452 (and (looking-at org-heading-regexp
)
13453 (org-get-priority (match-string 0))))))))
13454 (message "Priority is %d" (if pri pri -
1000))))
13456 (defun org-get-priority (s)
13457 "Find priority cookie and return priority."
13459 (if (functionp org-get-priority-function
)
13460 (funcall org-get-priority-function
)
13461 (if (not (string-match org-priority-regexp s
))
13462 (* 1000 (- org-lowest-priority org-default-priority
))
13463 (* 1000 (- org-lowest-priority
13464 (string-to-char (match-string 2 s
))))))))
13468 (defvar org-agenda-archives-mode
)
13469 (defvar org-map-continue-from nil
13470 "Position from where mapping should continue.
13471 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
13473 (defvar org-scanner-tags nil
13474 "The current tag list while the tags scanner is running.")
13476 (defvar org-trust-scanner-tags nil
13477 "Should `org-get-tags-at' use the tags for the scanner.
13478 This is for internal dynamical scoping only.
13479 When this is non-nil, the function `org-get-tags-at' will return the value
13480 of `org-scanner-tags' instead of building the list by itself. This
13481 can lead to large speed-ups when the tags scanner is used in a file with
13482 many entries, and when the list of tags is retrieved, for example to
13483 obtain a list of properties. Building the tags list for each entry in such
13484 a file becomes an N^2 operation - but with this variable set, it scales
13487 (defvar org--matcher-tags-todo-only nil
)
13489 (defun org-scan-tags (action matcher todo-only
&optional start-level
)
13490 "Scan headline tags with inheritance and produce output ACTION.
13492 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
13493 or `agenda' to produce an entry list for an agenda view. It can also be
13494 a Lisp form or a function that should be called at each matched headline, in
13495 this case the return value is a list of all return values from these calls.
13497 MATCHER is a function accepting three arguments, returning
13498 a non-nil value whenever a given set of tags qualifies a headline
13499 for inclusion. See `org-make-tags-matcher' for more information.
13500 As a special case, it can also be set to t (respectively nil) in
13501 order to match all (respectively none) headline.
13503 When TODO-ONLY is non-nil, only lines with a TODO keyword are
13504 included in the output.
13506 START-LEVEL can be a string with asterisks, reducing the scope to
13507 headlines matching this string."
13508 (require 'org-agenda
)
13509 (let* ((re (concat "^"
13511 ;; Get the correct level to match
13512 (concat "\\*\\{" (number-to-string start-level
) "\\} ")
13513 org-outline-regexp
)
13515 (mapconcat #'regexp-quote org-todo-keywords-1
"\\|")
13516 "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$"))
13517 (props (list 'face
'default
13518 'done-face
'org-agenda-done
13519 'undone-face
'default
13520 'mouse-face
'highlight
13521 'org-not-done-regexp org-not-done-regexp
13522 'org-todo-regexp org-todo-regexp
13523 'org-complex-heading-regexp org-complex-heading-regexp
13525 (format "mouse-2 or RET jump to Org file %S"
13526 (abbreviate-file-name
13527 (or (buffer-file-name (buffer-base-buffer))
13528 (buffer-name (buffer-base-buffer)))))))
13529 (org-map-continue-from nil
)
13530 lspos tags tags-list
13531 (tags-alist (list (cons 0 org-file-tags
)))
13532 (llast 0) rtn rtn1 level category i txt
13533 todo marker entry priority
13534 ts-date ts-date-type ts-date-pair
)
13535 (unless (or (member action
'(agenda sparse-tree
)) (functionp action
))
13536 (setq action
(list 'lambda nil action
)))
13538 (goto-char (point-min))
13539 (when (eq action
'sparse-tree
)
13541 (org-remove-occur-highlights))
13542 (while (let (case-fold-search)
13543 (re-search-forward re nil t
))
13544 (setq org-map-continue-from nil
)
13547 ;; TODO: is the 1-2 difference a bug?
13548 (when (match-end 1) (match-string-no-properties 2))
13549 tags
(when (match-end 4) (match-string-no-properties 4)))
13550 (goto-char (setq lspos
(match-beginning 0)))
13551 (setq level
(org-reduced-level (org-outline-level))
13552 category
(org-get-category))
13553 (when (eq action
'agenda
)
13554 (setq ts-date-pair
(org-agenda-entry-get-agenda-timestamp (point))
13555 ts-date
(car ts-date-pair
)
13556 ts-date-type
(cdr ts-date-pair
)))
13557 (setq i llast llast level
)
13558 ;; remove tag lists from same and sublevels
13559 (while (>= i level
)
13560 (when (setq entry
(assoc i tags-alist
))
13561 (setq tags-alist
(delete entry tags-alist
)))
13563 ;; add the next tags
13565 (setq tags
(org-split-string tags
":")
13567 (cons (cons level tags
) tags-alist
)))
13568 ;; compile tags for current headline
13570 (if org-use-tag-inheritance
13571 (apply 'append
(mapcar 'cdr
(reverse tags-alist
)))
13573 org-scanner-tags tags-list
)
13574 (when org-use-tag-inheritance
13575 (setcdr (car tags-alist
)
13576 (mapcar (lambda (x)
13577 (setq x
(copy-sequence x
))
13578 (org-add-prop-inherited x
))
13579 (cdar tags-alist
))))
13580 (when (and tags org-use-tag-inheritance
13581 (or (not (eq t org-use-tag-inheritance
))
13582 org-tags-exclude-from-inheritance
))
13583 ;; Selective inheritance, remove uninherited ones.
13584 (setcdr (car tags-alist
)
13585 (org-remove-uninherited-tags (cdar tags-alist
))))
13588 ;; eval matcher only when the todo condition is OK
13589 (and (or (not todo-only
) (member todo org-todo-keywords-1
))
13590 (if (functionp matcher
)
13591 (let ((case-fold-search t
) (org-trust-scanner-tags t
))
13592 (funcall matcher todo tags-list level
))
13595 ;; Call the skipper, but return t if it does not
13596 ;; skip, so that the `and' form continues evaluating.
13598 (unless (eq action
'sparse-tree
) (org-agenda-skip))
13601 ;; Check if timestamps are deselecting this entry
13602 (or (not todo-only
)
13603 (and (member todo org-todo-keywords-1
)
13604 (or (not org-agenda-tags-todo-honor-ignore-options
)
13605 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
13607 ;; select this headline
13609 ((eq action
'sparse-tree
)
13610 (and org-highlight-sparse-tree-matches
13611 (org-get-heading) (match-end 0)
13612 (org-highlight-new-match
13613 (match-beginning 1) (match-end 1)))
13614 (org-show-context 'tags-tree
))
13615 ((eq action
'agenda
)
13616 (setq txt
(org-agenda-format-item
13619 (if (eq org-tags-match-list-sublevels
'indented
)
13620 (make-string (1- level
) ?.
) "")
13622 (make-string level ?\s
)
13625 priority
(org-get-priority txt
))
13627 (setq marker
(org-agenda-new-marker))
13628 (org-add-props txt props
13629 'org-marker marker
'org-hd-marker marker
'org-category category
13633 'type
(concat "tagsmatch" ts-date-type
))
13635 ((functionp action
)
13636 (setq org-map-continue-from nil
)
13638 (setq rtn1
(funcall action
))
13640 (t (user-error "Invalid action")))
13642 ;; if we are to skip sublevels, jump to end of subtree
13643 (unless org-tags-match-list-sublevels
13644 (org-end-of-subtree t
)
13645 (backward-char 1))))
13646 ;; Get the correct position from where to continue
13647 (if org-map-continue-from
13648 (goto-char org-map-continue-from
)
13649 (and (= (point) lspos
) (end-of-line 1)))))
13650 (when (and (eq action
'sparse-tree
)
13651 (not org-sparse-tree-open-archived-trees
))
13652 (org-hide-archived-subtrees (point-min) (point-max)))
13655 (defun org-remove-uninherited-tags (tags)
13656 "Remove all tags that are not inherited from the list TAGS."
13658 ((eq org-use-tag-inheritance t
)
13659 (if org-tags-exclude-from-inheritance
13660 (org-delete-all org-tags-exclude-from-inheritance tags
)
13662 ((not org-use-tag-inheritance
) nil
)
13663 ((stringp org-use-tag-inheritance
)
13666 (if (and (string-match org-use-tag-inheritance x
)
13667 (not (member x org-tags-exclude-from-inheritance
)))
13670 ((listp org-use-tag-inheritance
)
13673 (if (member x org-use-tag-inheritance
) x nil
))
13676 (defun org-match-sparse-tree (&optional todo-only match
)
13677 "Create a sparse tree according to tags string MATCH.
13679 MATCH is a string with match syntax. It can contain a selection
13680 of tags (\"+work+urgent-boss\"), properties (\"LEVEL>3\"), and
13681 TODO keywords (\"TODO=\\\"WAITING\\\"\") or a combination of
13682 those. See the manual for details.
13684 If optional argument TODO-ONLY is non-nil, only select lines that
13685 are also TODO tasks."
13687 (org-agenda-prepare-buffers (list (current-buffer)))
13688 (let ((org--matcher-tags-todo-only todo-only
))
13689 (org-scan-tags 'sparse-tree
(cdr (org-make-tags-matcher match
))
13690 org--matcher-tags-todo-only
)))
13692 (defalias 'org-tags-sparse-tree
'org-match-sparse-tree
)
13694 (defvar org-cached-props nil
)
13695 (defun org-cached-entry-get (pom property
)
13696 (if (or (eq t org-use-property-inheritance
)
13697 (and (stringp org-use-property-inheritance
)
13698 (let ((case-fold-search t
))
13699 (string-match-p org-use-property-inheritance property
)))
13700 (and (listp org-use-property-inheritance
)
13701 (member-ignore-case property org-use-property-inheritance
)))
13702 ;; Caching is not possible, check it directly.
13703 (org-entry-get pom property
'inherit
)
13704 ;; Get all properties, so we can do complicated checks easily.
13705 (cdr (assoc-string property
13706 (or org-cached-props
13707 (setq org-cached-props
(org-entry-properties pom
)))
13710 (defun org-global-tags-completion-table (&optional files
)
13711 "Return the list of all tags in all agenda buffer/files.
13712 Optional FILES argument is a list of files which can be used
13713 instead of the agenda files."
13720 (set-buffer (find-file-noselect file
))
13721 (org-tag-add-to-alist
13722 (org-get-buffer-tags)
13723 (mapcar (lambda (x)
13724 (and (stringp (car-safe x
))
13725 (list (car-safe x
))))
13726 org-current-tag-alist
)))
13727 (if (car-safe files
) files
13728 (org-agenda-files))))))))
13730 (defun org-make-tags-matcher (match)
13731 "Create the TAGS/TODO matcher form for the selection string MATCH.
13733 Returns a cons of the selection string MATCH and a function
13734 implementing the matcher.
13736 The matcher is to be called at an Org entry, with point on the
13737 headline, and returns non-nil if the entry matches the selection
13738 string MATCH. It must be called with three arguments: the TODO
13739 keyword at the entry (or nil if none), the list of all tags at
13740 the entry including inherited ones and the reduced level of the
13741 headline. Additionally, the category of the entry, if any, must
13742 be specified as the text property `org-category' on the headline.
13744 This function sets the variable `org--matcher-tags-todo-only' to
13745 a non-nil value if the matcher restricts matching to TODO
13746 entries, otherwise it is not touched.
13748 See also `org-scan-tags'."
13750 ;; Get a new match request, with completion against the global
13751 ;; tags table and the local tags in current buffer.
13752 (let ((org-last-tags-completion-table
13753 (org-tag-add-to-alist
13754 (org-get-buffer-tags)
13755 (org-global-tags-completion-table))))
13759 'org-tags-completion-function nil nil nil
'org-tags-history
))))
13761 (let ((match0 match
)
13762 (re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)")
13764 tagsmatch todomatch tagsmatcher todomatcher
)
13766 ;; Expand group tags.
13767 (setq match
(org-tags-expand match
))
13769 ;; Check if there is a TODO part of this match, which would be the
13770 ;; part after a "/". To make sure that this slash is not part of
13771 ;; a property value to be matched against, we also check that
13772 ;; there is no / after that slash. First, find the last slash.
13774 (while (string-match "/+" match s
)
13775 (setq start
(match-beginning 0))
13776 (setq s
(match-end 0))))
13777 (if (and (string-match "/+" match start
)
13778 (not (string-match-p "\"" match start
)))
13779 ;; Match contains also a TODO-matching request.
13781 (setq tagsmatch
(substring match
0 (match-beginning 0)))
13782 (setq todomatch
(substring match
(match-end 0)))
13783 (when (string-prefix-p "!" todomatch
)
13784 (setq org--matcher-tags-todo-only t
)
13785 (setq todomatch
(substring todomatch
1)))
13786 (when (string-match "\\`\\s-*\\'" todomatch
)
13787 (setq todomatch nil
)))
13788 ;; Only matching tags.
13789 (setq tagsmatch match
)
13790 (setq todomatch nil
))
13792 ;; Make the tags matcher.
13793 (when (org-string-nw-p tagsmatch
)
13795 (orterms (org-split-string tagsmatch
"|"))
13797 (while (setq term
(pop orterms
))
13798 (while (and (equal (substring term -
1) "\\") orterms
)
13799 (setq term
(concat term
"|" (pop orterms
)))) ;repair bad split.
13800 (while (string-match re term
)
13801 (let* ((rest (substring term
(match-end 0)))
13802 (minus (and (match-end 1)
13803 (equal (match-string 1 term
) "-")))
13804 (tag (save-match-data
13805 (replace-regexp-in-string
13806 "\\\\-" "-" (match-string 2 term
))))
13807 (regexp (eq (string-to-char tag
) ?
{))
13808 (levelp (match-end 4))
13809 (propp (match-end 5))
13812 (regexp `(org-match-any-p ,(substring tag
1 -
1) tags-list
))
13814 `(,(org-op-to-function (match-string 3 term
))
13816 ,(string-to-number (match-string 4 term
))))
13818 (let* ((gv (pcase (upcase (match-string 5 term
))
13820 '(get-text-property (point) 'org-category
))
13822 (p `(org-cached-entry-get nil
,p
))))
13823 (pv (match-string 7 term
))
13824 (regexp (eq (string-to-char pv
) ?
{))
13825 (strp (eq (string-to-char pv
) ?
\"))
13826 (timep (string-match-p "^\"[[<].*[]>]\"$" pv
))
13827 (po (org-op-to-function (match-string 6 term
)
13828 (if timep
'time strp
))))
13829 (setq pv
(if (or regexp strp
) (substring pv
1 -
1) pv
))
13830 (when timep
(setq pv
(org-matcher-time pv
)))
13831 (cond ((and regexp
(eq po
'org
<>))
13832 `(not (string-match ,pv
(or ,gv
""))))
13833 (regexp `(string-match ,pv
(or ,gv
"")))
13834 (strp `(,po
(or ,gv
"") ,pv
))
13837 (string-to-number (or ,gv
""))
13838 ,(string-to-number pv
))))))
13839 (t `(member ,tag tags-list
)))))
13840 (push (if minus
`(not ,mm
) mm
) tagsmatcher
)
13842 (push `(and ,@tagsmatcher
) orlist
)
13843 (setq tagsmatcher nil
))
13844 (setq tagsmatcher
`(progn (setq org-cached-props nil
) (or ,@orlist
)))))
13846 ;; Make the TODO matcher.
13847 (when (org-string-nw-p todomatch
)
13848 (let ((orlist nil
))
13849 (dolist (term (org-split-string todomatch
"|"))
13850 (while (string-match re term
)
13851 (let* ((minus (and (match-end 1)
13852 (equal (match-string 1 term
) "-")))
13853 (kwd (match-string 2 term
))
13854 (regexp (eq (string-to-char kwd
) ?
{))
13855 (mm (if regexp
`(string-match ,(substring kwd
1 -
1) todo
)
13856 `(equal todo
,kwd
))))
13857 (push (if minus
`(not ,mm
) mm
) todomatcher
))
13858 (setq term
(substring term
(match-end 0))))
13859 (push (if (> (length todomatcher
) 1)
13860 (cons 'and todomatcher
)
13863 (setq todomatcher nil
))
13864 (setq todomatcher
(cons 'or orlist
))))
13866 ;; Return the string and function of the matcher. If no
13867 ;; tags-specific or todo-specific matcher exists, match
13869 (let ((matcher (if (and tagsmatcher todomatcher
)
13870 `(and ,tagsmatcher
,todomatcher
)
13871 (or tagsmatcher todomatcher t
))))
13872 (when org--matcher-tags-todo-only
13873 (setq matcher
`(and (member todo org-not-done-keywords
) ,matcher
)))
13874 (cons match0
`(lambda (todo tags-list level
) ,matcher
)))))
13876 (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded
)
13877 "Expand group tags in MATCH.
13879 This replaces every group tag in MATCH with a regexp tag search.
13880 For example, a group tag \"Work\" defined as { Work : Lab Conf }
13881 will be replaced like this:
13883 Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
13884 +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
13885 -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
13887 Replacing by a regexp preserves the structure of the match.
13888 E.g., this expansion
13890 Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
13892 will match anything tagged with \"Lab\" and \"Home\", or tagged
13893 with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
13895 A group tag in MATCH can contain regular expressions of its own.
13896 For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
13897 will be replaced like this:
13899 Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
13901 When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
13902 assumed to be a single group tag, and the function will return
13903 the list of tags in this group.
13905 When DOWNCASE is non-nil, expand downcased TAGS."
13907 (let* ((case-fold-search t
)
13908 (stable org-mode-syntax-table
)
13909 (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist
))
13910 (taggroups (if downcased
13911 (mapcar (lambda (tg) (mapcar #'downcase tg
))
13914 (taggroups-keys (mapcar #'car taggroups
))
13915 (return-match (if downcased
(downcase match
) match
))
13917 (work-already-expanded tags-already-expanded
)
13918 regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped
)
13919 ;; @ and _ are allowed as word-components in tags.
13920 (modify-syntax-entry ?
@ "w" stable
)
13921 (modify-syntax-entry ?_
"w" stable
)
13922 ;; Temporarily replace regexp-expressions in the match-expression.
13923 (while (string-match "{.+?}" return-match
)
13925 (push (match-string 0 return-match
) regexps-in-match
)
13926 (setq return-match
(replace-match (format "<%d>" count
) t nil return-match
)))
13927 (while (and taggroups-keys
13928 (with-syntax-table stable
13930 (concat "\\(?1:[+-]?\\)\\(?2:\\<"
13931 (regexp-opt taggroups-keys
) "\\>\\)")
13933 (let* ((dir (match-string 1 return-match
))
13934 (tag (match-string 2 return-match
))
13935 (tag (if downcased
(downcase tag
) tag
)))
13936 (unless (or (get-text-property 0 'grouptag
(match-string 2 return-match
))
13937 (member tag tags-already-expanded
))
13938 (setq tags-in-group
(assoc tag taggroups
))
13939 (push tag work-already-expanded
)
13940 ;; Recursively expand each tag in the group, if the tag hasn't
13941 ;; already been expanded. Restore the match-data after all recursive calls.
13943 (let (tags-expanded)
13944 (dolist (x (cdr tags-in-group
))
13945 (if (and (member x taggroups-keys
)
13946 (not (member x work-already-expanded
)))
13947 (setq tags-expanded
13950 (org-tags-expand x t downcased
13951 work-already-expanded
)
13953 (setq tags-expanded
13954 (append (list x
) tags-expanded
)))
13955 (setq work-already-expanded
13957 (append tags-expanded
13958 work-already-expanded
))))
13959 (setq tags-in-group
13960 (delete-dups (cons (car tags-in-group
)
13962 ;; Filter tag-regexps from tags.
13963 (setq regexp-in-group-escaped
13964 (delq nil
(mapcar (lambda (x)
13966 (and (equal "{" (substring x
0 1))
13967 (equal "}" (substring x -
1))
13972 (mapcar (lambda (x)
13973 (substring x
1 -
1))
13974 regexp-in-group-escaped
)
13976 (delq nil
(mapcar (lambda (x)
13978 (and (not (equal "{" (substring x
0 1)))
13979 (not (equal "}" (substring x -
1)))
13983 ;; If single-as-list, do no more in the while-loop.
13984 (if (not single-as-list
)
13986 (when regexp-in-group
13987 (setq regexp-in-group
13989 (mapconcat 'identity regexp-in-group
13991 (setq tags-in-group
13994 (regexp-opt tags-in-group
)
13998 (when (stringp tags-in-group
)
13999 (org-add-props tags-in-group
'(grouptag t
)))
14001 (replace-match tags-in-group t t return-match
)))
14002 (setq tags-in-group
14003 (append regexp-in-group-escaped tags-in-group
))))
14004 (setq taggroups-keys
(delete tag taggroups-keys
))))
14005 ;; Add the regular expressions back into the match-expression again.
14006 (while regexps-in-match
14007 (setq return-match
(replace-regexp-in-string (format "<%d>" count
)
14008 (pop regexps-in-match
)
14012 (if tags-in-group tags-in-group
(list return-match
))
14015 (list (if downcased
(downcase match
) match
))
14018 (defun org-op-to-function (op &optional stringp
)
14019 "Turn an operator into the appropriate function."
14022 ((equal op
"<" ) '(< org-string
< org-time
<))
14023 ((equal op
">" ) '(> org-string
> org-time
>))
14024 ((member op
'("<=" "=<")) '(<= org-string
<= org-time
<=))
14025 ((member op
'(">=" "=>")) '(>= org-string
>= org-time
>=))
14026 ((member op
'("=" "==")) '(= string
= org-time
=))
14027 ((member op
'("<>" "!=")) '(org<> org-string
<> org-time
<>))))
14028 (nth (if (eq stringp
'time
) 2 (if stringp
1 0)) op
))
14030 (defun org<> (a b
) (not (= a b
)))
14031 (defun org-time= (a b
) (setq a
(org-2ft a
) b
(org-2ft b
)) (and (> a
0) (> b
0) (= a b
)))
14032 (defun org-time< (a b
) (setq a
(org-2ft a
) b
(org-2ft b
)) (and (> a
0) (> b
0) (< a b
)))
14033 (defun org-time<= (a b
) (setq a
(org-2ft a
) b
(org-2ft b
)) (and (> a
0) (> b
0) (<= a b
)))
14034 (defun org-time> (a b
) (setq a
(org-2ft a
) b
(org-2ft b
)) (and (> a
0) (> b
0) (> a b
)))
14035 (defun org-time>= (a b
) (setq a
(org-2ft a
) b
(org-2ft b
)) (and (> a
0) (> b
0) (>= a b
)))
14036 (defun org-time<> (a b
) (setq a
(org-2ft a
) b
(org-2ft b
)) (and (> a
0) (> b
0) (org<> a b
)))
14038 "Convert S to a floating point time.
14039 If S is already a number, just return it. If it is a string, parse
14040 it as a time string and apply `float-time' to it. If S is nil, just return 0."
14044 (condition-case nil
14045 (float-time (apply #'encode-time
(org-parse-time-string s
)))
14049 (defun org-time-today ()
14050 "Time in seconds today at 0:00.
14051 Returns the float number of seconds since the beginning of the
14052 epoch to the beginning of today (00:00)."
14053 (float-time (apply 'encode-time
14054 (append '(0 0 0) (nthcdr 3 (decode-time))))))
14056 (defun org-matcher-time (s)
14057 "Interpret a time comparison value."
14060 ((string= s
"<now>") (float-time))
14061 ((string= s
"<today>") (org-time-today))
14062 ((string= s
"<tomorrow>") (+ 86400.0 (org-time-today)))
14063 ((string= s
"<yesterday>") (- (org-time-today) 86400.0))
14064 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s
)
14065 (+ (org-time-today)
14066 (* (string-to-number (match-string 1 s
))
14067 (cdr (assoc (match-string 2 s
)
14068 '(("d" .
86400.0) ("w" .
604800.0)
14069 ("m" .
2678400.0) ("y" .
31557600.0)))))))
14072 (defvar org-add-colon-after-tag-completion nil
) ;; dynamically scoped param
14073 (defvar org-tags-overlay
(make-overlay 1 1))
14074 (delete-overlay org-tags-overlay
)
14076 (defun org-get-local-tags-at (&optional pos
)
14077 "Get a list of tags defined in the current headline."
14078 (org-get-tags-at pos
'local
))
14080 (defun org-get-local-tags ()
14081 "Get a list of tags defined in the current headline."
14082 (org-get-tags-at nil
'local
))
14084 (defun org-get-tags-at (&optional pos local
)
14085 "Get a list of all headline tags applicable at POS.
14086 POS defaults to point. If tags are inherited, the list contains
14087 the targets in the same sequence as the headlines appear, i.e.
14088 the tags of the current headline come last.
14089 When LOCAL is non-nil, only return tags from the current headline,
14090 ignore inherited ones."
14092 (if (and org-trust-scanner-tags
14093 (or (not pos
) (equal pos
(point)))
14096 (let (tags ltags lastpos parent
)
14100 (goto-char (or pos
(point)))
14103 (condition-case nil
14105 (org-back-to-heading t
)
14106 (while (not (equal lastpos
(point)))
14107 (setq lastpos
(point))
14108 (when (looking-at ".+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
14109 (setq ltags
(org-split-string
14110 (match-string-no-properties 1) ":"))
14112 (setq ltags
(mapcar 'org-add-prop-inherited ltags
)))
14115 (org-remove-uninherited-tags ltags
)
14118 (or org-use-tag-inheritance
(throw 'done t
))
14119 (when local
(throw 'done t
))
14120 (or (org-up-heading-safe) (error nil
))
14125 (reverse (delete-dups
14127 (org-remove-uninherited-tags
14131 (defun org-add-prop-inherited (s)
14132 (add-text-properties 0 (length s
) '(inherited t
) s
)
14135 (defun org-toggle-tag (tag &optional onoff
)
14136 "Toggle the tag TAG for the current line.
14137 If ONOFF is `on' or `off', don't toggle but set to this state."
14139 (org-back-to-heading t
)
14141 (when (re-search-forward "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$"
14142 (line-end-position) t
)
14143 (let ((tags (match-string 1)))
14144 ;; Clear current tags.
14146 ;; Reverse the tags list so any new tag is appended to
14147 ;; the current list of tags.
14148 (nreverse (org-split-string tags
":")))))
14151 (`off
(setq current
(delete tag current
)))
14152 ((or `on
(guard (not (member tag current
))))
14154 (cl-pushnew tag current
:test
#'equal
))
14155 (_ (setq current
(delete tag current
))))
14159 (insert " :" (mapconcat #'identity
(nreverse current
) ":") ":")
14160 (org-set-tags nil t
))
14161 (delete-horizontal-space))
14162 (run-hooks 'org-after-tags-change-hook
)
14165 (defun org--align-tags-here (to-col)
14166 "Align tags on the current headline to TO-COL.
14167 Assume point is on a headline."
14168 (let ((pos (point)))
14169 (beginning-of-line)
14170 (if (or (not (looking-at ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14171 (>= pos
(match-beginning 2)))
14172 ;; No tags or point within tags: do not align.
14174 (goto-char (match-beginning 1))
14175 (let ((shift (max (- (if (>= to-col
0) to-col
14176 (- (abs to-col
) (string-width (match-string 2))))
14179 (replace-match (make-string shift ?\s
) nil nil nil
1)
14180 ;; Preserve initial position, if possible. In any case, stop
14182 (when (< pos
(point)) (goto-char pos
))))))
14184 (defun org-set-tags-command (&optional arg just-align
)
14185 "Call the set-tags command for the current entry."
14187 (if (or (org-at-heading-p) (and arg
(org-before-first-heading-p)))
14188 (org-set-tags arg just-align
)
14190 (unless (and (org-region-active-p)
14191 org-loop-over-headlines-in-active-region
)
14192 (org-back-to-heading t
))
14193 (org-set-tags arg just-align
))))
14195 (defun org-set-tags-to (data)
14196 "Set the tags of the current entry to DATA, replacing current tags.
14197 DATA may be a tags string like \":aa:bb:cc:\", or a list of tags.
14198 If DATA is nil or the empty string, all tags are removed."
14199 (interactive "sTags: ")
14201 (pcase (if (stringp data
) (org-trim data
) data
)
14203 ((pred listp
) (format ":%s:" (mapconcat #'identity data
":")))
14206 (mapconcat #'identity
(org-split-string data
":+") ":")))
14207 (_ (error "Invalid tag specification: %S" data
)))))
14208 (org-with-wide-buffer
14209 (org-back-to-heading t
)
14210 (let ((case-fold-search nil
)) (looking-at org-complex-heading-regexp
))
14211 (when (or (match-end 5) data
)
14212 (goto-char (or (match-beginning 5) (line-end-position)))
14213 (skip-chars-backward " \t")
14214 (delete-region (point) (line-end-position))
14217 (org-set-tags nil
'align
))))))
14219 (defun org-align-all-tags ()
14220 "Align the tags in all headings."
14223 (or (ignore-errors (org-back-to-heading t
))
14224 (outline-next-heading))
14225 (if (org-at-heading-p)
14227 (message "No headings"))))
14229 (defvar org-indent-indentation-per-level
)
14230 (defun org-set-tags (&optional arg just-align
)
14231 "Set the tags for the current headline.
14232 With prefix ARG, realign all tags in headings in the current buffer.
14233 When JUST-ALIGN is non-nil, only align tags."
14235 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
14236 (let ((cl (if (eq org-loop-over-headlines-in-active-region
'start-level
)
14237 'region-start-level
14239 org-loop-over-headlines-in-active-region
)
14241 ;; We don't use ARG and JUST-ALIGN here because these args
14242 ;; are not useful when looping over headlines.
14244 org-loop-over-headlines-in-active-region
14246 '(when (org-invisible-p) (org-end-of-subtree nil t
))))
14247 (let ((org-setting-tags t
))
14250 (goto-char (point-min))
14251 (while (re-search-forward org-outline-regexp-bol nil t
)
14252 (org-set-tags nil t
)
14254 (message "All tags realigned to column %d" org-tags-column
))
14255 (let* ((current (org-get-tags-string))
14257 (if just-align current
14258 ;; Get a new set of tags from the user.
14262 org-last-tags-completion-table
14263 (org-tag-add-to-alist
14265 org-complete-tags-always-offer-all-agenda-tags
14266 (org-global-tags-completion-table
14267 (org-agenda-files)))
14268 (or org-current-tag-alist
14269 (org-get-buffer-tags)))))
14270 (current-tags (org-split-string current
":"))
14272 (nreverse (nthcdr (length current-tags
)
14273 (nreverse (org-get-tags-at))))))
14274 (replace-regexp-in-string
14277 (if (or (eq t org-use-fast-tag-selection
)
14278 (and org-use-fast-tag-selection
14279 (delq nil
(mapcar #'cdr table
))))
14280 (org-fast-tag-selection
14281 current-tags inherited-tags table
14282 (and org-fast-tag-selection-include-todo
14283 org-todo-key-alist
))
14284 (let ((org-add-colon-after-tag-completion
14285 (< 1 (length table
))))
14289 #'org-tags-completion-function
14290 nil nil current
'org-tags-history
))))))))))
14292 (when org-tags-sort-function
14296 (sort (org-split-string tags
"[^[:alnum:]_@#%]+")
14297 org-tags-sort-function
)
14300 (if (or (string= ":" tags
)
14301 (string= "::" tags
))
14303 (if (not (org-string-nw-p tags
)) (setq tags
"")
14304 (unless (string-suffix-p ":" tags
) (setq tags
(concat tags
":")))
14305 (unless (string-prefix-p ":" tags
) (setq tags
(concat ":" tags
))))
14307 ;; Insert new tags at the correct column.
14308 (unless (equal current tags
)
14310 (beginning-of-line)
14311 (let ((case-fold-search nil
))
14312 (looking-at org-complex-heading-regexp
))
14313 ;; Remove current tags, if any.
14314 (when (match-end 5) (replace-match "" nil nil nil
5))
14315 ;; Insert new tags, if any. Otherwise, remove trailing
14318 (if (not (equal tags
""))
14319 ;; When text is being inserted on an invisible
14320 ;; region boundary, it can be inadvertently sucked
14321 ;; into invisibility.
14322 (org-flag-region (point) (progn (insert " " tags
) (point))
14325 (skip-chars-backward " \t")
14326 (delete-region (point) (line-end-position)))))
14327 ;; Align tags, if any. Fix tags column if `org-indent-mode'
14329 (unless (equal tags
"")
14330 (let* ((level (save-excursion
14331 (beginning-of-line)
14332 (skip-chars-forward "\\*")))
14333 (offset (if (bound-and-true-p org-indent-mode
)
14334 (* (1- org-indent-indentation-per-level
)
14339 (if (> org-tags-column
0) (- offset
) offset
))))
14340 (org--align-tags-here tags-column
))))
14341 (unless just-align
(run-hooks 'org-after-tags-change-hook
))))))
14343 (defun org-change-tag-in-region (beg end tag off
)
14344 "Add or remove TAG for each entry in the region.
14345 This works in the agenda, and also in an Org buffer."
14347 (list (region-beginning) (region-end)
14348 (let ((org-last-tags-completion-table
14349 (if (derived-mode-p 'org-mode
)
14350 (org-tag-add-to-alist
14351 (org-get-buffer-tags)
14352 (org-global-tags-completion-table))
14353 (org-global-tags-completion-table))))
14355 "Tag: " 'org-tags-completion-function nil nil nil
14356 'org-tags-history
))
14358 (message "[s]et or [r]emove? ")
14359 (equal (read-char-exclusive) ?r
))))
14360 (when (fboundp 'deactivate-mark
) (deactivate-mark))
14361 (let ((agendap (equal major-mode
'org-agenda-mode
))
14362 l1 l2 m buf pos newhead
(cnt 0))
14364 (setq l2
(1- (org-current-line)))
14366 (setq l1
(org-current-line))
14367 (cl-loop for l from l1 to l2 do
14369 (setq m
(get-text-property (point) 'org-hd-marker
))
14370 (when (or (and (derived-mode-p 'org-mode
) (org-at-heading-p))
14372 (setq buf
(if agendap
(marker-buffer m
) (current-buffer))
14373 pos
(if agendap m
(point)))
14374 (with-current-buffer buf
14378 (setq cnt
(1+ cnt
))
14379 (org-toggle-tag tag
(if off
'off
'on
))
14380 (setq newhead
(org-get-heading)))))
14381 (and agendap
(org-agenda-change-all-lines newhead m
))))
14382 (message "Tag :%s: %s in %d headings" tag
(if off
"removed" "set") cnt
)))
14384 (defun org-tags-completion-function (string _predicate
&optional flag
)
14385 (let (s1 s2 rtn
(ctable org-last-tags-completion-table
)
14386 (confirm (lambda (x) (stringp (car x
)))))
14387 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string
)
14388 (setq s1
(match-string 1 string
)
14389 s2
(match-string 2 string
))
14390 (setq s1
"" s2 string
))
14394 (setq rtn
(try-completion s2 ctable confirm
))
14395 (when (stringp rtn
)
14397 (concat s1 s2
(substring rtn
(length s2
))
14398 (if (and org-add-colon-after-tag-completion
14399 (assoc rtn ctable
))
14404 (all-completions s2 ctable confirm
))
14407 (assoc s2 ctable
)))))
14409 (defun org-fast-tag-insert (kwd tags face
&optional end
)
14410 "Insert KDW, and the TAGS, the latter with face FACE.
14412 (insert (format "%-12s" (concat kwd
":"))
14413 (org-add-props (mapconcat 'identity tags
" ") nil
'face face
)
14416 (defun org-fast-tag-show-exit (flag)
14419 (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t
)
14420 (replace-match ""))
14423 (org-move-to-column (- (window-width) 19) t
)
14424 (insert (org-add-props " Next change exits" nil
'face
'org-warning
)))))
14426 (defun org-set-current-tags-overlay (current prefix
)
14427 "Add an overlay to CURRENT tag with PREFIX."
14428 (let ((s (concat ":" (mapconcat 'identity current
":") ":")))
14429 (put-text-property 0 (length s
) 'face
'(secondary-selection org-tag
) s
)
14430 (org-overlay-display org-tags-overlay
(concat prefix s
))))
14432 (defvar org-last-tag-selection-key nil
)
14433 (defun org-fast-tag-selection (current inherited table
&optional todo-table
)
14434 "Fast tag selection with single keys.
14435 CURRENT is the current list of tags in the headline, INHERITED is the
14436 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
14437 possibly with grouping information. TODO-TABLE is a similar table with
14438 TODO keywords, should these have keys assigned to them.
14439 If the keys are nil, a-z are automatically assigned.
14440 Returns the new tags string, or nil to not change the current settings."
14441 (let* ((fulltable (append table todo-table
))
14442 (maxlen (apply 'max
(mapcar
14444 (if (stringp (car x
)) (string-width (car x
)) 0))
14446 (buf (current-buffer))
14447 (expert (eq org-fast-tag-selection-single-key
'expert
))
14449 (fwidth (+ maxlen
3 1 3))
14450 (ncol (/ (- (window-width) 4) fwidth
))
14453 tg cnt e c char c1 c2 ntable tbl rtn
14454 ov-start ov-end ov-prefix
14455 (exit-after-next org-fast-tag-selection-single-key
)
14456 (done-keywords org-done-keywords
)
14457 groups ingroup intaggroup
)
14459 (beginning-of-line 1)
14460 (if (looking-at ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
14461 (setq ov-start
(match-beginning 1)
14462 ov-end
(match-end 1)
14464 (setq ov-start
(1- (point-at-eol))
14465 ov-end
(1+ ov-start
))
14466 (skip-chars-forward "^\n\r")
14469 (buffer-substring (1- (point)) (point))
14470 (if (> (current-column) org-tags-column
)
14472 (make-string (- org-tags-column
(current-column)) ?\
))))))
14473 (move-overlay org-tags-overlay ov-start ov-end
)
14474 (save-window-excursion
14476 (set-buffer (get-buffer-create " *Org tags*"))
14477 (delete-other-windows)
14478 (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
14479 (org-switch-to-buffer-other-window " *Org tags*"))
14481 (setq-local org-done-keywords done-keywords
)
14482 (org-fast-tag-insert "Inherited" inherited i-face
"\n")
14483 (org-fast-tag-insert "Current" current c-face
"\n\n")
14484 (org-fast-tag-show-exit exit-after-next
)
14485 (org-set-current-tags-overlay current ov-prefix
)
14486 (setq tbl fulltable char ?a cnt
0)
14487 (while (setq e
(pop tbl
))
14489 ((eq (car e
) :startgroup
)
14490 (push '() groups
) (setq ingroup t
)
14491 (unless (zerop cnt
)
14494 (insert (if (cdr e
) (format "%s: " (cdr e
)) "") "{ "))
14495 ((eq (car e
) :endgroup
)
14496 (setq ingroup nil cnt
0)
14497 (insert "}" (if (cdr e
) (format " (%s) " (cdr e
)) "") "\n"))
14498 ((eq (car e
) :startgrouptag
)
14499 (setq intaggroup t
)
14500 (unless (zerop cnt
)
14504 ((eq (car e
) :endgrouptag
)
14505 (setq intaggroup nil cnt
0)
14507 ((equal e
'(:newline
))
14508 (unless (zerop cnt
)
14512 (while (equal (car tbl
) '(:newline
))
14514 (setq tbl
(cdr tbl
)))))
14515 ((equal e
'(:grouptags
)) (insert " : "))
14517 (setq tg
(copy-sequence (car e
)) c2 nil
)
14520 ;; automatically assign a character.
14521 (setq c1
(string-to-char
14522 (downcase (substring
14523 tg
(if (= (string-to-char tg
) ?
@) 1 0)))))
14524 (if (or (rassoc c1 ntable
) (rassoc c1 table
))
14525 (while (or (rassoc char ntable
) (rassoc char table
))
14526 (setq char
(1+ char
)))
14528 (setq c
(or c2 char
)))
14529 (when ingroup
(push tg
(car groups
)))
14530 (setq tg
(org-add-props tg nil
'face
14532 ((not (assoc tg table
))
14533 (org-get-todo-face tg
))
14534 ((member tg current
) c-face
)
14535 ((member tg inherited
) i-face
))))
14536 (when (equal (caar tbl
) :grouptags
)
14537 (org-add-props tg nil
'face
'org-tag-group
))
14538 (when (and (zerop cnt
) (not ingroup
) (not intaggroup
)) (insert " "))
14539 (insert "[" c
"] " tg
(make-string
14540 (- fwidth
4 (length tg
)) ?\
))
14541 (push (cons tg c
) ntable
)
14542 (when (= (cl-incf cnt
) ncol
)
14544 (when (or ingroup intaggroup
) (insert " "))
14546 (setq ntable
(nreverse ntable
))
14548 (goto-char (point-min))
14549 (unless expert
(org-fit-window-to-buffer))
14553 (message "[a-z..]:toggle [SPC]:clear [RET]:accept [TAB]:edit [!] %sgroups%s"
14554 (if (not groups
) "no " "")
14555 (if expert
" [C-c]:window" (if exit-after-next
" [C-c]:single" " [C-c]:multi")))
14556 (setq c
(let ((inhibit-quit t
)) (read-char-exclusive)))
14557 (setq org-last-tag-selection-key c
)
14559 ((= c ?
\r) (throw 'exit t
))
14561 (setq groups
(not groups
))
14562 (goto-char (point-min))
14563 (while (re-search-forward "[{}]" nil t
) (replace-match " ")))
14566 (org-fast-tag-show-exit
14567 (setq exit-after-next
(not exit-after-next
)))
14569 (delete-other-windows)
14570 (set-window-buffer (split-window-vertically) " *Org tags*")
14571 (org-switch-to-buffer-other-window " *Org tags*")
14572 (org-fit-window-to-buffer)))
14574 (and (= c ?q
) (not (rassoc c ntable
))))
14575 (delete-overlay org-tags-overlay
)
14576 (setq quit-flag t
))
14579 (when exit-after-next
(setq exit-after-next
'now
)))
14581 (condition-case nil
14582 (setq tg
(completing-read
14585 (with-current-buffer buf
14587 (org-get-buffer-tags))))))
14588 (quit (setq tg
"")))
14589 (when (string-match "\\S-" tg
)
14590 (cl-pushnew (list tg
) buffer-tags
:test
#'equal
)
14591 (if (member tg current
)
14592 (setq current
(delete tg current
))
14593 (push tg current
)))
14594 (when exit-after-next
(setq exit-after-next
'now
)))
14595 ((setq e
(rassoc c todo-table
) tg
(car e
))
14596 (with-current-buffer buf
14597 (save-excursion (org-todo tg
)))
14598 (when exit-after-next
(setq exit-after-next
'now
)))
14599 ((setq e
(rassoc c ntable
) tg
(car e
))
14600 (if (member tg current
)
14601 (setq current
(delete tg current
))
14602 (cl-loop for g in groups do
14603 (when (member tg g
)
14604 (dolist (x g
) (setq current
(delete x current
)))))
14606 (when exit-after-next
(setq exit-after-next
'now
))))
14608 ;; Create a sorted list
14612 (assoc b
(cdr (memq (assoc a ntable
) ntable
))))))
14613 (when (eq exit-after-next
'now
) (throw 'exit t
))
14614 (goto-char (point-min))
14615 (beginning-of-line 2)
14616 (delete-region (point) (point-at-eol))
14617 (org-fast-tag-insert "Current" current c-face
)
14618 (org-set-current-tags-overlay current ov-prefix
)
14619 (while (re-search-forward "\\[.\\] \\([[:alnum:]_@#%]+\\)" nil t
)
14620 (setq tg
(match-string 1))
14621 (add-text-properties
14622 (match-beginning 1) (match-end 1)
14625 ((member tg current
) c-face
)
14626 ((member tg inherited
) i-face
)
14627 (t (get-text-property (match-beginning 1) 'face
))))))
14628 (goto-char (point-min)))))
14629 (delete-overlay org-tags-overlay
)
14631 (mapconcat 'identity current
":")
14634 (defun org-get-tags-string ()
14635 "Get the TAGS string in the current headline."
14636 (unless (org-at-heading-p t
)
14637 (user-error "Not on a heading"))
14639 (beginning-of-line 1)
14640 (if (looking-at ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
14641 (match-string-no-properties 1)
14644 (defun org-get-tags ()
14645 "Get the list of tags specified in the current headline."
14646 (org-split-string (org-get-tags-string) ":"))
14648 (defun org-get-buffer-tags ()
14649 "Get a table of all tags used in the buffer, for completion."
14650 (org-with-wide-buffer
14651 (goto-char (point-min))
14652 (let ((tag-re (concat org-outline-regexp-bol
14653 "\\(?:.*?[ \t]\\)?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
14655 (while (re-search-forward tag-re nil t
)
14656 (dolist (tag (org-split-string (match-string-no-properties 1) ":"))
14658 (mapcar #'list
(append org-file-tags
(org-uniquify tags
))))))
14660 ;;;; The mapping API
14662 (defvar org-agenda-skip-comment-trees
)
14663 (defvar org-agenda-skip-function
)
14664 (defun org-map-entries (func &optional match scope
&rest skip
)
14665 "Call FUNC at each headline selected by MATCH in SCOPE.
14667 FUNC is a function or a lisp form. The function will be called without
14668 arguments, with the cursor positioned at the beginning of the headline.
14669 The return values of all calls to the function will be collected and
14670 returned as a list.
14672 The call to FUNC will be wrapped into a save-excursion form, so FUNC
14673 does not need to preserve point. After evaluation, the cursor will be
14674 moved to the end of the line (presumably of the headline of the
14675 processed entry) and search continues from there. Under some
14676 circumstances, this may not produce the wanted results. For example,
14677 if you have removed (e.g. archived) the current (sub)tree it could
14678 mean that the next entry will be skipped entirely. In such cases, you
14679 can specify the position from where search should continue by making
14680 FUNC set the variable `org-map-continue-from' to the desired buffer
14683 MATCH is a tags/property/todo match as it is used in the agenda tags view.
14684 Only headlines that are matched by this query will be considered during
14685 the iteration. When MATCH is nil or t, all headlines will be
14686 visited by the iteration.
14688 SCOPE determines the scope of this command. It can be any of:
14690 nil The current buffer, respecting the restriction if any
14691 tree The subtree started with the entry at point
14692 region The entries within the active region, if any
14694 The entries within the active region, but only those at
14695 the same level than the first one.
14696 file The current buffer, without restriction
14698 The current buffer, and any archives associated with it
14699 agenda All agenda files
14700 agenda-with-archives
14701 All agenda files with any archive files associated with them
14703 If this is a list, all files in the list will be scanned
14705 The remaining args are treated as settings for the skipping facilities of
14706 the scanner. The following items can be given here:
14708 archive skip trees with the archive tag
14709 comment skip trees with the COMMENT keyword
14710 function or Emacs Lisp form:
14711 will be used as value for `org-agenda-skip-function', so
14712 whenever the function returns a position, FUNC will not be
14713 called for that entry and search will continue from the
14716 If your function needs to retrieve the tags including inherited tags
14717 at the *current* entry, you can use the value of the variable
14718 `org-scanner-tags' which will be much faster than getting the value
14719 with `org-get-tags-at'. If your function gets properties with
14720 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
14721 to t around the call to `org-entry-properties' to get the same speedup.
14722 Note that if your function moves around to retrieve tags and properties at
14723 a *different* entry, you cannot use these techniques."
14724 (unless (and (or (eq scope
'region
) (eq scope
'region-start-level
))
14725 (not (org-region-active-p)))
14726 (let* ((org-agenda-archives-mode nil
) ; just to make sure
14727 (org-agenda-skip-archived-trees (memq 'archive skip
))
14728 (org-agenda-skip-comment-trees (memq 'comment skip
))
14729 (org-agenda-skip-function
14730 (car (org-delete-all '(comment archive
) skip
)))
14731 (org-tags-match-list-sublevels t
)
14732 (start-level (eq scope
'region-start-level
))
14734 org-todo-keywords-for-agenda
14735 org-done-keywords-for-agenda
14736 org-todo-keyword-alist-for-agenda
14737 org-tag-alist-for-agenda
14738 org--matcher-tags-todo-only
)
14741 ((eq match t
) (setq matcher t
))
14742 ((eq match nil
) (setq matcher t
))
14743 (t (setq matcher
(if match
(cdr (org-make-tags-matcher match
)) t
))))
14747 (cond ((eq scope
'tree
)
14748 (org-back-to-heading t
)
14749 (org-narrow-to-subtree)
14751 ((and (or (eq scope
'region
) (eq scope
'region-start-level
))
14752 (org-region-active-p))
14753 ;; If needed, set start-level to a string like "2"
14756 (goto-char (region-beginning))
14757 (unless (org-at-heading-p) (outline-next-heading))
14758 (setq start-level
(org-current-level))))
14759 (narrow-to-region (region-beginning)
14761 (goto-char (region-end))
14762 (unless (and (bolp) (org-at-heading-p))
14763 (outline-next-heading))
14769 (org-agenda-prepare-buffers
14770 (and buffer-file-name
(list buffer-file-name
)))
14773 func matcher org--matcher-tags-todo-only start-level
)))
14774 ;; Get the right scope
14776 ((and scope
(listp scope
) (symbolp (car scope
)))
14777 (setq scope
(eval scope
)))
14778 ((eq scope
'agenda
)
14779 (setq scope
(org-agenda-files t
)))
14780 ((eq scope
'agenda-with-archives
)
14781 (setq scope
(org-agenda-files t
))
14782 (setq scope
(org-add-archive-files scope
)))
14784 (setq scope
(and buffer-file-name
(list buffer-file-name
))))
14785 ((eq scope
'file-with-archives
)
14786 (setq scope
(org-add-archive-files (list (buffer-file-name))))))
14787 (org-agenda-prepare-buffers scope
)
14788 (dolist (file scope
)
14789 (with-current-buffer (org-find-base-buffer-visiting file
)
14790 (org-with-wide-buffer
14791 (goto-char (point-min))
14796 func matcher org--matcher-tags-todo-only
)))))))))
14801 (defconst org-special-properties
14802 '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
14803 "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
14804 "The special properties valid in Org mode.
14805 These are properties that are not defined in the property drawer,
14806 but in some other way.")
14808 (defconst org-default-properties
14809 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
14810 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
14811 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
14812 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
14813 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
14814 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
14815 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
14816 "Some properties that are used by Org mode for various purposes.
14817 Being in this list makes sure that they are offered for completion.")
14819 (defun org--valid-property-p (property)
14820 "Non nil when string PROPERTY is a valid property name."
14822 (or (equal property
"")
14823 (string-match-p "\\s-" property
))))
14825 (defun org--update-property-plist (key val props
)
14826 "Associate KEY to VAL in alist PROPS.
14827 Modifications are made by side-effect. Return new alist."
14828 (let* ((appending (string= (substring key -
1) "+"))
14829 (key (if appending
(substring key
0 -
1) key
))
14830 (old (assoc-string key props t
)))
14831 (if (not old
) (cons (cons key val
) props
)
14832 (setcdr old
(if appending
(concat (cdr old
) " " val
) val
))
14835 (defun org-get-property-block (&optional beg force
)
14836 "Return the (beg . end) range of the body of the property drawer.
14837 BEG is the beginning of the current subtree, or of the part
14838 before the first headline. If it is not given, it will be found.
14839 If the drawer does not exist, create it if FORCE is non-nil, or
14841 (org-with-wide-buffer
14842 (when beg
(goto-char beg
))
14843 (unless (org-before-first-heading-p)
14844 (let ((beg (cond (beg)
14845 ((or (not (featurep 'org-inlinetask
))
14846 (org-inlinetask-in-task-p))
14847 (org-back-to-heading t
))
14848 (t (org-with-limited-levels (org-back-to-heading t
))))))
14850 (when (looking-at-p org-planning-line-re
) (forward-line))
14851 (cond ((looking-at org-property-drawer-re
)
14853 (cons (point) (progn (goto-char (match-end 0))
14854 (line-beginning-position))))
14857 (org-insert-property-drawer)
14858 (let ((pos (save-excursion (search-forward ":END:")
14859 (line-beginning-position))))
14860 (cons pos pos
))))))))
14862 (defun org-at-property-p ()
14863 "Non-nil when point is inside a property drawer.
14864 See `org-property-re' for match data, if applicable."
14866 (beginning-of-line)
14867 (and (looking-at org-property-re
)
14868 (let ((property-drawer (save-match-data (org-get-property-block))))
14869 (and property-drawer
14870 (>= (point) (car property-drawer
))
14871 (< (point) (cdr property-drawer
)))))))
14873 (defun org-property-action ()
14874 "Do an action on properties."
14876 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
14877 (let ((c (read-char-exclusive)))
14879 (?s
(call-interactively #'org-set-property
))
14880 (?d
(call-interactively #'org-delete-property
))
14881 (?D
(call-interactively #'org-delete-property-globally
))
14882 (?c
(call-interactively #'org-compute-property-at-point
))
14883 (otherwise (user-error "No such property action %c" c
)))))
14885 (defun org-inc-effort ()
14886 "Increment the value of the effort property in the current entry."
14888 (org-set-effort t
))
14890 (defvar org-clock-effort
) ; Defined in org-clock.el.
14891 (defvar org-clock-current-task
) ; Defined in org-clock.el.
14892 (defun org-set-effort (&optional increment value
)
14893 "Set the effort property of the current entry.
14894 If INCREMENT is non-nil, set the property to the next allowed
14895 value. Otherwise, if optional argument VALUE is provided, use
14896 it. Eventually, prompt for the new value if none of the previous
14899 (let* ((allowed (org-property-get-allowed-values nil org-effort-property t
))
14900 (current (org-entry-get nil org-effort-property
))
14904 (unless allowed
(user-error "Allowed effort values are not set"))
14905 (or (cl-caadr (member (list current
) allowed
))
14906 (user-error "Unknown value %S among allowed values" current
)))
14908 (if (stringp value
) value
14909 (error "Invalid effort value: %S" value
)))
14913 (not (get-text-property 0 'org-unrestricted
14914 (caar allowed
))))))
14915 (completing-read "Effort: " allowed nil must-match
))))))
14916 (unless (equal current value
)
14917 (org-entry-put nil org-effort-property value
))
14918 (org-refresh-property '((effort . identity
)
14919 (effort-minutes . org-duration-to-minutes
))
14921 (when (equal (org-get-heading t t t t
)
14922 (bound-and-true-p org-clock-current-task
))
14923 (setq org-clock-effort
(org-get-at-bol 'effort
))
14924 (org-clock-update-mode-line))
14925 (message "%s is now %s" org-effort-property value
)))
14927 (defun org-entry-properties (&optional pom which
)
14928 "Get all properties of the current entry.
14930 When POM is a buffer position, get all properties from the entry
14933 This includes the TODO keyword, the tags, time strings for
14934 deadline, scheduled, and clocking, and any additional properties
14935 defined in the entry.
14937 If WHICH is nil or `all', get all properties. If WHICH is
14938 `special' or `standard', only get that subclass. If WHICH is
14939 a string, only get that property.
14941 Return value is an alist. Keys are properties, as upcased
14943 (org-with-point-at pom
14944 (when (and (derived-mode-p 'org-mode
)
14945 (ignore-errors (org-back-to-heading t
)))
14947 (let* ((beg (point))
14948 (specific (and (stringp which
) (upcase which
)))
14949 (which (cond ((not specific
) which
)
14950 ((member specific org-special-properties
) 'special
)
14953 ;; Get the special properties, like TODO and TAGS.
14954 (when (memq which
'(nil all special
))
14955 (when (or (not specific
) (string= specific
"CLOCKSUM"))
14956 (let ((clocksum (get-text-property (point) :org-clock-minutes
)))
14958 (push (cons "CLOCKSUM" (org-duration-from-minutes clocksum
))
14960 (when specific
(throw 'exit props
)))
14961 (when (or (not specific
) (string= specific
"CLOCKSUM_T"))
14962 (let ((clocksumt (get-text-property (point)
14963 :org-clock-minutes-today
)))
14965 (push (cons "CLOCKSUM_T"
14966 (org-duration-from-minutes clocksumt
))
14968 (when specific
(throw 'exit props
)))
14969 (when (or (not specific
) (string= specific
"ITEM"))
14970 (let ((case-fold-search nil
))
14971 (when (looking-at org-complex-heading-regexp
)
14973 (let ((title (match-string-no-properties 4)))
14974 (if (org-string-nw-p title
)
14975 (org-remove-tabs title
)
14978 (when specific
(throw 'exit props
)))
14979 (when (or (not specific
) (string= specific
"TODO"))
14980 (let ((case-fold-search nil
))
14981 (when (and (looking-at org-todo-line-regexp
) (match-end 2))
14982 (push (cons "TODO" (match-string-no-properties 2)) props
)))
14983 (when specific
(throw 'exit props
)))
14984 (when (or (not specific
) (string= specific
"PRIORITY"))
14985 (push (cons "PRIORITY"
14986 (if (looking-at org-priority-regexp
)
14987 (match-string-no-properties 2)
14988 (char-to-string org-default-priority
)))
14990 (when specific
(throw 'exit props
)))
14991 (when (or (not specific
) (string= specific
"FILE"))
14992 (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
14994 (when specific
(throw 'exit props
)))
14995 (when (or (not specific
) (string= specific
"TAGS"))
14996 (let ((value (org-string-nw-p (org-get-tags-string))))
14997 (when value
(push (cons "TAGS" value
) props
)))
14998 (when specific
(throw 'exit props
)))
14999 (when (or (not specific
) (string= specific
"ALLTAGS"))
15000 (let ((value (org-get-tags-at)))
15002 (push (cons "ALLTAGS"
15003 (format ":%s:" (mapconcat #'identity value
":")))
15005 (when specific
(throw 'exit props
)))
15006 (when (or (not specific
) (string= specific
"BLOCKED"))
15007 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props
)
15008 (when specific
(throw 'exit props
)))
15009 (when (or (not specific
)
15010 (member specific
'("CLOSED" "DEADLINE" "SCHEDULED")))
15012 (when (looking-at-p org-planning-line-re
)
15014 (let ((bol (line-beginning-position))
15015 ;; Backward compatibility: time keywords used to
15016 ;; be configurable (before 8.3). Make sure we
15017 ;; get the correct keyword.
15018 (key-assoc `(("CLOSED" .
,org-closed-string
)
15019 ("DEADLINE" .
,org-deadline-string
)
15020 ("SCHEDULED" .
,org-scheduled-string
))))
15021 (dolist (pair (if specific
(list (assoc specific key-assoc
))
15024 (when (search-backward (cdr pair
) bol t
)
15025 (goto-char (match-end 0))
15026 (skip-chars-forward " \t")
15027 (and (looking-at org-ts-regexp-both
)
15028 (push (cons (car pair
)
15029 (match-string-no-properties 0))
15031 (when specific
(throw 'exit props
)))
15032 (when (or (not specific
)
15033 (member specific
'("TIMESTAMP" "TIMESTAMP_IA")))
15036 ;; Fix next time-stamp before END. TS is the
15037 ;; list of time-stamps found so far.
15040 ((string= specific
"TIMESTAMP")
15042 ((string= specific
"TIMESTAMP_IA")
15043 org-ts-regexp-inactive
)
15044 ((assoc "TIMESTAMP_IA" ts
)
15046 ((assoc "TIMESTAMP" ts
)
15047 org-ts-regexp-inactive
)
15048 (t org-ts-regexp-both
))))
15050 (while (re-search-forward regexp end t
)
15052 (let ((object (org-element-context)))
15053 ;; Accept to match timestamps in node
15054 ;; properties, too.
15055 (when (memq (org-element-type object
)
15056 '(node-property timestamp
))
15058 (org-element-property :type object
)))
15060 ((and (memq type
'(active active-range
))
15061 (not (equal specific
"TIMESTAMP_IA")))
15062 (unless (assoc "TIMESTAMP" ts
)
15063 (push (cons "TIMESTAMP"
15064 (org-element-property
15065 :raw-value object
))
15067 (when specific
(throw 'exit ts
))))
15068 ((and (memq type
'(inactive inactive-range
))
15069 (not (string= specific
"TIMESTAMP")))
15070 (unless (assoc "TIMESTAMP_IA" ts
)
15071 (push (cons "TIMESTAMP_IA"
15072 (org-element-property
15073 :raw-value object
))
15075 (when specific
(throw 'exit ts
))))))
15076 ;; Both timestamp types are found,
15077 ;; move to next part.
15078 (when (= (length ts
) 2) (throw 'next ts
)))))
15081 ;; First look for timestamps within headline.
15082 (let ((ts (funcall find-ts
(line-end-position) nil
)))
15083 (if (= (length ts
) 2) (setq props
(nconc ts props
))
15084 ;; Then find timestamps in the section, skipping
15086 (let ((end (save-excursion (outline-next-heading))))
15088 (when (looking-at-p org-planning-line-re
) (forward-line))
15089 (setq props
(nconc (funcall find-ts end ts
) props
))))))))
15090 ;; Get the standard properties, like :PROP:.
15091 (when (memq which
'(nil all standard
))
15092 ;; If we are looking after a specific property, delegate
15093 ;; to `org-entry-get', which is faster. However, make an
15094 ;; exception for "CATEGORY", since it can be also set
15095 ;; through keywords (i.e. #+CATEGORY).
15096 (if (and specific
(not (equal specific
"CATEGORY")))
15097 (let ((value (org-entry-get beg specific nil t
)))
15098 (throw 'exit
(and value
(list (cons specific value
)))))
15099 (let ((range (org-get-property-block beg
)))
15101 (let ((end (cdr range
)) seen-base
)
15102 (goto-char (car range
))
15103 ;; Unlike to `org--update-property-plist', we
15104 ;; handle the case where base values is found
15105 ;; after its extension. We also forbid standard
15106 ;; properties to be named as special properties.
15107 (while (re-search-forward org-property-re end t
)
15108 (let* ((key (upcase (match-string-no-properties 2)))
15109 (extendp (string-match-p "\\+\\'" key
))
15110 (key-base (if extendp
(substring key
0 -
1) key
))
15111 (value (match-string-no-properties 3)))
15113 ((member-ignore-case key-base org-special-properties
))
15116 (org--update-property-plist key value props
)))
15117 ((member key seen-base
))
15118 (t (push key seen-base
)
15119 (let ((p (assoc-string key props t
)))
15120 (if p
(setcdr p
(concat value
" " (cdr p
)))
15121 (push (cons key value
) props
))))))))))))
15122 (unless (assoc "CATEGORY" props
)
15123 (push (cons "CATEGORY" (org-get-category beg
)) props
)
15124 (when (string= specific
"CATEGORY") (throw 'exit props
)))
15128 (defun org--property-local-values (property literal-nil
)
15129 "Return value for PROPERTY in current entry.
15130 Value is a list whose car is the base value for PROPERTY and cdr
15131 a list of accumulated values. Return nil if neither is found in
15132 the entry. Also return nil when PROPERTY is set to \"nil\",
15133 unless LITERAL-NIL is non-nil."
15134 (let ((range (org-get-property-block)))
15136 (goto-char (car range
))
15137 (let* ((case-fold-search t
)
15142 (let ((v (and (re-search-forward
15143 (org-re-property property nil t
) end t
)
15144 (match-string-no-properties 3))))
15145 (list (if literal-nil v
(org-not-nil v
)))))))
15146 ;; Find additional values.
15147 (let* ((property+ (org-re-property (concat property
"+") nil t
)))
15148 (while (re-search-forward property
+ end t
)
15149 (push (match-string-no-properties 3) value
)))
15150 ;; Return final values.
15151 (and (not (equal value
'(nil))) (nreverse value
))))))
15153 (defun org--property-global-value (property literal-nil
)
15154 "Return value for PROPERTY in current buffer.
15155 Return value is a string. Return nil if property is not set
15156 globally. Also return nil when PROPERTY is set to \"nil\",
15157 unless LITERAL-NIL is non-nil."
15159 (cdr (or (assoc-string property org-file-properties t
)
15160 (assoc-string property org-global-properties t
)
15161 (assoc-string property org-global-properties-fixed t
)))))
15162 (if literal-nil global
(org-not-nil global
))))
15164 (defun org-entry-get (pom property
&optional inherit literal-nil
)
15165 "Get value of PROPERTY for entry or content at point-or-marker POM.
15167 If INHERIT is non-nil and the entry does not have the property,
15168 then also check higher levels of the hierarchy. If INHERIT is
15169 the symbol `selective', use inheritance only if the setting in
15170 `org-use-property-inheritance' selects PROPERTY for inheritance.
15172 If the property is present but empty, the return value is the
15173 empty string. If the property is not present at all, nil is
15174 returned. In any other case, return the value as a string.
15175 Search is case-insensitive.
15177 If LITERAL-NIL is set, return the string value \"nil\" as
15178 a string, do not interpret it as the list atom nil. This is used
15179 for inheritance when a \"nil\" value can supersede a non-nil
15180 value higher up the hierarchy."
15181 (org-with-point-at pom
15183 ((member-ignore-case property
(cons "CATEGORY" org-special-properties
))
15184 ;; We need a special property. Use `org-entry-properties' to
15185 ;; retrieve it, but specify the wanted property.
15186 (cdr (assoc-string property
(org-entry-properties nil property
))))
15188 (or (not (eq inherit
'selective
)) (org-property-inherit-p property
)))
15189 (org-entry-get-with-inheritance property literal-nil
))
15191 (let* ((local (org--property-local-values property literal-nil
))
15192 (value (and local
(mapconcat #'identity
(delq nil local
) " "))))
15193 (if literal-nil value
(org-not-nil value
)))))))
15195 (defun org-property-or-variable-value (var &optional inherit
)
15196 "Check if there is a property fixing the value of VAR.
15197 If yes, return this value. If not, return the current value of the variable."
15198 (let ((prop (org-entry-get nil
(symbol-name var
) inherit
)))
15199 (if (and prop
(stringp prop
) (string-match "\\S-" prop
))
15201 (symbol-value var
))))
15203 (defun org-entry-delete (pom property
)
15204 "Delete PROPERTY from entry at point-or-marker POM.
15205 Accumulated properties, i.e. PROPERTY+, are also removed. Return
15206 non-nil when a property was removed."
15207 (org-with-point-at pom
15208 (pcase (org-get-property-block)
15209 (`(,begin .
,origin
)
15210 (let* ((end (copy-marker origin
))
15211 (re (org-re-property
15212 (concat (regexp-quote property
) "\\+?") t t
)))
15214 (while (re-search-forward re end t
)
15215 (delete-region (match-beginning 0) (line-beginning-position 2)))
15216 ;; If drawer is empty, remove it altogether.
15217 (when (= begin end
)
15218 (delete-region (line-beginning-position 0)
15219 (line-beginning-position 2)))
15220 ;; Return non-nil if some property was removed.
15221 (prog1 (/= end origin
) (set-marker end nil
))))
15224 ;; Multi-values properties are properties that contain multiple values
15225 ;; These values are assumed to be single words, separated by whitespace.
15226 (defun org-entry-add-to-multivalued-property (pom property value
)
15227 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
15228 (let* ((old (org-entry-get pom property
))
15229 (values (and old
(split-string old
))))
15230 (setq value
(org-entry-protect-space value
))
15231 (unless (member value values
)
15232 (setq values
(append values
(list value
)))
15233 (org-entry-put pom property
(mapconcat #'identity values
" ")))))
15235 (defun org-entry-remove-from-multivalued-property (pom property value
)
15236 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
15237 (let* ((old (org-entry-get pom property
))
15238 (values (and old
(split-string old
))))
15239 (setq value
(org-entry-protect-space value
))
15240 (when (member value values
)
15241 (setq values
(delete value values
))
15242 (org-entry-put pom property
(mapconcat #'identity values
" ")))))
15244 (defun org-entry-member-in-multivalued-property (pom property value
)
15245 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
15246 (let* ((old (org-entry-get pom property
))
15247 (values (and old
(split-string old
))))
15248 (setq value
(org-entry-protect-space value
))
15249 (member value values
)))
15251 (defun org-entry-get-multivalued-property (pom property
)
15252 "Return a list of values in a multivalued property."
15253 (let* ((value (org-entry-get pom property
))
15254 (values (and value
(split-string value
))))
15255 (mapcar #'org-entry-restore-space values
)))
15257 (defun org-entry-put-multivalued-property (pom property
&rest values
)
15258 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
15259 VALUES should be a list of strings. Spaces will be protected."
15260 (org-entry-put pom property
(mapconcat #'org-entry-protect-space values
" "))
15261 (let* ((value (org-entry-get pom property
))
15262 (values (and value
(split-string value
))))
15263 (mapcar #'org-entry-restore-space values
)))
15265 (defun org-entry-protect-space (s)
15266 "Protect spaces and newline in string S."
15267 (while (string-match " " s
)
15268 (setq s
(replace-match "%20" t t s
)))
15269 (while (string-match "\n" s
)
15270 (setq s
(replace-match "%0A" t t s
)))
15273 (defun org-entry-restore-space (s)
15274 "Restore spaces and newline in string S."
15275 (while (string-match "%20" s
)
15276 (setq s
(replace-match " " t t s
)))
15277 (while (string-match "%0A" s
)
15278 (setq s
(replace-match "\n" t t s
)))
15281 (defvar org-entry-property-inherited-from
(make-marker)
15282 "Marker pointing to the entry from where a property was inherited.
15283 Each call to `org-entry-get-with-inheritance' will set this marker to the
15284 location of the entry where the inheritance search matched. If there was
15285 no match, the marker will point nowhere.
15286 Note that also `org-entry-get' calls this function, if the INHERIT flag
15289 (defun org-entry-get-with-inheritance (property &optional literal-nil
)
15290 "Get PROPERTY of entry or content at point, search higher levels if needed.
15291 The search will stop at the first ancestor which has the property defined.
15292 If the value found is \"nil\", return nil to show that the property
15293 should be considered as undefined (this is the meaning of nil here).
15294 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
15295 (move-marker org-entry-property-inherited-from nil
)
15296 (org-with-wide-buffer
15300 (let ((v (org--property-local-values property literal-nil
)))
15303 (concat (mapconcat #'identity
(delq nil v
) " ")
15308 (org-back-to-heading t
)
15309 (move-marker org-entry-property-inherited-from
(point))
15311 ((org-up-heading-safe))
15313 (let ((global (org--property-global-value property literal-nil
)))
15314 (cond ((not global
))
15315 (value (setq value
(concat global
" " value
)))
15316 (t (setq value global
))))
15317 (throw 'exit nil
))))))
15318 (if literal-nil value
(org-not-nil value
)))))
15320 (defvar org-property-changed-functions nil
15321 "Hook called when the value of a property has changed.
15322 Each hook function should accept two arguments, the name of the property
15323 and the new value.")
15325 (defun org-entry-put (pom property value
)
15326 "Set PROPERTY to VALUE for entry at point-or-marker POM.
15328 If the value is nil, it is converted to the empty string. If it
15329 is not a string, an error is raised. Also raise an error on
15330 invalid property names.
15332 PROPERTY can be any regular property (see
15333 `org-special-properties'). It can also be \"TODO\",
15334 \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
15336 For the last two properties, VALUE may have any of the special
15337 values \"earlier\" and \"later\". The function then increases or
15338 decreases scheduled or deadline date by one day."
15339 (cond ((null value
) (setq value
""))
15340 ((not (stringp value
)) (error "Properties values should be strings"))
15341 ((not (org--valid-property-p property
))
15342 (user-error "Invalid property name: \"%s\"" property
)))
15343 (org-with-point-at pom
15344 (if (or (not (featurep 'org-inlinetask
)) (org-inlinetask-in-task-p))
15345 (org-back-to-heading t
)
15346 (org-with-limited-levels (org-back-to-heading t
)))
15347 (let ((beg (point)))
15349 ((equal property
"TODO")
15350 (cond ((not (org-string-nw-p value
)) (setq value
'none
))
15351 ((not (member value org-todo-keywords-1
))
15352 (user-error "\"%s\" is not a valid TODO state" value
)))
15354 (org-set-tags nil
'align
))
15355 ((equal property
"PRIORITY")
15356 (org-priority (if (org-string-nw-p value
) (string-to-char value
) ?\s
))
15357 (org-set-tags nil
'align
))
15358 ((equal property
"SCHEDULED")
15360 (if (and (looking-at-p org-planning-line-re
)
15362 org-scheduled-time-regexp
(line-end-position) t
))
15363 (cond ((string= value
"earlier") (org-timestamp-change -
1 'day
))
15364 ((string= value
"later") (org-timestamp-change 1 'day
))
15365 ((string= value
"") (org-schedule '(4)))
15366 (t (org-schedule nil value
)))
15367 (if (member value
'("earlier" "later" ""))
15368 (call-interactively #'org-schedule
)
15369 (org-schedule nil value
))))
15370 ((equal property
"DEADLINE")
15372 (if (and (looking-at-p org-planning-line-re
)
15374 org-deadline-time-regexp
(line-end-position) t
))
15375 (cond ((string= value
"earlier") (org-timestamp-change -
1 'day
))
15376 ((string= value
"later") (org-timestamp-change 1 'day
))
15377 ((string= value
"") (org-deadline '(4)))
15378 (t (org-deadline nil value
)))
15379 (if (member value
'("earlier" "later" ""))
15380 (call-interactively #'org-deadline
)
15381 (org-deadline nil value
))))
15382 ((member property org-special-properties
)
15383 (error "The %s property cannot be set with `org-entry-put'" property
))
15385 (let* ((range (org-get-property-block beg
'force
))
15387 (case-fold-search t
))
15388 (goto-char (car range
))
15389 (if (re-search-forward (org-re-property property nil t
) end t
)
15390 (progn (delete-region (match-beginning 0) (match-end 0))
15391 (goto-char (match-beginning 0)))
15395 (insert ":" property
":")
15396 (when value
(insert " " value
))
15397 (org-indent-line)))))
15398 (run-hook-with-args 'org-property-changed-functions property value
)))
15400 (defun org-buffer-property-keys (&optional specials defaults columns
)
15401 "Get all property keys in the current buffer.
15403 When SPECIALS is non-nil, also list the special properties that
15404 reflect things like tags and TODO state.
15406 When DEFAULTS is non-nil, also include properties that has
15407 special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
15408 DESCRIPTION, LOCATION, and LOGGING and others.
15410 When COLUMNS in non-nil, also include property names given in
15411 COLUMN formats in the current buffer."
15412 (let ((case-fold-search t
)
15414 (and specials org-special-properties
)
15415 (and defaults
(cons org-effort-property org-default-properties
))
15417 (org-with-wide-buffer
15418 (goto-char (point-min))
15419 (while (re-search-forward org-property-start-re nil t
)
15421 (let ((range (org-get-property-block)))
15422 (unless range
(throw :skip nil
))
15423 (goto-char (car range
))
15424 (let ((begin (car range
))
15426 ;; Make sure that found property block is not located
15427 ;; before current point, as it would generate an infloop.
15428 ;; It can happen, for example, in the following
15435 ;; *************** Inlinetask
15440 (if (< begin
(point)) (throw :skip nil
) (goto-char begin
))
15441 (while (< (point) end
)
15442 (let ((p (progn (looking-at org-property-re
)
15443 (match-string-no-properties 2))))
15444 ;; Only add true property name, not extension symbol.
15445 (push (if (not (string-match-p "\\+\\'" p
)) p
15446 (substring p
0 -
1))
15449 (outline-next-heading)))
15451 (goto-char (point-min))
15452 (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t
)
15453 (let ((element (org-element-at-point)))
15454 (when (memq (org-element-type element
) '(keyword node-property
))
15455 (let ((value (org-element-property :value element
))
15457 (while (string-match "%[0-9]*\\([[:alnum:]_-]+\\)\\(([^)]+)\\)?\
15460 (setq start
(match-end 0))
15461 (let ((p (match-string-no-properties 1 value
)))
15462 (unless (member-ignore-case p org-special-properties
)
15463 (push p props
))))))))))
15464 (sort (delete-dups props
) (lambda (a b
) (string< (upcase a
) (upcase b
))))))
15466 (defun org-property-values (key)
15467 "List all non-nil values of property KEY in current buffer."
15468 (org-with-wide-buffer
15469 (goto-char (point-min))
15470 (let ((case-fold-search t
)
15471 (re (org-re-property key
))
15473 (while (re-search-forward re nil t
)
15474 (push (org-entry-get (point) key
) values
))
15475 (delete-dups values
))))
15477 (defun org-insert-property-drawer ()
15478 "Insert a property drawer into the current entry."
15479 (org-with-wide-buffer
15480 (if (or (not (featurep 'org-inlinetask
)) (org-inlinetask-in-task-p))
15481 (org-back-to-heading t
)
15482 (org-with-limited-levels (org-back-to-heading t
)))
15484 (when (looking-at-p org-planning-line-re
) (forward-line))
15485 (unless (looking-at-p org-property-drawer-re
)
15486 ;; Make sure we start editing a line from current entry, not from
15487 ;; next one. It prevents extending text properties or overlays
15488 ;; belonging to the latter.
15489 (when (bolp) (backward-char))
15490 (let ((begin (1+ (point)))
15491 (inhibit-read-only t
))
15492 (insert "\n:PROPERTIES:\n:END:")
15493 (when (eobp) (insert "\n"))
15494 (org-indent-region begin
(point))))))
15496 (defun org-insert-drawer (&optional arg drawer
)
15497 "Insert a drawer at point.
15499 When optional argument ARG is non-nil, insert a property drawer.
15501 Optional argument DRAWER, when non-nil, is a string representing
15502 drawer's name. Otherwise, the user is prompted for a name.
15504 If a region is active, insert the drawer around that region
15507 Point is left between drawer's boundaries."
15509 (let* ((drawer (if arg
"PROPERTIES"
15510 (or drawer
(read-from-minibuffer "Drawer: ")))))
15512 ;; With C-u, fall back on `org-insert-property-drawer'
15513 (arg (org-insert-property-drawer))
15514 ;; Check validity of suggested drawer's name.
15515 ((not (string-match-p org-drawer-regexp
(format ":%s:" drawer
)))
15516 (user-error "Invalid drawer name"))
15517 ;; With an active region, insert a drawer at point.
15518 ((not (org-region-active-p))
15520 (unless (bolp) (insert "\n"))
15521 (insert (format ":%s:\n\n:END:\n" drawer
))
15522 (forward-line -
2)))
15523 ;; Otherwise, insert the drawer at point
15525 (let ((rbeg (region-beginning))
15526 (rend (copy-marker (region-end))))
15530 (beginning-of-line)
15531 (when (save-excursion
15532 (re-search-forward org-outline-regexp-bol rend t
))
15533 (user-error "Drawers cannot contain headlines"))
15534 ;; Position point at the beginning of the first
15535 ;; non-blank line in region. Insert drawer's opening
15536 ;; there, then indent it.
15537 (org-skip-whitespace)
15538 (beginning-of-line)
15539 (insert ":" drawer
":\n")
15541 (indent-for-tab-command)
15542 ;; Move point to the beginning of the first blank line
15543 ;; after the last non-blank line in region. Insert
15544 ;; drawer's closing, then indent it.
15546 (skip-chars-backward " \r\t\n")
15548 (deactivate-mark t
)
15549 (indent-for-tab-command)
15550 (unless (eolp) (insert "\n")))
15551 ;; Clear marker, whatever the outcome of insertion is.
15552 (set-marker rend nil
)))))))
15554 (defvar org-property-set-functions-alist nil
15555 "Property set function alist.
15556 Each entry should have the following format:
15558 (PROPERTY . READ-FUNCTION)
15560 The read function will be called with the same argument as
15561 `org-completing-read'.")
15563 (defun org-set-property-function (property)
15564 "Get the function that should be used to set PROPERTY.
15565 This is computed according to `org-property-set-functions-alist'."
15566 (or (cdr (assoc property org-property-set-functions-alist
))
15567 'org-completing-read
))
15569 (defun org-read-property-value (property)
15570 "Read PROPERTY value from user."
15571 (let* ((completion-ignore-case t
)
15572 (allowed (org-property-get-allowed-values nil property
'table
))
15573 (cur (org-entry-get nil property
))
15574 (prompt (concat property
" value"
15575 (if (and cur
(string-match "\\S-" cur
))
15576 (concat " [" cur
"]") "") ": "))
15577 (set-function (org-set-property-function property
))
15579 (funcall set-function prompt allowed nil
15580 (not (get-text-property 0 'org-unrestricted
15582 (funcall set-function prompt
15583 (mapcar 'list
(org-property-values property
))
15584 nil nil
"" nil cur
))))
15587 (defvar org-last-set-property nil
)
15588 (defvar org-last-set-property-value nil
)
15589 (defun org-read-property-name ()
15590 "Read a property name."
15591 (let ((completion-ignore-case t
)
15592 (default-prop (or (and (org-at-property-p)
15593 (match-string-no-properties 2))
15594 org-last-set-property
)))
15595 (org-completing-read
15597 (if default-prop
(concat " [" default-prop
"]") "")
15599 (mapcar #'list
(org-buffer-property-keys nil t t
))
15600 nil nil nil nil default-prop
)))
15602 (defun org-set-property-and-value (use-last)
15603 "Allow to set [PROPERTY]: [value] direction from prompt.
15604 When use-default, don't even ask, just use the last
15605 \"[PROPERTY]: [value]\" string from the history."
15607 (let* ((completion-ignore-case t
)
15608 (pv (or (and use-last org-last-set-property-value
)
15609 (org-completing-read
15610 "Enter a \"[Property]: [value]\" pair: "
15611 nil nil nil nil nil
15612 org-last-set-property-value
)))
15614 (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv
)
15615 (setq prop
(match-string 1 pv
)
15616 val
(match-string 2 pv
))
15617 (org-set-property prop val
))))
15619 (defun org-set-property (property value
)
15620 "In the current entry, set PROPERTY to VALUE.
15622 When called interactively, this will prompt for a property name, offering
15623 completion on existing and default properties. And then it will prompt
15624 for a value, offering completion either on allowed values (via an inherited
15625 xxx_ALL property) or on existing values in other instances of this property
15626 in the current file.
15628 Throw an error when trying to set a property with an invalid name."
15629 (interactive (list nil nil
))
15630 (let ((property (or property
(org-read-property-name))))
15631 ;; `org-entry-put' also makes the following check, but this one
15632 ;; avoids polluting `org-last-set-property' and
15633 ;; `org-last-set-property-value' needlessly.
15634 (unless (org--valid-property-p property
)
15635 (user-error "Invalid property name: \"%s\"" property
))
15636 (let ((value (or value
(org-read-property-value property
)))
15637 (fn (cdr (assoc-string property org-properties-postprocess-alist t
))))
15638 (setq org-last-set-property property
)
15639 (setq org-last-set-property-value
(concat property
": " value
))
15640 ;; Possibly postprocess the inserted value:
15641 (when fn
(setq value
(funcall fn value
)))
15642 (unless (equal (org-entry-get nil property
) value
)
15643 (org-entry-put nil property value
)))))
15645 (defun org-find-property (property &optional value
)
15646 "Find first entry in buffer that sets PROPERTY.
15648 When optional argument VALUE is non-nil, only consider an entry
15649 if it contains PROPERTY set to this value. If PROPERTY should be
15650 explicitly set to nil, use string \"nil\" for VALUE.
15652 Return position where the entry begins, or nil if there is no
15653 such entry. If narrowing is in effect, only search the visible
15654 part of the buffer."
15656 (goto-char (point-min))
15657 (let ((case-fold-search t
)
15658 (re (org-re-property property nil
(not value
) value
)))
15660 (while (re-search-forward re nil t
)
15661 (when (if value
(org-at-property-p)
15662 (org-entry-get (point) property nil t
))
15663 (throw 'exit
(progn (org-back-to-heading t
) (point)))))))))
15665 (defun org-delete-property (property)
15666 "In the current entry, delete PROPERTY."
15668 (let* ((completion-ignore-case t
)
15669 (cat (org-entry-get (point) "CATEGORY"))
15670 (props0 (org-entry-properties nil
'standard
))
15671 (props (if cat props0
15672 (delete `("CATEGORY" .
,(org-get-category)) props0
)))
15673 (prop (if (< 1 (length props
))
15674 (completing-read "Property: " props nil t
)
15678 (message "No property to delete in this entry")
15679 (org-entry-delete nil property
)
15680 (message "Property \"%s\" deleted" property
)))
15682 (defun org-delete-property-globally (property)
15683 "Remove PROPERTY globally, from all entries.
15684 This function ignores narrowing, if any."
15686 (let* ((completion-ignore-case t
)
15687 (prop (completing-read
15688 "Globally remove property: "
15689 (mapcar #'list
(org-buffer-property-keys)))))
15691 (org-with-wide-buffer
15692 (goto-char (point-min))
15694 (re (org-re-property (concat (regexp-quote property
) "\\+?") t t
)))
15695 (while (re-search-forward re nil t
)
15696 (when (org-entry-delete (point) property
) (cl-incf count
)))
15697 (message "Property \"%s\" removed from %d entries" property count
))))
15699 (defvar org-columns-current-fmt-compiled
) ; defined in org-colview.el
15701 (defun org-compute-property-at-point ()
15702 "Compute the property at point.
15703 This looks for an enclosing column format, extracts the operator and
15704 then applies it to the property in the column format's scope."
15706 (unless (org-at-property-p)
15707 (user-error "Not at a property"))
15708 (let ((prop (match-string-no-properties 2)))
15709 (org-columns-get-format-and-top-level)
15710 (unless (nth 3 (assoc-string prop org-columns-current-fmt-compiled t
))
15711 (user-error "No operator defined for property %s" prop
))
15712 (org-columns-compute prop
)))
15714 (defvar org-property-allowed-value-functions nil
15715 "Hook for functions supplying allowed values for a specific property.
15716 The functions must take a single argument, the name of the property, and
15717 return a flat list of allowed values. If \":ETC\" is one of
15718 the values, this means that these values are intended as defaults for
15719 completion, but that other values should be allowed too.
15720 The functions must return nil if they are not responsible for this
15723 (defun org-property-get-allowed-values (pom property
&optional table
)
15724 "Get allowed values for the property PROPERTY.
15725 When TABLE is non-nil, return an alist that can directly be used for
15729 ((equal property
"TODO")
15730 (setq vals
(org-with-point-at pom
15731 (append org-todo-keywords-1
'("")))))
15732 ((equal property
"PRIORITY")
15733 (let ((n org-lowest-priority
))
15734 (while (>= n org-highest-priority
)
15735 (push (char-to-string n
) vals
)
15737 ((equal property
"CATEGORY"))
15738 ((member property org-special-properties
))
15739 ((setq vals
(run-hook-with-args-until-success
15740 'org-property-allowed-value-functions property
)))
15742 (setq vals
(org-entry-get pom
(concat property
"_ALL") 'inherit
))
15743 (when (and vals
(string-match "\\S-" vals
))
15744 (setq vals
(car (read-from-string (concat "(" vals
")"))))
15745 (setq vals
(mapcar (lambda (x)
15746 (cond ((stringp x
) x
)
15747 ((numberp x
) (number-to-string x
))
15748 ((symbolp x
) (symbol-name x
))
15751 (when (member ":ETC" vals
)
15752 (setq vals
(remove ":ETC" vals
))
15753 (org-add-props (car vals
) '(org-unrestricted t
)))
15754 (if table
(mapcar 'list vals
) vals
)))
15756 (defun org-property-previous-allowed-value (&optional _previous
)
15757 "Switch to the next allowed value for this property."
15759 (org-property-next-allowed-value t
))
15761 (defun org-property-next-allowed-value (&optional previous
)
15762 "Switch to the next allowed value for this property."
15764 (unless (org-at-property-p)
15765 (user-error "Not at a property"))
15766 (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
15767 (key (match-string 2))
15768 (value (match-string 3))
15769 (allowed (or (org-property-get-allowed-values (point) key
)
15770 (and (member value
'("[ ]" "[-]" "[X]"))
15772 (heading (save-match-data (nth 4 (org-heading-components))))
15775 (user-error "Allowed values for this property have not been defined"))
15776 (when previous
(setq allowed
(reverse allowed
)))
15777 (when (member value allowed
)
15778 (setq nval
(car (cdr (member value allowed
)))))
15779 (setq nval
(or nval
(car allowed
)))
15780 (when (equal nval value
)
15781 (user-error "Only one allowed value for this property"))
15782 (org-at-property-p)
15783 (replace-match (concat " :" key
": " nval
) t t
)
15785 (beginning-of-line 1)
15786 (skip-chars-forward " \t")
15787 (when (equal prop org-effort-property
)
15788 (org-refresh-property
15789 '((effort . identity
)
15790 (effort-minutes . org-duration-to-minutes
))
15792 (when (string= org-clock-current-task heading
)
15793 (setq org-clock-effort nval
)
15794 (org-clock-update-mode-line)))
15795 (run-hook-with-args 'org-property-changed-functions key nval
)))
15797 (defun org-find-olp (path &optional this-buffer
)
15798 "Return a marker pointing to the entry at outline path OLP.
15799 If anything goes wrong, throw an error.
15800 You can wrap this call to catch the error like this:
15802 (condition-case msg
15803 (org-mobile-locate-entry (match-string 4))
15804 (error (nth 1 msg)))
15806 The return value will then be either a string with the error message,
15807 or a marker if everything is OK.
15809 If THIS-BUFFER is set, the outline path does not contain a file,
15811 (let* ((file (if this-buffer buffer-file-name
(pop path
)))
15812 (buffer (if this-buffer
(current-buffer) (find-file-noselect file
)))
15817 (unless buffer
(error "File not found :%s" file
))
15818 (with-current-buffer buffer
15819 (unless (derived-mode-p 'org-mode
)
15820 (error "Buffer %s needs to be in Org mode" buffer
))
15821 (org-with-wide-buffer
15822 (goto-char (point-min))
15823 (dolist (heading path
)
15824 (let ((re (format org-complex-heading-regexp-format
15825 (regexp-quote heading
)))
15827 (while (re-search-forward re end t
)
15828 (setq level
(- (match-end 1) (match-beginning 1)))
15829 (when (and (>= level lmin
) (<= level lmax
))
15830 (setq found
(match-beginning 0) flevel level cnt
(1+ cnt
))))
15832 (error "Heading not found on level %d: %s" lmax heading
))
15834 (error "Heading not unique on level %d: %s" lmax heading
))
15836 (setq lmin
(1+ flevel
) lmax
(+ lmin
(if org-odd-levels-only
1 0)))
15837 (setq end
(save-excursion (org-end-of-subtree t t
)))))
15838 (when (org-at-heading-p)
15839 (point-marker))))))
15841 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only
)
15842 "Find node HEADING in BUFFER.
15843 Return a marker to the heading if it was found, or nil if not.
15844 If POS-ONLY is set, return just the position instead of a marker.
15846 The heading text must match exact, but it may have a TODO keyword,
15847 a priority cookie and tags in the standard locations."
15848 (with-current-buffer (or buffer
(current-buffer))
15849 (org-with-wide-buffer
15850 (goto-char (point-min))
15851 (let (case-fold-search)
15852 (when (re-search-forward
15853 (format org-complex-heading-regexp-format
15854 (regexp-quote heading
)) nil t
)
15856 (match-beginning 0)
15857 (move-marker (make-marker) (match-beginning 0))))))))
15859 (defun org-find-exact-heading-in-directory (heading &optional dir
)
15860 "Find Org node headline HEADING in all \".org\" files in directory DIR.
15861 When the target headline is found, return a marker to this location."
15862 (let ((files (directory-files (or dir default-directory
)
15863 t
"\\`[^.#].*\\.org\\'"))
15866 (dolist (file files
)
15867 (message "trying %s" file
)
15868 (setq visiting
(org-find-base-buffer-visiting file
))
15869 (setq buffer
(or visiting
(find-file-noselect file
)))
15870 (setq m
(org-find-exact-headline-in-buffer
15872 (when (and (not m
) (not visiting
)) (kill-buffer buffer
))
15873 (and m
(throw 'found m
))))))
15875 (defun org-find-entry-with-id (ident)
15876 "Locate the entry that contains the ID property with exact value IDENT.
15877 IDENT can be a string, a symbol or a number, this function will search for
15878 the string representation of it.
15879 Return the position where this entry starts, or nil if there is no such entry."
15880 (interactive "sID: ")
15882 ((stringp ident
) ident
)
15883 ((symbolp ident
) (symbol-name ident
))
15884 ((numberp ident
) (number-to-string ident
))
15885 (t (error "IDENT %s must be a string, symbol or number" ident
)))))
15886 (org-with-wide-buffer (org-find-property "ID" id
))))
15890 (defvar org-last-changed-timestamp nil
)
15891 (defvar org-last-inserted-timestamp nil
15892 "The last time stamp inserted with `org-insert-time-stamp'.")
15894 (defun org-time-stamp (arg &optional inactive
)
15895 "Prompt for a date/time and insert a time stamp.
15897 If the user specifies a time like HH:MM or if this command is
15898 called with at least one prefix argument, the time stamp contains
15899 the date and the time. Otherwise, only the date is included.
15901 All parts of a date not specified by the user are filled in from
15902 the timestamp at point, if any, or the current date/time
15905 If there is already a timestamp at the cursor, it is replaced.
15907 With two universal prefix arguments, insert an active timestamp
15908 with the current time without prompting the user.
15910 When called from lisp, the timestamp is inactive if INACTIVE is
15914 ((org-at-date-range-p t
)
15915 (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
15916 ((org-at-timestamp-p 'lax
) (match-string 0))))
15917 ;; Default time is either the timestamp at point or today.
15918 ;; When entering a range, only the range start is considered.
15919 (default-time (if (not ts
) (current-time)
15920 (apply #'encode-time
(org-parse-time-string ts
))))
15921 (default-input (and ts
(org-get-compact-tod ts
)))
15923 (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts
)
15924 (match-string 0 ts
)))
15926 org-end-time-was-given
15928 (and (if (equal arg
'(16)) (current-time)
15929 ;; Preserve `this-command' and `last-command'.
15930 (let ((this-command this-command
)
15931 (last-command last-command
))
15933 arg
'totime nil nil default-time default-input
15937 (memq last-command
'(org-time-stamp org-time-stamp-inactive
))
15938 (memq this-command
'(org-time-stamp org-time-stamp-inactive
)))
15940 (org-insert-time-stamp time
(or org-time-was-given arg
) inactive
))
15942 ;; Make sure we're on a timestamp. When in the middle of a date
15943 ;; range, move arbitrarily to range end.
15944 (unless (org-at-timestamp-p 'lax
)
15945 (skip-chars-forward "-")
15946 (org-at-timestamp-p 'lax
))
15948 (setq org-last-changed-timestamp
15949 (org-insert-time-stamp
15950 time
(or org-time-was-given arg
)
15951 inactive nil nil
(list org-end-time-was-given
)))
15954 (insert " " repeater
)
15955 (setq org-last-changed-timestamp
15956 (concat (substring org-last-inserted-timestamp
0 -
1)
15957 " " repeater
">")))
15958 (message "Timestamp updated"))
15959 ((equal arg
'(16)) (org-insert-time-stamp time t inactive
))
15960 (t (org-insert-time-stamp
15961 time
(or org-time-was-given arg
) inactive nil nil
15962 (list org-end-time-was-given
))))))
15964 ;; FIXME: can we use this for something else, like computing time differences?
15965 (defun org-get-compact-tod (s)
15966 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s
)
15967 (let* ((t1 (match-string 1 s
))
15968 (h1 (string-to-number (match-string 2 s
)))
15969 (m1 (string-to-number (match-string 3 s
)))
15970 (t2 (and (match-end 4) (match-string 5 s
)))
15971 (h2 (and t2
(string-to-number (match-string 6 s
))))
15972 (m2 (and t2
(string-to-number (match-string 7 s
))))
15976 (setq dh
(- h2 h1
) dm
(- m2 m1
))
15977 (when (< dm
0) (setq dm
(+ dm
60) dh
(1- dh
)))
15978 (concat t1
"+" (number-to-string dh
)
15979 (and (/= 0 dm
) (format ":%02d" dm
)))))))
15981 (defun org-time-stamp-inactive (&optional arg
)
15982 "Insert an inactive time stamp.
15984 An inactive time stamp is enclosed in square brackets instead of angle
15985 brackets. It is inactive in the sense that it does not trigger agenda entries,
15986 does not link to the calendar and cannot be changed with the S-cursor keys.
15987 So these are more for recording a certain time/date.
15989 If the user specifies a time like HH:MM or if this command is called with
15990 at least one prefix argument, the time stamp contains the date and the time.
15991 Otherwise, only the date is included.
15993 When called with two universal prefix arguments, insert an active time stamp
15994 with the current time without prompting the user."
15996 (org-time-stamp arg
'inactive
))
15998 (defvar org-date-ovl
(make-overlay 1 1))
15999 (overlay-put org-date-ovl
'face
'org-date-selected
)
16000 (delete-overlay org-date-ovl
)
16002 (defvar org-ans1
) ; dynamically scoped parameter
16003 (defvar org-ans2
) ; dynamically scoped parameter
16005 (defvar org-plain-time-of-day-regexp
) ; defined below
16007 (defvar org-overriding-default-time nil
) ; dynamically scoped
16008 (defvar org-read-date-overlay nil
)
16009 (defvar org-dcst nil
) ; dynamically scoped
16010 (defvar org-read-date-history nil
)
16011 (defvar org-read-date-final-answer nil
)
16012 (defvar org-read-date-analyze-futurep nil
)
16013 (defvar org-read-date-analyze-forced-year nil
)
16014 (defvar org-read-date-inactive
)
16016 (defvar org-read-date-minibuffer-local-map
16017 (let* ((map (make-sparse-keymap)))
16018 (set-keymap-parent map minibuffer-local-map
)
16019 (org-defkey map
(kbd ".")
16020 (lambda () (interactive)
16021 ;; Are we at the beginning of the prompt?
16022 (if (looking-back "^[^:]+: "
16023 (let ((inhibit-field-text-motion t
))
16024 (line-beginning-position)))
16025 (org-eval-in-calendar '(calendar-goto-today))
16027 (org-defkey map
(kbd "C-.")
16028 (lambda () (interactive)
16029 (org-eval-in-calendar '(calendar-goto-today))))
16030 (org-defkey map
(kbd "M-S-<left>")
16031 (lambda () (interactive)
16032 (org-eval-in-calendar '(calendar-backward-month 1))))
16033 (org-defkey map
(kbd "ESC S-<left>")
16034 (lambda () (interactive)
16035 (org-eval-in-calendar '(calendar-backward-month 1))))
16036 (org-defkey map
(kbd "M-S-<right>")
16037 (lambda () (interactive)
16038 (org-eval-in-calendar '(calendar-forward-month 1))))
16039 (org-defkey map
(kbd "ESC S-<right>")
16040 (lambda () (interactive)
16041 (org-eval-in-calendar '(calendar-forward-month 1))))
16042 (org-defkey map
(kbd "M-S-<up>")
16043 (lambda () (interactive)
16044 (org-eval-in-calendar '(calendar-backward-year 1))))
16045 (org-defkey map
(kbd "ESC S-<up>")
16046 (lambda () (interactive)
16047 (org-eval-in-calendar '(calendar-backward-year 1))))
16048 (org-defkey map
(kbd "M-S-<down>")
16049 (lambda () (interactive)
16050 (org-eval-in-calendar '(calendar-forward-year 1))))
16051 (org-defkey map
(kbd "ESC S-<down>")
16052 (lambda () (interactive)
16053 (org-eval-in-calendar '(calendar-forward-year 1))))
16054 (org-defkey map
(kbd "S-<up>")
16055 (lambda () (interactive)
16056 (org-eval-in-calendar '(calendar-backward-week 1))))
16057 (org-defkey map
(kbd "S-<down>")
16058 (lambda () (interactive)
16059 (org-eval-in-calendar '(calendar-forward-week 1))))
16060 (org-defkey map
(kbd "S-<left>")
16061 (lambda () (interactive)
16062 (org-eval-in-calendar '(calendar-backward-day 1))))
16063 (org-defkey map
(kbd "S-<right>")
16064 (lambda () (interactive)
16065 (org-eval-in-calendar '(calendar-forward-day 1))))
16066 (org-defkey map
(kbd "!")
16067 (lambda () (interactive)
16068 (org-eval-in-calendar '(diary-view-entries))
16070 (org-defkey map
(kbd ">")
16071 (lambda () (interactive)
16072 (org-eval-in-calendar '(calendar-scroll-left 1))))
16073 (org-defkey map
(kbd "<")
16074 (lambda () (interactive)
16075 (org-eval-in-calendar '(calendar-scroll-right 1))))
16076 (org-defkey map
(kbd "C-v")
16077 (lambda () (interactive)
16078 (org-eval-in-calendar
16079 '(calendar-scroll-left-three-months 1))))
16080 (org-defkey map
(kbd "M-v")
16081 (lambda () (interactive)
16082 (org-eval-in-calendar
16083 '(calendar-scroll-right-three-months 1))))
16085 "Keymap for minibuffer commands when using `org-read-date'.")
16088 (defvar org-defdecode
)
16089 (defvar org-with-time
)
16091 (defvar calendar-setup
) ; Dynamically scoped.
16092 (defun org-read-date (&optional with-time to-time from-string prompt
16093 default-time default-input inactive
)
16094 "Read a date, possibly a time, and make things smooth for the user.
16095 The prompt will suggest to enter an ISO date, but you can also enter anything
16096 which will at least partially be understood by `parse-time-string'.
16097 Unrecognized parts of the date will default to the current day, month, year,
16098 hour and minute. If this command is called to replace a timestamp at point,
16099 or to enter the second timestamp of a range, the default time is taken
16100 from the existing stamp. Furthermore, the command prefers the future,
16101 so if you are giving a date where the year is not given, and the day-month
16102 combination is already past in the current year, it will assume you
16103 mean next year. For details, see the manual. A few examples:
16105 3-2-5 --> 2003-02-05
16106 feb 15 --> currentyear-02-15
16107 2/15 --> currentyear-02-15
16108 sep 12 9 --> 2009-09-12
16109 12:45 --> today 12:45
16110 22 sept 0:34 --> currentyear-09-22 0:34
16111 12 --> currentyear-currentmonth-12
16112 Fri --> nearest Friday after today
16113 -Tue --> last Tuesday
16116 Furthermore you can specify a relative date by giving, as the *first* thing
16117 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
16118 change in days weeks, months, years.
16119 With a single plus or minus, the date is relative to today. With a double
16120 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
16121 +4d --> four days from today
16122 +4 --> same as above
16123 +2w --> two weeks from today
16124 ++5 --> five days from default date
16126 The function understands only English month and weekday abbreviations.
16128 While prompting, a calendar is popped up - you can also select the
16129 date with the mouse (button 1). The calendar shows a period of three
16130 months. To scroll it to other months, use the keys `>' and `<'.
16131 If you don't like the calendar, turn it off with
16132 (setq org-read-date-popup-calendar nil)
16134 With optional argument TO-TIME, the date will immediately be converted
16135 to an internal time.
16136 With an optional argument WITH-TIME, the prompt will suggest to
16137 also insert a time. Note that when WITH-TIME is not set, you can
16138 still enter a time, and this function will inform the calling routine
16139 about this change. The calling routine may then choose to change the
16140 format used to insert the time stamp into the buffer to include the time.
16141 With optional argument FROM-STRING, read from this string instead from
16142 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
16143 the time/date that is used for everything that is not specified by the
16145 (require 'parse-time
)
16146 (let* ((org-with-time with-time
)
16147 (org-time-stamp-rounding-minutes
16148 (if (equal org-with-time
'(16))
16150 org-time-stamp-rounding-minutes
))
16151 (org-dcst org-display-custom-times
)
16152 (ct (org-current-time))
16153 (org-def (or org-overriding-default-time default-time ct
))
16154 (org-defdecode (decode-time org-def
))
16155 (cur-frame (selected-frame))
16156 (mouse-autoselect-window nil
) ; Don't let the mouse jump
16158 (and (eq calendar-setup
'calendar-only
) 'calendar-only
))
16159 (calendar-move-hook nil
)
16160 (calendar-view-diary-initially-flag nil
)
16161 (calendar-view-holidays-initially-flag nil
)
16162 ans
(org-ans0 "") org-ans1 org-ans2 final cal-frame
)
16163 ;; Rationalize `org-def' and `org-defdecode', if required.
16164 (when (< (nth 2 org-defdecode
) org-extend-today-until
)
16165 (setf (nth 2 org-defdecode
) -
1)
16166 (setf (nth 1 org-defdecode
) 59)
16167 (setq org-def
(apply #'encode-time org-defdecode
))
16168 (setq org-defdecode
(decode-time org-def
)))
16169 (let* ((timestr (format-time-string
16170 (if org-with-time
"%Y-%m-%d %H:%M" "%Y-%m-%d")
16172 (prompt (concat (if prompt
(concat prompt
" ") "")
16173 (format "Date+time [%s]: " timestr
))))
16175 (from-string (setq ans from-string
))
16176 (org-read-date-popup-calendar
16178 (save-window-excursion
16180 (when (eq calendar-setup
'calendar-only
)
16182 (window-frame (get-buffer-window "*Calendar*" 'visible
)))
16183 (select-frame cal-frame
))
16184 (org-eval-in-calendar '(setq cursor-type nil
) t
)
16187 (calendar-forward-day (- (time-to-days org-def
)
16188 (calendar-absolute-from-gregorian
16189 (calendar-current-date))))
16190 (org-eval-in-calendar nil t
)
16191 (let* ((old-map (current-local-map))
16192 (map (copy-keymap calendar-mode-map
))
16193 (minibuffer-local-map
16194 (copy-keymap org-read-date-minibuffer-local-map
)))
16195 (org-defkey map
(kbd "RET") 'org-calendar-select
)
16196 (org-defkey map
[mouse-1
] 'org-calendar-select-mouse
)
16197 (org-defkey map
[mouse-2
] 'org-calendar-select-mouse
)
16200 (use-local-map map
)
16201 (setq org-read-date-inactive inactive
)
16202 (add-hook 'post-command-hook
'org-read-date-display
)
16204 (read-string prompt
16206 'org-read-date-history
16208 ;; org-ans0: from prompt
16209 ;; org-ans1: from mouse click
16210 ;; org-ans2: from calendar motion
16212 (concat org-ans0
" " (or org-ans1 org-ans2
))))
16213 (remove-hook 'post-command-hook
'org-read-date-display
)
16214 (use-local-map old-map
)
16215 (when org-read-date-overlay
16216 (delete-overlay org-read-date-overlay
)
16217 (setq org-read-date-overlay nil
)))))
16218 (bury-buffer "*Calendar*")
16220 (delete-frame cal-frame
)
16221 (select-frame-set-input-focus cur-frame
))))))
16223 (t ; Naked prompt only
16225 (setq ans
(read-string prompt default-input
16226 'org-read-date-history timestr
))
16227 (when org-read-date-overlay
16228 (delete-overlay org-read-date-overlay
)
16229 (setq org-read-date-overlay nil
))))))
16231 (setq final
(org-read-date-analyze ans org-def org-defdecode
))
16233 (when org-read-date-analyze-forced-year
16234 (message "Year was forced into %s"
16235 (if org-read-date-force-compatible-dates
16236 "compatible range (1970-2037)"
16237 "range representable on this machine"))
16240 ;; One round trip to get rid of 34th of August and stuff like that....
16241 (setq final
(decode-time (apply 'encode-time final
)))
16243 (setq org-read-date-final-answer ans
)
16246 (apply 'encode-time final
)
16247 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
16248 (format "%04d-%02d-%02d %02d:%02d"
16249 (nth 5 final
) (nth 4 final
) (nth 3 final
)
16250 (nth 2 final
) (nth 1 final
))
16251 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
16253 (defun org-read-date-display ()
16254 "Display the current date prompt interpretation in the minibuffer."
16255 (when org-read-date-display-live
16256 (when org-read-date-overlay
16257 (delete-overlay org-read-date-overlay
))
16258 (when (minibufferp (current-buffer))
16261 (while (not (equal (buffer-substring
16262 (max (point-min) (- (point) 4)) (point))
16265 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
16266 " " (or org-ans1 org-ans2
)))
16267 (org-end-time-was-given nil
)
16268 (f (org-read-date-analyze ans org-def org-defdecode
))
16270 org-time-stamp-custom-formats
16271 org-time-stamp-formats
))
16272 (fmt (if (or org-with-time
16273 (and (boundp 'org-time-was-given
) org-time-was-given
))
16276 (txt (format-time-string fmt
(apply 'encode-time f
)))
16277 (txt (if org-read-date-inactive
(concat "[" (substring txt
1 -
1) "]") txt
))
16278 (txt (concat "=> " txt
)))
16279 (when (and org-end-time-was-given
16280 (string-match org-plain-time-of-day-regexp txt
))
16281 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
16282 org-end-time-was-given
16283 (substring txt
(match-end 0)))))
16284 (when org-read-date-analyze-futurep
16285 (setq txt
(concat txt
" (=>F)")))
16286 (setq org-read-date-overlay
16287 (make-overlay (1- (point-at-eol)) (point-at-eol)))
16288 (org-overlay-display org-read-date-overlay txt
'secondary-selection
)))))
16290 (defun org-read-date-analyze (ans def defdecode
)
16291 "Analyze the combined answer of the date prompt."
16292 ;; FIXME: cleanup and comment
16293 ;; Pass `current-time' result to `decode-time' (instead of calling
16294 ;; without arguments) so that only `current-time' has to be
16295 ;; overridden in tests.
16296 (let ((org-def def
)
16297 (org-defdecode defdecode
)
16298 (nowdecode (decode-time (current-time)))
16299 delta deltan deltaw deltadef year month day
16300 hour minute second wday pm h2 m2 tl wday1
16301 iso-year iso-weekday iso-week iso-date futurep kill-year
)
16302 (setq org-read-date-analyze-futurep nil
16303 org-read-date-analyze-forced-year nil
)
16304 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans
)
16307 (when (setq delta
(org-read-date-get-relative ans
(current-time) org-def
))
16308 (setq ans
(replace-match "" t t ans
)
16310 deltaw
(nth 1 delta
)
16311 deltadef
(nth 2 delta
)))
16313 ;; Check if there is an iso week date in there. If yes, store the
16314 ;; info and postpone interpreting it until the rest of the parsing
16316 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans
)
16317 (setq iso-year
(when (match-end 1)
16318 (org-small-year-to-year
16319 (string-to-number (match-string 1 ans
))))
16320 iso-weekday
(when (match-end 3)
16321 (string-to-number (match-string 3 ans
)))
16322 iso-week
(string-to-number (match-string 2 ans
)))
16323 (setq ans
(replace-match "" t t ans
)))
16325 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
16326 (when (string-match
16327 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
16328 (setq year
(if (match-end 2)
16329 (string-to-number (match-string 2 ans
))
16330 (progn (setq kill-year t
)
16331 (string-to-number (format-time-string "%Y"))))
16332 month
(string-to-number (match-string 3 ans
))
16333 day
(string-to-number (match-string 4 ans
)))
16334 (setq year
(org-small-year-to-year year
))
16335 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
16338 ;; Help matching dotted european dates
16339 (when (string-match
16340 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans
)
16341 (setq year
(if (match-end 3) (string-to-number (match-string 3 ans
))
16343 (string-to-number (format-time-string "%Y")))
16344 day
(string-to-number (match-string 1 ans
))
16345 month
(string-to-number (match-string 2 ans
))
16346 ans
(replace-match (format "%04d-%02d-%02d" year month day
)
16349 ;; Help matching american dates, like 5/30 or 5/30/7
16350 (when (string-match
16351 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans
)
16352 (setq year
(if (match-end 4)
16353 (string-to-number (match-string 4 ans
))
16354 (progn (setq kill-year t
)
16355 (string-to-number (format-time-string "%Y"))))
16356 month
(string-to-number (match-string 1 ans
))
16357 day
(string-to-number (match-string 2 ans
)))
16358 (setq year
(org-small-year-to-year year
))
16359 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
16361 ;; Help matching am/pm times, because `parse-time-string' does not do that.
16362 ;; If there is a time with am/pm, and *no* time without it, we convert
16363 ;; so that matching will be successful.
16364 (cl-loop for i from
1 to
2 do
; twice, for end time as well
16365 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
16366 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
16367 (setq hour
(string-to-number (match-string 1 ans
))
16368 minute
(if (match-end 3)
16369 (string-to-number (match-string 3 ans
))
16372 (string-to-char (downcase (match-string 4 ans
)))))
16373 (if (and (= hour
12) (not pm
))
16375 (when (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
16376 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
16379 ;; Check if a time range is given as a duration
16380 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
16381 (setq hour
(string-to-number (match-string 1 ans
))
16382 h2
(+ hour
(string-to-number (match-string 3 ans
)))
16383 minute
(string-to-number (match-string 2 ans
))
16384 m2
(+ minute
(if (match-end 5) (string-to-number
16385 (match-string 5 ans
))0)))
16386 (when (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
16387 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
)
16390 ;; Check if there is a time range
16391 (when (boundp 'org-end-time-was-given
)
16392 (setq org-time-was-given nil
)
16393 (when (and (string-match org-plain-time-of-day-regexp ans
)
16395 (setq org-end-time-was-given
(match-string 8 ans
))
16396 (setq ans
(concat (substring ans
0 (match-beginning 7))
16397 (substring ans
(match-end 7))))))
16399 (setq tl
(parse-time-string ans
)
16400 day
(or (nth 3 tl
) (nth 3 org-defdecode
))
16403 ((not org-read-date-prefer-future
) (nth 4 org-defdecode
))
16404 ;; Day was specified. Make sure DAY+MONTH
16405 ;; combination happens in the future.
16408 (if (< day
(nth 3 nowdecode
)) (1+ (nth 4 nowdecode
))
16409 (nth 4 nowdecode
)))
16410 (t (nth 4 org-defdecode
)))
16412 (cond ((and (not kill-year
) (nth 5 tl
)))
16413 ((not org-read-date-prefer-future
) (nth 5 org-defdecode
))
16414 ;; Month was guessed in the future and is at least
16415 ;; equal to NOWDECODE's. Fix year accordingly.
16417 (if (or (> month
(nth 4 nowdecode
))
16418 (>= day
(nth 3 nowdecode
)))
16420 (1+ (nth 5 nowdecode
))))
16421 ;; Month was specified. Make sure MONTH+YEAR
16422 ;; combination happens in the future.
16425 (cond ((> month
(nth 4 nowdecode
)) (nth 5 nowdecode
))
16426 ((< month
(nth 4 nowdecode
)) (1+ (nth 5 nowdecode
)))
16427 ((< day
(nth 3 nowdecode
)) (1+ (nth 5 nowdecode
)))
16428 (t (nth 5 nowdecode
))))
16429 (t (nth 5 org-defdecode
)))
16430 hour
(or (nth 2 tl
) (nth 2 org-defdecode
))
16431 minute
(or (nth 1 tl
) (nth 1 org-defdecode
))
16432 second
(or (nth 0 tl
) 0)
16435 (when (and (eq org-read-date-prefer-future
'time
)
16436 (not (nth 3 tl
)) (not (nth 4 tl
)) (not (nth 5 tl
))
16437 (equal day
(nth 3 nowdecode
))
16438 (equal month
(nth 4 nowdecode
))
16439 (equal year
(nth 5 nowdecode
))
16441 (or (< (nth 2 tl
) (nth 2 nowdecode
))
16442 (and (= (nth 2 tl
) (nth 2 nowdecode
))
16444 (< (nth 1 tl
) (nth 1 nowdecode
)))))
16448 ;; Special date definitions below
16451 ;; There was an iso week
16454 (setq year
(or iso-year year
)
16455 day
(or iso-weekday wday
1)
16456 wday nil
; to make sure that the trigger below does not match
16457 iso-date
(calendar-gregorian-from-absolute
16458 (calendar-iso-to-absolute
16459 (list iso-week day year
))))
16460 ; FIXME: Should we also push ISO weeks into the future?
16461 ; (when (and org-read-date-prefer-future
16463 ; (< (calendar-absolute-from-gregorian iso-date)
16464 ; (time-to-days (current-time))))
16465 ; (setq year (1+ year)
16466 ; iso-date (calendar-gregorian-from-absolute
16467 ; (calendar-iso-to-absolute
16468 ; (list iso-week day year)))))
16469 (setq month
(car iso-date
)
16470 year
(nth 2 iso-date
)
16471 day
(nth 1 iso-date
)))
16475 ;; Pass `current-time' result to `decode-time' (instead of
16476 ;; calling without arguments) so that only `current-time' has
16477 ;; to be overridden in tests.
16478 (let ((now (decode-time (current-time))))
16479 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
16480 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
16481 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
16482 ((equal deltaw
"m") (setq month
(+ month deltan
)))
16483 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
16484 ((and wday
(not (nth 3 tl
)))
16485 ;; Weekday was given, but no day, so pick that day in the week
16486 ;; on or after the derived date.
16487 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
16488 (unless (equal wday wday1
)
16489 (setq day
(+ day
(%
(- wday wday1 -
7) 7))))))
16490 (when (and (boundp 'org-time-was-given
)
16492 (setq org-time-was-given t
))
16493 (when (< year
100) (setq year
(+ 2000 year
)))
16494 ;; Check of the date is representable
16495 (if org-read-date-force-compatible-dates
16497 (when (< year
1970)
16498 (setq year
1970 org-read-date-analyze-forced-year t
))
16499 (when (> year
2037)
16500 (setq year
2037 org-read-date-analyze-forced-year t
)))
16501 (condition-case nil
16502 (ignore (encode-time second minute hour day month year
))
16504 (setq year
(nth 5 org-defdecode
))
16505 (setq org-read-date-analyze-forced-year t
))))
16506 (setq org-read-date-analyze-futurep futurep
)
16507 (list second minute hour day month year
)))
16509 (defvar parse-time-weekdays
)
16510 (defun org-read-date-get-relative (s today default
)
16511 "Check string S for special relative date string.
16512 TODAY and DEFAULT are internal times, for today and for a default.
16513 Return shift list (N what def-flag)
16514 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
16515 N is the number of WHATs to shift.
16516 DEF-FLAG is t when a double ++ or -- indicates shift relative to
16517 the DEFAULT date rather than TODAY."
16518 (require 'parse-time
)
16522 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
16524 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
16525 "\\([ \t]\\|$\\)") s
)
16526 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
16527 (let* ((dir (if (> (match-end 1) (match-beginning 1))
16528 (string-to-char (substring (match-string 1 s
) -
1))
16530 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
16531 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
16532 (what (if (match-end 3) (match-string 3 s
) "d"))
16533 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
16534 (date (if rel default today
))
16535 (wday (nth 6 (decode-time date
)))
16539 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
16540 (when (= delta
0) (setq delta
7))
16542 (setq delta
(- delta
7))
16543 (when (= delta
0) (setq delta -
7)))
16544 (when (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
16545 (list delta
"d" rel
))
16546 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
16548 (defun org-order-calendar-date-args (arg1 arg2 arg3
)
16549 "Turn a user-specified date into the internal representation.
16550 The internal representation needed by the calendar is (month day year).
16551 This is a wrapper to handle the brain-dead convention in calendar that
16552 user function argument order change dependent on argument order."
16553 (pcase calendar-date-style
16554 (`american
(list arg1 arg2 arg3
))
16555 (`european
(list arg2 arg1 arg3
))
16556 (`iso
(list arg2 arg3 arg1
))))
16558 (defun org-eval-in-calendar (form &optional keepdate
)
16559 "Eval FORM in the calendar window and return to current window.
16560 Unless KEEPDATE is non-nil, update `org-ans2' to the cursor date."
16561 (let ((sf (selected-frame))
16562 (sw (selected-window)))
16563 (select-window (get-buffer-window "*Calendar*" t
))
16565 (when (and (not keepdate
) (calendar-cursor-to-date))
16566 (let* ((date (calendar-cursor-to-date))
16567 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
16568 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
16569 (move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
16571 (select-frame-set-input-focus sf
)))
16573 (defun org-calendar-select ()
16574 "Return to `org-read-date' with the date currently selected.
16575 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16577 (when (calendar-cursor-to-date)
16578 (let* ((date (calendar-cursor-to-date))
16579 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
16580 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
16581 (when (active-minibuffer-window) (exit-minibuffer))))
16583 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
16584 "Insert a date stamp for the date given by the internal TIME.
16585 See `format-time-string' for the format of TIME.
16586 WITH-HM means use the stamp format that includes the time of the day.
16587 INACTIVE means use square brackets instead of angular ones, so that the
16588 stamp will not contribute to the agenda.
16589 PRE and POST are optional strings to be inserted before and after the
16591 The command returns the inserted time stamp."
16592 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
16594 (when inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
16595 (insert-before-markers (or pre
""))
16596 (when (listp extra
)
16597 (setq extra
(car extra
))
16598 (if (and (stringp extra
)
16599 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
16600 (setq extra
(format "-%02d:%02d"
16601 (string-to-number (match-string 1 extra
))
16602 (string-to-number (match-string 2 extra
))))
16605 (setq fmt
(concat (substring fmt
0 -
1) extra
(substring fmt -
1))))
16606 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
16607 (insert-before-markers (or post
""))
16608 (setq org-last-inserted-timestamp stamp
)))
16610 (defun org-toggle-time-stamp-overlays ()
16611 "Toggle the use of custom time stamp formats."
16613 (setq org-display-custom-times
(not org-display-custom-times
))
16614 (unless org-display-custom-times
16615 (let ((p (point-min)) (bmp (buffer-modified-p)))
16616 (while (setq p
(next-single-property-change p
'display
))
16617 (when (and (get-text-property p
'display
)
16618 (eq (get-text-property p
'face
) 'org-date
))
16619 (remove-text-properties
16620 p
(setq p
(next-single-property-change p
'display
))
16622 (set-buffer-modified-p bmp
)))
16623 (org-restart-font-lock)
16624 (setq org-table-may-need-update t
)
16625 (if org-display-custom-times
16626 (message "Time stamps are overlaid with custom format")
16627 (message "Time stamp overlays removed")))
16629 (defun org-display-custom-time (beg end
)
16630 "Overlay modified time stamp format over timestamp between BEG and END."
16631 (let* ((ts (buffer-substring beg end
))
16632 t1 with-hm tf time str
(off 0))
16634 (setq t1
(org-parse-time-string ts t
))
16635 (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts
)
16636 (setq off
(- (match-end 0) (match-beginning 0)))))
16637 (setq end
(- end off
))
16638 (setq with-hm
(and (nth 1 t1
) (nth 2 t1
))
16639 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
16640 time
(org-fix-decoded-time t1
)
16642 (format-time-string
16643 (substring tf
1 -
1) (apply 'encode-time time
))
16644 nil
'mouse-face
'highlight
))
16645 (put-text-property beg end
'display str
)))
16647 (defun org-fix-decoded-time (time)
16648 "Set 0 instead of nil for the first 6 elements of time.
16649 Don't touch the rest."
16651 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
16653 (defun org-time-stamp-to-now (timestamp-string &optional seconds
)
16654 "Difference between TIMESTAMP-STRING and now in days.
16655 If SECONDS is non-nil, return the difference in seconds."
16656 (let ((fdiff (if seconds
#'float-time
#'time-to-days
)))
16657 (- (funcall fdiff
(org-time-string-to-time timestamp-string
))
16658 (funcall fdiff
(current-time)))))
16660 (defun org-deadline-close-p (timestamp-string &optional ndays
)
16661 "Is the time in TIMESTAMP-STRING close to the current date?"
16662 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
16663 (and (<= (org-time-stamp-to-now timestamp-string
) ndays
)
16664 (not (org-entry-is-done-p))))
16666 (defun org-get-wdays (ts &optional delay zero-delay
)
16667 "Get the deadline lead time appropriate for timestring TS.
16668 When DELAY is non-nil, get the delay time for scheduled items
16669 instead of the deadline lead time. When ZERO-DELAY is non-nil
16670 and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
16671 don't try to find the delay cookie in the scheduled timestamp."
16672 (let ((tv (if delay org-scheduled-delay-days
16673 org-deadline-warning-days
)))
16675 ((or (and delay
(< tv
0))
16676 (and delay zero-delay
(<= tv
0))
16677 (and (not delay
) (<= tv
0)))
16678 ;; Enforce this value no matter what
16680 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts
)
16681 ;; lead time is specified.
16682 (floor (* (string-to-number (match-string 1 ts
))
16683 (cdr (assoc (match-string 2 ts
)
16684 '(("d" .
1) ("w" .
7)
16685 ("m" .
30.4) ("y" .
365.25)
16686 ("h" .
0.041667)))))))
16687 ;; go for the default.
16690 (defun org-calendar-select-mouse (ev)
16691 "Return to `org-read-date' with the date currently selected.
16692 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
16694 (mouse-set-point ev
)
16695 (when (calendar-cursor-to-date)
16696 (let* ((date (calendar-cursor-to-date))
16697 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
16698 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
16699 (when (active-minibuffer-window) (exit-minibuffer))))
16701 (defun org-check-deadlines (ndays)
16702 "Check if there are any deadlines due or past due.
16703 A deadline is considered due if it happens within `org-deadline-warning-days'
16704 days from today's date. If the deadline appears in an entry marked DONE,
16705 it is not shown. A numeric prefix argument NDAYS can be used to test that
16706 many days. If the prefix is a raw `\\[universal-argument]', all deadlines \
16709 (let* ((org-warn-days
16711 ((equal ndays
'(4)) 100000)
16712 (ndays (prefix-numeric-value ndays
))
16713 (t (abs org-deadline-warning-days
))))
16714 (case-fold-search nil
)
16715 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
16717 (lambda () (org-deadline-close-p (match-string 1) org-warn-days
))))
16718 (message "%d deadlines past-due or due within %d days"
16719 (org-occur regexp nil callback
)
16722 (defsubst org-re-timestamp
(type)
16723 "Return a regexp for timestamp TYPE.
16724 Allowed values for TYPE are:
16726 all: all timestamps
16727 active: only active timestamps (<...>)
16728 inactive: only inactive timestamps ([...])
16729 scheduled: only scheduled timestamps
16730 deadline: only deadline timestamps
16731 closed: only closed time-stamps
16733 When TYPE is nil, fall back on returning a regexp that matches
16734 both scheduled and deadline timestamps."
16736 (all org-ts-regexp-both
)
16737 (active org-ts-regexp
)
16738 (inactive org-ts-regexp-inactive
)
16739 (scheduled org-scheduled-time-regexp
)
16740 (deadline org-deadline-time-regexp
)
16741 (closed org-closed-time-regexp
)
16744 (regexp-opt (list org-deadline-string org-scheduled-string
))
16745 " *<\\([^>]+\\)>"))))
16747 (defun org-check-before-date (d)
16748 "Check if there are deadlines or scheduled entries before date D."
16749 (interactive (list (org-read-date)))
16750 (let* ((case-fold-search nil
)
16751 (regexp (org-re-timestamp org-ts-type
))
16752 (ts-type org-ts-type
)
16755 (let ((match (match-string 1)))
16756 (and (if (memq ts-type
'(active inactive all
))
16757 (eq (org-element-type (save-excursion
16759 (org-element-context)))
16761 (org-at-planning-p))
16763 (org-time-string-to-time match
)
16764 (org-time-string-to-time d
)))))))
16765 (message "%d entries before %s"
16766 (org-occur regexp nil callback
)
16769 (defun org-check-after-date (d)
16770 "Check if there are deadlines or scheduled entries after date D."
16771 (interactive (list (org-read-date)))
16772 (let* ((case-fold-search nil
)
16773 (regexp (org-re-timestamp org-ts-type
))
16774 (ts-type org-ts-type
)
16777 (let ((match (match-string 1)))
16778 (and (if (memq ts-type
'(active inactive all
))
16779 (eq (org-element-type (save-excursion
16781 (org-element-context)))
16783 (org-at-planning-p))
16785 (org-time-string-to-time match
)
16786 (org-time-string-to-time d
))))))))
16787 (message "%d entries after %s"
16788 (org-occur regexp nil callback
)
16791 (defun org-check-dates-range (start-date end-date
)
16792 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
16793 (interactive (list (org-read-date nil nil nil
"Range starts")
16794 (org-read-date nil nil nil
"Range end")))
16795 (let ((case-fold-search nil
)
16796 (regexp (org-re-timestamp org-ts-type
))
16798 (let ((type org-ts-type
))
16800 (let ((match (match-string 1)))
16802 (if (memq type
'(active inactive all
))
16803 (eq (org-element-type (save-excursion
16805 (org-element-context)))
16807 (org-at-planning-p))
16809 (org-time-string-to-time match
)
16810 (org-time-string-to-time start-date
)))
16812 (org-time-string-to-time match
)
16813 (org-time-string-to-time end-date
))))))))
16814 (message "%d entries between %s and %s"
16815 (org-occur regexp nil callback
) start-date end-date
)))
16817 (defun org-evaluate-time-range (&optional to-buffer
)
16818 "Evaluate a time range by computing the difference between start and end.
16819 Normally the result is just printed in the echo area, but with prefix arg
16820 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
16821 If the time range is actually in a table, the result is inserted into the
16823 For time difference computation, a year is assumed to be exactly 365
16824 days in order to avoid rounding problems."
16827 (org-clock-update-time-maybe)
16829 (unless (org-at-date-range-p t
)
16830 (goto-char (point-at-bol))
16831 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
16832 (unless (org-at-date-range-p t
)
16833 (user-error "Not at a time-stamp range, and none found in current line")))
16834 (let* ((ts1 (match-string 1))
16835 (ts2 (match-string 2))
16836 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
16837 (match-end (match-end 0))
16838 (time1 (org-time-string-to-time ts1
))
16839 (time2 (org-time-string-to-time ts2
))
16840 (t1 (float-time time1
))
16841 (t2 (float-time time2
))
16842 (diff (abs (- t2 t1
)))
16843 (negative (< (- t2 t1
) 0))
16844 ;; (ys (floor (* 365 24 60 60)))
16847 (fy "%dy %dd %02d:%02d")
16849 (fd "%dd %02d:%02d")
16854 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16856 d
(floor (/ diff ds
)) diff
(mod diff ds
)
16857 h
(floor (/ diff hs
)) diff
(mod diff hs
)
16858 m
(floor (/ diff
60)))
16859 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
16861 d
(floor (+ (/ diff ds
) 0.5))
16863 (if (not to-buffer
)
16864 (message "%s" (org-make-tdiff-string y d h m
))
16865 (if (org-at-table-p)
16867 (goto-char match-end
)
16869 (and (looking-at " *|") (goto-char (match-end 0))))
16870 (goto-char match-end
))
16872 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
16873 (replace-match ""))
16874 (when negative
(insert " -"))
16875 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
16876 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
16877 (insert " " (format fh h m
))))
16878 (when align
(org-table-align))
16879 (message "Time difference inserted")))))
16881 (defun org-make-tdiff-string (y d h m
)
16885 (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " "))
16888 (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " "))
16891 (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " "))
16894 (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " "))
16896 (apply 'format fmt
(nreverse l
))))
16898 (defun org-time-string-to-time (s)
16899 "Convert timestamp string S into internal time."
16900 (apply #'encode-time
(org-parse-time-string s
)))
16902 (defun org-time-string-to-seconds (s)
16903 "Convert a timestamp string S into a number of seconds."
16904 (float-time (org-time-string-to-time s
)))
16906 (org-define-error 'org-diary-sexp-no-match
"Unable to match diary sexp")
16908 (defun org-time-string-to-absolute (s &optional daynr prefer buffer pos
)
16909 "Convert time stamp S to an absolute day number.
16911 If DAYNR in non-nil, and there is a specifier for a cyclic time
16912 stamp, get the closest date to DAYNR. If PREFER is
16913 `past' (respectively `future') return a date past (respectively
16914 after) or equal to DAYNR.
16916 POS is the location of time stamp S, as a buffer position in
16919 Diary sexp timestamps are matched against DAYNR, when non-nil.
16920 If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
16923 ((string-match "\\`%%\\((.*)\\)" s
)
16924 ;; Sexp timestamp: try to match DAYNR, if available, since we're
16925 ;; only able to match individual dates. If it fails, raise an
16928 (org-diary-sexp-entry
16929 (match-string 1 s
) "" (calendar-gregorian-from-absolute daynr
)))
16931 (signal 'org-diary-sexp-no-match
(list s
))))
16932 (daynr (org-closest-date s daynr prefer
))
16934 (condition-case errdata
16935 (apply #'encode-time
(org-parse-time-string s
))
16936 (error (error "Bad timestamp `%s'%s\nError was: %s"
16938 (if (not (and buffer pos
)) ""
16939 (format-message " at %d in buffer `%s'" pos buffer
))
16940 (cdr errdata
))))))))
16942 (defun org-days-to-iso-week (days)
16943 "Return the iso week number."
16945 (car (calendar-iso-from-absolute days
)))
16947 (defun org-small-year-to-year (year)
16948 "Convert 2-digit years into 4-digit years.
16949 YEAR is expanded into one of the 30 next years, if possible, or
16950 into a past one. Any year larger than 99 is returned unchanged."
16951 (if (>= year
100) year
16952 (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
16953 (century (/ current
100))
16954 (offset (- year
(% current
100))))
16955 (cond ((> offset
30) (+ (* (1- century
) 100) year
))
16956 ((> offset -
70) (+ (* century
100) year
))
16957 (t (+ (* (1+ century
) 100) year
))))))
16959 (defun org-time-from-absolute (d)
16960 "Return the time corresponding to date D.
16961 D may be an absolute day number, or a calendar-type list (month day year)."
16962 (when (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
16963 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
16965 (defvar org-agenda-current-date
)
16966 (defun org-calendar-holiday ()
16967 "List of holidays, for Diary display in Org mode."
16968 (require 'holidays
)
16969 (let ((hl (calendar-check-holidays org-agenda-current-date
)))
16970 (and hl
(mapconcat #'identity hl
"; "))))
16972 (defun org-diary-sexp-entry (sexp entry d
)
16973 "Process a SEXP diary ENTRY for date D."
16974 (require 'diary-lib
)
16975 ;; `org-anniversary' and alike expect ENTRY and DATE to be bound
16977 (let* ((sexp `(let ((entry ,entry
)
16979 ,(car (read-from-string sexp
))))
16980 (result (if calendar-debug-sexp
(eval sexp
)
16981 (condition-case nil
16985 (message "Bad sexp at line %d in %s: %s"
16987 (buffer-file-name) sexp
)
16989 (cond ((stringp result
) (split-string result
"; "))
16990 ((and (consp result
)
16991 (not (consp (cdr result
)))
16992 (stringp (cdr result
))) (cdr result
))
16993 ((and (consp result
)
16994 (stringp (car result
))) result
)
16997 (defun org-diary-to-ical-string (frombuf)
16998 "Get iCalendar entries from diary entries in buffer FROMBUF.
16999 This uses the icalendar.el library."
17000 (let* ((tmpdir temporary-file-directory
)
17001 (tmpfile (make-temp-name
17002 (expand-file-name "orgics" tmpdir
)))
17004 (with-current-buffer frombuf
17005 (icalendar-export-region (point-min) (point-max) tmpfile
)
17006 (setq buf
(find-buffer-visiting tmpfile
))
17008 (goto-char (point-min))
17009 (when (re-search-forward "^BEGIN:VEVENT" nil t
)
17010 (setq b
(match-beginning 0)))
17011 (goto-char (point-max))
17012 (when (re-search-backward "^END:VEVENT" nil t
)
17013 (setq e
(match-end 0)))
17014 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
17016 (delete-file tmpfile
)
17019 (defun org-closest-date (start current prefer
)
17020 "Return closest date to CURRENT starting from START.
17022 CURRENT and START are both time stamps.
17024 When PREFER is `past', return a date that is either CURRENT or
17025 past. When PREFER is `future', return a date that is either
17028 Only time stamps with a repeater are modified. Any other time
17029 stamp stay unchanged. In any case, return value is an absolute
17031 (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start
))
17032 ;; No repeater. Do not shift time stamp.
17033 (time-to-days (apply #'encode-time
(org-parse-time-string start
)))
17034 (let ((value (string-to-number (match-string 1 start
)))
17035 (type (match-string 2 start
)))
17037 ;; Repeater with a 0-value is considered as void.
17038 (time-to-days (apply #'encode-time
(org-parse-time-string start
)))
17039 (let* ((base (org-date-to-gregorian start
))
17040 (target (org-date-to-gregorian current
))
17041 (sday (calendar-absolute-from-gregorian base
))
17042 (cday (calendar-absolute-from-gregorian target
))
17044 ;; If START is already past CURRENT, just return START.
17045 (if (<= cday sday
) sday
17046 ;; Compute closest date before (N1) and closest date past
17050 (let ((missing-hours
17051 (mod (+ (- (* 24 (- cday sday
))
17052 (nth 2 (org-parse-time-string start
)))
17053 org-extend-today-until
)
17055 (setf n1
(if (= missing-hours
0) cday
17056 (- cday
(1+ (/ missing-hours
24)))))
17057 (setf n2
(+ cday
(/ (- value missing-hours
) 24)))))
17059 (let ((value (if (equal type
"w") (* 7 value
) value
)))
17060 (setf n1
(+ sday
(* value
(/ (- cday sday
) value
))))
17061 (setf n2
(+ n1 value
))))
17065 ;; Add N months to gregorian date D, i.e.,
17066 ;; a list (MONTH DAY YEAR). Return a valid
17068 (let ((m (+ (nth 0 d
) n
)))
17071 (+ (/ m
12) (nth 2 d
))))))
17072 (months ; Complete months to TARGET.
17073 (* (/ (+ (* 12 (- (nth 2 target
) (nth 2 base
)))
17074 (- (nth 0 target
) (nth 0 base
))
17075 ;; If START's day is greater than
17076 ;; TARGET's, remove incomplete month.
17077 (if (> (nth 1 target
) (nth 1 base
)) 0 -
1))
17080 (before (funcall add-months base months
)))
17081 (setf n1
(calendar-absolute-from-gregorian before
))
17083 (calendar-absolute-from-gregorian
17084 (funcall add-months before value
)))))
17086 (let* ((d (nth 1 base
))
17089 (years ; Complete years to TARGET.
17090 (* (/ (- (nth 2 target
)
17092 ;; If START's month and day are
17093 ;; greater than TARGET's, remove
17094 ;; incomplete year.
17095 (if (or (> (nth 0 target
) m
)
17096 (and (= (nth 0 target
) m
)
17097 (> (nth 1 target
) d
)))
17102 (before (list m d
(+ y years
))))
17103 (setf n1
(calendar-absolute-from-gregorian before
))
17104 (setf n2
(calendar-absolute-from-gregorian
17105 (list m d
(+ (nth 2 before
) value
)))))))
17106 ;; Handle PREFER parameter, if any.
17108 ((eq prefer
'past
) (if (= cday n2
) n2 n1
))
17109 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
17110 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))))))))
17112 (defun org-date-to-gregorian (d)
17113 "Turn any specification of date D into a Gregorian date for the calendar."
17114 (cond ((integerp d
) (calendar-gregorian-from-absolute d
))
17115 ((and (listp d
) (= (length d
) 3)) d
)
17117 (let ((d (org-parse-time-string d
)))
17118 (list (nth 4 d
) (nth 3 d
) (nth 5 d
))))
17119 ((listp d
) (list (nth 4 d
) (nth 3 d
) (nth 5 d
)))))
17121 (defun org-parse-time-string (s &optional nodefault
)
17122 "Parse the standard Org time string.
17124 This should be a lot faster than the normal `parse-time-string'.
17126 If time is not given, defaults to 0:00. However, with optional
17127 NODEFAULT, hour and minute fields will be nil if not given."
17128 (cond ((string-match org-ts-regexp0 s
)
17130 (when (or (match-beginning 8) (not nodefault
))
17131 (string-to-number (or (match-string 8 s
) "0")))
17132 (when (or (match-beginning 7) (not nodefault
))
17133 (string-to-number (or (match-string 7 s
) "0")))
17134 (string-to-number (match-string 4 s
))
17135 (string-to-number (match-string 3 s
))
17136 (string-to-number (match-string 2 s
))
17138 ((string-match "^<[^>]+>$" s
)
17139 ;; FIXME: `decode-time' needs to be called with ZONE as its
17140 ;; second argument. However, this requires at least Emacs
17141 ;; 25.1. We can do it when we switch to this version as our
17142 ;; minimal requirement.
17143 (decode-time (seconds-to-time (org-matcher-time s
))))
17144 (t (error "Not a standard Org time string: %s" s
))))
17146 (defun org-timestamp-up (&optional arg
)
17147 "Increase the date item at the cursor by one.
17148 If the cursor is on the year, change the year. If it is on the month,
17149 the day or the time, change that.
17150 With prefix ARG, change by that many units."
17152 (org-timestamp-change (prefix-numeric-value arg
) nil
'updown
))
17154 (defun org-timestamp-down (&optional arg
)
17155 "Decrease the date item at the cursor by one.
17156 If the cursor is on the year, change the year. If it is on the month,
17157 the day or the time, change that.
17158 With prefix ARG, change by that many units."
17160 (org-timestamp-change (- (prefix-numeric-value arg
)) nil
'updown
))
17162 (defun org-timestamp-up-day (&optional arg
)
17163 "Increase the date in the time stamp by one day.
17164 With prefix ARG, change that many days."
17166 (if (and (not (org-at-timestamp-p 'lax
))
17167 (org-at-heading-p))
17169 (org-timestamp-change (prefix-numeric-value arg
) 'day
'updown
)))
17171 (defun org-timestamp-down-day (&optional arg
)
17172 "Decrease the date in the time stamp by one day.
17173 With prefix ARG, change that many days."
17175 (if (and (not (org-at-timestamp-p 'lax
))
17176 (org-at-heading-p))
17178 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
) 'updown
))
17180 (defun org-at-timestamp-p (&optional extended
)
17181 "Non-nil if point is inside a timestamp.
17183 By default, the function only consider syntactically valid active
17184 timestamps. However, the caller may have a broader definition
17185 for timestamps. As a consequence, optional argument EXTENDED can
17186 be set to the following values
17190 Include also syntactically valid inactive timestamps.
17194 Include timestamps allowed in Agenda, i.e., those in
17195 properties drawers, planning lines and clock lines.
17199 Ignore context. The function matches any part of the
17200 document looking like a timestamp. This includes comments,
17203 For backward-compatibility with Org 9.0, every other non-nil
17204 value is equivalent to `inactive'.
17206 When at a timestamp, return the position of the point as a symbol
17207 among `bracket', `after', `year', `month', `hour', `minute',
17208 `day' or a number of character from the last know part of the
17211 When matching, the match groups are the following:
17214 group 3: day number
17216 group 5: hours, if any
17217 group 6: minutes, if any"
17218 (let* ((regexp (if extended org-ts-regexp3 org-ts-regexp2
))
17221 (let ((boundaries (org-in-regexp regexp
)))
17223 (cond ((null boundaries
) nil
)
17224 ((eq extended
'lax
) t
)
17226 (or (and (eq extended
'agenda
)
17227 (or (org-at-planning-p)
17228 (org-at-property-p)
17229 (and (bound-and-true-p
17230 org-agenda-include-inactive-timestamps
)
17231 (org-at-clock-log-p))))
17234 (when (= pos
(cdr boundaries
)) (forward-char -
1))
17235 (org-element-type (org-element-context)))))))))))
17238 ((= pos
(match-beginning 0)) 'bracket
)
17239 ;; Distinguish location right before the closing bracket from
17241 ((= pos
(1- (match-end 0))) 'bracket
)
17242 ((= pos
(match-end 0)) 'after
)
17243 ((org-pos-in-match-range pos
2) 'year
)
17244 ((org-pos-in-match-range pos
3) 'month
)
17245 ((org-pos-in-match-range pos
7) 'hour
)
17246 ((org-pos-in-match-range pos
8) 'minute
)
17247 ((or (org-pos-in-match-range pos
4)
17248 (org-pos-in-match-range pos
5)) 'day
)
17249 ((and (> pos
(or (match-end 8) (match-end 5)))
17250 (< pos
(match-end 0)))
17251 (- pos
(or (match-end 8) (match-end 5))))
17254 (defun org-toggle-timestamp-type ()
17255 "Toggle the type (<active> or [inactive]) of a time stamp."
17257 (when (org-at-timestamp-p 'lax
)
17258 (let ((beg (match-beginning 0)) (end (match-end 0))
17259 (map '((?\
[ .
"<") (?\
] .
">") (?
< .
"[") (?
> .
"]"))))
17262 (while (re-search-forward "[][<>]" end t
)
17263 (replace-match (cdr (assoc (char-after (match-beginning 0)) map
))
17265 (message "Timestamp is now %sactive"
17266 (if (equal (char-after beg
) ?
<) "" "in")))))
17268 (defun org-at-clock-log-p ()
17269 "Non-nil if point is on a clock log line."
17270 (and (org-match-line org-clock-line-re
)
17271 (eq (org-element-type (save-match-data (org-element-at-point))) 'clock
)))
17273 (defvar org-clock-history
) ; defined in org-clock.el
17274 (defvar org-clock-adjust-closest nil
) ; defined in org-clock.el
17275 (defun org-timestamp-change (n &optional what updown suppress-tmp-delay
)
17276 "Change the date in the time stamp at point.
17277 The date will be changed by N times WHAT. WHAT can be `day', `month',
17278 `year', `minute', `second'. If WHAT is not given, the cursor position
17279 in the timestamp determines what will be changed.
17280 When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
17281 (let ((origin (point))
17282 (timestamp?
(org-at-timestamp-p 'lax
))
17285 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
17287 ts time time0 fixnext clrgx
)
17288 (unless timestamp?
(user-error "Not at a timestamp"))
17289 (if (and (not what
) (eq timestamp?
'bracket
))
17290 (org-toggle-timestamp-type)
17291 ;; Point isn't on brackets. Remember the part of the time-stamp
17292 ;; the point was in. Indeed, size of time-stamps may change,
17293 ;; but point must be kept in the same category nonetheless.
17294 (setq origin-cat timestamp?
)
17295 (when (and (not what
) (not (eq timestamp?
'day
))
17296 org-display-custom-times
17297 (get-text-property (point) 'display
)
17298 (not (get-text-property (1- (point)) 'display
)))
17299 (setq timestamp?
'day
))
17300 (setq timestamp?
(or what timestamp?
)
17301 inactive
(= (char-after (match-beginning 0)) ?\
[)
17302 ts
(match-string 0))
17304 (when (string-match
17305 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
17307 (setq extra
(match-string 1 ts
))
17308 (when suppress-tmp-delay
17309 (setq extra
(replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra
))))
17310 (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
17312 (setq time0
(org-parse-time-string ts
))
17314 (eq timestamp?
'minute
)
17315 (not current-prefix-arg
))
17316 ;; This looks like s-up and s-down. Change by one rounding step.
17317 (setq n
(* dm
(cond ((> n
0) 1) ((< n
0) -
1) (t 0))))
17318 (unless (= 0 (setq rem
(%
(nth 1 time0
) dm
)))
17319 (setcar (cdr time0
) (+ (nth 1 time0
)
17320 (if (> n
0) (- rem
) (- dm rem
))))))
17322 (apply #'encode-time
17324 (+ (if (eq timestamp?
'minute
) n
0) (nth 1 time0
))
17325 (+ (if (eq timestamp?
'hour
) n
0) (nth 2 time0
))
17326 (+ (if (eq timestamp?
'day
) n
0) (nth 3 time0
))
17327 (+ (if (eq timestamp?
'month
) n
0) (nth 4 time0
))
17328 (+ (if (eq timestamp?
'year
) n
0) (nth 5 time0
))
17330 (when (and (memq timestamp?
'(hour minute
))
17332 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra
))
17333 (setq extra
(org-modify-ts-extra
17335 (if (eq timestamp?
'hour
) 2 5)
17337 (when (integerp timestamp?
)
17338 (setq extra
(org-modify-ts-extra extra timestamp? n dm
)))
17339 (when (eq what
'calendar
)
17340 (let ((cal-date (org-get-date-from-calendar)))
17341 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
17342 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
17343 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
17344 (setcar time0
(or (car time0
) 0))
17345 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
17346 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
17347 (setq time
(apply 'encode-time time0
))))
17348 ;; Insert the new time-stamp, and ensure point stays in the same
17349 ;; category as before (i.e. not after the last position in that
17351 (let ((pos (point)))
17352 ;; Stay before inserted string. `save-excursion' is of no use.
17353 (setq org-last-changed-timestamp
17354 (org-insert-time-stamp time with-hm inactive nil nil extra
))
17357 (looking-at org-ts-regexp3
)
17360 ;; `day' category ends before `hour' if any, or at the end
17361 ;; of the day name.
17362 (`day
(min (or (match-beginning 7) (1- (match-end 5))) origin
))
17363 (`hour
(min (match-end 7) origin
))
17364 (`minute
(min (1- (match-end 8)) origin
))
17365 ((pred integerp
) (min (1- (match-end 0)) origin
))
17366 ;; Point was right after the time-stamp. However, the
17367 ;; time-stamp length might have changed, so refer to
17368 ;; (match-end 0) instead.
17369 (`after
(match-end 0))
17370 ;; `year' and `month' have both fixed size: point couldn't
17371 ;; have moved into another part.
17373 ;; Update clock if on a CLOCK line.
17374 (org-clock-update-time-maybe)
17375 ;; Maybe adjust the closest clock in `org-clock-history'
17376 (when org-clock-adjust-closest
17377 (if (not (and (org-at-clock-log-p)
17378 (< 1 (length (delq nil
(mapcar 'marker-position
17379 org-clock-history
))))))
17380 (message "No clock to adjust")
17381 (cond ((save-excursion ; fix previous clock?
17382 (re-search-backward org-ts-regexp0 nil t
)
17383 (looking-back (concat org-clock-string
" \\[")
17384 (line-beginning-position)))
17385 (setq fixnext
1 clrgx
(concat org-ts-regexp0
"\\] =>.*$")))
17386 ((save-excursion ; fix next clock?
17387 (re-search-backward org-ts-regexp0 nil t
)
17388 (looking-at (concat org-ts-regexp0
"\\] =>")))
17389 (setq fixnext -
1 clrgx
(concat org-clock-string
" \\[" org-ts-regexp0
))))
17390 (save-window-excursion
17391 ;; Find closest clock to point, adjust the previous/next one in history
17392 (let* ((p (save-excursion (org-back-to-heading t
)))
17393 (cl (mapcar (lambda(c) (abs (- (marker-position c
) p
))) org-clock-history
))
17395 (+ fixnext
(- (length cl
) (or (length (member (apply 'min cl
) cl
)) 100))))
17396 (clfixpos (unless (> 0 clfixnth
) (nth clfixnth org-clock-history
))))
17398 (message "No clock to adjust")
17400 (org-goto-marker-or-bmk clfixpos
)
17402 (when (re-search-forward clrgx nil t
)
17403 (goto-char (match-beginning 1))
17404 (let (org-clock-adjust-closest)
17405 (org-timestamp-change n timestamp? updown
))
17406 (message "Clock adjusted in %s for heading: %s"
17407 (file-name-nondirectory (buffer-file-name))
17408 (org-get-heading t t
)))))))))
17409 ;; Try to recenter the calendar window, if any.
17410 (when (and org-calendar-follow-timestamp-change
17411 (get-buffer-window "*Calendar*" t
)
17412 (memq timestamp?
'(day month year
)))
17413 (org-recenter-calendar (time-to-days time
))))))
17415 (defun org-modify-ts-extra (s pos n dm
)
17416 "Change the different parts of the lead-time and repeat fields in timestamp."
17417 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
17419 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
17421 ((or (org-pos-in-match-range pos
2)
17422 (org-pos-in-match-range pos
3))
17423 (setq m
(string-to-number (match-string 3 s
))
17424 h
(string-to-number (match-string 2 s
)))
17425 (if (org-pos-in-match-range pos
2)
17427 (setq n
(* dm
(with-no-warnings (signum n
))))
17428 (unless (= 0 (setq rem
(% m dm
)))
17429 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
17431 (when (< m
0) (setq m
(+ m
60) h
(1- h
)))
17432 (when (> m
59) (setq m
(- m
60) h
(1+ h
)))
17433 (setq h
(mod h
24))
17434 (setq ng
1 new
(format "-%02d:%02d" h m
)))
17435 ((org-pos-in-match-range pos
6)
17436 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
17437 ((org-pos-in-match-range pos
5)
17438 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
17440 ((org-pos-in-match-range pos
9)
17441 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
17442 ((org-pos-in-match-range pos
8)
17443 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
17447 (substring s
0 (match-beginning ng
))
17449 (substring s
(match-end ng
))))))
17452 (defun org-recenter-calendar (d)
17453 "If the calendar is visible, recenter it to date D."
17454 (let ((cwin (get-buffer-window "*Calendar*" t
)))
17456 (let ((calendar-move-hook nil
))
17457 (with-selected-window cwin
17458 (calendar-goto-date
17459 (if (listp d
) d
(calendar-gregorian-from-absolute d
))))))))
17461 (defun org-goto-calendar (&optional arg
)
17462 "Go to the Emacs calendar at the current date.
17463 If there is a time stamp in the current line, go to that date.
17464 A prefix ARG can be used to force the current date."
17466 (let ((calendar-move-hook nil
)
17467 (calendar-view-holidays-initially-flag nil
)
17468 (calendar-view-diary-initially-flag nil
)
17470 (when (or (org-at-timestamp-p 'lax
)
17471 (org-match-line (concat ".*" org-ts-regexp
)))
17472 (let ((d1 (time-to-days (current-time)))
17473 (d2 (time-to-days (org-time-string-to-time (match-string 1)))))
17474 (setq diff
(- d2 d1
))))
17476 (calendar-goto-today)
17477 (when (and diff
(not arg
)) (calendar-forward-day diff
))))
17479 (defun org-get-date-from-calendar ()
17480 "Return a list (month day year) of date at point in calendar."
17481 (with-current-buffer "*Calendar*"
17483 (calendar-cursor-to-date))))
17485 (defun org-date-from-calendar ()
17486 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
17487 If there is already a time stamp at the cursor position, update it."
17489 (if (org-at-timestamp-p 'lax
)
17490 (org-timestamp-change 0 'calendar
)
17491 (let ((cal-date (org-get-date-from-calendar)))
17492 (org-insert-time-stamp
17493 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
17495 (defcustom org-effort-durations
17499 ("w" .
,(* 60 8 5))
17500 ("m" .
,(* 60 8 5 4))
17501 ("y" .
,(* 60 8 5 40)))
17502 "Conversion factor to minutes for an effort modifier.
17504 Each entry has the form (MODIFIER . MINUTES).
17506 In an effort string, a number followed by MODIFIER is multiplied
17507 by the specified number of MINUTES to obtain an effort in
17510 For example, if the value of this variable is ((\"hours\" . 60)), then an
17511 effort string \"2hours\" is equivalent to 120 minutes."
17514 :package-version
'(Org .
"8.3")
17515 :type
'(alist :key-type
(string :tag
"Modifier")
17516 :value-type
(number :tag
"Minutes")))
17518 (defcustom org-image-actual-width t
17519 "Should we use the actual width of images when inlining them?
17521 When set to t, always use the image width.
17523 When set to a number, use imagemagick (when available) to set
17524 the image's width to this value.
17526 When set to a number in a list, try to get the width from any
17527 #+ATTR.* keyword if it matches a width specification like
17529 #+ATTR_HTML: :width 300px
17531 and fall back on that number if none is found.
17533 When set to nil, try to get the width from an #+ATTR.* keyword
17534 and fall back on the original width if none is found.
17536 This requires Emacs >= 24.1, build with imagemagick support."
17537 :group
'org-appearance
17539 :package-version
'(Org .
"8.0")
17541 (const :tag
"Use the image width" t
)
17542 (integer :tag
"Use a number of pixels")
17543 (list :tag
"Use #+ATTR* or a number of pixels" (integer))
17544 (const :tag
"Use #+ATTR* or don't resize" nil
)))
17546 (defcustom org-agenda-inhibit-startup nil
17547 "Inhibit startup when preparing agenda buffers.
17548 When this variable is t, the initialization of the Org agenda
17549 buffers is inhibited: e.g. the visibility state is not set, the
17550 tables are not re-aligned, etc."
17553 :group
'org-agenda
)
17555 (defcustom org-agenda-ignore-properties nil
17556 "Avoid updating text properties when building the agenda.
17557 Properties are used to prepare buffers for effort estimates,
17558 appointments, statistics and subtree-local categories.
17559 If you don't use these in the agenda, you can add them to this
17560 list and agenda building will be a bit faster.
17561 The value is a list, with zero or more of the symbols `effort', `appt',
17562 `stats' or `category'."
17563 :type
'(set :greedy t
17569 :package-version
'(Org .
"8.3")
17570 :group
'org-agenda
)
17574 (defun org-save-all-org-buffers ()
17575 "Save all Org buffers without user confirmation."
17577 (message "Saving all Org buffers...")
17578 (save-some-buffers t
(lambda () (derived-mode-p 'org-mode
)))
17579 (when (featurep 'org-id
) (org-id-locations-save))
17580 (message "Saving all Org buffers... done"))
17582 (defun org-revert-all-org-buffers ()
17583 "Revert all Org buffers.
17584 Prompt for confirmation when there are unsaved changes.
17585 Be sure you know what you are doing before letting this function
17586 overwrite your changes.
17588 This function is useful in a setup where one tracks Org files
17589 with a version control system, to revert on one machine after pulling
17590 changes from another. I believe the procedure must be like this:
17592 1. M-x org-save-all-org-buffers
17593 2. Pull changes from the other machine, resolve conflicts
17594 3. M-x org-revert-all-org-buffers"
17596 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
17597 (user-error "Abort"))
17599 (save-window-excursion
17600 (dolist (b (buffer-list))
17601 (when (and (with-current-buffer b
(derived-mode-p 'org-mode
))
17602 (with-current-buffer b buffer-file-name
))
17603 (pop-to-buffer-same-window b
)
17604 (revert-buffer t
'no-confirm
)))
17605 (when (and (featurep 'org-id
) org-id-track-globally
)
17606 (org-id-locations-load)))))
17611 (defun org-switchb (&optional arg
)
17612 "Switch between Org buffers.
17614 With `\\[universal-argument]' prefix, restrict available buffers to files.
17616 With `\\[universal-argument] \\[universal-argument]' \
17617 prefix, restrict available buffers to agenda files."
17619 (let ((blist (org-buffer-list
17620 (cond ((equal arg
'(4)) 'files
)
17621 ((equal arg
'(16)) 'agenda
)))))
17622 (pop-to-buffer-same-window
17623 (completing-read "Org buffer: "
17624 (mapcar #'list
(mapcar #'buffer-name blist
))
17627 (defun org-buffer-list (&optional predicate exclude-tmp
)
17628 "Return a list of Org buffers.
17629 PREDICATE can be `export', `files' or `agenda'.
17631 export restrict the list to Export buffers.
17632 files restrict the list to buffers visiting Org files.
17633 agenda restrict the list to buffers visiting agenda files.
17635 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
17637 (agenda-files (and (eq predicate
'agenda
)
17638 (mapcar 'file-truename
(org-agenda-files t
))))
17641 ((eq predicate
'files
)
17642 (lambda (b) (with-current-buffer b
(derived-mode-p 'org-mode
))))
17643 ((eq predicate
'export
)
17644 (lambda (b) (string-match "\\*Org .*Export" (buffer-name b
))))
17645 ((eq predicate
'agenda
)
17647 (with-current-buffer b
17648 (and (derived-mode-p 'org-mode
)
17649 (setq bfn
(buffer-file-name b
))
17650 (member (file-truename bfn
) agenda-files
)))))
17651 (t (lambda (b) (with-current-buffer b
17652 (or (derived-mode-p 'org-mode
)
17653 (string-match "\\*Org .*Export"
17654 (buffer-name b
)))))))))
17658 (if (and (funcall filter b
)
17659 (or (not exclude-tmp
)
17660 (not (string-match "tmp" (buffer-name b
)))))
17665 (defun org-agenda-files (&optional unrestricted archives
)
17666 "Get the list of agenda files.
17667 Optional UNRESTRICTED means return the full list even if a restriction
17668 is currently in place.
17669 When ARCHIVES is t, include all archive files that are really being
17670 used by the agenda files. If ARCHIVE is `ifmode', do this only if
17671 `org-agenda-archives-mode' is t."
17674 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
17675 ((stringp org-agenda-files
) (org-read-agenda-file-list))
17676 ((listp org-agenda-files
) org-agenda-files
)
17677 (t (error "Invalid value of `org-agenda-files'")))))
17678 (setq files
(apply 'append
17679 (mapcar (lambda (f)
17680 (if (file-directory-p f
)
17682 f t org-agenda-file-regexp
)
17685 (when org-agenda-skip-unavailable-files
17686 (setq files
(delq nil
17689 (and (file-readable-p file
) file
)))
17691 (when (or (eq archives t
)
17692 (and (eq archives
'ifmode
) (eq org-agenda-archives-mode t
)))
17693 (setq files
(org-add-archive-files files
)))
17696 (defun org-agenda-file-p (&optional file
)
17697 "Return non-nil, if FILE is an agenda file.
17698 If FILE is omitted, use the file associated with the current
17700 (let ((fname (or file
(buffer-file-name))))
17702 (member (file-truename fname
)
17703 (mapcar #'file-truename
(org-agenda-files t
))))))
17705 (defun org-edit-agenda-file-list ()
17706 "Edit the list of agenda files.
17707 Depending on setup, this either uses customize to edit the variable
17708 `org-agenda-files', or it visits the file that is holding the list. In the
17709 latter case, the buffer is set up in a way that saving it automatically kills
17710 the buffer and restores the previous window configuration."
17712 (if (stringp org-agenda-files
)
17713 (let ((cw (current-window-configuration)))
17714 (find-file org-agenda-files
)
17715 (setq-local org-window-configuration cw
)
17716 (add-hook 'after-save-hook
17718 (set-window-configuration
17719 (prog1 org-window-configuration
17720 (kill-buffer (current-buffer))))
17721 (org-install-agenda-files-menu)
17722 (message "New agenda file list installed"))
17724 (message "%s" (substitute-command-keys
17725 "Edit list and finish with \\[save-buffer]")))
17726 (customize-variable 'org-agenda-files
)))
17728 (defun org-store-new-agenda-file-list (list)
17729 "Set new value for the agenda file list and save it correctly."
17730 (if (stringp org-agenda-files
)
17731 (let ((fe (org-read-agenda-file-list t
)) b u
)
17732 (while (setq b
(find-buffer-visiting org-agenda-files
))
17734 (with-temp-file org-agenda-files
17737 (lambda (f) ;; Keep un-expanded entries.
17738 (if (setq u
(assoc f fe
))
17743 (let ((org-mode-hook nil
) (org-inhibit-startup t
)
17744 (org-insert-mode-line-in-empty-file nil
))
17745 (setq org-agenda-files list
)
17746 (customize-save-variable 'org-agenda-files org-agenda-files
))))
17748 (defun org-read-agenda-file-list (&optional pair-with-expansion
)
17749 "Read the list of agenda files from a file.
17750 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
17751 filenames, used by `org-store-new-agenda-file-list' to write back
17752 un-expanded file names."
17753 (when (file-directory-p org-agenda-files
)
17754 (error "`org-agenda-files' cannot be a single directory"))
17755 (when (stringp org-agenda-files
)
17757 (insert-file-contents org-agenda-files
)
17760 (let ((e (expand-file-name (substitute-in-file-name f
)
17762 (if pair-with-expansion
17765 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
17768 (defun org-cycle-agenda-files ()
17769 "Cycle through the files in `org-agenda-files'.
17770 If the current buffer visits an agenda file, find the next one in the list.
17771 If the current buffer does not, find the first agenda file."
17773 (let* ((fs (or (org-agenda-files t
)
17774 (user-error "No agenda files")))
17775 (files (copy-sequence fs
))
17776 (tcf (and buffer-file-name
(file-truename buffer-file-name
)))
17779 (while (and (setq file
(pop files
))
17780 (not (equal (file-truename file
) tcf
)))))
17781 (find-file (car (or files fs
)))
17782 (when (buffer-base-buffer) (pop-to-buffer-same-window (buffer-base-buffer)))))
17784 (defun org-agenda-file-to-front (&optional to-end
)
17785 "Move/add the current file to the top of the agenda file list.
17786 If the file is not present in the list, it is added to the front. If it is
17787 present, it is moved there. With optional argument TO-END, add/move to the
17790 (let ((org-agenda-skip-unavailable-files nil
)
17791 (file-alist (mapcar (lambda (x)
17792 (cons (file-truename x
) x
))
17793 (org-agenda-files t
)))
17794 (ctf (file-truename
17795 (or buffer-file-name
17796 (user-error "Please save the current buffer to a file"))))
17798 (setq x
(assoc ctf file-alist
) had x
)
17800 (unless x
(setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
17802 (setq file-alist
(append (delq x file-alist
) (list x
)))
17803 (setq file-alist
(cons x
(delq x file-alist
))))
17804 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
17805 (org-install-agenda-files-menu)
17806 (message "File %s to %s of agenda file list"
17807 (if had
"moved" "added") (if to-end
"end" "front"))))
17809 (defun org-remove-file (&optional file
)
17810 "Remove current file from the list of files in variable `org-agenda-files'.
17811 These are the files which are being checked for agenda entries.
17812 Optional argument FILE means use this file instead of the current."
17814 (let* ((org-agenda-skip-unavailable-files nil
)
17815 (file (or file buffer-file-name
17816 (user-error "Current buffer does not visit a file")))
17817 (true-file (file-truename file
))
17818 (afile (abbreviate-file-name file
))
17819 (files (delq nil
(mapcar
17821 (unless (equal true-file
17824 (org-agenda-files t
)))))
17825 (if (not (= (length files
) (length (org-agenda-files t
))))
17827 (org-store-new-agenda-file-list files
)
17828 (org-install-agenda-files-menu)
17829 (message "Removed from Org Agenda list: %s" afile
))
17830 (message "File was not in list: %s (not removed)" afile
))))
17832 (defun org-file-menu-entry (file)
17833 (vector file
(list 'find-file file
) t
))
17835 (defun org-check-agenda-file (file)
17836 "Make sure FILE exists. If not, ask user what to do."
17837 (unless (file-exists-p file
)
17838 (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
17839 (abbreviate-file-name file
))
17840 (let ((r (downcase (read-char-exclusive))))
17843 (org-remove-file file
)
17844 (throw 'nextfile t
))
17845 (t (user-error "Abort"))))))
17847 (defun org-get-agenda-file-buffer (file)
17848 "Get an agenda buffer visiting FILE.
17849 If the buffer needs to be created, add it to the list of buffers
17850 which might be released later."
17851 (let ((buf (org-find-base-buffer-visiting file
)))
17853 buf
; just return it
17854 ;; Make a new buffer and remember it
17855 (setq buf
(find-file-noselect file
))
17856 (when buf
(push buf org-agenda-new-buffers
))
17859 (defun org-release-buffers (blist)
17860 "Release all buffers in list, asking the user for confirmation when needed.
17861 When a buffer is unmodified, it is just killed. When modified, it is saved
17862 \(if the user agrees) and then killed."
17864 (dolist (buf blist
)
17865 (setq file
(buffer-file-name buf
))
17866 (when (and (buffer-modified-p buf
)
17868 (y-or-n-p (format "Save file %s? " file
)))
17869 (with-current-buffer buf
(save-buffer)))
17870 (kill-buffer buf
))))
17872 (defun org-agenda-prepare-buffers (files)
17873 "Create buffers for all agenda files, protect archived trees and comments."
17875 (let ((pa '(:org-archived t
))
17876 (pc '(:org-comment t
))
17877 (pall '(:org-archived t
:org-comment t
))
17878 (inhibit-read-only t
)
17879 (org-inhibit-startup org-agenda-inhibit-startup
)
17880 (rea (concat ":" org-archive-tag
":"))
17882 (setq org-tag-alist-for-agenda nil
17883 org-tag-groups-alist-for-agenda nil
)
17886 (dolist (file files
)
17890 (org-check-agenda-file file
)
17891 (set-buffer (org-get-agenda-file-buffer file
)))
17893 (org-set-regexps-and-options 'tags-only
)
17895 (or (memq 'category org-agenda-ignore-properties
)
17896 (org-refresh-category-properties))
17897 (or (memq 'stats org-agenda-ignore-properties
)
17898 (org-refresh-stats-properties))
17899 (or (memq 'effort org-agenda-ignore-properties
)
17900 (org-refresh-effort-properties))
17901 (or (memq 'appt org-agenda-ignore-properties
)
17902 (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime
))
17903 (setq org-todo-keywords-for-agenda
17904 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
17905 (setq org-done-keywords-for-agenda
17906 (append org-done-keywords-for-agenda org-done-keywords
))
17907 (setq org-todo-keyword-alist-for-agenda
17908 (append org-todo-keyword-alist-for-agenda org-todo-key-alist
))
17909 (setq org-tag-alist-for-agenda
17910 (org-tag-add-to-alist
17911 org-tag-alist-for-agenda
17912 org-current-tag-alist
))
17913 ;; Merge current file's tag groups into global
17914 ;; `org-tag-groups-alist-for-agenda'.
17915 (when org-group-tags
17916 (dolist (alist org-tag-groups-alist
)
17917 (let ((old (assoc (car alist
) org-tag-groups-alist-for-agenda
)))
17919 (setcdr old
(org-uniquify (append (cdr old
) (cdr alist
))))
17920 (push alist org-tag-groups-alist-for-agenda
)))))
17921 (org-with-silent-modifications
17923 (remove-text-properties (point-min) (point-max) pall
)
17924 (when org-agenda-skip-archived-trees
17925 (goto-char (point-min))
17926 (while (re-search-forward rea nil t
)
17927 (when (org-at-heading-p t
)
17928 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
17929 (goto-char (point-min))
17930 (setq re
(format "^\\*+ .*\\<%s\\>" org-comment-string
))
17931 (while (re-search-forward re nil t
)
17932 (when (save-match-data (org-in-commented-heading-p t
))
17933 (add-text-properties
17934 (match-beginning 0) (org-end-of-subtree t
) pc
)))))
17935 (goto-char pos
)))))
17936 (setq org-todo-keywords-for-agenda
17937 (org-uniquify org-todo-keywords-for-agenda
))
17938 (setq org-todo-keyword-alist-for-agenda
17939 (org-uniquify org-todo-keyword-alist-for-agenda
))))
17942 ;;;; CDLaTeX minor mode
17944 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
17945 "Keymap for the minor `org-cdlatex-mode'.")
17947 (org-defkey org-cdlatex-mode-map
(kbd "_") #'org-cdlatex-underscore-caret
)
17948 (org-defkey org-cdlatex-mode-map
(kbd "^") #'org-cdlatex-underscore-caret
)
17949 (org-defkey org-cdlatex-mode-map
(kbd "`") #'cdlatex-math-symbol
)
17950 (org-defkey org-cdlatex-mode-map
(kbd "'") #'org-cdlatex-math-modify
)
17951 (org-defkey org-cdlatex-mode-map
(kbd "C-c {") #'org-cdlatex-environment-indent
)
17953 (defvar org-cdlatex-texmathp-advice-is-done nil
17954 "Flag remembering if we have applied the advice to texmathp already.")
17956 (define-minor-mode org-cdlatex-mode
17957 "Toggle the minor `org-cdlatex-mode'.
17958 This mode supports entering LaTeX environment and math in LaTeX fragments
17960 \\{org-cdlatex-mode-map}"
17962 (when org-cdlatex-mode
17964 (run-hooks 'cdlatex-mode-hook
)
17965 (cdlatex-compute-tables))
17966 (unless org-cdlatex-texmathp-advice-is-done
17967 (setq org-cdlatex-texmathp-advice-is-done t
)
17968 (defadvice texmathp
(around org-math-always-on activate
)
17969 "Always return t in Org buffers.
17970 This is because we want to insert math symbols without dollars even outside
17971 the LaTeX math segments. If Org mode thinks that point is actually inside
17972 an embedded LaTeX fragment, let `texmathp' do its job.
17973 `\\[org-cdlatex-mode-map]'"
17977 ((not (derived-mode-p 'org-mode
)) ad-do-it
)
17978 ((eq this-command
'cdlatex-math-symbol
)
17979 (setq ad-return-value t
17980 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
17982 (let ((p (org-inside-LaTeX-fragment-p)))
17983 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
17984 (setq ad-return-value t
17985 texmathp-why
'("Org mode embedded math" .
0))
17986 (when p ad-do-it
)))))))))
17988 (defun turn-on-org-cdlatex ()
17989 "Unconditionally turn on `org-cdlatex-mode'."
17990 (org-cdlatex-mode 1))
17992 (defun org-try-cdlatex-tab ()
17993 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
17994 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
17995 - inside a LaTeX fragment, or
17996 - after the first word in a line, where an abbreviation expansion could
17997 insert a LaTeX environment."
17998 (when org-cdlatex-mode
18000 ;; Before any word on the line: No expansion possible.
18001 ((save-excursion (skip-chars-backward " \t") (bolp)) nil
)
18002 ;; Just after first word on the line: Expand it. Make sure it
18003 ;; cannot happen on headlines, though.
18005 (skip-chars-backward "a-zA-Z0-9*")
18006 (skip-chars-backward " \t")
18007 (and (bolp) (not (org-at-heading-p))))
18009 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t
))))
18011 (defun org-cdlatex-underscore-caret (&optional _arg
)
18012 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
18013 Revert to the normal definition outside of these fragments."
18015 (if (org-inside-LaTeX-fragment-p)
18016 (call-interactively 'cdlatex-sub-superscript
)
18017 (let (org-cdlatex-mode)
18018 (call-interactively (key-binding (vector last-input-event
))))))
18020 (defun org-cdlatex-math-modify (&optional _arg
)
18021 "Execute `cdlatex-math-modify' in LaTeX fragments.
18022 Revert to the normal definition outside of these fragments."
18024 (if (org-inside-LaTeX-fragment-p)
18025 (call-interactively 'cdlatex-math-modify
)
18026 (let (org-cdlatex-mode)
18027 (call-interactively (key-binding (vector last-input-event
))))))
18029 (defun org-cdlatex-environment-indent (&optional environment item
)
18030 "Execute `cdlatex-environment' and indent the inserted environment.
18032 ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
18034 The inserted environment is indented to current indentation
18035 unless point is at the beginning of the line, in which the
18036 environment remains unintended."
18038 ;; cdlatex-environment always return nil. Therefore, capture output
18039 ;; first and determine if an environment was selected.
18040 (let* ((beg (point-marker))
18041 (end (copy-marker (point) t
))
18043 (ignore-errors (cdlatex-environment environment item
))
18045 ;; Figure out how many lines to move forward after the
18046 ;; environment has been inserted.
18047 (lines (when inserted
18049 (- (cl-loop while
(< beg
(point))
18051 do
(forward-line -
1)
18054 (if (progn (goto-char beg
)
18055 (and (progn (skip-chars-forward " \t") (eolp))
18056 (progn (skip-chars-backward " \t") (bolp))))
18058 (env (org-trim (delete-and-extract-region beg end
))))
18060 ;; Get indentation of next line unless at column 0.
18061 (let ((ind (if (bolp) 0
18063 (org-return-indent)
18064 (prog1 (org-get-indentation)
18065 (when (progn (skip-chars-forward " \t") (eolp))
18066 (delete-region beg
(point)))))))
18067 (bol (progn (skip-chars-backward " \t") (bolp))))
18068 ;; Insert a newline before environment unless at column zero
18069 ;; to "escape" the current line. Insert a newline if
18070 ;; something is one the same line as \end{ENVIRONMENT}.
18072 (concat (unless bol
"\n") env
18073 (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
18074 (unless (zerop ind
)
18077 (while (< (point) end
)
18078 (unless (eolp) (indent-line-to ind
))
18081 (forward-line lines
)
18082 (indent-line-to ind
)))
18083 (set-marker beg nil
)
18084 (set-marker end nil
)))
18087 ;;;; LaTeX fragments
18089 (defun org-inside-LaTeX-fragment-p ()
18090 "Test if point is inside a LaTeX fragment.
18091 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
18092 sequence appearing also before point.
18093 Even though the matchers for math are configurable, this function assumes
18094 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
18095 delimiters are skipped when they have been removed by customization.
18096 The return value is nil, or a cons cell with the delimiter and the
18097 position of this delimiter.
18099 This function does a reasonably good job, but can locally be fooled by
18100 for example currency specifications. For example it will assume being in
18101 inline math after \"$22.34\". The LaTeX fragment formatter will only format
18102 fragments that are properly closed, but during editing, we have to live
18103 with the uncertainty caused by missing closing delimiters. This function
18104 looks only before point, not after."
18106 (let ((pos (point))
18107 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
18108 (lim (save-excursion (org-backward-paragraph) (point)))
18109 dd-on str
(start 0) m re
)
18112 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
18113 re
(nth 1 (assoc "$" org-latex-regexps
)))
18114 (while (string-match re str start
)
18116 ((= (match-end 0) (length str
))
18117 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
18118 ((= (match-end 0) (- (length str
) 5))
18120 (t (setq start
(match-end 0))))))
18121 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
18123 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
18124 (and (match-beginning 2) (throw 'exit nil
))
18126 (while (re-search-backward "\\$\\$" lim t
)
18127 (setq dd-on
(not dd-on
)))
18129 (when dd-on
(cons "$$" m
))))))
18131 (defun org-inside-latex-macro-p ()
18132 "Is point inside a LaTeX macro or its arguments?"
18135 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
18137 (defun org--format-latex-make-overlay (beg end image
&optional imagetype
)
18138 "Build an overlay between BEG and END using IMAGE file.
18139 Argument IMAGETYPE is the extension of the displayed image,
18140 as a string. It defaults to \"png\"."
18141 (let ((ov (make-overlay beg end
))
18142 (imagetype (or (intern imagetype
) 'png
)))
18143 (overlay-put ov
'org-overlay-type
'org-latex-overlay
)
18144 (overlay-put ov
'evaporate t
)
18146 'modification-hooks
18147 (list (lambda (o _flag _beg _end
&optional _l
)
18148 (delete-overlay o
))))
18151 (list 'image
:type imagetype
:file image
:ascent
'center
))))
18153 (defun org--list-latex-overlays (&optional beg end
)
18154 "List all Org LaTeX overlays in current buffer.
18155 Limit to overlays between BEG and END when those are provided."
18157 (lambda (o) (eq (overlay-get o
'org-overlay-type
) 'org-latex-overlay
))
18158 (overlays-in (or beg
(point-min)) (or end
(point-max)))))
18160 (defun org-remove-latex-fragment-image-overlays (&optional beg end
)
18161 "Remove all overlays with LaTeX fragment images in current buffer.
18162 When optional arguments BEG and END are non-nil, remove all
18163 overlays between them instead. Return a non-nil value when some
18164 overlays were removed, nil otherwise."
18165 (let ((overlays (org--list-latex-overlays beg end
)))
18166 (mapc #'delete-overlay overlays
)
18169 (defun org-toggle-latex-fragment (&optional arg
)
18170 "Preview the LaTeX fragment at point, or all locally or globally.
18172 If the cursor is on a LaTeX fragment, create the image and overlay
18173 it over the source code, if there is none. Remove it otherwise.
18174 If there is no fragment at point, display all fragments in the
18177 With prefix ARG, preview or clear image for all fragments in the
18178 current subtree or in the whole buffer when used before the first
18179 headline. With a prefix ARG `\\[universal-argument] \
18180 \\[universal-argument]' preview or clear images
18181 for all fragments in the buffer."
18183 (when (display-graphic-p)
18188 ((or (equal arg
'(16))
18189 (and (equal arg
'(4))
18190 (org-with-limited-levels (org-before-first-heading-p))))
18191 (if (org-remove-latex-fragment-image-overlays)
18192 (progn (message "LaTeX fragments images removed from buffer")
18194 (setq msg
"Creating images for buffer...")))
18196 (org-with-limited-levels (org-back-to-heading t
))
18198 (setq end
(progn (org-end-of-subtree t
) (point)))
18199 (if (org-remove-latex-fragment-image-overlays beg end
)
18201 (message "LaTeX fragment images removed from subtree")
18203 (setq msg
"Creating images for subtree...")))
18204 ((let ((datum (org-element-context)))
18205 (when (memq (org-element-type datum
)
18206 '(latex-environment latex-fragment
))
18207 (setq beg
(org-element-property :begin datum
))
18208 (setq end
(org-element-property :end datum
))
18209 (if (org-remove-latex-fragment-image-overlays beg end
)
18210 (progn (message "LaTeX fragment image removed")
18212 (setq msg
"Creating image...")))))
18214 (org-with-limited-levels
18215 (setq beg
(if (org-at-heading-p) (line-beginning-position)
18216 (outline-previous-heading)
18218 (setq end
(progn (outline-next-heading) (point)))
18219 (if (org-remove-latex-fragment-image-overlays beg end
)
18221 (message "LaTeX fragment images removed from section")
18223 (setq msg
"Creating images for section...")))))
18224 (let ((file (buffer-file-name (buffer-base-buffer))))
18226 (concat org-preview-latex-image-directory
"org-ltximg")
18228 ;; Emacs cannot overlay images from remote hosts. Create
18229 ;; it in `temporary-file-directory' instead.
18230 (if (or (not file
) (file-remote-p file
))
18231 temporary-file-directory
18233 'overlays msg
'forbuffer org-preview-latex-default-process
))
18234 (message (concat msg
"done")))))))
18236 (defun org-format-latex
18237 (prefix &optional beg end dir overlays msg forbuffer processing-type
)
18238 "Replace LaTeX fragments with links to an image.
18240 The function takes care of creating the replacement image.
18242 Only consider fragments between BEG and END when those are
18245 When optional argument OVERLAYS is non-nil, display the image on
18246 top of the fragment instead of replacing it.
18248 PROCESSING-TYPE is the conversion method to use, as a symbol.
18250 Some of the options can be changed using the variable
18251 `org-format-latex-options', which see."
18252 (when (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
18253 (unless (eq processing-type
'verbatim
)
18254 (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
18257 (goto-char (or beg
(point-min)))
18258 ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
18259 (when (and overlays
(memq processing-type
'(dvipng imagemagick
)))
18260 (overlay-recenter (or end
(point-max))))
18261 (while (re-search-forward math-regexp end t
)
18262 (unless (and overlays
18263 (eq (get-char-property (point) 'org-overlay-type
)
18264 'org-latex-overlay
))
18265 (let* ((context (org-element-context))
18266 (type (org-element-type context
)))
18267 (when (memq type
'(latex-environment latex-fragment
))
18268 (let ((block-type (eq type
'latex-environment
))
18269 (value (org-element-property :value context
))
18270 (beg (org-element-property :begin context
))
18271 (end (save-excursion
18272 (goto-char (org-element-property :end context
))
18273 (skip-chars-backward " \r\t\n")
18276 ((eq processing-type
'mathjax
)
18277 ;; Prepare for MathJax processing.
18278 (if (not (string-match "\\`\\$\\$?" value
))
18280 (delete-region beg end
)
18281 (if (string= (match-string 0 value
) "$$")
18282 (insert "\\[" (substring value
2 -
2) "\\]")
18283 (insert "\\(" (substring value
1 -
1) "\\)"))))
18284 ((assq processing-type org-preview-latex-process-alist
)
18285 ;; Process to an image.
18288 (let* ((processing-info
18289 (cdr (assq processing-type org-preview-latex-process-alist
)))
18290 (face (face-at-point))
18291 ;; Get the colors from the face at point.
18293 (let ((color (plist-get org-format-latex-options
18295 (if (and forbuffer
(eq color
'auto
))
18296 (face-attribute face
:foreground nil
'default
)
18299 (let ((color (plist-get org-format-latex-options
18301 (if (and forbuffer
(eq color
'auto
))
18302 (face-attribute face
:background nil
'default
)
18304 (hash (sha1 (prin1-to-string
18305 (list org-format-latex-header
18306 org-latex-default-packages-alist
18307 org-latex-packages-alist
18308 org-format-latex-options
18309 forbuffer value fg bg
))))
18310 (imagetype (or (plist-get processing-info
:image-output-type
) "png"))
18311 (absprefix (expand-file-name prefix dir
))
18312 (linkfile (format "%s_%s.%s" prefix hash imagetype
))
18313 (movefile (format "%s_%s.%s" absprefix hash imagetype
))
18314 (sep (and block-type
"\n\n"))
18315 (link (concat sep
"[[file:" linkfile
"]]" sep
))
18317 (org-combine-plists
18318 org-format-latex-options
18319 `(:foreground
,fg
:background
,bg
))))
18320 (when msg
(message msg cnt
))
18321 (unless checkdir-flag
; Ensure the directory exists.
18322 (setq checkdir-flag t
)
18323 (let ((todir (file-name-directory absprefix
)))
18324 (unless (file-directory-p todir
)
18325 (make-directory todir t
))))
18326 (unless (file-exists-p movefile
)
18327 (org-create-formula-image
18328 value movefile options forbuffer processing-type
))
18331 (dolist (o (overlays-in beg end
))
18332 (when (eq (overlay-get o
'org-overlay-type
)
18333 'org-latex-overlay
)
18334 (delete-overlay o
)))
18335 (org--format-latex-make-overlay beg end movefile imagetype
)
18337 (delete-region beg end
)
18339 (org-add-props link
18340 (list 'org-latex-src
18341 (replace-regexp-in-string "\"" "" value
)
18342 'org-latex-src-embed-type
18343 (if block-type
'paragraph
'character
)))))))
18344 ((eq processing-type
'mathml
)
18345 ;; Process to MathML.
18346 (unless (org-format-latex-mathml-available-p)
18347 (user-error "LaTeX to MathML converter not configured"))
18349 (when msg
(message msg cnt
))
18351 (delete-region beg end
)
18352 (insert (org-format-latex-as-mathml
18353 value block-type prefix dir
)))
18355 (error "Unknown conversion process %s for LaTeX fragments"
18356 processing-type
)))))))))))
18358 (defun org-create-math-formula (latex-frag &optional mathml-file
)
18359 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
18360 Use `org-latex-to-mathml-convert-command'. If the conversion is
18361 sucessful, return the portion between \"<math...> </math>\"
18362 elements otherwise return nil. When MATHML-FILE is specified,
18363 write the results in to that file. When invoked as an
18364 interactive command, prompt for LATEX-FRAG, with initial value
18365 set to the current active region and echo the results for user
18367 (interactive (list (let ((frag (when (org-region-active-p)
18368 (buffer-substring-no-properties
18369 (region-beginning) (region-end)))))
18370 (read-string "LaTeX Fragment: " frag nil frag
))))
18371 (unless latex-frag
(user-error "Invalid LaTeX fragment"))
18372 (let* ((tmp-in-file
18373 (let ((file (file-relative-name
18374 (make-temp-name (expand-file-name "ltxmathml-in")))))
18375 (write-region latex-frag nil file
)
18377 (tmp-out-file (file-relative-name
18378 (make-temp-name (expand-file-name "ltxmathml-out"))))
18380 org-latex-to-mathml-convert-command
18381 `((?j .
,(and org-latex-to-mathml-jar-file
18382 (shell-quote-argument
18384 org-latex-to-mathml-jar-file
))))
18385 (?I .
,(shell-quote-argument tmp-in-file
))
18387 (?o .
,(shell-quote-argument tmp-out-file
)))))
18388 mathml shell-command-output
)
18389 (when (called-interactively-p 'any
)
18390 (unless (org-format-latex-mathml-available-p)
18391 (user-error "LaTeX to MathML converter not configured")))
18392 (message "Running %s" cmd
)
18393 (setq shell-command-output
(shell-command-to-string cmd
))
18395 (when (file-readable-p tmp-out-file
)
18396 (with-current-buffer (find-file-noselect tmp-out-file t
)
18397 (goto-char (point-min))
18398 (when (re-search-forward
18399 (format "<math[^>]*?%s[^>]*?>\\(.\\|\n\\)*</math>"
18401 "xmlns=\"http://www.w3.org/1998/Math/MathML\""))
18403 (prog1 (match-string 0) (kill-buffer))))))
18407 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml
))
18409 (write-region mathml nil mathml-file
))
18410 (when (called-interactively-p 'any
)
18412 ((message "LaTeX to MathML conversion failed")
18413 (message shell-command-output
)))
18414 (delete-file tmp-in-file
)
18415 (when (file-exists-p tmp-out-file
)
18416 (delete-file tmp-out-file
))
18419 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
18420 prefix
&optional dir
)
18421 "Use `org-create-math-formula' but check local cache first."
18422 (let* ((absprefix (expand-file-name prefix dir
))
18423 (print-length nil
) (print-level nil
)
18424 (formula-id (concat
18429 org-latex-to-mathml-convert-command
)))))
18430 (formula-cache (format "%s-%s.mathml" absprefix formula-id
))
18431 (formula-cache-dir (file-name-directory formula-cache
)))
18433 (unless (file-directory-p formula-cache-dir
)
18434 (make-directory formula-cache-dir t
))
18436 (unless (file-exists-p formula-cache
)
18437 (org-create-math-formula latex-frag formula-cache
))
18439 (if (file-exists-p formula-cache
)
18440 ;; Successful conversion. Return the link to MathML file.
18442 (format "[[file:%s]]" (file-relative-name formula-cache dir
))
18443 (list 'org-latex-src
(replace-regexp-in-string "\"" "" latex-frag
)
18444 'org-latex-src-embed-type
(if latex-frag-type
18445 'paragraph
'character
)))
18446 ;; Failed conversion. Return the LaTeX fragment verbatim
18449 (defun org--get-display-dpi ()
18450 "Get the DPI of the display.
18451 The function assumes that the display has the same pixel width in
18452 the horizontal and vertical directions."
18453 (if (display-graphic-p)
18454 (round (/ (display-pixel-height)
18455 (/ (display-mm-height) 25.4)))
18456 (error "Attempt to calculate the dpi of a non-graphic display")))
18458 (defun org-create-formula-image
18459 (string tofile options buffer
&optional processing-type
)
18460 "Create an image from LaTeX source using external processes.
18462 The LaTeX STRING is saved to a temporary LaTeX file, then
18463 converted to an image file by process PROCESSING-TYPE defined in
18464 `org-preview-latex-process-alist'. A nil value defaults to
18465 `org-preview-latex-default-process'.
18467 The generated image file is eventually moved to TOFILE.
18469 The OPTIONS argument controls the size, foreground color and
18470 background color of the generated image.
18472 When BUFFER non-nil, this function is used for LaTeX previewing.
18473 Otherwise, it is used to deal with LaTeX snippets showed in
18475 (let* ((processing-type (or processing-type
18476 org-preview-latex-default-process
))
18478 (cdr (assq processing-type org-preview-latex-process-alist
)))
18479 (programs (plist-get processing-info
:programs
))
18480 (error-message (or (plist-get processing-info
:message
) ""))
18481 (use-xcolor (plist-get processing-info
:use-xcolor
))
18482 (image-input-type (plist-get processing-info
:image-input-type
))
18483 (image-output-type (plist-get processing-info
:image-output-type
))
18484 (post-clean (or (plist-get processing-info
:post-clean
)
18485 '(".dvi" ".xdv" ".pdf" ".tex" ".aux" ".log"
18486 ".svg" ".png" ".jpg" ".jpeg" ".out")))
18488 (or (plist-get processing-info
:latex-header
)
18489 (org-latex-make-preamble
18490 (org-export-get-environment (org-export-get-backend 'latex
))
18491 org-format-latex-header
18493 (latex-compiler (plist-get processing-info
:latex-compiler
))
18494 (image-converter (plist-get processing-info
:image-converter
))
18495 (tmpdir temporary-file-directory
)
18496 (texfilebase (make-temp-name
18497 (expand-file-name "orgtex" tmpdir
)))
18498 (texfile (concat texfilebase
".tex"))
18499 (image-size-adjust (or (plist-get processing-info
:image-size-adjust
)
18501 (scale (* (if buffer
(car image-size-adjust
) (cdr image-size-adjust
))
18502 (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0)))
18503 (dpi (* scale
(if buffer
(org--get-display-dpi) 140.0)))
18504 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
18506 (bg (or (plist-get options
(if buffer
:background
:html-background
))
18508 (log-buf (get-buffer-create "*Org Preview LaTeX Output*"))
18509 (resize-mini-windows nil
)) ;Fix Emacs flicker when creating image.
18510 (dolist (program programs
)
18511 (org-check-external-command program error-message
))
18513 (progn (if (eq fg
'default
)
18514 (setq fg
(org-latex-color :foreground
))
18515 (setq fg
(org-latex-color-format fg
)))
18516 (if (eq bg
'default
)
18517 (setq bg
(org-latex-color :background
))
18518 (setq bg
(org-latex-color-format
18519 (if (string= bg
"Transparent") "white" bg
))))
18520 (with-temp-file texfile
18521 (insert latex-header
)
18522 (insert "\n\\begin{document}\n"
18523 "\\definecolor{fg}{rgb}{" fg
"}\n"
18524 "\\definecolor{bg}{rgb}{" bg
"}\n"
18525 "\n\\pagecolor{bg}\n"
18529 "\n\\end{document}\n")))
18530 (if (eq fg
'default
)
18531 (setq fg
(org-dvipng-color :foreground
))
18532 (unless (string= fg
"Transparent")
18533 (setq fg
(org-dvipng-color-format fg
))))
18534 (if (eq bg
'default
)
18535 (setq bg
(org-dvipng-color :background
))
18536 (unless (string= bg
"Transparent")
18537 (setq bg
(org-dvipng-color-format bg
))))
18538 (with-temp-file texfile
18539 (insert latex-header
)
18540 (insert "\n\\begin{document}\n" string
"\n\\end{document}\n")))
18542 (let* ((err-msg (format "Please adjust `%s' part of \
18543 `org-preview-latex-process-alist'."
18547 texfile latex-compiler image-input-type err-msg log-buf
))
18550 image-input-file image-converter image-output-type err-msg log-buf
18551 `((?F .
,(shell-quote-argument fg
))
18552 (?B .
,(shell-quote-argument bg
))
18553 (?D .
,(shell-quote-argument (format "%s" dpi
)))
18554 (?S .
,(shell-quote-argument (format "%s" (/ dpi
140.0))))))))
18555 (copy-file image-output-file tofile
'replace
)
18556 (dolist (e post-clean
)
18557 (when (file-exists-p (concat texfilebase e
))
18558 (delete-file (concat texfilebase e
))))
18559 image-output-file
)))
18561 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p
&optional extra
)
18562 "Fill a LaTeX header template TPL.
18563 In the template, the following place holders will be recognized:
18565 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
18566 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
18567 [PACKAGES] \\usepackage statements for PKG
18568 [NO-PACKAGES] do not include PKG
18569 [EXTRA] the string EXTRA
18570 [NO-EXTRA] do not include EXTRA
18572 For backward compatibility, if both the positive and the negative place
18573 holder is missing, the positive one (without the \"NO-\") will be
18574 assumed to be present at the end of the template.
18575 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
18577 SNIPPETS-P indicates if this is run to create snippet images for HTML."
18578 (let (rpl (end ""))
18579 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl
)
18580 (setq rpl
(if (or (match-end 1) (not def-pkg
))
18581 "" (org-latex-packages-to-string def-pkg snippets-p t
))
18582 tpl
(replace-match rpl t t tpl
))
18583 (when def-pkg
(setq end
(org-latex-packages-to-string def-pkg snippets-p
))))
18585 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl
)
18586 (setq rpl
(if (or (match-end 1) (not pkg
))
18587 "" (org-latex-packages-to-string pkg snippets-p t
))
18588 tpl
(replace-match rpl t t tpl
))
18589 (when pkg
(setq end
18591 (org-latex-packages-to-string pkg snippets-p
)))))
18593 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl
)
18594 (setq rpl
(if (or (match-end 1) (not extra
))
18595 "" (concat extra
"\n"))
18596 tpl
(replace-match rpl t t tpl
))
18597 (when (and extra
(string-match "\\S-" extra
))
18598 (setq end
(concat end
"\n" extra
))))
18600 (if (string-match "\\S-" end
)
18601 (concat tpl
"\n" end
)
18604 (defun org-latex-packages-to-string (pkg &optional snippets-p newline
)
18605 "Turn an alist of packages into a string with the \\usepackage macros."
18606 (setq pkg
(mapconcat (lambda(p)
18609 ((and snippets-p
(>= (length p
) 3) (not (nth 2 p
)))
18610 (format "%% Package %s omitted" (cadr p
)))
18611 ((equal "" (car p
))
18612 (format "\\usepackage{%s}" (cadr p
)))
18614 (format "\\usepackage[%s]{%s}"
18615 (car p
) (cadr p
)))))
18618 (if newline
(concat pkg
"\n") pkg
))
18620 (defun org-dvipng-color (attr)
18621 "Return a RGB color specification for dvipng."
18622 (org-dvipng-color-format (face-attribute 'default attr nil
)))
18624 (defun org-dvipng-color-format (color-name)
18625 "Convert COLOR-NAME to a RGB color value for dvipng."
18626 (apply #'format
"rgb %s %s %s"
18627 (mapcar 'org-normalize-color
18628 (color-values color-name
))))
18630 (defun org-latex-color (attr)
18631 "Return a RGB color for the LaTeX color package."
18632 (org-latex-color-format (face-attribute 'default attr nil
)))
18634 (defun org-latex-color-format (color-name)
18635 "Convert COLOR-NAME to a RGB color value."
18636 (apply #'format
"%s,%s,%s"
18637 (mapcar 'org-normalize-color
18638 (color-values color-name
))))
18640 (defun org-normalize-color (value)
18641 "Return string to be used as color value for an RGB component."
18642 (format "%g" (/ value
65535.0)))
18647 (defvar-local org-inline-image-overlays nil
)
18649 (defun org-toggle-inline-images (&optional include-linked
)
18650 "Toggle the display of inline images.
18651 INCLUDE-LINKED is passed to `org-display-inline-images'."
18653 (if org-inline-image-overlays
18655 (org-remove-inline-images)
18656 (when (called-interactively-p 'interactive
)
18657 (message "Inline image display turned off")))
18658 (org-display-inline-images include-linked
)
18659 (when (called-interactively-p 'interactive
)
18660 (message (if org-inline-image-overlays
18661 (format "%d images displayed inline"
18662 (length org-inline-image-overlays
))
18663 "No images to display inline")))))
18665 (defun org-redisplay-inline-images ()
18666 "Refresh the display of inline images."
18668 (if (not org-inline-image-overlays
)
18669 (org-toggle-inline-images)
18670 (org-toggle-inline-images)
18671 (org-toggle-inline-images)))
18673 (defun org-display-inline-images (&optional include-linked refresh beg end
)
18674 "Display inline images.
18676 An inline image is a link which follows either of these
18679 1. Its path is a file with an extension matching return value
18680 from `image-file-name-regexp' and it has no contents.
18682 2. Its description consists in a single link of the previous
18685 When optional argument INCLUDE-LINKED is non-nil, also links with
18686 a text description part will be inlined. This can be nice for
18687 a quick look at those images, but it does not reflect what
18688 exported files will look like.
18690 When optional argument REFRESH is non-nil, refresh existing
18691 images between BEG and END. This will create new image displays
18692 only if necessary. BEG and END default to the buffer
18695 (when (display-graphic-p)
18697 (org-remove-inline-images)
18698 (when (fboundp 'clear-image-cache
) (clear-image-cache)))
18699 (org-with-wide-buffer
18700 (goto-char (or beg
(point-min)))
18701 (let* ((case-fold-search t
)
18702 (file-extension-re (image-file-name-regexp))
18703 (link-abbrevs (mapcar #'car
18704 (append org-link-abbrev-alist-local
18705 org-link-abbrev-alist
)))
18706 ;; Check absolute, relative file names and explicit
18707 ;; "file:" links. Also check link abbreviations since
18708 ;; some might expand to "file" links.
18709 (file-types-re (format "[][]\\[\\(?:file\\|[./~]%s\\)"
18710 (if (not link-abbrevs
) ""
18711 (format "\\|\\(?:%s:\\)"
18712 (regexp-opt link-abbrevs
))))))
18713 (while (re-search-forward file-types-re end t
)
18714 (let ((link (save-match-data (org-element-context))))
18715 ;; Check if we're at an inline image, i.e., an image file
18716 ;; link without a description (unless INCLUDE-LINKED is
18718 (when (and (equal "file" (org-element-property :type link
))
18720 (null (org-element-contents link
)))
18721 (string-match-p file-extension-re
18722 (org-element-property :path link
)))
18723 (let ((file (expand-file-name
18725 (org-element-property :path link
)))))
18726 (when (file-exists-p file
)
18728 ;; Apply `org-image-actual-width' specifications.
18730 ((not (image-type-available-p 'imagemagick
)) nil
)
18731 ((eq org-image-actual-width t
) nil
)
18732 ((listp org-image-actual-width
)
18734 ;; First try to find a width among
18735 ;; attributes associated to the paragraph
18736 ;; containing link.
18739 (while (and (setq e
(org-element-property
18741 (not (eq (org-element-type e
)
18746 (goto-char (org-element-property :begin paragraph
))
18749 "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
18750 (org-element-property
18751 :post-affiliated paragraph
)
18753 (string-to-number (match-string 1))))))
18754 ;; Otherwise, fall-back to provided number.
18755 (car org-image-actual-width
)))
18756 ((numberp org-image-actual-width
)
18757 org-image-actual-width
)))
18758 (old (get-char-property-and-overlay
18759 (org-element-property :begin link
)
18760 'org-image-overlay
)))
18761 (if (and (car-safe old
) refresh
)
18762 (image-refresh (overlay-get (cdr old
) 'display
))
18763 (let ((image (create-image file
18764 (and width
'imagemagick
)
18768 (let ((ov (make-overlay
18769 (org-element-property :begin link
)
18772 (org-element-property :end link
))
18773 (skip-chars-backward " \t")
18775 (overlay-put ov
'display image
)
18776 (overlay-put ov
'face
'default
)
18777 (overlay-put ov
'org-image-overlay t
)
18779 ov
'modification-hooks
18780 (list 'org-display-inline-remove-overlay
))
18781 (push ov org-inline-image-overlays
)))))))))))))))
18783 (defun org-display-inline-remove-overlay (ov after _beg _end
&optional _len
)
18784 "Remove inline-display overlay if a corresponding region is modified."
18785 (let ((inhibit-modification-hooks t
))
18786 (when (and ov after
)
18787 (delete ov org-inline-image-overlays
)
18788 (delete-overlay ov
))))
18790 (defun org-remove-inline-images ()
18791 "Remove inline display of images."
18793 (mapc #'delete-overlay org-inline-image-overlays
)
18794 (setq org-inline-image-overlays nil
))
18799 (defun org-remap (map &rest commands
)
18800 "In MAP, remap the functions given in COMMANDS.
18801 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
18804 (setq old
(pop commands
) new
(pop commands
))
18805 (org-defkey map
(vector 'remap old
) new
))))
18807 ;;;; Outline functions that can be remapped in Org
18808 (define-key org-mode-map
[remap outline-mark-subtree
] #'org-mark-subtree
)
18809 (define-key org-mode-map
[remap outline-show-subtree
] #'org-show-subtree
)
18810 (define-key org-mode-map
[remap outline-forward-same-level
]
18811 #'org-forward-heading-same-level
)
18812 (define-key org-mode-map
[remap outline-backward-same-level
]
18813 #'org-backward-heading-same-level
)
18814 (define-key org-mode-map
[remap outline-show-branches
]
18815 #'org-kill-note-or-show-branches
)
18816 (define-key org-mode-map
[remap outline-promote
] #'org-promote-subtree
)
18817 (define-key org-mode-map
[remap outline-demote
] #'org-demote-subtree
)
18818 (define-key org-mode-map
[remap outline-insert-heading
] #'org-ctrl-c-ret
)
18819 (define-key org-mode-map
[remap outline-next-visible-heading
]
18820 #'org-next-visible-heading
)
18821 (define-key org-mode-map
[remap outline-previous-visible-heading
]
18822 #'org-previous-visible-heading
)
18823 (define-key org-mode-map
[remap show-children
] #'org-show-children
)
18825 ;;;; Make `C-c C-x' a prefix key
18826 (org-defkey org-mode-map
(kbd "C-c C-x") (make-sparse-keymap))
18828 ;;;; TAB key with modifiers
18829 (org-defkey org-mode-map
(kbd "C-i") #'org-cycle
)
18830 (org-defkey org-mode-map
(kbd "<tab>") #'org-cycle
)
18831 (org-defkey org-mode-map
(kbd "C-<tab>") #'org-force-cycle-archived
)
18832 (org-defkey org-mode-map
(kbd "M-<tab>") #'pcomplete
)
18833 (org-defkey org-mode-map
(kbd "ESC <tab>") #'pcomplete
)
18835 (org-defkey org-mode-map
(kbd "<S-iso-leftab>") #'org-shifttab
)
18836 (org-defkey org-mode-map
(kbd "S-<tab>") #'org-shifttab
)
18837 (define-key org-mode-map
(kbd "<backtab>") #'org-shifttab
)
18839 ;;;; RET key with modifiers
18840 (org-defkey org-mode-map
(kbd "S-<return>") #'org-table-copy-down
)
18841 (org-defkey org-mode-map
(kbd "M-S-<return>") #'org-insert-todo-heading
)
18842 (org-defkey org-mode-map
(kbd "ESC S-<return>") #'org-insert-todo-heading
)
18843 (org-defkey org-mode-map
(kbd "M-RET") #'org-meta-return
)
18844 (org-defkey org-mode-map
(kbd "ESC RET") #'org-meta-return
)
18846 ;;;; Cursor keys with modifiers
18847 (org-defkey org-mode-map
(kbd "M-<left>") #'org-metaleft
)
18848 (org-defkey org-mode-map
(kbd "M-<right>") #'org-metaright
)
18849 (org-defkey org-mode-map
(kbd "ESC <right>") #'org-metaright
)
18850 (org-defkey org-mode-map
(kbd "M-<up>") #'org-metaup
)
18851 (org-defkey org-mode-map
(kbd "ESC <up>") #'org-metaup
)
18852 (org-defkey org-mode-map
(kbd "M-<down>") #'org-metadown
)
18853 (org-defkey org-mode-map
(kbd "ESC <down>") #'org-metadown
)
18855 (org-defkey org-mode-map
(kbd "C-M-S-<right>") #'org-increase-number-at-point
)
18856 (org-defkey org-mode-map
(kbd "C-M-S-<left>") #'org-decrease-number-at-point
)
18857 (org-defkey org-mode-map
(kbd "M-S-<left>") #'org-shiftmetaleft
)
18858 (org-defkey org-mode-map
(kbd "ESC S-<left>") #'org-shiftmetaleft
)
18859 (org-defkey org-mode-map
(kbd "M-S-<right>") #'org-shiftmetaright
)
18860 (org-defkey org-mode-map
(kbd "ESC S-<right>") #'org-shiftmetaright
)
18861 (org-defkey org-mode-map
(kbd "M-S-<up>") #'org-shiftmetaup
)
18862 (org-defkey org-mode-map
(kbd "ESC S-<up>") #'org-shiftmetaup
)
18863 (org-defkey org-mode-map
(kbd "M-S-<down>") #'org-shiftmetadown
)
18864 (org-defkey org-mode-map
(kbd "ESC S-<down>") #'org-shiftmetadown
)
18866 (org-defkey org-mode-map
(kbd "S-<up>") #'org-shiftup
)
18867 (org-defkey org-mode-map
(kbd "S-<down>") #'org-shiftdown
)
18868 (org-defkey org-mode-map
(kbd "S-<left>") #'org-shiftleft
)
18869 (org-defkey org-mode-map
(kbd "S-<right>") #'org-shiftright
)
18871 (org-defkey org-mode-map
(kbd "C-S-<right>") #'org-shiftcontrolright
)
18872 (org-defkey org-mode-map
(kbd "C-S-<left>") #'org-shiftcontrolleft
)
18873 (org-defkey org-mode-map
(kbd "C-S-<up>") #'org-shiftcontrolup
)
18874 (org-defkey org-mode-map
(kbd "C-S-<down>") #'org-shiftcontroldown
)
18877 (define-key org-mode-map org-babel-key-prefix org-babel-map
)
18878 (pcase-dolist (`(,key .
,def
) org-babel-key-bindings
)
18879 (define-key org-babel-map key def
))
18881 ;;;; Extra keys for TTY access.
18883 ;; We only set them when really needed because otherwise the
18884 ;; menus don't show the simple keys
18886 (when (or org-use-extra-keys
(not window-system
))
18887 (org-defkey org-mode-map
(kbd "C-c C-x c") #'org-table-copy-down
)
18888 (org-defkey org-mode-map
(kbd "C-c C-x m") #'org-meta-return
)
18889 (org-defkey org-mode-map
(kbd "C-c C-x M") #'org-insert-todo-heading
)
18890 (org-defkey org-mode-map
(kbd "C-c C-x RET") #'org-meta-return
)
18891 (org-defkey org-mode-map
(kbd "ESC RET") #'org-meta-return
)
18892 (org-defkey org-mode-map
(kbd "ESC <left>") #'org-metaleft
)
18893 (org-defkey org-mode-map
(kbd "C-c C-x l") #'org-metaleft
)
18894 (org-defkey org-mode-map
(kbd "ESC <right>") #'org-metaright
)
18895 (org-defkey org-mode-map
(kbd "C-c C-x r") #'org-metaright
)
18896 (org-defkey org-mode-map
(kbd "C-c C-x u") #'org-metaup
)
18897 (org-defkey org-mode-map
(kbd "C-c C-x d") #'org-metadown
)
18898 (org-defkey org-mode-map
(kbd "C-c C-x L") #'org-shiftmetaleft
)
18899 (org-defkey org-mode-map
(kbd "C-c C-x R") #'org-shiftmetaright
)
18900 (org-defkey org-mode-map
(kbd "C-c C-x U") #'org-shiftmetaup
)
18901 (org-defkey org-mode-map
(kbd "C-c C-x D") #'org-shiftmetadown
)
18902 (org-defkey org-mode-map
(kbd "C-c <up>") #'org-shiftup
)
18903 (org-defkey org-mode-map
(kbd "C-c <down>") #'org-shiftdown
)
18904 (org-defkey org-mode-map
(kbd "C-c <left>") #'org-shiftleft
)
18905 (org-defkey org-mode-map
(kbd "C-c <right>") #'org-shiftright
)
18906 (org-defkey org-mode-map
(kbd "C-c C-x <right>") #'org-shiftcontrolright
)
18907 (org-defkey org-mode-map
(kbd "C-c C-x <left>") #'org-shiftcontrolleft
))
18910 (org-defkey narrow-map
"s" #'org-narrow-to-subtree
)
18911 (org-defkey narrow-map
"b" #'org-narrow-to-block
)
18912 (org-defkey narrow-map
"e" #'org-narrow-to-element
)
18914 ;;;; Remap usual Emacs bindings
18915 (org-remap org-mode-map
18916 'self-insert-command
'org-self-insert-command
18917 'delete-char
'org-delete-char
18918 'delete-backward-char
'org-delete-backward-char
18919 'kill-line
'org-kill-line
18920 'open-line
'org-open-line
18922 'comment-dwim
'org-comment-dwim
18923 'move-beginning-of-line
'org-beginning-of-line
18924 'move-end-of-line
'org-end-of-line
18925 'forward-paragraph
'org-forward-paragraph
18926 'backward-paragraph
'org-backward-paragraph
18927 'backward-sentence
'org-backward-sentence
18928 'forward-sentence
'org-forward-sentence
18929 'fill-paragraph
'org-fill-paragraph
18930 'delete-indentation
'org-delete-indentation
18931 'transpose-words
'org-transpose-words
)
18933 ;;;; All the other keys
18934 (org-defkey org-mode-map
(kbd "|") #'org-force-self-insert
)
18935 (org-defkey org-mode-map
(kbd "C-c C-a") #'org-show-all
)
18936 (org-defkey org-mode-map
(kbd "C-c C-r") #'org-reveal
)
18937 (org-defkey org-mode-map
(kbd "C-M-t") #'org-transpose-element
)
18938 (org-defkey org-mode-map
(kbd "M-}") #'org-forward-element
)
18939 (org-defkey org-mode-map
(kbd "ESC }") #'org-forward-element
)
18940 (org-defkey org-mode-map
(kbd "M-{") #'org-backward-element
)
18941 (org-defkey org-mode-map
(kbd "ESC {") #'org-backward-element
)
18942 (org-defkey org-mode-map
(kbd "C-c C-^") #'org-up-element
)
18943 (org-defkey org-mode-map
(kbd "C-c C-_") #'org-down-element
)
18944 (org-defkey org-mode-map
(kbd "C-c C-f") #'org-forward-heading-same-level
)
18945 (org-defkey org-mode-map
(kbd "C-c C-b") #'org-backward-heading-same-level
)
18946 (org-defkey org-mode-map
(kbd "C-c M-f") #'org-next-block
)
18947 (org-defkey org-mode-map
(kbd "C-c M-b") #'org-previous-block
)
18948 (org-defkey org-mode-map
(kbd "C-c $") #'org-archive-subtree
)
18949 (org-defkey org-mode-map
(kbd "C-c C-x C-s") #'org-archive-subtree
)
18950 (org-defkey org-mode-map
(kbd "C-c C-x C-a") #'org-archive-subtree-default
)
18951 (org-defkey org-mode-map
(kbd "C-c C-x d") #'org-insert-drawer
)
18952 (org-defkey org-mode-map
(kbd "C-c C-x a") #'org-toggle-archive-tag
)
18953 (org-defkey org-mode-map
(kbd "C-c C-x A") #'org-archive-to-archive-sibling
)
18954 (org-defkey org-mode-map
(kbd "C-c C-x b") #'org-tree-to-indirect-buffer
)
18955 (org-defkey org-mode-map
(kbd "C-c C-x q") #'org-toggle-tags-groups
)
18956 (org-defkey org-mode-map
(kbd "C-c C-j") #'org-goto
)
18957 (org-defkey org-mode-map
(kbd "C-c C-t") #'org-todo
)
18958 (org-defkey org-mode-map
(kbd "C-c C-q") #'org-set-tags-command
)
18959 (org-defkey org-mode-map
(kbd "C-c C-s") #'org-schedule
)
18960 (org-defkey org-mode-map
(kbd "C-c C-d") #'org-deadline
)
18961 (org-defkey org-mode-map
(kbd "C-c ;") #'org-toggle-comment
)
18962 (org-defkey org-mode-map
(kbd "C-c C-w") #'org-refile
)
18963 (org-defkey org-mode-map
(kbd "C-c M-w") #'org-copy
)
18964 (org-defkey org-mode-map
(kbd "C-c /") #'org-sparse-tree
) ;minor-mode reserved
18965 (org-defkey org-mode-map
(kbd "C-c \\") #'org-match-sparse-tree
) ;minor-mode r.
18966 (org-defkey org-mode-map
(kbd "C-c RET") #'org-ctrl-c-ret
)
18967 (org-defkey org-mode-map
(kbd "C-c C-x c") #'org-clone-subtree-with-time-shift
)
18968 (org-defkey org-mode-map
(kbd "C-c C-x v") #'org-copy-visible
)
18969 (org-defkey org-mode-map
(kbd "C-<return>") #'org-insert-heading-respect-content
)
18970 (org-defkey org-mode-map
(kbd "C-S-<return>") #'org-insert-todo-heading-respect-content
)
18971 (org-defkey org-mode-map
(kbd "C-c C-x C-n") #'org-next-link
)
18972 (org-defkey org-mode-map
(kbd "C-c C-x C-p") #'org-previous-link
)
18973 (org-defkey org-mode-map
(kbd "C-c C-l") #'org-insert-link
)
18974 (org-defkey org-mode-map
(kbd "C-c M-l") #'org-insert-last-stored-link
)
18975 (org-defkey org-mode-map
(kbd "C-c C-M-l") #'org-insert-all-links
)
18976 (org-defkey org-mode-map
(kbd "C-c C-o") #'org-open-at-point
)
18977 (org-defkey org-mode-map
(kbd "C-c %") #'org-mark-ring-push
)
18978 (org-defkey org-mode-map
(kbd "C-c &") #'org-mark-ring-goto
)
18979 (org-defkey org-mode-map
(kbd "C-c C-z") #'org-add-note
) ;alternative binding
18980 (org-defkey org-mode-map
(kbd "C-c .") #'org-time-stamp
) ;minor-mode reserved
18981 (org-defkey org-mode-map
(kbd "C-c !") #'org-time-stamp-inactive
) ;minor-mode r.
18982 (org-defkey org-mode-map
(kbd "C-c ,") #'org-priority
) ;minor-mode reserved
18983 (org-defkey org-mode-map
(kbd "C-c C-y") #'org-evaluate-time-range
)
18984 (org-defkey org-mode-map
(kbd "C-c >") #'org-goto-calendar
)
18985 (org-defkey org-mode-map
(kbd "C-c <") #'org-date-from-calendar
)
18986 (org-defkey org-mode-map
(kbd "C-,") #'org-cycle-agenda-files
)
18987 (org-defkey org-mode-map
(kbd "C-'") #'org-cycle-agenda-files
)
18988 (org-defkey org-mode-map
(kbd "C-c [") #'org-agenda-file-to-front
)
18989 (org-defkey org-mode-map
(kbd "C-c ]") #'org-remove-file
)
18990 (org-defkey org-mode-map
(kbd "C-c C-x <") #'org-agenda-set-restriction-lock
)
18991 (org-defkey org-mode-map
(kbd "C-c C-x >") #'org-agenda-remove-restriction-lock
)
18992 (org-defkey org-mode-map
(kbd "C-c -") #'org-ctrl-c-minus
)
18993 (org-defkey org-mode-map
(kbd "C-c *") #'org-ctrl-c-star
)
18994 (org-defkey org-mode-map
(kbd "C-c TAB") #'org-ctrl-c-tab
)
18995 (org-defkey org-mode-map
(kbd "C-c ^") #'org-sort
)
18996 (org-defkey org-mode-map
(kbd "C-c C-c") #'org-ctrl-c-ctrl-c
)
18997 (org-defkey org-mode-map
(kbd "C-c C-k") #'org-kill-note-or-show-branches
)
18998 (org-defkey org-mode-map
(kbd "C-c #") #'org-update-statistics-cookies
)
18999 (org-defkey org-mode-map
(kbd "RET") #'org-return
)
19000 (org-defkey org-mode-map
(kbd "C-j") #'org-return-indent
)
19001 (org-defkey org-mode-map
(kbd "C-c ?") #'org-table-field-info
)
19002 (org-defkey org-mode-map
(kbd "C-c SPC") #'org-table-blank-field
)
19003 (org-defkey org-mode-map
(kbd "C-c +") #'org-table-sum
)
19004 (org-defkey org-mode-map
(kbd "C-c =") #'org-table-eval-formula
)
19005 (org-defkey org-mode-map
(kbd "C-c '") #'org-edit-special
)
19006 (org-defkey org-mode-map
(kbd "C-c `") #'org-table-edit-field
)
19007 (org-defkey org-mode-map
(kbd "C-c \" a") #'orgtbl-ascii-plot
)
19008 (org-defkey org-mode-map
(kbd "C-c \" g") #'org-plot
/gnuplot
)
19009 (org-defkey org-mode-map
(kbd "C-c |") #'org-table-create-or-convert-from-region
)
19010 (org-defkey org-mode-map
(kbd "C-#") #'org-table-rotate-recalc-marks
)
19011 (org-defkey org-mode-map
(kbd "C-c ~") #'org-table-create-with-table.el
)
19012 (org-defkey org-mode-map
(kbd "C-c C-a") #'org-attach
)
19013 (org-defkey org-mode-map
(kbd "C-c }") #'org-table-toggle-coordinate-overlays
)
19014 (org-defkey org-mode-map
(kbd "C-c {") #'org-table-toggle-formula-debugger
)
19015 (org-defkey org-mode-map
(kbd "C-c C-e") #'org-export-dispatch
)
19016 (org-defkey org-mode-map
(kbd "C-c :") #'org-toggle-fixed-width
)
19017 (org-defkey org-mode-map
(kbd "C-c C-x C-f") #'org-emphasize
)
19018 (org-defkey org-mode-map
(kbd "C-c C-x f") #'org-footnote-action
)
19019 (org-defkey org-mode-map
(kbd "C-c @") #'org-mark-subtree
)
19020 (org-defkey org-mode-map
(kbd "M-h") #'org-mark-element
)
19021 (org-defkey org-mode-map
(kbd "ESC h") #'org-mark-element
)
19022 (org-defkey org-mode-map
(kbd "C-c C-*") #'org-list-make-subtree
)
19024 (org-defkey org-mode-map
(kbd "C-c C-x C-w") #'org-cut-special
)
19025 (org-defkey org-mode-map
(kbd "C-c C-x M-w") #'org-copy-special
)
19026 (org-defkey org-mode-map
(kbd "C-c C-x C-y") #'org-paste-special
)
19028 (org-defkey org-mode-map
(kbd "C-c C-x C-t") #'org-toggle-time-stamp-overlays
)
19029 (org-defkey org-mode-map
(kbd "C-c C-x C-i") #'org-clock-in
)
19030 (org-defkey org-mode-map
(kbd "C-c C-x C-x") #'org-clock-in-last
)
19031 (org-defkey org-mode-map
(kbd "C-c C-x C-z") #'org-resolve-clocks
)
19032 (org-defkey org-mode-map
(kbd "C-c C-x C-o") #'org-clock-out
)
19033 (org-defkey org-mode-map
(kbd "C-c C-x C-j") #'org-clock-goto
)
19034 (org-defkey org-mode-map
(kbd "C-c C-x C-q") #'org-clock-cancel
)
19035 (org-defkey org-mode-map
(kbd "C-c C-x C-d") #'org-clock-display
)
19036 (org-defkey org-mode-map
(kbd "C-c C-x C-r") #'org-clock-report
)
19037 (org-defkey org-mode-map
(kbd "C-c C-x C-u") #'org-dblock-update
)
19038 (org-defkey org-mode-map
(kbd "C-c C-x C-l") #'org-toggle-latex-fragment
)
19039 (org-defkey org-mode-map
(kbd "C-c C-x C-v") #'org-toggle-inline-images
)
19040 (org-defkey org-mode-map
(kbd "C-c C-x C-M-v") #'org-redisplay-inline-images
)
19041 (org-defkey org-mode-map
(kbd "C-c C-x \\") #'org-toggle-pretty-entities
)
19042 (org-defkey org-mode-map
(kbd "C-c C-x C-b") #'org-toggle-checkbox
)
19043 (org-defkey org-mode-map
(kbd "C-c C-x p") #'org-set-property
)
19044 (org-defkey org-mode-map
(kbd "C-c C-x P") #'org-set-property-and-value
)
19045 (org-defkey org-mode-map
(kbd "C-c C-x e") #'org-set-effort
)
19046 (org-defkey org-mode-map
(kbd "C-c C-x E") #'org-inc-effort
)
19047 (org-defkey org-mode-map
(kbd "C-c C-x o") #'org-toggle-ordered-property
)
19048 (org-defkey org-mode-map
(kbd "C-c C-x i") #'org-columns-insert-dblock
)
19049 (org-defkey org-mode-map
(kbd "C-c C-x w") #'org-insert-structure-template
)
19051 (org-defkey org-mode-map
(kbd "C-c C-x .") #'org-timer
)
19052 (org-defkey org-mode-map
(kbd "C-c C-x -") #'org-timer-item
)
19053 (org-defkey org-mode-map
(kbd "C-c C-x 0") #'org-timer-start
)
19054 (org-defkey org-mode-map
(kbd "C-c C-x _") #'org-timer-stop
)
19055 (org-defkey org-mode-map
(kbd "C-c C-x ;") #'org-timer-set-timer
)
19056 (org-defkey org-mode-map
(kbd "C-c C-x ,") #'org-timer-pause-or-continue
)
19058 (define-key org-mode-map
(kbd "C-c C-x C-c") #'org-columns
)
19060 (define-key org-mode-map
(kbd "C-c C-x !") #'org-reload
)
19062 (define-key org-mode-map
(kbd "C-c C-x g") #'org-feed-update-all
)
19063 (define-key org-mode-map
(kbd "C-c C-x G") #'org-feed-goto-inbox
)
19065 (define-key org-mode-map
(kbd "C-c C-x [") #'org-reftex-citation
)
19070 (defconst org-speed-commands-default
19072 ("Outline Navigation")
19073 ("n" .
(org-speed-move-safe 'org-next-visible-heading
))
19074 ("p" .
(org-speed-move-safe 'org-previous-visible-heading
))
19075 ("f" .
(org-speed-move-safe 'org-forward-heading-same-level
))
19076 ("b" .
(org-speed-move-safe 'org-backward-heading-same-level
))
19077 ("F" . org-next-block
)
19078 ("B" . org-previous-block
)
19079 ("u" .
(org-speed-move-safe 'outline-up-heading
))
19081 ("g" .
(org-refile t
))
19082 ("Outline Visibility")
19084 ("C" . org-shifttab
)
19085 (" " . org-display-outline-path
)
19086 ("s" . org-narrow-to-subtree
)
19087 ("=" . org-columns
)
19088 ("Outline Structure Editing")
19090 ("D" . org-metadown
)
19091 ("r" . org-metaright
)
19092 ("l" . org-metaleft
)
19093 ("R" . org-shiftmetaright
)
19094 ("L" . org-shiftmetaleft
)
19095 ("i" .
(progn (forward-char 1) (call-interactively
19096 'org-insert-heading-respect-content
)))
19099 ("a" . org-archive-subtree-default-with-confirmation
)
19100 ("@" . org-mark-subtree
)
19101 ("#" . org-toggle-comment
)
19103 ("I" . org-clock-in
)
19104 ("O" . org-clock-out
)
19105 ("Meta Data Editing")
19107 ("," .
(org-priority))
19108 ("0" .
(org-priority ?\
))
19109 ("1" .
(org-priority ?A
))
19110 ("2" .
(org-priority ?B
))
19111 ("3" .
(org-priority ?C
))
19112 (":" . org-set-tags-command
)
19113 ("e" . org-set-effort
)
19114 ("E" . org-inc-effort
)
19115 ("W" .
(lambda(m) (interactive "sMinutes before warning: ")
19116 (org-entry-put (point) "APPT_WARNTIME" m
)))
19117 ("Agenda Views etc")
19119 ("/" . org-sparse-tree
)
19121 ("o" . org-open-at-point
)
19122 ("?" . org-speed-command-help
)
19123 ("<" .
(org-agenda-set-restriction-lock 'subtree
))
19124 (">" .
(org-agenda-remove-restriction-lock))
19126 "The default speed commands.")
19128 (defun org-print-speed-command (e)
19129 (if (> (length (car e
)) 1)
19134 (princ (make-string (length (car e
)) ?-
))
19138 (if (symbolp (cdr e
))
19139 (princ (symbol-name (cdr e
)))
19143 (defun org-speed-command-help ()
19144 "Show the available speed commands."
19146 (if (not org-use-speed-commands
)
19147 (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
19148 (with-output-to-temp-buffer "*Help*"
19149 (princ "User-defined Speed commands\n===========================\n")
19150 (mapc #'org-print-speed-command org-speed-commands-user
)
19152 (princ "Built-in Speed commands\n=======================\n")
19153 (mapc #'org-print-speed-command org-speed-commands-default
))
19154 (with-current-buffer "*Help*"
19155 (setq truncate-lines t
))))
19157 (defun org-speed-move-safe (cmd)
19158 "Execute CMD, but make sure that the cursor always ends up in a headline.
19159 If not, return to the original position and throw an error."
19161 (let ((pos (point)))
19162 (call-interactively cmd
)
19163 (unless (and (bolp) (org-at-heading-p))
19165 (error "Boundary reached while executing %s" cmd
))))
19167 (defvar org-self-insert-command-undo-counter
0)
19169 (defvar org-table-auto-blank-field
) ; defined in org-table.el
19170 (defvar org-speed-command nil
)
19172 (defun org-speed-command-activate (keys)
19173 "Hook for activating single-letter speed commands.
19174 `org-speed-commands-default' specifies a minimal command set.
19175 Use `org-speed-commands-user' for further customization."
19176 (when (or (and (bolp) (looking-at org-outline-regexp
))
19177 (and (functionp org-use-speed-commands
)
19178 (funcall org-use-speed-commands
)))
19179 (cdr (assoc keys
(append org-speed-commands-user
19180 org-speed-commands-default
)))))
19182 (defun org-babel-speed-command-activate (keys)
19183 "Hook for activating single-letter code block commands."
19184 (when (and (bolp) (looking-at org-babel-src-block-regexp
))
19185 (cdr (assoc keys org-babel-key-bindings
))))
19187 (defcustom org-speed-command-hook
19188 '(org-speed-command-activate org-babel-speed-command-activate
)
19189 "Hook for activating speed commands at strategic locations.
19190 Hook functions are called in sequence until a valid handler is
19193 Each hook takes a single argument, a user-pressed command key
19194 which is also a `self-insert-command' from the global map.
19196 Within the hook, examine the cursor position and the command key
19197 and return nil or a valid handler as appropriate. Handler could
19198 be one of an interactive command, a function, or a form.
19200 Set `org-use-speed-commands' to non-nil value to enable this
19201 hook. The default setting is `org-speed-command-activate'."
19202 :group
'org-structure
19206 (defun org-self-insert-command (N)
19207 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
19208 If the cursor is in a table looking at whitespace, the whitespace is
19209 overwritten, and the table is not marked as requiring realignment."
19211 (org-check-before-invisible-edit 'insert
)
19213 ((and org-use-speed-commands
19214 (let ((kv (this-command-keys-vector)))
19215 (setq org-speed-command
19216 (run-hook-with-args-until-success
19217 'org-speed-command-hook
19218 (make-string 1 (aref kv
(1- (length kv
))))))))
19220 ((commandp org-speed-command
)
19221 (setq this-command org-speed-command
)
19222 (call-interactively org-speed-command
))
19223 ((functionp org-speed-command
)
19224 (funcall org-speed-command
))
19225 ((and org-speed-command
(listp org-speed-command
))
19226 (eval org-speed-command
))
19227 (t (let (org-use-speed-commands)
19228 (call-interactively 'org-self-insert-command
)))))
19231 (not (org-region-active-p))
19234 ;; Check if we blank the field, and if that triggers align.
19235 (and (featurep 'org-table
)
19236 org-table-auto-blank-field
19238 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c
))
19239 (if (or (eq (char-after) ?\s
) (looking-at "[^|\n]* |"))
19240 ;; Got extra space, this field does not determine
19242 (let (org-table-may-need-update) (org-table-blank-field))
19243 ;; No extra space, this field may determine column
19245 (org-table-blank-field)))
19247 (looking-at "[^|\n]* |"))
19248 ;; There is room for insertion without re-aligning the table.
19249 (self-insert-command N
)
19250 (org-table-with-shrunk-field
19252 (skip-chars-forward "^|")
19253 ;; Do not delete last space, which is
19254 ;; `org-table-separator-space', but the regular space before
19256 (delete-region (- (point) 2) (1- (point))))))
19258 (setq org-table-may-need-update t
)
19259 (self-insert-command N
)
19260 (org-fix-tags-on-the-fly)
19261 (when org-self-insert-cluster-for-undo
19262 (if (not (eq last-command
'org-self-insert-command
))
19263 (setq org-self-insert-command-undo-counter
1)
19264 (if (>= org-self-insert-command-undo-counter
20)
19265 (setq org-self-insert-command-undo-counter
1)
19266 (and (> org-self-insert-command-undo-counter
0)
19267 buffer-undo-list
(listp buffer-undo-list
)
19268 (not (cadr buffer-undo-list
)) ; remove nil entry
19269 (setcdr buffer-undo-list
(cddr buffer-undo-list
)))
19270 (setq org-self-insert-command-undo-counter
19271 (1+ org-self-insert-command-undo-counter
))))))))
19273 (defun org-check-before-invisible-edit (kind)
19274 "Check is editing if kind KIND would be dangerous with invisible text around.
19275 The detailed reaction depends on the user option `org-catch-invisible-edits'."
19276 ;; First, try to get out of here as quickly as possible, to reduce overhead
19277 (when (and org-catch-invisible-edits
19278 (or (not (boundp 'visible-mode
)) (not visible-mode
))
19279 (or (get-char-property (point) 'invisible
)
19280 (get-char-property (max (point-min) (1- (point))) 'invisible
)))
19281 ;; OK, we need to take a closer look. Do not consider
19282 ;; invisibility obtained through text properties (e.g., link
19283 ;; fontification), as it cannot be toggled.
19284 (let* ((invisible-at-point
19285 (pcase (get-char-property-and-overlay (point) 'invisible
)
19286 (`(,_ .
,(and (pred overlayp
) o
)) o
)))
19287 ;; Assume that point cannot land in the middle of an
19288 ;; overlay, or between two overlays.
19289 (invisible-before-point
19290 (and (not invisible-at-point
)
19292 (pcase (get-char-property-and-overlay (1- (point)) 'invisible
)
19293 (`(,_ .
,(and (pred overlayp
) o
)) o
))))
19294 (border-and-ok-direction
19296 ;; Check if we are acting predictably before invisible
19298 (and invisible-at-point
19299 (memq kind
'(insert delete-backward
)))
19300 ;; Check if we are acting predictably after invisible text
19301 ;; This works not well, and I have turned it off. It seems
19302 ;; better to always show and stop after invisible text.
19303 ;; (and (not invisible-at-point) invisible-before-point
19304 ;; (memq kind '(insert delete)))
19306 (when (or invisible-at-point invisible-before-point
)
19307 (when (eq org-catch-invisible-edits
'error
)
19308 (user-error "Editing in invisible areas is prohibited, make them visible first"))
19309 (if (and org-custom-properties-overlays
19310 (y-or-n-p "Display invisible properties in this buffer? "))
19311 (org-toggle-custom-properties-visibility)
19312 ;; Make the area visible
19314 (when invisible-before-point
19316 (previous-single-char-property-change (point) 'invisible
)))
19317 ;; Remove whatever overlay is currently making yet-to-be
19318 ;; edited text invisible. Also remove nested invisibility
19319 ;; related overlays.
19320 (delete-overlay (or invisible-at-point invisible-before-point
))
19321 (let ((origin (if invisible-at-point
(point) (1- (point)))))
19322 (while (pcase (get-char-property-and-overlay origin
'invisible
)
19323 (`(,_ .
,(and (pred overlayp
) o
))
19327 ((eq org-catch-invisible-edits
'show
)
19328 ;; That's it, we do the edit after showing
19330 "Unfolding invisible region around point before editing")
19332 ((and (eq org-catch-invisible-edits
'smart
)
19333 border-and-ok-direction
)
19334 (message "Unfolding invisible region around point before editing"))
19336 ;; Don't do the edit, make the user repeat it in full visibility
19337 (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
19339 (defun org-fix-tags-on-the-fly ()
19340 "Align tags in headline at point.
19341 Unlike to `org-set-tags', it ignores region and sorting."
19342 (when (and (eq (char-after (line-beginning-position)) ?
*) ;short-circuit
19343 (org-at-heading-p))
19344 (let ((org-ignore-region t
)
19345 (org-tags-sort-function nil
))
19346 (org-set-tags nil t
))))
19348 (defun org-delete-backward-char (N)
19349 "Like `delete-backward-char', insert whitespace at field end in tables.
19350 When deleting backwards, in tables this function will insert whitespace in
19351 front of the next \"|\" separator, to keep the table aligned. The table will
19352 still be marked for re-alignment if the field did fill the entire column,
19353 because, in this case the deletion might narrow the column."
19356 (org-check-before-invisible-edit 'delete-backward
)
19358 (not overwrite-mode
)
19359 (not (org-region-active-p))
19360 (not (eq (char-before) ?|
))
19361 (save-excursion (skip-chars-backward " \t") (not (bolp)))
19362 (looking-at-p ".*?|")
19364 (progn (forward-char -
1) (org-delete-char 1))
19365 (backward-delete-char N
)
19366 (org-fix-tags-on-the-fly))))
19368 (defun org-delete-char (N)
19369 "Like `delete-char', but insert whitespace at field end in tables.
19370 When deleting characters, in tables this function will insert whitespace in
19371 front of the next \"|\" separator, to keep the table aligned. The table will
19372 still be marked for re-alignment if the field did fill the entire column,
19373 because, in this case the deletion might narrow the column."
19376 (org-check-before-invisible-edit 'delete
)
19379 (eq (char-after) ?|
)
19380 (save-excursion (skip-chars-backward " \t") (bolp))
19381 (not (org-at-table-p)))
19383 (org-fix-tags-on-the-fly))
19384 ((looking-at ".\\(.*?\\)|")
19385 (let* ((update? org-table-may-need-update
)
19386 (noalign (looking-at-p ".*? |")))
19388 (org-table-with-shrunk-field
19390 ;; Last space is `org-table-separator-space', so insert
19391 ;; a regular one before it instead.
19392 (goto-char (- (match-end 0) 2))
19394 ;; If there were two spaces at the end, this field does not
19395 ;; determine the width of the column.
19396 (when noalign
(setq org-table-may-need-update update?
))))
19398 (delete-char N
)))))
19400 ;; Make `delete-selection-mode' work with Org mode and Orgtbl mode
19401 (put 'org-self-insert-command
'delete-selection
19403 (not (run-hook-with-args-until-success
19404 'self-insert-uses-region-functions
))))
19405 (put 'orgtbl-self-insert-command
'delete-selection
19407 (not (run-hook-with-args-until-success
19408 'self-insert-uses-region-functions
))))
19409 (put 'org-delete-char
'delete-selection
'supersede
)
19410 (put 'org-delete-backward-char
'delete-selection
'supersede
)
19411 (put 'org-yank
'delete-selection
'yank
)
19413 ;; Make `flyspell-mode' delay after some commands
19414 (put 'org-self-insert-command
'flyspell-delayed t
)
19415 (put 'orgtbl-self-insert-command
'flyspell-delayed t
)
19416 (put 'org-delete-char
'flyspell-delayed t
)
19417 (put 'org-delete-backward-char
'flyspell-delayed t
)
19419 ;; Make pabbrev-mode expand after Org mode commands
19420 (put 'org-self-insert-command
'pabbrev-expand-after-command t
)
19421 (put 'orgtbl-self-insert-command
'pabbrev-expand-after-command t
)
19423 (defun org-transpose-words ()
19424 "Transpose words for Org.
19425 This uses the `org-mode-transpose-word-syntax-table' syntax
19426 table, which interprets characters in `org-emphasis-alist' as
19427 word constituents."
19429 (with-syntax-table org-mode-transpose-word-syntax-table
19430 (call-interactively 'transpose-words
)))
19432 (defvar org-ctrl-c-ctrl-c-hook nil
19433 "Hook for functions attaching themselves to `C-c C-c'.
19435 This can be used to add additional functionality to the C-c C-c
19436 key which executes context-dependent commands. This hook is run
19437 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
19438 run after the last test.
19440 Each function will be called with no arguments. The function
19441 must check if the context is appropriate for it to act. If yes,
19442 it should do its thing and then return a non-nil value. If the
19443 context is wrong, just do nothing and return nil.")
19445 (defvar org-ctrl-c-ctrl-c-final-hook nil
19446 "Hook for functions attaching themselves to `C-c C-c'.
19448 This can be used to add additional functionality to the C-c C-c
19449 key which executes context-dependent commands. This hook is run
19450 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
19451 before the first test.
19453 Each function will be called with no arguments. The function
19454 must check if the context is appropriate for it to act. If yes,
19455 it should do its thing and then return a non-nil value. If the
19456 context is wrong, just do nothing and return nil.")
19458 (defvar org-tab-first-hook nil
19459 "Hook for functions to attach themselves to TAB.
19460 See `org-ctrl-c-ctrl-c-hook' for more information.
19461 This hook runs as the first action when TAB is pressed, even before
19462 `org-cycle' messes around with the `outline-regexp' to cater for
19463 inline tasks and plain list item folding.
19464 If any function in this hook returns t, any other actions that
19465 would have been caused by TAB (such as table field motion or visibility
19466 cycling) will not occur.")
19468 (defvar org-tab-after-check-for-table-hook nil
19469 "Hook for functions to attach themselves to TAB.
19470 See `org-ctrl-c-ctrl-c-hook' for more information.
19471 This hook runs after it has been established that the cursor is not in a
19472 table, but before checking if the cursor is in a headline or if global cycling
19474 If any function in this hook returns t, not other actions like visibility
19475 cycling will be done.")
19477 (defvar org-tab-after-check-for-cycling-hook nil
19478 "Hook for functions to attach themselves to TAB.
19479 See `org-ctrl-c-ctrl-c-hook' for more information.
19480 This hook runs after it has been established that not table field motion and
19481 not visibility should be done because of current context. This is probably
19482 the place where a package like yasnippets can hook in.")
19484 (defvar org-tab-before-tab-emulation-hook nil
19485 "Hook for functions to attach themselves to TAB.
19486 See `org-ctrl-c-ctrl-c-hook' for more information.
19487 This hook runs after every other options for TAB have been exhausted, but
19488 before indentation and \t insertion takes place.")
19490 (defvar org-metaleft-hook nil
19491 "Hook for functions attaching themselves to `M-left'.
19492 See `org-ctrl-c-ctrl-c-hook' for more information.")
19493 (defvar org-metaright-hook nil
19494 "Hook for functions attaching themselves to `M-right'.
19495 See `org-ctrl-c-ctrl-c-hook' for more information.")
19496 (defvar org-metaup-hook nil
19497 "Hook for functions attaching themselves to `M-up'.
19498 See `org-ctrl-c-ctrl-c-hook' for more information.")
19499 (defvar org-metadown-hook nil
19500 "Hook for functions attaching themselves to `M-down'.
19501 See `org-ctrl-c-ctrl-c-hook' for more information.")
19502 (defvar org-shiftmetaleft-hook nil
19503 "Hook for functions attaching themselves to `M-S-left'.
19504 See `org-ctrl-c-ctrl-c-hook' for more information.")
19505 (defvar org-shiftmetaright-hook nil
19506 "Hook for functions attaching themselves to `M-S-right'.
19507 See `org-ctrl-c-ctrl-c-hook' for more information.")
19508 (defvar org-shiftmetaup-hook nil
19509 "Hook for functions attaching themselves to `M-S-up'.
19510 See `org-ctrl-c-ctrl-c-hook' for more information.")
19511 (defvar org-shiftmetadown-hook nil
19512 "Hook for functions attaching themselves to `M-S-down'.
19513 See `org-ctrl-c-ctrl-c-hook' for more information.")
19514 (defvar org-metareturn-hook nil
19515 "Hook for functions attaching themselves to `M-RET'.
19516 See `org-ctrl-c-ctrl-c-hook' for more information.")
19517 (defvar org-shiftup-hook nil
19518 "Hook for functions attaching themselves to `S-up'.
19519 See `org-ctrl-c-ctrl-c-hook' for more information.")
19520 (defvar org-shiftup-final-hook nil
19521 "Hook for functions attaching themselves to `S-up'.
19522 This one runs after all other options except shift-select have been excluded.
19523 See `org-ctrl-c-ctrl-c-hook' for more information.")
19524 (defvar org-shiftdown-hook nil
19525 "Hook for functions attaching themselves to `S-down'.
19526 See `org-ctrl-c-ctrl-c-hook' for more information.")
19527 (defvar org-shiftdown-final-hook nil
19528 "Hook for functions attaching themselves to `S-down'.
19529 This one runs after all other options except shift-select have been excluded.
19530 See `org-ctrl-c-ctrl-c-hook' for more information.")
19531 (defvar org-shiftleft-hook nil
19532 "Hook for functions attaching themselves to `S-left'.
19533 See `org-ctrl-c-ctrl-c-hook' for more information.")
19534 (defvar org-shiftleft-final-hook nil
19535 "Hook for functions attaching themselves to `S-left'.
19536 This one runs after all other options except shift-select have been excluded.
19537 See `org-ctrl-c-ctrl-c-hook' for more information.")
19538 (defvar org-shiftright-hook nil
19539 "Hook for functions attaching themselves to `S-right'.
19540 See `org-ctrl-c-ctrl-c-hook' for more information.")
19541 (defvar org-shiftright-final-hook nil
19542 "Hook for functions attaching themselves to `S-right'.
19543 This one runs after all other options except shift-select have been excluded.
19544 See `org-ctrl-c-ctrl-c-hook' for more information.")
19546 (defun org-modifier-cursor-error ()
19547 "Throw an error, a modified cursor command was applied in wrong context."
19548 (user-error "This command is active in special context like tables, headlines or items"))
19550 (defun org-shiftselect-error ()
19551 "Throw an error because Shift-Cursor command was applied in wrong context."
19552 (if (and (boundp 'shift-select-mode
) shift-select-mode
)
19553 (user-error "To use shift-selection with Org mode, customize `org-support-shift-select'")
19554 (user-error "This command works only in special context like headlines or timestamps")))
19556 (defun org-call-for-shift-select (cmd)
19557 (let ((this-command-keys-shift-translated t
))
19558 (call-interactively cmd
)))
19560 (defun org-shifttab (&optional arg
)
19561 "Global visibility cycling or move to previous table field.
19562 Call `org-table-previous-field' within a table.
19563 When ARG is nil, cycle globally through visibility states.
19564 When ARG is a numeric prefix, show contents of this level."
19567 ((org-at-table-p) (call-interactively 'org-table-previous-field
))
19569 (let ((arg2 (if org-odd-levels-only
(1- (* 2 arg
)) arg
)))
19570 (message "Content view to level: %d" arg
)
19571 (org-content (prefix-numeric-value arg2
))
19572 (org-cycle-show-empty-lines t
)
19573 (setq org-cycle-global-status
'overview
)))
19574 (t (call-interactively 'org-global-cycle
))))
19576 (defun org-shiftmetaleft ()
19577 "Promote subtree or delete table column.
19578 Calls `org-promote-subtree', `org-outdent-item-tree', or
19579 `org-table-delete-column', depending on context. See the
19580 individual commands for more information."
19583 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook
))
19584 ((org-at-table-p) (call-interactively 'org-table-delete-column
))
19585 ((org-at-heading-p) (call-interactively 'org-promote-subtree
))
19586 ((if (not (org-region-active-p)) (org-at-item-p)
19587 (save-excursion (goto-char (region-beginning))
19589 (call-interactively 'org-outdent-item-tree
))
19590 (t (org-modifier-cursor-error))))
19592 (defun org-shiftmetaright ()
19593 "Demote subtree or insert table column.
19594 Calls `org-demote-subtree', `org-indent-item-tree', or
19595 `org-table-insert-column', depending on context. See the
19596 individual commands for more information."
19599 ((run-hook-with-args-until-success 'org-shiftmetaright-hook
))
19600 ((org-at-table-p) (call-interactively 'org-table-insert-column
))
19601 ((org-at-heading-p) (call-interactively 'org-demote-subtree
))
19602 ((if (not (org-region-active-p)) (org-at-item-p)
19603 (save-excursion (goto-char (region-beginning))
19605 (call-interactively 'org-indent-item-tree
))
19606 (t (org-modifier-cursor-error))))
19608 (defun org-shiftmetaup (&optional _arg
)
19609 "Drag the line at point up.
19610 In a table, kill the current row.
19611 On a clock timestamp, update the value of the timestamp like `S-<up>'
19612 but also adjust the previous clocked item in the clock history.
19613 Everywhere else, drag the line at point up."
19616 ((run-hook-with-args-until-success 'org-shiftmetaup-hook
))
19617 ((org-at-table-p) (call-interactively 'org-table-kill-row
))
19618 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t
))
19619 (call-interactively 'org-timestamp-up
)))
19620 (t (call-interactively 'org-drag-line-backward
))))
19622 (defun org-shiftmetadown (&optional _arg
)
19623 "Drag the line at point down.
19624 In a table, insert an empty row at the current line.
19625 On a clock timestamp, update the value of the timestamp like `S-<down>'
19626 but also adjust the previous clocked item in the clock history.
19627 Everywhere else, drag the line at point down."
19630 ((run-hook-with-args-until-success 'org-shiftmetadown-hook
))
19631 ((org-at-table-p) (call-interactively 'org-table-insert-row
))
19632 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t
))
19633 (call-interactively 'org-timestamp-down
)))
19634 (t (call-interactively 'org-drag-line-forward
))))
19636 (defsubst org-hidden-tree-error
()
19638 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
19640 (defun org-metaleft (&optional _arg
)
19641 "Promote heading, list item at point or move table column left.
19643 Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
19644 depending on context. With no specific context, calls the Emacs
19645 default `backward-word'. See the individual commands for more
19648 This function runs the hook `org-metaleft-hook' as a first step,
19649 and returns at first non-nil value."
19652 ((run-hook-with-args-until-success 'org-metaleft-hook
))
19653 ((org-at-table-p) (org-call-with-arg 'org-table-move-column
'left
))
19654 ((org-with-limited-levels
19655 (or (org-at-heading-p)
19656 (and (org-region-active-p)
19658 (goto-char (region-beginning))
19659 (org-at-heading-p)))))
19660 (when (org-check-for-hidden 'headlines
) (org-hidden-tree-error))
19661 (call-interactively 'org-do-promote
))
19662 ;; At an inline task.
19663 ((org-at-heading-p)
19664 (call-interactively 'org-inlinetask-promote
))
19665 ((or (org-at-item-p)
19666 (and (org-region-active-p)
19668 (goto-char (region-beginning))
19670 (when (org-check-for-hidden 'items
) (org-hidden-tree-error))
19671 (call-interactively 'org-outdent-item
))
19672 (t (call-interactively 'backward-word
))))
19674 (defun org-metaright (&optional _arg
)
19675 "Demote heading, list item at point or move table column right.
19677 In front of a drawer or a block keyword, indent it correctly.
19679 Calls `org-do-demote', `org-indent-item', `org-table-move-column',
19680 `org-indent-drawer' or `org-indent-block' depending on context.
19681 With no specific context, calls the Emacs default `forward-word'.
19682 See the individual commands for more information.
19684 This function runs the hook `org-metaright-hook' as a first step,
19685 and returns at first non-nil value."
19688 ((run-hook-with-args-until-success 'org-metaright-hook
))
19689 ((org-at-table-p) (call-interactively 'org-table-move-column
))
19690 ((org-at-drawer-p) (call-interactively 'org-indent-drawer
))
19691 ((org-at-block-p) (call-interactively 'org-indent-block
))
19692 ((org-with-limited-levels
19693 (or (org-at-heading-p)
19694 (and (org-region-active-p)
19696 (goto-char (region-beginning))
19697 (org-at-heading-p)))))
19698 (when (org-check-for-hidden 'headlines
) (org-hidden-tree-error))
19699 (call-interactively 'org-do-demote
))
19700 ;; At an inline task.
19701 ((org-at-heading-p)
19702 (call-interactively 'org-inlinetask-demote
))
19703 ((or (org-at-item-p)
19704 (and (org-region-active-p)
19706 (goto-char (region-beginning))
19708 (when (org-check-for-hidden 'items
) (org-hidden-tree-error))
19709 (call-interactively 'org-indent-item
))
19710 (t (call-interactively 'forward-word
))))
19712 (defun org-check-for-hidden (what)
19713 "Check if there are hidden headlines/items in the current visual line.
19714 WHAT can be either `headlines' or `items'. If the current line is
19715 an outline or item heading and it has a folded subtree below it,
19716 this function returns t, nil otherwise."
19718 ((eq what
'headlines
) org-outline-regexp-bol
)
19719 ((eq what
'items
) (org-item-beginning-re))
19720 (t (error "This should not happen"))))
19724 (unless (org-region-active-p)
19725 (setq beg
(point-at-bol))
19726 (beginning-of-line 2)
19727 (while (and (not (eobp)) ;; this is like `next-line'
19728 (get-char-property (1- (point)) 'invisible
))
19729 (beginning-of-line 2))
19732 (goto-char (point-at-eol))
19733 (setq end
(max end
(point)))
19734 (while (re-search-forward re end t
)
19735 (when (get-char-property (match-beginning 0) 'invisible
)
19739 (defun org-metaup (&optional _arg
)
19740 "Move subtree up or move table row up.
19741 Calls `org-move-subtree-up' or `org-table-move-row' or
19742 `org-move-item-up', depending on context. See the individual commands
19743 for more information."
19746 ((run-hook-with-args-until-success 'org-metaup-hook
))
19747 ((org-region-active-p)
19748 (let* ((a (min (region-beginning) (region-end)))
19749 (b (1- (max (region-beginning) (region-end))))
19750 (c (save-excursion (goto-char a
)
19751 (move-beginning-of-line 0)))
19752 (d (save-excursion (goto-char a
)
19753 (move-end-of-line 0) (point))))
19754 (transpose-regions a b c d
)
19756 ((org-at-table-p) (org-call-with-arg 'org-table-move-row
'up
))
19757 ((org-at-heading-p) (call-interactively 'org-move-subtree-up
))
19758 ((org-at-item-p) (call-interactively 'org-move-item-up
))
19759 (t (org-drag-element-backward))))
19761 (defun org-metadown (&optional _arg
)
19762 "Move subtree down or move table row down.
19763 Calls `org-move-subtree-down' or `org-table-move-row' or
19764 `org-move-item-down', depending on context. See the individual
19765 commands for more information."
19768 ((run-hook-with-args-until-success 'org-metadown-hook
))
19769 ((org-region-active-p)
19770 (let* ((a (min (region-beginning) (region-end)))
19771 (b (max (region-beginning) (region-end)))
19772 (c (save-excursion (goto-char b
)
19773 (move-beginning-of-line 1)))
19774 (d (save-excursion (goto-char b
)
19775 (move-end-of-line 1) (1+ (point)))))
19776 (transpose-regions a b c d
)
19778 ((org-at-table-p) (call-interactively 'org-table-move-row
))
19779 ((org-at-heading-p) (call-interactively 'org-move-subtree-down
))
19780 ((org-at-item-p) (call-interactively 'org-move-item-down
))
19781 (t (org-drag-element-forward))))
19783 (defun org-shiftup (&optional arg
)
19784 "Increase item in timestamp or increase priority of current headline.
19785 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
19786 depending on context. See the individual commands for more information."
19789 ((run-hook-with-args-until-success 'org-shiftup-hook
))
19790 ((and org-support-shift-select
(org-region-active-p))
19791 (org-call-for-shift-select 'previous-line
))
19792 ((org-at-timestamp-p 'lax
)
19793 (call-interactively (if org-edit-timestamp-down-means-later
19794 'org-timestamp-down
'org-timestamp-up
)))
19795 ((and (not (eq org-support-shift-select
'always
))
19796 org-enable-priority-commands
19797 (org-at-heading-p))
19798 (call-interactively 'org-priority-up
))
19799 ((and (not org-support-shift-select
) (org-at-item-p))
19800 (call-interactively 'org-previous-item
))
19801 ((org-clocktable-try-shift 'up arg
))
19802 ((run-hook-with-args-until-success 'org-shiftup-final-hook
))
19803 (org-support-shift-select
19804 (org-call-for-shift-select 'previous-line
))
19805 (t (org-shiftselect-error))))
19807 (defun org-shiftdown (&optional arg
)
19808 "Decrease item in timestamp or decrease priority of current headline.
19809 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
19810 depending on context. See the individual commands for more information."
19813 ((run-hook-with-args-until-success 'org-shiftdown-hook
))
19814 ((and org-support-shift-select
(org-region-active-p))
19815 (org-call-for-shift-select 'next-line
))
19816 ((org-at-timestamp-p 'lax
)
19817 (call-interactively (if org-edit-timestamp-down-means-later
19818 'org-timestamp-up
'org-timestamp-down
)))
19819 ((and (not (eq org-support-shift-select
'always
))
19820 org-enable-priority-commands
19821 (org-at-heading-p))
19822 (call-interactively 'org-priority-down
))
19823 ((and (not org-support-shift-select
) (org-at-item-p))
19824 (call-interactively 'org-next-item
))
19825 ((org-clocktable-try-shift 'down arg
))
19826 ((run-hook-with-args-until-success 'org-shiftdown-final-hook
))
19827 (org-support-shift-select
19828 (org-call-for-shift-select 'next-line
))
19829 (t (org-shiftselect-error))))
19831 (defun org-shiftright (&optional arg
)
19832 "Cycle the thing at point or in the current line, depending on context.
19833 Depending on context, this does one of the following:
19835 - switch a timestamp at point one day into the future
19836 - on a headline, switch to the next TODO keyword.
19837 - on an item, switch entire list to the next bullet type
19838 - on a property line, switch to the next allowed value
19839 - on a clocktable definition line, move time block into the future"
19842 ((run-hook-with-args-until-success 'org-shiftright-hook
))
19843 ((and org-support-shift-select
(org-region-active-p))
19844 (org-call-for-shift-select 'forward-char
))
19845 ((org-at-timestamp-p 'lax
) (call-interactively 'org-timestamp-up-day
))
19846 ((and (not (eq org-support-shift-select
'always
))
19847 (org-at-heading-p))
19848 (let ((org-inhibit-logging
19849 (not org-treat-S-cursor-todo-selection-as-state-change
))
19850 (org-inhibit-blocking
19851 (not org-treat-S-cursor-todo-selection-as-state-change
)))
19852 (org-call-with-arg 'org-todo
'right
)))
19853 ((or (and org-support-shift-select
19854 (not (eq org-support-shift-select
'always
))
19855 (org-at-item-bullet-p))
19856 (and (not org-support-shift-select
) (org-at-item-p)))
19857 (org-call-with-arg 'org-cycle-list-bullet nil
))
19858 ((and (not (eq org-support-shift-select
'always
))
19859 (org-at-property-p))
19860 (call-interactively 'org-property-next-allowed-value
))
19861 ((org-clocktable-try-shift 'right arg
))
19862 ((run-hook-with-args-until-success 'org-shiftright-final-hook
))
19863 (org-support-shift-select
19864 (org-call-for-shift-select 'forward-char
))
19865 (t (org-shiftselect-error))))
19867 (defun org-shiftleft (&optional arg
)
19868 "Cycle the thing at point or in the current line, depending on context.
19869 Depending on context, this does one of the following:
19871 - switch a timestamp at point one day into the past
19872 - on a headline, switch to the previous TODO keyword.
19873 - on an item, switch entire list to the previous bullet type
19874 - on a property line, switch to the previous allowed value
19875 - on a clocktable definition line, move time block into the past"
19878 ((run-hook-with-args-until-success 'org-shiftleft-hook
))
19879 ((and org-support-shift-select
(org-region-active-p))
19880 (org-call-for-shift-select 'backward-char
))
19881 ((org-at-timestamp-p 'lax
) (call-interactively 'org-timestamp-down-day
))
19882 ((and (not (eq org-support-shift-select
'always
))
19883 (org-at-heading-p))
19884 (let ((org-inhibit-logging
19885 (not org-treat-S-cursor-todo-selection-as-state-change
))
19886 (org-inhibit-blocking
19887 (not org-treat-S-cursor-todo-selection-as-state-change
)))
19888 (org-call-with-arg 'org-todo
'left
)))
19889 ((or (and org-support-shift-select
19890 (not (eq org-support-shift-select
'always
))
19891 (org-at-item-bullet-p))
19892 (and (not org-support-shift-select
) (org-at-item-p)))
19893 (org-call-with-arg 'org-cycle-list-bullet
'previous
))
19894 ((and (not (eq org-support-shift-select
'always
))
19895 (org-at-property-p))
19896 (call-interactively 'org-property-previous-allowed-value
))
19897 ((org-clocktable-try-shift 'left arg
))
19898 ((run-hook-with-args-until-success 'org-shiftleft-final-hook
))
19899 (org-support-shift-select
19900 (org-call-for-shift-select 'backward-char
))
19901 (t (org-shiftselect-error))))
19903 (defun org-shiftcontrolright ()
19904 "Switch to next TODO set."
19907 ((and org-support-shift-select
(org-region-active-p))
19908 (org-call-for-shift-select 'forward-word
))
19909 ((and (not (eq org-support-shift-select
'always
))
19910 (org-at-heading-p))
19911 (org-call-with-arg 'org-todo
'nextset
))
19912 (org-support-shift-select
19913 (org-call-for-shift-select 'forward-word
))
19914 (t (org-shiftselect-error))))
19916 (defun org-shiftcontrolleft ()
19917 "Switch to previous TODO set."
19920 ((and org-support-shift-select
(org-region-active-p))
19921 (org-call-for-shift-select 'backward-word
))
19922 ((and (not (eq org-support-shift-select
'always
))
19923 (org-at-heading-p))
19924 (org-call-with-arg 'org-todo
'previousset
))
19925 (org-support-shift-select
19926 (org-call-for-shift-select 'backward-word
))
19927 (t (org-shiftselect-error))))
19929 (defun org-shiftcontrolup (&optional n
)
19930 "Change timestamps synchronously up in CLOCK log lines.
19931 Optional argument N tells to change by that many units."
19933 (if (and (org-at-clock-log-p) (org-at-timestamp-p 'lax
))
19934 (let (org-support-shift-select)
19935 (org-clock-timestamps-up n
))
19936 (user-error "Not at a clock log")))
19938 (defun org-shiftcontroldown (&optional n
)
19939 "Change timestamps synchronously down in CLOCK log lines.
19940 Optional argument N tells to change by that many units."
19942 (if (and (org-at-clock-log-p) (org-at-timestamp-p 'lax
))
19943 (let (org-support-shift-select)
19944 (org-clock-timestamps-down n
))
19945 (user-error "Not at a clock log")))
19947 (defun org-increase-number-at-point (&optional inc
)
19948 "Increment the number at point.
19949 With an optional prefix numeric argument INC, increment using
19950 this numeric value."
19952 (if (not (number-at-point))
19953 (user-error "Not on a number")
19954 (unless inc
(setq inc
1))
19955 (let ((pos (point))
19956 (beg (skip-chars-backward "-+^/*0-9eE."))
19957 (end (skip-chars-forward "-+^/*0-9eE^.")) nap
)
19958 (setq nap
(buffer-substring-no-properties
19959 (+ pos beg
) (+ pos beg end
)))
19960 (delete-region (+ pos beg
) (+ pos beg end
))
19961 (insert (calc-eval (concat (number-to-string inc
) "+" nap
))))
19962 (when (org-at-table-p)
19964 (org-table-end-of-field 1))))
19966 (defun org-decrease-number-at-point (&optional inc
)
19967 "Decrement the number at point.
19968 With an optional prefix numeric argument INC, decrement using
19969 this numeric value."
19971 (org-increase-number-at-point (- (or inc
1))))
19973 (defun org-ctrl-c-ret ()
19974 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
19977 ((org-at-table-p) (call-interactively 'org-table-hline-and-move
))
19978 (t (call-interactively 'org-insert-heading
))))
19980 (defun org-find-visible ()
19982 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
19983 (get-char-property s
'invisible
)))
19985 (defun org-find-invisible ()
19987 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
19988 (not (get-char-property s
'invisible
))))
19991 (defun org-copy-visible (beg end
)
19992 "Copy the visible parts of the region."
19995 (while (/= beg end
)
19996 (when (get-char-property beg
'invisible
)
19997 (setq beg
(next-single-char-property-change beg
'invisible nil end
)))
19998 (let ((next (next-single-char-property-change beg
'invisible nil end
)))
19999 (setq result
(concat result
(buffer-substring beg next
)))
20001 (kill-new result
)))
20003 (defun org-copy-special ()
20004 "Copy region in table or copy current subtree.
20005 Calls `org-table-copy-region' or `org-copy-subtree', depending on
20006 context. See the individual commands for more information."
20008 (call-interactively
20009 (if (org-at-table-p) #'org-table-copy-region
#'org-copy-subtree
)))
20011 (defun org-cut-special ()
20012 "Cut region in table or cut current subtree.
20013 Calls `org-table-cut-region' or `org-cut-subtree', depending on
20014 context. See the individual commands for more information."
20016 (call-interactively
20017 (if (org-at-table-p) #'org-table-cut-region
#'org-cut-subtree
)))
20019 (defun org-paste-special (arg)
20020 "Paste rectangular region into table, or past subtree relative to level.
20021 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
20022 See the individual commands for more information."
20024 (if (org-at-table-p)
20025 (org-table-paste-rectangle)
20026 (org-paste-subtree arg
)))
20028 (defun org-edit-special (&optional arg
)
20029 "Call a special editor for the element at point.
20030 When at a table, call the formula editor with `org-table-edit-formulas'.
20031 When in a source code block, call `org-edit-src-code'.
20032 When in a fixed-width region, call `org-edit-fixed-width-region'.
20033 When in an export block, call `org-edit-export-block'.
20034 When in a LaTeX environment, call `org-edit-latex-environment'.
20035 When at an #+INCLUDE keyword, visit the included file.
20036 When at a footnote reference, call `org-edit-footnote-reference'
20037 On a link, call `ffap' to visit the link at point.
20038 Otherwise, return a user error."
20040 (let ((element (org-element-at-point)))
20041 (barf-if-buffer-read-only)
20042 (pcase (org-element-type element
)
20044 (if (not arg
) (org-edit-src-code)
20045 (let* ((info (org-babel-get-src-block-info))
20046 (lang (nth 0 info
))
20047 (params (nth 2 info
))
20048 (session (cdr (assq :session params
))))
20049 (if (not session
) (org-edit-src-code)
20050 ;; At a src-block with a session and function called with
20051 ;; an ARG: switch to the buffer related to the inferior
20054 (funcall (intern (concat "org-babel-prep-session:" lang
))
20055 session params
))))))
20057 (if (member (org-element-property :key element
) '("INCLUDE" "SETUPFILE"))
20058 (org-open-link-from-string
20061 (let ((value (org-element-property :value element
)))
20062 (cond ((org-file-url-p value
)
20063 (user-error "The file is specified as a URL, cannot be edited"))
20064 ((not (org-string-nw-p value
))
20065 (user-error "No file to edit"))
20066 ((string-match "\\`\"\\(.*?\\)\"" value
)
20067 (match-string 1 value
))
20068 ((string-match "\\`[^ \t\"]\\S-*" value
)
20069 (match-string 0 value
))
20070 (t (user-error "No valid file specified")))))))
20071 (user-error "No special environment to edit here")))
20073 (if (eq (org-element-property :type element
) 'table.el
)
20074 (org-edit-table.el
)
20075 (call-interactively 'org-table-edit-formulas
)))
20076 ;; Only Org tables contain `table-row' type elements.
20077 (`table-row
(call-interactively 'org-table-edit-formulas
))
20078 (`example-block
(org-edit-src-code))
20079 (`export-block
(org-edit-export-block))
20080 (`fixed-width
(org-edit-fixed-width-region))
20081 (`latex-environment
(org-edit-latex-environment))
20083 ;; No notable element at point. Though, we may be at a link or
20084 ;; a footnote reference, which are objects. Thus, scan deeper.
20085 (let ((context (org-element-context element
)))
20086 (pcase (org-element-type context
)
20087 (`footnote-reference
(org-edit-footnote-reference))
20088 (`inline-src-block
(org-edit-inline-src-code))
20089 (`link
(call-interactively #'ffap
))
20090 (_ (user-error "No special environment to edit here"))))))))
20092 (defvar org-table-coordinate-overlays
) ; defined in org-table.el
20093 (defun org-ctrl-c-ctrl-c (&optional arg
)
20094 "Set tags in headline, or update according to changed information at point.
20096 This command does many different things, depending on context:
20098 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
20099 this is what we do.
20101 - If the cursor is on a statistics cookie, update it.
20103 - If the cursor is in a headline, prompt for tags and insert them
20104 into the current line, aligned to `org-tags-column'. When called
20105 with prefix arg, realign all tags in the current buffer.
20107 - If the cursor is in one of the special #+KEYWORD lines, this
20108 triggers scanning the buffer for these lines and updating the
20111 - If the cursor is inside a table, realign the table. This command
20112 works even if the automatic table editor has been turned off.
20114 - If the cursor is on a #+TBLFM line, re-apply the formulas to
20117 - If the cursor is at a footnote reference or definition, jump to
20118 the corresponding definition or references, respectively.
20120 - If the cursor is a the beginning of a dynamic block, update it.
20122 - If the current buffer is a capture buffer, close note and file it.
20124 - If the cursor is on a <<<target>>>, update radio targets and
20125 corresponding links in this buffer.
20127 - If the cursor is on a numbered item in a plain list, renumber the
20130 - If the cursor is on a checkbox, toggle it.
20132 - If the cursor is on a code block, evaluate it. The variable
20133 `org-confirm-babel-evaluate' can be used to control prompting
20134 before code block evaluation, by default every code block
20135 evaluation requires confirmation. Code block evaluation can be
20136 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
20139 ((or (bound-and-true-p org-clock-overlays
) org-occur-highlights
)
20140 (when (boundp 'org-clock-overlays
) (org-clock-remove-overlays))
20141 (org-remove-occur-highlights)
20142 (message "Temporary highlights/overlays removed from current buffer"))
20143 ((and (local-variable-p 'org-finish-function
)
20144 (fboundp org-finish-function
))
20145 (funcall org-finish-function
))
20146 ((org-babel-hash-at-point))
20147 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook
))
20150 (org-element-lineage
20151 (org-element-context)
20152 ;; Limit to supported contexts.
20153 '(babel-call clock dynamic-block footnote-definition
20154 footnote-reference inline-babel-call inline-src-block
20155 inlinetask item keyword node-property paragraph
20156 plain-list planning property-drawer radio-target
20157 src-block statistics-cookie table table-cell table-row
20160 (type (org-element-type context
)))
20161 ;; For convenience: at the first line of a paragraph on the same
20162 ;; line as an item, apply function on that item instead.
20163 (when (eq type
'paragraph
)
20164 (let ((parent (org-element-property :parent context
)))
20165 (when (and (eq (org-element-type parent
) 'item
)
20166 (= (line-beginning-position)
20167 (org-element-property :begin parent
)))
20168 (setq context parent
)
20169 (setq type
'item
))))
20170 ;; Act according to type of element or object at point.
20172 ;; Do nothing on a blank line, except if it is contained in
20173 ;; a src block. Hence, we first check if point is in such
20174 ;; a block and then if it is at a blank line.
20176 ((or `inline-src-block
`src-block
)
20177 (unless org-babel-no-eval-on-ctrl-c-ctrl-c
20178 (org-babel-eval-wipe-error-buffer)
20179 (org-babel-execute-src-block
20180 current-prefix-arg
(org-babel-get-src-block-info nil context
))))
20181 ((guard (org-match-line "[ \t]*$"))
20182 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook
)
20184 (substitute-command-keys
20185 "`\\[org-ctrl-c-ctrl-c]' can do nothing useful here"))))
20186 ((or `babel-call
`inline-babel-call
)
20187 (let ((info (org-babel-lob-get-info context
)))
20188 (when info
(org-babel-execute-src-block nil info
))))
20189 (`clock
(org-clock-update-time-maybe))
20192 (goto-char (org-element-property :post-affiliated context
))
20193 (org-update-dblock)))
20194 (`footnote-definition
20195 (goto-char (org-element-property :post-affiliated context
))
20196 (call-interactively 'org-footnote-action
))
20197 (`footnote-reference
(call-interactively #'org-footnote-action
))
20198 ((or `headline
`inlinetask
)
20199 (save-excursion (goto-char (org-element-property :begin context
))
20200 (call-interactively #'org-set-tags
)))
20202 ;; At an item: `C-u C-u' sets checkbox to "[-]"
20203 ;; unconditionally, whereas `C-u' will toggle its presence.
20204 ;; Without a universal argument, if the item has a checkbox,
20205 ;; toggle it. Otherwise repair the list.
20206 (let* ((box (org-element-property :checkbox context
))
20207 (struct (org-element-property :structure context
))
20208 (old-struct (copy-tree struct
))
20209 (parents (org-list-parents-alist struct
))
20210 (prevs (org-list-prevs-alist struct
))
20211 (orderedp (org-not-nil (org-entry-get nil
"ORDERED"))))
20212 (org-list-set-checkbox
20213 (org-element-property :begin context
) struct
20214 (cond ((equal arg
'(16)) "[-]")
20215 ((and (not box
) (equal arg
'(4))) "[ ]")
20216 ((or (not box
) (equal arg
'(4))) nil
)
20217 ((eq box
'on
) "[ ]")
20219 ;; Mimic `org-list-write-struct' but with grabbing a return
20220 ;; value from `org-list-struct-fix-box'.
20221 (org-list-struct-fix-ind struct parents
2)
20222 (org-list-struct-fix-item-end struct
)
20223 (org-list-struct-fix-bul struct prevs
)
20224 (org-list-struct-fix-ind struct parents
)
20226 (org-list-struct-fix-box struct parents prevs orderedp
)))
20227 (if (and box
(equal struct old-struct
))
20228 (if (equal arg
'(16))
20229 (message "Checkboxes already reset")
20230 (user-error "Cannot toggle this checkbox: %s"
20232 "all subitems checked"
20233 "unchecked subitems")))
20234 (org-list-struct-apply-struct struct old-struct
)
20235 (org-update-checkbox-count-maybe))
20237 (message "Checkboxes were removed due to empty box at line %d"
20238 (org-current-line block-item
))))))
20240 (let ((org-inhibit-startup-visibility-stuff t
)
20241 (org-startup-align-all-tables nil
))
20242 (when (boundp 'org-table-coordinate-overlays
)
20243 (mapc #'delete-overlay org-table-coordinate-overlays
)
20244 (setq org-table-coordinate-overlays nil
))
20245 (org-save-outline-visibility 'use-markers
(org-mode-restart)))
20246 (message "Local setup has been refreshed"))
20248 ;; At a plain list, with a double C-u argument, set
20249 ;; checkboxes of each item to "[-]", whereas a single one
20250 ;; will toggle their presence according to the state of the
20251 ;; first item in the list. Without an argument, repair the
20253 (let* ((begin (org-element-property :contents-begin context
))
20254 (struct (org-element-property :structure context
))
20255 (old-struct (copy-tree struct
))
20256 (first-box (save-excursion
20258 (looking-at org-list-full-item-re
)
20259 (match-string-no-properties 3)))
20260 (new-box (cond ((equal arg
'(16)) "[-]")
20261 ((equal arg
'(4)) (unless first-box
"[ ]"))
20262 ((equal first-box
"[X]") "[ ]")
20267 (org-list-get-all-items
20268 begin struct
(org-list-prevs-alist struct
)))
20269 (org-list-set-checkbox pos struct new-box
)))
20270 ((and first-box
(eq (point) begin
))
20271 ;; For convenience, when point is at bol on the first
20272 ;; item of the list and no argument is provided, simply
20273 ;; toggle checkbox of that item, if any.
20274 (org-list-set-checkbox begin struct new-box
)))
20275 (org-list-write-struct
20276 struct
(org-list-parents-alist struct
) old-struct
)
20277 (org-update-checkbox-count-maybe)))
20278 ((or `property-drawer
`node-property
)
20279 (call-interactively #'org-property-action
))
20281 (call-interactively #'org-update-radio-target-regexp
))
20282 (`statistics-cookie
20283 (call-interactively #'org-update-statistics-cookies
))
20284 ((or `table
`table-cell
`table-row
)
20285 ;; At a table, recalculate every field and align it. Also
20286 ;; send the table if necessary. If the table has
20287 ;; a `table.el' type, just give up. At a table row or cell,
20288 ;; maybe recalculate line but always align table.
20289 (if (eq (org-element-property :type context
) 'table.el
)
20290 (message "%s" (substitute-command-keys "\\<org-mode-map>\
20291 Use `\\[org-edit-special]' to edit table.el tables"))
20292 (if (or (eq type
'table
)
20293 ;; Check if point is at a TBLFM line.
20294 (and (eq type
'table-row
)
20295 (= (point) (org-element-property :end context
))))
20297 (if (org-at-TBLFM-p)
20298 (progn (require 'org-table
)
20299 (org-table-calc-current-TBLFM))
20300 (goto-char (org-element-property :contents-begin context
))
20301 (org-call-with-arg 'org-table-recalculate
(or arg t
))
20302 (orgtbl-send-table 'maybe
)))
20303 (org-table-maybe-eval-formula)
20304 (cond (arg (call-interactively #'org-table-recalculate
))
20305 ((org-table-maybe-recalculate-line))
20306 (t (org-table-align))))))
20307 ((or `timestamp
(and `planning
(guard (org-at-timestamp-p 'lax
))))
20308 (org-timestamp-change 0 'day
))
20309 ((and `nil
(guard (org-at-heading-p)))
20310 ;; When point is on an unsupported object type, we can miss
20311 ;; the fact that it also is at a heading. Handle it here.
20312 (call-interactively #'org-set-tags
))
20314 (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook
)))
20317 (substitute-command-keys
20318 "`\\[org-ctrl-c-ctrl-c]' can do nothing useful here"))))))))
20320 (defun org-mode-restart ()
20322 (let ((indent-status (bound-and-true-p org-indent-mode
)))
20323 (funcall major-mode
)
20324 (hack-local-variables)
20325 (when (and indent-status
(not (bound-and-true-p org-indent-mode
)))
20326 (org-indent-mode -
1))
20327 (org-reset-file-cache))
20328 (message "%s restarted" major-mode
))
20330 (defun org-kill-note-or-show-branches ()
20331 "Abort storing current note, or call `outline-show-branches'."
20333 (if (not org-finish-function
)
20336 (org-narrow-to-subtree)
20337 (org-flag-subtree t
)
20338 (call-interactively 'outline-show-branches
)
20339 (org-hide-archived-subtrees (point-min) (point-max))))
20340 (let ((org-note-abort t
))
20341 (funcall org-finish-function
))))
20343 (defun org-delete-indentation (&optional arg
)
20344 "Join current line to previous and fix whitespace at join.
20346 If previous line is a headline add to headline title. Otherwise
20347 the function calls `delete-indentation'.
20349 With a non-nil optional argument, join it to the following one."
20351 (if (save-excursion
20352 (beginning-of-line (if arg
1 0))
20353 (let ((case-fold-search nil
))
20354 (looking-at org-complex-heading-regexp
)))
20356 (let ((tags-column (when (match-beginning 5)
20357 (save-excursion (goto-char (match-beginning 5))
20358 (current-column))))
20359 (string (concat " " (progn (when arg
(forward-line 1))
20360 (org-trim (delete-and-extract-region
20361 (line-beginning-position)
20362 (line-end-position)))))))
20363 (unless (bobp) (delete-region (point) (1- (point))))
20364 (goto-char (or (match-end 4)
20365 (match-beginning 5)
20367 (skip-chars-backward " \t")
20368 (save-excursion (insert string
))
20369 ;; Adjust alignment of tags.
20371 ((not tags-column
)) ;no tags
20372 (org-auto-align-tags (org-set-tags nil t
))
20373 (t (org--align-tags-here tags-column
)))) ;preserve tags column
20374 (delete-indentation arg
)))
20376 (defun org-open-line (n)
20377 "Insert a new row in tables, call `open-line' elsewhere.
20378 If `org-special-ctrl-o' is nil, just call `open-line' everywhere.
20379 As a special case, when a document starts with a table, allow to
20380 call `open-line' on the very first character."
20382 (if (and org-special-ctrl-o
(/= (point) 1) (org-at-table-p))
20383 (org-table-insert-row)
20386 (defun org-return (&optional indent
)
20387 "Goto next table row or insert a newline.
20389 Calls `org-table-next-row' or `newline', depending on context.
20391 When optional INDENT argument is non-nil, call
20392 `newline-and-indent' instead of `newline'.
20394 When `org-return-follows-link' is non-nil and point is on
20395 a timestamp or a link, call `org-open-at-point'. However, it
20396 will not happen if point is in a table or on a \"dead\"
20397 object (e.g., within a comment). In these case, you need to use
20398 `org-open-at-point' directly."
20400 (let ((context (if org-return-follows-link
(org-element-context)
20401 (org-element-at-point))))
20403 ;; In a table, call `org-table-next-row'. However, before first
20404 ;; column or after last one, split the table.
20405 ((or (and (eq (org-element-type context
) 'table
)
20406 (>= (point) (org-element-property :contents-begin context
))
20407 (< (point) (org-element-property :contents-end context
)))
20408 (org-element-lineage context
'(table-row table-cell
) t
))
20409 (if (or (looking-at-p "[ \t]*$")
20410 (save-excursion (skip-chars-backward " \t") (bolp)))
20412 (org-table-justify-field-maybe)
20413 (call-interactively #'org-table-next-row
)))
20414 ;; On a link or a timestamp, call `org-open-at-point' if
20415 ;; `org-return-follows-link' allows it. Tolerate fuzzy
20416 ;; locations, e.g., in a comment, as `org-open-at-point'.
20417 ((and org-return-follows-link
20418 (or (org-in-regexp org-ts-regexp-both nil t
)
20419 (org-in-regexp org-tsr-regexp-both nil t
)
20420 (org-in-regexp org-any-link-re nil t
)))
20421 (call-interactively #'org-open-at-point
))
20422 ;; Insert newline in heading, but preserve tags.
20424 (save-excursion (beginning-of-line)
20425 (let ((case-fold-search nil
))
20426 (looking-at org-complex-heading-regexp
))))
20427 ;; At headline. Split line. However, if point is on keyword,
20428 ;; priority cookie or tags, do not break any of them: add
20429 ;; a newline after the headline instead.
20430 (let ((tags-column (and (match-beginning 5)
20431 (save-excursion (goto-char (match-beginning 5))
20432 (current-column))))
20434 (when (and (match-end 4) (org-point-in-group (point) 4))
20435 (delete-and-extract-region (point) (match-end 4)))))
20436 ;; Adjust tag alignment.
20438 ((not (and tags-column string
)))
20439 (org-auto-align-tags (org-set-tags nil t
))
20440 (t (org--align-tags-here tags-column
))) ;preserve tags column
20443 (if indent
(newline-and-indent) (newline))
20444 (when string
(save-excursion (insert (org-trim string
))))))
20445 ;; In a list, make sure indenting keeps trailing text within.
20448 (org-element-lineage context
'(item)))
20449 (let ((trailing-data
20450 (delete-and-extract-region (point) (line-end-position))))
20451 (newline-and-indent)
20452 (save-excursion (insert trailing-data
))))
20454 ;; Do not auto-fill when point is in an Org property drawer.
20455 (let ((auto-fill-function (and (not (org-at-property-p))
20456 auto-fill-function
)))
20458 (newline-and-indent)
20461 (defun org-return-indent ()
20462 "Goto next table row or insert a newline and indent.
20463 Calls `org-table-next-row' or `newline-and-indent', depending on
20464 context. See the individual commands for more information."
20468 (defun org-ctrl-c-tab (&optional _arg
)
20469 "Toggle columns width in a table, or show children.
20470 Call `org-table-toggle-column-width' if point is in a table.
20471 Otherwise, call `org-show-children'."
20473 (call-interactively
20474 (if (org-at-table-p) #'org-table-toggle-column-width
20475 #'org-show-children
)))
20477 (defun org-ctrl-c-star ()
20478 "Compute table, or change heading status of lines.
20479 Calls `org-table-recalculate' or `org-toggle-heading',
20480 depending on context."
20484 (call-interactively 'org-table-recalculate
))
20486 ;; Convert all lines in region to list items
20487 (call-interactively 'org-toggle-heading
))))
20489 (defun org-ctrl-c-minus ()
20490 "Insert separator line in table or modify bullet status of line.
20491 Also turns a plain line or a region of lines into list items.
20492 Calls `org-table-insert-hline', `org-toggle-item', or
20493 `org-cycle-list-bullet', depending on context."
20497 (call-interactively 'org-table-insert-hline
))
20498 ((org-region-active-p)
20499 (call-interactively 'org-toggle-item
))
20501 (call-interactively 'org-cycle-list-bullet
))
20503 (call-interactively 'org-toggle-item
))))
20505 (defun org-toggle-heading (&optional nstars
)
20506 "Convert headings to normal text, or items or text to headings.
20507 If there is no active region, only convert the current line.
20509 With a `\\[universal-argument]' prefix, convert the whole list at
20510 point into heading.
20514 - If the first non blank line is a headline, remove the stars
20515 from all headlines in the region.
20517 - If it is a normal line, turn each and every normal line (i.e.,
20518 not an heading or an item) in the region into headings. If you
20519 want to convert only the first line of this region, use one
20520 universal prefix argument.
20522 - If it is a plain list item, turn all plain list items into headings.
20524 When converting a line into a heading, the number of stars is chosen
20525 such that the lines become children of the current entry. However,
20526 when a numeric prefix argument is given, its value determines the
20527 number of stars to add."
20531 ;; Return beginning of first non-blank line, starting from
20536 (while (org-at-comment-p) (forward-line))
20537 (skip-chars-forward " \r\t\n")
20540 ;; Determine boundaries of changes. If a universal prefix has
20541 ;; been given, put the list in a region. If region ends at a bol,
20542 ;; do not consider the last line to be in the region.
20544 (when (and current-prefix-arg
(org-at-item-p))
20545 (when (listp current-prefix-arg
) (setq current-prefix-arg
1))
20546 (org-mark-element))
20548 (if (org-region-active-p)
20549 (setq beg
(funcall skip-blanks
(region-beginning))
20550 end
(copy-marker (save-excursion
20551 (goto-char (region-end))
20552 (if (bolp) (point) (point-at-eol)))))
20553 (setq beg
(funcall skip-blanks
(point-at-bol))
20554 end
(copy-marker (point-at-eol))))
20555 ;; Ensure inline tasks don't count as headings.
20556 (org-with-limited-levels
20560 ;; Case 1. Started at an heading: de-star headings.
20561 ((org-at-heading-p)
20562 (while (< (point) end
)
20563 (when (org-at-heading-p t
)
20564 (looking-at org-outline-regexp
) (replace-match "")
20567 ;; Case 2. Started at an item: change items into headlines.
20568 ;; One star will be added by `org-list-to-subtree'.
20570 (while (< (point) end
)
20571 (when (org-at-item-p)
20572 ;; Pay attention to cases when region ends before list.
20573 (let* ((struct (org-list-struct))
20575 (min (org-list-get-bottom-point struct
) (1+ end
))))
20577 (narrow-to-region (point) list-end
)
20578 (insert (org-list-to-subtree (org-list-to-lisp t
)) "\n")))
20581 ;; Case 3. Started at normal text: make every line an heading,
20582 ;; skipping headlines and items.
20585 (if (numberp nstars
) nstars
(or (org-current-level) 0)) ?
*))
20587 (cond (nstars "") ; stars from prefix only
20588 ((equal stars
"") "*") ; before first heading
20589 (org-odd-levels-only "**") ; inside heading, odd
20590 (t "*"))) ; inside heading, oddeven
20591 (rpl (concat stars add-stars
" "))
20592 (lend (when (listp nstars
) (save-excursion (end-of-line) (point)))))
20593 (while (< (point) (if (equal nstars
'(4)) lend end
))
20594 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
20595 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
20596 (replace-match (concat rpl
(match-string 2))) (setq toggled t
))
20597 (forward-line)))))))
20598 (unless toggled
(message "Cannot toggle heading from here"))))
20600 (defun org-meta-return (&optional arg
)
20601 "Insert a new heading or wrap a region in a table.
20602 Calls `org-insert-heading', `org-insert-item' or
20603 `org-table-wrap-region', depending on context. When called with
20604 an argument, unconditionally call `org-insert-heading'."
20606 (org-check-before-invisible-edit 'insert
)
20607 (or (run-hook-with-args-until-success 'org-metareturn-hook
)
20608 (call-interactively (cond (arg #'org-insert-heading
)
20609 ((org-at-table-p) #'org-table-wrap-region
)
20610 ((org-in-item-p) #'org-insert-item
)
20611 (t #'org-insert-heading
)))))
20615 (defsubst org-in-subtree-not-table-p
()
20616 "Are we in a subtree and not in a table?"
20617 (and (not (org-before-first-heading-p))
20618 (not (org-at-table-p))))
20620 ;; Define the Org mode menus
20621 (easy-menu-define org-tbl-menu org-mode-map
"Tbl menu"
20623 ["Align" org-ctrl-c-ctrl-c
:active
(org-at-table-p)]
20624 ["Next Field" org-cycle
(org-at-table-p)]
20625 ["Previous Field" org-shifttab
(org-at-table-p)]
20626 ["Next Row" org-return
(org-at-table-p)]
20628 ["Blank Field" org-table-blank-field
(org-at-table-p)]
20629 ["Edit Field" org-table-edit-field
(org-at-table-p)]
20630 ["Copy Field from Above" org-table-copy-down
(org-at-table-p)]
20633 ["Move Column Left" org-metaleft
(org-at-table-p)]
20634 ["Move Column Right" org-metaright
(org-at-table-p)]
20635 ["Delete Column" org-shiftmetaleft
(org-at-table-p)]
20636 ["Insert Column" org-shiftmetaright
(org-at-table-p)]
20637 ["Shrink Column" org-table-toggle-column-width
(org-at-table-p)])
20639 ["Move Row Up" org-metaup
(org-at-table-p)]
20640 ["Move Row Down" org-metadown
(org-at-table-p)]
20641 ["Delete Row" org-shiftmetaup
(org-at-table-p)]
20642 ["Insert Row" org-shiftmetadown
(org-at-table-p)]
20643 ["Sort lines in region" org-table-sort-lines
(org-at-table-p)]
20645 ["Insert Hline" org-ctrl-c-minus
(org-at-table-p)])
20647 ["Copy Rectangle" org-copy-special
(org-at-table-p)]
20648 ["Cut Rectangle" org-cut-special
(org-at-table-p)]
20649 ["Paste Rectangle" org-paste-special
(org-at-table-p)]
20650 ["Fill Rectangle" org-table-wrap-region
(org-at-table-p)])
20653 ["Set Column Formula" org-table-eval-formula
(org-at-table-p)]
20654 ["Set Field Formula" (org-table-eval-formula '(4)) :active
(org-at-table-p) :keys
"C-u C-c ="]
20655 ["Edit Formulas" org-edit-special
(org-at-table-p)]
20657 ["Recalculate line" org-table-recalculate
(org-at-table-p)]
20658 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active
(org-at-table-p) :keys
"C-u C-c *"]
20659 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active
(org-at-table-p) :keys
"C-u C-u C-c *"]
20661 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks
(org-at-table-p)]
20663 ["Sum Column/Rectangle" org-table-sum
20664 (or (org-at-table-p) (org-region-active-p))]
20665 ["Which Column?" org-table-current-column
(org-at-table-p)])
20667 org-table-toggle-formula-debugger
20668 :style toggle
:selected
(bound-and-true-p org-table-formula-debug
)]
20669 ["Show Col/Row Numbers"
20670 org-table-toggle-coordinate-overlays
20672 :selected
(bound-and-true-p org-table-overlay-coordinates
)]
20674 ["Create" org-table-create
(not (org-at-table-p))]
20675 ["Convert Region" org-table-convert-region
(not (org-at-table-p 'any
))]
20676 ["Import from File" org-table-import
(not (org-at-table-p))]
20677 ["Export to File" org-table-export
(org-at-table-p)]
20679 ["Create/Convert from/to table.el" org-table-create-with-table.el t
]
20682 ["Ascii plot" orgtbl-ascii-plot
:active
(org-at-table-p) :keys
"C-c \" a"]
20683 ["Gnuplot" org-plot
/gnuplot
:active
(org-at-table-p) :keys
"C-c \" g"])))
20685 (easy-menu-define org-org-menu org-mode-map
"Org menu"
20688 ["Cycle Visibility" org-cycle
:active
(or (bobp) (outline-on-heading-p))]
20689 ["Cycle Global Visibility" org-shifttab
:active
(not (org-at-table-p))]
20690 ["Sparse Tree..." org-sparse-tree t
]
20691 ["Reveal Context" org-reveal t
]
20692 ["Show All" org-show-all t
]
20694 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t
])
20696 ["New Heading" org-insert-heading t
]
20697 ("Navigate Headings"
20698 ["Up" outline-up-heading t
]
20699 ["Next" outline-next-visible-heading t
]
20700 ["Previous" outline-previous-visible-heading t
]
20701 ["Next Same Level" outline-forward-same-level t
]
20702 ["Previous Same Level" outline-backward-same-level t
]
20704 ["Jump" org-goto t
])
20706 ["Refile Subtree" org-refile
(org-in-subtree-not-table-p)]
20708 ["Move Subtree Up" org-metaup
(org-at-heading-p)]
20709 ["Move Subtree Down" org-metadown
(org-at-heading-p)]
20711 ["Copy Subtree" org-copy-special
(org-in-subtree-not-table-p)]
20712 ["Cut Subtree" org-cut-special
(org-in-subtree-not-table-p)]
20713 ["Paste Subtree" org-paste-special
(not (org-at-table-p))]
20715 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t
]
20717 ["Copy visible text" org-copy-visible t
]
20719 ["Promote Heading" org-metaleft
(org-in-subtree-not-table-p)]
20720 ["Promote Subtree" org-shiftmetaleft
(org-in-subtree-not-table-p)]
20721 ["Demote Heading" org-metaright
(org-in-subtree-not-table-p)]
20722 ["Demote Subtree" org-shiftmetaright
(org-in-subtree-not-table-p)]
20724 ["Sort Region/Children" org-sort t
]
20726 ["Convert to odd levels" org-convert-to-odd-levels t
]
20727 ["Convert to odd/even levels" org-convert-to-oddeven-levels t
])
20729 ["Emphasis..." org-emphasize t
]
20730 ["Edit Source Example" org-edit-special t
]
20732 ["Footnote new/jump" org-footnote-action t
]
20733 ["Footnote extra" (org-footnote-action t
) :active t
:keys
"C-u C-c C-x f"])
20735 ["Archive (default method)" org-archive-subtree-default
(org-in-subtree-not-table-p)]
20737 ["Move Subtree to Archive file" org-archive-subtree
(org-in-subtree-not-table-p)]
20738 ["Toggle ARCHIVE tag" org-toggle-archive-tag
(org-in-subtree-not-table-p)]
20739 ["Move subtree to Archive sibling" org-archive-to-archive-sibling
(org-in-subtree-not-table-p)]
20743 ["Store Link (Global)" org-store-link t
]
20744 ["Find existing link to here" org-occur-link-in-agenda-files t
]
20745 ["Insert Link" org-insert-link t
]
20746 ["Follow Link" org-open-at-point t
]
20748 ["Next link" org-next-link t
]
20749 ["Previous link" org-previous-link t
]
20751 ["Descriptive Links"
20752 org-toggle-link-display
20754 :selected org-descriptive-links
20757 org-toggle-link-display
20759 :selected
(not org-descriptive-links
)])
20762 ["TODO/DONE/-" org-todo t
]
20764 ["Next keyword" org-shiftright
(org-at-heading-p)]
20765 ["Previous keyword" org-shiftleft
(org-at-heading-p)]
20766 ["Complete Keyword" pcomplete
(assq :todo-keyword
(org-context))]
20767 ["Next keyword set" org-shiftcontrolright
(and (> (length org-todo-sets
) 1) (org-at-heading-p))]
20768 ["Previous keyword set" org-shiftcontrolright
(and (> (length org-todo-sets
) 1) (org-at-heading-p))])
20769 ["Show TODO Tree" org-show-todo-tree
:active t
:keys
"C-c / t"]
20770 ["Global TODO list" org-todo-list
:active t
:keys
"\\[org-agenda] t"]
20772 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies
)
20773 :selected org-enforce-todo-dependencies
:style toggle
:active t
]
20774 "Settings for tree at point"
20775 ["Do Children sequentially" org-toggle-ordered-property
:style radio
20776 :selected
(org-entry-get nil
"ORDERED")
20777 :active org-enforce-todo-dependencies
:keys
"C-c C-x o"]
20778 ["Do Children parallel" org-toggle-ordered-property
:style radio
20779 :selected
(not (org-entry-get nil
"ORDERED"))
20780 :active org-enforce-todo-dependencies
:keys
"C-c C-x o"]
20782 ["Set Priority" org-priority t
]
20783 ["Priority Up" org-shiftup t
]
20784 ["Priority Down" org-shiftdown t
]
20786 ["Get news from all feeds" org-feed-update-all t
]
20787 ["Go to the inbox of a feed..." org-feed-goto-inbox t
]
20788 ["Customize feeds" (customize-variable 'org-feed-alist
) t
])
20789 ("TAGS and Properties"
20790 ["Set Tags" org-set-tags-command
(not (org-before-first-heading-p))]
20791 ["Change tag in region" org-change-tag-in-region
(org-region-active-p)]
20793 ["Set property" org-set-property
(not (org-before-first-heading-p))]
20794 ["Column view of properties" org-columns t
]
20795 ["Insert Column View DBlock" org-columns-insert-dblock t
])
20796 ("Dates and Scheduling"
20797 ["Timestamp" org-time-stamp
(not (org-before-first-heading-p))]
20798 ["Timestamp (inactive)" org-time-stamp-inactive
(not (org-before-first-heading-p))]
20800 ["1 Day Later" org-shiftright
(org-at-timestamp-p 'lax
)]
20801 ["1 Day Earlier" org-shiftleft
(org-at-timestamp-p 'lax
)]
20802 ["1 ... Later" org-shiftup
(org-at-timestamp-p 'lax
)]
20803 ["1 ... Earlier" org-shiftdown
(org-at-timestamp-p 'lax
)])
20804 ["Compute Time Range" org-evaluate-time-range t
]
20805 ["Schedule Item" org-schedule
(not (org-before-first-heading-p))]
20806 ["Deadline" org-deadline
(not (org-before-first-heading-p))]
20808 ["Custom time format" org-toggle-time-stamp-overlays
20809 :style radio
:selected org-display-custom-times
]
20811 ["Goto Calendar" org-goto-calendar t
]
20812 ["Date from Calendar" org-date-from-calendar t
]
20814 ["Start/Restart Timer" org-timer-start t
]
20815 ["Pause/Continue Timer" org-timer-pause-or-continue t
]
20816 ["Stop Timer" org-timer-pause-or-continue
:active t
:keys
"C-u C-c C-x ,"]
20817 ["Insert Timer String" org-timer t
]
20818 ["Insert Timer Item" org-timer-item t
])
20820 ["Clock in" org-clock-in
:active t
:keys
"C-c C-x C-i"]
20821 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t
:keys
"C-u C-c C-x C-i"]
20822 ["Clock out" org-clock-out t
]
20823 ["Clock cancel" org-clock-cancel t
]
20825 ["Mark as default task" org-clock-mark-default-task t
]
20826 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t
:keys
"C-u C-u C-c C-x C-i"]
20827 ["Goto running clock" org-clock-goto t
]
20829 ["Display times" org-clock-display t
]
20830 ["Create clock table" org-clock-report t
]
20832 ["Record DONE time"
20833 (progn (setq org-log-done
(not org-log-done
))
20834 (message "Switching to %s will %s record a timestamp"
20835 (car org-done-keywords
)
20836 (if org-log-done
"automatically" "not")))
20837 :style toggle
:selected org-log-done
])
20839 ["Agenda Command..." org-agenda t
]
20840 ["Set Restriction Lock" org-agenda-set-restriction-lock t
]
20841 ("File List for Agenda")
20842 ("Special views current file"
20843 ["TODO Tree" org-show-todo-tree t
]
20844 ["Check Deadlines" org-check-deadlines t
]
20845 ["Tags/Property tree" org-match-sparse-tree t
])
20847 ["Export/Publish..." org-export-dispatch t
]
20849 ["Org CDLaTeX mode" org-cdlatex-mode
:active
(require 'cdlatex nil t
)
20850 :style toggle
:selected org-cdlatex-mode
]
20851 ["Insert Environment" cdlatex-environment
(fboundp 'cdlatex-environment
)]
20852 ["Insert math symbol" cdlatex-math-symbol
(fboundp 'cdlatex-math-symbol
)]
20853 ["Modify math symbol" org-cdlatex-math-modify
20854 (org-inside-LaTeX-fragment-p)]
20855 ["Insert citation" org-reftex-citation t
])
20858 ["Push Files and Views" org-mobile-push t
]
20859 ["Get Captured and Flagged" org-mobile-pull t
]
20860 ["Find FLAGGED Tasks" (org-agenda nil
"?") :active t
:keys
"\\[org-agenda] ?"]
20862 ["Setup" (progn (require 'org-mobile
) (customize-group 'org-mobile
)) t
])
20865 ["Show Version" org-version t
]
20866 ["Info Documentation" org-info t
])
20868 ["Browse Org Group" org-customize t
]
20870 ["Expand This Menu" org-create-customize-menu
20871 (fboundp 'customize-menu-create
)])
20872 ["Send bug report" org-submit-bug-report t
]
20875 ["Refresh setup current buffer" org-mode-restart t
]
20876 ["Reload Org (after update)" org-reload t
]
20877 ["Reload Org uncompiled" (org-reload t
) :active t
:keys
"C-u C-c C-x !"])
20880 (defun org-info (&optional node
)
20881 "Read documentation for Org in the info system.
20882 With optional NODE, go directly to that node."
20884 (info (format "(org)%s" (or node
""))))
20887 (defun org-submit-bug-report ()
20888 "Submit a bug report on Org via mail.
20890 Don't hesitate to report any problems or inaccurate documentation.
20892 If you don't have setup sending mail from (X)Emacs, please copy the
20893 output buffer into your mail program, as it gives us important
20894 information about your Org version and configuration."
20896 (require 'reporter
)
20897 (defvar reporter-prompt-for-summary-p
)
20898 (org-load-modules-maybe)
20899 (org-require-autoloaded-modules)
20900 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
20901 (reporter-submit-bug-report
20902 "emacs-orgmode@gnu.org"
20903 (org-version nil
'full
)
20905 (save-window-excursion
20906 (pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
20907 (delete-other-windows)
20909 (insert "You are about to submit a bug report to the Org mailing list.
20911 We would like to add your full Org and Outline configuration to the
20912 bug report. This greatly simplifies the work of the maintainer and
20913 other experts on the mailing list.
20915 HOWEVER, some variables you have customized may contain private
20916 information. The names of customers, colleagues, or friends, might
20917 appear in the form of file names, tags, todo states, or search strings.
20918 If you answer yes to the prompt, you might want to check and remove
20919 such private information before sending the email.")
20920 (add-text-properties (point-min) (point-max) '(face org-warning
))
20921 (when (yes-or-no-p "Include your Org configuration ")
20925 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v
))
20926 (or (and (symbol-value v
)
20927 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v
)))
20929 (get v
'custom-type
) (get v
'standard-value
)
20930 (not (equal (symbol-value v
) (eval (car (get v
'standard-value
)))))))
20932 (kill-buffer (get-buffer "*Warn about privacy*"))
20935 "Remember to cover the basics, that is, what you expected to happen and
20936 what in fact did happen. You don't know how to make a good report? See
20938 https://orgmode.org/manual/Feedback.html#Feedback
20940 Your bug report will be posted to the Org mailing list.
20941 ------------------------------------------------------------------------")
20943 (when (re-search-backward "^\\(Subject: \\)Org mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t
)
20944 (replace-match "\\1Bug: \\3 [\\2]")))))
20947 (defun org-install-agenda-files-menu ()
20948 (let ((bl (buffer-list)))
20951 (set-buffer (pop bl
))
20952 (when (derived-mode-p 'org-mode
) (setq bl nil
)))
20953 (when (derived-mode-p 'org-mode
)
20955 '("Org") "File List for Agenda"
20958 ["Edit File List" (org-edit-agenda-file-list) t
]
20959 ["Add/Move Current File to Front of List" org-agenda-file-to-front t
]
20960 ["Remove Current File from List" org-remove-file t
]
20961 ["Cycle through agenda files" org-cycle-agenda-files t
]
20962 ["Occur in all agenda files" org-occur-in-agenda-files t
]
20964 (mapcar 'org-file-menu-entry
20965 ;; Prevent initialization from failing.
20966 (ignore-errors (org-agenda-files t
)))))))))
20970 (defun org-require-autoloaded-modules ()
20973 '(org-agenda org-archive org-attach org-clock org-colview org-id
20974 org-table org-timer
)))
20977 (defun org-reload (&optional uncompiled
)
20978 "Reload all Org Lisp files.
20979 With prefix arg UNCOMPILED, load the uncompiled versions."
20981 (require 'loadhist
)
20982 (let* ((org-dir (org-find-library-dir "org"))
20983 (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir
))
20984 (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
20985 (remove-re (format "\\`%s\\'"
20986 (regexp-opt '("org" "org-loaddefs" "org-version"))))
20987 (feats (delete-dups
20988 (mapcar 'file-name-sans-extension
20989 (mapcar 'file-name-nondirectory
20991 (mapcar 'feature-file
20998 (if (and (string-match feature-re f
)
20999 (not (string-match remove-re f
)))
21003 (list "org-version" "org")))
21004 (load-suffixes (when (boundp 'load-suffixes
) load-suffixes
))
21005 (load-suffixes (if uncompiled
(reverse load-suffixes
) load-suffixes
))
21006 load-uncore load-misses
)
21009 (mapcar (lambda (f)
21010 (or (org-load-noerror-mustsuffix (concat org-dir f
))
21011 (and (string= org-dir contrib-dir
)
21012 (org-load-noerror-mustsuffix (concat contrib-dir f
)))
21013 (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f
) f
))
21014 (add-to-list 'load-uncore f
'append
)
21019 (message "The following feature%s found in load-path, please check if that's correct:\n%s"
21020 (if (> (length load-uncore
) 1) "s were" " was") load-uncore
))
21022 (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
21023 (if (> (length load-misses
) 1) "s" "") load-misses
(org-version nil
'full
))
21024 (message "Successfully reloaded Org\n%s" (org-version nil
'full
)))))
21027 (defun org-customize ()
21028 "Call the customize function with org as argument."
21030 (org-load-modules-maybe)
21031 (org-require-autoloaded-modules)
21032 (customize-browse 'org
))
21034 (defun org-create-customize-menu ()
21035 "Create a full customization menu for Org mode, insert it into the menu."
21037 (org-load-modules-maybe)
21038 (org-require-autoloaded-modules)
21039 (if (fboundp 'customize-menu-create
)
21042 '("Org") "Customize"
21043 `(["Browse Org group" org-customize t
]
21045 ,(customize-menu-create 'org
)
21046 ["Set" Custom-set t
]
21047 ["Save" Custom-save t
]
21048 ["Reset to Current" Custom-reset-current t
]
21049 ["Reset to Saved" Custom-reset-saved t
]
21050 ["Reset to Standard Settings" Custom-reset-standard t
]))
21051 (message "\"Org\"-menu now contains full customization menu"))
21052 (error "Cannot expand menu (outdated version of cus-edit.el)")))
21054 ;;;; Miscellaneous stuff
21056 ;;; Generally useful functions
21058 (defun org-link-display-format (s)
21059 "Replace links in string S with their description.
21060 If there is no description, use the link target."
21062 (replace-regexp-in-string
21063 org-bracket-link-analytic-regexp
21065 (if (match-end 5) (match-string 5 m
)
21066 (concat (match-string 1 m
) (match-string 3 m
))))
21069 (defun org-toggle-link-display ()
21070 "Toggle the literal or descriptive display of links."
21072 (if org-descriptive-links
21073 (progn (org-remove-from-invisibility-spec '(org-link))
21074 (org-restart-font-lock)
21075 (setq org-descriptive-links nil
))
21076 (progn (add-to-invisibility-spec '(org-link))
21077 (org-restart-font-lock)
21078 (setq org-descriptive-links t
))))
21080 (defun org-in-clocktable-p ()
21081 "Check if the cursor is in a clocktable."
21082 (let ((pos (point)) start
)
21085 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t
)
21086 (setq start
(match-beginning 0))
21087 (re-search-forward "^[ \t]*#\\+END:.*" nil t
)
21088 (>= (match-end 0) pos
)
21091 (defun org-in-verbatim-emphasis ()
21093 (and (org-in-regexp org-verbatim-re
2)
21094 (>= (point) (match-beginning 3))
21095 (<= (point) (match-end 4)))))
21097 (defun org-goto-marker-or-bmk (marker &optional bookmark
)
21098 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
21099 (if (and marker
(marker-buffer marker
)
21100 (buffer-live-p (marker-buffer marker
)))
21102 (pop-to-buffer-same-window (marker-buffer marker
))
21103 (when (or (> marker
(point-max)) (< marker
(point-min)))
21106 (org-show-context 'org-goto
))
21108 (bookmark-jump bookmark
)
21109 (error "Cannot find location"))))
21111 (defun org-quote-csv-field (s)
21112 "Quote field for inclusion in CSV material."
21113 (if (string-match "[\",]" s
)
21114 (concat "\"" (mapconcat 'identity
(split-string s
"\"") "\"\"") "\"")
21117 (defun org-force-self-insert (N)
21118 "Needed to enforce self-insert under remapping."
21120 (self-insert-command N
))
21122 (defun org-fill-template (template alist
)
21123 "Find each %key of ALIST in TEMPLATE and replace it."
21124 (let ((case-fold-search nil
))
21125 (dolist (entry (sort (copy-sequence alist
)
21126 (lambda (a b
) (< (length (car a
)) (length (car b
))))))
21128 (replace-regexp-in-string
21129 (concat "%" (regexp-quote (car entry
)))
21130 (or (cdr entry
) "") template t t
)))
21133 (defun org-quote-vert (s)
21134 "Replace \"|\" with \"\\vert\"."
21135 (while (string-match "|" s
)
21136 (setq s
(replace-match "\\vert" t t s
)))
21139 (defun org-uuidgen-p (s)
21140 "Is S an ID created by UUIDGEN?"
21141 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s
)))
21143 (defun org-in-src-block-p (&optional inside
)
21144 "Whether point is in a code source block.
21145 When INSIDE is non-nil, don't consider we are within a src block
21146 when point is at #+BEGIN_SRC or #+END_SRC."
21147 (let ((case-fold-search t
))
21148 (or (and (eq (get-char-property (point) 'src-block
) t
))
21152 (beginning-of-line)
21153 (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
21155 (defun org-context ()
21156 "Return a list of contexts of the current cursor position.
21157 If several contexts apply, all are returned.
21158 Each context entry is a list with a symbol naming the context, and
21159 two positions indicating start and end of the context. Possible
21162 :headline anywhere in a headline
21163 :headline-stars on the leading stars in a headline
21164 :todo-keyword on a TODO keyword (including DONE) in a headline
21165 :tags on the TAGS in a headline
21166 :priority on the priority cookie in a headline
21167 :item on the first line of a plain list item
21168 :item-bullet on the bullet/number of a plain list item
21169 :checkbox on the checkbox in a plain list item
21170 :table in an Org table
21171 :table-special on a special filed in a table
21172 :table-table in a table.el table
21173 :clocktable in a clocktable
21174 :src-block in a source block
21175 :link on a hyperlink
21176 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
21177 :target on a <<target>>
21178 :radio-target on a <<<radio-target>>>
21179 :latex-fragment on a LaTeX fragment
21180 :latex-preview on a LaTeX fragment with overlaid preview image
21182 This function expects the position to be visible because it uses font-lock
21183 faces as a help to recognize the following contexts: :table-special, :link,
21185 (let* ((f (get-text-property (point) 'face
))
21186 (faces (if (listp f
) f
(list f
)))
21187 (case-fold-search t
)
21188 (p (point)) clist o
)
21189 ;; First the large context
21191 ((org-at-heading-p t
)
21192 (push (list :headline
(point-at-bol) (point-at-eol)) clist
)
21194 (beginning-of-line 1)
21195 (looking-at org-todo-line-tags-regexp
))
21196 (push (org-point-in-group p
1 :headline-stars
) clist
)
21197 (push (org-point-in-group p
2 :todo-keyword
) clist
)
21198 (push (org-point-in-group p
4 :tags
) clist
))
21200 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
21201 (when (looking-at "\\[#[A-Z0-9]\\]")
21202 (push (org-point-in-group p
0 :priority
) clist
)))
21205 (push (org-point-in-group p
2 :item-bullet
) clist
)
21206 (push (list :item
(point-at-bol)
21207 (save-excursion (org-end-of-item) (point)))
21209 (and (org-at-item-checkbox-p)
21210 (push (org-point-in-group p
0 :checkbox
) clist
)))
21213 (push (list :table
(org-table-begin) (org-table-end)) clist
)
21214 (when (memq 'org-formula faces
)
21215 (push (list :table-special
21216 (previous-single-property-change p
'face
)
21217 (next-single-property-change p
'face
)) clist
)))
21218 ((org-at-table-p 'any
)
21219 (push (list :table-table
) clist
)))
21222 (let ((case-fold-search t
))
21223 ;; New the "medium" contexts: clocktables, source blocks
21224 (cond ((org-in-clocktable-p)
21225 (push (list :clocktable
21226 (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
21227 (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t
))
21228 (match-beginning 1))
21229 (and (re-search-forward "[ \t]*#\\+END:?" nil t
)
21230 (match-end 0))) clist
))
21231 ((org-in-src-block-p)
21232 (push (list :src-block
21233 (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
21234 (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t
))
21235 (match-beginning 1))
21236 (and (search-forward "#+END_SRC" nil t
)
21237 (match-beginning 0))) clist
))))
21240 ;; Now the small context
21242 ((org-at-timestamp-p)
21243 (push (org-point-in-group p
0 :timestamp
) clist
))
21244 ((memq 'org-link faces
)
21246 (previous-single-property-change p
'face
)
21247 (next-single-property-change p
'face
)) clist
))
21248 ((memq 'org-special-keyword faces
)
21249 (push (list :keyword
21250 (previous-single-property-change p
'face
)
21251 (next-single-property-change p
'face
)) clist
))
21253 (push (org-point-in-group p
0 :target
) clist
)
21254 (goto-char (1- (match-beginning 0)))
21255 (when (looking-at org-radio-target-regexp
)
21256 (push (org-point-in-group p
0 :radio-target
) clist
))
21260 (and (eq (overlay-get o
'org-overlay-type
) 'org-latex-overlay
)
21262 (overlays-at (point))))
21263 (push (list :latex-fragment
21264 (overlay-start o
) (overlay-end o
)) clist
)
21265 (push (list :latex-preview
21266 (overlay-start o
) (overlay-end o
)) clist
))
21267 ((org-inside-LaTeX-fragment-p)
21268 ;; FIXME: positions wrong.
21269 (push (list :latex-fragment
(point) (point)) clist
)))
21271 (setq clist
(nreverse (delq nil clist
)))
21274 (defun org-between-regexps-p (start-re end-re
&optional lim-up lim-down
)
21275 "Non-nil when point is between matches of START-RE and END-RE.
21277 Also return a non-nil value when point is on one of the matches.
21279 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
21280 buffer positions. Default values are the positions of headlines
21281 surrounding the point.
21283 The functions returns a cons cell whose car (resp. cdr) is the
21284 position before START-RE (resp. after END-RE)."
21286 (let ((pos (point))
21287 (limit-up (or lim-up
(save-excursion (outline-previous-heading))))
21288 (limit-down (or lim-down
(save-excursion (outline-next-heading))))
21291 ;; Point is on a block when on START-RE or if START-RE can be
21292 ;; found before it...
21293 (and (or (org-in-regexp start-re
)
21294 (re-search-backward start-re limit-up t
))
21295 (setq beg
(match-beginning 0))
21296 ;; ... and END-RE after it...
21297 (goto-char (match-end 0))
21298 (re-search-forward end-re limit-down t
)
21299 (> (setq end
(match-end 0)) pos
)
21300 ;; ... without another START-RE in-between.
21301 (goto-char (match-beginning 0))
21302 (not (re-search-backward start-re
(1+ beg
) t
))
21304 (cons beg end
))))))
21306 (defun org-in-block-p (names)
21307 "Non-nil when point belongs to a block whose name belongs to NAMES.
21309 NAMES is a list of strings containing names of blocks.
21311 Return first block name matched, or nil. Beware that in case of
21312 nested blocks, the returned name may not belong to the closest
21316 (let ((case-fold-search t
)
21317 (lim-up (save-excursion (outline-previous-heading)))
21318 (lim-down (save-excursion (outline-next-heading))))
21319 (dolist (name names
)
21320 (let ((n (regexp-quote name
)))
21321 (when (org-between-regexps-p
21322 (concat "^[ \t]*#\\+begin_" n
)
21323 (concat "^[ \t]*#\\+end_" n
)
21325 (throw 'exit n
)))))
21328 (defun org-occur-in-agenda-files (regexp &optional _nlines
)
21329 "Call `multi-occur' with buffers for all agenda files."
21330 (interactive "sOrg-files matching: ")
21331 (let* ((files (org-agenda-files))
21332 (tnames (mapcar #'file-truename files
))
21333 (extra org-agenda-text-search-extra-files
))
21334 (when (eq (car extra
) 'agenda-archives
)
21335 (setq extra
(cdr extra
))
21336 (setq files
(org-add-archive-files files
)))
21338 (unless (member (file-truename f
) tnames
)
21339 (unless (member f files
) (setq files
(append files
(list f
))))
21340 (setq tnames
(append tnames
(list (file-truename f
))))))
21342 (mapcar (lambda (x)
21343 (with-current-buffer
21344 ;; FIXME: Why not just (find-file-noselect x)?
21345 ;; Is it to avoid the "revert buffer" prompt?
21346 (or (get-file-buffer x
) (find-file-noselect x
))
21352 (add-hook 'occur-mode-find-occurrence-hook
21353 (lambda () (when (derived-mode-p 'org-mode
) (org-reveal))))
21355 (defun org-occur-link-in-agenda-files ()
21356 "Create a link and search for it in the agendas.
21357 The link is not stored in `org-stored-links', it is just created
21358 for the search purpose."
21360 (let ((link (condition-case nil
21361 (org-store-link nil
)
21362 (error "Unable to create a link to here"))))
21363 (org-occur-in-agenda-files (regexp-quote link
))))
21365 (defun org-back-over-empty-lines ()
21366 "Move backwards over whitespace, to the beginning of the first empty line.
21367 Returns the number of empty lines passed."
21368 (let ((pos (point)))
21369 (if (cdr (assq 'heading org-blank-before-new-entry
))
21370 (skip-chars-backward " \t\n\r")
21372 (forward-line -
1)))
21373 (beginning-of-line 2)
21374 (goto-char (min (point) pos
))
21375 (count-lines (point) pos
)))
21377 (defun org-switch-to-buffer-other-window (&rest args
)
21378 "Switch to buffer in a second window on the current frame.
21379 In particular, do not allow pop-up frames.
21380 Returns the newly created buffer."
21382 (apply 'switch-to-buffer-other-window args
)))
21384 (defun org-replace-escapes (string table
)
21385 "Replace %-escapes in STRING with values in TABLE.
21386 TABLE is an association list with keys like \"%a\" and string values.
21387 The sequences in STRING may contain normal field width and padding information,
21388 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
21389 so values can contain further %-escapes if they are define later in TABLE."
21390 (let ((tbl (copy-alist table
))
21391 (case-fold-search nil
)
21395 (setq re
(concat "%-?[0-9.]*" (substring (car e
) 1)))
21396 (when (and (cdr e
) (string-match re
(cdr e
)))
21397 (let ((sref (substring (cdr e
) (match-beginning 0) (match-end 0)))
21399 (add-text-properties 0 3 (list 'sref sref
) safe
)
21400 (setcdr e
(replace-match safe t t
(cdr e
)))))
21401 (while (string-match re string
)
21402 (setq rpl
(format (concat (substring (match-string 0 string
) 0 -
1) "s")
21404 (setq string
(replace-match rpl t t string
))))
21405 (while (setq pchg
(next-property-change pchg string
))
21406 (let ((sref (get-text-property pchg
'sref string
)))
21407 (when (and sref
(string-match "SREF" string pchg
))
21408 (setq string
(replace-match sref t t string
)))))
21411 ;;; TODO: Only called once, from ox-odt which should probably use
21412 ;;; org-export-inline-image-p or something.
21413 (defun org-file-image-p (file)
21414 "Return non-nil if FILE is an image."
21416 (string-match (image-file-name-regexp) file
)))
21418 (defun org-get-cursor-date (&optional with-time
)
21419 "Return the date at cursor in as a time.
21420 This works in the calendar and in the agenda, anywhere else it just
21421 returns the current time.
21422 If WITH-TIME is non-nil, returns the time of the event at point (in
21423 the agenda) or the current time of the day."
21424 (let (date day defd tp hod mod
)
21426 (setq tp
(get-text-property (point) 'time
))
21427 (when (and tp
(string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp
))
21428 (setq hod
(string-to-number (match-string 1 tp
))
21429 mod
(string-to-number (match-string 2 tp
))))
21430 (or tp
(let ((now (decode-time)))
21431 (setq hod
(nth 2 now
)
21432 mod
(nth 1 now
)))))
21434 ((eq major-mode
'calendar-mode
)
21435 (setq date
(calendar-cursor-to-date)
21436 defd
(encode-time 0 (or mod
0) (or hod
0)
21437 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
21438 ((eq major-mode
'org-agenda-mode
)
21439 (setq day
(get-text-property (point) 'day
))
21441 (setq date
(calendar-gregorian-from-absolute day
)
21442 defd
(encode-time 0 (or mod
0) (or hod
0)
21443 (nth 1 date
) (nth 0 date
) (nth 2 date
))))))
21444 (or defd
(current-time))))
21446 (defun org-mark-subtree (&optional up
)
21447 "Mark the current subtree.
21448 This puts point at the start of the current subtree, and mark at
21449 the end. If a numeric prefix UP is given, move up into the
21450 hierarchy of headlines by UP levels before marking the subtree."
21452 (org-with-limited-levels
21453 (cond ((org-at-heading-p) (beginning-of-line))
21454 ((org-before-first-heading-p) (user-error "Not in a subtree"))
21455 (t (outline-previous-visible-heading 1))))
21456 (when up
(while (and (> up
0) (org-up-heading-safe)) (cl-decf up
)))
21457 (if (called-interactively-p 'any
)
21458 (call-interactively 'org-mark-element
)
21459 (org-mark-element)))
21461 (defun org-file-newer-than-p (file time
)
21462 "Non-nil if FILE is newer than TIME.
21463 FILE is a filename, as a string, TIME is a list of integers, as
21464 returned by, e.g., `current-time'."
21465 (and (file-exists-p file
)
21466 ;; Only compare times up to whole seconds as some file-systems
21467 ;; (e.g. HFS+) do not retain any finer granularity. As
21468 ;; a consequence, make sure we return non-nil when the two
21469 ;; times are equal.
21470 (not (time-less-p (cl-subseq (nth 5 (file-attributes file
)) 0 2)
21471 (cl-subseq time
0 2)))))
21473 (defun org-compile-file (source process ext
&optional err-msg log-buf spec
)
21474 "Compile a SOURCE file using PROCESS.
21476 PROCESS is either a function or a list of shell commands, as
21477 strings. EXT is a file extension, without the leading dot, as
21478 a string. It is used to check if the process actually succeeded.
21480 PROCESS must create a file with the same base name and directory
21481 as SOURCE, but ending with EXT. The function then returns its
21482 filename. Otherwise, it raises an error. The error message can
21483 then be refined by providing string ERR-MSG, which is appended to
21484 the standard message.
21486 If PROCESS is a function, it is called with a single argument:
21489 If it is a list of commands, each of them is called using
21490 `shell-command'. By default, in each command, %b, %f, %F, %o and
21491 %O are replaced with, respectively, SOURCE base name, name, full
21492 name, directory and absolute output file name. It is possible,
21493 however, to use more place-holders by specifying them in optional
21494 argument SPEC, as an alist following the pattern
21496 (CHARACTER . REPLACEMENT-STRING).
21498 When PROCESS is a list of commands, optional argument LOG-BUF can
21499 be set to a buffer or a buffer name. `shell-command' then uses
21501 (let* ((base-name (file-name-base source
))
21502 (full-name (file-truename source
))
21503 (out-dir (or (file-name-directory source
) "./"))
21504 (output (expand-file-name (concat base-name
"." ext
) out-dir
))
21505 (time (current-time))
21506 (err-msg (if (stringp err-msg
) (concat ". " err-msg
) "")))
21507 (save-window-excursion
21509 ((pred functionp
) (funcall process
(shell-quote-argument source
)))
21511 (let ((log-buf (and log-buf
(get-buffer-create log-buf
)))
21513 `((?b .
,(shell-quote-argument base-name
))
21514 (?f .
,(shell-quote-argument source
))
21515 (?F .
,(shell-quote-argument full-name
))
21516 (?o .
,(shell-quote-argument out-dir
))
21517 (?O .
,(shell-quote-argument output
))))))
21518 (dolist (command process
)
21519 (shell-command (format-spec command spec
) log-buf
))
21520 (when log-buf
(with-current-buffer log-buf
(compilation-mode)))))
21521 (_ (error "No valid command to process %S%s" source err-msg
))))
21522 ;; Check for process failure. Output file is expected to be
21523 ;; located in the same directory as SOURCE.
21524 (unless (org-file-newer-than-p output time
)
21525 (error (format "File %S wasn't produced%s" output err-msg
)))
21530 (defvar org-element-greater-elements
)
21531 (defun org--get-expected-indentation (element contentsp
)
21532 "Expected indentation column for current line, according to ELEMENT.
21533 ELEMENT is an element containing point. CONTENTSP is non-nil
21534 when indentation is to be computed according to contents of
21536 (let ((type (org-element-type element
))
21537 (start (org-element-property :begin element
))
21538 (post-affiliated (org-element-property :post-affiliated element
)))
21539 (org-with-wide-buffer
21543 ((diary-sexp footnote-definition
) 0)
21544 ((headline inlinetask nil
)
21545 (if (not org-adapt-indentation
) 0
21546 (let ((level (org-current-level)))
21547 (if level
(1+ level
) 0))))
21548 ((item plain-list
) (org-list-item-body-column post-affiliated
))
21551 (org-get-indentation))))
21552 ((memq type
'(headline inlinetask nil
))
21553 (if (org-match-line "[ \t]*$")
21554 (org--get-expected-indentation element t
)
21556 ((memq type
'(diary-sexp footnote-definition
)) 0)
21557 ;; First paragraph of a footnote definition or an item.
21558 ;; Indent like parent.
21559 ((< (line-beginning-position) start
)
21560 (org--get-expected-indentation
21561 (org-element-property :parent element
) t
))
21562 ;; At first line: indent according to previous sibling, if any,
21563 ;; ignoring footnote definitions and inline tasks, or parent's
21565 ((= (line-beginning-position) start
)
21568 (if (= (point-min) start
) (throw 'exit
0)
21569 (goto-char (1- start
))
21570 (let* ((previous (org-element-at-point))
21572 (while (and parent
(<= (org-element-property :end parent
) start
))
21573 (setq previous parent
21574 parent
(org-element-property :parent parent
)))
21576 ((not previous
) (throw 'exit
0))
21577 ((> (org-element-property :end previous
) start
)
21578 (throw 'exit
(org--get-expected-indentation previous t
)))
21579 ((memq (org-element-type previous
)
21580 '(footnote-definition inlinetask
))
21581 (setq start
(org-element-property :begin previous
)))
21582 (t (goto-char (org-element-property :begin previous
))
21584 (if (bolp) (org-get-indentation)
21585 ;; At first paragraph in an item or
21586 ;; a footnote definition.
21587 (org--get-expected-indentation
21588 (org-element-property :parent previous
) t
))))))))))
21589 ;; Otherwise, move to the first non-blank line above.
21591 (beginning-of-line)
21592 (let ((pos (point)))
21593 (skip-chars-backward " \r\t\n")
21595 ;; Two blank lines end a footnote definition or a plain
21596 ;; list. When we indent an empty line after them, the
21597 ;; containing list or footnote definition is over, so it
21598 ;; qualifies as a previous sibling. Therefore, we indent
21599 ;; like its first line.
21600 ((and (memq type
'(footnote-definition plain-list
))
21601 (> (count-lines (point) pos
) 2))
21603 (org-get-indentation))
21604 ;; Line above is the first one of a paragraph at the
21605 ;; beginning of an item or a footnote definition. Indent
21607 ((< (line-beginning-position) start
)
21608 (org--get-expected-indentation
21609 (org-element-property :parent element
) t
))
21610 ;; Line above is the beginning of an element, i.e., point
21611 ;; was originally on the blank lines between element's start
21613 ((= (line-beginning-position) post-affiliated
)
21614 (org--get-expected-indentation element t
))
21615 ;; POS is after contents in a greater element. Indent like
21616 ;; the beginning of the element.
21617 ((and (memq type org-element-greater-elements
)
21618 (let ((cend (org-element-property :contents-end element
)))
21619 (and cend
(<= cend pos
))))
21620 ;; As a special case, if point is at the end of a footnote
21621 ;; definition or an item, indent like the very last element
21622 ;; within. If that last element is an item, indent like
21624 (if (memq type
'(footnote-definition item plain-list
))
21625 (let ((last (org-element-at-point)))
21627 (org--get-expected-indentation
21628 last
(eq (org-element-type last
) 'item
)))
21630 (org-get-indentation)))
21631 ;; In any other case, indent like the current line.
21632 (t (org-get-indentation)))))))))
21634 (defun org--align-node-property ()
21635 "Align node property at point.
21636 Alignment is done according to `org-property-format', which see."
21637 (when (save-excursion
21638 (beginning-of-line)
21639 (looking-at org-property-re
))
21641 (concat (match-string 4)
21643 (format org-property-format
(match-string 1) (match-string 3))))
21646 (defun org-indent-line ()
21647 "Indent line depending on context.
21649 Indentation is done according to the following rules:
21651 - Footnote definitions, diary sexps, headlines and inline tasks
21652 have to start at column 0.
21654 - On the very first line of an element, consider, in order, the
21655 next rules until one matches:
21657 1. If there's a sibling element before, ignoring footnote
21658 definitions and inline tasks, indent like its first line.
21660 2. If element has a parent, indent like its contents. More
21661 precisely, if parent is an item, indent after the
21662 description part, if any, or the bullet (see
21663 `org-list-description-max-indent'). Else, indent like
21664 parent's first line.
21666 3. Otherwise, indent relatively to current level, if
21667 `org-adapt-indentation' is non-nil, or to left margin.
21669 - On a blank line at the end of an element, indent according to
21670 the type of the element. More precisely
21672 1. If element is a plain list, an item, or a footnote
21673 definition, indent like the very last element within.
21675 2. If element is a paragraph, indent like its last non blank
21678 3. Otherwise, indent like its very first line.
21680 - In the code part of a source block, use language major mode
21681 to indent current line if `org-src-tab-acts-natively' is
21682 non-nil. If it is nil, do nothing.
21684 - Otherwise, indent like the first non-blank line above.
21686 The function doesn't indent an item as it could break the whole
21687 list structure. Instead, use \\<org-mode-map>`\\[org-shiftmetaleft]' or \
21688 `\\[org-shiftmetaright]'.
21690 Also align node properties according to `org-property-format'."
21693 ((org-at-heading-p) 'noindent
)
21695 (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
21696 (type (org-element-type element
)))
21697 (cond ((and (memq type
'(plain-list item
))
21698 (= (line-beginning-position)
21699 (org-element-property :post-affiliated element
)))
21701 ((and (eq type
'latex-environment
)
21702 (>= (point) (org-element-property :post-affiliated element
))
21703 (< (point) (org-with-wide-buffer
21704 (goto-char (org-element-property :end element
))
21705 (skip-chars-backward " \r\t\n")
21706 (line-beginning-position 2))))
21708 ((and (eq type
'src-block
)
21709 org-src-tab-acts-natively
21710 (> (line-beginning-position)
21711 (org-element-property :post-affiliated element
))
21712 (< (line-beginning-position)
21713 (org-with-wide-buffer
21714 (goto-char (org-element-property :end element
))
21715 (skip-chars-backward " \r\t\n")
21716 (line-beginning-position))))
21717 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
21719 (let ((column (org--get-expected-indentation element nil
)))
21720 ;; Preserve current column.
21721 (if (<= (current-column) (current-indentation))
21722 (indent-line-to column
)
21723 (save-excursion (indent-line-to column
))))
21724 ;; Align node property. Also preserve current column.
21725 (when (eq type
'node-property
)
21726 (let ((column (current-column)))
21727 (org--align-node-property)
21728 (org-move-to-column column
)))))))))
21730 (defun org-indent-region (start end
)
21731 "Indent each non-blank line in the region.
21732 Called from a program, START and END specify the region to
21733 indent. The function will not indent contents of example blocks,
21734 verse blocks and export blocks as leading white spaces are
21735 assumed to be significant there."
21739 (skip-chars-forward " \r\t\n")
21740 (unless (eobp) (beginning-of-line))
21743 ;; Set IND as indentation for all lines between point and
21744 ;; POS. Blank lines are ignored. Leave point after POS
21746 (let ((limit (copy-marker pos
)))
21747 (while (< (point) limit
)
21748 (unless (looking-at-p "[ \t]*$") (indent-line-to ind
))
21750 (set-marker limit nil
))))
21751 (end (copy-marker end
)))
21752 (while (< (point) end
)
21753 (if (or (looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
21754 (let* ((element (org-element-at-point))
21755 (type (org-element-type element
))
21756 (element-end (copy-marker (org-element-property :end element
)))
21757 (ind (org--get-expected-indentation element nil
)))
21759 ;; Element indented as a single block. Example blocks
21760 ;; preserving indentation are a special case since the
21761 ;; "contents" must not be indented whereas the block
21763 ((or (memq type
'(export-block latex-environment
))
21764 (and (eq type
'example-block
)
21766 (or org-src-preserve-indentation
21767 (org-element-property :preserve-indent element
)))))
21768 (let ((offset (- ind
(org-get-indentation))))
21769 (unless (zerop offset
)
21770 (indent-rigidly (org-element-property :begin element
)
21771 (org-element-property :end element
)
21773 (goto-char element-end
))
21774 ;; Elements indented line wise. Be sure to exclude
21775 ;; example blocks (preserving indentation) and source
21776 ;; blocks from this category as they are treated
21777 ;; specially later.
21778 ((or (memq type
'(paragraph table table-row
))
21779 (not (or (org-element-property :contents-begin element
)
21780 (memq type
'(example-block src-block
)))))
21781 (when (eq type
'node-property
)
21782 (org--align-node-property)
21783 (beginning-of-line))
21784 (funcall indent-to ind
(min element-end end
)))
21785 ;; Elements consisting of three parts: before the
21786 ;; contents, the contents, and after the contents. The
21787 ;; contents are treated specially, according to the
21788 ;; element type, or not indented at all. Other parts are
21789 ;; indented as a single block.
21791 (let* ((post (copy-marker
21792 (org-element-property :post-affiliated element
)))
21796 ((not (org-element-property :contents-begin element
))
21797 ;; Fake contents for source blocks.
21798 (org-with-wide-buffer
21800 (line-beginning-position 2)))
21801 ((memq type
'(footnote-definition item plain-list
))
21802 ;; Contents in these elements could start on
21803 ;; the same line as the beginning of the
21804 ;; element. Make sure we start indenting
21805 ;; from the second line.
21806 (org-with-wide-buffer
21809 (skip-chars-forward " \r\t\n")
21810 (if (eobp) (point) (line-beginning-position))))
21811 (t (org-element-property :contents-begin element
)))))
21813 (or (org-element-property :contents-end element
)
21814 ;; Fake contents for source blocks.
21815 (org-with-wide-buffer
21816 (goto-char element-end
)
21817 (skip-chars-backward " \r\t\n")
21818 (line-beginning-position)))
21820 ;; Do not change items indentation individually as it
21821 ;; might break the list as a whole. On the other
21822 ;; hand, when at a plain list, indent it as a whole.
21823 (cond ((eq type
'plain-list
)
21824 (let ((offset (- ind
(org-get-indentation))))
21825 (unless (zerop offset
)
21826 (indent-rigidly (org-element-property :begin element
)
21827 (org-element-property :end element
)
21830 ((eq type
'item
) (goto-char cbeg
))
21831 (t (funcall indent-to ind
(min cbeg end
))))
21832 (when (< (point) end
)
21834 ((example-block verse-block
))
21836 ;; In a source block, indent source code
21837 ;; according to language major mode, but only if
21838 ;; `org-src-tab-acts-natively' is non-nil.
21839 (when (and (< (point) end
) org-src-tab-acts-natively
)
21841 (org-babel-do-in-edit-buffer
21842 (indent-region (point-min) (point-max))))))
21843 (t (org-indent-region (point) (min cend end
))))
21844 (goto-char (min cend end
))
21845 (when (< (point) end
)
21846 (funcall indent-to ind
(min element-end end
))))
21847 (set-marker post nil
)
21848 (set-marker cbeg nil
)
21849 (set-marker cend nil
))))
21850 (set-marker element-end nil
))))
21851 (set-marker end nil
))))
21853 (defun org-indent-drawer ()
21854 "Indent the drawer at point."
21856 (unless (save-excursion
21857 (beginning-of-line)
21858 (looking-at-p org-drawer-regexp
))
21859 (user-error "Not at a drawer"))
21860 (let ((element (org-element-at-point)))
21861 (unless (memq (org-element-type element
) '(drawer property-drawer
))
21862 (user-error "Not at a drawer"))
21863 (org-with-wide-buffer
21864 (org-indent-region (org-element-property :begin element
)
21865 (org-element-property :end element
))))
21866 (message "Drawer at point indented"))
21868 (defun org-indent-block ()
21869 "Indent the block at point."
21871 (unless (save-excursion
21872 (beginning-of-line)
21873 (let ((case-fold-search t
))
21874 (looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
21875 (user-error "Not at a block"))
21876 (let ((element (org-element-at-point)))
21877 (unless (memq (org-element-type element
)
21878 '(comment-block center-block dynamic-block example-block
21879 export-block quote-block special-block
21880 src-block verse-block
))
21881 (user-error "Not at a block"))
21882 (org-with-wide-buffer
21883 (org-indent-region (org-element-property :begin element
)
21884 (org-element-property :end element
))))
21885 (message "Block at point indented"))
21890 ;; We use our own fill-paragraph and auto-fill functions.
21892 ;; `org-fill-paragraph' relies on adaptive filling and context
21893 ;; checking. Appropriate `fill-prefix' is computed with
21894 ;; `org-adaptive-fill-function'.
21896 ;; `org-auto-fill-function' takes care of auto-filling. It calls
21897 ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
21898 ;; `org-adaptive-fill-function' value. Internally,
21899 ;; `org-comment-line-break-function' breaks the line.
21901 ;; `org-setup-filling' installs filling and auto-filling related
21902 ;; variables during `org-mode' initialization.
21904 (defun org-setup-filling ()
21905 (require 'org-element
)
21906 ;; Prevent auto-fill from inserting unwanted new items.
21907 (when (boundp 'fill-nobreak-predicate
)
21909 fill-nobreak-predicate
21911 (append fill-nobreak-predicate
21912 '(org-fill-line-break-nobreak-p
21913 org-fill-n-macro-as-item-nobreak-p
21914 org-fill-paragraph-with-timestamp-nobreak-p
)))))
21915 (let ((paragraph-ending (substring org-element-paragraph-separate
1)))
21916 (setq-local paragraph-start paragraph-ending
)
21917 (setq-local paragraph-separate paragraph-ending
))
21918 (setq-local fill-paragraph-function
'org-fill-paragraph
)
21919 (setq-local auto-fill-inhibit-regexp nil
)
21920 (setq-local adaptive-fill-function
'org-adaptive-fill-function
)
21921 (setq-local normal-auto-fill-function
'org-auto-fill-function
)
21922 (setq-local comment-line-break-function
'org-comment-line-break-function
))
21924 (defun org-fill-line-break-nobreak-p ()
21925 "Non-nil when a new line at point would create an Org line break."
21927 (skip-chars-backward "[ \t]")
21928 (skip-chars-backward "\\\\")
21929 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
21931 (defun org-fill-paragraph-with-timestamp-nobreak-p ()
21932 "Non-nil when a new line at point would split a timestamp."
21933 (and (org-at-timestamp-p 'lax
)
21934 (not (looking-at org-ts-regexp-both
))))
21936 (defun org-fill-n-macro-as-item-nobreak-p ()
21937 "Non-nil when a new line at point would create a new list."
21938 ;; During export, a "n" macro followed by a dot or a closing
21939 ;; parenthesis can end up being parsed as a new list item.
21940 (looking-at-p "[ \t]*{{{n\\(?:([^\n)]*)\\)?}}}[.)]\\(?:$\\| \\)"))
21942 (defvar orgtbl-line-start-regexp
) ; From org-table.el
21943 (defun org-adaptive-fill-function ()
21944 "Compute a fill prefix for the current line.
21945 Return fill prefix, as a string, or nil if current line isn't
21946 meant to be filled. For convenience, if `adaptive-fill-regexp'
21947 matches in paragraphs or comments, use it."
21948 (org-with-wide-buffer
21949 (unless (org-at-heading-p)
21950 (let* ((p (line-beginning-position))
21951 (element (save-excursion
21952 (beginning-of-line)
21953 (org-element-at-point)))
21954 (type (org-element-type element
))
21955 (post-affiliated (org-element-property :post-affiliated element
)))
21956 (unless (< p post-affiliated
)
21960 (beginning-of-line)
21961 (looking-at "[ \t]*")
21962 (concat (match-string 0) "# ")))
21963 (footnote-definition "")
21965 (make-string (org-list-item-body-column post-affiliated
) ?\s
))
21967 ;; Fill prefix is usually the same as the current line,
21968 ;; unless the paragraph is at the beginning of an item.
21969 (let ((parent (org-element-property :parent element
)))
21971 (beginning-of-line)
21972 (cond ((eq (org-element-type parent
) 'item
)
21973 (make-string (org-list-item-body-column
21974 (org-element-property :begin parent
))
21976 ((and adaptive-fill-regexp
21978 ;; `adaptive-fill-function' to let
21979 ;; `fill-context-prefix' handle
21980 ;; `adaptive-fill-regexp' variable.
21981 (let (adaptive-fill-function)
21982 (fill-context-prefix
21984 (org-element-property :end element
)))))
21985 ((looking-at "[ \t]+") (match-string 0))
21988 ;; Only fill contents if P is within block boundaries.
21989 (let* ((cbeg (save-excursion (goto-char post-affiliated
)
21992 (cend (save-excursion
21993 (goto-char (org-element-property :end element
))
21994 (skip-chars-backward " \r\t\n")
21995 (line-beginning-position))))
21996 (when (and (>= p cbeg
) (< p cend
))
21997 (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
22001 (defun org-fill-element (&optional justify
)
22002 "Fill element at point, when applicable.
22004 This function only applies to comment blocks, comments, example
22005 blocks and paragraphs. Also, as a special case, re-align table
22006 when point is at one.
22008 If JUSTIFY is non-nil (interactively, with prefix argument),
22009 justify as well. If `sentence-end-double-space' is non-nil, then
22010 period followed by one space does not end a sentence, so don't
22011 break a line there. The variable `fill-column' controls the
22014 For convenience, when point is at a plain list, an item or
22015 a footnote definition, try to fill the first paragraph within."
22016 (with-syntax-table org-mode-transpose-word-syntax-table
22017 ;; Move to end of line in order to get the first paragraph within
22018 ;; a plain list or a footnote definition.
22019 (let ((element (save-excursion (end-of-line) (org-element-at-point))))
22020 ;; First check if point is in a blank line at the beginning of
22021 ;; the buffer. In that case, ignore filling.
22022 (cl-case (org-element-type element
)
22023 ;; Use major mode filling function is src blocks.
22024 (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
22025 ;; Align Org tables, leave table.el tables as-is.
22026 (table-row (org-table-align) t
)
22028 (when (eq (org-element-property :type element
) 'org
)
22030 (goto-char (org-element-property :post-affiliated element
))
22031 (org-table-align)))
22034 ;; Paragraphs may contain `line-break' type objects.
22035 (let ((beg (max (point-min)
22036 (org-element-property :contents-begin element
)))
22037 (end (min (point-max)
22038 (org-element-property :contents-end element
))))
22039 ;; Do nothing if point is at an affiliated keyword.
22040 (if (< (line-end-position) beg
) t
22041 ;; Fill paragraph, taking line breaks into account.
22044 (let ((cuts (list beg
)))
22045 (while (re-search-forward "\\\\\\\\[ \t]*\n" end t
)
22046 (when (eq 'line-break
22048 (save-excursion (backward-char)
22049 (org-element-context))))
22050 (push (point) cuts
)))
22051 (dolist (c (delq end cuts
))
22052 (fill-region-as-paragraph c end justify
)
22055 ;; Contents of `comment-block' type elements should be
22056 ;; filled as plain text, but only if point is within block
22059 (let* ((case-fold-search t
)
22060 (beg (save-excursion
22061 (goto-char (org-element-property :begin element
))
22062 (re-search-forward "^[ \t]*#\\+begin_comment" nil t
)
22065 (end (save-excursion
22066 (goto-char (org-element-property :end element
))
22067 (re-search-backward "^[ \t]*#\\+end_comment" nil t
)
22068 (line-beginning-position))))
22069 (if (or (< (point) beg
) (> (point) end
)) t
22070 (fill-region-as-paragraph
22071 (save-excursion (end-of-line)
22072 (re-search-backward "^[ \t]*$" beg
'move
)
22073 (line-beginning-position))
22074 (save-excursion (beginning-of-line)
22075 (re-search-forward "^[ \t]*$" end
'move
)
22076 (line-beginning-position))
22080 (let ((begin (org-element-property :post-affiliated element
))
22081 (end (org-element-property :end element
)))
22082 (when (and (>= (point) begin
) (<= (point) end
))
22083 (let ((begin (save-excursion
22085 (if (re-search-backward "^[ \t]*#[ \t]*$" begin t
)
22086 (progn (forward-line) (point))
22088 (end (save-excursion
22090 (if (re-search-forward "^[ \t]*#[ \t]*$" end
'move
)
22091 (1- (line-beginning-position))
22092 (skip-chars-backward " \r\t\n")
22093 (line-end-position)))))
22094 ;; Do not fill comments when at a blank line.
22095 (when (> end begin
)
22098 (beginning-of-line)
22099 (looking-at "[ \t]*#")
22100 (let ((comment-prefix (match-string 0)))
22101 (goto-char (match-end 0))
22102 (if (looking-at adaptive-fill-regexp
)
22103 (concat comment-prefix
(match-string 0))
22104 (concat comment-prefix
" "))))))
22106 (fill-region-as-paragraph begin end justify
))))))
22108 ;; Ignore every other element.
22111 (defun org-fill-paragraph (&optional justify region
)
22112 "Fill element at point, when applicable.
22114 This function only applies to comment blocks, comments, example
22115 blocks and paragraphs. Also, as a special case, re-align table
22116 when point is at one.
22118 For convenience, when point is at a plain list, an item or
22119 a footnote definition, try to fill the first paragraph within.
22121 If JUSTIFY is non-nil (interactively, with prefix argument),
22122 justify as well. If `sentence-end-double-space' is non-nil, then
22123 period followed by one space does not end a sentence, so don't
22124 break a line there. The variable `fill-column' controls the
22127 The REGION argument is non-nil if called interactively; in that
22128 case, if Transient Mark mode is enabled and the mark is active,
22129 fill each of the elements in the active region, instead of just
22130 filling the current element."
22131 (interactive (progn
22132 (barf-if-buffer-read-only)
22133 (list (if current-prefix-arg
'full
) t
)))
22135 ((and region transient-mark-mode mark-active
22136 (not (eq (region-beginning) (region-end))))
22137 (let ((origin (point-marker))
22138 (start (region-beginning)))
22141 (goto-char (region-end))
22142 (while (> (point) start
)
22143 (org-backward-paragraph)
22144 (org-fill-element justify
)))
22146 (set-marker origin nil
))))
22147 (t (org-fill-element justify
))))
22149 (defun org-auto-fill-function ()
22150 "Auto-fill function."
22151 ;; Check if auto-filling is meaningful.
22152 (let ((fc (current-fill-column)))
22153 (when (and fc
(> (current-column) fc
))
22154 (let* ((fill-prefix (org-adaptive-fill-function))
22155 ;; Enforce empty fill prefix, if required. Otherwise, it
22156 ;; will be computed again.
22157 (adaptive-fill-mode (not (equal fill-prefix
""))))
22158 (when fill-prefix
(do-auto-fill))))))
22160 (defun org-comment-line-break-function (&optional soft
)
22161 "Break line at point and indent, continuing comment if within one.
22162 The inserted newline is marked hard if variable
22163 `use-hard-newlines' is true, unless optional argument SOFT is
22165 (if soft
(insert-and-inherit ?
\n) (newline 1))
22166 (save-excursion (forward-char -
1) (delete-horizontal-space))
22167 (delete-horizontal-space)
22168 (indent-to-left-margin)
22169 (insert-before-markers-and-inherit fill-prefix
))
22172 ;;; Fixed Width Areas
22174 (defun org-toggle-fixed-width ()
22175 "Toggle fixed-width markup.
22177 Add or remove fixed-width markup on current line, whenever it
22178 makes sense. Return an error otherwise.
22180 If a region is active and if it contains only fixed-width areas
22181 or blank lines, remove all fixed-width markup in it. If the
22182 region contains anything else, convert all non-fixed-width lines
22183 to fixed-width ones.
22185 Blank lines at the end of the region are ignored unless the
22186 region only contains such lines."
22188 (if (not (org-region-active-p))
22191 ;; Remove fixed width marker only in a fixed-with element.
22193 ;; Add fixed width maker in paragraphs, in blank lines after
22194 ;; elements or at the beginning of a headline or an inlinetask,
22195 ;; and before any one-line elements (e.g., a clock).
22197 (beginning-of-line)
22198 (let* ((element (org-element-at-point))
22199 (type (org-element-type element
)))
22201 ((and (eq type
'fixed-width
)
22202 (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
22204 "" nil nil nil
(if (= (line-end-position) (match-end 0)) 0 1)))
22205 ((and (memq type
'(babel-call clock comment diary-sexp headline
22206 horizontal-rule keyword paragraph
22208 (<= (org-element-property :post-affiliated element
) (point)))
22209 (skip-chars-forward " \t")
22211 ((and (looking-at-p "[ \t]*$")
22212 (or (eq type
'inlinetask
)
22214 (skip-chars-forward " \r\t\n")
22215 (<= (org-element-property :end element
) (point)))))
22216 (delete-region (point) (line-end-position))
22219 (t (user-error "Cannot insert a fixed-width line here")))))
22221 (let* ((begin (save-excursion
22222 (goto-char (region-beginning))
22223 (line-beginning-position)))
22226 (goto-char (region-end))
22227 (unless (eolp) (beginning-of-line))
22228 (if (save-excursion (re-search-backward "\\S-" begin t
))
22229 (progn (skip-chars-backward " \r\t\n") (point))
22235 (skip-chars-forward " \r\t\n")
22236 (when (eobp) (throw 'not-all-p nil
))
22237 (while (< (point) end
)
22238 (let ((element (org-element-at-point)))
22239 (if (eq (org-element-type element
) 'fixed-width
)
22240 (goto-char (org-element-property :end element
))
22241 (throw 'not-all-p nil
))))
22243 (if all-fixed-width-p
22246 (while (< (point) end
)
22247 (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
22250 (if (= (line-end-position) (match-end 0)) 0 1)))
22252 (let ((min-ind (point-max)))
22253 ;; Find minimum indentation across all lines.
22256 (if (not (save-excursion (re-search-forward "\\S-" end t
)))
22259 (while (< (point) end
)
22260 (unless (looking-at-p "[ \t]*$")
22261 (let ((ind (org-get-indentation)))
22262 (setq min-ind
(min min-ind ind
))
22263 (when (zerop ind
) (throw 'zerop t
))))
22265 ;; Loop over all lines and add fixed-width markup everywhere
22266 ;; but in fixed-width lines.
22269 (while (< (point) end
)
22271 ((org-at-heading-p)
22274 (while (and (< (point) end
) (looking-at-p "[ \t]*$"))
22277 ((looking-at-p "[ \t]*:\\( \\|$\\)")
22278 (let* ((element (org-element-at-point))
22279 (element-end (org-element-property :end element
)))
22280 (if (eq (org-element-type element
) 'fixed-width
)
22281 (progn (goto-char element-end
)
22282 (skip-chars-backward " \r\t\n")
22284 (let ((limit (min end element-end
)))
22285 (while (< (point) limit
)
22286 (org-move-to-column min-ind t
)
22288 (forward-line))))))
22290 (org-move-to-column min-ind t
)
22292 (forward-line)))))))
22293 (set-marker end nil
))))
22298 (defun org-block-map (function &optional start end
)
22299 "Call FUNCTION at the head of all source blocks in the current buffer.
22300 Optional arguments START and END can be used to limit the range."
22301 (let ((start (or start
(point-min)))
22302 (end (or end
(point-max))))
22305 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
22308 (goto-char (match-beginning 0))
22309 (funcall function
)))))))
22311 (defun org-next-block (arg &optional backward block-regexp
)
22312 "Jump to the next block.
22314 With a prefix argument ARG, jump forward ARG many blocks.
22316 When BACKWARD is non-nil, jump to the previous block.
22318 When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
22319 Match data is set according to this regexp when the function
22322 Return point at beginning of the opening line of found block.
22323 Throw an error if no block is found."
22325 (let ((re (or block-regexp
"^[ \t]*#\\+BEGIN"))
22326 (case-fold-search t
)
22327 (search-fn (if backward
#'re-search-backward
#'re-search-forward
))
22331 (if backward
(beginning-of-line) (end-of-line))
22332 (while (and (> count
0) (funcall search-fn re nil t
))
22333 (let ((element (save-excursion
22334 (goto-char (match-beginning 0))
22335 (save-match-data (org-element-at-point)))))
22336 (when (and (memq (org-element-type element
)
22337 '(center-block comment-block dynamic-block
22338 example-block export-block quote-block
22339 special-block src-block verse-block
))
22340 (<= (match-beginning 0)
22341 (org-element-property :post-affiliated element
)))
22342 (setq last-element element
)
22345 (prog1 (goto-char (org-element-property :post-affiliated last-element
))
22346 (save-match-data (org-show-context)))
22348 (user-error "No %s code blocks" (if backward
"previous" "further")))))
22350 (defun org-previous-block (arg &optional block-regexp
)
22351 "Jump to the previous block.
22352 With a prefix argument ARG, jump backward ARG many source blocks.
22353 When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
22355 (org-next-block arg t block-regexp
))
22360 ;; Org comments syntax is quite complex. It requires the entire line
22361 ;; to be just a comment. Also, even with the right syntax at the
22362 ;; beginning of line, some elements (e.g., verse-block or
22363 ;; example-block) don't accept comments. Usual Emacs comment commands
22364 ;; cannot cope with those requirements. Therefore, Org replaces them.
22366 ;; Org still relies on `comment-dwim', but cannot trust
22367 ;; `comment-only-p'. So, `comment-region-function' and
22368 ;; `uncomment-region-function' both point
22369 ;; to`org-comment-or-uncomment-region'. Eventually,
22370 ;; `org-insert-comment' takes care of insertion of comments at the
22371 ;; beginning of line.
22373 ;; `org-setup-comments-handling' install comments related variables
22374 ;; during `org-mode' initialization.
22376 (defun org-setup-comments-handling ()
22378 (setq-local comment-use-syntax nil
)
22379 (setq-local comment-start
"# ")
22380 (setq-local comment-start-skip
"^\\s-*#\\(?: \\|$\\)")
22381 (setq-local comment-insert-comment-function
'org-insert-comment
)
22382 (setq-local comment-region-function
'org-comment-or-uncomment-region
)
22383 (setq-local uncomment-region-function
'org-comment-or-uncomment-region
))
22385 (defun org-insert-comment ()
22386 "Insert an empty comment above current line.
22387 If the line is empty, insert comment at its beginning. When
22388 point is within a source block, comment according to the related
22390 (if (let ((element (org-element-at-point)))
22391 (and (eq (org-element-type element
) 'src-block
)
22393 (goto-char (org-element-property :post-affiliated element
))
22394 (line-end-position))
22397 (goto-char (org-element-property :end element
))
22398 (skip-chars-backward " \r\t\n")
22399 (line-beginning-position))
22401 (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim
))
22402 (beginning-of-line)
22403 (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
22408 (defvar comment-empty-lines
) ; From newcomment.el.
22409 (defun org-comment-or-uncomment-region (beg end
&rest _
)
22410 "Comment or uncomment each non-blank line in the region.
22411 Uncomment each non-blank line between BEG and END if it only
22412 contains commented lines. Otherwise, comment them. If region is
22413 strictly within a source block, use appropriate comment syntax."
22414 (if (let ((element (org-element-at-point)))
22415 (and (eq (org-element-type element
) 'src-block
)
22417 (goto-char (org-element-property :post-affiliated element
))
22418 (line-end-position))
22420 (>= (save-excursion
22421 (goto-char (org-element-property :end element
))
22422 (skip-chars-backward " \r\t\n")
22423 (line-beginning-position))
22425 ;; Translate region boundaries for the Org buffer to the source
22427 (let ((offset (- end beg
)))
22430 (org-babel-do-in-edit-buffer
22431 (comment-or-uncomment-region (point) (+ offset
(point))))))
22434 (narrow-to-region (save-excursion (goto-char beg
)
22435 (skip-chars-forward " \r\t\n" end
)
22436 (line-beginning-position))
22437 (save-excursion (goto-char end
)
22438 (skip-chars-backward " \r\t\n" beg
)
22439 (line-end-position)))
22441 ;; UNCOMMENTP is non-nil when every non blank line between
22442 ;; BEG and END is a comment.
22444 (goto-char (point-min))
22445 (while (and (not (eobp))
22446 (let ((element (org-element-at-point)))
22447 (and (eq (org-element-type element
) 'comment
)
22448 (goto-char (min (point-max)
22449 (org-element-property
22450 :end element
)))))))
22453 ;; Only blank lines and comments in region: uncomment it.
22455 (goto-char (point-min))
22456 (while (not (eobp))
22457 (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
22458 (replace-match "" nil nil nil
1))
22460 ;; Comment each line in region.
22461 (let ((min-indent (point-max)))
22462 ;; First find the minimum indentation across all lines.
22464 (goto-char (point-min))
22465 (while (and (not (eobp)) (not (zerop min-indent
)))
22466 (unless (looking-at "[ \t]*$")
22467 (setq min-indent
(min min-indent
(current-indentation))))
22469 ;; Then loop over all lines.
22471 (goto-char (point-min))
22472 (while (not (eobp))
22473 (unless (and (not comment-empty-lines
) (looking-at "[ \t]*$"))
22474 ;; Don't get fooled by invisible text (e.g. link path)
22475 ;; when moving to column MIN-INDENT.
22476 (let ((buffer-invisibility-spec nil
))
22477 (org-move-to-column min-indent t
))
22478 (insert comment-start
))
22479 (forward-line)))))))))
22481 (defun org-comment-dwim (_arg)
22482 "Call the comment command you mean.
22483 Call `org-toggle-comment' if on a heading, otherwise call
22484 `comment-dwim', within a source edit buffer if needed."
22486 (cond ((org-at-heading-p)
22487 (call-interactively #'org-toggle-comment
))
22488 ((org-in-src-block-p)
22489 (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim
)))
22490 (t (call-interactively #'comment-dwim
))))
22495 ;; This section contains tools to operate on, or create, timestamp
22496 ;; objects, as returned by, e.g. `org-element-context'.
22498 (defun org-timestamp-from-string (s)
22499 "Convert Org timestamp S, as a string, into a timestamp object.
22500 Return nil if S is not a valid timestamp string."
22501 (when (org-string-nw-p s
)
22503 (save-excursion (insert s
))
22504 (org-element-timestamp-parser))))
22506 (defun org-timestamp-from-time (time &optional with-time inactive
)
22507 "Convert a time value into a timestamp object.
22509 TIME is an Emacs internal time representation, as returned, e.g.,
22512 When optional argument WITH-TIME is non-nil, return a timestamp
22513 object with a time part, i.e., with hours and minutes.
22515 Return an inactive timestamp if INACTIVE is non-nil. Otherwise,
22516 return an active timestamp."
22517 (pcase-let ((`(,_
,minute
,hour
,day
,month
,year .
,_
) (decode-time time
)))
22518 (org-element-create 'timestamp
22519 (list :type
(if inactive
'inactive
'active
)
22523 :hour-start
(and with-time hour
)
22524 :minute-start
(and with-time minute
)))))
22526 (defun org-timestamp-to-time (timestamp &optional end
)
22527 "Convert TIMESTAMP object into an Emacs internal time value.
22528 Use end of date range or time range when END is non-nil.
22529 Otherwise, use its start."
22530 (apply #'encode-time
22533 (lambda (prop) (or (org-element-property prop timestamp
) 0))
22534 (if end
'(:minute-end
:hour-end
:day-end
:month-end
:year-end
)
22535 '(:minute-start
:hour-start
:day-start
:month-start
22538 (defun org-timestamp-has-time-p (timestamp)
22539 "Non-nil when TIMESTAMP has a time specified."
22540 (org-element-property :hour-start timestamp
))
22542 (defun org-timestamp-format (timestamp format
&optional end utc
)
22543 "Format a TIMESTAMP object into a string.
22545 FORMAT is a format specifier to be passed to
22546 `format-time-string'.
22548 When optional argument END is non-nil, use end of date-range or
22549 time-range, if possible.
22551 When optional argument UTC is non-nil, time is be expressed as
22553 (format-time-string format
(org-timestamp-to-time timestamp end
)
22556 (defun org-timestamp-split-range (timestamp &optional end
)
22557 "Extract a TIMESTAMP object from a date or time range.
22559 END, when non-nil, means extract the end of the range.
22560 Otherwise, extract its start.
22562 Return a new timestamp object."
22563 (let ((type (org-element-property :type timestamp
)))
22564 (if (memq type
'(active inactive diary
)) timestamp
22565 (let ((split-ts (org-element-copy timestamp
)))
22567 (org-element-put-property
22568 split-ts
:type
(if (eq type
'active-range
) 'active
'inactive
))
22569 ;; Copy start properties over end properties if END is
22570 ;; non-nil. Otherwise, copy end properties over `start' ones.
22571 (let ((p-alist '((:minute-start .
:minute-end
)
22572 (:hour-start .
:hour-end
)
22573 (:day-start .
:day-end
)
22574 (:month-start .
:month-end
)
22575 (:year-start .
:year-end
))))
22576 (dolist (p-cell p-alist
)
22577 (org-element-put-property
22579 (funcall (if end
#'car
#'cdr
) p-cell
)
22580 (org-element-property
22581 (funcall (if end
#'cdr
#'car
) p-cell
) split-ts
)))
22582 ;; Eventually refresh `:raw-value'.
22583 (org-element-put-property split-ts
:raw-value nil
)
22584 (org-element-put-property
22585 split-ts
:raw-value
(org-element-interpret-data split-ts
)))))))
22587 (defun org-timestamp-translate (timestamp &optional boundary
)
22588 "Translate TIMESTAMP object to custom format.
22590 Format string is defined in `org-time-stamp-custom-formats',
22593 When optional argument BOUNDARY is non-nil, it is either the
22594 symbol `start' or `end'. In this case, only translate the
22595 starting or ending part of TIMESTAMP if it is a date or time
22596 range. Otherwise, translate both parts.
22598 Return timestamp as-is if `org-display-custom-times' is nil or if
22599 it has a `diary' type."
22600 (let ((type (org-element-property :type timestamp
)))
22601 (if (or (not org-display-custom-times
) (eq type
'diary
))
22602 (org-element-interpret-data timestamp
)
22603 (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp
) #'cdr
#'car
)
22604 org-time-stamp-custom-formats
)))
22605 (if (and (not boundary
) (memq type
'(active-range inactive-range
)))
22606 (concat (org-timestamp-format timestamp fmt
)
22608 (org-timestamp-format timestamp fmt t
))
22609 (org-timestamp-format timestamp fmt
(eq boundary
'end
)))))))
22613 (defvar reftex-docstruct-symbol
)
22616 (defun org-reftex-citation ()
22617 "Use reftex-citation to insert a citation into the buffer.
22618 This looks for a line like
22620 #+BIBLIOGRAPHY: foo plain option:-d
22622 and derives from it that foo.bib is the bibliography file relevant
22623 for this document. It then installs the necessary environment for RefTeX
22624 to work in this buffer and calls `reftex-citation' to insert a citation
22627 Export of such citations to both LaTeX and HTML is handled by the contributed
22628 package ox-bibtex by Taru Karttunen."
22630 (let ((reftex-docstruct-symbol 'org--rds
)
22632 (org-with-wide-buffer
22633 (let ((case-fold-search t
)
22634 (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
22635 (if (not (save-excursion
22636 (or (re-search-forward re nil t
)
22637 (re-search-backward re nil t
))))
22638 (user-error "No bibliography defined in file")
22639 (setq bib
(concat (match-string 1) ".bib")
22640 org--rds
(list (list 'bib bib
))))))
22641 (call-interactively 'reftex-citation
)))
22643 ;;;; Functions extending outline functionality
22645 (defun org-beginning-of-line (&optional n
)
22646 "Go to the beginning of the current visible line.
22648 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
22649 tags on the first attempt, and only move to after the tags when
22650 the cursor is already beyond the end of the headline.
22652 With argument N not nil or 1, move forward N - 1 lines first."
22654 (let ((origin (point))
22655 (special (pcase org-special-ctrl-a
/e
22656 (`(,C-a .
,_
) C-a
) (_ org-special-ctrl-a
/e
)))
22658 ;; First move to a visible line.
22659 (if (bound-and-true-p visual-line-mode
)
22660 (beginning-of-visual-line n
)
22661 (move-beginning-of-line n
)
22662 ;; `move-beginning-of-line' may leave point after invisible
22663 ;; characters if line starts with such of these (e.g., with
22664 ;; a link at column 0). Really move to the beginning of the
22665 ;; current visible line.
22666 (beginning-of-line))
22668 ;; No special behavior. Point is already at the beginning of
22669 ;; a line, logical or visual.
22671 ;; `beginning-of-visual-line' left point before logical beginning
22672 ;; of line: point is at the beginning of a visual line. Bail
22674 ((and (bound-and-true-p visual-line-mode
) (not (bolp))))
22675 ((let ((case-fold-search nil
)) (looking-at org-complex-heading-regexp
))
22676 ;; At a headline, special position is before the title, but
22677 ;; after any TODO keyword or priority cookie.
22678 (let ((refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
22679 (line-end-position)))
22681 (if (eq special
'reversed
)
22682 (when (and (= origin bol
) (eq last-command this-command
))
22683 (goto-char refpos
))
22684 (when (or (> origin refpos
) (= origin bol
))
22685 (goto-char refpos
)))))
22686 ((and (looking-at org-list-full-item-re
)
22687 (memq (org-element-type (save-match-data (org-element-at-point)))
22688 '(item plain-list
)))
22689 ;; Set special position at first white space character after
22690 ;; bullet, and check-box, if any.
22691 (let ((after-bullet
22692 (let ((box (match-end 3)))
22693 (cond ((not box
) (match-end 1))
22694 ((eq (char-after box
) ?\s
) (1+ box
))
22696 (if (eq special
'reversed
)
22697 (when (and (= (point) origin
) (eq last-command this-command
))
22698 (goto-char after-bullet
))
22699 (when (or (> origin after-bullet
) (= (point) origin
))
22700 (goto-char after-bullet
)))))
22701 ;; No special context. Point is already at beginning of line.
22704 (defun org-end-of-line (&optional n
)
22705 "Go to the end of the line, but before ellipsis, if any.
22707 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
22708 tags on the first attempt, and only move to after the tags when
22709 the cursor is already beyond the end of the headline.
22711 With argument N not nil or 1, move forward N - 1 lines first."
22713 (let ((origin (point))
22714 (special (pcase org-special-ctrl-a
/e
22715 (`(,_ .
,C-e
) C-e
) (_ org-special-ctrl-a
/e
)))
22717 ;; First move to a visible line.
22718 (if (bound-and-true-p visual-line-mode
)
22719 (beginning-of-visual-line n
)
22720 (move-beginning-of-line n
))
22722 ;; At a headline, with tags.
22725 (beginning-of-line)
22726 (let ((case-fold-search nil
))
22727 (looking-at org-complex-heading-regexp
)))
22729 (let ((tags (save-excursion
22730 (goto-char (match-beginning 5))
22731 (skip-chars-backward " \t")
22733 (visual-end (and (bound-and-true-p visual-line-mode
)
22735 (end-of-visual-line)
22737 ;; If `end-of-visual-line' brings us before end of line or
22738 ;; even tags, i.e., the headline spans over multiple visual
22739 ;; lines, move there.
22740 (cond ((and visual-end
22741 (< visual-end tags
)
22742 (<= origin visual-end
))
22743 (goto-char visual-end
))
22744 ((eq special
'reversed
)
22745 (if (and (= origin
(line-end-position))
22746 (eq this-command last-command
))
22750 (if (or (< origin tags
) (= origin
(line-end-position)))
22753 ((bound-and-true-p visual-line-mode
)
22754 (let ((bol (line-beginning-position)))
22755 (end-of-visual-line)
22756 ;; If `end-of-visual-line' gets us past the ellipsis at the
22757 ;; end of a line, backtrack and use `end-of-line' instead.
22758 (when (/= bol
(line-beginning-position))
22761 (t (end-of-line)))))
22763 (defun org-backward-sentence (&optional _arg
)
22764 "Go to beginning of sentence, or beginning of table field.
22765 This will call `backward-sentence' or `org-table-beginning-of-field',
22766 depending on context."
22768 (let* ((element (org-element-at-point))
22769 (contents-begin (org-element-property :contents-begin element
))
22770 (table (org-element-lineage element
'(table) t
)))
22772 (> (point) contents-begin
)
22773 (<= (point) (org-element-property :contents-end table
)))
22774 (call-interactively #'org-table-beginning-of-field
)
22776 (when (and contents-begin
22777 (< (point-min) contents-begin
)
22778 (> (point) contents-begin
))
22779 (narrow-to-region contents-begin
22780 (org-element-property :contents-end element
)))
22781 (call-interactively #'backward-sentence
)))))
22783 (defun org-forward-sentence (&optional _arg
)
22784 "Go to end of sentence, or end of table field.
22785 This will call `forward-sentence' or `org-table-end-of-field',
22786 depending on context."
22788 (if (and (org-at-heading-p)
22789 (save-restriction (skip-chars-forward " \t") (not (eolp))))
22791 (narrow-to-region (line-beginning-position) (line-end-position))
22792 (call-interactively #'forward-sentence
))
22793 (let* ((element (org-element-at-point))
22794 (contents-end (org-element-property :contents-end element
))
22795 (table (org-element-lineage element
'(table) t
)))
22797 (>= (point) (org-element-property :contents-begin table
))
22798 (< (point) contents-end
))
22799 (call-interactively #'org-table-end-of-field
)
22801 (when (and contents-end
22802 (> (point-max) contents-end
)
22803 ;; Skip blank lines between elements.
22804 (< (org-element-property :end element
)
22805 (save-excursion (goto-char contents-end
)
22806 (skip-chars-forward " \r\t\n"))))
22807 (narrow-to-region (org-element-property :contents-begin element
)
22809 ;; End of heading is considered as the end of a sentence.
22810 (let ((sentence-end (concat (sentence-end) "\\|^\\*+ .*$")))
22811 (call-interactively #'forward-sentence
)))))))
22813 (defun org-kill-line (&optional _arg
)
22814 "Kill line, to tags or end of line."
22817 ((or (not org-special-ctrl-k
)
22819 (not (org-at-heading-p)))
22820 (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible
)
22821 org-ctrl-k-protect-subtree
22822 (or (eq org-ctrl-k-protect-subtree
'error
)
22823 (not (y-or-n-p "Kill hidden subtree along with headline? "))))
22824 (user-error "C-k aborted as it would kill a hidden subtree"))
22825 (call-interactively
22826 (if (bound-and-true-p visual-line-mode
) 'kill-visual-line
'kill-line
)))
22827 ((looking-at ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")
22828 (kill-region (point) (match-beginning 1))
22829 (org-set-tags nil t
))
22830 (t (kill-region (point) (point-at-eol)))))
22832 (defun org-yank (&optional arg
)
22833 "Yank. If the kill is a subtree, treat it specially.
22834 This command will look at the current kill and check if is a single
22835 subtree, or a series of subtrees[1]. If it passes the test, and if the
22836 cursor is at the beginning of a line or after the stars of a currently
22837 empty headline, then the yank is handled specially. How exactly depends
22838 on the value of the following variables.
22840 `org-yank-folded-subtrees'
22841 By default, this variable is non-nil, which results in
22842 subtree(s) being folded after insertion, except if doing so
22843 would swallow text after the yanked text.
22845 `org-yank-adjusted-subtrees'
22846 When non-nil (the default value is nil), the subtree will be
22847 promoted or demoted in order to fit into the local outline tree
22848 structure, which means that the level will be adjusted so that it
22849 becomes the smaller one of the two *visible* surrounding headings.
22851 Any prefix to this command will cause `yank' to be called directly with
22852 no special treatment. In particular, a simple `\\[universal-argument]' prefix \
22854 plainly yank the text as it is.
22856 \[1] The test checks if the first non-white line is a heading
22857 and if there are no other headings with fewer stars."
22859 (org-yank-generic 'yank arg
))
22861 (defun org-yank-generic (command arg
)
22862 "Perform some yank-like command.
22864 This function implements the behavior described in the `org-yank'
22865 documentation. However, it has been generalized to work for any
22866 interactive command with similar behavior."
22868 ;; pretend to be command COMMAND
22869 (setq this-command command
)
22872 (call-interactively command
)
22874 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
22875 (and (org-kill-is-subtree-p)
22877 (and (looking-at "[ \t]*$")
22880 (buffer-substring (point-at-bol) (point)))))))
22883 ((and subtreep org-yank-folded-subtrees
)
22884 (let ((beg (point))
22886 (if (and subtreep org-yank-adjusted-subtrees
)
22887 (org-paste-subtree nil nil
'for-yank
)
22888 (call-interactively command
))
22892 (when (and (bolp) subtreep
22893 (not (setq swallowp
22894 (org-yank-folding-would-swallow-text beg end
))))
22895 (org-with-limited-levels
22896 (or (looking-at org-outline-regexp
)
22897 (re-search-forward org-outline-regexp-bol end t
))
22898 (while (and (< (point) end
) (looking-at org-outline-regexp
))
22899 (outline-hide-subtree)
22900 (org-cycle-show-empty-lines 'folded
)
22901 (condition-case nil
22902 (outline-forward-same-level 1)
22903 (error (goto-char end
))))))
22906 "Inserted text not folded because that would swallow text"))
22909 (skip-chars-forward " \t\n\r")
22910 (beginning-of-line 1)
22911 (push-mark beg
'nomsg
)))
22912 ((and subtreep org-yank-adjusted-subtrees
)
22913 (let ((beg (point-at-bol)))
22914 (org-paste-subtree nil nil
'for-yank
)
22915 (push-mark beg
'nomsg
)))
22917 (call-interactively command
))))))
22919 (defun org-yank-folding-would-swallow-text (beg end
)
22920 "Would hide-subtree at BEG swallow any text after END?"
22922 (org-with-limited-levels
22925 (when (or (looking-at org-outline-regexp
)
22926 (re-search-forward org-outline-regexp-bol end t
))
22927 (setq level
(org-outline-level)))
22929 (skip-chars-forward " \t\r\n\v\f")
22931 (and (bolp) (looking-at-p org-outline-regexp
)
22932 (<= (org-outline-level) level
))))))))
22934 (defun org-back-to-heading (&optional invisible-ok
)
22935 "Call `outline-back-to-heading', but provide a better error message."
22936 (condition-case nil
22937 (outline-back-to-heading invisible-ok
)
22938 (error (error "Before first headline at position %d in buffer %s"
22939 (point) (current-buffer)))))
22941 (defun org-before-first-heading-p ()
22942 "Before first heading?"
22945 (null (re-search-backward org-outline-regexp-bol nil t
))))
22947 (defun org-at-heading-p (&optional ignored
)
22948 (outline-on-heading-p t
))
22950 (defun org-in-commented-heading-p (&optional no-inheritance
)
22951 "Non-nil if point is under a commented heading.
22952 This function also checks ancestors of the current headline,
22953 unless optional argument NO-INHERITANCE is non-nil."
22955 ((org-before-first-heading-p) nil
)
22956 ((let ((headline (nth 4 (org-heading-components))))
22958 (let ((case-fold-search nil
))
22959 (string-match-p (concat "^" org-comment-string
"\\(?: \\|$\\)")
22961 (no-inheritance nil
)
22963 (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
22965 (defun org-at-comment-p nil
22966 "Is cursor in a commented line?"
22969 (beginning-of-line)
22970 (looking-at "^[ \t]*# "))))
22972 (defun org-at-drawer-p nil
22973 "Is cursor at a drawer keyword?"
22975 (move-beginning-of-line 1)
22976 (looking-at org-drawer-regexp
)))
22978 (defun org-at-block-p nil
22979 "Is cursor at a block keyword?"
22981 (move-beginning-of-line 1)
22982 (looking-at org-block-regexp
)))
22984 (defun org-point-at-end-of-empty-headline ()
22985 "If point is at the end of an empty headline, return t, else nil.
22986 If the heading only contains a TODO keyword, it is still still considered
22988 (let ((case-fold-search nil
))
22989 (and (looking-at "[ \t]*$")
22990 org-todo-line-regexp
22992 (beginning-of-line)
22993 (looking-at org-todo-line-regexp
)
22994 (string= (match-string 3) "")))))
22996 (defun org-at-heading-or-item-p ()
22997 (or (org-at-heading-p) (org-at-item-p)))
22999 (defun org-at-target-p ()
23000 (or (org-in-regexp org-radio-target-regexp
)
23001 (org-in-regexp org-target-regexp
)))
23002 ;; Compatibility alias with Org versions < 7.8.03
23003 (defalias 'org-on-target-p
'org-at-target-p
)
23005 (defun org-up-heading-all (arg)
23006 "Move to the heading line of which the present line is a subheading.
23007 This function considers both visible and invisible heading lines.
23008 With argument, move up ARG levels."
23009 (outline-up-heading arg t
))
23011 (defun org-up-heading-safe ()
23012 "Move to the heading line of which the present line is a subheading.
23013 This version will not throw an error. It will return the level of the
23014 headline found, or nil if no higher level is found.
23016 Also, this function will be a lot faster than `outline-up-heading',
23017 because it relies on stars being the outline starters. This can really
23018 make a significant difference in outlines with very many siblings."
23019 (when (ignore-errors (org-back-to-heading t
))
23020 (let ((level-up (1- (funcall outline-level
))))
23021 (and (> level-up
0)
23022 (re-search-backward (format "^\\*\\{1,%d\\} " level-up
) nil t
)
23023 (funcall outline-level
)))))
23025 (defun org-first-sibling-p ()
23026 "Is this heading the first child of its parents?"
23028 (let ((re org-outline-regexp-bol
)
23030 (unless (org-at-heading-p t
)
23031 (user-error "Not at a heading"))
23032 (setq level
(funcall outline-level
))
23034 (if (not (re-search-backward re nil t
))
23036 (setq l
(funcall outline-level
))
23039 (defun org-goto-sibling (&optional previous
)
23040 "Goto the next sibling, even if it is invisible.
23041 When PREVIOUS is set, go to the previous sibling instead. Returns t
23042 when a sibling was found. When none is found, return nil and don't
23044 (let ((fun (if previous
're-search-backward
're-search-forward
))
23046 (re org-outline-regexp-bol
)
23048 (when (ignore-errors (org-back-to-heading t
))
23049 (setq level
(funcall outline-level
))
23051 (or previous
(forward-char 1))
23052 (while (funcall fun re nil t
)
23053 (setq l
(funcall outline-level
))
23054 (when (< l level
) (goto-char pos
) (throw 'exit nil
))
23055 (when (= l level
) (goto-char (match-beginning 0)) (throw 'exit t
)))
23059 (defun org-show-siblings ()
23060 "Show all siblings of the current headline."
23062 (while (org-goto-sibling) (org-flag-heading nil
)))
23064 (while (org-goto-sibling 'previous
)
23065 (org-flag-heading nil
))))
23067 (defun org-goto-first-child ()
23068 "Goto the first child, even if it is invisible.
23069 Return t when a child was found. Otherwise don't move point and
23071 (let (level (pos (point)) (re org-outline-regexp-bol
))
23072 (when (ignore-errors (org-back-to-heading t
))
23073 (setq level
(outline-level))
23075 (if (and (re-search-forward re nil t
) (> (outline-level) level
))
23076 (progn (goto-char (match-beginning 0)) t
)
23077 (goto-char pos
) nil
))))
23079 (defun org-show-hidden-entry ()
23080 "Show an entry where even the heading is hidden."
23084 (defun org-flag-heading (flag &optional entry
)
23085 "Flag the current heading. FLAG non-nil means make invisible.
23086 When ENTRY is non-nil, show the entire entry."
23088 (org-back-to-heading t
)
23089 ;; Check if we should show the entire entry
23092 (line-end-position 0) (line-end-position) flag
'outline
)
23095 (and (outline-next-heading)
23096 (org-flag-heading nil
))))))
23098 (defun org-get-next-sibling ()
23099 "Move to next heading of the same level, and return point.
23100 If there is no such heading, return nil.
23101 This is like outline-next-sibling, but invisible headings are ok."
23102 (let ((level (funcall outline-level
)))
23103 (outline-next-heading)
23104 (while (and (not (eobp)) (> (funcall outline-level
) level
))
23105 (outline-next-heading))
23106 (unless (or (eobp) (< (funcall outline-level
) level
))
23109 (defun org-get-last-sibling ()
23110 "Move to previous heading of the same level, and return point.
23111 If there is no such heading, return nil."
23112 (let ((opoint (point))
23113 (level (funcall outline-level
)))
23114 (outline-previous-heading)
23115 (when (and (/= (point) opoint
) (outline-on-heading-p t
))
23116 (while (and (> (funcall outline-level
) level
)
23118 (outline-previous-heading))
23119 (unless (< (funcall outline-level
) level
)
23122 (defun org-end-of-subtree (&optional invisible-ok to-heading
)
23123 "Goto to the end of a subtree."
23124 ;; This contains an exact copy of the original function, but it uses
23125 ;; `org-back-to-heading', to make it work also in invisible
23126 ;; trees. And is uses an invisible-ok argument.
23127 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
23128 ;; Furthermore, when used inside Org, finding the end of a large subtree
23129 ;; with many children and grandchildren etc, this can be much faster
23130 ;; than the outline version.
23131 (org-back-to-heading invisible-ok
)
23133 (level (funcall outline-level
)))
23134 (if (and (derived-mode-p 'org-mode
) (< level
1000))
23135 ;; A true heading (not a plain list item), in Org
23136 ;; This means we can easily find the end by looking
23137 ;; only for the right number of stars. Using a regexp to do
23138 ;; this is so much faster than using a Lisp loop.
23139 (let ((re (concat "^\\*\\{1," (int-to-string level
) "\\} ")))
23141 (and (re-search-forward re nil
'move
) (beginning-of-line 1)))
23142 ;; something else, do it the slow way
23143 (while (and (not (eobp))
23144 (or first
(> (funcall outline-level
) level
)))
23146 (outline-next-heading)))
23148 (when (memq (preceding-char) '(?
\n ?\^M
))
23149 ;; Go to end of line before heading
23151 (when (memq (preceding-char) '(?
\n ?\^M
))
23152 ;; leave blank line before heading
23153 (forward-char -
1)))))
23156 (defun org-end-of-meta-data (&optional full
)
23157 "Skip planning line and properties drawer in current entry.
23158 When optional argument FULL is non-nil, also skip empty lines,
23159 clocking lines and regular drawers at the beginning of the
23161 (org-back-to-heading t
)
23163 (when (looking-at-p org-planning-line-re
) (forward-line))
23164 (when (looking-at org-property-drawer-re
)
23165 (goto-char (match-end 0))
23167 (when (and full
(not (org-at-heading-p)))
23169 (let ((end (save-excursion (outline-next-heading) (point)))
23170 (re (concat "[ \t]*$" "\\|" org-clock-line-re
)))
23171 (while (not (eobp))
23172 (cond ((looking-at-p org-drawer-regexp
)
23173 (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t
)
23176 ((looking-at-p re
) (forward-line))
23177 (t (throw 'exit t
))))))))
23179 (defun org-forward-heading-same-level (arg &optional invisible-ok
)
23180 "Move forward to the ARG'th subheading at same level as this one.
23181 Stop at the first and last subheadings of a superior heading.
23182 Normally this only looks at visible headings, but when INVISIBLE-OK is
23183 non-nil it will also look at invisible ones."
23185 (let ((backward?
(and arg
(< arg
0))))
23186 (if (org-before-first-heading-p)
23187 (if backward?
(goto-char (point-min)) (outline-next-heading))
23188 (org-back-to-heading invisible-ok
)
23189 (unless backward?
(end-of-line)) ;do not match current headline
23190 (let ((level (- (match-end 0) (match-beginning 0) 1))
23191 (f (if backward?
#'re-search-backward
#'re-search-forward
))
23192 (count (if arg
(abs arg
) 1))
23194 (while (and (> count
0)
23195 (funcall f org-outline-regexp-bol nil
'move
))
23196 (let ((l (- (match-end 0) (match-beginning 0) 1)))
23197 (cond ((< l level
) (setq count
0))
23200 (not (org-invisible-p
23201 (line-beginning-position)))))
23203 (when (= l level
) (setq result
(point)))))))
23204 (goto-char result
))
23205 (beginning-of-line))))
23207 (defun org-backward-heading-same-level (arg &optional invisible-ok
)
23208 "Move backward to the ARG'th subheading at same level as this one.
23209 Stop at the first and last subheadings of a superior heading."
23211 (org-forward-heading-same-level (if arg
(- arg
) -
1) invisible-ok
))
23213 (defun org-next-visible-heading (arg)
23214 "Move to the next visible heading.
23216 This function wraps `outline-next-visible-heading' with
23217 `org-with-limited-levels' in order to skip over inline tasks and
23218 respect customization of `org-odd-levels-only'."
23220 (org-with-limited-levels
23221 (outline-next-visible-heading arg
)))
23223 (defun org-previous-visible-heading (arg)
23224 "Move to the previous visible heading.
23226 This function wraps `outline-previous-visible-heading' with
23227 `org-with-limited-levels' in order to skip over inline tasks and
23228 respect customization of `org-odd-levels-only'."
23230 (org-with-limited-levels
23231 (outline-previous-visible-heading arg
)))
23233 (defun org-forward-paragraph ()
23234 "Move forward to beginning of next paragraph or equivalent.
23236 The function moves point to the beginning of the next visible
23237 structural element, which can be a paragraph, a table, a list
23238 item, etc. It also provides some special moves for convenience:
23240 - On an affiliated keyword, jump to the beginning of the
23242 - On an item or a footnote definition, move to the second
23243 element inside, if any.
23244 - On a table or a property drawer, jump after it.
23245 - On a verse or source block, stop after blank lines."
23248 (let* ((deactivate-mark nil
)
23249 (element (org-element-at-point))
23250 (type (org-element-type element
))
23251 (post-affiliated (org-element-property :post-affiliated element
))
23252 (contents-begin (org-element-property :contents-begin element
))
23253 (contents-end (org-element-property :contents-end element
))
23254 (end (let ((end (org-element-property :end element
)) (parent element
))
23255 (while (and (setq parent
(org-element-property :parent parent
))
23256 (= (org-element-property :contents-end parent
) end
))
23257 (setq end
(org-element-property :end parent
)))
23259 (cond ((not element
)
23260 (skip-chars-forward " \r\t\n")
23261 (or (eobp) (beginning-of-line)))
23262 ;; On affiliated keywords, move to element's beginning.
23263 ((< (point) post-affiliated
)
23264 (goto-char post-affiliated
))
23265 ;; At a table row, move to the end of the table. Similarly,
23266 ;; at a node property, move to the end of the property
23268 ((memq type
'(node-property table-row
))
23269 (goto-char (org-element-property
23270 :end
(org-element-property :parent element
))))
23271 ((memq type
'(property-drawer table
)) (goto-char end
))
23272 ;; Consider blank lines as separators in verse and source
23273 ;; blocks to ease editing.
23274 ((memq type
'(src-block verse-block
))
23275 (when (eq type
'src-block
)
23277 (save-excursion (goto-char end
)
23278 (skip-chars-backward " \r\t\n")
23279 (line-beginning-position))))
23280 (beginning-of-line)
23281 (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
23282 (if (not (re-search-forward "^[ \t]*$" contents-end t
))
23284 (skip-chars-forward " \r\t\n")
23285 (if (= (point) contents-end
) (goto-char end
)
23286 (beginning-of-line))))
23287 ;; With no contents, just skip element.
23288 ((not contents-begin
) (goto-char end
))
23289 ;; If contents are invisible, skip the element altogether.
23290 ((org-invisible-p (line-end-position))
23293 (org-with-limited-levels (outline-next-visible-heading 1)))
23294 ;; At a plain list, make sure we move to the next item
23295 ;; instead of skipping the whole list.
23296 (plain-list (forward-char)
23297 (org-forward-paragraph))
23298 (otherwise (goto-char end
))))
23299 ((>= (point) contents-end
) (goto-char end
))
23300 ((>= (point) contents-begin
)
23301 ;; This can only happen on paragraphs and plain lists.
23303 (paragraph (goto-char end
))
23304 ;; At a plain list, try to move to second element in
23305 ;; first item, if possible.
23306 (plain-list (end-of-line)
23307 (org-forward-paragraph))))
23308 ;; When contents start on the middle of a line (e.g. in
23309 ;; items and footnote definitions), try to reach first
23310 ;; element starting after current line.
23311 ((> (line-end-position) contents-begin
)
23313 (org-forward-paragraph))
23314 (t (goto-char contents-begin
))))))
23316 (defun org-backward-paragraph ()
23317 "Move backward to start of previous paragraph or equivalent.
23319 The function moves point to the beginning of the current
23320 structural element, which can be a paragraph, a table, a list
23321 item, etc., or to the beginning of the previous visible one if
23322 point is already there. It also provides some special moves for
23325 - On an affiliated keyword, jump to the first one.
23326 - On a table or a property drawer, move to its beginning.
23327 - On comment, example, export, src and verse blocks, stop
23328 before blank lines."
23331 (let* ((deactivate-mark nil
)
23332 (element (org-element-at-point))
23333 (type (org-element-type element
))
23334 (contents-end (org-element-property :contents-end element
))
23335 (post-affiliated (org-element-property :post-affiliated element
))
23336 (begin (org-element-property :begin element
))
23337 (special?
;blocks handled specially
23338 (memq type
'(comment-block example-block export-block src-block
23342 ;; These types have no proper contents. Fake line
23343 ;; below the block opening line as contents beginning.
23344 (save-excursion (goto-char begin
) (line-beginning-position 2))
23345 (org-element-property :contents-begin element
))))
23347 ((not element
) (goto-char (point-min)))
23350 (org-backward-paragraph))
23351 ((<= (point) post-affiliated
) (goto-char begin
))
23352 ;; Special behavior: on a table or a property drawer, move to
23354 ((memq type
'(node-property table-row
))
23355 (goto-char (org-element-property
23356 :post-affiliated
(org-element-property :parent element
))))
23358 (if (<= (point) contents-begin
) (goto-char post-affiliated
)
23359 ;; Inside a verse block, see blank lines as paragraph
23361 (let ((origin (point)))
23362 (skip-chars-backward " \r\t\n" contents-begin
)
23363 (when (re-search-backward "^[ \t]*$" contents-begin
'move
)
23364 (skip-chars-forward " \r\t\n" origin
)
23365 (if (= (point) origin
) (goto-char contents-begin
)
23366 (beginning-of-line))))))
23367 ((eq type
'paragraph
) (goto-char contents-begin
)
23368 ;; When at first paragraph in an item or a footnote definition,
23369 ;; move directly to beginning of line.
23370 (let ((parent-contents
23371 (org-element-property
23372 :contents-begin
(org-element-property :parent element
))))
23373 (when (and parent-contents
(= parent-contents contents-begin
))
23374 (beginning-of-line))))
23375 ;; At the end of a greater element, move to the beginning of
23376 ;; the last element within.
23377 ((and contents-end
(>= (point) contents-end
))
23378 (goto-char (1- contents-end
))
23379 (org-backward-paragraph))
23380 (t (goto-char (or post-affiliated begin
))))
23381 ;; Ensure we never leave point invisible.
23382 (when (org-invisible-p (point)) (beginning-of-visual-line)))))
23384 (defun org-forward-element ()
23385 "Move forward by one element.
23386 Move to the next element at the same level, when possible."
23388 (cond ((eobp) (user-error "Cannot move further down"))
23389 ((org-with-limited-levels (org-at-heading-p))
23390 (let ((origin (point)))
23391 (goto-char (org-end-of-subtree nil t
))
23392 (unless (org-with-limited-levels (org-at-heading-p))
23394 (user-error "Cannot move further down"))))
23396 (let* ((elem (org-element-at-point))
23397 (end (org-element-property :end elem
))
23398 (parent (org-element-property :parent elem
)))
23399 (cond ((and parent
(= (org-element-property :contents-end parent
) end
))
23400 (goto-char (org-element-property :end parent
)))
23401 ((integer-or-marker-p end
) (goto-char end
))
23402 (t (message "No element at point")))))))
23404 (defun org-backward-element ()
23405 "Move backward by one element.
23406 Move to the previous element at the same level, when possible."
23408 (cond ((bobp) (user-error "Cannot move further up"))
23409 ((org-with-limited-levels (org-at-heading-p))
23410 ;; At a headline, move to the previous one, if any, or stay
23412 (let ((origin (point)))
23413 (org-with-limited-levels (org-backward-heading-same-level 1))
23414 ;; When current headline has no sibling above, move to its
23416 (when (= (point) origin
)
23417 (or (org-with-limited-levels (org-up-heading-safe))
23418 (progn (goto-char origin
)
23419 (user-error "Cannot move further up"))))))
23421 (let* ((elem (org-element-at-point))
23422 (beg (org-element-property :begin elem
)))
23424 ;; Move to beginning of current element if point isn't
23426 ((null beg
) (message "No element at point"))
23427 ((/= (point) beg
) (goto-char beg
))
23429 (skip-chars-backward " \r\t\n")
23431 (let ((prev (org-element-at-point)))
23432 (goto-char (org-element-property :begin prev
))
23433 (while (and (setq prev
(org-element-property :parent prev
))
23434 (<= (org-element-property :end prev
) beg
))
23435 (goto-char (org-element-property :begin prev
)))))))))))
23437 (defun org-up-element ()
23438 "Move to upper element."
23440 (if (org-with-limited-levels (org-at-heading-p))
23441 (unless (org-up-heading-safe) (user-error "No surrounding element"))
23442 (let* ((elem (org-element-at-point))
23443 (parent (org-element-property :parent elem
)))
23444 (if parent
(goto-char (org-element-property :begin parent
))
23445 (if (org-with-limited-levels (org-before-first-heading-p))
23446 (user-error "No surrounding element")
23447 (org-with-limited-levels (org-back-to-heading)))))))
23449 (defun org-down-element ()
23450 "Move to inner element."
23452 (let ((element (org-element-at-point)))
23454 ((memq (org-element-type element
) '(plain-list table
))
23455 (goto-char (org-element-property :contents-begin element
))
23457 ((memq (org-element-type element
) org-element-greater-elements
)
23458 ;; If contents are hidden, first disclose them.
23459 (when (org-invisible-p (line-end-position)) (org-cycle))
23460 (goto-char (or (org-element-property :contents-begin element
)
23461 (user-error "No content for this element"))))
23462 (t (user-error "No inner element")))))
23464 (defun org-drag-element-backward ()
23465 "Move backward element at point."
23467 (let ((elem (or (org-element-at-point)
23468 (user-error "No element at point"))))
23469 (if (eq (org-element-type elem
) 'headline
)
23470 ;; Preserve point when moving a whole tree, even if point was
23471 ;; on blank lines below the headline.
23472 (let ((offset (skip-chars-backward " \t\n")))
23473 (unwind-protect (org-move-subtree-up)
23474 (forward-char (- offset
))))
23477 (goto-char (org-element-property :begin elem
))
23478 (skip-chars-backward " \r\t\n")
23480 (let* ((beg (org-element-property :begin elem
))
23481 (prev (org-element-at-point))
23483 (while (and (setq up
(org-element-property :parent up
))
23484 (<= (org-element-property :end up
) beg
))
23487 ;; Error out if no previous element or previous element is
23488 ;; a parent of the current one.
23489 (if (or (not prev-elem
) (org-element-nested-p elem prev-elem
))
23490 (user-error "Cannot drag element backward")
23491 (let ((pos (point)))
23492 (org-element-swap-A-B prev-elem elem
)
23493 (goto-char (+ (org-element-property :begin prev-elem
)
23494 (- pos
(org-element-property :begin elem
))))))))))
23496 (defun org-drag-element-forward ()
23497 "Move forward element at point."
23499 (let* ((pos (point))
23500 (elem (or (org-element-at-point)
23501 (user-error "No element at point"))))
23502 (when (= (point-max) (org-element-property :end elem
))
23503 (user-error "Cannot drag element forward"))
23504 (goto-char (org-element-property :end elem
))
23505 (let ((next-elem (org-element-at-point)))
23506 (when (or (org-element-nested-p elem next-elem
)
23507 (and (eq (org-element-type next-elem
) 'headline
)
23508 (not (eq (org-element-type elem
) 'headline
))))
23510 (user-error "Cannot drag element forward"))
23511 ;; Compute new position of point: it's shifted by NEXT-ELEM
23512 ;; body's length (without final blanks) and by the length of
23513 ;; blanks between ELEM and NEXT-ELEM.
23514 (let ((size-next (- (save-excursion
23515 (goto-char (org-element-property :end next-elem
))
23516 (skip-chars-backward " \r\t\n")
23518 ;; Small correction if buffer doesn't end
23519 ;; with a newline character.
23520 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
23521 (org-element-property :begin next-elem
)))
23522 (size-blank (- (org-element-property :end elem
)
23524 (goto-char (org-element-property :end elem
))
23525 (skip-chars-backward " \r\t\n")
23528 (org-element-swap-A-B elem next-elem
)
23529 (goto-char (+ pos size-next size-blank
))))))
23531 (defun org-drag-line-forward (arg)
23532 "Drag the line at point ARG lines forward."
23534 (dotimes (_ (abs arg
))
23535 (let ((c (current-column)))
23538 (beginning-of-line 2)
23539 (transpose-lines 1)
23540 (beginning-of-line 0))
23541 (transpose-lines 1)
23542 (beginning-of-line -
1))
23543 (org-move-to-column c
))))
23545 (defun org-drag-line-backward (arg)
23546 "Drag the line at point ARG lines backward."
23548 (org-drag-line-forward (- arg
)))
23550 (defun org-mark-element ()
23551 "Put point at beginning of this element, mark at end.
23553 Interactively, if this command is repeated or (in Transient Mark
23554 mode) if the mark is active, it marks the next element after the
23555 ones already marked."
23557 (let (deactivate-mark)
23558 (if (and (called-interactively-p 'any
)
23559 (or (and (eq last-command this-command
) (mark t
))
23560 (and transient-mark-mode mark-active
)))
23564 (goto-char (org-element-property :end
(org-element-at-point)))))
23565 (let ((element (org-element-at-point)))
23567 (push-mark (org-element-property :end element
) t t
)
23568 (goto-char (org-element-property :begin element
))))))
23570 (defun org-narrow-to-element ()
23571 "Narrow buffer to current element."
23573 (let ((elem (org-element-at-point)))
23575 ((eq (car elem
) 'headline
)
23577 (org-element-property :begin elem
)
23578 (org-element-property :end elem
)))
23579 ((memq (car elem
) org-element-greater-elements
)
23581 (org-element-property :contents-begin elem
)
23582 (org-element-property :contents-end elem
)))
23585 (org-element-property :begin elem
)
23586 (org-element-property :end elem
))))))
23588 (defun org-transpose-element ()
23589 "Transpose current and previous elements, keeping blank lines between.
23590 Point is moved after both elements."
23592 (org-skip-whitespace)
23593 (let ((end (org-element-property :end
(org-element-at-point))))
23594 (org-drag-element-backward)
23597 (defun org-unindent-buffer ()
23598 "Un-indent the visible part of the buffer.
23599 Relative indentation (between items, inside blocks, etc.) isn't
23602 (unless (eq major-mode
'org-mode
)
23603 (user-error "Cannot un-indent a buffer not in Org mode"))
23604 (letrec ((parse-tree (org-element-parse-buffer 'greater-element
))
23607 (dolist (element (reverse contents
))
23608 (if (memq (org-element-type element
) '(headline section
))
23609 (funcall unindent-tree
(org-element-contents element
))
23613 (org-element-property :begin element
)
23614 (org-element-property :end element
))
23615 (org-do-remove-indentation))))))))
23616 (funcall unindent-tree
(org-element-contents parse-tree
))))
23618 (defun org-make-options-regexp (kwds &optional extra
)
23619 "Make a regular expression for keyword lines.
23620 KWDS is a list of keywords, as strings. Optional argument EXTRA,
23621 when non-nil, is a regexp matching keywords names."
23622 (concat "^[ \t]*#\\+\\("
23624 (and extra
(concat (and kwds
"\\|") extra
))
23625 "\\):[ \t]*\\(.*\\)"))
23630 (add-hook 'org-mode-hook
;remove overlays when changing major mode
23631 (lambda () (add-hook 'change-major-mode-hook
23632 'org-show-all
'append
'local
)))
23636 (run-hooks 'org-load-hook
)
23638 ;;; org.el ends here