Fix multiple tag selection
[org-mode/org-tableheadings.git] / lisp / org.el
blob600693ba35eb51c72c0ec851e0f16625036b0efb
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.
5 ;;
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
10 ;; Version: 9.1.11
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/>.
27 ;;; Commentary:
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
46 ;; webpages.
48 ;; Installation and Activation
49 ;; ---------------------------
50 ;; See the corresponding sections in the manual at
52 ;; https://orgmode.org/org.html#Installation
54 ;; Documentation
55 ;; -------------
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
65 ;;; Code:
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'.")
71 ;;;; Require other packages
73 (require 'cl-lib)
75 (eval-when-compile (require 'gnus-sum))
77 (require 'calendar)
78 (require 'find-func)
79 (require 'format-spec)
81 (or (eq this-command 'eval-buffer)
82 (condition-case nil
83 (load (concat (file-name-directory load-file-name)
84 "org-loaddefs.el")
85 nil t t t)
86 (error
87 (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
88 (sit-for 3)
89 (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
90 (sit-for 3))))
92 (require 'org-macs)
93 (require 'org-compat)
95 ;; `org-outline-regexp' ought to be a defconst but is let-bound in
96 ;; some places -- e.g. see the macro `org-with-limited-levels'.
98 ;; In Org buffers, the value of `outline-regexp' is that of
99 ;; `org-outline-regexp'. The only function still directly relying on
100 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
101 ;; job when `orgstruct-mode' is active.
102 (defvar org-outline-regexp "\\*+ "
103 "Regexp to match Org headlines.")
105 (defvar org-outline-regexp-bol "^\\*+ "
106 "Regexp to match Org headlines.
107 This is similar to `org-outline-regexp' but additionally makes
108 sure that we are at the beginning of the line.")
110 (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
111 "Matches a headline, putting stars and text into groups.
112 Stars are put in group 1 and the trimmed body in group 2.")
114 (declare-function calendar-check-holidays "holidays" (date))
115 (declare-function cdlatex-environment "ext:cdlatex" (environment item))
116 (declare-function isearch-no-upper-case-p "isearch" (string regexp-flag))
117 (declare-function org-add-archive-files "org-archive" (files))
118 (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
119 (declare-function org-agenda-list "org-agenda" (&optional arg start-day span with-hour))
120 (declare-function org-agenda-redo "org-agenda" (&optional all))
121 (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body) t)
122 (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
123 (declare-function org-beamer-mode "ox-beamer" (&optional prefix) t)
124 (declare-function org-clock-get-last-clock-out-time "org-clock" ())
125 (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
126 (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
127 (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
128 (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
129 (declare-function org-clock-timestamps-down "org-clock" (&optional n))
130 (declare-function org-clock-timestamps-up "org-clock" (&optional n))
131 (declare-function org-clock-update-time-maybe "org-clock" ())
132 (declare-function org-clocking-buffer "org-clock" ())
133 (declare-function org-clocktable-shift "org-clock" (dir n))
134 (declare-function
135 org-duration-from-minutes "org-duration" (minutes &optional fmt canonical))
136 (declare-function org-element-at-point "org-element" ())
137 (declare-function org-element-cache-refresh "org-element" (pos))
138 (declare-function org-element-cache-reset "org-element" (&optional all))
139 (declare-function org-element-contents "org-element" (element))
140 (declare-function org-element-context "org-element" (&optional element))
141 (declare-function org-element-copy "org-element" (datum))
142 (declare-function org-element-interpret-data "org-element" (data))
143 (declare-function org-element-lineage "org-element" (blob &optional types with-self))
144 (declare-function org-element-link-parser "org-element" ())
145 (declare-function org-element-nested-p "org-element" (elem-a elem-b))
146 (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
147 (declare-function org-element-property "org-element" (property element))
148 (declare-function org-element-put-property "org-element" (element property value))
149 (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
150 (declare-function org-element-type "org-element" (element))
151 (declare-function org-element-update-syntax "org-element" ())
152 (declare-function org-id-find-id-file "org-id" (id))
153 (declare-function org-id-get-create "org-id" (&optional force))
154 (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
155 (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
156 (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
157 (declare-function org-plot/gnuplot "org-plot" (&optional params))
158 (declare-function org-table-align "org-table" ())
159 (declare-function org-table-begin "org-table" (&optional table-type))
160 (declare-function org-table-beginning-of-field "org-table" (&optional n))
161 (declare-function org-table-blank-field "org-table" ())
162 (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
163 (declare-function org-table-copy-region "org-table" (beg end &optional cut))
164 (declare-function org-table-cut-region "org-table" (beg end))
165 (declare-function org-table-edit-field "org-table" (arg))
166 (declare-function org-table-end "org-table" (&optional table-type))
167 (declare-function org-table-end-of-field "org-table" (&optional n))
168 (declare-function org-table-insert-row "org-table" (&optional arg))
169 (declare-function org-table-justify-field-maybe "org-table" (&optional new))
170 (declare-function org-table-maybe-eval-formula "org-table" ())
171 (declare-function org-table-maybe-recalculate-line "org-table" ())
172 (declare-function org-table-next-row "org-table" ())
173 (declare-function org-table-paste-rectangle "org-table" ())
174 (declare-function org-table-recalculate "org-table" (&optional all noalign))
175 (declare-function
176 org-table-sort-lines "org-table"
177 (&optional with-case sorting-type getkey-func compare-func interactive?))
178 (declare-function org-table-wrap-region "org-table" (arg))
179 (declare-function org-tags-view "org-agenda" (&optional todo-only match))
180 (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
181 (declare-function orgtbl-mode "org-table" (&optional arg))
182 (declare-function org-export-get-backend "ox" (name))
183 (declare-function org-export-get-environment "ox" (&optional backend subtreep ext-plist))
184 (declare-function org-latex-make-preamble "ox-latex" (info &optional template snippet?))
186 (defvar ffap-url-regexp)
187 (defvar org-element-paragraph-separate)
189 (defsubst org-uniquify (list)
190 "Non-destructively remove duplicate elements from LIST."
191 (let ((res (copy-sequence list))) (delete-dups res)))
193 (defsubst org-get-at-bol (property)
194 "Get text property PROPERTY at the beginning of line."
195 (get-text-property (point-at-bol) property))
197 (defsubst org-trim (s &optional keep-lead)
198 "Remove whitespace at the beginning and the end of string S.
199 When optional argument KEEP-LEAD is non-nil, removing blank lines
200 at the beginning of the string does not affect leading indentation."
201 (replace-regexp-in-string
202 (if keep-lead "\\`\\([ \t]*\n\\)+" "\\`[ \t\n\r]+") ""
203 (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
205 ;; load languages based on value of `org-babel-load-languages'
206 (defvar org-babel-load-languages)
208 ;;;###autoload
209 (defun org-babel-do-load-languages (sym value)
210 "Load the languages defined in `org-babel-load-languages'."
211 (set-default sym value)
212 (dolist (pair org-babel-load-languages)
213 (let ((active (cdr pair)) (lang (symbol-name (car pair))))
214 (if active
215 (require (intern (concat "ob-" lang)))
216 (funcall 'fmakunbound
217 (intern (concat "org-babel-execute:" lang)))
218 (funcall 'fmakunbound
219 (intern (concat "org-babel-expand-body:" lang)))))))
221 (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
222 ;;;###autoload
223 (defun org-babel-load-file (file &optional compile)
224 "Load Emacs Lisp source code blocks in the Org FILE.
225 This function exports the source code using `org-babel-tangle'
226 and then loads the resulting file using `load-file'. With prefix
227 arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
228 file to byte-code before it is loaded."
229 (interactive "fFile to load: \nP")
230 (let* ((age (lambda (file)
231 (float-time
232 (time-subtract (current-time)
233 (nth 5 (or (file-attributes (file-truename file))
234 (file-attributes file)))))))
235 (base-name (file-name-sans-extension file))
236 (exported-file (concat base-name ".el")))
237 ;; tangle if the Org file is newer than the elisp file
238 (unless (and (file-exists-p exported-file)
239 (> (funcall age file) (funcall age exported-file)))
240 ;; Tangle-file traversal returns reversed list of tangled files
241 ;; and we want to evaluate the first target.
242 (setq exported-file
243 (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
244 (message "%s %s"
245 (if compile
246 (progn (byte-compile-file exported-file 'load)
247 "Compiled and loaded")
248 (progn (load-file exported-file) "Loaded"))
249 exported-file)))
251 (defcustom org-babel-load-languages '((emacs-lisp . t))
252 "Languages which can be evaluated in Org buffers.
253 This list can be used to load support for any of the languages
254 below, note that each language will depend on a different set of
255 system executables and/or Emacs modes. When a language is
256 \"loaded\", then code blocks in that language can be evaluated
257 with `org-babel-execute-src-block' bound by default to C-c
258 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
259 be set to remove code block evaluation from the C-c C-c
260 keybinding. By default only Emacs Lisp (which has no
261 requirements) is loaded."
262 :group 'org-babel
263 :set 'org-babel-do-load-languages
264 :version "24.1"
265 :type '(alist :tag "Babel Languages"
266 :key-type
267 (choice
268 (const :tag "Awk" awk)
269 (const :tag "C" C)
270 (const :tag "R" R)
271 (const :tag "Asymptote" asymptote)
272 (const :tag "Calc" calc)
273 (const :tag "Clojure" clojure)
274 (const :tag "CSS" css)
275 (const :tag "Ditaa" ditaa)
276 (const :tag "Dot" dot)
277 (const :tag "Ebnf2ps" ebnf2ps)
278 (const :tag "Emacs Lisp" emacs-lisp)
279 (const :tag "Forth" forth)
280 (const :tag "Fortran" fortran)
281 (const :tag "Gnuplot" gnuplot)
282 (const :tag "Haskell" haskell)
283 (const :tag "hledger" hledger)
284 (const :tag "IO" io)
285 (const :tag "J" J)
286 (const :tag "Java" java)
287 (const :tag "Javascript" js)
288 (const :tag "LaTeX" latex)
289 (const :tag "Ledger" ledger)
290 (const :tag "Lilypond" lilypond)
291 (const :tag "Lisp" lisp)
292 (const :tag "Makefile" makefile)
293 (const :tag "Maxima" maxima)
294 (const :tag "Matlab" matlab)
295 (const :tag "Mscgen" mscgen)
296 (const :tag "Ocaml" ocaml)
297 (const :tag "Octave" octave)
298 (const :tag "Org" org)
299 (const :tag "Perl" perl)
300 (const :tag "Pico Lisp" picolisp)
301 (const :tag "PlantUML" plantuml)
302 (const :tag "Python" python)
303 (const :tag "Ruby" ruby)
304 (const :tag "Sass" sass)
305 (const :tag "Scala" scala)
306 (const :tag "Scheme" scheme)
307 (const :tag "Screen" screen)
308 (const :tag "Shell Script" shell)
309 (const :tag "Shen" shen)
310 (const :tag "Sql" sql)
311 (const :tag "Sqlite" sqlite)
312 (const :tag "Stan" stan)
313 (const :tag "Vala" vala))
314 :value-type (boolean :tag "Activate" :value t)))
316 ;;;; Customization variables
317 (defcustom org-clone-delete-id nil
318 "Remove ID property of clones of a subtree.
319 When non-nil, clones of a subtree don't inherit the ID property.
320 Otherwise they inherit the ID property with a new unique
321 identifier."
322 :type 'boolean
323 :version "24.1"
324 :group 'org-id)
326 ;;; Version
327 (org-check-version)
329 ;;;###autoload
330 (defun org-version (&optional here full message)
331 "Show the Org version.
332 Interactively, or when MESSAGE is non-nil, show it in echo area.
333 With prefix argument, or when HERE is non-nil, insert it at point.
334 In non-interactive uses, a reduced version string is output unless
335 FULL is given."
336 (interactive (list current-prefix-arg t (not current-prefix-arg)))
337 (let ((org-dir (ignore-errors (org-find-library-dir "org")))
338 (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
339 (load-suffixes (list ".el"))
340 (org-install-dir
341 (ignore-errors (org-find-library-dir "org-loaddefs"))))
342 (unless (and (fboundp 'org-release) (fboundp 'org-git-version))
343 (org-load-noerror-mustsuffix (concat org-dir "org-version")))
344 (let* ((load-suffixes save-load-suffixes)
345 (release (org-release))
346 (git-version (org-git-version))
347 (version (format "Org mode version %s (%s @ %s)"
348 release
349 git-version
350 (if org-install-dir
351 (if (string= org-dir org-install-dir)
352 org-install-dir
353 (concat "mixed installation! "
354 org-install-dir
355 " and "
356 org-dir))
357 "org-loaddefs.el can not be found!")))
358 (version1 (if full version release)))
359 (when here (insert version1))
360 (when message (message "%s" version1))
361 version1)))
363 (defconst org-version (org-version))
366 ;;; Syntax Constants
368 ;;;; Block
370 (defconst org-block-regexp
371 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
372 "Regular expression for hiding blocks.")
374 (defconst org-dblock-start-re
375 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
376 "Matches the start line of a dynamic block, with parameters.")
378 (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
379 "Matches the end of a dynamic block.")
381 ;;;; Clock and Planning
383 (defconst org-clock-string "CLOCK:"
384 "String used as prefix for timestamps clocking work hours on an item.")
386 (defvar org-closed-string "CLOSED:"
387 "String used as the prefix for timestamps logging closing a TODO entry.")
389 (defvar org-deadline-string "DEADLINE:"
390 "String to mark deadline entries.
391 \\<org-mode-map>
392 A deadline is this string, followed by a time stamp. It must be
393 a word, terminated by a colon. You can insert a schedule keyword
394 and a timestamp with `\\[org-deadline]'.")
396 (defvar org-scheduled-string "SCHEDULED:"
397 "String to mark scheduled TODO entries.
398 \\<org-mode-map>
399 A schedule is this string, followed by a time stamp. It must be
400 a word, terminated by a colon. You can insert a schedule keyword
401 and a timestamp with `\\[org-schedule]'.")
403 (defconst org-ds-keyword-length
404 (+ 2
405 (apply #'max
406 (mapcar #'length
407 (list org-deadline-string org-scheduled-string
408 org-clock-string org-closed-string))))
409 "Maximum length of the DEADLINE and SCHEDULED keywords.")
411 (defconst org-planning-line-re
412 (concat "^[ \t]*"
413 (regexp-opt
414 (list org-closed-string org-deadline-string org-scheduled-string)
416 "Matches a line with planning info.
417 Matched keyword is in group 1.")
419 (defconst org-clock-line-re
420 (concat "^[ \t]*" org-clock-string)
421 "Matches a line with clock info.")
423 (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
424 "Matches the DEADLINE keyword.")
426 (defconst org-deadline-time-regexp
427 (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
428 "Matches the DEADLINE keyword together with a time stamp.")
430 (defconst org-deadline-time-hour-regexp
431 (concat "\\<" org-deadline-string
432 " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
433 "Matches the DEADLINE keyword together with a time-and-hour stamp.")
435 (defconst org-deadline-line-regexp
436 (concat "\\<\\(" org-deadline-string "\\).*")
437 "Matches the DEADLINE keyword and the rest of the line.")
439 (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
440 "Matches the SCHEDULED keyword.")
442 (defconst org-scheduled-time-regexp
443 (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
444 "Matches the SCHEDULED keyword together with a time stamp.")
446 (defconst org-scheduled-time-hour-regexp
447 (concat "\\<" org-scheduled-string
448 " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
449 "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
451 (defconst org-closed-time-regexp
452 (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
453 "Matches the CLOSED keyword together with a time stamp.")
455 (defconst org-keyword-time-regexp
456 (concat "\\<"
457 (regexp-opt
458 (list org-scheduled-string org-deadline-string org-closed-string
459 org-clock-string)
461 " *[[<]\\([^]>]+\\)[]>]")
462 "Matches any of the 4 keywords, together with the time stamp.")
464 (defconst org-keyword-time-not-clock-regexp
465 (concat
466 "\\<"
467 (regexp-opt
468 (list org-scheduled-string org-deadline-string org-closed-string) t)
469 " *[[<]\\([^]>]+\\)[]>]")
470 "Matches any of the 3 keywords, together with the time stamp.")
472 (defconst org-maybe-keyword-time-regexp
473 (concat "\\(\\<"
474 (regexp-opt
475 (list org-scheduled-string org-deadline-string org-closed-string
476 org-clock-string)
478 "\\)?"
479 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
480 "\\|"
481 "<%%([^\r\n>]*>\\)")
482 "Matches a timestamp, possibly preceded by a keyword.")
484 (defconst org-all-time-keywords
485 (mapcar (lambda (w) (substring w 0 -1))
486 (list org-scheduled-string org-deadline-string
487 org-clock-string org-closed-string))
488 "List of time keywords.")
490 ;;;; Drawer
492 (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
493 "Matches first or last line of a hidden block.
494 Group 1 contains drawer's name or \"END\".")
496 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
497 "Regular expression matching the first line of a property drawer.")
499 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
500 "Regular expression matching the last line of a property drawer.")
502 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
503 "Regular expression matching the first line of a clock drawer.")
505 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
506 "Regular expression matching the last line of a clock drawer.")
508 (defconst org-property-drawer-re
509 (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
510 "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
511 "[ \t]*:END:[ \t]*$")
512 "Matches an entire property drawer.")
514 (defconst org-clock-drawer-re
515 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
516 org-clock-drawer-end-re "\\)\n?")
517 "Matches an entire clock drawer.")
519 ;;;; Headline
521 (defconst org-heading-keyword-regexp-format
522 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
523 "Printf format for a regexp matching a headline with some keyword.
524 This regexp will match the headline of any node which has the
525 exact keyword that is put into the format. The keyword isn't in
526 any group by default, but the stars and the body are.")
528 (defconst org-heading-keyword-maybe-regexp-format
529 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
530 "Printf format for a regexp matching a headline, possibly with some keyword.
531 This regexp can match any headline with the specified keyword, or
532 without a keyword. The keyword isn't in any group by default,
533 but the stars and the body are.")
535 (defconst org-archive-tag "ARCHIVE"
536 "The tag that marks a subtree as archived.
537 An archived subtree does not open during visibility cycling, and does
538 not contribute to the agenda listings.")
540 (eval-and-compile
541 (defconst org-comment-string "COMMENT"
542 "Entries starting with this keyword will never be exported.
543 \\<org-mode-map>
544 An entry can be toggled between COMMENT and normal with
545 `\\[org-toggle-comment]'."))
548 ;;;; LaTeX Environments and Fragments
550 (defconst org-latex-regexps
551 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
552 ;; ("$" "\\([ \t(]\\|^\\)\\(\\(\\([$]\\)\\([^ \t\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \t\n,.$]\\)\\4\\)\\)\\([ \t.,?;:'\")]\\|$\\)" 2 nil)
553 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
554 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \t\r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|'\\|$\\)" 2 nil)
555 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \t\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \t\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|'\\|$\\)" 2 nil)
556 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
557 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
558 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
559 "Regular expressions for matching embedded LaTeX.")
561 ;;;; Node Property
563 (defconst org-effort-property "Effort"
564 "The property that is being used to keep track of effort estimates.
565 Effort estimates given in this property need to have the format H:MM.")
567 ;;;; Table
569 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
570 "Detect an org-type or table-type table.")
572 (defconst org-table-line-regexp "^[ \t]*|"
573 "Detect an org-type table line.")
575 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
576 "Detect an org-type table line.")
578 (defconst org-table-hline-regexp "^[ \t]*|-"
579 "Detect an org-type table hline.")
581 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
582 "Detect a table-type table hline.")
584 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
585 "Detect the first line outside a table when searching from within it.
586 This works for both table types.")
588 (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
589 "Detect a #+TBLFM line.")
591 ;;;; Timestamp
593 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
594 "Regular expression for fast time stamp matching.")
596 (defconst org-ts-regexp-inactive
597 "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
598 "Regular expression for fast inactive time stamp matching.")
600 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
601 "Regular expression for fast time stamp matching.")
603 (defconst org-ts-regexp0
604 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
605 "Regular expression matching time strings for analysis.
606 This one does not require the space after the date, so it can be used
607 on a string that terminates immediately after the date.")
609 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
610 "Regular expression matching time strings for analysis.")
612 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
613 "Regular expression matching time stamps, with groups.")
615 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
616 "Regular expression matching time stamps (also [..]), with groups.")
618 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
619 "Regular expression matching a time stamp range.")
621 (defconst org-tr-regexp-both
622 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
623 "Regular expression matching a time stamp range.")
625 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
626 org-ts-regexp "\\)?")
627 "Regular expression matching a time stamp or time stamp range.")
629 (defconst org-tsr-regexp-both
630 (concat org-ts-regexp-both "\\(--?-?"
631 org-ts-regexp-both "\\)?")
632 "Regular expression matching a time stamp or time stamp range.
633 The time stamps may be either active or inactive.")
635 (defconst org-repeat-re
636 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
637 "Regular expression for specifying repeated events.
638 After a match, group 1 contains the repeat expression.")
640 (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
641 "Formats for `format-time-string' which are used for time stamps.")
644 ;;; The custom variables
646 (defgroup org nil
647 "Outline-based notes management and organizer."
648 :tag "Org"
649 :group 'outlines
650 :group 'calendar)
652 (defcustom org-mode-hook nil
653 "Mode hook for Org mode, run after the mode was turned on."
654 :group 'org
655 :type 'hook)
657 (defcustom org-load-hook nil
658 "Hook that is run after org.el has been loaded."
659 :group 'org
660 :type 'hook)
662 (defcustom org-log-buffer-setup-hook nil
663 "Hook that is run after an Org log buffer is created."
664 :group 'org
665 :version "24.1"
666 :type 'hook)
668 (defvar org-modules) ; defined below
669 (defvar org-modules-loaded nil
670 "Have the modules been loaded already?")
672 (defun org-load-modules-maybe (&optional force)
673 "Load all extensions listed in `org-modules'."
674 (when (or force (not org-modules-loaded))
675 (dolist (ext org-modules)
676 (condition-case nil (require ext)
677 (error (message "Problems while trying to load feature `%s'" ext))))
678 (setq org-modules-loaded t)))
680 (defun org-set-modules (var value)
681 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
682 (set var value)
683 (when (featurep 'org)
684 (org-load-modules-maybe 'force)
685 (org-element-cache-reset 'all)))
687 (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
688 "Modules that should always be loaded together with org.el.
690 If a description starts with <C>, the file is not part of Emacs
691 and loading it will require that you have downloaded and properly
692 installed the Org mode distribution.
694 You can also use this system to load external packages (i.e. neither Org
695 core modules, nor modules from the CONTRIB directory). Just add symbols
696 to the end of the list. If the package is called org-xyz.el, then you need
697 to add the symbol `xyz', and the package must have a call to:
699 (provide \\='org-xyz)
701 For export specific modules, see also `org-export-backends'."
702 :group 'org
703 :set 'org-set-modules
704 :version "24.4"
705 :package-version '(Org . "8.0")
706 :type
707 '(set :greedy t
708 (const :tag " bbdb: Links to BBDB entries" org-bbdb)
709 (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
710 (const :tag " crypt: Encryption of subtrees" org-crypt)
711 (const :tag " ctags: Access to Emacs tags with links" org-ctags)
712 (const :tag " docview: Links to doc-view buffers" org-docview)
713 (const :tag " eww: Store link to url of eww" org-eww)
714 (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
715 (const :tag " habit: Track your consistency with habits" org-habit)
716 (const :tag " id: Global IDs for identifying entries" org-id)
717 (const :tag " info: Links to Info nodes" org-info)
718 (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
719 (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
720 (const :tag " mhe: Links to MHE folders/messages" org-mhe)
721 (const :tag " mouse: Additional mouse support" org-mouse)
722 (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
723 (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
724 (const :tag " w3m: Special cut/paste from w3m to Org mode." org-w3m)
726 (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
727 (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
728 (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
729 (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
730 (const :tag "C collector: Collect properties into tables" org-collector)
731 (const :tag "C depend: TODO dependencies for Org mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
732 (const :tag "C drill: Flashcards and spaced repetition for Org mode" org-drill)
733 (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
734 (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
735 (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
736 (const :tag "C eval: Include command output as text" org-eval)
737 (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
738 (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
739 (const :tag "C git-link: Provide org links to specific file version" org-git-link)
740 (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
741 (const :tag "C invoice: Help manage client invoices in Org mode" org-invoice)
742 (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
743 (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
744 (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
745 (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
746 (const :tag "C man: Support for links to manpages in Org mode" org-man)
747 (const :tag "C mew: Links to Mew folders/messages" org-mew)
748 (const :tag "C mtags: Support for muse-like tags" org-mtags)
749 (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
750 (const :tag "C panel: Simple routines for us with bad memory" org-panel)
751 (const :tag "C registry: A registry for Org links" org-registry)
752 (const :tag "C screen: Visit screen sessions through Org links" org-screen)
753 (const :tag "C secretary: Team management with Org" org-secretary)
754 (const :tag "C sqlinsert: Convert Org tables to SQL insertions" orgtbl-sqlinsert)
755 (const :tag "C toc: Table of contents for Org buffer" org-toc)
756 (const :tag "C track: Keep up with Org mode development" org-track)
757 (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
758 (const :tag "C vm: Links to VM folders/messages" org-vm)
759 (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
760 (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
761 (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
763 (defvar org-export-registered-backends) ; From ox.el.
764 (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
765 (declare-function org-export-backend-name "ox" (backend) t)
766 (defcustom org-export-backends '(ascii html icalendar latex odt)
767 "List of export back-ends that should be always available.
769 If a description starts with <C>, the file is not part of Emacs
770 and loading it will require that you have downloaded and properly
771 installed the Org mode distribution.
773 Unlike to `org-modules', libraries in this list will not be
774 loaded along with Org, but only once the export framework is
775 needed.
777 This variable needs to be set before org.el is loaded. If you
778 need to make a change while Emacs is running, use the customize
779 interface or run the following code, where VAL stands for the new
780 value of the variable, after updating it:
782 (progn
783 (setq org-export-registered-backends
784 (cl-remove-if-not
785 (lambda (backend)
786 (let ((name (org-export-backend-name backend)))
787 (or (memq name val)
788 (catch \\='parentp
789 (dolist (b val)
790 (and (org-export-derived-backend-p b name)
791 (throw \\='parentp t)))))))
792 org-export-registered-backends))
793 (let ((new-list (mapcar #\\='org-export-backend-name
794 org-export-registered-backends)))
795 (dolist (backend val)
796 (cond
797 ((not (load (format \"ox-%s\" backend) t t))
798 (message \"Problems while trying to load export back-end \\=`%s\\='\"
799 backend))
800 ((not (memq backend new-list)) (push backend new-list))))
801 (set-default \\='org-export-backends new-list)))
803 Adding a back-end to this list will also pull the back-end it
804 depends on, if any."
805 :group 'org
806 :group 'org-export
807 :version "26.1"
808 :package-version '(Org . "9.0")
809 :initialize 'custom-initialize-set
810 :set (lambda (var val)
811 (if (not (featurep 'ox)) (set-default var val)
812 ;; Any back-end not required anymore (not present in VAL and not
813 ;; a parent of any back-end in the new value) is removed from the
814 ;; list of registered back-ends.
815 (setq org-export-registered-backends
816 (cl-remove-if-not
817 (lambda (backend)
818 (let ((name (org-export-backend-name backend)))
819 (or (memq name val)
820 (catch 'parentp
821 (dolist (b val)
822 (and (org-export-derived-backend-p b name)
823 (throw 'parentp t)))))))
824 org-export-registered-backends))
825 ;; Now build NEW-LIST of both new back-ends and required
826 ;; parents.
827 (let ((new-list (mapcar #'org-export-backend-name
828 org-export-registered-backends)))
829 (dolist (backend val)
830 (cond
831 ((not (load (format "ox-%s" backend) t t))
832 (message "Problems while trying to load export back-end `%s'"
833 backend))
834 ((not (memq backend new-list)) (push backend new-list))))
835 ;; Set VAR to that list with fixed dependencies.
836 (set-default var new-list))))
837 :type '(set :greedy t
838 (const :tag " ascii Export buffer to ASCII format" ascii)
839 (const :tag " beamer Export buffer to Beamer presentation" beamer)
840 (const :tag " html Export buffer to HTML format" html)
841 (const :tag " icalendar Export buffer to iCalendar format" icalendar)
842 (const :tag " latex Export buffer to LaTeX format" latex)
843 (const :tag " man Export buffer to MAN format" man)
844 (const :tag " md Export buffer to Markdown format" md)
845 (const :tag " odt Export buffer to ODT format" odt)
846 (const :tag " org Export buffer to Org format" org)
847 (const :tag " texinfo Export buffer to Texinfo format" texinfo)
848 (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
849 (const :tag "C deck Export buffer to deck.js presentations" deck)
850 (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
851 (const :tag "C groff Export buffer to Groff format" groff)
852 (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
853 (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
854 (const :tag "C s5 Export buffer to s5 presentations" s5)
855 (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
857 (eval-after-load 'ox
858 '(dolist (backend org-export-backends)
859 (condition-case nil (require (intern (format "ox-%s" backend)))
860 (error (message "Problems while trying to load export back-end `%s'"
861 backend)))))
863 (defcustom org-support-shift-select nil
864 "Non-nil means make shift-cursor commands select text when possible.
865 \\<org-mode-map>\
867 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
868 start selecting a region, or enlarge regions started in this way.
869 In Org mode, in special contexts, these same keys are used for
870 other purposes, important enough to compete with shift selection.
871 Org tries to balance these needs by supporting `shift-select-mode'
872 outside these special contexts, under control of this variable.
874 The default of this variable is nil, to avoid confusing behavior. Shifted
875 cursor keys will then execute Org commands in the following contexts:
876 - on a headline, changing TODO state (left/right) and priority (up/down)
877 - on a time stamp, changing the time
878 - in a plain list item, changing the bullet type
879 - in a property definition line, switching between allowed values
880 - in the BEGIN line of a clock table (changing the time block).
881 Outside these contexts, the commands will throw an error.
883 When this variable is t and the cursor is not in a special
884 context, Org mode will support shift-selection for making and
885 enlarging regions. To make this more effective, the bullet
886 cycling will no longer happen anywhere in an item line, but only
887 if the cursor is exactly on the bullet.
889 If you set this variable to the symbol `always', then the keys
890 will not be special in headlines, property lines, and item lines,
891 to make shift selection work there as well. If this is what you
892 want, you can use the following alternative commands:
893 `\\[org-todo]' and `\\[org-priority]' \
894 to change TODO state and priority,
895 `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
896 can be used to switch TODO sets,
897 `\\[org-ctrl-c-minus]' to cycle item bullet types,
898 and properties can be edited by hand or in column view.
900 However, when the cursor is on a timestamp, shift-cursor commands
901 will still edit the time stamp - this is just too good to give up."
902 :group 'org
903 :type '(choice
904 (const :tag "Never" nil)
905 (const :tag "When outside special context" t)
906 (const :tag "Everywhere except timestamps" always)))
908 (defcustom org-loop-over-headlines-in-active-region nil
909 "Shall some commands act upon headlines in the active region?
911 When set to t, some commands will be performed in all headlines
912 within the active region.
914 When set to `start-level', some commands will be performed in all
915 headlines within the active region, provided that these headlines
916 are of the same level than the first one.
918 When set to a string, those commands will be performed on the
919 matching headlines within the active region. Such string must be
920 a tags/property/todo match as it is used in the agenda tags view.
922 The list of commands is: `org-schedule', `org-deadline',
923 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
924 `org-archive-to-archive-sibling'. The archiving commands skip
925 already archived entries."
926 :type '(choice (const :tag "Don't loop" nil)
927 (const :tag "All headlines in active region" t)
928 (const :tag "In active region, headlines at the same level than the first one" start-level)
929 (string :tag "Tags/Property/Todo matcher"))
930 :version "24.1"
931 :group 'org-todo
932 :group 'org-archive)
934 (defgroup org-startup nil
935 "Options concerning startup of Org mode."
936 :tag "Org Startup"
937 :group 'org)
939 (defcustom org-startup-folded t
940 "Non-nil means entering Org mode will switch to OVERVIEW.
942 This can also be configured on a per-file basis by adding one of
943 the following lines anywhere in the buffer:
945 #+STARTUP: fold (or `overview', this is equivalent)
946 #+STARTUP: nofold (or `showall', this is equivalent)
947 #+STARTUP: content
948 #+STARTUP: showeverything
950 Set `org-agenda-inhibit-startup' to a non-nil value if you want
951 to ignore this option when Org opens agenda files for the first
952 time."
953 :group 'org-startup
954 :type '(choice
955 (const :tag "nofold: show all" nil)
956 (const :tag "fold: overview" t)
957 (const :tag "content: all headlines" content)
958 (const :tag "show everything, even drawers" showeverything)))
960 (defcustom org-startup-truncated t
961 "Non-nil means entering Org mode will set `truncate-lines'.
962 This is useful since some lines containing links can be very long and
963 uninteresting. Also tables look terrible when wrapped.
965 The variable `org-startup-truncated' allows to configure
966 truncation for Org mode different to the other modes that use the
967 variable `truncate-lines' and as a shortcut instead of putting
968 the variable `truncate-lines' into the `org-mode-hook'. If one
969 wants to configure truncation for Org mode not statically but
970 dynamically e. g. in a hook like `ediff-prepare-buffer-hook' then
971 the variable `truncate-lines' has to be used because in such a
972 case it is too late to set the variable `org-startup-truncated'."
973 :group 'org-startup
974 :type 'boolean)
976 (defcustom org-startup-indented nil
977 "Non-nil means turn on `org-indent-mode' on startup.
978 This can also be configured on a per-file basis by adding one of
979 the following lines anywhere in the buffer:
981 #+STARTUP: indent
982 #+STARTUP: noindent"
983 :group 'org-structure
984 :type '(choice
985 (const :tag "Not" nil)
986 (const :tag "Globally (slow on startup in large files)" t)))
988 (defcustom org-use-sub-superscripts t
989 "Non-nil means interpret \"_\" and \"^\" for display.
991 If you want to control how Org exports those characters, see
992 `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
993 used to be an alias for `org-export-with-sub-superscripts' in
994 Org <8.0, it is not anymore.
996 When this option is turned on, you can use TeX-like syntax for
997 sub- and superscripts within the buffer. Several characters after
998 \"_\" or \"^\" will be considered as a single item - so grouping
999 with {} is normally not needed. For example, the following things
1000 will be parsed as single sub- or superscripts:
1002 10^24 or 10^tau several digits will be considered 1 item.
1003 10^-12 or 10^-tau a leading sign with digits or a word
1004 x^2-y^3 will be read as x^2 - y^3, because items are
1005 terminated by almost any nonword/nondigit char.
1006 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
1008 Still, ambiguity is possible. So when in doubt, use {} to enclose
1009 the sub/superscript. If you set this variable to the symbol `{}',
1010 the braces are *required* in order to trigger interpretations as
1011 sub/superscript. This can be helpful in documents that need \"_\"
1012 frequently in plain text."
1013 :group 'org-startup
1014 :version "24.4"
1015 :package-version '(Org . "8.0")
1016 :type '(choice
1017 (const :tag "Always interpret" t)
1018 (const :tag "Only with braces" {})
1019 (const :tag "Never interpret" nil)))
1021 (defcustom org-startup-with-beamer-mode nil
1022 "Non-nil means turn on `org-beamer-mode' on startup.
1023 This can also be configured on a per-file basis by adding one of
1024 the following lines anywhere in the buffer:
1026 #+STARTUP: beamer"
1027 :group 'org-startup
1028 :version "24.1"
1029 :type 'boolean)
1031 (defcustom org-startup-align-all-tables nil
1032 "Non-nil means align all tables when visiting a file.
1033 This is useful when the column width in tables is forced with <N> cookies
1034 in table fields. Such tables will look correct only after the first re-align.
1035 This can also be configured on a per-file basis by adding one of
1036 the following lines anywhere in the buffer:
1037 #+STARTUP: align
1038 #+STARTUP: noalign"
1039 :group 'org-startup
1040 :type 'boolean)
1042 (defcustom org-startup-with-inline-images nil
1043 "Non-nil means show inline images when loading a new Org file.
1044 This can also be configured on a per-file basis by adding one of
1045 the following lines anywhere in the buffer:
1046 #+STARTUP: inlineimages
1047 #+STARTUP: noinlineimages"
1048 :group 'org-startup
1049 :version "24.1"
1050 :type 'boolean)
1052 (defcustom org-startup-with-latex-preview nil
1053 "Non-nil means preview LaTeX fragments when loading a new Org file.
1055 This can also be configured on a per-file basis by adding one of
1056 the following lines anywhere in the buffer:
1057 #+STARTUP: latexpreview
1058 #+STARTUP: nolatexpreview"
1059 :group 'org-startup
1060 :version "24.4"
1061 :package-version '(Org . "8.0")
1062 :type 'boolean)
1064 (defcustom org-insert-mode-line-in-empty-file nil
1065 "Non-nil means insert the first line setting Org mode in empty files.
1066 When the function `org-mode' is called interactively in an empty file, this
1067 normally means that the file name does not automatically trigger Org mode.
1068 To ensure that the file will always be in Org mode in the future, a
1069 line enforcing Org mode will be inserted into the buffer, if this option
1070 has been set."
1071 :group 'org-startup
1072 :type 'boolean)
1074 (defcustom org-replace-disputed-keys nil
1075 "Non-nil means use alternative key bindings for some keys.
1076 Org mode uses S-<cursor> keys for changing timestamps and priorities.
1077 These keys are also used by other packages like shift-selection-mode'
1078 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
1079 If you want to use Org mode together with one of these other modes,
1080 or more generally if you would like to move some Org mode commands to
1081 other keys, set this variable and configure the keys with the variable
1082 `org-disputed-keys'.
1084 This option is only relevant at load-time of Org mode, and must be set
1085 *before* org.el is loaded. Changing it requires a restart of Emacs to
1086 become effective."
1087 :group 'org-startup
1088 :type 'boolean)
1090 (defcustom org-use-extra-keys nil
1091 "Non-nil means use extra key sequence definitions for certain commands.
1092 This happens automatically if `window-system' is nil. This
1093 variable lets you do the same manually. You must set it before
1094 loading Org."
1095 :group 'org-startup
1096 :type 'boolean)
1098 (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
1100 (defcustom org-disputed-keys
1101 '(([(shift up)] . [(meta p)])
1102 ([(shift down)] . [(meta n)])
1103 ([(shift left)] . [(meta -)])
1104 ([(shift right)] . [(meta +)])
1105 ([(control shift right)] . [(meta shift +)])
1106 ([(control shift left)] . [(meta shift -)]))
1107 "Keys for which Org mode and other modes compete.
1108 This is an alist, cars are the default keys, second element specifies
1109 the alternative to use when `org-replace-disputed-keys' is t.
1111 Keys can be specified in any syntax supported by `define-key'.
1112 The value of this option takes effect only at Org mode startup,
1113 therefore you'll have to restart Emacs to apply it after changing."
1114 :group 'org-startup
1115 :type 'alist)
1117 (defun org-key (key)
1118 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
1119 Or return the original if not disputed."
1120 (when org-replace-disputed-keys
1121 (let* ((nkey (key-description key))
1122 (x (cl-find-if (lambda (x) (equal (key-description (car x)) nkey))
1123 org-disputed-keys)))
1124 (setq key (if x (cdr x) key))))
1125 key)
1127 (defun org-defkey (keymap key def)
1128 "Define a key, possibly translated, as returned by `org-key'."
1129 (define-key keymap (org-key key) def))
1131 (defcustom org-ellipsis nil
1132 "The ellipsis to use in the Org mode outline.
1134 When nil, just use the standard three dots. When a non-empty string,
1135 use that string instead.
1137 The change affects only Org mode (which will then use its own display table).
1138 Changing this requires executing `\\[org-mode]' in a buffer to become
1139 effective."
1140 :group 'org-startup
1141 :type '(choice (const :tag "Default" nil)
1142 (string :tag "String" :value "...#"))
1143 :safe (lambda (v) (and (string-or-null-p v) (not (equal "" v)))))
1145 (defvar org-display-table nil
1146 "The display table for Org mode, in case `org-ellipsis' is non-nil.")
1148 (defgroup org-keywords nil
1149 "Keywords in Org mode."
1150 :tag "Org Keywords"
1151 :group 'org)
1153 (defcustom org-closed-keep-when-no-todo nil
1154 "Remove CLOSED: time-stamp when switching back to a non-todo state?"
1155 :group 'org-todo
1156 :group 'org-keywords
1157 :version "24.4"
1158 :package-version '(Org . "8.0")
1159 :type 'boolean)
1161 (defgroup org-structure nil
1162 "Options concerning the general structure of Org files."
1163 :tag "Org Structure"
1164 :group 'org)
1166 (defgroup org-reveal-location nil
1167 "Options about how to make context of a location visible."
1168 :tag "Org Reveal Location"
1169 :group 'org-structure)
1171 (defcustom org-show-context-detail '((agenda . local)
1172 (bookmark-jump . lineage)
1173 (isearch . lineage)
1174 (default . ancestors))
1175 "Alist between context and visibility span when revealing a location.
1177 \\<org-mode-map>Some actions may move point into invisible
1178 locations. As a consequence, Org always expose a neighborhood
1179 around point. How much is shown depends on the initial action,
1180 or context. Valid contexts are
1182 agenda when exposing an entry from the agenda
1183 org-goto when using the command `org-goto' (`\\[org-goto]')
1184 occur-tree when using the command `org-occur' (`\\[org-sparse-tree] /')
1185 tags-tree when constructing a sparse tree based on tags matches
1186 link-search when exposing search matches associated with a link
1187 mark-goto when exposing the jump goal of a mark
1188 bookmark-jump when exposing a bookmark location
1189 isearch when exiting from an incremental search
1190 default default for all contexts not set explicitly
1192 Allowed visibility spans are
1194 minimal show current headline; if point is not on headline,
1195 also show entry
1197 local show current headline, entry and next headline
1199 ancestors show current headline and its direct ancestors; if
1200 point is not on headline, also show entry
1202 lineage show current headline, its direct ancestors and all
1203 their children; if point is not on headline, also show
1204 entry and first child
1206 tree show current headline, its direct ancestors and all
1207 their children; if point is not on headline, also show
1208 entry and all children
1210 canonical show current headline, its direct ancestors along with
1211 their entries and children; if point is not located on
1212 the headline, also show current entry and all children
1214 As special cases, a nil or t value means show all contexts in
1215 `minimal' or `canonical' view, respectively.
1217 Some views can make displayed information very compact, but also
1218 make it harder to edit the location of the match. In such
1219 a case, use the command `org-reveal' (`\\[org-reveal]') to show
1220 more context."
1221 :group 'org-reveal-location
1222 :version "26.1"
1223 :package-version '(Org . "9.0")
1224 :type '(choice
1225 (const :tag "Canonical" t)
1226 (const :tag "Minimal" nil)
1227 (repeat :greedy t :tag "Individual contexts"
1228 (cons
1229 (choice :tag "Context"
1230 (const agenda)
1231 (const org-goto)
1232 (const occur-tree)
1233 (const tags-tree)
1234 (const link-search)
1235 (const mark-goto)
1236 (const bookmark-jump)
1237 (const isearch)
1238 (const default))
1239 (choice :tag "Detail level"
1240 (const minimal)
1241 (const local)
1242 (const ancestors)
1243 (const lineage)
1244 (const tree)
1245 (const canonical))))))
1247 (defcustom org-indirect-buffer-display 'other-window
1248 "How should indirect tree buffers be displayed?
1250 This applies to indirect buffers created with the commands
1251 `org-tree-to-indirect-buffer' and `org-agenda-tree-to-indirect-buffer'.
1253 Valid values are:
1254 current-window Display in the current window
1255 other-window Just display in another window.
1256 dedicated-frame Create one new frame, and re-use it each time.
1257 new-frame Make a new frame each time. Note that in this case
1258 previously-made indirect buffers are kept, and you need to
1259 kill these buffers yourself."
1260 :group 'org-structure
1261 :group 'org-agenda-windows
1262 :type '(choice
1263 (const :tag "In current window" current-window)
1264 (const :tag "In current frame, other window" other-window)
1265 (const :tag "Each time a new frame" new-frame)
1266 (const :tag "One dedicated frame" dedicated-frame)))
1268 (defcustom org-use-speed-commands nil
1269 "Non-nil means activate single letter commands at beginning of a headline.
1270 This may also be a function to test for appropriate locations where speed
1271 commands should be active.
1273 For example, to activate speed commands when the point is on any
1274 star at the beginning of the headline, you can do this:
1276 (setq org-use-speed-commands
1277 (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
1278 :group 'org-structure
1279 :type '(choice
1280 (const :tag "Never" nil)
1281 (const :tag "At beginning of headline stars" t)
1282 (function)))
1284 (defcustom org-speed-commands-user nil
1285 "Alist of additional speed commands.
1286 This list will be checked before `org-speed-commands-default'
1287 when the variable `org-use-speed-commands' is non-nil
1288 and when the cursor is at the beginning of a headline.
1289 The car of each entry is a string with a single letter, which must
1290 be assigned to `self-insert-command' in the global map.
1291 The cdr is either a command to be called interactively, a function
1292 to be called, or a form to be evaluated.
1293 An entry that is just a list with a single string will be interpreted
1294 as a descriptive headline that will be added when listing the speed
1295 commands in the Help buffer using the `?' speed command."
1296 :group 'org-structure
1297 :type '(repeat :value ("k" . ignore)
1298 (choice :value ("k" . ignore)
1299 (list :tag "Descriptive Headline" (string :tag "Headline"))
1300 (cons :tag "Letter and Command"
1301 (string :tag "Command letter")
1302 (choice
1303 (function)
1304 (sexp))))))
1306 (defcustom org-bookmark-names-plist
1307 '(:last-capture "org-capture-last-stored"
1308 :last-refile "org-refile-last-stored"
1309 :last-capture-marker "org-capture-last-stored-marker")
1310 "Names for bookmarks automatically set by some Org commands.
1311 This can provide strings as names for a number of bookmarks Org sets
1312 automatically. The following keys are currently implemented:
1313 :last-capture
1314 :last-capture-marker
1315 :last-refile
1316 When a key does not show up in the property list, the corresponding bookmark
1317 is not set."
1318 :group 'org-structure
1319 :type 'plist)
1321 (defgroup org-cycle nil
1322 "Options concerning visibility cycling in Org mode."
1323 :tag "Org Cycle"
1324 :group 'org-structure)
1326 (defcustom org-cycle-skip-children-state-if-no-children t
1327 "Non-nil means skip CHILDREN state in entries that don't have any."
1328 :group 'org-cycle
1329 :type 'boolean)
1331 (defcustom org-cycle-max-level nil
1332 "Maximum level which should still be subject to visibility cycling.
1333 Levels higher than this will, for cycling, be treated as text, not a headline.
1334 When `org-odd-levels-only' is set, a value of N in this variable actually
1335 means 2N-1 stars as the limiting headline.
1336 When nil, cycle all levels.
1337 Note that the limiting level of cycling is also influenced by
1338 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
1339 `org-inlinetask-min-level' is, cycling will be limited to levels one less
1340 than its value."
1341 :group 'org-cycle
1342 :type '(choice
1343 (const :tag "No limit" nil)
1344 (integer :tag "Maximum level")))
1346 (defcustom org-hide-block-startup nil
1347 "Non-nil means entering Org mode will fold all blocks.
1348 This can also be set in on a per-file basis with
1350 #+STARTUP: hideblocks
1351 #+STARTUP: showblocks"
1352 :group 'org-startup
1353 :group 'org-cycle
1354 :type 'boolean)
1356 (defcustom org-cycle-global-at-bob nil
1357 "Cycle globally if cursor is at beginning of buffer and not at a headline.
1359 This makes it possible to do global cycling without having to use `S-TAB'
1360 or `\\[universal-argument] TAB'. For this special case to work, the first \
1361 line of the buffer
1362 must not be a headline -- it may be empty or some other text.
1364 When used in this way, `org-cycle-hook' is disabled temporarily to make
1365 sure the cursor stays at the beginning of the buffer.
1367 When this option is nil, don't do anything special at the beginning of
1368 the buffer."
1369 :group 'org-cycle
1370 :type 'boolean)
1372 (defcustom org-cycle-level-after-item/entry-creation t
1373 "Non-nil means cycle entry level or item indentation in new empty entries.
1375 When the cursor is at the end of an empty headline, i.e., with only stars
1376 and maybe a TODO keyword, TAB will then switch the entry to become a child,
1377 and then all possible ancestor states, before returning to the original state.
1378 This makes data entry extremely fast: M-RET to create a new headline,
1379 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
1381 When the cursor is at the end of an empty plain list item, one TAB will
1382 make it a subitem, two or more tabs will back up to make this an item
1383 higher up in the item hierarchy."
1384 :group 'org-cycle
1385 :type 'boolean)
1387 (defcustom org-cycle-emulate-tab t
1388 "Where should `org-cycle' emulate TAB.
1389 nil Never
1390 white Only in completely white lines
1391 whitestart Only at the beginning of lines, before the first non-white char
1392 t Everywhere except in headlines
1393 exc-hl-bol Everywhere except at the start of a headline
1394 If TAB is used in a place where it does not emulate TAB, the current subtree
1395 visibility is cycled."
1396 :group 'org-cycle
1397 :type '(choice (const :tag "Never" nil)
1398 (const :tag "Only in completely white lines" white)
1399 (const :tag "Before first char in a line" whitestart)
1400 (const :tag "Everywhere except in headlines" t)
1401 (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
1403 (defcustom org-cycle-separator-lines 2
1404 "Number of empty lines needed to keep an empty line between collapsed trees.
1405 If you leave an empty line between the end of a subtree and the following
1406 headline, this empty line is hidden when the subtree is folded.
1407 Org mode will leave (exactly) one empty line visible if the number of
1408 empty lines is equal or larger to the number given in this variable.
1409 So the default 2 means at least 2 empty lines after the end of a subtree
1410 are needed to produce free space between a collapsed subtree and the
1411 following headline.
1413 If the number is negative, and the number of empty lines is at least -N,
1414 all empty lines are shown.
1416 Special case: when 0, never leave empty lines in collapsed view."
1417 :group 'org-cycle
1418 :type 'integer)
1419 (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
1421 (defcustom org-pre-cycle-hook nil
1422 "Hook that is run before visibility cycling is happening.
1423 The function(s) in this hook must accept a single argument which indicates
1424 the new state that will be set right after running this hook. The
1425 argument is a symbol. Before a global state change, it can have the values
1426 `overview', `content', or `all'. Before a local state change, it can have
1427 the values `folded', `children', or `subtree'."
1428 :group 'org-cycle
1429 :type 'hook)
1431 (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
1432 org-cycle-hide-drawers
1433 org-cycle-show-empty-lines
1434 org-optimize-window-after-visibility-change)
1435 "Hook that is run after `org-cycle' has changed the buffer visibility.
1436 The function(s) in this hook must accept a single argument which indicates
1437 the new state that was set by the most recent `org-cycle' command. The
1438 argument is a symbol. After a global state change, it can have the values
1439 `overview', `contents', or `all'. After a local state change, it can have
1440 the values `folded', `children', or `subtree'."
1441 :group 'org-cycle
1442 :type 'hook
1443 :version "26.1"
1444 :package-version '(Org . "8.3"))
1446 (defgroup org-edit-structure nil
1447 "Options concerning structure editing in Org mode."
1448 :tag "Org Edit Structure"
1449 :group 'org-structure)
1451 (defcustom org-odd-levels-only nil
1452 "Non-nil means skip even levels and only use odd levels for the outline.
1453 This has the effect that two stars are being added/taken away in
1454 promotion/demotion commands. It also influences how levels are
1455 handled by the exporters.
1456 Changing it requires restart of `font-lock-mode' to become effective
1457 for fontification also in regions already fontified.
1458 You may also set this on a per-file basis by adding one of the following
1459 lines to the buffer:
1461 #+STARTUP: odd
1462 #+STARTUP: oddeven"
1463 :group 'org-edit-structure
1464 :group 'org-appearance
1465 :type 'boolean)
1467 (defcustom org-adapt-indentation t
1468 "Non-nil means adapt indentation to outline node level.
1470 When this variable is set, Org assumes that you write outlines by
1471 indenting text in each node to align with the headline (after the
1472 stars). The following issues are influenced by this variable:
1474 - The indentation is increased by one space in a demotion
1475 command, and decreased by one in a promotion command. However,
1476 in the latter case, if shifting some line in the entry body
1477 would alter document structure (e.g., insert a new headline),
1478 indentation is not changed at all.
1480 - Property drawers and planning information is inserted indented
1481 when this variable is set. When nil, they will not be indented.
1483 - TAB indents a line relative to current level. The lines below
1484 a headline will be indented when this variable is set.
1486 Note that this is all about true indentation, by adding and
1487 removing space characters. See also `org-indent.el' which does
1488 level-dependent indentation in a virtual way, i.e. at display
1489 time in Emacs."
1490 :group 'org-edit-structure
1491 :type 'boolean)
1493 (defcustom org-special-ctrl-a/e nil
1494 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1496 When t, `C-a' will bring back the cursor to the beginning of the
1497 headline text, i.e. after the stars and after a possible TODO
1498 keyword. In an item, this will be the position after bullet and
1499 check-box, if any. When the cursor is already at that position,
1500 another `C-a' will bring it to the beginning of the line.
1502 `C-e' will jump to the end of the headline, ignoring the presence
1503 of tags in the headline. A second `C-e' will then jump to the
1504 true end of the line, after any tags. This also means that, when
1505 this variable is non-nil, `C-e' also will never jump beyond the
1506 end of the heading of a folded section, i.e. not after the
1507 ellipses.
1509 When set to the symbol `reversed', the first `C-a' or `C-e' works
1510 normally, going to the true line boundary first. Only a directly
1511 following, identical keypress will bring the cursor to the
1512 special positions.
1514 This may also be a cons cell where the behavior for `C-a' and
1515 `C-e' is set separately."
1516 :group 'org-edit-structure
1517 :type '(choice
1518 (const :tag "off" nil)
1519 (const :tag "on: after stars/bullet and before tags first" t)
1520 (const :tag "reversed: true line boundary first" reversed)
1521 (cons :tag "Set C-a and C-e separately"
1522 (choice :tag "Special C-a"
1523 (const :tag "off" nil)
1524 (const :tag "on: after stars/bullet first" t)
1525 (const :tag "reversed: before stars/bullet first" reversed))
1526 (choice :tag "Special C-e"
1527 (const :tag "off" nil)
1528 (const :tag "on: before tags first" t)
1529 (const :tag "reversed: after tags first" reversed)))))
1530 (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
1532 (defcustom org-special-ctrl-k nil
1533 "Non-nil means `C-k' will behave specially in headlines.
1534 When nil, `C-k' will call the default `kill-line' command.
1535 When t, the following will happen while the cursor is in the headline:
1537 - When the cursor is at the beginning of a headline, kill the entire
1538 line and possible the folded subtree below the line.
1539 - When in the middle of the headline text, kill the headline up to the tags.
1540 - When after the headline text, kill the tags."
1541 :group 'org-edit-structure
1542 :type 'boolean)
1544 (defcustom org-ctrl-k-protect-subtree nil
1545 "Non-nil means, do not delete a hidden subtree with C-k.
1546 When set to the symbol `error', simply throw an error when C-k is
1547 used to kill (part-of) a headline that has hidden text behind it.
1548 Any other non-nil value will result in a query to the user, if it is
1549 OK to kill that hidden subtree. When nil, kill without remorse."
1550 :group 'org-edit-structure
1551 :version "24.1"
1552 :type '(choice
1553 (const :tag "Do not protect hidden subtrees" nil)
1554 (const :tag "Protect hidden subtrees with a security query" t)
1555 (const :tag "Never kill a hidden subtree with C-k" error)))
1557 (defcustom org-special-ctrl-o t
1558 "Non-nil means, make `C-o' insert a row in tables."
1559 :group 'org-edit-structure
1560 :type 'boolean)
1562 (defcustom org-catch-invisible-edits nil
1563 "Check if in invisible region before inserting or deleting a character.
1564 Valid values are:
1566 nil Do not check, so just do invisible edits.
1567 error Throw an error and do nothing.
1568 show Make point visible, and do the requested edit.
1569 show-and-error Make point visible, then throw an error and abort the edit.
1570 smart Make point visible, and do insertion/deletion if it is
1571 adjacent to visible text and the change feels predictable.
1572 Never delete a previously invisible character or add in the
1573 middle or right after an invisible region. Basically, this
1574 allows insertion and backward-delete right before ellipses.
1575 FIXME: maybe in this case we should not even show?"
1576 :group 'org-edit-structure
1577 :version "24.1"
1578 :type '(choice
1579 (const :tag "Do not check" nil)
1580 (const :tag "Throw error when trying to edit" error)
1581 (const :tag "Unhide, but do not do the edit" show-and-error)
1582 (const :tag "Show invisible part and do the edit" show)
1583 (const :tag "Be smart and do the right thing" smart)))
1585 (defcustom org-yank-folded-subtrees t
1586 "Non-nil means when yanking subtrees, fold them.
1587 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1588 it starts with a heading and all other headings in it are either children
1589 or siblings, then fold all the subtrees. However, do this only if no
1590 text after the yank would be swallowed into a folded tree by this action."
1591 :group 'org-edit-structure
1592 :type 'boolean)
1594 (defcustom org-yank-adjusted-subtrees nil
1595 "Non-nil means when yanking subtrees, adjust the level.
1596 With this setting, `org-paste-subtree' is used to insert the subtree, see
1597 this function for details."
1598 :group 'org-edit-structure
1599 :type 'boolean)
1601 (defcustom org-M-RET-may-split-line '((default . t))
1602 "Non-nil means M-RET will split the line at the cursor position.
1603 When nil, it will go to the end of the line before making a
1604 new line.
1605 You may also set this option in a different way for different
1606 contexts. Valid contexts are:
1608 headline when creating a new headline
1609 item when creating a new item
1610 table in a table field
1611 default the value to be used for all contexts not explicitly
1612 customized"
1613 :group 'org-structure
1614 :group 'org-table
1615 :type '(choice
1616 (const :tag "Always" t)
1617 (const :tag "Never" nil)
1618 (repeat :greedy t :tag "Individual contexts"
1619 (cons
1620 (choice :tag "Context"
1621 (const headline)
1622 (const item)
1623 (const table)
1624 (const default))
1625 (boolean)))))
1628 (defcustom org-insert-heading-respect-content nil
1629 "Non-nil means insert new headings after the current subtree.
1630 \\<org-mode-map>
1631 When nil, the new heading is created directly after the current line.
1632 The commands `\\[org-insert-heading-respect-content]' and \
1633 `\\[org-insert-todo-heading-respect-content]' turn this variable on
1634 for the duration of the command."
1635 :group 'org-structure
1636 :type 'boolean)
1638 (defcustom org-blank-before-new-entry '((heading . auto)
1639 (plain-list-item . auto))
1640 "Should `org-insert-heading' leave a blank line before new heading/item?
1641 The value is an alist, with `heading' and `plain-list-item' as CAR,
1642 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1643 which case Org will look at the surrounding headings/items and try to
1644 make an intelligent decision whether to insert a blank line or not."
1645 :group 'org-edit-structure
1646 :type '(list
1647 (cons (const heading)
1648 (choice (const :tag "Never" nil)
1649 (const :tag "Always" t)
1650 (const :tag "Auto" auto)))
1651 (cons (const plain-list-item)
1652 (choice (const :tag "Never" nil)
1653 (const :tag "Always" t)
1654 (const :tag "Auto" auto)))))
1656 (defcustom org-insert-heading-hook nil
1657 "Hook being run after inserting a new heading."
1658 :group 'org-edit-structure
1659 :type 'hook)
1661 (defcustom org-enable-fixed-width-editor t
1662 "Non-nil means lines starting with \":\" are treated as fixed-width.
1663 This currently only means they are never auto-wrapped.
1664 When nil, such lines will be treated like ordinary lines."
1665 :group 'org-edit-structure
1666 :type 'boolean)
1668 (defcustom org-goto-auto-isearch t
1669 "Non-nil means typing characters in `org-goto' starts incremental search.
1670 When nil, you can use these keybindings to navigate the buffer:
1672 q Quit the org-goto interface
1673 n Go to the next visible heading
1674 p Go to the previous visible heading
1675 f Go one heading forward on same level
1676 b Go one heading backward on same level
1677 u Go one heading up"
1678 :group 'org-edit-structure
1679 :type 'boolean)
1681 (defgroup org-sparse-trees nil
1682 "Options concerning sparse trees in Org mode."
1683 :tag "Org Sparse Trees"
1684 :group 'org-structure)
1686 (defcustom org-highlight-sparse-tree-matches t
1687 "Non-nil means highlight all matches that define a sparse tree.
1688 The highlights will automatically disappear the next time the buffer is
1689 changed by an edit command."
1690 :group 'org-sparse-trees
1691 :type 'boolean)
1693 (defcustom org-remove-highlights-with-change t
1694 "Non-nil means any change to the buffer will remove temporary highlights.
1695 \\<org-mode-map>\
1696 Such highlights are created by `org-occur' and `org-clock-display'.
1697 When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
1698 to get rid of the highlights.
1699 The highlights created by `org-toggle-latex-fragment' always need
1700 `\\[org-toggle-latex-fragment]' to be removed."
1701 :group 'org-sparse-trees
1702 :group 'org-time
1703 :type 'boolean)
1705 (defcustom org-occur-case-fold-search t
1706 "Non-nil means `org-occur' should be case-insensitive.
1707 If set to `smart' the search will be case-insensitive only if it
1708 doesn't specify any upper case character."
1709 :group 'org-sparse-trees
1710 :version "26.1"
1711 :type '(choice
1712 (const :tag "Case-sensitive" nil)
1713 (const :tag "Case-insensitive" t)
1714 (const :tag "Case-insensitive for lower case searches only" smart)))
1716 (defcustom org-occur-hook '(org-first-headline-recenter)
1717 "Hook that is run after `org-occur' has constructed a sparse tree.
1718 This can be used to recenter the window to show as much of the structure
1719 as possible."
1720 :group 'org-sparse-trees
1721 :type 'hook)
1723 (defgroup org-imenu-and-speedbar nil
1724 "Options concerning imenu and speedbar in Org mode."
1725 :tag "Org Imenu and Speedbar"
1726 :group 'org-structure)
1728 (defcustom org-imenu-depth 2
1729 "The maximum level for Imenu access to Org headlines.
1730 This also applied for speedbar access."
1731 :group 'org-imenu-and-speedbar
1732 :type 'integer)
1734 (defgroup org-table nil
1735 "Options concerning tables in Org mode."
1736 :tag "Org Table"
1737 :group 'org)
1739 (defcustom org-self-insert-cluster-for-undo nil
1740 "Non-nil means cluster self-insert commands for undo when possible.
1741 If this is set, then, like in the Emacs command loop, 20 consecutive
1742 characters will be undone together.
1743 This is configurable, because there is some impact on typing performance."
1744 :group 'org-table
1745 :type 'boolean)
1747 (defcustom org-table-tab-recognizes-table.el t
1748 "Non-nil means TAB will automatically notice a table.el table.
1749 When it sees such a table, it moves point into it and - if necessary -
1750 calls `table-recognize-table'."
1751 :group 'org-table-editing
1752 :type 'boolean)
1754 (defgroup org-link nil
1755 "Options concerning links in Org mode."
1756 :tag "Org Link"
1757 :group 'org)
1759 (defvar-local org-link-abbrev-alist-local nil
1760 "Buffer-local version of `org-link-abbrev-alist', which see.
1761 The value of this is taken from the #+LINK lines.")
1763 (defcustom org-link-parameters
1764 '(("doi" :follow org--open-doi-link)
1765 ("elisp" :follow org--open-elisp-link)
1766 ("file" :complete org-file-complete-link)
1767 ("ftp" :follow (lambda (path) (browse-url (concat "ftp:" path))))
1768 ("help" :follow org--open-help-link)
1769 ("http" :follow (lambda (path) (browse-url (concat "http:" path))))
1770 ("https" :follow (lambda (path) (browse-url (concat "https:" path))))
1771 ("mailto" :follow (lambda (path) (browse-url (concat "mailto:" path))))
1772 ("news" :follow (lambda (path) (browse-url (concat "news:" path))))
1773 ("shell" :follow org--open-shell-link))
1774 "An alist of properties that defines all the links in Org mode.
1775 The key in each association is a string of the link type.
1776 Subsequent optional elements make up a p-list of link properties.
1778 :follow - A function that takes the link path as an argument.
1780 :export - A function that takes the link path, description and
1781 export-backend as arguments.
1783 :store - A function responsible for storing the link. See the
1784 function `org-store-link-functions'.
1786 :complete - A function that inserts a link with completion. The
1787 function takes one optional prefix arg.
1789 :face - A face for the link, or a function that returns a face.
1790 The function takes one argument which is the link path. The
1791 default face is `org-link'.
1793 :mouse-face - The mouse-face. The default is `highlight'.
1795 :display - `full' will not fold the link in descriptive
1796 display. Default is `org-link'.
1798 :help-echo - A string or function that takes (window object position)
1799 as arguments and returns a string.
1801 :keymap - A keymap that is active on the link. The default is
1802 `org-mouse-map'.
1804 :htmlize-link - A function for the htmlize-link. Defaults
1805 to (list :uri \"type:path\")
1807 :activate-func - A function to run at the end of font-lock
1808 activation. The function must accept (link-start link-end path bracketp)
1809 as arguments."
1810 :group 'org-link
1811 :type '(alist :tag "Link display parameters"
1812 :value-type plist)
1813 :version "26.1"
1814 :package-version '(Org . "9.1"))
1816 (defun org-link-get-parameter (type key)
1817 "Get TYPE link property for KEY.
1818 TYPE is a string and KEY is a plist keyword."
1819 (plist-get
1820 (cdr (assoc type org-link-parameters))
1821 key))
1823 (defun org-link-set-parameters (type &rest parameters)
1824 "Set link TYPE properties to PARAMETERS.
1825 PARAMETERS should be :key val pairs."
1826 (let ((data (assoc type org-link-parameters)))
1827 (if data (setcdr data (org-combine-plists (cdr data) parameters))
1828 (push (cons type parameters) org-link-parameters)
1829 (org-make-link-regexps)
1830 (org-element-update-syntax))))
1832 (defun org-link-types ()
1833 "Return a list of known link types."
1834 (mapcar #'car org-link-parameters))
1836 (defcustom org-link-abbrev-alist nil
1837 "Alist of link abbreviations.
1838 The car of each element is a string, to be replaced at the start of a link.
1839 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1840 links in Org buffers can have an optional tag after a double colon, e.g.,
1842 [[linkkey:tag][description]]
1844 The `linkkey' must be a single word, starting with a letter, followed
1845 by letters, numbers, `-' or `_'.
1847 If REPLACE is a string, the tag will simply be appended to create the link.
1848 If the string contains \"%s\", the tag will be inserted there. If the string
1849 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1850 at that point (see the function `url-hexify-string'). If the string contains
1851 the specifier \"%(my-function)\", then the custom function `my-function' will
1852 be invoked: this function takes the tag as its only argument and must return
1853 a string.
1855 REPLACE may also be a function that will be called with the tag as the
1856 only argument to create the link, which should be returned as a string.
1858 See the manual for examples."
1859 :group 'org-link
1860 :type '(repeat
1861 (cons
1862 (string :tag "Protocol")
1863 (choice
1864 (string :tag "Format")
1865 (function)))))
1867 (defcustom org-descriptive-links t
1868 "Non-nil means Org will display descriptive links.
1869 E.g. [[https://orgmode.org][Org website]] will be displayed as
1870 \"Org Website\", hiding the link itself and just displaying its
1871 description. When set to nil, Org will display the full links
1872 literally.
1874 You can interactively set the value of this variable by calling
1875 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1876 :group 'org-link
1877 :type 'boolean)
1879 (defcustom org-link-file-path-type 'adaptive
1880 "How the path name in file links should be stored.
1881 Valid values are:
1883 relative Relative to the current directory, i.e. the directory of the file
1884 into which the link is being inserted.
1885 absolute Absolute path, if possible with ~ for home directory.
1886 noabbrev Absolute path, no abbreviation of home directory.
1887 adaptive Use relative path for files in the current directory and sub-
1888 directories of it. For other files, use an absolute path."
1889 :group 'org-link
1890 :type '(choice
1891 (const relative)
1892 (const absolute)
1893 (const noabbrev)
1894 (const adaptive)))
1896 (defvaralias 'org-activate-links 'org-highlight-links)
1897 (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
1898 "Types of links that should be highlighted in Org files.
1900 This is a list of symbols, each one of them leading to the
1901 highlighting of a certain link type.
1903 You can still open links that are not highlighted.
1905 In principle, it does not hurt to turn on highlighting for all
1906 link types. There may be a small gain when turning off unused
1907 link types. The types are:
1909 bracket The recommended [[link][description]] or [[link]] links with hiding.
1910 angle Links in angular brackets that may contain whitespace like
1911 <bbdb:Carsten Dominik>.
1912 plain Plain links in normal text, no whitespace, like http://google.com.
1913 radio Text that is matched by a radio target, see manual for details.
1914 tag Tag settings in a headline (link to tag search).
1915 date Time stamps (link to calendar).
1916 footnote Footnote labels.
1918 If you set this variable during an Emacs session, use `org-mode-restart'
1919 in the Org buffer so that the change takes effect."
1920 :group 'org-link
1921 :group 'org-appearance
1922 :type '(set :greedy t
1923 (const :tag "Double bracket links" bracket)
1924 (const :tag "Angular bracket links" angle)
1925 (const :tag "Plain text links" plain)
1926 (const :tag "Radio target matches" radio)
1927 (const :tag "Tags" tag)
1928 (const :tag "Timestamps" date)
1929 (const :tag "Footnotes" footnote)))
1931 (defcustom org-make-link-description-function nil
1932 "Function to use for generating link descriptions from links.
1933 This function must take two parameters: the first one is the
1934 link, the second one is the description generated by
1935 `org-insert-link'. The function should return the description to
1936 use."
1937 :group 'org-link
1938 :type '(choice (const nil) (function)))
1940 (defgroup org-link-store nil
1941 "Options concerning storing links in Org mode."
1942 :tag "Org Store Link"
1943 :group 'org-link)
1945 (defcustom org-url-hexify-p t
1946 "When non-nil, hexify URL when creating a link."
1947 :type 'boolean
1948 :version "24.3"
1949 :group 'org-link-store)
1951 (defcustom org-email-link-description-format "Email %c: %.30s"
1952 "Format of the description part of a link to an email or usenet message.
1953 The following %-escapes will be replaced by corresponding information:
1955 %F full \"From\" field
1956 %f name, taken from \"From\" field, address if no name
1957 %T full \"To\" field
1958 %t first name in \"To\" field, address if no name
1959 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1960 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1961 %s subject
1962 %d date
1963 %m message-id.
1965 You may use normal field width specification between the % and the letter.
1966 This is for example useful to limit the length of the subject.
1968 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1969 :group 'org-link-store
1970 :type 'string)
1972 (defcustom org-from-is-user-regexp
1973 (let (r1 r2)
1974 (when (and user-mail-address (not (string= user-mail-address "")))
1975 (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
1976 (when (and user-full-name (not (string= user-full-name "")))
1977 (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
1978 (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
1979 "Regexp matched against the \"From:\" header of an email or usenet message.
1980 It should match if the message is from the user him/herself."
1981 :group 'org-link-store
1982 :type 'regexp)
1984 (defcustom org-context-in-file-links t
1985 "Non-nil means file links from `org-store-link' contain context.
1986 \\<org-mode-map>
1987 A search string will be added to the file name with :: as separator
1988 and used to find the context when the link is activated by the command
1989 `org-open-at-point'. When this option is t, the entire active region
1990 will be placed in the search string of the file link. If set to a
1991 positive integer, only the first n lines of context will be stored.
1993 Using a prefix arg to the command `org-store-link' (`\\[universal-argument] \
1994 \\[org-store-link]')
1995 negates this setting for the duration of the command."
1996 :group 'org-link-store
1997 :type '(choice boolean integer))
1999 (defcustom org-keep-stored-link-after-insertion nil
2000 "Non-nil means keep link in list for entire session.
2001 \\<org-mode-map>
2002 The command `org-store-link' adds a link pointing to the current
2003 location to an internal list. These links accumulate during a session.
2004 The command `org-insert-link' can be used to insert links into any
2005 Org file (offering completion for all stored links).
2007 When this option is nil, every link which has been inserted once using
2008 `\\[org-insert-link]' will be removed from the list, to make completing the \
2009 unused
2010 links more efficient."
2011 :group 'org-link-store
2012 :type 'boolean)
2014 (defgroup org-link-follow nil
2015 "Options concerning following links in Org mode."
2016 :tag "Org Follow Link"
2017 :group 'org-link)
2019 (defcustom org-link-translation-function nil
2020 "Function to translate links with different syntax to Org syntax.
2021 This can be used to translate links created for example by the Planner
2022 or emacs-wiki packages to Org syntax.
2023 The function must accept two parameters, a TYPE containing the link
2024 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
2025 which is everything after the link protocol. It should return a cons
2026 with possibly modified values of type and path.
2027 Org contains a function for this, so if you set this variable to
2028 `org-translate-link-from-planner', you should be able follow many
2029 links created by planner."
2030 :group 'org-link-follow
2031 :type '(choice (const nil) (function)))
2033 (defcustom org-follow-link-hook nil
2034 "Hook that is run after a link has been followed."
2035 :group 'org-link-follow
2036 :type 'hook)
2038 (defcustom org-tab-follows-link nil
2039 "Non-nil means on links TAB will follow the link.
2040 Needs to be set before org.el is loaded.
2041 This really should not be used, it does not make sense, and the
2042 implementation is bad."
2043 :group 'org-link-follow
2044 :type 'boolean)
2046 (defcustom org-return-follows-link nil
2047 "Non-nil means on links RET will follow the link.
2048 In tables, the special behavior of RET has precedence."
2049 :group 'org-link-follow
2050 :type 'boolean)
2052 (defcustom org-mouse-1-follows-link
2053 (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
2054 "Non-nil means mouse-1 on a link will follow the link.
2055 A longer mouse click will still set point. Needs to be set
2056 before org.el is loaded."
2057 :group 'org-link-follow
2058 :version "26.1"
2059 :package-version '(Org . "8.3")
2060 :type '(choice
2061 (const :tag "A double click follows the link" double)
2062 (const :tag "Unconditionally follow the link with mouse-1" t)
2063 (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
2065 (defcustom org-mark-ring-length 4
2066 "Number of different positions to be recorded in the ring.
2067 Changing this requires a restart of Emacs to work correctly."
2068 :group 'org-link-follow
2069 :type 'integer)
2071 (defcustom org-link-search-must-match-exact-headline 'query-to-create
2072 "Non-nil means internal fuzzy links can only match headlines.
2074 When nil, the a fuzzy link may point to a target or a named
2075 construct in the document. When set to the special value
2076 `query-to-create', offer to create a new headline when none
2077 matched.
2079 Spaces and statistics cookies are ignored during heading searches."
2080 :group 'org-link-follow
2081 :version "24.1"
2082 :type '(choice
2083 (const :tag "Use fuzzy text search" nil)
2084 (const :tag "Match only exact headline" t)
2085 (const :tag "Match exact headline or query to create it"
2086 query-to-create))
2087 :safe #'symbolp)
2089 (defcustom org-link-frame-setup
2090 '((vm . vm-visit-folder-other-frame)
2091 (vm-imap . vm-visit-imap-folder-other-frame)
2092 (gnus . org-gnus-no-new-news)
2093 (file . find-file-other-window)
2094 (wl . wl-other-frame))
2095 "Setup the frame configuration for following links.
2096 When following a link with Emacs, it may often be useful to display
2097 this link in another window or frame. This variable can be used to
2098 set this up for the different types of links.
2099 For VM, use any of
2100 `vm-visit-folder'
2101 `vm-visit-folder-other-window'
2102 `vm-visit-folder-other-frame'
2103 For Gnus, use any of
2104 `gnus'
2105 `gnus-other-frame'
2106 `org-gnus-no-new-news'
2107 For FILE, use any of
2108 `find-file'
2109 `find-file-other-window'
2110 `find-file-other-frame'
2111 For Wanderlust use any of
2112 `wl'
2113 `wl-other-frame'
2114 For the calendar, use the variable `calendar-setup'.
2115 For BBDB, it is currently only possible to display the matches in
2116 another window."
2117 :group 'org-link-follow
2118 :type '(list
2119 (cons (const vm)
2120 (choice
2121 (const vm-visit-folder)
2122 (const vm-visit-folder-other-window)
2123 (const vm-visit-folder-other-frame)))
2124 (cons (const vm-imap)
2125 (choice
2126 (const vm-visit-imap-folder)
2127 (const vm-visit-imap-folder-other-window)
2128 (const vm-visit-imap-folder-other-frame)))
2129 (cons (const gnus)
2130 (choice
2131 (const gnus)
2132 (const gnus-other-frame)
2133 (const org-gnus-no-new-news)))
2134 (cons (const file)
2135 (choice
2136 (const find-file)
2137 (const find-file-other-window)
2138 (const find-file-other-frame)))
2139 (cons (const wl)
2140 (choice
2141 (const wl)
2142 (const wl-other-frame)))))
2144 (defcustom org-display-internal-link-with-indirect-buffer nil
2145 "Non-nil means use indirect buffer to display infile links.
2146 Activating internal links (from one location in a file to another location
2147 in the same file) normally just jumps to the location. When the link is
2148 activated with a `\\[universal-argument]' prefix (or with mouse-3), the link \
2149 is displayed in
2150 another window. When this option is set, the other window actually displays
2151 an indirect buffer clone of the current buffer, to avoid any visibility
2152 changes to the current buffer."
2153 :group 'org-link-follow
2154 :type 'boolean)
2156 (defcustom org-open-non-existing-files nil
2157 "Non-nil means `org-open-file' will open non-existing files.
2158 When nil, an error will be generated.
2159 This variable applies only to external applications because they
2160 might choke on non-existing files. If the link is to a file that
2161 will be opened in Emacs, the variable is ignored."
2162 :group 'org-link-follow
2163 :type 'boolean)
2165 (defcustom org-open-directory-means-index-dot-org nil
2166 "Non-nil means a link to a directory really means to index.org.
2167 When nil, following a directory link will run dired or open a finder/explorer
2168 window on that directory."
2169 :group 'org-link-follow
2170 :type 'boolean)
2172 (defcustom org-confirm-shell-link-function 'yes-or-no-p
2173 "Non-nil means ask for confirmation before executing shell links.
2174 Shell links can be dangerous: just think about a link
2176 [[shell:rm -rf ~/*][Google Search]]
2178 This link would show up in your Org document as \"Google Search\",
2179 but really it would remove your entire home directory.
2180 Therefore we advise against setting this variable to nil.
2181 Just change it to `y-or-n-p' if you want to confirm with a
2182 single keystroke rather than having to type \"yes\"."
2183 :group 'org-link-follow
2184 :type '(choice
2185 (const :tag "with yes-or-no (safer)" yes-or-no-p)
2186 (const :tag "with y-or-n (faster)" y-or-n-p)
2187 (const :tag "no confirmation (dangerous)" nil)))
2188 (put 'org-confirm-shell-link-function
2189 'safe-local-variable
2190 (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
2192 (defcustom org-confirm-shell-link-not-regexp ""
2193 "A regexp to skip confirmation for shell links."
2194 :group 'org-link-follow
2195 :version "24.1"
2196 :type 'regexp)
2198 (defcustom org-confirm-elisp-link-function 'yes-or-no-p
2199 "Non-nil means ask for confirmation before executing Emacs Lisp links.
2200 Elisp links can be dangerous: just think about a link
2202 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
2204 This link would show up in your Org document as \"Google Search\",
2205 but really it would remove your entire home directory.
2206 Therefore we advise against setting this variable to nil.
2207 Just change it to `y-or-n-p' if you want to confirm with a
2208 single keystroke rather than having to type \"yes\"."
2209 :group 'org-link-follow
2210 :type '(choice
2211 (const :tag "with yes-or-no (safer)" yes-or-no-p)
2212 (const :tag "with y-or-n (faster)" y-or-n-p)
2213 (const :tag "no confirmation (dangerous)" nil)))
2214 (put 'org-confirm-shell-link-function
2215 'safe-local-variable
2216 (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
2218 (defcustom org-confirm-elisp-link-not-regexp ""
2219 "A regexp to skip confirmation for Elisp links."
2220 :group 'org-link-follow
2221 :version "24.1"
2222 :type 'regexp)
2224 (defconst org-file-apps-defaults-gnu
2225 '((remote . emacs)
2226 (system . mailcap)
2227 (t . mailcap))
2228 "Default file applications on a UNIX or GNU/Linux system.
2229 See `org-file-apps'.")
2231 (defconst org-file-apps-defaults-macosx
2232 '((remote . emacs)
2233 (system . "open %s")
2234 ("ps.gz" . "gv %s")
2235 ("eps.gz" . "gv %s")
2236 ("dvi" . "xdvi %s")
2237 ("fig" . "xfig %s")
2238 (t . "open %s"))
2239 "Default file applications on a macOS system.
2240 The system \"open\" is known as a default, but we use X11 applications
2241 for some files for which the OS does not have a good default.
2242 See `org-file-apps'.")
2244 (defconst org-file-apps-defaults-windowsnt
2245 (list '(remote . emacs)
2246 (cons 'system (lambda (file _path)
2247 (with-no-warnings (w32-shell-execute "open" file))))
2248 (cons t (lambda (file _path)
2249 (with-no-warnings (w32-shell-execute "open" file)))))
2250 "Default file applications on a Windows NT system.
2251 The system \"open\" is used for most files.
2252 See `org-file-apps'.")
2254 (defcustom org-file-apps
2255 '((auto-mode . emacs)
2256 ("\\.mm\\'" . default)
2257 ("\\.x?html?\\'" . default)
2258 ("\\.pdf\\'" . default))
2259 "External applications for opening `file:path' items in a document.
2260 \\<org-mode-map>\
2262 Org mode uses system defaults for different file types, but
2263 you can use this variable to set the application for a given file
2264 extension. The entries in this list are cons cells where the car identifies
2265 files and the cdr the corresponding command.
2267 Possible values for the file identifier are:
2269 \"string\" A string as a file identifier can be interpreted in different
2270 ways, depending on its contents:
2272 - Alphanumeric characters only:
2273 Match links with this file extension.
2274 Example: (\"pdf\" . \"evince %s\")
2275 to open PDFs with evince.
2277 - Regular expression: Match links where the
2278 filename matches the regexp. If you want to
2279 use groups here, use shy groups.
2281 Example: (\"\\\\.x?html\\\\\\='\" . \"firefox %s\")
2282 (\"\\\\(?:xhtml\\\\|html\\\\)\\\\\\='\" . \"firefox %s\")
2283 to open *.html and *.xhtml with firefox.
2285 - Regular expression which contains (non-shy) groups:
2286 Match links where the whole link, including \"::\", and
2287 anything after that, matches the regexp.
2288 In a custom command string, %1, %2, etc. are replaced with
2289 the parts of the link that were matched by the groups.
2290 For backwards compatibility, if a command string is given
2291 that does not use any of the group matches, this case is
2292 handled identically to the second one (i.e. match against
2293 file name only).
2294 In a custom function, you can access the group matches with
2295 (match-string n link).
2297 Example: (\"\\\\.pdf::\\\\([0-9]+\\\\)\\\\\\='\" . \
2298 \"evince -p %1 %s\")
2299 to open [[file:document.pdf::5]] with evince at page 5.
2301 `directory' Matches a directory
2302 `remote' Matches a remote file, accessible through tramp or efs.
2303 Remote files most likely should be visited through Emacs
2304 because external applications cannot handle such paths.
2305 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
2306 so all files Emacs knows how to handle. Using this with
2307 command `emacs' will open most files in Emacs. Beware that this
2308 will also open html files inside Emacs, unless you add
2309 (\"html\" . default) to the list as well.
2310 `system' The system command to open files, like `open' on Windows
2311 and macOS, and mailcap under GNU/Linux. This is the command
2312 that will be selected if you call `org-open-at-point' with a
2313 double prefix argument (`\\[universal-argument] \
2314 \\[universal-argument] \\[org-open-at-point]').
2315 t Default for files not matched by any of the other options.
2317 Possible values for the command are:
2319 `emacs' The file will be visited by the current Emacs process.
2320 `default' Use the default application for this file type, which is the
2321 association for t in the list, most likely in the system-specific
2322 part. This can be used to overrule an unwanted setting in the
2323 system-specific variable.
2324 `system' Use the system command for opening files, like \"open\".
2325 This command is specified by the entry whose car is `system'.
2326 Most likely, the system-specific version of this variable
2327 does define this command, but you can overrule/replace it
2328 here.
2329 `mailcap' Use command specified in the mailcaps.
2330 string A command to be executed by a shell; %s will be replaced
2331 by the path to the file.
2332 function A Lisp function, which will be called with two arguments:
2333 the file path and the original link string, without the
2334 \"file:\" prefix.
2336 For more examples, see the system specific constants
2337 `org-file-apps-defaults-macosx'
2338 `org-file-apps-defaults-windowsnt'
2339 `org-file-apps-defaults-gnu'."
2340 :group 'org-link-follow
2341 :type '(repeat
2342 (cons (choice :value ""
2343 (string :tag "Extension")
2344 (const :tag "System command to open files" system)
2345 (const :tag "Default for unrecognized files" t)
2346 (const :tag "Remote file" remote)
2347 (const :tag "Links to a directory" directory)
2348 (const :tag "Any files that have Emacs modes"
2349 auto-mode))
2350 (choice :value ""
2351 (const :tag "Visit with Emacs" emacs)
2352 (const :tag "Use default" default)
2353 (const :tag "Use the system command" system)
2354 (string :tag "Command")
2355 (function :tag "Function")))))
2357 (defcustom org-doi-server-url "http://dx.doi.org/"
2358 "The URL of the DOI server."
2359 :type 'string
2360 :version "24.3"
2361 :group 'org-link-follow)
2363 (defgroup org-refile nil
2364 "Options concerning refiling entries in Org mode."
2365 :tag "Org Refile"
2366 :group 'org)
2368 (defcustom org-directory "~/org"
2369 "Directory with Org files.
2370 This is just a default location to look for Org files. There is no need
2371 at all to put your files into this directory. It is used in the
2372 following situations:
2374 1. When a capture template specifies a target file that is not an
2375 absolute path. The path will then be interpreted relative to
2376 `org-directory'
2377 2. When the value of variable `org-agenda-files' is a single file, any
2378 relative paths in this file will be taken as relative to
2379 `org-directory'."
2380 :group 'org-refile
2381 :group 'org-capture
2382 :type 'directory)
2384 (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
2385 "Default target for storing notes.
2386 Used as a fall back file for org-capture.el, for templates that
2387 do not specify a target file."
2388 :group 'org-refile
2389 :group 'org-capture
2390 :type 'file)
2392 (defcustom org-goto-interface 'outline
2393 "The default interface to be used for `org-goto'.
2394 Allowed values are:
2395 outline The interface shows an outline of the relevant file
2396 and the correct heading is found by moving through
2397 the outline or by searching with incremental search.
2398 outline-path-completion Headlines in the current buffer are offered via
2399 completion. This is the interface also used by
2400 the refile command."
2401 :group 'org-refile
2402 :type '(choice
2403 (const :tag "Outline" outline)
2404 (const :tag "Outline-path-completion" outline-path-completion)))
2406 (defcustom org-goto-max-level 5
2407 "Maximum target level when running `org-goto' with refile interface."
2408 :group 'org-refile
2409 :type 'integer)
2411 (defcustom org-reverse-note-order nil
2412 "Non-nil means store new notes at the beginning of a file or entry.
2413 When nil, new notes will be filed to the end of a file or entry.
2414 This can also be a list with cons cells of regular expressions that
2415 are matched against file names, and values."
2416 :group 'org-capture
2417 :group 'org-refile
2418 :type '(choice
2419 (const :tag "Reverse always" t)
2420 (const :tag "Reverse never" nil)
2421 (repeat :tag "By file name regexp"
2422 (cons regexp boolean))))
2424 (defcustom org-log-refile nil
2425 "Information to record when a task is refiled.
2427 Possible values are:
2429 nil Don't add anything
2430 time Add a time stamp to the task
2431 note Prompt for a note and add it with template `org-log-note-headings'
2433 This option can also be set with on a per-file-basis with
2435 #+STARTUP: nologrefile
2436 #+STARTUP: logrefile
2437 #+STARTUP: lognoterefile
2439 You can have local logging settings for a subtree by setting the LOGGING
2440 property to one or more of these keywords.
2442 When bulk-refiling from the agenda, the value `note' is forbidden and
2443 will temporarily be changed to `time'."
2444 :group 'org-refile
2445 :group 'org-progress
2446 :version "24.1"
2447 :type '(choice
2448 (const :tag "No logging" nil)
2449 (const :tag "Record timestamp" time)
2450 (const :tag "Record timestamp with note." note)))
2452 (defcustom org-refile-targets nil
2453 "Targets for refiling entries with `\\[org-refile]'.
2454 This is a list of cons cells. Each cell contains:
2455 - a specification of the files to be considered, either a list of files,
2456 or a symbol whose function or variable value will be used to retrieve
2457 a file name or a list of file names. If you use `org-agenda-files' for
2458 that, all agenda files will be scanned for targets. Nil means consider
2459 headings in the current buffer.
2460 - A specification of how to find candidate refile targets. This may be
2461 any of:
2462 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
2463 This tag has to be present in all target headlines, inheritance will
2464 not be considered.
2465 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
2466 todo keyword.
2467 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
2468 headlines that are refiling targets.
2469 - a cons cell (:level . N). Any headline of level N is considered a target.
2470 Note that, when `org-odd-levels-only' is set, level corresponds to
2471 order in hierarchy, not to the number of stars.
2472 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
2473 Note that, when `org-odd-levels-only' is set, level corresponds to
2474 order in hierarchy, not to the number of stars.
2476 Each element of this list generates a set of possible targets.
2477 The union of these sets is presented (with completion) to
2478 the user by `org-refile'.
2480 You can set the variable `org-refile-target-verify-function' to a function
2481 to verify each headline found by the simple criteria above.
2483 When this variable is nil, all top-level headlines in the current buffer
2484 are used, equivalent to the value `((nil . (:level . 1))'."
2485 :group 'org-refile
2486 :type '(repeat
2487 (cons
2488 (choice :value org-agenda-files
2489 (const :tag "All agenda files" org-agenda-files)
2490 (const :tag "Current buffer" nil)
2491 (function) (variable) (file))
2492 (choice :tag "Identify target headline by"
2493 (cons :tag "Specific tag" (const :value :tag) (string))
2494 (cons :tag "TODO keyword" (const :value :todo) (string))
2495 (cons :tag "Regular expression" (const :value :regexp) (regexp))
2496 (cons :tag "Level number" (const :value :level) (integer))
2497 (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
2499 (defcustom org-refile-target-verify-function nil
2500 "Function to verify if the headline at point should be a refile target.
2501 The function will be called without arguments, with point at the
2502 beginning of the headline. It should return t and leave point
2503 where it is if the headline is a valid target for refiling.
2505 If the target should not be selected, the function must return nil.
2506 In addition to this, it may move point to a place from where the search
2507 should be continued. For example, the function may decide that the entire
2508 subtree of the current entry should be excluded and move point to the end
2509 of the subtree."
2510 :group 'org-refile
2511 :type '(choice
2512 (const nil)
2513 (function)))
2515 (defcustom org-refile-use-cache nil
2516 "Non-nil means cache refile targets to speed up the process.
2517 \\<org-mode-map>\
2518 The cache for a particular file will be updated automatically when
2519 the buffer has been killed, or when any of the marker used for flagging
2520 refile targets no longer points at a live buffer.
2521 If you have added new entries to a buffer that might themselves be targets,
2522 you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
2523 if you find that easier, \
2524 `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
2525 \\[org-refile]'."
2526 :group 'org-refile
2527 :version "24.1"
2528 :type 'boolean)
2530 (defcustom org-refile-use-outline-path nil
2531 "Non-nil means provide refile targets as paths.
2532 So a level 3 headline will be available as level1/level2/level3.
2534 When the value is `file', also include the file name (without directory)
2535 into the path. In this case, you can also stop the completion after
2536 the file name, to get entries inserted as top level in the file.
2538 When `full-file-path', include the full file path.
2540 When `buffer-name', use the buffer name."
2541 :group 'org-refile
2542 :type '(choice
2543 (const :tag "Not" nil)
2544 (const :tag "Yes" t)
2545 (const :tag "Start with file name" file)
2546 (const :tag "Start with full file path" full-file-path)
2547 (const :tag "Start with buffer name" buffer-name)))
2549 (defcustom org-outline-path-complete-in-steps t
2550 "Non-nil means complete the outline path in hierarchical steps.
2551 When Org uses the refile interface to select an outline path (see
2552 `org-refile-use-outline-path'), the completion of the path can be
2553 done in a single go, or it can be done in steps down the headline
2554 hierarchy. Going in steps is probably the best if you do not use
2555 a special completion package like `ido' or `icicles'. However,
2556 when using these packages, going in one step can be very fast,
2557 while still showing the whole path to the entry."
2558 :group 'org-refile
2559 :type 'boolean)
2561 (defcustom org-refile-allow-creating-parent-nodes nil
2562 "Non-nil means allow the creation of new nodes as refile targets.
2563 New nodes are then created by adding \"/new node name\" to the completion
2564 of an existing node. When the value of this variable is `confirm',
2565 new node creation must be confirmed by the user (recommended).
2566 When nil, the completion must match an existing entry.
2568 Note that, if the new heading is not seen by the criteria
2569 listed in `org-refile-targets', multiple instances of the same
2570 heading would be created by trying again to file under the new
2571 heading."
2572 :group 'org-refile
2573 :type '(choice
2574 (const :tag "Never" nil)
2575 (const :tag "Always" t)
2576 (const :tag "Prompt for confirmation" confirm)))
2578 (defcustom org-refile-active-region-within-subtree nil
2579 "Non-nil means also refile active region within a subtree.
2581 By default `org-refile' doesn't allow refiling regions if they
2582 don't contain a set of subtrees, but it might be convenient to
2583 do so sometimes: in that case, the first line of the region is
2584 converted to a headline before refiling."
2585 :group 'org-refile
2586 :version "24.1"
2587 :type 'boolean)
2589 (defgroup org-todo nil
2590 "Options concerning TODO items in Org mode."
2591 :tag "Org TODO"
2592 :group 'org)
2594 (defgroup org-progress nil
2595 "Options concerning Progress logging in Org mode."
2596 :tag "Org Progress"
2597 :group 'org-time)
2599 (defvar org-todo-interpretation-widgets
2600 '((:tag "Sequence (cycling hits every state)" sequence)
2601 (:tag "Type (cycling directly to DONE)" type))
2602 "The available interpretation symbols for customizing `org-todo-keywords'.
2603 Interested libraries should add to this list.")
2605 (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
2606 "List of TODO entry keyword sequences and their interpretation.
2607 \\<org-mode-map>This is a list of sequences.
2609 Each sequence starts with a symbol, either `sequence' or `type',
2610 indicating if the keywords should be interpreted as a sequence of
2611 action steps, or as different types of TODO items. The first
2612 keywords are states requiring action - these states will select a headline
2613 for inclusion into the global TODO list Org produces. If one of the
2614 \"keywords\" is the vertical bar, \"|\", the remaining keywords
2615 signify that no further action is necessary. If \"|\" is not found,
2616 the last keyword is treated as the only DONE state of the sequence.
2618 The command `\\[org-todo]' cycles an entry through these states, and one
2619 additional state where no keyword is present. For details about this
2620 cycling, see the manual.
2622 TODO keywords and interpretation can also be set on a per-file basis with
2623 the special #+SEQ_TODO and #+TYP_TODO lines.
2625 Each keyword can optionally specify a character for fast state selection
2626 \(in combination with the variable `org-use-fast-todo-selection')
2627 and specifiers for state change logging, using the same syntax that
2628 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2629 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2630 indicates to record a time stamp each time this state is selected.
2632 Each keyword may also specify if a timestamp or a note should be
2633 recorded when entering or leaving the state, by adding additional
2634 characters in the parenthesis after the keyword. This looks like this:
2635 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2636 record only the time of the state change. With X and Y being either
2637 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2638 Y when leaving the state if and only if the *target* state does not
2639 define X. You may omit any of the fast-selection key or X or /Y,
2640 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2642 For backward compatibility, this variable may also be just a list
2643 of keywords. In this case the interpretation (sequence or type) will be
2644 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2645 :group 'org-todo
2646 :group 'org-keywords
2647 :type '(choice
2648 (repeat :tag "Old syntax, just keywords"
2649 (string :tag "Keyword"))
2650 (repeat :tag "New syntax"
2651 (cons
2652 (choice
2653 :tag "Interpretation"
2654 ;;Quick and dirty way to see
2655 ;;`org-todo-interpretations'. This takes the
2656 ;;place of item arguments
2657 :convert-widget
2658 (lambda (widget)
2659 (widget-put widget
2660 :args (mapcar
2661 (lambda (x)
2662 (widget-convert
2663 (cons 'const x)))
2664 org-todo-interpretation-widgets))
2665 widget))
2666 (repeat
2667 (string :tag "Keyword"))))))
2669 (defvar-local org-todo-keywords-1 nil
2670 "All TODO and DONE keywords active in a buffer.")
2671 (defvar org-todo-keywords-for-agenda nil)
2672 (defvar org-done-keywords-for-agenda nil)
2673 (defvar org-todo-keyword-alist-for-agenda nil)
2674 (defvar org-tag-alist-for-agenda nil
2675 "Alist of all tags from all agenda files.")
2676 (defvar org-tag-groups-alist-for-agenda nil
2677 "Alist of all groups tags from all current agenda files.")
2678 (defvar-local org-tag-groups-alist nil)
2679 (defvar org-agenda-contributing-files nil)
2680 (defvar-local org-current-tag-alist nil
2681 "Alist of all tag groups in current buffer.
2682 This variable takes into consideration `org-tag-alist',
2683 `org-tag-persistent-alist' and TAGS keywords in the buffer.")
2684 (defvar-local org-not-done-keywords nil)
2685 (defvar-local org-done-keywords nil)
2686 (defvar-local org-todo-heads nil)
2687 (defvar-local org-todo-sets nil)
2688 (defvar-local org-todo-log-states nil)
2689 (defvar-local org-todo-kwd-alist nil)
2690 (defvar-local org-todo-key-alist nil)
2691 (defvar-local org-todo-key-trigger nil)
2693 (defcustom org-todo-interpretation 'sequence
2694 "Controls how TODO keywords are interpreted.
2695 This variable is in principle obsolete and is only used for
2696 backward compatibility, if the interpretation of todo keywords is
2697 not given already in `org-todo-keywords'. See that variable for
2698 more information."
2699 :group 'org-todo
2700 :group 'org-keywords
2701 :type '(choice (const sequence)
2702 (const type)))
2704 (defcustom org-use-fast-todo-selection t
2705 "\\<org-mode-map>\
2706 Non-nil means use the fast todo selection scheme with `\\[org-todo]'.
2707 This variable describes if and under what circumstances the cycling
2708 mechanism for TODO keywords will be replaced by a single-key, direct
2709 selection scheme.
2711 When nil, fast selection is never used.
2713 When the symbol `prefix', it will be used when `org-todo' is called
2714 with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
2715 in an Org buffer, and
2716 `\\[universal-argument] t' in an agenda buffer.
2718 When t, fast selection is used by default. In this case, the prefix
2719 argument forces cycling instead.
2721 In all cases, the special interface is only used if access keys have
2722 actually been assigned by the user, i.e. if keywords in the configuration
2723 are followed by a letter in parenthesis, like TODO(t)."
2724 :group 'org-todo
2725 :type '(choice
2726 (const :tag "Never" nil)
2727 (const :tag "By default" t)
2728 (const :tag "Only with C-u C-c C-t" prefix)))
2730 (defcustom org-provide-todo-statistics t
2731 "Non-nil means update todo statistics after insert and toggle.
2732 ALL-HEADLINES means update todo statistics by including headlines
2733 with no TODO keyword as well, counting them as not done.
2734 A list of TODO keywords means the same, but skip keywords that are
2735 not in this list.
2736 When set to a list of two lists, the first list contains keywords
2737 to consider as TODO keywords, the second list contains keywords
2738 to consider as DONE keywords.
2740 When this is set, todo statistics is updated in the parent of the
2741 current entry each time a todo state is changed."
2742 :group 'org-todo
2743 :type '(choice
2744 (const :tag "Yes, only for TODO entries" t)
2745 (const :tag "Yes, including all entries" all-headlines)
2746 (repeat :tag "Yes, for TODOs in this list"
2747 (string :tag "TODO keyword"))
2748 (list :tag "Yes, for TODOs and DONEs in these lists"
2749 (repeat (string :tag "TODO keyword"))
2750 (repeat (string :tag "DONE keyword")))
2751 (other :tag "No TODO statistics" nil)))
2753 (defcustom org-hierarchical-todo-statistics t
2754 "Non-nil means TODO statistics covers just direct children.
2755 When nil, all entries in the subtree are considered.
2756 This has only an effect if `org-provide-todo-statistics' is set.
2757 To set this to nil for only a single subtree, use a COOKIE_DATA
2758 property and include the word \"recursive\" into the value."
2759 :group 'org-todo
2760 :type 'boolean)
2762 (defcustom org-after-todo-state-change-hook nil
2763 "Hook which is run after the state of a TODO item was changed.
2764 The new state (a string with a TODO keyword, or nil) is available in the
2765 Lisp variable `org-state'."
2766 :group 'org-todo
2767 :type 'hook)
2769 (defvar org-blocker-hook nil
2770 "Hook for functions that are allowed to block a state change.
2772 Functions in this hook should not modify the buffer.
2773 Each function gets as its single argument a property list,
2774 see `org-trigger-hook' for more information about this list.
2776 If any of the functions in this hook returns nil, the state change
2777 is blocked.")
2779 (defvar org-trigger-hook nil
2780 "Hook for functions that are triggered by a state change.
2782 Each function gets as its single argument a property list with at
2783 least the following elements:
2785 (:type type-of-change :position pos-at-entry-start
2786 :from old-state :to new-state)
2788 Depending on the type, more properties may be present.
2790 This mechanism is currently implemented for:
2792 TODO state changes
2793 ------------------
2794 :type todo-state-change
2795 :from previous state (keyword as a string), or nil, or a symbol
2796 `todo' or `done', to indicate the general type of state.
2797 :to new state, like in :from")
2799 (defcustom org-enforce-todo-dependencies nil
2800 "Non-nil means undone TODO entries will block switching the parent to DONE.
2801 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2802 be blocked if any prior sibling is not yet done.
2803 Finally, if the parent is blocked because of ordered siblings of its own,
2804 the child will also be blocked."
2805 :set (lambda (var val)
2806 (set var val)
2807 (if val
2808 (add-hook 'org-blocker-hook
2809 'org-block-todo-from-children-or-siblings-or-parent)
2810 (remove-hook 'org-blocker-hook
2811 'org-block-todo-from-children-or-siblings-or-parent)))
2812 :group 'org-todo
2813 :type 'boolean)
2815 (defcustom org-enforce-todo-checkbox-dependencies nil
2816 "Non-nil means unchecked boxes will block switching the parent to DONE.
2817 When this is nil, checkboxes have no influence on switching TODO states.
2818 When non-nil, you first need to check off all check boxes before the TODO
2819 entry can be switched to DONE.
2820 This variable needs to be set before org.el is loaded, and you need to
2821 restart Emacs after a change to make the change effective. The only way
2822 to change is while Emacs is running is through the customize interface."
2823 :set (lambda (var val)
2824 (set var val)
2825 (if val
2826 (add-hook 'org-blocker-hook
2827 'org-block-todo-from-checkboxes)
2828 (remove-hook 'org-blocker-hook
2829 'org-block-todo-from-checkboxes)))
2830 :group 'org-todo
2831 :type 'boolean)
2833 (defcustom org-treat-insert-todo-heading-as-state-change nil
2834 "Non-nil means inserting a TODO heading is treated as state change.
2835 So when the command `\\[org-insert-todo-heading]' is used, state change
2836 logging will apply if appropriate. When nil, the new TODO item will
2837 be inserted directly, and no logging will take place."
2838 :group 'org-todo
2839 :type 'boolean)
2841 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2842 "Non-nil means switching TODO states with S-cursor counts as state change.
2843 This is the default behavior. However, setting this to nil allows a
2844 convenient way to select a TODO state and bypass any logging associated
2845 with that."
2846 :group 'org-todo
2847 :type 'boolean)
2849 (defcustom org-todo-state-tags-triggers nil
2850 "Tag changes that should be triggered by TODO state changes.
2851 This is a list. Each entry is
2853 (state-change (tag . flag) .......)
2855 State-change can be a string with a state, and empty string to indicate the
2856 state that has no TODO keyword, or it can be one of the symbols `todo'
2857 or `done', meaning any not-done or done state, respectively."
2858 :group 'org-todo
2859 :group 'org-tags
2860 :type '(repeat
2861 (cons (choice :tag "When changing to"
2862 (const :tag "Not-done state" todo)
2863 (const :tag "Done state" done)
2864 (string :tag "State"))
2865 (repeat
2866 (cons :tag "Tag action"
2867 (string :tag "Tag")
2868 (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
2870 (defcustom org-log-done nil
2871 "Information to record when a task moves to the DONE state.
2873 Possible values are:
2875 nil Don't add anything, just change the keyword
2876 time Add a time stamp to the task
2877 note Prompt for a note and add it with template `org-log-note-headings'
2879 This option can also be set with on a per-file-basis with
2881 #+STARTUP: nologdone
2882 #+STARTUP: logdone
2883 #+STARTUP: lognotedone
2885 You can have local logging settings for a subtree by setting the LOGGING
2886 property to one or more of these keywords."
2887 :group 'org-todo
2888 :group 'org-progress
2889 :type '(choice
2890 (const :tag "No logging" nil)
2891 (const :tag "Record CLOSED timestamp" time)
2892 (const :tag "Record CLOSED timestamp with note." note)))
2894 ;; Normalize old uses of org-log-done.
2895 (cond
2896 ((eq org-log-done t) (setq org-log-done 'time))
2897 ((and (listp org-log-done) (memq 'done org-log-done))
2898 (setq org-log-done 'note)))
2900 (defcustom org-log-reschedule nil
2901 "Information to record when the scheduling date of a tasks is modified.
2903 Possible values are:
2905 nil Don't add anything, just change the date
2906 time Add a time stamp to the task
2907 note Prompt for a note and add it with template `org-log-note-headings'
2909 This option can also be set with on a per-file-basis with
2911 #+STARTUP: nologreschedule
2912 #+STARTUP: logreschedule
2913 #+STARTUP: lognotereschedule"
2914 :group 'org-todo
2915 :group 'org-progress
2916 :type '(choice
2917 (const :tag "No logging" nil)
2918 (const :tag "Record timestamp" time)
2919 (const :tag "Record timestamp with note." note)))
2921 (defcustom org-log-redeadline nil
2922 "Information to record when the deadline date of a tasks is modified.
2924 Possible values are:
2926 nil Don't add anything, just change the date
2927 time Add a time stamp to the task
2928 note Prompt for a note and add it with template `org-log-note-headings'
2930 This option can also be set with on a per-file-basis with
2932 #+STARTUP: nologredeadline
2933 #+STARTUP: logredeadline
2934 #+STARTUP: lognoteredeadline
2936 You can have local logging settings for a subtree by setting the LOGGING
2937 property to one or more of these keywords."
2938 :group 'org-todo
2939 :group 'org-progress
2940 :type '(choice
2941 (const :tag "No logging" nil)
2942 (const :tag "Record timestamp" time)
2943 (const :tag "Record timestamp with note." note)))
2945 (defcustom org-log-note-clock-out nil
2946 "Non-nil means record a note when clocking out of an item.
2947 This can also be configured on a per-file basis by adding one of
2948 the following lines anywhere in the buffer:
2950 #+STARTUP: lognoteclock-out
2951 #+STARTUP: nolognoteclock-out"
2952 :group 'org-todo
2953 :group 'org-progress
2954 :type 'boolean)
2956 (defcustom org-log-done-with-time t
2957 "Non-nil means the CLOSED time stamp will contain date and time.
2958 When nil, only the date will be recorded."
2959 :group 'org-progress
2960 :type 'boolean)
2962 (defcustom org-log-note-headings
2963 '((done . "CLOSING NOTE %t")
2964 (state . "State %-12s from %-12S %t")
2965 (note . "Note taken on %t")
2966 (reschedule . "Rescheduled from %S on %t")
2967 (delschedule . "Not scheduled, was %S on %t")
2968 (redeadline . "New deadline from %S on %t")
2969 (deldeadline . "Removed deadline, was %S on %t")
2970 (refile . "Refiled on %t")
2971 (clock-out . ""))
2972 "Headings for notes added to entries.
2974 The value is an alist, with the car being a symbol indicating the
2975 note context, and the cdr is the heading to be used. The heading
2976 may also be the empty string. The following placeholders can be
2977 used:
2979 %t a time stamp.
2980 %T an active time stamp instead the default inactive one
2981 %d a short-format time stamp.
2982 %D an active short-format time stamp.
2983 %s the new TODO state or time stamp (inactive), in double quotes.
2984 %S the old TODO state or time stamp (inactive), in double quotes.
2985 %u the user name.
2986 %U full user name.
2988 In fact, it is not a good idea to change the `state' entry,
2989 because Agenda Log mode depends on the format of these entries."
2990 :group 'org-todo
2991 :group 'org-progress
2992 :type '(list :greedy t
2993 (cons (const :tag "Heading when closing an item" done) string)
2994 (cons (const :tag
2995 "Heading when changing todo state (todo sequence only)"
2996 state) string)
2997 (cons (const :tag "Heading when just taking a note" note) string)
2998 (cons (const :tag "Heading when rescheduling" reschedule) string)
2999 (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
3000 (cons (const :tag "Heading when changing deadline" redeadline) string)
3001 (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
3002 (cons (const :tag "Heading when refiling" refile) string)
3003 (cons (const :tag "Heading when clocking out" clock-out) string)))
3005 (unless (assq 'note org-log-note-headings)
3006 (push '(note . "%t") org-log-note-headings))
3008 (defcustom org-log-into-drawer nil
3009 "Non-nil means insert state change notes and time stamps into a drawer.
3010 When nil, state changes notes will be inserted after the headline and
3011 any scheduling and clock lines, but not inside a drawer.
3013 The value of this variable should be the name of the drawer to use.
3014 LOGBOOK is proposed as the default drawer for this purpose, you can
3015 also set this to a string to define the drawer of your choice.
3017 A value of t is also allowed, representing \"LOGBOOK\".
3019 A value of t or nil can also be set with on a per-file-basis with
3021 #+STARTUP: logdrawer
3022 #+STARTUP: nologdrawer
3024 If this variable is set, `org-log-state-notes-insert-after-drawers'
3025 will be ignored.
3027 You can set the property LOG_INTO_DRAWER to overrule this setting for
3028 a subtree.
3030 Do not check directly this variable in a Lisp program. Call
3031 function `org-log-into-drawer' instead."
3032 :group 'org-todo
3033 :group 'org-progress
3034 :type '(choice
3035 (const :tag "Not into a drawer" nil)
3036 (const :tag "LOGBOOK" t)
3037 (string :tag "Other")))
3039 (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
3041 (defun org-log-into-drawer ()
3042 "Name of the log drawer, as a string, or nil.
3043 This is the value of `org-log-into-drawer'. However, if the
3044 current entry has or inherits a LOG_INTO_DRAWER property, it will
3045 be used instead of the default value."
3046 (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
3047 (cond ((equal p "nil") nil)
3048 ((equal p "t") "LOGBOOK")
3049 ((stringp p) p)
3050 (p "LOGBOOK")
3051 ((stringp org-log-into-drawer) org-log-into-drawer)
3052 (org-log-into-drawer "LOGBOOK"))))
3054 (defcustom org-log-state-notes-insert-after-drawers nil
3055 "Non-nil means insert state change notes after any drawers in entry.
3056 Only the drawers that *immediately* follow the headline and the
3057 deadline/scheduled line are skipped.
3058 When nil, insert notes right after the heading and perhaps the line
3059 with deadline/scheduling if present.
3061 This variable will have no effect if `org-log-into-drawer' is
3062 set."
3063 :group 'org-todo
3064 :group 'org-progress
3065 :type 'boolean)
3067 (defcustom org-log-states-order-reversed t
3068 "Non-nil means the latest state note will be directly after heading.
3069 When nil, the state change notes will be ordered according to time.
3071 This option can also be set with on a per-file-basis with
3073 #+STARTUP: logstatesreversed
3074 #+STARTUP: nologstatesreversed"
3075 :group 'org-todo
3076 :group 'org-progress
3077 :type 'boolean)
3079 (defcustom org-todo-repeat-to-state nil
3080 "The TODO state to which a repeater should return the repeating task.
3081 By default this is the first task in a TODO sequence, or the previous state
3082 in a TODO_TYP set. But you can specify another task here.
3083 alternatively, set the :REPEAT_TO_STATE: property of the entry."
3084 :group 'org-todo
3085 :version "24.1"
3086 :type '(choice (const :tag "Head of sequence" nil)
3087 (string :tag "Specific state")))
3089 (defcustom org-log-repeat 'time
3090 "Non-nil means record moving through the DONE state when triggering repeat.
3091 An auto-repeating task is immediately switched back to TODO when
3092 marked DONE. If you are not logging state changes (by adding \"@\"
3093 or \"!\" to the TODO keyword definition), or set `org-log-done' to
3094 record a closing note, there will be no record of the task moving
3095 through DONE. This variable forces taking a note anyway.
3097 nil Don't force a record
3098 time Record a time stamp
3099 note Prompt for a note and add it with template `org-log-note-headings'
3101 This option can also be set with on a per-file-basis with
3103 #+STARTUP: nologrepeat
3104 #+STARTUP: logrepeat
3105 #+STARTUP: lognoterepeat
3107 You can have local logging settings for a subtree by setting the LOGGING
3108 property to one or more of these keywords."
3109 :group 'org-todo
3110 :group 'org-progress
3111 :type '(choice
3112 (const :tag "Don't force a record" nil)
3113 (const :tag "Force recording the DONE state" time)
3114 (const :tag "Force recording a note with the DONE state" note)))
3117 (defgroup org-priorities nil
3118 "Priorities in Org mode."
3119 :tag "Org Priorities"
3120 :group 'org-todo)
3122 (defcustom org-enable-priority-commands t
3123 "Non-nil means priority commands are active.
3124 When nil, these commands will be disabled, so that you never accidentally
3125 set a priority."
3126 :group 'org-priorities
3127 :type 'boolean)
3129 (defcustom org-highest-priority ?A
3130 "The highest priority of TODO items. A character like ?A, ?B etc.
3131 Must have a smaller ASCII number than `org-lowest-priority'."
3132 :group 'org-priorities
3133 :type 'character)
3135 (defcustom org-lowest-priority ?C
3136 "The lowest priority of TODO items. A character like ?A, ?B etc.
3137 Must have a larger ASCII number than `org-highest-priority'."
3138 :group 'org-priorities
3139 :type 'character)
3141 (defcustom org-default-priority ?B
3142 "The default priority of TODO items.
3143 This is the priority an item gets if no explicit priority is given.
3144 When starting to cycle on an empty priority the first step in the cycle
3145 depends on `org-priority-start-cycle-with-default'. The resulting first
3146 step priority must not exceed the range from `org-highest-priority' to
3147 `org-lowest-priority' which means that `org-default-priority' has to be
3148 in this range exclusive or inclusive the range boundaries. Else the
3149 first step refuses to set the default and the second will fall back
3150 to (depending on the command used) the highest or lowest priority."
3151 :group 'org-priorities
3152 :type 'character)
3154 (defcustom org-priority-start-cycle-with-default t
3155 "Non-nil means start with default priority when starting to cycle.
3156 When this is nil, the first step in the cycle will be (depending on the
3157 command used) one higher or lower than the default priority.
3158 See also `org-default-priority'."
3159 :group 'org-priorities
3160 :type 'boolean)
3162 (defcustom org-get-priority-function nil
3163 "Function to extract the priority from a string.
3164 The string is normally the headline. If this is nil Org computes the
3165 priority from the priority cookie like [#A] in the headline. It returns
3166 an integer, increasing by 1000 for each priority level.
3167 The user can set a different function here, which should take a string
3168 as an argument and return the numeric priority."
3169 :group 'org-priorities
3170 :version "24.1"
3171 :type '(choice
3172 (const nil)
3173 (function)))
3175 (defgroup org-time nil
3176 "Options concerning time stamps and deadlines in Org mode."
3177 :tag "Org Time"
3178 :group 'org)
3180 (defcustom org-time-stamp-rounding-minutes '(0 5)
3181 "Number of minutes to round time stamps to.
3182 \\<org-mode-map>\
3183 These are two values, the first applies when first creating a time stamp.
3184 The second applies when changing it with the commands `S-up' and `S-down'.
3185 When changing the time stamp, this means that it will change in steps
3186 of N minutes, as given by the second value.
3188 When a setting is 0 or 1, insert the time unmodified. Useful rounding
3189 numbers should be factors of 60, so for example 5, 10, 15.
3191 When this is larger than 1, you can still force an exact time stamp by using
3192 a double prefix argument to a time stamp command like \
3193 `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
3194 and by using a prefix arg to `S-up/down' to specify the exact number
3195 of minutes to shift."
3196 :group 'org-time
3197 :get (lambda (var) ; Make sure both elements are there
3198 (if (integerp (default-value var))
3199 (list (default-value var) 5)
3200 (default-value var)))
3201 :type '(list
3202 (integer :tag "when inserting times")
3203 (integer :tag "when modifying times")))
3205 ;; Normalize old customizations of this variable.
3206 (when (integerp org-time-stamp-rounding-minutes)
3207 (setq org-time-stamp-rounding-minutes
3208 (list org-time-stamp-rounding-minutes
3209 org-time-stamp-rounding-minutes)))
3211 (defcustom org-display-custom-times nil
3212 "Non-nil means overlay custom formats over all time stamps.
3213 The formats are defined through the variable `org-time-stamp-custom-formats'.
3214 To turn this on on a per-file basis, insert anywhere in the file:
3215 #+STARTUP: customtime"
3216 :group 'org-time
3217 :set 'set-default
3218 :type 'sexp)
3219 (make-variable-buffer-local 'org-display-custom-times)
3221 (defcustom org-time-stamp-custom-formats
3222 '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
3223 "Custom formats for time stamps. See `format-time-string' for the syntax.
3224 These are overlaid over the default ISO format if the variable
3225 `org-display-custom-times' is set. Time like %H:%M should be at the
3226 end of the second format. The custom formats are also honored by export
3227 commands, if custom time display is turned on at the time of export."
3228 :group 'org-time
3229 :type 'sexp)
3231 (defun org-time-stamp-format (&optional long inactive)
3232 "Get the right format for a time string."
3233 (let ((f (if long (cdr org-time-stamp-formats)
3234 (car org-time-stamp-formats))))
3235 (if inactive
3236 (concat "[" (substring f 1 -1) "]")
3237 f)))
3239 (defcustom org-deadline-warning-days 14
3240 "Number of days before expiration during which a deadline becomes active.
3241 This variable governs the display in sparse trees and in the agenda.
3242 When 0 or negative, it means use this number (the absolute value of it)
3243 even if a deadline has a different individual lead time specified.
3245 Custom commands can set this variable in the options section."
3246 :group 'org-time
3247 :group 'org-agenda-daily/weekly
3248 :type 'integer)
3250 (defcustom org-scheduled-delay-days 0
3251 "Number of days before a scheduled item becomes active.
3252 This variable governs the display in sparse trees and in the agenda.
3253 The default value (i.e. 0) means: don't delay scheduled item.
3254 When negative, it means use this number (the absolute value of it)
3255 even if a scheduled item has a different individual delay time
3256 specified.
3258 Custom commands can set this variable in the options section."
3259 :group 'org-time
3260 :group 'org-agenda-daily/weekly
3261 :version "24.4"
3262 :package-version '(Org . "8.0")
3263 :type 'integer)
3265 (defcustom org-read-date-prefer-future t
3266 "Non-nil means assume future for incomplete date input from user.
3267 This affects the following situations:
3268 1. The user gives a month but not a year.
3269 For example, if it is April and you enter \"feb 2\", this will be read
3270 as Feb 2, *next* year. \"May 5\", however, will be this year.
3271 2. The user gives a day, but no month.
3272 For example, if today is the 15th, and you enter \"3\", Org will read
3273 this as the third of *next* month. However, if you enter \"17\",
3274 it will be considered as *this* month.
3276 If you set this variable to the symbol `time', then also the following
3277 will work:
3279 3. If the user gives a time.
3280 If the time is before now, it will be interpreted as tomorrow.
3282 Currently none of this works for ISO week specifications.
3284 When this option is nil, the current day, month and year will always be
3285 used as defaults.
3287 See also `org-agenda-jump-prefer-future'."
3288 :group 'org-time
3289 :type '(choice
3290 (const :tag "Never" nil)
3291 (const :tag "Check month and day" t)
3292 (const :tag "Check month, day, and time" time)))
3294 (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
3295 "Should the agenda jump command prefer the future for incomplete dates?
3296 The default is to do the same as configured in `org-read-date-prefer-future'.
3297 But you can also set a deviating value here.
3298 This may t or nil, or the symbol `org-read-date-prefer-future'."
3299 :group 'org-agenda
3300 :group 'org-time
3301 :version "24.1"
3302 :type '(choice
3303 (const :tag "Use org-read-date-prefer-future"
3304 org-read-date-prefer-future)
3305 (const :tag "Never" nil)
3306 (const :tag "Always" t)))
3308 (defcustom org-read-date-force-compatible-dates t
3309 "Should date/time prompt force dates that are guaranteed to work in Emacs?
3311 Depending on the system Emacs is running on, certain dates cannot
3312 be represented with the type used internally to represent time.
3313 Dates between 1970-1-1 and 2038-1-1 can always be represented
3314 correctly. Some systems allow for earlier dates, some for later,
3315 some for both. One way to find out it to insert any date into an
3316 Org buffer, putting the cursor on the year and hitting S-up and
3317 S-down to test the range.
3319 When this variable is set to t, the date/time prompt will not let
3320 you specify dates outside the 1970-2037 range, so it is certain that
3321 these dates will work in whatever version of Emacs you are
3322 running, and also that you can move a file from one Emacs implementation
3323 to another. WHenever Org is forcing the year for you, it will display
3324 a message and beep.
3326 When this variable is nil, Org will check if the date is
3327 representable in the specific Emacs implementation you are using.
3328 If not, it will force a year, usually the current year, and beep
3329 to remind you. Currently this setting is not recommended because
3330 the likelihood that you will open your Org files in an Emacs that
3331 has limited date range is not negligible.
3333 A workaround for this problem is to use diary sexp dates for time
3334 stamps outside of this range."
3335 :group 'org-time
3336 :version "24.1"
3337 :type 'boolean)
3339 (defcustom org-read-date-display-live t
3340 "Non-nil means display current interpretation of date prompt live.
3341 This display will be in an overlay, in the minibuffer."
3342 :group 'org-time
3343 :type 'boolean)
3345 (defcustom org-read-date-popup-calendar t
3346 "Non-nil means pop up a calendar when prompting for a date.
3347 In the calendar, the date can be selected with mouse-1. However, the
3348 minibuffer will also be active, and you can simply enter the date as well.
3349 When nil, only the minibuffer will be available."
3350 :group 'org-time
3351 :type 'boolean)
3352 (defvaralias 'org-popup-calendar-for-date-prompt
3353 'org-read-date-popup-calendar)
3355 (defcustom org-extend-today-until 0
3356 "The hour when your day really ends. Must be an integer.
3357 This has influence for the following applications:
3358 - When switching the agenda to \"today\". It it is still earlier than
3359 the time given here, the day recognized as TODAY is actually yesterday.
3360 - When a date is read from the user and it is still before the time given
3361 here, the current date and time will be assumed to be yesterday, 23:59.
3362 Also, timestamps inserted in capture templates follow this rule.
3364 IMPORTANT: This is a feature whose implementation is and likely will
3365 remain incomplete. Really, it is only here because past midnight seems to
3366 be the favorite working time of John Wiegley :-)"
3367 :group 'org-time
3368 :type 'integer)
3370 (defcustom org-use-effective-time nil
3371 "If non-nil, consider `org-extend-today-until' when creating timestamps.
3372 For example, if `org-extend-today-until' is 8, and it's 4am, then the
3373 \"effective time\" of any timestamps between midnight and 8am will be
3374 23:59 of the previous day."
3375 :group 'org-time
3376 :version "24.1"
3377 :type 'boolean)
3379 (defcustom org-use-last-clock-out-time-as-effective-time nil
3380 "When non-nil, use the last clock out time for `org-todo'.
3381 Note that this option has precedence over the combined use of
3382 `org-use-effective-time' and `org-extend-today-until'."
3383 :group 'org-time
3384 :version "24.4"
3385 :package-version '(Org . "8.0")
3386 :type 'boolean)
3388 (defcustom org-edit-timestamp-down-means-later nil
3389 "Non-nil means S-down will increase the time in a time stamp.
3390 When nil, S-up will increase."
3391 :group 'org-time
3392 :type 'boolean)
3394 (defcustom org-calendar-follow-timestamp-change t
3395 "Non-nil means make the calendar window follow timestamp changes.
3396 When a timestamp is modified and the calendar window is visible, it will be
3397 moved to the new date."
3398 :group 'org-time
3399 :type 'boolean)
3401 (defgroup org-tags nil
3402 "Options concerning tags in Org mode."
3403 :tag "Org Tags"
3404 :group 'org)
3406 (defcustom org-tag-alist nil
3407 "Default tags available in Org files.
3409 The value of this variable is an alist. Associations either:
3411 (TAG)
3412 (TAG . SELECT)
3413 (SPECIAL)
3415 where TAG is a tag as a string, SELECT is character, used to
3416 select that tag through the fast tag selection interface, and
3417 SPECIAL is one of the following keywords: `:startgroup',
3418 `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
3419 `:newline'. These keywords are used to define a hierarchy of
3420 tags. See manual for details.
3422 When this variable is nil, Org mode bases tag input on what is
3423 already in the buffer. The value can be overridden locally by
3424 using a TAGS keyword, e.g.,
3426 #+TAGS: tag1 tag2
3428 See also `org-tag-persistent-alist' to sidestep this behavior."
3429 :group 'org-tags
3430 :type '(repeat
3431 (choice
3432 (cons :tag "Tag with key"
3433 (string :tag "Tag name")
3434 (character :tag "Access char"))
3435 (list :tag "Tag" (string :tag "Tag name"))
3436 (const :tag "Start radio group" (:startgroup))
3437 (const :tag "Start tag group, non distinct" (:startgrouptag))
3438 (const :tag "Group tags delimiter" (:grouptags))
3439 (const :tag "End radio group" (:endgroup))
3440 (const :tag "End tag group, non distinct" (:endgrouptag))
3441 (const :tag "New line" (:newline)))))
3443 (defcustom org-tag-persistent-alist nil
3444 "Tags always available in Org files.
3446 The value of this variable is an alist. Associations either:
3448 (TAG)
3449 (TAG . SELECT)
3450 (SPECIAL)
3452 where TAG is a tag as a string, SELECT is a character, used to
3453 select that tag through the fast tag selection interface, and
3454 SPECIAL is one of the following keywords: `:startgroup',
3455 `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
3456 `:newline'. These keywords are used to define a hierarchy of
3457 tags. See manual for details.
3459 Unlike to `org-tag-alist', tags defined in this variable do not
3460 depend on a local TAGS keyword. Instead, to disable these tags
3461 on a per-file basis, insert anywhere in the file:
3463 #+STARTUP: noptag"
3464 :group 'org-tags
3465 :type '(repeat
3466 (choice
3467 (cons :tag "Tag with key"
3468 (string :tag "Tag name")
3469 (character :tag "Access char"))
3470 (list :tag "Tag" (string :tag "Tag name"))
3471 (const :tag "Start radio group" (:startgroup))
3472 (const :tag "Start tag group, non distinct" (:startgrouptag))
3473 (const :tag "Group tags delimiter" (:grouptags))
3474 (const :tag "End radio group" (:endgroup))
3475 (const :tag "End tag group, non distinct" (:endgrouptag))
3476 (const :tag "New line" (:newline)))))
3478 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
3479 "If non-nil, always offer completion for all tags of all agenda files.
3480 Instead of customizing this variable directly, you might want to
3481 set it locally for capture buffers, because there no list of
3482 tags in that file can be created dynamically (there are none).
3484 (add-hook \\='org-capture-mode-hook
3485 (lambda ()
3486 (setq-local org-complete-tags-always-offer-all-agenda-tags t)))"
3487 :group 'org-tags
3488 :version "24.1"
3489 :type 'boolean)
3491 (defvar org-file-tags nil
3492 "List of tags that can be inherited by all entries in the file.
3493 The tags will be inherited if the variable `org-use-tag-inheritance'
3494 says they should be.
3495 This variable is populated from #+FILETAGS lines.")
3497 (defcustom org-use-fast-tag-selection 'auto
3498 "Non-nil means use fast tag selection scheme.
3499 This is a special interface to select and deselect tags with single keys.
3500 When nil, fast selection is never used.
3501 When the symbol `auto', fast selection is used if and only if selection
3502 characters for tags have been configured, either through the variable
3503 `org-tag-alist' or through a #+TAGS line in the buffer.
3504 When t, fast selection is always used and selection keys are assigned
3505 automatically if necessary."
3506 :group 'org-tags
3507 :type '(choice
3508 (const :tag "Always" t)
3509 (const :tag "Never" nil)
3510 (const :tag "When selection characters are configured" auto)))
3512 (defcustom org-fast-tag-selection-single-key nil
3513 "Non-nil means fast tag selection exits after first change.
3514 When nil, you have to press RET to exit it.
3515 During fast tag selection, you can toggle this flag with `C-c'.
3516 This variable can also have the value `expert'. In this case, the window
3517 displaying the tags menu is not even shown, until you press C-c again."
3518 :group 'org-tags
3519 :type '(choice
3520 (const :tag "No" nil)
3521 (const :tag "Yes" t)
3522 (const :tag "Expert" expert)))
3524 (defvar org-fast-tag-selection-include-todo nil
3525 "Non-nil means fast tags selection interface will also offer TODO states.
3526 This is an undocumented feature, you should not rely on it.")
3528 (defcustom org-tags-column -77
3529 "The column to which tags should be indented in a headline.
3530 If this number is positive, it specifies the column. If it is negative,
3531 it means that the tags should be flushright to that column. For example,
3532 -80 works well for a normal 80 character screen.
3533 When 0, place tags directly after headline text, with only one space in
3534 between."
3535 :group 'org-tags
3536 :type 'integer)
3538 (defcustom org-auto-align-tags t
3539 "Non-nil keeps tags aligned when modifying headlines.
3540 Some operations (i.e. demoting) change the length of a headline and
3541 therefore shift the tags around. With this option turned on, after
3542 each such operation the tags are again aligned to `org-tags-column'."
3543 :group 'org-tags
3544 :type 'boolean)
3546 (defcustom org-use-tag-inheritance t
3547 "Non-nil means tags in levels apply also for sublevels.
3548 When nil, only the tags directly given in a specific line apply there.
3549 This may also be a list of tags that should be inherited, or a regexp that
3550 matches tags that should be inherited. Additional control is possible
3551 with the variable `org-tags-exclude-from-inheritance' which gives an
3552 explicit list of tags to be excluded from inheritance, even if the value of
3553 `org-use-tag-inheritance' would select it for inheritance.
3555 If this option is t, a match early-on in a tree can lead to a large
3556 number of matches in the subtree when constructing the agenda or creating
3557 a sparse tree. If you only want to see the first match in a tree during
3558 a search, check out the variable `org-tags-match-list-sublevels'."
3559 :group 'org-tags
3560 :type '(choice
3561 (const :tag "Not" nil)
3562 (const :tag "Always" t)
3563 (repeat :tag "Specific tags" (string :tag "Tag"))
3564 (regexp :tag "Tags matched by regexp")))
3566 (defcustom org-tags-exclude-from-inheritance nil
3567 "List of tags that should never be inherited.
3568 This is a way to exclude a few tags from inheritance. For way to do
3569 the opposite, to actively allow inheritance for selected tags,
3570 see the variable `org-use-tag-inheritance'."
3571 :group 'org-tags
3572 :type '(repeat (string :tag "Tag")))
3574 (defun org-tag-inherit-p (tag)
3575 "Check if TAG is one that should be inherited."
3576 (cond
3577 ((member tag org-tags-exclude-from-inheritance) nil)
3578 ((eq org-use-tag-inheritance t) t)
3579 ((not org-use-tag-inheritance) nil)
3580 ((stringp org-use-tag-inheritance)
3581 (string-match org-use-tag-inheritance tag))
3582 ((listp org-use-tag-inheritance)
3583 (member tag org-use-tag-inheritance))
3584 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3586 (defcustom org-tags-match-list-sublevels t
3587 "Non-nil means list also sublevels of headlines matching a search.
3588 This variable applies to tags/property searches, and also to stuck
3589 projects because this search is based on a tags match as well.
3591 When set to the symbol `indented', sublevels are indented with
3592 leading dots.
3594 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3595 the sublevels of a headline matching a tag search often also match
3596 the same search. Listing all of them can create very long lists.
3597 Setting this variable to nil causes subtrees of a match to be skipped.
3599 This variable is semi-obsolete and probably should always be true. It
3600 is better to limit inheritance to certain tags using the variables
3601 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3602 :group 'org-tags
3603 :type '(choice
3604 (const :tag "No, don't list them" nil)
3605 (const :tag "Yes, do list them" t)
3606 (const :tag "List them, indented with leading dots" indented)))
3608 (defcustom org-tags-sort-function nil
3609 "When set, tags are sorted using this function as a comparator."
3610 :group 'org-tags
3611 :type '(choice
3612 (const :tag "No sorting" nil)
3613 (const :tag "Alphabetical" string<)
3614 (const :tag "Reverse alphabetical" string>)
3615 (function :tag "Custom function" nil)))
3617 (defvar org-tags-history nil
3618 "History of minibuffer reads for tags.")
3619 (defvar org-last-tags-completion-table nil
3620 "The last used completion table for tags.")
3621 (defvar org-after-tags-change-hook nil
3622 "Hook that is run after the tags in a line have changed.")
3624 (defgroup org-properties nil
3625 "Options concerning properties in Org mode."
3626 :tag "Org Properties"
3627 :group 'org)
3629 (defcustom org-property-format "%-10s %s"
3630 "How property key/value pairs should be formatted by `indent-line'.
3631 When `indent-line' hits a property definition, it will format the line
3632 according to this format, mainly to make sure that the values are
3633 lined-up with respect to each other."
3634 :group 'org-properties
3635 :type 'string)
3637 (defcustom org-properties-postprocess-alist nil
3638 "Alist of properties and functions to adjust inserted values.
3639 Elements of this alist must be of the form
3641 ([string] [function])
3643 where [string] must be a property name and [function] must be a
3644 lambda expression: this lambda expression must take one argument,
3645 the value to adjust, and return the new value as a string.
3647 For example, this element will allow the property \"Remaining\"
3648 to be updated wrt the relation between the \"Effort\" property
3649 and the clock summary:
3651 ((\"Remaining\" (lambda(value)
3652 (let ((clocksum (org-clock-sum-current-item))
3653 (effort (org-duration-to-minutes
3654 (org-entry-get (point) \"Effort\"))))
3655 (org-minutes-to-clocksum-string (- effort clocksum))))))"
3656 :group 'org-properties
3657 :version "24.1"
3658 :type '(alist :key-type (string :tag "Property")
3659 :value-type (function :tag "Function")))
3661 (defcustom org-use-property-inheritance nil
3662 "Non-nil means properties apply also for sublevels.
3664 This setting is chiefly used during property searches. Turning it on can
3665 cause significant overhead when doing a search, which is why it is not
3666 on by default.
3668 When nil, only the properties directly given in the current entry count.
3669 When t, every property is inherited. The value may also be a list of
3670 properties that should have inheritance, or a regular expression matching
3671 properties that should be inherited.
3673 However, note that some special properties use inheritance under special
3674 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3675 and the properties ending in \"_ALL\" when they are used as descriptor
3676 for valid values of a property.
3678 Note for programmers:
3679 When querying an entry with `org-entry-get', you can control if inheritance
3680 should be used. By default, `org-entry-get' looks only at the local
3681 properties. You can request inheritance by setting the inherit argument
3682 to t (to force inheritance) or to `selective' (to respect the setting
3683 in this variable)."
3684 :group 'org-properties
3685 :type '(choice
3686 (const :tag "Not" nil)
3687 (const :tag "Always" t)
3688 (repeat :tag "Specific properties" (string :tag "Property"))
3689 (regexp :tag "Properties matched by regexp")))
3691 (defun org-property-inherit-p (property)
3692 "Return a non-nil value if PROPERTY should be inherited."
3693 (cond
3694 ((eq org-use-property-inheritance t) t)
3695 ((not org-use-property-inheritance) nil)
3696 ((stringp org-use-property-inheritance)
3697 (string-match org-use-property-inheritance property))
3698 ((listp org-use-property-inheritance)
3699 (member-ignore-case property org-use-property-inheritance))
3700 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3702 (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
3703 "The default column format, if no other format has been defined.
3704 This variable can be set on the per-file basis by inserting a line
3706 #+COLUMNS: %25ITEM ....."
3707 :group 'org-properties
3708 :type 'string)
3710 (defcustom org-columns-ellipses ".."
3711 "The ellipses to be used when a field in column view is truncated.
3712 When this is the empty string, as many characters as possible are shown,
3713 but then there will be no visual indication that the field has been truncated.
3714 When this is a string of length N, the last N characters of a truncated
3715 field are replaced by this string. If the column is narrower than the
3716 ellipses string, only part of the ellipses string will be shown."
3717 :group 'org-properties
3718 :type 'string)
3720 (defconst org-global-properties-fixed
3721 '(("VISIBILITY_ALL" . "folded children content all")
3722 ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
3723 "List of property/value pairs that can be inherited by any entry.
3725 These are fixed values, for the preset properties. The user variable
3726 that can be used to add to this list is `org-global-properties'.
3728 The entries in this list are cons cells where the car is a property
3729 name and cdr is a string with the value. If the value represents
3730 multiple items like an \"_ALL\" property, separate the items by
3731 spaces.")
3733 (defcustom org-global-properties nil
3734 "List of property/value pairs that can be inherited by any entry.
3736 This list will be combined with the constant `org-global-properties-fixed'.
3738 The entries in this list are cons cells where the car is a property
3739 name and cdr is a string with the value.
3741 You can set buffer-local values for the same purpose in the variable
3742 `org-file-properties' this by adding lines like
3744 #+PROPERTY: NAME VALUE"
3745 :group 'org-properties
3746 :type '(repeat
3747 (cons (string :tag "Property")
3748 (string :tag "Value"))))
3750 (defvar-local org-file-properties nil
3751 "List of property/value pairs that can be inherited by any entry.
3752 Valid for the current buffer.
3753 This variable is populated from #+PROPERTY lines.")
3755 (defgroup org-agenda nil
3756 "Options concerning agenda views in Org mode."
3757 :tag "Org Agenda"
3758 :group 'org)
3760 (defvar-local org-category nil
3761 "Variable used by Org files to set a category for agenda display.
3762 Such files should use a file variable to set it, for example
3764 # -*- mode: org; org-category: \"ELisp\"
3766 or contain a special line
3768 #+CATEGORY: ELisp
3770 If the file does not specify a category, then file's base name
3771 is used instead.")
3772 (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
3774 (defcustom org-agenda-files nil
3775 "The files to be used for agenda display.
3777 If an entry is a directory, all files in that directory that are matched
3778 by `org-agenda-file-regexp' will be part of the file list.
3780 If the value of the variable is not a list but a single file name, then
3781 the list of agenda files is actually stored and maintained in that file,
3782 one agenda file per line. In this file paths can be given relative to
3783 `org-directory'. Tilde expansion and environment variable substitution
3784 are also made.
3786 Entries may be added to this list with `\\[org-agenda-file-to-front]'
3787 and removed with `\\[org-remove-file]'."
3788 :group 'org-agenda
3789 :type '(choice
3790 (repeat :tag "List of files and directories" file)
3791 (file :tag "Store list in a file\n" :value "~/.agenda_files")))
3793 (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
3794 "Regular expression to match files for `org-agenda-files'.
3795 If any element in the list in that variable contains a directory instead
3796 of a normal file, all files in that directory that are matched by this
3797 regular expression will be included."
3798 :group 'org-agenda
3799 :type 'regexp)
3801 (defcustom org-agenda-text-search-extra-files nil
3802 "List of extra files to be searched by text search commands.
3803 These files will be searched in addition to the agenda files by the
3804 commands `org-search-view' (`\\[org-agenda] s') \
3805 and `org-occur-in-agenda-files'.
3806 Note that these files will only be searched for text search commands,
3807 not for the other agenda views like todo lists, tag searches or the weekly
3808 agenda. This variable is intended to list notes and possibly archive files
3809 that should also be searched by these two commands.
3810 In fact, if the first element in the list is the symbol `agenda-archives',
3811 then all archive files of all agenda files will be added to the search
3812 scope."
3813 :group 'org-agenda
3814 :type '(set :greedy t
3815 (const :tag "Agenda Archives" agenda-archives)
3816 (repeat :inline t (file))))
3818 (defvaralias 'org-agenda-multi-occur-extra-files
3819 'org-agenda-text-search-extra-files)
3821 (defcustom org-agenda-skip-unavailable-files nil
3822 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3823 A nil value means to remove them, after a query, from the list."
3824 :group 'org-agenda
3825 :type 'boolean)
3827 (defcustom org-calendar-to-agenda-key [?c]
3828 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3829 The command `org-calendar-goto-agenda' will be bound to this key. The
3830 default is the character `c' because then `c' can be used to switch back and
3831 forth between agenda and calendar."
3832 :group 'org-agenda
3833 :type 'sexp)
3835 (defcustom org-calendar-insert-diary-entry-key [?i]
3836 "The key to be installed in `calendar-mode-map' for adding diary entries.
3837 This option is irrelevant until `org-agenda-diary-file' has been configured
3838 to point to an Org file. When that is the case, the command
3839 `org-agenda-diary-entry' will be bound to the key given here, by default
3840 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3841 if you want to continue doing this, you need to change this to a different
3842 key."
3843 :group 'org-agenda
3844 :type 'sexp)
3846 (defcustom org-agenda-diary-file 'diary-file
3847 "File to which to add new entries with the `i' key in agenda and calendar.
3848 When this is the symbol `diary-file', the functionality in the Emacs
3849 calendar will be used to add entries to the `diary-file'. But when this
3850 points to a file, `org-agenda-diary-entry' will be used instead."
3851 :group 'org-agenda
3852 :type '(choice
3853 (const :tag "The standard Emacs diary file" diary-file)
3854 (file :tag "Special Org file diary entries")))
3856 (eval-after-load "calendar"
3857 '(progn
3858 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3859 'org-calendar-goto-agenda)
3860 (add-hook 'calendar-mode-hook
3861 (lambda ()
3862 (unless (eq org-agenda-diary-file 'diary-file)
3863 (define-key calendar-mode-map
3864 org-calendar-insert-diary-entry-key
3865 'org-agenda-diary-entry))))))
3867 (defgroup org-latex nil
3868 "Options for embedding LaTeX code into Org mode."
3869 :tag "Org LaTeX"
3870 :group 'org)
3872 (defcustom org-format-latex-options
3873 '(:foreground default :background default :scale 1.0
3874 :html-foreground "Black" :html-background "Transparent"
3875 :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
3876 "Options for creating images from LaTeX fragments.
3877 This is a property list with the following properties:
3878 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3879 `default' means use the foreground of the default face.
3880 `auto' means use the foreground from the text face.
3881 :background the background color, or \"Transparent\".
3882 `default' means use the background of the default face.
3883 `auto' means use the background from the text face.
3884 :scale a scaling factor for the size of the images, to get more pixels
3885 :html-foreground, :html-background, :html-scale
3886 the same numbers for HTML export.
3887 :matchers a list indicating which matchers should be used to
3888 find LaTeX fragments. Valid members of this list are:
3889 \"begin\" find environments
3890 \"$1\" find single characters surrounded by $.$
3891 \"$\" find math expressions surrounded by $...$
3892 \"$$\" find math expressions surrounded by $$....$$
3893 \"\\(\" find math expressions surrounded by \\(...\\)
3894 \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
3895 :group 'org-latex
3896 :type 'plist)
3898 (defcustom org-format-latex-signal-error t
3899 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3900 When nil, just push out a message."
3901 :group 'org-latex
3902 :version "24.1"
3903 :type 'boolean)
3905 (defcustom org-latex-to-mathml-jar-file nil
3906 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3907 Use this to specify additional executable file say a jar file.
3909 When using MathToWeb as the converter, specify the full-path to
3910 your mathtoweb.jar file."
3911 :group 'org-latex
3912 :version "24.1"
3913 :type '(choice
3914 (const :tag "None" nil)
3915 (file :tag "JAR file" :must-match t)))
3917 (defcustom org-latex-to-mathml-convert-command nil
3918 "Command to convert LaTeX fragments to MathML.
3919 Replace format-specifiers in the command as noted below and use
3920 `shell-command' to convert LaTeX to MathML.
3921 %j: Executable file in fully expanded form as specified by
3922 `org-latex-to-mathml-jar-file'.
3923 %I: Input LaTeX file in fully expanded form.
3924 %i: The latex fragment to be converted.
3925 %o: Output MathML file.
3927 This command is used by `org-create-math-formula'.
3929 When using MathToWeb as the converter, set this option to
3930 \"java -jar %j -unicode -force -df %o %I\".
3932 When using LaTeXML set this option to
3933 \"latexmlmath \"%i\" --presentationmathml=%o\"."
3934 :group 'org-latex
3935 :version "24.1"
3936 :type '(choice
3937 (const :tag "None" nil)
3938 (string :tag "\nShell command")))
3940 (defcustom org-preview-latex-default-process 'dvipng
3941 "The default process to convert LaTeX fragments to image files.
3942 All available processes and theirs documents can be found in
3943 `org-preview-latex-process-alist', which see."
3944 :group 'org-latex
3945 :version "26.1"
3946 :package-version '(Org . "9.0")
3947 :type 'symbol)
3949 (defcustom org-preview-latex-process-alist
3950 '((dvipng
3951 :programs ("latex" "dvipng")
3952 :description "dvi > png"
3953 :message "you need to install the programs: latex and dvipng."
3954 :image-input-type "dvi"
3955 :image-output-type "png"
3956 :image-size-adjust (1.0 . 1.0)
3957 :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
3958 :image-converter ("dvipng -fg %F -bg %B -D %D -T tight -o %O %f"))
3959 (dvisvgm
3960 :programs ("latex" "dvisvgm")
3961 :description "dvi > svg"
3962 :message "you need to install the programs: latex and dvisvgm."
3963 :use-xcolor t
3964 :image-input-type "dvi"
3965 :image-output-type "svg"
3966 :image-size-adjust (1.7 . 1.5)
3967 :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
3968 :image-converter ("dvisvgm %f -n -b min -c %S -o %O"))
3969 (imagemagick
3970 :programs ("latex" "convert")
3971 :description "pdf > png"
3972 :message "you need to install the programs: latex and imagemagick."
3973 :use-xcolor t
3974 :image-input-type "pdf"
3975 :image-output-type "png"
3976 :image-size-adjust (1.0 . 1.0)
3977 :latex-compiler ("pdflatex -interaction nonstopmode -output-directory %o %f")
3978 :image-converter
3979 ("convert -density %D -trim -antialias %f -quality 100 %O")))
3980 "Definitions of external processes for LaTeX previewing.
3981 Org mode can use some external commands to generate TeX snippet's images for
3982 previewing or inserting into HTML files, e.g., \"dvipng\". This variable tells
3983 `org-create-formula-image' how to call them.
3985 The value is an alist with the pattern (NAME . PROPERTIES). NAME is a symbol.
3986 PROPERTIES accepts the following attributes:
3988 :programs list of strings, required programs.
3989 :description string, describe the process.
3990 :message string, message it when required programs cannot be found.
3991 :image-input-type string, input file type of image converter (e.g., \"dvi\").
3992 :image-output-type string, output file type of image converter (e.g., \"png\").
3993 :use-xcolor boolean, when non-nil, LaTeX \"xcolor\" macro is used to
3994 deal with background and foreground color of image.
3995 Otherwise, dvipng style background and foreground color
3996 format are generated. You may then refer to them in
3997 command options with \"%F\" and \"%B\".
3998 :image-size-adjust cons of numbers, the car element is used to adjust LaTeX
3999 image size showed in buffer and the cdr element is for
4000 HTML file. This option is only useful for process
4001 developers, users should use variable
4002 `org-format-latex-options' instead.
4003 :post-clean list of strings, files matched are to be cleaned up once
4004 the image is generated. When nil, the files with \".dvi\",
4005 \".xdv\", \".pdf\", \".tex\", \".aux\", \".log\", \".svg\",
4006 \".png\", \".jpg\", \".jpeg\" or \".out\" extension will
4007 be cleaned up.
4008 :latex-header list of strings, the LaTeX header of the snippet file.
4009 When nil, the fallback value is used instead, which is
4010 controlled by `org-format-latex-header',
4011 `org-latex-default-packages-alist' and
4012 `org-latex-packages-alist', which see.
4013 :latex-compiler list of LaTeX commands, as strings. Each of them is given
4014 to the shell. Place-holders \"%t\", \"%b\" and \"%o\" are
4015 replaced with values defined below.
4016 :image-converter list of image converter commands strings. Each of them is
4017 given to the shell and supports any of the following
4018 place-holders defined below.
4020 Place-holders used by `:image-converter' and `:latex-compiler':
4022 %f input file name
4023 %b base name of input file
4024 %o base directory of input file
4025 %O absolute output file name
4027 Place-holders only used by `:image-converter':
4029 %F foreground of image
4030 %B background of image
4031 %D dpi, which is used to adjust image size by some processing commands.
4032 %S the image size scale ratio, which is used to adjust image size by some
4033 processing commands."
4034 :group 'org-latex
4035 :version "26.1"
4036 :package-version '(Org . "9.0")
4037 :type '(alist :tag "LaTeX to image backends"
4038 :value-type (plist)))
4040 (defcustom org-preview-latex-image-directory "ltximg/"
4041 "Path to store latex preview images.
4042 A relative path here creates many directories relative to the
4043 processed Org files paths. An absolute path puts all preview
4044 images at the same place."
4045 :group 'org-latex
4046 :version "26.1"
4047 :package-version '(Org . "9.0")
4048 :type 'string)
4050 (defun org-format-latex-mathml-available-p ()
4051 "Return t if `org-latex-to-mathml-convert-command' is usable."
4052 (save-match-data
4053 (when (and (boundp 'org-latex-to-mathml-convert-command)
4054 org-latex-to-mathml-convert-command)
4055 (let ((executable (car (split-string
4056 org-latex-to-mathml-convert-command))))
4057 (when (executable-find executable)
4058 (if (string-match
4059 "%j" org-latex-to-mathml-convert-command)
4060 (file-readable-p org-latex-to-mathml-jar-file)
4061 t))))))
4063 (defcustom org-format-latex-header "\\documentclass{article}
4064 \\usepackage[usenames]{color}
4065 \[PACKAGES]
4066 \[DEFAULT-PACKAGES]
4067 \\pagestyle{empty} % do not remove
4068 % The settings below are copied from fullpage.sty
4069 \\setlength{\\textwidth}{\\paperwidth}
4070 \\addtolength{\\textwidth}{-3cm}
4071 \\setlength{\\oddsidemargin}{1.5cm}
4072 \\addtolength{\\oddsidemargin}{-2.54cm}
4073 \\setlength{\\evensidemargin}{\\oddsidemargin}
4074 \\setlength{\\textheight}{\\paperheight}
4075 \\addtolength{\\textheight}{-\\headheight}
4076 \\addtolength{\\textheight}{-\\headsep}
4077 \\addtolength{\\textheight}{-\\footskip}
4078 \\addtolength{\\textheight}{-3cm}
4079 \\setlength{\\topmargin}{1.5cm}
4080 \\addtolength{\\topmargin}{-2.54cm}"
4081 "The document header used for processing LaTeX fragments.
4082 It is imperative that this header make sure that no page number
4083 appears on the page. The package defined in the variables
4084 `org-latex-default-packages-alist' and `org-latex-packages-alist'
4085 will either replace the placeholder \"[PACKAGES]\" in this
4086 header, or they will be appended."
4087 :group 'org-latex
4088 :type 'string)
4090 (defun org-set-packages-alist (var val)
4091 "Set the packages alist and make sure it has 3 elements per entry."
4092 (set var (mapcar (lambda (x)
4093 (if (and (consp x) (= (length x) 2))
4094 (list (car x) (nth 1 x) t)
4096 val)))
4098 (defun org-get-packages-alist (var)
4099 "Get the packages alist and make sure it has 3 elements per entry."
4100 (mapcar (lambda (x)
4101 (if (and (consp x) (= (length x) 2))
4102 (list (car x) (nth 1 x) t)
4104 (default-value var)))
4106 (defcustom org-latex-default-packages-alist
4107 '(("AUTO" "inputenc" t ("pdflatex"))
4108 ("T1" "fontenc" t ("pdflatex"))
4109 ("" "graphicx" t)
4110 ("" "grffile" t)
4111 ("" "longtable" nil)
4112 ("" "wrapfig" nil)
4113 ("" "rotating" nil)
4114 ("normalem" "ulem" t)
4115 ("" "amsmath" t)
4116 ("" "textcomp" t)
4117 ("" "amssymb" t)
4118 ("" "capt-of" nil)
4119 ("" "hyperref" nil))
4120 "Alist of default packages to be inserted in the header.
4122 Change this only if one of the packages here causes an
4123 incompatibility with another package you are using.
4125 The packages in this list are needed by one part or another of
4126 Org mode to function properly:
4128 - inputenc, fontenc: for basic font and character selection
4129 - graphicx: for including images
4130 - grffile: allow periods and spaces in graphics file names
4131 - longtable: For multipage tables
4132 - wrapfig: for figure placement
4133 - rotating: for sideways figures and tables
4134 - ulem: for underline and strike-through
4135 - amsmath: for subscript and superscript and math environments
4136 - textcomp, amssymb: for various symbols used
4137 for interpreting the entities in `org-entities'. You can skip
4138 some of these packages if you don't use any of their symbols.
4139 - capt-of: for captions outside of floats
4140 - hyperref: for cross references
4142 Therefore you should not modify this variable unless you know
4143 what you are doing. The one reason to change it anyway is that
4144 you might be loading some other package that conflicts with one
4145 of the default packages. Each element is either a cell or
4146 a string.
4148 A cell is of the format
4150 (\"options\" \"package\" SNIPPET-FLAG COMPILERS)
4152 If SNIPPET-FLAG is non-nil, the package also needs to be included
4153 when compiling LaTeX snippets into images for inclusion into
4154 non-LaTeX output. COMPILERS is a list of compilers that should
4155 include the package, see `org-latex-compiler'. If the document
4156 compiler is not in the list, and the list is non-nil, the package
4157 will not be inserted in the final document.
4159 A string will be inserted as-is in the header of the document."
4160 :group 'org-latex
4161 :group 'org-export-latex
4162 :set 'org-set-packages-alist
4163 :get 'org-get-packages-alist
4164 :version "26.1"
4165 :package-version '(Org . "8.3")
4166 :type '(repeat
4167 (choice
4168 (list :tag "options/package pair"
4169 (string :tag "options")
4170 (string :tag "package")
4171 (boolean :tag "Snippet")
4172 (choice
4173 (const :tag "For all compilers" nil)
4174 (repeat :tag "Allowed compiler" string)))
4175 (string :tag "A line of LaTeX"))))
4177 (defcustom org-latex-packages-alist nil
4178 "Alist of packages to be inserted in every LaTeX header.
4180 These will be inserted after `org-latex-default-packages-alist'.
4181 Each element is either a cell or a string.
4183 A cell is of the format:
4185 (\"options\" \"package\" SNIPPET-FLAG)
4187 SNIPPET-FLAG, when non-nil, indicates that this package is also
4188 needed when turning LaTeX snippets into images for inclusion into
4189 non-LaTeX output.
4191 A string will be inserted as-is in the header of the document.
4193 Make sure that you only list packages here which:
4195 - you want in every file;
4196 - do not conflict with the setup in `org-format-latex-header';
4197 - do not conflict with the default packages in
4198 `org-latex-default-packages-alist'."
4199 :group 'org-latex
4200 :group 'org-export-latex
4201 :set 'org-set-packages-alist
4202 :get 'org-get-packages-alist
4203 :type '(repeat
4204 (choice
4205 (list :tag "options/package pair"
4206 (string :tag "options")
4207 (string :tag "package")
4208 (boolean :tag "Snippet"))
4209 (string :tag "A line of LaTeX"))))
4211 (defgroup org-appearance nil
4212 "Settings for Org mode appearance."
4213 :tag "Org Appearance"
4214 :group 'org)
4216 (defcustom org-level-color-stars-only nil
4217 "Non-nil means fontify only the stars in each headline.
4218 When nil, the entire headline is fontified.
4219 Changing it requires restart of `font-lock-mode' to become effective
4220 also in regions already fontified."
4221 :group 'org-appearance
4222 :type 'boolean)
4224 (defcustom org-hide-leading-stars nil
4225 "Non-nil means hide the first N-1 stars in a headline.
4226 This works by using the face `org-hide' for these stars. This
4227 face is white for a light background, and black for a dark
4228 background. You may have to customize the face `org-hide' to
4229 make this work.
4230 Changing it requires restart of `font-lock-mode' to become effective
4231 also in regions already fontified.
4232 You may also set this on a per-file basis by adding one of the following
4233 lines to the buffer:
4235 #+STARTUP: hidestars
4236 #+STARTUP: showstars"
4237 :group 'org-appearance
4238 :type 'boolean)
4240 (defcustom org-hidden-keywords nil
4241 "List of symbols corresponding to keywords to be hidden in the Org buffer.
4242 For example, a value \\='(title) for this list makes the document's title
4243 appear in the buffer without the initial \"#+TITLE:\" part."
4244 :group 'org-appearance
4245 :version "24.1"
4246 :type '(set (const :tag "#+AUTHOR" author)
4247 (const :tag "#+DATE" date)
4248 (const :tag "#+EMAIL" email)
4249 (const :tag "#+TITLE" title)))
4251 (defcustom org-custom-properties nil
4252 "List of properties (as strings) with a special meaning.
4253 The default use of these custom properties is to let the user
4254 hide them with `org-toggle-custom-properties-visibility'."
4255 :group 'org-properties
4256 :group 'org-appearance
4257 :version "24.3"
4258 :type '(repeat (string :tag "Property Name")))
4260 (defcustom org-fontify-done-headline nil
4261 "Non-nil means change the face of a headline if it is marked DONE.
4262 Normally, only the TODO/DONE keyword indicates the state of a headline.
4263 When this is non-nil, the headline after the keyword is set to the
4264 `org-headline-done' as an additional indication."
4265 :group 'org-appearance
4266 :type 'boolean)
4268 (defcustom org-fontify-emphasized-text t
4269 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
4270 Changing this variable requires a restart of Emacs to take effect."
4271 :group 'org-appearance
4272 :type 'boolean)
4274 (defcustom org-fontify-whole-heading-line nil
4275 "Non-nil means fontify the whole line for headings.
4276 This is useful when setting a background color for the
4277 org-level-* faces."
4278 :group 'org-appearance
4279 :type 'boolean)
4281 (defcustom org-highlight-latex-and-related nil
4282 "Non-nil means highlight LaTeX related syntax in the buffer.
4283 When non nil, the value should be a list containing any of the
4284 following symbols:
4285 `latex' Highlight LaTeX snippets and environments.
4286 `script' Highlight subscript and superscript.
4287 `entities' Highlight entities."
4288 :group 'org-appearance
4289 :version "24.4"
4290 :package-version '(Org . "8.0")
4291 :type '(choice
4292 (const :tag "No highlighting" nil)
4293 (set :greedy t :tag "Highlight"
4294 (const :tag "LaTeX snippets and environments" latex)
4295 (const :tag "Subscript and superscript" script)
4296 (const :tag "Entities" entities))))
4298 (defcustom org-hide-emphasis-markers nil
4299 "Non-nil mean font-lock should hide the emphasis marker characters."
4300 :group 'org-appearance
4301 :type 'boolean)
4303 (defcustom org-hide-macro-markers nil
4304 "Non-nil mean font-lock should hide the brackets marking macro calls."
4305 :group 'org-appearance
4306 :type 'boolean)
4308 (defcustom org-pretty-entities nil
4309 "Non-nil means show entities as UTF8 characters.
4310 When nil, the \\name form remains in the buffer."
4311 :group 'org-appearance
4312 :version "24.1"
4313 :type 'boolean)
4315 (defcustom org-pretty-entities-include-sub-superscripts t
4316 "Non-nil means, pretty entity display includes formatting sub/superscripts."
4317 :group 'org-appearance
4318 :version "24.1"
4319 :type 'boolean)
4321 (defvar org-emph-re nil
4322 "Regular expression for matching emphasis.
4323 After a match, the match groups contain these elements:
4324 0 The match of the full regular expression, including the characters
4325 before and after the proper match
4326 1 The character before the proper match, or empty at beginning of line
4327 2 The proper match, including the leading and trailing markers
4328 3 The leading marker like * or /, indicating the type of highlighting
4329 4 The text between the emphasis markers, not including the markers
4330 5 The character after the match, empty at the end of a line")
4332 (defvar org-verbatim-re nil
4333 "Regular expression for matching verbatim text.")
4335 (defvar org-emphasis-regexp-components) ; defined just below
4336 (defvar org-emphasis-alist) ; defined just below
4337 (defun org-set-emph-re (var val)
4338 "Set variable and compute the emphasis regular expression."
4339 (set var val)
4340 (when (and (boundp 'org-emphasis-alist)
4341 (boundp 'org-emphasis-regexp-components)
4342 org-emphasis-alist org-emphasis-regexp-components)
4343 (pcase-let*
4344 ((`(,pre ,post ,border ,body ,nl) org-emphasis-regexp-components)
4345 (body (if (<= nl 0) body
4346 (format "%s*?\\(?:\n%s*?\\)\\{0,%d\\}" body body nl)))
4347 (template
4348 (format (concat "\\([%s]\\|^\\)" ;before markers
4349 "\\(\\([%%s]\\)\\([^%s]\\|[^%s]%s[^%s]\\)\\3\\)"
4350 "\\([%s]\\|$\\)") ;after markers
4351 pre border border body border post)))
4352 (setq org-emph-re (format template "*/_+"))
4353 (setq org-verbatim-re (format template "=~")))))
4355 ;; This used to be a defcustom (Org <8.0) but allowing the users to
4356 ;; set this option proved cumbersome. See this message/thread:
4357 ;; http://article.gmane.org/gmane.emacs.orgmode/68681
4358 (defvar org-emphasis-regexp-components
4359 '("- \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n" "." 1)
4360 "Components used to build the regular expression for emphasis.
4361 This is a list with five entries. Terminology: In an emphasis string
4362 like \" *strong word* \", we call the initial space PREMATCH, the final
4363 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
4364 and \"trong wor\" is the body. The different components in this variable
4365 specify what is allowed/forbidden in each part:
4367 pre Chars allowed as prematch. Beginning of line will be allowed too.
4368 post Chars allowed as postmatch. End of line will be allowed too.
4369 border The chars *forbidden* as border characters.
4370 body-regexp A regexp like \".\" to match a body character. Don't use
4371 non-shy groups here, and don't allow newline here.
4372 newline The maximum number of newlines allowed in an emphasis exp.
4374 You need to reload Org or to restart Emacs after customizing this.")
4376 (defcustom org-emphasis-alist
4377 '(("*" bold)
4378 ("/" italic)
4379 ("_" underline)
4380 ("=" org-verbatim verbatim)
4381 ("~" org-code verbatim)
4382 ("+" (:strike-through t)))
4383 "Alist of characters and faces to emphasize text.
4384 Text starting and ending with a special character will be emphasized,
4385 for example *bold*, _underlined_ and /italic/. This variable sets the
4386 marker characters and the face to be used by font-lock for highlighting
4387 in Org buffers.
4389 You need to reload Org or to restart Emacs after customizing this."
4390 :group 'org-appearance
4391 :set 'org-set-emph-re
4392 :version "24.4"
4393 :package-version '(Org . "8.0")
4394 :type '(repeat
4395 (list
4396 (string :tag "Marker character")
4397 (choice
4398 (face :tag "Font-lock-face")
4399 (plist :tag "Face property list"))
4400 (option (const verbatim)))))
4402 (defvar org-protecting-blocks '("src" "example" "export")
4403 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
4404 This is needed for font-lock setup.")
4406 ;;; Functions and variables from their packages
4407 ;; Declared here to avoid compiler warnings
4408 (defvar mark-active)
4410 ;; Various packages
4411 (declare-function calc-eval "calc" (str &optional separator &rest args))
4412 (declare-function calendar-forward-day "cal-move" (arg))
4413 (declare-function calendar-goto-date "cal-move" (date))
4414 (declare-function calendar-goto-today "cal-move" ())
4415 (declare-function calendar-iso-from-absolute "cal-iso" (date))
4416 (declare-function calendar-iso-to-absolute "cal-iso" (date))
4417 (declare-function cdlatex-compute-tables "ext:cdlatex" ())
4418 (declare-function cdlatex-tab "ext:cdlatex" ())
4419 (declare-function dired-get-filename
4420 "dired"
4421 (&optional localp no-error-if-not-filep))
4422 (declare-function iswitchb-read-buffer
4423 "iswitchb"
4424 (prompt &optional
4425 default require-match _predicate start matches-set))
4426 (declare-function org-agenda-change-all-lines
4427 "org-agenda"
4428 (newhead hdmarker &optional fixface just-this))
4429 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4430 "org-agenda"
4431 (&optional end))
4432 (declare-function org-agenda-copy-local-variable "org-agenda" (var))
4433 (declare-function org-agenda-format-item
4434 "org-agenda"
4435 (extra txt &optional level category tags dotime
4436 remove-re habitp))
4437 (declare-function org-agenda-maybe-redo "org-agenda" ())
4438 (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
4439 (declare-function org-agenda-save-markers-for-cut-and-paste
4440 "org-agenda"
4441 (beg end))
4442 (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
4443 (declare-function org-agenda-skip "org-agenda" ())
4444 (declare-function org-attach-reveal "org-attach" (&optional if-exists))
4445 (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
4446 (declare-function org-indent-mode "org-indent" (&optional arg))
4447 (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
4448 (declare-function org-inlinetask-goto-end "org-inlinetask" ())
4449 (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
4450 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
4451 (declare-function orgtbl-send-table "org-table" (&optional maybe))
4452 (declare-function parse-time-string "parse-time" (string))
4453 (declare-function speedbar-line-directory "speedbar" (&optional depth))
4455 (defvar align-mode-rules-list)
4456 (defvar calc-embedded-close-formula)
4457 (defvar calc-embedded-open-formula)
4458 (defvar calc-embedded-open-mode)
4459 (defvar font-lock-unfontify-region-function)
4460 (defvar iswitchb-temp-buflist)
4461 (defvar org-agenda-tags-todo-honor-ignore-options)
4462 (defvar remember-data-file)
4463 (defvar texmathp-why)
4465 ;;;###autoload
4466 (defun turn-on-orgtbl ()
4467 "Unconditionally turn on `orgtbl-mode'."
4468 (require 'org-table)
4469 (orgtbl-mode 1))
4471 (defun org-at-table-p (&optional table-type)
4472 "Non-nil if the cursor is inside an Org table.
4473 If TABLE-TYPE is non-nil, also check for table.el-type tables."
4474 (and (org-match-line (if table-type "[ \t]*[|+]" "[ \t]*|"))
4475 (or (not (derived-mode-p 'org-mode))
4476 (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
4477 (and e (or table-type
4478 (eq 'org (org-element-property :type e))))))))
4480 (defun org-at-table.el-p ()
4481 "Non-nil when point is at a table.el table."
4482 (and (org-match-line "[ \t]*[|+]")
4483 (let ((element (org-element-at-point)))
4484 (and (eq (org-element-type element) 'table)
4485 (eq (org-element-property :type element) 'table.el)))))
4487 (defun org-at-table-hline-p ()
4488 "Non-nil when point is inside a hline in a table.
4489 Assume point is already in a table."
4490 (org-match-line org-table-hline-regexp))
4492 (defun org-table-map-tables (function &optional quietly)
4493 "Apply FUNCTION to the start of all tables in the buffer."
4494 (org-with-wide-buffer
4495 (goto-char (point-min))
4496 (while (re-search-forward org-table-any-line-regexp nil t)
4497 (unless quietly
4498 (message "Mapping tables: %d%%"
4499 (floor (* 100.0 (point)) (buffer-size))))
4500 (beginning-of-line 1)
4501 (when (and (looking-at org-table-line-regexp)
4502 ;; Exclude tables in src/example/verbatim/clocktable blocks
4503 (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
4504 (save-excursion (funcall function))
4505 (or (looking-at org-table-line-regexp)
4506 (forward-char 1)))
4507 (re-search-forward org-table-any-border-regexp nil 1)))
4508 (unless quietly (message "Mapping tables: done")))
4510 (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
4511 (declare-function org-clock-update-mode-line "org-clock" ())
4512 (declare-function org-resolve-clocks "org-clock"
4513 (&optional also-non-dangling-p prompt last-valid))
4515 (defun org-at-TBLFM-p (&optional pos)
4516 "Non-nil when point (or POS) is in #+TBLFM line."
4517 (save-excursion
4518 (goto-char (or pos (point)))
4519 (beginning-of-line)
4520 (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
4521 (eq (org-element-type (org-element-at-point)) 'table))))
4523 (defvar org-clock-start-time)
4524 (defvar org-clock-marker (make-marker)
4525 "Marker recording the last clock-in.")
4526 (defvar org-clock-hd-marker (make-marker)
4527 "Marker recording the last clock-in, but the headline position.")
4528 (defvar org-clock-heading ""
4529 "The heading of the current clock entry.")
4530 (defun org-clock-is-active ()
4531 "Return the buffer where the clock is currently running.
4532 Return nil if no clock is running."
4533 (marker-buffer org-clock-marker))
4535 (defun org-check-running-clock ()
4536 "Check if the current buffer contains the running clock.
4537 If yes, offer to stop it and to save the buffer with the changes."
4538 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
4539 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4540 (buffer-name))))
4541 (org-clock-out)
4542 (when (y-or-n-p "Save changed buffer?")
4543 (save-buffer))))
4545 (defun org-clocktable-try-shift (dir n)
4546 "Check if this line starts a clock table, if yes, shift the time block."
4547 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4548 (org-clocktable-shift dir n)))
4550 ;;;###autoload
4551 (defun org-clock-persistence-insinuate ()
4552 "Set up hooks for clock persistence."
4553 (require 'org-clock)
4554 (add-hook 'org-mode-hook 'org-clock-load)
4555 (add-hook 'kill-emacs-hook 'org-clock-save))
4557 (defgroup org-archive nil
4558 "Options concerning archiving in Org mode."
4559 :tag "Org Archive"
4560 :group 'org-structure)
4562 (defcustom org-archive-location "%s_archive::"
4563 "The location where subtrees should be archived.
4565 The value of this variable is a string, consisting of two parts,
4566 separated by a double-colon. The first part is a filename and
4567 the second part is a headline.
4569 When the filename is omitted, archiving happens in the same file.
4570 %s in the filename will be replaced by the current file
4571 name (without the directory part). Archiving to a different file
4572 is useful to keep archived entries from contributing to the
4573 Org Agenda.
4575 The archived entries will be filed as subtrees of the specified
4576 headline. When the headline is omitted, the subtrees are simply
4577 filed away at the end of the file, as top-level entries. Also in
4578 the heading you can use %s to represent the file name, this can be
4579 useful when using the same archive for a number of different files.
4581 Here are a few examples:
4582 \"%s_archive::\"
4583 If the current file is Projects.org, archive in file
4584 Projects.org_archive, as top-level trees. This is the default.
4586 \"::* Archived Tasks\"
4587 Archive in the current file, under the top-level headline
4588 \"* Archived Tasks\".
4590 \"~/org/archive.org::\"
4591 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4593 \"~/org/archive.org::* From %s\"
4594 Archive in file ~/org/archive.org (absolute path), under headlines
4595 \"From FILENAME\" where file name is the current file name.
4597 \"~/org/datetree.org::datetree/* Finished Tasks\"
4598 The \"datetree/\" string is special, signifying to archive
4599 items to the datetree. Items are placed in either the CLOSED
4600 date of the item, or the current date if there is no CLOSED date.
4601 The heading will be a subentry to the current date. There doesn't
4602 need to be a heading, but there always needs to be a slash after
4603 datetree. For example, to store archived items directly in the
4604 datetree, use \"~/org/datetree.org::datetree/\".
4606 \"basement::** Finished Tasks\"
4607 Archive in file ./basement (relative path), as level 3 trees
4608 below the level 2 heading \"** Finished Tasks\".
4610 You may set this option on a per-file basis by adding to the buffer a
4611 line like
4613 #+ARCHIVE: basement::** Finished Tasks
4615 You may also define it locally for a subtree by setting an ARCHIVE property
4616 in the entry. If such a property is found in an entry, or anywhere up
4617 the hierarchy, it will be used."
4618 :group 'org-archive
4619 :type 'string)
4621 (defcustom org-agenda-skip-archived-trees t
4622 "Non-nil means the agenda will skip any items located in archived trees.
4623 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4624 variable is no longer recommended, you should leave it at the value t.
4625 Instead, use the key `v' to cycle the archives-mode in the agenda."
4626 :group 'org-archive
4627 :group 'org-agenda-skip
4628 :type 'boolean)
4630 (defcustom org-columns-skip-archived-trees t
4631 "Non-nil means ignore archived trees when creating column view."
4632 :group 'org-archive
4633 :group 'org-properties
4634 :type 'boolean)
4636 (defcustom org-cycle-open-archived-trees nil
4637 "Non-nil means `org-cycle' will open archived trees.
4638 An archived tree is a tree marked with the tag ARCHIVE.
4639 When nil, archived trees will stay folded. You can still open them with
4640 normal outline commands like `show-all', but not with the cycling commands."
4641 :group 'org-archive
4642 :group 'org-cycle
4643 :type 'boolean)
4645 (defcustom org-sparse-tree-open-archived-trees nil
4646 "Non-nil means sparse tree construction shows matches in archived trees.
4647 When nil, matches in these trees are highlighted, but the trees are kept in
4648 collapsed state."
4649 :group 'org-archive
4650 :group 'org-sparse-trees
4651 :type 'boolean)
4653 (defcustom org-sparse-tree-default-date-type nil
4654 "The default date type when building a sparse tree.
4655 When this is nil, a date is a scheduled or a deadline timestamp.
4656 Otherwise, these types are allowed:
4658 all: all timestamps
4659 active: only active timestamps (<...>)
4660 inactive: only inactive timestamps ([...])
4661 scheduled: only scheduled timestamps
4662 deadline: only deadline timestamps"
4663 :type '(choice (const :tag "Scheduled or deadline" nil)
4664 (const :tag "All timestamps" all)
4665 (const :tag "Only active timestamps" active)
4666 (const :tag "Only inactive timestamps" inactive)
4667 (const :tag "Only scheduled timestamps" scheduled)
4668 (const :tag "Only deadline timestamps" deadline)
4669 (const :tag "Only closed timestamps" closed))
4670 :version "26.1"
4671 :package-version '(Org . "8.3")
4672 :group 'org-sparse-trees)
4674 (defun org-cycle-hide-archived-subtrees (state)
4675 "Re-hide all archived subtrees after a visibility state change.
4676 STATE should be one of the symbols listed in the docstring of
4677 `org-cycle-hook'."
4678 (when (and (not org-cycle-open-archived-trees)
4679 (not (memq state '(overview folded))))
4680 (save-excursion
4681 (let* ((globalp (memq state '(contents all)))
4682 (beg (if globalp (point-min) (point)))
4683 (end (if globalp (point-max) (org-end-of-subtree t))))
4684 (org-hide-archived-subtrees beg end)
4685 (goto-char beg)
4686 (when (looking-at-p (concat ".*:" org-archive-tag ":"))
4687 (message "%s" (substitute-command-keys
4688 "Subtree is archived and stays closed. Use \
4689 `\\[org-force-cycle-archived]' to cycle it anyway.")))))))
4691 (defun org-force-cycle-archived ()
4692 "Cycle subtree even if it is archived."
4693 (interactive)
4694 (setq this-command 'org-cycle)
4695 (let ((org-cycle-open-archived-trees t))
4696 (call-interactively 'org-cycle)))
4698 (defun org-hide-archived-subtrees (beg end)
4699 "Re-hide all archived subtrees after a visibility state change."
4700 (org-with-wide-buffer
4701 (let ((case-fold-search nil)
4702 (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
4703 (goto-char beg)
4704 ;; Include headline point is currently on.
4705 (beginning-of-line)
4706 (while (and (< (point) end) (re-search-forward re end t))
4707 (when (member org-archive-tag (org-get-tags))
4708 (org-flag-subtree t)
4709 (org-end-of-subtree t))))))
4711 (declare-function outline-end-of-heading "outline" ())
4712 (declare-function outline-flag-region "outline" (from to flag))
4713 (defun org-flag-subtree (flag)
4714 (save-excursion
4715 (org-back-to-heading t)
4716 (outline-end-of-heading)
4717 (outline-flag-region (point)
4718 (progn (org-end-of-subtree t) (point))
4719 flag)))
4721 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
4723 ;; Declare Column View Code
4725 (declare-function org-columns-get-format-and-top-level "org-colview" ())
4726 (declare-function org-columns-compute "org-colview" (property))
4728 ;; Declare ID code
4730 (declare-function org-id-store-link "org-id")
4731 (declare-function org-id-locations-load "org-id")
4732 (declare-function org-id-locations-save "org-id")
4733 (defvar org-id-track-globally)
4735 ;;; Variables for pre-computed regular expressions, all buffer local
4737 (defvar-local org-todo-regexp nil
4738 "Matches any of the TODO state keywords.
4739 Since TODO keywords are case-sensitive, `case-fold-search' is
4740 expected to be bound to nil when matching against this regexp.")
4742 (defvar-local org-not-done-regexp nil
4743 "Matches any of the TODO state keywords except the last one.
4744 Since TODO keywords are case-sensitive, `case-fold-search' is
4745 expected to be bound to nil when matching against this regexp.")
4747 (defvar-local org-not-done-heading-regexp nil
4748 "Matches a TODO headline that is not done.
4749 Since TODO keywords are case-sensitive, `case-fold-search' is
4750 expected to be bound to nil when matching against this regexp.")
4752 (defvar-local org-todo-line-regexp nil
4753 "Matches a headline and puts TODO state into group 2 if present.
4754 Since TODO keywords are case-sensitive, `case-fold-search' is
4755 expected to be bound to nil when matching against this regexp.")
4757 (defvar-local org-complex-heading-regexp nil
4758 "Matches a headline and puts everything into groups:
4760 group 1: Stars
4761 group 2: The TODO keyword, maybe
4762 group 3: Priority cookie
4763 group 4: True headline
4764 group 5: Tags
4766 Since TODO keywords are case-sensitive, `case-fold-search' is
4767 expected to be bound to nil when matching against this regexp.")
4769 (defvar-local org-complex-heading-regexp-format nil
4770 "Printf format to make regexp to match an exact headline.
4771 This regexp will match the headline of any node which has the
4772 exact headline text that is put into the format, but may have any
4773 TODO state, priority and tags.")
4775 (defvar-local org-todo-line-tags-regexp nil
4776 "Matches a headline and puts TODO state into group 2 if present.
4777 Also put tags into group 4 if tags are present.")
4779 (defconst org-plain-time-of-day-regexp
4780 (concat
4781 "\\(\\<[012]?[0-9]"
4782 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4783 "\\(--?"
4784 "\\(\\<[012]?[0-9]"
4785 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4786 "\\)?")
4787 "Regular expression to match a plain time or time range.
4788 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4789 groups carry important information:
4790 0 the full match
4791 1 the first time, range or not
4792 8 the second time, if it is a range.")
4794 (defconst org-plain-time-extension-regexp
4795 (concat
4796 "\\(\\<[012]?[0-9]"
4797 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4798 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4799 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4800 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4801 groups carry important information:
4802 0 the full match
4803 7 hours of duration
4804 9 minutes of duration")
4806 (defconst org-stamp-time-of-day-regexp
4807 (concat
4808 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4809 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4810 "\\(--?"
4811 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4812 "Regular expression to match a timestamp time or time range.
4813 After a match, the following groups carry important information:
4814 0 the full match
4815 1 date plus weekday, for back referencing to make sure both times are on the same day
4816 2 the first time, range or not
4817 4 the second time, if it is a range.")
4819 (defconst org-startup-options
4820 '(("fold" org-startup-folded t)
4821 ("overview" org-startup-folded t)
4822 ("nofold" org-startup-folded nil)
4823 ("showall" org-startup-folded nil)
4824 ("showeverything" org-startup-folded showeverything)
4825 ("content" org-startup-folded content)
4826 ("indent" org-startup-indented t)
4827 ("noindent" org-startup-indented nil)
4828 ("hidestars" org-hide-leading-stars t)
4829 ("showstars" org-hide-leading-stars nil)
4830 ("odd" org-odd-levels-only t)
4831 ("oddeven" org-odd-levels-only nil)
4832 ("align" org-startup-align-all-tables t)
4833 ("noalign" org-startup-align-all-tables nil)
4834 ("inlineimages" org-startup-with-inline-images t)
4835 ("noinlineimages" org-startup-with-inline-images nil)
4836 ("latexpreview" org-startup-with-latex-preview t)
4837 ("nolatexpreview" org-startup-with-latex-preview nil)
4838 ("customtime" org-display-custom-times t)
4839 ("logdone" org-log-done time)
4840 ("lognotedone" org-log-done note)
4841 ("nologdone" org-log-done nil)
4842 ("lognoteclock-out" org-log-note-clock-out t)
4843 ("nolognoteclock-out" org-log-note-clock-out nil)
4844 ("logrepeat" org-log-repeat state)
4845 ("lognoterepeat" org-log-repeat note)
4846 ("logdrawer" org-log-into-drawer t)
4847 ("nologdrawer" org-log-into-drawer nil)
4848 ("logstatesreversed" org-log-states-order-reversed t)
4849 ("nologstatesreversed" org-log-states-order-reversed nil)
4850 ("nologrepeat" org-log-repeat nil)
4851 ("logreschedule" org-log-reschedule time)
4852 ("lognotereschedule" org-log-reschedule note)
4853 ("nologreschedule" org-log-reschedule nil)
4854 ("logredeadline" org-log-redeadline time)
4855 ("lognoteredeadline" org-log-redeadline note)
4856 ("nologredeadline" org-log-redeadline nil)
4857 ("logrefile" org-log-refile time)
4858 ("lognoterefile" org-log-refile note)
4859 ("nologrefile" org-log-refile nil)
4860 ("fninline" org-footnote-define-inline t)
4861 ("nofninline" org-footnote-define-inline nil)
4862 ("fnlocal" org-footnote-section nil)
4863 ("fnauto" org-footnote-auto-label t)
4864 ("fnprompt" org-footnote-auto-label nil)
4865 ("fnconfirm" org-footnote-auto-label confirm)
4866 ("fnplain" org-footnote-auto-label plain)
4867 ("fnadjust" org-footnote-auto-adjust t)
4868 ("nofnadjust" org-footnote-auto-adjust nil)
4869 ("constcgs" constants-unit-system cgs)
4870 ("constSI" constants-unit-system SI)
4871 ("noptag" org-tag-persistent-alist nil)
4872 ("hideblocks" org-hide-block-startup t)
4873 ("nohideblocks" org-hide-block-startup nil)
4874 ("beamer" org-startup-with-beamer-mode t)
4875 ("entitiespretty" org-pretty-entities t)
4876 ("entitiesplain" org-pretty-entities nil))
4877 "Variable associated with STARTUP options for Org.
4878 Each element is a list of three items: the startup options (as written
4879 in the #+STARTUP line), the corresponding variable, and the value to set
4880 this variable to if the option is found. An optional forth element PUSH
4881 means to push this value onto the list in the variable.")
4883 (defcustom org-group-tags t
4884 "When non-nil (the default), use group tags.
4885 This can be turned on/off through `org-toggle-tags-groups'."
4886 :group 'org-tags
4887 :group 'org-startup
4888 :type 'boolean)
4890 (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
4892 (defun org-toggle-tags-groups ()
4893 "Toggle support for group tags.
4894 Support for group tags is controlled by the option
4895 `org-group-tags', which is non-nil by default."
4896 (interactive)
4897 (setq org-group-tags (not org-group-tags))
4898 (cond ((and (derived-mode-p 'org-agenda-mode)
4899 org-group-tags)
4900 (org-agenda-redo))
4901 ((derived-mode-p 'org-mode)
4902 (let ((org-inhibit-startup t)) (org-mode))))
4903 (message "Groups tags support has been turned %s"
4904 (if org-group-tags "on" "off")))
4906 (defun org--tag-add-to-alist (alist1 alist2)
4907 "Merge tags from ALIST1 into ALIST2.
4909 Duplicates tags outside a group are removed. Keywords and order
4910 are preserved.
4912 The function assumes ALIST1 and ALIST2 are proper tag alists.
4913 See `org-tag-alist' for their structure."
4914 (cond
4915 ((null alist2) alist1)
4916 ((null alist1) alist2)
4918 (let ((to-add nil)
4919 (group-flag nil))
4920 (dolist (tag-pair alist1)
4921 (pcase tag-pair
4922 (`(,(or :startgrouptag :startgroup))
4923 (setq group-flag t)
4924 (push tag-pair to-add))
4925 (`(,(or :endgrouptag :endgroup))
4926 (setq group-flag nil)
4927 (push tag-pair to-add))
4928 (`(,(or :grouptags :newline))
4929 (push tag-pair to-add))
4930 (`(,tag . ,_)
4931 ;; Remove duplicates from ALIST1, unless they are in
4932 ;; a group. Indeed, it makes sense to have a tag appear in
4933 ;; multiple groups.
4934 (when (or group-flag (not (assoc tag alist2)))
4935 (push tag-pair to-add)))
4936 (_ (error "Invalid association in tag alist: %S" tag-pair))))
4937 ;; Preserve order of ALIST1.
4938 (append (nreverse to-add) alist2)))))
4940 (defun org-set-regexps-and-options (&optional tags-only)
4941 "Precompute regular expressions used in the current buffer.
4942 When optional argument TAGS-ONLY is non-nil, only compute tags
4943 related expressions."
4944 (when (derived-mode-p 'org-mode)
4945 (let ((alist (org--setup-collect-keywords
4946 (org-make-options-regexp
4947 (append '("FILETAGS" "TAGS" "SETUPFILE")
4948 (and (not tags-only)
4949 '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
4950 "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
4951 "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
4952 ;; Startup options. Get this early since it does change
4953 ;; behavior for other options (e.g., tags).
4954 (let ((startup (cdr (assq 'startup alist))))
4955 (dolist (option startup)
4956 (let ((entry (assoc-string option org-startup-options t)))
4957 (when entry
4958 (let ((var (nth 1 entry))
4959 (val (nth 2 entry)))
4960 (if (not (nth 3 entry)) (set (make-local-variable var) val)
4961 (unless (listp (symbol-value var))
4962 (set (make-local-variable var) nil))
4963 (add-to-list var val)))))))
4964 (setq-local org-file-tags
4965 (mapcar #'org-add-prop-inherited
4966 (cdr (assq 'filetags alist))))
4967 (setq org-current-tag-alist
4968 (org--tag-add-to-alist
4969 org-tag-persistent-alist
4970 (let ((tags (cdr (assq 'tags alist))))
4971 (if tags (org-tag-string-to-alist tags)
4972 org-tag-alist))))
4973 (setq org-tag-groups-alist
4974 (org-tag-alist-to-groups org-current-tag-alist))
4975 (unless tags-only
4976 ;; File properties.
4977 (setq-local org-file-properties (cdr (assq 'property alist)))
4978 ;; Archive location.
4979 (let ((archive (cdr (assq 'archive alist))))
4980 (when archive (setq-local org-archive-location archive)))
4981 ;; Category.
4982 (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
4983 (when cat
4984 (setq-local org-category (intern cat))
4985 (setq-local org-file-properties
4986 (org--update-property-plist
4987 "CATEGORY" cat org-file-properties))))
4988 ;; Columns.
4989 (let ((column (cdr (assq 'columns alist))))
4990 (when column (setq-local org-columns-default-format column)))
4991 ;; Constants.
4992 (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
4993 ;; Link abbreviations.
4994 (let ((links (cdr (assq 'link alist))))
4995 (when links (setq org-link-abbrev-alist-local (nreverse links))))
4996 ;; Priorities.
4997 (let ((priorities (cdr (assq 'priorities alist))))
4998 (when priorities
4999 (setq-local org-highest-priority (nth 0 priorities))
5000 (setq-local org-lowest-priority (nth 1 priorities))
5001 (setq-local org-default-priority (nth 2 priorities))))
5002 ;; Scripts.
5003 (let ((scripts (assq 'scripts alist)))
5004 (when scripts
5005 (setq-local org-use-sub-superscripts (cdr scripts))))
5006 ;; TODO keywords.
5007 (setq-local org-todo-kwd-alist nil)
5008 (setq-local org-todo-key-alist nil)
5009 (setq-local org-todo-key-trigger nil)
5010 (setq-local org-todo-keywords-1 nil)
5011 (setq-local org-done-keywords nil)
5012 (setq-local org-todo-heads nil)
5013 (setq-local org-todo-sets nil)
5014 (setq-local org-todo-log-states nil)
5015 (let ((todo-sequences
5016 (or (nreverse (cdr (assq 'todo alist)))
5017 (let ((d (default-value 'org-todo-keywords)))
5018 (if (not (stringp (car d))) d
5019 ;; XXX: Backward compatibility code.
5020 (list (cons org-todo-interpretation d)))))))
5021 (dolist (sequence todo-sequences)
5022 (let* ((sequence (or (run-hook-with-args-until-success
5023 'org-todo-setup-filter-hook sequence)
5024 sequence))
5025 (sequence-type (car sequence))
5026 (keywords (cdr sequence))
5027 (sep (member "|" keywords))
5028 names alist)
5029 (dolist (k (remove "|" keywords))
5030 (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
5032 (error "Invalid TODO keyword %s" k))
5033 (let ((name (match-string 1 k))
5034 (key (match-string 2 k))
5035 (log (org-extract-log-state-settings k)))
5036 (push name names)
5037 (push (cons name (and key (string-to-char key))) alist)
5038 (when log (push log org-todo-log-states))))
5039 (let* ((names (nreverse names))
5040 (done (if sep (org-remove-keyword-keys (cdr sep))
5041 (last names)))
5042 (head (car names))
5043 (tail (list sequence-type head (car done) (org-last done))))
5044 (add-to-list 'org-todo-heads head 'append)
5045 (push names org-todo-sets)
5046 (setq org-done-keywords (append org-done-keywords done nil))
5047 (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
5048 (setq org-todo-key-alist
5049 (append org-todo-key-alist
5050 (and alist
5051 (append '((:startgroup))
5052 (nreverse alist)
5053 '((:endgroup))))))
5054 (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
5055 (setq org-todo-sets (nreverse org-todo-sets)
5056 org-todo-kwd-alist (nreverse org-todo-kwd-alist)
5057 org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
5058 org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
5059 ;; Compute the regular expressions and other local variables.
5060 ;; Using `org-outline-regexp-bol' would complicate them much,
5061 ;; because of the fixed white space at the end of that string.
5062 (unless org-done-keywords
5063 (setq org-done-keywords
5064 (and org-todo-keywords-1 (last org-todo-keywords-1))))
5065 (setq org-not-done-keywords
5066 (org-delete-all org-done-keywords
5067 (copy-sequence org-todo-keywords-1))
5068 org-todo-regexp (regexp-opt org-todo-keywords-1 t)
5069 org-not-done-regexp (regexp-opt org-not-done-keywords t)
5070 org-not-done-heading-regexp
5071 (format org-heading-keyword-regexp-format org-not-done-regexp)
5072 org-todo-line-regexp
5073 (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
5074 org-complex-heading-regexp
5075 (concat "^\\(\\*+\\)"
5076 "\\(?: +" org-todo-regexp "\\)?"
5077 "\\(?: +\\(\\[#.\\]\\)\\)?"
5078 "\\(?: +\\(.*?\\)\\)??"
5079 "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?"
5080 "[ \t]*$")
5081 org-complex-heading-regexp-format
5082 (concat "^\\(\\*+\\)"
5083 "\\(?: +" org-todo-regexp "\\)?"
5084 "\\(?: +\\(\\[#.\\]\\)\\)?"
5085 "\\(?: +"
5086 ;; Stats cookies can be stuck to body.
5087 "\\(?:\\[[0-9%%/]+\\] *\\)*"
5088 "\\(%s\\)"
5089 "\\(?: *\\[[0-9%%/]+\\]\\)*"
5090 "\\)"
5091 "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?"
5092 "[ \t]*$")
5093 org-todo-line-tags-regexp
5094 (concat "^\\(\\*+\\)"
5095 "\\(?: +" org-todo-regexp "\\)?"
5096 "\\(?: +\\(.*?\\)\\)??"
5097 "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?"
5098 "[ \t]*$"))
5099 (org-compute-latex-and-related-regexp)))))
5101 (defun org--setup-collect-keywords (regexp &optional files alist)
5102 "Return setup keywords values as an alist.
5104 REGEXP matches a subset of setup keywords. FILES is a list of
5105 file names already visited. It is used to avoid circular setup
5106 files. ALIST, when non-nil, is the alist computed so far.
5108 Return value contains the following keys: `archive', `category',
5109 `columns', `constants', `filetags', `link', `priorities',
5110 `property', `scripts', `startup', `tags' and `todo'."
5111 (org-with-wide-buffer
5112 (goto-char (point-min))
5113 (let ((case-fold-search t))
5114 (while (re-search-forward regexp nil t)
5115 (let ((element (org-element-at-point)))
5116 (when (eq (org-element-type element) 'keyword)
5117 (let ((key (org-element-property :key element))
5118 (value (org-element-property :value element)))
5119 (cond
5120 ((equal key "ARCHIVE")
5121 (when (org-string-nw-p value)
5122 (push (cons 'archive value) alist)))
5123 ((equal key "CATEGORY") (push (cons 'category value) alist))
5124 ((equal key "COLUMNS") (push (cons 'columns value) alist))
5125 ((equal key "CONSTANTS")
5126 (let* ((constants (assq 'constants alist))
5127 (store (cdr constants)))
5128 (dolist (pair (split-string value))
5129 (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
5130 pair)
5131 (let* ((name (match-string 1 pair))
5132 (value (match-string 2 pair))
5133 (old (assoc name store)))
5134 (if old (setcdr old value)
5135 (push (cons name value) store)))))
5136 (if constants (setcdr constants store)
5137 (push (cons 'constants store) alist))))
5138 ((equal key "FILETAGS")
5139 (when (org-string-nw-p value)
5140 (let ((old (assq 'filetags alist))
5141 (new (apply #'nconc
5142 (mapcar (lambda (x) (org-split-string x ":"))
5143 (split-string value)))))
5144 (if old (setcdr old (append new (cdr old)))
5145 (push (cons 'filetags new) alist)))))
5146 ((equal key "LINK")
5147 (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
5148 (let ((links (assq 'link alist))
5149 (pair (cons (match-string-no-properties 1 value)
5150 (match-string-no-properties 2 value))))
5151 (if links (push pair (cdr links))
5152 (push (list 'link pair) alist)))))
5153 ((equal key "OPTIONS")
5154 (when (and (org-string-nw-p value)
5155 (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
5156 (push (cons 'scripts (read (match-string 1 value))) alist)))
5157 ((equal key "PRIORITIES")
5158 (push (cons 'priorities
5159 (let ((prio (split-string value)))
5160 (if (< (length prio) 3) '(?A ?C ?B)
5161 (mapcar #'string-to-char prio))))
5162 alist))
5163 ((equal key "PROPERTY")
5164 (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
5165 (let* ((property (assq 'property alist))
5166 (value (org--update-property-plist
5167 (match-string-no-properties 1 value)
5168 (match-string-no-properties 2 value)
5169 (cdr property))))
5170 (if property (setcdr property value)
5171 (push (cons 'property value) alist)))))
5172 ((equal key "STARTUP")
5173 (let ((startup (assq 'startup alist)))
5174 (if startup
5175 (setcdr startup
5176 (append (cdr startup) (split-string value)))
5177 (push (cons 'startup (split-string value)) alist))))
5178 ((equal key "TAGS")
5179 (let ((tag-cell (assq 'tags alist)))
5180 (if tag-cell
5181 (setcdr tag-cell (concat (cdr tag-cell) "\n" value))
5182 (push (cons 'tags value) alist))))
5183 ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
5184 (let ((todo (assq 'todo alist))
5185 (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
5186 (split-string value))))
5187 (if todo (push value (cdr todo))
5188 (push (list 'todo value) alist))))
5189 ((equal key "SETUPFILE")
5190 (unless buffer-read-only ; Do not check in Gnus messages.
5191 (let ((f (and (org-string-nw-p value)
5192 (expand-file-name
5193 (org-unbracket-string "\"" "\"" value)))))
5194 (when (and f (file-readable-p f) (not (member f files)))
5195 (with-temp-buffer
5196 (setq default-directory (file-name-directory f))
5197 (insert-file-contents f)
5198 (setq alist
5199 ;; Fake Org mode to benefit from cache
5200 ;; without recurring needlessly.
5201 (let ((major-mode 'org-mode))
5202 (org--setup-collect-keywords
5203 regexp (cons f files) alist)))))))))))))))
5204 alist)
5206 (defun org-tag-string-to-alist (s)
5207 "Return tag alist associated to string S.
5208 S is a value for TAGS keyword or produced with
5209 `org-tag-alist-to-string'. Return value is an alist suitable for
5210 `org-tag-alist' or `org-tag-persistent-alist'."
5211 (let ((lines (mapcar #'split-string (split-string s "\n" t)))
5212 (tag-re (concat "\\`\\([[:alnum:]_@#%]+"
5213 "\\|{.+?}\\)" ; regular expression
5214 "\\(?:(\\(.\\))\\)?\\'"))
5215 alist group-flag)
5216 (dolist (tokens lines (cdr (nreverse alist)))
5217 (push '(:newline) alist)
5218 (while tokens
5219 (let ((token (pop tokens)))
5220 (pcase token
5221 ("{"
5222 (push '(:startgroup) alist)
5223 (when (equal (nth 1 tokens) ":") (setq group-flag t)))
5224 ("}"
5225 (push '(:endgroup) alist)
5226 (setq group-flag nil))
5227 ("["
5228 (push '(:startgrouptag) alist)
5229 (when (equal (nth 1 tokens) ":") (setq group-flag t)))
5230 ("]"
5231 (push '(:endgrouptag) alist)
5232 (setq group-flag nil))
5233 (":"
5234 (push '(:grouptags) alist))
5235 ((guard (string-match tag-re token))
5236 (let ((tag (match-string 1 token))
5237 (key (and (match-beginning 2)
5238 (string-to-char (match-string 2 token)))))
5239 ;; Push all tags in groups, no matter if they already
5240 ;; appear somewhere else in the list.
5241 (when (or group-flag (not (assoc tag alist)))
5242 (push (cons tag key) alist))))))))))
5244 (defun org-tag-alist-to-string (alist &optional skip-key)
5245 "Return tag string associated to ALIST.
5247 ALIST is an alist, as defined in `org-tag-alist' or
5248 `org-tag-persistent-alist', or produced with
5249 `org-tag-string-to-alist'.
5251 Return value is a string suitable as a value for \"TAGS\"
5252 keyword.
5254 When optional argument SKIP-KEY is non-nil, skip selection keys
5255 next to tags."
5256 (mapconcat (lambda (token)
5257 (pcase token
5258 (`(:startgroup) "{")
5259 (`(:endgroup) "}")
5260 (`(:startgrouptag) "[")
5261 (`(:endgrouptag) "]")
5262 (`(:grouptags) ":")
5263 (`(:newline) "\\n")
5264 ((and
5265 (guard (not skip-key))
5266 `(,(and tag (pred stringp)) . ,(and key (pred characterp))))
5267 (format "%s(%c)" tag key))
5268 (`(,(and tag (pred stringp)) . ,_) tag)
5269 (_ (user-error "Invalid tag token: %S" token))))
5270 alist
5271 " "))
5273 (defun org-tag-alist-to-groups (alist)
5274 "Return group alist from tag ALIST.
5275 ALIST is an alist, as defined in `org-tag-alist' or
5276 `org-tag-persistent-alist', or produced with
5277 `org-tag-string-to-alist'. Return value is an alist following
5278 the pattern (GROUP-TAG TAGS) where GROUP-TAG is the tag, as
5279 a string, summarizing TAGS, as a list of strings."
5280 (let (groups group-status current-group)
5281 (dolist (token alist (nreverse groups))
5282 (pcase token
5283 (`(,(or :startgroup :startgrouptag)) (setq group-status t))
5284 (`(,(or :endgroup :endgrouptag))
5285 (when (eq group-status 'append)
5286 (push (nreverse current-group) groups))
5287 (setq group-status nil current-group nil))
5288 (`(:grouptags) (setq group-status 'append))
5289 ((and `(,tag . ,_) (guard group-status))
5290 (if (eq group-status 'append) (push tag current-group)
5291 (setq current-group (list tag))))
5292 (_ nil)))))
5294 (defvar org--file-cache (make-hash-table :test #'equal)
5295 "Hash table to store contents of files referenced via a URL.
5296 This is the cache of file URLs read using `org-file-contents'.")
5298 (defun org-reset-file-cache ()
5299 "Reset the cache of files downloaded by `org-file-contents'."
5300 (clrhash org--file-cache))
5302 (defun org-file-url-p (file)
5303 "Non-nil if FILE is a URL."
5304 (require 'ffap)
5305 (string-match-p ffap-url-regexp file))
5307 (defun org-file-contents (file &optional noerror nocache)
5308 "Return the contents of FILE, as a string.
5310 FILE can be a file name or URL.
5312 If FILE is a URL, download the contents. If the URL contents are
5313 already cached in the `org--file-cache' hash table, the download step
5314 is skipped.
5316 If NOERROR is non-nil, ignore the error when unable to read the FILE
5317 from file or URL.
5319 If NOCACHE is non-nil, do a fresh fetch of FILE even if cached version
5320 is available. This option applies only if FILE is a URL."
5321 (let* ((is-url (org-file-url-p file))
5322 (cache (and is-url
5323 (not nocache)
5324 (gethash file org--file-cache))))
5325 (cond
5326 (cache)
5327 (is-url
5328 (with-current-buffer (url-retrieve-synchronously file)
5329 (goto-char (point-min))
5330 ;; Move point to after the url-retrieve header.
5331 (search-forward "\n\n" nil :move)
5332 ;; Search for the success code only in the url-retrieve header.
5333 (if (save-excursion
5334 (re-search-backward "HTTP.*\\s-+200\\s-OK" nil :noerror))
5335 ;; Update the cache `org--file-cache' and return contents.
5336 (puthash file
5337 (buffer-substring-no-properties (point) (point-max))
5338 org--file-cache)
5339 (funcall (if noerror #'message #'user-error)
5340 "Unable to fetch file from %S"
5341 file))))
5343 (with-temp-buffer
5344 (condition-case nil
5345 (progn
5346 (insert-file-contents file)
5347 (buffer-string))
5348 (file-error
5349 (funcall (if noerror #'message #'user-error)
5350 "Unable to read file %S"
5351 file))))))))
5353 (defun org-extract-log-state-settings (x)
5354 "Extract the log state setting from a TODO keyword string.
5355 This will extract info from a string like \"WAIT(w@/!)\"."
5356 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
5357 (let ((kw (match-string 1 x))
5358 (log1 (and (match-end 3) (match-string 3 x)))
5359 (log2 (and (match-end 4) (match-string 4 x))))
5360 (and (or log1 log2)
5361 (list kw
5362 (and log1 (if (equal log1 "!") 'time 'note))
5363 (and log2 (if (equal log2 "!") 'time 'note)))))))
5365 (defun org-remove-keyword-keys (list)
5366 "Remove a pair of parenthesis at the end of each string in LIST."
5367 (mapcar (lambda (x)
5368 (if (string-match "(.*)$" x)
5369 (substring x 0 (match-beginning 0))
5371 list))
5373 (defun org-assign-fast-keys (alist)
5374 "Assign fast keys to a keyword-key alist.
5375 Respect keys that are already there."
5376 (let (new e (alt ?0))
5377 (while (setq e (pop alist))
5378 (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
5379 (cdr e)) ;; Key already assigned.
5380 (push e new)
5381 (let ((clist (string-to-list (downcase (car e))))
5382 (used (append new alist)))
5383 (when (= (car clist) ?@)
5384 (pop clist))
5385 (while (and clist (rassoc (car clist) used))
5386 (pop clist))
5387 (unless clist
5388 (while (rassoc alt used)
5389 (cl-incf alt)))
5390 (push (cons (car e) (or (car clist) alt)) new))))
5391 (nreverse new)))
5393 ;;; Some variables used in various places
5395 (defvar org-window-configuration nil
5396 "Used in various places to store a window configuration.")
5397 (defvar org-selected-window nil
5398 "Used in various places to store a window configuration.")
5399 (defvar org-finish-function nil
5400 "Function to be called when `C-c C-c' is used.
5401 This is for getting out of special buffers like capture.")
5402 (defvar org-last-state)
5404 ;; Defined somewhere in this file, but used before definition.
5405 (defvar org-entities) ;; defined in org-entities.el
5406 (defvar org-struct-menu)
5407 (defvar org-org-menu)
5408 (defvar org-tbl-menu)
5410 ;;;; Define the Org mode
5412 ;; We use a before-change function to check if a table might need
5413 ;; an update.
5414 (defvar org-table-may-need-update t
5415 "Indicates that a table might need an update.
5416 This variable is set by `org-before-change-function'.
5417 `org-table-align' sets it back to nil.")
5418 (defun org-before-change-function (_beg _end)
5419 "Every change indicates that a table might need an update."
5420 (setq org-table-may-need-update t))
5421 (defvar org-mode-map)
5422 (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
5423 (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
5424 (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
5425 (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
5426 (defvar org-table-buffer-is-an nil)
5428 (defvar bidi-paragraph-direction)
5429 (defvar buffer-face-mode-face)
5431 (require 'outline)
5433 ;; Other stuff we need.
5434 (require 'time-date)
5435 (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
5436 (require 'easymenu)
5437 (autoload 'easy-menu-add "easymenu")
5438 (require 'overlay)
5440 ;; (require 'org-macs) moved higher up in the file before it is first used
5441 (require 'org-entities)
5442 ;; (require 'org-compat) moved higher up in the file before it is first used
5443 (require 'org-faces)
5444 (require 'org-list)
5445 (require 'org-pcomplete)
5446 (require 'org-src)
5447 (require 'org-footnote)
5448 (require 'org-macro)
5450 ;; babel
5451 (require 'ob)
5453 ;;;###autoload
5454 (define-derived-mode org-mode outline-mode "Org"
5455 "Outline-based notes management and organizer, alias
5456 \"Carsten's outline-mode for keeping track of everything.\"
5458 Org mode develops organizational tasks around a NOTES file which
5459 contains information about projects as plain text. Org mode is
5460 implemented on top of Outline mode, which is ideal to keep the content
5461 of large files well structured. It supports ToDo items, deadlines and
5462 time stamps, which magically appear in the diary listing of the Emacs
5463 calendar. Tables are easily created with a built-in table editor.
5464 Plain text URL-like links connect to websites, emails (VM), Usenet
5465 messages (Gnus), BBDB entries, and any files related to the project.
5466 For printing and sharing of notes, an Org file (or a part of it)
5467 can be exported as a structured ASCII or HTML file.
5469 The following commands are available:
5471 \\{org-mode-map}"
5473 ;; Get rid of Outline menus, they are not needed
5474 ;; Need to do this here because define-derived-mode sets up
5475 ;; the keymap so late. Still, it is a waste to call this each time
5476 ;; we switch another buffer into Org mode.
5477 (define-key org-mode-map [menu-bar headings] 'undefined)
5478 (define-key org-mode-map [menu-bar hide] 'undefined)
5479 (define-key org-mode-map [menu-bar show] 'undefined)
5481 (org-load-modules-maybe)
5482 (org-install-agenda-files-menu)
5483 (when org-descriptive-links (add-to-invisibility-spec '(org-link)))
5484 (add-to-invisibility-spec '(org-cwidth))
5485 (add-to-invisibility-spec '(org-hide-block . t))
5486 (setq-local outline-regexp org-outline-regexp)
5487 (setq-local outline-level 'org-outline-level)
5488 (setq bidi-paragraph-direction 'left-to-right)
5489 (when (and (stringp org-ellipsis) (not (equal "" org-ellipsis)))
5490 (unless org-display-table
5491 (setq org-display-table (make-display-table)))
5492 (set-display-table-slot
5493 org-display-table 4
5494 (vconcat (mapcar (lambda (c) (make-glyph-code c 'org-ellipsis))
5495 org-ellipsis)))
5496 (setq buffer-display-table org-display-table))
5497 (org-set-regexps-and-options)
5498 (org-set-font-lock-defaults)
5499 (when (and org-tag-faces (not org-tags-special-faces-re))
5500 ;; tag faces set outside customize.... force initialization.
5501 (org-set-tag-faces 'org-tag-faces org-tag-faces))
5502 ;; Calc embedded
5503 (setq-local calc-embedded-open-mode "# ")
5504 ;; Modify a few syntax entries
5505 (modify-syntax-entry ?@ "w")
5506 (modify-syntax-entry ?\" "\"")
5507 (modify-syntax-entry ?\\ "_")
5508 (modify-syntax-entry ?~ "_")
5509 (setq-local font-lock-unfontify-region-function 'org-unfontify-region)
5510 ;; Activate before-change-function
5511 (setq-local org-table-may-need-update t)
5512 (add-hook 'before-change-functions 'org-before-change-function nil 'local)
5513 ;; Check for running clock before killing a buffer
5514 (add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
5515 ;; Initialize macros templates.
5516 (org-macro-initialize-templates)
5517 ;; Initialize radio targets.
5518 (org-update-radio-target-regexp)
5519 ;; Indentation.
5520 (setq-local indent-line-function 'org-indent-line)
5521 (setq-local indent-region-function 'org-indent-region)
5522 ;; Filling and auto-filling.
5523 (org-setup-filling)
5524 ;; Comments.
5525 (org-setup-comments-handling)
5526 ;; Initialize cache.
5527 (org-element-cache-reset)
5528 ;; Beginning/end of defun
5529 (setq-local beginning-of-defun-function 'org-backward-element)
5530 (setq-local end-of-defun-function
5531 (lambda ()
5532 (if (not (org-at-heading-p))
5533 (org-forward-element)
5534 (org-forward-element)
5535 (forward-char -1))))
5536 ;; Next error for sparse trees
5537 (setq-local next-error-function 'org-occur-next-match)
5538 ;; Make sure dependence stuff works reliably, even for users who set it
5539 ;; too late :-(
5540 (if org-enforce-todo-dependencies
5541 (add-hook 'org-blocker-hook
5542 'org-block-todo-from-children-or-siblings-or-parent)
5543 (remove-hook 'org-blocker-hook
5544 'org-block-todo-from-children-or-siblings-or-parent))
5545 (if org-enforce-todo-checkbox-dependencies
5546 (add-hook 'org-blocker-hook
5547 'org-block-todo-from-checkboxes)
5548 (remove-hook 'org-blocker-hook
5549 'org-block-todo-from-checkboxes))
5551 ;; Align options lines
5552 (setq-local
5553 align-mode-rules-list
5554 '((org-in-buffer-settings
5555 (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5556 (modes . '(org-mode)))))
5558 ;; Imenu
5559 (setq-local imenu-create-index-function 'org-imenu-get-tree)
5561 ;; Make isearch reveal context
5562 (setq-local outline-isearch-open-invisible-function
5563 (lambda (&rest _) (org-show-context 'isearch)))
5565 ;; Setup the pcomplete hooks
5566 (setq-local pcomplete-command-completion-function 'org-pcomplete-initial)
5567 (setq-local pcomplete-command-name-function 'org-command-at-point)
5568 (setq-local pcomplete-default-completion-function 'ignore)
5569 (setq-local pcomplete-parse-arguments-function 'org-parse-arguments)
5570 (setq-local pcomplete-termination-string "")
5571 (setq-local buffer-face-mode-face 'org-default)
5573 ;; If empty file that did not turn on Org mode automatically, make
5574 ;; it to.
5575 (when (and org-insert-mode-line-in-empty-file
5576 (called-interactively-p 'any)
5577 (= (point-min) (point-max)))
5578 (insert "# -*- mode: org -*-\n\n"))
5579 (unless org-inhibit-startup
5580 (org-unmodified
5581 (when org-startup-with-beamer-mode (org-beamer-mode))
5582 (when org-startup-align-all-tables
5583 (org-table-map-tables #'org-table-align t))
5584 (when org-startup-with-inline-images (org-display-inline-images))
5585 (when org-startup-with-latex-preview (org-toggle-latex-fragment '(16)))
5586 (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))
5587 (when org-startup-truncated (setq truncate-lines t))
5588 (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
5589 (org-refresh-effort-properties)))
5590 ;; Try to set `org-hide' face correctly.
5591 (let ((foreground (org-find-invisible-foreground)))
5592 (when foreground
5593 (set-face-foreground 'org-hide foreground))))
5595 ;; Update `customize-package-emacs-version-alist'
5596 (add-to-list 'customize-package-emacs-version-alist
5597 '(Org ("8.0" . "24.4")
5598 ("8.1" . "24.4")
5599 ("8.2" . "24.4")
5600 ("8.2.7" . "24.4")
5601 ("8.3" . "26.1")
5602 ("9.0" . "26.1")
5603 ("9.1" . "26.1")))
5605 (defvar org-mode-transpose-word-syntax-table
5606 (let ((st (make-syntax-table text-mode-syntax-table)))
5607 (dolist (c org-emphasis-alist st)
5608 (modify-syntax-entry (string-to-char (car c)) "w p" st))))
5610 (when (fboundp 'abbrev-table-put)
5611 (abbrev-table-put org-mode-abbrev-table
5612 :parents (list text-mode-abbrev-table)))
5614 (defun org-find-invisible-foreground ()
5615 (let ((candidates (remove
5616 "unspecified-bg"
5617 (nconc
5618 (list (face-background 'default)
5619 (face-background 'org-default))
5620 (mapcar
5621 (lambda (alist)
5622 (when (boundp alist)
5623 (cdr (assq 'background-color (symbol-value alist)))))
5624 '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
5625 (list (face-foreground 'org-hide))))))
5626 (car (remove nil candidates))))
5628 (defun org-current-time (&optional rounding-minutes past)
5629 "Current time, possibly rounded to ROUNDING-MINUTES.
5630 When ROUNDING-MINUTES is not an integer, fall back on the car of
5631 `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
5632 the rounding returns a past time."
5633 (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
5634 (car org-time-stamp-rounding-minutes)))
5635 (time (decode-time)) res)
5636 (if (< r 1)
5637 (current-time)
5638 (setq res
5639 (apply 'encode-time
5640 (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
5641 (nthcdr 2 time))))
5642 (if (and past (< (float-time (time-subtract (current-time) res)) 0))
5643 (seconds-to-time (- (float-time res) (* r 60)))
5644 res))))
5646 (defun org-today ()
5647 "Return today date, considering `org-extend-today-until'."
5648 (time-to-days
5649 (time-subtract (current-time)
5650 (list 0 (* 3600 org-extend-today-until) 0))))
5652 ;;;; Font-Lock stuff, including the activators
5654 (defvar org-mouse-map (make-sparse-keymap))
5655 (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
5656 (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
5657 (when org-mouse-1-follows-link
5658 (org-defkey org-mouse-map [follow-link] 'mouse-face))
5659 (when org-tab-follows-link
5660 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
5661 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
5663 (require 'font-lock)
5665 (defconst org-non-link-chars "]\t\n\r<>")
5666 (defvar org-link-types-re nil
5667 "Matches a link that has a url-like prefix like \"http:\"")
5668 (defvar org-link-re-with-space nil
5669 "Matches a link with spaces, optional angular brackets around it.")
5670 (defvar org-link-re-with-space2 nil
5671 "Matches a link with spaces, optional angular brackets around it.")
5672 (defvar org-link-re-with-space3 nil
5673 "Matches a link with spaces, only for internal part in bracket links.")
5674 (defvar org-angle-link-re nil
5675 "Matches link with angular brackets, spaces are allowed.")
5676 (defvar org-plain-link-re nil
5677 "Matches plain link, without spaces.")
5678 (defvar org-bracket-link-regexp nil
5679 "Matches a link in double brackets.")
5680 (defvar org-bracket-link-analytic-regexp nil
5681 "Regular expression used to analyze links.
5682 Here is what the match groups contain after a match:
5683 1: http:
5684 2: http
5685 3: path
5686 4: [desc]
5687 5: desc")
5688 (defvar org-bracket-link-analytic-regexp++ nil
5689 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5690 (defvar org-any-link-re nil
5691 "Regular expression matching any link.")
5693 (defconst org-match-sexp-depth 3
5694 "Number of stacked braces for sub/superscript matching.")
5696 (defun org-create-multibrace-regexp (left right n)
5697 "Create a regular expression which will match a balanced sexp.
5698 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5699 as single character strings.
5700 The regexp returned will match the entire expression including the
5701 delimiters. It will also define a single group which contains the
5702 match except for the outermost delimiters. The maximum depth of
5703 stacked delimiters is N. Escaping delimiters is not possible."
5704 (let* ((nothing (concat "[^" left right "]*?"))
5705 (or "\\|")
5706 (re nothing)
5707 (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
5708 (while (> n 1)
5709 (setq n (1- n)
5710 re (concat re or next)
5711 next (concat "\\(?:" nothing left next right "\\)+" nothing)))
5712 (concat left "\\(" re "\\)" right)))
5714 (defconst org-match-substring-regexp
5715 (concat
5716 "\\(\\S-\\)\\([_^]\\)\\("
5717 "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
5718 "\\|"
5719 "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
5720 "\\|"
5721 "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
5722 "The regular expression matching a sub- or superscript.")
5724 (defconst org-match-substring-with-braces-regexp
5725 (concat
5726 "\\(\\S-\\)\\([_^]\\)"
5727 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
5728 "The regular expression matching a sub- or superscript, forcing braces.")
5730 (defun org-make-link-regexps ()
5731 "Update the link regular expressions.
5732 This should be called after the variable `org-link-parameters' has changed."
5733 (let ((types-re (regexp-opt (org-link-types) t)))
5734 (setq org-link-types-re
5735 (concat "\\`" types-re ":")
5736 org-link-re-with-space
5737 (concat "<?" types-re ":"
5738 "\\([^" org-non-link-chars " ]"
5739 "[^" org-non-link-chars "]*"
5740 "[^" org-non-link-chars " ]\\)>?")
5741 org-link-re-with-space2
5742 (concat "<?" types-re ":"
5743 "\\([^" org-non-link-chars " ]"
5744 "[^\t\n\r]*"
5745 "[^" org-non-link-chars " ]\\)>?")
5746 org-link-re-with-space3
5747 (concat "<?" types-re ":"
5748 "\\([^" org-non-link-chars " ]"
5749 "[^\t\n\r]*\\)")
5750 org-angle-link-re
5751 (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
5752 types-re)
5753 org-plain-link-re
5754 (concat
5755 "\\<" types-re ":"
5756 "\\([^][ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)")
5757 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5758 org-bracket-link-regexp
5759 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5760 org-bracket-link-analytic-regexp
5761 (concat
5762 "\\[\\["
5763 "\\(" types-re ":\\)?"
5764 "\\([^]]+\\)"
5765 "\\]"
5766 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5767 "\\]")
5768 org-bracket-link-analytic-regexp++
5769 (concat
5770 "\\[\\["
5771 "\\(" (regexp-opt (cons "coderef" (org-link-types)) t) ":\\)?"
5772 "\\([^]]+\\)"
5773 "\\]"
5774 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5775 "\\]")
5776 org-any-link-re
5777 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
5778 org-angle-link-re "\\)\\|\\("
5779 org-plain-link-re "\\)"))))
5781 (org-make-link-regexps)
5783 (defvar org-emph-face nil)
5785 (defun org-do-emphasis-faces (limit)
5786 "Run through the buffer and emphasize strings."
5787 (let ((quick-re (format "\\([%s]\\|^\\)\\([~=*/_+]\\)"
5788 (car org-emphasis-regexp-components))))
5789 (catch :exit
5790 (while (re-search-forward quick-re limit t)
5791 (let* ((marker (match-string 2))
5792 (verbatim? (member marker '("~" "="))))
5793 (when (save-excursion
5794 (goto-char (match-beginning 0))
5795 (and
5796 ;; Do not match table hlines.
5797 (not (and (equal marker "+")
5798 (org-match-line
5799 "[ \t]*\\(|[-+]+|?\\|\\+[-+]+\\+\\)[ \t]*$")))
5800 ;; Do not match headline stars. Do not consider
5801 ;; stars of a headline as closing marker for bold
5802 ;; markup either.
5803 (not (and (equal marker "*")
5804 (save-excursion
5805 (forward-char)
5806 (skip-chars-backward "*")
5807 (looking-at-p org-outline-regexp-bol))))
5808 ;; Match full emphasis markup regexp.
5809 (looking-at (if verbatim? org-verbatim-re org-emph-re))
5810 ;; Do not span over paragraph boundaries.
5811 (not (string-match-p org-element-paragraph-separate
5812 (match-string 2)))
5813 ;; Do not span over cells in table rows.
5814 (not (and (save-match-data (org-match-line "[ \t]*|"))
5815 (string-match-p "|" (match-string 4))))))
5816 (pcase-let ((`(,_ ,face ,_) (assoc marker org-emphasis-alist)))
5817 (font-lock-prepend-text-property
5818 (match-beginning 2) (match-end 2) 'face face)
5819 (when verbatim?
5820 (org-remove-flyspell-overlays-in
5821 (match-beginning 0) (match-end 0)))
5822 (add-text-properties (match-beginning 2) (match-end 2)
5823 '(font-lock-multiline t org-emphasis t))
5824 (when org-hide-emphasis-markers
5825 (add-text-properties (match-end 4) (match-beginning 5)
5826 '(invisible org-link))
5827 (add-text-properties (match-beginning 3) (match-end 3)
5828 '(invisible org-link)))
5829 (throw :exit t))))))))
5831 (defun org-emphasize (&optional char)
5832 "Insert or change an emphasis, i.e. a font like bold or italic.
5833 If there is an active region, change that region to a new emphasis.
5834 If there is no region, just insert the marker characters and position
5835 the cursor between them.
5836 CHAR should be the marker character. If it is a space, it means to
5837 remove the emphasis of the selected region.
5838 If CHAR is not given (for example in an interactive call) it will be
5839 prompted for."
5840 (interactive)
5841 (let ((erc org-emphasis-regexp-components)
5842 (string "") beg end move s)
5843 (if (org-region-active-p)
5844 (setq beg (region-beginning)
5845 end (region-end)
5846 string (buffer-substring beg end))
5847 (setq move t))
5849 (unless char
5850 (message "Emphasis marker or tag: [%s]"
5851 (mapconcat #'car org-emphasis-alist ""))
5852 (setq char (read-char-exclusive)))
5853 (if (equal char ?\s)
5854 (setq s ""
5855 move nil)
5856 (unless (assoc (char-to-string char) org-emphasis-alist)
5857 (user-error "No such emphasis marker: \"%c\"" char))
5858 (setq s (char-to-string char)))
5859 (while (and (> (length string) 1)
5860 (equal (substring string 0 1) (substring string -1))
5861 (assoc (substring string 0 1) org-emphasis-alist))
5862 (setq string (substring string 1 -1)))
5863 (setq string (concat s string s))
5864 (when beg (delete-region beg end))
5865 (unless (or (bolp)
5866 (string-match (concat "[" (nth 0 erc) "\n]")
5867 (char-to-string (char-before (point)))))
5868 (insert " "))
5869 (unless (or (eobp)
5870 (string-match (concat "[" (nth 1 erc) "\n]")
5871 (char-to-string (char-after (point)))))
5872 (insert " ") (backward-char 1))
5873 (insert string)
5874 (and move (backward-char 1))))
5876 (defconst org-nonsticky-props
5877 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
5879 (defsubst org-rear-nonsticky-at (pos)
5880 (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
5882 (defun org-activate-links (limit)
5883 "Add link properties to links.
5884 This includes angle, plain, and bracket links."
5885 (catch :exit
5886 (while (re-search-forward org-any-link-re limit t)
5887 (let* ((start (match-beginning 0))
5888 (end (match-end 0))
5889 (style (cond ((eq ?< (char-after start)) 'angle)
5890 ((eq ?\[ (char-after (1+ start))) 'bracket)
5891 (t 'plain))))
5892 (when (and (memq style org-highlight-links)
5893 ;; Do not confuse plain links with tags.
5894 (not (and (eq style 'plain)
5895 (let ((face (get-text-property
5896 (max (1- start) (point-min)) 'face)))
5897 (if (consp face) (memq 'org-tag face)
5898 (eq 'org-tag face))))))
5899 (let* ((link-object (save-excursion
5900 (goto-char start)
5901 (save-match-data (org-element-link-parser))))
5902 (link (org-element-property :raw-link link-object))
5903 (type (org-element-property :type link-object))
5904 (path (org-element-property :path link-object))
5905 (properties ;for link's visible part
5906 (list
5907 'face (pcase (org-link-get-parameter type :face)
5908 ((and (pred functionp) face) (funcall face path))
5909 ((and (pred facep) face) face)
5910 ((and (pred consp) face) face) ;anonymous
5911 (_ 'org-link))
5912 'mouse-face (or (org-link-get-parameter type :mouse-face)
5913 'highlight)
5914 'keymap (or (org-link-get-parameter type :keymap)
5915 org-mouse-map)
5916 'help-echo (pcase (org-link-get-parameter type :help-echo)
5917 ((and (pred stringp) echo) echo)
5918 ((and (pred functionp) echo) echo)
5919 (_ (concat "LINK: " link)))
5920 'htmlize-link (pcase (org-link-get-parameter type
5921 :htmlize-link)
5922 ((and (pred functionp) f) (funcall f))
5923 (_ `(:uri ,link)))
5924 'font-lock-multiline t)))
5925 (org-remove-flyspell-overlays-in start end)
5926 (org-rear-nonsticky-at end)
5927 (if (not (eq 'bracket style))
5928 (add-text-properties start end properties)
5929 ;; Handle invisible parts in bracket links.
5930 (remove-text-properties start end '(invisible nil))
5931 (let ((hidden
5932 (append `(invisible
5933 ,(or (org-link-get-parameter type :display)
5934 'org-link))
5935 properties))
5936 (visible-start (or (match-beginning 4) (match-beginning 2)))
5937 (visible-end (or (match-end 4) (match-end 2))))
5938 (add-text-properties start visible-start hidden)
5939 (add-text-properties visible-start visible-end properties)
5940 (add-text-properties visible-end end hidden)
5941 (org-rear-nonsticky-at visible-start)
5942 (org-rear-nonsticky-at visible-end)))
5943 (let ((f (org-link-get-parameter type :activate-func)))
5944 (when (functionp f)
5945 (funcall f start end path (eq style 'bracket))))
5946 (throw :exit t))))) ;signal success
5947 nil))
5949 (defun org-activate-code (limit)
5950 (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
5951 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5952 (remove-text-properties (match-beginning 0) (match-end 0)
5953 '(display t invisible t intangible t))
5956 (defcustom org-src-fontify-natively t
5957 "When non-nil, fontify code in code blocks.
5958 See also the `org-block' face."
5959 :type 'boolean
5960 :version "26.1"
5961 :package-version '(Org . "8.3")
5962 :group 'org-appearance
5963 :group 'org-babel)
5965 (defcustom org-allow-promoting-top-level-subtree nil
5966 "When non-nil, allow promoting a top level subtree.
5967 The leading star of the top level headline will be replaced
5968 by a #."
5969 :type 'boolean
5970 :version "24.1"
5971 :group 'org-appearance)
5973 (defun org-fontify-meta-lines-and-blocks (limit)
5974 (condition-case nil
5975 (org-fontify-meta-lines-and-blocks-1 limit)
5976 (error (message "Org mode fontification error in %S at %d"
5977 (current-buffer)
5978 (line-number-at-pos)))))
5980 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5981 "Fontify #+ lines and blocks."
5982 (let ((case-fold-search t))
5983 (when (re-search-forward
5984 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5985 limit t)
5986 (let ((beg (match-beginning 0))
5987 (block-start (match-end 0))
5988 (block-end nil)
5989 (lang (match-string 7))
5990 (beg1 (line-beginning-position 2))
5991 (dc1 (downcase (match-string 2)))
5992 (dc3 (downcase (match-string 3)))
5993 end end1 quoting block-type)
5994 (cond
5995 ((and (match-end 4) (equal dc3 "+begin"))
5996 ;; Truly a block
5997 (setq block-type (downcase (match-string 5))
5998 quoting (member block-type org-protecting-blocks))
5999 (when (re-search-forward
6000 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
6001 nil t) ;; on purpose, we look further than LIMIT
6002 (setq end (min (point-max) (match-end 0))
6003 end1 (min (point-max) (1- (match-beginning 0))))
6004 (setq block-end (match-beginning 0))
6005 (when quoting
6006 (org-remove-flyspell-overlays-in beg1 end1)
6007 (remove-text-properties beg end
6008 '(display t invisible t intangible t)))
6009 (add-text-properties
6010 beg end '(font-lock-fontified t font-lock-multiline t))
6011 (add-text-properties beg beg1 '(face org-meta-line))
6012 (org-remove-flyspell-overlays-in beg beg1)
6013 (add-text-properties ; For end_src
6014 end1 (min (point-max) (1+ end)) '(face org-meta-line))
6015 (org-remove-flyspell-overlays-in end1 end)
6016 (cond
6017 ((and lang (not (string= lang "")) org-src-fontify-natively)
6018 (org-src-font-lock-fontify-block lang block-start block-end)
6019 (add-text-properties beg1 block-end '(src-block t)))
6020 (quoting
6021 (add-text-properties beg1 (min (point-max) (1+ end1))
6022 (list 'face
6023 (list :inherit
6024 (let ((face-name
6025 (intern (format "org-block-%s" lang))))
6026 (append (and (facep face-name) (list face-name))
6027 '(org-block))))))) ; end of source block
6028 ((not org-fontify-quote-and-verse-blocks))
6029 ((string= block-type "quote")
6030 (add-face-text-property
6031 beg1 (min (point-max) (1+ end1)) 'org-quote t))
6032 ((string= block-type "verse")
6033 (add-face-text-property
6034 beg1 (min (point-max) (1+ end1)) 'org-verse t)))
6035 (add-text-properties beg beg1 '(face org-block-begin-line))
6036 (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
6037 '(face org-block-end-line))
6039 ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
6040 (org-remove-flyspell-overlays-in
6041 (match-beginning 0)
6042 (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
6043 (add-text-properties
6044 beg (match-end 3)
6045 (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
6046 '(font-lock-fontified t invisible t)
6047 '(font-lock-fontified t face org-document-info-keyword)))
6048 (add-text-properties
6049 (match-beginning 6) (min (point-max) (1+ (match-end 6)))
6050 (if (string-equal dc1 "+title:")
6051 '(font-lock-fontified t face org-document-title)
6052 '(font-lock-fontified t face org-document-info))))
6053 ((string-prefix-p "+caption" dc1)
6054 (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
6055 (remove-text-properties (match-beginning 0) (match-end 0)
6056 '(display t invisible t intangible t))
6057 ;; Handle short captions.
6058 (save-excursion
6059 (beginning-of-line)
6060 (looking-at "\\([ \t]*#\\+caption\\(?:\\[.*\\]\\)?:\\)[ \t]*"))
6061 (add-text-properties (line-beginning-position) (match-end 1)
6062 '(font-lock-fontified t face org-meta-line))
6063 (add-text-properties (match-end 0) (line-end-position)
6064 '(font-lock-fontified t face org-block))
6066 ((member dc3 '(" " ""))
6067 (org-remove-flyspell-overlays-in beg (match-end 0))
6068 (add-text-properties
6069 beg (match-end 0)
6070 '(font-lock-fontified t face font-lock-comment-face)))
6071 (t ;; just any other in-buffer setting, but not indented
6072 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
6073 (remove-text-properties (match-beginning 0) (match-end 0)
6074 '(display t invisible t intangible t))
6075 (add-text-properties beg (match-end 0)
6076 '(font-lock-fontified t face org-meta-line))
6077 t))))))
6079 (defun org-fontify-drawers (limit)
6080 "Fontify drawers."
6081 (when (re-search-forward org-drawer-regexp limit t)
6082 (add-text-properties
6083 (match-beginning 0) (match-end 0)
6084 '(font-lock-fontified t face org-special-keyword))
6085 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
6088 (defun org-fontify-macros (limit)
6089 "Fontify macros."
6090 (when (re-search-forward "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)" limit t)
6091 (let ((begin (match-beginning 0))
6092 (opening-end (match-beginning 1)))
6093 (when (and (re-search-forward "\n[ \t]*\n\\|\\(}}}\\)" limit t)
6094 (match-string 1))
6095 (let ((end (match-end 1))
6096 (closing-start (match-beginning 1)))
6097 (add-text-properties
6098 begin end
6099 '(font-lock-multiline t font-lock-fontified t face org-macro))
6100 (org-remove-flyspell-overlays-in begin end)
6101 (when org-hide-macro-markers
6102 (add-text-properties begin opening-end '(invisible t))
6103 (add-text-properties closing-start end '(invisible t)))
6104 t)))))
6106 (defun org-activate-footnote-links (limit)
6107 "Add text properties for footnotes."
6108 (let ((fn (org-footnote-next-reference-or-definition limit)))
6109 (when fn
6110 (let* ((beg (nth 1 fn))
6111 (end (nth 2 fn))
6112 (label (car fn))
6113 (referencep (/= (line-beginning-position) beg)))
6114 (when (and referencep (nth 3 fn))
6115 (save-excursion
6116 (goto-char beg)
6117 (search-forward (or label "fn:"))
6118 (org-remove-flyspell-overlays-in beg (match-end 0))))
6119 (add-text-properties beg end
6120 (list 'mouse-face 'highlight
6121 'keymap org-mouse-map
6122 'help-echo
6123 (if referencep "Footnote reference"
6124 "Footnote definition")
6125 'font-lock-fontified t
6126 'font-lock-multiline t
6127 'face 'org-footnote))))))
6129 (defun org-activate-dates (limit)
6130 "Add text properties for dates."
6131 (when (and (re-search-forward org-tsr-regexp-both limit t)
6132 (not (equal (char-before (match-beginning 0)) 91)))
6133 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
6134 (add-text-properties (match-beginning 0) (match-end 0)
6135 (list 'mouse-face 'highlight
6136 'keymap org-mouse-map))
6137 (org-rear-nonsticky-at (match-end 0))
6138 (when org-display-custom-times
6139 ;; If it's a date range, activate custom time for second date.
6140 (when (match-end 3)
6141 (org-display-custom-time (match-beginning 3) (match-end 3)))
6142 (org-display-custom-time (match-beginning 1) (match-end 1)))
6145 (defvar-local org-target-link-regexp nil
6146 "Regular expression matching radio targets in plain text.")
6148 (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
6149 (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
6150 border border border))
6151 "Regular expression matching a link target.")
6153 (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
6154 "Regular expression matching a radio target.")
6156 (defconst org-any-target-regexp
6157 (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
6158 "Regular expression matching any target.")
6160 (defun org-activate-target-links (limit)
6161 "Add text properties for target matches."
6162 (when org-target-link-regexp
6163 (let ((case-fold-search t))
6164 (when (re-search-forward org-target-link-regexp limit t)
6165 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
6166 (add-text-properties (match-beginning 1) (match-end 1)
6167 (list 'mouse-face 'highlight
6168 'keymap org-mouse-map
6169 'help-echo "Radio target link"
6170 'org-linked-text t))
6171 (org-rear-nonsticky-at (match-end 1))
6172 t))))
6174 (defun org-update-radio-target-regexp ()
6175 "Find all radio targets in this file and update the regular expression.
6176 Also refresh fontification if needed."
6177 (interactive)
6178 (let ((old-regexp org-target-link-regexp)
6179 (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
6180 (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
6181 (targets
6182 (org-with-wide-buffer
6183 (goto-char (point-min))
6184 (let (rtn)
6185 (while (re-search-forward org-radio-target-regexp nil t)
6186 ;; Make sure point is really within the object.
6187 (backward-char)
6188 (let ((obj (org-element-context)))
6189 (when (eq (org-element-type obj) 'radio-target)
6190 (cl-pushnew (org-element-property :value obj) rtn
6191 :test #'equal))))
6192 rtn))))
6193 (setq org-target-link-regexp
6194 (and targets
6195 (concat before-re
6196 (mapconcat
6197 (lambda (x)
6198 (replace-regexp-in-string
6199 " +" "\\s-+" (regexp-quote x) t t))
6200 targets
6201 "\\|")
6202 after-re)))
6203 (unless (equal old-regexp org-target-link-regexp)
6204 ;; Clean-up cache.
6205 (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
6206 ((not org-target-link-regexp) old-regexp)
6208 (concat before-re
6209 (mapconcat
6210 (lambda (re)
6211 (substring re (length before-re)
6212 (- (length after-re))))
6213 (list old-regexp org-target-link-regexp)
6214 "\\|")
6215 after-re)))))
6216 (org-with-wide-buffer
6217 (goto-char (point-min))
6218 (while (re-search-forward regexp nil t)
6219 (org-element-cache-refresh (match-beginning 1)))))
6220 ;; Re fontify buffer.
6221 (when (memq 'radio org-highlight-links)
6222 (org-restart-font-lock)))))
6224 (defun org-hide-wide-columns (limit)
6225 (let (s e)
6226 (setq s (text-property-any (point) (or limit (point-max))
6227 'org-cwidth t))
6228 (when s
6229 (setq e (next-single-property-change s 'org-cwidth))
6230 (add-text-properties s e '(invisible org-cwidth))
6231 (goto-char e)
6232 t)))
6234 (defvar org-latex-and-related-regexp nil
6235 "Regular expression for highlighting LaTeX, entities and sub/superscript.")
6237 (defun org-compute-latex-and-related-regexp ()
6238 "Compute regular expression for LaTeX, entities and sub/superscript.
6239 Result depends on variable `org-highlight-latex-and-related'."
6240 (setq-local
6241 org-latex-and-related-regexp
6242 (let* ((re-sub
6243 (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
6244 ((eq org-use-sub-superscripts '{})
6245 (list org-match-substring-with-braces-regexp))
6246 (org-use-sub-superscripts (list org-match-substring-regexp))))
6247 (re-latex
6248 (when (memq 'latex org-highlight-latex-and-related)
6249 (let ((matchers (plist-get org-format-latex-options :matchers)))
6250 (delq nil
6251 (mapcar (lambda (x)
6252 (and (member (car x) matchers) (nth 1 x)))
6253 org-latex-regexps)))))
6254 (re-entities
6255 (when (memq 'entities org-highlight-latex-and-related)
6256 (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
6257 (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
6259 (defun org-do-latex-and-related (limit)
6260 "Highlight LaTeX snippets and environments, entities and sub/superscript.
6261 LIMIT bounds the search for syntax to highlight. Stop at first
6262 highlighted object, if any. Return t if some highlighting was
6263 done, nil otherwise."
6264 (when (org-string-nw-p org-latex-and-related-regexp)
6265 (catch 'found
6266 (while (re-search-forward org-latex-and-related-regexp limit t)
6267 (unless
6268 (cl-some
6269 (lambda (f)
6270 (memq f '(org-code org-verbatim underline org-special-keyword)))
6271 (save-excursion
6272 (goto-char (1+ (match-beginning 0)))
6273 (face-at-point nil t)))
6274 (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
6275 '(?_ ?^))
6277 0)))
6278 (font-lock-prepend-text-property
6279 (+ offset (match-beginning 0)) (match-end 0)
6280 'face 'org-latex-and-related)
6281 (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
6282 '(font-lock-multiline t)))
6283 (throw 'found t)))
6284 nil)))
6286 (defun org-restart-font-lock ()
6287 "Restart `font-lock-mode', to force refontification."
6288 (when (and (boundp 'font-lock-mode) font-lock-mode)
6289 (font-lock-mode -1)
6290 (font-lock-mode 1)))
6292 (defun org-activate-tags (limit)
6293 (when (re-search-forward
6294 "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" limit t)
6295 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
6296 (add-text-properties (match-beginning 1) (match-end 1)
6297 (list 'mouse-face 'highlight
6298 'keymap org-mouse-map))
6299 (org-rear-nonsticky-at (match-end 1))
6302 (defun org-outline-level ()
6303 "Compute the outline level of the heading at point.
6305 If this is called at a normal headline, the level is the number
6306 of stars. Use `org-reduced-level' to remove the effect of
6307 `org-odd-levels'. Unlike to `org-current-level', this function
6308 takes into consideration inlinetasks."
6309 (org-with-wide-buffer
6310 (end-of-line)
6311 (if (re-search-backward org-outline-regexp-bol nil t)
6312 (1- (- (match-end 0) (match-beginning 0)))
6313 0)))
6315 (defvar org-font-lock-keywords nil)
6317 (defsubst org-re-property (property &optional literal allow-null value)
6318 "Return a regexp matching a PROPERTY line.
6320 When optional argument LITERAL is non-nil, do not quote PROPERTY.
6321 This is useful when PROPERTY is a regexp. When ALLOW-NULL is
6322 non-nil, match properties even without a value.
6324 Match group 3 is set to the value when it exists. If there is no
6325 value and ALLOW-NULL is non-nil, it is set to the empty string.
6327 With optional argument VALUE, match only property lines with
6328 that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
6329 unless LITERAL is non-nil."
6330 (concat
6331 "^\\(?4:[ \t]*\\)"
6332 (format "\\(?1::\\(?2:%s\\):\\)"
6333 (if literal property (regexp-quote property)))
6334 (cond (value
6335 (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
6336 (if literal value (regexp-quote value))))
6337 (allow-null
6338 "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
6340 "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
6342 (defconst org-property-re
6343 (org-re-property "\\S-+" 'literal t)
6344 "Regular expression matching a property line.
6345 There are four matching groups:
6346 1: :PROPKEY: including the leading and trailing colon,
6347 2: PROPKEY without the leading and trailing colon,
6348 3: PROPVAL without leading or trailing spaces,
6349 4: the indentation of the current line,
6350 5: trailing whitespace.")
6352 (defvar org-font-lock-hook nil
6353 "Functions to be called for special font lock stuff.")
6355 (defvar org-font-lock-extra-keywords nil) ;Dynamically scoped.
6357 (defvar org-font-lock-set-keywords-hook nil
6358 "Functions that can manipulate `org-font-lock-extra-keywords'.
6359 This is called after `org-font-lock-extra-keywords' is defined, but before
6360 it is installed to be used by font lock. This can be useful if something
6361 needs to be inserted at a specific position in the font-lock sequence.")
6363 (defun org-font-lock-hook (limit)
6364 "Run `org-font-lock-hook' within LIMIT."
6365 (run-hook-with-args 'org-font-lock-hook limit))
6367 (defun org-set-font-lock-defaults ()
6368 "Set font lock defaults for the current buffer."
6369 (let* ((em org-fontify-emphasized-text)
6370 (lk org-highlight-links)
6371 (org-font-lock-extra-keywords
6372 (list
6373 ;; Call the hook
6374 '(org-font-lock-hook)
6375 ;; Headlines
6376 `(,(if org-fontify-whole-heading-line
6377 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
6378 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
6379 (1 (org-get-level-face 1))
6380 (2 (org-get-level-face 2))
6381 (3 (org-get-level-face 3)))
6382 ;; Table lines
6383 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
6384 (1 'org-table t))
6385 ;; Table internals
6386 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
6387 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
6388 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
6389 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
6390 ;; Drawers
6391 '(org-fontify-drawers)
6392 ;; Properties
6393 (list org-property-re
6394 '(1 'org-special-keyword t)
6395 '(3 'org-property-value t))
6396 ;; Link related fontification.
6397 '(org-activate-links)
6398 (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
6399 (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
6400 (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
6401 (when (memq 'footnote lk) '(org-activate-footnote-links))
6402 ;; Targets.
6403 (list org-any-target-regexp '(0 'org-target t))
6404 ;; Diary sexps.
6405 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
6406 ;; Macro
6407 '(org-fontify-macros)
6408 '(org-hide-wide-columns (0 nil append))
6409 ;; TODO keyword
6410 (list (format org-heading-keyword-regexp-format
6411 org-todo-regexp)
6412 '(2 (org-get-todo-face 2) t))
6413 ;; DONE
6414 (if org-fontify-done-headline
6415 (list (format org-heading-keyword-regexp-format
6416 (concat
6417 "\\(?:"
6418 (mapconcat 'regexp-quote org-done-keywords "\\|")
6419 "\\)"))
6420 '(2 'org-headline-done t))
6421 nil)
6422 ;; Priorities
6423 '(org-font-lock-add-priority-faces)
6424 ;; Tags
6425 '(org-font-lock-add-tag-faces)
6426 ;; Tags groups
6427 (when (and org-group-tags org-tag-groups-alist)
6428 (list (concat org-outline-regexp-bol ".+\\(:"
6429 (regexp-opt (mapcar 'car org-tag-groups-alist))
6430 ":\\).*$")
6431 '(1 'org-tag-group prepend)))
6432 ;; Special keywords
6433 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
6434 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
6435 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
6436 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
6437 ;; Emphasis
6438 (when em '(org-do-emphasis-faces))
6439 ;; Checkboxes
6440 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
6441 1 'org-checkbox prepend)
6442 (when (cdr (assq 'checkbox org-list-automatic-rules))
6443 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
6444 (0 (org-get-checkbox-statistics-face) t)))
6445 ;; Description list items
6446 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
6447 1 'org-list-dt prepend)
6448 ;; ARCHIVEd headings
6449 (list (concat
6450 org-outline-regexp-bol
6451 "\\(.*:" org-archive-tag ":.*\\)")
6452 '(1 'org-archived prepend))
6453 ;; Specials
6454 '(org-do-latex-and-related)
6455 '(org-fontify-entities)
6456 '(org-raise-scripts)
6457 ;; Code
6458 '(org-activate-code (1 'org-code t))
6459 ;; COMMENT
6460 (list (format
6461 "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
6462 org-todo-regexp
6463 org-comment-string)
6464 '(9 'org-special-keyword t))
6465 ;; Blocks and meta lines
6466 '(org-fontify-meta-lines-and-blocks))))
6467 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
6468 (run-hooks 'org-font-lock-set-keywords-hook)
6469 ;; Now set the full font-lock-keywords
6470 (setq-local org-font-lock-keywords org-font-lock-extra-keywords)
6471 (setq-local font-lock-defaults
6472 '(org-font-lock-keywords t nil nil backward-paragraph))
6473 (kill-local-variable 'font-lock-keywords)
6474 nil))
6476 (defun org-toggle-pretty-entities ()
6477 "Toggle the composition display of entities as UTF8 characters."
6478 (interactive)
6479 (setq-local org-pretty-entities (not org-pretty-entities))
6480 (org-restart-font-lock)
6481 (if org-pretty-entities
6482 (message "Entities are now displayed as UTF8 characters")
6483 (save-restriction
6484 (widen)
6485 (decompose-region (point-min) (point-max))
6486 (message "Entities are now displayed as plain text"))))
6488 (defvar-local org-custom-properties-overlays nil
6489 "List of overlays used for custom properties.")
6491 (defun org-toggle-custom-properties-visibility ()
6492 "Display or hide properties in `org-custom-properties'."
6493 (interactive)
6494 (if org-custom-properties-overlays
6495 (progn (mapc #'delete-overlay org-custom-properties-overlays)
6496 (setq org-custom-properties-overlays nil))
6497 (when org-custom-properties
6498 (org-with-wide-buffer
6499 (goto-char (point-min))
6500 (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
6501 (while (re-search-forward regexp nil t)
6502 (let ((end (cdr (save-match-data (org-get-property-block)))))
6503 (when (and end (< (point) end))
6504 ;; Hide first custom property in current drawer.
6505 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
6506 (overlay-put o 'invisible t)
6507 (overlay-put o 'org-custom-property t)
6508 (push o org-custom-properties-overlays))
6509 ;; Hide additional custom properties in the same drawer.
6510 (while (re-search-forward regexp end t)
6511 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
6512 (overlay-put o 'invisible t)
6513 (overlay-put o 'org-custom-property t)
6514 (push o org-custom-properties-overlays)))))
6515 ;; Each entry is limited to a single property drawer.
6516 (outline-next-heading)))))))
6518 (defun org-fontify-entities (limit)
6519 "Find an entity to fontify."
6520 (let (ee)
6521 (when org-pretty-entities
6522 (catch 'match
6523 ;; "\_ "-family is left out on purpose. Only the first one,
6524 ;; i.e., "\_ ", could be fontified anyway, and it would be
6525 ;; confusing when adding a second white space character.
6526 (while (re-search-forward
6527 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
6528 limit t)
6529 (when (and (not (org-at-comment-p))
6530 (setq ee (org-entity-get (match-string 1)))
6531 (= (length (nth 6 ee)) 1))
6532 (let* ((end (if (equal (match-string 2) "{}")
6533 (match-end 2)
6534 (match-end 1))))
6535 (add-text-properties
6536 (match-beginning 0) end
6537 (list 'font-lock-fontified t))
6538 (compose-region (match-beginning 0) end
6539 (nth 6 ee) nil)
6540 (backward-char 1)
6541 (throw 'match t))))
6542 nil))))
6544 (defun org-fontify-like-in-org-mode (s &optional odd-levels)
6545 "Fontify string S like in Org mode."
6546 (with-temp-buffer
6547 (insert s)
6548 (let ((org-odd-levels-only odd-levels))
6549 (org-mode)
6550 (org-font-lock-ensure)
6551 (buffer-string))))
6553 (defvar org-m nil)
6554 (defvar org-l nil)
6555 (defvar org-f nil)
6556 (defun org-get-level-face (n)
6557 "Get the right face for match N in font-lock matching of headlines."
6558 (setq org-l (- (match-end 2) (match-beginning 1) 1))
6559 (when org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
6560 (if org-cycle-level-faces
6561 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
6562 (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
6563 (cond
6564 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
6565 ((eq n 2) org-f)
6566 (t (unless org-level-color-stars-only org-f))))
6568 (defun org-face-from-face-or-color (context inherit face-or-color)
6569 "Create a face list that inherits INHERIT, but sets the foreground color.
6570 When FACE-OR-COLOR is not a string, just return it."
6571 (if (stringp face-or-color)
6572 (list :inherit inherit
6573 (cdr (assoc context org-faces-easy-properties))
6574 face-or-color)
6575 face-or-color))
6577 (defun org-get-todo-face (kwd)
6578 "Get the right face for a TODO keyword KWD.
6579 If KWD is a number, get the corresponding match group."
6580 (when (numberp kwd) (setq kwd (match-string kwd)))
6581 (or (org-face-from-face-or-color
6582 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
6583 (and (member kwd org-done-keywords) 'org-done)
6584 'org-todo))
6586 (defun org-get-priority-face (priority)
6587 "Get the right face for PRIORITY.
6588 PRIORITY is a character."
6589 (or (org-face-from-face-or-color
6590 'priority 'org-priority (cdr (assq priority org-priority-faces)))
6591 'org-priority))
6593 (defun org-get-tag-face (tag)
6594 "Get the right face for TAG.
6595 If TAG is a number, get the corresponding match group."
6596 (let ((tag (if (wholenump tag) (match-string tag) tag)))
6597 (or (org-face-from-face-or-color
6598 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
6599 'org-tag)))
6601 (defun org-font-lock-add-priority-faces (limit)
6602 "Add the special priority faces."
6603 (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
6604 (add-text-properties
6605 (match-beginning 1) (match-end 1)
6606 (list 'face (org-get-priority-face (string-to-char (match-string 2)))
6607 'font-lock-fontified t))))
6609 (defun org-font-lock-add-tag-faces (limit)
6610 "Add the special tag faces."
6611 (when (and org-tag-faces org-tags-special-faces-re)
6612 (while (re-search-forward org-tags-special-faces-re limit t)
6613 (add-text-properties (match-beginning 1) (match-end 1)
6614 (list 'face (org-get-tag-face 1)
6615 'font-lock-fontified t))
6616 (backward-char 1))))
6618 (defun org-unfontify-region (beg end &optional _maybe_loudly)
6619 "Remove fontification and activation overlays from links."
6620 (font-lock-default-unfontify-region beg end)
6621 (let* ((buffer-undo-list t)
6622 (inhibit-read-only t) (inhibit-point-motion-hooks t)
6623 (inhibit-modification-hooks t)
6624 deactivate-mark buffer-file-name buffer-file-truename)
6625 (decompose-region beg end)
6626 (remove-text-properties beg end
6627 '(mouse-face t keymap t org-linked-text t
6628 invisible t intangible t
6629 org-emphasis t))
6630 (org-remove-font-lock-display-properties beg end)))
6632 (defconst org-script-display '(((raise -0.3) (height 0.7))
6633 ((raise 0.3) (height 0.7))
6634 ((raise -0.5))
6635 ((raise 0.5)))
6636 "Display properties for showing superscripts and subscripts.")
6638 (defun org-remove-font-lock-display-properties (beg end)
6639 "Remove specific display properties that have been added by font lock.
6640 The will remove the raise properties that are used to show superscripts
6641 and subscripts."
6642 (let (next prop)
6643 (while (< beg end)
6644 (setq next (next-single-property-change beg 'display nil end)
6645 prop (get-text-property beg 'display))
6646 (when (member prop org-script-display)
6647 (put-text-property beg next 'display nil))
6648 (setq beg next))))
6650 (defun org-raise-scripts (limit)
6651 "Add raise properties to sub/superscripts."
6652 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
6653 (re-search-forward
6654 (if (eq org-use-sub-superscripts t)
6655 org-match-substring-regexp
6656 org-match-substring-with-braces-regexp)
6657 limit t))
6658 (let* ((pos (point)) table-p comment-p
6659 (mpos (match-beginning 3))
6660 (emph-p (get-text-property mpos 'org-emphasis))
6661 (link-p (get-text-property mpos 'mouse-face))
6662 (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
6663 (goto-char (point-at-bol))
6664 (setq table-p (looking-at-p org-table-dataline-regexp)
6665 comment-p (looking-at-p "^[ \t]*#[ +]"))
6666 (goto-char pos)
6667 ;; Handle a_b^c
6668 (when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
6669 (unless (or comment-p emph-p link-p keyw-p)
6670 (put-text-property (match-beginning 3) (match-end 0)
6671 'display
6672 (if (equal (char-after (match-beginning 2)) ?^)
6673 (nth (if table-p 3 1) org-script-display)
6674 (nth (if table-p 2 0) org-script-display)))
6675 (add-text-properties (match-beginning 2) (match-end 2)
6676 (list 'invisible t))
6677 (when (and (eq (char-after (match-beginning 3)) ?{)
6678 (eq (char-before (match-end 3)) ?}))
6679 (add-text-properties (match-beginning 3) (1+ (match-beginning 3))
6680 (list 'invisible t))
6681 (add-text-properties (1- (match-end 3)) (match-end 3)
6682 (list 'invisible t))))
6683 t)))
6685 ;;;; Visibility cycling, including org-goto and indirect buffer
6687 ;;; Cycling
6689 (defvar-local org-cycle-global-status nil)
6690 (put 'org-cycle-global-status 'org-state t)
6691 (defvar-local org-cycle-subtree-status nil)
6692 (put 'org-cycle-subtree-status 'org-state t)
6694 (defvar org-inlinetask-min-level)
6696 (defun org-unlogged-message (&rest args)
6697 "Display a message, but avoid logging it in the *Messages* buffer."
6698 (let ((message-log-max nil))
6699 (apply 'message args)))
6701 ;;;###autoload
6702 (defun org-cycle (&optional arg)
6703 "TAB-action and visibility cycling for Org mode.
6705 This is the command invoked in Org mode by the `TAB' key. Its main
6706 purpose is outline visibility cycling, but it also invokes other actions
6707 in special contexts.
6709 When this function is called with a `\\[universal-argument]' prefix, rotate \
6710 the entire
6711 buffer through 3 states (global cycling)
6712 1. OVERVIEW: Show only top-level headlines.
6713 2. CONTENTS: Show all headlines of all levels, but no body text.
6714 3. SHOW ALL: Show everything.
6716 With a `\\[universal-argument] \\[universal-argument]' prefix argument, \
6717 switch to the startup visibility,
6718 determined by the variable `org-startup-folded', and by any VISIBILITY
6719 properties in the buffer.
6721 With a `\\[universal-argument] \\[universal-argument] \
6722 \\[universal-argument]' prefix argument, show the entire buffer, including
6723 any drawers.
6725 When inside a table, re-align the table and move to the next field.
6727 When point is at the beginning of a headline, rotate the subtree started
6728 by this line through 3 different states (local cycling)
6729 1. FOLDED: Only the main headline is shown.
6730 2. CHILDREN: The main headline and the direct children are shown.
6731 From this state, you can move to one of the children
6732 and zoom in further.
6733 3. SUBTREE: Show the entire subtree, including body text.
6734 If there is no subtree, switch directly from CHILDREN to FOLDED.
6736 When point is at the beginning of an empty headline and the variable
6737 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6738 of the headline by demoting and promoting it to likely levels. This
6739 speeds up creation document structure by pressing `TAB' once or several
6740 times right after creating a new headline.
6742 When there is a numeric prefix, go up to a heading with level ARG, do
6743 a `show-subtree' and return to the previous cursor position. If ARG
6744 is negative, go up that many levels.
6746 When point is not at the beginning of a headline, execute the global
6747 binding for `TAB', which is re-indenting the line. See the option
6748 `org-cycle-emulate-tab' for details.
6750 As a special case, if point is at the beginning of the buffer and there is
6751 no headline in line 1, this function will act as if called with prefix arg
6752 \(`\\[universal-argument] TAB', same as `S-TAB') also when called without \
6753 prefix arg, but only
6754 if the variable `org-cycle-global-at-bob' is t."
6755 (interactive "P")
6756 (org-load-modules-maybe)
6757 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
6758 (and org-cycle-level-after-item/entry-creation
6759 (or (org-cycle-level)
6760 (org-cycle-item-indentation))))
6761 (let* ((limit-level
6762 (or org-cycle-max-level
6763 (and (boundp 'org-inlinetask-min-level)
6764 org-inlinetask-min-level
6765 (1- org-inlinetask-min-level))))
6766 (nstars (and limit-level
6767 (if org-odd-levels-only
6768 (and limit-level (1- (* limit-level 2)))
6769 limit-level)))
6770 (org-outline-regexp
6771 (if (not (derived-mode-p 'org-mode))
6772 outline-regexp
6773 (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
6774 (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
6775 (not (looking-at org-outline-regexp))))
6776 (org-cycle-hook
6777 (if bob-special
6778 (delq 'org-optimize-window-after-visibility-change
6779 (copy-sequence org-cycle-hook))
6780 org-cycle-hook))
6781 (pos (point)))
6783 (cond
6785 ((equal arg '(16))
6786 (setq last-command 'dummy)
6787 (org-set-startup-visibility)
6788 (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
6790 ((equal arg '(64))
6791 (outline-show-all)
6792 (org-unlogged-message "Entire buffer visible, including drawers"))
6794 ((equal arg '(4)) (org-cycle-internal-global))
6796 ;; Try hiding block at point.
6797 ((org-hide-block-toggle-maybe))
6799 ;; Try cdlatex TAB completion
6800 ((org-try-cdlatex-tab))
6802 ;; Table: enter it or move to the next field.
6803 ((org-at-table-p 'any)
6804 (if (org-at-table.el-p)
6805 (message "%s" (substitute-command-keys "\\<org-mode-map>\
6806 Use `\\[org-edit-special]' to edit table.el tables"))
6807 (if arg (org-table-edit-field t)
6808 (org-table-justify-field-maybe)
6809 (call-interactively 'org-table-next-field))))
6811 ((run-hook-with-args-until-success 'org-tab-after-check-for-table-hook))
6813 ;; Global cycling: delegate to `org-cycle-internal-global'.
6814 (bob-special (org-cycle-internal-global))
6816 ;; Drawers: delegate to `org-flag-drawer'.
6817 ((save-excursion
6818 (beginning-of-line 1)
6819 (looking-at org-drawer-regexp))
6820 (org-flag-drawer ; toggle block visibility
6821 (not (get-char-property (match-end 0) 'invisible))))
6823 ;; Show-subtree, ARG levels up from here.
6824 ((integerp arg)
6825 (save-excursion
6826 (org-back-to-heading)
6827 (outline-up-heading (if (< arg 0) (- arg)
6828 (- (funcall outline-level) arg)))
6829 (org-show-subtree)))
6831 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6832 ((and (featurep 'org-inlinetask)
6833 (org-inlinetask-at-task-p)
6834 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
6835 (org-inlinetask-toggle-visibility))
6837 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6838 ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
6839 (save-excursion (move-beginning-of-line 1)
6840 (looking-at org-outline-regexp)))
6841 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
6842 (org-cycle-internal-local))
6844 ;; From there: TAB emulation and template completion.
6845 (buffer-read-only (org-back-to-heading))
6847 ((run-hook-with-args-until-success
6848 'org-tab-after-check-for-cycling-hook))
6850 ((org-try-structure-completion))
6852 ((run-hook-with-args-until-success
6853 'org-tab-before-tab-emulation-hook))
6855 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
6856 (or (not (bolp))
6857 (not (looking-at org-outline-regexp))))
6858 (call-interactively (global-key-binding "\t")))
6860 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
6861 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6862 (or (and (eq org-cycle-emulate-tab 'white)
6863 (= (match-end 0) (point-at-eol)))
6864 (and (eq org-cycle-emulate-tab 'whitestart)
6865 (>= (match-end 0) pos))))
6867 (eq org-cycle-emulate-tab t))
6868 (call-interactively (global-key-binding "\t")))
6870 (t (save-excursion
6871 (org-back-to-heading)
6872 (org-cycle)))))))
6874 (defun org-cycle-internal-global ()
6875 "Do the global cycling action."
6876 ;; Hack to avoid display of messages for .org attachments in Gnus
6877 (let ((ga (string-match "\\*fontification" (buffer-name))))
6878 (cond
6879 ((and (eq last-command this-command)
6880 (eq org-cycle-global-status 'overview))
6881 ;; We just created the overview - now do table of contents
6882 ;; This can be slow in very large buffers, so indicate action
6883 (run-hook-with-args 'org-pre-cycle-hook 'contents)
6884 (unless ga (org-unlogged-message "CONTENTS..."))
6885 (org-content)
6886 (unless ga (org-unlogged-message "CONTENTS...done"))
6887 (setq org-cycle-global-status 'contents)
6888 (run-hook-with-args 'org-cycle-hook 'contents))
6890 ((and (eq last-command this-command)
6891 (eq org-cycle-global-status 'contents))
6892 ;; We just showed the table of contents - now show everything
6893 (run-hook-with-args 'org-pre-cycle-hook 'all)
6894 (outline-show-all)
6895 (unless ga (org-unlogged-message "SHOW ALL"))
6896 (setq org-cycle-global-status 'all)
6897 (run-hook-with-args 'org-cycle-hook 'all))
6900 ;; Default action: go to overview
6901 (run-hook-with-args 'org-pre-cycle-hook 'overview)
6902 (org-overview)
6903 (unless ga (org-unlogged-message "OVERVIEW"))
6904 (setq org-cycle-global-status 'overview)
6905 (run-hook-with-args 'org-cycle-hook 'overview)))))
6907 (defvar org-called-with-limited-levels nil
6908 "Non-nil when `org-with-limited-levels' is currently active.")
6910 (defun org-invisible-p (&optional pos)
6911 "Non-nil if the character after POS is invisible.
6912 If POS is nil, use `point' instead."
6913 (get-char-property (or pos (point)) 'invisible))
6915 (defun org-cycle-internal-local ()
6916 "Do the local cycling action."
6917 (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
6918 ;; First, determine end of headline (EOH), end of subtree or item
6919 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6920 (save-excursion
6921 (if (org-at-item-p)
6922 (progn
6923 (beginning-of-line)
6924 (setq struct (org-list-struct))
6925 (setq eoh (point-at-eol))
6926 (setq eos (org-list-get-item-end-before-blank (point) struct))
6927 (setq has-children (org-list-has-child-p (point) struct)))
6928 (org-back-to-heading)
6929 (setq eoh (save-excursion (outline-end-of-heading) (point)))
6930 (setq eos (save-excursion (org-end-of-subtree t t)
6931 (when (bolp) (backward-char)) (point)))
6932 (setq has-children
6933 (or (save-excursion
6934 (let ((level (funcall outline-level)))
6935 (outline-next-heading)
6936 (and (org-at-heading-p t)
6937 (> (funcall outline-level) level))))
6938 (save-excursion
6939 (org-list-search-forward (org-item-beginning-re) eos t)))))
6940 ;; Determine end invisible part of buffer (EOL)
6941 (beginning-of-line 2)
6942 (while (and (not (eobp)) ;This is like `next-line'.
6943 (get-char-property (1- (point)) 'invisible))
6944 (goto-char (next-single-char-property-change (point) 'invisible))
6945 (and (eolp) (beginning-of-line 2)))
6946 (setq eol (point)))
6947 ;; Find out what to do next and set `this-command'
6948 (cond
6949 ((= eos eoh)
6950 ;; Nothing is hidden behind this heading
6951 (unless (org-before-first-heading-p)
6952 (run-hook-with-args 'org-pre-cycle-hook 'empty))
6953 (org-unlogged-message "EMPTY ENTRY")
6954 (setq org-cycle-subtree-status nil)
6955 (save-excursion
6956 (goto-char eos)
6957 (outline-next-heading)
6958 (when (org-invisible-p) (org-flag-heading nil))))
6959 ((and (or (>= eol eos)
6960 (not (string-match "\\S-" (buffer-substring eol eos))))
6961 (or has-children
6962 (not (setq children-skipped
6963 org-cycle-skip-children-state-if-no-children))))
6964 ;; Entire subtree is hidden in one line: children view
6965 (unless (org-before-first-heading-p)
6966 (run-hook-with-args 'org-pre-cycle-hook 'children))
6967 (if (org-at-item-p)
6968 (org-list-set-item-visibility (point-at-bol) struct 'children)
6969 (org-show-entry)
6970 (org-with-limited-levels (org-show-children))
6971 (org-show-set-visibility 'canonical)
6972 ;; FIXME: This slows down the func way too much.
6973 ;; How keep drawers hidden in subtree anyway?
6974 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6975 ;; (org-cycle-hide-drawers 'subtree))
6977 ;; Fold every list in subtree to top-level items.
6978 (when (eq org-cycle-include-plain-lists 'integrate)
6979 (save-excursion
6980 (org-back-to-heading)
6981 (while (org-list-search-forward (org-item-beginning-re) eos t)
6982 (beginning-of-line 1)
6983 (let* ((struct (org-list-struct))
6984 (prevs (org-list-prevs-alist struct))
6985 (end (org-list-get-bottom-point struct)))
6986 (dolist (e (org-list-get-all-items (point) struct prevs))
6987 (org-list-set-item-visibility e struct 'folded))
6988 (goto-char (if (< end eos) end eos)))))))
6989 (org-unlogged-message "CHILDREN")
6990 (save-excursion
6991 (goto-char eos)
6992 (outline-next-heading)
6993 (when (org-invisible-p) (org-flag-heading nil)))
6994 (setq org-cycle-subtree-status 'children)
6995 (unless (org-before-first-heading-p)
6996 (run-hook-with-args 'org-cycle-hook 'children)))
6997 ((or children-skipped
6998 (and (eq last-command this-command)
6999 (eq org-cycle-subtree-status 'children)))
7000 ;; We just showed the children, or no children are there,
7001 ;; now show everything.
7002 (unless (org-before-first-heading-p)
7003 (run-hook-with-args 'org-pre-cycle-hook 'subtree))
7004 (outline-flag-region eoh eos nil)
7005 (org-unlogged-message
7006 (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
7007 (setq org-cycle-subtree-status 'subtree)
7008 (unless (org-before-first-heading-p)
7009 (run-hook-with-args 'org-cycle-hook 'subtree)))
7011 ;; Default action: hide the subtree.
7012 (run-hook-with-args 'org-pre-cycle-hook 'folded)
7013 (outline-flag-region eoh eos t)
7014 (org-unlogged-message "FOLDED")
7015 (setq org-cycle-subtree-status 'folded)
7016 (unless (org-before-first-heading-p)
7017 (run-hook-with-args 'org-cycle-hook 'folded))))))
7019 ;;;###autoload
7020 (defun org-global-cycle (&optional arg)
7021 "Cycle the global visibility. For details see `org-cycle'.
7022 With `\\[universal-argument]' prefix ARG, switch to startup visibility.
7023 With a numeric prefix, show all headlines up to that level."
7024 (interactive "P")
7025 (let ((org-cycle-include-plain-lists
7026 (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
7027 (cond
7028 ((integerp arg)
7029 (outline-show-all)
7030 (outline-hide-sublevels arg)
7031 (setq org-cycle-global-status 'contents))
7032 ((equal arg '(4))
7033 (org-set-startup-visibility)
7034 (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
7036 (org-cycle '(4))))))
7038 (defun org-set-startup-visibility ()
7039 "Set the visibility required by startup options and properties."
7040 (cond
7041 ((eq org-startup-folded t)
7042 (org-overview))
7043 ((eq org-startup-folded 'content)
7044 (org-content))
7045 ((or (eq org-startup-folded 'showeverything)
7046 (eq org-startup-folded nil))
7047 (outline-show-all)))
7048 (unless (eq org-startup-folded 'showeverything)
7049 (when org-hide-block-startup (org-hide-block-all))
7050 (org-set-visibility-according-to-property 'no-cleanup)
7051 (org-cycle-hide-archived-subtrees 'all)
7052 (org-cycle-hide-drawers 'all)
7053 (org-cycle-show-empty-lines t)))
7055 (defun org-set-visibility-according-to-property (&optional no-cleanup)
7056 "Switch subtree visibilities according to :VISIBILITY: property."
7057 (interactive)
7058 (org-with-wide-buffer
7059 (goto-char (point-min))
7060 (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
7061 (if (not (org-at-property-p)) (outline-next-heading)
7062 (let ((state (match-string 3)))
7063 (save-excursion
7064 (org-back-to-heading t)
7065 (outline-hide-subtree)
7066 (org-reveal))
7067 (cond
7068 ((equal state "folded")
7069 (outline-hide-subtree)
7070 (org-end-of-subtree t t))
7071 ((equal state "children")
7072 (org-show-hidden-entry)
7073 (org-show-children))
7074 ((equal state "content")
7075 (save-excursion
7076 (save-restriction
7077 (org-narrow-to-subtree)
7078 (org-content)))
7079 (org-end-of-subtree t t))
7080 ((member state '("all" "showall"))
7081 (outline-show-subtree))))))
7082 (unless no-cleanup
7083 (org-cycle-hide-archived-subtrees 'all)
7084 (org-cycle-hide-drawers 'all)
7085 (org-cycle-show-empty-lines 'all))))
7087 ;; This function uses outline-regexp instead of the more fundamental
7088 ;; org-outline-regexp so that org-cycle-global works outside of Org
7089 ;; buffers, where outline-regexp is needed.
7090 (defun org-overview ()
7091 "Switch to overview mode, showing only top-level headlines.
7092 This shows all headlines with a level equal or greater than the level
7093 of the first headline in the buffer. This is important, because if the
7094 first headline is not level one, then (hide-sublevels 1) gives confusing
7095 results."
7096 (interactive)
7097 (save-excursion
7098 (let ((level
7099 (save-excursion
7100 (goto-char (point-min))
7101 (when (re-search-forward (concat "^" outline-regexp) nil t)
7102 (goto-char (match-beginning 0))
7103 (funcall outline-level)))))
7104 (and level (outline-hide-sublevels level)))))
7106 (defun org-content (&optional arg)
7107 "Show all headlines in the buffer, like a table of contents.
7108 With numerical argument N, show content up to level N."
7109 (interactive "P")
7110 (org-overview)
7111 (save-excursion
7112 ;; Visit all headings and show their offspring
7113 (and (integerp arg) (org-overview))
7114 (goto-char (point-max))
7115 (catch 'exit
7116 (while (and (progn (condition-case nil
7117 (outline-previous-visible-heading 1)
7118 (error (goto-char (point-min))))
7120 (looking-at org-outline-regexp))
7121 (if (integerp arg)
7122 (org-show-children (1- arg))
7123 (outline-show-branches))
7124 (when (bobp) (throw 'exit nil))))))
7126 (defun org-optimize-window-after-visibility-change (state)
7127 "Adjust the window after a change in outline visibility.
7128 This function is the default value of the hook `org-cycle-hook'."
7129 (when (get-buffer-window (current-buffer))
7130 (cond
7131 ((eq state 'content) nil)
7132 ((eq state 'all) nil)
7133 ((eq state 'folded) nil)
7134 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
7135 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
7137 (defun org-remove-empty-overlays-at (pos)
7138 "Remove outline overlays that do not contain non-white stuff."
7139 (dolist (o (overlays-at pos))
7140 (and (eq 'outline (overlay-get o 'invisible))
7141 (not (string-match "\\S-" (buffer-substring (overlay-start o)
7142 (overlay-end o))))
7143 (delete-overlay o))))
7145 (defun org-clean-visibility-after-subtree-move ()
7146 "Fix visibility issues after moving a subtree."
7147 ;; First, find a reasonable region to look at:
7148 ;; Start two siblings above, end three below
7149 (let* ((beg (save-excursion
7150 (and (org-get-last-sibling)
7151 (org-get-last-sibling))
7152 (point)))
7153 (end (save-excursion
7154 (and (org-get-next-sibling)
7155 (org-get-next-sibling)
7156 (org-get-next-sibling))
7157 (if (org-at-heading-p)
7158 (point-at-eol)
7159 (point))))
7160 (level (looking-at "\\*+"))
7161 (re (when level (concat "^" (regexp-quote (match-string 0)) " "))))
7162 (save-excursion
7163 (save-restriction
7164 (narrow-to-region beg end)
7165 (when re
7166 ;; Properly fold already folded siblings
7167 (goto-char (point-min))
7168 (while (re-search-forward re nil t)
7169 (when (and (not (org-invisible-p))
7170 (save-excursion
7171 (goto-char (point-at-eol)) (org-invisible-p)))
7172 (outline-hide-entry))))
7173 (org-cycle-show-empty-lines 'overview)
7174 (org-cycle-hide-drawers 'overview)))))
7176 (defun org-cycle-show-empty-lines (state)
7177 "Show empty lines above all visible headlines.
7178 The region to be covered depends on STATE when called through
7179 `org-cycle-hook'. Lisp program can use t for STATE to get the
7180 entire buffer covered. Note that an empty line is only shown if there
7181 are at least `org-cycle-separator-lines' empty lines before the headline."
7182 (when (/= org-cycle-separator-lines 0)
7183 (save-excursion
7184 (let* ((n (abs org-cycle-separator-lines))
7185 (re (cond
7186 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
7187 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
7188 (t (let ((ns (number-to-string (- n 2))))
7189 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
7190 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
7191 beg end)
7192 (cond
7193 ((memq state '(overview contents t))
7194 (setq beg (point-min) end (point-max)))
7195 ((memq state '(children folded))
7196 (setq beg (point)
7197 end (progn (org-end-of-subtree t t)
7198 (line-beginning-position 2)))))
7199 (when beg
7200 (goto-char beg)
7201 (while (re-search-forward re end t)
7202 (unless (get-char-property (match-end 1) 'invisible)
7203 (let ((e (match-end 1))
7204 (b (if (>= org-cycle-separator-lines 0)
7205 (match-beginning 1)
7206 (save-excursion
7207 (goto-char (match-beginning 0))
7208 (skip-chars-backward " \t\n")
7209 (line-end-position)))))
7210 (outline-flag-region b e nil))))))))
7211 ;; Never hide empty lines at the end of the file.
7212 (save-excursion
7213 (goto-char (point-max))
7214 (outline-previous-heading)
7215 (outline-end-of-heading)
7216 (when (and (looking-at "[ \t\n]+")
7217 (= (match-end 0) (point-max)))
7218 (outline-flag-region (point) (match-end 0) nil))))
7220 (defun org-show-empty-lines-in-parent ()
7221 "Move to the parent and re-show empty lines before visible headlines."
7222 (save-excursion
7223 (let ((context (if (org-up-heading-safe) 'children 'overview)))
7224 (org-cycle-show-empty-lines context))))
7226 (defun org-files-list ()
7227 "Return `org-agenda-files' list, plus all open Org files.
7228 This is useful for operations that need to scan all of a user's
7229 open and agenda-wise Org files."
7230 (let ((files (mapcar #'expand-file-name (org-agenda-files))))
7231 (dolist (buf (buffer-list))
7232 (with-current-buffer buf
7233 (when (and (derived-mode-p 'org-mode) (buffer-file-name))
7234 (cl-pushnew (expand-file-name (buffer-file-name)) files
7235 :test #'equal))))
7236 files))
7238 (defsubst org-entry-beginning-position ()
7239 "Return the beginning position of the current entry."
7240 (save-excursion (org-back-to-heading t) (point)))
7242 (defsubst org-entry-end-position ()
7243 "Return the end position of the current entry."
7244 (save-excursion (outline-next-heading) (point)))
7246 (defun org-cycle-hide-drawers (state &optional exceptions)
7247 "Re-hide all drawers after a visibility state change.
7248 STATE should be one of the symbols listed in the docstring of
7249 `org-cycle-hook'. When non-nil, optional argument EXCEPTIONS is
7250 a list of strings specifying which drawers should not be hidden."
7251 (when (and (derived-mode-p 'org-mode)
7252 (not (memq state '(overview folded contents))))
7253 (save-excursion
7254 (let* ((globalp (eq state 'all))
7255 (beg (if globalp (point-min) (point)))
7256 (end (if globalp (point-max)
7257 (if (eq state 'children)
7258 (save-excursion (outline-next-heading) (point))
7259 (org-end-of-subtree t)))))
7260 (goto-char beg)
7261 (while (re-search-forward org-drawer-regexp (max end (point)) t)
7262 (unless (member-ignore-case (match-string 1) exceptions)
7263 (let ((drawer (org-element-at-point)))
7264 (when (memq (org-element-type drawer) '(drawer property-drawer))
7265 (org-flag-drawer t drawer)
7266 ;; Make sure to skip drawer entirely or we might flag
7267 ;; it another time when matching its ending line with
7268 ;; `org-drawer-regexp'.
7269 (goto-char (org-element-property :end drawer))))))))))
7271 (defun org-flag-drawer (flag &optional element)
7272 "When FLAG is non-nil, hide the drawer we are at.
7273 Otherwise make it visible. When optional argument ELEMENT is
7274 a parsed drawer, as returned by `org-element-at-point', hide or
7275 show that drawer instead."
7276 (let ((drawer (or element
7277 (and (save-excursion
7278 (beginning-of-line)
7279 (looking-at-p org-drawer-regexp))
7280 (org-element-at-point)))))
7281 (when (memq (org-element-type drawer) '(drawer property-drawer))
7282 (let ((post (org-element-property :post-affiliated drawer)))
7283 (save-excursion
7284 (outline-flag-region
7285 (progn (goto-char post) (line-end-position))
7286 (progn (goto-char (org-element-property :end drawer))
7287 (skip-chars-backward " \r\t\n")
7288 (line-end-position))
7289 flag))
7290 ;; When the drawer is hidden away, make sure point lies in
7291 ;; a visible part of the buffer.
7292 (when (and flag (> (line-beginning-position) post))
7293 (goto-char post))))))
7295 (defun org-subtree-end-visible-p ()
7296 "Is the end of the current subtree visible?"
7297 (pos-visible-in-window-p
7298 (save-excursion (org-end-of-subtree t) (point))))
7300 (defun org-first-headline-recenter ()
7301 "Move cursor to the first headline and recenter the headline."
7302 (let ((window (get-buffer-window)))
7303 (when window
7304 (goto-char (point-min))
7305 (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
7306 (set-window-start window (line-beginning-position))))))
7308 ;;; Saving and restoring visibility
7310 (defun org-outline-overlay-data (&optional use-markers)
7311 "Return a list of the locations of all outline overlays.
7312 These are overlays with the `invisible' property value `outline'.
7313 The return value is a list of cons cells, with start and stop
7314 positions for each overlay.
7315 If USE-MARKERS is set, return the positions as markers."
7316 (let (beg end)
7317 (org-with-wide-buffer
7318 (delq nil
7319 (mapcar (lambda (o)
7320 (when (eq (overlay-get o 'invisible) 'outline)
7321 (setq beg (overlay-start o)
7322 end (overlay-end o))
7323 (and beg end (> end beg)
7324 (if use-markers
7325 (cons (copy-marker beg)
7326 (copy-marker end t))
7327 (cons beg end)))))
7328 (overlays-in (point-min) (point-max)))))))
7330 (defun org-set-outline-overlay-data (data)
7331 "Create visibility overlays for all positions in DATA.
7332 DATA should have been made by `org-outline-overlay-data'."
7333 (org-with-wide-buffer
7334 (outline-show-all)
7335 (dolist (c data) (outline-flag-region (car c) (cdr c) t))))
7337 ;;; Folding of blocks
7339 (defvar-local org-hide-block-overlays nil
7340 "Overlays hiding blocks.")
7342 (defun org-block-map (function &optional start end)
7343 "Call FUNCTION at the head of all source blocks in the current buffer.
7344 Optional arguments START and END can be used to limit the range."
7345 (let ((start (or start (point-min)))
7346 (end (or end (point-max))))
7347 (save-excursion
7348 (goto-char start)
7349 (while (and (< (point) end) (re-search-forward org-block-regexp end t))
7350 (save-excursion
7351 (save-match-data
7352 (goto-char (match-beginning 0))
7353 (funcall function)))))))
7355 (defun org-hide-block-toggle-all ()
7356 "Toggle the visibility of all blocks in the current buffer."
7357 (org-block-map 'org-hide-block-toggle))
7359 (defun org-hide-block-all ()
7360 "Fold all blocks in the current buffer."
7361 (interactive)
7362 (org-show-block-all)
7363 (org-block-map 'org-hide-block-toggle-maybe))
7365 (defun org-show-block-all ()
7366 "Unfold all blocks in the current buffer."
7367 (interactive)
7368 (mapc #'delete-overlay org-hide-block-overlays)
7369 (setq org-hide-block-overlays nil))
7371 (defun org-hide-block-toggle-maybe ()
7372 "Toggle visibility of block at point.
7373 Unlike to `org-hide-block-toggle', this function does not throw
7374 an error. Return a non-nil value when toggling is successful."
7375 (interactive)
7376 (ignore-errors (org-hide-block-toggle)))
7378 (defun org-hide-block-toggle (&optional force)
7379 "Toggle the visibility of the current block.
7380 When optional argument FORCE is `off', make block visible. If it
7381 is non-nil, hide it unconditionally. Throw an error when not at
7382 a block. Return a non-nil value when toggling is successful."
7383 (interactive)
7384 (let ((element (org-element-at-point)))
7385 (unless (memq (org-element-type element)
7386 '(center-block comment-block dynamic-block example-block
7387 export-block quote-block special-block
7388 src-block verse-block))
7389 (user-error "Not at a block"))
7390 (let* ((start (save-excursion
7391 (goto-char (org-element-property :post-affiliated element))
7392 (line-end-position)))
7393 (end (save-excursion
7394 (goto-char (org-element-property :end element))
7395 (skip-chars-backward " \r\t\n")
7396 (line-end-position)))
7397 (overlays (overlays-at start)))
7398 (cond
7399 ;; Do nothing when not before or at the block opening line or
7400 ;; at the block closing line.
7401 ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
7402 ((and (not (eq force 'off))
7403 (not (memq t (mapcar
7404 (lambda (o)
7405 (eq (overlay-get o 'invisible) 'org-hide-block))
7406 overlays))))
7407 (let ((ov (make-overlay start end)))
7408 (overlay-put ov 'invisible 'org-hide-block)
7409 ;; Make the block accessible to `isearch'.
7410 (overlay-put
7411 ov 'isearch-open-invisible
7412 (lambda (ov)
7413 (when (memq ov org-hide-block-overlays)
7414 (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
7415 (when (eq (overlay-get ov 'invisible) 'org-hide-block)
7416 (delete-overlay ov))))
7417 (push ov org-hide-block-overlays)
7418 ;; When the block is hidden away, make sure point is left in
7419 ;; a visible part of the buffer.
7420 (when (> (line-beginning-position) start)
7421 (goto-char start)
7422 (beginning-of-line))
7423 ;; Signal successful toggling.
7425 ((or (not force) (eq force 'off))
7426 (dolist (ov overlays t)
7427 (when (memq ov org-hide-block-overlays)
7428 (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
7429 (when (eq (overlay-get ov 'invisible) 'org-hide-block)
7430 (delete-overlay ov))))))))
7432 ;; Remove overlays when changing major mode
7433 (add-hook 'org-mode-hook
7434 (lambda () (add-hook 'change-major-mode-hook
7435 'org-show-block-all 'append 'local)))
7437 ;;; Org-goto
7439 (defvar org-goto-window-configuration nil)
7440 (defvar org-goto-marker nil)
7441 (defvar org-goto-map)
7442 (defun org-goto-map ()
7443 "Set the keymap `org-goto'."
7444 (setq org-goto-map
7445 (let ((map (make-sparse-keymap)))
7446 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
7447 mouse-drag-region universal-argument org-occur)))
7448 (dolist (cmd cmds)
7449 (substitute-key-definition cmd cmd map global-map)))
7450 (suppress-keymap map)
7451 (org-defkey map "\C-m" 'org-goto-ret)
7452 (org-defkey map [(return)] 'org-goto-ret)
7453 (org-defkey map [(left)] 'org-goto-left)
7454 (org-defkey map [(right)] 'org-goto-right)
7455 (org-defkey map [(control ?g)] 'org-goto-quit)
7456 (org-defkey map "\C-i" 'org-cycle)
7457 (org-defkey map [(tab)] 'org-cycle)
7458 (org-defkey map [(down)] 'outline-next-visible-heading)
7459 (org-defkey map [(up)] 'outline-previous-visible-heading)
7460 (if org-goto-auto-isearch
7461 (if (fboundp 'define-key-after)
7462 (define-key-after map [t] 'org-goto-local-auto-isearch)
7463 nil)
7464 (org-defkey map "q" 'org-goto-quit)
7465 (org-defkey map "n" 'outline-next-visible-heading)
7466 (org-defkey map "p" 'outline-previous-visible-heading)
7467 (org-defkey map "f" 'outline-forward-same-level)
7468 (org-defkey map "b" 'outline-backward-same-level)
7469 (org-defkey map "u" 'outline-up-heading))
7470 (org-defkey map "/" 'org-occur)
7471 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
7472 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
7473 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
7474 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
7475 (org-defkey map "\C-c\C-u" 'outline-up-heading)
7476 map)))
7478 (defconst org-goto-help
7479 "Browse buffer copy, to find location or copy text.%s
7480 RET=jump to location C-g=quit and return to previous location
7481 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
7483 (defvar org-goto-start-pos) ; dynamically scoped parameter
7485 (defun org-goto (&optional alternative-interface)
7486 "Look up a different location in the current file, keeping current visibility.
7488 When you want look-up or go to a different location in a
7489 document, the fastest way is often to fold the entire buffer and
7490 then dive into the tree. This method has the disadvantage, that
7491 the previous location will be folded, which may not be what you
7492 want.
7494 This command works around this by showing a copy of the current
7495 buffer in an indirect buffer, in overview mode. You can dive
7496 into the tree in that copy, use org-occur and incremental search
7497 to find a location. When pressing RET or `Q', the command
7498 returns to the original buffer in which the visibility is still
7499 unchanged. After RET it will also jump to the location selected
7500 in the indirect buffer and expose the headline hierarchy above.
7502 With a prefix argument, use the alternative interface: e.g., if
7503 `org-goto-interface' is `outline' use `outline-path-completion'."
7504 (interactive "P")
7505 (org-goto-map)
7506 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
7507 (org-refile-use-outline-path t)
7508 (org-refile-target-verify-function nil)
7509 (interface
7510 (if (not alternative-interface)
7511 org-goto-interface
7512 (if (eq org-goto-interface 'outline)
7513 'outline-path-completion
7514 'outline)))
7515 (org-goto-start-pos (point))
7516 (selected-point
7517 (if (eq interface 'outline)
7518 (car (org-get-location (current-buffer) org-goto-help))
7519 (let ((pa (org-refile-get-location "Goto")))
7520 (org-refile-check-position pa)
7521 (nth 3 pa)))))
7522 (if selected-point
7523 (progn
7524 (org-mark-ring-push org-goto-start-pos)
7525 (goto-char selected-point)
7526 (when (or (org-invisible-p) (org-invisible-p2))
7527 (org-show-context 'org-goto)))
7528 (message "Quit"))))
7530 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
7531 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
7532 (defvar org-goto-local-auto-isearch-map) ; defined below
7534 (defun org-get-location (_buf help)
7535 "Let the user select a location in current buffer.
7536 This function uses a recursive edit. It returns the selected position
7537 or nil."
7538 (org-no-popups
7539 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
7540 (isearch-hide-immediately nil)
7541 (isearch-search-fun-function
7542 (lambda () 'org-goto-local-search-headings))
7543 (org-goto-selected-point org-goto-exit-command))
7544 (save-excursion
7545 (save-window-excursion
7546 (delete-other-windows)
7547 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
7548 (pop-to-buffer-same-window
7549 (condition-case nil
7550 (make-indirect-buffer (current-buffer) "*org-goto*")
7551 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
7552 (with-output-to-temp-buffer "*Org Help*"
7553 (princ (format help (if org-goto-auto-isearch
7554 " Just type for auto-isearch."
7555 " n/p/f/b/u to navigate, q to quit."))))
7556 (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
7557 (setq buffer-read-only nil)
7558 (let ((org-startup-truncated t)
7559 (org-startup-folded nil)
7560 (org-startup-align-all-tables nil))
7561 (org-mode)
7562 (org-overview))
7563 (setq buffer-read-only t)
7564 (if (and (boundp 'org-goto-start-pos)
7565 (integer-or-marker-p org-goto-start-pos))
7566 (progn (goto-char org-goto-start-pos)
7567 (when (org-invisible-p)
7568 (org-show-set-visibility 'lineage)))
7569 (goto-char (point-min)))
7570 (let (org-special-ctrl-a/e) (org-beginning-of-line))
7571 (message "Select location and press RET")
7572 (use-local-map org-goto-map)
7573 (recursive-edit)))
7574 (kill-buffer "*org-goto*")
7575 (cons org-goto-selected-point org-goto-exit-command))))
7577 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
7578 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
7579 ;; `isearch-other-control-char' was removed in Emacs 24.4.
7580 (if (fboundp 'isearch-other-control-char)
7581 (progn
7582 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
7583 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
7584 (define-key org-goto-local-auto-isearch-map "\C-i" nil)
7585 (define-key org-goto-local-auto-isearch-map "\C-m" nil)
7586 (define-key org-goto-local-auto-isearch-map [return] nil))
7588 (defun org-goto-local-search-headings (string bound noerror)
7589 "Search and make sure that any matches are in headlines."
7590 (catch 'return
7591 (while (if isearch-forward
7592 (search-forward string bound noerror)
7593 (search-backward string bound noerror))
7594 (when (save-match-data
7595 (and (save-excursion
7596 (beginning-of-line)
7597 (looking-at org-complex-heading-regexp))
7598 (or (not (match-beginning 5))
7599 (< (point) (match-beginning 5)))))
7600 (throw 'return (point))))))
7602 (defun org-goto-local-auto-isearch ()
7603 "Start isearch."
7604 (interactive)
7605 (goto-char (point-min))
7606 (let ((keys (this-command-keys)))
7607 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
7608 (isearch-mode t)
7609 (isearch-process-search-char (string-to-char keys)))))
7611 (defun org-goto-ret (&optional _arg)
7612 "Finish `org-goto' by going to the new location."
7613 (interactive "P")
7614 (setq org-goto-selected-point (point))
7615 (setq org-goto-exit-command 'return)
7616 (throw 'exit nil))
7618 (defun org-goto-left ()
7619 "Finish `org-goto' by going to the new location."
7620 (interactive)
7621 (if (org-at-heading-p)
7622 (progn
7623 (beginning-of-line 1)
7624 (setq org-goto-selected-point (point)
7625 org-goto-exit-command 'left)
7626 (throw 'exit nil))
7627 (user-error "Not on a heading")))
7629 (defun org-goto-right ()
7630 "Finish `org-goto' by going to the new location."
7631 (interactive)
7632 (if (org-at-heading-p)
7633 (progn
7634 (setq org-goto-selected-point (point)
7635 org-goto-exit-command 'right)
7636 (throw 'exit nil))
7637 (user-error "Not on a heading")))
7639 (defun org-goto-quit ()
7640 "Finish `org-goto' without cursor motion."
7641 (interactive)
7642 (setq org-goto-selected-point nil)
7643 (setq org-goto-exit-command 'quit)
7644 (throw 'exit nil))
7646 ;;; Indirect buffer display of subtrees
7648 (defvar org-indirect-dedicated-frame nil
7649 "This is the frame being used for indirect tree display.")
7650 (defvar org-last-indirect-buffer nil)
7652 (defun org-tree-to-indirect-buffer (&optional arg)
7653 "Create indirect buffer and narrow it to current subtree.
7655 With a numerical prefix ARG, go up to this level and then take that tree.
7656 If ARG is negative, go up that many levels.
7658 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7659 indirect buffer previously made with this command, to avoid proliferation of
7660 indirect buffers. However, when you call the command with a \
7661 `\\[universal-argument]' prefix, or
7662 when `org-indirect-buffer-display' is `new-frame', the last buffer is kept
7663 so that you can work with several indirect buffers at the same time. If
7664 `org-indirect-buffer-display' is `dedicated-frame', the \
7665 `\\[universal-argument]' prefix also
7666 requests that a new frame be made for the new buffer, so that the dedicated
7667 frame is not changed."
7668 (interactive "P")
7669 (let ((cbuf (current-buffer))
7670 (cwin (selected-window))
7671 (pos (point))
7672 beg end level heading ibuf)
7673 (save-excursion
7674 (org-back-to-heading t)
7675 (when (numberp arg)
7676 (setq level (org-outline-level))
7677 (when (< arg 0) (setq arg (+ level arg)))
7678 (while (> (setq level (org-outline-level)) arg)
7679 (org-up-heading-safe)))
7680 (setq beg (point)
7681 heading (org-get-heading 'no-tags))
7682 (org-end-of-subtree t t)
7683 (when (org-at-heading-p) (backward-char 1))
7684 (setq end (point)))
7685 (when (and (buffer-live-p org-last-indirect-buffer)
7686 (not (eq org-indirect-buffer-display 'new-frame))
7687 (not arg))
7688 (kill-buffer org-last-indirect-buffer))
7689 (setq ibuf (org-get-indirect-buffer cbuf heading)
7690 org-last-indirect-buffer ibuf)
7691 (cond
7692 ((or (eq org-indirect-buffer-display 'new-frame)
7693 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7694 (select-frame (make-frame))
7695 (delete-other-windows)
7696 (pop-to-buffer-same-window ibuf)
7697 (org-set-frame-title heading))
7698 ((eq org-indirect-buffer-display 'dedicated-frame)
7699 (raise-frame
7700 (select-frame (or (and org-indirect-dedicated-frame
7701 (frame-live-p org-indirect-dedicated-frame)
7702 org-indirect-dedicated-frame)
7703 (setq org-indirect-dedicated-frame (make-frame)))))
7704 (delete-other-windows)
7705 (pop-to-buffer-same-window ibuf)
7706 (org-set-frame-title (concat "Indirect: " heading)))
7707 ((eq org-indirect-buffer-display 'current-window)
7708 (pop-to-buffer-same-window ibuf))
7709 ((eq org-indirect-buffer-display 'other-window)
7710 (pop-to-buffer ibuf))
7711 (t (error "Invalid value")))
7712 (narrow-to-region beg end)
7713 (outline-show-all)
7714 (goto-char pos)
7715 (run-hook-with-args 'org-cycle-hook 'all)
7716 (and (window-live-p cwin) (select-window cwin))))
7718 (defun org-get-indirect-buffer (&optional buffer heading)
7719 (setq buffer (or buffer (current-buffer)))
7720 (let ((n 1) (base (buffer-name buffer)) bname)
7721 (while (buffer-live-p
7722 (get-buffer
7723 (setq bname
7724 (concat base "-"
7725 (if heading (concat heading "-" (number-to-string n))
7726 (number-to-string n))))))
7727 (setq n (1+ n)))
7728 (condition-case nil
7729 (make-indirect-buffer buffer bname 'clone)
7730 (error (make-indirect-buffer buffer bname)))))
7732 (defun org-set-frame-title (title)
7733 "Set the title of the current frame to the string TITLE."
7734 (modify-frame-parameters (selected-frame) (list (cons 'name title))))
7736 ;;;; Structure editing
7738 ;;; Inserting headlines
7740 (defun org--line-empty-p (n)
7741 "Is the Nth next line empty?
7743 Counts the current line as N = 1 and the previous line as N = 0;
7744 see `beginning-of-line'."
7745 (save-excursion
7746 (and (not (bobp))
7747 (or (beginning-of-line n) t)
7748 (save-match-data
7749 (looking-at "[ \t]*$")))))
7751 (defun org-previous-line-empty-p ()
7752 "Is the previous line a blank line?
7753 When NEXT is non-nil, check the next line instead."
7754 (org--line-empty-p 0))
7756 (defun org-next-line-empty-p ()
7757 "Is the previous line a blank line?
7758 When NEXT is non-nil, check the next line instead."
7759 (org--line-empty-p 2))
7761 (defun org--blank-before-heading-p (&optional parent)
7762 "Non-nil when an empty line should precede a new heading here.
7763 When optional argument PARENT is non-nil, consider parent
7764 headline instead of current one."
7765 (pcase (assq 'heading org-blank-before-new-entry)
7766 (`(heading . auto)
7767 (save-excursion
7768 (org-with-limited-levels
7769 (unless (and (org-before-first-heading-p)
7770 (not (outline-next-heading)))
7771 (org-back-to-heading t)
7772 (when parent (org-up-heading-safe))
7773 (cond ((not (bobp))
7774 (org-previous-line-empty-p))
7775 ((outline-next-heading)
7776 (org-previous-line-empty-p))
7777 ;; Ignore trailing spaces on last buffer line.
7778 ((progn (skip-chars-backward " \t") (bolp))
7779 (org-previous-line-empty-p))
7780 (t nil))))))
7781 (`(heading . ,value) value)
7782 (_ nil)))
7784 (defun org-insert-heading (&optional arg invisible-ok top)
7785 "Insert a new heading or an item with the same depth at point.
7787 If point is at the beginning of a heading, insert a new heading
7788 or a new headline above the current one. When at the beginning
7789 of a regular line of text, turn it into a heading.
7791 If point is in the middle of a line, split it and create a new
7792 headline with the text in the current line after point (see
7793 `org-M-RET-may-split-line' on how to modify this behavior). As
7794 a special case, on a headline, splitting can only happen on the
7795 title itself. E.g., this excludes breaking stars or tags.
7797 With a `\\[universal-argument]' prefix, set \
7798 `org-insert-heading-respect-content' to
7799 a non-nil value for the duration of the command. This forces the
7800 insertion of a heading after the current subtree, independently
7801 on the location of point.
7803 With a `\\[universal-argument] \\[universal-argument]' prefix, \
7804 insert the heading at the end of the tree
7805 above the current heading. For example, if point is within a
7806 2nd-level heading, then it will insert a 2nd-level heading at
7807 the end of the 1st-level parent subtree.
7809 When INVISIBLE-OK is set, stop at invisible headlines when going
7810 back. This is important for non-interactive uses of the
7811 command.
7813 When optional argument TOP is non-nil, insert a level 1 heading,
7814 unconditionally."
7815 (interactive "P")
7816 (let* ((blank? (org--blank-before-heading-p (equal arg '(16))))
7817 (level (org-current-level))
7818 (stars (make-string (if (and level (not top)) level 1) ?*)))
7819 (cond
7820 ((or org-insert-heading-respect-content
7821 (member arg '((4) (16)))
7822 (and (not invisible-ok)
7823 (invisible-p (max (1- (point)) (point-min)))))
7824 ;; Position point at the location of insertion.
7825 (if (not level) ;before first headline
7826 (org-with-limited-levels (outline-next-heading))
7827 ;; Make sure we end up on a visible headline if INVISIBLE-OK
7828 ;; is nil.
7829 (org-with-limited-levels (org-back-to-heading invisible-ok))
7830 (cond ((equal arg '(16))
7831 (org-up-heading-safe)
7832 (org-end-of-subtree t t))
7834 (org-end-of-subtree t t))))
7835 (unless (bolp) (insert "\n")) ;ensure final newline
7836 (unless (and blank? (org-previous-line-empty-p))
7837 (org-N-empty-lines-before-current (if blank? 1 0)))
7838 (insert stars " \n")
7839 (forward-char -1))
7840 ;; At a headline...
7841 ((org-at-heading-p)
7842 (cond ((bolp)
7843 (when blank? (save-excursion (insert "\n")))
7844 (save-excursion (insert stars " \n"))
7845 (unless (and blank? (org-previous-line-empty-p))
7846 (org-N-empty-lines-before-current (if blank? 1 0)))
7847 (end-of-line))
7848 ((and (org-get-alist-option org-M-RET-may-split-line 'headline)
7849 (org-match-line org-complex-heading-regexp)
7850 (org-pos-in-match-range (point) 4))
7851 ;; Grab the text that should moved to the new headline.
7852 ;; Preserve tags.
7853 (let ((split (delete-and-extract-region (point) (match-end 4))))
7854 (if (looking-at "[ \t]*$") (replace-match "")
7855 (org-set-tags nil t))
7856 (end-of-line)
7857 (when blank? (insert "\n"))
7858 (insert "\n" stars " ")
7859 (when (org-string-nw-p split) (insert split))
7860 (when (eobp) (save-excursion (insert "\n")))))
7862 (end-of-line)
7863 (when blank? (insert "\n"))
7864 (insert "\n" stars " ")
7865 (when (eobp) (save-excursion (insert "\n"))))))
7866 ;; On regular text, turn line into a headline or split, if
7867 ;; appropriate.
7868 ((bolp)
7869 (insert stars " ")
7870 (unless (and blank? (org-previous-line-empty-p))
7871 (org-N-empty-lines-before-current (if blank? 1 0))))
7873 (unless (org-get-alist-option org-M-RET-may-split-line 'headline)
7874 (end-of-line))
7875 (insert "\n" stars " ")
7876 (unless (and blank? (org-previous-line-empty-p))
7877 (org-N-empty-lines-before-current (if blank? 1 0))))))
7878 (run-hooks 'org-insert-heading-hook))
7880 (defun org-N-empty-lines-before-current (n)
7881 "Make the number of empty lines before current exactly N.
7882 So this will delete or add empty lines."
7883 (let ((column (current-column)))
7884 (beginning-of-line)
7885 (unless (bobp)
7886 (let ((start (save-excursion
7887 (skip-chars-backward " \r\t\n")
7888 (line-end-position))))
7889 (delete-region start (line-end-position 0))))
7890 (insert (make-string n ?\n))
7891 (move-to-column column)))
7893 (defun org-get-heading (&optional no-tags no-todo no-priority no-comment)
7894 "Return the heading of the current entry, without the stars.
7895 When NO-TAGS is non-nil, don't include tags.
7896 When NO-TODO is non-nil, don't include TODO keywords.
7897 When NO-PRIORITY is non-nil, don't include priority cookie.
7898 When NO-COMMENT is non-nil, don't include COMMENT string."
7899 (save-excursion
7900 (org-back-to-heading t)
7901 (let ((case-fold-search nil))
7902 (looking-at org-complex-heading-regexp)
7903 (let ((todo (and (not no-todo) (match-string 2)))
7904 (priority (and (not no-priority) (match-string 3)))
7905 (headline (pcase (match-string 4)
7906 (`nil "")
7907 ((and (guard no-comment) h)
7908 (replace-regexp-in-string
7909 (eval-when-compile
7910 (format "\\`%s[ \t]+" org-comment-string))
7911 "" h))
7912 (h h)))
7913 (tags (and (not no-tags) (match-string 5))))
7914 (mapconcat #'identity
7915 (delq nil (list todo priority headline tags))
7916 " ")))))
7918 (defvar orgstruct-mode) ; defined below
7920 (defun org-heading-components ()
7921 "Return the components of the current heading.
7922 This is a list with the following elements:
7923 - the level as an integer
7924 - the reduced level, different if `org-odd-levels-only' is set.
7925 - the TODO keyword, or nil
7926 - the priority character, like ?A, or nil if no priority is given
7927 - the headline text itself, or the tags string if no headline text
7928 - the tags string, or nil."
7929 (save-excursion
7930 (org-back-to-heading t)
7931 (when (let (case-fold-search)
7932 (looking-at
7933 (if orgstruct-mode
7934 org-heading-regexp
7935 org-complex-heading-regexp)))
7936 (if orgstruct-mode
7937 (list (length (match-string 1))
7938 (org-reduced-level (length (match-string 1)))
7941 (match-string 2)
7942 nil)
7943 (list (length (match-string 1))
7944 (org-reduced-level (length (match-string 1)))
7945 (match-string-no-properties 2)
7946 (and (match-end 3) (aref (match-string 3) 2))
7947 (match-string-no-properties 4)
7948 (match-string-no-properties 5))))))
7950 (defun org-get-entry ()
7951 "Get the entry text, after heading, entire subtree."
7952 (save-excursion
7953 (org-back-to-heading t)
7954 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7956 (defun org-edit-headline (&optional heading)
7957 "Edit the current headline.
7958 Set it to HEADING when provided."
7959 (interactive)
7960 (org-with-wide-buffer
7961 (org-back-to-heading t)
7962 (let ((case-fold-search nil))
7963 (when (looking-at org-complex-heading-regexp)
7964 (let* ((old (match-string-no-properties 4))
7965 (new (save-match-data
7966 (org-trim (or heading (read-string "Edit: " old))))))
7967 (unless (equal old new)
7968 (if old (replace-match new t t nil 4)
7969 (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
7970 (insert " " new))
7971 (org-set-tags nil t)
7972 (when (looking-at "[ \t]*$") (replace-match ""))))))))
7974 (defun org-insert-heading-after-current ()
7975 "Insert a new heading with same level as current, after current subtree."
7976 (interactive)
7977 (org-back-to-heading)
7978 (org-insert-heading)
7979 (org-move-subtree-down)
7980 (end-of-line 1))
7982 (defun org-insert-heading-respect-content (&optional invisible-ok)
7983 "Insert heading with `org-insert-heading-respect-content' set to t."
7984 (interactive)
7985 (org-insert-heading '(4) invisible-ok))
7987 (defun org-insert-todo-heading-respect-content (&optional force-state)
7988 "Insert TODO heading with `org-insert-heading-respect-content' set to t."
7989 (interactive)
7990 (org-insert-todo-heading force-state '(4)))
7992 (defun org-insert-todo-heading (arg &optional force-heading)
7993 "Insert a new heading with the same level and TODO state as current heading.
7995 If the heading has no TODO state, or if the state is DONE, use
7996 the first state (TODO by default). Also with one prefix arg,
7997 force first state. With two prefix args, force inserting at the
7998 end of the parent subtree.
8000 When called at a plain list item, insert a new item with an
8001 unchecked check box."
8002 (interactive "P")
8003 (when (or force-heading (not (org-insert-item 'checkbox)))
8004 (org-insert-heading (or (and (equal arg '(16)) '(16))
8005 force-heading))
8006 (save-excursion
8007 (org-forward-heading-same-level -1)
8008 (let ((case-fold-search nil)) (looking-at org-todo-line-regexp)))
8009 (let* ((new-mark-x
8010 (if (or (equal arg '(4))
8011 (not (match-beginning 2))
8012 (member (match-string 2) org-done-keywords))
8013 (car org-todo-keywords-1)
8014 (match-string 2)))
8015 (new-mark
8017 (run-hook-with-args-until-success
8018 'org-todo-get-default-hook new-mark-x nil)
8019 new-mark-x)))
8020 (beginning-of-line 1)
8021 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
8022 (if org-treat-insert-todo-heading-as-state-change
8023 (org-todo new-mark)
8024 (insert new-mark " "))))
8025 (when org-provide-todo-statistics
8026 (org-update-parent-todo-statistics))))
8028 (defun org-insert-subheading (arg)
8029 "Insert a new subheading and demote it.
8030 Works for outline headings and for plain lists alike."
8031 (interactive "P")
8032 (org-insert-heading arg)
8033 (cond
8034 ((org-at-heading-p) (org-do-demote))
8035 ((org-at-item-p) (org-indent-item))))
8037 (defun org-insert-todo-subheading (arg)
8038 "Insert a new subheading with TODO keyword or checkbox and demote it.
8039 Works for outline headings and for plain lists alike."
8040 (interactive "P")
8041 (org-insert-todo-heading arg)
8042 (cond
8043 ((org-at-heading-p) (org-do-demote))
8044 ((org-at-item-p) (org-indent-item))))
8046 ;;; Promotion and Demotion
8048 (defvar org-after-demote-entry-hook nil
8049 "Hook run after an entry has been demoted.
8050 The cursor will be at the beginning of the entry.
8051 When a subtree is being demoted, the hook will be called for each node.")
8053 (defvar org-after-promote-entry-hook nil
8054 "Hook run after an entry has been promoted.
8055 The cursor will be at the beginning of the entry.
8056 When a subtree is being promoted, the hook will be called for each node.")
8058 (defun org-promote-subtree ()
8059 "Promote the entire subtree.
8060 See also `org-promote'."
8061 (interactive)
8062 (save-excursion
8063 (org-with-limited-levels (org-map-tree 'org-promote)))
8064 (org-fix-position-after-promote))
8066 (defun org-demote-subtree ()
8067 "Demote the entire subtree.
8068 See `org-demote' and `org-promote'."
8069 (interactive)
8070 (save-excursion
8071 (org-with-limited-levels (org-map-tree 'org-demote)))
8072 (org-fix-position-after-promote))
8074 (defun org-do-promote ()
8075 "Promote the current heading higher up the tree.
8076 If the region is active in `transient-mark-mode', promote all
8077 headings in the region."
8078 (interactive)
8079 (save-excursion
8080 (if (org-region-active-p)
8081 (org-map-region 'org-promote (region-beginning) (region-end))
8082 (org-promote)))
8083 (org-fix-position-after-promote))
8085 (defun org-do-demote ()
8086 "Demote the current heading lower down the tree.
8087 If the region is active in `transient-mark-mode', demote all
8088 headings in the region."
8089 (interactive)
8090 (save-excursion
8091 (if (org-region-active-p)
8092 (org-map-region 'org-demote (region-beginning) (region-end))
8093 (org-demote)))
8094 (org-fix-position-after-promote))
8096 (defun org-fix-position-after-promote ()
8097 "Fix cursor position and indentation after demoting/promoting."
8098 (let ((pos (point)))
8099 (when (save-excursion
8100 (beginning-of-line)
8101 (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
8102 (or (eq pos (match-end 1)) (eq pos (match-end 2))))
8103 (cond ((eobp) (insert " "))
8104 ((eolp) (insert " "))
8105 ((equal (char-after) ?\s) (forward-char 1))))))
8107 (defun org-current-level ()
8108 "Return the level of the current entry, or nil if before the first headline.
8109 The level is the number of stars at the beginning of the
8110 headline. Use `org-reduced-level' to remove the effect of
8111 `org-odd-levels'. Unlike to `org-outline-level', this function
8112 ignores inlinetasks."
8113 (let ((level (org-with-limited-levels (org-outline-level))))
8114 (and (> level 0) level)))
8116 (defun org-get-previous-line-level ()
8117 "Return the outline depth of the last headline before the current line.
8118 Returns 0 for the first headline in the buffer, and nil if before the
8119 first headline."
8120 (and (org-current-level)
8121 (or (and (/= (line-beginning-position) (point-min))
8122 (save-excursion (beginning-of-line 0) (org-current-level)))
8123 0)))
8125 (defun org-reduced-level (l)
8126 "Compute the effective level of a heading.
8127 This takes into account the setting of `org-odd-levels-only'."
8128 (cond
8129 ((zerop l) 0)
8130 (org-odd-levels-only (1+ (floor (/ l 2))))
8131 (t l)))
8133 (defun org-level-increment ()
8134 "Return the number of stars that will be added or removed at a
8135 time to headlines when structure editing, based on the value of
8136 `org-odd-levels-only'."
8137 (if org-odd-levels-only 2 1))
8139 (defun org-get-valid-level (level &optional change)
8140 "Rectify a level change under the influence of `org-odd-levels-only'.
8141 LEVEL is a current level, CHANGE is by how much the level should
8142 be modified. Even if CHANGE is nil, LEVEL may be returned
8143 modified because even level numbers will become the next higher
8144 odd number. Returns values greater than 0."
8145 (if org-odd-levels-only
8146 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
8147 ((> change 0) (1+ (* 2 (/ (+ (1- level) (* 2 change)) 2))))
8148 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
8149 (max 1 (+ level (or change 0)))))
8151 (defun org-promote ()
8152 "Promote the current heading higher up the tree."
8153 (org-with-wide-buffer
8154 (org-back-to-heading t)
8155 (let* ((after-change-functions (remq 'flyspell-after-change-function
8156 after-change-functions))
8157 (level (save-match-data (funcall outline-level)))
8158 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
8159 (diff (abs (- level (length up-head) -1))))
8160 (cond
8161 ((and (= level 1) org-allow-promoting-top-level-subtree)
8162 (replace-match "# " nil t))
8163 ((= level 1)
8164 (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
8165 (t (replace-match up-head nil t)))
8166 (unless (= level 1)
8167 (when org-auto-align-tags (org-set-tags nil 'ignore-column))
8168 (when org-adapt-indentation (org-fixup-indentation (- diff))))
8169 (run-hooks 'org-after-promote-entry-hook))))
8171 (defun org-demote ()
8172 "Demote the current heading lower down the tree."
8173 (org-with-wide-buffer
8174 (org-back-to-heading t)
8175 (let* ((after-change-functions (remq 'flyspell-after-change-function
8176 after-change-functions))
8177 (level (save-match-data (funcall outline-level)))
8178 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
8179 (diff (abs (- level (length down-head) -1))))
8180 (replace-match down-head nil t)
8181 (when org-auto-align-tags (org-set-tags nil 'ignore-column))
8182 (when org-adapt-indentation (org-fixup-indentation diff))
8183 (run-hooks 'org-after-demote-entry-hook))))
8185 (defun org-cycle-level ()
8186 "Cycle the level of an empty headline through possible states.
8187 This goes first to child, then to parent, level, then up the hierarchy.
8188 After top level, it switches back to sibling level."
8189 (interactive)
8190 (let ((org-adapt-indentation nil))
8191 (when (org-point-at-end-of-empty-headline)
8192 (setq this-command 'org-cycle-level) ; Only needed for caching
8193 (let ((cur-level (org-current-level))
8194 (prev-level (org-get-previous-line-level)))
8195 (cond
8196 ;; If first headline in file, promote to top-level.
8197 ((= prev-level 0)
8198 (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
8199 do (org-do-promote)))
8200 ;; If same level as prev, demote one.
8201 ((= prev-level cur-level)
8202 (org-do-demote))
8203 ;; If parent is top-level, promote to top level if not already.
8204 ((= prev-level 1)
8205 (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
8206 do (org-do-promote)))
8207 ;; If top-level, return to prev-level.
8208 ((= cur-level 1)
8209 (cl-loop repeat (/ (- prev-level 1) (org-level-increment))
8210 do (org-do-demote)))
8211 ;; If less than prev-level, promote one.
8212 ((< cur-level prev-level)
8213 (org-do-promote))
8214 ;; If deeper than prev-level, promote until higher than
8215 ;; prev-level.
8216 ((> cur-level prev-level)
8217 (cl-loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
8218 do (org-do-promote))))
8219 t))))
8221 (defun org-map-tree (fun)
8222 "Call FUN for every heading underneath the current one."
8223 (org-back-to-heading t)
8224 (let ((level (funcall outline-level)))
8225 (save-excursion
8226 (funcall fun)
8227 (while (and (progn
8228 (outline-next-heading)
8229 (> (funcall outline-level) level))
8230 (not (eobp)))
8231 (funcall fun)))))
8233 (defun org-map-region (fun beg end)
8234 "Call FUN for every heading between BEG and END."
8235 (let ((org-ignore-region t))
8236 (save-excursion
8237 (setq end (copy-marker end))
8238 (goto-char beg)
8239 (when (and (re-search-forward org-outline-regexp-bol nil t)
8240 (< (point) end))
8241 (funcall fun))
8242 (while (and (progn
8243 (outline-next-heading)
8244 (< (point) end))
8245 (not (eobp)))
8246 (funcall fun)))))
8248 (defun org-fixup-indentation (diff)
8249 "Change the indentation in the current entry by DIFF.
8251 DIFF is an integer. Indentation is done according to the
8252 following rules:
8254 - Planning information and property drawers are always indented
8255 according to the new level of the headline;
8257 - Footnote definitions and their contents are ignored;
8259 - Inlinetasks' boundaries are not shifted;
8261 - Empty lines are ignored;
8263 - Other lines' indentation are shifted by DIFF columns, unless
8264 it would introduce a structural change in the document, in
8265 which case no shifting is done at all.
8267 Assume point is at a heading or an inlinetask beginning."
8268 (org-with-wide-buffer
8269 (narrow-to-region (line-beginning-position)
8270 (save-excursion
8271 (if (org-with-limited-levels (org-at-heading-p))
8272 (org-with-limited-levels (outline-next-heading))
8273 (org-inlinetask-goto-end))
8274 (point)))
8275 (forward-line)
8276 ;; Indent properly planning info and property drawer.
8277 (when (looking-at-p org-planning-line-re)
8278 (org-indent-line)
8279 (forward-line))
8280 (when (looking-at org-property-drawer-re)
8281 (goto-char (match-end 0))
8282 (forward-line)
8283 (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
8284 (catch 'no-shift
8285 (when (zerop diff) (throw 'no-shift nil))
8286 ;; If DIFF is negative, first check if a shift is possible at all
8287 ;; (e.g., it doesn't break structure). This can only happen if
8288 ;; some contents are not properly indented.
8289 (let ((case-fold-search t))
8290 (when (< diff 0)
8291 (let ((diff (- diff))
8292 (forbidden-re (concat org-outline-regexp
8293 "\\|"
8294 (substring org-footnote-definition-re 1))))
8295 (save-excursion
8296 (while (not (eobp))
8297 (cond
8298 ((looking-at-p "[ \t]*$") (forward-line))
8299 ((and (looking-at-p org-footnote-definition-re)
8300 (let ((e (org-element-at-point)))
8301 (and (eq (org-element-type e) 'footnote-definition)
8302 (goto-char (org-element-property :end e))))))
8303 ((looking-at-p org-outline-regexp) (forward-line))
8304 ;; Give up if shifting would move before column 0 or
8305 ;; if it would introduce a headline or a footnote
8306 ;; definition.
8308 (skip-chars-forward " \t")
8309 (let ((ind (current-column)))
8310 (when (or (< ind diff)
8311 (and (= ind diff) (looking-at-p forbidden-re)))
8312 (throw 'no-shift nil)))
8313 ;; Ignore contents of example blocks and source
8314 ;; blocks if their indentation is meant to be
8315 ;; preserved. Jump to block's closing line.
8316 (beginning-of-line)
8317 (or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
8318 (let ((e (org-element-at-point)))
8319 (and (memq (org-element-type e)
8320 '(example-block src-block))
8321 (or org-src-preserve-indentation
8322 (org-element-property :preserve-indent e))
8323 (goto-char (org-element-property :end e))
8324 (progn (skip-chars-backward " \r\t\n")
8325 (beginning-of-line)
8326 t))))
8327 (forward-line))))))))
8328 ;; Shift lines but footnote definitions, inlinetasks boundaries
8329 ;; by DIFF. Also skip contents of source or example blocks
8330 ;; when indentation is meant to be preserved.
8331 (while (not (eobp))
8332 (cond
8333 ((and (looking-at-p org-footnote-definition-re)
8334 (let ((e (org-element-at-point)))
8335 (and (eq (org-element-type e) 'footnote-definition)
8336 (goto-char (org-element-property :end e))))))
8337 ((looking-at-p org-outline-regexp) (forward-line))
8338 ((looking-at-p "[ \t]*$") (forward-line))
8340 (indent-line-to (+ (org-get-indentation) diff))
8341 (beginning-of-line)
8342 (or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
8343 (let ((e (org-element-at-point)))
8344 (and (memq (org-element-type e)
8345 '(example-block src-block))
8346 (or org-src-preserve-indentation
8347 (org-element-property :preserve-indent e))
8348 (goto-char (org-element-property :end e))
8349 (progn (skip-chars-backward " \r\t\n")
8350 (beginning-of-line)
8351 t))))
8352 (forward-line)))))))))
8354 (defun org-convert-to-odd-levels ()
8355 "Convert an Org file with all levels allowed to one with odd levels.
8356 This will leave level 1 alone, convert level 2 to level 3, level 3 to
8357 level 5 etc."
8358 (interactive)
8359 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
8360 (let ((outline-level 'org-outline-level)
8361 (org-odd-levels-only nil) n)
8362 (save-excursion
8363 (goto-char (point-min))
8364 (while (re-search-forward "^\\*\\*+ " nil t)
8365 (setq n (- (length (match-string 0)) 2))
8366 (while (>= (setq n (1- n)) 0)
8367 (org-demote))
8368 (end-of-line 1))))))
8370 (defun org-convert-to-oddeven-levels ()
8371 "Convert an Org file with only odd levels to one with odd/even levels.
8372 This promotes level 3 to level 2, level 5 to level 3 etc. If the
8373 file contains a section with an even level, conversion would
8374 destroy the structure of the file. An error is signaled in this
8375 case."
8376 (interactive)
8377 (goto-char (point-min))
8378 ;; First check if there are no even levels
8379 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
8380 (org-show-set-visibility 'canonical)
8381 (error "Not all levels are odd in this file. Conversion not possible"))
8382 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
8383 (let ((outline-regexp org-outline-regexp)
8384 (outline-level 'org-outline-level)
8385 (org-odd-levels-only nil) n)
8386 (save-excursion
8387 (goto-char (point-min))
8388 (while (re-search-forward "^\\*\\*+ " nil t)
8389 (setq n (/ (1- (length (match-string 0))) 2))
8390 (while (>= (setq n (1- n)) 0)
8391 (org-promote))
8392 (end-of-line 1))))))
8394 (defun org-tr-level (n)
8395 "Make N odd if required."
8396 (if org-odd-levels-only (1+ (/ n 2)) n))
8398 ;;; Vertical tree motion, cutting and pasting of subtrees
8400 (defun org-move-subtree-up (&optional arg)
8401 "Move the current subtree up past ARG headlines of the same level."
8402 (interactive "p")
8403 (org-move-subtree-down (- (prefix-numeric-value arg))))
8405 (defun org-move-subtree-down (&optional arg)
8406 "Move the current subtree down past ARG headlines of the same level."
8407 (interactive "p")
8408 (setq arg (prefix-numeric-value arg))
8409 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
8410 'org-get-last-sibling))
8411 (ins-point (make-marker))
8412 (cnt (abs arg))
8413 (col (current-column))
8414 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
8415 ;; Select the tree
8416 (org-back-to-heading)
8417 (setq beg0 (point))
8418 (save-excursion
8419 (setq ne-beg (org-back-over-empty-lines))
8420 (setq beg (point)))
8421 (save-match-data
8422 (save-excursion (outline-end-of-heading)
8423 (setq folded (org-invisible-p)))
8424 (progn (org-end-of-subtree nil t)
8425 (unless (eobp) (backward-char))))
8426 (outline-next-heading)
8427 (setq ne-end (org-back-over-empty-lines))
8428 (setq end (point))
8429 (goto-char beg0)
8430 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
8431 ;; include less whitespace
8432 (save-excursion
8433 (goto-char beg)
8434 (forward-line (- ne-beg ne-end))
8435 (setq beg (point))))
8436 ;; Find insertion point, with error handling
8437 (while (> cnt 0)
8438 (or (and (funcall movfunc) (looking-at org-outline-regexp))
8439 (progn (goto-char beg0)
8440 (user-error "Cannot move past superior level or buffer limit")))
8441 (setq cnt (1- cnt)))
8442 (when (> arg 0)
8443 ;; Moving forward - still need to move over subtree
8444 (org-end-of-subtree t t)
8445 (save-excursion
8446 (org-back-over-empty-lines)
8447 (or (bolp) (newline))))
8448 (setq ne-ins (org-back-over-empty-lines))
8449 (move-marker ins-point (point))
8450 (setq txt (buffer-substring beg end))
8451 (org-save-markers-in-region beg end)
8452 (delete-region beg end)
8453 (org-remove-empty-overlays-at beg)
8454 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
8455 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
8456 (and (not (bolp)) (looking-at "\n") (forward-char 1))
8457 (let ((bbb (point)))
8458 (insert-before-markers txt)
8459 (org-reinstall-markers-in-region bbb)
8460 (move-marker ins-point bbb))
8461 (or (bolp) (insert "\n"))
8462 (setq ins-end (point))
8463 (goto-char ins-point)
8464 (org-skip-whitespace)
8465 (when (and (< arg 0)
8466 (org-first-sibling-p)
8467 (> ne-ins ne-beg))
8468 ;; Move whitespace back to beginning
8469 (save-excursion
8470 (goto-char ins-end)
8471 (let ((kill-whole-line t))
8472 (kill-line (- ne-ins ne-beg)) (point)))
8473 (insert (make-string (- ne-ins ne-beg) ?\n)))
8474 (move-marker ins-point nil)
8475 (if folded
8476 (outline-hide-subtree)
8477 (org-show-entry)
8478 (org-show-children)
8479 (org-cycle-hide-drawers 'children))
8480 (org-clean-visibility-after-subtree-move)
8481 ;; move back to the initial column we were at
8482 (move-to-column col)))
8484 (defvar org-subtree-clip ""
8485 "Clipboard for cut and paste of subtrees.
8486 This is actually only a copy of the kill, because we use the normal kill
8487 ring. We need it to check if the kill was created by `org-copy-subtree'.")
8489 (defvar org-subtree-clip-folded nil
8490 "Was the last copied subtree folded?
8491 This is used to fold the tree back after pasting.")
8493 (defun org-cut-subtree (&optional n)
8494 "Cut the current subtree into the clipboard.
8495 With prefix arg N, cut this many sequential subtrees.
8496 This is a short-hand for marking the subtree and then cutting it."
8497 (interactive "p")
8498 (org-copy-subtree n 'cut))
8500 (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
8501 "Copy the current subtree into the clipboard.
8502 With prefix arg N, copy this many sequential subtrees.
8503 This is a short-hand for marking the subtree and then copying it.
8504 If CUT is non-nil, actually cut the subtree.
8505 If FORCE-STORE-MARKERS is non-nil, store the relative locations
8506 of some markers in the region, even if CUT is non-nil. This is
8507 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
8508 (interactive "p")
8509 (let (beg end folded (beg0 (point)))
8510 (if (called-interactively-p 'any)
8511 (org-back-to-heading nil) ; take what looks like a subtree
8512 (org-back-to-heading t)) ; take what is really there
8513 (setq beg (point))
8514 (skip-chars-forward " \t\r\n")
8515 (save-match-data
8516 (if nosubtrees
8517 (outline-next-heading)
8518 (save-excursion (outline-end-of-heading)
8519 (setq folded (org-invisible-p)))
8520 (ignore-errors (org-forward-heading-same-level (1- n) t))
8521 (org-end-of-subtree t t)))
8522 ;; Include the end of an inlinetask
8523 (when (and (featurep 'org-inlinetask)
8524 (looking-at-p (concat (org-inlinetask-outline-regexp)
8525 "END[ \t]*$")))
8526 (end-of-line))
8527 (setq end (point))
8528 (goto-char beg0)
8529 (when (> end beg)
8530 (setq org-subtree-clip-folded folded)
8531 (when (or cut force-store-markers)
8532 (org-save-markers-in-region beg end))
8533 (if cut (kill-region beg end) (copy-region-as-kill beg end))
8534 (setq org-subtree-clip (current-kill 0))
8535 (message "%s: Subtree(s) with %d characters"
8536 (if cut "Cut" "Copied")
8537 (length org-subtree-clip)))))
8539 (defun org-paste-subtree (&optional level tree for-yank remove)
8540 "Paste the clipboard as a subtree, with modification of headline level.
8541 The entire subtree is promoted or demoted in order to match a new headline
8542 level.
8544 If the cursor is at the beginning of a headline, the same level as
8545 that headline is used to paste the tree.
8547 If not, the new level is derived from the *visible* headings
8548 before and after the insertion point, and taken to be the inferior headline
8549 level of the two. So if the previous visible heading is level 3 and the
8550 next is level 4 (or vice versa), level 4 will be used for insertion.
8551 This makes sure that the subtree remains an independent subtree and does
8552 not swallow low level entries.
8554 You can also force a different level, either by using a numeric prefix
8555 argument, or by inserting the heading marker by hand. For example, if the
8556 cursor is after \"*****\", then the tree will be shifted to level 5.
8558 If optional TREE is given, use this text instead of the kill ring.
8560 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
8561 move back over whitespace before inserting, and move point to the end of
8562 the inserted text when done.
8564 When REMOVE is non-nil, remove the subtree from the clipboard."
8565 (interactive "P")
8566 (setq tree (or tree (and kill-ring (current-kill 0))))
8567 (unless (org-kill-is-subtree-p tree)
8568 (user-error "%s"
8569 (substitute-command-keys
8570 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
8571 (org-with-limited-levels
8572 (let* ((visp (not (org-invisible-p)))
8573 (txt tree)
8574 (old-level (if (string-match org-outline-regexp-bol txt)
8575 (- (match-end 0) (match-beginning 0) 1)
8576 -1))
8577 (force-level (cond (level (prefix-numeric-value level))
8578 ((and (looking-at "[ \t]*$")
8579 (string-match
8580 "^\\*+$" (buffer-substring
8581 (point-at-bol) (point))))
8582 (- (match-end 0) (match-beginning 0)))
8583 ((and (bolp)
8584 (looking-at org-outline-regexp))
8585 (- (match-end 0) (point) 1))))
8586 (previous-level (save-excursion
8587 (condition-case nil
8588 (progn
8589 (outline-previous-visible-heading 1)
8590 (if (looking-at org-outline-regexp-bol)
8591 (- (match-end 0) (match-beginning 0) 1)
8593 (error 1))))
8594 (next-level (save-excursion
8595 (condition-case nil
8596 (progn
8597 (or (looking-at org-outline-regexp)
8598 (outline-next-visible-heading 1))
8599 (if (looking-at org-outline-regexp-bol)
8600 (- (match-end 0) (match-beginning 0) 1)
8602 (error 1))))
8603 (new-level (or force-level (max previous-level next-level)))
8604 (shift (if (or (= old-level -1)
8605 (= new-level -1)
8606 (= old-level new-level))
8608 (- new-level old-level)))
8609 (delta (if (> shift 0) -1 1))
8610 (func (if (> shift 0) 'org-demote 'org-promote))
8611 (org-odd-levels-only nil)
8612 beg end newend)
8613 ;; Remove the forced level indicator
8614 (when force-level
8615 (delete-region (point-at-bol) (point)))
8616 ;; Paste
8617 (beginning-of-line (if (bolp) 1 2))
8618 (setq beg (point))
8619 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
8620 (insert-before-markers txt)
8621 (unless (string-suffix-p "\n" txt) (insert "\n"))
8622 (setq newend (point))
8623 (org-reinstall-markers-in-region beg)
8624 (setq end (point))
8625 (goto-char beg)
8626 (skip-chars-forward " \t\n\r")
8627 (setq beg (point))
8628 (when (and (org-invisible-p) visp)
8629 (save-excursion (outline-show-heading)))
8630 ;; Shift if necessary
8631 (unless (= shift 0)
8632 (save-restriction
8633 (narrow-to-region beg end)
8634 (while (not (= shift 0))
8635 (org-map-region func (point-min) (point-max))
8636 (setq shift (+ delta shift)))
8637 (goto-char (point-min))
8638 (setq newend (point-max))))
8639 (when (or (called-interactively-p 'interactive) for-yank)
8640 (message "Clipboard pasted as level %d subtree" new-level))
8641 (when (and (not for-yank) ; in this case, org-yank will decide about folding
8642 kill-ring
8643 (eq org-subtree-clip (current-kill 0))
8644 org-subtree-clip-folded)
8645 ;; The tree was folded before it was killed/copied
8646 (outline-hide-subtree))
8647 (and for-yank (goto-char newend))
8648 (and remove (setq kill-ring (cdr kill-ring))))))
8650 (defun org-kill-is-subtree-p (&optional txt)
8651 "Check if the current kill is an outline subtree, or a set of trees.
8652 Returns nil if kill does not start with a headline, or if the first
8653 headline level is not the largest headline level in the tree.
8654 So this will actually accept several entries of equal levels as well,
8655 which is OK for `org-paste-subtree'.
8656 If optional TXT is given, check this string instead of the current kill."
8657 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
8658 (re (org-get-limited-outline-regexp))
8659 (^re (concat "^" re))
8660 (start-level (and kill
8661 (string-match
8662 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
8663 kill)
8664 (- (match-end 2) (match-beginning 2) 1)))
8665 (start (1+ (or (match-beginning 2) -1))))
8666 (if (not start-level)
8667 (progn
8668 nil) ;; does not even start with a heading
8669 (catch 'exit
8670 (while (setq start (string-match ^re kill (1+ start)))
8671 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
8672 (throw 'exit nil)))
8673 t))))
8675 (defvar org-markers-to-move nil
8676 "Markers that should be moved with a cut-and-paste operation.
8677 Those markers are stored together with their positions relative to
8678 the start of the region.")
8680 (defun org-save-markers-in-region (beg end)
8681 "Check markers in region.
8682 If these markers are between BEG and END, record their position relative
8683 to BEG, so that after moving the block of text, we can put the markers back
8684 into place.
8685 This function gets called just before an entry or tree gets cut from the
8686 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
8687 called immediately, to move the markers with the entries."
8688 (setq org-markers-to-move nil)
8689 (when (featurep 'org-clock)
8690 (org-clock-save-markers-for-cut-and-paste beg end))
8691 (when (featurep 'org-agenda)
8692 (org-agenda-save-markers-for-cut-and-paste beg end)))
8694 (defun org-check-and-save-marker (marker beg end)
8695 "Check if MARKER is between BEG and END.
8696 If yes, remember the marker and the distance to BEG."
8697 (when (and (marker-buffer marker)
8698 (equal (marker-buffer marker) (current-buffer))
8699 (>= marker beg) (< marker end))
8700 (push (cons marker (- marker beg)) org-markers-to-move)))
8702 (defun org-reinstall-markers-in-region (beg)
8703 "Move all remembered markers to their position relative to BEG."
8704 (dolist (x org-markers-to-move)
8705 (move-marker (car x) (+ beg (cdr x))))
8706 (setq org-markers-to-move nil))
8708 (defun org-narrow-to-subtree ()
8709 "Narrow buffer to the current subtree."
8710 (interactive)
8711 (save-excursion
8712 (save-match-data
8713 (org-with-limited-levels
8714 (narrow-to-region
8715 (progn (org-back-to-heading t) (point))
8716 (progn (org-end-of-subtree t t)
8717 (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
8718 (point)))))))
8720 (defun org-narrow-to-block ()
8721 "Narrow buffer to the current block."
8722 (interactive)
8723 (let* ((case-fold-search t)
8724 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
8725 "^[ \t]*#\\+end_.*")))
8726 (if blockp
8727 (narrow-to-region (car blockp) (cdr blockp))
8728 (user-error "Not in a block"))))
8730 (defun org-clone-subtree-with-time-shift (n &optional shift)
8731 "Clone the task (subtree) at point N times.
8732 The clones will be inserted as siblings.
8734 In interactive use, the user will be prompted for the number of
8735 clones to be produced. If the entry has a timestamp, the user
8736 will also be prompted for a time shift, which may be a repeater
8737 as used in time stamps, for example `+3d'. To disable this,
8738 you can call the function with a universal prefix argument.
8740 When a valid repeater is given and the entry contains any time
8741 stamps, the clones will become a sequence in time, with time
8742 stamps in the subtree shifted for each clone produced. If SHIFT
8743 is nil or the empty string, time stamps will be left alone. The
8744 ID property of the original subtree is removed.
8746 In each clone, all the CLOCK entries will be removed. This
8747 prevents Org from considering that the clocked times overlap.
8749 If the original subtree did contain time stamps with a repeater,
8750 the following will happen:
8751 - the repeater will be removed in each clone
8752 - an additional clone will be produced, with the current, unshifted
8753 date(s) in the entry.
8754 - the original entry will be placed *after* all the clones, with
8755 repeater intact.
8756 - the start days in the repeater in the original entry will be shifted
8757 to past the last clone.
8758 In this way you can spell out a number of instances of a repeating task,
8759 and still retain the repeater to cover future instances of the task.
8761 As described above, N+1 clones are produced when the original
8762 subtree has a repeater. Setting N to 0, then, can be used to
8763 remove the repeater from a subtree and create a shifted clone
8764 with the original repeater."
8765 (interactive "nNumber of clones to produce: ")
8766 (unless (wholenump n) (user-error "Invalid number of replications %s" n))
8767 (when (org-before-first-heading-p) (user-error "No subtree to clone"))
8768 (let* ((beg (save-excursion (org-back-to-heading t) (point)))
8769 (end-of-tree (save-excursion (org-end-of-subtree t t) (point)))
8770 (shift
8771 (or shift
8772 (if (and (not (equal current-prefix-arg '(4)))
8773 (save-excursion
8774 (goto-char beg)
8775 (re-search-forward org-ts-regexp-both end-of-tree t)))
8776 (read-from-minibuffer
8777 "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
8778 ""))) ;No time shift
8779 (doshift
8780 (and (org-string-nw-p shift)
8781 (or (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
8782 shift)
8783 (user-error "Invalid shift specification %s" shift)))))
8784 (goto-char end-of-tree)
8785 (unless (bolp) (insert "\n"))
8786 (let* ((end (point))
8787 (template (buffer-substring beg end))
8788 (shift-n (and doshift (string-to-number (match-string 1 shift))))
8789 (shift-what (pcase (and doshift (match-string 2 shift))
8790 (`nil nil)
8791 ("d" 'day)
8792 ("w" (setq shift-n (* 7 shift-n)) 'day)
8793 ("m" 'month)
8794 ("y" 'year)
8795 (_ (error "Unsupported time unit"))))
8796 (nmin 1)
8797 (nmax n)
8798 (n-no-remove -1)
8799 (idprop (org-entry-get nil "ID")))
8800 (when (and doshift
8801 (string-match-p "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>"
8802 template))
8803 (delete-region beg end)
8804 (setq end beg)
8805 (setq nmin 0)
8806 (setq nmax (1+ nmax))
8807 (setq n-no-remove nmax))
8808 (goto-char end)
8809 (cl-loop for n from nmin to nmax do
8810 (insert
8811 ;; Prepare clone.
8812 (with-temp-buffer
8813 (insert template)
8814 (org-mode)
8815 (goto-char (point-min))
8816 (org-show-subtree)
8817 (and idprop (if org-clone-delete-id
8818 (org-entry-delete nil "ID")
8819 (org-id-get-create t)))
8820 (unless (= n 0)
8821 (while (re-search-forward org-clock-line-re nil t)
8822 (delete-region (line-beginning-position)
8823 (line-beginning-position 2)))
8824 (goto-char (point-min))
8825 (while (re-search-forward org-drawer-regexp nil t)
8826 (org-remove-empty-drawer-at (point))))
8827 (goto-char (point-min))
8828 (when doshift
8829 (while (re-search-forward org-ts-regexp-both nil t)
8830 (org-timestamp-change (* n shift-n) shift-what))
8831 (unless (= n n-no-remove)
8832 (goto-char (point-min))
8833 (while (re-search-forward org-ts-regexp nil t)
8834 (save-excursion
8835 (goto-char (match-beginning 0))
8836 (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
8837 (delete-region (match-beginning 1) (match-end 1)))))))
8838 (buffer-string)))))
8839 (goto-char beg)))
8841 ;;; Outline Sorting
8843 (defun org-sort (&optional with-case)
8844 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8845 Optional argument WITH-CASE means sort case-sensitively."
8846 (interactive "P")
8847 (org-call-with-arg
8848 (cond ((org-at-table-p) #'org-table-sort-lines)
8849 ((org-at-item-p) #'org-sort-list)
8850 (t #'org-sort-entries))
8851 with-case))
8853 (defun org-sort-remove-invisible (s)
8854 "Remove invisible part of links and emphasis markers from string S."
8855 (remove-text-properties 0 (length s) org-rm-props s)
8856 (replace-regexp-in-string
8857 org-verbatim-re (lambda (m) (format "%s " (match-string 4 m)))
8858 (replace-regexp-in-string
8859 org-emph-re (lambda (m) (format " %s " (match-string 4 m)))
8860 (org-link-display-format s)
8861 t t) t t))
8863 (defvar org-priority-regexp) ; defined later in the file
8865 (defvar org-after-sorting-entries-or-items-hook nil
8866 "Hook that is run after a bunch of entries or items have been sorted.
8867 When children are sorted, the cursor is in the parent line when this
8868 hook gets called. When a region or a plain list is sorted, the cursor
8869 will be in the first entry of the sorted region/list.")
8871 (defun org-sort-entries
8872 (&optional with-case sorting-type getkey-func compare-func property
8873 interactive?)
8874 "Sort entries on a certain level of an outline tree.
8875 If there is an active region, the entries in the region are sorted.
8876 Else, if the cursor is before the first entry, sort the top-level items.
8877 Else, the children of the entry at point are sorted.
8879 Sorting can be alphabetically, numerically, by date/time as given by
8880 a time stamp, by a property, by priority order, or by a custom function.
8882 The command prompts for the sorting type unless it has been given to the
8883 function through the SORTING-TYPE argument, which needs to be a character,
8884 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
8885 the precise meaning of each character:
8887 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8888 c By creation time, which is assumed to be the first inactive time stamp
8889 at the beginning of a line.
8890 d By deadline date/time.
8891 k By clocking time.
8892 n Numerically, by converting the beginning of the entry/item to a number.
8893 o By order of TODO keywords.
8894 p By priority according to the cookie.
8895 r By the value of a property.
8896 s By scheduled date/time.
8897 t By date/time, either the first active time stamp in the entry, or, if
8898 none exist, by the first inactive one.
8900 Capital letters will reverse the sort order.
8902 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8903 called with point at the beginning of the record. It must return a
8904 value that is compatible with COMPARE-FUNC, the function used to
8905 compare entries.
8907 Comparing entries ignores case by default. However, with an optional argument
8908 WITH-CASE, the sorting considers case as well.
8910 Sorting is done against the visible part of the headlines, it ignores hidden
8911 links.
8913 When sorting is done, call `org-after-sorting-entries-or-items-hook'.
8915 A non-nil value for INTERACTIVE? is used to signal that this
8916 function is being called interactively."
8917 (interactive (list current-prefix-arg nil nil nil nil t))
8918 (let ((case-func (if with-case 'identity 'downcase))
8919 start beg end stars re re2
8920 txt what tmp)
8921 ;; Find beginning and end of region to sort
8922 (cond
8923 ((org-region-active-p)
8924 ;; we will sort the region
8925 (setq end (region-end)
8926 what "region")
8927 (goto-char (region-beginning))
8928 (unless (org-at-heading-p) (outline-next-heading))
8929 (setq start (point)))
8930 ((or (org-at-heading-p)
8931 (ignore-errors (progn (org-back-to-heading) t)))
8932 ;; we will sort the children of the current headline
8933 (org-back-to-heading)
8934 (setq start (point)
8935 end (progn (org-end-of-subtree t t)
8936 (or (bolp) (insert "\n"))
8937 (when (>= (org-back-over-empty-lines) 1)
8938 (forward-line 1))
8939 (point))
8940 what "children")
8941 (goto-char start)
8942 (outline-show-subtree)
8943 (outline-next-heading))
8945 ;; we will sort the top-level entries in this file
8946 (goto-char (point-min))
8947 (or (org-at-heading-p) (outline-next-heading))
8948 (setq start (point))
8949 (goto-char (point-max))
8950 (beginning-of-line 1)
8951 (when (looking-at ".*?\\S-")
8952 ;; File ends in a non-white line
8953 (end-of-line 1)
8954 (insert "\n"))
8955 (setq end (point-max))
8956 (setq what "top-level")
8957 (goto-char start)
8958 (outline-show-all)))
8960 (setq beg (point))
8961 (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
8963 (looking-at "\\(\\*+\\)")
8964 (setq stars (match-string 1)
8965 re (concat "^" (regexp-quote stars) " +")
8966 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8967 txt (buffer-substring beg end))
8968 (unless (equal (substring txt -1) "\n") (setq txt (concat txt "\n")))
8969 (when (and (not (equal stars "*")) (string-match re2 txt))
8970 (user-error "Region to sort contains a level above the first entry"))
8972 (unless sorting-type
8973 (message
8974 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8975 [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
8976 A/N/P/R/O/F/T/S/D/C/K means reversed:"
8977 what)
8978 (setq sorting-type (read-char-exclusive)))
8980 (unless getkey-func
8981 (and (= (downcase sorting-type) ?f)
8982 (setq getkey-func
8983 (or (and interactive?
8984 (org-read-function
8985 "Function for extracting keys: "))
8986 (error "Missing key extractor")))))
8988 (and (= (downcase sorting-type) ?r)
8989 (not property)
8990 (setq property
8991 (completing-read "Property: "
8992 (mapcar #'list (org-buffer-property-keys t))
8993 nil t)))
8995 (when (member sorting-type '(?k ?K)) (org-clock-sum))
8996 (message "Sorting entries...")
8998 (save-restriction
8999 (narrow-to-region start end)
9000 (let ((restore-clock?
9001 ;; The clock marker is lost when using `sort-subr'; mark
9002 ;; the clock with temporary `:org-clock-marker-backup'
9003 ;; text property.
9004 (when (and (eq (org-clock-is-active) (current-buffer))
9005 (<= start (marker-position org-clock-marker))
9006 (>= end (marker-position org-clock-marker)))
9007 (org-with-silent-modifications
9008 (put-text-property (1- org-clock-marker) org-clock-marker
9009 :org-clock-marker-backup t))
9011 (dcst (downcase sorting-type))
9012 (case-fold-search nil)
9013 (now (current-time)))
9014 (sort-subr
9015 (/= dcst sorting-type)
9016 ;; This function moves to the beginning character of the "record" to
9017 ;; be sorted.
9018 (lambda nil
9019 (if (re-search-forward re nil t)
9020 (goto-char (match-beginning 0))
9021 (goto-char (point-max))))
9022 ;; This function moves to the last character of the "record" being
9023 ;; sorted.
9024 (lambda nil
9025 (save-match-data
9026 (condition-case nil
9027 (outline-forward-same-level 1)
9028 (error
9029 (goto-char (point-max))))))
9030 ;; This function returns the value that gets sorted against.
9031 (lambda nil
9032 (cond
9033 ((= dcst ?n)
9034 (if (looking-at org-complex-heading-regexp)
9035 (string-to-number (org-sort-remove-invisible (match-string 4)))
9036 nil))
9037 ((= dcst ?a)
9038 (if (looking-at org-complex-heading-regexp)
9039 (funcall case-func (org-sort-remove-invisible (match-string 4)))
9040 nil))
9041 ((= dcst ?k)
9042 (or (get-text-property (point) :org-clock-minutes) 0))
9043 ((= dcst ?t)
9044 (let ((end (save-excursion (outline-next-heading) (point))))
9045 (if (or (re-search-forward org-ts-regexp end t)
9046 (re-search-forward org-ts-regexp-both end t))
9047 (org-time-string-to-seconds (match-string 0))
9048 (float-time now))))
9049 ((= dcst ?c)
9050 (let ((end (save-excursion (outline-next-heading) (point))))
9051 (if (re-search-forward
9052 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
9053 end t)
9054 (org-time-string-to-seconds (match-string 0))
9055 (float-time now))))
9056 ((= dcst ?s)
9057 (let ((end (save-excursion (outline-next-heading) (point))))
9058 (if (re-search-forward org-scheduled-time-regexp end t)
9059 (org-time-string-to-seconds (match-string 1))
9060 (float-time now))))
9061 ((= dcst ?d)
9062 (let ((end (save-excursion (outline-next-heading) (point))))
9063 (if (re-search-forward org-deadline-time-regexp end t)
9064 (org-time-string-to-seconds (match-string 1))
9065 (float-time now))))
9066 ((= dcst ?p)
9067 (if (re-search-forward org-priority-regexp (point-at-eol) t)
9068 (string-to-char (match-string 2))
9069 org-default-priority))
9070 ((= dcst ?r)
9071 (or (org-entry-get nil property) ""))
9072 ((= dcst ?o)
9073 (when (looking-at org-complex-heading-regexp)
9074 (let* ((m (match-string 2))
9075 (s (if (member m org-done-keywords) '- '+)))
9076 (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
9077 ((= dcst ?f)
9078 (if getkey-func
9079 (progn
9080 (setq tmp (funcall getkey-func))
9081 (when (stringp tmp) (setq tmp (funcall case-func tmp)))
9082 tmp)
9083 (error "Invalid key function `%s'" getkey-func)))
9084 (t (error "Invalid sorting type `%c'" sorting-type))))
9086 (cond
9087 ((= dcst ?a) 'string<)
9088 ((= dcst ?f)
9089 (or compare-func
9090 (and interactive?
9091 (org-read-function
9092 (concat "Function for comparing keys "
9093 "(empty for default `sort-subr' predicate): ")
9094 'allow-empty))))
9095 ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))
9096 (when restore-clock?
9097 (move-marker org-clock-marker
9098 (1+ (next-single-property-change
9099 start :org-clock-marker-backup)))
9100 (remove-text-properties (1- org-clock-marker) org-clock-marker
9101 '(:org-clock-marker-backup t)))))
9102 (run-hooks 'org-after-sorting-entries-or-items-hook)
9103 (message "Sorting entries...done")))
9105 ;;; The orgstruct minor mode
9107 ;; Define a minor mode which can be used in other modes in order to
9108 ;; integrate the Org mode structure editing commands.
9110 ;; This is really a hack, because the Org mode structure commands use
9111 ;; keys which normally belong to the major mode. Here is how it
9112 ;; works: The minor mode defines all the keys necessary to operate the
9113 ;; structure commands, but wraps the commands into a function which
9114 ;; tests if the cursor is currently at a headline or a plain list
9115 ;; item. If that is the case, the structure command is used,
9116 ;; temporarily setting many Org mode variables like regular
9117 ;; expressions for filling etc. However, when any of those keys is
9118 ;; used at a different location, function uses `key-binding' to look
9119 ;; up if the key has an associated command in another currently active
9120 ;; keymap (minor modes, major mode, global), and executes that
9121 ;; command. There might be problems if any of the keys is otherwise
9122 ;; used as a prefix key.
9124 (defcustom orgstruct-heading-prefix-regexp ""
9125 "Regexp that matches the custom prefix of Org headlines in
9126 orgstruct(++)-mode."
9127 :group 'org
9128 :version "26.1"
9129 :package-version '(Org . "8.3")
9130 :type 'regexp)
9131 ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
9133 (defcustom orgstruct-setup-hook nil
9134 "Hook run after orgstruct-mode-map is filled."
9135 :group 'org
9136 :version "24.4"
9137 :package-version '(Org . "8.0")
9138 :type 'hook)
9140 (defvar orgstruct-initialized nil)
9142 (defvar org-local-vars nil
9143 "List of local variables, for use by `orgstruct-mode'.")
9145 ;;;###autoload
9146 (define-minor-mode orgstruct-mode
9147 "Toggle the minor mode `orgstruct-mode'.
9148 This mode is for using Org mode structure commands in other
9149 modes. The following keys behave as if Org mode were active, if
9150 the cursor is on a headline, or on a plain list item (both as
9151 defined by Org mode)."
9152 nil " OrgStruct" (make-sparse-keymap)
9153 (funcall (if orgstruct-mode
9154 'add-to-invisibility-spec
9155 'remove-from-invisibility-spec)
9156 '(outline . t))
9157 (when orgstruct-mode
9158 (org-load-modules-maybe)
9159 (unless orgstruct-initialized
9160 (orgstruct-setup)
9161 (setq orgstruct-initialized t))))
9163 ;;;###autoload
9164 (defun turn-on-orgstruct ()
9165 "Unconditionally turn on `orgstruct-mode'."
9166 (orgstruct-mode 1))
9168 (defvar-local orgstruct-is-++ nil
9169 "Is `orgstruct-mode' in ++ version in the current-buffer?")
9170 (defvar-local org-fb-vars nil)
9171 (defun orgstruct++-mode (&optional arg)
9172 "Toggle `orgstruct-mode', the enhanced version of it.
9173 In addition to setting orgstruct-mode, this also exports all
9174 indentation and autofilling variables from Org mode into the
9175 buffer. It will also recognize item context in multiline items."
9176 (interactive "P")
9177 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
9178 (if (< arg 1)
9179 (progn (orgstruct-mode -1)
9180 (dolist (v org-fb-vars)
9181 (set (make-local-variable (car v))
9182 (if (eq (car-safe (cadr v)) 'quote)
9183 (cl-cadadr v)
9184 (nth 1 v)))))
9185 (orgstruct-mode 1)
9186 (setq org-fb-vars nil)
9187 (unless org-local-vars
9188 (setq org-local-vars (org-get-local-variables)))
9189 (let (var val)
9190 (dolist (x org-local-vars)
9191 (when (string-match
9192 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\
9193 \\|fill-prefix\\|indent-\\)"
9194 (symbol-name (car x)))
9195 (setq var (car x) val (nth 1 x))
9196 (push (list var `(quote ,(eval var))) org-fb-vars)
9197 (set (make-local-variable var)
9198 (if (eq (car-safe val) 'quote) (nth 1 val) val))))
9199 (setq-local orgstruct-is-++ t))))
9201 ;;;###autoload
9202 (defun turn-on-orgstruct++ ()
9203 "Unconditionally turn on `orgstruct++-mode'."
9204 (orgstruct++-mode 1))
9206 (defun orgstruct-error ()
9207 "Error when there is no default binding for a structure key."
9208 (interactive)
9209 (funcall (if (fboundp 'user-error)
9210 'user-error
9211 'error)
9212 "This key has no function outside structure elements"))
9214 (defun orgstruct-setup ()
9215 "Setup orgstruct keymap."
9216 (dolist (cell '((org-demote . t)
9217 (org-metaleft . t)
9218 (org-metaright . t)
9219 (org-promote . t)
9220 (org-shiftmetaleft . t)
9221 (org-shiftmetaright . t)
9222 org-backward-element
9223 org-backward-heading-same-level
9224 org-ctrl-c-ret
9225 org-ctrl-c-minus
9226 org-ctrl-c-star
9227 org-cycle
9228 org-force-cycle-archived
9229 org-forward-heading-same-level
9230 org-insert-heading
9231 org-insert-heading-respect-content
9232 org-kill-note-or-show-branches
9233 org-mark-subtree
9234 org-meta-return
9235 org-metadown
9236 org-metaup
9237 org-narrow-to-subtree
9238 org-promote-subtree
9239 org-reveal
9240 org-shiftdown
9241 org-shiftleft
9242 org-shiftmetadown
9243 org-shiftmetaup
9244 org-shiftright
9245 org-shifttab
9246 org-shifttab
9247 org-shiftup
9248 org-show-children
9249 org-show-subtree
9250 org-sort
9251 org-up-element
9252 outline-demote
9253 outline-next-visible-heading
9254 outline-previous-visible-heading
9255 outline-promote
9256 outline-up-heading))
9257 (let ((f (or (car-safe cell) cell))
9258 (disable-when-heading-prefix (cdr-safe cell)))
9259 (when (fboundp f)
9260 (let ((new-bindings))
9261 (dolist (binding (nconc (where-is-internal f org-mode-map)
9262 (where-is-internal f outline-mode-map)))
9263 (push binding new-bindings)
9264 ;; TODO use local-function-key-map
9265 (dolist (rep '(("<tab>" . "TAB")
9266 ("<return>" . "RET")
9267 ("<escape>" . "ESC")
9268 ("<delete>" . "DEL")))
9269 (setq binding (read-kbd-macro
9270 (let ((case-fold-search))
9271 (replace-regexp-in-string
9272 (regexp-quote (cdr rep))
9273 (car rep)
9274 (key-description binding)))))
9275 (cl-pushnew binding new-bindings :test 'equal)))
9276 (dolist (binding new-bindings)
9277 (let ((key (lookup-key orgstruct-mode-map binding)))
9278 (when (or (not key) (numberp key))
9279 (ignore-errors
9280 (org-defkey orgstruct-mode-map
9281 binding
9282 (orgstruct-make-binding
9283 f binding disable-when-heading-prefix))))))))))
9284 (run-hooks 'orgstruct-setup-hook))
9286 (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
9287 "Create a function for binding in the structure minor mode.
9288 FUN is the command to call inside a table. KEY is the key that
9289 should be checked in for a command to execute outside of tables.
9290 Non-nil `disable-when-heading-prefix' means to disable the command
9291 if `orgstruct-heading-prefix-regexp' is not empty."
9292 (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
9293 (let ((nname name)
9294 (i 0))
9295 (while (fboundp (intern nname))
9296 (setq nname (format "%s-%d" name (setq i (1+ i)))))
9297 (setq name (intern nname)))
9298 (eval
9299 (let ((bindings '((org-heading-regexp
9300 (concat "^"
9301 orgstruct-heading-prefix-regexp
9302 "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
9303 (org-outline-regexp
9304 (concat orgstruct-heading-prefix-regexp "\\*+ "))
9305 (org-outline-regexp-bol
9306 (concat "^" org-outline-regexp))
9307 (outline-regexp org-outline-regexp)
9308 (outline-heading-end-regexp "\n")
9309 (outline-level 'org-outline-level)
9310 (outline-heading-alist))))
9311 `(defun ,name (arg)
9312 ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
9313 "Outside of structure, run the binding of `"
9314 (key-description key) "'."
9315 (when disable-when-heading-prefix
9316 (concat
9317 "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
9318 "back to the default binding due to limitations of Org's implementation of\n"
9319 "`" (symbol-name fun) "'.")))
9320 (interactive "p")
9321 (let* ((disable
9322 ,(and disable-when-heading-prefix
9323 '(not (string= orgstruct-heading-prefix-regexp ""))))
9324 (fallback
9325 (or disable
9326 (not
9327 (let* ,bindings
9328 (org-context-p 'headline 'item
9329 ,(when (memq fun
9330 '(org-insert-heading
9331 org-insert-heading-respect-content
9332 org-meta-return))
9333 '(when orgstruct-is-++
9334 'item-body))))))))
9335 (if fallback
9336 (let* ((orgstruct-mode)
9337 (binding
9338 (let ((key ,key))
9339 (catch 'exit
9340 (dolist
9341 (rep
9342 '(nil
9343 ("<\\([^>]*\\)tab>" . "\\1TAB")
9344 ("<\\([^>]*\\)return>" . "\\1RET")
9345 ("<\\([^>]*\\)escape>" . "\\1ESC")
9346 ("<\\([^>]*\\)delete>" . "\\1DEL"))
9347 nil)
9348 (when rep
9349 (setq key (read-kbd-macro
9350 (let ((case-fold-search))
9351 (replace-regexp-in-string
9352 (car rep)
9353 (cdr rep)
9354 (key-description key))))))
9355 (when (key-binding key)
9356 (throw 'exit (key-binding key))))))))
9357 (if (keymapp binding)
9358 (org-set-transient-map binding)
9359 (let ((func (or binding
9360 (unless disable
9361 'orgstruct-error))))
9362 (when func
9363 (call-interactively func)))))
9364 (org-run-like-in-org-mode
9365 (lambda ()
9366 (interactive)
9367 (let* ,bindings
9368 (call-interactively ',fun)))))))))
9369 name))
9371 (defun org-contextualize-keys (alist contexts)
9372 "Return valid elements in ALIST depending on CONTEXTS.
9374 `org-agenda-custom-commands' or `org-capture-templates' are the
9375 values used for ALIST, and `org-agenda-custom-commands-contexts'
9376 or `org-capture-templates-contexts' are the associated contexts
9377 definitions."
9378 (let ((contexts
9379 ;; normalize contexts
9380 (mapcar
9381 (lambda(c) (cond ((listp (cadr c))
9382 (list (car c) (car c) (nth 1 c)))
9383 ((string= "" (cadr c))
9384 (list (car c) (car c) (nth 2 c)))
9385 (t c)))
9386 contexts))
9387 (a alist) r s)
9388 ;; loop over all commands or templates
9389 (dolist (c a)
9390 (let (vrules repl)
9391 (cond
9392 ((not (assoc (car c) contexts))
9393 (push c r))
9394 ((and (assoc (car c) contexts)
9395 (setq vrules (org-contextualize-validate-key
9396 (car c) contexts)))
9397 (mapc (lambda (vr)
9398 (unless (equal (car vr) (cadr vr))
9399 (setq repl vr)))
9400 vrules)
9401 (if (not repl) (push c r)
9402 (push (cadr repl) s)
9403 (push
9404 (cons (car c)
9405 (cdr (or (assoc (cadr repl) alist)
9406 (error "Undefined key `%s' as contextual replacement for `%s'"
9407 (cadr repl) (car c)))))
9408 r))))))
9409 ;; Return limited ALIST, possibly with keys modified, and deduplicated
9410 (delq
9412 (delete-dups
9413 (mapcar (lambda (x)
9414 (let ((tpl (car x)))
9415 (unless (delq
9417 (mapcar (lambda (y)
9418 (equal y tpl))
9420 x)))
9421 (reverse r))))))
9423 (defun org-contextualize-validate-key (key contexts)
9424 "Check CONTEXTS for agenda or capture KEY."
9425 (let (res)
9426 (dolist (r contexts)
9427 (dolist (rr (car (last r)))
9428 (when
9429 (and (equal key (car r))
9430 (if (functionp rr) (funcall rr)
9431 (or (and (eq (car rr) 'in-file)
9432 (buffer-file-name)
9433 (string-match (cdr rr) (buffer-file-name)))
9434 (and (eq (car rr) 'in-mode)
9435 (string-match (cdr rr) (symbol-name major-mode)))
9436 (and (eq (car rr) 'in-buffer)
9437 (string-match (cdr rr) (buffer-name)))
9438 (when (and (eq (car rr) 'not-in-file)
9439 (buffer-file-name))
9440 (not (string-match (cdr rr) (buffer-file-name))))
9441 (when (eq (car rr) 'not-in-mode)
9442 (not (string-match (cdr rr) (symbol-name major-mode))))
9443 (when (eq (car rr) 'not-in-buffer)
9444 (not (string-match (cdr rr) (buffer-name)))))))
9445 (push r res))))
9446 (delete-dups (delq nil res))))
9448 (defun org-context-p (&rest contexts)
9449 "Check if local context is any of CONTEXTS.
9450 Possible values in the list of contexts are `table', `headline', and `item'."
9451 (let ((pos (point)))
9452 (goto-char (point-at-bol))
9453 (prog1 (or (and (memq 'table contexts)
9454 (looking-at "[ \t]*|"))
9455 (and (memq 'headline contexts)
9456 (looking-at org-outline-regexp))
9457 (and (memq 'item contexts)
9458 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
9459 (and (memq 'item-body contexts)
9460 (org-in-item-p)))
9461 (goto-char pos))))
9463 ;;;###autoload
9464 (defun org-run-like-in-org-mode (cmd)
9465 "Run a command, pretending that the current buffer is in Org mode.
9466 This will temporarily bind local variables that are typically bound in
9467 Org mode to the values they have in Org mode, and then interactively
9468 call CMD."
9469 (org-load-modules-maybe)
9470 (unless org-local-vars
9471 (setq org-local-vars (org-get-local-variables)))
9472 (let (binds)
9473 (dolist (var org-local-vars)
9474 (when (or (not (boundp (car var)))
9475 (eq (symbol-value (car var))
9476 (default-value (car var))))
9477 (push (list (car var) `(quote ,(cadr var))) binds)))
9478 (eval `(let ,binds
9479 (call-interactively (quote ,cmd))))))
9481 (defun org-get-category (&optional pos force-refresh)
9482 "Get the category applying to position POS."
9483 (save-match-data
9484 (when force-refresh (org-refresh-category-properties))
9485 (let ((pos (or pos (point))))
9486 (or (get-text-property pos 'org-category)
9487 (progn (org-refresh-category-properties)
9488 (get-text-property pos 'org-category))))))
9490 ;;; Refresh properties
9492 (defun org-refresh-properties (dprop tprop)
9493 "Refresh buffer text properties.
9494 DPROP is the drawer property and TPROP is either the
9495 corresponding text property to set, or an alist with each element
9496 being a text property (as a symbol) and a function to apply to
9497 the value of the drawer property."
9498 (let* ((case-fold-search t)
9499 (inhibit-read-only t)
9500 (inherit? (org-property-inherit-p dprop))
9501 (property-re (org-re-property (concat (regexp-quote dprop) "\\+?") t))
9502 (global (and inherit? (org--property-global-value dprop nil))))
9503 (org-with-silent-modifications
9504 (org-with-point-at 1
9505 ;; Set global values (e.g., values defined through
9506 ;; "#+PROPERTY:" keywords) to the whole buffer.
9507 (when global (put-text-property (point-min) (point-max) tprop global))
9508 ;; Set local values.
9509 (while (re-search-forward property-re nil t)
9510 (when (org-at-property-p)
9511 (org-refresh-property tprop (org-entry-get (point) dprop) inherit?))
9512 (outline-next-heading))))))
9514 (defun org-refresh-property (tprop p &optional inherit)
9515 "Refresh the buffer text property TPROP from the drawer property P.
9516 The refresh happens only for the current headline, or the whole
9517 sub-tree if optional argument INHERIT is non-nil."
9518 (unless (org-before-first-heading-p)
9519 (save-excursion
9520 (org-back-to-heading t)
9521 (let ((start (point))
9522 (end (save-excursion
9523 (if inherit (org-end-of-subtree t t)
9524 (or (outline-next-heading) (point-max))))))
9525 (if (symbolp tprop)
9526 ;; TPROP is a text property symbol.
9527 (put-text-property start end tprop p)
9528 ;; TPROP is an alist with (property . function) elements.
9529 (pcase-dolist (`(,prop . ,f) tprop)
9530 (put-text-property start end prop (funcall f p))))))))
9532 (defun org-refresh-category-properties ()
9533 "Refresh category text properties in the buffer."
9534 (let ((case-fold-search t)
9535 (inhibit-read-only t)
9536 (default-category
9537 (cond ((null org-category)
9538 (if buffer-file-name
9539 (file-name-sans-extension
9540 (file-name-nondirectory buffer-file-name))
9541 "???"))
9542 ((symbolp org-category) (symbol-name org-category))
9543 (t org-category))))
9544 (org-with-silent-modifications
9545 (org-with-wide-buffer
9546 ;; Set buffer-wide category. Search last #+CATEGORY keyword.
9547 ;; This is the default category for the buffer. If none is
9548 ;; found, fall-back to `org-category' or buffer file name.
9549 (put-text-property
9550 (point-min) (point-max)
9551 'org-category
9552 (catch 'buffer-category
9553 (goto-char (point-max))
9554 (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
9555 (let ((element (org-element-at-point)))
9556 (when (eq (org-element-type element) 'keyword)
9557 (throw 'buffer-category
9558 (org-element-property :value element)))))
9559 default-category))
9560 ;; Set sub-tree specific categories.
9561 (goto-char (point-min))
9562 (let ((regexp (org-re-property "CATEGORY")))
9563 (while (re-search-forward regexp nil t)
9564 (let ((value (match-string-no-properties 3)))
9565 (when (org-at-property-p)
9566 (put-text-property
9567 (save-excursion (org-back-to-heading t) (point))
9568 (save-excursion (org-end-of-subtree t t) (point))
9569 'org-category
9570 value)))))))))
9572 (defun org-refresh-stats-properties ()
9573 "Refresh stats text properties in the buffer."
9574 (org-with-silent-modifications
9575 (org-with-point-at 1
9576 (let ((regexp (concat org-outline-regexp-bol
9577 ".*\\[\\([0-9]*\\)\\(?:%\\|/\\([0-9]*\\)\\)\\]")))
9578 (while (re-search-forward regexp nil t)
9579 (let* ((numerator (string-to-number (match-string 1)))
9580 (denominator (and (match-end 2)
9581 (string-to-number (match-string 2))))
9582 (stats (cond ((not denominator) numerator) ;percent
9583 ((= denominator 0) 0)
9584 (t (/ (* numerator 100) denominator)))))
9585 (put-text-property (point) (progn (org-end-of-subtree t t) (point))
9586 'org-stats stats)))))))
9588 (defun org-refresh-effort-properties ()
9589 "Refresh effort properties"
9590 (org-refresh-properties
9591 org-effort-property
9592 '((effort . identity)
9593 (effort-minutes . org-duration-to-minutes))))
9595 ;;;; Link Stuff
9597 ;;; Link abbreviations
9599 (defun org-link-expand-abbrev (link)
9600 "Apply replacements as defined in `org-link-abbrev-alist'."
9601 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
9602 (let* ((key (match-string 1 link))
9603 (as (or (assoc key org-link-abbrev-alist-local)
9604 (assoc key org-link-abbrev-alist)))
9605 (tag (and (match-end 2) (match-string 3 link)))
9606 rpl)
9607 (if (not as)
9608 link
9609 (setq rpl (cdr as))
9610 (cond
9611 ((symbolp rpl) (funcall rpl tag))
9612 ((string-match "%(\\([^)]+\\))" rpl)
9613 (replace-match
9614 (save-match-data
9615 (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
9616 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
9617 ((string-match "%h" rpl)
9618 (replace-match (url-hexify-string (or tag "")) t t rpl))
9619 (t (concat rpl tag)))))
9620 link))
9622 ;;; Storing and inserting links
9624 (defvar org-insert-link-history nil
9625 "Minibuffer history for links inserted with `org-insert-link'.")
9627 (defvar org-stored-links nil
9628 "Contains the links stored with `org-store-link'.")
9630 (defvar org-store-link-plist nil
9631 "Plist with info about the most recently link created with `org-store-link'.")
9633 (defun org-store-link-functions ()
9634 "Return a list of functions that are called to create and store a link.
9635 The functions defined in the :store property of
9636 `org-link-parameters'.
9638 Each function will be called in turn until one returns a non-nil
9639 value. Each function should check if it is responsible for
9640 creating this link (for example by looking at the major mode).
9641 If not, it must exit and return nil. If yes, it should return
9642 a non-nil value after calling `org-store-link-props' with a list
9643 of properties and values. Special properties are:
9645 :type The link prefix, like \"http\". This must be given.
9646 :link The link, like \"http://www.astro.uva.nl/~dominik\".
9647 This is obligatory as well.
9648 :description Optional default description for the second pair
9649 of brackets in an Org mode link. The user can still change
9650 this when inserting this link into an Org mode buffer.
9652 In addition to these, any additional properties can be specified
9653 and then used in capture templates."
9654 (cl-loop for link in org-link-parameters
9655 with store-func
9656 do (setq store-func (org-link-get-parameter (car link) :store))
9657 if store-func
9658 collect store-func))
9660 (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
9661 (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
9663 ;;;###autoload
9664 (defun org-store-link (arg)
9665 "Store an org-link to the current location.
9666 \\<org-mode-map>
9667 This link is added to `org-stored-links' and can later be inserted
9668 into an Org buffer with `org-insert-link' (`\\[org-insert-link]').
9670 For some link types, a `\\[universal-argument]' prefix ARG is interpreted. \
9671 A single
9672 `\\[universal-argument]' negates `org-context-in-file-links' for file links or
9673 `org-gnus-prefer-web-links' for links to Usenet articles.
9675 A `\\[universal-argument] \\[universal-argument]' prefix ARG forces \
9676 skipping storing functions that are not
9677 part of Org core.
9679 A `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
9680 prefix ARG forces storing a link for each line in the
9681 active region."
9682 (interactive "P")
9683 (org-load-modules-maybe)
9684 (if (and (equal arg '(64)) (org-region-active-p))
9685 (save-excursion
9686 (let ((end (region-end)))
9687 (goto-char (region-beginning))
9688 (set-mark (point))
9689 (while (< (point-at-eol) end)
9690 (move-end-of-line 1) (activate-mark)
9691 (let (current-prefix-arg)
9692 (call-interactively 'org-store-link))
9693 (move-beginning-of-line 2)
9694 (set-mark (point)))))
9695 (setq org-store-link-plist nil)
9696 (let (link cpltxt desc description search txt custom-id agenda-link)
9697 (cond
9698 ;; Store a link using an external link type, if any function is
9699 ;; available. If more than one can generate a link from current
9700 ;; location, ask which one to use.
9701 ((and (not (equal arg '(16)))
9702 (let ((results-alist nil))
9703 (dolist (f (org-store-link-functions))
9704 (when (funcall f)
9705 ;; XXX: return value is not link's plist, so we
9706 ;; store the new value before it is modified. It
9707 ;; would be cleaner to ask store link functions to
9708 ;; return the plist instead.
9709 (push (cons f (copy-sequence org-store-link-plist))
9710 results-alist)))
9711 (pcase results-alist
9712 (`nil nil)
9713 (`((,_ . ,_)) t) ;single choice: nothing to do
9714 (`((,name . ,_) . ,_)
9715 ;; Reinstate link plist associated to the chosen
9716 ;; function.
9717 (apply #'org-store-link-props
9718 (cdr (assoc-string
9719 (completing-read
9720 "Which function for creating the link? "
9721 (mapcar #'car results-alist)
9722 nil t (symbol-name name))
9723 results-alist)))
9724 t))))
9725 (setq link (plist-get org-store-link-plist :link))
9726 (setq desc (or (plist-get org-store-link-plist :description)
9727 link)))
9729 ;; Store a link from a source code buffer.
9730 ((org-src-edit-buffer-p)
9731 (let ((coderef-format (org-src-coderef-format)))
9732 (cond ((save-excursion
9733 (beginning-of-line)
9734 (looking-at (org-src-coderef-regexp coderef-format)))
9735 (setq link (format "(%s)" (match-string-no-properties 3))))
9736 ((called-interactively-p 'any)
9737 (let ((label (read-string "Code line label: ")))
9738 (end-of-line)
9739 (setq link (format coderef-format label))
9740 (let ((gc (- 79 (length link))))
9741 (if (< (current-column) gc)
9742 (org-move-to-column gc t)
9743 (insert " ")))
9744 (insert link)
9745 (setq link (concat "(" label ")"))
9746 (setq desc nil)))
9747 (t (setq link nil)))))
9749 ;; We are in the agenda, link to referenced location
9750 ((equal (bound-and-true-p org-agenda-buffer-name) (buffer-name))
9751 (let ((m (or (get-text-property (point) 'org-hd-marker)
9752 (get-text-property (point) 'org-marker))))
9753 (when m
9754 (org-with-point-at m
9755 (setq agenda-link
9756 (if (called-interactively-p 'any)
9757 (call-interactively 'org-store-link)
9758 (org-store-link nil)))))))
9760 ((eq major-mode 'calendar-mode)
9761 (let ((cd (calendar-cursor-to-date)))
9762 (setq link
9763 (format-time-string
9764 (car org-time-stamp-formats)
9765 (apply 'encode-time
9766 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
9767 nil nil nil))))
9768 (org-store-link-props :type "calendar" :date cd)))
9770 ((eq major-mode 'help-mode)
9771 (setq link (concat "help:" (save-excursion
9772 (goto-char (point-min))
9773 (looking-at "^[^ ]+")
9774 (match-string 0))))
9775 (org-store-link-props :type "help"))
9777 ((eq major-mode 'w3-mode)
9778 (setq cpltxt (if (and (buffer-name)
9779 (not (string-match "Untitled" (buffer-name))))
9780 (buffer-name)
9781 (url-view-url t))
9782 link (url-view-url t))
9783 (org-store-link-props :type "w3" :url (url-view-url t)))
9785 ((eq major-mode 'image-mode)
9786 (setq cpltxt (concat "file:"
9787 (abbreviate-file-name buffer-file-name))
9788 link cpltxt)
9789 (org-store-link-props :type "image" :file buffer-file-name))
9791 ;; In dired, store a link to the file of the current line
9792 ((derived-mode-p 'dired-mode)
9793 (let ((file (dired-get-filename nil t)))
9794 (setq file (if file
9795 (abbreviate-file-name
9796 (expand-file-name (dired-get-filename nil t)))
9797 ;; otherwise, no file so use current directory.
9798 default-directory))
9799 (setq cpltxt (concat "file:" file)
9800 link cpltxt)))
9802 ((setq search (run-hook-with-args-until-success
9803 'org-create-file-search-functions))
9804 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
9805 "::" search))
9806 (setq cpltxt (or description link)))
9808 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
9809 (org-with-limited-levels
9810 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
9811 (cond
9812 ;; Store a link using the target at point
9813 ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
9814 (setq cpltxt
9815 (concat "file:"
9816 (abbreviate-file-name
9817 (buffer-file-name (buffer-base-buffer)))
9818 "::" (match-string 1))
9819 link cpltxt))
9820 ((and (featurep 'org-id)
9821 (or (eq org-id-link-to-org-use-id t)
9822 (and (called-interactively-p 'any)
9823 (or (eq org-id-link-to-org-use-id 'create-if-interactive)
9824 (and (eq org-id-link-to-org-use-id
9825 'create-if-interactive-and-no-custom-id)
9826 (not custom-id))))
9827 (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
9828 ;; Store a link using the ID at point
9829 (setq link (condition-case nil
9830 (prog1 (org-id-store-link)
9831 (setq desc (or (plist-get org-store-link-plist
9832 :description)
9833 "")))
9834 (error
9835 ;; Probably before first headline, link only to file
9836 (concat "file:"
9837 (abbreviate-file-name
9838 (buffer-file-name (buffer-base-buffer))))))))
9840 ;; Just link to current headline
9841 (setq cpltxt (concat "file:"
9842 (abbreviate-file-name
9843 (buffer-file-name (buffer-base-buffer)))))
9844 ;; Add a context search string
9845 (when (org-xor org-context-in-file-links
9846 (equal arg '(4)))
9847 (let* ((element (org-element-at-point))
9848 (name (org-element-property :name element)))
9849 (setq txt (cond
9850 ((org-at-heading-p) nil)
9851 (name)
9852 ((org-region-active-p)
9853 (buffer-substring (region-beginning) (region-end)))))
9854 (when (or (null txt) (string-match "\\S-" txt))
9855 (setq cpltxt
9856 (concat cpltxt "::"
9857 (condition-case nil
9858 (org-make-org-heading-search-string txt)
9859 (error "")))
9860 desc (or name
9861 (nth 4 (ignore-errors (org-heading-components)))
9862 "NONE")))))
9863 (when (string-match "::\\'" cpltxt)
9864 (setq cpltxt (substring cpltxt 0 -2)))
9865 (setq link cpltxt)))))
9867 ((buffer-file-name (buffer-base-buffer))
9868 ;; Just link to this file here.
9869 (setq cpltxt (concat "file:"
9870 (abbreviate-file-name
9871 (buffer-file-name (buffer-base-buffer)))))
9872 ;; Add a context string.
9873 (when (org-xor org-context-in-file-links
9874 (equal arg '(4)))
9875 (setq txt (if (org-region-active-p)
9876 (buffer-substring (region-beginning) (region-end))
9877 (buffer-substring (point-at-bol) (point-at-eol))))
9878 ;; Only use search option if there is some text.
9879 (when (string-match "\\S-" txt)
9880 (setq cpltxt
9881 (concat cpltxt "::" (org-make-org-heading-search-string txt))
9882 desc "NONE")))
9883 (setq link cpltxt))
9885 ((called-interactively-p 'interactive)
9886 (user-error "No method for storing a link from this buffer"))
9888 (t (setq link nil)))
9890 ;; We're done setting link and desc, clean up
9891 (when (consp link) (setq cpltxt (car link) link (cdr link)))
9892 (setq link (or link cpltxt)
9893 desc (or desc cpltxt))
9894 (cond ((not desc))
9895 ((equal desc "NONE") (setq desc nil))
9896 (t (setq desc
9897 (replace-regexp-in-string
9898 org-bracket-link-analytic-regexp
9899 (lambda (m) (or (match-string 5 m) (match-string 3 m)))
9900 desc))))
9901 ;; Return the link
9902 (if (not (and (or (called-interactively-p 'any)
9903 executing-kbd-macro)
9904 link))
9905 (or agenda-link (and link (org-make-link-string link desc)))
9906 (push (list link desc) org-stored-links)
9907 (message "Stored: %s" (or desc link))
9908 (when custom-id
9909 (setq link (concat "file:" (abbreviate-file-name
9910 (buffer-file-name)) "::#" custom-id))
9911 (push (list link desc) org-stored-links))
9912 (car org-stored-links)))))
9914 (defun org-store-link-props (&rest plist)
9915 "Store link properties.
9916 The properties are pre-processed by extracting names, addresses
9917 and dates."
9918 (let ((x (plist-get plist :from)))
9919 (when x
9920 (let ((adr (mail-extract-address-components x)))
9921 (setq plist (plist-put plist :fromname (car adr)))
9922 (setq plist (plist-put plist :fromaddress (nth 1 adr))))))
9923 (let ((x (plist-get plist :to)))
9924 (when x
9925 (let ((adr (mail-extract-address-components x)))
9926 (setq plist (plist-put plist :toname (car adr)))
9927 (setq plist (plist-put plist :toaddress (nth 1 adr))))))
9928 (let ((x (ignore-errors (date-to-time (plist-get plist :date)))))
9929 (when x
9930 (setq plist (plist-put plist :date-timestamp
9931 (format-time-string
9932 (org-time-stamp-format t) x)))
9933 (setq plist (plist-put plist :date-timestamp-inactive
9934 (format-time-string
9935 (org-time-stamp-format t t) x)))))
9936 (let ((from (plist-get plist :from))
9937 (to (plist-get plist :to)))
9938 (when (and from to org-from-is-user-regexp)
9939 (setq plist
9940 (plist-put plist :fromto
9941 (if (string-match org-from-is-user-regexp from)
9942 (concat "to %t")
9943 (concat "from %f"))))))
9944 (setq org-store-link-plist plist))
9946 (defun org-add-link-props (&rest plist)
9947 "Add these properties to the link property list."
9948 (let (key value)
9949 (while plist
9950 (setq key (pop plist) value (pop plist))
9951 (setq org-store-link-plist
9952 (plist-put org-store-link-plist key value)))))
9954 (defun org-email-link-description (&optional fmt)
9955 "Return the description part of an email link.
9956 This takes information from `org-store-link-plist' and formats it
9957 according to FMT (default from `org-email-link-description-format')."
9958 (setq fmt (or fmt org-email-link-description-format))
9959 (let* ((p org-store-link-plist)
9960 (to (plist-get p :toaddress))
9961 (from (plist-get p :fromaddress))
9962 (table
9963 (list
9964 (cons "%c" (plist-get p :fromto))
9965 (cons "%F" (plist-get p :from))
9966 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
9967 (cons "%T" (plist-get p :to))
9968 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
9969 (cons "%s" (plist-get p :subject))
9970 (cons "%d" (plist-get p :date))
9971 (cons "%m" (plist-get p :message-id)))))
9972 (when (string-match "%c" fmt)
9973 ;; Check if the user wrote this message
9974 (if (and org-from-is-user-regexp from to
9975 (save-match-data (string-match org-from-is-user-regexp from)))
9976 (setq fmt (replace-match "to %t" t t fmt))
9977 (setq fmt (replace-match "from %f" t t fmt))))
9978 (org-replace-escapes fmt table)))
9980 (defun org-make-org-heading-search-string (&optional string)
9981 "Make search string for the current headline or STRING."
9982 (let ((s (or string
9983 (and (derived-mode-p 'org-mode)
9984 (save-excursion
9985 (org-back-to-heading t)
9986 (org-element-property :raw-value (org-element-at-point))))))
9987 (lines org-context-in-file-links))
9988 (unless string (setq s (concat "*" s))) ;Add * for headlines
9989 (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
9990 (when (and string (integerp lines) (> lines 0))
9991 (let ((slines (org-split-string s "\n")))
9992 (when (< lines (length slines))
9993 (setq s (mapconcat
9994 'identity
9995 (reverse (nthcdr (- (length slines) lines)
9996 (reverse slines))) "\n")))))
9997 (mapconcat #'identity (split-string s) " ")))
9999 (defconst org-link-escape-chars
10000 ;;%20 %5B %5D %25
10001 '(?\s ?\[ ?\] ?%)
10002 "List of characters that should be escaped in a link when stored to Org.
10003 This is the list that is used for internal purposes.")
10005 (defun org-make-link-string (link &optional description)
10006 "Make a link with brackets, consisting of LINK and DESCRIPTION."
10007 (unless (org-string-nw-p link) (error "Empty link"))
10008 (let ((uri (cond ((string-match org-link-types-re link)
10009 (concat (match-string 1 link)
10010 (org-link-escape (substring link (match-end 1)))))
10011 ((or (file-name-absolute-p link)
10012 (string-match-p "\\`\\.\\.?/" link))
10013 (org-link-escape link))
10014 ;; For readability, do not encode space characters
10015 ;; in fuzzy links.
10016 (t (org-link-escape link (remq ?\s org-link-escape-chars)))))
10017 (description
10018 (and (org-string-nw-p description)
10019 ;; Remove brackets from description, as they are fatal.
10020 (replace-regexp-in-string
10021 "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
10022 (org-trim description)))))
10023 (format "[[%s]%s]"
10025 (if description (format "[%s]" description) ""))))
10027 (defun org-link-escape (text &optional table merge)
10028 "Return percent escaped representation of TEXT.
10029 TEXT is a string with the text to escape.
10030 Optional argument TABLE is a list with characters that should be
10031 escaped. When nil, `org-link-escape-chars' is used.
10032 If optional argument MERGE is set, merge TABLE into
10033 `org-link-escape-chars'."
10034 (let ((characters-to-encode
10035 (cond ((null table) org-link-escape-chars)
10036 (merge (append org-link-escape-chars table))
10037 (t table))))
10038 (mapconcat
10039 (lambda (c)
10040 (if (or (memq c characters-to-encode)
10041 (and org-url-hexify-p (or (< c 32) (> c 126))))
10042 (mapconcat (lambda (e) (format "%%%.2X" e))
10043 (or (encode-coding-char c 'utf-8)
10044 (error "Unable to percent escape character: %c" c))
10046 (char-to-string c)))
10047 text "")))
10049 (defun org-link-unescape (str)
10050 "Unhex hexified Unicode parts in string STR.
10051 E.g. `%C3%B6' becomes the german o-Umlaut. This is the
10052 reciprocal of `org-link-escape', which see."
10053 (if (org-string-nw-p str)
10054 (replace-regexp-in-string
10055 "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
10056 str))
10058 (defun org-link-unescape-compound (hex)
10059 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
10060 Note: this function also decodes single byte encodings like
10061 `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
10062 (save-match-data
10063 (let* ((bytes (cdr (split-string hex "%")))
10064 (ret "")
10065 (eat 0)
10066 (sum 0))
10067 (while bytes
10068 (let* ((val (string-to-number (pop bytes) 16))
10069 (shift-xor
10070 (if (= 0 eat)
10071 (cond
10072 ((>= val 252) (cons 6 252))
10073 ((>= val 248) (cons 5 248))
10074 ((>= val 240) (cons 4 240))
10075 ((>= val 224) (cons 3 224))
10076 ((>= val 192) (cons 2 192))
10077 (t (cons 0 0)))
10078 (cons 6 128))))
10079 (when (>= val 192) (setq eat (car shift-xor)))
10080 (setq val (logxor val (cdr shift-xor)))
10081 (setq sum (+ (lsh sum (car shift-xor)) val))
10082 (when (> eat 0) (setq eat (- eat 1)))
10083 (cond
10084 ((= 0 eat) ;multi byte
10085 (setq ret (concat ret (char-to-string sum)))
10086 (setq sum 0))
10087 ((not bytes) ; single byte(s)
10088 (setq ret (org-link-unescape-single-byte-sequence hex))))))
10089 ret)))
10091 (defun org-link-unescape-single-byte-sequence (hex)
10092 "Unhexify hex-encoded single byte character sequences."
10093 (mapconcat (lambda (byte)
10094 (char-to-string (string-to-number byte 16)))
10095 (cdr (split-string hex "%")) ""))
10097 (defun org-xor (a b)
10098 "Exclusive or."
10099 (if a (not b) b))
10101 (defun org-fixup-message-id-for-http (s)
10102 "Replace special characters in a message id, so it can be used in an http query."
10103 (when (string-match "%" s)
10104 (setq s (mapconcat (lambda (c)
10105 (if (eq c ?%)
10106 "%25"
10107 (char-to-string c)))
10108 s "")))
10109 (while (string-match "<" s)
10110 (setq s (replace-match "%3C" t t s)))
10111 (while (string-match ">" s)
10112 (setq s (replace-match "%3E" t t s)))
10113 (while (string-match "@" s)
10114 (setq s (replace-match "%40" t t s)))
10117 (defun org-link-prettify (link)
10118 "Return a human-readable representation of LINK.
10119 The car of LINK must be a raw link.
10120 The cdr of LINK must be either a link description or nil."
10121 (let ((desc (or (cadr link) "<no description>")))
10122 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
10123 "<" (car link) ">")))
10125 ;;;###autoload
10126 (defun org-insert-link-global ()
10127 "Insert a link like Org mode does.
10128 This command can be called in any mode to insert a link in Org syntax."
10129 (interactive)
10130 (org-load-modules-maybe)
10131 (org-run-like-in-org-mode 'org-insert-link))
10133 (defun org-insert-all-links (arg &optional pre post)
10134 "Insert all links in `org-stored-links'.
10135 When a universal prefix, do not delete the links from `org-stored-links'.
10136 When `ARG' is a number, insert the last N link(s).
10137 `PRE' and `POST' are optional arguments to define a string to
10138 prepend or to append."
10139 (interactive "P")
10140 (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
10141 (links (copy-sequence org-stored-links))
10142 (pr (or pre "- "))
10143 (po (or post "\n"))
10144 (cnt 1) l)
10145 (if (null org-stored-links)
10146 (message "No link to insert")
10147 (while (and (or (listp arg) (>= arg cnt))
10148 (setq l (if (listp arg)
10149 (pop links)
10150 (pop org-stored-links))))
10151 (setq cnt (1+ cnt))
10152 (insert pr)
10153 (org-insert-link nil (car l) (or (cadr l) "<no description>"))
10154 (insert po)))))
10156 (defun org-insert-last-stored-link (arg)
10157 "Insert the last link stored in `org-stored-links'."
10158 (interactive "p")
10159 (org-insert-all-links arg "" "\n"))
10161 (defun org-link-fontify-links-to-this-file ()
10162 "Fontify links to the current file in `org-stored-links'."
10163 (let ((f (buffer-file-name)) a b)
10164 (setq a (mapcar (lambda(l)
10165 (let ((ll (car l)))
10166 (when (and (string-match "^file:\\(.+\\)::" ll)
10167 (equal f (expand-file-name (match-string 1 ll))))
10168 ll)))
10169 org-stored-links))
10170 (when (featurep 'org-id)
10171 (setq b (mapcar (lambda(l)
10172 (let ((ll (car l)))
10173 (when (and (string-match "^id:\\(.+\\)$" ll)
10174 (equal f (expand-file-name
10175 (or (org-id-find-id-file
10176 (match-string 1 ll)) ""))))
10177 ll)))
10178 org-stored-links)))
10179 (mapcar (lambda(l)
10180 (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
10181 (delq nil (append a b)))))
10183 (defvar org--links-history nil)
10184 (defun org-insert-link (&optional complete-file link-location default-description)
10185 "Insert a link. At the prompt, enter the link.
10187 Completion can be used to insert any of the link protocol prefixes in use.
10189 The history can be used to select a link previously stored with
10190 `org-store-link'. When the empty string is entered (i.e. if you just
10191 press `RET' at the prompt), the link defaults to the most recently
10192 stored link. As `SPC' triggers completion in the minibuffer, you need to
10193 use `M-SPC' or `C-q SPC' to force the insertion of a space character.
10195 You will also be prompted for a description, and if one is given, it will
10196 be displayed in the buffer instead of the link.
10198 If there is already a link at point, this command will allow you to edit
10199 link and description parts.
10201 With a `\\[universal-argument]' prefix, prompts for a file to link to. The \
10202 file name can be
10203 selected using completion. The path to the file will be relative to the
10204 current directory if the file is in the current directory or a subdirectory.
10205 Otherwise, the link will be the absolute path as completed in the minibuffer
10206 \(i.e. normally ~/path/to/file). You can configure this behavior using the
10207 option `org-link-file-path-type'.
10209 With a `\\[universal-argument] \\[universal-argument]' prefix, enforce an \
10210 absolute path even if the file is in
10211 the current directory or below.
10213 A `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
10214 prefix negates `org-keep-stored-link-after-insertion'.
10216 If the LINK-LOCATION parameter is non-nil, this value will be used as
10217 the link location instead of reading one interactively.
10219 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
10220 be used as the default description. Otherwise, if
10221 `org-make-link-description-function' is non-nil, this function
10222 will be called with the link target, and the result will be the
10223 default link description."
10224 (interactive "P")
10225 (let* ((wcf (current-window-configuration))
10226 (origbuf (current-buffer))
10227 (region (when (org-region-active-p)
10228 (buffer-substring (region-beginning) (region-end))))
10229 (remove (and region (list (region-beginning) (region-end))))
10230 (desc region)
10231 (link link-location)
10232 (abbrevs org-link-abbrev-alist-local)
10233 entry all-prefixes auto-desc)
10234 (cond
10235 (link-location) ; specified by arg, just use it.
10236 ((org-in-regexp org-bracket-link-regexp 1)
10237 ;; We do have a link at point, and we are going to edit it.
10238 (setq remove (list (match-beginning 0) (match-end 0)))
10239 (setq desc (when (match-end 3) (match-string-no-properties 3)))
10240 (setq link (read-string "Link: "
10241 (org-link-unescape
10242 (match-string-no-properties 1)))))
10243 ((or (org-in-regexp org-angle-link-re)
10244 (org-in-regexp org-plain-link-re))
10245 ;; Convert to bracket link
10246 (setq remove (list (match-beginning 0) (match-end 0))
10247 link (read-string "Link: "
10248 (org-unbracket-string "<" ">" (match-string 0)))))
10249 ((member complete-file '((4) (16)))
10250 ;; Completing read for file names.
10251 (setq link (org-file-complete-link complete-file)))
10253 ;; Read link, with completion for stored links.
10254 (org-link-fontify-links-to-this-file)
10255 (org-switch-to-buffer-other-window "*Org Links*")
10256 (with-current-buffer "*Org Links*"
10257 (erase-buffer)
10258 (insert "Insert a link.
10259 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
10260 (when org-stored-links
10261 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
10262 (insert (mapconcat 'org-link-prettify
10263 (reverse org-stored-links) "\n")))
10264 (goto-char (point-min)))
10265 (let ((cw (selected-window)))
10266 (select-window (get-buffer-window "*Org Links*" 'visible))
10267 (with-current-buffer "*Org Links*" (setq truncate-lines t))
10268 (unless (pos-visible-in-window-p (point-max))
10269 (org-fit-window-to-buffer))
10270 (and (window-live-p cw) (select-window cw)))
10271 (setq all-prefixes (append (mapcar 'car abbrevs)
10272 (mapcar 'car org-link-abbrev-alist)
10273 (org-link-types)))
10274 (unwind-protect
10275 ;; Fake a link history, containing the stored links.
10276 (let ((org--links-history
10277 (append (mapcar #'car org-stored-links)
10278 org-insert-link-history)))
10279 (setq link
10280 (org-completing-read
10281 "Link: "
10282 (append
10283 (mapcar (lambda (x) (concat x ":")) all-prefixes)
10284 (mapcar #'car org-stored-links))
10285 nil nil nil
10286 'org--links-history
10287 (caar org-stored-links)))
10288 (unless (org-string-nw-p link) (user-error "No link selected"))
10289 (dolist (l org-stored-links)
10290 (when (equal link (cadr l))
10291 (setq link (car l))
10292 (setq auto-desc t)))
10293 (when (or (member link all-prefixes)
10294 (and (equal ":" (substring link -1))
10295 (member (substring link 0 -1) all-prefixes)
10296 (setq link (substring link 0 -1))))
10297 (setq link (with-current-buffer origbuf
10298 (org-link-try-special-completion link)))))
10299 (set-window-configuration wcf)
10300 (kill-buffer "*Org Links*"))
10301 (setq entry (assoc link org-stored-links))
10302 (or entry (push link org-insert-link-history))
10303 (setq desc (or desc (nth 1 entry)))))
10305 (when (funcall (if (equal complete-file '(64)) 'not 'identity)
10306 (not org-keep-stored-link-after-insertion))
10307 (setq org-stored-links (delq (assoc link org-stored-links)
10308 org-stored-links)))
10310 (when (and (string-match org-plain-link-re link)
10311 (not (string-match org-ts-regexp link)))
10312 ;; URL-like link, normalize the use of angular brackets.
10313 (setq link (org-unbracket-string "<" ">" link)))
10315 ;; Check if we are linking to the current file with a search
10316 ;; option If yes, simplify the link by using only the search
10317 ;; option.
10318 (when (and buffer-file-name
10319 (let ((case-fold-search nil))
10320 (string-match "\\`file:\\(.+?\\)::" link)))
10321 (let ((path (match-string-no-properties 1 link))
10322 (search (substring-no-properties link (match-end 0))))
10323 (save-match-data
10324 (when (equal (file-truename buffer-file-name) (file-truename path))
10325 ;; We are linking to this same file, with a search option
10326 (setq link search)))))
10328 ;; Check if we can/should use a relative path. If yes, simplify
10329 ;; the link.
10330 (let ((case-fold-search nil))
10331 (when (string-match "\\`\\(file\\|docview\\):" link)
10332 (let* ((type (match-string-no-properties 0 link))
10333 (path-start (match-end 0))
10334 (search (and (string-match "::\\(.*\\)\\'" link)
10335 (match-string 1 link)))
10336 (path
10337 (if search
10338 (substring-no-properties
10339 link path-start (match-beginning 0))
10340 (substring-no-properties link (match-end 0))))
10341 (origpath path))
10342 (cond
10343 ((or (eq org-link-file-path-type 'absolute)
10344 (equal complete-file '(16)))
10345 (setq path (abbreviate-file-name (expand-file-name path))))
10346 ((eq org-link-file-path-type 'noabbrev)
10347 (setq path (expand-file-name path)))
10348 ((eq org-link-file-path-type 'relative)
10349 (setq path (file-relative-name path)))
10351 (save-match-data
10352 (if (string-match (concat "^" (regexp-quote
10353 (expand-file-name
10354 (file-name-as-directory
10355 default-directory))))
10356 (expand-file-name path))
10357 ;; We are linking a file with relative path name.
10358 (setq path (substring (expand-file-name path)
10359 (match-end 0)))
10360 (setq path (abbreviate-file-name (expand-file-name path)))))))
10361 (setq link (concat type path (and search (concat "::" search))))
10362 (when (equal desc origpath)
10363 (setq desc path)))))
10365 (unless auto-desc
10366 (let ((initial-input
10367 (cond
10368 (default-description)
10369 ((not org-make-link-description-function) desc)
10370 (t (condition-case nil
10371 (funcall org-make-link-description-function link desc)
10372 (error
10373 (message "Can't get link description from `%s'"
10374 (symbol-name org-make-link-description-function))
10375 (sit-for 2)
10376 nil))))))
10377 (setq desc (read-string "Description: " initial-input))))
10379 (unless (string-match "\\S-" desc) (setq desc nil))
10380 (when remove (apply 'delete-region remove))
10381 (insert (org-make-link-string link desc))
10382 ;; Redisplay so as the new link has proper invisible characters.
10383 (sit-for 0)))
10385 (defun org-link-try-special-completion (type)
10386 "If there is completion support for link type TYPE, offer it."
10387 (let ((fun (org-link-get-parameter type :complete)))
10388 (if (functionp fun)
10389 (funcall fun)
10390 (read-string "Link (no completion support): " (concat type ":")))))
10392 (defun org-file-complete-link (&optional arg)
10393 "Create a file link using completion."
10394 (let ((file (read-file-name "File: "))
10395 (pwd (file-name-as-directory (expand-file-name ".")))
10396 (pwd1 (file-name-as-directory (abbreviate-file-name
10397 (expand-file-name ".")))))
10398 (cond ((equal arg '(16))
10399 (concat "file:"
10400 (abbreviate-file-name (expand-file-name file))))
10401 ((string-match
10402 (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
10403 (concat "file:" (match-string 1 file)))
10404 ((string-match
10405 (concat "^" (regexp-quote pwd) "\\(.+\\)")
10406 (expand-file-name file))
10407 (concat "file:"
10408 (match-string 1 (expand-file-name file))))
10409 (t (concat "file:" file)))))
10411 (defun org-completing-read (&rest args)
10412 "Completing-read with SPACE being a normal character."
10413 (let ((enable-recursive-minibuffers t)
10414 (minibuffer-local-completion-map
10415 (copy-keymap minibuffer-local-completion-map)))
10416 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
10417 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
10418 (org-defkey minibuffer-local-completion-map (kbd "C-c !")
10419 'org-time-stamp-inactive)
10420 (apply #'completing-read args)))
10422 ;;; Opening/following a link
10424 (defvar org-link-search-failed nil)
10426 (defvar org-open-link-functions nil
10427 "Hook for functions finding a plain text link.
10428 These functions must take a single argument, the link content.
10429 They will be called for links that look like [[link text][description]]
10430 when LINK TEXT does not have a protocol like \"http:\" and does not look
10431 like a filename (e.g. \"./blue.png\").
10433 These functions will be called *before* Org attempts to resolve the
10434 link by doing text searches in the current buffer - so if you want a
10435 link \"[[target]]\" to still find \"<<target>>\", your function should
10436 handle this as a special case.
10438 When the function does handle the link, it must return a non-nil value.
10439 If it decides that it is not responsible for this link, it must return
10440 nil to indicate that that Org can continue with other options like
10441 exact and fuzzy text search.")
10443 (defun org-next-link (&optional search-backward)
10444 "Move forward to the next link.
10445 If the link is in hidden text, expose it."
10446 (interactive "P")
10447 (when (and org-link-search-failed (eq this-command last-command))
10448 (goto-char (point-min))
10449 (message "Link search wrapped back to beginning of buffer"))
10450 (setq org-link-search-failed nil)
10451 (let* ((pos (point))
10452 (ct (org-context))
10453 (a (assq :link ct))
10454 (srch-fun (if search-backward 're-search-backward 're-search-forward)))
10455 (cond (a (goto-char (nth (if search-backward 1 2) a)))
10456 ((looking-at org-any-link-re)
10457 ;; Don't stay stuck at link without an org-link face
10458 (forward-char (if search-backward -1 1))))
10459 (if (funcall srch-fun org-any-link-re nil t)
10460 (progn
10461 (goto-char (match-beginning 0))
10462 (when (org-invisible-p) (org-show-context)))
10463 (goto-char pos)
10464 (setq org-link-search-failed t)
10465 (message "No further link found"))))
10467 (defun org-previous-link ()
10468 "Move backward to the previous link.
10469 If the link is in hidden text, expose it."
10470 (interactive)
10471 (funcall 'org-next-link t))
10473 (defun org-translate-link (s)
10474 "Translate a link string if a translation function has been defined."
10475 (with-temp-buffer
10476 (insert (org-trim s))
10477 (org-trim (org-element-interpret-data (org-element-context)))))
10479 (defun org-translate-link-from-planner (type path)
10480 "Translate a link from Emacs Planner syntax so that Org can follow it.
10481 This is still an experimental function, your mileage may vary."
10482 (cond
10483 ((member type '("http" "https" "news" "ftp"))
10484 ;; standard Internet links are the same.
10485 nil)
10486 ((and (equal type "irc") (string-match "^//" path))
10487 ;; Planner has two / at the beginning of an irc link, we have 1.
10488 ;; We should have zero, actually....
10489 (setq path (substring path 1)))
10490 ((and (equal type "lisp") (string-match "^/" path))
10491 ;; Planner has a slash, we do not.
10492 (setq type "elisp" path (substring path 1)))
10493 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
10494 ;; A typical message link. Planner has the id after the final slash,
10495 ;; we separate it with a hash mark
10496 (setq path (concat (match-string 1 path) "#"
10497 (org-unbracket-string "<" ">" (match-string 2 path))))))
10498 (cons type path))
10500 (defun org-find-file-at-mouse (ev)
10501 "Open file link or URL at mouse."
10502 (interactive "e")
10503 (mouse-set-point ev)
10504 (org-open-at-point 'in-emacs))
10506 (defun org-open-at-mouse (ev)
10507 "Open file link or URL at mouse.
10508 See the docstring of `org-open-file' for details."
10509 (interactive "e")
10510 (mouse-set-point ev)
10511 (when (eq major-mode 'org-agenda-mode)
10512 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
10513 (org-open-at-point))
10515 (defvar org-window-config-before-follow-link nil
10516 "The window configuration before following a link.
10517 This is saved in case the need arises to restore it.")
10519 ;;;###autoload
10520 (defun org-open-at-point-global ()
10521 "Follow a link or time-stamp like Org mode does.
10522 This command can be called in any mode to follow an external link
10523 or a time-stamp that has Org mode syntax. Its behavior is
10524 undefined when called on internal links (e.g., fuzzy links).
10525 Raise an error when there is nothing to follow. "
10526 (interactive)
10527 (cond ((org-in-regexp org-any-link-re)
10528 (org-open-link-from-string (match-string-no-properties 0)))
10529 ((or (org-in-regexp org-ts-regexp-both nil t)
10530 (org-in-regexp org-tsr-regexp-both nil t))
10531 (org-follow-timestamp-link))
10532 (t (user-error "No link found"))))
10534 ;;;###autoload
10535 (defun org-open-link-from-string (s &optional arg reference-buffer)
10536 "Open a link in the string S, as if it was in Org mode."
10537 (interactive "sLink: \nP")
10538 (let ((reference-buffer (or reference-buffer (current-buffer))))
10539 (with-temp-buffer
10540 (let ((org-inhibit-startup (not reference-buffer)))
10541 (org-mode)
10542 (insert s)
10543 (goto-char (point-min))
10544 (when reference-buffer
10545 (setq org-link-abbrev-alist-local
10546 (with-current-buffer reference-buffer
10547 org-link-abbrev-alist-local)))
10548 (org-open-at-point arg reference-buffer)))))
10550 (defvar org-open-at-point-functions nil
10551 "Hook that is run when following a link at point.
10553 Functions in this hook must return t if they identify and follow
10554 a link at point. If they don't find anything interesting at point,
10555 they must return nil.")
10557 (defvar org-link-search-inhibit-query nil)
10558 (defvar clean-buffer-list-kill-buffer-names) ;Defined in midnight.el
10559 (defun org--open-doi-link (path)
10560 "Open a \"doi\" type link.
10561 PATH is a the path to search for, as a string."
10562 (browse-url (url-encode-url (concat org-doi-server-url path))))
10564 (defun org--open-elisp-link (path)
10565 "Open a \"elisp\" type link.
10566 PATH is the sexp to evaluate, as a string."
10567 (let ((cmd path))
10568 (if (or (and (org-string-nw-p
10569 org-confirm-elisp-link-not-regexp)
10570 (string-match-p org-confirm-elisp-link-not-regexp cmd))
10571 (not org-confirm-elisp-link-function)
10572 (funcall org-confirm-elisp-link-function
10573 (format "Execute \"%s\" as elisp? "
10574 (org-add-props cmd nil 'face 'org-warning))))
10575 (message "%s => %s" cmd
10576 (if (eq (string-to-char cmd) ?\()
10577 (eval (read cmd))
10578 (call-interactively (read cmd))))
10579 (user-error "Abort"))))
10581 (defun org--open-help-link (path)
10582 "Open a \"help\" type link.
10583 PATH is a symbol name, as a string."
10584 (pcase (intern path)
10585 ((and (pred fboundp) variable) (describe-function variable))
10586 ((and (pred boundp) function) (describe-variable function))
10587 (name (user-error "Unknown function or variable: %s" name))))
10589 (defun org--open-shell-link (path)
10590 "Open a \"shell\" type link.
10591 PATH is the command to execute, as a string."
10592 (let ((buf (generate-new-buffer "*Org Shell Output*"))
10593 (cmd path))
10594 (if (or (and (org-string-nw-p
10595 org-confirm-shell-link-not-regexp)
10596 (string-match
10597 org-confirm-shell-link-not-regexp cmd))
10598 (not org-confirm-shell-link-function)
10599 (funcall org-confirm-shell-link-function
10600 (format "Execute \"%s\" in shell? "
10601 (org-add-props cmd nil
10602 'face 'org-warning))))
10603 (progn
10604 (message "Executing %s" cmd)
10605 (shell-command cmd buf)
10606 (when (featurep 'midnight)
10607 (setq clean-buffer-list-kill-buffer-names
10608 (cons (buffer-name buf)
10609 clean-buffer-list-kill-buffer-names))))
10610 (user-error "Abort"))))
10612 (defun org-open-at-point (&optional arg reference-buffer)
10613 "Open link, timestamp, footnote or tags at point.
10615 When point is on a link, follow it. Normally, files will be
10616 opened by an appropriate application. If the optional prefix
10617 argument ARG is non-nil, Emacs will visit the file. With
10618 a double prefix argument, try to open outside of Emacs, in the
10619 application the system uses for this file type.
10621 When point is on a timestamp, open the agenda at the day
10622 specified.
10624 When point is a footnote definition, move to the first reference
10625 found. If it is on a reference, move to the associated
10626 definition.
10628 When point is on a headline, display a list of every link in the
10629 entry, so it is possible to pick one, or all, of them. If point
10630 is on a tag, call `org-tags-view' instead.
10632 When optional argument REFERENCE-BUFFER is non-nil, it should
10633 specify a buffer from where the link search should happen. This
10634 is used internally by `org-open-link-from-string'.
10636 On top of syntactically correct links, this function will also
10637 try to open links and time-stamps in comments, example
10638 blocks... i.e., whenever point is on something looking like
10639 a timestamp or a link."
10640 (interactive "P")
10641 ;; On a code block, open block's results.
10642 (unless (call-interactively 'org-babel-open-src-block-result)
10643 (org-load-modules-maybe)
10644 (setq org-window-config-before-follow-link (current-window-configuration))
10645 (org-remove-occur-highlights nil nil t)
10646 (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
10647 (let* ((context
10648 ;; Only consider supported types, even if they are not
10649 ;; the closest one.
10650 (org-element-lineage
10651 (org-element-context)
10652 '(clock footnote-definition footnote-reference headline
10653 inlinetask link timestamp)
10655 (type (org-element-type context))
10656 (value (org-element-property :value context)))
10657 (cond
10658 ;; On a headline or an inlinetask, but not on a timestamp,
10659 ;; a link, a footnote reference.
10660 ((memq type '(headline inlinetask))
10661 (org-match-line org-complex-heading-regexp)
10662 (if (and (match-beginning 5)
10663 (>= (point) (match-beginning 5))
10664 (< (point) (match-end 5)))
10665 ;; On tags.
10666 (org-tags-view arg (substring (match-string 5) 0 -1))
10667 ;; Not on tags.
10668 (pcase (org-offer-links-in-entry (current-buffer) (point) arg)
10669 (`(nil . ,_)
10670 (require 'org-attach)
10671 (org-attach-reveal 'if-exists))
10672 (`(,links . ,links-end)
10673 (dolist (link (if (stringp links) (list links) links))
10674 (search-forward link nil links-end)
10675 (goto-char (match-beginning 0))
10676 (org-open-at-point))))))
10677 ;; On a footnote reference or at definition's label.
10678 ((or (eq type 'footnote-reference)
10679 (and (eq type 'footnote-definition)
10680 (save-excursion
10681 ;; Do not validate action when point is on the
10682 ;; spaces right after the footnote label, in
10683 ;; order to be on par with behavior on links.
10684 (skip-chars-forward " \t")
10685 (let ((begin
10686 (org-element-property :contents-begin context)))
10687 (if begin (< (point) begin)
10688 (= (org-element-property :post-affiliated context)
10689 (line-beginning-position)))))))
10690 (org-footnote-action))
10691 ;; No valid context. Ignore catch-all types like `headline'.
10692 ;; If point is on something looking like a link or
10693 ;; a time-stamp, try opening it. It may be useful in
10694 ;; comments, example blocks...
10695 ((memq type '(footnote-definition headline inlinetask nil))
10696 (call-interactively #'org-open-at-point-global))
10697 ;; On a clock line, make sure point is on the timestamp
10698 ;; before opening it.
10699 ((and (eq type 'clock)
10700 value
10701 (>= (point) (org-element-property :begin value))
10702 (<= (point) (org-element-property :end value)))
10703 (org-follow-timestamp-link))
10704 ;; Do nothing on white spaces after an object.
10705 ((>= (point)
10706 (save-excursion
10707 (goto-char (org-element-property :end context))
10708 (skip-chars-backward " \t")
10709 (point)))
10710 (user-error "No link found"))
10711 ((eq type 'timestamp) (org-follow-timestamp-link))
10712 ((eq type 'link)
10713 (let ((type (org-element-property :type context))
10714 (path (org-link-unescape (org-element-property :path context))))
10715 ;; Switch back to REFERENCE-BUFFER needed when called in
10716 ;; a temporary buffer through `org-open-link-from-string'.
10717 (with-current-buffer (or reference-buffer (current-buffer))
10718 (cond
10719 ((equal type "file")
10720 (if (string-match "[*?{]" (file-name-nondirectory path))
10721 (dired path)
10722 ;; Look into `org-link-parameters' in order to find
10723 ;; a DEDICATED-FUNCTION to open file. The function
10724 ;; will be applied on raw link instead of parsed
10725 ;; link due to the limitation in `org-add-link-type'
10726 ;; ("open" function called with a single argument).
10727 ;; If no such function is found, fallback to
10728 ;; `org-open-file'.
10729 (let* ((option (org-element-property :search-option context))
10730 (app (org-element-property :application context))
10731 (dedicated-function
10732 (org-link-get-parameter
10733 (if app (concat type "+" app) type)
10734 :follow)))
10735 (if dedicated-function
10736 (funcall dedicated-function
10737 (concat path
10738 (and option (concat "::" option))))
10739 (apply #'org-open-file
10740 path
10741 (cond (arg)
10742 ((equal app "emacs") 'emacs)
10743 ((equal app "sys") 'system))
10744 (cond ((not option) nil)
10745 ((string-match-p "\\`[0-9]+\\'" option)
10746 (list (string-to-number option)))
10747 (t (list nil
10748 (org-link-unescape option)))))))))
10749 ((functionp (org-link-get-parameter type :follow))
10750 (funcall (org-link-get-parameter type :follow) path))
10751 ((member type '("coderef" "custom-id" "fuzzy" "radio"))
10752 (unless (run-hook-with-args-until-success
10753 'org-open-link-functions path)
10754 (if (not arg) (org-mark-ring-push)
10755 (switch-to-buffer-other-window
10756 (org-get-buffer-for-internal-link (current-buffer))))
10757 (let ((destination
10758 (org-with-wide-buffer
10759 (if (equal type "radio")
10760 (org-search-radio-target
10761 (org-element-property :path context))
10762 (org-link-search
10763 (if (member type '("custom-id" "coderef"))
10764 (org-element-property :raw-link context)
10765 path)
10766 ;; Prevent fuzzy links from matching
10767 ;; themselves.
10768 (and (equal type "fuzzy")
10769 (+ 2 (org-element-property :begin context)))))
10770 (point))))
10771 (unless (and (<= (point-min) destination)
10772 (>= (point-max) destination))
10773 (widen))
10774 (goto-char destination))))
10775 (t (browse-url-at-point))))))
10776 (t (user-error "No link found")))))
10777 (run-hook-with-args 'org-follow-link-hook)))
10779 (defun org-offer-links-in-entry (buffer marker &optional nth zero)
10780 "Offer links in the current entry and return the selected link.
10781 If there is only one link, return it.
10782 If NTH is an integer, return the NTH link found.
10783 If ZERO is a string, check also this string for a link, and if
10784 there is one, return it."
10785 (with-current-buffer buffer
10786 (org-with-wide-buffer
10787 (goto-char marker)
10788 (let ((cnt ?0)
10789 have-zero end links link c)
10790 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
10791 (push (match-string 0 zero) links)
10792 (setq cnt (1- cnt) have-zero t))
10793 (save-excursion
10794 (org-back-to-heading t)
10795 (setq end (save-excursion (outline-next-heading) (point)))
10796 (while (re-search-forward org-any-link-re end t)
10797 (push (match-string 0) links))
10798 (setq links (org-uniquify (reverse links))))
10799 (cond
10800 ((null links)
10801 (message "No links"))
10802 ((equal (length links) 1)
10803 (setq link (car links)))
10804 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
10805 (setq link (nth (if have-zero nth (1- nth)) links)))
10806 (t ; we have to select a link
10807 (save-excursion
10808 (save-window-excursion
10809 (delete-other-windows)
10810 (with-output-to-temp-buffer "*Select Link*"
10811 (dolist (l links)
10812 (cond
10813 ((not (string-match org-bracket-link-regexp l))
10814 (princ (format "[%c] %s\n" (cl-incf cnt)
10815 (org-unbracket-string "<" ">" l))))
10816 ((match-end 3)
10817 (princ (format "[%c] %s (%s)\n" (cl-incf cnt)
10818 (match-string 3 l) (match-string 1 l))))
10819 (t (princ (format "[%c] %s\n" (cl-incf cnt)
10820 (match-string 1 l)))))))
10821 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
10822 (message "Select link to open, RET to open all:")
10823 (setq c (read-char-exclusive))
10824 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
10825 (when (equal c ?q) (user-error "Abort"))
10826 (if (equal c ?\C-m)
10827 (setq link links)
10828 (setq nth (- c ?0))
10829 (when have-zero (setq nth (1+ nth)))
10830 (unless (and (integerp nth) (>= (length links) nth))
10831 (user-error "Invalid link selection"))
10832 (setq link (nth (1- nth) links)))))
10833 (cons link end)))))
10835 ;; TODO: These functions are deprecated since `org-open-at-point'
10836 ;; hard-codes behavior for "file+emacs" and "file+sys" types.
10837 (defun org-open-file-with-system (path)
10838 "Open file at PATH using the system way of opening it."
10839 (org-open-file path 'system))
10840 (defun org-open-file-with-emacs (path)
10841 "Open file at PATH in Emacs."
10842 (org-open-file path 'emacs))
10845 ;;; File search
10847 (defvar org-create-file-search-functions nil
10848 "List of functions to construct the right search string for a file link.
10849 These functions are called in turn with point at the location to
10850 which the link should point.
10852 A function in the hook should first test if it would like to
10853 handle this file type, for example by checking the `major-mode'
10854 or the file extension. If it decides not to handle this file, it
10855 should just return nil to give other functions a chance. If it
10856 does handle the file, it must return the search string to be used
10857 when following the link. The search string will be part of the
10858 file link, given after a double colon, and `org-open-at-point'
10859 will automatically search for it. If special measures must be
10860 taken to make the search successful, another function should be
10861 added to the companion hook `org-execute-file-search-functions',
10862 which see.
10864 A function in this hook may also use `setq' to set the variable
10865 `description' to provide a suggestion for the descriptive text to
10866 be used for this link when it gets inserted into an Org buffer
10867 with \\[org-insert-link].")
10869 (defvar org-execute-file-search-functions nil
10870 "List of functions to execute a file search triggered by a link.
10872 Functions added to this hook must accept a single argument, the
10873 search string that was part of the file link, the part after the
10874 double colon. The function must first check if it would like to
10875 handle this search, for example by checking the `major-mode' or
10876 the file extension. If it decides not to handle this search, it
10877 should just return nil to give other functions a chance. If it
10878 does handle the search, it must return a non-nil value to keep
10879 other functions from trying.
10881 Each function can access the current prefix argument through the
10882 variable `current-prefix-arg'. Note that a single prefix is used
10883 to force opening a link in Emacs, so it may be good to only use a
10884 numeric or double prefix to guide the search function.
10886 In case this is needed, a function in this hook can also restore
10887 the window configuration before `org-open-at-point' was called using:
10889 (set-window-configuration org-window-config-before-follow-link)")
10891 (defun org-search-radio-target (target)
10892 "Search a radio target matching TARGET in current buffer.
10893 White spaces are not significant."
10894 (let ((re (format "<<<%s>>>"
10895 (mapconcat #'regexp-quote
10896 (split-string target)
10897 "[ \t]+\\(?:\n[ \t]*\\)?")))
10898 (origin (point)))
10899 (goto-char (point-min))
10900 (catch :radio-match
10901 (while (re-search-forward re nil t)
10902 (backward-char)
10903 (let ((object (org-element-context)))
10904 (when (eq (org-element-type object) 'radio-target)
10905 (goto-char (org-element-property :begin object))
10906 (org-show-context 'link-search)
10907 (throw :radio-match nil))))
10908 (goto-char origin)
10909 (user-error "No match for radio target: %s" target))))
10911 (defun org-link-search (s &optional avoid-pos stealth)
10912 "Search for a search string S.
10914 If S starts with \"#\", it triggers a custom ID search.
10916 If S is enclosed within parenthesis, it initiates a coderef
10917 search.
10919 If S is surrounded by forward slashes, it is interpreted as
10920 a regular expression. In Org mode files, this will create an
10921 `org-occur' sparse tree. In ordinary files, `occur' will be used
10922 to list matches. If the current buffer is in `dired-mode', grep
10923 will be used to search in all files.
10925 When AVOID-POS is given, ignore matches near that position.
10927 When optional argument STEALTH is non-nil, do not modify
10928 visibility around point, thus ignoring `org-show-context-detail'
10929 variable.
10931 Search is case-insensitive and ignores white spaces. Return type
10932 of matched result, which is either `dedicated' or `fuzzy'."
10933 (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
10934 (let* ((case-fold-search t)
10935 (origin (point))
10936 (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
10937 (starred (eq (string-to-char normalized) ?*))
10938 (words (split-string (if starred (substring s 1) s)))
10939 (s-multi-re (mapconcat #'regexp-quote words "\\(?:[ \t\n]+\\)"))
10940 (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
10941 type)
10942 (cond
10943 ;; Check if there are any special search functions.
10944 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
10945 ((eq (string-to-char s) ?#)
10946 ;; Look for a custom ID S if S starts with "#".
10947 (let* ((id (substring normalized 1))
10948 (match (org-find-property "CUSTOM_ID" id)))
10949 (if match (progn (goto-char match) (setf type 'dedicated))
10950 (error "No match for custom ID: %s" id))))
10951 ((string-match "\\`(\\(.*\\))\\'" normalized)
10952 ;; Look for coderef targets if S is enclosed within parenthesis.
10953 (let ((coderef (match-string-no-properties 1 normalized))
10954 (re (substring s-single-re 1 -1)))
10955 (goto-char (point-min))
10956 (catch :coderef-match
10957 (while (re-search-forward re nil t)
10958 (let ((element (org-element-at-point)))
10959 (when (and (memq (org-element-type element)
10960 '(example-block src-block))
10961 ;; Build proper regexp according to current
10962 ;; block's label format.
10963 (let ((label-fmt
10964 (regexp-quote
10965 (or (org-element-property :label-fmt element)
10966 org-coderef-label-format))))
10967 (save-excursion
10968 (beginning-of-line)
10969 (looking-at (format ".*?\\(%s\\)[ \t]*$"
10970 (format label-fmt coderef))))))
10971 (setq type 'dedicated)
10972 (goto-char (match-beginning 1))
10973 (throw :coderef-match nil))))
10974 (goto-char origin)
10975 (error "No match for coderef: %s" coderef))))
10976 ((string-match "\\`/\\(.*\\)/\\'" normalized)
10977 ;; Look for a regular expression.
10978 (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
10979 (match-string 1 s)))
10980 ;; From here, we handle fuzzy links.
10982 ;; Look for targets, only if not in a headline search.
10983 ((and (not starred)
10984 (let ((target (format "<<%s>>" s-multi-re)))
10985 (catch :target-match
10986 (goto-char (point-min))
10987 (while (re-search-forward target nil t)
10988 (backward-char)
10989 (let ((context (org-element-context)))
10990 (when (eq (org-element-type context) 'target)
10991 (setq type 'dedicated)
10992 (goto-char (org-element-property :begin context))
10993 (throw :target-match t))))
10994 nil))))
10995 ;; Look for elements named after S, only if not in a headline
10996 ;; search.
10997 ((and (not starred)
10998 (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
10999 (catch :name-match
11000 (goto-char (point-min))
11001 (while (re-search-forward name nil t)
11002 (let ((element (org-element-at-point)))
11003 (when (equal words
11004 (split-string
11005 (org-element-property :name element)))
11006 (setq type 'dedicated)
11007 (beginning-of-line)
11008 (throw :name-match t))))
11009 nil))))
11010 ;; Regular text search. Prefer headlines in Org mode buffers.
11011 ;; Ignore COMMENT keyword, TODO keywords, priority cookies,
11012 ;; statistics cookies and tags.
11013 ((and (derived-mode-p 'org-mode)
11014 (let ((title-re
11015 (format "%s.*\\(?:%s[ \t]\\)?.*%s"
11016 org-outline-regexp-bol
11017 org-comment-string
11018 (mapconcat #'regexp-quote words ".+")))
11019 (cookie-re "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]")
11020 (comment-re (eval-when-compile
11021 (format "\\`%s[ \t]+" org-comment-string))))
11022 (goto-char (point-min))
11023 (catch :found
11024 (while (re-search-forward title-re nil t)
11025 (when (equal words
11026 (split-string
11027 (replace-regexp-in-string
11028 cookie-re ""
11029 (replace-regexp-in-string
11030 comment-re "" (org-get-heading t t t)))))
11031 (throw :found t)))
11032 nil)))
11033 (beginning-of-line)
11034 (setq type 'dedicated))
11035 ;; Offer to create non-existent headline depending on
11036 ;; `org-link-search-must-match-exact-headline'.
11037 ((and (derived-mode-p 'org-mode)
11038 (not org-link-search-inhibit-query)
11039 (eq org-link-search-must-match-exact-headline 'query-to-create)
11040 (yes-or-no-p "No match - create this as a new heading? "))
11041 (goto-char (point-max))
11042 (unless (bolp) (newline))
11043 (org-insert-heading nil t t)
11044 (insert s "\n")
11045 (beginning-of-line 0))
11046 ;; Only headlines are looked after. No need to process
11047 ;; further: throw an error.
11048 ((and (derived-mode-p 'org-mode)
11049 (or starred org-link-search-must-match-exact-headline))
11050 (goto-char origin)
11051 (error "No match for fuzzy expression: %s" normalized))
11052 ;; Regular text search.
11053 ((catch :fuzzy-match
11054 (goto-char (point-min))
11055 (while (re-search-forward s-multi-re nil t)
11056 ;; Skip match if it contains AVOID-POS or it is included in
11057 ;; a link with a description but outside the description.
11058 (unless (or (and avoid-pos
11059 (<= (match-beginning 0) avoid-pos)
11060 (> (match-end 0) avoid-pos))
11061 (and (save-match-data
11062 (org-in-regexp org-bracket-link-regexp))
11063 (match-beginning 3)
11064 (or (> (match-beginning 3) (point))
11065 (<= (match-end 3) (point)))
11066 (org-element-lineage
11067 (save-match-data (org-element-context))
11068 '(link) t)))
11069 (goto-char (match-beginning 0))
11070 (setq type 'fuzzy)
11071 (throw :fuzzy-match t)))
11072 nil))
11073 ;; All failed. Throw an error.
11074 (t (goto-char origin)
11075 (error "No match for fuzzy expression: %s" normalized)))
11076 ;; Disclose surroundings of match, if appropriate.
11077 (when (and (derived-mode-p 'org-mode) (not stealth))
11078 (org-show-context 'link-search))
11079 type))
11081 (defun org-get-buffer-for-internal-link (buffer)
11082 "Return a buffer to be used for displaying the link target of internal links."
11083 (cond
11084 ((not org-display-internal-link-with-indirect-buffer)
11085 buffer)
11086 ((string-suffix-p "(Clone)" (buffer-name buffer))
11087 (message "Buffer is already a clone, not making another one")
11088 ;; we also do not modify visibility in this case
11089 buffer)
11090 (t ; make a new indirect buffer for displaying the link
11091 (let* ((bn (buffer-name buffer))
11092 (ibn (concat bn "(Clone)"))
11093 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
11094 (with-current-buffer ib (org-overview))
11095 ib))))
11097 (defun org-do-occur (regexp &optional cleanup)
11098 "Call the Emacs command `occur'.
11099 If CLEANUP is non-nil, remove the printout of the regular expression
11100 in the *Occur* buffer. This is useful if the regex is long and not useful
11101 to read."
11102 (occur regexp)
11103 (when cleanup
11104 (let ((cwin (selected-window)) win beg end)
11105 (when (setq win (get-buffer-window "*Occur*"))
11106 (select-window win))
11107 (goto-char (point-min))
11108 (when (re-search-forward "match[a-z]+" nil t)
11109 (setq beg (match-end 0))
11110 (when (re-search-forward "^[ \t]*[0-9]+" nil t)
11111 (setq end (1- (match-beginning 0)))))
11112 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
11113 (goto-char (point-min))
11114 (select-window cwin))))
11116 ;;; The mark ring for links jumps
11118 (defvar org-mark-ring nil
11119 "Mark ring for positions before jumps in Org mode.")
11120 (defvar org-mark-ring-last-goto nil
11121 "Last position in the mark ring used to go back.")
11122 ;; Fill and close the ring
11123 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
11124 (dotimes (_ org-mark-ring-length)
11125 (push (make-marker) org-mark-ring))
11126 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
11127 org-mark-ring)
11129 (defun org-mark-ring-push (&optional pos buffer)
11130 "Put the current position or POS into the mark ring and rotate it."
11131 (interactive)
11132 (setq pos (or pos (point)))
11133 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
11134 (move-marker (car org-mark-ring)
11135 (or pos (point))
11136 (or buffer (current-buffer)))
11137 (message "%s"
11138 (substitute-command-keys
11139 "Position saved to mark ring, go back with \
11140 `\\[org-mark-ring-goto]'.")))
11142 (defun org-mark-ring-goto (&optional n)
11143 "Jump to the previous position in the mark ring.
11144 With prefix arg N, jump back that many stored positions. When
11145 called several times in succession, walk through the entire ring.
11146 Org mode commands jumping to a different position in the current file,
11147 or to another Org file, automatically push the old position onto the ring."
11148 (interactive "p")
11149 (let (p m)
11150 (if (eq last-command this-command)
11151 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
11152 (setq p org-mark-ring))
11153 (setq org-mark-ring-last-goto p)
11154 (setq m (car p))
11155 (pop-to-buffer-same-window (marker-buffer m))
11156 (goto-char m)
11157 (when (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
11159 (defun org-add-angle-brackets (s)
11160 (unless (equal (substring s 0 1) "<") (setq s (concat "<" s)))
11161 (unless (equal (substring s -1) ">") (setq s (concat s ">")))
11164 ;;; Following specific links
11166 (defvar org-agenda-buffer-tmp-name)
11167 (defvar org-agenda-start-on-weekday)
11168 (defun org-follow-timestamp-link ()
11169 "Open an agenda view for the time-stamp date/range at point."
11170 (cond
11171 ((org-at-date-range-p t)
11172 (let ((org-agenda-start-on-weekday)
11173 (t1 (match-string 1))
11174 (t2 (match-string 2)) tt1 tt2)
11175 (setq tt1 (time-to-days (org-time-string-to-time t1))
11176 tt2 (time-to-days (org-time-string-to-time t2)))
11177 (let ((org-agenda-buffer-tmp-name
11178 (format "*Org Agenda(a:%s)"
11179 (concat (substring t1 0 10) "--" (substring t2 0 10)))))
11180 (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
11181 ((org-at-timestamp-p 'lax)
11182 (let ((org-agenda-buffer-tmp-name
11183 (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
11184 (org-agenda-list nil (time-to-days (org-time-string-to-time
11185 (substring (match-string 1) 0 10)))
11186 1)))
11187 (t (error "This should not happen"))))
11190 ;;; Following file links
11191 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
11192 (declare-function mailcap-extension-to-mime "mailcap" (extn))
11193 (declare-function mailcap-mime-info
11194 "mailcap" (string &optional request no-decode))
11195 (defvar org-wait nil)
11196 (defun org-open-file (path &optional in-emacs line search)
11197 "Open the file at PATH.
11198 First, this expands any special file name abbreviations. Then the
11199 configuration variable `org-file-apps' is checked if it contains an
11200 entry for this file type, and if yes, the corresponding command is launched.
11202 If no application is found, Emacs simply visits the file.
11204 With optional prefix argument IN-EMACS, Emacs will visit the file.
11205 With a double \\[universal-argument] \\[universal-argument] \
11206 prefix arg, Org tries to avoid opening in Emacs
11207 and to use an external application to visit the file.
11209 Optional LINE specifies a line to go to, optional SEARCH a string
11210 to search for. If LINE or SEARCH is given, the file will be
11211 opened in Emacs, unless an entry from org-file-apps that makes
11212 use of groups in a regexp matches.
11214 If you want to change the way frames are used when following a
11215 link, please customize `org-link-frame-setup'.
11217 If the file does not exist, an error is thrown."
11218 (let* ((file (if (equal path "")
11219 buffer-file-name
11220 (substitute-in-file-name (expand-file-name path))))
11221 (file-apps (append org-file-apps (org-default-apps)))
11222 (apps (cl-remove-if
11223 'org-file-apps-entry-match-against-dlink-p file-apps))
11224 (apps-dlink (cl-remove-if-not
11225 'org-file-apps-entry-match-against-dlink-p file-apps))
11226 (remp (and (assq 'remote apps) (org-file-remote-p file)))
11227 (dirp (unless remp (file-directory-p file)))
11228 (file (if (and dirp org-open-directory-means-index-dot-org)
11229 (concat (file-name-as-directory file) "index.org")
11230 file))
11231 (a-m-a-p (assq 'auto-mode apps))
11232 (dfile (downcase file))
11233 ;; Reconstruct the original link from the PATH, LINE and
11234 ;; SEARCH args.
11235 (link (cond (line (concat file "::" (number-to-string line)))
11236 (search (concat file "::" search))
11237 (t file)))
11238 (dlink (downcase link))
11239 (ext
11240 (and (string-match "\\`.*?\\.\\([a-zA-Z0-9]+\\(\\.gz\\)?\\)\\'" dfile)
11241 (match-string 1 dfile)))
11242 (save-position-maybe
11243 (let ((old-buffer (current-buffer))
11244 (old-pos (point))
11245 (old-mode major-mode))
11246 (lambda ()
11247 (and (derived-mode-p 'org-mode)
11248 (eq old-mode 'org-mode)
11249 (or (not (eq old-buffer (current-buffer)))
11250 (not (eq old-pos (point))))
11251 (org-mark-ring-push old-pos old-buffer)))))
11252 cmd link-match-data)
11253 (cond
11254 ((member in-emacs '((16) system))
11255 (setq cmd (cdr (assq 'system apps))))
11256 (in-emacs (setq cmd 'emacs))
11258 (setq cmd (or (and remp (cdr (assq 'remote apps)))
11259 (and dirp (cdr (assq 'directory apps)))
11260 ;; First, try matching against apps-dlink if we
11261 ;; get a match here, store the match data for
11262 ;; later.
11263 (let ((match (assoc-default dlink apps-dlink
11264 'string-match)))
11265 (if match
11266 (progn (setq link-match-data (match-data))
11267 match)
11268 (progn (setq in-emacs (or in-emacs line search))
11269 nil))) ; if we have no match in apps-dlink,
11270 ; always open the file in emacs if line or search
11271 ; is given (for backwards compatibility)
11272 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
11273 'string-match)
11274 (cdr (assoc ext apps))
11275 (cdr (assq t apps))))))
11276 (when (eq cmd 'system)
11277 (setq cmd (cdr (assq 'system apps))))
11278 (when (eq cmd 'default)
11279 (setq cmd (cdr (assoc t apps))))
11280 (when (eq cmd 'mailcap)
11281 (require 'mailcap)
11282 (mailcap-parse-mailcaps)
11283 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
11284 (command (mailcap-mime-info mime-type)))
11285 (if (stringp command)
11286 (setq cmd command)
11287 (setq cmd 'emacs))))
11288 (when (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
11289 (not (file-exists-p file))
11290 (not org-open-non-existing-files))
11291 (user-error "No such file: %s" file))
11292 (cond
11293 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
11294 ;; Remove quotes around the file name - we'll use shell-quote-argument.
11295 (while (string-match "['\"]%s['\"]" cmd)
11296 (setq cmd (replace-match "%s" t t cmd)))
11297 (setq cmd (replace-regexp-in-string
11298 "%s"
11299 (shell-quote-argument (convert-standard-filename file))
11301 nil t))
11303 ;; Replace "%1", "%2" etc. in command with group matches from regex
11304 (save-match-data
11305 (let ((match-index 1)
11306 (number-of-groups (- (/ (length link-match-data) 2) 1)))
11307 (set-match-data link-match-data)
11308 (while (<= match-index number-of-groups)
11309 (let ((regex (concat "%" (number-to-string match-index)))
11310 (replace-with (match-string match-index dlink)))
11311 (while (string-match regex cmd)
11312 (setq cmd (replace-match replace-with t t cmd))))
11313 (setq match-index (+ match-index 1)))))
11315 (save-window-excursion
11316 (message "Running %s...done" cmd)
11317 (start-process-shell-command cmd nil cmd)
11318 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
11319 ((or (stringp cmd)
11320 (eq cmd 'emacs))
11321 (funcall (cdr (assq 'file org-link-frame-setup)) file)
11322 (widen)
11323 (cond (line (org-goto-line line)
11324 (when (derived-mode-p 'org-mode) (org-reveal)))
11325 (search (condition-case err
11326 (org-link-search search)
11327 ;; Save position before error-ing out so user
11328 ;; can easily move back to the original buffer.
11329 (error (funcall save-position-maybe)
11330 (error (nth 1 err)))))))
11331 ((functionp cmd)
11332 (save-match-data
11333 (set-match-data link-match-data)
11334 (condition-case nil
11335 (funcall cmd file link)
11336 ;; FIXME: Remove this check when most default installations
11337 ;; of Emacs have at least Org 9.0.
11338 ((debug wrong-number-of-arguments wrong-type-argument
11339 invalid-function)
11340 (user-error "Please see Org News for version 9.0 about \
11341 `org-file-apps'--Lisp error: %S" cmd)))))
11342 ((consp cmd)
11343 ;; FIXME: Remove this check when most default installations of
11344 ;; Emacs have at least Org 9.0. Heads-up instead of silently
11345 ;; fall back to `org-link-frame-setup' for an old usage of
11346 ;; `org-file-apps' with sexp instead of a function for `cmd'.
11347 (user-error "Please see Org News for version 9.0 about \
11348 `org-file-apps'--Error: Deprecated usage of %S" cmd))
11349 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
11350 (funcall save-position-maybe)))
11352 (defun org-file-apps-entry-match-against-dlink-p (entry)
11353 "This function returns non-nil if `entry' uses a regular
11354 expression which should be matched against the whole link by
11355 org-open-file.
11357 It assumes that is the case when the entry uses a regular
11358 expression which has at least one grouping construct and the
11359 action is either a lisp form or a command string containing
11360 `%1', i.e. using at least one subexpression match as a
11361 parameter."
11362 (let ((selector (car entry))
11363 (action (cdr entry)))
11364 (if (stringp selector)
11365 (and (> (regexp-opt-depth selector) 0)
11366 (or (and (stringp action)
11367 (string-match "%[0-9]" action))
11368 (consp action)))
11369 nil)))
11371 (defun org-default-apps ()
11372 "Return the default applications for this operating system."
11373 (cond
11374 ((eq system-type 'darwin)
11375 org-file-apps-defaults-macosx)
11376 ((eq system-type 'windows-nt)
11377 org-file-apps-defaults-windowsnt)
11378 (t org-file-apps-defaults-gnu)))
11380 (defun org-apps-regexp-alist (list &optional add-auto-mode)
11381 "Convert extensions to regular expressions in the cars of LIST.
11382 Also, weed out any non-string entries, because the return value is used
11383 only for regexp matching.
11384 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
11385 point to the symbol `emacs', indicating that the file should
11386 be opened in Emacs."
11387 (append
11388 (delq nil
11389 (mapcar (lambda (x)
11390 (unless (not (stringp (car x)))
11391 (if (string-match "\\W" (car x))
11393 (cons (concat "\\." (car x) "\\'") (cdr x)))))
11394 list))
11395 (when add-auto-mode
11396 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
11398 (defvar ange-ftp-name-format)
11399 (defun org-file-remote-p (file)
11400 "Test whether FILE specifies a location on a remote system.
11401 Return non-nil if the location is indeed remote.
11403 For example, the filename \"/user@host:/foo\" specifies a location
11404 on the system \"/user@host:\"."
11405 (cond ((fboundp 'file-remote-p)
11406 (file-remote-p file))
11407 ((fboundp 'tramp-handle-file-remote-p)
11408 (tramp-handle-file-remote-p file))
11409 ((and (boundp 'ange-ftp-name-format)
11410 (string-match (car ange-ftp-name-format) file))
11411 t)))
11414 ;;;; Refiling
11416 (defun org-get-org-file ()
11417 "Read a filename, with default directory `org-directory'."
11418 (let ((default (or org-default-notes-file remember-data-file)))
11419 (read-file-name (format "File name [%s]: " default)
11420 (file-name-as-directory org-directory)
11421 default)))
11423 (defun org-notes-order-reversed-p ()
11424 "Check if the current file should receive notes in reversed order."
11425 (cond
11426 ((not org-reverse-note-order) nil)
11427 ((eq t org-reverse-note-order) t)
11428 ((not (listp org-reverse-note-order)) nil)
11429 (t (catch 'exit
11430 (dolist (entry org-reverse-note-order)
11431 (when (string-match (car entry) buffer-file-name)
11432 (throw 'exit (cdr entry))))))))
11434 (defvar org-refile-target-table nil
11435 "The list of refile targets, created by `org-refile'.")
11437 (defvar org-agenda-new-buffers nil
11438 "Buffers created to visit agenda files.")
11440 (defvar org-refile-cache nil
11441 "Cache for refile targets.")
11443 (defvar org-refile-markers nil
11444 "All the markers used for caching refile locations.")
11446 (defun org-refile-marker (pos)
11447 "Get a new refile marker, but only if caching is in use."
11448 (if (not org-refile-use-cache)
11450 (let ((m (make-marker)))
11451 (move-marker m pos)
11452 (push m org-refile-markers)
11453 m)))
11455 (defun org-refile-cache-clear ()
11456 "Clear the refile cache and disable all the markers."
11457 (dolist (m org-refile-markers) (move-marker m nil))
11458 (setq org-refile-markers nil)
11459 (setq org-refile-cache nil)
11460 (message "Refile cache has been cleared"))
11462 (defun org-refile-cache-check-set (set)
11463 "Check if all the markers in the cache still have live buffers."
11464 (let (marker)
11465 (catch 'exit
11466 (while (and set (setq marker (nth 3 (pop set))))
11467 ;; If `org-refile-use-outline-path' is 'file, marker may be nil
11468 (when (and marker (null (marker-buffer marker)))
11469 (message "Please regenerate the refile cache with `C-0 C-c C-w'")
11470 (sit-for 3)
11471 (throw 'exit nil)))
11472 t)))
11474 (defun org-refile-cache-put (set &rest identifiers)
11475 "Push the refile targets SET into the cache, under IDENTIFIERS."
11476 (let* ((key (sha1 (prin1-to-string identifiers)))
11477 (entry (assoc key org-refile-cache)))
11478 (if entry
11479 (setcdr entry set)
11480 (push (cons key set) org-refile-cache))))
11482 (defun org-refile-cache-get (&rest identifiers)
11483 "Retrieve the cached value for refile targets given by IDENTIFIERS."
11484 (cond
11485 ((not org-refile-cache) nil)
11486 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
11488 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
11489 org-refile-cache))))
11490 (and set (org-refile-cache-check-set set) set)))))
11492 (defvar org-outline-path-cache nil
11493 "Alist between buffer positions and outline paths.
11494 It value is an alist (POSITION . PATH) where POSITION is the
11495 buffer position at the beginning of an entry and PATH is a list
11496 of strings describing the outline path for that entry, in reverse
11497 order.")
11499 (defun org-refile-get-targets (&optional default-buffer)
11500 "Produce a table with refile targets."
11501 (let ((case-fold-search nil)
11502 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
11503 (entries (or org-refile-targets '((nil . (:level . 1)))))
11504 targets tgs files desc descre)
11505 (message "Getting targets...")
11506 (with-current-buffer (or default-buffer (current-buffer))
11507 (dolist (entry entries)
11508 (setq files (car entry) desc (cdr entry))
11509 (cond
11510 ((null files) (setq files (list (current-buffer))))
11511 ((eq files 'org-agenda-files)
11512 (setq files (org-agenda-files 'unrestricted)))
11513 ((and (symbolp files) (fboundp files))
11514 (setq files (funcall files)))
11515 ((and (symbolp files) (boundp files))
11516 (setq files (symbol-value files))))
11517 (when (stringp files) (setq files (list files)))
11518 (cond
11519 ((eq (car desc) :tag)
11520 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
11521 ((eq (car desc) :todo)
11522 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
11523 ((eq (car desc) :regexp)
11524 (setq descre (cdr desc)))
11525 ((eq (car desc) :level)
11526 (setq descre (concat "^\\*\\{" (number-to-string
11527 (if org-odd-levels-only
11528 (1- (* 2 (cdr desc)))
11529 (cdr desc)))
11530 "\\}[ \t]")))
11531 ((eq (car desc) :maxlevel)
11532 (setq descre (concat "^\\*\\{1," (number-to-string
11533 (if org-odd-levels-only
11534 (1- (* 2 (cdr desc)))
11535 (cdr desc)))
11536 "\\}[ \t]")))
11537 (t (error "Bad refiling target description %s" desc)))
11538 (dolist (f files)
11539 (with-current-buffer (if (bufferp f) f (org-get-agenda-file-buffer f))
11541 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
11542 (progn
11543 (when (bufferp f)
11544 (setq f (buffer-file-name (buffer-base-buffer f))))
11545 (setq f (and f (expand-file-name f)))
11546 (when (eq org-refile-use-outline-path 'file)
11547 (push (list (file-name-nondirectory f) f nil nil) tgs))
11548 (when (eq org-refile-use-outline-path 'buffer-name)
11549 (push (list (buffer-name (buffer-base-buffer)) f nil nil) tgs))
11550 (when (eq org-refile-use-outline-path 'full-file-path)
11551 (push (list (file-truename (buffer-file-name (buffer-base-buffer))) f nil nil) tgs))
11552 (org-with-wide-buffer
11553 (goto-char (point-min))
11554 (setq org-outline-path-cache nil)
11555 (while (re-search-forward descre nil t)
11556 (beginning-of-line)
11557 (let ((case-fold-search nil))
11558 (looking-at org-complex-heading-regexp))
11559 (let ((begin (point))
11560 (heading (match-string-no-properties 4)))
11561 (unless (or (and
11562 org-refile-target-verify-function
11563 (not
11564 (funcall org-refile-target-verify-function)))
11565 (not heading))
11566 (let ((re (format org-complex-heading-regexp-format
11567 (regexp-quote heading)))
11568 (target
11569 (if (not org-refile-use-outline-path) heading
11570 (mapconcat
11571 #'identity
11572 (append
11573 (pcase org-refile-use-outline-path
11574 (`file (list (file-name-nondirectory
11575 (buffer-file-name
11576 (buffer-base-buffer)))))
11577 (`full-file-path
11578 (list (buffer-file-name
11579 (buffer-base-buffer))))
11580 (`buffer-name
11581 (list (buffer-name
11582 (buffer-base-buffer))))
11583 (_ nil))
11584 (mapcar (lambda (s) (replace-regexp-in-string
11585 "/" "\\/" s nil t))
11586 (org-get-outline-path t t)))
11587 "/"))))
11588 (push (list target f re (org-refile-marker (point)))
11589 tgs)))
11590 (when (= (point) begin)
11591 ;; Verification function has not moved point.
11592 (end-of-line)))))))
11593 (when org-refile-use-cache
11594 (org-refile-cache-put tgs (buffer-file-name) descre))
11595 (setq targets (append tgs targets))))))
11596 (message "Getting targets...done")
11597 (delete-dups (nreverse targets))))
11599 (defun org--get-outline-path-1 (&optional use-cache)
11600 "Return outline path to current headline.
11602 Outline path is a list of strings, in reverse order. When
11603 optional argument USE-CACHE is non-nil, make use of a cache. See
11604 `org-get-outline-path' for details.
11606 Assume buffer is widened and point is on a headline."
11607 (or (and use-cache (cdr (assq (point) org-outline-path-cache)))
11608 (let ((p (point))
11609 (heading (let ((case-fold-search nil))
11610 (looking-at org-complex-heading-regexp)
11611 (if (not (match-end 4)) ""
11612 ;; Remove statistics cookies.
11613 (org-trim
11614 (org-link-display-format
11615 (replace-regexp-in-string
11616 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
11617 (match-string-no-properties 4))))))))
11618 (if (org-up-heading-safe)
11619 (let ((path (cons heading (org--get-outline-path-1 use-cache))))
11620 (when use-cache
11621 (push (cons p path) org-outline-path-cache))
11622 path)
11623 ;; This is a new root node. Since we assume we are moving
11624 ;; forward, we can drop previous cache so as to limit number
11625 ;; of associations there.
11626 (let ((path (list heading)))
11627 (when use-cache (setq org-outline-path-cache (list (cons p path))))
11628 path)))))
11630 (defun org-get-outline-path (&optional with-self use-cache)
11631 "Return the outline path to the current entry.
11633 An outline path is a list of ancestors for current headline, as
11634 a list of strings. Statistics cookies are removed and links are
11635 replaced with their description, if any, or their path otherwise.
11637 When optional argument WITH-SELF is non-nil, the path also
11638 includes the current headline.
11640 When optional argument USE-CACHE is non-nil, cache outline paths
11641 between calls to this function so as to avoid backtracking. This
11642 argument is useful when planning to find more than one outline
11643 path in the same document. In that case, there are two
11644 conditions to satisfy:
11645 - `org-outline-path-cache' is set to nil before starting the
11646 process;
11647 - outline paths are computed by increasing buffer positions."
11648 (org-with-wide-buffer
11649 (and (or (and with-self (org-back-to-heading t))
11650 (org-up-heading-safe))
11651 (reverse (org--get-outline-path-1 use-cache)))))
11653 (defun org-format-outline-path (path &optional width prefix separator)
11654 "Format the outline path PATH for display.
11655 WIDTH is the maximum number of characters that is available.
11656 PREFIX is a prefix to be included in the returned string,
11657 such as the file name.
11658 SEPARATOR is inserted between the different parts of the path,
11659 the default is \"/\"."
11660 (setq width (or width 79))
11661 (setq path (delq nil path))
11662 (unless (> width 0)
11663 (user-error "Argument `width' must be positive"))
11664 (setq separator (or separator "/"))
11665 (let* ((org-odd-levels-only nil)
11666 (fpath (concat
11667 prefix (and prefix path separator)
11668 (mapconcat
11669 (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
11670 (cl-loop for head in path
11671 for n from 0
11672 collect (org-add-props
11673 head nil 'face
11674 (nth (% n org-n-level-faces) org-level-faces)))
11675 separator))))
11676 (when (> (length fpath) width)
11677 (if (< width 7)
11678 ;; It's unlikely that `width' will be this small, but don't
11679 ;; waste characters by adding ".." if it is.
11680 (setq fpath (substring fpath 0 width))
11681 (setf (substring fpath (- width 2)) "..")))
11682 fpath))
11684 (defun org-display-outline-path (&optional file current separator just-return-string)
11685 "Display the current outline path in the echo area.
11687 If FILE is non-nil, prepend the output with the file name.
11688 If CURRENT is non-nil, append the current heading to the output.
11689 SEPARATOR is passed through to `org-format-outline-path'. It separates
11690 the different parts of the path and defaults to \"/\".
11691 If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
11692 (interactive "P")
11693 (let* (case-fold-search
11694 (bfn (buffer-file-name (buffer-base-buffer)))
11695 (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
11696 res)
11697 (when current (setq path (append path
11698 (save-excursion
11699 (org-back-to-heading t)
11700 (when (looking-at org-complex-heading-regexp)
11701 (list (match-string 4)))))))
11702 (setq res
11703 (org-format-outline-path
11704 path
11705 (1- (frame-width))
11706 (and file bfn (concat (file-name-nondirectory bfn) separator))
11707 separator))
11708 (if just-return-string
11709 (org-no-properties res)
11710 (org-unlogged-message "%s" res))))
11712 (defvar org-refile-history nil
11713 "History for refiling operations.")
11715 (defvar org-after-refile-insert-hook nil
11716 "Hook run after `org-refile' has inserted its stuff at the new location.
11717 Note that this is still *before* the stuff will be removed from
11718 the *old* location.")
11720 (defvar org-capture-last-stored-marker)
11721 (defvar org-refile-keep nil
11722 "Non-nil means `org-refile' will copy instead of refile.")
11724 (defun org-copy ()
11725 "Like `org-refile', but copy."
11726 (interactive)
11727 (let ((org-refile-keep t))
11728 (funcall 'org-refile nil nil nil "Copy")))
11730 (defun org-refile (&optional arg default-buffer rfloc msg)
11731 "Move the entry or entries at point to another heading.
11733 The list of target headings is compiled using the information in
11734 `org-refile-targets', which see.
11736 At the target location, the entry is filed as a subitem of the
11737 target heading. Depending on `org-reverse-note-order', the new
11738 subitem will either be the first or the last subitem.
11740 If there is an active region, all entries in that region will be
11741 refiled. However, the region must fulfill the requirement that
11742 the first heading sets the top-level of the moved text.
11744 With a `\\[universal-argument]' ARG, the command will only visit the target \
11745 location
11746 and not actually move anything.
11748 With a prefix `\\[universal-argument] \\[universal-argument]', go to the \
11749 location where the last
11750 refiling operation has put the subtree.
11752 With a numeric prefix argument of `2', refile to the running clock.
11754 With a numeric prefix argument of `3', emulate `org-refile-keep'
11755 being set to t and copy to the target location, don't move it.
11756 Beware that keeping refiled entries may result in duplicated ID
11757 properties.
11759 RFLOC can be a refile location obtained in a different way.
11761 MSG is a string to replace \"Refile\" in the default prompt with
11762 another verb. E.g. `org-copy' sets this parameter to \"Copy\".
11764 See also `org-refile-use-outline-path'.
11766 If you are using target caching (see `org-refile-use-cache'), you
11767 have to clear the target cache in order to find new targets.
11768 This can be done with a `0' prefix (`C-0 C-c C-w') or a triple
11769 prefix argument (`C-u C-u C-u C-c C-w')."
11770 (interactive "P")
11771 (if (member arg '(0 (64)))
11772 (org-refile-cache-clear)
11773 (let* ((actionmsg (cond (msg msg)
11774 ((equal arg 3) "Refile (and keep)")
11775 (t "Refile")))
11776 (regionp (org-region-active-p))
11777 (region-start (and regionp (region-beginning)))
11778 (region-end (and regionp (region-end)))
11779 (org-refile-keep (if (equal arg 3) t org-refile-keep))
11780 pos it nbuf file level reversed)
11781 (setq last-command nil)
11782 (when regionp
11783 (goto-char region-start)
11784 (or (bolp) (goto-char (point-at-bol)))
11785 (setq region-start (point))
11786 (unless (or (org-kill-is-subtree-p
11787 (buffer-substring region-start region-end))
11788 (prog1 org-refile-active-region-within-subtree
11789 (let ((s (point-at-eol)))
11790 (org-toggle-heading)
11791 (setq region-end (+ (- (point-at-eol) s) region-end)))))
11792 (user-error "The region is not a (sequence of) subtree(s)")))
11793 (if (equal arg '(16))
11794 (org-refile-goto-last-stored)
11795 (when (or
11796 (and (equal arg 2)
11797 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
11798 (prog1
11799 (setq it (list (or org-clock-heading "running clock")
11800 (buffer-file-name
11801 (marker-buffer org-clock-hd-marker))
11803 (marker-position org-clock-hd-marker)))
11804 (setq arg nil)))
11805 (setq it
11806 (or rfloc
11807 (let (heading-text)
11808 (save-excursion
11809 (unless (and arg (listp arg))
11810 (org-back-to-heading t)
11811 (setq heading-text
11812 (replace-regexp-in-string
11813 org-bracket-link-regexp
11814 "\\3"
11815 (or (nth 4 (org-heading-components))
11816 ""))))
11817 (org-refile-get-location
11818 (cond ((and arg (listp arg)) "Goto")
11819 (regionp (concat actionmsg " region to"))
11820 (t (concat actionmsg " subtree \""
11821 heading-text "\" to")))
11822 default-buffer
11823 (and (not (equal '(4) arg))
11824 org-refile-allow-creating-parent-nodes)))))))
11825 (setq file (nth 1 it)
11826 pos (nth 3 it))
11827 (when (and (not arg)
11829 (equal (buffer-file-name) file)
11830 (if regionp
11831 (and (>= pos region-start)
11832 (<= pos region-end))
11833 (and (>= pos (point))
11834 (< pos (save-excursion
11835 (org-end-of-subtree t t))))))
11836 (error "Cannot refile to position inside the tree or region"))
11837 (setq nbuf (or (find-buffer-visiting file)
11838 (find-file-noselect file)))
11839 (if (and arg (not (equal arg 3)))
11840 (progn
11841 (pop-to-buffer-same-window nbuf)
11842 (goto-char (cond (pos)
11843 ((org-notes-order-reversed-p) (point-min))
11844 (t (point-max))))
11845 (org-show-context 'org-goto))
11846 (if regionp
11847 (progn
11848 (org-kill-new (buffer-substring region-start region-end))
11849 (org-save-markers-in-region region-start region-end))
11850 (org-copy-subtree 1 nil t))
11851 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
11852 (find-file-noselect file)))
11853 (setq reversed (org-notes-order-reversed-p))
11854 (org-with-wide-buffer
11855 (if pos
11856 (progn
11857 (goto-char pos)
11858 (setq level (org-get-valid-level (funcall outline-level) 1))
11859 (goto-char
11860 (if reversed
11861 (or (outline-next-heading) (point-max))
11862 (or (save-excursion (org-get-next-sibling))
11863 (org-end-of-subtree t t)
11864 (point-max)))))
11865 (setq level 1)
11866 (if (not reversed)
11867 (goto-char (point-max))
11868 (goto-char (point-min))
11869 (or (outline-next-heading) (goto-char (point-max)))))
11870 (unless (bolp) (newline))
11871 (org-paste-subtree level nil nil t)
11872 (when org-log-refile
11873 (org-add-log-setup 'refile nil nil org-log-refile)
11874 (unless (eq org-log-refile 'note)
11875 (save-excursion (org-add-log-note))))
11876 (and org-auto-align-tags
11877 (let ((org-loop-over-headlines-in-active-region nil))
11878 (org-set-tags nil t)))
11879 (let ((bookmark-name (plist-get org-bookmark-names-plist
11880 :last-refile)))
11881 (when bookmark-name
11882 (with-demoted-errors
11883 (bookmark-set bookmark-name))))
11884 ;; If we are refiling for capture, make sure that the
11885 ;; last-capture pointers point here
11886 (when (bound-and-true-p org-capture-is-refiling)
11887 (let ((bookmark-name (plist-get org-bookmark-names-plist
11888 :last-capture-marker)))
11889 (when bookmark-name
11890 (with-demoted-errors
11891 (bookmark-set bookmark-name))))
11892 (move-marker org-capture-last-stored-marker (point)))
11893 (when (fboundp 'deactivate-mark) (deactivate-mark))
11894 (run-hooks 'org-after-refile-insert-hook)))
11895 (unless org-refile-keep
11896 (if regionp
11897 (delete-region (point) (+ (point) (- region-end region-start)))
11898 (delete-region
11899 (and (org-back-to-heading t) (point))
11900 (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
11901 (when (featurep 'org-inlinetask)
11902 (org-inlinetask-remove-END-maybe))
11903 (setq org-markers-to-move nil)
11904 (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
11906 (defun org-refile-goto-last-stored ()
11907 "Go to the location where the last refile was stored."
11908 (interactive)
11909 (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
11910 (message "This is the location of the last refile"))
11912 (defun org-refile--get-location (refloc tbl)
11913 "When user refile to REFLOC, find the associated target in TBL.
11914 Also check `org-refile-target-table'."
11915 (car (delq
11917 (mapcar
11918 (lambda (r) (or (assoc r tbl)
11919 (assoc r org-refile-target-table)))
11920 (list (replace-regexp-in-string "/$" "" refloc)
11921 (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
11923 (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
11924 "Prompt the user for a refile location, using PROMPT.
11925 PROMPT should not be suffixed with a colon and a space, because
11926 this function appends the default value from
11927 `org-refile-history' automatically, if that is not empty."
11928 (let ((org-refile-targets org-refile-targets)
11929 (org-refile-use-outline-path org-refile-use-outline-path))
11930 (setq org-refile-target-table (org-refile-get-targets default-buffer)))
11931 (unless org-refile-target-table
11932 (user-error "No refile targets"))
11933 (let* ((cbuf (current-buffer))
11934 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
11935 (cfunc (if (and org-refile-use-outline-path
11936 org-outline-path-complete-in-steps)
11937 #'org-olpath-completing-read
11938 #'completing-read))
11939 (extra (if org-refile-use-outline-path "/" ""))
11940 (cbnex (concat (buffer-name) extra))
11941 (filename (and cfn (expand-file-name cfn)))
11942 (tbl (mapcar
11943 (lambda (x)
11944 (if (and (not (member org-refile-use-outline-path
11945 '(file full-file-path)))
11946 (not (equal filename (nth 1 x))))
11947 (cons (concat (car x) extra " ("
11948 (file-name-nondirectory (nth 1 x)) ")")
11949 (cdr x))
11950 (cons (concat (car x) extra) (cdr x))))
11951 org-refile-target-table))
11952 (completion-ignore-case t)
11953 cdef
11954 (prompt (concat prompt
11955 (or (and (car org-refile-history)
11956 (concat " (default " (car org-refile-history) ")"))
11957 (and (assoc cbnex tbl) (setq cdef cbnex)
11958 (concat " (default " cbnex ")"))) ": "))
11959 pa answ parent-target child parent old-hist)
11960 (setq old-hist org-refile-history)
11961 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
11962 nil 'org-refile-history (or cdef (car org-refile-history))))
11963 (if (setq pa (org-refile--get-location answ tbl))
11964 (progn
11965 (org-refile-check-position pa)
11966 (when (or (not org-refile-history)
11967 (not (eq old-hist org-refile-history))
11968 (not (equal (car pa) (car org-refile-history))))
11969 (setq org-refile-history
11970 (cons (car pa) (if (assoc (car org-refile-history) tbl)
11971 org-refile-history
11972 (cdr org-refile-history))))
11973 (when (equal (car org-refile-history) (nth 1 org-refile-history))
11974 (pop org-refile-history)))
11976 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
11977 (progn
11978 (setq parent (match-string 1 answ)
11979 child (match-string 2 answ))
11980 (setq parent-target (org-refile--get-location parent tbl))
11981 (when (and parent-target
11982 (or (eq new-nodes t)
11983 (and (eq new-nodes 'confirm)
11984 (y-or-n-p (format "Create new node \"%s\"? "
11985 child)))))
11986 (org-refile-new-child parent-target child)))
11987 (user-error "Invalid target location")))))
11989 (declare-function org-string-nw-p "org-macs" (s))
11990 (defun org-refile-check-position (refile-pointer)
11991 "Check if the refile pointer matches the headline to which it points."
11992 (let* ((file (nth 1 refile-pointer))
11993 (re (nth 2 refile-pointer))
11994 (pos (nth 3 refile-pointer))
11995 buffer)
11996 (if (and (not (markerp pos)) (not file))
11997 (user-error "Please indicate a target file in the refile path")
11998 (when (org-string-nw-p re)
11999 (setq buffer (if (markerp pos)
12000 (marker-buffer pos)
12001 (or (find-buffer-visiting file)
12002 (find-file-noselect file))))
12003 (with-current-buffer buffer
12004 (org-with-wide-buffer
12005 (goto-char pos)
12006 (beginning-of-line 1)
12007 (unless (looking-at-p re)
12008 (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
12010 (defun org-refile-new-child (parent-target child)
12011 "Use refile target PARENT-TARGET to add new CHILD below it."
12012 (unless parent-target
12013 (error "Cannot find parent for new node"))
12014 (let ((file (nth 1 parent-target))
12015 (pos (nth 3 parent-target))
12016 level)
12017 (with-current-buffer (or (find-buffer-visiting file)
12018 (find-file-noselect file))
12019 (org-with-wide-buffer
12020 (if pos
12021 (goto-char pos)
12022 (goto-char (point-max))
12023 (unless (bolp) (newline)))
12024 (when (looking-at org-outline-regexp)
12025 (setq level (funcall outline-level))
12026 (org-end-of-subtree t t))
12027 (org-back-over-empty-lines)
12028 (insert "\n" (make-string
12029 (if pos (org-get-valid-level level 1) 1) ?*)
12030 " " child "\n")
12031 (beginning-of-line 0)
12032 (list (concat (car parent-target) "/" child) file "" (point))))))
12034 (defun org-olpath-completing-read (prompt collection &rest args)
12035 "Read an outline path like a file name."
12036 (let ((thetable collection))
12037 (apply #'completing-read
12038 prompt
12039 (lambda (string predicate &optional flag)
12040 (cond
12041 ((eq flag nil) (try-completion string thetable))
12042 ((eq flag t)
12043 (let ((l (length string)))
12044 (mapcar (lambda (x)
12045 (let ((r (substring x l))
12046 (f (if (string-match " ([^)]*)$" x)
12047 (match-string 0 x)
12048 "")))
12049 (if (string-match "/" r)
12050 (concat string (substring r 0 (match-end 0)) f)
12051 x)))
12052 (all-completions string thetable predicate))))
12053 ;; Exact match?
12054 ((eq flag 'lambda) (assoc string thetable))))
12055 args)))
12057 ;;;; Dynamic blocks
12059 (defun org-find-dblock (name)
12060 "Find the first dynamic block with name NAME in the buffer.
12061 If not found, stay at current position and return nil."
12062 (let ((case-fold-search t) pos)
12063 (save-excursion
12064 (goto-char (point-min))
12065 (setq pos (and (re-search-forward
12066 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
12067 (match-beginning 0))))
12068 (when pos (goto-char pos))
12069 pos))
12071 (defun org-create-dblock (plist)
12072 "Create a dynamic block section, with parameters taken from PLIST.
12073 PLIST must contain a :name entry which is used as the name of the block."
12074 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
12075 (end-of-line 1)
12076 (newline))
12077 (let ((col (current-column))
12078 (name (plist-get plist :name)))
12079 (insert "#+BEGIN: " name)
12080 (while plist
12081 (if (eq (car plist) :name)
12082 (setq plist (cddr plist))
12083 (insert " " (prin1-to-string (pop plist)))))
12084 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
12085 (beginning-of-line -2)))
12087 (defun org-prepare-dblock ()
12088 "Prepare dynamic block for refresh.
12089 This empties the block, puts the cursor at the insert position and returns
12090 the property list including an extra property :name with the block name."
12091 (unless (looking-at org-dblock-start-re)
12092 (user-error "Not at a dynamic block"))
12093 (let* ((begdel (1+ (match-end 0)))
12094 (name (org-no-properties (match-string 1)))
12095 (params (append (list :name name)
12096 (read (concat "(" (match-string 3) ")")))))
12097 (save-excursion
12098 (beginning-of-line 1)
12099 (skip-chars-forward " \t")
12100 (setq params (plist-put params :indentation-column (current-column))))
12101 (unless (re-search-forward org-dblock-end-re nil t)
12102 (error "Dynamic block not terminated"))
12103 (setq params
12104 (append params
12105 (list :content (buffer-substring
12106 begdel (match-beginning 0)))))
12107 (delete-region begdel (match-beginning 0))
12108 (goto-char begdel)
12109 (open-line 1)
12110 params))
12112 (defun org-map-dblocks (&optional command)
12113 "Apply COMMAND to all dynamic blocks in the current buffer.
12114 If COMMAND is not given, use `org-update-dblock'."
12115 (let ((cmd (or command 'org-update-dblock)))
12116 (save-excursion
12117 (goto-char (point-min))
12118 (while (re-search-forward org-dblock-start-re nil t)
12119 (goto-char (match-beginning 0))
12120 (save-excursion
12121 (condition-case nil
12122 (funcall cmd)
12123 (error (message "Error during update of dynamic block"))))
12124 (unless (re-search-forward org-dblock-end-re nil t)
12125 (error "Dynamic block not terminated"))))))
12127 (defun org-dblock-update (&optional arg)
12128 "User command for updating dynamic blocks.
12129 Update the dynamic block at point. With prefix ARG, update all dynamic
12130 blocks in the buffer."
12131 (interactive "P")
12132 (if arg
12133 (org-update-all-dblocks)
12134 (or (looking-at org-dblock-start-re)
12135 (org-beginning-of-dblock))
12136 (org-update-dblock)))
12138 (defun org-update-dblock ()
12139 "Update the dynamic block at point.
12140 This means to empty the block, parse for parameters and then call
12141 the correct writing function."
12142 (interactive)
12143 (save-excursion
12144 (let* ((win (selected-window))
12145 (pos (point))
12146 (line (org-current-line))
12147 (params (org-prepare-dblock))
12148 (name (plist-get params :name))
12149 (indent (plist-get params :indentation-column))
12150 (cmd (intern (concat "org-dblock-write:" name))))
12151 (message "Updating dynamic block `%s' at line %d..." name line)
12152 (funcall cmd params)
12153 (message "Updating dynamic block `%s' at line %d...done" name line)
12154 (goto-char pos)
12155 (when (and indent (> indent 0))
12156 (setq indent (make-string indent ?\ ))
12157 (save-excursion
12158 (select-window win)
12159 (org-beginning-of-dblock)
12160 (forward-line 1)
12161 (while (not (looking-at org-dblock-end-re))
12162 (insert indent)
12163 (beginning-of-line 2))
12164 (when (looking-at org-dblock-end-re)
12165 (and (looking-at "[ \t]+")
12166 (replace-match ""))
12167 (insert indent)))))))
12169 (defun org-beginning-of-dblock ()
12170 "Find the beginning of the dynamic block at point.
12171 Error if there is no such block at point."
12172 (let ((pos (point))
12173 beg)
12174 (end-of-line 1)
12175 (if (and (re-search-backward org-dblock-start-re nil t)
12176 (setq beg (match-beginning 0))
12177 (re-search-forward org-dblock-end-re nil t)
12178 (> (match-end 0) pos))
12179 (goto-char beg)
12180 (goto-char pos)
12181 (error "Not in a dynamic block"))))
12183 (defun org-update-all-dblocks ()
12184 "Update all dynamic blocks in the buffer.
12185 This function can be used in a hook."
12186 (interactive)
12187 (when (derived-mode-p 'org-mode)
12188 (org-map-dblocks 'org-update-dblock)))
12191 ;;;; Completion
12193 (declare-function org-export-backend-options "ox" (cl-x) t)
12194 (defun org-get-export-keywords ()
12195 "Return a list of all currently understood export keywords.
12196 Export keywords include options, block names, attributes and
12197 keywords relative to each registered export back-end."
12198 (let (keywords)
12199 (dolist (backend
12200 (bound-and-true-p org-export-registered-backends)
12201 (delq nil keywords))
12202 ;; Back-end name (for keywords, like #+LATEX:)
12203 (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
12204 (dolist (option-entry (org-export-backend-options backend))
12205 ;; Back-end options.
12206 (push (nth 1 option-entry) keywords)))))
12208 (defconst org-options-keywords
12209 '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
12210 "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
12211 "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
12212 "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
12213 "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
12215 (defcustom org-structure-template-alist
12216 '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
12217 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
12218 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
12219 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
12220 ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
12221 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
12222 ("C" "#+BEGIN_COMMENT\n?\n#+END_COMMENT")
12223 ("l" "#+BEGIN_EXPORT latex\n?\n#+END_EXPORT")
12224 ("L" "#+LaTeX: ")
12225 ("h" "#+BEGIN_EXPORT html\n?\n#+END_EXPORT")
12226 ("H" "#+HTML: ")
12227 ("a" "#+BEGIN_EXPORT ascii\n?\n#+END_EXPORT")
12228 ("A" "#+ASCII: ")
12229 ("i" "#+INDEX: ?")
12230 ("I" "#+INCLUDE: %file ?"))
12231 "Structure completion elements.
12232 This is a list of abbreviation keys and values. The value gets inserted
12233 if you type `<' followed by the key and then press the completion key,
12234 usually `TAB'. %file will be replaced by a file name after prompting
12235 for the file using completion. The cursor will be placed at the position
12236 of the `?' in the template.
12237 There are two templates for each key, the first uses the original Org syntax,
12238 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
12239 the default when the /org-mtags.el/ module has been loaded. See also the
12240 variable `org-mtags-prefer-muse-templates'."
12241 :group 'org-edit-structure
12242 :type '(repeat
12243 (list
12244 (string :tag "Key")
12245 (string :tag "Template")))
12246 :version "26.1"
12247 :package-version '(Org . "8.3"))
12249 (defun org-try-structure-completion ()
12250 "Try to complete a structure template before point.
12251 This looks for strings like \"<e\" on an otherwise empty line and
12252 expands them."
12253 (let ((l (buffer-substring (point-at-bol) (point)))
12255 (when (and (looking-at "[ \t]*$")
12256 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
12257 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
12258 (org-complete-expand-structure-template (+ -1 (point-at-bol)
12259 (match-beginning 1)) a)
12260 t)))
12262 (defun org-complete-expand-structure-template (start cell)
12263 "Expand a structure template."
12264 (let ((rpl (nth 1 cell))
12265 (ind ""))
12266 (delete-region start (point))
12267 (when (string-match "\\`[ \t]*#\\+" rpl)
12268 (cond
12269 ((bolp))
12270 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
12271 (setq ind (buffer-substring (point-at-bol) (point))))
12272 (t (newline))))
12273 (setq start (point))
12274 (when (string-match "%file" rpl)
12275 (setq rpl (replace-match
12276 (concat
12277 "\""
12278 (save-match-data
12279 (abbreviate-file-name (read-file-name "Include file: ")))
12280 "\"")
12281 t t rpl)))
12282 (setq rpl (mapconcat 'identity (split-string rpl "\n")
12283 (concat "\n" ind)))
12284 (insert rpl)
12285 (when (re-search-backward "\\?" start t) (delete-char 1))))
12287 ;;;; TODO, DEADLINE, Comments
12289 (defun org-toggle-comment ()
12290 "Change the COMMENT state of an entry."
12291 (interactive)
12292 (save-excursion
12293 (org-back-to-heading)
12294 (let ((case-fold-search nil))
12295 (looking-at org-complex-heading-regexp))
12296 (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
12297 (skip-chars-forward " \t")
12298 (unless (memq (char-before) '(?\s ?\t)) (insert " "))
12299 (if (org-in-commented-heading-p t)
12300 (delete-region (point)
12301 (progn (search-forward " " (line-end-position) 'move)
12302 (skip-chars-forward " \t")
12303 (point)))
12304 (insert org-comment-string)
12305 (unless (eolp) (insert " ")))))
12307 (defvar org-last-todo-state-is-todo nil
12308 "This is non-nil when the last TODO state change led to a TODO state.
12309 If the last change removed the TODO tag or switched to DONE, then
12310 this is nil.")
12312 (defvar org-setting-tags nil) ; dynamically skipped
12314 (defvar org-todo-setup-filter-hook nil
12315 "Hook for functions that pre-filter todo specs.
12316 Each function takes a todo spec and returns either nil or the spec
12317 transformed into canonical form." )
12319 (defvar org-todo-get-default-hook nil
12320 "Hook for functions that get a default item for todo.
12321 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
12322 nil or a string to be used for the todo mark." )
12324 (defvar org-agenda-headline-snapshot-before-repeat)
12326 (defun org-current-effective-time ()
12327 "Return current time adjusted for `org-extend-today-until' variable."
12328 (let* ((ct (org-current-time))
12329 (dct (decode-time ct))
12330 (ct1
12331 (cond
12332 (org-use-last-clock-out-time-as-effective-time
12333 (or (org-clock-get-last-clock-out-time) ct))
12334 ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
12335 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
12336 (t ct))))
12337 ct1))
12339 (defun org-todo-yesterday (&optional arg)
12340 "Like `org-todo' but the time of change will be 23:59 of yesterday."
12341 (interactive "P")
12342 (if (eq major-mode 'org-agenda-mode)
12343 (apply 'org-agenda-todo-yesterday arg)
12344 (let* ((org-use-effective-time t)
12345 (hour (nth 2 (decode-time (org-current-time))))
12346 (org-extend-today-until (1+ hour)))
12347 (org-todo arg))))
12349 (defvar org-block-entry-blocking ""
12350 "First entry preventing the TODO state change.")
12352 (defun org-cancel-repeater ()
12353 "Cancel a repeater by setting its numeric value to zero."
12354 (interactive)
12355 (save-excursion
12356 (org-back-to-heading t)
12357 (let ((bound1 (point))
12358 (bound0 (save-excursion (outline-next-heading) (point))))
12359 (when (and (re-search-forward
12360 (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12361 org-deadline-time-regexp "\\)\\|\\("
12362 org-ts-regexp "\\)")
12363 bound0 t)
12364 (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
12365 bound1 t))
12366 (replace-match "0" t nil nil 1)))))
12368 (defvar org-state)
12369 (defvar org-blocked-by-checkboxes)
12370 (defun org-todo (&optional arg)
12371 "Change the TODO state of an item.
12373 The state of an item is given by a keyword at the start of the heading,
12374 like
12375 *** TODO Write paper
12376 *** DONE Call mom
12378 The different keywords are specified in the variable `org-todo-keywords'.
12379 By default the available states are \"TODO\" and \"DONE\". So, for this
12380 example: when the item starts with TODO, it is changed to DONE.
12381 When it starts with DONE, the DONE is removed. And when neither TODO nor
12382 DONE are present, add TODO at the beginning of the heading.
12384 With `\\[universal-argument]' prefix ARG, use completion to determine the new \
12385 state.
12386 With numeric prefix ARG, switch to that state.
12387 With a `\\[universal-argument] \\[universal-argument]' prefix, switch to the \
12388 next set of TODO \
12389 keywords (nextset).
12390 With a `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
12391 prefix, circumvent any state blocking.
12392 With a numeric prefix arg of 0, inhibit note taking for the change.
12393 With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
12395 When called through ELisp, arg is also interpreted in the following way:
12396 `none' -> empty state
12397 \"\" -> switch to empty state
12398 `done' -> switch to DONE
12399 `nextset' -> switch to the next set of keywords
12400 `previousset' -> switch to the previous set of keywords
12401 \"WAITING\" -> switch to the specified keyword, but only if it
12402 really is a member of `org-todo-keywords'."
12403 (interactive "P")
12404 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12405 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12406 'region-start-level 'region))
12407 org-loop-over-headlines-in-active-region)
12408 (org-map-entries
12409 `(org-todo ,arg)
12410 org-loop-over-headlines-in-active-region
12411 cl (when (org-invisible-p) (org-end-of-subtree nil t))))
12412 (when (equal arg '(16)) (setq arg 'nextset))
12413 (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
12414 (let ((org-blocker-hook org-blocker-hook)
12415 commentp
12416 case-fold-search)
12417 (when (equal arg '(64))
12418 (setq arg nil org-blocker-hook nil))
12419 (when (and org-blocker-hook
12420 (or org-inhibit-blocking
12421 (org-entry-get nil "NOBLOCKING")))
12422 (setq org-blocker-hook nil))
12423 (save-excursion
12424 (catch 'exit
12425 (org-back-to-heading t)
12426 (when (org-in-commented-heading-p t)
12427 (org-toggle-comment)
12428 (setq commentp t))
12429 (when (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
12430 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
12431 (looking-at "\\(?: *\\|[ \t]*$\\)"))
12432 (let* ((match-data (match-data))
12433 (startpos (copy-marker (line-beginning-position)))
12434 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
12435 (org-log-done org-log-done)
12436 (org-log-repeat org-log-repeat)
12437 (org-todo-log-states org-todo-log-states)
12438 (org-inhibit-logging
12439 (if (equal arg 0)
12440 (progn (setq arg nil) 'note) org-inhibit-logging))
12441 (this (match-string 1))
12442 (hl-pos (match-beginning 0))
12443 (head (org-get-todo-sequence-head this))
12444 (ass (assoc head org-todo-kwd-alist))
12445 (interpret (nth 1 ass))
12446 (done-word (nth 3 ass))
12447 (final-done-word (nth 4 ass))
12448 (org-last-state (or this ""))
12449 (completion-ignore-case t)
12450 (member (member this org-todo-keywords-1))
12451 (tail (cdr member))
12452 (org-state (cond
12453 ((and org-todo-key-trigger
12454 (or (and (equal arg '(4))
12455 (eq org-use-fast-todo-selection 'prefix))
12456 (and (not arg) org-use-fast-todo-selection
12457 (not (eq org-use-fast-todo-selection
12458 'prefix)))))
12459 ;; Use fast selection.
12460 (org-fast-todo-selection))
12461 ((and (equal arg '(4))
12462 (or (not org-use-fast-todo-selection)
12463 (not org-todo-key-trigger)))
12464 ;; Read a state with completion.
12465 (completing-read
12466 "State: " (mapcar #'list org-todo-keywords-1)
12467 nil t))
12468 ((eq arg 'right)
12469 (if this
12470 (if tail (car tail) nil)
12471 (car org-todo-keywords-1)))
12472 ((eq arg 'left)
12473 (unless (equal member org-todo-keywords-1)
12474 (if this
12475 (nth (- (length org-todo-keywords-1)
12476 (length tail) 2)
12477 org-todo-keywords-1)
12478 (org-last org-todo-keywords-1))))
12479 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
12480 (setq arg nil))) ;hack to fall back to cycling
12481 (arg
12482 ;; User or caller requests a specific state.
12483 (cond
12484 ((equal arg "") nil)
12485 ((eq arg 'none) nil)
12486 ((eq arg 'done) (or done-word (car org-done-keywords)))
12487 ((eq arg 'nextset)
12488 (or (car (cdr (member head org-todo-heads)))
12489 (car org-todo-heads)))
12490 ((eq arg 'previousset)
12491 (let ((org-todo-heads (reverse org-todo-heads)))
12492 (or (car (cdr (member head org-todo-heads)))
12493 (car org-todo-heads))))
12494 ((car (member arg org-todo-keywords-1)))
12495 ((stringp arg)
12496 (user-error "State `%s' not valid in this file" arg))
12497 ((nth (1- (prefix-numeric-value arg))
12498 org-todo-keywords-1))))
12499 ((null member) (or head (car org-todo-keywords-1)))
12500 ((equal this final-done-word) nil) ;-> make empty
12501 ((null tail) nil) ;-> first entry
12502 ((memq interpret '(type priority))
12503 (if (eq this-command last-command)
12504 (car tail)
12505 (if (> (length tail) 0)
12506 (or done-word (car org-done-keywords))
12507 nil)))
12509 (car tail))))
12510 (org-state (or
12511 (run-hook-with-args-until-success
12512 'org-todo-get-default-hook org-state org-last-state)
12513 org-state))
12514 (next (if org-state (concat " " org-state " ") " "))
12515 (change-plist (list :type 'todo-state-change :from this :to org-state
12516 :position startpos))
12517 dolog now-done-p)
12518 (when org-blocker-hook
12519 (let (org-blocked-by-checkboxes block-reason)
12520 (setq org-last-todo-state-is-todo
12521 (not (member this org-done-keywords)))
12522 (unless (save-excursion
12523 (save-match-data
12524 (org-with-wide-buffer
12525 (run-hook-with-args-until-failure
12526 'org-blocker-hook change-plist))))
12527 (setq block-reason (if org-blocked-by-checkboxes
12528 "contained checkboxes"
12529 (format "\"%s\"" org-block-entry-blocking)))
12530 (if (called-interactively-p 'interactive)
12531 (user-error "TODO state change from %s to %s blocked (by %s)"
12532 this org-state block-reason)
12533 ;; Fail silently.
12534 (message "TODO state change from %s to %s blocked (by %s)"
12535 this org-state block-reason)
12536 (throw 'exit nil)))))
12537 (store-match-data match-data)
12538 (replace-match next t t)
12539 (cond ((equal this org-state)
12540 (message "TODO state was already %s" (org-trim next)))
12541 ((not (pos-visible-in-window-p hl-pos))
12542 (message "TODO state changed to %s" (org-trim next))))
12543 (unless head
12544 (setq head (org-get-todo-sequence-head org-state)
12545 ass (assoc head org-todo-kwd-alist)
12546 interpret (nth 1 ass)
12547 done-word (nth 3 ass)
12548 final-done-word (nth 4 ass)))
12549 (when (memq arg '(nextset previousset))
12550 (message "Keyword-Set %d/%d: %s"
12551 (- (length org-todo-sets) -1
12552 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
12553 (length org-todo-sets)
12554 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
12555 (setq org-last-todo-state-is-todo
12556 (not (member org-state org-done-keywords)))
12557 (setq now-done-p (and (member org-state org-done-keywords)
12558 (not (member this org-done-keywords))))
12559 (and logging (org-local-logging logging))
12560 (when (and (or org-todo-log-states org-log-done)
12561 (not (eq org-inhibit-logging t))
12562 (not (memq arg '(nextset previousset))))
12563 ;; We need to look at recording a time and note.
12564 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
12565 (nth 2 (assoc this org-todo-log-states))))
12566 (when (and (eq dolog 'note) (eq org-inhibit-logging 'note))
12567 (setq dolog 'time))
12568 (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
12569 (and org-state
12570 (member org-state org-not-done-keywords)
12571 (not (member this org-not-done-keywords))))
12572 ;; This is now a todo state and was not one before
12573 ;; If there was a CLOSED time stamp, get rid of it.
12574 (org-add-planning-info nil nil 'closed))
12575 (when (and now-done-p org-log-done)
12576 ;; It is now done, and it was not done before.
12577 (org-add-planning-info 'closed (org-current-effective-time))
12578 (when (and (not dolog) (eq 'note org-log-done))
12579 (org-add-log-setup 'done org-state this 'note)))
12580 (when (and org-state dolog)
12581 ;; This is a non-nil state, and we need to log it.
12582 (org-add-log-setup 'state org-state this dolog)))
12583 ;; Fixup tag positioning.
12584 (org-todo-trigger-tag-changes org-state)
12585 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
12586 (when org-provide-todo-statistics
12587 (org-update-parent-todo-statistics))
12588 (run-hooks 'org-after-todo-state-change-hook)
12589 (when (and arg (not (member org-state org-done-keywords)))
12590 (setq head (org-get-todo-sequence-head org-state)))
12591 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
12592 ;; Do we need to trigger a repeat?
12593 (when now-done-p
12594 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
12595 ;; This is for the agenda, take a snapshot of the headline.
12596 (save-match-data
12597 (setq org-agenda-headline-snapshot-before-repeat
12598 (org-get-heading))))
12599 (org-auto-repeat-maybe org-state))
12600 ;; Fixup cursor location if close to the keyword.
12601 (when (and (outline-on-heading-p)
12602 (not (bolp))
12603 (save-excursion (beginning-of-line 1)
12604 (looking-at org-todo-line-regexp))
12605 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
12606 (goto-char (or (match-end 2) (match-end 1)))
12607 (and (looking-at " ") (just-one-space)))
12608 (when org-trigger-hook
12609 (save-excursion
12610 (run-hook-with-args 'org-trigger-hook change-plist)))
12611 (when commentp (org-toggle-comment))))))))
12613 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
12614 "Block turning an entry into a TODO, using the hierarchy.
12615 This checks whether the current task should be blocked from state
12616 changes. Such blocking occurs when:
12618 1. The task has children which are not all in a completed state.
12620 2. A task has a parent with the property :ORDERED:, and there
12621 are siblings prior to the current task with incomplete
12622 status.
12624 3. The parent of the task is blocked because it has siblings that should
12625 be done first, or is child of a block grandparent TODO entry."
12627 (if (not org-enforce-todo-dependencies)
12628 t ; if locally turned off don't block
12629 (catch 'dont-block
12630 ;; If this is not a todo state change, or if this entry is already DONE,
12631 ;; do not block
12632 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
12633 (member (plist-get change-plist :from)
12634 (cons 'done org-done-keywords))
12635 (member (plist-get change-plist :to)
12636 (cons 'todo org-not-done-keywords))
12637 (not (plist-get change-plist :to)))
12638 (throw 'dont-block t))
12639 ;; If this task has children, and any are undone, it's blocked
12640 (save-excursion
12641 (org-back-to-heading t)
12642 (let ((this-level (funcall outline-level)))
12643 (outline-next-heading)
12644 (let ((child-level (funcall outline-level)))
12645 (while (and (not (eobp))
12646 (> child-level this-level))
12647 ;; this todo has children, check whether they are all
12648 ;; completed
12649 (when (and (not (org-entry-is-done-p))
12650 (org-entry-is-todo-p))
12651 (setq org-block-entry-blocking (org-get-heading))
12652 (throw 'dont-block nil))
12653 (outline-next-heading)
12654 (setq child-level (funcall outline-level))))))
12655 ;; Otherwise, if the task's parent has the :ORDERED: property, and
12656 ;; any previous siblings are undone, it's blocked
12657 (save-excursion
12658 (org-back-to-heading t)
12659 (let* ((pos (point))
12660 (parent-pos (and (org-up-heading-safe) (point)))
12661 (case-fold-search nil))
12662 (unless parent-pos (throw 'dont-block t)) ; no parent
12663 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12664 (forward-line 1)
12665 (re-search-forward org-not-done-heading-regexp pos t))
12666 (setq org-block-entry-blocking (match-string 0))
12667 (throw 'dont-block nil)) ; block, there is an older sibling not done.
12668 ;; Search further up the hierarchy, to see if an ancestor is blocked
12669 (while t
12670 (goto-char parent-pos)
12671 (unless (looking-at org-not-done-heading-regexp)
12672 (throw 'dont-block t)) ; do not block, parent is not a TODO
12673 (setq pos (point))
12674 (setq parent-pos (and (org-up-heading-safe) (point)))
12675 (unless parent-pos (throw 'dont-block t)) ; no parent
12676 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
12677 (forward-line 1)
12678 (re-search-forward org-not-done-heading-regexp pos t)
12679 (setq org-block-entry-blocking (org-get-heading)))
12680 (throw 'dont-block nil)))))))) ; block, older sibling not done.
12682 (defcustom org-track-ordered-property-with-tag nil
12683 "Should the ORDERED property also be shown as a tag?
12684 The ORDERED property decides if an entry should require subtasks to be
12685 completed in sequence. Since a property is not very visible, setting
12686 this option means that toggling the ORDERED property with the command
12687 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
12688 not relevant for the behavior, but it makes things more visible.
12690 Note that toggling the tag with tags commands will not change the property
12691 and therefore not influence behavior!
12693 This can be t, meaning the tag ORDERED should be used, It can also be a
12694 string to select a different tag for this task."
12695 :group 'org-todo
12696 :type '(choice
12697 (const :tag "No tracking" nil)
12698 (const :tag "Track with ORDERED tag" t)
12699 (string :tag "Use other tag")))
12701 (defun org-toggle-ordered-property ()
12702 "Toggle the ORDERED property of the current entry.
12703 For better visibility, you can track the value of this property with a tag.
12704 See variable `org-track-ordered-property-with-tag'."
12705 (interactive)
12706 (let* ((t1 org-track-ordered-property-with-tag)
12707 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
12708 (save-excursion
12709 (org-back-to-heading)
12710 (if (org-entry-get nil "ORDERED")
12711 (progn
12712 (org-delete-property "ORDERED")
12713 (and tag (org-toggle-tag tag 'off))
12714 (message "Subtasks can be completed in arbitrary order"))
12715 (org-entry-put nil "ORDERED" "t")
12716 (and tag (org-toggle-tag tag 'on))
12717 (message "Subtasks must be completed in sequence")))))
12719 (defun org-block-todo-from-checkboxes (change-plist)
12720 "Block turning an entry into a TODO, using checkboxes.
12721 This checks whether the current task should be blocked from state
12722 changes because there are unchecked boxes in this entry."
12723 (if (not org-enforce-todo-checkbox-dependencies)
12724 t ; if locally turned off don't block
12725 (catch 'dont-block
12726 ;; If this is not a todo state change, or if this entry is already DONE,
12727 ;; do not block
12728 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
12729 (member (plist-get change-plist :from)
12730 (cons 'done org-done-keywords))
12731 (member (plist-get change-plist :to)
12732 (cons 'todo org-not-done-keywords))
12733 (not (plist-get change-plist :to)))
12734 (throw 'dont-block t))
12735 ;; If this task has checkboxes that are not checked, it's blocked
12736 (save-excursion
12737 (org-back-to-heading t)
12738 (let ((beg (point)) end)
12739 (outline-next-heading)
12740 (setq end (point))
12741 (goto-char beg)
12742 (when (org-list-search-forward
12743 (concat (org-item-beginning-re)
12744 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
12745 "\\[[- ]\\]")
12746 end t)
12747 (when (boundp 'org-blocked-by-checkboxes)
12748 (setq org-blocked-by-checkboxes t))
12749 (throw 'dont-block nil))))
12750 t))) ; do not block
12752 (defun org-entry-blocked-p ()
12753 "Non-nil if entry at point is blocked."
12754 (and (not (org-entry-get nil "NOBLOCKING"))
12755 (member (org-entry-get nil "TODO") org-not-done-keywords)
12756 (not (run-hook-with-args-until-failure
12757 'org-blocker-hook
12758 (list :type 'todo-state-change
12759 :position (point)
12760 :from 'todo
12761 :to 'done)))))
12763 (defun org-update-statistics-cookies (all)
12764 "Update the statistics cookie, either from TODO or from checkboxes.
12765 This should be called with the cursor in a line with a statistics
12766 cookie. When called with a \\[universal-argument] prefix, update
12767 all statistics cookies in the buffer."
12768 (interactive "P")
12769 (if all
12770 (progn
12771 (org-update-checkbox-count 'all)
12772 (org-map-entries 'org-update-parent-todo-statistics))
12773 (if (not (org-at-heading-p))
12774 (org-update-checkbox-count)
12775 (let ((pos (point-marker))
12776 end l1 l2)
12777 (ignore-errors (org-back-to-heading t))
12778 (if (not (org-at-heading-p))
12779 (org-update-checkbox-count)
12780 (setq l1 (org-outline-level))
12781 (setq end (save-excursion
12782 (outline-next-heading)
12783 (when (org-at-heading-p) (setq l2 (org-outline-level)))
12784 (point)))
12785 (if (and (save-excursion
12786 (re-search-forward
12787 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
12788 (not (save-excursion (re-search-forward
12789 ":COOKIE_DATA:.*\\<todo\\>" end t))))
12790 (org-update-checkbox-count)
12791 (if (and l2 (> l2 l1))
12792 (progn
12793 (goto-char end)
12794 (org-update-parent-todo-statistics))
12795 (goto-char pos)
12796 (beginning-of-line 1)
12797 (while (re-search-forward
12798 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
12799 (point-at-eol) t)
12800 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
12801 (goto-char pos)
12802 (move-marker pos nil)))))
12804 (defvar org-entry-property-inherited-from) ;; defined below
12805 (defun org-update-parent-todo-statistics ()
12806 "Update any statistics cookie in the parent of the current headline.
12807 When `org-hierarchical-todo-statistics' is nil, statistics will cover
12808 the entire subtree and this will travel up the hierarchy and update
12809 statistics everywhere."
12810 (let* ((prop (save-excursion (org-up-heading-safe)
12811 (org-entry-get nil "COOKIE_DATA" 'inherit)))
12812 (recursive (or (not org-hierarchical-todo-statistics)
12813 (and prop (string-match "\\<recursive\\>" prop))))
12814 (lim (or (and prop (marker-position org-entry-property-inherited-from))
12816 (first t)
12817 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
12818 level ltoggle l1 new ndel
12819 (cnt-all 0) (cnt-done 0) is-percent kwd
12820 checkbox-beg cookie-present)
12821 (catch 'exit
12822 (save-excursion
12823 (beginning-of-line 1)
12824 (setq ltoggle (funcall outline-level))
12825 ;; Three situations are to consider:
12827 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
12828 ;; to the top-level ancestor on the headline;
12830 ;; 2. If parent has "recursive" property, repeat up to the
12831 ;; headline setting that property, taking inheritance into
12832 ;; account;
12834 ;; 3. Else, move up to direct parent and proceed only once.
12835 (while (and (setq level (org-up-heading-safe))
12836 (or recursive first)
12837 (>= (point) lim))
12838 (setq first nil cookie-present nil)
12839 (unless (and level
12840 (not (string-match
12841 "\\<checkbox\\>"
12842 (downcase (or (org-entry-get nil "COOKIE_DATA")
12843 "")))))
12844 (throw 'exit nil))
12845 (while (re-search-forward box-re (point-at-eol) t)
12846 (setq cnt-all 0 cnt-done 0 cookie-present t)
12847 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
12848 (save-match-data
12849 (unless (outline-next-heading) (throw 'exit nil))
12850 (while (and (looking-at org-complex-heading-regexp)
12851 (> (setq l1 (length (match-string 1))) level))
12852 (setq kwd (and (or recursive (= l1 ltoggle))
12853 (match-string 2)))
12854 (if (or (eq org-provide-todo-statistics 'all-headlines)
12855 (and (eq org-provide-todo-statistics t)
12856 (or (member kwd org-done-keywords)))
12857 (and (listp org-provide-todo-statistics)
12858 (stringp (car org-provide-todo-statistics))
12859 (or (member kwd org-provide-todo-statistics)
12860 (member kwd org-done-keywords)))
12861 (and (listp org-provide-todo-statistics)
12862 (listp (car org-provide-todo-statistics))
12863 (or (member kwd (car org-provide-todo-statistics))
12864 (and (member kwd org-done-keywords)
12865 (member kwd (cadr org-provide-todo-statistics))))))
12866 (setq cnt-all (1+ cnt-all))
12867 (and (eq org-provide-todo-statistics t)
12869 (setq cnt-all (1+ cnt-all))))
12870 (when (or (and (member org-provide-todo-statistics '(t all-headlines))
12871 (member kwd org-done-keywords))
12872 (and (listp org-provide-todo-statistics)
12873 (listp (car org-provide-todo-statistics))
12874 (member kwd org-done-keywords)
12875 (member kwd (cadr org-provide-todo-statistics)))
12876 (and (listp org-provide-todo-statistics)
12877 (stringp (car org-provide-todo-statistics))
12878 (member kwd org-done-keywords)))
12879 (setq cnt-done (1+ cnt-done)))
12880 (outline-next-heading)))
12881 (setq new
12882 (if is-percent
12883 (format "[%d%%]" (floor (* 100.0 cnt-done)
12884 (max 1 cnt-all)))
12885 (format "[%d/%d]" cnt-done cnt-all))
12886 ndel (- (match-end 0) checkbox-beg))
12887 (goto-char checkbox-beg)
12888 (insert new)
12889 (delete-region (point) (+ (point) ndel))
12890 (when org-auto-align-tags (org-fix-tags-on-the-fly)))
12891 (when cookie-present
12892 (run-hook-with-args 'org-after-todo-statistics-hook
12893 cnt-done (- cnt-all cnt-done))))))
12894 (run-hooks 'org-todo-statistics-hook)))
12896 (defvar org-after-todo-statistics-hook nil
12897 "Hook that is called after a TODO statistics cookie has been updated.
12898 Each function is called with two arguments: the number of not-done entries
12899 and the number of done entries.
12901 For example, the following function, when added to this hook, will switch
12902 an entry to DONE when all children are done, and back to TODO when new
12903 entries are set to a TODO status. Note that this hook is only called
12904 when there is a statistics cookie in the headline!
12906 (defun org-summary-todo (n-done n-not-done)
12907 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
12908 (let (org-log-done org-log-states) ; turn off logging
12909 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
12912 (defvar org-todo-statistics-hook nil
12913 "Hook that is run whenever Org thinks TODO statistics should be updated.
12914 This hook runs even if there is no statistics cookie present, in which case
12915 `org-after-todo-statistics-hook' would not run.")
12917 (defun org-todo-trigger-tag-changes (state)
12918 "Apply the changes defined in `org-todo-state-tags-triggers'."
12919 (let ((l org-todo-state-tags-triggers)
12920 changes)
12921 (when (or (not state) (equal state ""))
12922 (setq changes (append changes (cdr (assoc "" l)))))
12923 (when (and (stringp state) (> (length state) 0))
12924 (setq changes (append changes (cdr (assoc state l)))))
12925 (when (member state org-not-done-keywords)
12926 (setq changes (append changes (cdr (assq 'todo l)))))
12927 (when (member state org-done-keywords)
12928 (setq changes (append changes (cdr (assq 'done l)))))
12929 (dolist (c changes)
12930 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
12932 (defun org-local-logging (value)
12933 "Get logging settings from a property VALUE."
12934 ;; Directly set the variables, they are already local.
12935 (setq org-log-done nil
12936 org-log-repeat nil
12937 org-todo-log-states nil)
12938 (dolist (w (split-string value))
12939 (let (a)
12940 (cond
12941 ((setq a (assoc w org-startup-options))
12942 (and (member (nth 1 a) '(org-log-done org-log-repeat))
12943 (set (nth 1 a) (nth 2 a))))
12944 ((setq a (org-extract-log-state-settings w))
12945 (and (member (car a) org-todo-keywords-1)
12946 (push a org-todo-log-states)))))))
12948 (defun org-get-todo-sequence-head (kwd)
12949 "Return the head of the TODO sequence to which KWD belongs.
12950 If KWD is not set, check if there is a text property remembering the
12951 right sequence."
12952 (let (p)
12953 (cond
12954 ((not kwd)
12955 (or (get-text-property (point-at-bol) 'org-todo-head)
12956 (progn
12957 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
12958 nil (point-at-eol)))
12959 (get-text-property p 'org-todo-head))))
12960 ((not (member kwd org-todo-keywords-1))
12961 (car org-todo-keywords-1))
12962 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
12964 (defun org-fast-todo-selection ()
12965 "Fast TODO keyword selection with single keys.
12966 Returns the new TODO keyword, or nil if no state change should occur."
12967 (let* ((fulltable org-todo-key-alist)
12968 (done-keywords org-done-keywords) ;; needed for the faces.
12969 (maxlen (apply 'max (mapcar
12970 (lambda (x)
12971 (if (stringp (car x)) (string-width (car x)) 0))
12972 fulltable)))
12973 (expert nil)
12974 (fwidth (+ maxlen 3 1 3))
12975 (ncol (/ (- (window-width) 4) fwidth))
12976 tg cnt e c tbl
12977 groups ingroup)
12978 (save-excursion
12979 (save-window-excursion
12980 (if expert
12981 (set-buffer (get-buffer-create " *Org todo*"))
12982 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
12983 (erase-buffer)
12984 (setq-local org-done-keywords done-keywords)
12985 (setq tbl fulltable cnt 0)
12986 (while (setq e (pop tbl))
12987 (cond
12988 ((equal e '(:startgroup))
12989 (push '() groups) (setq ingroup t)
12990 (unless (= cnt 0)
12991 (setq cnt 0)
12992 (insert "\n"))
12993 (insert "{ "))
12994 ((equal e '(:endgroup))
12995 (setq ingroup nil cnt 0)
12996 (insert "}\n"))
12997 ((equal e '(:newline))
12998 (unless (= cnt 0)
12999 (setq cnt 0)
13000 (insert "\n")
13001 (setq e (car tbl))
13002 (while (equal (car tbl) '(:newline))
13003 (insert "\n")
13004 (setq tbl (cdr tbl)))))
13006 (setq tg (car e) c (cdr e))
13007 (when ingroup (push tg (car groups)))
13008 (setq tg (org-add-props tg nil 'face
13009 (org-get-todo-face tg)))
13010 (when (and (= cnt 0) (not ingroup)) (insert " "))
13011 (insert "[" c "] " tg (make-string
13012 (- fwidth 4 (length tg)) ?\ ))
13013 (when (= (setq cnt (1+ cnt)) ncol)
13014 (insert "\n")
13015 (when ingroup (insert " "))
13016 (setq cnt 0)))))
13017 (insert "\n")
13018 (goto-char (point-min))
13019 (unless expert (org-fit-window-to-buffer))
13020 (message "[a-z..]:Set [SPC]:clear")
13021 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13022 (cond
13023 ((or (= c ?\C-g)
13024 (and (= c ?q) (not (rassoc c fulltable))))
13025 (setq quit-flag t))
13026 ((= c ?\ ) nil)
13027 ((setq e (rassoc c fulltable) tg (car e))
13029 (t (setq quit-flag t)))))))
13031 (defun org-entry-is-todo-p ()
13032 (member (org-get-todo-state) org-not-done-keywords))
13034 (defun org-entry-is-done-p ()
13035 (member (org-get-todo-state) org-done-keywords))
13037 (defun org-get-todo-state ()
13038 "Return the TODO keyword of the current subtree."
13039 (save-excursion
13040 (org-back-to-heading t)
13041 (and (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
13042 (match-end 2)
13043 (match-string 2))))
13045 (defun org-at-date-range-p (&optional inactive-ok)
13046 "Non-nil if point is inside a date range.
13048 When optional argument INACTIVE-OK is non-nil, also consider
13049 inactive time ranges.
13051 When this function returns a non-nil value, match data is set
13052 according to `org-tr-regexp-both' or `org-tr-regexp', depending
13053 on INACTIVE-OK."
13054 (interactive)
13055 (save-excursion
13056 (catch 'exit
13057 (let ((pos (point)))
13058 (skip-chars-backward "^[<\r\n")
13059 (skip-chars-backward "<[")
13060 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
13061 (>= (match-end 0) pos)
13062 (throw 'exit t))
13063 (skip-chars-backward "^<[\r\n")
13064 (skip-chars-backward "<[")
13065 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
13066 (>= (match-end 0) pos)
13067 (throw 'exit t)))
13068 nil)))
13070 (defun org-get-repeat (&optional timestamp)
13071 "Check if there is a time-stamp with repeater in this entry.
13073 Return the repeater, as a string, or nil. Also return nil when
13074 this function is called before first heading.
13076 When optional argument TIMESTAMP is a string, extract the
13077 repeater from there instead."
13078 (save-match-data
13079 (cond (timestamp
13080 (and (string-match org-repeat-re timestamp)
13081 (match-string-no-properties 1 timestamp)))
13082 ((org-before-first-heading-p) nil)
13084 (save-excursion
13085 (org-back-to-heading t)
13086 (let ((end (org-entry-end-position)))
13087 (catch :repeat
13088 (while (re-search-forward org-repeat-re end t)
13089 (when (save-match-data (org-at-timestamp-p 'agenda))
13090 (throw :repeat (match-string-no-properties 1)))))))))))
13092 (defvar org-last-changed-timestamp)
13093 (defvar org-last-inserted-timestamp)
13094 (defvar org-log-post-message)
13095 (defvar org-log-note-purpose)
13096 (defvar org-log-note-how nil)
13097 (defvar org-log-note-extra)
13098 (defun org-auto-repeat-maybe (done-word)
13099 "Check if the current headline contains a repeated time-stamp.
13101 If yes, set TODO state back to what it was and change the base date
13102 of repeating deadline/scheduled time stamps to new date.
13104 This function is run automatically after each state change to a DONE state."
13105 (let* ((repeat (org-get-repeat))
13106 (aa (assoc org-last-state org-todo-kwd-alist))
13107 (interpret (nth 1 aa))
13108 (head (nth 2 aa))
13109 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
13110 (msg "Entry repeats: ")
13111 (org-log-done nil)
13112 (org-todo-log-states nil)
13113 (end (copy-marker (org-entry-end-position))))
13114 (unwind-protect
13115 (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
13116 (when (eq org-log-repeat t) (setq org-log-repeat 'state))
13117 (let ((to-state (or (org-entry-get nil "REPEAT_TO_STATE" 'selective)
13118 org-todo-repeat-to-state)))
13119 (org-todo (cond
13120 ((and to-state (member to-state org-todo-keywords-1))
13121 to-state)
13122 ((eq interpret 'type) org-last-state)
13123 (head)
13124 (t 'none))))
13125 (org-back-to-heading t)
13126 (org-add-planning-info nil nil 'closed)
13127 ;; When `org-log-repeat' is non-nil or entry contains
13128 ;; a clock, set LAST_REPEAT property.
13129 (when (or org-log-repeat
13130 (catch :clock
13131 (save-excursion
13132 (while (re-search-forward org-clock-line-re end t)
13133 (when (org-at-clock-log-p) (throw :clock t))))))
13134 (org-entry-put nil "LAST_REPEAT" (format-time-string
13135 (org-time-stamp-format t t)
13136 (current-time))))
13137 (when org-log-repeat
13138 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
13139 (memq 'org-add-log-note post-command-hook))
13140 ;; We are already setup for some record.
13141 (when (eq org-log-repeat 'note)
13142 ;; Make sure we take a note, not only a time stamp.
13143 (setq org-log-note-how 'note))
13144 ;; Set up for taking a record.
13145 (org-add-log-setup 'state
13146 (or done-word (car org-done-keywords))
13147 org-last-state
13148 org-log-repeat)))
13149 (let ((planning-re (regexp-opt
13150 (list org-scheduled-string org-deadline-string))))
13151 (while (re-search-forward org-ts-regexp end t)
13152 (let* ((ts (match-string 0))
13153 (planning? (org-at-planning-p))
13154 (type (if (not planning?) "Plain:"
13155 (save-excursion
13156 (re-search-backward
13157 planning-re (line-beginning-position) t)
13158 (match-string 0)))))
13159 (cond
13160 ;; Ignore fake time-stamps (e.g., within comments).
13161 ((not (org-at-timestamp-p 'agenda)))
13162 ;; Time-stamps without a repeater are usually
13163 ;; skipped. However, a SCHEDULED time-stamp without
13164 ;; one is removed, as they are no longer relevant.
13165 ((not (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
13166 ts))
13167 (when (equal type org-scheduled-string)
13168 (org-remove-timestamp-with-keyword type)))
13170 (let ((n (string-to-number (match-string 2 ts)))
13171 (what (match-string 3 ts)))
13172 (when (equal what "w") (setq n (* n 7) what "d"))
13173 (when (and (equal what "h")
13174 (not (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}"
13175 ts)))
13176 (user-error
13177 "Cannot repeat in Repeat in %d hour(s) because no hour \
13178 has been set"
13180 ;; Preparation, see if we need to modify the start
13181 ;; date for the change.
13182 (when (match-end 1)
13183 (let ((time (save-match-data
13184 (org-time-string-to-time ts))))
13185 (cond
13186 ((equal (match-string 1 ts) ".")
13187 ;; Shift starting date to today
13188 (org-timestamp-change
13189 (- (org-today) (time-to-days time))
13190 'day))
13191 ((equal (match-string 1 ts) "+")
13192 (let ((nshiftmax 10)
13193 (nshift 0))
13194 (while (or (= nshift 0)
13195 (not (time-less-p (current-time) time)))
13196 (when (= (cl-incf nshift) nshiftmax)
13197 (or (y-or-n-p
13198 (format "%d repeater intervals were not \
13199 enough to shift date past today. Continue? "
13200 nshift))
13201 (user-error "Abort")))
13202 (org-timestamp-change n (cdr (assoc what whata)))
13203 (org-in-regexp org-ts-regexp3)
13204 (setq ts (match-string 1))
13205 (setq time
13206 (save-match-data
13207 (org-time-string-to-time ts)))))
13208 (org-timestamp-change (- n) (cdr (assoc what whata)))
13209 ;; Rematch, so that we have everything in place
13210 ;; for the real shift.
13211 (org-in-regexp org-ts-regexp3)
13212 (setq ts (match-string 1))
13213 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
13214 ts)))))
13215 (save-excursion
13216 (org-timestamp-change n (cdr (assoc what whata)) nil t))
13217 (setq msg
13218 (concat
13219 msg type " " org-last-changed-timestamp " "))))))))
13220 (setq org-log-post-message msg)
13221 (message "%s" msg))
13222 (set-marker end nil))))
13224 (defun org-show-todo-tree (arg)
13225 "Make a compact tree which shows all headlines marked with TODO.
13226 The tree will show the lines where the regexp matches, and all higher
13227 headlines above the match.
13228 With a `\\[universal-argument]' prefix, prompt for a regexp to match.
13229 With a numeric prefix N, construct a sparse tree for the Nth element
13230 of `org-todo-keywords-1'."
13231 (interactive "P")
13232 (let ((case-fold-search nil)
13233 (kwd-re
13234 (cond ((null arg) org-not-done-regexp)
13235 ((equal arg '(4))
13236 (let ((kwd
13237 (completing-read "Keyword (or KWD1|KWD2|...): "
13238 (mapcar #'list org-todo-keywords-1))))
13239 (concat "\\("
13240 (mapconcat 'identity (org-split-string kwd "|") "\\|")
13241 "\\)\\>")))
13242 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
13243 (regexp-quote (nth (1- (prefix-numeric-value arg))
13244 org-todo-keywords-1)))
13245 (t (user-error "Invalid prefix argument: %s" arg)))))
13246 (message "%d TODO entries found"
13247 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
13249 (defun org--deadline-or-schedule (arg type time)
13250 "Insert DEADLINE or SCHEDULE information in current entry.
13251 TYPE is either `deadline' or `scheduled'. See `org-deadline' or
13252 `org-schedule' for information about ARG and TIME arguments."
13253 (let* ((deadline? (eq type 'deadline))
13254 (keyword (if deadline? org-deadline-string org-scheduled-string))
13255 (log (if deadline? org-log-redeadline org-log-reschedule))
13256 (old-date (org-entry-get nil (if deadline? "DEADLINE" "SCHEDULED")))
13257 (old-date-time (and old-date (org-time-string-to-time old-date)))
13258 ;; Save repeater cookie from either TIME or current scheduled
13259 ;; time stamp. We are going to insert it back at the end of
13260 ;; the process.
13261 (repeater (or (and (org-string-nw-p time)
13262 ;; We use `org-repeat-re' because we need
13263 ;; to tell the difference between a real
13264 ;; repeater and a time delta, e.g. "+2d".
13265 (string-match org-repeat-re time)
13266 (match-string 1 time))
13267 (and (org-string-nw-p old-date)
13268 (string-match "\\([.+-]+[0-9]+[hdwmy]\
13269 \\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\)"
13270 old-date)
13271 (match-string 1 old-date)))))
13272 (pcase arg
13273 (`(4)
13274 (when (and old-date log)
13275 (org-add-log-setup (if deadline? 'deldeadline 'delschedule)
13276 nil old-date log))
13277 (org-remove-timestamp-with-keyword keyword)
13278 (message (if deadline? "Item no longer has a deadline."
13279 "Item is no longer scheduled.")))
13280 (`(16)
13281 (save-excursion
13282 (org-back-to-heading t)
13283 (let ((regexp (if deadline? org-deadline-time-regexp
13284 org-scheduled-time-regexp)))
13285 (if (not (re-search-forward regexp (line-end-position 2) t))
13286 (user-error (if deadline? "No deadline information to update"
13287 "No scheduled information to update"))
13288 (let* ((rpl0 (match-string 1))
13289 (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0))
13290 (msg (if deadline? "Warn starting from" "Delay until")))
13291 (replace-match
13292 (concat keyword
13293 " <" rpl
13294 (format " -%dd"
13295 (abs (- (time-to-days
13296 (save-match-data
13297 (org-read-date
13298 nil t nil msg old-date-time)))
13299 (time-to-days old-date-time))))
13300 ">") t t))))))
13302 (org-add-planning-info type time 'closed)
13303 (when (and old-date
13305 (not (equal old-date org-last-inserted-timestamp)))
13306 (org-add-log-setup (if deadline? 'redeadline 'reschedule)
13307 org-last-inserted-timestamp
13308 old-date
13309 log))
13310 (when repeater
13311 (save-excursion
13312 (org-back-to-heading t)
13313 (when (re-search-forward
13314 (concat keyword " " org-last-inserted-timestamp)
13315 (line-end-position 2)
13317 (goto-char (1- (match-end 0)))
13318 (insert " " repeater)
13319 (setq org-last-inserted-timestamp
13320 (concat (substring org-last-inserted-timestamp 0 -1)
13321 " " repeater
13322 (substring org-last-inserted-timestamp -1))))))
13323 (message (if deadline? "Deadline on %s" "Scheduled to %s")
13324 org-last-inserted-timestamp)))))
13326 (defun org-deadline (arg &optional time)
13327 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
13328 With one universal prefix argument, remove any deadline from the item.
13329 With two universal prefix arguments, prompt for a warning delay.
13330 With argument TIME, set the deadline at the corresponding date. TIME
13331 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
13332 (interactive "P")
13333 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13334 (org-map-entries
13335 (lambda () (org--deadline-or-schedule arg 'deadline time))
13337 (if (eq org-loop-over-headlines-in-active-region 'start-level)
13338 'region-start-level
13339 'region)
13340 (lambda () (when (org-invisible-p) (org-end-of-subtree nil t))))
13341 (org--deadline-or-schedule arg 'deadline time)))
13343 (defun org-schedule (arg &optional time)
13344 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
13345 With one universal prefix argument, remove any scheduling date from the item.
13346 With two universal prefix arguments, prompt for a delay cookie.
13347 With argument TIME, scheduled at the corresponding date. TIME can
13348 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
13349 (interactive "P")
13350 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13351 (org-map-entries
13352 (lambda () (org--deadline-or-schedule arg 'scheduled time))
13354 (if (eq org-loop-over-headlines-in-active-region 'start-level)
13355 'region-start-level
13356 'region)
13357 (lambda () (when (org-invisible-p) (org-end-of-subtree nil t))))
13358 (org--deadline-or-schedule arg 'scheduled time)))
13360 (defun org-get-scheduled-time (pom &optional inherit)
13361 "Get the scheduled time as a time tuple, of a format suitable
13362 for calling org-schedule with, or if there is no scheduling,
13363 returns nil."
13364 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
13365 (when time
13366 (apply 'encode-time (org-parse-time-string time)))))
13368 (defun org-get-deadline-time (pom &optional inherit)
13369 "Get the deadline as a time tuple, of a format suitable for
13370 calling org-deadline with, or if there is no scheduling, returns
13371 nil."
13372 (let ((time (org-entry-get pom "DEADLINE" inherit)))
13373 (when time
13374 (apply 'encode-time (org-parse-time-string time)))))
13376 (defun org-remove-timestamp-with-keyword (keyword)
13377 "Remove all time stamps with KEYWORD in the current entry."
13378 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
13379 beg)
13380 (save-excursion
13381 (org-back-to-heading t)
13382 (setq beg (point))
13383 (outline-next-heading)
13384 (while (re-search-backward re beg t)
13385 (replace-match "")
13386 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
13387 (equal (char-before) ?\ ))
13388 (backward-delete-char 1)
13389 (when (string-match "^[ \t]*$" (buffer-substring
13390 (point-at-bol) (point-at-eol)))
13391 (delete-region (point-at-bol)
13392 (min (point-max) (1+ (point-at-eol))))))))))
13394 (defvar org-time-was-given) ; dynamically scoped parameter
13395 (defvar org-end-time-was-given) ; dynamically scoped parameter
13397 (defun org-at-planning-p ()
13398 "Non-nil when point is on a planning info line."
13399 ;; This is as accurate and faster than `org-element-at-point' since
13400 ;; planning info location is fixed in the section.
13401 (org-with-wide-buffer
13402 (beginning-of-line)
13403 (and (looking-at-p org-planning-line-re)
13404 (eq (point)
13405 (ignore-errors
13406 (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
13407 (org-back-to-heading t)
13408 (org-with-limited-levels (org-back-to-heading t)))
13409 (line-beginning-position 2))))))
13411 (defun org-add-planning-info (what &optional time &rest remove)
13412 "Insert new timestamp with keyword in the planning line.
13413 WHAT indicates what kind of time stamp to add. It is a symbol
13414 among `closed', `deadline', `scheduled' and nil. TIME indicates
13415 the time to use. If none is given, the user is prompted for
13416 a date. REMOVE indicates what kind of entries to remove. An old
13417 WHAT entry will also be removed."
13418 (let (org-time-was-given org-end-time-was-given default-time default-input)
13419 (catch 'exit
13420 (when (and (memq what '(scheduled deadline))
13421 (or (not time)
13422 (and (stringp time)
13423 (string-match "^[-+]+[0-9]" time))))
13424 ;; Try to get a default date/time from existing timestamp
13425 (save-excursion
13426 (org-back-to-heading t)
13427 (let ((end (save-excursion (outline-next-heading) (point))) ts)
13428 (when (re-search-forward (if (eq what 'scheduled)
13429 org-scheduled-time-regexp
13430 org-deadline-time-regexp)
13431 end t)
13432 (setq ts (match-string 1)
13433 default-time (apply 'encode-time (org-parse-time-string ts))
13434 default-input (and ts (org-get-compact-tod ts)))))))
13435 (when what
13436 (setq time
13437 (if (stringp time)
13438 ;; This is a string (relative or absolute), set
13439 ;; proper date.
13440 (apply #'encode-time
13441 (org-read-date-analyze
13442 time default-time (decode-time default-time)))
13443 ;; If necessary, get the time from the user
13444 (or time (org-read-date nil 'to-time nil nil
13445 default-time default-input)))))
13447 (org-with-wide-buffer
13448 (org-back-to-heading t)
13449 (forward-line)
13450 (unless (bolp) (insert "\n"))
13451 (cond ((looking-at-p org-planning-line-re)
13452 ;; Move to current indentation.
13453 (skip-chars-forward " \t")
13454 ;; Check if we have to remove something.
13455 (dolist (type (if what (cons what remove) remove))
13456 (save-excursion
13457 (when (re-search-forward
13458 (cl-case type
13459 (closed org-closed-time-regexp)
13460 (deadline org-deadline-time-regexp)
13461 (scheduled org-scheduled-time-regexp)
13462 (otherwise
13463 (error "Invalid planning type: %s" type)))
13464 (line-end-position) t)
13465 ;; Delete until next keyword or end of line.
13466 (delete-region
13467 (match-beginning 0)
13468 (if (re-search-forward org-keyword-time-not-clock-regexp
13469 (line-end-position)
13471 (match-beginning 0)
13472 (line-end-position))))))
13473 ;; If there is nothing more to add and no more keyword
13474 ;; is left, remove the line completely.
13475 (if (and (looking-at-p "[ \t]*$") (not what))
13476 (delete-region (line-beginning-position)
13477 (line-beginning-position 2))
13478 ;; If we removed last keyword, do not leave trailing
13479 ;; white space at the end of line.
13480 (let ((p (point)))
13481 (save-excursion
13482 (end-of-line)
13483 (unless (= (skip-chars-backward " \t" p) 0)
13484 (delete-region (point) (line-end-position)))))))
13485 ((not what) (throw 'exit nil)) ; Nothing to do.
13486 (t (insert-before-markers "\n")
13487 (backward-char 1)
13488 (when org-adapt-indentation
13489 (indent-to-column (1+ (org-outline-level))))))
13490 (when what
13491 ;; Insert planning keyword.
13492 (insert (cl-case what
13493 (closed org-closed-string)
13494 (deadline org-deadline-string)
13495 (scheduled org-scheduled-string)
13496 (otherwise (error "Invalid planning type: %s" what)))
13497 " ")
13498 ;; Insert associated timestamp.
13499 (let ((ts (org-insert-time-stamp
13500 time
13501 (or org-time-was-given
13502 (and (eq what 'closed) org-log-done-with-time))
13503 (eq what 'closed)
13504 nil nil (list org-end-time-was-given))))
13505 (unless (eolp) (insert " "))
13506 ts))))))
13508 (defvar org-log-note-marker (make-marker)
13509 "Marker pointing at the entry where the note is to be inserted.")
13510 (defvar org-log-note-purpose nil)
13511 (defvar org-log-note-state nil)
13512 (defvar org-log-note-previous-state nil)
13513 (defvar org-log-note-extra nil)
13514 (defvar org-log-note-window-configuration nil)
13515 (defvar org-log-note-return-to (make-marker))
13516 (defvar org-log-note-effective-time nil
13517 "Remembered current time so that dynamically scoped
13518 `org-extend-today-until' affects timestamps in state change log")
13520 (defvar org-log-post-message nil
13521 "Message to be displayed after a log note has been stored.
13522 The auto-repeater uses this.")
13524 (defun org-add-note ()
13525 "Add a note to the current entry.
13526 This is done in the same way as adding a state change note."
13527 (interactive)
13528 (org-add-log-setup 'note))
13530 (defun org-log-beginning (&optional create)
13531 "Return expected start of log notes in current entry.
13532 When optional argument CREATE is non-nil, the function creates
13533 a drawer to store notes, if necessary. Returned position ignores
13534 narrowing."
13535 (org-with-wide-buffer
13536 (let ((drawer (org-log-into-drawer)))
13537 (cond
13538 (drawer
13539 (org-end-of-meta-data)
13540 (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
13541 (end (if (org-at-heading-p) (point)
13542 (save-excursion (outline-next-heading) (point))))
13543 (case-fold-search t))
13544 (catch 'exit
13545 ;; Try to find existing drawer.
13546 (while (re-search-forward regexp end t)
13547 (let ((element (org-element-at-point)))
13548 (when (eq (org-element-type element) 'drawer)
13549 (let ((cend (org-element-property :contents-end element)))
13550 (when (and (not org-log-states-order-reversed) cend)
13551 (goto-char cend)))
13552 (throw 'exit nil))))
13553 ;; No drawer found. Create one, if permitted.
13554 (when create
13555 (unless (bolp) (insert "\n"))
13556 (let ((beg (point)))
13557 (insert ":" drawer ":\n:END:\n")
13558 (org-indent-region beg (point)))
13559 (end-of-line -1)))))
13561 (org-end-of-meta-data org-log-state-notes-insert-after-drawers)
13562 (skip-chars-forward " \t\n")
13563 (beginning-of-line)
13564 (unless org-log-states-order-reversed
13565 (org-skip-over-state-notes)
13566 (skip-chars-backward " \t\n")
13567 (forward-line)))))
13568 (if (bolp) (point) (line-beginning-position 2))))
13570 (defun org-add-log-setup (&optional purpose state prev-state how extra)
13571 "Set up the post command hook to take a note.
13572 If this is about to TODO state change, the new state is expected in STATE.
13573 HOW is an indicator what kind of note should be created.
13574 EXTRA is additional text that will be inserted into the notes buffer."
13575 (move-marker org-log-note-marker (point))
13576 (setq org-log-note-purpose purpose
13577 org-log-note-state state
13578 org-log-note-previous-state prev-state
13579 org-log-note-how how
13580 org-log-note-extra extra
13581 org-log-note-effective-time (org-current-effective-time))
13582 (add-hook 'post-command-hook 'org-add-log-note 'append))
13584 (defun org-skip-over-state-notes ()
13585 "Skip past the list of State notes in an entry."
13586 (when (ignore-errors (goto-char (org-in-item-p)))
13587 (let* ((struct (org-list-struct))
13588 (prevs (org-list-prevs-alist struct))
13589 (regexp
13590 (concat "[ \t]*- +"
13591 (replace-regexp-in-string
13592 " +" " +"
13593 (org-replace-escapes
13594 (regexp-quote (cdr (assq 'state org-log-note-headings)))
13595 `(("%d" . ,org-ts-regexp-inactive)
13596 ("%D" . ,org-ts-regexp)
13597 ("%s" . "\"\\S-+\"")
13598 ("%S" . "\"\\S-+\"")
13599 ("%t" . ,org-ts-regexp-inactive)
13600 ("%T" . ,org-ts-regexp)
13601 ("%u" . ".*?")
13602 ("%U" . ".*?")))))))
13603 (while (looking-at-p regexp)
13604 (goto-char (or (org-list-get-next-item (point) struct prevs)
13605 (org-list-get-item-end (point) struct)))))))
13607 (defun org-add-log-note (&optional _purpose)
13608 "Pop up a window for taking a note, and add this note later."
13609 (remove-hook 'post-command-hook 'org-add-log-note)
13610 (setq org-log-note-window-configuration (current-window-configuration))
13611 (delete-other-windows)
13612 (move-marker org-log-note-return-to (point))
13613 (pop-to-buffer-same-window (marker-buffer org-log-note-marker))
13614 (goto-char org-log-note-marker)
13615 (org-switch-to-buffer-other-window "*Org Note*")
13616 (erase-buffer)
13617 (if (memq org-log-note-how '(time state))
13618 (org-store-log-note)
13619 (let ((org-inhibit-startup t)) (org-mode))
13620 (insert (format "# Insert note for %s.
13621 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
13622 (cond
13623 ((eq org-log-note-purpose 'clock-out) "stopped clock")
13624 ((eq org-log-note-purpose 'done) "closed todo item")
13625 ((eq org-log-note-purpose 'state)
13626 (format "state change from \"%s\" to \"%s\""
13627 (or org-log-note-previous-state "")
13628 (or org-log-note-state "")))
13629 ((eq org-log-note-purpose 'reschedule)
13630 "rescheduling")
13631 ((eq org-log-note-purpose 'delschedule)
13632 "no longer scheduled")
13633 ((eq org-log-note-purpose 'redeadline)
13634 "changing deadline")
13635 ((eq org-log-note-purpose 'deldeadline)
13636 "removing deadline")
13637 ((eq org-log-note-purpose 'refile)
13638 "refiling")
13639 ((eq org-log-note-purpose 'note)
13640 "this entry")
13641 (t (error "This should not happen")))))
13642 (when org-log-note-extra (insert org-log-note-extra))
13643 (setq-local org-finish-function 'org-store-log-note)
13644 (run-hooks 'org-log-buffer-setup-hook)))
13646 (defvar org-note-abort nil) ; dynamically scoped
13647 (defun org-store-log-note ()
13648 "Finish taking a log note, and insert it to where it belongs."
13649 (let ((txt (prog1 (buffer-string)
13650 (kill-buffer)))
13651 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
13652 lines)
13653 (while (string-match "\\`# .*\n[ \t\n]*" txt)
13654 (setq txt (replace-match "" t t txt)))
13655 (when (string-match "\\s-+\\'" txt)
13656 (setq txt (replace-match "" t t txt)))
13657 (setq lines (and (not (equal "" txt)) (org-split-string txt "\n")))
13658 (when (org-string-nw-p note)
13659 (setq note
13660 (org-replace-escapes
13661 note
13662 (list (cons "%u" (user-login-name))
13663 (cons "%U" user-full-name)
13664 (cons "%t" (format-time-string
13665 (org-time-stamp-format 'long 'inactive)
13666 org-log-note-effective-time))
13667 (cons "%T" (format-time-string
13668 (org-time-stamp-format 'long nil)
13669 org-log-note-effective-time))
13670 (cons "%d" (format-time-string
13671 (org-time-stamp-format nil 'inactive)
13672 org-log-note-effective-time))
13673 (cons "%D" (format-time-string
13674 (org-time-stamp-format nil nil)
13675 org-log-note-effective-time))
13676 (cons "%s" (cond
13677 ((not org-log-note-state) "")
13678 ((string-match-p org-ts-regexp
13679 org-log-note-state)
13680 (format "\"[%s]\""
13681 (substring org-log-note-state 1 -1)))
13682 (t (format "\"%s\"" org-log-note-state))))
13683 (cons "%S"
13684 (cond
13685 ((not org-log-note-previous-state) "")
13686 ((string-match-p org-ts-regexp
13687 org-log-note-previous-state)
13688 (format "\"[%s]\""
13689 (substring
13690 org-log-note-previous-state 1 -1)))
13691 (t (format "\"%s\""
13692 org-log-note-previous-state)))))))
13693 (when lines (setq note (concat note " \\\\")))
13694 (push note lines))
13695 (when (and lines (not org-note-abort))
13696 (with-current-buffer (marker-buffer org-log-note-marker)
13697 (org-with-wide-buffer
13698 ;; Find location for the new note.
13699 (goto-char org-log-note-marker)
13700 (set-marker org-log-note-marker nil)
13701 ;; Note associated to a clock is to be located right after
13702 ;; the clock. Do not move point.
13703 (unless (eq org-log-note-purpose 'clock-out)
13704 (goto-char (org-log-beginning t)))
13705 ;; Make sure point is at the beginning of an empty line.
13706 (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
13707 ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
13708 ;; In an existing list, add a new item at the top level.
13709 ;; Otherwise, indent line like a regular one.
13710 (let ((itemp (org-in-item-p)))
13711 (if itemp
13712 (indent-line-to
13713 (let ((struct (save-excursion
13714 (goto-char itemp) (org-list-struct))))
13715 (org-list-get-ind (org-list-get-top-point struct) struct)))
13716 (org-indent-line)))
13717 (insert (org-list-bullet-string "-") (pop lines))
13718 (let ((ind (org-list-item-body-column (line-beginning-position))))
13719 (dolist (line lines)
13720 (insert "\n")
13721 (indent-line-to ind)
13722 (insert line)))
13723 (message "Note stored")
13724 (org-back-to-heading t)
13725 (org-cycle-hide-drawers 'children))
13726 ;; Fix `buffer-undo-list' when `org-store-log-note' is called
13727 ;; from within `org-add-log-note' because `buffer-undo-list'
13728 ;; is then modified outside of `org-with-remote-undo'.
13729 (when (eq this-command 'org-agenda-todo)
13730 (setcdr buffer-undo-list (cddr buffer-undo-list))))))
13731 ;; Don't add undo information when called from `org-agenda-todo'.
13732 (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
13733 (set-window-configuration org-log-note-window-configuration)
13734 (with-current-buffer (marker-buffer org-log-note-return-to)
13735 (goto-char org-log-note-return-to))
13736 (move-marker org-log-note-return-to nil)
13737 (when org-log-post-message (message "%s" org-log-post-message))))
13739 (defun org-remove-empty-drawer-at (pos)
13740 "Remove an empty drawer at position POS.
13741 POS may also be a marker."
13742 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
13743 (org-with-wide-buffer
13744 (goto-char pos)
13745 (let ((drawer (org-element-at-point)))
13746 (when (and (memq (org-element-type drawer) '(drawer property-drawer))
13747 (not (org-element-property :contents-begin drawer)))
13748 (delete-region (org-element-property :begin drawer)
13749 (progn (goto-char (org-element-property :end drawer))
13750 (skip-chars-backward " \r\t\n")
13751 (forward-line)
13752 (point))))))))
13754 (defvar org-ts-type nil)
13755 (defun org-sparse-tree (&optional arg type)
13756 "Create a sparse tree, prompt for the details.
13757 This command can create sparse trees. You first need to select the type
13758 of match used to create the tree:
13760 t Show all TODO entries.
13761 T Show entries with a specific TODO keyword.
13762 m Show entries selected by a tags/property match.
13763 p Enter a property name and its value (both with completion on existing
13764 names/values) and show entries with that property.
13765 r Show entries matching a regular expression (`/' can be used as well).
13766 b Show deadlines and scheduled items before a date.
13767 a Show deadlines and scheduled items after a date.
13768 d Show deadlines due within `org-deadline-warning-days'.
13769 D Show deadlines and scheduled items between a date range."
13770 (interactive "P")
13771 (setq type (or type org-sparse-tree-default-date-type))
13772 (setq org-ts-type type)
13773 (message "Sparse tree: [r]egexp [t]odo [T]odo-kwd [m]atch [p]roperty
13774 \[d]eadlines [b]efore-date [a]fter-date [D]ates range
13775 \[c]ycle through date types: %s"
13776 (cl-case type
13777 (all "all timestamps")
13778 (scheduled "only scheduled")
13779 (deadline "only deadline")
13780 (active "only active timestamps")
13781 (inactive "only inactive timestamps")
13782 (closed "with a closed time-stamp")
13783 (otherwise "scheduled/deadline")))
13784 (let ((answer (read-char-exclusive)))
13785 (cl-case answer
13787 (org-sparse-tree
13789 (cadr
13790 (memq type '(nil all scheduled deadline active inactive closed)))))
13791 (?d (call-interactively 'org-check-deadlines))
13792 (?b (call-interactively 'org-check-before-date))
13793 (?a (call-interactively 'org-check-after-date))
13794 (?D (call-interactively 'org-check-dates-range))
13795 (?t (call-interactively 'org-show-todo-tree))
13796 (?T (org-show-todo-tree '(4)))
13797 (?m (call-interactively 'org-match-sparse-tree))
13798 ((?p ?P)
13799 (let* ((kwd (completing-read
13800 "Property: " (mapcar #'list (org-buffer-property-keys))))
13801 (value (completing-read
13802 "Value: " (mapcar #'list (org-property-values kwd)))))
13803 (unless (string-match "\\`{.*}\\'" value)
13804 (setq value (concat "\"" value "\"")))
13805 (org-match-sparse-tree arg (concat kwd "=" value))))
13806 ((?r ?R ?/) (call-interactively 'org-occur))
13807 (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
13809 (defvar-local org-occur-highlights nil
13810 "List of overlays used for occur matches.")
13811 (defvar-local org-occur-parameters nil
13812 "Parameters of the active org-occur calls.
13813 This is a list, each call to org-occur pushes as cons cell,
13814 containing the regular expression and the callback, onto the list.
13815 The list can contain several entries if `org-occur' has been called
13816 several time with the KEEP-PREVIOUS argument. Otherwise, this list
13817 will only contain one set of parameters. When the highlights are
13818 removed (for example with `C-c C-c', or with the next edit (depending
13819 on `org-remove-highlights-with-change'), this variable is emptied
13820 as well.")
13822 (defun org-occur (regexp &optional keep-previous callback)
13823 "Make a compact tree which shows all matches of REGEXP.
13825 The tree will show the lines where the regexp matches, and any other context
13826 defined in `org-show-context-detail', which see.
13828 When optional argument KEEP-PREVIOUS is non-nil, highlighting and exposing
13829 done by a previous call to `org-occur' will be kept, to allow stacking of
13830 calls to this command.
13832 Optional argument CALLBACK can be a function of no argument. In this case,
13833 it is called with point at the end of the match, match data being set
13834 accordingly. Current match is shown only if the return value is non-nil.
13835 The function must neither move point nor alter narrowing."
13836 (interactive "sRegexp: \nP")
13837 (when (equal regexp "")
13838 (user-error "Regexp cannot be empty"))
13839 (unless keep-previous
13840 (org-remove-occur-highlights nil nil t))
13841 (push (cons regexp callback) org-occur-parameters)
13842 (let ((cnt 0))
13843 (save-excursion
13844 (goto-char (point-min))
13845 (when (or (not keep-previous) ; do not want to keep
13846 (not org-occur-highlights)) ; no previous matches
13847 ;; hide everything
13848 (org-overview))
13849 (let ((case-fold-search (if (eq org-occur-case-fold-search 'smart)
13850 (isearch-no-upper-case-p regexp t)
13851 org-occur-case-fold-search)))
13852 (while (re-search-forward regexp nil t)
13853 (when (or (not callback)
13854 (save-match-data (funcall callback)))
13855 (setq cnt (1+ cnt))
13856 (when org-highlight-sparse-tree-matches
13857 (org-highlight-new-match (match-beginning 0) (match-end 0)))
13858 (org-show-context 'occur-tree)))))
13859 (when org-remove-highlights-with-change
13860 (add-hook 'before-change-functions 'org-remove-occur-highlights
13861 nil 'local))
13862 (unless org-sparse-tree-open-archived-trees
13863 (org-hide-archived-subtrees (point-min) (point-max)))
13864 (run-hooks 'org-occur-hook)
13865 (when (called-interactively-p 'interactive)
13866 (message "%d match(es) for regexp %s" cnt regexp))
13867 cnt))
13869 (defun org-occur-next-match (&optional n _reset)
13870 "Function for `next-error-function' to find sparse tree matches.
13871 N is the number of matches to move, when negative move backwards.
13872 This function always goes back to the starting point when no
13873 match is found."
13874 (let* ((limit (if (< n 0) (point-min) (point-max)))
13875 (search-func (if (< n 0)
13876 'previous-single-char-property-change
13877 'next-single-char-property-change))
13878 (n (abs n))
13879 (pos (point))
13881 (catch 'exit
13882 (while (setq p1 (funcall search-func (point) 'org-type))
13883 (when (equal p1 limit)
13884 (goto-char pos)
13885 (user-error "No more matches"))
13886 (when (equal (get-char-property p1 'org-type) 'org-occur)
13887 (setq n (1- n))
13888 (when (= n 0)
13889 (goto-char p1)
13890 (throw 'exit (point))))
13891 (goto-char p1))
13892 (goto-char p1)
13893 (user-error "No more matches"))))
13895 (defun org-show-context (&optional key)
13896 "Make sure point and context are visible.
13897 Optional argument KEY, when non-nil, is a symbol. See
13898 `org-show-context-detail' for allowed values and how much is to
13899 be shown."
13900 (org-show-set-visibility
13901 (cond ((symbolp org-show-context-detail) org-show-context-detail)
13902 ((cdr (assq key org-show-context-detail)))
13903 (t (cdr (assq 'default org-show-context-detail))))))
13905 (defun org-show-set-visibility (detail)
13906 "Set visibility around point according to DETAIL.
13907 DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
13908 `tree', `canonical' or t. See `org-show-context-detail' for more
13909 information."
13910 ;; Show current heading and possibly its entry, following headline
13911 ;; or all children.
13912 (if (and (org-at-heading-p) (not (eq detail 'local)))
13913 (org-flag-heading nil)
13914 (org-show-entry)
13915 ;; If point is hidden within a drawer or a block, make sure to
13916 ;; expose it.
13917 (dolist (o (overlays-at (point)))
13918 (when (memq (overlay-get o 'invisible) '(org-hide-block outline))
13919 (delete-overlay o)))
13920 (unless (org-before-first-heading-p)
13921 (org-with-limited-levels
13922 (cl-case detail
13923 ((tree canonical t) (org-show-children))
13924 ((nil minimal ancestors))
13925 (t (save-excursion
13926 (outline-next-heading)
13927 (org-flag-heading nil)))))))
13928 ;; Show all siblings.
13929 (when (eq detail 'lineage) (org-show-siblings))
13930 ;; Show ancestors, possibly with their children.
13931 (when (memq detail '(ancestors lineage tree canonical t))
13932 (save-excursion
13933 (while (org-up-heading-safe)
13934 (org-flag-heading nil)
13935 (when (memq detail '(canonical t)) (org-show-entry))
13936 (when (memq detail '(tree canonical t)) (org-show-children))))))
13938 (defvar org-reveal-start-hook nil
13939 "Hook run before revealing a location.")
13941 (defun org-reveal (&optional siblings)
13942 "Show current entry, hierarchy above it, and the following headline.
13944 This can be used to show a consistent set of context around
13945 locations exposed with `org-show-context'.
13947 With optional argument SIBLINGS, on each level of the hierarchy all
13948 siblings are shown. This repairs the tree structure to what it would
13949 look like when opened with hierarchical calls to `org-cycle'.
13951 With a \\[universal-argument] \\[universal-argument] prefix, \
13952 go to the parent and show the entire tree."
13953 (interactive "P")
13954 (run-hooks 'org-reveal-start-hook)
13955 (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
13956 ((equal siblings '(16))
13957 (save-excursion
13958 (when (org-up-heading-safe)
13959 (org-show-subtree)
13960 (run-hook-with-args 'org-cycle-hook 'subtree))))
13961 (t (org-show-set-visibility 'lineage))))
13963 (defun org-highlight-new-match (beg end)
13964 "Highlight from BEG to END and mark the highlight is an occur headline."
13965 (let ((ov (make-overlay beg end)))
13966 (overlay-put ov 'face 'secondary-selection)
13967 (overlay-put ov 'org-type 'org-occur)
13968 (push ov org-occur-highlights)))
13970 (defun org-remove-occur-highlights (&optional _beg _end noremove)
13971 "Remove the occur highlights from the buffer.
13972 BEG and END are ignored. If NOREMOVE is nil, remove this function
13973 from the `before-change-functions' in the current buffer."
13974 (interactive)
13975 (unless org-inhibit-highlight-removal
13976 (mapc #'delete-overlay org-occur-highlights)
13977 (setq org-occur-highlights nil)
13978 (setq org-occur-parameters nil)
13979 (unless noremove
13980 (remove-hook 'before-change-functions
13981 'org-remove-occur-highlights 'local))))
13983 ;;;; Priorities
13985 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
13986 "Regular expression matching the priority indicator.")
13988 (defvar org-remove-priority-next-time nil)
13990 (defun org-priority-up ()
13991 "Increase the priority of the current item."
13992 (interactive)
13993 (org-priority 'up))
13995 (defun org-priority-down ()
13996 "Decrease the priority of the current item."
13997 (interactive)
13998 (org-priority 'down))
14000 (defun org-priority (&optional action _show)
14001 "Change the priority of an item.
14002 ACTION can be `set', `up', `down', or a character."
14003 (interactive "P")
14004 (if (equal action '(4))
14005 (org-show-priority)
14006 (unless org-enable-priority-commands
14007 (user-error "Priority commands are disabled"))
14008 (setq action (or action 'set))
14009 (let (current new news have remove)
14010 (save-excursion
14011 (org-back-to-heading t)
14012 (when (looking-at org-priority-regexp)
14013 (setq current (string-to-char (match-string 2))
14014 have t))
14015 (cond
14016 ((eq action 'remove)
14017 (setq remove t new ?\ ))
14018 ((or (eq action 'set)
14019 (integerp action))
14020 (if (not (eq action 'set))
14021 (setq new action)
14022 (message "Priority %c-%c, SPC to remove: "
14023 org-highest-priority org-lowest-priority)
14024 (save-match-data
14025 (setq new (read-char-exclusive))))
14026 (when (and (= (upcase org-highest-priority) org-highest-priority)
14027 (= (upcase org-lowest-priority) org-lowest-priority))
14028 (setq new (upcase new)))
14029 (cond ((equal new ?\ ) (setq remove t))
14030 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
14031 (user-error "Priority must be between `%c' and `%c'"
14032 org-highest-priority org-lowest-priority))))
14033 ((eq action 'up)
14034 (setq new (if have
14035 (1- current) ; normal cycling
14036 ;; last priority was empty
14037 (if (eq last-command this-command)
14038 org-lowest-priority ; wrap around empty to lowest
14039 ;; default
14040 (if org-priority-start-cycle-with-default
14041 org-default-priority
14042 (1- org-default-priority))))))
14043 ((eq action 'down)
14044 (setq new (if have
14045 (1+ current) ; normal cycling
14046 ;; last priority was empty
14047 (if (eq last-command this-command)
14048 org-highest-priority ; wrap around empty to highest
14049 ;; default
14050 (if org-priority-start-cycle-with-default
14051 org-default-priority
14052 (1+ org-default-priority))))))
14053 (t (user-error "Invalid action")))
14054 (when (or (< (upcase new) org-highest-priority)
14055 (> (upcase new) org-lowest-priority))
14056 (if (and (memq action '(up down))
14057 (not have) (not (eq last-command this-command)))
14058 ;; `new' is from default priority
14059 (error
14060 "The default can not be set, see `org-default-priority' why")
14061 ;; normal cycling: `new' is beyond highest/lowest priority
14062 ;; and is wrapped around to the empty priority
14063 (setq remove t)))
14064 (setq news (format "%c" new))
14065 (if have
14066 (if remove
14067 (replace-match "" t t nil 1)
14068 (replace-match news t t nil 2))
14069 (if remove
14070 (user-error "No priority cookie found in line")
14071 (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
14072 (if (match-end 2)
14073 (progn
14074 (goto-char (match-end 2))
14075 (insert " [#" news "]"))
14076 (goto-char (match-beginning 3))
14077 (insert "[#" news "] "))))
14078 (org-set-tags nil 'align))
14079 (if remove
14080 (message "Priority removed")
14081 (message "Priority of current item set to %s" news)))))
14083 (defun org-show-priority ()
14084 "Show the priority of the current item.
14085 This priority is composed of the main priority given with the [#A] cookies,
14086 and by additional input from the age of a schedules or deadline entry."
14087 (interactive)
14088 (let ((pri (if (eq major-mode 'org-agenda-mode)
14089 (org-get-at-bol 'priority)
14090 (save-excursion
14091 (save-match-data
14092 (beginning-of-line)
14093 (and (looking-at org-heading-regexp)
14094 (org-get-priority (match-string 0))))))))
14095 (message "Priority is %d" (if pri pri -1000))))
14097 (defun org-get-priority (s)
14098 "Find priority cookie and return priority."
14099 (save-match-data
14100 (if (functionp org-get-priority-function)
14101 (funcall org-get-priority-function)
14102 (if (not (string-match org-priority-regexp s))
14103 (* 1000 (- org-lowest-priority org-default-priority))
14104 (* 1000 (- org-lowest-priority
14105 (string-to-char (match-string 2 s))))))))
14107 ;;;; Tags
14109 (defvar org-agenda-archives-mode)
14110 (defvar org-map-continue-from nil
14111 "Position from where mapping should continue.
14112 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
14114 (defvar org-scanner-tags nil
14115 "The current tag list while the tags scanner is running.")
14117 (defvar org-trust-scanner-tags nil
14118 "Should `org-get-tags-at' use the tags for the scanner.
14119 This is for internal dynamical scoping only.
14120 When this is non-nil, the function `org-get-tags-at' will return the value
14121 of `org-scanner-tags' instead of building the list by itself. This
14122 can lead to large speed-ups when the tags scanner is used in a file with
14123 many entries, and when the list of tags is retrieved, for example to
14124 obtain a list of properties. Building the tags list for each entry in such
14125 a file becomes an N^2 operation - but with this variable set, it scales
14126 as N.")
14128 (defvar org--matcher-tags-todo-only nil)
14130 (defun org-scan-tags (action matcher todo-only &optional start-level)
14131 "Scan headline tags with inheritance and produce output ACTION.
14133 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
14134 or `agenda' to produce an entry list for an agenda view. It can also be
14135 a Lisp form or a function that should be called at each matched headline, in
14136 this case the return value is a list of all return values from these calls.
14138 MATCHER is a function accepting three arguments, returning
14139 a non-nil value whenever a given set of tags qualifies a headline
14140 for inclusion. See `org-make-tags-matcher' for more information.
14141 As a special case, it can also be set to t (respectively nil) in
14142 order to match all (respectively none) headline.
14144 When TODO-ONLY is non-nil, only lines with a TODO keyword are
14145 included in the output.
14147 START-LEVEL can be a string with asterisks, reducing the scope to
14148 headlines matching this string."
14149 (require 'org-agenda)
14150 (let* ((re (concat "^"
14151 (if start-level
14152 ;; Get the correct level to match
14153 (concat "\\*\\{" (number-to-string start-level) "\\} ")
14154 org-outline-regexp)
14155 " *\\(\\<\\("
14156 (mapconcat #'regexp-quote org-todo-keywords-1 "\\|")
14157 "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$"))
14158 (props (list 'face 'default
14159 'done-face 'org-agenda-done
14160 'undone-face 'default
14161 'mouse-face 'highlight
14162 'org-not-done-regexp org-not-done-regexp
14163 'org-todo-regexp org-todo-regexp
14164 'org-complex-heading-regexp org-complex-heading-regexp
14165 'help-echo
14166 (format "mouse-2 or RET jump to Org file %S"
14167 (abbreviate-file-name
14168 (or (buffer-file-name (buffer-base-buffer))
14169 (buffer-name (buffer-base-buffer)))))))
14170 (org-map-continue-from nil)
14171 lspos tags tags-list
14172 (tags-alist (list (cons 0 org-file-tags)))
14173 (llast 0) rtn rtn1 level category i txt
14174 todo marker entry priority
14175 ts-date ts-date-type ts-date-pair)
14176 (unless (or (member action '(agenda sparse-tree)) (functionp action))
14177 (setq action (list 'lambda nil action)))
14178 (save-excursion
14179 (goto-char (point-min))
14180 (when (eq action 'sparse-tree)
14181 (org-overview)
14182 (org-remove-occur-highlights))
14183 (while (let (case-fold-search)
14184 (re-search-forward re nil t))
14185 (setq org-map-continue-from nil)
14186 (catch :skip
14187 ;; Ignore closing parts of inline tasks.
14188 (when (and (fboundp 'org-inlinetask-end-p) (org-inlinetask-end-p))
14189 (throw :skip t))
14190 (setq todo
14191 ;; TODO: is the 1-2 difference a bug?
14192 (when (match-end 1) (match-string-no-properties 2))
14193 tags (when (match-end 4) (match-string-no-properties 4)))
14194 (goto-char (setq lspos (match-beginning 0)))
14195 (setq level (org-reduced-level (org-outline-level))
14196 category (org-get-category))
14197 (when (eq action 'agenda)
14198 (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
14199 ts-date (car ts-date-pair)
14200 ts-date-type (cdr ts-date-pair)))
14201 (setq i llast llast level)
14202 ;; remove tag lists from same and sublevels
14203 (while (>= i level)
14204 (when (setq entry (assoc i tags-alist))
14205 (setq tags-alist (delete entry tags-alist)))
14206 (setq i (1- i)))
14207 ;; add the next tags
14208 (when tags
14209 (setq tags (org-split-string tags ":")
14210 tags-alist
14211 (cons (cons level tags) tags-alist)))
14212 ;; compile tags for current headline
14213 (setq tags-list
14214 (if org-use-tag-inheritance
14215 (apply 'append (mapcar 'cdr (reverse tags-alist)))
14216 tags)
14217 org-scanner-tags tags-list)
14218 (when org-use-tag-inheritance
14219 (setcdr (car tags-alist)
14220 (mapcar (lambda (x)
14221 (setq x (copy-sequence x))
14222 (org-add-prop-inherited x))
14223 (cdar tags-alist))))
14224 (when (and tags org-use-tag-inheritance
14225 (or (not (eq t org-use-tag-inheritance))
14226 org-tags-exclude-from-inheritance))
14227 ;; Selective inheritance, remove uninherited ones.
14228 (setcdr (car tags-alist)
14229 (org-remove-uninherited-tags (cdar tags-alist))))
14230 (when (and
14232 ;; eval matcher only when the todo condition is OK
14233 (and (or (not todo-only) (member todo org-todo-keywords-1))
14234 (if (functionp matcher)
14235 (let ((case-fold-search t) (org-trust-scanner-tags t))
14236 (funcall matcher todo tags-list level))
14237 matcher))
14239 ;; Call the skipper, but return t if it does not
14240 ;; skip, so that the `and' form continues evaluating.
14241 (progn
14242 (unless (eq action 'sparse-tree) (org-agenda-skip))
14245 ;; Check if timestamps are deselecting this entry
14246 (or (not todo-only)
14247 (and (member todo org-todo-keywords-1)
14248 (or (not org-agenda-tags-todo-honor-ignore-options)
14249 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
14251 ;; select this headline
14252 (cond
14253 ((eq action 'sparse-tree)
14254 (and org-highlight-sparse-tree-matches
14255 (org-get-heading) (match-end 0)
14256 (org-highlight-new-match
14257 (match-beginning 1) (match-end 1)))
14258 (org-show-context 'tags-tree))
14259 ((eq action 'agenda)
14260 (setq txt (org-agenda-format-item
14262 (concat
14263 (if (eq org-tags-match-list-sublevels 'indented)
14264 (make-string (1- level) ?.) "")
14265 (org-get-heading))
14266 (make-string level ?\s)
14267 category
14268 tags-list)
14269 priority (org-get-priority txt))
14270 (goto-char lspos)
14271 (setq marker (org-agenda-new-marker))
14272 (org-add-props txt props
14273 'org-marker marker 'org-hd-marker marker 'org-category category
14274 'todo-state todo
14275 'ts-date ts-date
14276 'priority priority
14277 'type (concat "tagsmatch" ts-date-type))
14278 (push txt rtn))
14279 ((functionp action)
14280 (setq org-map-continue-from nil)
14281 (save-excursion
14282 (setq rtn1 (funcall action))
14283 (push rtn1 rtn)))
14284 (t (user-error "Invalid action")))
14286 ;; if we are to skip sublevels, jump to end of subtree
14287 (unless org-tags-match-list-sublevels
14288 (org-end-of-subtree t)
14289 (backward-char 1))))
14290 ;; Get the correct position from where to continue
14291 (if org-map-continue-from
14292 (goto-char org-map-continue-from)
14293 (and (= (point) lspos) (end-of-line 1)))))
14294 (when (and (eq action 'sparse-tree)
14295 (not org-sparse-tree-open-archived-trees))
14296 (org-hide-archived-subtrees (point-min) (point-max)))
14297 (nreverse rtn)))
14299 (defun org-remove-uninherited-tags (tags)
14300 "Remove all tags that are not inherited from the list TAGS."
14301 (cond
14302 ((eq org-use-tag-inheritance t)
14303 (if org-tags-exclude-from-inheritance
14304 (org-delete-all org-tags-exclude-from-inheritance tags)
14305 tags))
14306 ((not org-use-tag-inheritance) nil)
14307 ((stringp org-use-tag-inheritance)
14308 (delq nil (mapcar
14309 (lambda (x)
14310 (if (and (string-match org-use-tag-inheritance x)
14311 (not (member x org-tags-exclude-from-inheritance)))
14312 x nil))
14313 tags)))
14314 ((listp org-use-tag-inheritance)
14315 (delq nil (mapcar
14316 (lambda (x)
14317 (if (member x org-use-tag-inheritance) x nil))
14318 tags)))))
14320 (defun org-match-sparse-tree (&optional todo-only match)
14321 "Create a sparse tree according to tags string MATCH.
14323 MATCH is a string with match syntax. It can contain a selection
14324 of tags (\"+work+urgent-boss\"), properties (\"LEVEL>3\"), and
14325 TODO keywords (\"TODO=\\\"WAITING\\\"\") or a combination of
14326 those. See the manual for details.
14328 If optional argument TODO-ONLY is non-nil, only select lines that
14329 are also TODO tasks."
14330 (interactive "P")
14331 (org-agenda-prepare-buffers (list (current-buffer)))
14332 (let ((org--matcher-tags-todo-only todo-only))
14333 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))
14334 org--matcher-tags-todo-only)))
14336 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
14338 (defvar org-cached-props nil)
14339 (defun org-cached-entry-get (pom property)
14340 (if (or (eq t org-use-property-inheritance)
14341 (and (stringp org-use-property-inheritance)
14342 (let ((case-fold-search t))
14343 (string-match-p org-use-property-inheritance property)))
14344 (and (listp org-use-property-inheritance)
14345 (member-ignore-case property org-use-property-inheritance)))
14346 ;; Caching is not possible, check it directly.
14347 (org-entry-get pom property 'inherit)
14348 ;; Get all properties, so we can do complicated checks easily.
14349 (cdr (assoc-string property
14350 (or org-cached-props
14351 (setq org-cached-props (org-entry-properties pom)))
14352 t))))
14354 (defun org-global-tags-completion-table (&optional files)
14355 "Return the list of all tags in all agenda buffer/files.
14356 Optional FILES argument is a list of files which can be used
14357 instead of the agenda files."
14358 (save-excursion
14359 (org-uniquify
14360 (delq nil
14361 (apply #'append
14362 (mapcar
14363 (lambda (file)
14364 (set-buffer (find-file-noselect file))
14365 (org--tag-add-to-alist
14366 (org-get-buffer-tags)
14367 (mapcar (lambda (x)
14368 (and (stringp (car-safe x))
14369 (list (car-safe x))))
14370 org-current-tag-alist)))
14371 (if (car-safe files) files
14372 (org-agenda-files))))))))
14374 (defun org-make-tags-matcher (match)
14375 "Create the TAGS/TODO matcher form for the selection string MATCH.
14377 Returns a cons of the selection string MATCH and a function
14378 implementing the matcher.
14380 The matcher is to be called at an Org entry, with point on the
14381 headline, and returns non-nil if the entry matches the selection
14382 string MATCH. It must be called with three arguments: the TODO
14383 keyword at the entry (or nil if none), the list of all tags at
14384 the entry including inherited ones and the reduced level of the
14385 headline. Additionally, the category of the entry, if any, must
14386 be specified as the text property `org-category' on the headline.
14388 This function sets the variable `org--matcher-tags-todo-only' to
14389 a non-nil value if the matcher restricts matching to TODO
14390 entries, otherwise it is not touched.
14392 See also `org-scan-tags'."
14393 (unless match
14394 ;; Get a new match request, with completion against the global
14395 ;; tags table and the local tags in current buffer.
14396 (let ((org-last-tags-completion-table
14397 (org--tag-add-to-alist
14398 (org-get-buffer-tags)
14399 (org-global-tags-completion-table))))
14400 (setq match
14401 (completing-read
14402 "Match: "
14403 'org-tags-completion-function nil nil nil 'org-tags-history))))
14405 (let ((match0 match)
14406 (re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)")
14407 (start 0)
14408 tagsmatch todomatch tagsmatcher todomatcher)
14410 ;; Expand group tags.
14411 (setq match (org-tags-expand match))
14413 ;; Check if there is a TODO part of this match, which would be the
14414 ;; part after a "/". To make sure that this slash is not part of
14415 ;; a property value to be matched against, we also check that
14416 ;; there is no / after that slash. First, find the last slash.
14417 (let ((s 0))
14418 (while (string-match "/+" match s)
14419 (setq start (match-beginning 0))
14420 (setq s (match-end 0))))
14421 (if (and (string-match "/+" match start)
14422 (not (string-match-p "\"" match start)))
14423 ;; Match contains also a TODO-matching request.
14424 (progn
14425 (setq tagsmatch (substring match 0 (match-beginning 0)))
14426 (setq todomatch (substring match (match-end 0)))
14427 (when (string-prefix-p "!" todomatch)
14428 (setq org--matcher-tags-todo-only t)
14429 (setq todomatch (substring todomatch 1)))
14430 (when (string-match "\\`\\s-*\\'" todomatch)
14431 (setq todomatch nil)))
14432 ;; Only matching tags.
14433 (setq tagsmatch match)
14434 (setq todomatch nil))
14436 ;; Make the tags matcher.
14437 (when (org-string-nw-p tagsmatch)
14438 (let ((orlist nil)
14439 (orterms (org-split-string tagsmatch "|"))
14440 term)
14441 (while (setq term (pop orterms))
14442 (while (and (equal (substring term -1) "\\") orterms)
14443 (setq term (concat term "|" (pop orterms)))) ;repair bad split.
14444 (while (string-match re term)
14445 (let* ((rest (substring term (match-end 0)))
14446 (minus (and (match-end 1)
14447 (equal (match-string 1 term) "-")))
14448 (tag (save-match-data
14449 (replace-regexp-in-string
14450 "\\\\-" "-" (match-string 2 term))))
14451 (regexp (eq (string-to-char tag) ?{))
14452 (levelp (match-end 4))
14453 (propp (match-end 5))
14455 (cond
14456 (regexp `(org-match-any-p ,(substring tag 1 -1) tags-list))
14457 (levelp
14458 `(,(org-op-to-function (match-string 3 term))
14459 level
14460 ,(string-to-number (match-string 4 term))))
14461 (propp
14462 (let* ((gv (pcase (upcase (match-string 5 term))
14463 ("CATEGORY"
14464 '(get-text-property (point) 'org-category))
14465 ("TODO" 'todo)
14466 (p `(org-cached-entry-get nil ,p))))
14467 (pv (match-string 7 term))
14468 (regexp (eq (string-to-char pv) ?{))
14469 (strp (eq (string-to-char pv) ?\"))
14470 (timep (string-match-p "^\"[[<].*[]>]\"$" pv))
14471 (po (org-op-to-function (match-string 6 term)
14472 (if timep 'time strp))))
14473 (setq pv (if (or regexp strp) (substring pv 1 -1) pv))
14474 (when timep (setq pv (org-matcher-time pv)))
14475 (cond ((and regexp (eq po 'org<>))
14476 `(not (string-match ,pv (or ,gv ""))))
14477 (regexp `(string-match ,pv (or ,gv "")))
14478 (strp `(,po (or ,gv "") ,pv))
14480 `(,po
14481 (string-to-number (or ,gv ""))
14482 ,(string-to-number pv))))))
14483 (t `(member ,tag tags-list)))))
14484 (push (if minus `(not ,mm) mm) tagsmatcher)
14485 (setq term rest)))
14486 (push `(and ,@tagsmatcher) orlist)
14487 (setq tagsmatcher nil))
14488 (setq tagsmatcher `(progn (setq org-cached-props nil) (or ,@orlist)))))
14490 ;; Make the TODO matcher.
14491 (when (org-string-nw-p todomatch)
14492 (let ((orlist nil))
14493 (dolist (term (org-split-string todomatch "|"))
14494 (while (string-match re term)
14495 (let* ((minus (and (match-end 1)
14496 (equal (match-string 1 term) "-")))
14497 (kwd (match-string 2 term))
14498 (regexp (eq (string-to-char kwd) ?{))
14499 (mm (if regexp `(string-match ,(substring kwd 1 -1) todo)
14500 `(equal todo ,kwd))))
14501 (push (if minus `(not ,mm) mm) todomatcher))
14502 (setq term (substring term (match-end 0))))
14503 (push (if (> (length todomatcher) 1)
14504 (cons 'and todomatcher)
14505 (car todomatcher))
14506 orlist)
14507 (setq todomatcher nil))
14508 (setq todomatcher (cons 'or orlist))))
14510 ;; Return the string and function of the matcher. If no
14511 ;; tags-specific or todo-specific matcher exists, match
14512 ;; everything.
14513 (let ((matcher (if (and tagsmatcher todomatcher)
14514 `(and ,tagsmatcher ,todomatcher)
14515 (or tagsmatcher todomatcher t))))
14516 (when org--matcher-tags-todo-only
14517 (setq matcher `(and (member todo org-not-done-keywords) ,matcher)))
14518 (cons match0 `(lambda (todo tags-list level) ,matcher)))))
14520 (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
14521 "Expand group tags in MATCH.
14523 This replaces every group tag in MATCH with a regexp tag search.
14524 For example, a group tag \"Work\" defined as { Work : Lab Conf }
14525 will be replaced like this:
14527 Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
14528 +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
14529 -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
14531 Replacing by a regexp preserves the structure of the match.
14532 E.g., this expansion
14534 Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
14536 will match anything tagged with \"Lab\" and \"Home\", or tagged
14537 with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
14539 A group tag in MATCH can contain regular expressions of its own.
14540 For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
14541 will be replaced like this:
14543 Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
14545 When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
14546 assumed to be a single group tag, and the function will return
14547 the list of tags in this group.
14549 When DOWNCASE is non-nil, expand downcased TAGS."
14550 (if org-group-tags
14551 (let* ((case-fold-search t)
14552 (stable org-mode-syntax-table)
14553 (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
14554 (taggroups (if downcased
14555 (mapcar (lambda (tg) (mapcar #'downcase tg))
14556 taggroups)
14557 taggroups))
14558 (taggroups-keys (mapcar #'car taggroups))
14559 (return-match (if downcased (downcase match) match))
14560 (count 0)
14561 (work-already-expanded tags-already-expanded)
14562 regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
14563 ;; @ and _ are allowed as word-components in tags.
14564 (modify-syntax-entry ?@ "w" stable)
14565 (modify-syntax-entry ?_ "w" stable)
14566 ;; Temporarily replace regexp-expressions in the match-expression.
14567 (while (string-match "{.+?}" return-match)
14568 (cl-incf count)
14569 (push (match-string 0 return-match) regexps-in-match)
14570 (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
14571 (while (and taggroups-keys
14572 (with-syntax-table stable
14573 (string-match
14574 (concat "\\(?1:[+-]?\\)\\(?2:\\<"
14575 (regexp-opt taggroups-keys) "\\>\\)")
14576 return-match)))
14577 (let* ((dir (match-string 1 return-match))
14578 (tag (match-string 2 return-match))
14579 (tag (if downcased (downcase tag) tag)))
14580 (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
14581 (member tag tags-already-expanded))
14582 (setq tags-in-group (assoc tag taggroups))
14583 (push tag work-already-expanded)
14584 ;; Recursively expand each tag in the group, if the tag hasn't
14585 ;; already been expanded. Restore the match-data after all recursive calls.
14586 (save-match-data
14587 (let (tags-expanded)
14588 (dolist (x (cdr tags-in-group))
14589 (if (and (member x taggroups-keys)
14590 (not (member x work-already-expanded)))
14591 (setq tags-expanded
14592 (delete-dups
14593 (append
14594 (org-tags-expand x t downcased
14595 work-already-expanded)
14596 tags-expanded)))
14597 (setq tags-expanded
14598 (append (list x) tags-expanded)))
14599 (setq work-already-expanded
14600 (delete-dups
14601 (append tags-expanded
14602 work-already-expanded))))
14603 (setq tags-in-group
14604 (delete-dups (cons (car tags-in-group)
14605 tags-expanded)))))
14606 ;; Filter tag-regexps from tags.
14607 (setq regexp-in-group-escaped
14608 (delq nil (mapcar (lambda (x)
14609 (if (stringp x)
14610 (and (equal "{" (substring x 0 1))
14611 (equal "}" (substring x -1))
14614 tags-in-group))
14615 regexp-in-group
14616 (mapcar (lambda (x)
14617 (substring x 1 -1))
14618 regexp-in-group-escaped)
14619 tags-in-group
14620 (delq nil (mapcar (lambda (x)
14621 (if (stringp x)
14622 (and (not (equal "{" (substring x 0 1)))
14623 (not (equal "}" (substring x -1)))
14626 tags-in-group)))
14627 ;; If single-as-list, do no more in the while-loop.
14628 (if (not single-as-list)
14629 (progn
14630 (when regexp-in-group
14631 (setq regexp-in-group
14632 (concat "\\|"
14633 (mapconcat 'identity regexp-in-group
14634 "\\|"))))
14635 (setq tags-in-group
14636 (concat dir
14637 "{\\<"
14638 (regexp-opt tags-in-group)
14639 "\\>"
14640 regexp-in-group
14641 "}"))
14642 (when (stringp tags-in-group)
14643 (org-add-props tags-in-group '(grouptag t)))
14644 (setq return-match
14645 (replace-match tags-in-group t t return-match)))
14646 (setq tags-in-group
14647 (append regexp-in-group-escaped tags-in-group))))
14648 (setq taggroups-keys (delete tag taggroups-keys))))
14649 ;; Add the regular expressions back into the match-expression again.
14650 (while regexps-in-match
14651 (setq return-match (replace-regexp-in-string (format "<%d>" count)
14652 (pop regexps-in-match)
14653 return-match t t))
14654 (cl-decf count))
14655 (if single-as-list
14656 (if tags-in-group tags-in-group (list return-match))
14657 return-match))
14658 (if single-as-list
14659 (list (if downcased (downcase match) match))
14660 match)))
14662 (defun org-op-to-function (op &optional stringp)
14663 "Turn an operator into the appropriate function."
14664 (setq op
14665 (cond
14666 ((equal op "<" ) '(< string< org-time<))
14667 ((equal op ">" ) '(> org-string> org-time>))
14668 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
14669 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
14670 ((member op '("=" "==")) '(= string= org-time=))
14671 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
14672 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
14674 (defun org<> (a b) (not (= a b)))
14675 (defun org-string<= (a b) (or (string= a b) (string< a b)))
14676 (defun org-string>= (a b) (not (string< a b)))
14677 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
14678 (defun org-string<> (a b) (not (string= a b)))
14679 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
14680 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
14681 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
14682 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
14683 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
14684 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
14685 (defun org-2ft (s)
14686 "Convert S to a floating point time.
14687 If S is already a number, just return it. If it is a string, parse
14688 it as a time string and apply `float-time' to it. If S is nil, just return 0."
14689 (cond
14690 ((numberp s) s)
14691 ((stringp s)
14692 (condition-case nil
14693 (float-time (apply #'encode-time (org-parse-time-string s)))
14694 (error 0.)))
14695 (t 0.)))
14697 (defun org-time-today ()
14698 "Time in seconds today at 0:00.
14699 Returns the float number of seconds since the beginning of the
14700 epoch to the beginning of today (00:00)."
14701 (float-time (apply 'encode-time
14702 (append '(0 0 0) (nthcdr 3 (decode-time))))))
14704 (defun org-matcher-time (s)
14705 "Interpret a time comparison value."
14706 (save-match-data
14707 (cond
14708 ((string= s "<now>") (float-time))
14709 ((string= s "<today>") (org-time-today))
14710 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
14711 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
14712 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
14713 (+ (org-time-today)
14714 (* (string-to-number (match-string 1 s))
14715 (cdr (assoc (match-string 2 s)
14716 '(("d" . 86400.0) ("w" . 604800.0)
14717 ("m" . 2678400.0) ("y" . 31557600.0)))))))
14718 (t (org-2ft s)))))
14720 (defun org-match-any-p (re list)
14721 "Does re match any element of list?"
14722 (setq list (mapcar (lambda (x) (string-match re x)) list))
14723 (delq nil list))
14725 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
14726 (defvar org-tags-overlay (make-overlay 1 1))
14727 (delete-overlay org-tags-overlay)
14729 (defun org-get-local-tags-at (&optional pos)
14730 "Get a list of tags defined in the current headline."
14731 (org-get-tags-at pos 'local))
14733 (defun org-get-local-tags ()
14734 "Get a list of tags defined in the current headline."
14735 (org-get-tags-at nil 'local))
14737 (defun org-get-tags-at (&optional pos local)
14738 "Get a list of all headline tags applicable at POS.
14739 POS defaults to point. If tags are inherited, the list contains
14740 the targets in the same sequence as the headlines appear, i.e.
14741 the tags of the current headline come last.
14742 When LOCAL is non-nil, only return tags from the current headline,
14743 ignore inherited ones."
14744 (interactive)
14745 (if (and org-trust-scanner-tags
14746 (or (not pos) (equal pos (point)))
14747 (not local))
14748 org-scanner-tags
14749 (let (tags ltags lastpos parent)
14750 (save-excursion
14751 (save-restriction
14752 (widen)
14753 (goto-char (or pos (point)))
14754 (save-match-data
14755 (catch 'done
14756 (condition-case nil
14757 (progn
14758 (org-back-to-heading t)
14759 (while (not (equal lastpos (point)))
14760 (setq lastpos (point))
14761 (when (looking-at ".+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
14762 (setq ltags (org-split-string
14763 (match-string-no-properties 1) ":"))
14764 (when parent
14765 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
14766 (setq tags (append
14767 (if parent
14768 (org-remove-uninherited-tags ltags)
14769 ltags)
14770 tags)))
14771 (or org-use-tag-inheritance (throw 'done t))
14772 (when local (throw 'done t))
14773 (or (org-up-heading-safe) (error nil))
14774 (setq parent t)))
14775 (error nil)))))
14776 (if local
14777 tags
14778 (reverse (delete-dups
14779 (reverse (append
14780 (org-remove-uninherited-tags
14781 org-file-tags)
14782 tags)))))))))
14784 (defun org-add-prop-inherited (s)
14785 (add-text-properties 0 (length s) '(inherited t) s)
14788 (defun org-toggle-tag (tag &optional onoff)
14789 "Toggle the tag TAG for the current line.
14790 If ONOFF is `on' or `off', don't toggle but set to this state."
14791 (save-excursion
14792 (org-back-to-heading t)
14793 (let ((current
14794 (when (re-search-forward "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$"
14795 (line-end-position) t)
14796 (let ((tags (match-string 1)))
14797 ;; Clear current tags.
14798 (replace-match "")
14799 ;; Reverse the tags list so any new tag is appended to
14800 ;; the current list of tags.
14801 (nreverse (org-split-string tags ":")))))
14802 res)
14803 (pcase onoff
14804 (`off (setq current (delete tag current)))
14805 ((or `on (guard (not (member tag current))))
14806 (setq res t)
14807 (cl-pushnew tag current :test #'equal))
14808 (_ (setq current (delete tag current))))
14809 (end-of-line)
14810 (if current
14811 (progn
14812 (insert " :" (mapconcat #'identity (nreverse current) ":") ":")
14813 (org-set-tags nil t))
14814 (delete-horizontal-space))
14815 (run-hooks 'org-after-tags-change-hook)
14816 res)))
14818 (defun org--align-tags-here (to-col)
14819 "Align tags on the current headline to TO-COL.
14820 Assume point is on a headline."
14821 (let ((pos (point)))
14822 (beginning-of-line)
14823 (if (or (not (looking-at ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
14824 (>= pos (match-beginning 2)))
14825 ;; No tags or point within tags: do not align.
14826 (goto-char pos)
14827 (goto-char (match-beginning 1))
14828 (let ((shift (max (- (if (>= to-col 0) to-col
14829 (- (abs to-col) (string-width (match-string 2))))
14830 (current-column))
14831 1)))
14832 (replace-match (make-string shift ?\s) nil nil nil 1)
14833 ;; Preserve initial position, if possible. In any case, stop
14834 ;; before tags.
14835 (when (< pos (point)) (goto-char pos))))))
14837 (defun org-set-tags-command (&optional arg just-align)
14838 "Call the set-tags command for the current entry."
14839 (interactive "P")
14840 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
14841 (org-set-tags arg just-align)
14842 (save-excursion
14843 (unless (and (org-region-active-p)
14844 org-loop-over-headlines-in-active-region)
14845 (org-back-to-heading t))
14846 (org-set-tags arg just-align))))
14848 (defun org-set-tags-to (data)
14849 "Set the tags of the current entry to DATA, replacing current tags.
14850 DATA may be a tags string like \":aa:bb:cc:\", or a list of tags.
14851 If DATA is nil or the empty string, all tags are removed."
14852 (interactive "sTags: ")
14853 (let ((data
14854 (pcase (if (stringp data) (org-trim data) data)
14855 ((or `nil "") nil)
14856 ((pred listp) (format ":%s:" (mapconcat #'identity data ":")))
14857 ((pred stringp)
14858 (format ":%s:"
14859 (mapconcat #'identity (org-split-string data ":+") ":")))
14860 (_ (error "Invalid tag specification: %S" data)))))
14861 (org-with-wide-buffer
14862 (org-back-to-heading t)
14863 (let ((case-fold-search nil)) (looking-at org-complex-heading-regexp))
14864 (when (or (match-end 5) data)
14865 (goto-char (or (match-beginning 5) (line-end-position)))
14866 (skip-chars-backward " \t")
14867 (delete-region (point) (line-end-position))
14868 (when data
14869 (insert " " data)
14870 (org-set-tags nil 'align))))))
14872 (defun org-align-all-tags ()
14873 "Align the tags in all headings."
14874 (interactive)
14875 (save-excursion
14876 (or (ignore-errors (org-back-to-heading t))
14877 (outline-next-heading))
14878 (if (org-at-heading-p)
14879 (org-set-tags t)
14880 (message "No headings"))))
14882 (defvar org-indent-indentation-per-level)
14883 (defun org-set-tags (&optional arg just-align)
14884 "Set the tags for the current headline.
14885 With prefix ARG, realign all tags in headings in the current buffer.
14886 When JUST-ALIGN is non-nil, only align tags."
14887 (interactive "P")
14888 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
14889 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
14890 'region-start-level
14891 'region))
14892 org-loop-over-headlines-in-active-region)
14893 (org-map-entries
14894 ;; We don't use ARG and JUST-ALIGN here because these args
14895 ;; are not useful when looping over headlines.
14896 #'org-set-tags
14897 org-loop-over-headlines-in-active-region
14899 '(when (org-invisible-p) (org-end-of-subtree nil t))))
14900 (let ((org-setting-tags t))
14901 (if arg
14902 (save-excursion
14903 (goto-char (point-min))
14904 (while (re-search-forward org-outline-regexp-bol nil t)
14905 (org-set-tags nil t)
14906 (end-of-line))
14907 (message "All tags realigned to column %d" org-tags-column))
14908 (let* ((current (org-get-tags-string))
14909 (tags
14910 (if just-align current
14911 ;; Get a new set of tags from the user.
14912 (save-excursion
14913 (let* ((table
14914 (setq
14915 org-last-tags-completion-table
14916 (org--tag-add-to-alist
14917 (and
14918 org-complete-tags-always-offer-all-agenda-tags
14919 (org-global-tags-completion-table
14920 (org-agenda-files)))
14921 (or org-current-tag-alist
14922 (org-get-buffer-tags)))))
14923 (current-tags (org-split-string current ":"))
14924 (inherited-tags
14925 (nreverse (nthcdr (length current-tags)
14926 (nreverse (org-get-tags-at))))))
14927 (replace-regexp-in-string
14928 "\\([-+&]+\\|,\\)"
14930 (if (or (eq t org-use-fast-tag-selection)
14931 (and org-use-fast-tag-selection
14932 (delq nil (mapcar #'cdr table))))
14933 (org-fast-tag-selection
14934 current-tags inherited-tags table
14935 (and org-fast-tag-selection-include-todo
14936 org-todo-key-alist))
14937 (let ((org-add-colon-after-tag-completion
14938 (< 1 (length table))))
14939 (org-trim
14940 (completing-read
14941 "Tags: "
14942 #'org-tags-completion-function
14943 nil nil current 'org-tags-history))))))))))
14945 (when org-tags-sort-function
14946 (setq tags
14947 (mapconcat
14948 #'identity
14949 (sort (org-split-string tags "[^[:alnum:]_@#%]+")
14950 org-tags-sort-function)
14951 ":")))
14953 (if (or (string= ":" tags)
14954 (string= "::" tags))
14955 (setq tags ""))
14956 (if (not (org-string-nw-p tags)) (setq tags "")
14957 (unless (string-suffix-p ":" tags) (setq tags (concat tags ":")))
14958 (unless (string-prefix-p ":" tags) (setq tags (concat ":" tags))))
14960 ;; Insert new tags at the correct column.
14961 (unless (equal current tags)
14962 (save-excursion
14963 (beginning-of-line)
14964 (let ((case-fold-search nil))
14965 (looking-at org-complex-heading-regexp))
14966 ;; Remove current tags, if any.
14967 (when (match-end 5) (replace-match "" nil nil nil 5))
14968 ;; Insert new tags, if any. Otherwise, remove trailing
14969 ;; white spaces.
14970 (end-of-line)
14971 (if (not (equal tags ""))
14972 ;; When text is being inserted on an invisible
14973 ;; region boundary, it can be inadvertently sucked
14974 ;; into invisibility.
14975 (outline-flag-region (point) (progn (insert " " tags) (point)) nil)
14976 (skip-chars-backward " \t")
14977 (delete-region (point) (line-end-position)))))
14978 ;; Align tags, if any. Fix tags column if `org-indent-mode'
14979 ;; is on.
14980 (unless (equal tags "")
14981 (let* ((level (save-excursion
14982 (beginning-of-line)
14983 (skip-chars-forward "\\*")))
14984 (offset (if (bound-and-true-p org-indent-mode)
14985 (* (1- org-indent-indentation-per-level)
14986 (1- level))
14988 (tags-column
14989 (+ org-tags-column
14990 (if (> org-tags-column 0) (- offset) offset))))
14991 (org--align-tags-here tags-column))))
14992 (unless just-align (run-hooks 'org-after-tags-change-hook))))))
14994 (defun org-change-tag-in-region (beg end tag off)
14995 "Add or remove TAG for each entry in the region.
14996 This works in the agenda, and also in an Org buffer."
14997 (interactive
14998 (list (region-beginning) (region-end)
14999 (let ((org-last-tags-completion-table
15000 (if (derived-mode-p 'org-mode)
15001 (org--tag-add-to-alist
15002 (org-get-buffer-tags)
15003 (org-global-tags-completion-table))
15004 (org-global-tags-completion-table))))
15005 (completing-read
15006 "Tag: " 'org-tags-completion-function nil nil nil
15007 'org-tags-history))
15008 (progn
15009 (message "[s]et or [r]emove? ")
15010 (equal (read-char-exclusive) ?r))))
15011 (when (fboundp 'deactivate-mark) (deactivate-mark))
15012 (let ((agendap (equal major-mode 'org-agenda-mode))
15013 l1 l2 m buf pos newhead (cnt 0))
15014 (goto-char end)
15015 (setq l2 (1- (org-current-line)))
15016 (goto-char beg)
15017 (setq l1 (org-current-line))
15018 (cl-loop for l from l1 to l2 do
15019 (org-goto-line l)
15020 (setq m (get-text-property (point) 'org-hd-marker))
15021 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
15022 (and agendap m))
15023 (setq buf (if agendap (marker-buffer m) (current-buffer))
15024 pos (if agendap m (point)))
15025 (with-current-buffer buf
15026 (save-excursion
15027 (save-restriction
15028 (goto-char pos)
15029 (setq cnt (1+ cnt))
15030 (org-toggle-tag tag (if off 'off 'on))
15031 (setq newhead (org-get-heading)))))
15032 (and agendap (org-agenda-change-all-lines newhead m))))
15033 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
15035 (defun org-tags-completion-function (string _predicate &optional flag)
15036 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
15037 (confirm (lambda (x) (stringp (car x)))))
15038 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
15039 (setq s1 (match-string 1 string)
15040 s2 (match-string 2 string))
15041 (setq s1 "" s2 string))
15042 (cond
15043 ((eq flag nil)
15044 ;; try completion
15045 (setq rtn (try-completion s2 ctable confirm))
15046 (when (stringp rtn)
15047 (setq rtn
15048 (concat s1 s2 (substring rtn (length s2))
15049 (if (and org-add-colon-after-tag-completion
15050 (assoc rtn ctable))
15051 ":" ""))))
15052 rtn)
15053 ((eq flag t)
15054 ;; all-completions
15055 (all-completions s2 ctable confirm))
15056 ((eq flag 'lambda)
15057 ;; exact match?
15058 (assoc s2 ctable)))))
15060 (defun org-fast-tag-insert (kwd tags face &optional end)
15061 "Insert KDW, and the TAGS, the latter with face FACE.
15062 Also insert END."
15063 (insert (format "%-12s" (concat kwd ":"))
15064 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
15065 (or end "")))
15067 (defun org-fast-tag-show-exit (flag)
15068 (save-excursion
15069 (org-goto-line 3)
15070 (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
15071 (replace-match ""))
15072 (when flag
15073 (end-of-line 1)
15074 (org-move-to-column (- (window-width) 19) t)
15075 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
15077 (defun org-set-current-tags-overlay (current prefix)
15078 "Add an overlay to CURRENT tag with PREFIX."
15079 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
15080 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
15081 (org-overlay-display org-tags-overlay (concat prefix s))))
15083 (defvar org-last-tag-selection-key nil)
15084 (defun org-fast-tag-selection (current inherited table &optional todo-table)
15085 "Fast tag selection with single keys.
15086 CURRENT is the current list of tags in the headline, INHERITED is the
15087 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
15088 possibly with grouping information. TODO-TABLE is a similar table with
15089 TODO keywords, should these have keys assigned to them.
15090 If the keys are nil, a-z are automatically assigned.
15091 Returns the new tags string, or nil to not change the current settings."
15092 (let* ((fulltable (append table todo-table))
15093 (maxlen (apply 'max (mapcar
15094 (lambda (x)
15095 (if (stringp (car x)) (string-width (car x)) 0))
15096 fulltable)))
15097 (buf (current-buffer))
15098 (expert (eq org-fast-tag-selection-single-key 'expert))
15099 (buffer-tags nil)
15100 (fwidth (+ maxlen 3 1 3))
15101 (ncol (/ (- (window-width) 4) fwidth))
15102 (i-face 'org-done)
15103 (c-face 'org-todo)
15104 tg cnt e c char c1 c2 ntable tbl rtn
15105 ov-start ov-end ov-prefix
15106 (exit-after-next org-fast-tag-selection-single-key)
15107 (done-keywords org-done-keywords)
15108 groups ingroup intaggroup)
15109 (save-excursion
15110 (beginning-of-line 1)
15111 (if (looking-at ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
15112 (setq ov-start (match-beginning 1)
15113 ov-end (match-end 1)
15114 ov-prefix "")
15115 (setq ov-start (1- (point-at-eol))
15116 ov-end (1+ ov-start))
15117 (skip-chars-forward "^\n\r")
15118 (setq ov-prefix
15119 (concat
15120 (buffer-substring (1- (point)) (point))
15121 (if (> (current-column) org-tags-column)
15123 (make-string (- org-tags-column (current-column)) ?\ ))))))
15124 (move-overlay org-tags-overlay ov-start ov-end)
15125 (save-window-excursion
15126 (if expert
15127 (set-buffer (get-buffer-create " *Org tags*"))
15128 (delete-other-windows)
15129 (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
15130 (org-switch-to-buffer-other-window " *Org tags*"))
15131 (erase-buffer)
15132 (setq-local org-done-keywords done-keywords)
15133 (org-fast-tag-insert "Inherited" inherited i-face "\n")
15134 (org-fast-tag-insert "Current" current c-face "\n\n")
15135 (org-fast-tag-show-exit exit-after-next)
15136 (org-set-current-tags-overlay current ov-prefix)
15137 (setq tbl fulltable char ?a cnt 0)
15138 (while (setq e (pop tbl))
15139 (cond
15140 ((eq (car e) :startgroup)
15141 (push '() groups) (setq ingroup t)
15142 (unless (zerop cnt)
15143 (setq cnt 0)
15144 (insert "\n"))
15145 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
15146 ((eq (car e) :endgroup)
15147 (setq ingroup nil cnt 0)
15148 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
15149 ((eq (car e) :startgrouptag)
15150 (setq intaggroup t)
15151 (unless (zerop cnt)
15152 (setq cnt 0)
15153 (insert "\n"))
15154 (insert "[ "))
15155 ((eq (car e) :endgrouptag)
15156 (setq intaggroup nil cnt 0)
15157 (insert "]\n"))
15158 ((equal e '(:newline))
15159 (unless (zerop cnt)
15160 (setq cnt 0)
15161 (insert "\n")
15162 (setq e (car tbl))
15163 (while (equal (car tbl) '(:newline))
15164 (insert "\n")
15165 (setq tbl (cdr tbl)))))
15166 ((equal e '(:grouptags)) (insert " : "))
15168 (setq tg (copy-sequence (car e)) c2 nil)
15169 (if (cdr e)
15170 (setq c (cdr e))
15171 ;; automatically assign a character.
15172 (setq c1 (string-to-char
15173 (downcase (substring
15174 tg (if (= (string-to-char tg) ?@) 1 0)))))
15175 (if (or (rassoc c1 ntable) (rassoc c1 table))
15176 (while (or (rassoc char ntable) (rassoc char table))
15177 (setq char (1+ char)))
15178 (setq c2 c1))
15179 (setq c (or c2 char)))
15180 (when ingroup (push tg (car groups)))
15181 (setq tg (org-add-props tg nil 'face
15182 (cond
15183 ((not (assoc tg table))
15184 (org-get-todo-face tg))
15185 ((member tg current) c-face)
15186 ((member tg inherited) i-face))))
15187 (when (equal (caar tbl) :grouptags)
15188 (org-add-props tg nil 'face 'org-tag-group))
15189 (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
15190 (insert "[" c "] " tg (make-string
15191 (- fwidth 4 (length tg)) ?\ ))
15192 (push (cons tg c) ntable)
15193 (when (= (cl-incf cnt) ncol)
15194 (insert "\n")
15195 (when (or ingroup intaggroup) (insert " "))
15196 (setq cnt 0)))))
15197 (setq ntable (nreverse ntable))
15198 (insert "\n")
15199 (goto-char (point-min))
15200 (unless expert (org-fit-window-to-buffer))
15201 (setq rtn
15202 (catch 'exit
15203 (while t
15204 (message "[a-z..]:toggle [SPC]:clear [RET]:accept [TAB]:edit [!] %sgroups%s"
15205 (if (not groups) "no " "")
15206 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
15207 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
15208 (setq org-last-tag-selection-key c)
15209 (cond
15210 ((= c ?\r) (throw 'exit t))
15211 ((= c ?!)
15212 (setq groups (not groups))
15213 (goto-char (point-min))
15214 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
15215 ((= c ?\C-c)
15216 (if (not expert)
15217 (org-fast-tag-show-exit
15218 (setq exit-after-next (not exit-after-next)))
15219 (setq expert nil)
15220 (delete-other-windows)
15221 (set-window-buffer (split-window-vertically) " *Org tags*")
15222 (org-switch-to-buffer-other-window " *Org tags*")
15223 (org-fit-window-to-buffer)))
15224 ((or (= c ?\C-g)
15225 (and (= c ?q) (not (rassoc c ntable))))
15226 (delete-overlay org-tags-overlay)
15227 (setq quit-flag t))
15228 ((= c ?\ )
15229 (setq current nil)
15230 (when exit-after-next (setq exit-after-next 'now)))
15231 ((= c ?\t)
15232 (condition-case nil
15233 (setq tg (completing-read
15234 "Tag: "
15235 (or buffer-tags
15236 (with-current-buffer buf
15237 (setq buffer-tags
15238 (org-get-buffer-tags))))))
15239 (quit (setq tg "")))
15240 (when (string-match "\\S-" tg)
15241 (cl-pushnew (list tg) buffer-tags :test #'equal)
15242 (if (member tg current)
15243 (setq current (delete tg current))
15244 (push tg current)))
15245 (when exit-after-next (setq exit-after-next 'now)))
15246 ((setq e (rassoc c todo-table) tg (car e))
15247 (with-current-buffer buf
15248 (save-excursion (org-todo tg)))
15249 (when exit-after-next (setq exit-after-next 'now)))
15250 ((setq e (rassoc c ntable) tg (car e))
15251 (if (member tg current)
15252 (setq current (delete tg current))
15253 (cl-loop for g in groups do
15254 (when (member tg g)
15255 (dolist (x g) (setq current (delete x current)))))
15256 (push tg current))
15257 (when exit-after-next (setq exit-after-next 'now))))
15259 ;; Create a sorted list
15260 (setq current
15261 (sort current
15262 (lambda (a b)
15263 (assoc b (cdr (memq (assoc a ntable) ntable))))))
15264 (when (eq exit-after-next 'now) (throw 'exit t))
15265 (goto-char (point-min))
15266 (beginning-of-line 2)
15267 (delete-region (point) (point-at-eol))
15268 (org-fast-tag-insert "Current" current c-face)
15269 (org-set-current-tags-overlay current ov-prefix)
15270 (while (re-search-forward "\\[.\\] \\([[:alnum:]_@#%]+\\)" nil t)
15271 (setq tg (match-string 1))
15272 (add-text-properties
15273 (match-beginning 1) (match-end 1)
15274 (list 'face
15275 (cond
15276 ((member tg current) c-face)
15277 ((member tg inherited) i-face)
15278 (t (get-text-property (match-beginning 1) 'face))))))
15279 (goto-char (point-min)))))
15280 (delete-overlay org-tags-overlay)
15281 (if rtn
15282 (mapconcat 'identity current ":")
15283 nil))))
15285 (defun org-get-tags-string ()
15286 "Get the TAGS string in the current headline."
15287 (unless (org-at-heading-p t)
15288 (user-error "Not on a heading"))
15289 (save-excursion
15290 (beginning-of-line 1)
15291 (if (looking-at ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
15292 (match-string-no-properties 1)
15293 "")))
15295 (defun org-get-tags ()
15296 "Get the list of tags specified in the current headline."
15297 (org-split-string (org-get-tags-string) ":"))
15299 (defun org-get-buffer-tags ()
15300 "Get a table of all tags used in the buffer, for completion."
15301 (org-with-wide-buffer
15302 (goto-char (point-min))
15303 (let ((tag-re (concat org-outline-regexp-bol
15304 "\\(?:.*?[ \t]\\)?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
15305 tags)
15306 (while (re-search-forward tag-re nil t)
15307 (dolist (tag (org-split-string (match-string-no-properties 1) ":"))
15308 (push tag tags)))
15309 (mapcar #'list (append org-file-tags (org-uniquify tags))))))
15311 ;;;; The mapping API
15313 (defvar org-agenda-skip-comment-trees)
15314 (defvar org-agenda-skip-function)
15315 (defun org-map-entries (func &optional match scope &rest skip)
15316 "Call FUNC at each headline selected by MATCH in SCOPE.
15318 FUNC is a function or a lisp form. The function will be called without
15319 arguments, with the cursor positioned at the beginning of the headline.
15320 The return values of all calls to the function will be collected and
15321 returned as a list.
15323 The call to FUNC will be wrapped into a save-excursion form, so FUNC
15324 does not need to preserve point. After evaluation, the cursor will be
15325 moved to the end of the line (presumably of the headline of the
15326 processed entry) and search continues from there. Under some
15327 circumstances, this may not produce the wanted results. For example,
15328 if you have removed (e.g. archived) the current (sub)tree it could
15329 mean that the next entry will be skipped entirely. In such cases, you
15330 can specify the position from where search should continue by making
15331 FUNC set the variable `org-map-continue-from' to the desired buffer
15332 position.
15334 MATCH is a tags/property/todo match as it is used in the agenda tags view.
15335 Only headlines that are matched by this query will be considered during
15336 the iteration. When MATCH is nil or t, all headlines will be
15337 visited by the iteration.
15339 SCOPE determines the scope of this command. It can be any of:
15341 nil The current buffer, respecting the restriction if any
15342 tree The subtree started with the entry at point
15343 region The entries within the active region, if any
15344 region-start-level
15345 The entries within the active region, but only those at
15346 the same level than the first one.
15347 file The current buffer, without restriction
15348 file-with-archives
15349 The current buffer, and any archives associated with it
15350 agenda All agenda files
15351 agenda-with-archives
15352 All agenda files with any archive files associated with them
15353 \(file1 file2 ...)
15354 If this is a list, all files in the list will be scanned
15356 The remaining args are treated as settings for the skipping facilities of
15357 the scanner. The following items can be given here:
15359 archive skip trees with the archive tag
15360 comment skip trees with the COMMENT keyword
15361 function or Emacs Lisp form:
15362 will be used as value for `org-agenda-skip-function', so
15363 whenever the function returns a position, FUNC will not be
15364 called for that entry and search will continue from the
15365 position returned
15367 If your function needs to retrieve the tags including inherited tags
15368 at the *current* entry, you can use the value of the variable
15369 `org-scanner-tags' which will be much faster than getting the value
15370 with `org-get-tags-at'. If your function gets properties with
15371 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
15372 to t around the call to `org-entry-properties' to get the same speedup.
15373 Note that if your function moves around to retrieve tags and properties at
15374 a *different* entry, you cannot use these techniques."
15375 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
15376 (not (org-region-active-p)))
15377 (let* ((org-agenda-archives-mode nil) ; just to make sure
15378 (org-agenda-skip-archived-trees (memq 'archive skip))
15379 (org-agenda-skip-comment-trees (memq 'comment skip))
15380 (org-agenda-skip-function
15381 (car (org-delete-all '(comment archive) skip)))
15382 (org-tags-match-list-sublevels t)
15383 (start-level (eq scope 'region-start-level))
15384 matcher res
15385 org-todo-keywords-for-agenda
15386 org-done-keywords-for-agenda
15387 org-todo-keyword-alist-for-agenda
15388 org-tag-alist-for-agenda
15389 org--matcher-tags-todo-only)
15391 (cond
15392 ((eq match t) (setq matcher t))
15393 ((eq match nil) (setq matcher t))
15394 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
15396 (save-excursion
15397 (save-restriction
15398 (cond ((eq scope 'tree)
15399 (org-back-to-heading t)
15400 (org-narrow-to-subtree)
15401 (setq scope nil))
15402 ((and (or (eq scope 'region) (eq scope 'region-start-level))
15403 (org-region-active-p))
15404 ;; If needed, set start-level to a string like "2"
15405 (when start-level
15406 (save-excursion
15407 (goto-char (region-beginning))
15408 (unless (org-at-heading-p) (outline-next-heading))
15409 (setq start-level (org-current-level))))
15410 (narrow-to-region (region-beginning)
15411 (save-excursion
15412 (goto-char (region-end))
15413 (unless (and (bolp) (org-at-heading-p))
15414 (outline-next-heading))
15415 (point)))
15416 (setq scope nil)))
15418 (if (not scope)
15419 (progn
15420 (org-agenda-prepare-buffers
15421 (and buffer-file-name (list buffer-file-name)))
15422 (setq res
15423 (org-scan-tags
15424 func matcher org--matcher-tags-todo-only start-level)))
15425 ;; Get the right scope
15426 (cond
15427 ((and scope (listp scope) (symbolp (car scope)))
15428 (setq scope (eval scope)))
15429 ((eq scope 'agenda)
15430 (setq scope (org-agenda-files t)))
15431 ((eq scope 'agenda-with-archives)
15432 (setq scope (org-agenda-files t))
15433 (setq scope (org-add-archive-files scope)))
15434 ((eq scope 'file)
15435 (setq scope (and buffer-file-name (list buffer-file-name))))
15436 ((eq scope 'file-with-archives)
15437 (setq scope (org-add-archive-files (list (buffer-file-name))))))
15438 (org-agenda-prepare-buffers scope)
15439 (dolist (file scope)
15440 (with-current-buffer (org-find-base-buffer-visiting file)
15441 (org-with-wide-buffer
15442 (goto-char (point-min))
15443 (setq res
15444 (append
15446 (org-scan-tags
15447 func matcher org--matcher-tags-todo-only)))))))))
15448 res)))
15450 ;;; Properties API
15452 (defconst org-special-properties
15453 '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
15454 "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
15455 "The special properties valid in Org mode.
15456 These are properties that are not defined in the property drawer,
15457 but in some other way.")
15459 (defconst org-default-properties
15460 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
15461 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
15462 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
15463 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
15464 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
15465 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
15466 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
15467 "Some properties that are used by Org mode for various purposes.
15468 Being in this list makes sure that they are offered for completion.")
15470 (defun org--valid-property-p (property)
15471 "Non nil when string PROPERTY is a valid property name."
15472 (not
15473 (or (equal property "")
15474 (string-match-p "\\s-" property))))
15476 (defun org--update-property-plist (key val props)
15477 "Associate KEY to VAL in alist PROPS.
15478 Modifications are made by side-effect. Return new alist."
15479 (let* ((appending (string= (substring key -1) "+"))
15480 (key (if appending (substring key 0 -1) key))
15481 (old (assoc-string key props t)))
15482 (if (not old) (cons (cons key val) props)
15483 (setcdr old (if appending (concat (cdr old) " " val) val))
15484 props)))
15486 (defun org-get-property-block (&optional beg force)
15487 "Return the (beg . end) range of the body of the property drawer.
15488 BEG is the beginning of the current subtree, or of the part
15489 before the first headline. If it is not given, it will be found.
15490 If the drawer does not exist, create it if FORCE is non-nil, or
15491 return nil."
15492 (org-with-wide-buffer
15493 (when beg (goto-char beg))
15494 (unless (org-before-first-heading-p)
15495 (let ((beg (cond (beg)
15496 ((or (not (featurep 'org-inlinetask))
15497 (org-inlinetask-in-task-p))
15498 (org-back-to-heading t))
15499 (t (org-with-limited-levels (org-back-to-heading t))))))
15500 (forward-line)
15501 (when (looking-at-p org-planning-line-re) (forward-line))
15502 (cond ((looking-at org-property-drawer-re)
15503 (forward-line)
15504 (cons (point) (progn (goto-char (match-end 0))
15505 (line-beginning-position))))
15506 (force
15507 (goto-char beg)
15508 (org-insert-property-drawer)
15509 (let ((pos (save-excursion (search-forward ":END:")
15510 (line-beginning-position))))
15511 (cons pos pos))))))))
15513 (defun org-at-property-p ()
15514 "Non-nil when point is inside a property drawer.
15515 See `org-property-re' for match data, if applicable."
15516 (save-excursion
15517 (beginning-of-line)
15518 (and (looking-at org-property-re)
15519 (let ((property-drawer (save-match-data (org-get-property-block))))
15520 (and property-drawer
15521 (>= (point) (car property-drawer))
15522 (< (point) (cdr property-drawer)))))))
15524 (defun org-property-action ()
15525 "Do an action on properties."
15526 (interactive)
15527 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
15528 (let ((c (read-char-exclusive)))
15529 (cl-case c
15530 (?s (call-interactively #'org-set-property))
15531 (?d (call-interactively #'org-delete-property))
15532 (?D (call-interactively #'org-delete-property-globally))
15533 (?c (call-interactively #'org-compute-property-at-point))
15534 (otherwise (user-error "No such property action %c" c)))))
15536 (defun org-inc-effort ()
15537 "Increment the value of the effort property in the current entry."
15538 (interactive)
15539 (org-set-effort nil t))
15541 (defvar org-clock-effort) ; Defined in org-clock.el.
15542 (defvar org-clock-current-task) ; Defined in org-clock.el.
15543 (defun org-set-effort (&optional value increment)
15544 "Set the effort property of the current entry.
15545 With numerical prefix arg, use the nth allowed value, 0 stands for the
15546 10th allowed value.
15548 When INCREMENT is non-nil, set the property to the next allowed value."
15549 (interactive "P")
15550 (when (equal value 0) (setq value 10))
15551 (let* ((completion-ignore-case t)
15552 (prop org-effort-property)
15553 (cur (org-entry-get nil prop))
15554 (allowed (org-property-get-allowed-values nil prop 'table))
15555 (existing (mapcar 'list (org-property-values prop)))
15556 (heading (nth 4 (org-heading-components)))
15558 (val (cond
15559 ((stringp value) value)
15560 ((and allowed (integerp value))
15561 (or (car (nth (1- value) allowed))
15562 (car (org-last allowed))))
15563 ((and allowed increment)
15564 (or (cl-caadr (member (list cur) allowed))
15565 (user-error "Allowed effort values are not set")))
15566 (allowed
15567 (message "Select 1-9,0, [RET%s]: %s"
15568 (if cur (concat "=" cur) "")
15569 (mapconcat 'car allowed " "))
15570 (setq rpl (read-char-exclusive))
15571 (if (equal rpl ?\r)
15573 (setq rpl (- rpl ?0))
15574 (when (equal rpl 0) (setq rpl 10))
15575 (if (and (> rpl 0) (<= rpl (length allowed)))
15576 (car (nth (1- rpl) allowed))
15577 (org-completing-read "Effort: " allowed nil))))
15579 (org-completing-read
15580 (concat "Effort" (and cur (string-match "\\S-" cur)
15581 (concat " [" cur "]"))
15582 ": ")
15583 existing nil nil "" nil cur)))))
15584 (unless (equal (org-entry-get nil prop) val)
15585 (org-entry-put nil prop val))
15586 (org-refresh-property
15587 '((effort . identity)
15588 (effort-minutes . org-duration-to-minutes))
15589 val)
15590 (when (equal heading (bound-and-true-p org-clock-current-task))
15591 (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
15592 (org-clock-update-mode-line))
15593 (message "%s is now %s" prop val)))
15595 (defun org-entry-properties (&optional pom which)
15596 "Get all properties of the current entry.
15598 When POM is a buffer position, get all properties from the entry
15599 there instead.
15601 This includes the TODO keyword, the tags, time strings for
15602 deadline, scheduled, and clocking, and any additional properties
15603 defined in the entry.
15605 If WHICH is nil or `all', get all properties. If WHICH is
15606 `special' or `standard', only get that subclass. If WHICH is
15607 a string, only get that property.
15609 Return value is an alist. Keys are properties, as upcased
15610 strings."
15611 (org-with-point-at pom
15612 (when (and (derived-mode-p 'org-mode)
15613 (ignore-errors (org-back-to-heading t)))
15614 (catch 'exit
15615 (let* ((beg (point))
15616 (specific (and (stringp which) (upcase which)))
15617 (which (cond ((not specific) which)
15618 ((member specific org-special-properties) 'special)
15619 (t 'standard)))
15620 props)
15621 ;; Get the special properties, like TODO and TAGS.
15622 (when (memq which '(nil all special))
15623 (when (or (not specific) (string= specific "CLOCKSUM"))
15624 (let ((clocksum (get-text-property (point) :org-clock-minutes)))
15625 (when clocksum
15626 (push (cons "CLOCKSUM" (org-duration-from-minutes clocksum))
15627 props)))
15628 (when specific (throw 'exit props)))
15629 (when (or (not specific) (string= specific "CLOCKSUM_T"))
15630 (let ((clocksumt (get-text-property (point)
15631 :org-clock-minutes-today)))
15632 (when clocksumt
15633 (push (cons "CLOCKSUM_T"
15634 (org-duration-from-minutes clocksumt))
15635 props)))
15636 (when specific (throw 'exit props)))
15637 (when (or (not specific) (string= specific "ITEM"))
15638 (let ((case-fold-search nil))
15639 (when (looking-at org-complex-heading-regexp)
15640 (push (cons "ITEM"
15641 (let ((title (match-string-no-properties 4)))
15642 (if (org-string-nw-p title)
15643 (org-remove-tabs title)
15644 "")))
15645 props)))
15646 (when specific (throw 'exit props)))
15647 (when (or (not specific) (string= specific "TODO"))
15648 (let ((case-fold-search nil))
15649 (when (and (looking-at org-todo-line-regexp) (match-end 2))
15650 (push (cons "TODO" (match-string-no-properties 2)) props)))
15651 (when specific (throw 'exit props)))
15652 (when (or (not specific) (string= specific "PRIORITY"))
15653 (push (cons "PRIORITY"
15654 (if (looking-at org-priority-regexp)
15655 (match-string-no-properties 2)
15656 (char-to-string org-default-priority)))
15657 props)
15658 (when specific (throw 'exit props)))
15659 (when (or (not specific) (string= specific "FILE"))
15660 (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
15661 props)
15662 (when specific (throw 'exit props)))
15663 (when (or (not specific) (string= specific "TAGS"))
15664 (let ((value (org-string-nw-p (org-get-tags-string))))
15665 (when value (push (cons "TAGS" value) props)))
15666 (when specific (throw 'exit props)))
15667 (when (or (not specific) (string= specific "ALLTAGS"))
15668 (let ((value (org-get-tags-at)))
15669 (when value
15670 (push (cons "ALLTAGS"
15671 (format ":%s:" (mapconcat #'identity value ":")))
15672 props)))
15673 (when specific (throw 'exit props)))
15674 (when (or (not specific) (string= specific "BLOCKED"))
15675 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
15676 (when specific (throw 'exit props)))
15677 (when (or (not specific)
15678 (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
15679 (forward-line)
15680 (when (looking-at-p org-planning-line-re)
15681 (end-of-line)
15682 (let ((bol (line-beginning-position))
15683 ;; Backward compatibility: time keywords used to
15684 ;; be configurable (before 8.3). Make sure we
15685 ;; get the correct keyword.
15686 (key-assoc `(("CLOSED" . ,org-closed-string)
15687 ("DEADLINE" . ,org-deadline-string)
15688 ("SCHEDULED" . ,org-scheduled-string))))
15689 (dolist (pair (if specific (list (assoc specific key-assoc))
15690 key-assoc))
15691 (save-excursion
15692 (when (search-backward (cdr pair) bol t)
15693 (goto-char (match-end 0))
15694 (skip-chars-forward " \t")
15695 (and (looking-at org-ts-regexp-both)
15696 (push (cons (car pair)
15697 (match-string-no-properties 0))
15698 props)))))))
15699 (when specific (throw 'exit props)))
15700 (when (or (not specific)
15701 (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
15702 (let ((find-ts
15703 (lambda (end ts)
15704 ;; Fix next time-stamp before END. TS is the
15705 ;; list of time-stamps found so far.
15706 (let ((ts ts)
15707 (regexp (cond
15708 ((string= specific "TIMESTAMP")
15709 org-ts-regexp)
15710 ((string= specific "TIMESTAMP_IA")
15711 org-ts-regexp-inactive)
15712 ((assoc "TIMESTAMP_IA" ts)
15713 org-ts-regexp)
15714 ((assoc "TIMESTAMP" ts)
15715 org-ts-regexp-inactive)
15716 (t org-ts-regexp-both))))
15717 (catch 'next
15718 (while (re-search-forward regexp end t)
15719 (backward-char)
15720 (let ((object (org-element-context)))
15721 ;; Accept to match timestamps in node
15722 ;; properties, too.
15723 (when (memq (org-element-type object)
15724 '(node-property timestamp))
15725 (let ((type
15726 (org-element-property :type object)))
15727 (cond
15728 ((and (memq type '(active active-range))
15729 (not (equal specific "TIMESTAMP_IA")))
15730 (unless (assoc "TIMESTAMP" ts)
15731 (push (cons "TIMESTAMP"
15732 (org-element-property
15733 :raw-value object))
15735 (when specific (throw 'exit ts))))
15736 ((and (memq type '(inactive inactive-range))
15737 (not (string= specific "TIMESTAMP")))
15738 (unless (assoc "TIMESTAMP_IA" ts)
15739 (push (cons "TIMESTAMP_IA"
15740 (org-element-property
15741 :raw-value object))
15743 (when specific (throw 'exit ts))))))
15744 ;; Both timestamp types are found,
15745 ;; move to next part.
15746 (when (= (length ts) 2) (throw 'next ts)))))
15747 ts)))))
15748 (goto-char beg)
15749 ;; First look for timestamps within headline.
15750 (let ((ts (funcall find-ts (line-end-position) nil)))
15751 (if (= (length ts) 2) (setq props (nconc ts props))
15752 ;; Then find timestamps in the section, skipping
15753 ;; planning line.
15754 (let ((end (save-excursion (outline-next-heading))))
15755 (forward-line)
15756 (when (looking-at-p org-planning-line-re) (forward-line))
15757 (setq props (nconc (funcall find-ts end ts) props))))))))
15758 ;; Get the standard properties, like :PROP:.
15759 (when (memq which '(nil all standard))
15760 ;; If we are looking after a specific property, delegate
15761 ;; to `org-entry-get', which is faster. However, make an
15762 ;; exception for "CATEGORY", since it can be also set
15763 ;; through keywords (i.e. #+CATEGORY).
15764 (if (and specific (not (equal specific "CATEGORY")))
15765 (let ((value (org-entry-get beg specific nil t)))
15766 (throw 'exit (and value (list (cons specific value)))))
15767 (let ((range (org-get-property-block beg)))
15768 (when range
15769 (let ((end (cdr range)) seen-base)
15770 (goto-char (car range))
15771 ;; Unlike to `org--update-property-plist', we
15772 ;; handle the case where base values is found
15773 ;; after its extension. We also forbid standard
15774 ;; properties to be named as special properties.
15775 (while (re-search-forward org-property-re end t)
15776 (let* ((key (upcase (match-string-no-properties 2)))
15777 (extendp (string-match-p "\\+\\'" key))
15778 (key-base (if extendp (substring key 0 -1) key))
15779 (value (match-string-no-properties 3)))
15780 (cond
15781 ((member-ignore-case key-base org-special-properties))
15782 (extendp
15783 (setq props
15784 (org--update-property-plist key value props)))
15785 ((member key seen-base))
15786 (t (push key seen-base)
15787 (let ((p (assoc-string key props t)))
15788 (if p (setcdr p (concat value " " (cdr p)))
15789 (push (cons key value) props))))))))))))
15790 (unless (assoc "CATEGORY" props)
15791 (push (cons "CATEGORY" (org-get-category beg)) props)
15792 (when (string= specific "CATEGORY") (throw 'exit props)))
15793 ;; Return value.
15794 props)))))
15796 (defun org--property-local-values (property literal-nil)
15797 "Return value for PROPERTY in current entry.
15798 Value is a list whose car is the base value for PROPERTY and cdr
15799 a list of accumulated values. Return nil if neither is found in
15800 the entry. Also return nil when PROPERTY is set to \"nil\",
15801 unless LITERAL-NIL is non-nil."
15802 (let ((range (org-get-property-block)))
15803 (when range
15804 (goto-char (car range))
15805 (let* ((case-fold-search t)
15806 (end (cdr range))
15807 (value
15808 ;; Base value.
15809 (save-excursion
15810 (let ((v (and (re-search-forward
15811 (org-re-property property nil t) end t)
15812 (match-string-no-properties 3))))
15813 (list (if literal-nil v (org-not-nil v)))))))
15814 ;; Find additional values.
15815 (let* ((property+ (org-re-property (concat property "+") nil t)))
15816 (while (re-search-forward property+ end t)
15817 (push (match-string-no-properties 3) value)))
15818 ;; Return final values.
15819 (and (not (equal value '(nil))) (nreverse value))))))
15821 (defun org--property-global-value (property literal-nil)
15822 "Return value for PROPERTY in current buffer.
15823 Return value is a string. Return nil if property is not set
15824 globally. Also return nil when PROPERTY is set to \"nil\",
15825 unless LITERAL-NIL is non-nil."
15826 (let ((global
15827 (cdr (or (assoc-string property org-file-properties t)
15828 (assoc-string property org-global-properties t)
15829 (assoc-string property org-global-properties-fixed t)))))
15830 (if literal-nil global (org-not-nil global))))
15832 (defun org-entry-get (pom property &optional inherit literal-nil)
15833 "Get value of PROPERTY for entry or content at point-or-marker POM.
15835 If INHERIT is non-nil and the entry does not have the property,
15836 then also check higher levels of the hierarchy. If INHERIT is
15837 the symbol `selective', use inheritance only if the setting in
15838 `org-use-property-inheritance' selects PROPERTY for inheritance.
15840 If the property is present but empty, the return value is the
15841 empty string. If the property is not present at all, nil is
15842 returned. In any other case, return the value as a string.
15843 Search is case-insensitive.
15845 If LITERAL-NIL is set, return the string value \"nil\" as
15846 a string, do not interpret it as the list atom nil. This is used
15847 for inheritance when a \"nil\" value can supersede a non-nil
15848 value higher up the hierarchy."
15849 (org-with-point-at pom
15850 (cond
15851 ((member-ignore-case property (cons "CATEGORY" org-special-properties))
15852 ;; We need a special property. Use `org-entry-properties' to
15853 ;; retrieve it, but specify the wanted property.
15854 (cdr (assoc-string property (org-entry-properties nil property))))
15855 ((and inherit
15856 (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
15857 (org-entry-get-with-inheritance property literal-nil))
15859 (let* ((local (org--property-local-values property literal-nil))
15860 (value (and local (mapconcat #'identity (delq nil local) " "))))
15861 (if literal-nil value (org-not-nil value)))))))
15863 (defun org-property-or-variable-value (var &optional inherit)
15864 "Check if there is a property fixing the value of VAR.
15865 If yes, return this value. If not, return the current value of the variable."
15866 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
15867 (if (and prop (stringp prop) (string-match "\\S-" prop))
15868 (read prop)
15869 (symbol-value var))))
15871 (defun org-entry-delete (pom property)
15872 "Delete PROPERTY from entry at point-or-marker POM.
15873 Accumulated properties, i.e. PROPERTY+, are also removed. Return
15874 non-nil when a property was removed."
15875 (org-with-point-at pom
15876 (pcase (org-get-property-block)
15877 (`(,begin . ,origin)
15878 (let* ((end (copy-marker origin))
15879 (re (org-re-property
15880 (concat (regexp-quote property) "\\+?") t t)))
15881 (goto-char begin)
15882 (while (re-search-forward re end t)
15883 (delete-region (match-beginning 0) (line-beginning-position 2)))
15884 ;; If drawer is empty, remove it altogether.
15885 (when (= begin end)
15886 (delete-region (line-beginning-position 0)
15887 (line-beginning-position 2)))
15888 ;; Return non-nil if some property was removed.
15889 (prog1 (/= end origin) (set-marker end nil))))
15890 (_ nil))))
15892 ;; Multi-values properties are properties that contain multiple values
15893 ;; These values are assumed to be single words, separated by whitespace.
15894 (defun org-entry-add-to-multivalued-property (pom property value)
15895 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
15896 (let* ((old (org-entry-get pom property))
15897 (values (and old (split-string old))))
15898 (setq value (org-entry-protect-space value))
15899 (unless (member value values)
15900 (setq values (append values (list value)))
15901 (org-entry-put pom property (mapconcat #'identity values " ")))))
15903 (defun org-entry-remove-from-multivalued-property (pom property value)
15904 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
15905 (let* ((old (org-entry-get pom property))
15906 (values (and old (split-string old))))
15907 (setq value (org-entry-protect-space value))
15908 (when (member value values)
15909 (setq values (delete value values))
15910 (org-entry-put pom property (mapconcat #'identity values " ")))))
15912 (defun org-entry-member-in-multivalued-property (pom property value)
15913 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
15914 (let* ((old (org-entry-get pom property))
15915 (values (and old (split-string old))))
15916 (setq value (org-entry-protect-space value))
15917 (member value values)))
15919 (defun org-entry-get-multivalued-property (pom property)
15920 "Return a list of values in a multivalued property."
15921 (let* ((value (org-entry-get pom property))
15922 (values (and value (split-string value))))
15923 (mapcar #'org-entry-restore-space values)))
15925 (defun org-entry-put-multivalued-property (pom property &rest values)
15926 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
15927 VALUES should be a list of strings. Spaces will be protected."
15928 (org-entry-put pom property (mapconcat #'org-entry-protect-space values " "))
15929 (let* ((value (org-entry-get pom property))
15930 (values (and value (split-string value))))
15931 (mapcar #'org-entry-restore-space values)))
15933 (defun org-entry-protect-space (s)
15934 "Protect spaces and newline in string S."
15935 (while (string-match " " s)
15936 (setq s (replace-match "%20" t t s)))
15937 (while (string-match "\n" s)
15938 (setq s (replace-match "%0A" t t s)))
15941 (defun org-entry-restore-space (s)
15942 "Restore spaces and newline in string S."
15943 (while (string-match "%20" s)
15944 (setq s (replace-match " " t t s)))
15945 (while (string-match "%0A" s)
15946 (setq s (replace-match "\n" t t s)))
15949 (defvar org-entry-property-inherited-from (make-marker)
15950 "Marker pointing to the entry from where a property was inherited.
15951 Each call to `org-entry-get-with-inheritance' will set this marker to the
15952 location of the entry where the inheritance search matched. If there was
15953 no match, the marker will point nowhere.
15954 Note that also `org-entry-get' calls this function, if the INHERIT flag
15955 is set.")
15957 (defun org-entry-get-with-inheritance (property &optional literal-nil)
15958 "Get PROPERTY of entry or content at point, search higher levels if needed.
15959 The search will stop at the first ancestor which has the property defined.
15960 If the value found is \"nil\", return nil to show that the property
15961 should be considered as undefined (this is the meaning of nil here).
15962 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
15963 (move-marker org-entry-property-inherited-from nil)
15964 (org-with-wide-buffer
15965 (let (value)
15966 (catch 'exit
15967 (while t
15968 (let ((v (org--property-local-values property literal-nil)))
15969 (when v
15970 (setq value
15971 (concat (mapconcat #'identity (delq nil v) " ")
15972 (and value " ")
15973 value)))
15974 (cond
15975 ((car v)
15976 (org-back-to-heading t)
15977 (move-marker org-entry-property-inherited-from (point))
15978 (throw 'exit nil))
15979 ((org-up-heading-safe))
15981 (let ((global (org--property-global-value property literal-nil)))
15982 (cond ((not global))
15983 (value (setq value (concat global " " value)))
15984 (t (setq value global))))
15985 (throw 'exit nil))))))
15986 (if literal-nil value (org-not-nil value)))))
15988 (defvar org-property-changed-functions nil
15989 "Hook called when the value of a property has changed.
15990 Each hook function should accept two arguments, the name of the property
15991 and the new value.")
15993 (defun org-entry-put (pom property value)
15994 "Set PROPERTY to VALUE for entry at point-or-marker POM.
15996 If the value is nil, it is converted to the empty string. If it
15997 is not a string, an error is raised. Also raise an error on
15998 invalid property names.
16000 PROPERTY can be any regular property (see
16001 `org-special-properties'). It can also be \"TODO\",
16002 \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
16004 For the last two properties, VALUE may have any of the special
16005 values \"earlier\" and \"later\". The function then increases or
16006 decreases scheduled or deadline date by one day."
16007 (cond ((null value) (setq value ""))
16008 ((not (stringp value)) (error "Properties values should be strings"))
16009 ((not (org--valid-property-p property))
16010 (user-error "Invalid property name: \"%s\"" property)))
16011 (org-with-point-at pom
16012 (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
16013 (org-back-to-heading t)
16014 (org-with-limited-levels (org-back-to-heading t)))
16015 (let ((beg (point)))
16016 (cond
16017 ((equal property "TODO")
16018 (cond ((not (org-string-nw-p value)) (setq value 'none))
16019 ((not (member value org-todo-keywords-1))
16020 (user-error "\"%s\" is not a valid TODO state" value)))
16021 (org-todo value)
16022 (org-set-tags nil 'align))
16023 ((equal property "PRIORITY")
16024 (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
16025 (org-set-tags nil 'align))
16026 ((equal property "SCHEDULED")
16027 (forward-line)
16028 (if (and (looking-at-p org-planning-line-re)
16029 (re-search-forward
16030 org-scheduled-time-regexp (line-end-position) t))
16031 (cond ((string= value "earlier") (org-timestamp-change -1 'day))
16032 ((string= value "later") (org-timestamp-change 1 'day))
16033 ((string= value "") (org-schedule '(4)))
16034 (t (org-schedule nil value)))
16035 (if (member value '("earlier" "later" ""))
16036 (call-interactively #'org-schedule)
16037 (org-schedule nil value))))
16038 ((equal property "DEADLINE")
16039 (forward-line)
16040 (if (and (looking-at-p org-planning-line-re)
16041 (re-search-forward
16042 org-deadline-time-regexp (line-end-position) t))
16043 (cond ((string= value "earlier") (org-timestamp-change -1 'day))
16044 ((string= value "later") (org-timestamp-change 1 'day))
16045 ((string= value "") (org-deadline '(4)))
16046 (t (org-deadline nil value)))
16047 (if (member value '("earlier" "later" ""))
16048 (call-interactively #'org-deadline)
16049 (org-deadline nil value))))
16050 ((member property org-special-properties)
16051 (error "The %s property cannot be set with `org-entry-put'" property))
16053 (let* ((range (org-get-property-block beg 'force))
16054 (end (cdr range))
16055 (case-fold-search t))
16056 (goto-char (car range))
16057 (if (re-search-forward (org-re-property property nil t) end t)
16058 (progn (delete-region (match-beginning 0) (match-end 0))
16059 (goto-char (match-beginning 0)))
16060 (goto-char end)
16061 (insert "\n")
16062 (backward-char))
16063 (insert ":" property ":")
16064 (when value (insert " " value))
16065 (org-indent-line)))))
16066 (run-hook-with-args 'org-property-changed-functions property value)))
16068 (defun org-buffer-property-keys
16069 (&optional specials defaults columns ignore-malformed)
16070 "Get all property keys in the current buffer.
16072 When SPECIALS is non-nil, also list the special properties that
16073 reflect things like tags and TODO state.
16075 When DEFAULTS is non-nil, also include properties that has
16076 special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
16077 DESCRIPTION, LOCATION, and LOGGING and others.
16079 When COLUMNS in non-nil, also include property names given in
16080 COLUMN formats in the current buffer.
16082 When IGNORE-MALFORMED is non-nil, malformed drawer repair will not be
16083 automatically performed, such drawers will be silently ignored."
16084 (let ((case-fold-search t)
16085 (props (append
16086 (and specials org-special-properties)
16087 (and defaults (cons org-effort-property org-default-properties))
16088 nil)))
16089 (org-with-wide-buffer
16090 (goto-char (point-min))
16091 (while (re-search-forward org-property-start-re nil t)
16092 (let ((range (org-get-property-block)))
16093 (catch 'skip
16094 (unless range
16095 (when (and (not ignore-malformed)
16096 (not (org-before-first-heading-p))
16097 (y-or-n-p (format "Malformed drawer at %d, repair?"
16098 (line-beginning-position))))
16099 (org-get-property-block nil t))
16100 (throw 'skip nil))
16101 (goto-char (car range))
16102 (let ((begin (car range))
16103 (end (cdr range)))
16104 ;; Make sure that found property block is not located
16105 ;; before current point, as it would generate an infloop.
16106 ;; It can happen, for example, in the following
16107 ;; situation:
16109 ;; * Headline
16110 ;; :PROPERTIES:
16111 ;; ...
16112 ;; :END:
16113 ;; *************** Inlinetask
16114 ;; #+BEGIN_EXAMPLE
16115 ;; :PROPERTIES:
16116 ;; #+END_EXAMPLE
16118 (if (< begin (point)) (throw 'skip nil) (goto-char begin))
16119 (while (< (point) end)
16120 (let ((p (progn (looking-at org-property-re)
16121 (match-string-no-properties 2))))
16122 ;; Only add true property name, not extension symbol.
16123 (push (if (not (string-match-p "\\+\\'" p)) p
16124 (substring p 0 -1))
16125 props))
16126 (forward-line))))
16127 (outline-next-heading)))
16128 (when columns
16129 (goto-char (point-min))
16130 (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
16131 (let ((element (org-element-at-point)))
16132 (when (memq (org-element-type element) '(keyword node-property))
16133 (let ((value (org-element-property :value element))
16134 (start 0))
16135 (while (string-match "%[0-9]*\\([[:alnum:]_-]+\\)\\(([^)]+)\\)?\
16136 \\(?:{[^}]+}\\)?"
16137 value start)
16138 (setq start (match-end 0))
16139 (let ((p (match-string-no-properties 1 value)))
16140 (unless (member-ignore-case p org-special-properties)
16141 (push p props))))))))))
16142 (sort (delete-dups props) (lambda (a b) (string< (upcase a) (upcase b))))))
16144 (defun org-property-values (key)
16145 "List all non-nil values of property KEY in current buffer."
16146 (org-with-wide-buffer
16147 (goto-char (point-min))
16148 (let ((case-fold-search t)
16149 (re (org-re-property key))
16150 values)
16151 (while (re-search-forward re nil t)
16152 (push (org-entry-get (point) key) values))
16153 (delete-dups values))))
16155 (defun org-insert-property-drawer ()
16156 "Insert a property drawer into the current entry."
16157 (org-with-wide-buffer
16158 (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
16159 (org-back-to-heading t)
16160 (org-with-limited-levels (org-back-to-heading t)))
16161 (forward-line)
16162 (when (looking-at-p org-planning-line-re) (forward-line))
16163 (unless (looking-at-p org-property-drawer-re)
16164 ;; Make sure we start editing a line from current entry, not from
16165 ;; next one. It prevents extending text properties or overlays
16166 ;; belonging to the latter.
16167 (when (bolp) (backward-char))
16168 (let ((begin (1+ (point)))
16169 (inhibit-read-only t))
16170 (insert "\n:PROPERTIES:\n:END:")
16171 (when (eobp) (insert "\n"))
16172 (org-indent-region begin (point))))))
16174 (defun org-insert-drawer (&optional arg drawer)
16175 "Insert a drawer at point.
16177 When optional argument ARG is non-nil, insert a property drawer.
16179 Optional argument DRAWER, when non-nil, is a string representing
16180 drawer's name. Otherwise, the user is prompted for a name.
16182 If a region is active, insert the drawer around that region
16183 instead.
16185 Point is left between drawer's boundaries."
16186 (interactive "P")
16187 (let* ((drawer (if arg "PROPERTIES"
16188 (or drawer (read-from-minibuffer "Drawer: ")))))
16189 (cond
16190 ;; With C-u, fall back on `org-insert-property-drawer'
16191 (arg (org-insert-property-drawer))
16192 ;; Check validity of suggested drawer's name.
16193 ((not (string-match-p org-drawer-regexp (format ":%s:" drawer)))
16194 (user-error "Invalid drawer name"))
16195 ;; With an active region, insert a drawer at point.
16196 ((not (org-region-active-p))
16197 (progn
16198 (unless (bolp) (insert "\n"))
16199 (insert (format ":%s:\n\n:END:\n" drawer))
16200 (forward-line -2)))
16201 ;; Otherwise, insert the drawer at point
16203 (let ((rbeg (region-beginning))
16204 (rend (copy-marker (region-end))))
16205 (unwind-protect
16206 (progn
16207 (goto-char rbeg)
16208 (beginning-of-line)
16209 (when (save-excursion
16210 (re-search-forward org-outline-regexp-bol rend t))
16211 (user-error "Drawers cannot contain headlines"))
16212 ;; Position point at the beginning of the first
16213 ;; non-blank line in region. Insert drawer's opening
16214 ;; there, then indent it.
16215 (org-skip-whitespace)
16216 (beginning-of-line)
16217 (insert ":" drawer ":\n")
16218 (forward-line -1)
16219 (indent-for-tab-command)
16220 ;; Move point to the beginning of the first blank line
16221 ;; after the last non-blank line in region. Insert
16222 ;; drawer's closing, then indent it.
16223 (goto-char rend)
16224 (skip-chars-backward " \r\t\n")
16225 (insert "\n:END:")
16226 (deactivate-mark t)
16227 (indent-for-tab-command)
16228 (unless (eolp) (insert "\n")))
16229 ;; Clear marker, whatever the outcome of insertion is.
16230 (set-marker rend nil)))))))
16232 (defvar org-property-set-functions-alist nil
16233 "Property set function alist.
16234 Each entry should have the following format:
16236 (PROPERTY . READ-FUNCTION)
16238 The read function will be called with the same argument as
16239 `org-completing-read'.")
16241 (defun org-set-property-function (property)
16242 "Get the function that should be used to set PROPERTY.
16243 This is computed according to `org-property-set-functions-alist'."
16244 (or (cdr (assoc property org-property-set-functions-alist))
16245 'org-completing-read))
16247 (defun org-read-property-value (property)
16248 "Read PROPERTY value from user."
16249 (let* ((completion-ignore-case t)
16250 (allowed (org-property-get-allowed-values nil property 'table))
16251 (cur (org-entry-get nil property))
16252 (prompt (concat property " value"
16253 (if (and cur (string-match "\\S-" cur))
16254 (concat " [" cur "]") "") ": "))
16255 (set-function (org-set-property-function property))
16256 (val (if allowed
16257 (funcall set-function prompt allowed nil
16258 (not (get-text-property 0 'org-unrestricted
16259 (caar allowed))))
16260 (funcall set-function prompt
16261 (mapcar 'list (org-property-values property))
16262 nil nil "" nil cur))))
16263 (org-trim val)))
16265 (defvar org-last-set-property nil)
16266 (defvar org-last-set-property-value nil)
16267 (defun org-read-property-name ()
16268 "Read a property name."
16269 (let ((completion-ignore-case t)
16270 (default-prop (or (and (org-at-property-p)
16271 (match-string-no-properties 2))
16272 org-last-set-property)))
16273 (org-completing-read
16274 (concat "Property"
16275 (if default-prop (concat " [" default-prop "]") "")
16276 ": ")
16277 (mapcar #'list (org-buffer-property-keys nil t t))
16278 nil nil nil nil default-prop)))
16280 (defun org-set-property-and-value (use-last)
16281 "Allow to set [PROPERTY]: [value] direction from prompt.
16282 When use-default, don't even ask, just use the last
16283 \"[PROPERTY]: [value]\" string from the history."
16284 (interactive "P")
16285 (let* ((completion-ignore-case t)
16286 (pv (or (and use-last org-last-set-property-value)
16287 (org-completing-read
16288 "Enter a \"[Property]: [value]\" pair: "
16289 nil nil nil nil nil
16290 org-last-set-property-value)))
16291 prop val)
16292 (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
16293 (setq prop (match-string 1 pv)
16294 val (match-string 2 pv))
16295 (org-set-property prop val))))
16297 (defun org-set-property (property value)
16298 "In the current entry, set PROPERTY to VALUE.
16300 When called interactively, this will prompt for a property name, offering
16301 completion on existing and default properties. And then it will prompt
16302 for a value, offering completion either on allowed values (via an inherited
16303 xxx_ALL property) or on existing values in other instances of this property
16304 in the current file.
16306 Throw an error when trying to set a property with an invalid name."
16307 (interactive (list nil nil))
16308 (let ((property (or property (org-read-property-name))))
16309 ;; `org-entry-put' also makes the following check, but this one
16310 ;; avoids polluting `org-last-set-property' and
16311 ;; `org-last-set-property-value' needlessly.
16312 (unless (org--valid-property-p property)
16313 (user-error "Invalid property name: \"%s\"" property))
16314 (let ((value (or value (org-read-property-value property)))
16315 (fn (cdr (assoc-string property org-properties-postprocess-alist t))))
16316 (setq org-last-set-property property)
16317 (setq org-last-set-property-value (concat property ": " value))
16318 ;; Possibly postprocess the inserted value:
16319 (when fn (setq value (funcall fn value)))
16320 (unless (equal (org-entry-get nil property) value)
16321 (org-entry-put nil property value)))))
16323 (defun org-find-property (property &optional value)
16324 "Find first entry in buffer that sets PROPERTY.
16326 When optional argument VALUE is non-nil, only consider an entry
16327 if it contains PROPERTY set to this value. If PROPERTY should be
16328 explicitly set to nil, use string \"nil\" for VALUE.
16330 Return position where the entry begins, or nil if there is no
16331 such entry. If narrowing is in effect, only search the visible
16332 part of the buffer."
16333 (save-excursion
16334 (goto-char (point-min))
16335 (let ((case-fold-search t)
16336 (re (org-re-property property nil (not value) value)))
16337 (catch 'exit
16338 (while (re-search-forward re nil t)
16339 (when (if value (org-at-property-p)
16340 (org-entry-get (point) property nil t))
16341 (throw 'exit (progn (org-back-to-heading t) (point)))))))))
16343 (defun org-delete-property (property)
16344 "In the current entry, delete PROPERTY."
16345 (interactive
16346 (let* ((completion-ignore-case t)
16347 (cat (org-entry-get (point) "CATEGORY"))
16348 (props0 (org-entry-properties nil 'standard))
16349 (props (if cat props0
16350 (delete `("CATEGORY" . ,(org-get-category)) props0)))
16351 (prop (if (< 1 (length props))
16352 (completing-read "Property: " props nil t)
16353 (caar props))))
16354 (list prop)))
16355 (if (not property)
16356 (message "No property to delete in this entry")
16357 (org-entry-delete nil property)
16358 (message "Property \"%s\" deleted" property)))
16360 (defun org-delete-property-globally (property)
16361 "Remove PROPERTY globally, from all entries.
16362 This function ignores narrowing, if any."
16363 (interactive
16364 (let* ((completion-ignore-case t)
16365 (prop (completing-read
16366 "Globally remove property: "
16367 (mapcar #'list (org-buffer-property-keys)))))
16368 (list prop)))
16369 (org-with-wide-buffer
16370 (goto-char (point-min))
16371 (let ((count 0)
16372 (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
16373 (while (re-search-forward re nil t)
16374 (when (org-entry-delete (point) property) (cl-incf count)))
16375 (message "Property \"%s\" removed from %d entries" property count))))
16377 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
16379 (defun org-compute-property-at-point ()
16380 "Compute the property at point.
16381 This looks for an enclosing column format, extracts the operator and
16382 then applies it to the property in the column format's scope."
16383 (interactive)
16384 (unless (org-at-property-p)
16385 (user-error "Not at a property"))
16386 (let ((prop (match-string-no-properties 2)))
16387 (org-columns-get-format-and-top-level)
16388 (unless (nth 3 (assoc-string prop org-columns-current-fmt-compiled t))
16389 (user-error "No operator defined for property %s" prop))
16390 (org-columns-compute prop)))
16392 (defvar org-property-allowed-value-functions nil
16393 "Hook for functions supplying allowed values for a specific property.
16394 The functions must take a single argument, the name of the property, and
16395 return a flat list of allowed values. If \":ETC\" is one of
16396 the values, this means that these values are intended as defaults for
16397 completion, but that other values should be allowed too.
16398 The functions must return nil if they are not responsible for this
16399 property.")
16401 (defun org-property-get-allowed-values (pom property &optional table)
16402 "Get allowed values for the property PROPERTY.
16403 When TABLE is non-nil, return an alist that can directly be used for
16404 completion."
16405 (let (vals)
16406 (cond
16407 ((equal property "TODO")
16408 (setq vals (org-with-point-at pom
16409 (append org-todo-keywords-1 '("")))))
16410 ((equal property "PRIORITY")
16411 (let ((n org-lowest-priority))
16412 (while (>= n org-highest-priority)
16413 (push (char-to-string n) vals)
16414 (setq n (1- n)))))
16415 ((equal property "CATEGORY"))
16416 ((member property org-special-properties))
16417 ((setq vals (run-hook-with-args-until-success
16418 'org-property-allowed-value-functions property)))
16420 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
16421 (when (and vals (string-match "\\S-" vals))
16422 (setq vals (car (read-from-string (concat "(" vals ")"))))
16423 (setq vals (mapcar (lambda (x)
16424 (cond ((stringp x) x)
16425 ((numberp x) (number-to-string x))
16426 ((symbolp x) (symbol-name x))
16427 (t "???")))
16428 vals)))))
16429 (when (member ":ETC" vals)
16430 (setq vals (remove ":ETC" vals))
16431 (org-add-props (car vals) '(org-unrestricted t)))
16432 (if table (mapcar 'list vals) vals)))
16434 (defun org-property-previous-allowed-value (&optional _previous)
16435 "Switch to the next allowed value for this property."
16436 (interactive)
16437 (org-property-next-allowed-value t))
16439 (defun org-property-next-allowed-value (&optional previous)
16440 "Switch to the next allowed value for this property."
16441 (interactive)
16442 (unless (org-at-property-p)
16443 (user-error "Not at a property"))
16444 (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
16445 (key (match-string 2))
16446 (value (match-string 3))
16447 (allowed (or (org-property-get-allowed-values (point) key)
16448 (and (member value '("[ ]" "[-]" "[X]"))
16449 '("[ ]" "[X]"))))
16450 (heading (save-match-data (nth 4 (org-heading-components))))
16451 nval)
16452 (unless allowed
16453 (user-error "Allowed values for this property have not been defined"))
16454 (when previous (setq allowed (reverse allowed)))
16455 (when (member value allowed)
16456 (setq nval (car (cdr (member value allowed)))))
16457 (setq nval (or nval (car allowed)))
16458 (when (equal nval value)
16459 (user-error "Only one allowed value for this property"))
16460 (org-at-property-p)
16461 (replace-match (concat " :" key ": " nval) t t)
16462 (org-indent-line)
16463 (beginning-of-line 1)
16464 (skip-chars-forward " \t")
16465 (when (equal prop org-effort-property)
16466 (org-refresh-property
16467 '((effort . identity)
16468 (effort-minutes . org-duration-to-minutes))
16469 nval)
16470 (when (string= org-clock-current-task heading)
16471 (setq org-clock-effort nval)
16472 (org-clock-update-mode-line)))
16473 (run-hook-with-args 'org-property-changed-functions key nval)))
16475 (defun org-find-olp (path &optional this-buffer)
16476 "Return a marker pointing to the entry at outline path OLP.
16477 If anything goes wrong, throw an error.
16478 You can wrap this call to catch the error like this:
16480 (condition-case msg
16481 (org-mobile-locate-entry (match-string 4))
16482 (error (nth 1 msg)))
16484 The return value will then be either a string with the error message,
16485 or a marker if everything is OK.
16487 If THIS-BUFFER is set, the outline path does not contain a file,
16488 only headings."
16489 (let* ((file (if this-buffer buffer-file-name (pop path)))
16490 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
16491 (level 1)
16492 (lmin 1)
16493 (lmax 1)
16494 end found flevel)
16495 (unless buffer (error "File not found :%s" file))
16496 (with-current-buffer buffer
16497 (unless (derived-mode-p 'org-mode)
16498 (error "Buffer %s needs to be in Org mode" buffer))
16499 (org-with-wide-buffer
16500 (goto-char (point-min))
16501 (dolist (heading path)
16502 (let ((re (format org-complex-heading-regexp-format
16503 (regexp-quote heading)))
16504 (cnt 0))
16505 (while (re-search-forward re end t)
16506 (setq level (- (match-end 1) (match-beginning 1)))
16507 (when (and (>= level lmin) (<= level lmax))
16508 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
16509 (when (= cnt 0)
16510 (error "Heading not found on level %d: %s" lmax heading))
16511 (when (> cnt 1)
16512 (error "Heading not unique on level %d: %s" lmax heading))
16513 (goto-char found)
16514 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
16515 (setq end (save-excursion (org-end-of-subtree t t)))))
16516 (when (org-at-heading-p)
16517 (point-marker))))))
16519 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
16520 "Find node HEADING in BUFFER.
16521 Return a marker to the heading if it was found, or nil if not.
16522 If POS-ONLY is set, return just the position instead of a marker.
16524 The heading text must match exact, but it may have a TODO keyword,
16525 a priority cookie and tags in the standard locations."
16526 (with-current-buffer (or buffer (current-buffer))
16527 (org-with-wide-buffer
16528 (goto-char (point-min))
16529 (let (case-fold-search)
16530 (when (re-search-forward
16531 (format org-complex-heading-regexp-format
16532 (regexp-quote heading)) nil t)
16533 (if pos-only
16534 (match-beginning 0)
16535 (move-marker (make-marker) (match-beginning 0))))))))
16537 (defun org-find-exact-heading-in-directory (heading &optional dir)
16538 "Find Org node headline HEADING in all \".org\" files in directory DIR.
16539 When the target headline is found, return a marker to this location."
16540 (let ((files (directory-files (or dir default-directory)
16541 t "\\`[^.#].*\\.org\\'"))
16542 visiting m buffer)
16543 (catch 'found
16544 (dolist (file files)
16545 (message "trying %s" file)
16546 (setq visiting (org-find-base-buffer-visiting file))
16547 (setq buffer (or visiting (find-file-noselect file)))
16548 (setq m (org-find-exact-headline-in-buffer
16549 heading buffer))
16550 (when (and (not m) (not visiting)) (kill-buffer buffer))
16551 (and m (throw 'found m))))))
16553 (defun org-find-entry-with-id (ident)
16554 "Locate the entry that contains the ID property with exact value IDENT.
16555 IDENT can be a string, a symbol or a number, this function will search for
16556 the string representation of it.
16557 Return the position where this entry starts, or nil if there is no such entry."
16558 (interactive "sID: ")
16559 (let ((id (cond
16560 ((stringp ident) ident)
16561 ((symbolp ident) (symbol-name ident))
16562 ((numberp ident) (number-to-string ident))
16563 (t (error "IDENT %s must be a string, symbol or number" ident)))))
16564 (org-with-wide-buffer (org-find-property "ID" id))))
16566 ;;;; Timestamps
16568 (defvar org-last-changed-timestamp nil)
16569 (defvar org-last-inserted-timestamp nil
16570 "The last time stamp inserted with `org-insert-time-stamp'.")
16572 (defun org-time-stamp (arg &optional inactive)
16573 "Prompt for a date/time and insert a time stamp.
16575 If the user specifies a time like HH:MM or if this command is
16576 called with at least one prefix argument, the time stamp contains
16577 the date and the time. Otherwise, only the date is included.
16579 All parts of a date not specified by the user are filled in from
16580 the timestamp at point, if any, or the current date/time
16581 otherwise.
16583 If there is already a timestamp at the cursor, it is replaced.
16585 With two universal prefix arguments, insert an active timestamp
16586 with the current time without prompting the user.
16588 When called from lisp, the timestamp is inactive if INACTIVE is
16589 non-nil."
16590 (interactive "P")
16591 (let* ((ts (cond
16592 ((org-at-date-range-p t)
16593 (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
16594 ((org-at-timestamp-p 'lax) (match-string 0))))
16595 ;; Default time is either the timestamp at point or today.
16596 ;; When entering a range, only the range start is considered.
16597 (default-time (if (not ts) (current-time)
16598 (apply #'encode-time (org-parse-time-string ts))))
16599 (default-input (and ts (org-get-compact-tod ts)))
16600 (repeater (and ts
16601 (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
16602 (match-string 0 ts)))
16603 org-time-was-given
16604 org-end-time-was-given
16605 (time
16606 (and (if (equal arg '(16)) (current-time)
16607 ;; Preserve `this-command' and `last-command'.
16608 (let ((this-command this-command)
16609 (last-command last-command))
16610 (org-read-date
16611 arg 'totime nil nil default-time default-input
16612 inactive))))))
16613 (cond
16614 ((and ts
16615 (memq last-command '(org-time-stamp org-time-stamp-inactive))
16616 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
16617 (insert "--")
16618 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
16620 ;; Make sure we're on a timestamp. When in the middle of a date
16621 ;; range, move arbitrarily to range end.
16622 (unless (org-at-timestamp-p 'lax)
16623 (skip-chars-forward "-")
16624 (org-at-timestamp-p 'lax))
16625 (replace-match "")
16626 (setq org-last-changed-timestamp
16627 (org-insert-time-stamp
16628 time (or org-time-was-given arg)
16629 inactive nil nil (list org-end-time-was-given)))
16630 (when repeater
16631 (backward-char)
16632 (insert " " repeater)
16633 (setq org-last-changed-timestamp
16634 (concat (substring org-last-inserted-timestamp 0 -1)
16635 " " repeater ">")))
16636 (message "Timestamp updated"))
16637 ((equal arg '(16)) (org-insert-time-stamp time t inactive))
16638 (t (org-insert-time-stamp
16639 time (or org-time-was-given arg) inactive nil nil
16640 (list org-end-time-was-given))))))
16642 ;; FIXME: can we use this for something else, like computing time differences?
16643 (defun org-get-compact-tod (s)
16644 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
16645 (let* ((t1 (match-string 1 s))
16646 (h1 (string-to-number (match-string 2 s)))
16647 (m1 (string-to-number (match-string 3 s)))
16648 (t2 (and (match-end 4) (match-string 5 s)))
16649 (h2 (and t2 (string-to-number (match-string 6 s))))
16650 (m2 (and t2 (string-to-number (match-string 7 s))))
16651 dh dm)
16652 (if (not t2)
16654 (setq dh (- h2 h1) dm (- m2 m1))
16655 (when (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
16656 (concat t1 "+" (number-to-string dh)
16657 (and (/= 0 dm) (format ":%02d" dm)))))))
16659 (defun org-time-stamp-inactive (&optional arg)
16660 "Insert an inactive time stamp.
16662 An inactive time stamp is enclosed in square brackets instead of angle
16663 brackets. It is inactive in the sense that it does not trigger agenda entries,
16664 does not link to the calendar and cannot be changed with the S-cursor keys.
16665 So these are more for recording a certain time/date.
16667 If the user specifies a time like HH:MM or if this command is called with
16668 at least one prefix argument, the time stamp contains the date and the time.
16669 Otherwise, only the date is included.
16671 When called with two universal prefix arguments, insert an active time stamp
16672 with the current time without prompting the user."
16673 (interactive "P")
16674 (org-time-stamp arg 'inactive))
16676 (defvar org-date-ovl (make-overlay 1 1))
16677 (overlay-put org-date-ovl 'face 'org-date-selected)
16678 (delete-overlay org-date-ovl)
16680 (defvar org-ans1) ; dynamically scoped parameter
16681 (defvar org-ans2) ; dynamically scoped parameter
16683 (defvar org-plain-time-of-day-regexp) ; defined below
16685 (defvar org-overriding-default-time nil) ; dynamically scoped
16686 (defvar org-read-date-overlay nil)
16687 (defvar org-dcst nil) ; dynamically scoped
16688 (defvar org-read-date-history nil)
16689 (defvar org-read-date-final-answer nil)
16690 (defvar org-read-date-analyze-futurep nil)
16691 (defvar org-read-date-analyze-forced-year nil)
16692 (defvar org-read-date-inactive)
16694 (defvar org-read-date-minibuffer-local-map
16695 (let* ((map (make-sparse-keymap)))
16696 (set-keymap-parent map minibuffer-local-map)
16697 (org-defkey map (kbd ".")
16698 (lambda () (interactive)
16699 ;; Are we at the beginning of the prompt?
16700 (if (looking-back "^[^:]+: "
16701 (let ((inhibit-field-text-motion t))
16702 (line-beginning-position)))
16703 (org-eval-in-calendar '(calendar-goto-today))
16704 (insert "."))))
16705 (org-defkey map (kbd "C-.")
16706 (lambda () (interactive)
16707 (org-eval-in-calendar '(calendar-goto-today))))
16708 (org-defkey map [(meta shift left)]
16709 (lambda () (interactive)
16710 (org-eval-in-calendar '(calendar-backward-month 1))))
16711 (org-defkey map [(meta shift right)]
16712 (lambda () (interactive)
16713 (org-eval-in-calendar '(calendar-forward-month 1))))
16714 (org-defkey map [(meta shift up)]
16715 (lambda () (interactive)
16716 (org-eval-in-calendar '(calendar-backward-year 1))))
16717 (org-defkey map [(meta shift down)]
16718 (lambda () (interactive)
16719 (org-eval-in-calendar '(calendar-forward-year 1))))
16720 (org-defkey map [?\e (shift left)]
16721 (lambda () (interactive)
16722 (org-eval-in-calendar '(calendar-backward-month 1))))
16723 (org-defkey map [?\e (shift right)]
16724 (lambda () (interactive)
16725 (org-eval-in-calendar '(calendar-forward-month 1))))
16726 (org-defkey map [?\e (shift up)]
16727 (lambda () (interactive)
16728 (org-eval-in-calendar '(calendar-backward-year 1))))
16729 (org-defkey map [?\e (shift down)]
16730 (lambda () (interactive)
16731 (org-eval-in-calendar '(calendar-forward-year 1))))
16732 (org-defkey map [(shift up)]
16733 (lambda () (interactive)
16734 (org-eval-in-calendar '(calendar-backward-week 1))))
16735 (org-defkey map [(shift down)]
16736 (lambda () (interactive)
16737 (org-eval-in-calendar '(calendar-forward-week 1))))
16738 (org-defkey map [(shift left)]
16739 (lambda () (interactive)
16740 (org-eval-in-calendar '(calendar-backward-day 1))))
16741 (org-defkey map [(shift right)]
16742 (lambda () (interactive)
16743 (org-eval-in-calendar '(calendar-forward-day 1))))
16744 (org-defkey map "!"
16745 (lambda () (interactive)
16746 (org-eval-in-calendar '(diary-view-entries))
16747 (message "")))
16748 (org-defkey map ">"
16749 (lambda () (interactive)
16750 (org-eval-in-calendar '(calendar-scroll-left 1))))
16751 (org-defkey map "<"
16752 (lambda () (interactive)
16753 (org-eval-in-calendar '(calendar-scroll-right 1))))
16754 (org-defkey map "\C-v"
16755 (lambda () (interactive)
16756 (org-eval-in-calendar
16757 '(calendar-scroll-left-three-months 1))))
16758 (org-defkey map "\M-v"
16759 (lambda () (interactive)
16760 (org-eval-in-calendar
16761 '(calendar-scroll-right-three-months 1))))
16762 map)
16763 "Keymap for minibuffer commands when using `org-read-date'.")
16765 (defvar org-def)
16766 (defvar org-defdecode)
16767 (defvar org-with-time)
16769 (defvar calendar-setup) ; Dynamically scoped.
16770 (defun org-read-date (&optional with-time to-time from-string prompt
16771 default-time default-input inactive)
16772 "Read a date, possibly a time, and make things smooth for the user.
16773 The prompt will suggest to enter an ISO date, but you can also enter anything
16774 which will at least partially be understood by `parse-time-string'.
16775 Unrecognized parts of the date will default to the current day, month, year,
16776 hour and minute. If this command is called to replace a timestamp at point,
16777 or to enter the second timestamp of a range, the default time is taken
16778 from the existing stamp. Furthermore, the command prefers the future,
16779 so if you are giving a date where the year is not given, and the day-month
16780 combination is already past in the current year, it will assume you
16781 mean next year. For details, see the manual. A few examples:
16783 3-2-5 --> 2003-02-05
16784 feb 15 --> currentyear-02-15
16785 2/15 --> currentyear-02-15
16786 sep 12 9 --> 2009-09-12
16787 12:45 --> today 12:45
16788 22 sept 0:34 --> currentyear-09-22 0:34
16789 12 --> currentyear-currentmonth-12
16790 Fri --> nearest Friday after today
16791 -Tue --> last Tuesday
16792 etc.
16794 Furthermore you can specify a relative date by giving, as the *first* thing
16795 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
16796 change in days weeks, months, years.
16797 With a single plus or minus, the date is relative to today. With a double
16798 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
16799 +4d --> four days from today
16800 +4 --> same as above
16801 +2w --> two weeks from today
16802 ++5 --> five days from default date
16804 The function understands only English month and weekday abbreviations.
16806 While prompting, a calendar is popped up - you can also select the
16807 date with the mouse (button 1). The calendar shows a period of three
16808 months. To scroll it to other months, use the keys `>' and `<'.
16809 If you don't like the calendar, turn it off with
16810 (setq org-read-date-popup-calendar nil)
16812 With optional argument TO-TIME, the date will immediately be converted
16813 to an internal time.
16814 With an optional argument WITH-TIME, the prompt will suggest to
16815 also insert a time. Note that when WITH-TIME is not set, you can
16816 still enter a time, and this function will inform the calling routine
16817 about this change. The calling routine may then choose to change the
16818 format used to insert the time stamp into the buffer to include the time.
16819 With optional argument FROM-STRING, read from this string instead from
16820 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
16821 the time/date that is used for everything that is not specified by the
16822 user."
16823 (require 'parse-time)
16824 (let* ((org-with-time with-time)
16825 (org-time-stamp-rounding-minutes
16826 (if (equal org-with-time '(16))
16827 '(0 0)
16828 org-time-stamp-rounding-minutes))
16829 (org-dcst org-display-custom-times)
16830 (ct (org-current-time))
16831 (org-def (or org-overriding-default-time default-time ct))
16832 (org-defdecode (decode-time org-def))
16833 (cur-frame (selected-frame))
16834 (mouse-autoselect-window nil) ; Don't let the mouse jump
16835 (calendar-setup
16836 (and (eq calendar-setup 'calendar-only) 'calendar-only))
16837 (calendar-move-hook nil)
16838 (calendar-view-diary-initially-flag nil)
16839 (calendar-view-holidays-initially-flag nil)
16840 ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
16841 ;; Rationalize `org-def' and `org-defdecode', if required.
16842 (when (< (nth 2 org-defdecode) org-extend-today-until)
16843 (setf (nth 2 org-defdecode) -1)
16844 (setf (nth 1 org-defdecode) 59)
16845 (setq org-def (apply #'encode-time org-defdecode))
16846 (setq org-defdecode (decode-time org-def)))
16847 (let* ((timestr (format-time-string
16848 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d")
16849 org-def))
16850 (prompt (concat (if prompt (concat prompt " ") "")
16851 (format "Date+time [%s]: " timestr))))
16852 (cond
16853 (from-string (setq ans from-string))
16854 (org-read-date-popup-calendar
16855 (save-excursion
16856 (save-window-excursion
16857 (calendar)
16858 (when (eq calendar-setup 'calendar-only)
16859 (setq cal-frame
16860 (window-frame (get-buffer-window "*Calendar*" 'visible)))
16861 (select-frame cal-frame))
16862 (org-eval-in-calendar '(setq cursor-type nil) t)
16863 (unwind-protect
16864 (progn
16865 (calendar-forward-day (- (time-to-days org-def)
16866 (calendar-absolute-from-gregorian
16867 (calendar-current-date))))
16868 (org-eval-in-calendar nil t)
16869 (let* ((old-map (current-local-map))
16870 (map (copy-keymap calendar-mode-map))
16871 (minibuffer-local-map
16872 (copy-keymap org-read-date-minibuffer-local-map)))
16873 (org-defkey map (kbd "RET") 'org-calendar-select)
16874 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
16875 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
16876 (unwind-protect
16877 (progn
16878 (use-local-map map)
16879 (setq org-read-date-inactive inactive)
16880 (add-hook 'post-command-hook 'org-read-date-display)
16881 (setq org-ans0
16882 (read-string prompt
16883 default-input
16884 'org-read-date-history
16885 nil))
16886 ;; org-ans0: from prompt
16887 ;; org-ans1: from mouse click
16888 ;; org-ans2: from calendar motion
16889 (setq ans
16890 (concat org-ans0 " " (or org-ans1 org-ans2))))
16891 (remove-hook 'post-command-hook 'org-read-date-display)
16892 (use-local-map old-map)
16893 (when org-read-date-overlay
16894 (delete-overlay org-read-date-overlay)
16895 (setq org-read-date-overlay nil)))))
16896 (bury-buffer "*Calendar*")
16897 (when cal-frame
16898 (delete-frame cal-frame)
16899 (select-frame-set-input-focus cur-frame))))))
16901 (t ; Naked prompt only
16902 (unwind-protect
16903 (setq ans (read-string prompt default-input
16904 'org-read-date-history timestr))
16905 (when org-read-date-overlay
16906 (delete-overlay org-read-date-overlay)
16907 (setq org-read-date-overlay nil))))))
16909 (setq final (org-read-date-analyze ans org-def org-defdecode))
16911 (when org-read-date-analyze-forced-year
16912 (message "Year was forced into %s"
16913 (if org-read-date-force-compatible-dates
16914 "compatible range (1970-2037)"
16915 "range representable on this machine"))
16916 (ding))
16918 ;; One round trip to get rid of 34th of August and stuff like that....
16919 (setq final (decode-time (apply 'encode-time final)))
16921 (setq org-read-date-final-answer ans)
16923 (if to-time
16924 (apply 'encode-time final)
16925 (if (and (boundp 'org-time-was-given) org-time-was-given)
16926 (format "%04d-%02d-%02d %02d:%02d"
16927 (nth 5 final) (nth 4 final) (nth 3 final)
16928 (nth 2 final) (nth 1 final))
16929 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
16931 (defun org-read-date-display ()
16932 "Display the current date prompt interpretation in the minibuffer."
16933 (when org-read-date-display-live
16934 (when org-read-date-overlay
16935 (delete-overlay org-read-date-overlay))
16936 (when (minibufferp (current-buffer))
16937 (save-excursion
16938 (end-of-line 1)
16939 (while (not (equal (buffer-substring
16940 (max (point-min) (- (point) 4)) (point))
16941 " "))
16942 (insert " ")))
16943 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
16944 " " (or org-ans1 org-ans2)))
16945 (org-end-time-was-given nil)
16946 (f (org-read-date-analyze ans org-def org-defdecode))
16947 (fmts (if org-dcst
16948 org-time-stamp-custom-formats
16949 org-time-stamp-formats))
16950 (fmt (if (or org-with-time
16951 (and (boundp 'org-time-was-given) org-time-was-given))
16952 (cdr fmts)
16953 (car fmts)))
16954 (txt (format-time-string fmt (apply 'encode-time f)))
16955 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
16956 (txt (concat "=> " txt)))
16957 (when (and org-end-time-was-given
16958 (string-match org-plain-time-of-day-regexp txt))
16959 (setq txt (concat (substring txt 0 (match-end 0)) "-"
16960 org-end-time-was-given
16961 (substring txt (match-end 0)))))
16962 (when org-read-date-analyze-futurep
16963 (setq txt (concat txt " (=>F)")))
16964 (setq org-read-date-overlay
16965 (make-overlay (1- (point-at-eol)) (point-at-eol)))
16966 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
16968 (defun org-read-date-analyze (ans def defdecode)
16969 "Analyze the combined answer of the date prompt."
16970 ;; FIXME: cleanup and comment
16971 ;; Pass `current-time' result to `decode-time' (instead of calling
16972 ;; without arguments) so that only `current-time' has to be
16973 ;; overridden in tests.
16974 (let ((org-def def)
16975 (org-defdecode defdecode)
16976 (nowdecode (decode-time (current-time)))
16977 delta deltan deltaw deltadef year month day
16978 hour minute second wday pm h2 m2 tl wday1
16979 iso-year iso-weekday iso-week iso-date futurep kill-year)
16980 (setq org-read-date-analyze-futurep nil
16981 org-read-date-analyze-forced-year nil)
16982 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
16983 (setq ans "+0"))
16985 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
16986 (setq ans (replace-match "" t t ans)
16987 deltan (car delta)
16988 deltaw (nth 1 delta)
16989 deltadef (nth 2 delta)))
16991 ;; Check if there is an iso week date in there. If yes, store the
16992 ;; info and postpone interpreting it until the rest of the parsing
16993 ;; is done.
16994 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
16995 (setq iso-year (when (match-end 1)
16996 (org-small-year-to-year
16997 (string-to-number (match-string 1 ans))))
16998 iso-weekday (when (match-end 3)
16999 (string-to-number (match-string 3 ans)))
17000 iso-week (string-to-number (match-string 2 ans)))
17001 (setq ans (replace-match "" t t ans)))
17003 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
17004 (when (string-match
17005 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
17006 (setq year (if (match-end 2)
17007 (string-to-number (match-string 2 ans))
17008 (progn (setq kill-year t)
17009 (string-to-number (format-time-string "%Y"))))
17010 month (string-to-number (match-string 3 ans))
17011 day (string-to-number (match-string 4 ans)))
17012 (setq year (org-small-year-to-year year))
17013 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
17014 t nil ans)))
17016 ;; Help matching dotted european dates
17017 (when (string-match
17018 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
17019 (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
17020 (setq kill-year t)
17021 (string-to-number (format-time-string "%Y")))
17022 day (string-to-number (match-string 1 ans))
17023 month (string-to-number (match-string 2 ans))
17024 ans (replace-match (format "%04d-%02d-%02d" year month day)
17025 t nil ans)))
17027 ;; Help matching american dates, like 5/30 or 5/30/7
17028 (when (string-match
17029 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
17030 (setq year (if (match-end 4)
17031 (string-to-number (match-string 4 ans))
17032 (progn (setq kill-year t)
17033 (string-to-number (format-time-string "%Y"))))
17034 month (string-to-number (match-string 1 ans))
17035 day (string-to-number (match-string 2 ans)))
17036 (setq year (org-small-year-to-year year))
17037 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
17038 t nil ans)))
17039 ;; Help matching am/pm times, because `parse-time-string' does not do that.
17040 ;; If there is a time with am/pm, and *no* time without it, we convert
17041 ;; so that matching will be successful.
17042 (cl-loop for i from 1 to 2 do ; twice, for end time as well
17043 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
17044 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
17045 (setq hour (string-to-number (match-string 1 ans))
17046 minute (if (match-end 3)
17047 (string-to-number (match-string 3 ans))
17049 pm (equal ?p
17050 (string-to-char (downcase (match-string 4 ans)))))
17051 (if (and (= hour 12) (not pm))
17052 (setq hour 0)
17053 (when (and pm (< hour 12)) (setq hour (+ 12 hour))))
17054 (setq ans (replace-match (format "%02d:%02d" hour minute)
17055 t t ans))))
17057 ;; Check if a time range is given as a duration
17058 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
17059 (setq hour (string-to-number (match-string 1 ans))
17060 h2 (+ hour (string-to-number (match-string 3 ans)))
17061 minute (string-to-number (match-string 2 ans))
17062 m2 (+ minute (if (match-end 5) (string-to-number
17063 (match-string 5 ans))0)))
17064 (when (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
17065 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
17066 t t ans)))
17068 ;; Check if there is a time range
17069 (when (boundp 'org-end-time-was-given)
17070 (setq org-time-was-given nil)
17071 (when (and (string-match org-plain-time-of-day-regexp ans)
17072 (match-end 8))
17073 (setq org-end-time-was-given (match-string 8 ans))
17074 (setq ans (concat (substring ans 0 (match-beginning 7))
17075 (substring ans (match-end 7))))))
17077 (setq tl (parse-time-string ans)
17078 day (or (nth 3 tl) (nth 3 org-defdecode))
17079 month
17080 (cond ((nth 4 tl))
17081 ((not org-read-date-prefer-future) (nth 4 org-defdecode))
17082 ;; Day was specified. Make sure DAY+MONTH
17083 ;; combination happens in the future.
17084 ((nth 3 tl)
17085 (setq futurep t)
17086 (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
17087 (nth 4 nowdecode)))
17088 (t (nth 4 org-defdecode)))
17089 year
17090 (cond ((and (not kill-year) (nth 5 tl)))
17091 ((not org-read-date-prefer-future) (nth 5 org-defdecode))
17092 ;; Month was guessed in the future and is at least
17093 ;; equal to NOWDECODE's. Fix year accordingly.
17094 (futurep
17095 (if (or (> month (nth 4 nowdecode))
17096 (>= day (nth 3 nowdecode)))
17097 (nth 5 nowdecode)
17098 (1+ (nth 5 nowdecode))))
17099 ;; Month was specified. Make sure MONTH+YEAR
17100 ;; combination happens in the future.
17101 ((nth 4 tl)
17102 (setq futurep t)
17103 (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
17104 ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
17105 ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
17106 (t (nth 5 nowdecode))))
17107 (t (nth 5 org-defdecode)))
17108 hour (or (nth 2 tl) (nth 2 org-defdecode))
17109 minute (or (nth 1 tl) (nth 1 org-defdecode))
17110 second (or (nth 0 tl) 0)
17111 wday (nth 6 tl))
17113 (when (and (eq org-read-date-prefer-future 'time)
17114 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
17115 (equal day (nth 3 nowdecode))
17116 (equal month (nth 4 nowdecode))
17117 (equal year (nth 5 nowdecode))
17118 (nth 2 tl)
17119 (or (< (nth 2 tl) (nth 2 nowdecode))
17120 (and (= (nth 2 tl) (nth 2 nowdecode))
17121 (nth 1 tl)
17122 (< (nth 1 tl) (nth 1 nowdecode)))))
17123 (setq day (1+ day)
17124 futurep t))
17126 ;; Special date definitions below
17127 (cond
17128 (iso-week
17129 ;; There was an iso week
17130 (require 'cal-iso)
17131 (setq futurep nil)
17132 (setq year (or iso-year year)
17133 day (or iso-weekday wday 1)
17134 wday nil ; to make sure that the trigger below does not match
17135 iso-date (calendar-gregorian-from-absolute
17136 (calendar-iso-to-absolute
17137 (list iso-week day year))))
17138 ; FIXME: Should we also push ISO weeks into the future?
17139 ; (when (and org-read-date-prefer-future
17140 ; (not iso-year)
17141 ; (< (calendar-absolute-from-gregorian iso-date)
17142 ; (time-to-days (current-time))))
17143 ; (setq year (1+ year)
17144 ; iso-date (calendar-gregorian-from-absolute
17145 ; (calendar-iso-to-absolute
17146 ; (list iso-week day year)))))
17147 (setq month (car iso-date)
17148 year (nth 2 iso-date)
17149 day (nth 1 iso-date)))
17150 (deltan
17151 (setq futurep nil)
17152 (unless deltadef
17153 ;; Pass `current-time' result to `decode-time' (instead of
17154 ;; calling without arguments) so that only `current-time' has
17155 ;; to be overridden in tests.
17156 (let ((now (decode-time (current-time))))
17157 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
17158 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
17159 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
17160 ((equal deltaw "m") (setq month (+ month deltan)))
17161 ((equal deltaw "y") (setq year (+ year deltan)))))
17162 ((and wday (not (nth 3 tl)))
17163 ;; Weekday was given, but no day, so pick that day in the week
17164 ;; on or after the derived date.
17165 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
17166 (unless (equal wday wday1)
17167 (setq day (+ day (% (- wday wday1 -7) 7))))))
17168 (when (and (boundp 'org-time-was-given)
17169 (nth 2 tl))
17170 (setq org-time-was-given t))
17171 (when (< year 100) (setq year (+ 2000 year)))
17172 ;; Check of the date is representable
17173 (if org-read-date-force-compatible-dates
17174 (progn
17175 (when (< year 1970)
17176 (setq year 1970 org-read-date-analyze-forced-year t))
17177 (when (> year 2037)
17178 (setq year 2037 org-read-date-analyze-forced-year t)))
17179 (condition-case nil
17180 (ignore (encode-time second minute hour day month year))
17181 (error
17182 (setq year (nth 5 org-defdecode))
17183 (setq org-read-date-analyze-forced-year t))))
17184 (setq org-read-date-analyze-futurep futurep)
17185 (list second minute hour day month year)))
17187 (defvar parse-time-weekdays)
17188 (defun org-read-date-get-relative (s today default)
17189 "Check string S for special relative date string.
17190 TODAY and DEFAULT are internal times, for today and for a default.
17191 Return shift list (N what def-flag)
17192 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
17193 N is the number of WHATs to shift.
17194 DEF-FLAG is t when a double ++ or -- indicates shift relative to
17195 the DEFAULT date rather than TODAY."
17196 (require 'parse-time)
17197 (when (and
17198 (string-match
17199 (concat
17200 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
17201 "\\([0-9]+\\)?"
17202 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
17203 "\\([ \t]\\|$\\)") s)
17204 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
17205 (let* ((dir (if (> (match-end 1) (match-beginning 1))
17206 (string-to-char (substring (match-string 1 s) -1))
17207 ?+))
17208 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
17209 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
17210 (what (if (match-end 3) (match-string 3 s) "d"))
17211 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
17212 (date (if rel default today))
17213 (wday (nth 6 (decode-time date)))
17214 delta)
17215 (if wday1
17216 (progn
17217 (setq delta (mod (+ 7 (- wday1 wday)) 7))
17218 (when (= delta 0) (setq delta 7))
17219 (when (= dir ?-)
17220 (setq delta (- delta 7))
17221 (when (= delta 0) (setq delta -7)))
17222 (when (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
17223 (list delta "d" rel))
17224 (list (* n (if (= dir ?-) -1 1)) what rel)))))
17226 (defun org-order-calendar-date-args (arg1 arg2 arg3)
17227 "Turn a user-specified date into the internal representation.
17228 The internal representation needed by the calendar is (month day year).
17229 This is a wrapper to handle the brain-dead convention in calendar that
17230 user function argument order change dependent on argument order."
17231 (pcase calendar-date-style
17232 (`american (list arg1 arg2 arg3))
17233 (`european (list arg2 arg1 arg3))
17234 (`iso (list arg2 arg3 arg1))))
17236 (defun org-eval-in-calendar (form &optional keepdate)
17237 "Eval FORM in the calendar window and return to current window.
17238 Unless KEEPDATE is non-nil, update `org-ans2' to the cursor date."
17239 (let ((sf (selected-frame))
17240 (sw (selected-window)))
17241 (select-window (get-buffer-window "*Calendar*" t))
17242 (eval form)
17243 (when (and (not keepdate) (calendar-cursor-to-date))
17244 (let* ((date (calendar-cursor-to-date))
17245 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
17246 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
17247 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
17248 (select-window sw)
17249 (select-frame-set-input-focus sf)))
17251 (defun org-calendar-select ()
17252 "Return to `org-read-date' with the date currently selected.
17253 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
17254 (interactive)
17255 (when (calendar-cursor-to-date)
17256 (let* ((date (calendar-cursor-to-date))
17257 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
17258 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
17259 (when (active-minibuffer-window) (exit-minibuffer))))
17261 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
17262 "Insert a date stamp for the date given by the internal TIME.
17263 See `format-time-string' for the format of TIME.
17264 WITH-HM means use the stamp format that includes the time of the day.
17265 INACTIVE means use square brackets instead of angular ones, so that the
17266 stamp will not contribute to the agenda.
17267 PRE and POST are optional strings to be inserted before and after the
17268 stamp.
17269 The command returns the inserted time stamp."
17270 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
17271 stamp)
17272 (when inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
17273 (insert-before-markers (or pre ""))
17274 (when (listp extra)
17275 (setq extra (car extra))
17276 (if (and (stringp extra)
17277 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
17278 (setq extra (format "-%02d:%02d"
17279 (string-to-number (match-string 1 extra))
17280 (string-to-number (match-string 2 extra))))
17281 (setq extra nil)))
17282 (when extra
17283 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
17284 (insert-before-markers (setq stamp (format-time-string fmt time)))
17285 (insert-before-markers (or post ""))
17286 (setq org-last-inserted-timestamp stamp)))
17288 (defun org-toggle-time-stamp-overlays ()
17289 "Toggle the use of custom time stamp formats."
17290 (interactive)
17291 (setq org-display-custom-times (not org-display-custom-times))
17292 (unless org-display-custom-times
17293 (let ((p (point-min)) (bmp (buffer-modified-p)))
17294 (while (setq p (next-single-property-change p 'display))
17295 (when (and (get-text-property p 'display)
17296 (eq (get-text-property p 'face) 'org-date))
17297 (remove-text-properties
17298 p (setq p (next-single-property-change p 'display))
17299 '(display t))))
17300 (set-buffer-modified-p bmp)))
17301 (org-restart-font-lock)
17302 (setq org-table-may-need-update t)
17303 (if org-display-custom-times
17304 (message "Time stamps are overlaid with custom format")
17305 (message "Time stamp overlays removed")))
17307 (defun org-display-custom-time (beg end)
17308 "Overlay modified time stamp format over timestamp between BEG and END."
17309 (let* ((ts (buffer-substring beg end))
17310 t1 with-hm tf time str (off 0))
17311 (save-match-data
17312 (setq t1 (org-parse-time-string ts t))
17313 (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
17314 (setq off (- (match-end 0) (match-beginning 0)))))
17315 (setq end (- end off))
17316 (setq with-hm (and (nth 1 t1) (nth 2 t1))
17317 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
17318 time (org-fix-decoded-time t1)
17319 str (org-add-props
17320 (format-time-string
17321 (substring tf 1 -1) (apply 'encode-time time))
17322 nil 'mouse-face 'highlight))
17323 (put-text-property beg end 'display str)))
17325 (defun org-fix-decoded-time (time)
17326 "Set 0 instead of nil for the first 6 elements of time.
17327 Don't touch the rest."
17328 (let ((n 0))
17329 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
17331 (defun org-time-stamp-to-now (timestamp-string &optional seconds)
17332 "Difference between TIMESTAMP-STRING and now in days.
17333 If SECONDS is non-nil, return the difference in seconds."
17334 (let ((fdiff (if seconds #'float-time #'time-to-days)))
17335 (- (funcall fdiff (org-time-string-to-time timestamp-string))
17336 (funcall fdiff (current-time)))))
17338 (defun org-deadline-close-p (timestamp-string &optional ndays)
17339 "Is the time in TIMESTAMP-STRING close to the current date?"
17340 (setq ndays (or ndays (org-get-wdays timestamp-string)))
17341 (and (<= (org-time-stamp-to-now timestamp-string) ndays)
17342 (not (org-entry-is-done-p))))
17344 (defun org-get-wdays (ts &optional delay zero-delay)
17345 "Get the deadline lead time appropriate for timestring TS.
17346 When DELAY is non-nil, get the delay time for scheduled items
17347 instead of the deadline lead time. When ZERO-DELAY is non-nil
17348 and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
17349 don't try to find the delay cookie in the scheduled timestamp."
17350 (let ((tv (if delay org-scheduled-delay-days
17351 org-deadline-warning-days)))
17352 (cond
17353 ((or (and delay (< tv 0))
17354 (and delay zero-delay (<= tv 0))
17355 (and (not delay) (<= tv 0)))
17356 ;; Enforce this value no matter what
17357 (- tv))
17358 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
17359 ;; lead time is specified.
17360 (floor (* (string-to-number (match-string 1 ts))
17361 (cdr (assoc (match-string 2 ts)
17362 '(("d" . 1) ("w" . 7)
17363 ("m" . 30.4) ("y" . 365.25)
17364 ("h" . 0.041667)))))))
17365 ;; go for the default.
17366 (t tv))))
17368 (defun org-calendar-select-mouse (ev)
17369 "Return to `org-read-date' with the date currently selected.
17370 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
17371 (interactive "e")
17372 (mouse-set-point ev)
17373 (when (calendar-cursor-to-date)
17374 (let* ((date (calendar-cursor-to-date))
17375 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
17376 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
17377 (when (active-minibuffer-window) (exit-minibuffer))))
17379 (defun org-check-deadlines (ndays)
17380 "Check if there are any deadlines due or past due.
17381 A deadline is considered due if it happens within `org-deadline-warning-days'
17382 days from today's date. If the deadline appears in an entry marked DONE,
17383 it is not shown. A numeric prefix argument NDAYS can be used to test that
17384 many days. If the prefix is a raw `\\[universal-argument]', all deadlines \
17385 are shown."
17386 (interactive "P")
17387 (let* ((org-warn-days
17388 (cond
17389 ((equal ndays '(4)) 100000)
17390 (ndays (prefix-numeric-value ndays))
17391 (t (abs org-deadline-warning-days))))
17392 (case-fold-search nil)
17393 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
17394 (callback
17395 (lambda () (org-deadline-close-p (match-string 1) org-warn-days))))
17396 (message "%d deadlines past-due or due within %d days"
17397 (org-occur regexp nil callback)
17398 org-warn-days)))
17400 (defsubst org-re-timestamp (type)
17401 "Return a regexp for timestamp TYPE.
17402 Allowed values for TYPE are:
17404 all: all timestamps
17405 active: only active timestamps (<...>)
17406 inactive: only inactive timestamps ([...])
17407 scheduled: only scheduled timestamps
17408 deadline: only deadline timestamps
17409 closed: only closed time-stamps
17411 When TYPE is nil, fall back on returning a regexp that matches
17412 both scheduled and deadline timestamps."
17413 (cl-case type
17414 (all org-ts-regexp-both)
17415 (active org-ts-regexp)
17416 (inactive org-ts-regexp-inactive)
17417 (scheduled org-scheduled-time-regexp)
17418 (deadline org-deadline-time-regexp)
17419 (closed org-closed-time-regexp)
17420 (otherwise
17421 (concat "\\<"
17422 (regexp-opt (list org-deadline-string org-scheduled-string))
17423 " *<\\([^>]+\\)>"))))
17425 (defun org-check-before-date (d)
17426 "Check if there are deadlines or scheduled entries before date D."
17427 (interactive (list (org-read-date)))
17428 (let* ((case-fold-search nil)
17429 (regexp (org-re-timestamp org-ts-type))
17430 (ts-type org-ts-type)
17431 (callback
17432 (lambda ()
17433 (let ((match (match-string 1)))
17434 (and (if (memq ts-type '(active inactive all))
17435 (eq (org-element-type (save-excursion
17436 (backward-char)
17437 (org-element-context)))
17438 'timestamp)
17439 (org-at-planning-p))
17440 (time-less-p
17441 (org-time-string-to-time match)
17442 (org-time-string-to-time d)))))))
17443 (message "%d entries before %s"
17444 (org-occur regexp nil callback)
17445 d)))
17447 (defun org-check-after-date (d)
17448 "Check if there are deadlines or scheduled entries after date D."
17449 (interactive (list (org-read-date)))
17450 (let* ((case-fold-search nil)
17451 (regexp (org-re-timestamp org-ts-type))
17452 (ts-type org-ts-type)
17453 (callback
17454 (lambda ()
17455 (let ((match (match-string 1)))
17456 (and (if (memq ts-type '(active inactive all))
17457 (eq (org-element-type (save-excursion
17458 (backward-char)
17459 (org-element-context)))
17460 'timestamp)
17461 (org-at-planning-p))
17462 (not (time-less-p
17463 (org-time-string-to-time match)
17464 (org-time-string-to-time d))))))))
17465 (message "%d entries after %s"
17466 (org-occur regexp nil callback)
17467 d)))
17469 (defun org-check-dates-range (start-date end-date)
17470 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
17471 (interactive (list (org-read-date nil nil nil "Range starts")
17472 (org-read-date nil nil nil "Range end")))
17473 (let ((case-fold-search nil)
17474 (regexp (org-re-timestamp org-ts-type))
17475 (callback
17476 (let ((type org-ts-type))
17477 (lambda ()
17478 (let ((match (match-string 1)))
17479 (and
17480 (if (memq type '(active inactive all))
17481 (eq (org-element-type (save-excursion
17482 (backward-char)
17483 (org-element-context)))
17484 'timestamp)
17485 (org-at-planning-p))
17486 (not (time-less-p
17487 (org-time-string-to-time match)
17488 (org-time-string-to-time start-date)))
17489 (time-less-p
17490 (org-time-string-to-time match)
17491 (org-time-string-to-time end-date))))))))
17492 (message "%d entries between %s and %s"
17493 (org-occur regexp nil callback) start-date end-date)))
17495 (defun org-evaluate-time-range (&optional to-buffer)
17496 "Evaluate a time range by computing the difference between start and end.
17497 Normally the result is just printed in the echo area, but with prefix arg
17498 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
17499 If the time range is actually in a table, the result is inserted into the
17500 next column.
17501 For time difference computation, a year is assumed to be exactly 365
17502 days in order to avoid rounding problems."
17503 (interactive "P")
17505 (org-clock-update-time-maybe)
17506 (save-excursion
17507 (unless (org-at-date-range-p t)
17508 (goto-char (point-at-bol))
17509 (re-search-forward org-tr-regexp-both (point-at-eol) t))
17510 (unless (org-at-date-range-p t)
17511 (user-error "Not at a time-stamp range, and none found in current line")))
17512 (let* ((ts1 (match-string 1))
17513 (ts2 (match-string 2))
17514 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
17515 (match-end (match-end 0))
17516 (time1 (org-time-string-to-time ts1))
17517 (time2 (org-time-string-to-time ts2))
17518 (t1 (float-time time1))
17519 (t2 (float-time time2))
17520 (diff (abs (- t2 t1)))
17521 (negative (< (- t2 t1) 0))
17522 ;; (ys (floor (* 365 24 60 60)))
17523 (ds (* 24 60 60))
17524 (hs (* 60 60))
17525 (fy "%dy %dd %02d:%02d")
17526 (fy1 "%dy %dd")
17527 (fd "%dd %02d:%02d")
17528 (fd1 "%dd")
17529 (fh "%02d:%02d")
17530 y d h m align)
17531 (if havetime
17532 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
17534 d (floor (/ diff ds)) diff (mod diff ds)
17535 h (floor (/ diff hs)) diff (mod diff hs)
17536 m (floor (/ diff 60)))
17537 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
17539 d (floor (+ (/ diff ds) 0.5))
17540 h 0 m 0))
17541 (if (not to-buffer)
17542 (message "%s" (org-make-tdiff-string y d h m))
17543 (if (org-at-table-p)
17544 (progn
17545 (goto-char match-end)
17546 (setq align t)
17547 (and (looking-at " *|") (goto-char (match-end 0))))
17548 (goto-char match-end))
17549 (when (looking-at
17550 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
17551 (replace-match ""))
17552 (when negative (insert " -"))
17553 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
17554 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
17555 (insert " " (format fh h m))))
17556 (when align (org-table-align))
17557 (message "Time difference inserted")))))
17559 (defun org-make-tdiff-string (y d h m)
17560 (let ((fmt "")
17561 (l nil))
17562 (when (> y 0)
17563 (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " "))
17564 (push y l))
17565 (when (> d 0)
17566 (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " "))
17567 (push d l))
17568 (when (> h 0)
17569 (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " "))
17570 (push h l))
17571 (when (> m 0)
17572 (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " "))
17573 (push m l))
17574 (apply 'format fmt (nreverse l))))
17576 (defun org-time-string-to-time (s)
17577 "Convert timestamp string S into internal time."
17578 (apply #'encode-time (org-parse-time-string s)))
17580 (defun org-time-string-to-seconds (s)
17581 "Convert a timestamp string S into a number of seconds."
17582 (float-time (org-time-string-to-time s)))
17584 (org-define-error 'org-diary-sexp-no-match "Unable to match diary sexp")
17586 (defun org-time-string-to-absolute (s &optional daynr prefer buffer pos)
17587 "Convert time stamp S to an absolute day number.
17589 If DAYNR in non-nil, and there is a specifier for a cyclic time
17590 stamp, get the closest date to DAYNR. If PREFER is
17591 `past' (respectively `future') return a date past (respectively
17592 after) or equal to DAYNR.
17594 POS is the location of time stamp S, as a buffer position in
17595 BUFFER.
17597 Diary sexp timestamps are matched against DAYNR, when non-nil.
17598 If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
17599 signaled."
17600 (cond
17601 ((string-match "\\`%%\\((.*)\\)" s)
17602 ;; Sexp timestamp: try to match DAYNR, if available, since we're
17603 ;; only able to match individual dates. If it fails, raise an
17604 ;; error.
17605 (if (and daynr
17606 (org-diary-sexp-entry
17607 (match-string 1 s) "" (calendar-gregorian-from-absolute daynr)))
17608 daynr
17609 (signal 'org-diary-sexp-no-match (list s))))
17610 (daynr (org-closest-date s daynr prefer))
17611 (t (time-to-days
17612 (condition-case errdata
17613 (apply #'encode-time (org-parse-time-string s))
17614 (error (error "Bad timestamp `%s'%s\nError was: %s"
17616 (if (not (and buffer pos)) ""
17617 (format-message " at %d in buffer `%s'" pos buffer))
17618 (cdr errdata))))))))
17620 (defun org-days-to-iso-week (days)
17621 "Return the iso week number."
17622 (require 'cal-iso)
17623 (car (calendar-iso-from-absolute days)))
17625 (defun org-small-year-to-year (year)
17626 "Convert 2-digit years into 4-digit years.
17627 YEAR is expanded into one of the 30 next years, if possible, or
17628 into a past one. Any year larger than 99 is returned unchanged."
17629 (if (>= year 100) year
17630 (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
17631 (century (/ current 100))
17632 (offset (- year (% current 100))))
17633 (cond ((> offset 30) (+ (* (1- century) 100) year))
17634 ((> offset -70) (+ (* century 100) year))
17635 (t (+ (* (1+ century) 100) year))))))
17637 (defun org-time-from-absolute (d)
17638 "Return the time corresponding to date D.
17639 D may be an absolute day number, or a calendar-type list (month day year)."
17640 (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
17641 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
17643 (defvar org-agenda-current-date)
17644 (defun org-calendar-holiday ()
17645 "List of holidays, for Diary display in Org mode."
17646 (require 'holidays)
17647 (let ((hl (calendar-check-holidays org-agenda-current-date)))
17648 (and hl (mapconcat #'identity hl "; "))))
17650 (defun org-diary-sexp-entry (sexp entry d)
17651 "Process a SEXP diary ENTRY for date D."
17652 (require 'diary-lib)
17653 ;; `org-anniversary' and alike expect ENTRY and DATE to be bound
17654 ;; dynamically.
17655 (let* ((sexp `(let ((entry ,entry)
17656 (date ',d))
17657 ,(car (read-from-string sexp))))
17658 (result (if calendar-debug-sexp (eval sexp)
17659 (condition-case nil
17660 (eval sexp)
17661 (error
17662 (beep)
17663 (message "Bad sexp at line %d in %s: %s"
17664 (org-current-line)
17665 (buffer-file-name) sexp)
17666 (sleep-for 2))))))
17667 (cond ((stringp result) (split-string result "; "))
17668 ((and (consp result)
17669 (not (consp (cdr result)))
17670 (stringp (cdr result))) (cdr result))
17671 ((and (consp result)
17672 (stringp (car result))) result)
17673 (result entry))))
17675 (defun org-diary-to-ical-string (frombuf)
17676 "Get iCalendar entries from diary entries in buffer FROMBUF.
17677 This uses the icalendar.el library."
17678 (let* ((tmpdir temporary-file-directory)
17679 (tmpfile (make-temp-name
17680 (expand-file-name "orgics" tmpdir)))
17681 buf rtn b e)
17682 (with-current-buffer frombuf
17683 (icalendar-export-region (point-min) (point-max) tmpfile)
17684 (setq buf (find-buffer-visiting tmpfile))
17685 (set-buffer buf)
17686 (goto-char (point-min))
17687 (when (re-search-forward "^BEGIN:VEVENT" nil t)
17688 (setq b (match-beginning 0)))
17689 (goto-char (point-max))
17690 (when (re-search-backward "^END:VEVENT" nil t)
17691 (setq e (match-end 0)))
17692 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
17693 (kill-buffer buf)
17694 (delete-file tmpfile)
17695 rtn))
17697 (defun org-closest-date (start current prefer)
17698 "Return closest date to CURRENT starting from START.
17700 CURRENT and START are both time stamps.
17702 When PREFER is `past', return a date that is either CURRENT or
17703 past. When PREFER is `future', return a date that is either
17704 CURRENT or future.
17706 Only time stamps with a repeater are modified. Any other time
17707 stamp stay unchanged. In any case, return value is an absolute
17708 day number."
17709 (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start))
17710 ;; No repeater. Do not shift time stamp.
17711 (time-to-days (apply #'encode-time (org-parse-time-string start)))
17712 (let ((value (string-to-number (match-string 1 start)))
17713 (type (match-string 2 start)))
17714 (if (= 0 value)
17715 ;; Repeater with a 0-value is considered as void.
17716 (time-to-days (apply #'encode-time (org-parse-time-string start)))
17717 (let* ((base (org-date-to-gregorian start))
17718 (target (org-date-to-gregorian current))
17719 (sday (calendar-absolute-from-gregorian base))
17720 (cday (calendar-absolute-from-gregorian target))
17721 n1 n2)
17722 ;; If START is already past CURRENT, just return START.
17723 (if (<= cday sday) sday
17724 ;; Compute closest date before (N1) and closest date past
17725 ;; (N2) CURRENT.
17726 (pcase type
17727 ("h"
17728 (let ((missing-hours
17729 (mod (+ (- (* 24 (- cday sday))
17730 (nth 2 (org-parse-time-string start)))
17731 org-extend-today-until)
17732 value)))
17733 (setf n1 (if (= missing-hours 0) cday
17734 (- cday (1+ (/ missing-hours 24)))))
17735 (setf n2 (+ cday (/ (- value missing-hours) 24)))))
17736 ((or "d" "w")
17737 (let ((value (if (equal type "w") (* 7 value) value)))
17738 (setf n1 (+ sday (* value (/ (- cday sday) value))))
17739 (setf n2 (+ n1 value))))
17740 ("m"
17741 (let* ((add-months
17742 (lambda (d n)
17743 ;; Add N months to gregorian date D, i.e.,
17744 ;; a list (MONTH DAY YEAR). Return a valid
17745 ;; gregorian date.
17746 (let ((m (+ (nth 0 d) n)))
17747 (list (mod m 12)
17748 (nth 1 d)
17749 (+ (/ m 12) (nth 2 d))))))
17750 (months ; Complete months to TARGET.
17751 (* (/ (+ (* 12 (- (nth 2 target) (nth 2 base)))
17752 (- (nth 0 target) (nth 0 base))
17753 ;; If START's day is greater than
17754 ;; TARGET's, remove incomplete month.
17755 (if (> (nth 1 target) (nth 1 base)) 0 -1))
17756 value)
17757 value))
17758 (before (funcall add-months base months)))
17759 (setf n1 (calendar-absolute-from-gregorian before))
17760 (setf n2
17761 (calendar-absolute-from-gregorian
17762 (funcall add-months before value)))))
17764 (let* ((d (nth 1 base))
17765 (m (nth 0 base))
17766 (y (nth 2 base))
17767 (years ; Complete years to TARGET.
17768 (* (/ (- (nth 2 target)
17770 ;; If START's month and day are
17771 ;; greater than TARGET's, remove
17772 ;; incomplete year.
17773 (if (or (> (nth 0 target) m)
17774 (and (= (nth 0 target) m)
17775 (> (nth 1 target) d)))
17778 value)
17779 value))
17780 (before (list m d (+ y years))))
17781 (setf n1 (calendar-absolute-from-gregorian before))
17782 (setf n2 (calendar-absolute-from-gregorian
17783 (list m d (+ (nth 2 before) value)))))))
17784 ;; Handle PREFER parameter, if any.
17785 (cond
17786 ((eq prefer 'past) (if (= cday n2) n2 n1))
17787 ((eq prefer 'future) (if (= cday n1) n1 n2))
17788 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))))))))
17790 (defun org-date-to-gregorian (d)
17791 "Turn any specification of date D into a Gregorian date for the calendar."
17792 (cond ((integerp d) (calendar-gregorian-from-absolute d))
17793 ((and (listp d) (= (length d) 3)) d)
17794 ((stringp d)
17795 (let ((d (org-parse-time-string d)))
17796 (list (nth 4 d) (nth 3 d) (nth 5 d))))
17797 ((listp d) (list (nth 4 d) (nth 3 d) (nth 5 d)))))
17799 (defun org-parse-time-string (s &optional nodefault)
17800 "Parse the standard Org time string.
17802 This should be a lot faster than the normal `parse-time-string'.
17804 If time is not given, defaults to 0:00. However, with optional
17805 NODEFAULT, hour and minute fields will be nil if not given."
17806 (cond ((string-match org-ts-regexp0 s)
17807 (list 0
17808 (when (or (match-beginning 8) (not nodefault))
17809 (string-to-number (or (match-string 8 s) "0")))
17810 (when (or (match-beginning 7) (not nodefault))
17811 (string-to-number (or (match-string 7 s) "0")))
17812 (string-to-number (match-string 4 s))
17813 (string-to-number (match-string 3 s))
17814 (string-to-number (match-string 2 s))
17815 nil nil nil))
17816 ((string-match "^<[^>]+>$" s)
17817 ;; FIXME: `decode-time' needs to be called with ZONE as its
17818 ;; second argument. However, this requires at least Emacs
17819 ;; 25.1. We can do it when we switch to this version as our
17820 ;; minimal requirement.
17821 (decode-time (seconds-to-time (org-matcher-time s))))
17822 (t (error "Not a standard Org time string: %s" s))))
17824 (defun org-timestamp-up (&optional arg)
17825 "Increase the date item at the cursor by one.
17826 If the cursor is on the year, change the year. If it is on the month,
17827 the day or the time, change that. If the cursor is on the enclosing
17828 bracket, change the timestamp type.
17829 With prefix ARG, change by that many units."
17830 (interactive "p")
17831 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
17833 (defun org-timestamp-down (&optional arg)
17834 "Decrease the date item at the cursor by one.
17835 If the cursor is on the year, change the year. If it is on the month,
17836 the day or the time, change that. If the cursor is on the enclosing
17837 bracket, change the timestamp type.
17838 With prefix ARG, change by that many units."
17839 (interactive "p")
17840 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
17842 (defun org-timestamp-up-day (&optional arg)
17843 "Increase the date in the time stamp by one day.
17844 With prefix ARG, change that many days."
17845 (interactive "p")
17846 (if (and (not (org-at-timestamp-p 'lax))
17847 (org-at-heading-p))
17848 (org-todo 'up)
17849 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
17851 (defun org-timestamp-down-day (&optional arg)
17852 "Decrease the date in the time stamp by one day.
17853 With prefix ARG, change that many days."
17854 (interactive "p")
17855 (if (and (not (org-at-timestamp-p 'lax))
17856 (org-at-heading-p))
17857 (org-todo 'down)
17858 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
17860 (defun org-at-timestamp-p (&optional extended)
17861 "Non-nil if point is inside a timestamp.
17863 By default, the function only consider syntactically valid active
17864 timestamps. However, the caller may have a broader definition
17865 for timestamps. As a consequence, optional argument EXTENDED can
17866 be set to the following values
17868 `inactive'
17870 Include also syntactically valid inactive timestamps.
17872 `agenda'
17874 Include timestamps allowed in Agenda, i.e., those in
17875 properties drawers, planning lines and clock lines.
17877 `lax'
17879 Ignore context. The function matches any part of the
17880 document looking like a timestamp. This includes comments,
17881 example blocks...
17883 For backward-compatibility with Org 9.0, every other non-nil
17884 value is equivalent to `inactive'.
17886 When at a timestamp, return the position of the point as a symbol
17887 among `bracket', `after', `year', `month', `hour', `minute',
17888 `day' or a number of character from the last know part of the
17889 time stamp.
17891 When matching, the match groups are the following:
17892 group 1: year
17893 group 2: month
17894 group 3: day number
17895 group 4: day name
17896 group 5: hours, if any
17897 group 6: minutes, if any"
17898 (let* ((regexp (if extended org-ts-regexp3 org-ts-regexp2))
17899 (pos (point))
17900 (match?
17901 (let ((boundaries (org-in-regexp regexp)))
17902 (save-match-data
17903 (cond ((null boundaries) nil)
17904 ((eq extended 'lax) t)
17906 (or (and (eq extended 'agenda)
17907 (or (org-at-planning-p)
17908 (org-at-property-p)
17909 (and (bound-and-true-p
17910 org-agenda-include-inactive-timestamps)
17911 (org-at-clock-log-p))))
17912 (eq 'timestamp
17913 (save-excursion
17914 (when (= pos (cdr boundaries)) (forward-char -1))
17915 (org-element-type (org-element-context)))))))))))
17916 (cond
17917 ((not match?) nil)
17918 ((= pos (match-beginning 0)) 'bracket)
17919 ;; Distinguish location right before the closing bracket from
17920 ;; right after it.
17921 ((= pos (1- (match-end 0))) 'bracket)
17922 ((= pos (match-end 0)) 'after)
17923 ((org-pos-in-match-range pos 2) 'year)
17924 ((org-pos-in-match-range pos 3) 'month)
17925 ((org-pos-in-match-range pos 7) 'hour)
17926 ((org-pos-in-match-range pos 8) 'minute)
17927 ((or (org-pos-in-match-range pos 4)
17928 (org-pos-in-match-range pos 5)) 'day)
17929 ((and (> pos (or (match-end 8) (match-end 5)))
17930 (< pos (match-end 0)))
17931 (- pos (or (match-end 8) (match-end 5))))
17932 (t 'day))))
17934 (defun org-toggle-timestamp-type ()
17935 "Toggle the type (<active> or [inactive]) of a time stamp."
17936 (interactive)
17937 (when (org-at-timestamp-p 'lax)
17938 (let ((beg (match-beginning 0)) (end (match-end 0))
17939 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
17940 (save-excursion
17941 (goto-char beg)
17942 (while (re-search-forward "[][<>]" end t)
17943 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
17944 t t)))
17945 (message "Timestamp is now %sactive"
17946 (if (equal (char-after beg) ?<) "" "in")))))
17948 (defun org-at-clock-log-p ()
17949 "Non-nil if point is on a clock log line."
17950 (and (org-match-line org-clock-line-re)
17951 (eq (org-element-type (save-match-data (org-element-at-point))) 'clock)))
17953 (defvar org-clock-history) ; defined in org-clock.el
17954 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
17955 (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
17956 "Change the date in the time stamp at point.
17957 The date will be changed by N times WHAT. WHAT can be `day', `month',
17958 `year', `minute', `second'. If WHAT is not given, the cursor position
17959 in the timestamp determines what will be changed.
17960 When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
17961 (let ((origin (point))
17962 (timestamp? (org-at-timestamp-p 'lax))
17963 origin-cat
17964 with-hm inactive
17965 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
17966 extra rem
17967 ts time time0 fixnext clrgx)
17968 (unless timestamp? (user-error "Not at a timestamp"))
17969 (if (and (not what) (eq timestamp? 'bracket))
17970 (org-toggle-timestamp-type)
17971 ;; Point isn't on brackets. Remember the part of the time-stamp
17972 ;; the point was in. Indeed, size of time-stamps may change,
17973 ;; but point must be kept in the same category nonetheless.
17974 (setq origin-cat timestamp?)
17975 (when (and (not what) (not (eq timestamp? 'day))
17976 org-display-custom-times
17977 (get-text-property (point) 'display)
17978 (not (get-text-property (1- (point)) 'display)))
17979 (setq timestamp? 'day))
17980 (setq timestamp? (or what timestamp?)
17981 inactive (= (char-after (match-beginning 0)) ?\[)
17982 ts (match-string 0))
17983 (replace-match "")
17984 (when (string-match
17985 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
17987 (setq extra (match-string 1 ts))
17988 (when suppress-tmp-delay
17989 (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
17990 (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
17991 (setq with-hm t))
17992 (setq time0 (org-parse-time-string ts))
17993 (when (and updown
17994 (eq timestamp? 'minute)
17995 (not current-prefix-arg))
17996 ;; This looks like s-up and s-down. Change by one rounding step.
17997 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
17998 (unless (= 0 (setq rem (% (nth 1 time0) dm)))
17999 (setcar (cdr time0) (+ (nth 1 time0)
18000 (if (> n 0) (- rem) (- dm rem))))))
18001 (setq time
18002 (apply #'encode-time
18003 (or (car time0) 0)
18004 (+ (if (eq timestamp? 'minute) n 0) (nth 1 time0))
18005 (+ (if (eq timestamp? 'hour) n 0) (nth 2 time0))
18006 (+ (if (eq timestamp? 'day) n 0) (nth 3 time0))
18007 (+ (if (eq timestamp? 'month) n 0) (nth 4 time0))
18008 (+ (if (eq timestamp? 'year) n 0) (nth 5 time0))
18009 (nthcdr 6 time0)))
18010 (when (and (memq timestamp? '(hour minute))
18011 extra
18012 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
18013 (setq extra (org-modify-ts-extra
18014 extra
18015 (if (eq timestamp? 'hour) 2 5)
18016 n dm)))
18017 (when (integerp timestamp?)
18018 (setq extra (org-modify-ts-extra extra timestamp? n dm)))
18019 (when (eq what 'calendar)
18020 (let ((cal-date (org-get-date-from-calendar)))
18021 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
18022 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
18023 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
18024 (setcar time0 (or (car time0) 0))
18025 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
18026 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
18027 (setq time (apply 'encode-time time0))))
18028 ;; Insert the new time-stamp, and ensure point stays in the same
18029 ;; category as before (i.e. not after the last position in that
18030 ;; category).
18031 (let ((pos (point)))
18032 ;; Stay before inserted string. `save-excursion' is of no use.
18033 (setq org-last-changed-timestamp
18034 (org-insert-time-stamp time with-hm inactive nil nil extra))
18035 (goto-char pos))
18036 (save-match-data
18037 (looking-at org-ts-regexp3)
18038 (goto-char
18039 (pcase origin-cat
18040 ;; `day' category ends before `hour' if any, or at the end
18041 ;; of the day name.
18042 (`day (min (or (match-beginning 7) (1- (match-end 5))) origin))
18043 (`hour (min (match-end 7) origin))
18044 (`minute (min (1- (match-end 8)) origin))
18045 ((pred integerp) (min (1- (match-end 0)) origin))
18046 ;; Point was right after the time-stamp. However, the
18047 ;; time-stamp length might have changed, so refer to
18048 ;; (match-end 0) instead.
18049 (`after (match-end 0))
18050 ;; `year' and `month' have both fixed size: point couldn't
18051 ;; have moved into another part.
18052 (_ origin))))
18053 ;; Update clock if on a CLOCK line.
18054 (org-clock-update-time-maybe)
18055 ;; Maybe adjust the closest clock in `org-clock-history'
18056 (when org-clock-adjust-closest
18057 (if (not (and (org-at-clock-log-p)
18058 (< 1 (length (delq nil (mapcar 'marker-position
18059 org-clock-history))))))
18060 (message "No clock to adjust")
18061 (cond ((save-excursion ; fix previous clock?
18062 (re-search-backward org-ts-regexp0 nil t)
18063 (looking-back (concat org-clock-string " \\[")
18064 (line-beginning-position)))
18065 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
18066 ((save-excursion ; fix next clock?
18067 (re-search-backward org-ts-regexp0 nil t)
18068 (looking-at (concat org-ts-regexp0 "\\] =>")))
18069 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
18070 (save-window-excursion
18071 ;; Find closest clock to point, adjust the previous/next one in history
18072 (let* ((p (save-excursion (org-back-to-heading t)))
18073 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
18074 (clfixnth
18075 (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
18076 (clfixpos (unless (> 0 clfixnth) (nth clfixnth org-clock-history))))
18077 (if (not clfixpos)
18078 (message "No clock to adjust")
18079 (save-excursion
18080 (org-goto-marker-or-bmk clfixpos)
18081 (org-show-subtree)
18082 (when (re-search-forward clrgx nil t)
18083 (goto-char (match-beginning 1))
18084 (let (org-clock-adjust-closest)
18085 (org-timestamp-change n timestamp? updown))
18086 (message "Clock adjusted in %s for heading: %s"
18087 (file-name-nondirectory (buffer-file-name))
18088 (org-get-heading t t)))))))))
18089 ;; Try to recenter the calendar window, if any.
18090 (when (and org-calendar-follow-timestamp-change
18091 (get-buffer-window "*Calendar*" t)
18092 (memq timestamp? '(day month year)))
18093 (org-recenter-calendar (time-to-days time))))))
18095 (defun org-modify-ts-extra (s pos n dm)
18096 "Change the different parts of the lead-time and repeat fields in timestamp."
18097 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
18098 ng h m new rem)
18099 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
18100 (cond
18101 ((or (org-pos-in-match-range pos 2)
18102 (org-pos-in-match-range pos 3))
18103 (setq m (string-to-number (match-string 3 s))
18104 h (string-to-number (match-string 2 s)))
18105 (if (org-pos-in-match-range pos 2)
18106 (setq h (+ h n))
18107 (setq n (* dm (with-no-warnings (signum n))))
18108 (unless (= 0 (setq rem (% m dm)))
18109 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
18110 (setq m (+ m n)))
18111 (when (< m 0) (setq m (+ m 60) h (1- h)))
18112 (when (> m 59) (setq m (- m 60) h (1+ h)))
18113 (setq h (mod h 24))
18114 (setq ng 1 new (format "-%02d:%02d" h m)))
18115 ((org-pos-in-match-range pos 6)
18116 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
18117 ((org-pos-in-match-range pos 5)
18118 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
18120 ((org-pos-in-match-range pos 9)
18121 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
18122 ((org-pos-in-match-range pos 8)
18123 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
18125 (when ng
18126 (setq s (concat
18127 (substring s 0 (match-beginning ng))
18129 (substring s (match-end ng))))))
18132 (defun org-recenter-calendar (d)
18133 "If the calendar is visible, recenter it to date D."
18134 (let ((cwin (get-buffer-window "*Calendar*" t)))
18135 (when cwin
18136 (let ((calendar-move-hook nil))
18137 (with-selected-window cwin
18138 (calendar-goto-date
18139 (if (listp d) d (calendar-gregorian-from-absolute d))))))))
18141 (defun org-goto-calendar (&optional arg)
18142 "Go to the Emacs calendar at the current date.
18143 If there is a time stamp in the current line, go to that date.
18144 A prefix ARG can be used to force the current date."
18145 (interactive "P")
18146 (let ((calendar-move-hook nil)
18147 (calendar-view-holidays-initially-flag nil)
18148 (calendar-view-diary-initially-flag nil)
18149 diff)
18150 (when (or (org-at-timestamp-p 'lax)
18151 (org-match-line (concat ".*" org-ts-regexp)))
18152 (let ((d1 (time-to-days (current-time)))
18153 (d2 (time-to-days (org-time-string-to-time (match-string 1)))))
18154 (setq diff (- d2 d1))))
18155 (calendar)
18156 (calendar-goto-today)
18157 (when (and diff (not arg)) (calendar-forward-day diff))))
18159 (defun org-get-date-from-calendar ()
18160 "Return a list (month day year) of date at point in calendar."
18161 (with-current-buffer "*Calendar*"
18162 (save-match-data
18163 (calendar-cursor-to-date))))
18165 (defun org-date-from-calendar ()
18166 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
18167 If there is already a time stamp at the cursor position, update it."
18168 (interactive)
18169 (if (org-at-timestamp-p 'lax)
18170 (org-timestamp-change 0 'calendar)
18171 (let ((cal-date (org-get-date-from-calendar)))
18172 (org-insert-time-stamp
18173 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
18175 (defcustom org-effort-durations
18176 `(("min" . 1)
18177 ("h" . 60)
18178 ("d" . ,(* 60 8))
18179 ("w" . ,(* 60 8 5))
18180 ("m" . ,(* 60 8 5 4))
18181 ("y" . ,(* 60 8 5 40)))
18182 "Conversion factor to minutes for an effort modifier.
18184 Each entry has the form (MODIFIER . MINUTES).
18186 In an effort string, a number followed by MODIFIER is multiplied
18187 by the specified number of MINUTES to obtain an effort in
18188 minutes.
18190 For example, if the value of this variable is ((\"hours\" . 60)), then an
18191 effort string \"2hours\" is equivalent to 120 minutes."
18192 :group 'org-agenda
18193 :version "26.1"
18194 :package-version '(Org . "8.3")
18195 :type '(alist :key-type (string :tag "Modifier")
18196 :value-type (number :tag "Minutes")))
18198 (defcustom org-image-actual-width t
18199 "Should we use the actual width of images when inlining them?
18201 When set to t, always use the image width.
18203 When set to a number, use imagemagick (when available) to set
18204 the image's width to this value.
18206 When set to a number in a list, try to get the width from any
18207 #+ATTR.* keyword if it matches a width specification like
18209 #+ATTR_HTML: :width 300px
18211 and fall back on that number if none is found.
18213 When set to nil, try to get the width from an #+ATTR.* keyword
18214 and fall back on the original width if none is found.
18216 This requires Emacs >= 24.1, build with imagemagick support."
18217 :group 'org-appearance
18218 :version "24.4"
18219 :package-version '(Org . "8.0")
18220 :type '(choice
18221 (const :tag "Use the image width" t)
18222 (integer :tag "Use a number of pixels")
18223 (list :tag "Use #+ATTR* or a number of pixels" (integer))
18224 (const :tag "Use #+ATTR* or don't resize" nil)))
18226 (defcustom org-agenda-inhibit-startup nil
18227 "Inhibit startup when preparing agenda buffers.
18228 When this variable is t, the initialization of the Org agenda
18229 buffers is inhibited: e.g. the visibility state is not set, the
18230 tables are not re-aligned, etc."
18231 :type 'boolean
18232 :version "24.3"
18233 :group 'org-agenda)
18235 (defcustom org-agenda-ignore-properties nil
18236 "Avoid updating text properties when building the agenda.
18237 Properties are used to prepare buffers for effort estimates,
18238 appointments, statistics and subtree-local categories.
18239 If you don't use these in the agenda, you can add them to this
18240 list and agenda building will be a bit faster.
18241 The value is a list, with zero or more of the symbols `effort', `appt',
18242 `stats' or `category'."
18243 :type '(set :greedy t
18244 (const effort)
18245 (const appt)
18246 (const stats)
18247 (const category))
18248 :version "26.1"
18249 :package-version '(Org . "8.3")
18250 :group 'org-agenda)
18252 ;;;; Files
18254 (defun org-save-all-org-buffers ()
18255 "Save all Org buffers without user confirmation."
18256 (interactive)
18257 (message "Saving all Org buffers...")
18258 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
18259 (when (featurep 'org-id) (org-id-locations-save))
18260 (message "Saving all Org buffers... done"))
18262 (defun org-revert-all-org-buffers ()
18263 "Revert all Org buffers.
18264 Prompt for confirmation when there are unsaved changes.
18265 Be sure you know what you are doing before letting this function
18266 overwrite your changes.
18268 This function is useful in a setup where one tracks Org files
18269 with a version control system, to revert on one machine after pulling
18270 changes from another. I believe the procedure must be like this:
18272 1. M-x org-save-all-org-buffers
18273 2. Pull changes from the other machine, resolve conflicts
18274 3. M-x org-revert-all-org-buffers"
18275 (interactive)
18276 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
18277 (user-error "Abort"))
18278 (save-excursion
18279 (save-window-excursion
18280 (dolist (b (buffer-list))
18281 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
18282 (with-current-buffer b buffer-file-name))
18283 (pop-to-buffer-same-window b)
18284 (revert-buffer t 'no-confirm)))
18285 (when (and (featurep 'org-id) org-id-track-globally)
18286 (org-id-locations-load)))))
18288 ;;;; Agenda files
18290 ;;;###autoload
18291 (defun org-switchb (&optional arg)
18292 "Switch between Org buffers.
18294 With `\\[universal-argument]' prefix, restrict available buffers to files.
18296 With `\\[universal-argument] \\[universal-argument]' \
18297 prefix, restrict available buffers to agenda files."
18298 (interactive "P")
18299 (let ((blist (org-buffer-list
18300 (cond ((equal arg '(4)) 'files)
18301 ((equal arg '(16)) 'agenda)))))
18302 (pop-to-buffer-same-window
18303 (completing-read "Org buffer: "
18304 (mapcar #'list (mapcar #'buffer-name blist))
18305 nil t))))
18307 (defun org-buffer-list (&optional predicate exclude-tmp)
18308 "Return a list of Org buffers.
18309 PREDICATE can be `export', `files' or `agenda'.
18311 export restrict the list to Export buffers.
18312 files restrict the list to buffers visiting Org files.
18313 agenda restrict the list to buffers visiting agenda files.
18315 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
18316 (let* ((bfn nil)
18317 (agenda-files (and (eq predicate 'agenda)
18318 (mapcar 'file-truename (org-agenda-files t))))
18319 (filter
18320 (cond
18321 ((eq predicate 'files)
18322 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
18323 ((eq predicate 'export)
18324 (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
18325 ((eq predicate 'agenda)
18326 (lambda (b)
18327 (with-current-buffer b
18328 (and (derived-mode-p 'org-mode)
18329 (setq bfn (buffer-file-name b))
18330 (member (file-truename bfn) agenda-files)))))
18331 (t (lambda (b) (with-current-buffer b
18332 (or (derived-mode-p 'org-mode)
18333 (string-match "\\*Org .*Export"
18334 (buffer-name b)))))))))
18335 (delq nil
18336 (mapcar
18337 (lambda(b)
18338 (if (and (funcall filter b)
18339 (or (not exclude-tmp)
18340 (not (string-match "tmp" (buffer-name b)))))
18342 nil))
18343 (buffer-list)))))
18345 (defun org-agenda-files (&optional unrestricted archives)
18346 "Get the list of agenda files.
18347 Optional UNRESTRICTED means return the full list even if a restriction
18348 is currently in place.
18349 When ARCHIVES is t, include all archive files that are really being
18350 used by the agenda files. If ARCHIVE is `ifmode', do this only if
18351 `org-agenda-archives-mode' is t."
18352 (let ((files
18353 (cond
18354 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
18355 ((stringp org-agenda-files) (org-read-agenda-file-list))
18356 ((listp org-agenda-files) org-agenda-files)
18357 (t (error "Invalid value of `org-agenda-files'")))))
18358 (setq files (apply 'append
18359 (mapcar (lambda (f)
18360 (if (file-directory-p f)
18361 (directory-files
18362 f t org-agenda-file-regexp)
18363 (list f)))
18364 files)))
18365 (when org-agenda-skip-unavailable-files
18366 (setq files (delq nil
18367 (mapcar (function
18368 (lambda (file)
18369 (and (file-readable-p file) file)))
18370 files))))
18371 (when (or (eq archives t)
18372 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
18373 (setq files (org-add-archive-files files)))
18374 files))
18376 (defun org-agenda-file-p (&optional file)
18377 "Return non-nil, if FILE is an agenda file.
18378 If FILE is omitted, use the file associated with the current
18379 buffer."
18380 (let ((fname (or file (buffer-file-name))))
18381 (and fname
18382 (member (file-truename fname)
18383 (mapcar #'file-truename (org-agenda-files t))))))
18385 (defun org-edit-agenda-file-list ()
18386 "Edit the list of agenda files.
18387 Depending on setup, this either uses customize to edit the variable
18388 `org-agenda-files', or it visits the file that is holding the list. In the
18389 latter case, the buffer is set up in a way that saving it automatically kills
18390 the buffer and restores the previous window configuration."
18391 (interactive)
18392 (if (stringp org-agenda-files)
18393 (let ((cw (current-window-configuration)))
18394 (find-file org-agenda-files)
18395 (setq-local org-window-configuration cw)
18396 (add-hook 'after-save-hook
18397 (lambda ()
18398 (set-window-configuration
18399 (prog1 org-window-configuration
18400 (kill-buffer (current-buffer))))
18401 (org-install-agenda-files-menu)
18402 (message "New agenda file list installed"))
18403 nil 'local)
18404 (message "%s" (substitute-command-keys
18405 "Edit list and finish with \\[save-buffer]")))
18406 (customize-variable 'org-agenda-files)))
18408 (defun org-store-new-agenda-file-list (list)
18409 "Set new value for the agenda file list and save it correctly."
18410 (if (stringp org-agenda-files)
18411 (let ((fe (org-read-agenda-file-list t)) b u)
18412 (while (setq b (find-buffer-visiting org-agenda-files))
18413 (kill-buffer b))
18414 (with-temp-file org-agenda-files
18415 (insert
18416 (mapconcat
18417 (lambda (f) ;; Keep un-expanded entries.
18418 (if (setq u (assoc f fe))
18419 (cdr u)
18421 list "\n")
18422 "\n")))
18423 (let ((org-mode-hook nil) (org-inhibit-startup t)
18424 (org-insert-mode-line-in-empty-file nil))
18425 (setq org-agenda-files list)
18426 (customize-save-variable 'org-agenda-files org-agenda-files))))
18428 (defun org-read-agenda-file-list (&optional pair-with-expansion)
18429 "Read the list of agenda files from a file.
18430 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
18431 filenames, used by `org-store-new-agenda-file-list' to write back
18432 un-expanded file names."
18433 (when (file-directory-p org-agenda-files)
18434 (error "`org-agenda-files' cannot be a single directory"))
18435 (when (stringp org-agenda-files)
18436 (with-temp-buffer
18437 (insert-file-contents org-agenda-files)
18438 (mapcar
18439 (lambda (f)
18440 (let ((e (expand-file-name (substitute-in-file-name f)
18441 org-directory)))
18442 (if pair-with-expansion
18443 (cons e f)
18444 e)))
18445 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
18447 ;;;###autoload
18448 (defun org-cycle-agenda-files ()
18449 "Cycle through the files in `org-agenda-files'.
18450 If the current buffer visits an agenda file, find the next one in the list.
18451 If the current buffer does not, find the first agenda file."
18452 (interactive)
18453 (let* ((fs (or (org-agenda-files t)
18454 (user-error "No agenda files")))
18455 (files (copy-sequence fs))
18456 (tcf (and buffer-file-name (file-truename buffer-file-name)))
18457 file)
18458 (when tcf
18459 (while (and (setq file (pop files))
18460 (not (equal (file-truename file) tcf)))))
18461 (find-file (car (or files fs)))
18462 (when (buffer-base-buffer) (pop-to-buffer-same-window (buffer-base-buffer)))))
18464 (defun org-agenda-file-to-front (&optional to-end)
18465 "Move/add the current file to the top of the agenda file list.
18466 If the file is not present in the list, it is added to the front. If it is
18467 present, it is moved there. With optional argument TO-END, add/move to the
18468 end of the list."
18469 (interactive "P")
18470 (let ((org-agenda-skip-unavailable-files nil)
18471 (file-alist (mapcar (lambda (x)
18472 (cons (file-truename x) x))
18473 (org-agenda-files t)))
18474 (ctf (file-truename
18475 (or buffer-file-name
18476 (user-error "Please save the current buffer to a file"))))
18477 x had)
18478 (setq x (assoc ctf file-alist) had x)
18480 (unless x (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
18481 (if to-end
18482 (setq file-alist (append (delq x file-alist) (list x)))
18483 (setq file-alist (cons x (delq x file-alist))))
18484 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
18485 (org-install-agenda-files-menu)
18486 (message "File %s to %s of agenda file list"
18487 (if had "moved" "added") (if to-end "end" "front"))))
18489 (defun org-remove-file (&optional file)
18490 "Remove current file from the list of files in variable `org-agenda-files'.
18491 These are the files which are being checked for agenda entries.
18492 Optional argument FILE means use this file instead of the current."
18493 (interactive)
18494 (let* ((org-agenda-skip-unavailable-files nil)
18495 (file (or file buffer-file-name
18496 (user-error "Current buffer does not visit a file")))
18497 (true-file (file-truename file))
18498 (afile (abbreviate-file-name file))
18499 (files (delq nil (mapcar
18500 (lambda (x)
18501 (unless (equal true-file
18502 (file-truename x))
18504 (org-agenda-files t)))))
18505 (if (not (= (length files) (length (org-agenda-files t))))
18506 (progn
18507 (org-store-new-agenda-file-list files)
18508 (org-install-agenda-files-menu)
18509 (message "Removed from Org Agenda list: %s" afile))
18510 (message "File was not in list: %s (not removed)" afile))))
18512 (defun org-file-menu-entry (file)
18513 (vector file (list 'find-file file) t))
18515 (defun org-check-agenda-file (file)
18516 "Make sure FILE exists. If not, ask user what to do."
18517 (unless (file-exists-p file)
18518 (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
18519 (abbreviate-file-name file))
18520 (let ((r (downcase (read-char-exclusive))))
18521 (cond
18522 ((equal r ?r)
18523 (org-remove-file file)
18524 (throw 'nextfile t))
18525 (t (user-error "Abort"))))))
18527 (defun org-get-agenda-file-buffer (file)
18528 "Get an agenda buffer visiting FILE.
18529 If the buffer needs to be created, add it to the list of buffers
18530 which might be released later."
18531 (let ((buf (org-find-base-buffer-visiting file)))
18532 (if buf
18533 buf ; just return it
18534 ;; Make a new buffer and remember it
18535 (setq buf (find-file-noselect file))
18536 (when buf (push buf org-agenda-new-buffers))
18537 buf)))
18539 (defun org-release-buffers (blist)
18540 "Release all buffers in list, asking the user for confirmation when needed.
18541 When a buffer is unmodified, it is just killed. When modified, it is saved
18542 \(if the user agrees) and then killed."
18543 (let (file)
18544 (dolist (buf blist)
18545 (setq file (buffer-file-name buf))
18546 (when (and (buffer-modified-p buf)
18547 file
18548 (y-or-n-p (format "Save file %s? " file)))
18549 (with-current-buffer buf (save-buffer)))
18550 (kill-buffer buf))))
18552 (defun org-agenda-prepare-buffers (files)
18553 "Create buffers for all agenda files, protect archived trees and comments."
18554 (interactive)
18555 (let ((pa '(:org-archived t))
18556 (pc '(:org-comment t))
18557 (pall '(:org-archived t :org-comment t))
18558 (inhibit-read-only t)
18559 (org-inhibit-startup org-agenda-inhibit-startup)
18560 (rea (concat ":" org-archive-tag ":"))
18561 re pos)
18562 (setq org-tag-alist-for-agenda nil
18563 org-tag-groups-alist-for-agenda nil)
18564 (save-excursion
18565 (save-restriction
18566 (dolist (file files)
18567 (catch 'nextfile
18568 (if (bufferp file)
18569 (set-buffer file)
18570 (org-check-agenda-file file)
18571 (set-buffer (org-get-agenda-file-buffer file)))
18572 (widen)
18573 (org-set-regexps-and-options 'tags-only)
18574 (setq pos (point))
18575 (or (memq 'category org-agenda-ignore-properties)
18576 (org-refresh-category-properties))
18577 (or (memq 'stats org-agenda-ignore-properties)
18578 (org-refresh-stats-properties))
18579 (or (memq 'effort org-agenda-ignore-properties)
18580 (org-refresh-effort-properties))
18581 (or (memq 'appt org-agenda-ignore-properties)
18582 (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
18583 (setq org-todo-keywords-for-agenda
18584 (append org-todo-keywords-for-agenda org-todo-keywords-1))
18585 (setq org-done-keywords-for-agenda
18586 (append org-done-keywords-for-agenda org-done-keywords))
18587 (setq org-todo-keyword-alist-for-agenda
18588 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
18589 (setq org-tag-alist-for-agenda
18590 (org--tag-add-to-alist
18591 org-tag-alist-for-agenda
18592 org-current-tag-alist))
18593 ;; Merge current file's tag groups into global
18594 ;; `org-tag-groups-alist-for-agenda'.
18595 (when org-group-tags
18596 (dolist (alist org-tag-groups-alist)
18597 (let ((old (assoc (car alist) org-tag-groups-alist-for-agenda)))
18598 (if old
18599 (setcdr old (org-uniquify (append (cdr old) (cdr alist))))
18600 (push alist org-tag-groups-alist-for-agenda)))))
18601 (org-with-silent-modifications
18602 (save-excursion
18603 (remove-text-properties (point-min) (point-max) pall)
18604 (when org-agenda-skip-archived-trees
18605 (goto-char (point-min))
18606 (while (re-search-forward rea nil t)
18607 (when (org-at-heading-p t)
18608 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
18609 (goto-char (point-min))
18610 (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
18611 (while (re-search-forward re nil t)
18612 (when (save-match-data (org-in-commented-heading-p t))
18613 (add-text-properties
18614 (match-beginning 0) (org-end-of-subtree t) pc)))))
18615 (goto-char pos)))))
18616 (setq org-todo-keywords-for-agenda
18617 (org-uniquify org-todo-keywords-for-agenda))
18618 (setq org-todo-keyword-alist-for-agenda
18619 (org-uniquify org-todo-keyword-alist-for-agenda))))
18622 ;;;; CDLaTeX minor mode
18624 (defvar org-cdlatex-mode-map (make-sparse-keymap)
18625 "Keymap for the minor `org-cdlatex-mode'.")
18627 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
18628 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
18629 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
18630 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
18631 (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
18633 (defvar org-cdlatex-texmathp-advice-is-done nil
18634 "Flag remembering if we have applied the advice to texmathp already.")
18636 (define-minor-mode org-cdlatex-mode
18637 "Toggle the minor `org-cdlatex-mode'.
18638 This mode supports entering LaTeX environment and math in LaTeX fragments
18639 in Org mode.
18640 \\{org-cdlatex-mode-map}"
18641 nil " OCDL" nil
18642 (when org-cdlatex-mode
18643 (require 'cdlatex)
18644 (run-hooks 'cdlatex-mode-hook)
18645 (cdlatex-compute-tables))
18646 (unless org-cdlatex-texmathp-advice-is-done
18647 (setq org-cdlatex-texmathp-advice-is-done t)
18648 (defadvice texmathp (around org-math-always-on activate)
18649 "Always return t in Org buffers.
18650 This is because we want to insert math symbols without dollars even outside
18651 the LaTeX math segments. If Org mode thinks that point is actually inside
18652 an embedded LaTeX fragment, let `texmathp' do its job.
18653 `\\[org-cdlatex-mode-map]'"
18654 (interactive)
18655 (let (p)
18656 (cond
18657 ((not (derived-mode-p 'org-mode)) ad-do-it)
18658 ((eq this-command 'cdlatex-math-symbol)
18659 (setq ad-return-value t
18660 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
18662 (let ((p (org-inside-LaTeX-fragment-p)))
18663 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
18664 (setq ad-return-value t
18665 texmathp-why '("Org mode embedded math" . 0))
18666 (when p ad-do-it)))))))))
18668 (defun turn-on-org-cdlatex ()
18669 "Unconditionally turn on `org-cdlatex-mode'."
18670 (org-cdlatex-mode 1))
18672 (defun org-try-cdlatex-tab ()
18673 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
18674 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
18675 - inside a LaTeX fragment, or
18676 - after the first word in a line, where an abbreviation expansion could
18677 insert a LaTeX environment."
18678 (when org-cdlatex-mode
18679 (cond
18680 ;; Before any word on the line: No expansion possible.
18681 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
18682 ;; Just after first word on the line: Expand it. Make sure it
18683 ;; cannot happen on headlines, though.
18684 ((save-excursion
18685 (skip-chars-backward "a-zA-Z0-9*")
18686 (skip-chars-backward " \t")
18687 (and (bolp) (not (org-at-heading-p))))
18688 (cdlatex-tab) t)
18689 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
18691 (defun org-cdlatex-underscore-caret (&optional _arg)
18692 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
18693 Revert to the normal definition outside of these fragments."
18694 (interactive "P")
18695 (if (org-inside-LaTeX-fragment-p)
18696 (call-interactively 'cdlatex-sub-superscript)
18697 (let (org-cdlatex-mode)
18698 (call-interactively (key-binding (vector last-input-event))))))
18700 (defun org-cdlatex-math-modify (&optional _arg)
18701 "Execute `cdlatex-math-modify' in LaTeX fragments.
18702 Revert to the normal definition outside of these fragments."
18703 (interactive "P")
18704 (if (org-inside-LaTeX-fragment-p)
18705 (call-interactively 'cdlatex-math-modify)
18706 (let (org-cdlatex-mode)
18707 (call-interactively (key-binding (vector last-input-event))))))
18709 (defun org-cdlatex-environment-indent (&optional environment item)
18710 "Execute `cdlatex-environment' and indent the inserted environment.
18712 ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
18714 The inserted environment is indented to current indentation
18715 unless point is at the beginning of the line, in which the
18716 environment remains unintended."
18717 (interactive)
18718 ;; cdlatex-environment always return nil. Therefore, capture output
18719 ;; first and determine if an environment was selected.
18720 (let* ((beg (point-marker))
18721 (end (copy-marker (point) t))
18722 (inserted (progn
18723 (ignore-errors (cdlatex-environment environment item))
18724 (< beg end)))
18725 ;; Figure out how many lines to move forward after the
18726 ;; environment has been inserted.
18727 (lines (when inserted
18728 (save-excursion
18729 (- (cl-loop while (< beg (point))
18730 with x = 0
18731 do (forward-line -1)
18732 (cl-incf x)
18733 finally return x)
18734 (if (progn (goto-char beg)
18735 (and (progn (skip-chars-forward " \t") (eolp))
18736 (progn (skip-chars-backward " \t") (bolp))))
18737 1 0)))))
18738 (env (org-trim (delete-and-extract-region beg end))))
18739 (when inserted
18740 ;; Get indentation of next line unless at column 0.
18741 (let ((ind (if (bolp) 0
18742 (save-excursion
18743 (org-return-indent)
18744 (prog1 (org-get-indentation)
18745 (when (progn (skip-chars-forward " \t") (eolp))
18746 (delete-region beg (point)))))))
18747 (bol (progn (skip-chars-backward " \t") (bolp))))
18748 ;; Insert a newline before environment unless at column zero
18749 ;; to "escape" the current line. Insert a newline if
18750 ;; something is one the same line as \end{ENVIRONMENT}.
18751 (insert
18752 (concat (unless bol "\n") env
18753 (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
18754 (unless (zerop ind)
18755 (save-excursion
18756 (goto-char beg)
18757 (while (< (point) end)
18758 (unless (eolp) (indent-line-to ind))
18759 (forward-line))))
18760 (goto-char beg)
18761 (forward-line lines)
18762 (indent-line-to ind)))
18763 (set-marker beg nil)
18764 (set-marker end nil)))
18767 ;;;; LaTeX fragments
18769 (defun org-inside-LaTeX-fragment-p ()
18770 "Test if point is inside a LaTeX fragment.
18771 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
18772 sequence appearing also before point.
18773 Even though the matchers for math are configurable, this function assumes
18774 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
18775 delimiters are skipped when they have been removed by customization.
18776 The return value is nil, or a cons cell with the delimiter and the
18777 position of this delimiter.
18779 This function does a reasonably good job, but can locally be fooled by
18780 for example currency specifications. For example it will assume being in
18781 inline math after \"$22.34\". The LaTeX fragment formatter will only format
18782 fragments that are properly closed, but during editing, we have to live
18783 with the uncertainty caused by missing closing delimiters. This function
18784 looks only before point, not after."
18785 (catch 'exit
18786 (let ((pos (point))
18787 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
18788 (lim (save-excursion (org-backward-paragraph) (point)))
18789 dd-on str (start 0) m re)
18790 (goto-char pos)
18791 (when dodollar
18792 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
18793 re (nth 1 (assoc "$" org-latex-regexps)))
18794 (while (string-match re str start)
18795 (cond
18796 ((= (match-end 0) (length str))
18797 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
18798 ((= (match-end 0) (- (length str) 5))
18799 (throw 'exit nil))
18800 (t (setq start (match-end 0))))))
18801 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
18802 (goto-char pos)
18803 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
18804 (and (match-beginning 2) (throw 'exit nil))
18805 ;; count $$
18806 (while (re-search-backward "\\$\\$" lim t)
18807 (setq dd-on (not dd-on)))
18808 (goto-char pos)
18809 (when dd-on (cons "$$" m))))))
18811 (defun org-inside-latex-macro-p ()
18812 "Is point inside a LaTeX macro or its arguments?"
18813 (save-match-data
18814 (org-in-regexp
18815 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
18817 (defun org--format-latex-make-overlay (beg end image &optional imagetype)
18818 "Build an overlay between BEG and END using IMAGE file.
18819 Argument IMAGETYPE is the extension of the displayed image,
18820 as a string. It defaults to \"png\"."
18821 (let ((ov (make-overlay beg end))
18822 (imagetype (or (intern imagetype) 'png)))
18823 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
18824 (overlay-put ov 'evaporate t)
18825 (overlay-put ov
18826 'modification-hooks
18827 (list (lambda (o _flag _beg _end &optional _l)
18828 (delete-overlay o))))
18829 (overlay-put ov
18830 'display
18831 (list 'image :type imagetype :file image :ascent 'center))))
18833 (defun org--list-latex-overlays (&optional beg end)
18834 "List all Org LaTeX overlays in current buffer.
18835 Limit to overlays between BEG and END when those are provided."
18836 (cl-remove-if-not
18837 (lambda (o) (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay))
18838 (overlays-in (or beg (point-min)) (or end (point-max)))))
18840 (defun org-remove-latex-fragment-image-overlays (&optional beg end)
18841 "Remove all overlays with LaTeX fragment images in current buffer.
18842 When optional arguments BEG and END are non-nil, remove all
18843 overlays between them instead. Return a non-nil value when some
18844 overlays were removed, nil otherwise."
18845 (let ((overlays (org--list-latex-overlays beg end)))
18846 (mapc #'delete-overlay overlays)
18847 overlays))
18849 (defun org-toggle-latex-fragment (&optional arg)
18850 "Preview the LaTeX fragment at point, or all locally or globally.
18852 If the cursor is on a LaTeX fragment, create the image and overlay
18853 it over the source code, if there is none. Remove it otherwise.
18854 If there is no fragment at point, display all fragments in the
18855 current section.
18857 With prefix ARG, preview or clear image for all fragments in the
18858 current subtree or in the whole buffer when used before the first
18859 headline. With a prefix ARG `\\[universal-argument] \
18860 \\[universal-argument]' preview or clear images
18861 for all fragments in the buffer."
18862 (interactive "P")
18863 (when (display-graphic-p)
18864 (catch 'exit
18865 (save-excursion
18866 (let (beg end msg)
18867 (cond
18868 ((or (equal arg '(16))
18869 (and (equal arg '(4))
18870 (org-with-limited-levels (org-before-first-heading-p))))
18871 (if (org-remove-latex-fragment-image-overlays)
18872 (progn (message "LaTeX fragments images removed from buffer")
18873 (throw 'exit nil))
18874 (setq msg "Creating images for buffer...")))
18875 ((equal arg '(4))
18876 (org-with-limited-levels (org-back-to-heading t))
18877 (setq beg (point))
18878 (setq end (progn (org-end-of-subtree t) (point)))
18879 (if (org-remove-latex-fragment-image-overlays beg end)
18880 (progn
18881 (message "LaTeX fragment images removed from subtree")
18882 (throw 'exit nil))
18883 (setq msg "Creating images for subtree...")))
18884 ((let ((datum (org-element-context)))
18885 (when (memq (org-element-type datum)
18886 '(latex-environment latex-fragment))
18887 (setq beg (org-element-property :begin datum))
18888 (setq end (org-element-property :end datum))
18889 (if (org-remove-latex-fragment-image-overlays beg end)
18890 (progn (message "LaTeX fragment image removed")
18891 (throw 'exit nil))
18892 (setq msg "Creating image...")))))
18894 (org-with-limited-levels
18895 (setq beg (if (org-at-heading-p) (line-beginning-position)
18896 (outline-previous-heading)
18897 (point)))
18898 (setq end (progn (outline-next-heading) (point)))
18899 (if (org-remove-latex-fragment-image-overlays beg end)
18900 (progn
18901 (message "LaTeX fragment images removed from section")
18902 (throw 'exit nil))
18903 (setq msg "Creating images for section...")))))
18904 (let ((file (buffer-file-name (buffer-base-buffer))))
18905 (org-format-latex
18906 (concat org-preview-latex-image-directory "org-ltximg")
18907 beg end
18908 ;; Emacs cannot overlay images from remote hosts. Create
18909 ;; it in `temporary-file-directory' instead.
18910 (if (or (not file) (file-remote-p file))
18911 temporary-file-directory
18912 default-directory)
18913 'overlays msg 'forbuffer org-preview-latex-default-process))
18914 (message (concat msg "done")))))))
18916 (defun org-format-latex
18917 (prefix &optional beg end dir overlays msg forbuffer processing-type)
18918 "Replace LaTeX fragments with links to an image.
18920 The function takes care of creating the replacement image.
18922 Only consider fragments between BEG and END when those are
18923 provided.
18925 When optional argument OVERLAYS is non-nil, display the image on
18926 top of the fragment instead of replacing it.
18928 PROCESSING-TYPE is the conversion method to use, as a symbol.
18930 Some of the options can be changed using the variable
18931 `org-format-latex-options', which see."
18932 (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
18933 (unless (eq processing-type 'verbatim)
18934 (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
18935 (cnt 0)
18936 checkdir-flag)
18937 (goto-char (or beg (point-min)))
18938 ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
18939 (when (and overlays (memq processing-type '(dvipng imagemagick)))
18940 (overlay-recenter (or end (point-max))))
18941 (while (re-search-forward math-regexp end t)
18942 (unless (and overlays
18943 (eq (get-char-property (point) 'org-overlay-type)
18944 'org-latex-overlay))
18945 (let* ((context (org-element-context))
18946 (type (org-element-type context)))
18947 (when (memq type '(latex-environment latex-fragment))
18948 (let ((block-type (eq type 'latex-environment))
18949 (value (org-element-property :value context))
18950 (beg (org-element-property :begin context))
18951 (end (save-excursion
18952 (goto-char (org-element-property :end context))
18953 (skip-chars-backward " \r\t\n")
18954 (point))))
18955 (cond
18956 ((eq processing-type 'mathjax)
18957 ;; Prepare for MathJax processing.
18958 (if (not (string-match "\\`\\$\\$?" value))
18959 (goto-char end)
18960 (delete-region beg end)
18961 (if (string= (match-string 0 value) "$$")
18962 (insert "\\[" (substring value 2 -2) "\\]")
18963 (insert "\\(" (substring value 1 -1) "\\)"))))
18964 ((assq processing-type org-preview-latex-process-alist)
18965 ;; Process to an image.
18966 (cl-incf cnt)
18967 (goto-char beg)
18968 (let* ((processing-info
18969 (cdr (assq processing-type org-preview-latex-process-alist)))
18970 (face (face-at-point))
18971 ;; Get the colors from the face at point.
18973 (let ((color (plist-get org-format-latex-options
18974 :foreground)))
18975 (if (and forbuffer (eq color 'auto))
18976 (face-attribute face :foreground nil 'default)
18977 color)))
18979 (let ((color (plist-get org-format-latex-options
18980 :background)))
18981 (if (and forbuffer (eq color 'auto))
18982 (face-attribute face :background nil 'default)
18983 color)))
18984 (hash (sha1 (prin1-to-string
18985 (list org-format-latex-header
18986 org-latex-default-packages-alist
18987 org-latex-packages-alist
18988 org-format-latex-options
18989 forbuffer value fg bg))))
18990 (imagetype (or (plist-get processing-info :image-output-type) "png"))
18991 (absprefix (expand-file-name prefix dir))
18992 (linkfile (format "%s_%s.%s" prefix hash imagetype))
18993 (movefile (format "%s_%s.%s" absprefix hash imagetype))
18994 (sep (and block-type "\n\n"))
18995 (link (concat sep "[[file:" linkfile "]]" sep))
18996 (options
18997 (org-combine-plists
18998 org-format-latex-options
18999 `(:foreground ,fg :background ,bg))))
19000 (when msg (message msg cnt))
19001 (unless checkdir-flag ; Ensure the directory exists.
19002 (setq checkdir-flag t)
19003 (let ((todir (file-name-directory absprefix)))
19004 (unless (file-directory-p todir)
19005 (make-directory todir t))))
19006 (unless (file-exists-p movefile)
19007 (org-create-formula-image
19008 value movefile options forbuffer processing-type))
19009 (if overlays
19010 (progn
19011 (dolist (o (overlays-in beg end))
19012 (when (eq (overlay-get o 'org-overlay-type)
19013 'org-latex-overlay)
19014 (delete-overlay o)))
19015 (org--format-latex-make-overlay beg end movefile imagetype)
19016 (goto-char end))
19017 (delete-region beg end)
19018 (insert
19019 (org-add-props link
19020 (list 'org-latex-src
19021 (replace-regexp-in-string "\"" "" value)
19022 'org-latex-src-embed-type
19023 (if block-type 'paragraph 'character)))))))
19024 ((eq processing-type 'mathml)
19025 ;; Process to MathML.
19026 (unless (org-format-latex-mathml-available-p)
19027 (user-error "LaTeX to MathML converter not configured"))
19028 (cl-incf cnt)
19029 (when msg (message msg cnt))
19030 (goto-char beg)
19031 (delete-region beg end)
19032 (insert (org-format-latex-as-mathml
19033 value block-type prefix dir)))
19035 (error "Unknown conversion process %s for LaTeX fragments"
19036 processing-type)))))))))))
19038 (defun org-create-math-formula (latex-frag &optional mathml-file)
19039 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
19040 Use `org-latex-to-mathml-convert-command'. If the conversion is
19041 sucessful, return the portion between \"<math...> </math>\"
19042 elements otherwise return nil. When MATHML-FILE is specified,
19043 write the results in to that file. When invoked as an
19044 interactive command, prompt for LATEX-FRAG, with initial value
19045 set to the current active region and echo the results for user
19046 inspection."
19047 (interactive (list (let ((frag (when (org-region-active-p)
19048 (buffer-substring-no-properties
19049 (region-beginning) (region-end)))))
19050 (read-string "LaTeX Fragment: " frag nil frag))))
19051 (unless latex-frag (user-error "Invalid LaTeX fragment"))
19052 (let* ((tmp-in-file
19053 (let ((file (file-relative-name
19054 (make-temp-name (expand-file-name "ltxmathml-in")))))
19055 (write-region latex-frag nil file)
19056 file))
19057 (tmp-out-file (file-relative-name
19058 (make-temp-name (expand-file-name "ltxmathml-out"))))
19059 (cmd (format-spec
19060 org-latex-to-mathml-convert-command
19061 `((?j . ,(and org-latex-to-mathml-jar-file
19062 (shell-quote-argument
19063 (expand-file-name
19064 org-latex-to-mathml-jar-file))))
19065 (?I . ,(shell-quote-argument tmp-in-file))
19066 (?i . ,latex-frag)
19067 (?o . ,(shell-quote-argument tmp-out-file)))))
19068 mathml shell-command-output)
19069 (when (called-interactively-p 'any)
19070 (unless (org-format-latex-mathml-available-p)
19071 (user-error "LaTeX to MathML converter not configured")))
19072 (message "Running %s" cmd)
19073 (setq shell-command-output (shell-command-to-string cmd))
19074 (setq mathml
19075 (when (file-readable-p tmp-out-file)
19076 (with-current-buffer (find-file-noselect tmp-out-file t)
19077 (goto-char (point-min))
19078 (when (re-search-forward
19079 (format "<math[^>]*?%s[^>]*?>\\(.\\|\n\\)*</math>"
19080 (regexp-quote
19081 "xmlns=\"http://www.w3.org/1998/Math/MathML\""))
19082 nil t)
19083 (prog1 (match-string 0) (kill-buffer))))))
19084 (cond
19085 (mathml
19086 (setq mathml
19087 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
19088 (when mathml-file
19089 (write-region mathml nil mathml-file))
19090 (when (called-interactively-p 'any)
19091 (message mathml)))
19092 ((message "LaTeX to MathML conversion failed")
19093 (message shell-command-output)))
19094 (delete-file tmp-in-file)
19095 (when (file-exists-p tmp-out-file)
19096 (delete-file tmp-out-file))
19097 mathml))
19099 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
19100 prefix &optional dir)
19101 "Use `org-create-math-formula' but check local cache first."
19102 (let* ((absprefix (expand-file-name prefix dir))
19103 (print-length nil) (print-level nil)
19104 (formula-id (concat
19105 "formula-"
19106 (sha1
19107 (prin1-to-string
19108 (list latex-frag
19109 org-latex-to-mathml-convert-command)))))
19110 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
19111 (formula-cache-dir (file-name-directory formula-cache)))
19113 (unless (file-directory-p formula-cache-dir)
19114 (make-directory formula-cache-dir t))
19116 (unless (file-exists-p formula-cache)
19117 (org-create-math-formula latex-frag formula-cache))
19119 (if (file-exists-p formula-cache)
19120 ;; Successful conversion. Return the link to MathML file.
19121 (org-add-props
19122 (format "[[file:%s]]" (file-relative-name formula-cache dir))
19123 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
19124 'org-latex-src-embed-type (if latex-frag-type
19125 'paragraph 'character)))
19126 ;; Failed conversion. Return the LaTeX fragment verbatim
19127 latex-frag)))
19129 (defun org--get-display-dpi ()
19130 "Get the DPI of the display.
19131 The function assumes that the display has the same pixel width in
19132 the horizontal and vertical directions."
19133 (if (display-graphic-p)
19134 (round (/ (display-pixel-height)
19135 (/ (display-mm-height) 25.4)))
19136 (error "Attempt to calculate the dpi of a non-graphic display")))
19138 (defun org-create-formula-image
19139 (string tofile options buffer &optional processing-type)
19140 "Create an image from LaTeX source using external processes.
19142 The LaTeX STRING is saved to a temporary LaTeX file, then
19143 converted to an image file by process PROCESSING-TYPE defined in
19144 `org-preview-latex-process-alist'. A nil value defaults to
19145 `org-preview-latex-default-process'.
19147 The generated image file is eventually moved to TOFILE.
19149 The OPTIONS argument controls the size, foreground color and
19150 background color of the generated image.
19152 When BUFFER non-nil, this function is used for LaTeX previewing.
19153 Otherwise, it is used to deal with LaTeX snippets showed in
19154 a HTML file."
19155 (let* ((processing-type (or processing-type
19156 org-preview-latex-default-process))
19157 (processing-info
19158 (cdr (assq processing-type org-preview-latex-process-alist)))
19159 (programs (plist-get processing-info :programs))
19160 (error-message (or (plist-get processing-info :message) ""))
19161 (use-xcolor (plist-get processing-info :use-xcolor))
19162 (image-input-type (plist-get processing-info :image-input-type))
19163 (image-output-type (plist-get processing-info :image-output-type))
19164 (post-clean (or (plist-get processing-info :post-clean)
19165 '(".dvi" ".xdv" ".pdf" ".tex" ".aux" ".log"
19166 ".svg" ".png" ".jpg" ".jpeg" ".out")))
19167 (latex-header
19168 (or (plist-get processing-info :latex-header)
19169 (org-latex-make-preamble
19170 (org-export-get-environment (org-export-get-backend 'latex))
19171 org-format-latex-header
19172 'snippet)))
19173 (latex-compiler (plist-get processing-info :latex-compiler))
19174 (image-converter (plist-get processing-info :image-converter))
19175 (tmpdir temporary-file-directory)
19176 (texfilebase (make-temp-name
19177 (expand-file-name "orgtex" tmpdir)))
19178 (texfile (concat texfilebase ".tex"))
19179 (image-size-adjust (or (plist-get processing-info :image-size-adjust)
19180 '(1.0 . 1.0)))
19181 (scale (* (if buffer (car image-size-adjust) (cdr image-size-adjust))
19182 (or (plist-get options (if buffer :scale :html-scale)) 1.0)))
19183 (dpi (* scale (if buffer (org--get-display-dpi) 140.0)))
19184 (fg (or (plist-get options (if buffer :foreground :html-foreground))
19185 "Black"))
19186 (bg (or (plist-get options (if buffer :background :html-background))
19187 "Transparent"))
19188 (log-buf (get-buffer-create "*Org Preview LaTeX Output*"))
19189 (resize-mini-windows nil)) ;Fix Emacs flicker when creating image.
19190 (dolist (program programs)
19191 (org-check-external-command program error-message))
19192 (if use-xcolor
19193 (progn (if (eq fg 'default)
19194 (setq fg (org-latex-color :foreground))
19195 (setq fg (org-latex-color-format fg)))
19196 (if (eq bg 'default)
19197 (setq bg (org-latex-color :background))
19198 (setq bg (org-latex-color-format
19199 (if (string= bg "Transparent") "white" bg))))
19200 (with-temp-file texfile
19201 (insert latex-header)
19202 (insert "\n\\begin{document}\n"
19203 "\\definecolor{fg}{rgb}{" fg "}\n"
19204 "\\definecolor{bg}{rgb}{" bg "}\n"
19205 "\n\\pagecolor{bg}\n"
19206 "\n{\\color{fg}\n"
19207 string
19208 "\n}\n"
19209 "\n\\end{document}\n")))
19210 (if (eq fg 'default)
19211 (setq fg (org-dvipng-color :foreground))
19212 (unless (string= fg "Transparent")
19213 (setq fg (org-dvipng-color-format fg))))
19214 (if (eq bg 'default)
19215 (setq bg (org-dvipng-color :background))
19216 (unless (string= bg "Transparent")
19217 (setq bg (org-dvipng-color-format bg))))
19218 (with-temp-file texfile
19219 (insert latex-header)
19220 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
19222 (let* ((err-msg (format "Please adjust `%s' part of \
19223 `org-preview-latex-process-alist'."
19224 processing-type))
19225 (image-input-file
19226 (org-compile-file
19227 texfile latex-compiler image-input-type err-msg log-buf))
19228 (image-output-file
19229 (org-compile-file
19230 image-input-file image-converter image-output-type err-msg log-buf
19231 `((?F . ,(shell-quote-argument fg))
19232 (?B . ,(shell-quote-argument bg))
19233 (?D . ,(shell-quote-argument (format "%s" dpi)))
19234 (?S . ,(shell-quote-argument (format "%s" (/ dpi 140.0))))))))
19235 (copy-file image-output-file tofile 'replace)
19236 (dolist (e post-clean)
19237 (when (file-exists-p (concat texfilebase e))
19238 (delete-file (concat texfilebase e))))
19239 image-output-file)))
19241 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
19242 "Fill a LaTeX header template TPL.
19243 In the template, the following place holders will be recognized:
19245 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
19246 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
19247 [PACKAGES] \\usepackage statements for PKG
19248 [NO-PACKAGES] do not include PKG
19249 [EXTRA] the string EXTRA
19250 [NO-EXTRA] do not include EXTRA
19252 For backward compatibility, if both the positive and the negative place
19253 holder is missing, the positive one (without the \"NO-\") will be
19254 assumed to be present at the end of the template.
19255 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
19256 EXTRA is a string.
19257 SNIPPETS-P indicates if this is run to create snippet images for HTML."
19258 (let (rpl (end ""))
19259 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
19260 (setq rpl (if (or (match-end 1) (not def-pkg))
19261 "" (org-latex-packages-to-string def-pkg snippets-p t))
19262 tpl (replace-match rpl t t tpl))
19263 (when def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
19265 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
19266 (setq rpl (if (or (match-end 1) (not pkg))
19267 "" (org-latex-packages-to-string pkg snippets-p t))
19268 tpl (replace-match rpl t t tpl))
19269 (when pkg (setq end
19270 (concat end "\n"
19271 (org-latex-packages-to-string pkg snippets-p)))))
19273 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
19274 (setq rpl (if (or (match-end 1) (not extra))
19275 "" (concat extra "\n"))
19276 tpl (replace-match rpl t t tpl))
19277 (when (and extra (string-match "\\S-" extra))
19278 (setq end (concat end "\n" extra))))
19280 (if (string-match "\\S-" end)
19281 (concat tpl "\n" end)
19282 tpl)))
19284 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
19285 "Turn an alist of packages into a string with the \\usepackage macros."
19286 (setq pkg (mapconcat (lambda(p)
19287 (cond
19288 ((stringp p) p)
19289 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
19290 (format "%% Package %s omitted" (cadr p)))
19291 ((equal "" (car p))
19292 (format "\\usepackage{%s}" (cadr p)))
19294 (format "\\usepackage[%s]{%s}"
19295 (car p) (cadr p)))))
19297 "\n"))
19298 (if newline (concat pkg "\n") pkg))
19300 (defun org-dvipng-color (attr)
19301 "Return a RGB color specification for dvipng."
19302 (org-dvipng-color-format (face-attribute 'default attr nil)))
19304 (defun org-dvipng-color-format (color-name)
19305 "Convert COLOR-NAME to a RGB color value for dvipng."
19306 (apply #'format "rgb %s %s %s"
19307 (mapcar 'org-normalize-color
19308 (color-values color-name))))
19310 (defun org-latex-color (attr)
19311 "Return a RGB color for the LaTeX color package."
19312 (org-latex-color-format (face-attribute 'default attr nil)))
19314 (defun org-latex-color-format (color-name)
19315 "Convert COLOR-NAME to a RGB color value."
19316 (apply #'format "%s,%s,%s"
19317 (mapcar 'org-normalize-color
19318 (color-values color-name))))
19320 (defun org-normalize-color (value)
19321 "Return string to be used as color value for an RGB component."
19322 (format "%g" (/ value 65535.0)))
19326 ;; Image display
19328 (defvar-local org-inline-image-overlays nil)
19330 (defun org-toggle-inline-images (&optional include-linked)
19331 "Toggle the display of inline images.
19332 INCLUDE-LINKED is passed to `org-display-inline-images'."
19333 (interactive "P")
19334 (if org-inline-image-overlays
19335 (progn
19336 (org-remove-inline-images)
19337 (when (called-interactively-p 'interactive)
19338 (message "Inline image display turned off")))
19339 (org-display-inline-images include-linked)
19340 (when (called-interactively-p 'interactive)
19341 (message (if org-inline-image-overlays
19342 (format "%d images displayed inline"
19343 (length org-inline-image-overlays))
19344 "No images to display inline")))))
19346 (defun org-redisplay-inline-images ()
19347 "Refresh the display of inline images."
19348 (interactive)
19349 (if (not org-inline-image-overlays)
19350 (org-toggle-inline-images)
19351 (org-toggle-inline-images)
19352 (org-toggle-inline-images)))
19354 (defun org-display-inline-images (&optional include-linked refresh beg end)
19355 "Display inline images.
19357 An inline image is a link which follows either of these
19358 conventions:
19360 1. Its path is a file with an extension matching return value
19361 from `image-file-name-regexp' and it has no contents.
19363 2. Its description consists in a single link of the previous
19364 type.
19366 When optional argument INCLUDE-LINKED is non-nil, also links with
19367 a text description part will be inlined. This can be nice for
19368 a quick look at those images, but it does not reflect what
19369 exported files will look like.
19371 When optional argument REFRESH is non-nil, refresh existing
19372 images between BEG and END. This will create new image displays
19373 only if necessary. BEG and END default to the buffer
19374 boundaries."
19375 (interactive "P")
19376 (when (display-graphic-p)
19377 (unless refresh
19378 (org-remove-inline-images)
19379 (when (fboundp 'clear-image-cache) (clear-image-cache)))
19380 (org-with-wide-buffer
19381 (goto-char (or beg (point-min)))
19382 (let* ((case-fold-search t)
19383 (file-extension-re (image-file-name-regexp))
19384 (link-abbrevs (mapcar #'car
19385 (append org-link-abbrev-alist-local
19386 org-link-abbrev-alist)))
19387 ;; Check absolute, relative file names and explicit
19388 ;; "file:" links. Also check link abbreviations since
19389 ;; some might expand to "file" links.
19390 (file-types-re (format "[][]\\[\\(?:file\\|[./~]%s\\)"
19391 (if (not link-abbrevs) ""
19392 (format "\\|\\(?:%s:\\)"
19393 (regexp-opt link-abbrevs))))))
19394 (while (re-search-forward file-types-re end t)
19395 (let ((link (save-match-data (org-element-context))))
19396 ;; Check if we're at an inline image, i.e., an image file
19397 ;; link without a description (unless INCLUDE-LINKED is
19398 ;; non-nil).
19399 (when (and (equal "file" (org-element-property :type link))
19400 (or include-linked
19401 (null (org-element-contents link)))
19402 (string-match-p file-extension-re
19403 (org-element-property :path link)))
19404 (let ((file (expand-file-name
19405 (org-link-unescape
19406 (org-element-property :path link)))))
19407 (when (file-exists-p file)
19408 (let ((width
19409 ;; Apply `org-image-actual-width' specifications.
19410 (cond
19411 ((not (image-type-available-p 'imagemagick)) nil)
19412 ((eq org-image-actual-width t) nil)
19413 ((listp org-image-actual-width)
19415 ;; First try to find a width among
19416 ;; attributes associated to the paragraph
19417 ;; containing link.
19418 (let ((paragraph
19419 (let ((e link))
19420 (while (and (setq e (org-element-property
19421 :parent e))
19422 (not (eq (org-element-type e)
19423 'paragraph))))
19424 e)))
19425 (when paragraph
19426 (save-excursion
19427 (goto-char (org-element-property :begin paragraph))
19428 (when
19429 (re-search-forward
19430 "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
19431 (org-element-property
19432 :post-affiliated paragraph)
19434 (string-to-number (match-string 1))))))
19435 ;; Otherwise, fall-back to provided number.
19436 (car org-image-actual-width)))
19437 ((numberp org-image-actual-width)
19438 org-image-actual-width)))
19439 (old (get-char-property-and-overlay
19440 (org-element-property :begin link)
19441 'org-image-overlay)))
19442 (if (and (car-safe old) refresh)
19443 (image-refresh (overlay-get (cdr old) 'display))
19444 (let ((image (create-image file
19445 (and width 'imagemagick)
19447 :width width)))
19448 (when image
19449 (let ((ov (make-overlay
19450 (org-element-property :begin link)
19451 (progn
19452 (goto-char
19453 (org-element-property :end link))
19454 (skip-chars-backward " \t")
19455 (point)))))
19456 (overlay-put ov 'display image)
19457 (overlay-put ov 'face 'default)
19458 (overlay-put ov 'org-image-overlay t)
19459 (overlay-put
19460 ov 'modification-hooks
19461 (list 'org-display-inline-remove-overlay))
19462 (push ov org-inline-image-overlays)))))))))))))))
19464 (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len)
19465 "Remove inline-display overlay if a corresponding region is modified."
19466 (let ((inhibit-modification-hooks t))
19467 (when (and ov after)
19468 (delete ov org-inline-image-overlays)
19469 (delete-overlay ov))))
19471 (defun org-remove-inline-images ()
19472 "Remove inline display of images."
19473 (interactive)
19474 (mapc #'delete-overlay org-inline-image-overlays)
19475 (setq org-inline-image-overlays nil))
19477 ;;;; Key bindings
19479 (defun org-remap (map &rest commands)
19480 "In MAP, remap the functions given in COMMANDS.
19481 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
19482 (let (new old)
19483 (while commands
19484 (setq old (pop commands) new (pop commands))
19485 (org-defkey map (vector 'remap old) new))))
19487 ;; Outline functions from `outline-mode-prefix-map'
19488 ;; that can be remapped in Org:
19489 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
19490 (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
19491 (define-key org-mode-map [remap outline-forward-same-level]
19492 'org-forward-heading-same-level)
19493 (define-key org-mode-map [remap outline-backward-same-level]
19494 'org-backward-heading-same-level)
19495 (define-key org-mode-map [remap outline-show-branches]
19496 'org-kill-note-or-show-branches)
19497 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
19498 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
19499 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
19500 (define-key org-mode-map [remap outline-next-visible-heading]
19501 'org-next-visible-heading)
19502 (define-key org-mode-map [remap outline-previous-visible-heading]
19503 'org-previous-visible-heading)
19504 (define-key org-mode-map [remap show-children] 'org-show-children)
19506 ;; Outline functions from `outline-mode-prefix-map' that can not
19507 ;; be remapped in Org:
19509 ;; - the column "key binding" shows whether the Outline function is still
19510 ;; available in Org mode on the same key that it has been bound to in
19511 ;; Outline mode:
19512 ;; - "overridden": key used for a different functionality in Org mode
19513 ;; - else: key still bound to the same Outline function in Org mode
19515 ;; | Outline function | key binding | Org replacement |
19516 ;; |------------------------------------+-------------+--------------------------|
19517 ;; | `outline-up-heading' | `C-c C-u' | still same function |
19518 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
19519 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
19520 ;; | `show-entry' | overridden | no replacement |
19521 ;; | `show-branches' | `C-c C-k' | still same function |
19522 ;; | `show-subtree' | overridden | visibility cycling |
19523 ;; | `show-all' | overridden | no replacement |
19524 ;; | `hide-subtree' | overridden | visibility cycling |
19525 ;; | `hide-body' | overridden | no replacement |
19526 ;; | `hide-entry' | overridden | visibility cycling |
19527 ;; | `hide-leaves' | overridden | no replacement |
19528 ;; | `hide-sublevels' | overridden | no replacement |
19529 ;; | `hide-other' | overridden | no replacement |
19531 ;; Make `C-c C-x' a prefix key
19532 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
19534 ;; TAB key with modifiers
19535 (org-defkey org-mode-map "\C-i" 'org-cycle)
19536 (org-defkey org-mode-map [(tab)] 'org-cycle)
19537 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
19538 (org-defkey org-mode-map "\M-\t" #'pcomplete)
19540 ;; The following line is necessary under Suse GNU/Linux
19541 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab)
19542 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
19543 (define-key org-mode-map [backtab] 'org-shifttab)
19545 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
19546 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
19547 (org-defkey org-mode-map (kbd "M-RET") #'org-meta-return)
19549 ;; Cursor keys with modifiers
19550 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
19551 (org-defkey org-mode-map [(meta right)] 'org-metaright)
19552 (org-defkey org-mode-map [(meta up)] 'org-metaup)
19553 (org-defkey org-mode-map [(meta down)] 'org-metadown)
19555 (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
19556 (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
19557 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
19558 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
19559 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
19560 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
19562 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
19563 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
19564 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
19565 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
19567 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
19568 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
19569 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
19570 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
19572 ;; Babel keys
19573 (define-key org-mode-map org-babel-key-prefix org-babel-map)
19574 (dolist (pair org-babel-key-bindings)
19575 (define-key org-babel-map (car pair) (cdr pair)))
19577 ;;; Extra keys for tty access.
19578 ;; We only set them when really needed because otherwise the
19579 ;; menus don't show the simple keys
19581 (when (or org-use-extra-keys (not window-system))
19582 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
19583 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
19584 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
19585 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
19586 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
19587 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
19588 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
19589 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
19590 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
19591 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
19592 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
19593 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
19594 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
19595 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
19596 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
19597 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
19598 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
19599 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
19600 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
19601 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
19602 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
19603 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
19604 (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
19605 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
19606 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
19607 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
19608 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
19609 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
19611 ;; All the other keys
19612 (org-remap org-mode-map
19613 'self-insert-command 'org-self-insert-command
19614 'delete-char 'org-delete-char
19615 'delete-backward-char 'org-delete-backward-char)
19616 (org-defkey org-mode-map "|" 'org-force-self-insert)
19618 (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
19619 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
19620 (if (boundp 'narrow-map)
19621 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
19622 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
19623 (if (boundp 'narrow-map)
19624 (org-defkey narrow-map "b" 'org-narrow-to-block)
19625 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
19626 (if (boundp 'narrow-map)
19627 (org-defkey narrow-map "e" 'org-narrow-to-element)
19628 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
19629 (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
19630 (org-defkey org-mode-map "\M-}" 'org-forward-element)
19631 (org-defkey org-mode-map "\M-{" 'org-backward-element)
19632 (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
19633 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
19634 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
19635 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
19636 (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
19637 (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
19638 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
19639 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
19640 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
19641 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
19642 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
19643 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
19644 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
19645 (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
19646 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
19647 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
19648 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
19649 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
19650 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
19651 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
19652 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
19653 (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
19654 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
19655 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
19656 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
19657 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
19658 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
19659 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
19660 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
19661 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
19662 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
19663 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
19664 (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
19665 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
19666 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
19667 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
19668 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
19669 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
19670 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
19671 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
19672 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
19673 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
19674 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
19675 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
19676 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
19677 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
19678 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
19679 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
19680 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
19681 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
19682 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
19683 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
19684 (org-defkey org-mode-map "\C-c^" 'org-sort)
19685 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
19686 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
19687 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
19688 (org-defkey org-mode-map [remap open-line] 'org-open-line)
19689 (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
19690 (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
19691 (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
19692 (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
19693 (org-defkey org-mode-map "\C-m" 'org-return)
19694 (org-defkey org-mode-map "\C-j" 'org-return-indent)
19695 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
19696 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
19697 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
19698 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
19699 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
19700 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
19701 (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
19702 (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
19703 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
19704 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
19705 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
19706 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
19707 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
19708 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
19709 (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
19710 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
19711 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
19712 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
19713 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
19714 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
19715 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
19716 (org-defkey org-mode-map "\M-h" 'org-mark-element)
19717 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
19718 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
19720 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
19721 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
19722 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
19724 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
19725 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
19726 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
19727 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
19728 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
19729 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
19730 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
19731 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
19732 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
19733 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
19734 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
19735 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
19736 (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
19737 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
19738 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
19739 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
19740 (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
19741 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
19742 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
19743 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
19744 (org-defkey org-mode-map "\C-c\C-xi" 'org-columns-insert-dblock)
19745 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
19747 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
19748 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
19749 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
19750 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
19751 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
19753 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
19755 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
19757 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
19758 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
19760 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
19763 (defconst org-speed-commands-default
19765 ("Outline Navigation")
19766 ("n" . (org-speed-move-safe 'org-next-visible-heading))
19767 ("p" . (org-speed-move-safe 'org-previous-visible-heading))
19768 ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
19769 ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
19770 ("F" . org-next-block)
19771 ("B" . org-previous-block)
19772 ("u" . (org-speed-move-safe 'outline-up-heading))
19773 ("j" . org-goto)
19774 ("g" . (org-refile t))
19775 ("Outline Visibility")
19776 ("c" . org-cycle)
19777 ("C" . org-shifttab)
19778 (" " . org-display-outline-path)
19779 ("s" . org-narrow-to-subtree)
19780 ("=" . org-columns)
19781 ("Outline Structure Editing")
19782 ("U" . org-metaup)
19783 ("D" . org-metadown)
19784 ("r" . org-metaright)
19785 ("l" . org-metaleft)
19786 ("R" . org-shiftmetaright)
19787 ("L" . org-shiftmetaleft)
19788 ("i" . (progn (forward-char 1) (call-interactively
19789 'org-insert-heading-respect-content)))
19790 ("^" . org-sort)
19791 ("w" . org-refile)
19792 ("a" . org-archive-subtree-default-with-confirmation)
19793 ("@" . org-mark-subtree)
19794 ("#" . org-toggle-comment)
19795 ("Clock Commands")
19796 ("I" . org-clock-in)
19797 ("O" . org-clock-out)
19798 ("Meta Data Editing")
19799 ("t" . org-todo)
19800 ("," . (org-priority))
19801 ("0" . (org-priority ?\ ))
19802 ("1" . (org-priority ?A))
19803 ("2" . (org-priority ?B))
19804 ("3" . (org-priority ?C))
19805 (":" . org-set-tags-command)
19806 ("e" . org-set-effort)
19807 ("E" . org-inc-effort)
19808 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
19809 (org-entry-put (point) "APPT_WARNTIME" m)))
19810 ("Agenda Views etc")
19811 ("v" . org-agenda)
19812 ("/" . org-sparse-tree)
19813 ("Misc")
19814 ("o" . org-open-at-point)
19815 ("?" . org-speed-command-help)
19816 ("<" . (org-agenda-set-restriction-lock 'subtree))
19817 (">" . (org-agenda-remove-restriction-lock))
19819 "The default speed commands.")
19821 (defun org-print-speed-command (e)
19822 (if (> (length (car e)) 1)
19823 (progn
19824 (princ "\n")
19825 (princ (car e))
19826 (princ "\n")
19827 (princ (make-string (length (car e)) ?-))
19828 (princ "\n"))
19829 (princ (car e))
19830 (princ " ")
19831 (if (symbolp (cdr e))
19832 (princ (symbol-name (cdr e)))
19833 (prin1 (cdr e)))
19834 (princ "\n")))
19836 (defun org-speed-command-help ()
19837 "Show the available speed commands."
19838 (interactive)
19839 (if (not org-use-speed-commands)
19840 (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
19841 (with-output-to-temp-buffer "*Help*"
19842 (princ "User-defined Speed commands\n===========================\n")
19843 (mapc #'org-print-speed-command org-speed-commands-user)
19844 (princ "\n")
19845 (princ "Built-in Speed commands\n=======================\n")
19846 (mapc #'org-print-speed-command org-speed-commands-default))
19847 (with-current-buffer "*Help*"
19848 (setq truncate-lines t))))
19850 (defun org-speed-move-safe (cmd)
19851 "Execute CMD, but make sure that the cursor always ends up in a headline.
19852 If not, return to the original position and throw an error."
19853 (interactive)
19854 (let ((pos (point)))
19855 (call-interactively cmd)
19856 (unless (and (bolp) (org-at-heading-p))
19857 (goto-char pos)
19858 (error "Boundary reached while executing %s" cmd))))
19860 (defvar org-self-insert-command-undo-counter 0)
19862 (defvar org-table-auto-blank-field) ; defined in org-table.el
19863 (defvar org-speed-command nil)
19865 (defun org-speed-command-activate (keys)
19866 "Hook for activating single-letter speed commands.
19867 `org-speed-commands-default' specifies a minimal command set.
19868 Use `org-speed-commands-user' for further customization."
19869 (when (or (and (bolp) (looking-at org-outline-regexp))
19870 (and (functionp org-use-speed-commands)
19871 (funcall org-use-speed-commands)))
19872 (cdr (assoc keys (append org-speed-commands-user
19873 org-speed-commands-default)))))
19875 (defun org-babel-speed-command-activate (keys)
19876 "Hook for activating single-letter code block commands."
19877 (when (and (bolp) (looking-at org-babel-src-block-regexp))
19878 (cdr (assoc keys org-babel-key-bindings))))
19880 (defcustom org-speed-command-hook
19881 '(org-speed-command-activate org-babel-speed-command-activate)
19882 "Hook for activating speed commands at strategic locations.
19883 Hook functions are called in sequence until a valid handler is
19884 found.
19886 Each hook takes a single argument, a user-pressed command key
19887 which is also a `self-insert-command' from the global map.
19889 Within the hook, examine the cursor position and the command key
19890 and return nil or a valid handler as appropriate. Handler could
19891 be one of an interactive command, a function, or a form.
19893 Set `org-use-speed-commands' to non-nil value to enable this
19894 hook. The default setting is `org-speed-command-activate'."
19895 :group 'org-structure
19896 :version "24.1"
19897 :type 'hook)
19899 (defun org-self-insert-command (N)
19900 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
19901 If the cursor is in a table looking at whitespace, the whitespace is
19902 overwritten, and the table is not marked as requiring realignment."
19903 (interactive "p")
19904 (org-check-before-invisible-edit 'insert)
19905 (cond
19906 ((and org-use-speed-commands
19907 (let ((kv (this-command-keys-vector)))
19908 (setq org-speed-command
19909 (run-hook-with-args-until-success
19910 'org-speed-command-hook
19911 (make-string 1 (aref kv (1- (length kv))))))))
19912 (cond
19913 ((commandp org-speed-command)
19914 (setq this-command org-speed-command)
19915 (call-interactively org-speed-command))
19916 ((functionp org-speed-command)
19917 (funcall org-speed-command))
19918 ((and org-speed-command (listp org-speed-command))
19919 (eval org-speed-command))
19920 (t (let (org-use-speed-commands)
19921 (call-interactively 'org-self-insert-command)))))
19922 ((and
19923 (org-at-table-p)
19924 (eq N 1)
19925 (not (org-region-active-p))
19926 (progn
19927 ;; Check if we blank the field, and if that triggers align.
19928 (and (featurep 'org-table) org-table-auto-blank-field
19929 (memq last-command
19930 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
19931 (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
19932 ;; Got extra space, this field does not determine
19933 ;; column width.
19934 (let (org-table-may-need-update) (org-table-blank-field))
19935 ;; No extra space, this field may determine column
19936 ;; width.
19937 (org-table-blank-field)))
19939 (looking-at "[^|\n]* \\( \\)|"))
19940 ;; There is room for insertion without re-aligning the table.
19941 (delete-region (match-beginning 1) (match-end 1))
19942 (self-insert-command N))
19944 (setq org-table-may-need-update t)
19945 (self-insert-command N)
19946 (org-fix-tags-on-the-fly)
19947 (when org-self-insert-cluster-for-undo
19948 (if (not (eq last-command 'org-self-insert-command))
19949 (setq org-self-insert-command-undo-counter 1)
19950 (if (>= org-self-insert-command-undo-counter 20)
19951 (setq org-self-insert-command-undo-counter 1)
19952 (and (> org-self-insert-command-undo-counter 0)
19953 buffer-undo-list (listp buffer-undo-list)
19954 (not (cadr buffer-undo-list)) ; remove nil entry
19955 (setcdr buffer-undo-list (cddr buffer-undo-list)))
19956 (setq org-self-insert-command-undo-counter
19957 (1+ org-self-insert-command-undo-counter))))))))
19959 (defun org-check-before-invisible-edit (kind)
19960 "Check is editing if kind KIND would be dangerous with invisible text around.
19961 The detailed reaction depends on the user option `org-catch-invisible-edits'."
19962 ;; First, try to get out of here as quickly as possible, to reduce overhead
19963 (when (and org-catch-invisible-edits
19964 (or (not (boundp 'visible-mode)) (not visible-mode))
19965 (or (get-char-property (point) 'invisible)
19966 (get-char-property (max (point-min) (1- (point))) 'invisible)))
19967 ;; OK, we need to take a closer look. Do not consider
19968 ;; invisibility obtained through text properties (e.g., link
19969 ;; fontification), as it cannot be toggled.
19970 (let* ((invisible-at-point
19971 (pcase (get-char-property-and-overlay (point) 'invisible)
19972 (`(,_ . ,(and (pred overlayp) o)) o)))
19973 ;; Assume that point cannot land in the middle of an
19974 ;; overlay, or between two overlays.
19975 (invisible-before-point
19976 (and (not invisible-at-point)
19977 (not (bobp))
19978 (pcase (get-char-property-and-overlay (1- (point)) 'invisible)
19979 (`(,_ . ,(and (pred overlayp) o)) o))))
19980 (border-and-ok-direction
19982 ;; Check if we are acting predictably before invisible
19983 ;; text.
19984 (and invisible-at-point
19985 (memq kind '(insert delete-backward)))
19986 ;; Check if we are acting predictably after invisible text
19987 ;; This works not well, and I have turned it off. It seems
19988 ;; better to always show and stop after invisible text.
19989 ;; (and (not invisible-at-point) invisible-before-point
19990 ;; (memq kind '(insert delete)))
19992 (when (or invisible-at-point invisible-before-point)
19993 (when (eq org-catch-invisible-edits 'error)
19994 (user-error "Editing in invisible areas is prohibited, make them visible first"))
19995 (if (and org-custom-properties-overlays
19996 (y-or-n-p "Display invisible properties in this buffer? "))
19997 (org-toggle-custom-properties-visibility)
19998 ;; Make the area visible
19999 (save-excursion
20000 (when invisible-before-point
20001 (goto-char
20002 (previous-single-char-property-change (point) 'invisible)))
20003 ;; Remove whatever overlay is currently making yet-to-be
20004 ;; edited text invisible. Also remove nested invisibility
20005 ;; related overlays.
20006 (delete-overlay (or invisible-at-point invisible-before-point))
20007 (let ((origin (if invisible-at-point (point) (1- (point)))))
20008 (while (pcase (get-char-property-and-overlay origin 'invisible)
20009 (`(,_ . ,(and (pred overlayp) o))
20010 (delete-overlay o)
20011 t)))))
20012 (cond
20013 ((eq org-catch-invisible-edits 'show)
20014 ;; That's it, we do the edit after showing
20015 (message
20016 "Unfolding invisible region around point before editing")
20017 (sit-for 1))
20018 ((and (eq org-catch-invisible-edits 'smart)
20019 border-and-ok-direction)
20020 (message "Unfolding invisible region around point before editing"))
20022 ;; Don't do the edit, make the user repeat it in full visibility
20023 (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
20025 (defun org-fix-tags-on-the-fly ()
20026 "Align tags in headline at point.
20027 Unlike to `org-set-tags', it ignores region and sorting."
20028 (when (and (eq (char-after (line-beginning-position)) ?*) ;short-circuit
20029 (org-at-heading-p))
20030 (let ((org-ignore-region t)
20031 (org-tags-sort-function nil))
20032 (org-set-tags nil t))))
20034 (defun org-delete-backward-char (N)
20035 "Like `delete-backward-char', insert whitespace at field end in tables.
20036 When deleting backwards, in tables this function will insert whitespace in
20037 front of the next \"|\" separator, to keep the table aligned. The table will
20038 still be marked for re-alignment if the field did fill the entire column,
20039 because, in this case the deletion might narrow the column."
20040 (interactive "p")
20041 (save-match-data
20042 (org-check-before-invisible-edit 'delete-backward)
20043 (if (and (org-at-table-p)
20044 (eq N 1)
20045 (not (org-region-active-p))
20046 (string-match "|" (buffer-substring (point-at-bol) (point)))
20047 (looking-at ".*?|"))
20048 (let ((pos (point))
20049 (noalign (looking-at "[^|\n\r]* |"))
20050 (c org-table-may-need-update))
20051 (backward-delete-char N)
20052 (unless overwrite-mode
20053 (skip-chars-forward "^|")
20054 (insert " ")
20055 (goto-char (1- pos)))
20056 ;; noalign: if there were two spaces at the end, this field
20057 ;; does not determine the width of the column.
20058 (when noalign (setq org-table-may-need-update c)))
20059 (backward-delete-char N)
20060 (org-fix-tags-on-the-fly))))
20062 (defun org-delete-char (N)
20063 "Like `delete-char', but insert whitespace at field end in tables.
20064 When deleting characters, in tables this function will insert whitespace in
20065 front of the next \"|\" separator, to keep the table aligned. The table will
20066 still be marked for re-alignment if the field did fill the entire column,
20067 because, in this case the deletion might narrow the column."
20068 (interactive "p")
20069 (save-match-data
20070 (org-check-before-invisible-edit 'delete)
20071 (if (and (org-at-table-p)
20072 (not (bolp))
20073 (not (= (char-after) ?|))
20074 (eq N 1))
20075 (if (looking-at ".*?|")
20076 (let ((pos (point))
20077 (noalign (looking-at "[^|\n\r]* |"))
20078 (c org-table-may-need-update))
20079 (replace-match
20080 (concat (substring (match-string 0) 1 -1) " |") nil t)
20081 (goto-char pos)
20082 ;; noalign: if there were two spaces at the end, this field
20083 ;; does not determine the width of the column.
20084 (when noalign (setq org-table-may-need-update c)))
20085 (delete-char N))
20086 (delete-char N)
20087 (org-fix-tags-on-the-fly))))
20089 ;; Make `delete-selection-mode' work with Org mode and Orgtbl mode
20090 (put 'org-self-insert-command 'delete-selection
20091 (lambda ()
20092 (not (run-hook-with-args-until-success
20093 'self-insert-uses-region-functions))))
20094 (put 'orgtbl-self-insert-command 'delete-selection
20095 (lambda ()
20096 (not (run-hook-with-args-until-success
20097 'self-insert-uses-region-functions))))
20098 (put 'org-delete-char 'delete-selection 'supersede)
20099 (put 'org-delete-backward-char 'delete-selection 'supersede)
20100 (put 'org-yank 'delete-selection 'yank)
20102 ;; Make `flyspell-mode' delay after some commands
20103 (put 'org-self-insert-command 'flyspell-delayed t)
20104 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
20105 (put 'org-delete-char 'flyspell-delayed t)
20106 (put 'org-delete-backward-char 'flyspell-delayed t)
20108 ;; Make pabbrev-mode expand after Org mode commands
20109 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
20110 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
20112 (defun org-transpose-words ()
20113 "Transpose words for Org.
20114 This uses the `org-mode-transpose-word-syntax-table' syntax
20115 table, which interprets characters in `org-emphasis-alist' as
20116 word constituents."
20117 (interactive)
20118 (with-syntax-table org-mode-transpose-word-syntax-table
20119 (call-interactively 'transpose-words)))
20120 (org-remap org-mode-map 'transpose-words 'org-transpose-words)
20122 (defvar org-ctrl-c-ctrl-c-hook nil
20123 "Hook for functions attaching themselves to `C-c C-c'.
20125 This can be used to add additional functionality to the C-c C-c
20126 key which executes context-dependent commands. This hook is run
20127 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
20128 run after the last test.
20130 Each function will be called with no arguments. The function
20131 must check if the context is appropriate for it to act. If yes,
20132 it should do its thing and then return a non-nil value. If the
20133 context is wrong, just do nothing and return nil.")
20135 (defvar org-ctrl-c-ctrl-c-final-hook nil
20136 "Hook for functions attaching themselves to `C-c C-c'.
20138 This can be used to add additional functionality to the C-c C-c
20139 key which executes context-dependent commands. This hook is run
20140 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
20141 before the first test.
20143 Each function will be called with no arguments. The function
20144 must check if the context is appropriate for it to act. If yes,
20145 it should do its thing and then return a non-nil value. If the
20146 context is wrong, just do nothing and return nil.")
20148 (defvar org-tab-first-hook nil
20149 "Hook for functions to attach themselves to TAB.
20150 See `org-ctrl-c-ctrl-c-hook' for more information.
20151 This hook runs as the first action when TAB is pressed, even before
20152 `org-cycle' messes around with the `outline-regexp' to cater for
20153 inline tasks and plain list item folding.
20154 If any function in this hook returns t, any other actions that
20155 would have been caused by TAB (such as table field motion or visibility
20156 cycling) will not occur.")
20158 (defvar org-tab-after-check-for-table-hook nil
20159 "Hook for functions to attach themselves to TAB.
20160 See `org-ctrl-c-ctrl-c-hook' for more information.
20161 This hook runs after it has been established that the cursor is not in a
20162 table, but before checking if the cursor is in a headline or if global cycling
20163 should be done.
20164 If any function in this hook returns t, not other actions like visibility
20165 cycling will be done.")
20167 (defvar org-tab-after-check-for-cycling-hook nil
20168 "Hook for functions to attach themselves to TAB.
20169 See `org-ctrl-c-ctrl-c-hook' for more information.
20170 This hook runs after it has been established that not table field motion and
20171 not visibility should be done because of current context. This is probably
20172 the place where a package like yasnippets can hook in.")
20174 (defvar org-tab-before-tab-emulation-hook nil
20175 "Hook for functions to attach themselves to TAB.
20176 See `org-ctrl-c-ctrl-c-hook' for more information.
20177 This hook runs after every other options for TAB have been exhausted, but
20178 before indentation and \t insertion takes place.")
20180 (defvar org-metaleft-hook nil
20181 "Hook for functions attaching themselves to `M-left'.
20182 See `org-ctrl-c-ctrl-c-hook' for more information.")
20183 (defvar org-metaright-hook nil
20184 "Hook for functions attaching themselves to `M-right'.
20185 See `org-ctrl-c-ctrl-c-hook' for more information.")
20186 (defvar org-metaup-hook nil
20187 "Hook for functions attaching themselves to `M-up'.
20188 See `org-ctrl-c-ctrl-c-hook' for more information.")
20189 (defvar org-metadown-hook nil
20190 "Hook for functions attaching themselves to `M-down'.
20191 See `org-ctrl-c-ctrl-c-hook' for more information.")
20192 (defvar org-shiftmetaleft-hook nil
20193 "Hook for functions attaching themselves to `M-S-left'.
20194 See `org-ctrl-c-ctrl-c-hook' for more information.")
20195 (defvar org-shiftmetaright-hook nil
20196 "Hook for functions attaching themselves to `M-S-right'.
20197 See `org-ctrl-c-ctrl-c-hook' for more information.")
20198 (defvar org-shiftmetaup-hook nil
20199 "Hook for functions attaching themselves to `M-S-up'.
20200 See `org-ctrl-c-ctrl-c-hook' for more information.")
20201 (defvar org-shiftmetadown-hook nil
20202 "Hook for functions attaching themselves to `M-S-down'.
20203 See `org-ctrl-c-ctrl-c-hook' for more information.")
20204 (defvar org-metareturn-hook nil
20205 "Hook for functions attaching themselves to `M-RET'.
20206 See `org-ctrl-c-ctrl-c-hook' for more information.")
20207 (defvar org-shiftup-hook nil
20208 "Hook for functions attaching themselves to `S-up'.
20209 See `org-ctrl-c-ctrl-c-hook' for more information.")
20210 (defvar org-shiftup-final-hook nil
20211 "Hook for functions attaching themselves to `S-up'.
20212 This one runs after all other options except shift-select have been excluded.
20213 See `org-ctrl-c-ctrl-c-hook' for more information.")
20214 (defvar org-shiftdown-hook nil
20215 "Hook for functions attaching themselves to `S-down'.
20216 See `org-ctrl-c-ctrl-c-hook' for more information.")
20217 (defvar org-shiftdown-final-hook nil
20218 "Hook for functions attaching themselves to `S-down'.
20219 This one runs after all other options except shift-select have been excluded.
20220 See `org-ctrl-c-ctrl-c-hook' for more information.")
20221 (defvar org-shiftleft-hook nil
20222 "Hook for functions attaching themselves to `S-left'.
20223 See `org-ctrl-c-ctrl-c-hook' for more information.")
20224 (defvar org-shiftleft-final-hook nil
20225 "Hook for functions attaching themselves to `S-left'.
20226 This one runs after all other options except shift-select have been excluded.
20227 See `org-ctrl-c-ctrl-c-hook' for more information.")
20228 (defvar org-shiftright-hook nil
20229 "Hook for functions attaching themselves to `S-right'.
20230 See `org-ctrl-c-ctrl-c-hook' for more information.")
20231 (defvar org-shiftright-final-hook nil
20232 "Hook for functions attaching themselves to `S-right'.
20233 This one runs after all other options except shift-select have been excluded.
20234 See `org-ctrl-c-ctrl-c-hook' for more information.")
20236 (defun org-modifier-cursor-error ()
20237 "Throw an error, a modified cursor command was applied in wrong context."
20238 (user-error "This command is active in special context like tables, headlines or items"))
20240 (defun org-shiftselect-error ()
20241 "Throw an error because Shift-Cursor command was applied in wrong context."
20242 (if (and (boundp 'shift-select-mode) shift-select-mode)
20243 (user-error "To use shift-selection with Org mode, customize `org-support-shift-select'")
20244 (user-error "This command works only in special context like headlines or timestamps")))
20246 (defun org-call-for-shift-select (cmd)
20247 (let ((this-command-keys-shift-translated t))
20248 (call-interactively cmd)))
20250 (defun org-shifttab (&optional arg)
20251 "Global visibility cycling or move to previous table field.
20252 Call `org-table-previous-field' within a table.
20253 When ARG is nil, cycle globally through visibility states.
20254 When ARG is a numeric prefix, show contents of this level."
20255 (interactive "P")
20256 (cond
20257 ((org-at-table-p) (call-interactively 'org-table-previous-field))
20258 ((integerp arg)
20259 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
20260 (message "Content view to level: %d" arg)
20261 (org-content (prefix-numeric-value arg2))
20262 (org-cycle-show-empty-lines t)
20263 (setq org-cycle-global-status 'overview)))
20264 (t (call-interactively 'org-global-cycle))))
20266 (defun org-shiftmetaleft ()
20267 "Promote subtree or delete table column.
20268 Calls `org-promote-subtree', `org-outdent-item-tree', or
20269 `org-table-delete-column', depending on context. See the
20270 individual commands for more information."
20271 (interactive)
20272 (cond
20273 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
20274 ((org-at-table-p) (call-interactively 'org-table-delete-column))
20275 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
20276 ((if (not (org-region-active-p)) (org-at-item-p)
20277 (save-excursion (goto-char (region-beginning))
20278 (org-at-item-p)))
20279 (call-interactively 'org-outdent-item-tree))
20280 (t (org-modifier-cursor-error))))
20282 (defun org-shiftmetaright ()
20283 "Demote subtree or insert table column.
20284 Calls `org-demote-subtree', `org-indent-item-tree', or
20285 `org-table-insert-column', depending on context. See the
20286 individual commands for more information."
20287 (interactive)
20288 (cond
20289 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
20290 ((org-at-table-p) (call-interactively 'org-table-insert-column))
20291 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
20292 ((if (not (org-region-active-p)) (org-at-item-p)
20293 (save-excursion (goto-char (region-beginning))
20294 (org-at-item-p)))
20295 (call-interactively 'org-indent-item-tree))
20296 (t (org-modifier-cursor-error))))
20298 (defun org-shiftmetaup (&optional _arg)
20299 "Drag the line at point up.
20300 In a table, kill the current row.
20301 On a clock timestamp, update the value of the timestamp like `S-<up>'
20302 but also adjust the previous clocked item in the clock history.
20303 Everywhere else, drag the line at point up."
20304 (interactive "P")
20305 (cond
20306 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
20307 ((org-at-table-p) (call-interactively 'org-table-kill-row))
20308 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
20309 (call-interactively 'org-timestamp-up)))
20310 (t (call-interactively 'org-drag-line-backward))))
20312 (defun org-shiftmetadown (&optional _arg)
20313 "Drag the line at point down.
20314 In a table, insert an empty row at the current line.
20315 On a clock timestamp, update the value of the timestamp like `S-<down>'
20316 but also adjust the previous clocked item in the clock history.
20317 Everywhere else, drag the line at point down."
20318 (interactive "P")
20319 (cond
20320 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
20321 ((org-at-table-p) (call-interactively 'org-table-insert-row))
20322 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
20323 (call-interactively 'org-timestamp-down)))
20324 (t (call-interactively 'org-drag-line-forward))))
20326 (defsubst org-hidden-tree-error ()
20327 (user-error
20328 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
20330 (defun org-metaleft (&optional _arg)
20331 "Promote heading, list item at point or move table column left.
20333 Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
20334 depending on context. With no specific context, calls the Emacs
20335 default `backward-word'. See the individual commands for more
20336 information.
20338 This function runs the hook `org-metaleft-hook' as a first step,
20339 and returns at first non-nil value."
20340 (interactive "P")
20341 (cond
20342 ((run-hook-with-args-until-success 'org-metaleft-hook))
20343 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
20344 ((org-with-limited-levels
20345 (or (org-at-heading-p)
20346 (and (org-region-active-p)
20347 (save-excursion
20348 (goto-char (region-beginning))
20349 (org-at-heading-p)))))
20350 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
20351 (call-interactively 'org-do-promote))
20352 ;; At an inline task.
20353 ((org-at-heading-p)
20354 (call-interactively 'org-inlinetask-promote))
20355 ((or (org-at-item-p)
20356 (and (org-region-active-p)
20357 (save-excursion
20358 (goto-char (region-beginning))
20359 (org-at-item-p))))
20360 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
20361 (call-interactively 'org-outdent-item))
20362 (t (call-interactively 'backward-word))))
20364 (defun org-metaright (&optional _arg)
20365 "Demote heading, list item at point or move table column right.
20367 In front of a drawer or a block keyword, indent it correctly.
20369 Calls `org-do-demote', `org-indent-item', `org-table-move-column',
20370 `org-indent-drawer' or `org-indent-block' depending on context.
20371 With no specific context, calls the Emacs default `forward-word'.
20372 See the individual commands for more information.
20374 This function runs the hook `org-metaright-hook' as a first step,
20375 and returns at first non-nil value."
20376 (interactive "P")
20377 (cond
20378 ((run-hook-with-args-until-success 'org-metaright-hook))
20379 ((org-at-table-p) (call-interactively 'org-table-move-column))
20380 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
20381 ((org-at-block-p) (call-interactively 'org-indent-block))
20382 ((org-with-limited-levels
20383 (or (org-at-heading-p)
20384 (and (org-region-active-p)
20385 (save-excursion
20386 (goto-char (region-beginning))
20387 (org-at-heading-p)))))
20388 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
20389 (call-interactively 'org-do-demote))
20390 ;; At an inline task.
20391 ((org-at-heading-p)
20392 (call-interactively 'org-inlinetask-demote))
20393 ((or (org-at-item-p)
20394 (and (org-region-active-p)
20395 (save-excursion
20396 (goto-char (region-beginning))
20397 (org-at-item-p))))
20398 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
20399 (call-interactively 'org-indent-item))
20400 (t (call-interactively 'forward-word))))
20402 (defun org-check-for-hidden (what)
20403 "Check if there are hidden headlines/items in the current visual line.
20404 WHAT can be either `headlines' or `items'. If the current line is
20405 an outline or item heading and it has a folded subtree below it,
20406 this function returns t, nil otherwise."
20407 (let ((re (cond
20408 ((eq what 'headlines) org-outline-regexp-bol)
20409 ((eq what 'items) (org-item-beginning-re))
20410 (t (error "This should not happen"))))
20411 beg end)
20412 (save-excursion
20413 (catch 'exit
20414 (unless (org-region-active-p)
20415 (setq beg (point-at-bol))
20416 (beginning-of-line 2)
20417 (while (and (not (eobp)) ;; this is like `next-line'
20418 (get-char-property (1- (point)) 'invisible))
20419 (beginning-of-line 2))
20420 (setq end (point))
20421 (goto-char beg)
20422 (goto-char (point-at-eol))
20423 (setq end (max end (point)))
20424 (while (re-search-forward re end t)
20425 (when (get-char-property (match-beginning 0) 'invisible)
20426 (throw 'exit t))))
20427 nil))))
20429 (defun org-metaup (&optional _arg)
20430 "Move subtree up or move table row up.
20431 Calls `org-move-subtree-up' or `org-table-move-row' or
20432 `org-move-item-up', depending on context. See the individual commands
20433 for more information."
20434 (interactive "P")
20435 (cond
20436 ((run-hook-with-args-until-success 'org-metaup-hook))
20437 ((org-region-active-p)
20438 (let* ((a (min (region-beginning) (region-end)))
20439 (b (1- (max (region-beginning) (region-end))))
20440 (c (save-excursion (goto-char a)
20441 (move-beginning-of-line 0)))
20442 (d (save-excursion (goto-char a)
20443 (move-end-of-line 0) (point))))
20444 (transpose-regions a b c d)
20445 (goto-char c)))
20446 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
20447 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
20448 ((org-at-item-p) (call-interactively 'org-move-item-up))
20449 (t (org-drag-element-backward))))
20451 (defun org-metadown (&optional _arg)
20452 "Move subtree down or move table row down.
20453 Calls `org-move-subtree-down' or `org-table-move-row' or
20454 `org-move-item-down', depending on context. See the individual
20455 commands for more information."
20456 (interactive "P")
20457 (cond
20458 ((run-hook-with-args-until-success 'org-metadown-hook))
20459 ((org-region-active-p)
20460 (let* ((a (min (region-beginning) (region-end)))
20461 (b (max (region-beginning) (region-end)))
20462 (c (save-excursion (goto-char b)
20463 (move-beginning-of-line 1)))
20464 (d (save-excursion (goto-char b)
20465 (move-end-of-line 1) (1+ (point)))))
20466 (transpose-regions a b c d)
20467 (goto-char d)))
20468 ((org-at-table-p) (call-interactively 'org-table-move-row))
20469 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
20470 ((org-at-item-p) (call-interactively 'org-move-item-down))
20471 (t (org-drag-element-forward))))
20473 (defun org-shiftup (&optional arg)
20474 "Increase item in timestamp or increase priority of current headline.
20475 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
20476 depending on context. See the individual commands for more information."
20477 (interactive "P")
20478 (cond
20479 ((run-hook-with-args-until-success 'org-shiftup-hook))
20480 ((and org-support-shift-select (org-region-active-p))
20481 (org-call-for-shift-select 'previous-line))
20482 ((org-at-timestamp-p 'lax)
20483 (call-interactively (if org-edit-timestamp-down-means-later
20484 'org-timestamp-down 'org-timestamp-up)))
20485 ((and (not (eq org-support-shift-select 'always))
20486 org-enable-priority-commands
20487 (org-at-heading-p))
20488 (call-interactively 'org-priority-up))
20489 ((and (not org-support-shift-select) (org-at-item-p))
20490 (call-interactively 'org-previous-item))
20491 ((org-clocktable-try-shift 'up arg))
20492 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
20493 (org-support-shift-select
20494 (org-call-for-shift-select 'previous-line))
20495 (t (org-shiftselect-error))))
20497 (defun org-shiftdown (&optional arg)
20498 "Decrease item in timestamp or decrease priority of current headline.
20499 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
20500 depending on context. See the individual commands for more information."
20501 (interactive "P")
20502 (cond
20503 ((run-hook-with-args-until-success 'org-shiftdown-hook))
20504 ((and org-support-shift-select (org-region-active-p))
20505 (org-call-for-shift-select 'next-line))
20506 ((org-at-timestamp-p 'lax)
20507 (call-interactively (if org-edit-timestamp-down-means-later
20508 'org-timestamp-up 'org-timestamp-down)))
20509 ((and (not (eq org-support-shift-select 'always))
20510 org-enable-priority-commands
20511 (org-at-heading-p))
20512 (call-interactively 'org-priority-down))
20513 ((and (not org-support-shift-select) (org-at-item-p))
20514 (call-interactively 'org-next-item))
20515 ((org-clocktable-try-shift 'down arg))
20516 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
20517 (org-support-shift-select
20518 (org-call-for-shift-select 'next-line))
20519 (t (org-shiftselect-error))))
20521 (defun org-shiftright (&optional arg)
20522 "Cycle the thing at point or in the current line, depending on context.
20523 Depending on context, this does one of the following:
20525 - switch a timestamp at point one day into the future
20526 - on a headline, switch to the next TODO keyword.
20527 - on an item, switch entire list to the next bullet type
20528 - on a property line, switch to the next allowed value
20529 - on a clocktable definition line, move time block into the future"
20530 (interactive "P")
20531 (cond
20532 ((run-hook-with-args-until-success 'org-shiftright-hook))
20533 ((and org-support-shift-select (org-region-active-p))
20534 (org-call-for-shift-select 'forward-char))
20535 ((org-at-timestamp-p 'lax) (call-interactively 'org-timestamp-up-day))
20536 ((and (not (eq org-support-shift-select 'always))
20537 (org-at-heading-p))
20538 (let ((org-inhibit-logging
20539 (not org-treat-S-cursor-todo-selection-as-state-change))
20540 (org-inhibit-blocking
20541 (not org-treat-S-cursor-todo-selection-as-state-change)))
20542 (org-call-with-arg 'org-todo 'right)))
20543 ((or (and org-support-shift-select
20544 (not (eq org-support-shift-select 'always))
20545 (org-at-item-bullet-p))
20546 (and (not org-support-shift-select) (org-at-item-p)))
20547 (org-call-with-arg 'org-cycle-list-bullet nil))
20548 ((and (not (eq org-support-shift-select 'always))
20549 (org-at-property-p))
20550 (call-interactively 'org-property-next-allowed-value))
20551 ((org-clocktable-try-shift 'right arg))
20552 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
20553 (org-support-shift-select
20554 (org-call-for-shift-select 'forward-char))
20555 (t (org-shiftselect-error))))
20557 (defun org-shiftleft (&optional arg)
20558 "Cycle the thing at point or in the current line, depending on context.
20559 Depending on context, this does one of the following:
20561 - switch a timestamp at point one day into the past
20562 - on a headline, switch to the previous TODO keyword.
20563 - on an item, switch entire list to the previous bullet type
20564 - on a property line, switch to the previous allowed value
20565 - on a clocktable definition line, move time block into the past"
20566 (interactive "P")
20567 (cond
20568 ((run-hook-with-args-until-success 'org-shiftleft-hook))
20569 ((and org-support-shift-select (org-region-active-p))
20570 (org-call-for-shift-select 'backward-char))
20571 ((org-at-timestamp-p 'lax) (call-interactively 'org-timestamp-down-day))
20572 ((and (not (eq org-support-shift-select 'always))
20573 (org-at-heading-p))
20574 (let ((org-inhibit-logging
20575 (not org-treat-S-cursor-todo-selection-as-state-change))
20576 (org-inhibit-blocking
20577 (not org-treat-S-cursor-todo-selection-as-state-change)))
20578 (org-call-with-arg 'org-todo 'left)))
20579 ((or (and org-support-shift-select
20580 (not (eq org-support-shift-select 'always))
20581 (org-at-item-bullet-p))
20582 (and (not org-support-shift-select) (org-at-item-p)))
20583 (org-call-with-arg 'org-cycle-list-bullet 'previous))
20584 ((and (not (eq org-support-shift-select 'always))
20585 (org-at-property-p))
20586 (call-interactively 'org-property-previous-allowed-value))
20587 ((org-clocktable-try-shift 'left arg))
20588 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
20589 (org-support-shift-select
20590 (org-call-for-shift-select 'backward-char))
20591 (t (org-shiftselect-error))))
20593 (defun org-shiftcontrolright ()
20594 "Switch to next TODO set."
20595 (interactive)
20596 (cond
20597 ((and org-support-shift-select (org-region-active-p))
20598 (org-call-for-shift-select 'forward-word))
20599 ((and (not (eq org-support-shift-select 'always))
20600 (org-at-heading-p))
20601 (org-call-with-arg 'org-todo 'nextset))
20602 (org-support-shift-select
20603 (org-call-for-shift-select 'forward-word))
20604 (t (org-shiftselect-error))))
20606 (defun org-shiftcontrolleft ()
20607 "Switch to previous TODO set."
20608 (interactive)
20609 (cond
20610 ((and org-support-shift-select (org-region-active-p))
20611 (org-call-for-shift-select 'backward-word))
20612 ((and (not (eq org-support-shift-select 'always))
20613 (org-at-heading-p))
20614 (org-call-with-arg 'org-todo 'previousset))
20615 (org-support-shift-select
20616 (org-call-for-shift-select 'backward-word))
20617 (t (org-shiftselect-error))))
20619 (defun org-shiftcontrolup (&optional n)
20620 "Change timestamps synchronously up in CLOCK log lines.
20621 Optional argument N tells to change by that many units."
20622 (interactive "P")
20623 (if (and (org-at-clock-log-p) (org-at-timestamp-p 'lax))
20624 (let (org-support-shift-select)
20625 (org-clock-timestamps-up n))
20626 (user-error "Not at a clock log")))
20628 (defun org-shiftcontroldown (&optional n)
20629 "Change timestamps synchronously down in CLOCK log lines.
20630 Optional argument N tells to change by that many units."
20631 (interactive "P")
20632 (if (and (org-at-clock-log-p) (org-at-timestamp-p 'lax))
20633 (let (org-support-shift-select)
20634 (org-clock-timestamps-down n))
20635 (user-error "Not at a clock log")))
20637 (defun org-increase-number-at-point (&optional inc)
20638 "Increment the number at point.
20639 With an optional prefix numeric argument INC, increment using
20640 this numeric value."
20641 (interactive "p")
20642 (if (not (number-at-point))
20643 (user-error "Not on a number")
20644 (unless inc (setq inc 1))
20645 (let ((pos (point))
20646 (beg (skip-chars-backward "-+^/*0-9eE."))
20647 (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
20648 (setq nap (buffer-substring-no-properties
20649 (+ pos beg) (+ pos beg end)))
20650 (delete-region (+ pos beg) (+ pos beg end))
20651 (insert (calc-eval (concat (number-to-string inc) "+" nap))))
20652 (when (org-at-table-p)
20653 (org-table-align)
20654 (org-table-end-of-field 1))))
20656 (defun org-decrease-number-at-point (&optional inc)
20657 "Decrement the number at point.
20658 With an optional prefix numeric argument INC, decrement using
20659 this numeric value."
20660 (interactive "p")
20661 (org-increase-number-at-point (- (or inc 1))))
20663 (defun org-ctrl-c-ret ()
20664 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
20665 (interactive)
20666 (cond
20667 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
20668 (t (call-interactively 'org-insert-heading))))
20670 (defun org-find-visible ()
20671 (let ((s (point)))
20672 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
20673 (get-char-property s 'invisible)))
20675 (defun org-find-invisible ()
20676 (let ((s (point)))
20677 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
20678 (not (get-char-property s 'invisible))))
20681 (defun org-copy-visible (beg end)
20682 "Copy the visible parts of the region."
20683 (interactive "r")
20684 (let ((result ""))
20685 (while (/= beg end)
20686 (when (get-char-property beg 'invisible)
20687 (setq beg (next-single-char-property-change beg 'invisible nil end)))
20688 (let ((next (next-single-char-property-change beg 'invisible nil end)))
20689 (setq result (concat result (buffer-substring beg next)))
20690 (setq beg next)))
20691 (kill-new result)))
20693 (defun org-copy-special ()
20694 "Copy region in table or copy current subtree.
20695 Calls `org-table-copy-region' or `org-copy-subtree', depending on
20696 context. See the individual commands for more information."
20697 (interactive)
20698 (call-interactively
20699 (if (org-at-table-p) #'org-table-copy-region #'org-copy-subtree)))
20701 (defun org-cut-special ()
20702 "Cut region in table or cut current subtree.
20703 Calls `org-table-cut-region' or `org-cut-subtree', depending on
20704 context. See the individual commands for more information."
20705 (interactive)
20706 (call-interactively
20707 (if (org-at-table-p) #'org-table-cut-region #'org-cut-subtree)))
20709 (defun org-paste-special (arg)
20710 "Paste rectangular region into table, or past subtree relative to level.
20711 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
20712 See the individual commands for more information."
20713 (interactive "P")
20714 (if (org-at-table-p)
20715 (org-table-paste-rectangle)
20716 (org-paste-subtree arg)))
20718 (defun org-edit-special (&optional arg)
20719 "Call a special editor for the element at point.
20720 When at a table, call the formula editor with `org-table-edit-formulas'.
20721 When in a source code block, call `org-edit-src-code'.
20722 When in a fixed-width region, call `org-edit-fixed-width-region'.
20723 When in an export block, call `org-edit-export-block'.
20724 When in a LaTeX environment, call `org-edit-latex-environment'.
20725 When at an #+INCLUDE keyword, visit the included file.
20726 When at a footnote reference, call `org-edit-footnote-reference'
20727 On a link, call `ffap' to visit the link at point.
20728 Otherwise, return a user error."
20729 (interactive "P")
20730 (let ((element (org-element-at-point)))
20731 (barf-if-buffer-read-only)
20732 (pcase (org-element-type element)
20733 (`src-block
20734 (if (not arg) (org-edit-src-code)
20735 (let* ((info (org-babel-get-src-block-info))
20736 (lang (nth 0 info))
20737 (params (nth 2 info))
20738 (session (cdr (assq :session params))))
20739 (if (not session) (org-edit-src-code)
20740 ;; At a src-block with a session and function called with
20741 ;; an ARG: switch to the buffer related to the inferior
20742 ;; process.
20743 (switch-to-buffer
20744 (funcall (intern (concat "org-babel-prep-session:" lang))
20745 session params))))))
20746 (`keyword
20747 (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
20748 (org-open-link-from-string
20749 (format "[[%s]]"
20750 (expand-file-name
20751 (let ((value (org-element-property :value element)))
20752 (cond ((org-file-url-p value)
20753 (user-error "The file is specified as a URL, cannot be edited"))
20754 ((not (org-string-nw-p value))
20755 (user-error "No file to edit"))
20756 ((string-match "\\`\"\\(.*?\\)\"" value)
20757 (match-string 1 value))
20758 ((string-match "\\`[^ \t\"]\\S-*" value)
20759 (match-string 0 value))
20760 (t (user-error "No valid file specified")))))))
20761 (user-error "No special environment to edit here")))
20762 (`table
20763 (if (eq (org-element-property :type element) 'table.el)
20764 (org-edit-table.el)
20765 (call-interactively 'org-table-edit-formulas)))
20766 ;; Only Org tables contain `table-row' type elements.
20767 (`table-row (call-interactively 'org-table-edit-formulas))
20768 (`example-block (org-edit-src-code))
20769 (`export-block (org-edit-export-block))
20770 (`fixed-width (org-edit-fixed-width-region))
20771 (`latex-environment (org-edit-latex-environment))
20773 ;; No notable element at point. Though, we may be at a link or
20774 ;; a footnote reference, which are objects. Thus, scan deeper.
20775 (let ((context (org-element-context element)))
20776 (pcase (org-element-type context)
20777 (`footnote-reference (org-edit-footnote-reference))
20778 (`inline-src-block (org-edit-inline-src-code))
20779 (`link (call-interactively #'ffap))
20780 (_ (user-error "No special environment to edit here"))))))))
20782 (defvar org-table-coordinate-overlays) ; defined in org-table.el
20783 (defun org-ctrl-c-ctrl-c (&optional arg)
20784 "Set tags in headline, or update according to changed information at point.
20786 This command does many different things, depending on context:
20788 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
20789 this is what we do.
20791 - If the cursor is on a statistics cookie, update it.
20793 - If the cursor is in a headline, prompt for tags and insert them
20794 into the current line, aligned to `org-tags-column'. When called
20795 with prefix arg, realign all tags in the current buffer.
20797 - If the cursor is in one of the special #+KEYWORD lines, this
20798 triggers scanning the buffer for these lines and updating the
20799 information.
20801 - If the cursor is inside a table, realign the table. This command
20802 works even if the automatic table editor has been turned off.
20804 - If the cursor is on a #+TBLFM line, re-apply the formulas to
20805 the entire table.
20807 - If the cursor is at a footnote reference or definition, jump to
20808 the corresponding definition or references, respectively.
20810 - If the cursor is a the beginning of a dynamic block, update it.
20812 - If the current buffer is a capture buffer, close note and file it.
20814 - If the cursor is on a <<<target>>>, update radio targets and
20815 corresponding links in this buffer.
20817 - If the cursor is on a numbered item in a plain list, renumber the
20818 ordered list.
20820 - If the cursor is on a checkbox, toggle it.
20822 - If the cursor is on a code block, evaluate it. The variable
20823 `org-confirm-babel-evaluate' can be used to control prompting
20824 before code block evaluation, by default every code block
20825 evaluation requires confirmation. Code block evaluation can be
20826 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
20827 (interactive "P")
20828 (cond
20829 ((or (bound-and-true-p org-clock-overlays) org-occur-highlights)
20830 (when (boundp 'org-clock-overlays) (org-clock-remove-overlays))
20831 (org-remove-occur-highlights)
20832 (message "Temporary highlights/overlays removed from current buffer"))
20833 ((and (local-variable-p 'org-finish-function)
20834 (fboundp org-finish-function))
20835 (funcall org-finish-function))
20836 ((org-babel-hash-at-point))
20837 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
20839 (let* ((context
20840 (org-element-lineage
20841 (org-element-context)
20842 ;; Limit to supported contexts.
20843 '(babel-call clock dynamic-block footnote-definition
20844 footnote-reference inline-babel-call inline-src-block
20845 inlinetask item keyword node-property paragraph
20846 plain-list planning property-drawer radio-target
20847 src-block statistics-cookie table table-cell table-row
20848 timestamp)
20850 (type (org-element-type context)))
20851 ;; For convenience: at the first line of a paragraph on the same
20852 ;; line as an item, apply function on that item instead.
20853 (when (eq type 'paragraph)
20854 (let ((parent (org-element-property :parent context)))
20855 (when (and (eq (org-element-type parent) 'item)
20856 (= (line-beginning-position)
20857 (org-element-property :begin parent)))
20858 (setq context parent)
20859 (setq type 'item))))
20860 ;; Act according to type of element or object at point.
20862 ;; Do nothing on a blank line, except if it is contained in
20863 ;; a src block. Hence, we first check if point is in such
20864 ;; a block and then if it is at a blank line.
20865 (pcase type
20866 ((or `inline-src-block `src-block)
20867 (unless org-babel-no-eval-on-ctrl-c-ctrl-c
20868 (org-babel-eval-wipe-error-buffer)
20869 (org-babel-execute-src-block
20870 current-prefix-arg (org-babel-get-src-block-info nil context))))
20871 ((guard (org-match-line "[ \t]*$"))
20872 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
20873 (user-error
20874 (substitute-command-keys
20875 "`\\[org-ctrl-c-ctrl-c]' can do nothing useful here"))))
20876 ((or `babel-call `inline-babel-call)
20877 (let ((info (org-babel-lob-get-info context)))
20878 (when info (org-babel-execute-src-block nil info))))
20879 (`clock (org-clock-update-time-maybe))
20880 (`dynamic-block
20881 (save-excursion
20882 (goto-char (org-element-property :post-affiliated context))
20883 (org-update-dblock)))
20884 (`footnote-definition
20885 (goto-char (org-element-property :post-affiliated context))
20886 (call-interactively 'org-footnote-action))
20887 (`footnote-reference (call-interactively #'org-footnote-action))
20888 ((or `headline `inlinetask)
20889 (save-excursion (goto-char (org-element-property :begin context))
20890 (call-interactively #'org-set-tags)))
20891 (`item
20892 ;; At an item: `C-u C-u' sets checkbox to "[-]"
20893 ;; unconditionally, whereas `C-u' will toggle its presence.
20894 ;; Without a universal argument, if the item has a checkbox,
20895 ;; toggle it. Otherwise repair the list.
20896 (let* ((box (org-element-property :checkbox context))
20897 (struct (org-element-property :structure context))
20898 (old-struct (copy-tree struct))
20899 (parents (org-list-parents-alist struct))
20900 (prevs (org-list-prevs-alist struct))
20901 (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
20902 (org-list-set-checkbox
20903 (org-element-property :begin context) struct
20904 (cond ((equal arg '(16)) "[-]")
20905 ((and (not box) (equal arg '(4))) "[ ]")
20906 ((or (not box) (equal arg '(4))) nil)
20907 ((eq box 'on) "[ ]")
20908 (t "[X]")))
20909 ;; Mimic `org-list-write-struct' but with grabbing a return
20910 ;; value from `org-list-struct-fix-box'.
20911 (org-list-struct-fix-ind struct parents 2)
20912 (org-list-struct-fix-item-end struct)
20913 (org-list-struct-fix-bul struct prevs)
20914 (org-list-struct-fix-ind struct parents)
20915 (let ((block-item
20916 (org-list-struct-fix-box struct parents prevs orderedp)))
20917 (if (and box (equal struct old-struct))
20918 (if (equal arg '(16))
20919 (message "Checkboxes already reset")
20920 (user-error "Cannot toggle this checkbox: %s"
20921 (if (eq box 'on)
20922 "all subitems checked"
20923 "unchecked subitems")))
20924 (org-list-struct-apply-struct struct old-struct)
20925 (org-update-checkbox-count-maybe))
20926 (when block-item
20927 (message "Checkboxes were removed due to empty box at line %d"
20928 (org-current-line block-item))))))
20929 (`keyword
20930 (let ((org-inhibit-startup-visibility-stuff t)
20931 (org-startup-align-all-tables nil))
20932 (when (boundp 'org-table-coordinate-overlays)
20933 (mapc #'delete-overlay org-table-coordinate-overlays)
20934 (setq org-table-coordinate-overlays nil))
20935 (org-save-outline-visibility 'use-markers (org-mode-restart)))
20936 (message "Local setup has been refreshed"))
20937 (`plain-list
20938 ;; At a plain list, with a double C-u argument, set
20939 ;; checkboxes of each item to "[-]", whereas a single one
20940 ;; will toggle their presence according to the state of the
20941 ;; first item in the list. Without an argument, repair the
20942 ;; list.
20943 (let* ((begin (org-element-property :contents-begin context))
20944 (beginm (move-marker (make-marker) begin))
20945 (struct (org-element-property :structure context))
20946 (old-struct (copy-tree struct))
20947 (first-box (save-excursion
20948 (goto-char begin)
20949 (looking-at org-list-full-item-re)
20950 (match-string-no-properties 3)))
20951 (new-box (cond ((equal arg '(16)) "[-]")
20952 ((equal arg '(4)) (unless first-box "[ ]"))
20953 ((equal first-box "[X]") "[ ]")
20954 (t "[X]"))))
20955 (cond
20956 (arg
20957 (dolist (pos
20958 (org-list-get-all-items
20959 begin struct (org-list-prevs-alist struct)))
20960 (org-list-set-checkbox pos struct new-box)))
20961 ((and first-box (eq (point) begin))
20962 ;; For convenience, when point is at bol on the first
20963 ;; item of the list and no argument is provided, simply
20964 ;; toggle checkbox of that item, if any.
20965 (org-list-set-checkbox begin struct new-box)))
20966 (org-list-write-struct
20967 struct (org-list-parents-alist struct) old-struct)
20968 (org-update-checkbox-count-maybe)
20969 (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
20970 ((or `property-drawer `node-property)
20971 (call-interactively #'org-property-action))
20972 (`radio-target
20973 (call-interactively #'org-update-radio-target-regexp))
20974 (`statistics-cookie
20975 (call-interactively #'org-update-statistics-cookies))
20976 ((or `table `table-cell `table-row)
20977 ;; At a table, recalculate every field and align it. Also
20978 ;; send the table if necessary. If the table has
20979 ;; a `table.el' type, just give up. At a table row or cell,
20980 ;; maybe recalculate line but always align table.
20981 (if (eq (org-element-property :type context) 'table.el)
20982 (message "%s" (substitute-command-keys "\\<org-mode-map>\
20983 Use `\\[org-edit-special]' to edit table.el tables"))
20984 (if (or (eq type 'table)
20985 ;; Check if point is at a TBLFM line.
20986 (and (eq type 'table-row)
20987 (= (point) (org-element-property :end context))))
20988 (save-excursion
20989 (if (org-at-TBLFM-p)
20990 (progn (require 'org-table)
20991 (org-table-calc-current-TBLFM))
20992 (goto-char (org-element-property :contents-begin context))
20993 (org-call-with-arg 'org-table-recalculate (or arg t))
20994 (orgtbl-send-table 'maybe)))
20995 (org-table-maybe-eval-formula)
20996 (cond (arg (call-interactively #'org-table-recalculate))
20997 ((org-table-maybe-recalculate-line))
20998 (t (org-table-align))))))
20999 ((or `timestamp (and `planning (guard (org-at-timestamp-p 'lax))))
21000 (org-timestamp-change 0 'day))
21001 ((and `nil (guard (org-at-heading-p)))
21002 ;; When point is on an unsupported object type, we can miss
21003 ;; the fact that it also is at a heading. Handle it here.
21004 (call-interactively #'org-set-tags))
21005 ((guard
21006 (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)))
21008 (user-error
21009 (substitute-command-keys
21010 "`\\[org-ctrl-c-ctrl-c]' can do nothing useful here"))))))))
21012 (defun org-mode-restart ()
21013 (interactive)
21014 (let ((indent-status (bound-and-true-p org-indent-mode)))
21015 (funcall major-mode)
21016 (hack-local-variables)
21017 (when (and indent-status (not (bound-and-true-p org-indent-mode)))
21018 (org-indent-mode -1))
21019 (org-reset-file-cache))
21020 (message "%s restarted" major-mode))
21022 (defun org-kill-note-or-show-branches ()
21023 "Abort storing current note, or call `outline-show-branches'."
21024 (interactive)
21025 (if (not org-finish-function)
21026 (save-excursion
21027 (save-restriction
21028 (org-narrow-to-subtree)
21029 (org-flag-subtree t)
21030 (call-interactively 'outline-show-branches)
21031 (org-hide-archived-subtrees (point-min) (point-max))))
21032 (let ((org-note-abort t))
21033 (funcall org-finish-function))))
21035 (defun org-delete-indentation (&optional arg)
21036 "Join current line to previous and fix whitespace at join.
21038 If previous line is a headline add to headline title. Otherwise
21039 the function calls `delete-indentation'.
21041 With a non-nil optional argument, join it to the following one."
21042 (interactive "*P")
21043 (if (save-excursion
21044 (beginning-of-line (if arg 1 0))
21045 (let ((case-fold-search nil))
21046 (looking-at org-complex-heading-regexp)))
21047 ;; At headline.
21048 (let ((tags-column (when (match-beginning 5)
21049 (save-excursion (goto-char (match-beginning 5))
21050 (current-column))))
21051 (string (concat " " (progn (when arg (forward-line 1))
21052 (org-trim (delete-and-extract-region
21053 (line-beginning-position)
21054 (line-end-position)))))))
21055 (unless (bobp) (delete-region (point) (1- (point))))
21056 (goto-char (or (match-end 4)
21057 (match-beginning 5)
21058 (match-end 0)))
21059 (skip-chars-backward " \t")
21060 (save-excursion (insert string))
21061 ;; Adjust alignment of tags.
21062 (cond
21063 ((not tags-column)) ;no tags
21064 (org-auto-align-tags (org-set-tags nil t))
21065 (t (org--align-tags-here tags-column)))) ;preserve tags column
21066 (delete-indentation arg)))
21068 (defun org-open-line (n)
21069 "Insert a new row in tables, call `open-line' elsewhere.
21070 If `org-special-ctrl-o' is nil, just call `open-line' everywhere.
21071 As a special case, when a document starts with a table, allow to
21072 call `open-line' on the very first character."
21073 (interactive "*p")
21074 (if (and org-special-ctrl-o (/= (point) 1) (org-at-table-p))
21075 (org-table-insert-row)
21076 (open-line n)))
21078 (defun org-return (&optional indent)
21079 "Goto next table row or insert a newline.
21081 Calls `org-table-next-row' or `newline', depending on context.
21083 When optional INDENT argument is non-nil, call
21084 `newline-and-indent' instead of `newline'.
21086 When `org-return-follows-link' is non-nil and point is on
21087 a timestamp or a link, call `org-open-at-point'. However, it
21088 will not happen if point is in a table or on a \"dead\"
21089 object (e.g., within a comment). In these case, you need to use
21090 `org-open-at-point' directly."
21091 (interactive)
21092 (let ((context (if org-return-follows-link (org-element-context)
21093 (org-element-at-point))))
21094 (cond
21095 ;; In a table, call `org-table-next-row'. However, before first
21096 ;; column or after last one, split the table.
21097 ((or (and (eq (org-element-type context) 'table)
21098 (>= (point) (org-element-property :contents-begin context))
21099 (< (point) (org-element-property :contents-end context)))
21100 (org-element-lineage context '(table-row table-cell) t))
21101 (if (or (looking-at-p "[ \t]*$")
21102 (save-excursion (skip-chars-backward " \t") (bolp)))
21103 (insert "\n")
21104 (org-table-justify-field-maybe)
21105 (call-interactively #'org-table-next-row)))
21106 ;; On a link or a timestamp, call `org-open-at-point' if
21107 ;; `org-return-follows-link' allows it. Tolerate fuzzy
21108 ;; locations, e.g., in a comment, as `org-open-at-point'.
21109 ((and org-return-follows-link
21110 (or (and (eq 'link (org-element-type context))
21111 ;; Ensure point is not on the white spaces after
21112 ;; the link.
21113 (let ((origin (point)))
21114 (org-with-point-at (org-element-property :end context)
21115 (skip-chars-backward " \t")
21116 (> (point) origin))))
21117 (org-in-regexp org-ts-regexp-both nil t)
21118 (org-in-regexp org-tsr-regexp-both nil t)
21119 (org-in-regexp org-any-link-re nil t)))
21120 (call-interactively #'org-open-at-point))
21121 ;; Insert newline in heading, but preserve tags.
21122 ((and (not (bolp))
21123 (save-excursion (beginning-of-line)
21124 (let ((case-fold-search nil))
21125 (looking-at org-complex-heading-regexp))))
21126 ;; At headline. Split line. However, if point is on keyword,
21127 ;; priority cookie or tags, do not break any of them: add
21128 ;; a newline after the headline instead.
21129 (let ((tags-column (and (match-beginning 5)
21130 (save-excursion (goto-char (match-beginning 5))
21131 (current-column))))
21132 (string
21133 (when (and (match-end 4) (org-point-in-group (point) 4))
21134 (delete-and-extract-region (point) (match-end 4)))))
21135 ;; Adjust tag alignment.
21136 (cond
21137 ((not (and tags-column string)))
21138 (org-auto-align-tags (org-set-tags nil t))
21139 (t (org--align-tags-here tags-column))) ;preserve tags column
21140 (end-of-line)
21141 (org-show-entry)
21142 (if indent (newline-and-indent) (newline))
21143 (when string (save-excursion (insert (org-trim string))))))
21144 ;; In a list, make sure indenting keeps trailing text within.
21145 ((and indent
21146 (not (eolp))
21147 (org-element-lineage context '(item)))
21148 (let ((trailing-data
21149 (delete-and-extract-region (point) (line-end-position))))
21150 (newline-and-indent)
21151 (save-excursion (insert trailing-data))))
21153 ;; Do not auto-fill when point is in an Org property drawer.
21154 (let ((auto-fill-function (and (not (org-at-property-p))
21155 auto-fill-function)))
21156 (if indent
21157 (newline-and-indent)
21158 (newline)))))))
21160 (defun org-return-indent ()
21161 "Goto next table row or insert a newline and indent.
21162 Calls `org-table-next-row' or `newline-and-indent', depending on
21163 context. See the individual commands for more information."
21164 (interactive)
21165 (org-return t))
21167 (defun org-ctrl-c-star ()
21168 "Compute table, or change heading status of lines.
21169 Calls `org-table-recalculate' or `org-toggle-heading',
21170 depending on context."
21171 (interactive)
21172 (cond
21173 ((org-at-table-p)
21174 (call-interactively 'org-table-recalculate))
21176 ;; Convert all lines in region to list items
21177 (call-interactively 'org-toggle-heading))))
21179 (defun org-ctrl-c-minus ()
21180 "Insert separator line in table or modify bullet status of line.
21181 Also turns a plain line or a region of lines into list items.
21182 Calls `org-table-insert-hline', `org-toggle-item', or
21183 `org-cycle-list-bullet', depending on context."
21184 (interactive)
21185 (cond
21186 ((org-at-table-p)
21187 (call-interactively 'org-table-insert-hline))
21188 ((org-region-active-p)
21189 (call-interactively 'org-toggle-item))
21190 ((org-in-item-p)
21191 (call-interactively 'org-cycle-list-bullet))
21193 (call-interactively 'org-toggle-item))))
21195 (defun org-toggle-heading (&optional nstars)
21196 "Convert headings to normal text, or items or text to headings.
21197 If there is no active region, only convert the current line.
21199 With a `\\[universal-argument]' prefix, convert the whole list at
21200 point into heading.
21202 In a region:
21204 - If the first non blank line is a headline, remove the stars
21205 from all headlines in the region.
21207 - If it is a normal line, turn each and every normal line (i.e.,
21208 not an heading or an item) in the region into headings. If you
21209 want to convert only the first line of this region, use one
21210 universal prefix argument.
21212 - If it is a plain list item, turn all plain list items into headings.
21214 When converting a line into a heading, the number of stars is chosen
21215 such that the lines become children of the current entry. However,
21216 when a numeric prefix argument is given, its value determines the
21217 number of stars to add."
21218 (interactive "P")
21219 (let ((skip-blanks
21220 (function
21221 ;; Return beginning of first non-blank line, starting from
21222 ;; line at POS.
21223 (lambda (pos)
21224 (save-excursion
21225 (goto-char pos)
21226 (while (org-at-comment-p) (forward-line))
21227 (skip-chars-forward " \r\t\n")
21228 (point-at-bol)))))
21229 beg end toggled)
21230 ;; Determine boundaries of changes. If a universal prefix has
21231 ;; been given, put the list in a region. If region ends at a bol,
21232 ;; do not consider the last line to be in the region.
21234 (when (and current-prefix-arg (org-at-item-p))
21235 (when (listp current-prefix-arg) (setq current-prefix-arg 1))
21236 (org-mark-element))
21238 (if (org-region-active-p)
21239 (setq beg (funcall skip-blanks (region-beginning))
21240 end (copy-marker (save-excursion
21241 (goto-char (region-end))
21242 (if (bolp) (point) (point-at-eol)))))
21243 (setq beg (funcall skip-blanks (point-at-bol))
21244 end (copy-marker (point-at-eol))))
21245 ;; Ensure inline tasks don't count as headings.
21246 (org-with-limited-levels
21247 (save-excursion
21248 (goto-char beg)
21249 (cond
21250 ;; Case 1. Started at an heading: de-star headings.
21251 ((org-at-heading-p)
21252 (while (< (point) end)
21253 (when (org-at-heading-p t)
21254 (looking-at org-outline-regexp) (replace-match "")
21255 (setq toggled t))
21256 (forward-line)))
21257 ;; Case 2. Started at an item: change items into headlines.
21258 ;; One star will be added by `org-list-to-subtree'.
21259 ((org-at-item-p)
21260 (while (< (point) end)
21261 (when (org-at-item-p)
21262 ;; Pay attention to cases when region ends before list.
21263 (let* ((struct (org-list-struct))
21264 (list-end
21265 (min (org-list-get-bottom-point struct) (1+ end))))
21266 (save-restriction
21267 (narrow-to-region (point) list-end)
21268 (insert (org-list-to-subtree (org-list-to-lisp t)) "\n")))
21269 (setq toggled t))
21270 (forward-line)))
21271 ;; Case 3. Started at normal text: make every line an heading,
21272 ;; skipping headlines and items.
21273 (t (let* ((stars
21274 (make-string
21275 (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
21276 (add-stars
21277 (cond (nstars "") ; stars from prefix only
21278 ((equal stars "") "*") ; before first heading
21279 (org-odd-levels-only "**") ; inside heading, odd
21280 (t "*"))) ; inside heading, oddeven
21281 (rpl (concat stars add-stars " "))
21282 (lend (when (listp nstars) (save-excursion (end-of-line) (point)))))
21283 (while (< (point) (if (equal nstars '(4)) lend end))
21284 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
21285 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
21286 (replace-match (concat rpl (match-string 2))) (setq toggled t))
21287 (forward-line)))))))
21288 (unless toggled (message "Cannot toggle heading from here"))))
21290 (defun org-meta-return (&optional arg)
21291 "Insert a new heading or wrap a region in a table.
21292 Calls `org-insert-heading', `org-insert-item' or
21293 `org-table-wrap-region', depending on context. When called with
21294 an argument, unconditionally call `org-insert-heading'."
21295 (interactive "P")
21296 (org-check-before-invisible-edit 'insert)
21297 (or (run-hook-with-args-until-success 'org-metareturn-hook)
21298 (call-interactively (cond (arg #'org-insert-heading)
21299 ((org-at-table-p) #'org-table-wrap-region)
21300 ((org-in-item-p) #'org-insert-item)
21301 (t #'org-insert-heading)))))
21303 ;;; Menu entries
21305 (defsubst org-in-subtree-not-table-p ()
21306 "Are we in a subtree and not in a table?"
21307 (and (not (org-before-first-heading-p))
21308 (not (org-at-table-p))))
21310 ;; Define the Org mode menus
21311 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
21312 '("Tbl"
21313 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
21314 ["Next Field" org-cycle (org-at-table-p)]
21315 ["Previous Field" org-shifttab (org-at-table-p)]
21316 ["Next Row" org-return (org-at-table-p)]
21317 "--"
21318 ["Blank Field" org-table-blank-field (org-at-table-p)]
21319 ["Edit Field" org-table-edit-field (org-at-table-p)]
21320 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
21321 "--"
21322 ("Column"
21323 ["Move Column Left" org-metaleft (org-at-table-p)]
21324 ["Move Column Right" org-metaright (org-at-table-p)]
21325 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
21326 ["Insert Column" org-shiftmetaright (org-at-table-p)])
21327 ("Row"
21328 ["Move Row Up" org-metaup (org-at-table-p)]
21329 ["Move Row Down" org-metadown (org-at-table-p)]
21330 ["Delete Row" org-shiftmetaup (org-at-table-p)]
21331 ["Insert Row" org-shiftmetadown (org-at-table-p)]
21332 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
21333 "--"
21334 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
21335 ("Rectangle"
21336 ["Copy Rectangle" org-copy-special (org-at-table-p)]
21337 ["Cut Rectangle" org-cut-special (org-at-table-p)]
21338 ["Paste Rectangle" org-paste-special (org-at-table-p)]
21339 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
21340 "--"
21341 ("Calculate"
21342 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
21343 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
21344 ["Edit Formulas" org-edit-special (org-at-table-p)]
21345 "--"
21346 ["Recalculate line" org-table-recalculate (org-at-table-p)]
21347 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
21348 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
21349 "--"
21350 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
21351 "--"
21352 ["Sum Column/Rectangle" org-table-sum
21353 (or (org-at-table-p) (org-region-active-p))]
21354 ["Which Column?" org-table-current-column (org-at-table-p)])
21355 ["Debug Formulas"
21356 org-table-toggle-formula-debugger
21357 :style toggle :selected (bound-and-true-p org-table-formula-debug)]
21358 ["Show Col/Row Numbers"
21359 org-table-toggle-coordinate-overlays
21360 :style toggle
21361 :selected (bound-and-true-p org-table-overlay-coordinates)]
21362 "--"
21363 ["Create" org-table-create (not (org-at-table-p))]
21364 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
21365 ["Import from File" org-table-import (not (org-at-table-p))]
21366 ["Export to File" org-table-export (org-at-table-p)]
21367 "--"
21368 ["Create/Convert from/to table.el" org-table-create-with-table.el t]
21369 "--"
21370 ("Plot"
21371 ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
21372 ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
21374 (easy-menu-define org-org-menu org-mode-map "Org menu"
21375 '("Org"
21376 ("Show/Hide"
21377 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
21378 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
21379 ["Sparse Tree..." org-sparse-tree t]
21380 ["Reveal Context" org-reveal t]
21381 ["Show All" outline-show-all t]
21382 "--"
21383 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
21384 "--"
21385 ["New Heading" org-insert-heading t]
21386 ("Navigate Headings"
21387 ["Up" outline-up-heading t]
21388 ["Next" outline-next-visible-heading t]
21389 ["Previous" outline-previous-visible-heading t]
21390 ["Next Same Level" outline-forward-same-level t]
21391 ["Previous Same Level" outline-backward-same-level t]
21392 "--"
21393 ["Jump" org-goto t])
21394 ("Edit Structure"
21395 ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
21396 "--"
21397 ["Move Subtree Up" org-metaup (org-at-heading-p)]
21398 ["Move Subtree Down" org-metadown (org-at-heading-p)]
21399 "--"
21400 ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
21401 ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
21402 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
21403 "--"
21404 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
21405 "--"
21406 ["Copy visible text" org-copy-visible t]
21407 "--"
21408 ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
21409 ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
21410 ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
21411 ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
21412 "--"
21413 ["Sort Region/Children" org-sort t]
21414 "--"
21415 ["Convert to odd levels" org-convert-to-odd-levels t]
21416 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
21417 ("Editing"
21418 ["Emphasis..." org-emphasize t]
21419 ["Edit Source Example" org-edit-special t]
21420 "--"
21421 ["Footnote new/jump" org-footnote-action t]
21422 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
21423 ("Archive"
21424 ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
21425 "--"
21426 ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
21427 ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
21428 ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
21430 "--"
21431 ("Hyperlinks"
21432 ["Store Link (Global)" org-store-link t]
21433 ["Find existing link to here" org-occur-link-in-agenda-files t]
21434 ["Insert Link" org-insert-link t]
21435 ["Follow Link" org-open-at-point t]
21436 "--"
21437 ["Next link" org-next-link t]
21438 ["Previous link" org-previous-link t]
21439 "--"
21440 ["Descriptive Links"
21441 org-toggle-link-display
21442 :style radio
21443 :selected org-descriptive-links
21445 ["Literal Links"
21446 org-toggle-link-display
21447 :style radio
21448 :selected (not org-descriptive-links)])
21449 "--"
21450 ("TODO Lists"
21451 ["TODO/DONE/-" org-todo t]
21452 ("Select keyword"
21453 ["Next keyword" org-shiftright (org-at-heading-p)]
21454 ["Previous keyword" org-shiftleft (org-at-heading-p)]
21455 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
21456 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
21457 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
21458 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
21459 ["Global TODO list" org-todo-list :active t :keys "\\[org-agenda] t"]
21460 "--"
21461 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
21462 :selected org-enforce-todo-dependencies :style toggle :active t]
21463 "Settings for tree at point"
21464 ["Do Children sequentially" org-toggle-ordered-property :style radio
21465 :selected (org-entry-get nil "ORDERED")
21466 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
21467 ["Do Children parallel" org-toggle-ordered-property :style radio
21468 :selected (not (org-entry-get nil "ORDERED"))
21469 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
21470 "--"
21471 ["Set Priority" org-priority t]
21472 ["Priority Up" org-shiftup t]
21473 ["Priority Down" org-shiftdown t]
21474 "--"
21475 ["Get news from all feeds" org-feed-update-all t]
21476 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
21477 ["Customize feeds" (customize-variable 'org-feed-alist) t])
21478 ("TAGS and Properties"
21479 ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
21480 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
21481 "--"
21482 ["Set property" org-set-property (not (org-before-first-heading-p))]
21483 ["Column view of properties" org-columns t]
21484 ["Insert Column View DBlock" org-columns-insert-dblock t])
21485 ("Dates and Scheduling"
21486 ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
21487 ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
21488 ("Change Date"
21489 ["1 Day Later" org-shiftright (org-at-timestamp-p 'lax)]
21490 ["1 Day Earlier" org-shiftleft (org-at-timestamp-p 'lax)]
21491 ["1 ... Later" org-shiftup (org-at-timestamp-p 'lax)]
21492 ["1 ... Earlier" org-shiftdown (org-at-timestamp-p 'lax)])
21493 ["Compute Time Range" org-evaluate-time-range t]
21494 ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
21495 ["Deadline" org-deadline (not (org-before-first-heading-p))]
21496 "--"
21497 ["Custom time format" org-toggle-time-stamp-overlays
21498 :style radio :selected org-display-custom-times]
21499 "--"
21500 ["Goto Calendar" org-goto-calendar t]
21501 ["Date from Calendar" org-date-from-calendar t]
21502 "--"
21503 ["Start/Restart Timer" org-timer-start t]
21504 ["Pause/Continue Timer" org-timer-pause-or-continue t]
21505 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
21506 ["Insert Timer String" org-timer t]
21507 ["Insert Timer Item" org-timer-item t])
21508 ("Logging work"
21509 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
21510 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
21511 ["Clock out" org-clock-out t]
21512 ["Clock cancel" org-clock-cancel t]
21513 "--"
21514 ["Mark as default task" org-clock-mark-default-task t]
21515 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
21516 ["Goto running clock" org-clock-goto t]
21517 "--"
21518 ["Display times" org-clock-display t]
21519 ["Create clock table" org-clock-report t]
21520 "--"
21521 ["Record DONE time"
21522 (progn (setq org-log-done (not org-log-done))
21523 (message "Switching to %s will %s record a timestamp"
21524 (car org-done-keywords)
21525 (if org-log-done "automatically" "not")))
21526 :style toggle :selected org-log-done])
21527 "--"
21528 ["Agenda Command..." org-agenda t]
21529 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
21530 ("File List for Agenda")
21531 ("Special views current file"
21532 ["TODO Tree" org-show-todo-tree t]
21533 ["Check Deadlines" org-check-deadlines t]
21534 ["Tags/Property tree" org-match-sparse-tree t])
21535 "--"
21536 ["Export/Publish..." org-export-dispatch t]
21537 ("LaTeX"
21538 ["Org CDLaTeX mode" org-cdlatex-mode :active (require 'cdlatex nil t)
21539 :style toggle :selected org-cdlatex-mode]
21540 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
21541 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
21542 ["Modify math symbol" org-cdlatex-math-modify
21543 (org-inside-LaTeX-fragment-p)]
21544 ["Insert citation" org-reftex-citation t])
21545 "--"
21546 ("MobileOrg"
21547 ["Push Files and Views" org-mobile-push t]
21548 ["Get Captured and Flagged" org-mobile-pull t]
21549 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "\\[org-agenda] ?"]
21550 "--"
21551 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
21552 "--"
21553 ("Documentation"
21554 ["Show Version" org-version t]
21555 ["Info Documentation" org-info t])
21556 ("Customize"
21557 ["Browse Org Group" org-customize t]
21558 "--"
21559 ["Expand This Menu" org-create-customize-menu
21560 (fboundp 'customize-menu-create)])
21561 ["Send bug report" org-submit-bug-report t]
21562 "--"
21563 ("Refresh/Reload"
21564 ["Refresh setup current buffer" org-mode-restart t]
21565 ["Reload Org (after update)" org-reload t]
21566 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
21569 (defun org-info (&optional node)
21570 "Read documentation for Org in the info system.
21571 With optional NODE, go directly to that node."
21572 (interactive)
21573 (info (format "(org)%s" (or node ""))))
21575 ;;;###autoload
21576 (defun org-submit-bug-report ()
21577 "Submit a bug report on Org via mail.
21579 Don't hesitate to report any problems or inaccurate documentation.
21581 If you don't have setup sending mail from (X)Emacs, please copy the
21582 output buffer into your mail program, as it gives us important
21583 information about your Org version and configuration."
21584 (interactive)
21585 (require 'reporter)
21586 (defvar reporter-prompt-for-summary-p)
21587 (org-load-modules-maybe)
21588 (org-require-autoloaded-modules)
21589 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
21590 (reporter-submit-bug-report
21591 "emacs-orgmode@gnu.org"
21592 (org-version nil 'full)
21593 (let (list)
21594 (save-window-excursion
21595 (pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
21596 (delete-other-windows)
21597 (erase-buffer)
21598 (insert "You are about to submit a bug report to the Org mailing list.
21600 We would like to add your full Org and Outline configuration to the
21601 bug report. This greatly simplifies the work of the maintainer and
21602 other experts on the mailing list.
21604 HOWEVER, some variables you have customized may contain private
21605 information. The names of customers, colleagues, or friends, might
21606 appear in the form of file names, tags, todo states, or search strings.
21607 If you answer yes to the prompt, you might want to check and remove
21608 such private information before sending the email.")
21609 (add-text-properties (point-min) (point-max) '(face org-warning))
21610 (when (yes-or-no-p "Include your Org configuration ")
21611 (mapatoms
21612 (lambda (v)
21613 (and (boundp v)
21614 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
21615 (or (and (symbol-value v)
21616 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
21617 (and
21618 (get v 'custom-type) (get v 'standard-value)
21619 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
21620 (push v list)))))
21621 (kill-buffer (get-buffer "*Warn about privacy*"))
21622 list))
21623 nil nil
21624 "Remember to cover the basics, that is, what you expected to happen and
21625 what in fact did happen. You don't know how to make a good report? See
21627 https://orgmode.org/manual/Feedback.html#Feedback
21629 Your bug report will be posted to the Org mailing list.
21630 ------------------------------------------------------------------------")
21631 (save-excursion
21632 (when (re-search-backward "^\\(Subject: \\)Org mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
21633 (replace-match "\\1Bug: \\3 [\\2]")))))
21636 (defun org-install-agenda-files-menu ()
21637 (let ((bl (buffer-list)))
21638 (save-excursion
21639 (while bl
21640 (set-buffer (pop bl))
21641 (when (derived-mode-p 'org-mode) (setq bl nil)))
21642 (when (derived-mode-p 'org-mode)
21643 (easy-menu-change
21644 '("Org") "File List for Agenda"
21645 (append
21646 (list
21647 ["Edit File List" (org-edit-agenda-file-list) t]
21648 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
21649 ["Remove Current File from List" org-remove-file t]
21650 ["Cycle through agenda files" org-cycle-agenda-files t]
21651 ["Occur in all agenda files" org-occur-in-agenda-files t]
21652 "--")
21653 (mapcar 'org-file-menu-entry
21654 ;; Prevent initialization from failing.
21655 (ignore-errors (org-agenda-files t)))))))))
21657 ;;;; Documentation
21659 (defun org-require-autoloaded-modules ()
21660 (interactive)
21661 (mapc #'require
21662 '(org-agenda org-archive org-attach org-clock org-colview org-id
21663 org-table org-timer)))
21665 ;;;###autoload
21666 (defun org-reload (&optional uncompiled)
21667 "Reload all Org Lisp files.
21668 With prefix arg UNCOMPILED, load the uncompiled versions."
21669 (interactive "P")
21670 (require 'loadhist)
21671 (let* ((org-dir (org-find-library-dir "org"))
21672 (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
21673 (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
21674 (remove-re (format "\\`%s\\'"
21675 (regexp-opt '("org" "org-loaddefs" "org-version"))))
21676 (feats (delete-dups
21677 (mapcar 'file-name-sans-extension
21678 (mapcar 'file-name-nondirectory
21679 (delq nil
21680 (mapcar 'feature-file
21681 features))))))
21682 (lfeat (append
21683 (sort
21684 (setq feats
21685 (delq nil (mapcar
21686 (lambda (f)
21687 (if (and (string-match feature-re f)
21688 (not (string-match remove-re f)))
21689 f nil))
21690 feats)))
21691 'string-lessp)
21692 (list "org-version" "org")))
21693 (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
21694 (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
21695 load-uncore load-misses)
21696 (setq load-misses
21697 (delq 't
21698 (mapcar (lambda (f)
21699 (or (org-load-noerror-mustsuffix (concat org-dir f))
21700 (and (string= org-dir contrib-dir)
21701 (org-load-noerror-mustsuffix (concat contrib-dir f)))
21702 (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
21703 (add-to-list 'load-uncore f 'append)
21706 lfeat)))
21707 (when load-uncore
21708 (message "The following feature%s found in load-path, please check if that's correct:\n%s"
21709 (if (> (length load-uncore) 1) "s were" " was") load-uncore))
21710 (if load-misses
21711 (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
21712 (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
21713 (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
21715 ;;;###autoload
21716 (defun org-customize ()
21717 "Call the customize function with org as argument."
21718 (interactive)
21719 (org-load-modules-maybe)
21720 (org-require-autoloaded-modules)
21721 (customize-browse 'org))
21723 (defun org-create-customize-menu ()
21724 "Create a full customization menu for Org mode, insert it into the menu."
21725 (interactive)
21726 (org-load-modules-maybe)
21727 (org-require-autoloaded-modules)
21728 (if (fboundp 'customize-menu-create)
21729 (progn
21730 (easy-menu-change
21731 '("Org") "Customize"
21732 `(["Browse Org group" org-customize t]
21733 "--"
21734 ,(customize-menu-create 'org)
21735 ["Set" Custom-set t]
21736 ["Save" Custom-save t]
21737 ["Reset to Current" Custom-reset-current t]
21738 ["Reset to Saved" Custom-reset-saved t]
21739 ["Reset to Standard Settings" Custom-reset-standard t]))
21740 (message "\"Org\"-menu now contains full customization menu"))
21741 (error "Cannot expand menu (outdated version of cus-edit.el)")))
21743 ;;;; Miscellaneous stuff
21745 ;;; Generally useful functions
21747 (defun org-get-at-eol (property n)
21748 "Get text property PROPERTY at the end of line less N characters."
21749 (get-text-property (- (point-at-eol) n) property))
21751 (defun org-find-text-property-in-string (prop s)
21752 "Return the first non-nil value of property PROP in string S."
21753 (or (get-text-property 0 prop s)
21754 (get-text-property (or (next-single-property-change 0 prop s) 0)
21755 prop s)))
21757 (defun org-display-warning (message)
21758 "Display the given MESSAGE as a warning."
21759 (display-warning 'org message :warning))
21761 (defun org-eval (form)
21762 "Eval FORM and return result."
21763 (condition-case error
21764 (eval form)
21765 (error (format "%%![Error: %s]" error))))
21767 (defun org-in-clocktable-p ()
21768 "Check if the cursor is in a clocktable."
21769 (let ((pos (point)) start)
21770 (save-excursion
21771 (end-of-line 1)
21772 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
21773 (setq start (match-beginning 0))
21774 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
21775 (>= (match-end 0) pos)
21776 start))))
21778 (defun org-in-verbatim-emphasis ()
21779 (save-match-data
21780 (and (org-in-regexp org-verbatim-re 2)
21781 (>= (point) (match-beginning 3))
21782 (<= (point) (match-end 4)))))
21784 (defun org-overlay-display (ovl text &optional face evap)
21785 "Make overlay OVL display TEXT with face FACE."
21786 (overlay-put ovl 'display text)
21787 (if face (overlay-put ovl 'face face))
21788 (if evap (overlay-put ovl 'evaporate t)))
21790 (defun org-overlay-before-string (ovl text &optional face evap)
21791 "Make overlay OVL display TEXT with face FACE."
21792 (if face (org-add-props text nil 'face face))
21793 (overlay-put ovl 'before-string text)
21794 (if evap (overlay-put ovl 'evaporate t)))
21796 (defun org-find-overlays (prop &optional pos delete)
21797 "Find all overlays specifying PROP at POS or point.
21798 If DELETE is non-nil, delete all those overlays."
21799 (let (found)
21800 (dolist (ov (overlays-at (or pos (point))) found)
21801 (cond ((not (overlay-get ov prop)))
21802 (delete (delete-overlay ov))
21803 (t (push ov found))))))
21805 (defun org-goto-marker-or-bmk (marker &optional bookmark)
21806 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
21807 (if (and marker (marker-buffer marker)
21808 (buffer-live-p (marker-buffer marker)))
21809 (progn
21810 (pop-to-buffer-same-window (marker-buffer marker))
21811 (when (or (> marker (point-max)) (< marker (point-min)))
21812 (widen))
21813 (goto-char marker)
21814 (org-show-context 'org-goto))
21815 (if bookmark
21816 (bookmark-jump bookmark)
21817 (error "Cannot find location"))))
21819 (defun org-quote-csv-field (s)
21820 "Quote field for inclusion in CSV material."
21821 (if (string-match "[\",]" s)
21822 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
21825 (defun org-force-self-insert (N)
21826 "Needed to enforce self-insert under remapping."
21827 (interactive "p")
21828 (self-insert-command N))
21830 (defun org-shorten-string (s maxlength)
21831 "Shorten string S so that it is no longer than MAXLENGTH characters.
21832 If the string is shorter or has length MAXLENGTH, just return the
21833 original string. If it is longer, the functions finds a space in the
21834 string, breaks this string off at that locations and adds three dots
21835 as ellipsis. Including the ellipsis, the string will not be longer
21836 than MAXLENGTH. If finding a good breaking point in the string does
21837 not work, the string is just chopped off in the middle of a word
21838 if necessary."
21839 (if (<= (length s) maxlength)
21841 (let* ((n (max (- maxlength 4) 1))
21842 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
21843 (if (string-match re s)
21844 (concat (match-string 1 s) "...")
21845 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
21847 (defun org-get-indentation (&optional line)
21848 "Get the indentation of the current line, interpreting tabs.
21849 When LINE is given, assume it represents a line and compute its indentation."
21850 (if line
21851 (when (string-match "^ *" (org-remove-tabs line))
21852 (match-end 0))
21853 (save-excursion
21854 (beginning-of-line 1)
21855 (skip-chars-forward " \t")
21856 (current-column))))
21858 (defun org-get-string-indentation (s)
21859 "What indentation has S due to SPACE and TAB at the beginning of the string?"
21860 (let ((n -1) (i 0) (w tab-width) c)
21861 (catch 'exit
21862 (while (< (setq n (1+ n)) (length s))
21863 (setq c (aref s n))
21864 (cond ((= c ?\ ) (setq i (1+ i)))
21865 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
21866 (t (throw 'exit t)))))
21869 (defun org-remove-tabs (s &optional width)
21870 "Replace tabulators in S with spaces.
21871 Assumes that s is a single line, starting in column 0."
21872 (setq width (or width tab-width))
21873 (while (string-match "\t" s)
21874 (setq s (replace-match
21875 (make-string
21876 (- (* width (/ (+ (match-beginning 0) width) width))
21877 (match-beginning 0)) ?\ )
21878 t t s)))
21881 (defun org-fix-indentation (line ind)
21882 "Fix indentation in LINE.
21883 IND is a cons cell with target and minimum indentation.
21884 If the current indentation in LINE is smaller than the minimum,
21885 leave it alone. If it is larger than ind, set it to the target."
21886 (let* ((l (org-remove-tabs line))
21887 (i (org-get-indentation l))
21888 (i1 (car ind)) (i2 (cdr ind)))
21889 (when (>= i i2) (setq l (substring line i2)))
21890 (if (> i1 0)
21891 (concat (make-string i1 ?\ ) l)
21892 l)))
21894 (defun org-remove-indentation (code &optional n)
21895 "Remove maximum common indentation in string CODE and return it.
21896 N may optionally be the number of columns to remove. Return CODE
21897 as-is if removal failed."
21898 (with-temp-buffer
21899 (insert code)
21900 (if (org-do-remove-indentation n) (buffer-string) code)))
21902 (defun org-do-remove-indentation (&optional n)
21903 "Remove the maximum common indentation from the buffer.
21904 When optional argument N is a positive integer, remove exactly
21905 that much characters from indentation, if possible. Return nil
21906 if it fails."
21907 (catch :exit
21908 (goto-char (point-min))
21909 ;; Find maximum common indentation, if not specified.
21910 (let ((n (or n
21911 (let ((min-ind (point-max)))
21912 (save-excursion
21913 (while (re-search-forward "^[ \t]*\\S-" nil t)
21914 (let ((ind (1- (current-column))))
21915 (if (zerop ind) (throw :exit nil)
21916 (setq min-ind (min min-ind ind))))))
21917 min-ind))))
21918 (if (zerop n) (throw :exit nil)
21919 ;; Remove exactly N indentation, but give up if not possible.
21920 (while (not (eobp))
21921 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
21922 (cond ((eolp) (delete-region (line-beginning-position) (point)))
21923 ((< ind n) (throw :exit nil))
21924 (t (indent-line-to (- ind n))))
21925 (forward-line)))
21926 ;; Signal success.
21927 t))))
21929 (defun org-fill-template (template alist)
21930 "Find each %key of ALIST in TEMPLATE and replace it."
21931 (let ((case-fold-search nil))
21932 (dolist (entry (sort (copy-sequence alist)
21933 (lambda (a b) (< (length (car a)) (length (car b))))))
21934 (setq template
21935 (replace-regexp-in-string
21936 (concat "%" (regexp-quote (car entry)))
21937 (or (cdr entry) "") template t t)))
21938 template))
21940 (defun org-base-buffer (buffer)
21941 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
21942 (if (not buffer)
21943 buffer
21944 (or (buffer-base-buffer buffer)
21945 buffer)))
21947 (defun org-wrap (string &optional width lines)
21948 "Wrap string to either a number of lines, or a width in characters.
21949 If WIDTH is non-nil, the string is wrapped to that width, however many lines
21950 that costs. If there is a word longer than WIDTH, the text is actually
21951 wrapped to the length of that word.
21952 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
21953 many lines, whatever width that takes.
21954 The return value is a list of lines, without newlines at the end."
21955 (let* ((words (split-string string))
21956 (maxword (apply 'max (mapcar 'org-string-width words)))
21957 w ll)
21958 (cond (width
21959 (org-do-wrap words (max maxword width)))
21960 (lines
21961 (setq w maxword)
21962 (setq ll (org-do-wrap words maxword))
21963 (if (<= (length ll) lines)
21965 (setq ll words)
21966 (while (> (length ll) lines)
21967 (setq w (1+ w))
21968 (setq ll (org-do-wrap words w)))
21969 ll))
21970 (t (error "Cannot wrap this")))))
21972 (defun org-do-wrap (words width)
21973 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
21974 (let (lines line)
21975 (while words
21976 (setq line (pop words))
21977 (while (and words (< (+ (length line) (length (car words))) width))
21978 (setq line (concat line " " (pop words))))
21979 (setq lines (push line lines)))
21980 (nreverse lines)))
21982 (defun org-quote-vert (s)
21983 "Replace \"|\" with \"\\vert\"."
21984 (while (string-match "|" s)
21985 (setq s (replace-match "\\vert" t t s)))
21988 (defun org-uuidgen-p (s)
21989 "Is S an ID created by UUIDGEN?"
21990 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
21992 (defun org-in-src-block-p (&optional inside)
21993 "Whether point is in a code source block.
21994 When INSIDE is non-nil, don't consider we are within a src block
21995 when point is at #+BEGIN_SRC or #+END_SRC."
21996 (let ((case-fold-search t))
21997 (or (and (eq (get-char-property (point) 'src-block) t))
21998 (and (not inside)
21999 (save-match-data
22000 (save-excursion
22001 (beginning-of-line)
22002 (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
22004 (defun org-context ()
22005 "Return a list of contexts of the current cursor position.
22006 If several contexts apply, all are returned.
22007 Each context entry is a list with a symbol naming the context, and
22008 two positions indicating start and end of the context. Possible
22009 contexts are:
22011 :headline anywhere in a headline
22012 :headline-stars on the leading stars in a headline
22013 :todo-keyword on a TODO keyword (including DONE) in a headline
22014 :tags on the TAGS in a headline
22015 :priority on the priority cookie in a headline
22016 :item on the first line of a plain list item
22017 :item-bullet on the bullet/number of a plain list item
22018 :checkbox on the checkbox in a plain list item
22019 :table in an Org table
22020 :table-special on a special filed in a table
22021 :table-table in a table.el table
22022 :clocktable in a clocktable
22023 :src-block in a source block
22024 :link on a hyperlink
22025 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
22026 :target on a <<target>>
22027 :radio-target on a <<<radio-target>>>
22028 :latex-fragment on a LaTeX fragment
22029 :latex-preview on a LaTeX fragment with overlaid preview image
22031 This function expects the position to be visible because it uses font-lock
22032 faces as a help to recognize the following contexts: :table-special, :link,
22033 and :keyword."
22034 (let* ((f (get-text-property (point) 'face))
22035 (faces (if (listp f) f (list f)))
22036 (case-fold-search t)
22037 (p (point)) clist o)
22038 ;; First the large context
22039 (cond
22040 ((org-at-heading-p t)
22041 (push (list :headline (point-at-bol) (point-at-eol)) clist)
22042 (when (progn
22043 (beginning-of-line 1)
22044 (looking-at org-todo-line-tags-regexp))
22045 (push (org-point-in-group p 1 :headline-stars) clist)
22046 (push (org-point-in-group p 2 :todo-keyword) clist)
22047 (push (org-point-in-group p 4 :tags) clist))
22048 (goto-char p)
22049 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
22050 (when (looking-at "\\[#[A-Z0-9]\\]")
22051 (push (org-point-in-group p 0 :priority) clist)))
22053 ((org-at-item-p)
22054 (push (org-point-in-group p 2 :item-bullet) clist)
22055 (push (list :item (point-at-bol)
22056 (save-excursion (org-end-of-item) (point)))
22057 clist)
22058 (and (org-at-item-checkbox-p)
22059 (push (org-point-in-group p 0 :checkbox) clist)))
22061 ((org-at-table-p)
22062 (push (list :table (org-table-begin) (org-table-end)) clist)
22063 (when (memq 'org-formula faces)
22064 (push (list :table-special
22065 (previous-single-property-change p 'face)
22066 (next-single-property-change p 'face)) clist)))
22067 ((org-at-table-p 'any)
22068 (push (list :table-table) clist)))
22069 (goto-char p)
22071 (let ((case-fold-search t))
22072 ;; New the "medium" contexts: clocktables, source blocks
22073 (cond ((org-in-clocktable-p)
22074 (push (list :clocktable
22075 (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
22076 (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
22077 (match-beginning 1))
22078 (and (re-search-forward "[ \t]*#\\+END:?" nil t)
22079 (match-end 0))) clist))
22080 ((org-in-src-block-p)
22081 (push (list :src-block
22082 (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
22083 (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
22084 (match-beginning 1))
22085 (and (search-forward "#+END_SRC" nil t)
22086 (match-beginning 0))) clist))))
22087 (goto-char p)
22089 ;; Now the small context
22090 (cond
22091 ((org-at-timestamp-p)
22092 (push (org-point-in-group p 0 :timestamp) clist))
22093 ((memq 'org-link faces)
22094 (push (list :link
22095 (previous-single-property-change p 'face)
22096 (next-single-property-change p 'face)) clist))
22097 ((memq 'org-special-keyword faces)
22098 (push (list :keyword
22099 (previous-single-property-change p 'face)
22100 (next-single-property-change p 'face)) clist))
22101 ((org-at-target-p)
22102 (push (org-point-in-group p 0 :target) clist)
22103 (goto-char (1- (match-beginning 0)))
22104 (when (looking-at org-radio-target-regexp)
22105 (push (org-point-in-group p 0 :radio-target) clist))
22106 (goto-char p))
22107 ((setq o (cl-some
22108 (lambda (o)
22109 (and (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay)
22111 (overlays-at (point))))
22112 (push (list :latex-fragment
22113 (overlay-start o) (overlay-end o)) clist)
22114 (push (list :latex-preview
22115 (overlay-start o) (overlay-end o)) clist))
22116 ((org-inside-LaTeX-fragment-p)
22117 ;; FIXME: positions wrong.
22118 (push (list :latex-fragment (point) (point)) clist)))
22120 (setq clist (nreverse (delq nil clist)))
22121 clist))
22123 (defun org-in-regexp (regexp &optional nlines visually)
22124 "Check if point is inside a match of REGEXP.
22126 Normally only the current line is checked, but you can include
22127 NLINES extra lines around point into the search. If VISUALLY is
22128 set, require that the cursor is not after the match but really
22129 on, so that the block visually is on the match.
22131 Return nil or a cons cell (BEG . END) where BEG and END are,
22132 respectively, the positions at the beginning and the end of the
22133 match."
22134 (catch :exit
22135 (let ((pos (point))
22136 (eol (line-end-position (if nlines (1+ nlines) 1))))
22137 (save-excursion
22138 (beginning-of-line (- 1 (or nlines 0)))
22139 (while (and (re-search-forward regexp eol t)
22140 (<= (match-beginning 0) pos))
22141 (let ((end (match-end 0)))
22142 (when (or (> end pos) (and (= end pos) (not visually)))
22143 (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
22145 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
22146 "Non-nil when point is between matches of START-RE and END-RE.
22148 Also return a non-nil value when point is on one of the matches.
22150 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
22151 buffer positions. Default values are the positions of headlines
22152 surrounding the point.
22154 The functions returns a cons cell whose car (resp. cdr) is the
22155 position before START-RE (resp. after END-RE)."
22156 (save-match-data
22157 (let ((pos (point))
22158 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
22159 (limit-down (or lim-down (save-excursion (outline-next-heading))))
22160 beg end)
22161 (save-excursion
22162 ;; Point is on a block when on START-RE or if START-RE can be
22163 ;; found before it...
22164 (and (or (org-in-regexp start-re)
22165 (re-search-backward start-re limit-up t))
22166 (setq beg (match-beginning 0))
22167 ;; ... and END-RE after it...
22168 (goto-char (match-end 0))
22169 (re-search-forward end-re limit-down t)
22170 (> (setq end (match-end 0)) pos)
22171 ;; ... without another START-RE in-between.
22172 (goto-char (match-beginning 0))
22173 (not (re-search-backward start-re (1+ beg) t))
22174 ;; Return value.
22175 (cons beg end))))))
22177 (defun org-in-block-p (names)
22178 "Non-nil when point belongs to a block whose name belongs to NAMES.
22180 NAMES is a list of strings containing names of blocks.
22182 Return first block name matched, or nil. Beware that in case of
22183 nested blocks, the returned name may not belong to the closest
22184 block from point."
22185 (save-match-data
22186 (catch 'exit
22187 (let ((case-fold-search t)
22188 (lim-up (save-excursion (outline-previous-heading)))
22189 (lim-down (save-excursion (outline-next-heading))))
22190 (dolist (name names)
22191 (let ((n (regexp-quote name)))
22192 (when (org-between-regexps-p
22193 (concat "^[ \t]*#\\+begin_" n)
22194 (concat "^[ \t]*#\\+end_" n)
22195 lim-up lim-down)
22196 (throw 'exit n)))))
22197 nil)))
22199 (defun org-occur-in-agenda-files (regexp &optional _nlines)
22200 "Call `multi-occur' with buffers for all agenda files."
22201 (interactive "sOrg-files matching: ")
22202 (let* ((files (org-agenda-files))
22203 (tnames (mapcar #'file-truename files))
22204 (extra org-agenda-text-search-extra-files))
22205 (when (eq (car extra) 'agenda-archives)
22206 (setq extra (cdr extra))
22207 (setq files (org-add-archive-files files)))
22208 (dolist (f extra)
22209 (unless (member (file-truename f) tnames)
22210 (unless (member f files) (setq files (append files (list f))))
22211 (setq tnames (append tnames (list (file-truename f))))))
22212 (multi-occur
22213 (mapcar (lambda (x)
22214 (with-current-buffer
22215 ;; FIXME: Why not just (find-file-noselect x)?
22216 ;; Is it to avoid the "revert buffer" prompt?
22217 (or (get-file-buffer x) (find-file-noselect x))
22218 (widen)
22219 (current-buffer)))
22220 files)
22221 regexp)))
22223 (add-hook 'occur-mode-find-occurrence-hook
22224 (lambda () (when (derived-mode-p 'org-mode) (org-reveal))))
22226 (defun org-occur-link-in-agenda-files ()
22227 "Create a link and search for it in the agendas.
22228 The link is not stored in `org-stored-links', it is just created
22229 for the search purpose."
22230 (interactive)
22231 (let ((link (condition-case nil
22232 (org-store-link nil)
22233 (error "Unable to create a link to here"))))
22234 (org-occur-in-agenda-files (regexp-quote link))))
22236 (defun org-reverse-string (string)
22237 "Return the reverse of STRING."
22238 (apply 'string (reverse (string-to-list string))))
22240 ;; defsubst org-uniquify must be defined before first use
22242 (defun org-uniquify-alist (alist)
22243 "Merge elements of ALIST with the same key.
22245 For example, in this alist:
22247 \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
22248 => \\='((a 1 3) (b 2))
22250 merge (a 1) and (a 3) into (a 1 3).
22252 The function returns the new ALIST."
22253 (let (rtn)
22254 (dolist (e alist rtn)
22255 (let (n)
22256 (if (not (assoc (car e) rtn))
22257 (push e rtn)
22258 (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
22259 (setq rtn (assq-delete-all (car e) rtn))
22260 (push n rtn))))))
22262 (defun org-delete-all (elts list)
22263 "Remove all elements in ELTS from LIST.
22264 Comparison is done with `equal'. It is a destructive operation
22265 that may remove elements by altering the list structure."
22266 (while elts
22267 (setq list (delete (pop elts) list)))
22268 list)
22270 (defun org-back-over-empty-lines ()
22271 "Move backwards over whitespace, to the beginning of the first empty line.
22272 Returns the number of empty lines passed."
22273 (let ((pos (point)))
22274 (if (cdr (assq 'heading org-blank-before-new-entry))
22275 (skip-chars-backward " \t\n\r")
22276 (unless (eobp)
22277 (forward-line -1)))
22278 (beginning-of-line 2)
22279 (goto-char (min (point) pos))
22280 (count-lines (point) pos)))
22282 (defun org-skip-whitespace ()
22283 (skip-chars-forward " \t\n\r"))
22285 (defun org-point-in-group (point group &optional context)
22286 "Check if POINT is in match-group GROUP.
22287 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
22288 match. If the match group does not exist or point is not inside it,
22289 return nil."
22290 (and (match-beginning group)
22291 (>= point (match-beginning group))
22292 (<= point (match-end group))
22293 (if context
22294 (list context (match-beginning group) (match-end group))
22295 t)))
22297 (defun org-switch-to-buffer-other-window (&rest args)
22298 "Switch to buffer in a second window on the current frame.
22299 In particular, do not allow pop-up frames.
22300 Returns the newly created buffer."
22301 (org-no-popups
22302 (apply 'switch-to-buffer-other-window args)))
22304 (defun org-combine-plists (&rest plists)
22305 "Create a single property list from all plists in PLISTS.
22306 The process starts by copying the first list, and then setting properties
22307 from the other lists. Settings in the last list are the most significant
22308 ones and overrule settings in the other lists."
22309 (let ((rtn (copy-sequence (pop plists)))
22310 p v ls)
22311 (while plists
22312 (setq ls (pop plists))
22313 (while ls
22314 (setq p (pop ls) v (pop ls))
22315 (setq rtn (plist-put rtn p v))))
22316 rtn))
22318 (defun org-replace-escapes (string table)
22319 "Replace %-escapes in STRING with values in TABLE.
22320 TABLE is an association list with keys like \"%a\" and string values.
22321 The sequences in STRING may contain normal field width and padding information,
22322 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
22323 so values can contain further %-escapes if they are define later in TABLE."
22324 (let ((tbl (copy-alist table))
22325 (case-fold-search nil)
22326 (pchg 0)
22327 re rpl)
22328 (dolist (e tbl)
22329 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
22330 (when (and (cdr e) (string-match re (cdr e)))
22331 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
22332 (safe "SREF"))
22333 (add-text-properties 0 3 (list 'sref sref) safe)
22334 (setcdr e (replace-match safe t t (cdr e)))))
22335 (while (string-match re string)
22336 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
22337 (cdr e)))
22338 (setq string (replace-match rpl t t string))))
22339 (while (setq pchg (next-property-change pchg string))
22340 (let ((sref (get-text-property pchg 'sref string)))
22341 (when (and sref (string-match "SREF" string pchg))
22342 (setq string (replace-match sref t t string)))))
22343 string))
22345 (defun org-find-base-buffer-visiting (file)
22346 "Like `find-buffer-visiting' but always return the base buffer and
22347 not an indirect buffer."
22348 (let ((buf (or (get-file-buffer file)
22349 (find-buffer-visiting file))))
22350 (if buf
22351 (or (buffer-base-buffer buf) buf)
22352 nil)))
22354 ;;; TODO: Only called once, from ox-odt which should probably use
22355 ;;; org-export-inline-image-p or something.
22356 (defun org-file-image-p (file)
22357 "Return non-nil if FILE is an image."
22358 (save-match-data
22359 (string-match (image-file-name-regexp) file)))
22361 (defun org-get-cursor-date (&optional with-time)
22362 "Return the date at cursor in as a time.
22363 This works in the calendar and in the agenda, anywhere else it just
22364 returns the current time.
22365 If WITH-TIME is non-nil, returns the time of the event at point (in
22366 the agenda) or the current time of the day."
22367 (let (date day defd tp hod mod)
22368 (when with-time
22369 (setq tp (get-text-property (point) 'time))
22370 (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
22371 (setq hod (string-to-number (match-string 1 tp))
22372 mod (string-to-number (match-string 2 tp))))
22373 (or tp (let ((now (decode-time)))
22374 (setq hod (nth 2 now)
22375 mod (nth 1 now)))))
22376 (cond
22377 ((eq major-mode 'calendar-mode)
22378 (setq date (calendar-cursor-to-date)
22379 defd (encode-time 0 (or mod 0) (or hod 0)
22380 (nth 1 date) (nth 0 date) (nth 2 date))))
22381 ((eq major-mode 'org-agenda-mode)
22382 (setq day (get-text-property (point) 'day))
22383 (when day
22384 (setq date (calendar-gregorian-from-absolute day)
22385 defd (encode-time 0 (or mod 0) (or hod 0)
22386 (nth 1 date) (nth 0 date) (nth 2 date))))))
22387 (or defd (current-time))))
22389 (defun org-mark-subtree (&optional up)
22390 "Mark the current subtree.
22391 This puts point at the start of the current subtree, and mark at
22392 the end. If a numeric prefix UP is given, move up into the
22393 hierarchy of headlines by UP levels before marking the subtree."
22394 (interactive "P")
22395 (org-with-limited-levels
22396 (cond ((org-at-heading-p) (beginning-of-line))
22397 ((org-before-first-heading-p) (user-error "Not in a subtree"))
22398 (t (outline-previous-visible-heading 1))))
22399 (when up (while (and (> up 0) (org-up-heading-safe)) (cl-decf up)))
22400 (if (called-interactively-p 'any)
22401 (call-interactively 'org-mark-element)
22402 (org-mark-element)))
22404 (defun org-file-newer-than-p (file time)
22405 "Non-nil if FILE is newer than TIME.
22406 FILE is a filename, as a string, TIME is a list of integers, as
22407 returned by, e.g., `current-time'."
22408 (and (file-exists-p file)
22409 ;; Only compare times up to whole seconds as some file-systems
22410 ;; (e.g. HFS+) do not retain any finer granularity. As
22411 ;; a consequence, make sure we return non-nil when the two
22412 ;; times are equal.
22413 (not (time-less-p (cl-subseq (nth 5 (file-attributes file)) 0 2)
22414 (cl-subseq time 0 2)))))
22416 (defun org-compile-file (source process ext &optional err-msg log-buf spec)
22417 "Compile a SOURCE file using PROCESS.
22419 PROCESS is either a function or a list of shell commands, as
22420 strings. EXT is a file extension, without the leading dot, as
22421 a string. It is used to check if the process actually succeeded.
22423 PROCESS must create a file with the same base name and directory
22424 as SOURCE, but ending with EXT. The function then returns its
22425 filename. Otherwise, it raises an error. The error message can
22426 then be refined by providing string ERR-MSG, which is appended to
22427 the standard message.
22429 If PROCESS is a function, it is called with a single argument:
22430 the SOURCE file.
22432 If it is a list of commands, each of them is called using
22433 `shell-command'. By default, in each command, %b, %f, %F, %o and
22434 %O are replaced with, respectively, SOURCE base name, name, full
22435 name, directory and absolute output file name. It is possible,
22436 however, to use more place-holders by specifying them in optional
22437 argument SPEC, as an alist following the pattern
22439 (CHARACTER . REPLACEMENT-STRING).
22441 When PROCESS is a list of commands, optional argument LOG-BUF can
22442 be set to a buffer or a buffer name. `shell-command' then uses
22443 it for output."
22444 (let* ((base-name (file-name-base source))
22445 (full-name (file-truename source))
22446 (out-dir (or (file-name-directory source) "./"))
22447 (output (expand-file-name (concat base-name "." ext) out-dir))
22448 (time (current-time))
22449 (err-msg (if (stringp err-msg) (concat ". " err-msg) "")))
22450 (save-window-excursion
22451 (pcase process
22452 ((pred functionp) (funcall process (shell-quote-argument source)))
22453 ((pred consp)
22454 (let ((log-buf (and log-buf (get-buffer-create log-buf)))
22455 (spec (append spec
22456 `((?b . ,(shell-quote-argument base-name))
22457 (?f . ,(shell-quote-argument source))
22458 (?F . ,(shell-quote-argument full-name))
22459 (?o . ,(shell-quote-argument out-dir))
22460 (?O . ,(shell-quote-argument output))))))
22461 (dolist (command process)
22462 (shell-command (format-spec command spec) log-buf))
22463 (when log-buf (with-current-buffer log-buf (compilation-mode)))))
22464 (_ (error "No valid command to process %S%s" source err-msg))))
22465 ;; Check for process failure. Output file is expected to be
22466 ;; located in the same directory as SOURCE.
22467 (unless (org-file-newer-than-p output time)
22468 (error (format "File %S wasn't produced%s" output err-msg)))
22469 output))
22471 ;;; Indentation
22473 (defvar org-element-greater-elements)
22474 (defun org--get-expected-indentation (element contentsp)
22475 "Expected indentation column for current line, according to ELEMENT.
22476 ELEMENT is an element containing point. CONTENTSP is non-nil
22477 when indentation is to be computed according to contents of
22478 ELEMENT."
22479 (let ((type (org-element-type element))
22480 (start (org-element-property :begin element))
22481 (post-affiliated (org-element-property :post-affiliated element)))
22482 (org-with-wide-buffer
22483 (cond
22484 (contentsp
22485 (cl-case type
22486 ((diary-sexp footnote-definition) 0)
22487 ((headline inlinetask nil)
22488 (if (not org-adapt-indentation) 0
22489 (let ((level (org-current-level)))
22490 (if level (1+ level) 0))))
22491 ((item plain-list) (org-list-item-body-column post-affiliated))
22493 (goto-char start)
22494 (org-get-indentation))))
22495 ((memq type '(headline inlinetask nil))
22496 (if (org-match-line "[ \t]*$")
22497 (org--get-expected-indentation element t)
22499 ((memq type '(diary-sexp footnote-definition)) 0)
22500 ;; First paragraph of a footnote definition or an item.
22501 ;; Indent like parent.
22502 ((< (line-beginning-position) start)
22503 (org--get-expected-indentation
22504 (org-element-property :parent element) t))
22505 ;; At first line: indent according to previous sibling, if any,
22506 ;; ignoring footnote definitions and inline tasks, or parent's
22507 ;; contents.
22508 ((= (line-beginning-position) start)
22509 (catch 'exit
22510 (while t
22511 (if (= (point-min) start) (throw 'exit 0)
22512 (goto-char (1- start))
22513 (let* ((previous (org-element-at-point))
22514 (parent previous))
22515 (while (and parent (<= (org-element-property :end parent) start))
22516 (setq previous parent
22517 parent (org-element-property :parent parent)))
22518 (cond
22519 ((not previous) (throw 'exit 0))
22520 ((> (org-element-property :end previous) start)
22521 (throw 'exit (org--get-expected-indentation previous t)))
22522 ((memq (org-element-type previous)
22523 '(footnote-definition inlinetask))
22524 (setq start (org-element-property :begin previous)))
22525 (t (goto-char (org-element-property :begin previous))
22526 (throw 'exit
22527 (if (bolp) (org-get-indentation)
22528 ;; At first paragraph in an item or
22529 ;; a footnote definition.
22530 (org--get-expected-indentation
22531 (org-element-property :parent previous) t))))))))))
22532 ;; Otherwise, move to the first non-blank line above.
22534 (beginning-of-line)
22535 (let ((pos (point)))
22536 (skip-chars-backward " \r\t\n")
22537 (cond
22538 ;; Two blank lines end a footnote definition or a plain
22539 ;; list. When we indent an empty line after them, the
22540 ;; containing list or footnote definition is over, so it
22541 ;; qualifies as a previous sibling. Therefore, we indent
22542 ;; like its first line.
22543 ((and (memq type '(footnote-definition plain-list))
22544 (> (count-lines (point) pos) 2))
22545 (goto-char start)
22546 (org-get-indentation))
22547 ;; Line above is the first one of a paragraph at the
22548 ;; beginning of an item or a footnote definition. Indent
22549 ;; like parent.
22550 ((< (line-beginning-position) start)
22551 (org--get-expected-indentation
22552 (org-element-property :parent element) t))
22553 ;; Line above is the beginning of an element, i.e., point
22554 ;; was originally on the blank lines between element's start
22555 ;; and contents.
22556 ((= (line-beginning-position) post-affiliated)
22557 (org--get-expected-indentation element t))
22558 ;; POS is after contents in a greater element. Indent like
22559 ;; the beginning of the element.
22560 ((and (memq type org-element-greater-elements)
22561 (let ((cend (org-element-property :contents-end element)))
22562 (and cend (<= cend pos))))
22563 ;; As a special case, if point is at the end of a footnote
22564 ;; definition or an item, indent like the very last element
22565 ;; within. If that last element is an item, indent like
22566 ;; its contents.
22567 (if (memq type '(footnote-definition item plain-list))
22568 (let ((last (org-element-at-point)))
22569 (goto-char pos)
22570 (org--get-expected-indentation
22571 last (eq (org-element-type last) 'item)))
22572 (goto-char start)
22573 (org-get-indentation)))
22574 ;; In any other case, indent like the current line.
22575 (t (org-get-indentation)))))))))
22577 (defun org--align-node-property ()
22578 "Align node property at point.
22579 Alignment is done according to `org-property-format', which see."
22580 (when (save-excursion
22581 (beginning-of-line)
22582 (looking-at org-property-re))
22583 (replace-match
22584 (concat (match-string 4)
22585 (org-trim
22586 (format org-property-format (match-string 1) (match-string 3))))
22587 t t)))
22589 (defun org-indent-line ()
22590 "Indent line depending on context.
22592 Indentation is done according to the following rules:
22594 - Footnote definitions, diary sexps, headlines and inline tasks
22595 have to start at column 0.
22597 - On the very first line of an element, consider, in order, the
22598 next rules until one matches:
22600 1. If there's a sibling element before, ignoring footnote
22601 definitions and inline tasks, indent like its first line.
22603 2. If element has a parent, indent like its contents. More
22604 precisely, if parent is an item, indent after the
22605 description part, if any, or the bullet (see
22606 `org-list-description-max-indent'). Else, indent like
22607 parent's first line.
22609 3. Otherwise, indent relatively to current level, if
22610 `org-adapt-indentation' is non-nil, or to left margin.
22612 - On a blank line at the end of an element, indent according to
22613 the type of the element. More precisely
22615 1. If element is a plain list, an item, or a footnote
22616 definition, indent like the very last element within.
22618 2. If element is a paragraph, indent like its last non blank
22619 line.
22621 3. Otherwise, indent like its very first line.
22623 - In the code part of a source block, use language major mode
22624 to indent current line if `org-src-tab-acts-natively' is
22625 non-nil. If it is nil, do nothing.
22627 - Otherwise, indent like the first non-blank line above.
22629 The function doesn't indent an item as it could break the whole
22630 list structure. Instead, use \\<org-mode-map>`\\[org-shiftmetaleft]' or \
22631 `\\[org-shiftmetaright]'.
22633 Also align node properties according to `org-property-format'."
22634 (interactive)
22635 (cond
22636 (orgstruct-is-++
22637 (let ((indent-line-function
22638 (cl-cadadr (assq 'indent-line-function org-fb-vars))))
22639 (indent-according-to-mode)))
22640 ((org-at-heading-p) 'noindent)
22642 (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
22643 (type (org-element-type element)))
22644 (cond ((and (memq type '(plain-list item))
22645 (= (line-beginning-position)
22646 (org-element-property :post-affiliated element)))
22647 'noindent)
22648 ((and (eq type 'latex-environment)
22649 (>= (point) (org-element-property :post-affiliated element))
22650 (< (point) (org-with-wide-buffer
22651 (goto-char (org-element-property :end element))
22652 (skip-chars-backward " \r\t\n")
22653 (line-beginning-position 2))))
22654 'noindent)
22655 ((and (eq type 'src-block)
22656 org-src-tab-acts-natively
22657 (> (line-beginning-position)
22658 (org-element-property :post-affiliated element))
22659 (< (line-beginning-position)
22660 (org-with-wide-buffer
22661 (goto-char (org-element-property :end element))
22662 (skip-chars-backward " \r\t\n")
22663 (line-beginning-position))))
22664 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
22666 (let ((column (org--get-expected-indentation element nil)))
22667 ;; Preserve current column.
22668 (if (<= (current-column) (current-indentation))
22669 (indent-line-to column)
22670 (save-excursion (indent-line-to column))))
22671 ;; Align node property. Also preserve current column.
22672 (when (eq type 'node-property)
22673 (let ((column (current-column)))
22674 (org--align-node-property)
22675 (org-move-to-column column)))))))))
22677 (defun org-indent-region (start end)
22678 "Indent each non-blank line in the region.
22679 Called from a program, START and END specify the region to
22680 indent. The function will not indent contents of example blocks,
22681 verse blocks and export blocks as leading white spaces are
22682 assumed to be significant there."
22683 (interactive "r")
22684 (save-excursion
22685 (goto-char start)
22686 (skip-chars-forward " \r\t\n")
22687 (unless (eobp) (beginning-of-line))
22688 (let ((indent-to
22689 (lambda (ind pos)
22690 ;; Set IND as indentation for all lines between point and
22691 ;; POS. Blank lines are ignored. Leave point after POS
22692 ;; once done.
22693 (let ((limit (copy-marker pos)))
22694 (while (< (point) limit)
22695 (unless (looking-at-p "[ \t]*$") (indent-line-to ind))
22696 (forward-line))
22697 (set-marker limit nil))))
22698 (end (copy-marker end)))
22699 (while (< (point) end)
22700 (if (or (looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
22701 (let* ((element (org-element-at-point))
22702 (type (org-element-type element))
22703 (element-end (copy-marker (org-element-property :end element)))
22704 (ind (org--get-expected-indentation element nil)))
22705 (cond
22706 ;; Element indented as a single block. Example blocks
22707 ;; preserving indentation are a special case since the
22708 ;; "contents" must not be indented whereas the block
22709 ;; boundaries can.
22710 ((or (memq type '(export-block latex-environment))
22711 (and (eq type 'example-block)
22712 (not
22713 (or org-src-preserve-indentation
22714 (org-element-property :preserve-indent element)))))
22715 (let ((offset (- ind (org-get-indentation))))
22716 (unless (zerop offset)
22717 (indent-rigidly (org-element-property :begin element)
22718 (org-element-property :end element)
22719 offset)))
22720 (goto-char element-end))
22721 ;; Elements indented line wise. Be sure to exclude
22722 ;; example blocks (preserving indentation) and source
22723 ;; blocks from this category as they are treated
22724 ;; specially later.
22725 ((or (memq type '(paragraph table table-row))
22726 (not (or (org-element-property :contents-begin element)
22727 (memq type '(example-block src-block)))))
22728 (when (eq type 'node-property)
22729 (org--align-node-property)
22730 (beginning-of-line))
22731 (funcall indent-to ind (min element-end end)))
22732 ;; Elements consisting of three parts: before the
22733 ;; contents, the contents, and after the contents. The
22734 ;; contents are treated specially, according to the
22735 ;; element type, or not indented at all. Other parts are
22736 ;; indented as a single block.
22738 (let* ((post (copy-marker
22739 (org-element-property :post-affiliated element)))
22740 (cbeg
22741 (copy-marker
22742 (cond
22743 ((not (org-element-property :contents-begin element))
22744 ;; Fake contents for source blocks.
22745 (org-with-wide-buffer
22746 (goto-char post)
22747 (line-beginning-position 2)))
22748 ((memq type '(footnote-definition item plain-list))
22749 ;; Contents in these elements could start on
22750 ;; the same line as the beginning of the
22751 ;; element. Make sure we start indenting
22752 ;; from the second line.
22753 (org-with-wide-buffer
22754 (goto-char post)
22755 (end-of-line)
22756 (skip-chars-forward " \r\t\n")
22757 (if (eobp) (point) (line-beginning-position))))
22758 (t (org-element-property :contents-begin element)))))
22759 (cend (copy-marker
22760 (or (org-element-property :contents-end element)
22761 ;; Fake contents for source blocks.
22762 (org-with-wide-buffer
22763 (goto-char element-end)
22764 (skip-chars-backward " \r\t\n")
22765 (line-beginning-position)))
22766 t)))
22767 ;; Do not change items indentation individually as it
22768 ;; might break the list as a whole. On the other
22769 ;; hand, when at a plain list, indent it as a whole.
22770 (cond ((eq type 'plain-list)
22771 (let ((offset (- ind (org-get-indentation))))
22772 (unless (zerop offset)
22773 (indent-rigidly (org-element-property :begin element)
22774 (org-element-property :end element)
22775 offset))
22776 (goto-char cbeg)))
22777 ((eq type 'item) (goto-char cbeg))
22778 (t (funcall indent-to ind (min cbeg end))))
22779 (when (< (point) end)
22780 (cl-case type
22781 ((example-block verse-block))
22782 (src-block
22783 ;; In a source block, indent source code
22784 ;; according to language major mode, but only if
22785 ;; `org-src-tab-acts-natively' is non-nil.
22786 (when (and (< (point) end) org-src-tab-acts-natively)
22787 (ignore-errors
22788 (org-babel-do-in-edit-buffer
22789 (indent-region (point-min) (point-max))))))
22790 (t (org-indent-region (point) (min cend end))))
22791 (goto-char (min cend end))
22792 (when (< (point) end)
22793 (funcall indent-to ind (min element-end end))))
22794 (set-marker post nil)
22795 (set-marker cbeg nil)
22796 (set-marker cend nil))))
22797 (set-marker element-end nil))))
22798 (set-marker end nil))))
22800 (defun org-indent-drawer ()
22801 "Indent the drawer at point."
22802 (interactive)
22803 (unless (save-excursion
22804 (beginning-of-line)
22805 (looking-at-p org-drawer-regexp))
22806 (user-error "Not at a drawer"))
22807 (let ((element (org-element-at-point)))
22808 (unless (memq (org-element-type element) '(drawer property-drawer))
22809 (user-error "Not at a drawer"))
22810 (org-with-wide-buffer
22811 (org-indent-region (org-element-property :begin element)
22812 (org-element-property :end element))))
22813 (message "Drawer at point indented"))
22815 (defun org-indent-block ()
22816 "Indent the block at point."
22817 (interactive)
22818 (unless (save-excursion
22819 (beginning-of-line)
22820 (let ((case-fold-search t))
22821 (looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
22822 (user-error "Not at a block"))
22823 (let ((element (org-element-at-point)))
22824 (unless (memq (org-element-type element)
22825 '(comment-block center-block dynamic-block example-block
22826 export-block quote-block special-block
22827 src-block verse-block))
22828 (user-error "Not at a block"))
22829 (org-with-wide-buffer
22830 (org-indent-region (org-element-property :begin element)
22831 (org-element-property :end element))))
22832 (message "Block at point indented"))
22835 ;;; Filling
22837 ;; We use our own fill-paragraph and auto-fill functions.
22839 ;; `org-fill-paragraph' relies on adaptive filling and context
22840 ;; checking. Appropriate `fill-prefix' is computed with
22841 ;; `org-adaptive-fill-function'.
22843 ;; `org-auto-fill-function' takes care of auto-filling. It calls
22844 ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
22845 ;; `org-adaptive-fill-function' value. Internally,
22846 ;; `org-comment-line-break-function' breaks the line.
22848 ;; `org-setup-filling' installs filling and auto-filling related
22849 ;; variables during `org-mode' initialization.
22851 (defun org-setup-filling ()
22852 (require 'org-element)
22853 ;; Prevent auto-fill from inserting unwanted new items.
22854 (when (boundp 'fill-nobreak-predicate)
22855 (setq-local
22856 fill-nobreak-predicate
22857 (org-uniquify
22858 (append fill-nobreak-predicate
22859 '(org-fill-line-break-nobreak-p
22860 org-fill-n-macro-as-item-nobreak-p
22861 org-fill-paragraph-with-timestamp-nobreak-p)))))
22862 (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
22863 (setq-local paragraph-start paragraph-ending)
22864 (setq-local paragraph-separate paragraph-ending))
22865 (setq-local fill-paragraph-function 'org-fill-paragraph)
22866 (setq-local auto-fill-inhibit-regexp nil)
22867 (setq-local adaptive-fill-function 'org-adaptive-fill-function)
22868 (setq-local normal-auto-fill-function 'org-auto-fill-function)
22869 (setq-local comment-line-break-function 'org-comment-line-break-function))
22871 (defun org-fill-line-break-nobreak-p ()
22872 "Non-nil when a new line at point would create an Org line break."
22873 (save-excursion
22874 (skip-chars-backward "[ \t]")
22875 (skip-chars-backward "\\\\")
22876 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
22878 (defun org-fill-paragraph-with-timestamp-nobreak-p ()
22879 "Non-nil when a new line at point would split a timestamp."
22880 (and (org-at-timestamp-p 'lax)
22881 (not (looking-at org-ts-regexp-both))))
22883 (defun org-fill-n-macro-as-item-nobreak-p ()
22884 "Non-nil when a new line at point would create a new list."
22885 ;; During export, a "n" macro followed by a dot or a closing
22886 ;; parenthesis can end up being parsed as a new list item.
22887 (looking-at-p "[ \t]*{{{n\\(?:([^\n)]*)\\)?}}}[.)]\\(?:$\\| \\)"))
22889 (declare-function message-in-body-p "message" ())
22890 (defvar orgtbl-line-start-regexp) ; From org-table.el
22891 (defun org-adaptive-fill-function ()
22892 "Compute a fill prefix for the current line.
22893 Return fill prefix, as a string, or nil if current line isn't
22894 meant to be filled. For convenience, if `adaptive-fill-regexp'
22895 matches in paragraphs or comments, use it."
22896 (catch 'exit
22897 (when (derived-mode-p 'message-mode)
22898 (save-excursion
22899 (beginning-of-line)
22900 (cond ((not (message-in-body-p)) (throw 'exit nil))
22901 ((looking-at-p org-table-line-regexp) (throw 'exit nil))
22902 ((looking-at message-cite-prefix-regexp)
22903 (throw 'exit (match-string-no-properties 0)))
22904 ((looking-at org-outline-regexp)
22905 (throw 'exit (make-string (length (match-string 0)) ?\s))))))
22906 (org-with-wide-buffer
22907 (unless (org-at-heading-p)
22908 (let* ((p (line-beginning-position))
22909 (element (save-excursion
22910 (beginning-of-line)
22911 (org-element-at-point)))
22912 (type (org-element-type element))
22913 (post-affiliated (org-element-property :post-affiliated element)))
22914 (unless (< p post-affiliated)
22915 (cl-case type
22916 (comment
22917 (save-excursion
22918 (beginning-of-line)
22919 (looking-at "[ \t]*")
22920 (concat (match-string 0) "# ")))
22921 (footnote-definition "")
22922 ((item plain-list)
22923 (make-string (org-list-item-body-column post-affiliated) ?\s))
22924 (paragraph
22925 ;; Fill prefix is usually the same as the current line,
22926 ;; unless the paragraph is at the beginning of an item.
22927 (let ((parent (org-element-property :parent element)))
22928 (save-excursion
22929 (beginning-of-line)
22930 (cond ((eq (org-element-type parent) 'item)
22931 (make-string (org-list-item-body-column
22932 (org-element-property :begin parent))
22933 ?\s))
22934 ((and adaptive-fill-regexp
22935 ;; Locally disable
22936 ;; `adaptive-fill-function' to let
22937 ;; `fill-context-prefix' handle
22938 ;; `adaptive-fill-regexp' variable.
22939 (let (adaptive-fill-function)
22940 (fill-context-prefix
22941 post-affiliated
22942 (org-element-property :end element)))))
22943 ((looking-at "[ \t]+") (match-string 0))
22944 (t "")))))
22945 (comment-block
22946 ;; Only fill contents if P is within block boundaries.
22947 (let* ((cbeg (save-excursion (goto-char post-affiliated)
22948 (forward-line)
22949 (point)))
22950 (cend (save-excursion
22951 (goto-char (org-element-property :end element))
22952 (skip-chars-backward " \r\t\n")
22953 (line-beginning-position))))
22954 (when (and (>= p cbeg) (< p cend))
22955 (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
22956 (match-string 0)
22957 "")))))))))))
22959 (declare-function message-goto-body "message" ())
22960 (defvar message-cite-prefix-regexp) ; From message.el
22962 (defun org-fill-element (&optional justify)
22963 "Fill element at point, when applicable.
22965 This function only applies to comment blocks, comments, example
22966 blocks and paragraphs. Also, as a special case, re-align table
22967 when point is at one.
22969 If JUSTIFY is non-nil (interactively, with prefix argument),
22970 justify as well. If `sentence-end-double-space' is non-nil, then
22971 period followed by one space does not end a sentence, so don't
22972 break a line there. The variable `fill-column' controls the
22973 width for filling.
22975 For convenience, when point is at a plain list, an item or
22976 a footnote definition, try to fill the first paragraph within."
22977 (with-syntax-table org-mode-transpose-word-syntax-table
22978 ;; Move to end of line in order to get the first paragraph within
22979 ;; a plain list or a footnote definition.
22980 (let ((element (save-excursion (end-of-line) (org-element-at-point))))
22981 ;; First check if point is in a blank line at the beginning of
22982 ;; the buffer. In that case, ignore filling.
22983 (cl-case (org-element-type element)
22984 ;; Use major mode filling function is src blocks.
22985 (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
22986 ;; Align Org tables, leave table.el tables as-is.
22987 (table-row (org-table-align) t)
22988 (table
22989 (when (eq (org-element-property :type element) 'org)
22990 (save-excursion
22991 (goto-char (org-element-property :post-affiliated element))
22992 (org-table-align)))
22994 (paragraph
22995 ;; Paragraphs may contain `line-break' type objects.
22996 (let ((beg (max (point-min)
22997 (org-element-property :contents-begin element)))
22998 (end (min (point-max)
22999 (org-element-property :contents-end element))))
23000 ;; Do nothing if point is at an affiliated keyword.
23001 (if (< (line-end-position) beg) t
23002 (when (derived-mode-p 'message-mode)
23003 ;; In `message-mode', do not fill following citation
23004 ;; in current paragraph nor text before message body.
23005 (let ((body-start (save-excursion (message-goto-body))))
23006 (when body-start (setq beg (max body-start beg))))
23007 (when (save-excursion
23008 (re-search-forward
23009 (concat "^" message-cite-prefix-regexp) end t))
23010 (setq end (match-beginning 0))))
23011 ;; Fill paragraph, taking line breaks into account.
23012 (save-excursion
23013 (goto-char beg)
23014 (let ((cuts (list beg)))
23015 (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
23016 (when (eq 'line-break
23017 (org-element-type
23018 (save-excursion (backward-char)
23019 (org-element-context))))
23020 (push (point) cuts)))
23021 (dolist (c (delq end cuts))
23022 (fill-region-as-paragraph c end justify)
23023 (setq end c))))
23024 t)))
23025 ;; Contents of `comment-block' type elements should be
23026 ;; filled as plain text, but only if point is within block
23027 ;; markers.
23028 (comment-block
23029 (let* ((case-fold-search t)
23030 (beg (save-excursion
23031 (goto-char (org-element-property :begin element))
23032 (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
23033 (forward-line)
23034 (point)))
23035 (end (save-excursion
23036 (goto-char (org-element-property :end element))
23037 (re-search-backward "^[ \t]*#\\+end_comment" nil t)
23038 (line-beginning-position))))
23039 (if (or (< (point) beg) (> (point) end)) t
23040 (fill-region-as-paragraph
23041 (save-excursion (end-of-line)
23042 (re-search-backward "^[ \t]*$" beg 'move)
23043 (line-beginning-position))
23044 (save-excursion (beginning-of-line)
23045 (re-search-forward "^[ \t]*$" end 'move)
23046 (line-beginning-position))
23047 justify))))
23048 ;; Fill comments.
23049 (comment
23050 (let ((begin (org-element-property :post-affiliated element))
23051 (end (org-element-property :end element)))
23052 (when (and (>= (point) begin) (<= (point) end))
23053 (let ((begin (save-excursion
23054 (end-of-line)
23055 (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
23056 (progn (forward-line) (point))
23057 begin)))
23058 (end (save-excursion
23059 (end-of-line)
23060 (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
23061 (1- (line-beginning-position))
23062 (skip-chars-backward " \r\t\n")
23063 (line-end-position)))))
23064 ;; Do not fill comments when at a blank line.
23065 (when (> end begin)
23066 (let ((fill-prefix
23067 (save-excursion
23068 (beginning-of-line)
23069 (looking-at "[ \t]*#")
23070 (let ((comment-prefix (match-string 0)))
23071 (goto-char (match-end 0))
23072 (if (looking-at adaptive-fill-regexp)
23073 (concat comment-prefix (match-string 0))
23074 (concat comment-prefix " "))))))
23075 (save-excursion
23076 (fill-region-as-paragraph begin end justify))))))
23078 ;; Ignore every other element.
23079 (otherwise t)))))
23081 (defun org-fill-paragraph (&optional justify region)
23082 "Fill element at point, when applicable.
23084 This function only applies to comment blocks, comments, example
23085 blocks and paragraphs. Also, as a special case, re-align table
23086 when point is at one.
23088 For convenience, when point is at a plain list, an item or
23089 a footnote definition, try to fill the first paragraph within.
23091 If JUSTIFY is non-nil (interactively, with prefix argument),
23092 justify as well. If `sentence-end-double-space' is non-nil, then
23093 period followed by one space does not end a sentence, so don't
23094 break a line there. The variable `fill-column' controls the
23095 width for filling.
23097 The REGION argument is non-nil if called interactively; in that
23098 case, if Transient Mark mode is enabled and the mark is active,
23099 fill each of the elements in the active region, instead of just
23100 filling the current element."
23101 (interactive (progn
23102 (barf-if-buffer-read-only)
23103 (list (if current-prefix-arg 'full) t)))
23104 (cond
23105 ((and (derived-mode-p 'message-mode)
23106 (or (not (message-in-body-p))
23107 (save-excursion (move-beginning-of-line 1)
23108 (looking-at message-cite-prefix-regexp))))
23109 ;; First ensure filling is correct in message-mode.
23110 (let ((fill-paragraph-function
23111 (cl-cadadr (assq 'fill-paragraph-function org-fb-vars)))
23112 (fill-prefix (cl-cadadr (assq 'fill-prefix org-fb-vars)))
23113 (paragraph-start (cl-cadadr (assq 'paragraph-start org-fb-vars)))
23114 (paragraph-separate
23115 (cl-cadadr (assq 'paragraph-separate org-fb-vars))))
23116 (fill-paragraph nil)))
23117 ((and region transient-mark-mode mark-active
23118 (not (eq (region-beginning) (region-end))))
23119 (let ((origin (point-marker))
23120 (start (region-beginning)))
23121 (unwind-protect
23122 (progn
23123 (goto-char (region-end))
23124 (while (> (point) start)
23125 (org-backward-paragraph)
23126 (org-fill-element justify)))
23127 (goto-char origin)
23128 (set-marker origin nil))))
23129 (t (org-fill-element justify))))
23130 (org-remap org-mode-map 'fill-paragraph 'org-fill-paragraph)
23132 (defun org-auto-fill-function ()
23133 "Auto-fill function."
23134 ;; Check if auto-filling is meaningful.
23135 (let ((fc (current-fill-column)))
23136 (when (and fc (> (current-column) fc))
23137 (let* ((fill-prefix (org-adaptive-fill-function))
23138 ;; Enforce empty fill prefix, if required. Otherwise, it
23139 ;; will be computed again.
23140 (adaptive-fill-mode (not (equal fill-prefix ""))))
23141 (when fill-prefix (do-auto-fill))))))
23143 (defun org-comment-line-break-function (&optional soft)
23144 "Break line at point and indent, continuing comment if within one.
23145 The inserted newline is marked hard if variable
23146 `use-hard-newlines' is true, unless optional argument SOFT is
23147 non-nil."
23148 (if soft (insert-and-inherit ?\n) (newline 1))
23149 (save-excursion (forward-char -1) (delete-horizontal-space))
23150 (delete-horizontal-space)
23151 (indent-to-left-margin)
23152 (insert-before-markers-and-inherit fill-prefix))
23155 ;;; Fixed Width Areas
23157 (defun org-toggle-fixed-width ()
23158 "Toggle fixed-width markup.
23160 Add or remove fixed-width markup on current line, whenever it
23161 makes sense. Return an error otherwise.
23163 If a region is active and if it contains only fixed-width areas
23164 or blank lines, remove all fixed-width markup in it. If the
23165 region contains anything else, convert all non-fixed-width lines
23166 to fixed-width ones.
23168 Blank lines at the end of the region are ignored unless the
23169 region only contains such lines."
23170 (interactive)
23171 (if (not (org-region-active-p))
23172 ;; No region:
23174 ;; Remove fixed width marker only in a fixed-with element.
23176 ;; Add fixed width maker in paragraphs, in blank lines after
23177 ;; elements or at the beginning of a headline or an inlinetask,
23178 ;; and before any one-line elements (e.g., a clock).
23179 (progn
23180 (beginning-of-line)
23181 (let* ((element (org-element-at-point))
23182 (type (org-element-type element)))
23183 (cond
23184 ((and (eq type 'fixed-width)
23185 (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
23186 (replace-match
23187 "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
23188 ((and (memq type '(babel-call clock comment diary-sexp headline
23189 horizontal-rule keyword paragraph
23190 planning))
23191 (<= (org-element-property :post-affiliated element) (point)))
23192 (skip-chars-forward " \t")
23193 (insert ": "))
23194 ((and (looking-at-p "[ \t]*$")
23195 (or (eq type 'inlinetask)
23196 (save-excursion
23197 (skip-chars-forward " \r\t\n")
23198 (<= (org-element-property :end element) (point)))))
23199 (delete-region (point) (line-end-position))
23200 (org-indent-line)
23201 (insert ": "))
23202 (t (user-error "Cannot insert a fixed-width line here")))))
23203 ;; Region active.
23204 (let* ((begin (save-excursion
23205 (goto-char (region-beginning))
23206 (line-beginning-position)))
23207 (end (copy-marker
23208 (save-excursion
23209 (goto-char (region-end))
23210 (unless (eolp) (beginning-of-line))
23211 (if (save-excursion (re-search-backward "\\S-" begin t))
23212 (progn (skip-chars-backward " \r\t\n") (point))
23213 (point)))))
23214 (all-fixed-width-p
23215 (catch 'not-all-p
23216 (save-excursion
23217 (goto-char begin)
23218 (skip-chars-forward " \r\t\n")
23219 (when (eobp) (throw 'not-all-p nil))
23220 (while (< (point) end)
23221 (let ((element (org-element-at-point)))
23222 (if (eq (org-element-type element) 'fixed-width)
23223 (goto-char (org-element-property :end element))
23224 (throw 'not-all-p nil))))
23225 t))))
23226 (if all-fixed-width-p
23227 (save-excursion
23228 (goto-char begin)
23229 (while (< (point) end)
23230 (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
23231 (replace-match
23232 "" nil nil nil
23233 (if (= (line-end-position) (match-end 0)) 0 1)))
23234 (forward-line)))
23235 (let ((min-ind (point-max)))
23236 ;; Find minimum indentation across all lines.
23237 (save-excursion
23238 (goto-char begin)
23239 (if (not (save-excursion (re-search-forward "\\S-" end t)))
23240 (setq min-ind 0)
23241 (catch 'zerop
23242 (while (< (point) end)
23243 (unless (looking-at-p "[ \t]*$")
23244 (let ((ind (org-get-indentation)))
23245 (setq min-ind (min min-ind ind))
23246 (when (zerop ind) (throw 'zerop t))))
23247 (forward-line)))))
23248 ;; Loop over all lines and add fixed-width markup everywhere
23249 ;; but in fixed-width lines.
23250 (save-excursion
23251 (goto-char begin)
23252 (while (< (point) end)
23253 (cond
23254 ((org-at-heading-p)
23255 (insert ": ")
23256 (forward-line)
23257 (while (and (< (point) end) (looking-at-p "[ \t]*$"))
23258 (insert ":")
23259 (forward-line)))
23260 ((looking-at-p "[ \t]*:\\( \\|$\\)")
23261 (let* ((element (org-element-at-point))
23262 (element-end (org-element-property :end element)))
23263 (if (eq (org-element-type element) 'fixed-width)
23264 (progn (goto-char element-end)
23265 (skip-chars-backward " \r\t\n")
23266 (forward-line))
23267 (let ((limit (min end element-end)))
23268 (while (< (point) limit)
23269 (org-move-to-column min-ind t)
23270 (insert ": ")
23271 (forward-line))))))
23273 (org-move-to-column min-ind t)
23274 (insert ": ")
23275 (forward-line)))))))
23276 (set-marker end nil))))
23279 ;;; Comments
23281 ;; Org comments syntax is quite complex. It requires the entire line
23282 ;; to be just a comment. Also, even with the right syntax at the
23283 ;; beginning of line, some elements (e.g., verse-block or
23284 ;; example-block) don't accept comments. Usual Emacs comment commands
23285 ;; cannot cope with those requirements. Therefore, Org replaces them.
23287 ;; Org still relies on `comment-dwim', but cannot trust
23288 ;; `comment-only-p'. So, `comment-region-function' and
23289 ;; `uncomment-region-function' both point
23290 ;; to`org-comment-or-uncomment-region'. Eventually,
23291 ;; `org-insert-comment' takes care of insertion of comments at the
23292 ;; beginning of line.
23294 ;; `org-setup-comments-handling' install comments related variables
23295 ;; during `org-mode' initialization.
23297 (defun org-setup-comments-handling ()
23298 (interactive)
23299 (setq-local comment-use-syntax nil)
23300 (setq-local comment-start "# ")
23301 (setq-local comment-start-skip "^\\s-*#\\(?: \\|$\\)")
23302 (setq-local comment-insert-comment-function 'org-insert-comment)
23303 (setq-local comment-region-function 'org-comment-or-uncomment-region)
23304 (setq-local uncomment-region-function 'org-comment-or-uncomment-region))
23306 (defun org-insert-comment ()
23307 "Insert an empty comment above current line.
23308 If the line is empty, insert comment at its beginning. When
23309 point is within a source block, comment according to the related
23310 major mode."
23311 (if (let ((element (org-element-at-point)))
23312 (and (eq (org-element-type element) 'src-block)
23313 (< (save-excursion
23314 (goto-char (org-element-property :post-affiliated element))
23315 (line-end-position))
23316 (point))
23317 (> (save-excursion
23318 (goto-char (org-element-property :end element))
23319 (skip-chars-backward " \r\t\n")
23320 (line-beginning-position))
23321 (point))))
23322 (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
23323 (beginning-of-line)
23324 (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
23325 (open-line 1))
23326 (org-indent-line)
23327 (insert "# ")))
23329 (defvar comment-empty-lines) ; From newcomment.el.
23330 (defun org-comment-or-uncomment-region (beg end &rest _)
23331 "Comment or uncomment each non-blank line in the region.
23332 Uncomment each non-blank line between BEG and END if it only
23333 contains commented lines. Otherwise, comment them. If region is
23334 strictly within a source block, use appropriate comment syntax."
23335 (if (let ((element (org-element-at-point)))
23336 (and (eq (org-element-type element) 'src-block)
23337 (< (save-excursion
23338 (goto-char (org-element-property :post-affiliated element))
23339 (line-end-position))
23340 beg)
23341 (>= (save-excursion
23342 (goto-char (org-element-property :end element))
23343 (skip-chars-backward " \r\t\n")
23344 (line-beginning-position))
23345 end)))
23346 ;; Translate region boundaries for the Org buffer to the source
23347 ;; buffer.
23348 (let ((offset (- end beg)))
23349 (save-excursion
23350 (goto-char beg)
23351 (org-babel-do-in-edit-buffer
23352 (comment-or-uncomment-region (point) (+ offset (point))))))
23353 (save-restriction
23354 ;; Restrict region
23355 (narrow-to-region (save-excursion (goto-char beg)
23356 (skip-chars-forward " \r\t\n" end)
23357 (line-beginning-position))
23358 (save-excursion (goto-char end)
23359 (skip-chars-backward " \r\t\n" beg)
23360 (line-end-position)))
23361 (let ((uncommentp
23362 ;; UNCOMMENTP is non-nil when every non blank line between
23363 ;; BEG and END is a comment.
23364 (save-excursion
23365 (goto-char (point-min))
23366 (while (and (not (eobp))
23367 (let ((element (org-element-at-point)))
23368 (and (eq (org-element-type element) 'comment)
23369 (goto-char (min (point-max)
23370 (org-element-property
23371 :end element)))))))
23372 (eobp))))
23373 (if uncommentp
23374 ;; Only blank lines and comments in region: uncomment it.
23375 (save-excursion
23376 (goto-char (point-min))
23377 (while (not (eobp))
23378 (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
23379 (replace-match "" nil nil nil 1))
23380 (forward-line)))
23381 ;; Comment each line in region.
23382 (let ((min-indent (point-max)))
23383 ;; First find the minimum indentation across all lines.
23384 (save-excursion
23385 (goto-char (point-min))
23386 (while (and (not (eobp)) (not (zerop min-indent)))
23387 (unless (looking-at "[ \t]*$")
23388 (setq min-indent (min min-indent (current-indentation))))
23389 (forward-line)))
23390 ;; Then loop over all lines.
23391 (save-excursion
23392 (goto-char (point-min))
23393 (while (not (eobp))
23394 (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
23395 ;; Don't get fooled by invisible text (e.g. link path)
23396 ;; when moving to column MIN-INDENT.
23397 (let ((buffer-invisibility-spec nil))
23398 (org-move-to-column min-indent t))
23399 (insert comment-start))
23400 (forward-line)))))))))
23402 (defun org-comment-dwim (_arg)
23403 "Call `comment-dwim' within a source edit buffer if needed."
23404 (interactive "*P")
23405 (if (org-in-src-block-p)
23406 (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
23407 (call-interactively 'comment-dwim)))
23410 ;;; Timestamps API
23412 ;; This section contains tools to operate on timestamp objects, as
23413 ;; returned by, e.g. `org-element-context'.
23415 (defun org-timestamp--to-internal-time (timestamp &optional end)
23416 "Encode TIMESTAMP object into Emacs internal time.
23417 Use end of date range or time range when END is non-nil."
23418 (apply #'encode-time
23419 (cons 0
23420 (mapcar
23421 (lambda (prop) (or (org-element-property prop timestamp) 0))
23422 (if end '(:minute-end :hour-end :day-end :month-end :year-end)
23423 '(:minute-start :hour-start :day-start :month-start
23424 :year-start))))))
23426 (defun org-timestamp-has-time-p (timestamp)
23427 "Non-nil when TIMESTAMP has a time specified."
23428 (org-element-property :hour-start timestamp))
23430 (defun org-timestamp-format (timestamp format &optional end utc)
23431 "Format a TIMESTAMP object into a string.
23433 FORMAT is a format specifier to be passed to
23434 `format-time-string'.
23436 When optional argument END is non-nil, use end of date-range or
23437 time-range, if possible.
23439 When optional argument UTC is non-nil, time will be expressed as
23440 Universal Time."
23441 (format-time-string
23442 format (org-timestamp--to-internal-time timestamp end)
23443 (and utc t)))
23445 (defun org-timestamp-split-range (timestamp &optional end)
23446 "Extract a TIMESTAMP object from a date or time range.
23448 END, when non-nil, means extract the end of the range.
23449 Otherwise, extract its start.
23451 Return a new timestamp object."
23452 (let ((type (org-element-property :type timestamp)))
23453 (if (memq type '(active inactive diary)) timestamp
23454 (let ((split-ts (org-element-copy timestamp)))
23455 ;; Set new type.
23456 (org-element-put-property
23457 split-ts :type (if (eq type 'active-range) 'active 'inactive))
23458 ;; Copy start properties over end properties if END is
23459 ;; non-nil. Otherwise, copy end properties over `start' ones.
23460 (let ((p-alist '((:minute-start . :minute-end)
23461 (:hour-start . :hour-end)
23462 (:day-start . :day-end)
23463 (:month-start . :month-end)
23464 (:year-start . :year-end))))
23465 (dolist (p-cell p-alist)
23466 (org-element-put-property
23467 split-ts
23468 (funcall (if end #'car #'cdr) p-cell)
23469 (org-element-property
23470 (funcall (if end #'cdr #'car) p-cell) split-ts)))
23471 ;; Eventually refresh `:raw-value'.
23472 (org-element-put-property split-ts :raw-value nil)
23473 (org-element-put-property
23474 split-ts :raw-value (org-element-interpret-data split-ts)))))))
23476 (defun org-timestamp-translate (timestamp &optional boundary)
23477 "Translate TIMESTAMP object to custom format.
23479 Format string is defined in `org-time-stamp-custom-formats',
23480 which see.
23482 When optional argument BOUNDARY is non-nil, it is either the
23483 symbol `start' or `end'. In this case, only translate the
23484 starting or ending part of TIMESTAMP if it is a date or time
23485 range. Otherwise, translate both parts.
23487 Return timestamp as-is if `org-display-custom-times' is nil or if
23488 it has a `diary' type."
23489 (let ((type (org-element-property :type timestamp)))
23490 (if (or (not org-display-custom-times) (eq type 'diary))
23491 (org-element-interpret-data timestamp)
23492 (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
23493 org-time-stamp-custom-formats)))
23494 (if (and (not boundary) (memq type '(active-range inactive-range)))
23495 (concat (org-timestamp-format timestamp fmt)
23496 "--"
23497 (org-timestamp-format timestamp fmt t))
23498 (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
23502 ;;; Other stuff.
23504 (defvar reftex-docstruct-symbol)
23505 (defvar org--rds)
23507 (defun org-reftex-citation ()
23508 "Use reftex-citation to insert a citation into the buffer.
23509 This looks for a line like
23511 #+BIBLIOGRAPHY: foo plain option:-d
23513 and derives from it that foo.bib is the bibliography file relevant
23514 for this document. It then installs the necessary environment for RefTeX
23515 to work in this buffer and calls `reftex-citation' to insert a citation
23516 into the buffer.
23518 Export of such citations to both LaTeX and HTML is handled by the contributed
23519 package ox-bibtex by Taru Karttunen."
23520 (interactive)
23521 (let ((reftex-docstruct-symbol 'org--rds)
23522 org--rds bib)
23523 (org-with-wide-buffer
23524 (let ((case-fold-search t)
23525 (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
23526 (if (not (save-excursion
23527 (or (re-search-forward re nil t)
23528 (re-search-backward re nil t))))
23529 (user-error "No bibliography defined in file")
23530 (setq bib (concat (match-string 1) ".bib")
23531 org--rds (list (list 'bib bib))))))
23532 (call-interactively 'reftex-citation)))
23534 ;;;; Functions extending outline functionality
23536 (defun org-beginning-of-line (&optional n)
23537 "Go to the beginning of the current visible line.
23539 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
23540 tags on the first attempt, and only move to after the tags when
23541 the cursor is already beyond the end of the headline.
23543 With argument N not nil or 1, move forward N - 1 lines first."
23544 (interactive "^p")
23545 (let ((origin (point))
23546 (special (pcase org-special-ctrl-a/e
23547 (`(,C-a . ,_) C-a) (_ org-special-ctrl-a/e)))
23548 deactivate-mark)
23549 ;; First move to a visible line.
23550 (if (bound-and-true-p visual-line-mode)
23551 (beginning-of-visual-line n)
23552 (move-beginning-of-line n)
23553 ;; `move-beginning-of-line' may leave point after invisible
23554 ;; characters if line starts with such of these (e.g., with
23555 ;; a link at column 0). Really move to the beginning of the
23556 ;; current visible line.
23557 (beginning-of-line))
23558 (cond
23559 ;; No special behavior. Point is already at the beginning of
23560 ;; a line, logical or visual.
23561 ((not special))
23562 ;; `beginning-of-visual-line' left point before logical beginning
23563 ;; of line: point is at the beginning of a visual line. Bail
23564 ;; out.
23565 ((and (bound-and-true-p visual-line-mode) (not (bolp))))
23566 ((let ((case-fold-search nil)) (looking-at org-complex-heading-regexp))
23567 ;; At a headline, special position is before the title, but
23568 ;; after any TODO keyword or priority cookie.
23569 (let ((refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
23570 (line-end-position)))
23571 (bol (point)))
23572 (if (eq special 'reversed)
23573 (when (and (= origin bol) (eq last-command this-command))
23574 (goto-char refpos))
23575 (when (or (> origin refpos) (= origin bol))
23576 (goto-char refpos)))))
23577 ((and (looking-at org-list-full-item-re)
23578 (memq (org-element-type (save-match-data (org-element-at-point)))
23579 '(item plain-list)))
23580 ;; Set special position at first white space character after
23581 ;; bullet, and check-box, if any.
23582 (let ((after-bullet
23583 (let ((box (match-end 3)))
23584 (cond ((not box) (match-end 1))
23585 ((eq (char-after box) ?\s) (1+ box))
23586 (t box)))))
23587 (if (eq special 'reversed)
23588 (when (and (= (point) origin) (eq last-command this-command))
23589 (goto-char after-bullet))
23590 (when (or (> origin after-bullet) (= (point) origin))
23591 (goto-char after-bullet)))))
23592 ;; No special context. Point is already at beginning of line.
23593 (t nil))))
23595 (defun org-end-of-line (&optional n)
23596 "Go to the end of the line, but before ellipsis, if any.
23598 If this is a headline, and `org-special-ctrl-a/e' is set, ignore
23599 tags on the first attempt, and only move to after the tags when
23600 the cursor is already beyond the end of the headline.
23602 With argument N not nil or 1, move forward N - 1 lines first."
23603 (interactive "^p")
23604 (let ((origin (point))
23605 (special (pcase org-special-ctrl-a/e
23606 (`(,_ . ,C-e) C-e) (_ org-special-ctrl-a/e)))
23607 deactivate-mark)
23608 ;; First move to a visible line.
23609 (if (bound-and-true-p visual-line-mode)
23610 (beginning-of-visual-line n)
23611 (move-beginning-of-line n))
23612 (cond
23613 ;; At a headline, with tags.
23614 ((and special
23615 (save-excursion
23616 (beginning-of-line)
23617 (let ((case-fold-search nil))
23618 (looking-at org-complex-heading-regexp)))
23619 (match-end 5))
23620 (let ((tags (save-excursion
23621 (goto-char (match-beginning 5))
23622 (skip-chars-backward " \t")
23623 (point)))
23624 (visual-end (and (bound-and-true-p visual-line-mode)
23625 (save-excursion
23626 (end-of-visual-line)
23627 (point)))))
23628 ;; If `end-of-visual-line' brings us before end of line or
23629 ;; even tags, i.e., the headline spans over multiple visual
23630 ;; lines, move there.
23631 (cond ((and visual-end
23632 (< visual-end tags)
23633 (<= origin visual-end))
23634 (goto-char visual-end))
23635 ((eq special 'reversed)
23636 (if (and (= origin (line-end-position))
23637 (eq this-command last-command))
23638 (goto-char tags)
23639 (end-of-line)))
23641 (if (or (< origin tags) (= origin (line-end-position)))
23642 (goto-char tags)
23643 (end-of-line))))))
23644 ((bound-and-true-p visual-line-mode)
23645 (let ((bol (line-beginning-position)))
23646 (end-of-visual-line)
23647 ;; If `end-of-visual-line' gets us past the ellipsis at the
23648 ;; end of a line, backtrack and use `end-of-line' instead.
23649 (when (/= bol (line-beginning-position))
23650 (goto-char bol)
23651 (end-of-line))))
23652 (t (end-of-line)))))
23654 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
23655 (define-key org-mode-map "\C-e" 'org-end-of-line)
23657 (defun org-backward-sentence (&optional _arg)
23658 "Go to beginning of sentence, or beginning of table field.
23659 This will call `backward-sentence' or `org-table-beginning-of-field',
23660 depending on context."
23661 (interactive)
23662 (let* ((element (org-element-at-point))
23663 (contents-begin (org-element-property :contents-begin element))
23664 (table (org-element-lineage element '(table) t)))
23665 (if (and table
23666 (> (point) contents-begin)
23667 (<= (point) (org-element-property :contents-end table)))
23668 (call-interactively #'org-table-beginning-of-field)
23669 (save-restriction
23670 (when (and contents-begin
23671 (< (point-min) contents-begin)
23672 (> (point) contents-begin))
23673 (narrow-to-region contents-begin
23674 (org-element-property :contents-end element)))
23675 (call-interactively #'backward-sentence)))))
23677 (defun org-forward-sentence (&optional _arg)
23678 "Go to end of sentence, or end of table field.
23679 This will call `forward-sentence' or `org-table-end-of-field',
23680 depending on context."
23681 (interactive)
23682 (if (and (org-at-heading-p)
23683 (save-restriction (skip-chars-forward " \t") (not (eolp))))
23684 (save-restriction
23685 (narrow-to-region (line-beginning-position) (line-end-position))
23686 (call-interactively #'forward-sentence))
23687 (let* ((element (org-element-at-point))
23688 (contents-end (org-element-property :contents-end element))
23689 (table (org-element-lineage element '(table) t)))
23690 (if (and table
23691 (>= (point) (org-element-property :contents-begin table))
23692 (< (point) contents-end))
23693 (call-interactively #'org-table-end-of-field)
23694 (save-restriction
23695 (when (and contents-end
23696 (> (point-max) contents-end)
23697 ;; Skip blank lines between elements.
23698 (< (org-element-property :end element)
23699 (save-excursion (goto-char contents-end)
23700 (skip-chars-forward " \r\t\n"))))
23701 (narrow-to-region (org-element-property :contents-begin element)
23702 contents-end))
23703 ;; End of heading is considered as the end of a sentence.
23704 (let ((sentence-end (concat (sentence-end) "\\|^\\*+ .*$")))
23705 (call-interactively #'forward-sentence)))))))
23707 (define-key org-mode-map "\M-a" 'org-backward-sentence)
23708 (define-key org-mode-map "\M-e" 'org-forward-sentence)
23710 (defun org-kill-line (&optional _arg)
23711 "Kill line, to tags or end of line."
23712 (interactive)
23713 (cond
23714 ((or (not org-special-ctrl-k)
23715 (bolp)
23716 (not (org-at-heading-p)))
23717 (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
23718 org-ctrl-k-protect-subtree
23719 (or (eq org-ctrl-k-protect-subtree 'error)
23720 (not (y-or-n-p "Kill hidden subtree along with headline? "))))
23721 (user-error
23722 (substitute-command-keys
23723 "`\\[org-kill-line]' aborted as it would kill a hidden subtree")))
23724 (call-interactively
23725 (if (bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
23726 ((looking-at ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")
23727 (kill-region (point) (match-beginning 1))
23728 (org-set-tags nil t))
23729 (t (kill-region (point) (point-at-eol)))))
23731 (define-key org-mode-map "\C-k" 'org-kill-line)
23733 (defun org-yank (&optional arg)
23734 "Yank. If the kill is a subtree, treat it specially.
23735 This command will look at the current kill and check if is a single
23736 subtree, or a series of subtrees[1]. If it passes the test, and if the
23737 cursor is at the beginning of a line or after the stars of a currently
23738 empty headline, then the yank is handled specially. How exactly depends
23739 on the value of the following variables.
23741 `org-yank-folded-subtrees'
23742 By default, this variable is non-nil, which results in
23743 subtree(s) being folded after insertion, except if doing so
23744 would swallow text after the yanked text.
23746 `org-yank-adjusted-subtrees'
23747 When non-nil (the default value is nil), the subtree will be
23748 promoted or demoted in order to fit into the local outline tree
23749 structure, which means that the level will be adjusted so that it
23750 becomes the smaller one of the two *visible* surrounding headings.
23752 Any prefix to this command will cause `yank' to be called directly with
23753 no special treatment. In particular, a simple `\\[universal-argument]' prefix \
23754 will just
23755 plainly yank the text as it is.
23757 \[1] The test checks if the first non-white line is a heading
23758 and if there are no other headings with fewer stars."
23759 (interactive "P")
23760 (org-yank-generic 'yank arg))
23762 (defun org-yank-generic (command arg)
23763 "Perform some yank-like command.
23765 This function implements the behavior described in the `org-yank'
23766 documentation. However, it has been generalized to work for any
23767 interactive command with similar behavior."
23769 ;; pretend to be command COMMAND
23770 (setq this-command command)
23772 (if arg
23773 (call-interactively command)
23775 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
23776 (and (org-kill-is-subtree-p)
23777 (or (bolp)
23778 (and (looking-at "[ \t]*$")
23779 (string-match
23780 "\\`\\*+\\'"
23781 (buffer-substring (point-at-bol) (point)))))))
23782 swallowp)
23783 (cond
23784 ((and subtreep org-yank-folded-subtrees)
23785 (let ((beg (point))
23786 end)
23787 (if (and subtreep org-yank-adjusted-subtrees)
23788 (org-paste-subtree nil nil 'for-yank)
23789 (call-interactively command))
23791 (setq end (point))
23792 (goto-char beg)
23793 (when (and (bolp) subtreep
23794 (not (setq swallowp
23795 (org-yank-folding-would-swallow-text beg end))))
23796 (org-with-limited-levels
23797 (or (looking-at org-outline-regexp)
23798 (re-search-forward org-outline-regexp-bol end t))
23799 (while (and (< (point) end) (looking-at org-outline-regexp))
23800 (outline-hide-subtree)
23801 (org-cycle-show-empty-lines 'folded)
23802 (condition-case nil
23803 (outline-forward-same-level 1)
23804 (error (goto-char end))))))
23805 (when swallowp
23806 (message
23807 "Inserted text not folded because that would swallow text"))
23809 (goto-char end)
23810 (skip-chars-forward " \t\n\r")
23811 (beginning-of-line 1)
23812 (push-mark beg 'nomsg)))
23813 ((and subtreep org-yank-adjusted-subtrees)
23814 (let ((beg (point-at-bol)))
23815 (org-paste-subtree nil nil 'for-yank)
23816 (push-mark beg 'nomsg)))
23818 (call-interactively command))))))
23820 (defun org-yank-folding-would-swallow-text (beg end)
23821 "Would hide-subtree at BEG swallow any text after END?"
23822 (let (level)
23823 (org-with-limited-levels
23824 (save-excursion
23825 (goto-char beg)
23826 (when (or (looking-at org-outline-regexp)
23827 (re-search-forward org-outline-regexp-bol end t))
23828 (setq level (org-outline-level)))
23829 (goto-char end)
23830 (skip-chars-forward " \t\r\n\v\f")
23831 (not (or (eobp)
23832 (and (bolp) (looking-at-p org-outline-regexp)
23833 (<= (org-outline-level) level))))))))
23835 (define-key org-mode-map "\C-y" 'org-yank)
23837 (defun org-truely-invisible-p ()
23838 "Check if point is at a character currently not visible.
23839 This version does not only check the character property, but also
23840 `visible-mode'."
23841 (unless (bound-and-true-p visible-mode)
23842 (org-invisible-p)))
23844 (defun org-invisible-p2 ()
23845 "Check if point is at a character currently not visible.
23847 If the point is at EOL (and not at the beginning of a buffer too),
23848 move it back by one char before doing this check."
23849 (save-excursion
23850 (when (and (eolp) (not (bobp)))
23851 (backward-char 1))
23852 (org-invisible-p)))
23854 (defun org-back-to-heading (&optional invisible-ok)
23855 "Call `outline-back-to-heading', but provide a better error message."
23856 (condition-case nil
23857 (outline-back-to-heading invisible-ok)
23858 (error (error "Before first headline at position %d in buffer %s"
23859 (point) (current-buffer)))))
23861 (defun org-before-first-heading-p ()
23862 "Before first heading?"
23863 (save-excursion
23864 (end-of-line)
23865 (null (re-search-backward org-outline-regexp-bol nil t))))
23867 (defun org-at-heading-p (&optional ignored)
23868 (outline-on-heading-p t))
23870 (defun org-in-commented-heading-p (&optional no-inheritance)
23871 "Non-nil if point is under a commented heading.
23872 This function also checks ancestors of the current headline,
23873 unless optional argument NO-INHERITANCE is non-nil."
23874 (cond
23875 ((org-before-first-heading-p) nil)
23876 ((let ((headline (nth 4 (org-heading-components))))
23877 (and headline
23878 (let ((case-fold-search nil))
23879 (string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
23880 headline)))))
23881 (no-inheritance nil)
23883 (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
23885 (defun org-at-comment-p nil
23886 "Is cursor in a commented line?"
23887 (save-excursion
23888 (save-match-data
23889 (beginning-of-line)
23890 (looking-at "^[ \t]*# "))))
23892 (defun org-at-drawer-p nil
23893 "Is cursor at a drawer keyword?"
23894 (save-excursion
23895 (move-beginning-of-line 1)
23896 (looking-at org-drawer-regexp)))
23898 (defun org-at-block-p nil
23899 "Is cursor at a block keyword?"
23900 (save-excursion
23901 (move-beginning-of-line 1)
23902 (looking-at org-block-regexp)))
23904 (defun org-point-at-end-of-empty-headline ()
23905 "If point is at the end of an empty headline, return t, else nil.
23906 If the heading only contains a TODO keyword, it is still still considered
23907 empty."
23908 (let ((case-fold-search nil))
23909 (and (looking-at "[ \t]*$")
23910 org-todo-line-regexp
23911 (save-excursion
23912 (beginning-of-line)
23913 (looking-at org-todo-line-regexp)
23914 (string= (match-string 3) "")))))
23916 (defun org-at-heading-or-item-p ()
23917 (or (org-at-heading-p) (org-at-item-p)))
23919 (defun org-at-target-p ()
23920 (or (org-in-regexp org-radio-target-regexp)
23921 (org-in-regexp org-target-regexp)))
23922 ;; Compatibility alias with Org versions < 7.8.03
23923 (defalias 'org-on-target-p 'org-at-target-p)
23925 (defun org-up-heading-all (arg)
23926 "Move to the heading line of which the present line is a subheading.
23927 This function considers both visible and invisible heading lines.
23928 With argument, move up ARG levels."
23929 (outline-up-heading arg t))
23931 (defun org-up-heading-safe ()
23932 "Move to the heading line of which the present line is a subheading.
23933 This version will not throw an error. It will return the level of the
23934 headline found, or nil if no higher level is found.
23936 Also, this function will be a lot faster than `outline-up-heading',
23937 because it relies on stars being the outline starters. This can really
23938 make a significant difference in outlines with very many siblings."
23939 (when (ignore-errors (org-back-to-heading t))
23940 (let ((level-up (1- (funcall outline-level))))
23941 (and (> level-up 0)
23942 (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
23943 (funcall outline-level)))))
23945 (defun org-first-sibling-p ()
23946 "Is this heading the first child of its parents?"
23947 (interactive)
23948 (let ((re org-outline-regexp-bol)
23949 level l)
23950 (unless (org-at-heading-p t)
23951 (user-error "Not at a heading"))
23952 (setq level (funcall outline-level))
23953 (save-excursion
23954 (if (not (re-search-backward re nil t))
23956 (setq l (funcall outline-level))
23957 (< l level)))))
23959 (defun org-goto-sibling (&optional previous)
23960 "Goto the next sibling, even if it is invisible.
23961 When PREVIOUS is set, go to the previous sibling instead. Returns t
23962 when a sibling was found. When none is found, return nil and don't
23963 move point."
23964 (let ((fun (if previous 're-search-backward 're-search-forward))
23965 (pos (point))
23966 (re org-outline-regexp-bol)
23967 level l)
23968 (when (ignore-errors (org-back-to-heading t))
23969 (setq level (funcall outline-level))
23970 (catch 'exit
23971 (or previous (forward-char 1))
23972 (while (funcall fun re nil t)
23973 (setq l (funcall outline-level))
23974 (when (< l level) (goto-char pos) (throw 'exit nil))
23975 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
23976 (goto-char pos)
23977 nil))))
23979 (defun org-show-siblings ()
23980 "Show all siblings of the current headline."
23981 (save-excursion
23982 (while (org-goto-sibling) (org-flag-heading nil)))
23983 (save-excursion
23984 (while (org-goto-sibling 'previous)
23985 (org-flag-heading nil))))
23987 (defun org-goto-first-child ()
23988 "Goto the first child, even if it is invisible.
23989 Return t when a child was found. Otherwise don't move point and
23990 return nil."
23991 (let (level (pos (point)) (re org-outline-regexp-bol))
23992 (when (ignore-errors (org-back-to-heading t))
23993 (setq level (outline-level))
23994 (forward-char 1)
23995 (if (and (re-search-forward re nil t) (> (outline-level) level))
23996 (progn (goto-char (match-beginning 0)) t)
23997 (goto-char pos) nil))))
23999 (defun org-show-hidden-entry ()
24000 "Show an entry where even the heading is hidden."
24001 (save-excursion
24002 (org-show-entry)))
24004 (defun org-flag-heading (flag &optional entry)
24005 "Flag the current heading. FLAG non-nil means make invisible.
24006 When ENTRY is non-nil, show the entire entry."
24007 (save-excursion
24008 (org-back-to-heading t)
24009 ;; Check if we should show the entire entry
24010 (if entry
24011 (progn
24012 (org-show-entry)
24013 (save-excursion
24014 (and (outline-next-heading)
24015 (org-flag-heading nil))))
24016 (outline-flag-region (max (point-min) (1- (point)))
24017 (save-excursion (outline-end-of-heading) (point))
24018 flag))))
24020 (defun org-get-next-sibling ()
24021 "Move to next heading of the same level, and return point.
24022 If there is no such heading, return nil.
24023 This is like outline-next-sibling, but invisible headings are ok."
24024 (let ((level (funcall outline-level)))
24025 (outline-next-heading)
24026 (while (and (not (eobp)) (> (funcall outline-level) level))
24027 (outline-next-heading))
24028 (unless (or (eobp) (< (funcall outline-level) level))
24029 (point))))
24031 (defun org-get-last-sibling ()
24032 "Move to previous heading of the same level, and return point.
24033 If there is no such heading, return nil."
24034 (let ((opoint (point))
24035 (level (funcall outline-level)))
24036 (outline-previous-heading)
24037 (when (and (/= (point) opoint) (outline-on-heading-p t))
24038 (while (and (> (funcall outline-level) level)
24039 (not (bobp)))
24040 (outline-previous-heading))
24041 (unless (< (funcall outline-level) level)
24042 (point)))))
24044 (defun org-end-of-subtree (&optional invisible-ok to-heading)
24045 "Goto to the end of a subtree."
24046 ;; This contains an exact copy of the original function, but it uses
24047 ;; `org-back-to-heading', to make it work also in invisible
24048 ;; trees. And is uses an invisible-ok argument.
24049 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
24050 ;; Furthermore, when used inside Org, finding the end of a large subtree
24051 ;; with many children and grandchildren etc, this can be much faster
24052 ;; than the outline version.
24053 (org-back-to-heading invisible-ok)
24054 (let ((first t)
24055 (level (funcall outline-level)))
24056 (if (and (derived-mode-p 'org-mode) (< level 1000))
24057 ;; A true heading (not a plain list item), in Org
24058 ;; This means we can easily find the end by looking
24059 ;; only for the right number of stars. Using a regexp to do
24060 ;; this is so much faster than using a Lisp loop.
24061 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
24062 (forward-char 1)
24063 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
24064 ;; something else, do it the slow way
24065 (while (and (not (eobp))
24066 (or first (> (funcall outline-level) level)))
24067 (setq first nil)
24068 (outline-next-heading)))
24069 (unless to-heading
24070 (when (memq (preceding-char) '(?\n ?\^M))
24071 ;; Go to end of line before heading
24072 (forward-char -1)
24073 (when (memq (preceding-char) '(?\n ?\^M))
24074 ;; leave blank line before heading
24075 (forward-char -1)))))
24076 (point))
24078 (defun org-end-of-meta-data (&optional full)
24079 "Skip planning line and properties drawer in current entry.
24080 When optional argument FULL is non-nil, also skip empty lines,
24081 clocking lines and regular drawers at the beginning of the
24082 entry."
24083 (org-back-to-heading t)
24084 (forward-line)
24085 (when (looking-at-p org-planning-line-re) (forward-line))
24086 (when (looking-at org-property-drawer-re)
24087 (goto-char (match-end 0))
24088 (forward-line))
24089 (when (and full (not (org-at-heading-p)))
24090 (catch 'exit
24091 (let ((end (save-excursion (outline-next-heading) (point)))
24092 (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
24093 (while (not (eobp))
24094 (cond ((looking-at-p org-drawer-regexp)
24095 (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
24096 (forward-line)
24097 (throw 'exit t)))
24098 ((looking-at-p re) (forward-line))
24099 (t (throw 'exit t))))))))
24101 (defun org-forward-heading-same-level (arg &optional invisible-ok)
24102 "Move forward to the ARG'th subheading at same level as this one.
24103 Stop at the first and last subheadings of a superior heading.
24104 Normally this only looks at visible headings, but when INVISIBLE-OK is
24105 non-nil it will also look at invisible ones."
24106 (interactive "p")
24107 (let ((backward? (and arg (< arg 0))))
24108 (if (org-before-first-heading-p)
24109 (if backward? (goto-char (point-min)) (outline-next-heading))
24110 (org-back-to-heading invisible-ok)
24111 (unless backward? (end-of-line)) ;do not match current headline
24112 (let ((level (- (match-end 0) (match-beginning 0) 1))
24113 (f (if backward? #'re-search-backward #'re-search-forward))
24114 (count (if arg (abs arg) 1))
24115 (result (point)))
24116 (while (and (> count 0)
24117 (funcall f org-outline-regexp-bol nil 'move))
24118 (let ((l (- (match-end 0) (match-beginning 0) 1)))
24119 (cond ((< l level) (setq count 0))
24120 ((and (= l level)
24121 (or invisible-ok
24122 (not (org-invisible-p
24123 (line-beginning-position)))))
24124 (cl-decf count)
24125 (when (= l level) (setq result (point)))))))
24126 (goto-char result))
24127 (beginning-of-line))))
24129 (defun org-backward-heading-same-level (arg &optional invisible-ok)
24130 "Move backward to the ARG'th subheading at same level as this one.
24131 Stop at the first and last subheadings of a superior heading."
24132 (interactive "p")
24133 (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
24135 (defun org-next-visible-heading (arg)
24136 "Move to the next visible heading.
24138 This function wraps `outline-next-visible-heading' with
24139 `org-with-limited-levels' in order to skip over inline tasks and
24140 respect customization of `org-odd-levels-only'."
24141 (interactive "p")
24142 (org-with-limited-levels
24143 (outline-next-visible-heading arg)))
24145 (defun org-previous-visible-heading (arg)
24146 "Move to the previous visible heading.
24148 This function wraps `outline-previous-visible-heading' with
24149 `org-with-limited-levels' in order to skip over inline tasks and
24150 respect customization of `org-odd-levels-only'."
24151 (interactive "p")
24152 (org-with-limited-levels
24153 (outline-previous-visible-heading arg)))
24155 (defun org-next-block (arg &optional backward block-regexp)
24156 "Jump to the next block.
24158 With a prefix argument ARG, jump forward ARG many blocks.
24160 When BACKWARD is non-nil, jump to the previous block.
24162 When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
24163 Match data is set according to this regexp when the function
24164 returns.
24166 Return point at beginning of the opening line of found block.
24167 Throw an error if no block is found."
24168 (interactive "p")
24169 (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
24170 (case-fold-search t)
24171 (search-fn (if backward #'re-search-backward #'re-search-forward))
24172 (count (or arg 1))
24173 (origin (point))
24174 last-element)
24175 (if backward (beginning-of-line) (end-of-line))
24176 (while (and (> count 0) (funcall search-fn re nil t))
24177 (let ((element (save-excursion
24178 (goto-char (match-beginning 0))
24179 (save-match-data (org-element-at-point)))))
24180 (when (and (memq (org-element-type element)
24181 '(center-block comment-block dynamic-block
24182 example-block export-block quote-block
24183 special-block src-block verse-block))
24184 (<= (match-beginning 0)
24185 (org-element-property :post-affiliated element)))
24186 (setq last-element element)
24187 (cl-decf count))))
24188 (if (= count 0)
24189 (prog1 (goto-char (org-element-property :post-affiliated last-element))
24190 (save-match-data (org-show-context)))
24191 (goto-char origin)
24192 (user-error "No %s code blocks" (if backward "previous" "further")))))
24194 (defun org-previous-block (arg &optional block-regexp)
24195 "Jump to the previous block.
24196 With a prefix argument ARG, jump backward ARG many source blocks.
24197 When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
24198 (interactive "p")
24199 (org-next-block arg t block-regexp))
24201 (defun org-forward-paragraph ()
24202 "Move forward to beginning of next paragraph or equivalent.
24204 The function moves point to the beginning of the next visible
24205 structural element, which can be a paragraph, a table, a list
24206 item, etc. It also provides some special moves for convenience:
24208 - On an affiliated keyword, jump to the beginning of the
24209 relative element.
24210 - On an item or a footnote definition, move to the second
24211 element inside, if any.
24212 - On a table or a property drawer, jump after it.
24213 - On a verse or source block, stop after blank lines."
24214 (interactive)
24215 (unless (eobp)
24216 (let* ((deactivate-mark nil)
24217 (element (org-element-at-point))
24218 (type (org-element-type element))
24219 (post-affiliated (org-element-property :post-affiliated element))
24220 (contents-begin (org-element-property :contents-begin element))
24221 (contents-end (org-element-property :contents-end element))
24222 (end (let ((end (org-element-property :end element)) (parent element))
24223 (while (and (setq parent (org-element-property :parent parent))
24224 (= (org-element-property :contents-end parent) end))
24225 (setq end (org-element-property :end parent)))
24226 end)))
24227 (cond ((not element)
24228 (skip-chars-forward " \r\t\n")
24229 (or (eobp) (beginning-of-line)))
24230 ;; On affiliated keywords, move to element's beginning.
24231 ((< (point) post-affiliated)
24232 (goto-char post-affiliated))
24233 ;; At a table row, move to the end of the table. Similarly,
24234 ;; at a node property, move to the end of the property
24235 ;; drawer.
24236 ((memq type '(node-property table-row))
24237 (goto-char (org-element-property
24238 :end (org-element-property :parent element))))
24239 ((memq type '(property-drawer table)) (goto-char end))
24240 ;; Consider blank lines as separators in verse and source
24241 ;; blocks to ease editing.
24242 ((memq type '(src-block verse-block))
24243 (when (eq type 'src-block)
24244 (setq contents-end
24245 (save-excursion (goto-char end)
24246 (skip-chars-backward " \r\t\n")
24247 (line-beginning-position))))
24248 (beginning-of-line)
24249 (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
24250 (if (not (re-search-forward "^[ \t]*$" contents-end t))
24251 (goto-char end)
24252 (skip-chars-forward " \r\t\n")
24253 (if (= (point) contents-end) (goto-char end)
24254 (beginning-of-line))))
24255 ;; With no contents, just skip element.
24256 ((not contents-begin) (goto-char end))
24257 ;; If contents are invisible, skip the element altogether.
24258 ((org-invisible-p (line-end-position))
24259 (cl-case type
24260 (headline
24261 (org-with-limited-levels (outline-next-visible-heading 1)))
24262 ;; At a plain list, make sure we move to the next item
24263 ;; instead of skipping the whole list.
24264 (plain-list (forward-char)
24265 (org-forward-paragraph))
24266 (otherwise (goto-char end))))
24267 ((>= (point) contents-end) (goto-char end))
24268 ((>= (point) contents-begin)
24269 ;; This can only happen on paragraphs and plain lists.
24270 (cl-case type
24271 (paragraph (goto-char end))
24272 ;; At a plain list, try to move to second element in
24273 ;; first item, if possible.
24274 (plain-list (end-of-line)
24275 (org-forward-paragraph))))
24276 ;; When contents start on the middle of a line (e.g. in
24277 ;; items and footnote definitions), try to reach first
24278 ;; element starting after current line.
24279 ((> (line-end-position) contents-begin)
24280 (end-of-line)
24281 (org-forward-paragraph))
24282 (t (goto-char contents-begin))))))
24284 (defun org-backward-paragraph ()
24285 "Move backward to start of previous paragraph or equivalent.
24287 The function moves point to the beginning of the current
24288 structural element, which can be a paragraph, a table, a list
24289 item, etc., or to the beginning of the previous visible one if
24290 point is already there. It also provides some special moves for
24291 convenience:
24293 - On an affiliated keyword, jump to the first one.
24294 - On a table or a property drawer, move to its beginning.
24295 - On comment, example, export, src and verse blocks, stop
24296 before blank lines."
24297 (interactive)
24298 (unless (bobp)
24299 (let* ((deactivate-mark nil)
24300 (element (org-element-at-point))
24301 (type (org-element-type element))
24302 (contents-end (org-element-property :contents-end element))
24303 (post-affiliated (org-element-property :post-affiliated element))
24304 (begin (org-element-property :begin element))
24305 (special? ;blocks handled specially
24306 (memq type '(comment-block example-block export-block src-block
24307 verse-block)))
24308 (contents-begin
24309 (if special?
24310 ;; These types have no proper contents. Fake line
24311 ;; below the block opening line as contents beginning.
24312 (save-excursion (goto-char begin) (line-beginning-position 2))
24313 (org-element-property :contents-begin element))))
24314 (cond
24315 ((not element) (goto-char (point-min)))
24316 ((= (point) begin)
24317 (backward-char)
24318 (org-backward-paragraph))
24319 ((<= (point) post-affiliated) (goto-char begin))
24320 ;; Special behavior: on a table or a property drawer, move to
24321 ;; its beginning.
24322 ((memq type '(node-property table-row))
24323 (goto-char (org-element-property
24324 :post-affiliated (org-element-property :parent element))))
24325 (special?
24326 (if (<= (point) contents-begin) (goto-char post-affiliated)
24327 ;; Inside a verse block, see blank lines as paragraph
24328 ;; separators.
24329 (let ((origin (point)))
24330 (skip-chars-backward " \r\t\n" contents-begin)
24331 (when (re-search-backward "^[ \t]*$" contents-begin 'move)
24332 (skip-chars-forward " \r\t\n" origin)
24333 (if (= (point) origin) (goto-char contents-begin)
24334 (beginning-of-line))))))
24335 ((eq type 'paragraph) (goto-char contents-begin)
24336 ;; When at first paragraph in an item or a footnote definition,
24337 ;; move directly to beginning of line.
24338 (let ((parent-contents
24339 (org-element-property
24340 :contents-begin (org-element-property :parent element))))
24341 (when (and parent-contents (= parent-contents contents-begin))
24342 (beginning-of-line))))
24343 ;; At the end of a greater element, move to the beginning of
24344 ;; the last element within.
24345 ((and contents-end (>= (point) contents-end))
24346 (goto-char (1- contents-end))
24347 (org-backward-paragraph))
24348 (t (goto-char (or post-affiliated begin))))
24349 ;; Ensure we never leave point invisible.
24350 (when (org-invisible-p (point)) (beginning-of-visual-line)))))
24352 (defun org-forward-element ()
24353 "Move forward by one element.
24354 Move to the next element at the same level, when possible."
24355 (interactive)
24356 (cond ((eobp) (user-error "Cannot move further down"))
24357 ((org-with-limited-levels (org-at-heading-p))
24358 (let ((origin (point)))
24359 (goto-char (org-end-of-subtree nil t))
24360 (unless (org-with-limited-levels (org-at-heading-p))
24361 (goto-char origin)
24362 (user-error "Cannot move further down"))))
24364 (let* ((elem (org-element-at-point))
24365 (end (org-element-property :end elem))
24366 (parent (org-element-property :parent elem)))
24367 (cond ((and parent (= (org-element-property :contents-end parent) end))
24368 (goto-char (org-element-property :end parent)))
24369 ((integer-or-marker-p end) (goto-char end))
24370 (t (message "No element at point")))))))
24372 (defun org-backward-element ()
24373 "Move backward by one element.
24374 Move to the previous element at the same level, when possible."
24375 (interactive)
24376 (cond ((bobp) (user-error "Cannot move further up"))
24377 ((org-with-limited-levels (org-at-heading-p))
24378 ;; At a headline, move to the previous one, if any, or stay
24379 ;; here.
24380 (let ((origin (point)))
24381 (org-with-limited-levels (org-backward-heading-same-level 1))
24382 ;; When current headline has no sibling above, move to its
24383 ;; parent.
24384 (when (= (point) origin)
24385 (or (org-with-limited-levels (org-up-heading-safe))
24386 (progn (goto-char origin)
24387 (user-error "Cannot move further up"))))))
24389 (let* ((elem (org-element-at-point))
24390 (beg (org-element-property :begin elem)))
24391 (cond
24392 ;; Move to beginning of current element if point isn't
24393 ;; there already.
24394 ((null beg) (message "No element at point"))
24395 ((/= (point) beg) (goto-char beg))
24396 (t (goto-char beg)
24397 (skip-chars-backward " \r\t\n")
24398 (unless (bobp)
24399 (let ((prev (org-element-at-point)))
24400 (goto-char (org-element-property :begin prev))
24401 (while (and (setq prev (org-element-property :parent prev))
24402 (<= (org-element-property :end prev) beg))
24403 (goto-char (org-element-property :begin prev)))))))))))
24405 (defun org-up-element ()
24406 "Move to upper element."
24407 (interactive)
24408 (if (org-with-limited-levels (org-at-heading-p))
24409 (unless (org-up-heading-safe) (user-error "No surrounding element"))
24410 (let* ((elem (org-element-at-point))
24411 (parent (org-element-property :parent elem)))
24412 (if parent (goto-char (org-element-property :begin parent))
24413 (if (org-with-limited-levels (org-before-first-heading-p))
24414 (user-error "No surrounding element")
24415 (org-with-limited-levels (org-back-to-heading)))))))
24417 (defun org-down-element ()
24418 "Move to inner element."
24419 (interactive)
24420 (let ((element (org-element-at-point)))
24421 (cond
24422 ((memq (org-element-type element) '(plain-list table))
24423 (goto-char (org-element-property :contents-begin element))
24424 (forward-char))
24425 ((memq (org-element-type element) org-element-greater-elements)
24426 ;; If contents are hidden, first disclose them.
24427 (when (org-invisible-p (line-end-position)) (org-cycle))
24428 (goto-char (or (org-element-property :contents-begin element)
24429 (user-error "No content for this element"))))
24430 (t (user-error "No inner element")))))
24432 (defun org-drag-element-backward ()
24433 "Move backward element at point."
24434 (interactive)
24435 (let ((elem (or (org-element-at-point)
24436 (user-error "No element at point"))))
24437 (if (eq (org-element-type elem) 'headline)
24438 ;; Preserve point when moving a whole tree, even if point was
24439 ;; on blank lines below the headline.
24440 (let ((offset (skip-chars-backward " \t\n")))
24441 (unwind-protect (org-move-subtree-up)
24442 (forward-char (- offset))))
24443 (let ((prev-elem
24444 (save-excursion
24445 (goto-char (org-element-property :begin elem))
24446 (skip-chars-backward " \r\t\n")
24447 (unless (bobp)
24448 (let* ((beg (org-element-property :begin elem))
24449 (prev (org-element-at-point))
24450 (up prev))
24451 (while (and (setq up (org-element-property :parent up))
24452 (<= (org-element-property :end up) beg))
24453 (setq prev up))
24454 prev)))))
24455 ;; Error out if no previous element or previous element is
24456 ;; a parent of the current one.
24457 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
24458 (user-error "Cannot drag element backward")
24459 (let ((pos (point)))
24460 (org-element-swap-A-B prev-elem elem)
24461 (goto-char (+ (org-element-property :begin prev-elem)
24462 (- pos (org-element-property :begin elem))))))))))
24464 (defun org-drag-element-forward ()
24465 "Move forward element at point."
24466 (interactive)
24467 (let* ((pos (point))
24468 (elem (or (org-element-at-point)
24469 (user-error "No element at point"))))
24470 (when (= (point-max) (org-element-property :end elem))
24471 (user-error "Cannot drag element forward"))
24472 (goto-char (org-element-property :end elem))
24473 (let ((next-elem (org-element-at-point)))
24474 (when (or (org-element-nested-p elem next-elem)
24475 (and (eq (org-element-type next-elem) 'headline)
24476 (not (eq (org-element-type elem) 'headline))))
24477 (goto-char pos)
24478 (user-error "Cannot drag element forward"))
24479 ;; Compute new position of point: it's shifted by NEXT-ELEM
24480 ;; body's length (without final blanks) and by the length of
24481 ;; blanks between ELEM and NEXT-ELEM.
24482 (let ((size-next (- (save-excursion
24483 (goto-char (org-element-property :end next-elem))
24484 (skip-chars-backward " \r\t\n")
24485 (forward-line)
24486 ;; Small correction if buffer doesn't end
24487 ;; with a newline character.
24488 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
24489 (org-element-property :begin next-elem)))
24490 (size-blank (- (org-element-property :end elem)
24491 (save-excursion
24492 (goto-char (org-element-property :end elem))
24493 (skip-chars-backward " \r\t\n")
24494 (forward-line)
24495 (point)))))
24496 (org-element-swap-A-B elem next-elem)
24497 (goto-char (+ pos size-next size-blank))))))
24499 (defun org-drag-line-forward (arg)
24500 "Drag the line at point ARG lines forward."
24501 (interactive "p")
24502 (dotimes (_ (abs arg))
24503 (let ((c (current-column)))
24504 (if (< 0 arg)
24505 (progn
24506 (beginning-of-line 2)
24507 (transpose-lines 1)
24508 (beginning-of-line 0))
24509 (transpose-lines 1)
24510 (beginning-of-line -1))
24511 (org-move-to-column c))))
24513 (defun org-drag-line-backward (arg)
24514 "Drag the line at point ARG lines backward."
24515 (interactive "p")
24516 (org-drag-line-forward (- arg)))
24518 (defun org-mark-element ()
24519 "Put point at beginning of this element, mark at end.
24521 Interactively, if this command is repeated or (in Transient Mark
24522 mode) if the mark is active, it marks the next element after the
24523 ones already marked."
24524 (interactive)
24525 (let (deactivate-mark)
24526 (if (and (called-interactively-p 'any)
24527 (or (and (eq last-command this-command) (mark t))
24528 (and transient-mark-mode mark-active)))
24529 (set-mark
24530 (save-excursion
24531 (goto-char (mark))
24532 (goto-char (org-element-property :end (org-element-at-point)))))
24533 (let ((element (org-element-at-point)))
24534 (end-of-line)
24535 (push-mark (org-element-property :end element) t t)
24536 (goto-char (org-element-property :begin element))))))
24538 (defun org-narrow-to-element ()
24539 "Narrow buffer to current element."
24540 (interactive)
24541 (let ((elem (org-element-at-point)))
24542 (cond
24543 ((eq (car elem) 'headline)
24544 (narrow-to-region
24545 (org-element-property :begin elem)
24546 (org-element-property :end elem)))
24547 ((memq (car elem) org-element-greater-elements)
24548 (narrow-to-region
24549 (org-element-property :contents-begin elem)
24550 (org-element-property :contents-end elem)))
24552 (narrow-to-region
24553 (org-element-property :begin elem)
24554 (org-element-property :end elem))))))
24556 (defun org-transpose-element ()
24557 "Transpose current and previous elements, keeping blank lines between.
24558 Point is moved after both elements."
24559 (interactive)
24560 (org-skip-whitespace)
24561 (let ((end (org-element-property :end (org-element-at-point))))
24562 (org-drag-element-backward)
24563 (goto-char end)))
24565 (defun org-unindent-buffer ()
24566 "Un-indent the visible part of the buffer.
24567 Relative indentation (between items, inside blocks, etc.) isn't
24568 modified."
24569 (interactive)
24570 (unless (eq major-mode 'org-mode)
24571 (user-error "Cannot un-indent a buffer not in Org mode"))
24572 (letrec ((parse-tree (org-element-parse-buffer 'greater-element))
24573 (unindent-tree
24574 (lambda (contents)
24575 (dolist (element (reverse contents))
24576 (if (memq (org-element-type element) '(headline section))
24577 (funcall unindent-tree (org-element-contents element))
24578 (save-excursion
24579 (save-restriction
24580 (narrow-to-region
24581 (org-element-property :begin element)
24582 (org-element-property :end element))
24583 (org-do-remove-indentation))))))))
24584 (funcall unindent-tree (org-element-contents parse-tree))))
24586 (defun org-show-children (&optional level)
24587 "Show all direct subheadings of this heading.
24588 Prefix arg LEVEL is how many levels below the current level
24589 should be shown. Default is enough to cause the following
24590 heading to appear."
24591 (interactive "p")
24592 ;; If `orgstruct-mode' is active, use the slower version.
24593 (if orgstruct-mode (call-interactively #'outline-show-children)
24594 (save-excursion
24595 (org-back-to-heading t)
24596 (let* ((current-level (funcall outline-level))
24597 (max-level (org-get-valid-level
24598 current-level
24599 (if level (prefix-numeric-value level) 1)))
24600 (end (save-excursion (org-end-of-subtree t t)))
24601 (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
24602 (past-first-child nil)
24603 ;; Make sure to skip inlinetasks.
24604 (re (format regexp-fmt
24605 current-level
24606 (cond
24607 ((not (featurep 'org-inlinetask)) "")
24608 (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
24610 (t (1- org-inlinetask-min-level))))))
24611 ;; Display parent heading.
24612 (outline-flag-region (line-end-position 0) (line-end-position) nil)
24613 (forward-line)
24614 ;; Display children. First child may be deeper than expected
24615 ;; MAX-LEVEL. Since we want to display it anyway, adjust
24616 ;; MAX-LEVEL accordingly.
24617 (while (re-search-forward re end t)
24618 (unless past-first-child
24619 (setq re (format regexp-fmt
24620 current-level
24621 (max (funcall outline-level) max-level)))
24622 (setq past-first-child t))
24623 (outline-flag-region
24624 (line-end-position 0) (line-end-position) nil))))))
24626 (defun org-show-subtree ()
24627 "Show everything after this heading at deeper levels."
24628 (interactive)
24629 (outline-flag-region
24630 (point)
24631 (save-excursion
24632 (org-end-of-subtree t t))
24633 nil))
24635 (defun org-show-entry ()
24636 "Show the body directly following this heading.
24637 Show the heading too, if it is currently invisible."
24638 (interactive)
24639 (save-excursion
24640 (ignore-errors
24641 (org-back-to-heading t)
24642 (outline-flag-region
24643 (max (point-min) (1- (point)))
24644 (save-excursion
24645 (if (re-search-forward
24646 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
24647 (match-beginning 1)
24648 (point-max)))
24649 nil)
24650 (org-cycle-hide-drawers 'children))))
24652 (defun org-make-options-regexp (kwds &optional extra)
24653 "Make a regular expression for keyword lines.
24654 KWDS is a list of keywords, as strings. Optional argument EXTRA,
24655 when non-nil, is a regexp matching keywords names."
24656 (concat "^[ \t]*#\\+\\("
24657 (regexp-opt kwds)
24658 (and extra (concat (and kwds "\\|") extra))
24659 "\\):[ \t]*\\(.*\\)"))
24661 ;;;; Integration with and fixes for other packages
24663 ;;; Imenu support
24665 (defvar-local org-imenu-markers nil
24666 "All markers currently used by Imenu.")
24668 (defun org-imenu-new-marker (&optional pos)
24669 "Return a new marker for use by Imenu, and remember the marker."
24670 (let ((m (make-marker)))
24671 (move-marker m (or pos (point)))
24672 (push m org-imenu-markers)
24675 (defun org-imenu-get-tree ()
24676 "Produce the index for Imenu."
24677 (dolist (x org-imenu-markers) (move-marker x nil))
24678 (setq org-imenu-markers nil)
24679 (let* ((case-fold-search nil)
24680 (n org-imenu-depth)
24681 (re (concat "^" (org-get-limited-outline-regexp)))
24682 (subs (make-vector (1+ n) nil))
24683 (last-level 0)
24684 m level head0 head)
24685 (org-with-wide-buffer
24686 (goto-char (point-max))
24687 (while (re-search-backward re nil t)
24688 (setq level (org-reduced-level (funcall outline-level)))
24689 (when (and (<= level n)
24690 (looking-at org-complex-heading-regexp)
24691 (setq head0 (match-string-no-properties 4)))
24692 (setq head (org-link-display-format head0)
24693 m (org-imenu-new-marker))
24694 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
24695 (if (>= level last-level)
24696 (push (cons head m) (aref subs level))
24697 (push (cons head (aref subs (1+ level))) (aref subs level))
24698 (cl-loop for i from (1+ level) to n do (aset subs i nil)))
24699 (setq last-level level))))
24700 (aref subs 1)))
24702 (eval-after-load "imenu"
24703 '(progn
24704 (add-hook 'imenu-after-jump-hook
24705 (lambda ()
24706 (when (derived-mode-p 'org-mode)
24707 (org-show-context 'org-goto))))))
24709 (defun org-link-display-format (s)
24710 "Replace links in string S with their description.
24711 If there is no description, use the link target."
24712 (save-match-data
24713 (replace-regexp-in-string
24714 org-bracket-link-analytic-regexp
24715 (lambda (m)
24716 (if (match-end 5) (match-string 5 m)
24717 (concat (match-string 1 m) (match-string 3 m))))
24718 s nil t)))
24720 (defun org-toggle-link-display ()
24721 "Toggle the literal or descriptive display of links."
24722 (interactive)
24723 (if org-descriptive-links
24724 (progn (org-remove-from-invisibility-spec '(org-link))
24725 (org-restart-font-lock)
24726 (setq org-descriptive-links nil))
24727 (progn (add-to-invisibility-spec '(org-link))
24728 (org-restart-font-lock)
24729 (setq org-descriptive-links t))))
24731 ;; Speedbar support
24733 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
24734 "Overlay marking the agenda restriction line in speedbar.")
24735 (overlay-put org-speedbar-restriction-lock-overlay
24736 'face 'org-agenda-restriction-lock)
24737 (overlay-put org-speedbar-restriction-lock-overlay
24738 'help-echo "Agendas are currently limited to this item.")
24739 (delete-overlay org-speedbar-restriction-lock-overlay)
24741 (defun org-speedbar-set-agenda-restriction ()
24742 "Restrict future agenda commands to the location at point in speedbar.
24743 To get rid of the restriction, use `\\[org-agenda-remove-restriction-lock]'."
24744 (interactive)
24745 (require 'org-agenda)
24746 (let (p m tp np dir txt)
24747 (cond
24748 ((setq p (text-property-any (point-at-bol) (point-at-eol)
24749 'org-imenu t))
24750 (setq m (get-text-property p 'org-imenu-marker))
24751 (with-current-buffer (marker-buffer m)
24752 (goto-char m)
24753 (org-agenda-set-restriction-lock 'subtree)))
24754 ((setq p (text-property-any (point-at-bol) (point-at-eol)
24755 'speedbar-function 'speedbar-find-file))
24756 (setq tp (previous-single-property-change
24757 (1+ p) 'speedbar-function)
24758 np (next-single-property-change
24759 tp 'speedbar-function)
24760 dir (speedbar-line-directory)
24761 txt (buffer-substring-no-properties (or tp (point-min))
24762 (or np (point-max))))
24763 (with-current-buffer (find-file-noselect
24764 (let ((default-directory dir))
24765 (expand-file-name txt)))
24766 (unless (derived-mode-p 'org-mode)
24767 (user-error "Cannot restrict to non-Org mode file"))
24768 (org-agenda-set-restriction-lock 'file)))
24769 (t (user-error "Don't know how to restrict Org mode agenda")))
24770 (move-overlay org-speedbar-restriction-lock-overlay
24771 (point-at-bol) (point-at-eol))
24772 (setq current-prefix-arg nil)
24773 (org-agenda-maybe-redo)))
24775 (defvar speedbar-file-key-map)
24776 (declare-function speedbar-add-supported-extension "speedbar" (extension))
24777 (eval-after-load "speedbar"
24778 '(progn
24779 (speedbar-add-supported-extension ".org")
24780 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
24781 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
24782 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
24783 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
24784 (add-hook 'speedbar-visiting-tag-hook
24785 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
24787 ;;; Fixes and Hacks for problems with other packages
24789 (defun org--flyspell-object-check-p (element)
24790 "Non-nil when Flyspell can check object at point.
24791 ELEMENT is the element at point."
24792 (let ((object (save-excursion
24793 (when (looking-at-p "\\>") (backward-char))
24794 (org-element-context element))))
24795 (cl-case (org-element-type object)
24796 ;; Prevent checks in links due to keybinding conflict with
24797 ;; Flyspell.
24798 ((code entity export-snippet inline-babel-call
24799 inline-src-block line-break latex-fragment link macro
24800 statistics-cookie target timestamp verbatim)
24801 nil)
24802 (footnote-reference
24803 ;; Only in inline footnotes, within the definition.
24804 (and (eq (org-element-property :type object) 'inline)
24805 (< (save-excursion
24806 (goto-char (org-element-property :begin object))
24807 (search-forward ":" nil t 2))
24808 (point))))
24809 (otherwise t))))
24811 (defun org-mode-flyspell-verify ()
24812 "Function used for `flyspell-generic-check-word-predicate'."
24813 (if (org-at-heading-p)
24814 ;; At a headline or an inlinetask, check title only. This is
24815 ;; faster than relying on `org-element-at-point'.
24816 (and (save-excursion (beginning-of-line)
24817 (and (let ((case-fold-search t))
24818 (not (looking-at-p "\\*+ END[ \t]*$")))
24819 (let ((case-fold-search nil))
24820 (looking-at org-complex-heading-regexp))))
24821 (match-beginning 4)
24822 (>= (point) (match-beginning 4))
24823 (or (not (match-beginning 5))
24824 (< (point) (match-beginning 5))))
24825 (let* ((element (org-element-at-point))
24826 (post-affiliated (org-element-property :post-affiliated element)))
24827 (cond
24828 ;; Ignore checks in all affiliated keywords but captions.
24829 ((< (point) post-affiliated)
24830 (and (save-excursion
24831 (beginning-of-line)
24832 (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
24833 (> (point) (match-end 0))
24834 (org--flyspell-object-check-p element)))
24835 ;; Ignore checks in LOGBOOK (or equivalent) drawer.
24836 ((let ((log (org-log-into-drawer)))
24837 (and log
24838 (let ((drawer (org-element-lineage element '(drawer))))
24839 (and drawer
24840 (eq (compare-strings
24841 log nil nil
24842 (org-element-property :drawer-name drawer) nil nil t)
24843 t)))))
24844 nil)
24846 (cl-case (org-element-type element)
24847 ((comment quote-section) t)
24848 (comment-block
24849 ;; Allow checks between block markers, not on them.
24850 (and (> (line-beginning-position) post-affiliated)
24851 (save-excursion
24852 (end-of-line)
24853 (skip-chars-forward " \r\t\n")
24854 (< (point) (org-element-property :end element)))))
24855 ;; Arbitrary list of keywords where checks are meaningful.
24856 ;; Make sure point is on the value part of the element.
24857 (keyword
24858 (and (member (org-element-property :key element)
24859 '("DESCRIPTION" "TITLE"))
24860 (save-excursion
24861 (search-backward ":" (line-beginning-position) t))))
24862 ;; Check is globally allowed in paragraphs verse blocks and
24863 ;; table rows (after affiliated keywords) but some objects
24864 ;; must not be affected.
24865 ((paragraph table-row verse-block)
24866 (let ((cbeg (org-element-property :contents-begin element))
24867 (cend (org-element-property :contents-end element)))
24868 (and cbeg (>= (point) cbeg) (< (point) cend)
24869 (org--flyspell-object-check-p element))))))))))
24870 (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
24872 (defun org-remove-flyspell-overlays-in (beg end)
24873 "Remove flyspell overlays in region."
24874 (and (bound-and-true-p flyspell-mode)
24875 (fboundp 'flyspell-delete-region-overlays)
24876 (flyspell-delete-region-overlays beg end)))
24878 (defvar flyspell-delayed-commands)
24879 (eval-after-load "flyspell"
24880 '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
24882 ;; Make `bookmark-jump' shows the jump location if it was hidden.
24883 (eval-after-load "bookmark"
24884 '(if (boundp 'bookmark-after-jump-hook)
24885 ;; We can use the hook
24886 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
24887 ;; Hook not available, use advice
24888 (defadvice bookmark-jump (after org-make-visible activate)
24889 "Make the position visible."
24890 (org-bookmark-jump-unhide))))
24892 ;; Make sure saveplace shows the location if it was hidden
24893 (eval-after-load "saveplace"
24894 '(defadvice save-place-find-file-hook (after org-make-visible activate)
24895 "Make the position visible."
24896 (org-bookmark-jump-unhide)))
24898 ;; Make sure ecb shows the location if it was hidden
24899 (eval-after-load "ecb"
24900 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
24901 "Make hierarchy visible when jumping into location from ECB tree buffer."
24902 (when (derived-mode-p 'org-mode)
24903 (org-show-context))))
24905 (defun org-bookmark-jump-unhide ()
24906 "Unhide the current position, to show the bookmark location."
24907 (and (derived-mode-p 'org-mode)
24908 (or (org-invisible-p)
24909 (save-excursion (goto-char (max (point-min) (1- (point))))
24910 (org-invisible-p)))
24911 (org-show-context 'bookmark-jump)))
24913 (defun org-mark-jump-unhide ()
24914 "Make the point visible with `org-show-context' after jumping to the mark."
24915 (when (and (derived-mode-p 'org-mode)
24916 (org-invisible-p))
24917 (org-show-context 'mark-goto)))
24919 (eval-after-load "simple"
24920 '(defadvice pop-to-mark-command (after org-make-visible activate)
24921 "Make the point visible with `org-show-context'."
24922 (org-mark-jump-unhide)))
24924 (eval-after-load "simple"
24925 '(defadvice exchange-point-and-mark (after org-make-visible activate)
24926 "Make the point visible with `org-show-context'."
24927 (org-mark-jump-unhide)))
24929 (eval-after-load "simple"
24930 '(defadvice pop-global-mark (after org-make-visible activate)
24931 "Make the point visible with `org-show-context'."
24932 (org-mark-jump-unhide)))
24934 ;; Make session.el ignore our circular variable
24935 (defvar session-globals-exclude)
24936 (eval-after-load "session"
24937 '(add-to-list 'session-globals-exclude 'org-mark-ring))
24939 ;;;; Finish up
24941 (provide 'org)
24943 (run-hooks 'org-load-hook)
24945 ;;; org.el ends here