Backport commit 65c8c7c from Emacs
[org-mode.git] / lisp / org-compat.el
blob208d209e4e40002d59f1ee78056458483b99975f
1 ;;; org-compat.el --- Compatibility code for Org-mode
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 XEmacs and older
28 ;; versions of GNU Emacs.
30 ;;; Code:
32 (eval-when-compile
33 (require 'cl))
35 (require 'org-macs)
37 ;; The following constant is for backward compatibility. We do not use
38 ;; it in org-mode, because the Byte compiler evaluates (featurep 'xemacs)
39 ;; at compilation time and can therefore optimize code better.
40 (defconst org-xemacs-p (featurep 'xemacs))
42 (defun org-compatible-face (inherits specs)
43 "Make a compatible face specification.
44 If INHERITS is an existing face and if the Emacs version supports it,
45 just inherit the face. If INHERITS is set and the Emacs version does
46 not support it, copy the face specification from the inheritance face.
47 If INHERITS is not given and SPECS is, use SPECS to define the face.
48 XEmacs and Emacs 21 do not know about the `min-colors' attribute.
49 For them we convert a (min-colors 8) entry to a `tty' entry and move it
50 to the top of the list. The `min-colors' attribute will be removed from
51 any other entries, and any resulting duplicates will be removed entirely."
52 (when (and inherits (facep inherits) (not specs))
53 (setq specs (or specs
54 (get inherits 'saved-face)
55 (get inherits 'face-defface-spec))))
56 (cond
57 ((and inherits (facep inherits)
58 (not (featurep 'xemacs))
59 (>= emacs-major-version 22)
60 ;; do not inherit outline faces before Emacs 23
61 (or (>= emacs-major-version 23)
62 (not (string-match "\\`outline-[0-9]+"
63 (symbol-name inherits)))))
64 (list (list t :inherit inherits)))
65 ((or (featurep 'xemacs) (< emacs-major-version 22))
66 ;; These do not understand the `min-colors' attribute.
67 (let (r e a)
68 (while (setq e (pop specs))
69 (cond
70 ((memq (car e) '(t default)) (push e r))
71 ((setq a (member '(min-colors 8) (car e)))
72 (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
73 (cdr e)))))
74 ((setq a (assq 'min-colors (car e)))
75 (setq e (cons (delq a (car e)) (cdr e)))
76 (or (assoc (car e) r) (push e r)))
77 (t (or (assoc (car e) r) (push e r)))))
78 (nreverse r)))
79 (t specs)))
80 (put 'org-compatible-face 'lisp-indent-function 1)
82 (defun org-version-check (version feature level)
83 (let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
84 (v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
85 (rmaj (or (nth 0 v1) 99))
86 (rmin (or (nth 1 v1) 99))
87 (rbld (or (nth 2 v1) 99))
88 (maj (or (nth 0 v2) 0))
89 (min (or (nth 1 v2) 0))
90 (bld (or (nth 2 v2) 0)))
91 (if (or (< maj rmaj)
92 (and (= maj rmaj)
93 (< min rmin))
94 (and (= maj rmaj)
95 (= min rmin)
96 (< bld rbld)))
97 (if (eq level :predicate)
98 ;; just return if we have the version
99 nil
100 (let ((msg (format "Emacs %s or greater is recommended for %s"
101 version feature)))
102 (display-warning 'org msg level)
104 t)))
107 ;;;; Emacs/XEmacs compatibility
109 (eval-and-compile
110 (defun org-defvaralias (new-alias base-variable &optional docstring)
111 "Compatibility function for defvaralias.
112 Don't do the aliasing when `defvaralias' is not bound."
113 (declare (indent 1))
114 (when (fboundp 'defvaralias)
115 (defvaralias new-alias base-variable docstring)))
117 (when (and (not (boundp 'user-emacs-directory))
118 (boundp 'user-init-directory))
119 (org-defvaralias 'user-emacs-directory 'user-init-directory)))
121 (when (featurep 'xemacs)
122 (defadvice custom-handle-keyword
123 (around org-custom-handle-keyword
124 activate preactivate)
125 "Remove custom keywords not recognized to avoid producing an error."
126 (cond
127 ((eq (ad-get-arg 1) :package-version))
128 (t ad-do-it)))
129 (defadvice define-obsolete-variable-alias
130 (around org-define-obsolete-variable-alias
131 (obsolete-name current-name &optional when docstring)
132 activate preactivate)
133 "Declare arguments defined in later versions of Emacs."
134 ad-do-it)
135 (defadvice define-obsolete-function-alias
136 (around org-define-obsolete-function-alias
137 (obsolete-name current-name &optional when docstring)
138 activate preactivate)
139 "Declare arguments defined in later versions of Emacs."
140 ad-do-it)
141 (defvar customize-package-emacs-version-alist nil)
142 (defvar temporary-file-directory (temp-directory)))
144 ;; Keys
145 (defconst org-xemacs-key-equivalents
146 '(([mouse-1] . [button1])
147 ([mouse-2] . [button2])
148 ([mouse-3] . [button3])
149 ([C-mouse-4] . [(control mouse-4)])
150 ([C-mouse-5] . [(control mouse-5)]))
151 "Translation alist for a couple of keys.")
153 ;; Overlay compatibility functions
154 (defun org-detach-overlay (ovl)
155 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
156 (defun org-overlay-display (ovl text &optional face evap)
157 "Make overlay OVL display TEXT with face FACE."
158 (if (featurep 'xemacs)
159 (let ((gl (make-glyph text)))
160 (and face (set-glyph-face gl face))
161 (set-extent-property ovl 'invisible t)
162 (set-extent-property ovl 'end-glyph gl))
163 (overlay-put ovl 'display text)
164 (if face (overlay-put ovl 'face face))
165 (if evap (overlay-put ovl 'evaporate t))))
166 (defun org-overlay-before-string (ovl text &optional face evap)
167 "Make overlay OVL display TEXT with face FACE."
168 (if (featurep 'xemacs)
169 (let ((gl (make-glyph text)))
170 (and face (set-glyph-face gl face))
171 (set-extent-property ovl 'begin-glyph gl))
172 (if face (org-add-props text nil 'face face))
173 (overlay-put ovl 'before-string text)
174 (if evap (overlay-put ovl 'evaporate t))))
175 (defun org-find-overlays (prop &optional pos delete)
176 "Find all overlays specifying PROP at POS or point.
177 If DELETE is non-nil, delete all those overlays."
178 (let ((overlays (overlays-at (or pos (point))))
179 ov found)
180 (while (setq ov (pop overlays))
181 (if (overlay-get ov prop)
182 (if delete (delete-overlay ov) (push ov found))))
183 found))
185 (defun org-get-x-clipboard (value)
186 "Get the value of the x or Windows clipboard, compatible with XEmacs, and GNU Emacs 21."
187 (cond ((eq window-system 'x)
188 (let ((x (org-get-x-clipboard-compat value)))
189 (if x (org-no-properties x))))
190 ((and (eq window-system 'w32) (fboundp 'w32-get-clipboard-data))
191 (w32-get-clipboard-data))))
193 (defsubst org-decompose-region (beg end)
194 "Decompose from BEG to END."
195 (if (featurep 'xemacs)
196 (let ((modified-p (buffer-modified-p))
197 (buffer-read-only nil))
198 (remove-text-properties beg end '(composition nil))
199 (set-buffer-modified-p modified-p))
200 (decompose-region beg end)))
202 ;; Miscellaneous functions
204 (defun org-add-hook (hook function &optional append local)
205 "Add-hook, compatible with both Emacsen."
206 (if (and local (featurep 'xemacs))
207 (add-local-hook hook function append)
208 (add-hook hook function append local)))
210 (defun org-add-props (string plist &rest props)
211 "Add text properties to entire string, from beginning to end.
212 PLIST may be a list of properties, PROPS are individual properties and values
213 that will be added to PLIST. Returns the string that was modified."
214 (add-text-properties
215 0 (length string) (if props (append plist props) plist) string)
216 string)
217 (put 'org-add-props 'lisp-indent-function 2)
219 (defun org-fit-window-to-buffer (&optional window max-height min-height
220 shrink-only)
221 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
222 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
223 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
224 `shrink-window-if-larger-than-buffer' instead, the height limit is
225 ignored in this case."
226 (cond ((if (fboundp 'window-full-width-p)
227 (not (window-full-width-p window))
228 ;; do nothing if another window would suffer
229 (> (frame-width) (window-width window))))
230 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
231 (fit-window-to-buffer window max-height min-height))
232 ((fboundp 'shrink-window-if-larger-than-buffer)
233 (shrink-window-if-larger-than-buffer window)))
234 (or window (selected-window)))
236 (defun org-number-sequence (from &optional to inc)
237 "Call `number-sequence' or emulate it."
238 (if (fboundp 'number-sequence)
239 (number-sequence from to inc)
240 (if (or (not to) (= from to))
241 (list from)
242 (or inc (setq inc 1))
243 (when (zerop inc) (error "The increment can not be zero"))
244 (let (seq (n 0) (next from))
245 (if (> inc 0)
246 (while (<= next to)
247 (setq seq (cons next seq)
248 n (1+ n)
249 next (+ from (* n inc))))
250 (while (>= next to)
251 (setq seq (cons next seq)
252 n (1+ n)
253 next (+ from (* n inc)))))
254 (nreverse seq)))))
256 ;; `set-transient-map' is only in Emacs >= 24.4
257 (defalias 'org-set-transient-map
258 (if (fboundp 'set-transient-map)
259 'set-transient-map
260 'set-temporary-overlay-map))
262 ;; Region compatibility
264 (defvar org-ignore-region nil
265 "Non-nil means temporarily disable the active region.")
267 (defun org-region-active-p ()
268 "Is `transient-mark-mode' on and the region active?
269 Works on both Emacs and XEmacs."
270 (if org-ignore-region
272 (if (featurep 'xemacs)
273 (and zmacs-regions (region-active-p))
274 (if (fboundp 'use-region-p)
275 (use-region-p)
276 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
278 (defun org-cursor-to-region-beginning ()
279 (when (and (org-region-active-p)
280 (> (point) (region-beginning)))
281 (exchange-point-and-mark)))
283 ;; Old alias for emacs 22 compatibility, now dropped
284 (define-obsolete-function-alias 'org-activate-mark 'activate-mark)
286 ;; Invisibility compatibility
288 (defun org-remove-from-invisibility-spec (arg)
289 "Remove elements from `buffer-invisibility-spec'."
290 (if (fboundp 'remove-from-invisibility-spec)
291 (remove-from-invisibility-spec arg)
292 (if (consp buffer-invisibility-spec)
293 (setq buffer-invisibility-spec
294 (delete arg buffer-invisibility-spec)))))
296 (defun org-in-invisibility-spec-p (arg)
297 "Is ARG a member of `buffer-invisibility-spec'?"
298 (if (consp buffer-invisibility-spec)
299 (member arg buffer-invisibility-spec)))
301 (defmacro org-xemacs-without-invisibility (&rest body)
302 "Turn off extents with invisibility while executing BODY."
303 `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
304 'all-extents-closed-open 'invisible))
305 ext-inv-specs)
306 (dolist (ext ext-inv)
307 (when (extent-property ext 'invisible)
308 (add-to-list 'ext-inv-specs (list ext (extent-property
309 ext 'invisible)))
310 (set-extent-property ext 'invisible nil)))
311 ,@body
312 (dolist (ext-inv-spec ext-inv-specs)
313 (set-extent-property (car ext-inv-spec) 'invisible
314 (cadr ext-inv-spec)))))
315 (def-edebug-spec org-xemacs-without-invisibility (body))
317 (defun org-indent-to-column (column &optional minimum buffer)
318 "Work around a bug with extents with invisibility in XEmacs."
319 (if (featurep 'xemacs)
320 (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
321 (indent-to-column column minimum)))
323 (defun org-indent-line-to (column)
324 "Work around a bug with extents with invisibility in XEmacs."
325 (if (featurep 'xemacs)
326 (org-xemacs-without-invisibility (indent-line-to column))
327 (indent-line-to column)))
329 (defun org-move-to-column (column &optional force buffer)
330 "Move to column COLUMN.
331 Pass COLUMN and FORCE to `move-to-column'.
332 Pass BUFFER to the XEmacs version of `move-to-column'."
333 (let ((buffer-invisibility-spec
334 (if (listp buffer-invisibility-spec)
335 (remove '(org-filtered) buffer-invisibility-spec)
336 buffer-invisibility-spec)))
337 (if (featurep 'xemacs)
338 (org-xemacs-without-invisibility
339 (move-to-column column force buffer))
340 (move-to-column column force))))
342 (defun org-get-x-clipboard-compat (value)
343 "Get the clipboard value on XEmacs or Emacs 21."
344 (cond ((featurep 'xemacs)
345 (org-no-warnings (get-selection-no-error value)))
346 ((fboundp 'x-get-selection)
347 (condition-case nil
348 (or (x-get-selection value 'UTF8_STRING)
349 (x-get-selection value 'COMPOUND_TEXT)
350 (x-get-selection value 'STRING)
351 (x-get-selection value 'TEXT))
352 (error nil)))))
354 (defun org-propertize (string &rest properties)
355 (if (featurep 'xemacs)
356 (progn
357 (add-text-properties 0 (length string) properties string)
358 string)
359 (apply 'propertize string properties)))
361 (defmacro org-find-library-dir (library)
362 `(file-name-directory (or (locate-library ,library) "")))
364 (defun org-count-lines (s)
365 "How many lines in string S?"
366 (let ((start 0) (n 1))
367 (while (string-match "\n" s start)
368 (setq start (match-end 0) n (1+ n)))
369 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
370 (setq n (1- n)))
373 (defun org-kill-new (string &rest args)
374 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
375 string)
376 (apply 'kill-new string args))
378 (defun org-select-frame-set-input-focus (frame)
379 "Select FRAME, raise it, and set input focus, if possible."
380 (cond ((featurep 'xemacs)
381 (if (fboundp 'select-frame-set-input-focus)
382 (select-frame-set-input-focus frame)
383 (raise-frame frame)
384 (select-frame frame)
385 (focus-frame frame)))
386 ;; `select-frame-set-input-focus' defined in Emacs 21 will not
387 ;; set the input focus.
388 ((>= emacs-major-version 22)
389 (select-frame-set-input-focus frame))
391 (raise-frame frame)
392 (select-frame frame)
393 (cond ((memq window-system '(x ns mac))
394 (x-focus-frame frame))
395 ((eq window-system 'w32)
396 (w32-focus-frame frame)))
397 (when focus-follows-mouse
398 (set-mouse-position frame (1- (frame-width frame)) 0)))))
400 (defalias 'org-float-time
401 (if (featurep 'xemacs) 'time-to-seconds 'float-time))
403 ;; `user-error' is only available from 24.2.50 on
404 (unless (fboundp 'user-error)
405 (defalias 'user-error 'error))
407 ;; ‘format-message’ is available only from 25 on
408 (unless (fboundp 'format-message)
409 (defalias 'format-message 'format))
411 ;; `font-lock-ensure' is only available from 24.4.50 on
412 (defalias 'org-font-lock-ensure
413 (if (fboundp 'font-lock-ensure)
414 #'font-lock-ensure
415 (lambda (&optional _beg _end) (font-lock-fontify-buffer))))
417 (defmacro org-no-popups (&rest body)
418 "Suppress popup windows.
419 Let-bind some variables to nil around BODY to achieve the desired
420 effect, which variables to use depends on the Emacs version."
421 (if (org-version-check "24.2.50" "" :predicate)
422 `(let (pop-up-frames display-buffer-alist)
423 ,@body)
424 `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
425 ,@body)))
427 (if (fboundp 'string-match-p)
428 (defalias 'org-string-match-p 'string-match-p)
429 (defun org-string-match-p (regexp string &optional start)
430 (save-match-data
431 (funcall 'string-match regexp string start))))
433 (if (fboundp 'looking-at-p)
434 (defalias 'org-looking-at-p 'looking-at-p)
435 (defun org-looking-at-p (&rest args)
436 (save-match-data
437 (apply 'looking-at args))))
439 ;; XEmacs does not have `looking-back'.
440 (if (fboundp 'looking-back)
441 (defalias 'org-looking-back 'looking-back)
442 (defun org-looking-back (regexp &optional limit greedy)
443 "Return non-nil if text before point matches regular expression REGEXP.
444 Like `looking-at' except matches before point, and is slower.
445 LIMIT if non-nil speeds up the search by specifying a minimum
446 starting position, to avoid checking matches that would start
447 before LIMIT.
449 If GREEDY is non-nil, extend the match backwards as far as
450 possible, stopping when a single additional previous character
451 cannot be part of a match for REGEXP. When the match is
452 extended, its starting position is allowed to occur before
453 LIMIT."
454 (let ((start (point))
455 (pos
456 (save-excursion
457 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
458 (point)))))
459 (if (and greedy pos)
460 (save-restriction
461 (narrow-to-region (point-min) start)
462 (while (and (> pos (point-min))
463 (save-excursion
464 (goto-char pos)
465 (backward-char 1)
466 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
467 (setq pos (1- pos)))
468 (save-excursion
469 (goto-char pos)
470 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
471 (not (null pos)))))
473 (defun org-floor* (x &optional y)
474 "Return a list of the floor of X and the fractional part of X.
475 With two arguments, return floor and remainder of their quotient."
476 (let ((q (floor x y)))
477 (list q (- x (if y (* y q) q)))))
479 ;; `pop-to-buffer-same-window' has been introduced in Emacs 24.1.
480 (defun org-pop-to-buffer-same-window
481 (&optional buffer-or-name norecord label)
482 "Pop to buffer specified by BUFFER-OR-NAME in the selected window."
483 (if (fboundp 'pop-to-buffer-same-window)
484 (funcall
485 'pop-to-buffer-same-window buffer-or-name norecord)
486 (funcall 'switch-to-buffer buffer-or-name norecord)))
488 ;; RECURSIVE has been introduced with Emacs 23.2.
489 ;; This is copying and adapted from `tramp-compat-delete-directory'
490 (defun org-delete-directory (directory &optional recursive)
491 "Compatibility function for `delete-directory'."
492 (if (null recursive)
493 (delete-directory directory)
494 (condition-case nil
495 (funcall 'delete-directory directory recursive)
496 ;; This Emacs version does not support the RECURSIVE flag. We
497 ;; use the implementation from Emacs 23.2.
498 (wrong-number-of-arguments
499 (setq directory (directory-file-name (expand-file-name directory)))
500 (if (not (file-symlink-p directory))
501 (mapc (lambda (file)
502 (if (eq t (car (file-attributes file)))
503 (org-delete-directory file recursive)
504 (delete-file file)))
505 (directory-files
506 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
507 (delete-directory directory)))))
509 ;;;###autoload
510 (defmacro org-check-version ()
511 "Try very hard to provide sensible version strings."
512 (let* ((org-dir (org-find-library-dir "org"))
513 (org-version.el (concat org-dir "org-version.el"))
514 (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
515 (if (require 'org-version org-version.el 'noerror)
516 '(progn
517 (autoload 'org-release "org-version.el")
518 (autoload 'org-git-version "org-version.el"))
519 (if (require 'org-fixup org-fixup.el 'noerror)
520 '(org-fixup)
521 ;; provide fallback definitions and complain
522 (warn "Could not define org version correctly. Check installation!")
523 '(progn
524 (defun org-release () "N/A")
525 (defun org-git-version () "N/A !!check installation!!"))))))
527 (defun org-file-equal-p (f1 f2)
528 "Return t if files F1 and F2 are the same.
529 Implements `file-equal-p' for older emacsen and XEmacs."
530 (if (fboundp 'file-equal-p)
531 (file-equal-p f1 f2)
532 (let (f1-attr f2-attr)
533 (and (setq f1-attr (file-attributes (file-truename f1)))
534 (setq f2-attr (file-attributes (file-truename f2)))
535 (equal f1-attr f2-attr)))))
537 ;; `buffer-narrowed-p' is available for Emacs >=24.3
538 (defun org-buffer-narrowed-p ()
539 "Compatibility function for `buffer-narrowed-p'."
540 (if (fboundp 'buffer-narrowed-p)
541 (buffer-narrowed-p)
542 (/= (- (point-max) (point-min)) (buffer-size))))
544 ;; As of Emacs 25.1, `outline-mode` functions are under the 'outline-'
545 ;; prefix.
546 (when (< emacs-major-version 25)
547 (defalias 'outline-show-all 'show-all)
548 (defalias 'outline-hide-subtree 'hide-subtree)
549 (defalias 'outline-show-subtree 'show-subtree)
550 (defalias 'outline-show-branches 'show-branches)
551 (defalias 'outline-show-children 'show-children)
552 (defalias 'outline-show-entry 'show-entry)
553 (defalias 'outline-hide-entry 'hide-entry)
554 (defalias 'outline-hide-sublevels 'hide-sublevels))
556 (defmacro org-with-silent-modifications (&rest body)
557 (if (fboundp 'with-silent-modifications)
558 `(with-silent-modifications ,@body)
559 `(org-unmodified ,@body)))
560 (def-edebug-spec org-with-silent-modifications (body))
562 (provide 'org-compat)
564 ;;; org-compat.el ends here