test files: use cl- prefixed functions, and require cl-lib
[org-mode/org-tableheadings.git] / lisp / org-compat.el
blob2f06f358717658e1f26a044804259ad39b8e16c3
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-agenda-remove-restriction-lock "org-agenda" (&optional noupdate))
38 (declare-function org-align-tags "org" (&optional all))
39 (declare-function org-at-heading-p "org" (&optional ignored))
40 (declare-function org-at-table.el-p "org" ())
41 (declare-function org-element-at-point "org-element" ())
42 (declare-function org-element-context "org-element" (&optional element))
43 (declare-function org-element-lineage "org-element" (blob &optional types with-self))
44 (declare-function org-element-type "org-element" (element))
45 (declare-function org-element-property "org-element" (property element))
46 (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
47 (declare-function org-get-tags "org" (&optional pos local))
48 (declare-function org-link-display-format "org" (s))
49 (declare-function org-link-set-parameters "org" (type &rest rest))
50 (declare-function org-log-into-drawer "org" ())
51 (declare-function org-make-tag-string "org" (tags))
52 (declare-function org-reduced-level "org" (l))
53 (declare-function org-show-context "org" (&optional key))
54 (declare-function org-table-end "org-table" (&optional table-type))
55 (declare-function outline-next-heading "outline" ())
56 (declare-function speedbar-line-directory "speedbar" (&optional depth))
57 (declare-function table--at-cell-p "table" (position &optional object at-column))
59 (defvar calendar-mode-map)
60 (defvar org-complex-heading-regexp)
61 (defvar org-agenda-diary-file)
62 (defvar org-agenda-overriding-restriction)
63 (defvar org-agenda-restriction-lock-overlay)
64 (defvar org-table-any-border-regexp)
65 (defvar org-table-dataline-regexp)
66 (defvar org-table-tab-recognizes-table.el)
67 (defvar org-table1-hline-regexp)
70 ;;; Emacs < 26.1 compatibility
72 (if (fboundp 'line-number-display-width)
73 (defalias 'org-line-number-display-width 'line-number-display-width)
74 (defun org-line-number-display-width (&rest _) 0))
77 ;;; Emacs < 25.1 compatibility
79 (when (< emacs-major-version 25)
80 (defalias 'outline-hide-entry 'hide-entry)
81 (defalias 'outline-hide-sublevels 'hide-sublevels)
82 (defalias 'outline-hide-subtree 'hide-subtree)
83 (defalias 'outline-show-branches 'show-branches)
84 (defalias 'outline-show-children 'show-children)
85 (defalias 'outline-show-entry 'show-entry)
86 (defalias 'outline-show-subtree 'show-subtree)
87 (defalias 'xref-find-definitions 'find-tag)
88 (defalias 'format-message 'format)
89 (defalias 'gui-get-selection 'x-get-selection))
91 (unless (fboundp 'directory-name-p)
92 (defun directory-name-p (name)
93 "Return non-nil if NAME ends with a directory separator character."
94 (let ((len (length name))
95 (lastc ?.))
96 (if (> len 0)
97 (setq lastc (aref name (1- len))))
98 (or (= lastc ?/)
99 (and (memq system-type '(windows-nt ms-dos))
100 (= lastc ?\\))))))
102 ;; `string-collate-lessp' is new in Emacs 25.
103 (if (fboundp 'string-collate-lessp)
104 (defalias 'org-string-collate-lessp
105 'string-collate-lessp)
106 (defun org-string-collate-lessp (s1 s2 &rest _)
107 "Return non-nil if STRING1 is less than STRING2 in lexicographic order.
108 Case is significant."
109 (string< s1 s2)))
112 ;;; Obsolete aliases (remove them after the next major release).
114 ;;;; XEmacs compatibility, now removed.
115 (define-obsolete-function-alias 'org-activate-mark 'activate-mark "Org 9.0")
116 (define-obsolete-function-alias 'org-add-hook 'add-hook "Org 9.0")
117 (define-obsolete-function-alias 'org-bound-and-true-p 'bound-and-true-p "Org 9.0")
118 (define-obsolete-function-alias 'org-decompose-region 'decompose-region "Org 9.0")
119 (define-obsolete-function-alias 'org-defvaralias 'defvaralias "Org 9.0")
120 (define-obsolete-function-alias 'org-detach-overlay 'delete-overlay "Org 9.0")
121 (define-obsolete-function-alias 'org-file-equal-p 'file-equal-p "Org 9.0")
122 (define-obsolete-function-alias 'org-float-time 'float-time "Org 9.0")
123 (define-obsolete-function-alias 'org-indent-line-to 'indent-line-to "Org 9.0")
124 (define-obsolete-function-alias 'org-indent-to-column 'indent-to-column "Org 9.0")
125 (define-obsolete-function-alias 'org-looking-at-p 'looking-at-p "Org 9.0")
126 (define-obsolete-function-alias 'org-looking-back 'looking-back "Org 9.0")
127 (define-obsolete-function-alias 'org-match-string-no-properties 'match-string-no-properties "Org 9.0")
128 (define-obsolete-function-alias 'org-propertize 'propertize "Org 9.0")
129 (define-obsolete-function-alias 'org-select-frame-set-input-focus 'select-frame-set-input-focus "Org 9.0")
130 (define-obsolete-function-alias 'org-file-remote-p 'file-remote-p "Org 9.2")
132 (defmacro org-re (s)
133 "Replace posix classes in regular expression S."
134 (declare (debug (form))
135 (obsolete "you can safely remove it." "Org 9.0"))
138 ;;;; Functions from cl-lib that Org used to have its own implementation of.
139 (define-obsolete-function-alias 'org-count 'cl-count "Org 9.0")
140 (define-obsolete-function-alias 'org-every 'cl-every "Org 9.0")
141 (define-obsolete-function-alias 'org-find-if 'cl-find-if "Org 9.0")
142 (define-obsolete-function-alias 'org-reduce 'cl-reduce "Org 9.0")
143 (define-obsolete-function-alias 'org-remove-if 'cl-remove-if "Org 9.0")
144 (define-obsolete-function-alias 'org-remove-if-not 'cl-remove-if-not "Org 9.0")
145 (define-obsolete-function-alias 'org-some 'cl-some "Org 9.0")
146 (define-obsolete-function-alias 'org-floor* 'cl-floor "Org 9.0")
148 (defun org-sublist (list start end)
149 "Return a section of LIST, from START to END.
150 Counting starts at 1."
151 (cl-subseq list (1- start) end))
152 (make-obsolete 'org-sublist
153 "use cl-subseq (note the 0-based counting)."
154 "Org 9.0")
157 ;;;; Functions available since Emacs 24.3
158 (define-obsolete-function-alias 'org-buffer-narrowed-p 'buffer-narrowed-p "Org 9.0")
159 (define-obsolete-function-alias 'org-called-interactively-p 'called-interactively-p "Org 9.0")
160 (define-obsolete-function-alias 'org-char-to-string 'char-to-string "Org 9.0")
161 (define-obsolete-function-alias 'org-delete-directory 'delete-directory "Org 9.0")
162 (define-obsolete-function-alias 'org-format-seconds 'format-seconds "Org 9.0")
163 (define-obsolete-function-alias 'org-link-escape-browser 'url-encode-url "Org 9.0")
164 (define-obsolete-function-alias 'org-no-warnings 'with-no-warnings "Org 9.0")
165 (define-obsolete-function-alias 'org-number-sequence 'number-sequence "Org 9.0")
166 (define-obsolete-function-alias 'org-pop-to-buffer-same-window 'pop-to-buffer-same-window "Org 9.0")
167 (define-obsolete-function-alias 'org-string-match-p 'string-match-p "Org 9.0")
169 ;;;; Functions and variables from previous releases now obsolete.
170 (define-obsolete-function-alias 'org-element-remove-indentation
171 'org-remove-indentation "Org 9.0")
172 (define-obsolete-variable-alias 'org-latex-create-formula-image-program
173 'org-preview-latex-default-process "Org 9.0")
174 (define-obsolete-variable-alias 'org-latex-preview-ltxpng-directory
175 'org-preview-latex-image-directory "Org 9.0")
176 (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
177 (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 9.0")
178 (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp "Org 8.3")
179 (define-obsolete-function-alias 'org-image-file-name-regexp
180 'image-file-name-regexp "Org 9.0")
181 (define-obsolete-function-alias 'org-completing-read-no-i
182 'completing-read "Org 9.0")
183 (define-obsolete-function-alias 'org-icompleting-read
184 'completing-read "Org 9.0")
185 (define-obsolete-function-alias 'org-iread-file-name 'read-file-name "Org 9.0")
186 (define-obsolete-function-alias 'org-days-to-time
187 'org-time-stamp-to-now "Org 8.2")
188 (define-obsolete-variable-alias 'org-agenda-ignore-drawer-properties
189 'org-agenda-ignore-properties "Org 9.0")
190 (define-obsolete-function-alias 'org-preview-latex-fragment
191 'org-toggle-latex-fragment "Org 8.3")
192 (define-obsolete-function-alias 'org-export-get-genealogy
193 'org-element-lineage "Org 9.0")
194 (define-obsolete-variable-alias 'org-latex-with-hyperref
195 'org-latex-hyperref-template "Org 9.0")
196 (define-obsolete-variable-alias 'hfy-optimisations 'hfy-optimizations "Org 9.0")
197 (define-obsolete-variable-alias 'org-export-htmlized-org-css-url
198 'org-org-htmlized-css-url "Org 8.2")
199 (define-obsolete-function-alias 'org-list-parse-list 'org-list-to-lisp "Org 9.0")
200 (define-obsolete-function-alias 'org-agenda-todayp
201 'org-agenda-today-p "Org 9.0")
202 (define-obsolete-function-alias 'org-babel-examplize-region
203 'org-babel-examplify-region "Org 9.0")
204 (define-obsolete-variable-alias 'org-babel-capitalize-example-region-markers
205 'org-babel-uppercase-example-markers "Org 9.1")
207 (define-obsolete-function-alias 'org-babel-trim 'org-trim "Org 9.0")
208 (define-obsolete-variable-alias 'org-html-style 'org-html-head "24.4")
209 (define-obsolete-function-alias 'org-insert-columns-dblock
210 'org-columns-insert-dblock "Org 9.0")
211 (define-obsolete-variable-alias 'org-export-babel-evaluate
212 'org-export-use-babel "Org 9.1")
213 (define-obsolete-function-alias 'org-activate-bracket-links
214 'org-activate-links "Org 9.0")
215 (define-obsolete-function-alias 'org-activate-plain-links 'ignore "Org 9.0")
216 (define-obsolete-function-alias 'org-activate-angle-links 'ignore "Org 9.0")
217 (define-obsolete-function-alias 'org-get-indentation
218 'current-indentation "Org 9.2")
220 (defun org-in-fixed-width-region-p ()
221 "Non-nil if point in a fixed-width region."
222 (save-match-data
223 (eq 'fixed-width (org-element-type (org-element-at-point)))))
224 (make-obsolete 'org-in-fixed-width-region-p
225 "use `org-element' library"
226 "Org 9.0")
228 (defun org-compatible-face (inherits specs)
229 "Make a compatible face specification.
230 If INHERITS is an existing face and if the Emacs version supports
231 it, just inherit the face. If INHERITS is not given and SPECS
232 is, use SPECS to define the face."
233 (declare (indent 1))
234 (if (facep inherits)
235 (list (list t :inherit inherits))
236 specs))
237 (make-obsolete 'org-compatible-face "you can remove it." "Org 9.0")
239 (defun org-add-link-type (type &optional follow export)
240 "Add a new TYPE link.
241 FOLLOW and EXPORT are two functions.
243 FOLLOW should take the link path as the single argument and do whatever
244 is necessary to follow the link, for example find a file or display
245 a mail message.
247 EXPORT should format the link path for export to one of the export formats.
248 It should be a function accepting three arguments:
250 path the path of the link, the text after the prefix (like \"http:\")
251 desc the description of the link, if any
252 format the export format, a symbol like `html' or `latex' or `ascii'.
254 The function may use the FORMAT information to return different values
255 depending on the format. The return value will be put literally into
256 the exported file. If the return value is nil, this means Org should
257 do what it normally does with links which do not have EXPORT defined.
259 Org mode has a built-in default for exporting links. If you are happy with
260 this default, there is no need to define an export function for the link
261 type. For a simple example of an export function, see `org-bbdb.el'.
263 If TYPE already exists, update it with the arguments.
264 See `org-link-parameters' for documentation on the other parameters."
265 (org-link-set-parameters type :follow follow :export export)
266 (message "Created %s link." type))
268 (make-obsolete 'org-add-link-type "use `org-link-set-parameters' instead." "Org 9.0")
270 ;;;; Functions unused in Org core.
271 (defun org-table-recognize-table.el ()
272 "If there is a table.el table nearby, recognize it and move into it."
273 (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
274 (beginning-of-line)
275 (unless (or (looking-at org-table-dataline-regexp)
276 (not (looking-at org-table1-hline-regexp)))
277 (forward-line)
278 (when (looking-at org-table-any-border-regexp)
279 (forward-line -2)))
280 (if (re-search-forward "|" (org-table-end t) t)
281 (progn
282 (require 'table)
283 (if (table--at-cell-p (point)) t
284 (message "recognizing table.el table...")
285 (table-recognize-table)
286 (message "recognizing table.el table...done")))
287 (error "This should not happen"))))
289 ;; Not used since commit 6d1e3082, Feb 2010.
290 (make-obsolete 'org-table-recognize-table.el
291 "please notify Org mailing list if you use this function."
292 "Org 9.0")
294 (defmacro org-preserve-lc (&rest body)
295 (declare (debug (body))
296 (obsolete "please notify Org mailing list if you use this function."
297 "Org 9.2"))
298 (org-with-gensyms (line col)
299 `(let ((,line (org-current-line))
300 (,col (current-column)))
301 (unwind-protect
302 (progn ,@body)
303 (org-goto-line ,line)
304 (org-move-to-column ,col)))))
306 (defun org-version-check (version &rest _)
307 "Non-nil if VERSION is lower (older) than `emacs-version'."
308 (declare (obsolete "use `version<' or `fboundp' instead."
309 "Org 9.2"))
310 (version< version emacs-version))
312 (defun org-remove-angle-brackets (s)
313 (org-unbracket-string "<" ">" s))
314 (make-obsolete 'org-remove-angle-brackets 'org-unbracket-string "Org 9.0")
316 (defun org-remove-double-quotes (s)
317 (org-unbracket-string "\"" "\"" s))
318 (make-obsolete 'org-remove-double-quotes 'org-unbracket-string "Org 9.0")
320 (defcustom org-publish-sitemap-file-entry-format "%t"
321 "Format string for site-map file entry.
322 You could use brackets to delimit on what part the link will be.
324 %t is the title.
325 %a is the author.
326 %d is the date formatted using `org-publish-sitemap-date-format'."
327 :group 'org-export-publish
328 :type 'string)
329 (make-obsolete-variable
330 'org-publish-sitemap-file-entry-format
331 "set `:sitemap-format-entry' in `org-publish-project-alist' instead."
332 "Org 9.1")
334 (defvar org-agenda-skip-regexp)
335 (defun org-agenda-skip-entry-when-regexp-matches ()
336 "Check if the current entry contains match for `org-agenda-skip-regexp'.
337 If yes, it returns the end position of this entry, causing agenda commands
338 to skip the entry but continuing the search in the subtree. This is a
339 function that can be put into `org-agenda-skip-function' for the duration
340 of a command."
341 (declare (obsolete "use `org-agenda-skip-if' instead." "Org 9.1"))
342 (let ((end (save-excursion (org-end-of-subtree t)))
343 skip)
344 (save-excursion
345 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
346 (and skip end)))
348 (defun org-agenda-skip-subtree-when-regexp-matches ()
349 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
350 If yes, it returns the end position of this tree, causing agenda commands
351 to skip this subtree. This is a function that can be put into
352 `org-agenda-skip-function' for the duration of a command."
353 (declare (obsolete "use `org-agenda-skip-if' instead." "Org 9.1"))
354 (let ((end (save-excursion (org-end-of-subtree t)))
355 skip)
356 (save-excursion
357 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
358 (and skip end)))
360 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
361 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
362 If yes, it returns the end position of the current entry (NOT the tree),
363 causing agenda commands to skip the entry but continuing the search in
364 the subtree. This is a function that can be put into
365 `org-agenda-skip-function' for the duration of a command. An important
366 use of this function is for the stuck project list."
367 (declare (obsolete "use `org-agenda-skip-if' instead." "Org 9.1"))
368 (let ((end (save-excursion (org-end-of-subtree t)))
369 (entry-end (save-excursion (outline-next-heading) (1- (point))))
370 skip)
371 (save-excursion
372 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
373 (and skip entry-end)))
375 (define-obsolete-function-alias 'org-minutes-to-clocksum-string
376 'org-duration-from-minutes "Org 9.1")
378 (define-obsolete-function-alias 'org-hh:mm-string-to-minutes
379 'org-duration-to-minutes "Org 9.1")
381 (define-obsolete-function-alias 'org-duration-string-to-minutes
382 'org-duration-to-minutes "Org 9.1")
384 (make-obsolete-variable 'org-time-clocksum-format
385 "set `org-duration-format' instead." "Org 9.1")
387 (make-obsolete-variable 'org-time-clocksum-use-fractional
388 "set `org-duration-format' instead." "Org 9.1")
390 (make-obsolete-variable 'org-time-clocksum-fractional-format
391 "set `org-duration-format' instead." "Org 9.1")
393 (make-obsolete-variable 'org-time-clocksum-use-effort-durations
394 "set `org-duration-units' instead." "Org 9.1")
396 (define-obsolete-function-alias 'org-babel-number-p
397 'org-babel--string-to-number "Org 9.0")
399 (define-obsolete-variable-alias 'org-usenet-links-prefer-google
400 'org-gnus-prefer-web-links "Org 9.1")
402 (define-obsolete-variable-alias 'org-texinfo-def-table-markup
403 'org-texinfo-table-default-markup "Org 9.1")
405 ;; The function was made obsolete by commit 65399674d5 of 2013-02-22.
406 ;; This make-obsolete call was added 2016-09-01.
407 (make-obsolete 'org-capture-import-remember-templates
408 "use the `org-capture-templates' variable instead."
409 "Org 9.0")
411 (defun org-show-block-all ()
412 "Unfold all blocks in the current buffer."
413 (interactive)
414 (remove-overlays nil nil 'invisible 'org-hide-block))
416 (make-obsolete 'org-show-block-all
417 "use `org-show-all' instead."
418 "Org 9.2")
420 (define-obsolete-function-alias 'org-get-tags-at 'org-get-tags "Org 9.2")
422 (defun org-get-local-tags ()
423 "Get a list of tags defined in the current headline."
424 (declare (obsolete "use `org-get-tags' instead." "Org 9.2"))
425 (org-get-tags nil 'local))
427 (defun org-get-local-tags-at (&optional pos)
428 "Get a list of tags defined in the current headline."
429 (declare (obsolete "use `org-get-tags' instead." "Org 9.2"))
430 (org-get-tags pos 'local))
432 (defun org-get-tags-string ()
433 "Get the TAGS string in the current headline."
434 (declare (obsolete "use `org-make-tag-string' instead." "Org 9.2"))
435 (org-make-tag-string (org-get-tags nil t)))
437 (define-obsolete-function-alias 'org-set-tags-to 'org-set-tags "Org 9.2")
439 (defun org-align-all-tags ()
440 "Align the tags in all headings."
441 (declare (obsolete "use `org-align-tags' instead." "Org 9.2"))
442 (org-align-tags t))
444 (defmacro org-with-silent-modifications (&rest body)
445 (declare (obsolete "use `with-silent-modifications' instead." "Org 9.2")
446 (debug (body)))
447 `(with-silent-modifications ,@body))
449 ;;;; Obsolete link types
451 (eval-after-load 'org
452 '(progn
453 (org-link-set-parameters "file+emacs") ;since Org 9.0
454 (org-link-set-parameters "file+sys"))) ;since Org 9.0
458 ;;; Miscellaneous functions
460 (defun org-get-x-clipboard (value)
461 "Get the value of the X or Windows clipboard."
462 (cond ((and (eq window-system 'x)
463 (fboundp 'gui-get-selection)) ;Silence byte-compiler.
464 (org-no-properties
465 (ignore-errors
466 (or (gui-get-selection value 'UTF8_STRING)
467 (gui-get-selection value 'COMPOUND_TEXT)
468 (gui-get-selection value 'STRING)
469 (gui-get-selection value 'TEXT)))))
470 ((and (eq window-system 'w32) (fboundp 'w32-get-clipboard-data))
471 (w32-get-clipboard-data))))
473 ;; `set-transient-map' is only in Emacs >= 24.4
474 (defalias 'org-set-transient-map
475 (if (fboundp 'set-transient-map)
476 'set-transient-map
477 'set-temporary-overlay-map))
480 ;;; Region compatibility
482 (defvar org-ignore-region nil
483 "Non-nil means temporarily disable the active region.")
485 (defun org-region-active-p ()
486 "Non-nil when the region active.
487 Unlike to `use-region-p', this function also checks
488 `org-ignore-region'."
489 (and (not org-ignore-region) (use-region-p)))
491 (defun org-cursor-to-region-beginning ()
492 (when (and (org-region-active-p)
493 (> (point) (region-beginning)))
494 (exchange-point-and-mark)))
497 ;;; Invisibility compatibility
499 (define-obsolete-function-alias 'org-remove-from-invisibility-spec 'remove-from-invisibility-spec
500 "Org 9.2")
502 (defun org-in-invisibility-spec-p (arg)
503 "Is ARG a member of `buffer-invisibility-spec'?"
504 (when (consp buffer-invisibility-spec)
505 (member arg buffer-invisibility-spec)))
507 (defun org-move-to-column (column &optional force _buffer)
508 "Move to column COLUMN.
509 Pass COLUMN and FORCE to `move-to-column'."
510 (let ((buffer-invisibility-spec
511 (if (listp buffer-invisibility-spec)
512 (remove '(org-filtered) buffer-invisibility-spec)
513 buffer-invisibility-spec)))
514 (move-to-column column force)))
516 (defmacro org-find-library-dir (library)
517 `(file-name-directory (or (locate-library ,library) "")))
519 (defun org-count-lines (s)
520 "How many lines in string S?"
521 (let ((start 0) (n 1))
522 (while (string-match "\n" s start)
523 (setq start (match-end 0) n (1+ n)))
524 (when (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
525 (setq n (1- n)))
528 (defun org-kill-new (string &rest args)
529 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
530 string)
531 (apply 'kill-new string args))
533 ;; `font-lock-ensure' is only available from 24.4.50 on
534 (defalias 'org-font-lock-ensure
535 (if (fboundp 'font-lock-ensure)
536 #'font-lock-ensure
537 (lambda (&optional _beg _end)
538 (with-no-warnings (font-lock-fontify-buffer)))))
540 ;; `file-local-name' was added in Emacs 26.1.
541 (defalias 'org-babel-local-file-name
542 (if (fboundp 'file-local-name)
543 'file-local-name
544 (lambda (file)
545 "Return the local name component of FILE."
546 (or (file-remote-p file 'localname) file))))
548 ;;;###autoload
549 (defmacro org-check-version ()
550 "Try very hard to provide sensible version strings."
551 (let* ((org-dir (org-find-library-dir "org"))
552 (org-version.el (concat org-dir "org-version.el"))
553 (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
554 (if (require 'org-version org-version.el 'noerror)
555 '(progn
556 (autoload 'org-release "org-version.el")
557 (autoload 'org-git-version "org-version.el"))
558 (if (require 'org-fixup org-fixup.el 'noerror)
559 '(org-fixup)
560 ;; provide fallback definitions and complain
561 (warn "Could not define org version correctly. Check installation!")
562 '(progn
563 (defun org-release () "N/A")
564 (defun org-git-version () "N/A !!check installation!!"))))))
568 ;;; Functions for Emacs < 24.4 compatibility
570 (defun org-define-error (name message)
571 "Define NAME as a new error signal.
572 MESSAGE is a string that will be output to the echo area if such
573 an error is signaled without being caught by a `condition-case'.
574 Implements `define-error' for older emacsen."
575 (if (fboundp 'define-error) (define-error name message)
576 (put name 'error-conditions
577 (copy-sequence (cons name (get 'error 'error-conditions))))))
579 (unless (fboundp 'string-suffix-p)
580 ;; From Emacs subr.el.
581 (defun string-suffix-p (suffix string &optional ignore-case)
582 "Return non-nil if SUFFIX is a suffix of STRING.
583 If IGNORE-CASE is non-nil, the comparison is done without paying
584 attention to case differences."
585 (let ((start-pos (- (length string) (length suffix))))
586 (and (>= start-pos 0)
587 (eq t (compare-strings suffix nil nil
588 string start-pos nil ignore-case))))))
591 ;;; Integration with and fixes for other packages
593 (defgroup org-imenu-and-speedbar nil
594 "Options concerning imenu and speedbar in Org mode."
595 :tag "Org Imenu and Speedbar"
596 :group 'org-structure)
598 (defcustom org-imenu-depth 2
599 "The maximum level for Imenu access to Org headlines.
600 This also applied for speedbar access."
601 :group 'org-imenu-and-speedbar
602 :type 'integer)
604 ;;;; Imenu
606 (defvar-local org-imenu-markers nil
607 "All markers currently used by Imenu.")
609 (defun org-imenu-new-marker (&optional pos)
610 "Return a new marker for use by Imenu, and remember the marker."
611 (let ((m (make-marker)))
612 (move-marker m (or pos (point)))
613 (push m org-imenu-markers)
616 (defun org-imenu-get-tree ()
617 "Produce the index for Imenu."
618 (dolist (x org-imenu-markers) (move-marker x nil))
619 (setq org-imenu-markers nil)
620 (let* ((case-fold-search nil)
621 (n org-imenu-depth)
622 (re (concat "^" (org-get-limited-outline-regexp)))
623 (subs (make-vector (1+ n) nil))
624 (last-level 0)
625 m level head0 head)
626 (org-with-wide-buffer
627 (goto-char (point-max))
628 (while (re-search-backward re nil t)
629 (setq level (org-reduced-level (funcall outline-level)))
630 (when (and (<= level n)
631 (looking-at org-complex-heading-regexp)
632 (setq head0 (match-string-no-properties 4)))
633 (setq head (org-link-display-format head0)
634 m (org-imenu-new-marker))
635 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
636 (if (>= level last-level)
637 (push (cons head m) (aref subs level))
638 (push (cons head (aref subs (1+ level))) (aref subs level))
639 (cl-loop for i from (1+ level) to n do (aset subs i nil)))
640 (setq last-level level))))
641 (aref subs 1)))
643 (eval-after-load "imenu"
644 '(progn
645 (add-hook 'imenu-after-jump-hook
646 (lambda ()
647 (when (derived-mode-p 'org-mode)
648 (org-show-context 'org-goto))))))
650 ;;;; Speedbar
652 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
653 "Overlay marking the agenda restriction line in speedbar.")
654 (overlay-put org-speedbar-restriction-lock-overlay
655 'face 'org-agenda-restriction-lock)
656 (overlay-put org-speedbar-restriction-lock-overlay
657 'help-echo "Agendas are currently limited to this item.")
658 (delete-overlay org-speedbar-restriction-lock-overlay)
660 (defun org-speedbar-set-agenda-restriction ()
661 "Restrict future agenda commands to the location at point in speedbar.
662 If there is already a restriction lock at the location, remove it.
664 To get rid of the restriction, use `\\[org-agenda-remove-restriction-lock]'."
665 (interactive)
666 (require 'org-agenda)
667 (let (p m tp np dir txt)
668 (cond
669 ((setq p (text-property-any (point-at-bol) (point-at-eol)
670 'org-imenu t))
671 (setq m (get-text-property p 'org-imenu-marker))
672 (with-current-buffer (marker-buffer m)
673 (goto-char m)
674 (if (and org-agenda-overriding-restriction
675 (member org-agenda-restriction-lock-overlay
676 (overlays-at (point))))
677 (org-agenda-remove-restriction-lock 'noupdate)
678 (org-agenda-set-restriction-lock 'subtree))))
679 ((setq p (text-property-any (point-at-bol) (point-at-eol)
680 'speedbar-function 'speedbar-find-file))
681 (setq tp (previous-single-property-change
682 (1+ p) 'speedbar-function)
683 np (next-single-property-change
684 tp 'speedbar-function)
685 dir (speedbar-line-directory)
686 txt (buffer-substring-no-properties (or tp (point-min))
687 (or np (point-max))))
688 (with-current-buffer (find-file-noselect
689 (let ((default-directory dir))
690 (expand-file-name txt)))
691 (unless (derived-mode-p 'org-mode)
692 (user-error "Cannot restrict to non-Org mode file"))
693 (org-agenda-set-restriction-lock 'file)))
694 (t (user-error "Don't know how to restrict Org mode agenda")))
695 (move-overlay org-speedbar-restriction-lock-overlay
696 (point-at-bol) (point-at-eol))
697 (setq current-prefix-arg nil)
698 (org-agenda-maybe-redo)))
700 (defvar speedbar-file-key-map)
701 (declare-function speedbar-add-supported-extension "speedbar" (extension))
702 (eval-after-load "speedbar"
703 '(progn
704 (speedbar-add-supported-extension ".org")
705 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
706 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
707 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
708 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
709 (add-hook 'speedbar-visiting-tag-hook
710 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
712 ;;;; Flyspell
714 (defun org--flyspell-object-check-p (element)
715 "Non-nil when Flyspell can check object at point.
716 ELEMENT is the element at point."
717 (let ((object (save-excursion
718 (when (looking-at-p "\\>") (backward-char))
719 (org-element-context element))))
720 (cl-case (org-element-type object)
721 ;; Prevent checks in links due to keybinding conflict with
722 ;; Flyspell.
723 ((code entity export-snippet inline-babel-call
724 inline-src-block line-break latex-fragment link macro
725 statistics-cookie target timestamp verbatim)
726 nil)
727 (footnote-reference
728 ;; Only in inline footnotes, within the definition.
729 (and (eq (org-element-property :type object) 'inline)
730 (< (save-excursion
731 (goto-char (org-element-property :begin object))
732 (search-forward ":" nil t 2))
733 (point))))
734 (otherwise t))))
736 (defun org-mode-flyspell-verify ()
737 "Function used for `flyspell-generic-check-word-predicate'."
738 (if (org-at-heading-p)
739 ;; At a headline or an inlinetask, check title only. This is
740 ;; faster than relying on `org-element-at-point'.
741 (and (save-excursion (beginning-of-line)
742 (and (let ((case-fold-search t))
743 (not (looking-at-p "\\*+ END[ \t]*$")))
744 (let ((case-fold-search nil))
745 (looking-at org-complex-heading-regexp))))
746 (match-beginning 4)
747 (>= (point) (match-beginning 4))
748 (or (not (match-beginning 5))
749 (< (point) (match-beginning 5))))
750 (let* ((element (org-element-at-point))
751 (post-affiliated (org-element-property :post-affiliated element)))
752 (cond
753 ;; Ignore checks in all affiliated keywords but captions.
754 ((< (point) post-affiliated)
755 (and (save-excursion
756 (beginning-of-line)
757 (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
758 (> (point) (match-end 0))
759 (org--flyspell-object-check-p element)))
760 ;; Ignore checks in LOGBOOK (or equivalent) drawer.
761 ((let ((log (org-log-into-drawer)))
762 (and log
763 (let ((drawer (org-element-lineage element '(drawer))))
764 (and drawer
765 (eq (compare-strings
766 log nil nil
767 (org-element-property :drawer-name drawer) nil nil t)
768 t)))))
769 nil)
771 (cl-case (org-element-type element)
772 ((comment quote-section) t)
773 (comment-block
774 ;; Allow checks between block markers, not on them.
775 (and (> (line-beginning-position) post-affiliated)
776 (save-excursion
777 (end-of-line)
778 (skip-chars-forward " \r\t\n")
779 (< (point) (org-element-property :end element)))))
780 ;; Arbitrary list of keywords where checks are meaningful.
781 ;; Make sure point is on the value part of the element.
782 (keyword
783 (and (member (org-element-property :key element)
784 '("DESCRIPTION" "TITLE"))
785 (save-excursion
786 (search-backward ":" (line-beginning-position) t))))
787 ;; Check is globally allowed in paragraphs verse blocks and
788 ;; table rows (after affiliated keywords) but some objects
789 ;; must not be affected.
790 ((paragraph table-row verse-block)
791 (let ((cbeg (org-element-property :contents-begin element))
792 (cend (org-element-property :contents-end element)))
793 (and cbeg (>= (point) cbeg) (< (point) cend)
794 (org--flyspell-object-check-p element))))))))))
795 (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
797 (defun org-remove-flyspell-overlays-in (beg end)
798 "Remove flyspell overlays in region."
799 (and (bound-and-true-p flyspell-mode)
800 (fboundp 'flyspell-delete-region-overlays)
801 (flyspell-delete-region-overlays beg end)))
803 (defvar flyspell-delayed-commands)
804 (eval-after-load "flyspell"
805 '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
807 ;;;; Bookmark
809 (defun org-bookmark-jump-unhide ()
810 "Unhide the current position, to show the bookmark location."
811 (and (derived-mode-p 'org-mode)
812 (or (org-invisible-p)
813 (save-excursion (goto-char (max (point-min) (1- (point))))
814 (org-invisible-p)))
815 (org-show-context 'bookmark-jump)))
817 ;; Make `bookmark-jump' shows the jump location if it was hidden.
818 (eval-after-load "bookmark"
819 '(if (boundp 'bookmark-after-jump-hook)
820 ;; We can use the hook
821 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
822 ;; Hook not available, use advice
823 (defadvice bookmark-jump (after org-make-visible activate)
824 "Make the position visible."
825 (org-bookmark-jump-unhide))))
827 ;;;; Calendar
829 (defcustom org-calendar-to-agenda-key 'default
830 "Key to be installed in `calendar-mode-map' for switching to the agenda.
832 The command `org-calendar-goto-agenda' will be bound to this key.
834 When set to `default', bind the function to `c', but only if it is
835 available in the Calendar keymap. This is the default choice because
836 `c' can then be used to switch back and forth between agenda and calendar.
838 When nil, `org-calendar-goto-agenda' is not bound to any key."
839 :group 'org-agenda
840 :type '(choice
841 (const :tag "Bind to `c' if available" default)
842 (key-sequence :tag "Other binding")
843 (const :tag "No binding" nil))
844 :safe (lambda (v) (or (symbolp v) (stringp v)))
845 :package-version '(Org . "9.2"))
847 (defcustom org-calendar-insert-diary-entry-key [?i]
848 "The key to be installed in `calendar-mode-map' for adding diary entries.
849 This option is irrelevant until `org-agenda-diary-file' has been configured
850 to point to an Org file. When that is the case, the command
851 `org-agenda-diary-entry' will be bound to the key given here, by default
852 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
853 if you want to continue doing this, you need to change this to a different
854 key."
855 :group 'org-agenda
856 :type 'sexp)
858 (defun org--setup-calendar-bindings ()
859 "Bind Org functions in Calendar keymap."
860 (pcase org-calendar-to-agenda-key
861 (`nil nil)
862 ((and key (pred stringp))
863 (local-set-key (kbd key) #'org-calendar-goto-agenda))
864 ((guard (not (lookup-key calendar-mode-map "c")))
865 (local-set-key "c" #'org-calendar-goto-agenda))
866 (_ nil))
867 (unless (eq org-agenda-diary-file 'diary-file)
868 (local-set-key org-calendar-insert-diary-entry-key
869 #'org-agenda-diary-entry)))
871 (eval-after-load "calendar"
872 '(add-hook 'calendar-mode-hook #'org--setup-calendar-bindings))
874 ;;;; Saveplace
876 ;; Make sure saveplace shows the location if it was hidden
877 (eval-after-load "saveplace"
878 '(defadvice save-place-find-file-hook (after org-make-visible activate)
879 "Make the position visible."
880 (org-bookmark-jump-unhide)))
882 ;;;; Ecb
884 ;; Make sure ecb shows the location if it was hidden
885 (eval-after-load "ecb"
886 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
887 "Make hierarchy visible when jumping into location from ECB tree buffer."
888 (when (derived-mode-p 'org-mode)
889 (org-show-context))))
891 ;;;; Simple
893 (defun org-mark-jump-unhide ()
894 "Make the point visible with `org-show-context' after jumping to the mark."
895 (when (and (derived-mode-p 'org-mode)
896 (org-invisible-p))
897 (org-show-context 'mark-goto)))
899 (eval-after-load "simple"
900 '(defadvice pop-to-mark-command (after org-make-visible activate)
901 "Make the point visible with `org-show-context'."
902 (org-mark-jump-unhide)))
904 (eval-after-load "simple"
905 '(defadvice exchange-point-and-mark (after org-make-visible activate)
906 "Make the point visible with `org-show-context'."
907 (org-mark-jump-unhide)))
909 (eval-after-load "simple"
910 '(defadvice pop-global-mark (after org-make-visible activate)
911 "Make the point visible with `org-show-context'."
912 (org-mark-jump-unhide)))
914 ;;;; Session
916 ;; Make "session.el" ignore our circular variable.
917 (defvar session-globals-exclude)
918 (eval-after-load "session"
919 '(add-to-list 'session-globals-exclude 'org-mark-ring))
921 (provide 'org-compat)
923 ;;; org-compat.el ends here