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