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