Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-compat.el
blob6a18ac89928156f36af9ed6188ce96ab86f26517
1 ;;; org-compat.el --- Compatibility Code for Older Emacsen -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://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.
30 ;;; Code:
32 (require 'cl-lib)
33 (require 'org-macs)
35 (declare-function org-agenda-maybe-redo "org-agenda" ())
36 (declare-function org-at-heading-p "org" (&optional ignored))
37 (declare-function org-at-table.el-p "org" ())
38 (declare-function org-element-at-point "org-element" ())
39 (declare-function org-element-context "org-element" (&optional element))
40 (declare-function org-element-lineage "org-element" (blob &optional types with-self))
41 (declare-function org-element-type "org-element" (element))
42 (declare-function org-element-property "org-element" (property element))
43 (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
44 (declare-function org-invisible-p "org" (&optional pos))
45 (declare-function org-link-display-format "org" (s))
46 (declare-function org-link-set-parameters "org" (type &rest rest))
47 (declare-function org-log-into-drawer "org" ())
48 (declare-function org-reduced-level "org" (l))
49 (declare-function org-show-context "org" (&optional key))
50 (declare-function org-table-end "org-table" (&optional table-type))
51 (declare-function outline-next-heading "outline" ())
52 (declare-function speedbar-line-directory "speedbar" (&optional depth))
53 (declare-function table--at-cell-p "table" (position &optional object at-column))
55 (defvar org-complex-heading-regexp)
56 (defvar org-table-any-border-regexp)
57 (defvar org-table-dataline-regexp)
58 (defvar org-table-tab-recognizes-table.el)
59 (defvar org-table1-hline-regexp)
62 ;;; Emacs < 25.1 compatibility
64 (when (< emacs-major-version 25)
65 (defalias 'outline-hide-entry 'hide-entry)
66 (defalias 'outline-hide-sublevels 'hide-sublevels)
67 (defalias 'outline-hide-subtree 'hide-subtree)
68 (defalias 'outline-show-all 'show-all)
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")
404 ;;;; Obsolete link types
406 (eval-after-load 'org
407 '(progn
408 (org-link-set-parameters "file+emacs") ;since Org 9.0
409 (org-link-set-parameters "file+sys"))) ;since Org 9.0
413 ;;; Miscellaneous functions
415 (defun org-version-check (version feature level)
416 (let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
417 (v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
418 (rmaj (or (nth 0 v1) 99))
419 (rmin (or (nth 1 v1) 99))
420 (rbld (or (nth 2 v1) 99))
421 (maj (or (nth 0 v2) 0))
422 (min (or (nth 1 v2) 0))
423 (bld (or (nth 2 v2) 0)))
424 (if (or (< maj rmaj)
425 (and (= maj rmaj)
426 (< min rmin))
427 (and (= maj rmaj)
428 (= min rmin)
429 (< bld rbld)))
430 (if (eq level :predicate)
431 ;; just return if we have the version
433 (let ((msg (format "Emacs %s or greater is recommended for %s"
434 version feature)))
435 (display-warning 'org msg level)
437 t)))
439 (defun org-get-x-clipboard (value)
440 "Get the value of the X or Windows clipboard."
441 (cond ((and (eq window-system 'x)
442 (fboundp 'gui-get-selection)) ;Silence byte-compiler.
443 (org-no-properties
444 (ignore-errors
445 (or (gui-get-selection value 'UTF8_STRING)
446 (gui-get-selection value 'COMPOUND_TEXT)
447 (gui-get-selection value 'STRING)
448 (gui-get-selection value 'TEXT)))))
449 ((and (eq window-system 'w32) (fboundp 'w32-get-clipboard-data))
450 (w32-get-clipboard-data))))
452 (defun org-fit-window-to-buffer (&optional window max-height min-height
453 shrink-only)
454 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
455 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
456 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
457 `shrink-window-if-larger-than-buffer' instead, the height limit is
458 ignored in this case."
459 (cond ((if (fboundp 'window-full-width-p)
460 (not (window-full-width-p window))
461 ;; do nothing if another window would suffer
462 (> (frame-width) (window-width window))))
463 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
464 (fit-window-to-buffer window max-height min-height))
465 ((fboundp 'shrink-window-if-larger-than-buffer)
466 (shrink-window-if-larger-than-buffer window)))
467 (or window (selected-window)))
469 ;; `set-transient-map' is only in Emacs >= 24.4
470 (defalias 'org-set-transient-map
471 (if (fboundp 'set-transient-map)
472 'set-transient-map
473 'set-temporary-overlay-map))
476 ;;; Region compatibility
478 (defvar org-ignore-region nil
479 "Non-nil means temporarily disable the active region.")
481 (defun org-region-active-p ()
482 "Non-nil when the region active.
483 Unlike to `use-region-p', this function also checks
484 `org-ignore-region'."
485 (and (not org-ignore-region) (use-region-p)))
487 (defun org-cursor-to-region-beginning ()
488 (when (and (org-region-active-p)
489 (> (point) (region-beginning)))
490 (exchange-point-and-mark)))
493 ;;; Invisibility compatibility
495 (defun org-remove-from-invisibility-spec (arg)
496 "Remove elements from `buffer-invisibility-spec'."
497 (if (fboundp 'remove-from-invisibility-spec)
498 (remove-from-invisibility-spec arg)
499 (if (consp buffer-invisibility-spec)
500 (setq buffer-invisibility-spec
501 (delete arg buffer-invisibility-spec)))))
503 (defun org-in-invisibility-spec-p (arg)
504 "Is ARG a member of `buffer-invisibility-spec'?"
505 (if (consp buffer-invisibility-spec)
506 (member arg buffer-invisibility-spec)))
508 (defun org-move-to-column (column &optional force _buffer)
509 "Move to column COLUMN.
510 Pass COLUMN and FORCE to `move-to-column'."
511 (let ((buffer-invisibility-spec
512 (if (listp buffer-invisibility-spec)
513 (remove '(org-filtered) buffer-invisibility-spec)
514 buffer-invisibility-spec)))
515 (move-to-column column force)))
517 (defmacro org-find-library-dir (library)
518 `(file-name-directory (or (locate-library ,library) "")))
520 (defun org-count-lines (s)
521 "How many lines in string S?"
522 (let ((start 0) (n 1))
523 (while (string-match "\n" s start)
524 (setq start (match-end 0) n (1+ n)))
525 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
526 (setq n (1- n)))
529 (defun org-kill-new (string &rest args)
530 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
531 string)
532 (apply 'kill-new string args))
534 ;; `font-lock-ensure' is only available from 24.4.50 on
535 (defalias 'org-font-lock-ensure
536 (if (fboundp 'font-lock-ensure)
537 #'font-lock-ensure
538 (lambda (&optional _beg _end)
539 (with-no-warnings (font-lock-fontify-buffer)))))
541 ;; `file-local-name' was added in Emacs 26.1.
542 (defalias 'org-babel-local-file-name
543 (if (fboundp 'file-local-name)
544 'file-local-name
545 (lambda (file)
546 "Return the local name component of FILE."
547 (or (file-remote-p file 'localname) file))))
549 (defmacro org-no-popups (&rest body)
550 "Suppress popup windows.
551 Let-bind some variables to nil around BODY to achieve the desired
552 effect, which variables to use depends on the Emacs version."
553 (if (org-version-check "24.2.50" "" :predicate)
554 `(let (pop-up-frames display-buffer-alist)
555 ,@body)
556 `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
557 ,@body)))
559 ;;;###autoload
560 (defmacro org-check-version ()
561 "Try very hard to provide sensible version strings."
562 (let* ((org-dir (org-find-library-dir "org"))
563 (org-version.el (concat org-dir "org-version.el"))
564 (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
565 (if (require 'org-version org-version.el 'noerror)
566 '(progn
567 (autoload 'org-release "org-version.el")
568 (autoload 'org-git-version "org-version.el"))
569 (if (require 'org-fixup org-fixup.el 'noerror)
570 '(org-fixup)
571 ;; provide fallback definitions and complain
572 (warn "Could not define org version correctly. Check installation!")
573 '(progn
574 (defun org-release () "N/A")
575 (defun org-git-version () "N/A !!check installation!!"))))))
577 (defmacro org-with-silent-modifications (&rest body)
578 (if (fboundp 'with-silent-modifications)
579 `(with-silent-modifications ,@body)
580 `(org-unmodified ,@body)))
581 (def-edebug-spec org-with-silent-modifications (body))
584 ;;; Functions for Emacs < 24.4 compatibility
586 (defun org-define-error (name message)
587 "Define NAME as a new error signal.
588 MESSAGE is a string that will be output to the echo area if such
589 an error is signaled without being caught by a `condition-case'.
590 Implements `define-error' for older emacsen."
591 (if (fboundp 'define-error) (define-error name message)
592 (put name 'error-conditions
593 (copy-sequence (cons name (get 'error 'error-conditions))))))
595 (unless (fboundp 'string-suffix-p)
596 ;; From Emacs subr.el.
597 (defun string-suffix-p (suffix string &optional ignore-case)
598 "Return non-nil if SUFFIX is a suffix of STRING.
599 If IGNORE-CASE is non-nil, the comparison is done without paying
600 attention to case differences."
601 (let ((start-pos (- (length string) (length suffix))))
602 (and (>= start-pos 0)
603 (eq t (compare-strings suffix nil nil
604 string start-pos nil ignore-case))))))
607 ;;; Integration with and fixes for other packages
609 (defgroup org-imenu-and-speedbar nil
610 "Options concerning imenu and speedbar in Org mode."
611 :tag "Org Imenu and Speedbar"
612 :group 'org-structure)
614 (defcustom org-imenu-depth 2
615 "The maximum level for Imenu access to Org headlines.
616 This also applied for speedbar access."
617 :group 'org-imenu-and-speedbar
618 :type 'integer)
620 ;;;; Imenu
622 (defvar-local org-imenu-markers nil
623 "All markers currently used by Imenu.")
625 (defun org-imenu-new-marker (&optional pos)
626 "Return a new marker for use by Imenu, and remember the marker."
627 (let ((m (make-marker)))
628 (move-marker m (or pos (point)))
629 (push m org-imenu-markers)
632 (defun org-imenu-get-tree ()
633 "Produce the index for Imenu."
634 (dolist (x org-imenu-markers) (move-marker x nil))
635 (setq org-imenu-markers nil)
636 (let* ((case-fold-search nil)
637 (n org-imenu-depth)
638 (re (concat "^" (org-get-limited-outline-regexp)))
639 (subs (make-vector (1+ n) nil))
640 (last-level 0)
641 m level head0 head)
642 (org-with-wide-buffer
643 (goto-char (point-max))
644 (while (re-search-backward re nil t)
645 (setq level (org-reduced-level (funcall outline-level)))
646 (when (and (<= level n)
647 (looking-at org-complex-heading-regexp)
648 (setq head0 (match-string-no-properties 4)))
649 (setq head (org-link-display-format head0)
650 m (org-imenu-new-marker))
651 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
652 (if (>= level last-level)
653 (push (cons head m) (aref subs level))
654 (push (cons head (aref subs (1+ level))) (aref subs level))
655 (cl-loop for i from (1+ level) to n do (aset subs i nil)))
656 (setq last-level level))))
657 (aref subs 1)))
659 (eval-after-load "imenu"
660 '(progn
661 (add-hook 'imenu-after-jump-hook
662 (lambda ()
663 (when (derived-mode-p 'org-mode)
664 (org-show-context 'org-goto))))))
666 ;;;; Speedbar
668 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
669 "Overlay marking the agenda restriction line in speedbar.")
670 (overlay-put org-speedbar-restriction-lock-overlay
671 'face 'org-agenda-restriction-lock)
672 (overlay-put org-speedbar-restriction-lock-overlay
673 'help-echo "Agendas are currently limited to this item.")
674 (delete-overlay org-speedbar-restriction-lock-overlay)
676 (defun org-speedbar-set-agenda-restriction ()
677 "Restrict future agenda commands to the location at point in speedbar.
678 To get rid of the restriction, use `\\[org-agenda-remove-restriction-lock]'."
679 (interactive)
680 (require 'org-agenda)
681 (let (p m tp np dir txt)
682 (cond
683 ((setq p (text-property-any (point-at-bol) (point-at-eol)
684 'org-imenu t))
685 (setq m (get-text-property p 'org-imenu-marker))
686 (with-current-buffer (marker-buffer m)
687 (goto-char m)
688 (org-agenda-set-restriction-lock 'subtree)))
689 ((setq p (text-property-any (point-at-bol) (point-at-eol)
690 'speedbar-function 'speedbar-find-file))
691 (setq tp (previous-single-property-change
692 (1+ p) 'speedbar-function)
693 np (next-single-property-change
694 tp 'speedbar-function)
695 dir (speedbar-line-directory)
696 txt (buffer-substring-no-properties (or tp (point-min))
697 (or np (point-max))))
698 (with-current-buffer (find-file-noselect
699 (let ((default-directory dir))
700 (expand-file-name txt)))
701 (unless (derived-mode-p 'org-mode)
702 (user-error "Cannot restrict to non-Org mode file"))
703 (org-agenda-set-restriction-lock 'file)))
704 (t (user-error "Don't know how to restrict Org mode agenda")))
705 (move-overlay org-speedbar-restriction-lock-overlay
706 (point-at-bol) (point-at-eol))
707 (setq current-prefix-arg nil)
708 (org-agenda-maybe-redo)))
710 (defvar speedbar-file-key-map)
711 (declare-function speedbar-add-supported-extension "speedbar" (extension))
712 (eval-after-load "speedbar"
713 '(progn
714 (speedbar-add-supported-extension ".org")
715 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
716 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
717 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
718 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
719 (add-hook 'speedbar-visiting-tag-hook
720 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
722 ;;;; Flyspell
724 (defun org--flyspell-object-check-p (element)
725 "Non-nil when Flyspell can check object at point.
726 ELEMENT is the element at point."
727 (let ((object (save-excursion
728 (when (looking-at-p "\\>") (backward-char))
729 (org-element-context element))))
730 (cl-case (org-element-type object)
731 ;; Prevent checks in links due to keybinding conflict with
732 ;; Flyspell.
733 ((code entity export-snippet inline-babel-call
734 inline-src-block line-break latex-fragment link macro
735 statistics-cookie target timestamp verbatim)
736 nil)
737 (footnote-reference
738 ;; Only in inline footnotes, within the definition.
739 (and (eq (org-element-property :type object) 'inline)
740 (< (save-excursion
741 (goto-char (org-element-property :begin object))
742 (search-forward ":" nil t 2))
743 (point))))
744 (otherwise t))))
746 (defun org-mode-flyspell-verify ()
747 "Function used for `flyspell-generic-check-word-predicate'."
748 (if (org-at-heading-p)
749 ;; At a headline or an inlinetask, check title only. This is
750 ;; faster than relying on `org-element-at-point'.
751 (and (save-excursion (beginning-of-line)
752 (and (let ((case-fold-search t))
753 (not (looking-at-p "\\*+ END[ \t]*$")))
754 (let ((case-fold-search nil))
755 (looking-at org-complex-heading-regexp))))
756 (match-beginning 4)
757 (>= (point) (match-beginning 4))
758 (or (not (match-beginning 5))
759 (< (point) (match-beginning 5))))
760 (let* ((element (org-element-at-point))
761 (post-affiliated (org-element-property :post-affiliated element)))
762 (cond
763 ;; Ignore checks in all affiliated keywords but captions.
764 ((< (point) post-affiliated)
765 (and (save-excursion
766 (beginning-of-line)
767 (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
768 (> (point) (match-end 0))
769 (org--flyspell-object-check-p element)))
770 ;; Ignore checks in LOGBOOK (or equivalent) drawer.
771 ((let ((log (org-log-into-drawer)))
772 (and log
773 (let ((drawer (org-element-lineage element '(drawer))))
774 (and drawer
775 (eq (compare-strings
776 log nil nil
777 (org-element-property :drawer-name drawer) nil nil t)
778 t)))))
779 nil)
781 (cl-case (org-element-type element)
782 ((comment quote-section) t)
783 (comment-block
784 ;; Allow checks between block markers, not on them.
785 (and (> (line-beginning-position) post-affiliated)
786 (save-excursion
787 (end-of-line)
788 (skip-chars-forward " \r\t\n")
789 (< (point) (org-element-property :end element)))))
790 ;; Arbitrary list of keywords where checks are meaningful.
791 ;; Make sure point is on the value part of the element.
792 (keyword
793 (and (member (org-element-property :key element)
794 '("DESCRIPTION" "TITLE"))
795 (save-excursion
796 (search-backward ":" (line-beginning-position) t))))
797 ;; Check is globally allowed in paragraphs verse blocks and
798 ;; table rows (after affiliated keywords) but some objects
799 ;; must not be affected.
800 ((paragraph table-row verse-block)
801 (let ((cbeg (org-element-property :contents-begin element))
802 (cend (org-element-property :contents-end element)))
803 (and cbeg (>= (point) cbeg) (< (point) cend)
804 (org--flyspell-object-check-p element))))))))))
805 (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
807 (defun org-remove-flyspell-overlays-in (beg end)
808 "Remove flyspell overlays in region."
809 (and (bound-and-true-p flyspell-mode)
810 (fboundp 'flyspell-delete-region-overlays)
811 (flyspell-delete-region-overlays beg end)))
813 (defvar flyspell-delayed-commands)
814 (eval-after-load "flyspell"
815 '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
817 ;;;; Bookmark
819 (defun org-bookmark-jump-unhide ()
820 "Unhide the current position, to show the bookmark location."
821 (and (derived-mode-p 'org-mode)
822 (or (org-invisible-p)
823 (save-excursion (goto-char (max (point-min) (1- (point))))
824 (org-invisible-p)))
825 (org-show-context 'bookmark-jump)))
827 ;; Make `bookmark-jump' shows the jump location if it was hidden.
828 (eval-after-load "bookmark"
829 '(if (boundp 'bookmark-after-jump-hook)
830 ;; We can use the hook
831 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
832 ;; Hook not available, use advice
833 (defadvice bookmark-jump (after org-make-visible activate)
834 "Make the position visible."
835 (org-bookmark-jump-unhide))))
837 ;;;; Saveplace
839 ;; Make sure saveplace shows the location if it was hidden
840 (eval-after-load "saveplace"
841 '(defadvice save-place-find-file-hook (after org-make-visible activate)
842 "Make the position visible."
843 (org-bookmark-jump-unhide)))
845 ;;;; Ecb
847 ;; Make sure ecb shows the location if it was hidden
848 (eval-after-load "ecb"
849 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
850 "Make hierarchy visible when jumping into location from ECB tree buffer."
851 (when (derived-mode-p 'org-mode)
852 (org-show-context))))
854 ;;;; Simple
856 (defun org-mark-jump-unhide ()
857 "Make the point visible with `org-show-context' after jumping to the mark."
858 (when (and (derived-mode-p 'org-mode)
859 (org-invisible-p))
860 (org-show-context 'mark-goto)))
862 (eval-after-load "simple"
863 '(defadvice pop-to-mark-command (after org-make-visible activate)
864 "Make the point visible with `org-show-context'."
865 (org-mark-jump-unhide)))
867 (eval-after-load "simple"
868 '(defadvice exchange-point-and-mark (after org-make-visible activate)
869 "Make the point visible with `org-show-context'."
870 (org-mark-jump-unhide)))
872 (eval-after-load "simple"
873 '(defadvice pop-global-mark (after org-make-visible activate)
874 "Make the point visible with `org-show-context'."
875 (org-mark-jump-unhide)))
877 ;;;; Session
879 ;; Make "session.el" ignore our circular variable.
880 (defvar session-globals-exclude)
881 (eval-after-load "session"
882 '(add-to-list 'session-globals-exclude 'org-mark-ring))
884 (provide 'org-compat)
886 ;;; org-compat.el ends here