Compatibility: XEmacs does not have user-emacs-directory, use user-init-directory...
[org-mode.git] / lisp / org-compat.el
blob2d200b79e7079555a53d6e0760e6ac9dcf6a5343
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 (eval-and-compile
117 (when (and (not (boundp 'user-emacs-directory))
118 (boundp 'user-init-directory))
119 (defvaralias 'user-emacs-directory 'user-init-directory)))
121 ;; Keys
122 (defconst org-xemacs-key-equivalents
123 '(([mouse-1] . [button1])
124 ([mouse-2] . [button2])
125 ([mouse-3] . [button3])
126 ([C-mouse-4] . [(control mouse-4)])
127 ([C-mouse-5] . [(control mouse-5)]))
128 "Translation alist for a couple of keys.")
130 ;; Overlay compatibility functions
131 (defun org-detach-overlay (ovl)
132 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
133 (defun org-overlay-display (ovl text &optional face evap)
134 "Make overlay OVL display TEXT with face FACE."
135 (if (featurep 'xemacs)
136 (let ((gl (make-glyph text)))
137 (and face (set-glyph-face gl face))
138 (set-extent-property ovl 'invisible t)
139 (set-extent-property ovl 'end-glyph gl))
140 (overlay-put ovl 'display text)
141 (if face (overlay-put ovl 'face face))
142 (if evap (overlay-put ovl 'evaporate t))))
143 (defun org-overlay-before-string (ovl text &optional face evap)
144 "Make overlay OVL display TEXT with face FACE."
145 (if (featurep 'xemacs)
146 (let ((gl (make-glyph text)))
147 (and face (set-glyph-face gl face))
148 (set-extent-property ovl 'begin-glyph gl))
149 (if face (org-add-props text nil 'face face))
150 (overlay-put ovl 'before-string text)
151 (if evap (overlay-put ovl 'evaporate t))))
152 (defun org-find-overlays (prop &optional pos delete)
153 "Find all overlays specifying PROP at POS or point.
154 If DELETE is non-nil, delete all those overlays."
155 (let ((overlays (overlays-at (or pos (point))))
156 ov found)
157 (while (setq ov (pop overlays))
158 (if (overlay-get ov prop)
159 (if delete (delete-overlay ov) (push ov found))))
160 found))
162 (defun org-get-x-clipboard (value)
163 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
164 (if (eq window-system 'x)
165 (let ((x (org-get-x-clipboard-compat value)))
166 (if x (org-no-properties x)))))
168 (defsubst org-decompose-region (beg end)
169 "Decompose from BEG to END."
170 (if (featurep 'xemacs)
171 (let ((modified-p (buffer-modified-p))
172 (buffer-read-only nil))
173 (remove-text-properties beg end '(composition nil))
174 (set-buffer-modified-p modified-p))
175 (decompose-region beg end)))
177 (defmacro org-define-obsolete-function-alias (o-name c-name when &optional doc)
178 "Reconcile the two-argument form of
179 `define-obsolete-function-alias' in XEmacs/Emacs 22 with the 3-4
180 argument form in Emacs 23 and later."
181 (if (or (featurep 'xemacs)
182 (< emacs-major-version 23))
183 `(define-obsolete-function-alias ,o-name ,c-name)
184 `(define-obsolete-function-alias ,o-name ,c-name ,when ,doc)))
186 (defmacro org-define-obsolete-variable-alias (o-name c-name when &optional doc)
187 "Reconcile the two-argument form of
188 `define-obsolete-variable-alias' in XEmacs/Emacs 22 with the 3-4
189 argument form in Emacs 23 and later."
190 (if (or (featurep 'xemacs)
191 (< emacs-major-version 23))
192 `(define-obsolete-variable-alias ,o-name ,c-name)
193 `(define-obsolete-variable-alias ,o-name ,c-name ,when ,doc)))
195 ;; Miscellaneous functions
197 (defun org-add-hook (hook function &optional append local)
198 "Add-hook, compatible with both Emacsen."
199 (if (and local (featurep 'xemacs))
200 (add-local-hook hook function append)
201 (add-hook hook function append local)))
203 (defun org-add-props (string plist &rest props)
204 "Add text properties to entire string, from beginning to end.
205 PLIST may be a list of properties, PROPS are individual properties and values
206 that will be added to PLIST. Returns the string that was modified."
207 (add-text-properties
208 0 (length string) (if props (append plist props) plist) string)
209 string)
210 (put 'org-add-props 'lisp-indent-function 2)
212 (defun org-fit-window-to-buffer (&optional window max-height min-height
213 shrink-only)
214 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
215 WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
216 passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
217 `shrink-window-if-larger-than-buffer' instead, the height limit is
218 ignored in this case."
219 (cond ((if (fboundp 'window-full-width-p)
220 (not (window-full-width-p window))
221 ;; do nothing if another window would suffer
222 (> (frame-width) (window-width window))))
223 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
224 (fit-window-to-buffer window max-height min-height))
225 ((fboundp 'shrink-window-if-larger-than-buffer)
226 (shrink-window-if-larger-than-buffer window)))
227 (or window (selected-window)))
229 (defun org-number-sequence (from &optional to inc)
230 "Call `number-sequence or emulate it."
231 (if (fboundp 'number-sequence)
232 (number-sequence from to inc)
233 (if (or (not to) (= from to))
234 (list from)
235 (or inc (setq inc 1))
236 (when (zerop inc) (error "The increment can not be zero"))
237 (let (seq (n 0) (next from))
238 (if (> inc 0)
239 (while (<= next to)
240 (setq seq (cons next seq)
241 n (1+ n)
242 next (+ from (* n inc))))
243 (while (>= next to)
244 (setq seq (cons next seq)
245 n (1+ n)
246 next (+ from (* n inc)))))
247 (nreverse seq)))))
249 ;; Region compatibility
251 (defvar org-ignore-region nil
252 "To temporarily disable the active region.")
254 (defun org-region-active-p ()
255 "Is `transient-mark-mode' on and the region active?
256 Works on both Emacs and XEmacs."
257 (if org-ignore-region
259 (if (featurep 'xemacs)
260 (and zmacs-regions (region-active-p))
261 (if (fboundp 'use-region-p)
262 (use-region-p)
263 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
265 (defun org-cursor-to-region-beginning ()
266 (when (and (org-region-active-p)
267 (> (point) (region-beginning)))
268 (exchange-point-and-mark)))
270 ;; Emacs 22 misses `activate-mark'
271 (if (fboundp 'activate-mark)
272 (defalias 'org-activate-mark 'activate-mark)
273 (defun org-activate-mark ()
274 (when (mark t)
275 (setq mark-active t)
276 (when (and (boundp 'transient-mark-mode)
277 (not transient-mark-mode))
278 (setq transient-mark-mode 'lambda))
279 (when (boundp 'zmacs-regions)
280 (setq zmacs-regions t)))))
282 ;; Invisibility compatibility
284 (defun org-remove-from-invisibility-spec (arg)
285 "Remove elements from `buffer-invisibility-spec'."
286 (if (fboundp 'remove-from-invisibility-spec)
287 (remove-from-invisibility-spec arg)
288 (if (consp buffer-invisibility-spec)
289 (setq buffer-invisibility-spec
290 (delete arg buffer-invisibility-spec)))))
292 (defun org-in-invisibility-spec-p (arg)
293 "Is ARG a member of `buffer-invisibility-spec'?"
294 (if (consp buffer-invisibility-spec)
295 (member arg buffer-invisibility-spec)
296 nil))
298 (defmacro org-xemacs-without-invisibility (&rest body)
299 "Turn off extents with invisibility while executing BODY."
300 `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
301 'all-extents-closed-open 'invisible))
302 ext-inv-specs)
303 (dolist (ext ext-inv)
304 (when (extent-property ext 'invisible)
305 (add-to-list 'ext-inv-specs (list ext (extent-property
306 ext 'invisible)))
307 (set-extent-property ext 'invisible nil)))
308 ,@body
309 (dolist (ext-inv-spec ext-inv-specs)
310 (set-extent-property (car ext-inv-spec) 'invisible
311 (cadr ext-inv-spec)))))
312 (def-edebug-spec org-xemacs-without-invisibility (body))
314 (defun org-indent-to-column (column &optional minimum buffer)
315 "Work around a bug with extents with invisibility in XEmacs."
316 (if (featurep 'xemacs)
317 (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
318 (indent-to-column column minimum)))
320 (defun org-indent-line-to (column)
321 "Work around a bug with extents with invisibility in XEmacs."
322 (if (featurep 'xemacs)
323 (org-xemacs-without-invisibility (indent-line-to column))
324 (indent-line-to column)))
326 (defun org-move-to-column (column &optional force buffer)
327 (if (featurep 'xemacs)
328 (org-xemacs-without-invisibility (move-to-column column force buffer))
329 (move-to-column column force)))
331 (defun org-get-x-clipboard-compat (value)
332 "Get the clipboard value on XEmacs or Emacs 21."
333 (cond ((featurep 'xemacs)
334 (org-no-warnings (get-selection-no-error value)))
335 ((fboundp 'x-get-selection)
336 (condition-case nil
337 (or (x-get-selection value 'UTF8_STRING)
338 (x-get-selection value 'COMPOUND_TEXT)
339 (x-get-selection value 'STRING)
340 (x-get-selection value 'TEXT))
341 (error nil)))))
343 (defun org-propertize (string &rest properties)
344 (if (featurep 'xemacs)
345 (progn
346 (add-text-properties 0 (length string) properties string)
347 string)
348 (apply 'propertize string properties)))
350 (defmacro org-find-library-dir (library)
351 `(file-name-directory (or (locate-library ,library) "")))
353 (defun org-count-lines (s)
354 "How many lines in string S?"
355 (let ((start 0) (n 1))
356 (while (string-match "\n" s start)
357 (setq start (match-end 0) n (1+ n)))
358 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
359 (setq n (1- n)))
362 (defun org-kill-new (string &rest args)
363 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
364 string)
365 (apply 'kill-new string args))
367 (defun org-select-frame-set-input-focus (frame)
368 "Select FRAME, raise it, and set input focus, if possible."
369 (cond ((featurep 'xemacs)
370 (if (fboundp 'select-frame-set-input-focus)
371 (select-frame-set-input-focus frame)
372 (raise-frame frame)
373 (select-frame frame)
374 (focus-frame frame)))
375 ;; `select-frame-set-input-focus' defined in Emacs 21 will not
376 ;; set the input focus.
377 ((>= emacs-major-version 22)
378 (select-frame-set-input-focus frame))
380 (raise-frame frame)
381 (select-frame frame)
382 (cond ((memq window-system '(x ns mac))
383 (x-focus-frame frame))
384 ((eq window-system 'w32)
385 (w32-focus-frame frame)))
386 (when focus-follows-mouse
387 (set-mouse-position frame (1- (frame-width frame)) 0)))))
389 (defun org-float-time (&optional time)
390 "Convert time value TIME to a floating point number.
391 TIME defaults to the current time."
392 (if (featurep 'xemacs)
393 (time-to-seconds (or time (current-time)))
394 (float-time time)))
396 ;; `user-error' is only available from 24.2.50 on
397 (unless (fboundp 'user-error)
398 (defalias 'user-error 'error))
400 (defmacro org-no-popups (&rest body)
401 "Suppress popup windows.
402 Let-bind some variables to nil around BODY to achieve the desired
403 effect, which variables to use depends on the Emacs version."
404 (if (org-version-check "24.2.50" "" :predicate)
405 `(let (pop-up-frames display-buffer-alist)
406 ,@body)
407 `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
408 ,@body)))
410 (if (fboundp 'string-match-p)
411 (defalias 'org-string-match-p 'string-match-p)
412 (defun org-string-match-p (regexp string &optional start)
413 (save-match-data
414 (funcall 'string-match regexp string start))))
416 (if (fboundp 'looking-at-p)
417 (defalias 'org-looking-at-p 'looking-at-p)
418 (defun org-looking-at-p (&rest args)
419 (save-match-data
420 (apply 'looking-at args))))
422 ;; XEmacs does not have `looking-back'.
423 (if (fboundp 'looking-back)
424 (defalias 'org-looking-back 'looking-back)
425 (defun org-looking-back (regexp &optional limit greedy)
426 "Return non-nil if text before point matches regular expression REGEXP.
427 Like `looking-at' except matches before point, and is slower.
428 LIMIT if non-nil speeds up the search by specifying a minimum
429 starting position, to avoid checking matches that would start
430 before LIMIT.
432 If GREEDY is non-nil, extend the match backwards as far as
433 possible, stopping when a single additional previous character
434 cannot be part of a match for REGEXP. When the match is
435 extended, its starting position is allowed to occur before
436 LIMIT."
437 (let ((start (point))
438 (pos
439 (save-excursion
440 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
441 (point)))))
442 (if (and greedy pos)
443 (save-restriction
444 (narrow-to-region (point-min) start)
445 (while (and (> pos (point-min))
446 (save-excursion
447 (goto-char pos)
448 (backward-char 1)
449 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
450 (setq pos (1- pos)))
451 (save-excursion
452 (goto-char pos)
453 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
454 (not (null pos)))))
456 (defun org-floor* (x &optional y)
457 "Return a list of the floor of X and the fractional part of X.
458 With two arguments, return floor and remainder of their quotient."
459 (let ((q (floor x y)))
460 (list q (- x (if y (* y q) q)))))
462 ;; `pop-to-buffer-same-window' has been introduced in Emacs 24.1.
463 (defun org-pop-to-buffer-same-window
464 (&optional buffer-or-name norecord label)
465 "Pop to buffer specified by BUFFER-OR-NAME in the selected window."
466 (if (fboundp 'pop-to-buffer-same-window)
467 (funcall
468 'pop-to-buffer-same-window buffer-or-name norecord)
469 (funcall 'switch-to-buffer buffer-or-name norecord)))
471 ;; `condition-case-unless-debug' has been introduced in Emacs 24.1
472 ;; `condition-case-no-debug' has been introduced in Emacs 23.1
473 (defmacro org-condition-case-unless-debug (var bodyform &rest handlers)
474 (declare (debug condition-case) (indent 2))
475 (or (and (fboundp 'condition-case-unless-debug)
476 `(condition-case-unless-debug ,var ,bodyform ,@handlers))
477 (and (fboundp 'condition-case-no-debug)
478 `(condition-case-no-debug ,var ,bodyform ,@handlers))
479 `(condition-case ,var ,bodyform ,@handlers)))
481 ;; RECURSIVE has been introduced with Emacs 23.2.
482 ;; This is copying and adapted from `tramp-compat-delete-directory'
483 (defun org-delete-directory (directory &optional recursive)
484 "Compatibility function for `delete-directory'."
485 (if (null recursive)
486 (delete-directory directory)
487 (condition-case nil
488 (funcall 'delete-directory directory recursive)
489 ;; This Emacs version does not support the RECURSIVE flag. We
490 ;; use the implementation from Emacs 23.2.
491 (wrong-number-of-arguments
492 (setq directory (directory-file-name (expand-file-name directory)))
493 (if (not (file-symlink-p directory))
494 (mapc (lambda (file)
495 (if (eq t (car (file-attributes file)))
496 (org-delete-directory file recursive)
497 (delete-file file)))
498 (directory-files
499 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
500 (delete-directory directory)))))
502 ;;;###autoload
503 (defmacro org-check-version ()
504 "Try very hard to provide sensible version strings."
505 (let* ((org-dir (org-find-library-dir "org"))
506 (org-version.el (concat org-dir "org-version.el"))
507 (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
508 (if (require 'org-version org-version.el 'noerror)
509 '(progn
510 (autoload 'org-release "org-version.el")
511 (autoload 'org-git-version "org-version.el"))
512 (if (require 'org-fixup org-fixup.el 'noerror)
513 '(org-fixup)
514 ;; provide fallback definitions and complain
515 (warn "Could not define org version correctly. Check installation!")
516 '(progn
517 (defun org-release () "N/A")
518 (defun org-git-version () "N/A !!check installation!!"))))))
520 (provide 'org-compat)
522 ;;; org-compat.el ends here