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