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