Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-compat.el
blobf6dd12801a5109af5530f51e45be7cdab43133a8
1 ;;; org-compat.el --- Compatibility Code for Older Emacsen -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2018 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: https://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains code needed for compatibility with older
28 ;; versions of GNU Emacs and integration with other packages.
30 ;;; Code:
32 (require 'cl-lib)
33 (require 'org-macs)
35 (declare-function org-agenda-diary-entry "org-agenda")
36 (declare-function org-agenda-maybe-redo "org-agenda" ())
37 (declare-function org-at-heading-p "org" (&optional ignored))
38 (declare-function org-at-table.el-p "org" ())
39 (declare-function org-element-at-point "org-element" ())
40 (declare-function org-element-context "org-element" (&optional element))
41 (declare-function org-element-lineage "org-element" (blob &optional types with-self))
42 (declare-function org-element-type "org-element" (element))
43 (declare-function org-element-property "org-element" (property element))
44 (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
45 (declare-function org-invisible-p "org" (&optional pos))
46 (declare-function org-link-display-format "org" (s))
47 (declare-function org-link-set-parameters "org" (type &rest rest))
48 (declare-function org-log-into-drawer "org" ())
49 (declare-function org-reduced-level "org" (l))
50 (declare-function org-show-context "org" (&optional key))
51 (declare-function org-table-end "org-table" (&optional table-type))
52 (declare-function outline-next-heading "outline" ())
53 (declare-function speedbar-line-directory "speedbar" (&optional depth))
54 (declare-function table--at-cell-p "table" (position &optional object at-column))
56 (defvar calendar-mode-map)
57 (defvar org-complex-heading-regexp)
58 (defvar org-agenda-diary-file)
59 (defvar org-table-any-border-regexp)
60 (defvar org-table-dataline-regexp)
61 (defvar org-table-tab-recognizes-table.el)
62 (defvar org-table1-hline-regexp)
65 ;;; Emacs < 25.1 compatibility
67 (when (< emacs-major-version 25)
68 (defalias 'outline-hide-entry 'hide-entry)
69 (defalias 'outline-hide-sublevels 'hide-sublevels)
70 (defalias 'outline-hide-subtree 'hide-subtree)
71 (defalias 'outline-show-branches 'show-branches)
72 (defalias 'outline-show-children 'show-children)
73 (defalias 'outline-show-entry 'show-entry)
74 (defalias 'outline-show-subtree 'show-subtree)
75 (defalias 'xref-find-definitions 'find-tag)
76 (defalias 'format-message 'format)
77 (defalias 'gui-get-selection 'x-get-selection))
79 (unless (fboundp 'directory-name-p)
80 (defun directory-name-p (name)
81 "Return non-nil if NAME ends with a directory separator character."
82 (let ((len (length name))
83 (lastc ?.))
84 (if (> len 0)
85 (setq lastc (aref name (1- len))))
86 (or (= lastc ?/)
87 (and (memq system-type '(windows-nt ms-dos))
88 (= lastc ?\\))))))
90 ;; `string-collate-lessp' is new in Emacs 25.
91 (if (fboundp 'string-collate-lessp)
92 (defalias 'org-string-collate-lessp
93 'string-collate-lessp)
94 (defun org-string-collate-lessp (s1 s2 &rest _)
95 "Return non-nil if STRING1 is less than STRING2 in lexicographic order.
96 Case is significant."
97 (string< s1 s2)))
100 ;;; Obsolete aliases (remove them after the next major release).
102 ;;;; XEmacs compatibility, now removed.
103 (define-obsolete-function-alias 'org-activate-mark 'activate-mark)
104 (define-obsolete-function-alias 'org-add-hook 'add-hook "Org 9.0")
105 (define-obsolete-function-alias 'org-bound-and-true-p 'bound-and-true-p "Org 9.0")
106 (define-obsolete-function-alias 'org-decompose-region 'decompose-region "Org 9.0")
107 (define-obsolete-function-alias 'org-defvaralias 'defvaralias "Org 9.0")
108 (define-obsolete-function-alias 'org-detach-overlay 'delete-overlay "Org 9.0")
109 (define-obsolete-function-alias 'org-file-equal-p 'file-equal-p "Org 9.0")
110 (define-obsolete-function-alias 'org-float-time 'float-time "Org 9.0")
111 (define-obsolete-function-alias 'org-indent-line-to 'indent-line-to "Org 9.0")
112 (define-obsolete-function-alias 'org-indent-to-column 'indent-to-column "Org 9.0")
113 (define-obsolete-function-alias 'org-looking-at-p 'looking-at-p "Org 9.0")
114 (define-obsolete-function-alias 'org-looking-back 'looking-back "Org 9.0")
115 (define-obsolete-function-alias 'org-match-string-no-properties 'match-string-no-properties "Org 9.0")
116 (define-obsolete-function-alias 'org-propertize 'propertize "Org 9.0")
117 (define-obsolete-function-alias 'org-select-frame-set-input-focus 'select-frame-set-input-focus "Org 9.0")
119 (defmacro org-re (s)
120 "Replace posix classes in regular expression S."
121 (declare (debug (form))
122 (obsolete "you can safely remove it." "Org 9.0"))
125 ;;;; Functions from cl-lib that Org used to have its own implementation of.
126 (define-obsolete-function-alias 'org-count 'cl-count "Org 9.0")
127 (define-obsolete-function-alias 'org-every 'cl-every "Org 9.0")
128 (define-obsolete-function-alias 'org-find-if 'cl-find-if "Org 9.0")
129 (define-obsolete-function-alias 'org-reduce 'cl-reduce "Org 9.0")
130 (define-obsolete-function-alias 'org-remove-if 'cl-remove-if "Org 9.0")
131 (define-obsolete-function-alias 'org-remove-if-not 'cl-remove-if-not "Org 9.0")
132 (define-obsolete-function-alias 'org-some 'cl-some "Org 9.0")
133 (define-obsolete-function-alias 'org-floor* 'cl-floor "Org 9.0")
135 (defun org-sublist (list start end)
136 "Return a section of LIST, from START to END.
137 Counting starts at 1."
138 (cl-subseq list (1- start) end))
139 (make-obsolete 'org-sublist
140 "use cl-subseq (note the 0-based counting)."
141 "Org 9.0")
144 ;;;; Functions available since Emacs 24.3
145 (define-obsolete-function-alias 'org-buffer-narrowed-p 'buffer-narrowed-p "Org 9.0")
146 (define-obsolete-function-alias 'org-called-interactively-p 'called-interactively-p "Org 9.0")
147 (define-obsolete-function-alias 'org-char-to-string 'char-to-string "Org 9.0")
148 (define-obsolete-function-alias 'org-delete-directory 'delete-directory "Org 9.0")
149 (define-obsolete-function-alias 'org-format-seconds 'format-seconds "Org 9.0")
150 (define-obsolete-function-alias 'org-link-escape-browser 'url-encode-url "Org 9.0")
151 (define-obsolete-function-alias 'org-no-warnings 'with-no-warnings "Org 9.0")
152 (define-obsolete-function-alias 'org-number-sequence 'number-sequence "Org 9.0")
153 (define-obsolete-function-alias 'org-pop-to-buffer-same-window 'pop-to-buffer-same-window "Org 9.0")
154 (define-obsolete-function-alias 'org-string-match-p 'string-match-p "Org 9.0")
156 ;;;; Functions and variables from previous releases now obsolete.
157 (define-obsolete-function-alias 'org-element-remove-indentation
158 'org-remove-indentation "Org 9.0")
159 (define-obsolete-variable-alias 'org-latex-create-formula-image-program
160 'org-preview-latex-default-process "Org 9.0")
161 (define-obsolete-variable-alias 'org-latex-preview-ltxpng-directory
162 'org-preview-latex-image-directory "Org 9.0")
163 (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
164 (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 9.0")
165 (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp "Org 8.3")
166 (define-obsolete-function-alias 'org-image-file-name-regexp
167 'image-file-name-regexp "Org 9.0")
168 (define-obsolete-function-alias 'org-completing-read-no-i
169 'completing-read "Org 9.0")
170 (define-obsolete-function-alias 'org-icompleting-read
171 'completing-read "Org 9.0")
172 (define-obsolete-function-alias 'org-iread-file-name 'read-file-name "Org 9.0")
173 (define-obsolete-function-alias 'org-days-to-time
174 'org-time-stamp-to-now "Org 8.2")
175 (define-obsolete-variable-alias 'org-agenda-ignore-drawer-properties
176 'org-agenda-ignore-properties "Org 9.0")
177 (define-obsolete-function-alias 'org-preview-latex-fragment
178 'org-toggle-latex-fragment "Org 8.3")
179 (define-obsolete-function-alias 'org-export-get-genealogy
180 'org-element-lineage "Org 9.0")
181 (define-obsolete-variable-alias 'org-latex-with-hyperref
182 'org-latex-hyperref-template "Org 9.0")
183 (define-obsolete-variable-alias 'hfy-optimisations 'hfy-optimizations "Org 9.0")
184 (define-obsolete-variable-alias 'org-export-htmlized-org-css-url
185 'org-org-htmlized-css-url "Org 8.2")
186 (define-obsolete-function-alias 'org-list-parse-list 'org-list-to-lisp "Org 9.0")
187 (define-obsolete-function-alias 'org-agenda-todayp
188 'org-agenda-today-p "Org 9.0")
189 (define-obsolete-function-alias 'org-babel-examplize-region
190 'org-babel-examplify-region "Org 9.0")
191 (define-obsolete-variable-alias 'org-babel-capitalize-example-region-markers
192 'org-babel-uppercase-example-markers "Org 9.1")
194 (define-obsolete-function-alias 'org-babel-trim 'org-trim "Org 9.0")
195 (define-obsolete-variable-alias 'org-html-style 'org-html-head "24.4")
196 (define-obsolete-function-alias 'org-insert-columns-dblock
197 'org-columns-insert-dblock "Org 9.0")
198 (define-obsolete-variable-alias 'org-export-babel-evaluate
199 'org-export-use-babel "Org 9.1")
200 (define-obsolete-function-alias 'org-activate-bracket-links
201 'org-activate-links "Org 9.0")
202 (define-obsolete-function-alias 'org-activate-plain-links 'ignore "Org 9.0")
203 (define-obsolete-function-alias 'org-activate-angle-links 'ignore "Org 9.0")
205 (defun org-in-fixed-width-region-p ()
206 "Non-nil if point in a fixed-width region."
207 (save-match-data
208 (eq 'fixed-width (org-element-type (org-element-at-point)))))
209 (make-obsolete 'org-in-fixed-width-region-p
210 "use `org-element' library"
211 "Org 9.0")
213 (defun org-compatible-face (inherits specs)
214 "Make a compatible face specification.
215 If INHERITS is an existing face and if the Emacs version supports
216 it, just inherit the face. If INHERITS is not given and SPECS
217 is, use SPECS to define the face."
218 (declare (indent 1))
219 (if (facep inherits)
220 (list (list t :inherit inherits))
221 specs))
222 (make-obsolete 'org-compatible-face "you can remove it." "Org 9.0")
224 (defun org-add-link-type (type &optional follow export)
225 "Add a new TYPE link.
226 FOLLOW and EXPORT are two functions.
228 FOLLOW should take the link path as the single argument and do whatever
229 is necessary to follow the link, for example find a file or display
230 a mail message.
232 EXPORT should format the link path for export to one of the export formats.
233 It should be a function accepting three arguments:
235 path the path of the link, the text after the prefix (like \"http:\")
236 desc the description of the link, if any
237 format the export format, a symbol like `html' or `latex' or `ascii'.
239 The function may use the FORMAT information to return different values
240 depending on the format. The return value will be put literally into
241 the exported file. If the return value is nil, this means Org should
242 do what it normally does with links which do not have EXPORT defined.
244 Org mode has a built-in default for exporting links. If you are happy with
245 this default, there is no need to define an export function for the link
246 type. For a simple example of an export function, see `org-bbdb.el'.
248 If TYPE already exists, update it with the arguments.
249 See `org-link-parameters' for documentation on the other parameters."
250 (org-link-set-parameters type :follow follow :export export)
251 (message "Created %s link." type))
253 (make-obsolete 'org-add-link-type "use `org-link-set-parameters' instead." "Org 9.0")
255 (defun org-table-recognize-table.el ()
256 "If there is a table.el table nearby, recognize it and move into it."
257 (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
258 (beginning-of-line)
259 (unless (or (looking-at org-table-dataline-regexp)
260 (not (looking-at org-table1-hline-regexp)))
261 (forward-line)
262 (when (looking-at org-table-any-border-regexp)
263 (forward-line -2)))
264 (if (re-search-forward "|" (org-table-end t) t)
265 (progn
266 (require 'table)
267 (if (table--at-cell-p (point)) t
268 (message "recognizing table.el table...")
269 (table-recognize-table)
270 (message "recognizing table.el table...done")))
271 (error "This should not happen"))))
273 ;; Not used by Org core since commit 6d1e3082, Feb 2010.
274 (make-obsolete 'org-table-recognize-table.el
275 "please notify the org mailing list if you use this function."
276 "Org 9.0")
278 (defun org-remove-angle-brackets (s)
279 (org-unbracket-string "<" ">" s))
280 (make-obsolete 'org-remove-angle-brackets 'org-unbracket-string "Org 9.0")
282 (defun org-remove-double-quotes (s)
283 (org-unbracket-string "\"" "\"" s))
284 (make-obsolete 'org-remove-double-quotes 'org-unbracket-string "Org 9.0")
286 (defcustom org-publish-sitemap-file-entry-format "%t"
287 "Format string for site-map file entry.
288 You could use brackets to delimit on what part the link will be.
290 %t is the title.
291 %a is the author.
292 %d is the date formatted using `org-publish-sitemap-date-format'."
293 :group 'org-export-publish
294 :type 'string)
295 (make-obsolete-variable
296 'org-publish-sitemap-file-entry-format
297 "set `:sitemap-format-entry' in `org-publish-project-alist' instead."
298 "Org 9.1")
300 (defvar org-agenda-skip-regexp)
301 (defun org-agenda-skip-entry-when-regexp-matches ()
302 "Check if the current entry contains match for `org-agenda-skip-regexp'.
303 If yes, it returns the end position of this entry, causing agenda commands
304 to skip the entry but continuing the search in the subtree. This is a
305 function that can be put into `org-agenda-skip-function' for the duration
306 of a command."
307 (declare (obsolete "use `org-agenda-skip-if' instead." "Org 9.1"))
308 (let ((end (save-excursion (org-end-of-subtree t)))
309 skip)
310 (save-excursion
311 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
312 (and skip end)))
314 (defun org-agenda-skip-subtree-when-regexp-matches ()
315 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
316 If yes, it returns the end position of this tree, causing agenda commands
317 to skip this subtree. This is a function that can be put into
318 `org-agenda-skip-function' for the duration of a command."
319 (declare (obsolete "use `org-agenda-skip-if' instead." "Org 9.1"))
320 (let ((end (save-excursion (org-end-of-subtree t)))
321 skip)
322 (save-excursion
323 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
324 (and skip end)))
326 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
327 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
328 If yes, it returns the end position of the current entry (NOT the tree),
329 causing agenda commands to skip the entry but continuing the search in
330 the subtree. This is a function that can be put into
331 `org-agenda-skip-function' for the duration of a command. An important
332 use of this function is for the stuck project list."
333 (declare (obsolete "use `org-agenda-skip-if' instead." "Org 9.1"))
334 (let ((end (save-excursion (org-end-of-subtree t)))
335 (entry-end (save-excursion (outline-next-heading) (1- (point))))
336 skip)
337 (save-excursion
338 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
339 (and skip entry-end)))
341 (define-obsolete-function-alias 'org-minutes-to-clocksum-string
342 'org-duration-from-minutes "Org 9.1")
344 (define-obsolete-function-alias 'org-hh:mm-string-to-minutes
345 'org-duration-to-minutes "Org 9.1")
347 (define-obsolete-function-alias 'org-duration-string-to-minutes
348 'org-duration-to-minutes "Org 9.1")
350 (make-obsolete-variable 'org-time-clocksum-format
351 "set `org-duration-format' instead." "Org 9.1")
353 (make-obsolete-variable 'org-time-clocksum-use-fractional
354 "set `org-duration-format' instead." "Org 9.1")
356 (make-obsolete-variable 'org-time-clocksum-fractional-format
357 "set `org-duration-format' instead." "Org 9.1")
359 (make-obsolete-variable 'org-time-clocksum-use-effort-durations
360 "set `org-duration-units' instead." "Org 9.1")
362 (define-obsolete-function-alias 'org-babel-number-p
363 'org-babel--string-to-number "Org 9.0")
365 (define-obsolete-variable-alias 'org-usenet-links-prefer-google
366 'org-gnus-prefer-web-links "Org 9.1")
368 (define-obsolete-variable-alias 'org-texinfo-def-table-markup
369 'org-texinfo-table-default-markup "Org 9.1")
371 ;; The function was made obsolete by commit 65399674d5 of 2013-02-22.
372 ;; This make-obsolete call was added 2016-09-01.
373 (make-obsolete 'org-capture-import-remember-templates
374 "use the `org-capture-templates' variable instead."
375 "Org 9.0")
377 (defun org-show-block-all ()
378 "Unfold all blocks in the current buffer."
379 (interactive)
380 (remove-overlays nil nil 'invisible 'org-hide-block))
382 (make-obsolete 'org-show-block-all
383 "use `org-show-all' instead."
384 "Org 9.2")
386 (define-obsolete-function-alias 'org-get-tags-at 'org-get-tags "Org 9.2")
388 (defun org-get-local-tags ()
389 "Get a list of tags defined in the current headline."
390 (declare (obsolete "use `org-get-tags' instead." "Org 9.2"))
391 (org-get-tags nil 'local))
393 (defun org-get-local-tags-at (&optional pos)
394 "Get a list of tags defined in the current headline."
395 (declare (obsolete "use `org-get-tags' instead." "Org 9.2"))
396 (org-get-tags pos 'local))
398 (defun org-get-tags-string ()
399 "Get the TAGS string in the current headline."
400 (declare (obsolete "use `org-make-tag-string' instead." "Org 9.2"))
401 (org-make-tag-string (org-get-tags nil t)))
403 (define-obsolete-function-alias 'org-set-tags-to 'org-set-tags "Org 9.2")
405 (defun org-align-all-tags ()
406 "Align the tags in all headings."
407 (declare (obsolete "use `org-align-tags' instead." "Org 9.2"))
408 (org-align-tags t))
410 ;;;; Obsolete link types
412 (eval-after-load 'org
413 '(progn
414 (org-link-set-parameters "file+emacs") ;since Org 9.0
415 (org-link-set-parameters "file+sys"))) ;since Org 9.0
419 ;;; Miscellaneous functions
421 (defun org-version-check (version feature level)
422 (let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
423 (v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
424 (rmaj (or (nth 0 v1) 99))
425 (rmin (or (nth 1 v1) 99))
426 (rbld (or (nth 2 v1) 99))
427 (maj (or (nth 0 v2) 0))
428 (min (or (nth 1 v2) 0))
429 (bld (or (nth 2 v2) 0)))
430 (if (or (< maj rmaj)
431 (and (= maj rmaj)
432 (< min rmin))
433 (and (= maj rmaj)
434 (= min rmin)
435 (< bld rbld)))
436 (if (eq level :predicate)
437 ;; just return if we have the version
439 (let ((msg (format "Emacs %s or greater is recommended for %s"
440 version feature)))
441 (display-warning 'org msg level)
443 t)))
445 (defun org-get-x-clipboard (value)
446 "Get the value of the X or Windows clipboard."
447 (cond ((and (eq window-system 'x)
448 (fboundp 'gui-get-selection)) ;Silence byte-compiler.
449 (org-no-properties
450 (ignore-errors
451 (or (gui-get-selection value 'UTF8_STRING)
452 (gui-get-selection value 'COMPOUND_TEXT)
453 (gui-get-selection value 'STRING)
454 (gui-get-selection value 'TEXT)))))
455 ((and (eq window-system 'w32) (fboundp 'w32-get-clipboard-data))
456 (w32-get-clipboard-data))))
458 (defun org-fit-window-to-buffer (&optional window max-height min-height
459 shrink-only)
460 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
461 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
462 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
463 `shrink-window-if-larger-than-buffer' instead, the height limit is
464 ignored in this case."
465 (cond ((if (fboundp 'window-full-width-p)
466 (not (window-full-width-p window))
467 ;; do nothing if another window would suffer
468 (> (frame-width) (window-width window))))
469 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
470 (fit-window-to-buffer window max-height min-height))
471 ((fboundp 'shrink-window-if-larger-than-buffer)
472 (shrink-window-if-larger-than-buffer window)))
473 (or window (selected-window)))
475 ;; `set-transient-map' is only in Emacs >= 24.4
476 (defalias 'org-set-transient-map
477 (if (fboundp 'set-transient-map)
478 'set-transient-map
479 'set-temporary-overlay-map))
482 ;;; Region compatibility
484 (defvar org-ignore-region nil
485 "Non-nil means temporarily disable the active region.")
487 (defun org-region-active-p ()
488 "Non-nil when the region active.
489 Unlike to `use-region-p', this function also checks
490 `org-ignore-region'."
491 (and (not org-ignore-region) (use-region-p)))
493 (defun org-cursor-to-region-beginning ()
494 (when (and (org-region-active-p)
495 (> (point) (region-beginning)))
496 (exchange-point-and-mark)))
499 ;;; Invisibility compatibility
501 (defun org-remove-from-invisibility-spec (arg)
502 "Remove elements from `buffer-invisibility-spec'."
503 (if (fboundp 'remove-from-invisibility-spec)
504 (remove-from-invisibility-spec arg)
505 (if (consp buffer-invisibility-spec)
506 (setq buffer-invisibility-spec
507 (delete arg buffer-invisibility-spec)))))
509 (defun org-in-invisibility-spec-p (arg)
510 "Is ARG a member of `buffer-invisibility-spec'?"
511 (if (consp buffer-invisibility-spec)
512 (member arg buffer-invisibility-spec)))
514 (defun org-move-to-column (column &optional force _buffer)
515 "Move to column COLUMN.
516 Pass COLUMN and FORCE to `move-to-column'."
517 (let ((buffer-invisibility-spec
518 (if (listp buffer-invisibility-spec)
519 (remove '(org-filtered) buffer-invisibility-spec)
520 buffer-invisibility-spec)))
521 (move-to-column column force)))
523 (defmacro org-find-library-dir (library)
524 `(file-name-directory (or (locate-library ,library) "")))
526 (defun org-count-lines (s)
527 "How many lines in string S?"
528 (let ((start 0) (n 1))
529 (while (string-match "\n" s start)
530 (setq start (match-end 0) n (1+ n)))
531 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
532 (setq n (1- n)))
535 (defun org-kill-new (string &rest args)
536 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
537 string)
538 (apply 'kill-new string args))
540 ;; `font-lock-ensure' is only available from 24.4.50 on
541 (defalias 'org-font-lock-ensure
542 (if (fboundp 'font-lock-ensure)
543 #'font-lock-ensure
544 (lambda (&optional _beg _end)
545 (with-no-warnings (font-lock-fontify-buffer)))))
547 ;; `file-local-name' was added in Emacs 26.1.
548 (defalias 'org-babel-local-file-name
549 (if (fboundp 'file-local-name)
550 'file-local-name
551 (lambda (file)
552 "Return the local name component of FILE."
553 (or (file-remote-p file 'localname) file))))
555 (defmacro org-no-popups (&rest body)
556 "Suppress popup windows.
557 Let-bind some variables to nil around BODY to achieve the desired
558 effect, which variables to use depends on the Emacs version."
559 (if (org-version-check "24.2.50" "" :predicate)
560 `(let (pop-up-frames display-buffer-alist)
561 ,@body)
562 `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
563 ,@body)))
565 ;;;###autoload
566 (defmacro org-check-version ()
567 "Try very hard to provide sensible version strings."
568 (let* ((org-dir (org-find-library-dir "org"))
569 (org-version.el (concat org-dir "org-version.el"))
570 (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
571 (if (require 'org-version org-version.el 'noerror)
572 '(progn
573 (autoload 'org-release "org-version.el")
574 (autoload 'org-git-version "org-version.el"))
575 (if (require 'org-fixup org-fixup.el 'noerror)
576 '(org-fixup)
577 ;; provide fallback definitions and complain
578 (warn "Could not define org version correctly. Check installation!")
579 '(progn
580 (defun org-release () "N/A")
581 (defun org-git-version () "N/A !!check installation!!"))))))
583 (defmacro org-with-silent-modifications (&rest body)
584 (if (fboundp 'with-silent-modifications)
585 `(with-silent-modifications ,@body)
586 `(org-unmodified ,@body)))
587 (def-edebug-spec org-with-silent-modifications (body))
590 ;;; Functions for Emacs < 24.4 compatibility
592 (defun org-define-error (name message)
593 "Define NAME as a new error signal.
594 MESSAGE is a string that will be output to the echo area if such
595 an error is signaled without being caught by a `condition-case'.
596 Implements `define-error' for older emacsen."
597 (if (fboundp 'define-error) (define-error name message)
598 (put name 'error-conditions
599 (copy-sequence (cons name (get 'error 'error-conditions))))))
601 (unless (fboundp 'string-suffix-p)
602 ;; From Emacs subr.el.
603 (defun string-suffix-p (suffix string &optional ignore-case)
604 "Return non-nil if SUFFIX is a suffix of STRING.
605 If IGNORE-CASE is non-nil, the comparison is done without paying
606 attention to case differences."
607 (let ((start-pos (- (length string) (length suffix))))
608 (and (>= start-pos 0)
609 (eq t (compare-strings suffix nil nil
610 string start-pos nil ignore-case))))))
613 ;;; Integration with and fixes for other packages
615 (defgroup org-imenu-and-speedbar nil
616 "Options concerning imenu and speedbar in Org mode."
617 :tag "Org Imenu and Speedbar"
618 :group 'org-structure)
620 (defcustom org-imenu-depth 2
621 "The maximum level for Imenu access to Org headlines.
622 This also applied for speedbar access."
623 :group 'org-imenu-and-speedbar
624 :type 'integer)
626 ;;;; Imenu
628 (defvar-local org-imenu-markers nil
629 "All markers currently used by Imenu.")
631 (defun org-imenu-new-marker (&optional pos)
632 "Return a new marker for use by Imenu, and remember the marker."
633 (let ((m (make-marker)))
634 (move-marker m (or pos (point)))
635 (push m org-imenu-markers)
638 (defun org-imenu-get-tree ()
639 "Produce the index for Imenu."
640 (dolist (x org-imenu-markers) (move-marker x nil))
641 (setq org-imenu-markers nil)
642 (let* ((case-fold-search nil)
643 (n org-imenu-depth)
644 (re (concat "^" (org-get-limited-outline-regexp)))
645 (subs (make-vector (1+ n) nil))
646 (last-level 0)
647 m level head0 head)
648 (org-with-wide-buffer
649 (goto-char (point-max))
650 (while (re-search-backward re nil t)
651 (setq level (org-reduced-level (funcall outline-level)))
652 (when (and (<= level n)
653 (looking-at org-complex-heading-regexp)
654 (setq head0 (match-string-no-properties 4)))
655 (setq head (org-link-display-format head0)
656 m (org-imenu-new-marker))
657 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
658 (if (>= level last-level)
659 (push (cons head m) (aref subs level))
660 (push (cons head (aref subs (1+ level))) (aref subs level))
661 (cl-loop for i from (1+ level) to n do (aset subs i nil)))
662 (setq last-level level))))
663 (aref subs 1)))
665 (eval-after-load "imenu"
666 '(progn
667 (add-hook 'imenu-after-jump-hook
668 (lambda ()
669 (when (derived-mode-p 'org-mode)
670 (org-show-context 'org-goto))))))
672 ;;;; Speedbar
674 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
675 "Overlay marking the agenda restriction line in speedbar.")
676 (overlay-put org-speedbar-restriction-lock-overlay
677 'face 'org-agenda-restriction-lock)
678 (overlay-put org-speedbar-restriction-lock-overlay
679 'help-echo "Agendas are currently limited to this item.")
680 (delete-overlay org-speedbar-restriction-lock-overlay)
682 (defun org-speedbar-set-agenda-restriction ()
683 "Restrict future agenda commands to the location at point in speedbar.
684 To get rid of the restriction, use `\\[org-agenda-remove-restriction-lock]'."
685 (interactive)
686 (require 'org-agenda)
687 (let (p m tp np dir txt)
688 (cond
689 ((setq p (text-property-any (point-at-bol) (point-at-eol)
690 'org-imenu t))
691 (setq m (get-text-property p 'org-imenu-marker))
692 (with-current-buffer (marker-buffer m)
693 (goto-char m)
694 (org-agenda-set-restriction-lock 'subtree)))
695 ((setq p (text-property-any (point-at-bol) (point-at-eol)
696 'speedbar-function 'speedbar-find-file))
697 (setq tp (previous-single-property-change
698 (1+ p) 'speedbar-function)
699 np (next-single-property-change
700 tp 'speedbar-function)
701 dir (speedbar-line-directory)
702 txt (buffer-substring-no-properties (or tp (point-min))
703 (or np (point-max))))
704 (with-current-buffer (find-file-noselect
705 (let ((default-directory dir))
706 (expand-file-name txt)))
707 (unless (derived-mode-p 'org-mode)
708 (user-error "Cannot restrict to non-Org mode file"))
709 (org-agenda-set-restriction-lock 'file)))
710 (t (user-error "Don't know how to restrict Org mode agenda")))
711 (move-overlay org-speedbar-restriction-lock-overlay
712 (point-at-bol) (point-at-eol))
713 (setq current-prefix-arg nil)
714 (org-agenda-maybe-redo)))
716 (defvar speedbar-file-key-map)
717 (declare-function speedbar-add-supported-extension "speedbar" (extension))
718 (eval-after-load "speedbar"
719 '(progn
720 (speedbar-add-supported-extension ".org")
721 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
722 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
723 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
724 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
725 (add-hook 'speedbar-visiting-tag-hook
726 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
728 ;;;; Flyspell
730 (defun org--flyspell-object-check-p (element)
731 "Non-nil when Flyspell can check object at point.
732 ELEMENT is the element at point."
733 (let ((object (save-excursion
734 (when (looking-at-p "\\>") (backward-char))
735 (org-element-context element))))
736 (cl-case (org-element-type object)
737 ;; Prevent checks in links due to keybinding conflict with
738 ;; Flyspell.
739 ((code entity export-snippet inline-babel-call
740 inline-src-block line-break latex-fragment link macro
741 statistics-cookie target timestamp verbatim)
742 nil)
743 (footnote-reference
744 ;; Only in inline footnotes, within the definition.
745 (and (eq (org-element-property :type object) 'inline)
746 (< (save-excursion
747 (goto-char (org-element-property :begin object))
748 (search-forward ":" nil t 2))
749 (point))))
750 (otherwise t))))
752 (defun org-mode-flyspell-verify ()
753 "Function used for `flyspell-generic-check-word-predicate'."
754 (if (org-at-heading-p)
755 ;; At a headline or an inlinetask, check title only. This is
756 ;; faster than relying on `org-element-at-point'.
757 (and (save-excursion (beginning-of-line)
758 (and (let ((case-fold-search t))
759 (not (looking-at-p "\\*+ END[ \t]*$")))
760 (let ((case-fold-search nil))
761 (looking-at org-complex-heading-regexp))))
762 (match-beginning 4)
763 (>= (point) (match-beginning 4))
764 (or (not (match-beginning 5))
765 (< (point) (match-beginning 5))))
766 (let* ((element (org-element-at-point))
767 (post-affiliated (org-element-property :post-affiliated element)))
768 (cond
769 ;; Ignore checks in all affiliated keywords but captions.
770 ((< (point) post-affiliated)
771 (and (save-excursion
772 (beginning-of-line)
773 (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
774 (> (point) (match-end 0))
775 (org--flyspell-object-check-p element)))
776 ;; Ignore checks in LOGBOOK (or equivalent) drawer.
777 ((let ((log (org-log-into-drawer)))
778 (and log
779 (let ((drawer (org-element-lineage element '(drawer))))
780 (and drawer
781 (eq (compare-strings
782 log nil nil
783 (org-element-property :drawer-name drawer) nil nil t)
784 t)))))
785 nil)
787 (cl-case (org-element-type element)
788 ((comment quote-section) t)
789 (comment-block
790 ;; Allow checks between block markers, not on them.
791 (and (> (line-beginning-position) post-affiliated)
792 (save-excursion
793 (end-of-line)
794 (skip-chars-forward " \r\t\n")
795 (< (point) (org-element-property :end element)))))
796 ;; Arbitrary list of keywords where checks are meaningful.
797 ;; Make sure point is on the value part of the element.
798 (keyword
799 (and (member (org-element-property :key element)
800 '("DESCRIPTION" "TITLE"))
801 (save-excursion
802 (search-backward ":" (line-beginning-position) t))))
803 ;; Check is globally allowed in paragraphs verse blocks and
804 ;; table rows (after affiliated keywords) but some objects
805 ;; must not be affected.
806 ((paragraph table-row verse-block)
807 (let ((cbeg (org-element-property :contents-begin element))
808 (cend (org-element-property :contents-end element)))
809 (and cbeg (>= (point) cbeg) (< (point) cend)
810 (org--flyspell-object-check-p element))))))))))
811 (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
813 (defun org-remove-flyspell-overlays-in (beg end)
814 "Remove flyspell overlays in region."
815 (and (bound-and-true-p flyspell-mode)
816 (fboundp 'flyspell-delete-region-overlays)
817 (flyspell-delete-region-overlays beg end)))
819 (defvar flyspell-delayed-commands)
820 (eval-after-load "flyspell"
821 '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
823 ;;;; Bookmark
825 (defun org-bookmark-jump-unhide ()
826 "Unhide the current position, to show the bookmark location."
827 (and (derived-mode-p 'org-mode)
828 (or (org-invisible-p)
829 (save-excursion (goto-char (max (point-min) (1- (point))))
830 (org-invisible-p)))
831 (org-show-context 'bookmark-jump)))
833 ;; Make `bookmark-jump' shows the jump location if it was hidden.
834 (eval-after-load "bookmark"
835 '(if (boundp 'bookmark-after-jump-hook)
836 ;; We can use the hook
837 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
838 ;; Hook not available, use advice
839 (defadvice bookmark-jump (after org-make-visible activate)
840 "Make the position visible."
841 (org-bookmark-jump-unhide))))
843 ;;;; Calendar
845 (defcustom org-calendar-to-agenda-key 'default
846 "Key to be installed in `calendar-mode-map' for switching to the agenda.
848 The command `org-calendar-goto-agenda' will be bound to this key.
850 When set to `default', bind the function to `c', but only if it is
851 available in the Calendar keymap. This is the default choice because
852 `c' can then be used to switch back and forth between agenda and calendar.
854 When nil, `org-calendar-goto-agenda' is not bound to any key."
855 :group 'org-agenda
856 :type '(choice
857 (const :tag "Bind to `c' if available" default)
858 (key-sequence :tag "Other binding")
859 (const :tag "No binding" nil))
860 :safe (lambda (v) (or (symbolp v) (stringp v)))
861 :package-version '(Org . "9.2"))
863 (defcustom org-calendar-insert-diary-entry-key [?i]
864 "The key to be installed in `calendar-mode-map' for adding diary entries.
865 This option is irrelevant until `org-agenda-diary-file' has been configured
866 to point to an Org file. When that is the case, the command
867 `org-agenda-diary-entry' will be bound to the key given here, by default
868 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
869 if you want to continue doing this, you need to change this to a different
870 key."
871 :group 'org-agenda
872 :type 'sexp)
874 (defun org--setup-calendar-bindings ()
875 "Bind Org functions in Calendar keymap."
876 (pcase org-calendar-to-agenda-key
877 (`nil nil)
878 ((and key (pred stringp))
879 (local-set-key (kbd key) #'org-calendar-goto-agenda))
880 ((guard (not (lookup-key calendar-mode-map "c")))
881 (local-set-key "c" #'org-calendar-goto-agenda))
882 (_ nil))
883 (unless (eq org-agenda-diary-file 'diary-file)
884 (local-set-key org-calendar-insert-diary-entry-key
885 #'org-agenda-diary-entry)))
887 (eval-after-load "calendar"
888 '(add-hook 'calendar-mode-hook #'org--setup-calendar-bindings))
890 ;;;; Saveplace
892 ;; Make sure saveplace shows the location if it was hidden
893 (eval-after-load "saveplace"
894 '(defadvice save-place-find-file-hook (after org-make-visible activate)
895 "Make the position visible."
896 (org-bookmark-jump-unhide)))
898 ;;;; Ecb
900 ;; Make sure ecb shows the location if it was hidden
901 (eval-after-load "ecb"
902 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
903 "Make hierarchy visible when jumping into location from ECB tree buffer."
904 (when (derived-mode-p 'org-mode)
905 (org-show-context))))
907 ;;;; Simple
909 (defun org-mark-jump-unhide ()
910 "Make the point visible with `org-show-context' after jumping to the mark."
911 (when (and (derived-mode-p 'org-mode)
912 (org-invisible-p))
913 (org-show-context 'mark-goto)))
915 (eval-after-load "simple"
916 '(defadvice pop-to-mark-command (after org-make-visible activate)
917 "Make the point visible with `org-show-context'."
918 (org-mark-jump-unhide)))
920 (eval-after-load "simple"
921 '(defadvice exchange-point-and-mark (after org-make-visible activate)
922 "Make the point visible with `org-show-context'."
923 (org-mark-jump-unhide)))
925 (eval-after-load "simple"
926 '(defadvice pop-global-mark (after org-make-visible activate)
927 "Make the point visible with `org-show-context'."
928 (org-mark-jump-unhide)))
930 ;;;; Session
932 ;; Make "session.el" ignore our circular variable.
933 (defvar session-globals-exclude)
934 (eval-after-load "session"
935 '(add-to-list 'session-globals-exclude 'org-mark-ring))
937 (provide 'org-compat)
939 ;;; org-compat.el ends here