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