Merge Org 7.9.3 (commit 31c1aea)
[bpt/emacs.git] / lisp / org / org-compat.el
CommitLineData
20908596
CD
1;;; org-compat.el --- Compatibility code for Org-mode
2
ab422c4d 3;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
20908596
CD
4
5;; Author: Carsten Dominik <carsten at orgmode dot org>
6;; Keywords: outlines, hypermedia, calendar, wp
7;; Homepage: http://orgmode.org
20908596
CD
8;;
9;; This file is part of GNU Emacs.
10;;
b1fc2b50 11;; GNU Emacs is free software: you can redistribute it and/or modify
20908596 12;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
20908596
CD
15
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.
20
21;; You should have received a copy of the GNU General Public License
b1fc2b50 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20908596
CD
23;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24;;
25;;; Commentary:
26
27;; This file contains code needed for compatibility with XEmacs and older
28;; versions of GNU Emacs.
29
30;;; Code:
31
c8d0cf5c
CD
32(eval-when-compile
33 (require 'cl))
34
621f83e4
CD
35(require 'org-macs)
36
9d459fc5 37(declare-function w32-focus-frame "term/w32-win" (frame))
c8d0cf5c 38
86fbb8ca
CD
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))
20908596
CD
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?")
48
49(defun org-compatible-face (inherits specs)
50 "Make a compatible face specification.
51If INHERITS is an existing face and if the Emacs version supports it,
c8d0cf5c
CD
52just inherit the face. If INHERITS is set and the Emacs version does
53not support it, copy the face specification from the inheritance face.
54If INHERITS is not given and SPECS is, use SPECS to define the face.
20908596
CD
55XEmacs and Emacs 21 do not know about the `min-colors' attribute.
56For them we convert a (min-colors 8) entry to a `tty' entry and move it
57to the top of the list. The `min-colors' attribute will be removed from
58any other entries, and any resulting duplicates will be removed entirely."
c8d0cf5c
CD
59 (when (and inherits (facep inherits) (not specs))
60 (setq specs (or specs
61 (get inherits 'saved-face)
62 (get inherits 'face-defface-spec))))
20908596
CD
63 (cond
64 ((and inherits (facep inherits)
c8d0cf5c
CD
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)))))
20908596
CD
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)
88
86fbb8ca
CD
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
106 nil
107 (let ((msg (format "Emacs %s or greater is recommended for %s"
108 version feature)))
109 (display-warning 'org msg level)
110 t))
111 t)))
112
8223b1d2 113\f
20908596
CD
114;;;; Emacs/XEmacs compatibility
115
86fbb8ca
CD
116;; Keys
117(defconst org-xemacs-key-equivalents
118 '(([mouse-1] . [button1])
119 ([mouse-2] . [button2])
120 ([mouse-3] . [button3])
121 ([C-mouse-4] . [(control mouse-4)])
122 ([C-mouse-5] . [(control mouse-5)]))
123 "Translation alist for a couple of keys.")
124
20908596 125;; Overlay compatibility functions
20908596
CD
126(defun org-detach-overlay (ovl)
127 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
20908596
CD
128(defun org-overlay-display (ovl text &optional face evap)
129 "Make overlay OVL display TEXT with face FACE."
130 (if (featurep 'xemacs)
131 (let ((gl (make-glyph text)))
132 (and face (set-glyph-face gl face))
133 (set-extent-property ovl 'invisible t)
134 (set-extent-property ovl 'end-glyph gl))
135 (overlay-put ovl 'display text)
136 (if face (overlay-put ovl 'face face))
137 (if evap (overlay-put ovl 'evaporate t))))
138(defun org-overlay-before-string (ovl text &optional face evap)
139 "Make overlay OVL display TEXT with face FACE."
140 (if (featurep 'xemacs)
141 (let ((gl (make-glyph text)))
142 (and face (set-glyph-face gl face))
143 (set-extent-property ovl 'begin-glyph gl))
144 (if face (org-add-props text nil 'face face))
145 (overlay-put ovl 'before-string text)
146 (if evap (overlay-put ovl 'evaporate t))))
20908596
CD
147(defun org-find-overlays (prop &optional pos delete)
148 "Find all overlays specifying PROP at POS or point.
149If DELETE is non-nil, delete all those overlays."
86fbb8ca 150 (let ((overlays (overlays-at (or pos (point))))
20908596
CD
151 ov found)
152 (while (setq ov (pop overlays))
86fbb8ca
CD
153 (if (overlay-get ov prop)
154 (if delete (delete-overlay ov) (push ov found))))
20908596
CD
155 found))
156
86fbb8ca
CD
157(defun org-get-x-clipboard (value)
158 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
159 (if (eq window-system 'x)
160 (let ((x (org-get-x-clipboard-compat value)))
161 (if x (org-no-properties x)))))
162
afe98dfa
CD
163(defsubst org-decompose-region (beg end)
164 "Decompose from BEG to END."
165 (if (featurep 'xemacs)
166 (let ((modified-p (buffer-modified-p))
167 (buffer-read-only nil))
168 (remove-text-properties beg end '(composition nil))
169 (set-buffer-modified-p modified-p))
170 (decompose-region beg end)))
171
c7cf0ebc
BG
172(defmacro org-define-obsolete-function-alias (o-name c-name when &optional doc)
173 "Reconcile the two-argument form of
174`define-obsolete-function-alias' in XEmacs/Emacs 22 with the 3-4
175argument form in Emacs 23 and later."
176 (if (or (featurep 'xemacs)
177 (< emacs-major-version 23))
178 `(define-obsolete-function-alias ,o-name ,c-name)
179 `(define-obsolete-function-alias ,o-name ,c-name ,when ,doc)))
180
181(defmacro org-define-obsolete-variable-alias (o-name c-name when &optional doc)
182 "Reconcile the two-argument form of
183`define-obsolete-variable-alias' in XEmacs/Emacs 22 with the 3-4
184argument form in Emacs 23 and later."
185 (if (or (featurep 'xemacs)
186 (< emacs-major-version 23))
187 `(define-obsolete-variable-alias ,o-name ,c-name)
188 `(define-obsolete-variable-alias ,o-name ,c-name ,when ,doc)))
189
86fbb8ca
CD
190;; Miscellaneous functions
191
20908596
CD
192(defun org-add-hook (hook function &optional append local)
193 "Add-hook, compatible with both Emacsen."
194 (if (and local (featurep 'xemacs))
195 (add-local-hook hook function append)
196 (add-hook hook function append local)))
197
198(defun org-add-props (string plist &rest props)
199 "Add text properties to entire string, from beginning to end.
200PLIST may be a list of properties, PROPS are individual properties and values
201that will be added to PLIST. Returns the string that was modified."
202 (add-text-properties
203 0 (length string) (if props (append plist props) plist) string)
204 string)
205(put 'org-add-props 'lisp-indent-function 2)
206
93b62de8
CD
207(defun org-fit-window-to-buffer (&optional window max-height min-height
208 shrink-only)
209 "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
210WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
211passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
86fbb8ca 212`shrink-window-if-larger-than-buffer' instead, the height limit is
93b62de8 213ignored in this case."
0bd48b37
CD
214 (cond ((if (fboundp 'window-full-width-p)
215 (not (window-full-width-p window))
c7cf0ebc
BG
216 ;; do nothing if another window would suffer
217 (> (frame-width) (window-width window))))
93b62de8
CD
218 ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
219 (fit-window-to-buffer window max-height min-height))
220 ((fboundp 'shrink-window-if-larger-than-buffer)
221 (shrink-window-if-larger-than-buffer window)))
222 (or window (selected-window)))
223
afe98dfa
CD
224(defun org-number-sequence (from &optional to inc)
225 "Call `number-sequence or emulate it."
226 (if (fboundp 'number-sequence)
227 (number-sequence from to inc)
228 (if (or (not to) (= from to))
229 (list from)
230 (or inc (setq inc 1))
231 (when (zerop inc) (error "The increment can not be zero"))
232 (let (seq (n 0) (next from))
233 (if (> inc 0)
234 (while (<= next to)
235 (setq seq (cons next seq)
236 n (1+ n)
237 next (+ from (* n inc))))
238 (while (>= next to)
239 (setq seq (cons next seq)
240 n (1+ n)
241 next (+ from (* n inc)))))
242 (nreverse seq)))))
243
20908596
CD
244;; Region compatibility
245
246(defvar org-ignore-region nil
247 "To temporarily disable the active region.")
248
249(defun org-region-active-p ()
250 "Is `transient-mark-mode' on and the region active?
251Works on both Emacs and XEmacs."
252 (if org-ignore-region
253 nil
254 (if (featurep 'xemacs)
255 (and zmacs-regions (region-active-p))
256 (if (fboundp 'use-region-p)
257 (use-region-p)
258 (and transient-mark-mode mark-active))))) ; Emacs 22 and before
259
c8d0cf5c
CD
260(defun org-cursor-to-region-beginning ()
261 (when (and (org-region-active-p)
262 (> (point) (region-beginning)))
263 (exchange-point-and-mark)))
264
3ab2c837
BG
265;; Emacs 22 misses `activate-mark'
266(if (fboundp 'activate-mark)
267 (defalias 'org-activate-mark 'activate-mark)
268 (defun org-activate-mark ()
269 (when (mark t)
270 (setq mark-active t)
e66ba1df
BG
271 (when (and (boundp 'transient-mark-mode)
272 (not transient-mark-mode))
273 (setq transient-mark-mode 'lambda))
274 (when (boundp 'zmacs-regions)
275 (setq zmacs-regions t)))))
276
20908596
CD
277;; Invisibility compatibility
278
20908596
CD
279(defun org-remove-from-invisibility-spec (arg)
280 "Remove elements from `buffer-invisibility-spec'."
281 (if (fboundp 'remove-from-invisibility-spec)
282 (remove-from-invisibility-spec arg)
283 (if (consp buffer-invisibility-spec)
284 (setq buffer-invisibility-spec
285 (delete arg buffer-invisibility-spec)))))
286
287(defun org-in-invisibility-spec-p (arg)
288 "Is ARG a member of `buffer-invisibility-spec'?"
289 (if (consp buffer-invisibility-spec)
290 (member arg buffer-invisibility-spec)
291 nil))
292
86fbb8ca 293(defmacro org-xemacs-without-invisibility (&rest body)
27e428e7 294 "Turn off extents with invisibility while executing BODY."
86fbb8ca
CD
295 `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
296 'all-extents-closed-open 'invisible))
297 ext-inv-specs)
298 (dolist (ext ext-inv)
299 (when (extent-property ext 'invisible)
300 (add-to-list 'ext-inv-specs (list ext (extent-property
301 ext 'invisible)))
302 (set-extent-property ext 'invisible nil)))
303 ,@body
304 (dolist (ext-inv-spec ext-inv-specs)
305 (set-extent-property (car ext-inv-spec) 'invisible
306 (cadr ext-inv-spec)))))
e66ba1df 307(def-edebug-spec org-xemacs-without-invisibility (body))
86fbb8ca 308
20908596
CD
309(defun org-indent-to-column (column &optional minimum buffer)
310 "Work around a bug with extents with invisibility in XEmacs."
71d35b24 311 (if (featurep 'xemacs)
86fbb8ca 312 (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
71d35b24 313 (indent-to-column column minimum)))
20908596
CD
314
315(defun org-indent-line-to (column)
316 "Work around a bug with extents with invisibility in XEmacs."
71d35b24 317 (if (featurep 'xemacs)
86fbb8ca 318 (org-xemacs-without-invisibility (indent-line-to column))
71d35b24 319 (indent-line-to column)))
20908596
CD
320
321(defun org-move-to-column (column &optional force buffer)
71d35b24 322 (if (featurep 'xemacs)
86fbb8ca 323 (org-xemacs-without-invisibility (move-to-column column force buffer))
71d35b24 324 (move-to-column column force)))
621f83e4
CD
325
326(defun org-get-x-clipboard-compat (value)
86fbb8ca
CD
327 "Get the clipboard value on XEmacs or Emacs 21."
328 (cond ((featurep 'xemacs)
329 (org-no-warnings (get-selection-no-error value)))
621f83e4
CD
330 ((fboundp 'x-get-selection)
331 (condition-case nil
332 (or (x-get-selection value 'UTF8_STRING)
333 (x-get-selection value 'COMPOUND_TEXT)
334 (x-get-selection value 'STRING)
335 (x-get-selection value 'TEXT))
336 (error nil)))))
337
338(defun org-propertize (string &rest properties)
339 (if (featurep 'xemacs)
ce4fdcb9
CD
340 (progn
341 (add-text-properties 0 (length string) properties string)
342 string)
621f83e4 343 (apply 'propertize string properties)))
71d35b24 344
8223b1d2 345(defmacro org-find-library-dir (library)
bdebdb64 346 `(file-name-directory (or (locate-library ,library) "")))
c8d0cf5c 347
0bd48b37
CD
348(defun org-count-lines (s)
349 "How many lines in string S?"
350 (let ((start 0) (n 1))
351 (while (string-match "\n" s start)
352 (setq start (match-end 0) n (1+ n)))
353 (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
354 (setq n (1- n)))
355 n))
356
c8d0cf5c
CD
357(defun org-kill-new (string &rest args)
358 (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
359 string)
360 (apply 'kill-new string args))
361
54a0dee5
CD
362(defun org-select-frame-set-input-focus (frame)
363 "Select FRAME, raise it, and set input focus, if possible."
364 (cond ((featurep 'xemacs)
365 (if (fboundp 'select-frame-set-input-focus)
366 (select-frame-set-input-focus frame)
367 (raise-frame frame)
368 (select-frame frame)
369 (focus-frame frame)))
370 ;; `select-frame-set-input-focus' defined in Emacs 21 will not
371 ;; set the input focus.
372 ((>= emacs-major-version 22)
373 (select-frame-set-input-focus frame))
374 (t
375 (raise-frame frame)
376 (select-frame frame)
377 (cond ((memq window-system '(x ns mac))
378 (x-focus-frame frame))
379 ((eq window-system 'w32)
380 (w32-focus-frame frame)))
381 (when focus-follows-mouse
382 (set-mouse-position frame (1- (frame-width frame)) 0)))))
383
384(defun org-float-time (&optional time)
385 "Convert time value TIME to a floating point number.
386TIME defaults to the current time."
387 (if (featurep 'xemacs)
388 (time-to-seconds (or time (current-time)))
389 (float-time time)))
390
c7cf0ebc
BG
391;; `user-error' is only available from 24.2.50 on
392(unless (fboundp 'user-error)
393 (defalias 'user-error 'error))
394
395(defmacro org-no-popups (&rest body)
396 "Suppress popup windows.
397Let-bind some variables to nil around BODY to achieve the desired
398effect, which variables to use depends on the Emacs version."
399 (if (org-version-check "24.2.50" "" :predicate)
400 `(let (pop-up-frames display-buffer-alist)
401 ,@body)
402 `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
403 ,@body)))
404
acedf35c
CD
405(if (fboundp 'string-match-p)
406 (defalias 'org-string-match-p 'string-match-p)
407 (defun org-string-match-p (regexp string &optional start)
86fbb8ca 408 (save-match-data
acedf35c 409 (funcall 'string-match regexp string start))))
86fbb8ca 410
acedf35c
CD
411(if (fboundp 'looking-at-p)
412 (defalias 'org-looking-at-p 'looking-at-p)
413 (defun org-looking-at-p (&rest args)
86fbb8ca 414 (save-match-data
afe98dfa 415 (apply 'looking-at args))))
86fbb8ca 416
c7cf0ebc 417;; XEmacs does not have `looking-back'.
ed21c5c8
CD
418(if (fboundp 'looking-back)
419 (defalias 'org-looking-back 'looking-back)
420 (defun org-looking-back (regexp &optional limit greedy)
421 "Return non-nil if text before point matches regular expression REGEXP.
422Like `looking-at' except matches before point, and is slower.
423LIMIT if non-nil speeds up the search by specifying a minimum
424starting position, to avoid checking matches that would start
425before LIMIT.
426
427If GREEDY is non-nil, extend the match backwards as far as
428possible, stopping when a single additional previous character
429cannot be part of a match for REGEXP. When the match is
430extended, its starting position is allowed to occur before
431LIMIT."
432 (let ((start (point))
433 (pos
434 (save-excursion
435 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
436 (point)))))
437 (if (and greedy pos)
438 (save-restriction
439 (narrow-to-region (point-min) start)
440 (while (and (> pos (point-min))
441 (save-excursion
442 (goto-char pos)
443 (backward-char 1)
444 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
445 (setq pos (1- pos)))
446 (save-excursion
447 (goto-char pos)
448 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
449 (not (null pos)))))
450
acedf35c
CD
451(defun org-floor* (x &optional y)
452 "Return a list of the floor of X and the fractional part of X.
453With two arguments, return floor and remainder of their quotient."
454 (let ((q (floor x y)))
455 (list q (- x (if y (* y q) q)))))
456
8223b1d2 457;; `pop-to-buffer-same-window' has been introduced in Emacs 24.1.
e66ba1df
BG
458(defun org-pop-to-buffer-same-window
459 (&optional buffer-or-name norecord label)
460 "Pop to buffer specified by BUFFER-OR-NAME in the selected window."
461 (if (fboundp 'pop-to-buffer-same-window)
462 (funcall
463 'pop-to-buffer-same-window buffer-or-name norecord)
464 (funcall 'switch-to-buffer buffer-or-name norecord)))
5b409b39 465
8223b1d2
BG
466;; `condition-case-unless-debug' has been introduced in Emacs 24.1
467;; `condition-case-no-debug' has been introduced in Emacs 23.1
c7cf0ebc
BG
468(defmacro org-condition-case-unless-debug (var bodyform &rest handlers)
469 (declare (debug condition-case) (indent 2))
8223b1d2 470 (or (and (fboundp 'condition-case-unless-debug)
c7cf0ebc 471 `(condition-case-unless-debug ,var ,bodyform ,@handlers))
8223b1d2 472 (and (fboundp 'condition-case-no-debug)
c7cf0ebc
BG
473 `(condition-case-no-debug ,var ,bodyform ,@handlers))
474 `(condition-case ,var ,bodyform ,@handlers)))
475
476;; RECURSIVE has been introduced with Emacs 23.2.
477;; This is copying and adapted from `tramp-compat-delete-directory'
478(defun org-delete-directory (directory &optional recursive)
479 "Compatibility function for `delete-directory'."
480 (if (null recursive)
481 (delete-directory directory)
482 (condition-case nil
483 (funcall 'delete-directory directory recursive)
484 ;; This Emacs version does not support the RECURSIVE flag. We
485 ;; use the implementation from Emacs 23.2.
486 (wrong-number-of-arguments
487 (setq directory (directory-file-name (expand-file-name directory)))
488 (if (not (file-symlink-p directory))
489 (mapc (lambda (file)
490 (if (eq t (car (file-attributes file)))
491 (org-delete-directory file recursive)
492 (delete-file file)))
493 (directory-files
494 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
495 (delete-directory directory)))))
8223b1d2
BG
496
497;;;###autoload
498(defmacro org-check-version ()
499 "Try very hard to provide sensible version strings."
500 (let* ((org-dir (org-find-library-dir "org"))
501 (org-version.el (concat org-dir "org-version.el"))
502 (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
503 (if (require 'org-version org-version.el 'noerror)
504 '(progn
505 (autoload 'org-release "org-version.el")
506 (autoload 'org-git-version "org-version.el"))
507 (if (require 'org-fixup org-fixup.el 'noerror)
508 '(org-fixup)
509 ;; provide fallback definitions and complain
510 (warn "Could not define org version correctly. Check installation!")
511 '(progn
512 (defun org-release () "N/A")
513 (defun org-git-version () "N/A !!check installation!!"))))))
514
e66ba1df 515(provide 'org-compat)
b349f79f 516
20908596 517;;; org-compat.el ends here