ORG-NEWS: Backport commit 62803a2ef from Emacs
[org-mode.git] / lisp / org-compat.el
blobe1d40369f196b0bad417c02475e0455bc196ed42
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 <http://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-at-table.el-p "org" (&optional table-type))
36 (declare-function org-element-at-point "org-element" ())
37 (declare-function org-element-type "org-element" (element))
38 (declare-function org-link-set-parameters "org" (type &rest rest))
39 (declare-function org-table-end (&optional table-type))
40 (declare-function table--at-cell-p "table" (position &optional object at-column))
42 (defvar org-table-any-border-regexp)
43 (defvar org-table-dataline-regexp)
44 (defvar org-table-tab-recognizes-table.el)
45 (defvar org-table1-hline-regexp)
47 ;; As of Emacs 25.1, `outline-mode' functions are under the 'outline-'
48 ;; prefix, `find-tag' is replaced with `xref-find-definition' and
49 ;; `x-get-selection' with `gui-get-selection'.
50 (when (< emacs-major-version 25)
51 (defalias 'outline-hide-entry 'hide-entry)
52 (defalias 'outline-hide-sublevels 'hide-sublevels)
53 (defalias 'outline-hide-subtree 'hide-subtree)
54 (defalias 'outline-show-all 'show-all)
55 (defalias 'outline-show-branches 'show-branches)
56 (defalias 'outline-show-children 'show-children)
57 (defalias 'outline-show-entry 'show-entry)
58 (defalias 'outline-show-subtree 'show-subtree)
59 (defalias 'xref-find-definitions 'find-tag)
60 (defalias 'format-message 'format)
61 (defalias 'gui-get-selection 'x-get-selection))
64 ;;; Obsolete aliases (remove them after the next major release).
66 ;;;; XEmacs compatibility, now removed.
67 (define-obsolete-function-alias 'org-activate-mark 'activate-mark)
68 (define-obsolete-function-alias 'org-add-hook 'add-hook "Org 9.0")
69 (define-obsolete-function-alias 'org-bound-and-true-p 'bound-and-true-p "Org 9.0")
70 (define-obsolete-function-alias 'org-decompose-region 'decompose-region "Org 9.0")
71 (define-obsolete-function-alias 'org-defvaralias 'defvaralias "Org 9.0")
72 (define-obsolete-function-alias 'org-detach-overlay 'delete-overlay "Org 9.0")
73 (define-obsolete-function-alias 'org-file-equal-p 'file-equal-p "Org 9.0")
74 (define-obsolete-function-alias 'org-float-time 'float-time "Org 9.0")
75 (define-obsolete-function-alias 'org-indent-line-to 'indent-line-to "Org 9.0")
76 (define-obsolete-function-alias 'org-indent-to-column 'indent-to-column "Org 9.0")
77 (define-obsolete-function-alias 'org-looking-at-p 'looking-at-p "Org 9.0")
78 (define-obsolete-function-alias 'org-looking-back 'looking-back "Org 9.0")
79 (define-obsolete-function-alias 'org-match-string-no-properties 'match-string-no-properties "Org 9.0")
80 (define-obsolete-function-alias 'org-propertize 'propertize "Org 9.0")
81 (define-obsolete-function-alias 'org-select-frame-set-input-focus 'select-frame-set-input-focus "Org 9.0")
83 (defmacro org-re (s)
84 "Replace posix classes in regular expression S."
85 (declare (debug (form))
86 (obsolete "you can safely remove it." "Org 9.0"))
89 ;;;; Functions from cl-lib that Org used to have its own implementation of.
90 (define-obsolete-function-alias 'org-count 'cl-count "Org 9.0")
91 (define-obsolete-function-alias 'org-every 'cl-every "Org 9.0")
92 (define-obsolete-function-alias 'org-find-if 'cl-find-if "Org 9.0")
93 (define-obsolete-function-alias 'org-reduce 'cl-reduce "Org 9.0")
94 (define-obsolete-function-alias 'org-remove-if 'cl-remove-if "Org 9.0")
95 (define-obsolete-function-alias 'org-remove-if-not 'cl-remove-if-not "Org 9.0")
96 (define-obsolete-function-alias 'org-some 'cl-some "Org 9.0")
97 (define-obsolete-function-alias 'org-floor* 'cl-floor "Org 9.0")
99 (defun org-sublist (list start end)
100 "Return a section of LIST, from START to END.
101 Counting starts at 1."
102 (cl-subseq list (1- start) end))
103 (make-obsolete 'org-sublist
104 "use cl-subseq (note the 0-based counting)."
105 "Org 9.0")
108 ;;;; Functions available since Emacs 24.3
109 (define-obsolete-function-alias 'org-buffer-narrowed-p 'buffer-narrowed-p "Org 9.0")
110 (define-obsolete-function-alias 'org-called-interactively-p 'called-interactively-p "Org 9.0")
111 (define-obsolete-function-alias 'org-char-to-string 'char-to-string "Org 9.0")
112 (define-obsolete-function-alias 'org-delete-directory 'delete-directory "Org 9.0")
113 (define-obsolete-function-alias 'org-format-seconds 'format-seconds "Org 9.0")
114 (define-obsolete-function-alias 'org-link-escape-browser 'url-encode-url "Org 9.0")
115 (define-obsolete-function-alias 'org-no-warnings 'with-no-warnings "Org 9.0")
116 (define-obsolete-function-alias 'org-number-sequence 'number-sequence "Org 9.0")
117 (define-obsolete-function-alias 'org-pop-to-buffer-same-window 'pop-to-buffer-same-window "Org 9.0")
118 (define-obsolete-function-alias 'org-string-match-p 'string-match-p "Org 9.0")
120 ;;;; Functions and variables from previous releases now obsolete.
121 (define-obsolete-function-alias 'org-element-remove-indentation
122 'org-remove-indentation "Org 9.0")
123 (define-obsolete-variable-alias 'org-hierarchical-checkbox-statistics
124 'org-checkbox-hierarchical-statistics "Org 8.0")
125 (define-obsolete-variable-alias 'org-description-max-indent
126 'org-list-description-max-indent "Org 8.0")
127 (define-obsolete-variable-alias 'org-latex-create-formula-image-program
128 'org-preview-latex-default-process "Org 9.0")
129 (define-obsolete-variable-alias 'org-latex-preview-ltxpng-directory
130 'org-preview-latex-image-directory "Org 9.0")
131 (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
132 (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 9.0")
133 (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp "Org 8.3")
134 (define-obsolete-function-alias 'org-speed-command-default-hook
135 'org-speed-command-activate "Org 8.0")
136 (define-obsolete-function-alias 'org-babel-speed-command-hook
137 'org-babel-speed-command-activate "Org 8.0")
138 (define-obsolete-function-alias 'org-image-file-name-regexp
139 'image-file-name-regexp "Org 9.0")
140 (define-obsolete-function-alias 'org-get-legal-level
141 'org-get-valid-level "Org 7.8")
142 (define-obsolete-function-alias 'org-completing-read-no-i
143 'completing-read "Org 9.0")
144 (define-obsolete-function-alias 'org-icompleting-read
145 'completing-read "Org 9.0")
146 (define-obsolete-function-alias 'org-iread-file-name 'read-file-name "Org 9.0")
147 (define-obsolete-function-alias 'org-days-to-time
148 'org-time-stamp-to-now "Org 8.2")
149 (define-obsolete-variable-alias 'org-agenda-ignore-drawer-properties
150 'org-agenda-ignore-properties "Org 9.0")
151 (define-obsolete-function-alias 'org-preview-latex-fragment
152 'org-toggle-latex-fragment "Org 8.3")
153 (define-obsolete-function-alias 'org-display-inline-modification-hook
154 'org-display-inline-remove-overlay "Org 8.0")
155 (define-obsolete-function-alias 'org-export-get-genealogy
156 'org-element-lineage "Org 9.0")
157 (define-obsolete-variable-alias 'org-latex-with-hyperref
158 'org-latex-hyperref-template "Org 9.0")
159 (define-obsolete-variable-alias 'org-link-to-org-use-id
160 'org-id-link-to-org-use-id "Org 8.0")
161 (define-obsolete-variable-alias 'hfy-optimisations 'hfy-optimizations "Org 9.0")
162 (define-obsolete-variable-alias 'org-clock-modeline-total
163 'org-clock-mode-line-total "Org 8.0")
164 (define-obsolete-function-alias 'org-protocol-unhex-compound
165 'org-link-unescape-compound "Org 7.8")
166 (define-obsolete-function-alias 'org-protocol-unhex-string
167 'org-link-unescape "Org 7.8")
168 (define-obsolete-function-alias 'org-protocol-unhex-single-byte-sequence
169 'org-link-unescape-single-byte-sequence "Org 7.8")
170 (define-obsolete-variable-alias 'org-export-htmlized-org-css-url
171 'org-org-htmlized-css-url "Org 8.2")
172 (define-obsolete-variable-alias 'org-alphabetical-lists
173 'org-list-allow-alphabetical "Org 8.0")
174 (define-obsolete-function-alias 'org-list-parse-list 'org-list-to-lisp "Org 9.0")
175 (define-obsolete-variable-alias 'org-agenda-menu-two-column
176 'org-agenda-menu-two-columns "Org 8.0")
177 (define-obsolete-variable-alias 'org-finalize-agenda-hook
178 'org-agenda-finalize-hook "Org 8.0")
179 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "Org 7.8")
180 (define-obsolete-function-alias 'org-agenda-post-command-hook
181 'org-agenda-update-agenda-type "Org 8.0")
182 (define-obsolete-function-alias 'org-agenda-todayp
183 'org-agenda-today-p "Org 9.0")
184 (define-obsolete-function-alias 'org-babel-examplize-region
185 'org-babel-examplify-region "Org 9.0")
186 (define-obsolete-function-alias 'org-babel-trim 'org-trim "Org 9.0")
187 (define-obsolete-variable-alias 'org-html-style-include-scripts
188 'org-html-head-include-scripts "Org 8.0")
189 (define-obsolete-variable-alias 'org-html-style-include-default
190 'org-html-head-include-default-style "Org 8.0")
191 (define-obsolete-variable-alias 'org-html-style 'org-html-head "24.4")
192 (define-obsolete-function-alias 'org-insert-columns-dblock
193 'org-columns-insert-dblock "Org 9.0")
194 (define-obsolete-function-alias 'org-activate-bracket-links
195 'org-activate-links "Org 9.0")
196 (define-obsolete-function-alias 'org-activate-plain-links 'ignore "Org 9.0")
197 (define-obsolete-function-alias 'org-activate-angle-links 'ignore "Org 9.0")
199 (defun org-in-fixed-width-region-p ()
200 "Non-nil if point in a fixed-width region."
201 (save-match-data
202 (eq 'fixed-width (org-element-type (org-element-at-point)))))
203 (make-obsolete 'org-in-fixed-width-region-p
204 "use `org-element' library"
205 "Org 9.0")
207 (defcustom org-read-date-minibuffer-setup-hook nil
208 "Hook to be used to set up keys for the date/time interface.
209 Add key definitions to `minibuffer-local-map', which will be a
210 temporary copy."
211 :group 'org-time
212 :type 'hook)
213 (make-obsolete-variable
214 'org-read-date-minibuffer-setup-hook
215 "set `org-read-date-minibuffer-local-map' instead." "Org 8.0")
217 (defun org-compatible-face (inherits specs)
218 "Make a compatible face specification.
219 If INHERITS is an existing face and if the Emacs version supports
220 it, just inherit the face. If INHERITS is not given and SPECS
221 is, use SPECS to define the face."
222 (declare (indent 1))
223 (if (facep inherits)
224 (list (list t :inherit inherits))
225 specs))
226 (make-obsolete 'org-compatible-face "you can remove it." "Org 9.0")
228 (defun org-add-link-type (type &optional follow export)
229 "Add a new TYPE link.
230 FOLLOW and EXPORT are two functions.
232 FOLLOW should take the link path as the single argument and do whatever
233 is necessary to follow the link, for example find a file or display
234 a mail message.
236 EXPORT should format the link path for export to one of the export formats.
237 It should be a function accepting three arguments:
239 path the path of the link, the text after the prefix (like \"http:\")
240 desc the description of the link, if any
241 format the export format, a symbol like `html' or `latex' or `ascii'.
243 The function may use the FORMAT information to return different values
244 depending on the format. The return value will be put literally into
245 the exported file. If the return value is nil, this means Org should
246 do what it normally does with links which do not have EXPORT defined.
248 Org mode has a built-in default for exporting links. If you are happy with
249 this default, there is no need to define an export function for the link
250 type. For a simple example of an export function, see `org-bbdb.el'.
252 If TYPE already exists, update it with the arguments.
253 See `org-link-parameters' for documentation on the other parameters."
254 (org-link-set-parameters type :follow follow :export export)
255 (message "Created %s link." type))
257 (make-obsolete 'org-add-link-type "use `org-link-set-parameters' instead." "Org 9.0")
259 (defun org-table-recognize-table.el ()
260 "If there is a table.el table nearby, recognize it and move into it."
261 (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
262 (beginning-of-line)
263 (unless (or (looking-at org-table-dataline-regexp)
264 (not (looking-at org-table1-hline-regexp)))
265 (forward-line)
266 (when (looking-at org-table-any-border-regexp)
267 (forward-line -2)))
268 (if (re-search-forward "|" (org-table-end t) t)
269 (progn
270 (require 'table)
271 (if (table--at-cell-p (point)) t
272 (message "recognizing table.el table...")
273 (table-recognize-table)
274 (message "recognizing table.el table...done")))
275 (error "This should not happen"))))
277 ;; Not used by Org core since commit 6d1e3082, Feb 2010.
278 (make-obsolete 'org-table-recognize-table.el
279 "please notify the org mailing list if you use this function."
280 "Org 9.0")
282 (define-obsolete-function-alias
283 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string "Org 8.0")
285 (defun org-remove-angle-brackets (s)
286 (org-unbracket-string "<" ">" s))
287 (make-obsolete 'org-remove-angle-brackets 'org-unbracket-string "Org 9.0")
289 (defun org-remove-double-quotes (s)
290 (org-unbracket-string "\"" "\"" s))
291 (make-obsolete 'org-remove-double-quotes 'org-unbracket-string "Org 9.0")
293 (define-obsolete-function-alias 'org-babel-number-p
294 'org-babel--string-to-number "Org 9.0")
298 ;;;; Obsolete link types
300 (eval-after-load 'org
301 '(progn
302 (org-link-set-parameters "file+emacs") ;since Org 9.0
303 (org-link-set-parameters "file+sys"))) ;since Org 9.0
307 ;;; Miscellaneous functions
309 (defun org-version-check (version feature level)
310 (let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
311 (v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
312 (rmaj (or (nth 0 v1) 99))
313 (rmin (or (nth 1 v1) 99))
314 (rbld (or (nth 2 v1) 99))
315 (maj (or (nth 0 v2) 0))
316 (min (or (nth 1 v2) 0))
317 (bld (or (nth 2 v2) 0)))
318 (if (or (< maj rmaj)
319 (and (= maj rmaj)
320 (< min rmin))
321 (and (= maj rmaj)
322 (= min rmin)
323 (< bld rbld)))
324 (if (eq level :predicate)
325 ;; just return if we have the version
327 (let ((msg (format "Emacs %s or greater is recommended for %s"
328 version feature)))
329 (display-warning 'org msg level)
331 t)))
333 (defun org-get-x-clipboard (value)
334 "Get the value of the X or Windows clipboard."
335 (cond ((and (eq window-system 'x)
336 (fboundp 'gui-get-selection)) ;Silence byte-compiler.
337 (org-no-properties
338 (ignore-errors
339 (or (gui-get-selection value 'UTF8_STRING)
340 (gui-get-selection value 'COMPOUND_TEXT)
341 (gui-get-selection value 'STRING)
342 (gui-get-selection value 'TEXT)))))
343 ((and (eq window-system 'w32) (fboundp 'w32-get-clipboard-data))
344 (w32-get-clipboard-data))))
346 (defun org-add-props (string plist &rest props)
347 "Add text properties to entire string, from beginning to end.
348 PLIST may be a list of properties, PROPS are individual properties and values
349 that will be added to PLIST. Returns the string that was modified."
350 (add-text-properties
351 0 (length string) (if props (append plist props) plist) string)
352 string)
353 (put 'org-add-props 'lisp-indent-function 2)
355 (defun org-fit-window-to-buffer (&optional window max-height min-height
356 shrink-only)
357 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
358 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
359 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
360 `shrink-window-if-larger-than-buffer' instead, the height limit is
361 ignored in this case."
362 (cond ((if (fboundp 'window-full-width-p)
363 (not (window-full-width-p window))
364 ;; do nothing if another window would suffer
365 (> (frame-width) (window-width window))))
366 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
367 (fit-window-to-buffer window max-height min-height))
368 ((fboundp 'shrink-window-if-larger-than-buffer)
369 (shrink-window-if-larger-than-buffer window)))
370 (or window (selected-window)))
372 ;; `set-transient-map' is only in Emacs >= 24.4
373 (defalias 'org-set-transient-map
374 (if (fboundp 'set-transient-map)
375 'set-transient-map
376 'set-temporary-overlay-map))
378 ;;; Region compatibility
380 (defvar org-ignore-region nil
381 "Non-nil means temporarily disable the active region.")
383 (defun org-region-active-p ()
384 "Non-nil when the region active.
385 Unlike to `use-region-p', this function also checks
386 `org-ignore-region'."
387 (and (not org-ignore-region) (use-region-p)))
389 (defun org-cursor-to-region-beginning ()
390 (when (and (org-region-active-p)
391 (> (point) (region-beginning)))
392 (exchange-point-and-mark)))
394 ;;; Invisibility compatibility
396 (defun org-remove-from-invisibility-spec (arg)
397 "Remove elements from `buffer-invisibility-spec'."
398 (if (fboundp 'remove-from-invisibility-spec)
399 (remove-from-invisibility-spec arg)
400 (if (consp buffer-invisibility-spec)
401 (setq buffer-invisibility-spec
402 (delete arg buffer-invisibility-spec)))))
404 (defun org-in-invisibility-spec-p (arg)
405 "Is ARG a member of `buffer-invisibility-spec'?"
406 (if (consp buffer-invisibility-spec)
407 (member arg buffer-invisibility-spec)))
409 (defun org-move-to-column (column &optional force _buffer)
410 "Move to column COLUMN.
411 Pass COLUMN and FORCE to `move-to-column'."
412 (let ((buffer-invisibility-spec
413 (if (listp buffer-invisibility-spec)
414 (remove '(org-filtered) buffer-invisibility-spec)
415 buffer-invisibility-spec)))
416 (move-to-column column force)))
418 (defmacro org-find-library-dir (library)
419 `(file-name-directory (or (locate-library ,library) "")))
421 (defun org-count-lines (s)
422 "How many lines in string S?"
423 (let ((start 0) (n 1))
424 (while (string-match "\n" s start)
425 (setq start (match-end 0) n (1+ n)))
426 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
427 (setq n (1- n)))
430 (defun org-kill-new (string &rest args)
431 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
432 string)
433 (apply 'kill-new string args))
435 ;; `font-lock-ensure' is only available from 24.4.50 on
436 (defalias 'org-font-lock-ensure
437 (if (fboundp 'font-lock-ensure)
438 #'font-lock-ensure
439 (lambda (&optional _beg _end)
440 (with-no-warnings (font-lock-fontify-buffer)))))
442 ;; `file-local-name' was added in Emacs 26.1.
443 (defalias 'org-babel-local-file-name
444 (if (fboundp 'file-local-name)
445 'file-local-name
446 (lambda (file)
447 "Return the local name component of FILE."
448 (or (file-remote-p file 'localname) file))))
450 (defmacro org-no-popups (&rest body)
451 "Suppress popup windows.
452 Let-bind some variables to nil around BODY to achieve the desired
453 effect, which variables to use depends on the Emacs version."
454 (if (org-version-check "24.2.50" "" :predicate)
455 `(let (pop-up-frames display-buffer-alist)
456 ,@body)
457 `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
458 ,@body)))
460 ;;;###autoload
461 (defmacro org-check-version ()
462 "Try very hard to provide sensible version strings."
463 (let* ((org-dir (org-find-library-dir "org"))
464 (org-version.el (concat org-dir "org-version.el"))
465 (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
466 (if (require 'org-version org-version.el 'noerror)
467 '(progn
468 (autoload 'org-release "org-version.el")
469 (autoload 'org-git-version "org-version.el"))
470 (if (require 'org-fixup org-fixup.el 'noerror)
471 '(org-fixup)
472 ;; provide fallback definitions and complain
473 (warn "Could not define org version correctly. Check installation!")
474 '(progn
475 (defun org-release () "N/A")
476 (defun org-git-version () "N/A !!check installation!!"))))))
478 (defmacro org-with-silent-modifications (&rest body)
479 (if (fboundp 'with-silent-modifications)
480 `(with-silent-modifications ,@body)
481 `(org-unmodified ,@body)))
482 (def-edebug-spec org-with-silent-modifications (body))
484 ;; Functions for Emacs < 24.4 compatibility
485 (defun org-define-error (name message)
486 "Define NAME as a new error signal.
487 MESSAGE is a string that will be output to the echo area if such
488 an error is signaled without being caught by a `condition-case'.
489 Implements `define-error' for older emacsen."
490 (if (fboundp 'define-error) (define-error name message)
491 (put name 'error-conditions
492 (copy-sequence (cons name (get 'error 'error-conditions))))))
494 (unless (fboundp 'string-suffix-p)
495 ;; From Emacs subr.el.
496 (defun string-suffix-p (suffix string &optional ignore-case)
497 "Return non-nil if SUFFIX is a suffix of STRING.
498 If IGNORE-CASE is non-nil, the comparison is done without paying
499 attention to case differences."
500 (let ((start-pos (- (length string) (length suffix))))
501 (and (>= start-pos 0)
502 (eq t (compare-strings suffix nil nil
503 string start-pos nil ignore-case))))))
505 (provide 'org-compat)
507 ;;; org-compat.el ends here