Merge branch 'maint'
[org-mode.git] / lisp / org-compat.el
blobd10aeea78eb10bc3a48de610b95eaef1e2e278ed
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 (defun org-defvaralias (new-alias base-variable &optional docstring)
118 "Compatibility function for defvaralias.
119 Don't do the aliasing when `defvaralias' is not bound."
120 (declare (indent 1))
121 (when (fboundp 'defvaralias)
122 (defvaralias new-alias base-variable docstring)))
124 (when (and (not (boundp 'user-emacs-directory))
125 (boundp 'user-init-directory))
126 (org-defvaralias 'user-emacs-directory 'user-init-directory)))
128 (when (featurep 'xemacs)
129 (defadvice custom-handle-keyword
130 (around org-custom-handle-keyword
131 activate preactivate)
132 "Remove custom keywords not recognized to avoid producing an error."
133 (cond
134 ((eq (ad-get-arg 1) :package-version))
135 (t ad-do-it)))
136 (defadvice define-obsolete-variable-alias
137 (around org-define-obsolete-variable-alias
138 (obsolete-name current-name &optional when docstring)
139 activate preactivate)
140 "Declare arguments defined in later versions of Emacs."
141 ad-do-it)
142 (defadvice define-obsolete-function-alias
143 (around org-define-obsolete-function-alias
144 (obsolete-name current-name &optional when docstring)
145 activate preactivate)
146 "Declare arguments defined in later versions of Emacs."
147 ad-do-it)
148 (defvar customize-package-emacs-version-alist nil)
149 (defvar temporary-file-directory (temp-directory)))
151 ;; Keys
152 (defconst org-xemacs-key-equivalents
153 '(([mouse-1] . [button1])
154 ([mouse-2] . [button2])
155 ([mouse-3] . [button3])
156 ([C-mouse-4] . [(control mouse-4)])
157 ([C-mouse-5] . [(control mouse-5)]))
158 "Translation alist for a couple of keys.")
160 ;; Overlay compatibility functions
161 (defun org-detach-overlay (ovl)
162 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
163 (defun org-overlay-display (ovl text &optional face evap)
164 "Make overlay OVL display TEXT with face FACE."
165 (if (featurep 'xemacs)
166 (let ((gl (make-glyph text)))
167 (and face (set-glyph-face gl face))
168 (set-extent-property ovl 'invisible t)
169 (set-extent-property ovl 'end-glyph gl))
170 (overlay-put ovl 'display text)
171 (if face (overlay-put ovl 'face face))
172 (if evap (overlay-put ovl 'evaporate t))))
173 (defun org-overlay-before-string (ovl text &optional face evap)
174 "Make overlay OVL display TEXT with face FACE."
175 (if (featurep 'xemacs)
176 (let ((gl (make-glyph text)))
177 (and face (set-glyph-face gl face))
178 (set-extent-property ovl 'begin-glyph gl))
179 (if face (org-add-props text nil 'face face))
180 (overlay-put ovl 'before-string text)
181 (if evap (overlay-put ovl 'evaporate t))))
182 (defun org-find-overlays (prop &optional pos delete)
183 "Find all overlays specifying PROP at POS or point.
184 If DELETE is non-nil, delete all those overlays."
185 (let ((overlays (overlays-at (or pos (point))))
186 ov found)
187 (while (setq ov (pop overlays))
188 (if (overlay-get ov prop)
189 (if delete (delete-overlay ov) (push ov found))))
190 found))
192 (defun org-get-x-clipboard (value)
193 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
194 (if (eq window-system 'x)
195 (let ((x (org-get-x-clipboard-compat value)))
196 (if x (org-no-properties x)))))
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 ;; Region compatibility
263 (defvar org-ignore-region nil
264 "Non-nil means temporarily disable the active region.")
266 (defun org-region-active-p ()
267 "Is `transient-mark-mode' on and the region active?
268 Works on both Emacs and XEmacs."
269 (if org-ignore-region
271 (if (featurep 'xemacs)
272 (and zmacs-regions (region-active-p))
273 (if (fboundp 'use-region-p)
274 (use-region-p)
275 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
277 (defun org-cursor-to-region-beginning ()
278 (when (and (org-region-active-p)
279 (> (point) (region-beginning)))
280 (exchange-point-and-mark)))
282 ;; Emacs 22 misses `activate-mark'
283 (if (fboundp 'activate-mark)
284 (defalias 'org-activate-mark 'activate-mark)
285 (defun org-activate-mark ()
286 (when (mark t)
287 (setq mark-active t)
288 (when (and (boundp 'transient-mark-mode)
289 (not transient-mark-mode))
290 (setq transient-mark-mode 'lambda))
291 (when (boundp 'zmacs-regions)
292 (setq zmacs-regions t)))))
294 ;; Invisibility compatibility
296 (defun org-remove-from-invisibility-spec (arg)
297 "Remove elements from `buffer-invisibility-spec'."
298 (if (fboundp 'remove-from-invisibility-spec)
299 (remove-from-invisibility-spec arg)
300 (if (consp buffer-invisibility-spec)
301 (setq buffer-invisibility-spec
302 (delete arg buffer-invisibility-spec)))))
304 (defun org-in-invisibility-spec-p (arg)
305 "Is ARG a member of `buffer-invisibility-spec'?"
306 (if (consp buffer-invisibility-spec)
307 (member arg buffer-invisibility-spec)
308 nil))
310 (defmacro org-xemacs-without-invisibility (&rest body)
311 "Turn off extents with invisibility while executing BODY."
312 `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
313 'all-extents-closed-open 'invisible))
314 ext-inv-specs)
315 (dolist (ext ext-inv)
316 (when (extent-property ext 'invisible)
317 (add-to-list 'ext-inv-specs (list ext (extent-property
318 ext 'invisible)))
319 (set-extent-property ext 'invisible nil)))
320 ,@body
321 (dolist (ext-inv-spec ext-inv-specs)
322 (set-extent-property (car ext-inv-spec) 'invisible
323 (cadr ext-inv-spec)))))
324 (def-edebug-spec org-xemacs-without-invisibility (body))
326 (defun org-indent-to-column (column &optional minimum buffer)
327 "Work around a bug with extents with invisibility in XEmacs."
328 (if (featurep 'xemacs)
329 (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
330 (indent-to-column column minimum)))
332 (defun org-indent-line-to (column)
333 "Work around a bug with extents with invisibility in XEmacs."
334 (if (featurep 'xemacs)
335 (org-xemacs-without-invisibility (indent-line-to column))
336 (indent-line-to column)))
338 (defun org-move-to-column (column &optional force buffer)
339 (if (featurep 'xemacs)
340 (org-xemacs-without-invisibility (move-to-column column force buffer))
341 (move-to-column column force)))
343 (defun org-get-x-clipboard-compat (value)
344 "Get the clipboard value on XEmacs or Emacs 21."
345 (cond ((featurep 'xemacs)
346 (org-no-warnings (get-selection-no-error value)))
347 ((fboundp 'x-get-selection)
348 (condition-case nil
349 (or (x-get-selection value 'UTF8_STRING)
350 (x-get-selection value 'COMPOUND_TEXT)
351 (x-get-selection value 'STRING)
352 (x-get-selection value 'TEXT))
353 (error nil)))))
355 (defun org-propertize (string &rest properties)
356 (if (featurep 'xemacs)
357 (progn
358 (add-text-properties 0 (length string) properties string)
359 string)
360 (apply 'propertize string properties)))
362 (defmacro org-find-library-dir (library)
363 `(file-name-directory (or (locate-library ,library) "")))
365 (defun org-count-lines (s)
366 "How many lines in string S?"
367 (let ((start 0) (n 1))
368 (while (string-match "\n" s start)
369 (setq start (match-end 0) n (1+ n)))
370 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
371 (setq n (1- n)))
374 (defun org-kill-new (string &rest args)
375 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
376 string)
377 (apply 'kill-new string args))
379 (defun org-select-frame-set-input-focus (frame)
380 "Select FRAME, raise it, and set input focus, if possible."
381 (cond ((featurep 'xemacs)
382 (if (fboundp 'select-frame-set-input-focus)
383 (select-frame-set-input-focus frame)
384 (raise-frame frame)
385 (select-frame frame)
386 (focus-frame frame)))
387 ;; `select-frame-set-input-focus' defined in Emacs 21 will not
388 ;; set the input focus.
389 ((>= emacs-major-version 22)
390 (select-frame-set-input-focus frame))
392 (raise-frame frame)
393 (select-frame frame)
394 (cond ((memq window-system '(x ns mac))
395 (x-focus-frame frame))
396 ((eq window-system 'w32)
397 (w32-focus-frame frame)))
398 (when focus-follows-mouse
399 (set-mouse-position frame (1- (frame-width frame)) 0)))))
401 (defun org-float-time (&optional time)
402 "Convert time value TIME to a floating point number.
403 TIME defaults to the current time."
404 (if (featurep 'xemacs)
405 (time-to-seconds (or time (current-time)))
406 (float-time time)))
408 ;; `user-error' is only available from 24.2.50 on
409 (unless (fboundp 'user-error)
410 (defalias 'user-error 'error))
412 (defmacro org-no-popups (&rest body)
413 "Suppress popup windows.
414 Let-bind some variables to nil around BODY to achieve the desired
415 effect, which variables to use depends on the Emacs version."
416 (if (org-version-check "24.2.50" "" :predicate)
417 `(let (pop-up-frames display-buffer-alist)
418 ,@body)
419 `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
420 ,@body)))
422 (if (fboundp 'string-match-p)
423 (defalias 'org-string-match-p 'string-match-p)
424 (defun org-string-match-p (regexp string &optional start)
425 (save-match-data
426 (funcall 'string-match regexp string start))))
428 (if (fboundp 'looking-at-p)
429 (defalias 'org-looking-at-p 'looking-at-p)
430 (defun org-looking-at-p (&rest args)
431 (save-match-data
432 (apply 'looking-at args))))
434 ;; XEmacs does not have `looking-back'.
435 (if (fboundp 'looking-back)
436 (defalias 'org-looking-back 'looking-back)
437 (defun org-looking-back (regexp &optional limit greedy)
438 "Return non-nil if text before point matches regular expression REGEXP.
439 Like `looking-at' except matches before point, and is slower.
440 LIMIT if non-nil speeds up the search by specifying a minimum
441 starting position, to avoid checking matches that would start
442 before LIMIT.
444 If GREEDY is non-nil, extend the match backwards as far as
445 possible, stopping when a single additional previous character
446 cannot be part of a match for REGEXP. When the match is
447 extended, its starting position is allowed to occur before
448 LIMIT."
449 (let ((start (point))
450 (pos
451 (save-excursion
452 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
453 (point)))))
454 (if (and greedy pos)
455 (save-restriction
456 (narrow-to-region (point-min) start)
457 (while (and (> pos (point-min))
458 (save-excursion
459 (goto-char pos)
460 (backward-char 1)
461 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
462 (setq pos (1- pos)))
463 (save-excursion
464 (goto-char pos)
465 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
466 (not (null pos)))))
468 (defun org-floor* (x &optional y)
469 "Return a list of the floor of X and the fractional part of X.
470 With two arguments, return floor and remainder of their quotient."
471 (let ((q (floor x y)))
472 (list q (- x (if y (* y q) q)))))
474 ;; `pop-to-buffer-same-window' has been introduced in Emacs 24.1.
475 (defun org-pop-to-buffer-same-window
476 (&optional buffer-or-name norecord label)
477 "Pop to buffer specified by BUFFER-OR-NAME in the selected window."
478 (if (fboundp 'pop-to-buffer-same-window)
479 (funcall
480 'pop-to-buffer-same-window buffer-or-name norecord)
481 (funcall 'switch-to-buffer buffer-or-name norecord)))
483 ;; RECURSIVE has been introduced with Emacs 23.2.
484 ;; This is copying and adapted from `tramp-compat-delete-directory'
485 (defun org-delete-directory (directory &optional recursive)
486 "Compatibility function for `delete-directory'."
487 (if (null recursive)
488 (delete-directory directory)
489 (condition-case nil
490 (funcall 'delete-directory directory recursive)
491 ;; This Emacs version does not support the RECURSIVE flag. We
492 ;; use the implementation from Emacs 23.2.
493 (wrong-number-of-arguments
494 (setq directory (directory-file-name (expand-file-name directory)))
495 (if (not (file-symlink-p directory))
496 (mapc (lambda (file)
497 (if (eq t (car (file-attributes file)))
498 (org-delete-directory file recursive)
499 (delete-file file)))
500 (directory-files
501 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
502 (delete-directory directory)))))
504 ;;;###autoload
505 (defmacro org-check-version ()
506 "Try very hard to provide sensible version strings."
507 (let* ((org-dir (org-find-library-dir "org"))
508 (org-version.el (concat org-dir "org-version.el"))
509 (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
510 (if (require 'org-version org-version.el 'noerror)
511 '(progn
512 (autoload 'org-release "org-version.el")
513 (autoload 'org-git-version "org-version.el"))
514 (if (require 'org-fixup org-fixup.el 'noerror)
515 '(org-fixup)
516 ;; provide fallback definitions and complain
517 (warn "Could not define org version correctly. Check installation!")
518 '(progn
519 (defun org-release () "N/A")
520 (defun org-git-version () "N/A !!check installation!!"))))))
522 (defun org-file-equal-p (f1 f2)
523 "Return t if files F1 and F2 are the same.
524 Implements `file-equal-p' for older emacsen and XEmacs."
525 (if (fboundp 'file-equal-p)
526 (file-equal-p f1 f2)
527 (let (f1-attr f2-attr)
528 (and (setq f1-attr (file-attributes (file-truename f1)))
529 (setq f2-attr (file-attributes (file-truename f2)))
530 (equal f1-attr f2-attr)))))
532 ;; `buffer-narrowed-p' is available for Emacs >=24.3
533 (defun org-buffer-narrowed-p ()
534 "Compatibility function for `buffer-narrowed-p'."
535 (if (fboundp 'buffer-narrowed-p)
536 (buffer-narrowed-p)
537 (/= (- (point-max) (point-min)) (buffer-size))))
539 (defmacro org-with-silent-modifications (&rest body)
540 (if (fboundp 'with-silent-modifications)
541 `(with-silent-modifications ,@body)
542 `(org-unmodified ,@body)))
543 (def-edebug-spec org-with-silent-modifications (body))
545 (provide 'org-compat)
547 ;;; org-compat.el ends here