org-compat.el 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. ;;; org-compat.el --- Compatibility code for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 1.0
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 3, or (at your option)
  13. ;; any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file contains code needed for compatibility with XEmacs and older
  26. ;; versions of GNU Emacs.
  27. ;;; Code:
  28. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  29. (defconst org-format-transports-properties-p
  30. (let ((x "a"))
  31. (add-text-properties 0 1 '(test t) x)
  32. (get-text-property 0 'test (format "%s" x)))
  33. "Does format transport text properties?")
  34. (defun org-compatible-face (inherits specs)
  35. "Make a compatible face specification.
  36. If INHERITS is an existing face and if the Emacs version supports it,
  37. just inherit the face. If not, use SPECS to define the face.
  38. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  39. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  40. to the top of the list. The `min-colors' attribute will be removed from
  41. any other entries, and any resulting duplicates will be removed entirely."
  42. (cond
  43. ((and inherits (facep inherits)
  44. (not (featurep 'xemacs)) (> emacs-major-version 22))
  45. ;; In Emacs 23, we use inheritance where possible.
  46. ;; We only do this in Emacs 23, because only there the outline
  47. ;; faces have been changed to the original org-mode-level-faces.
  48. (list (list t :inherit inherits)))
  49. ((or (featurep 'xemacs) (< emacs-major-version 22))
  50. ;; These do not understand the `min-colors' attribute.
  51. (let (r e a)
  52. (while (setq e (pop specs))
  53. (cond
  54. ((memq (car e) '(t default)) (push e r))
  55. ((setq a (member '(min-colors 8) (car e)))
  56. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  57. (cdr e)))))
  58. ((setq a (assq 'min-colors (car e)))
  59. (setq e (cons (delq a (car e)) (cdr e)))
  60. (or (assoc (car e) r) (push e r)))
  61. (t (or (assoc (car e) r) (push e r)))))
  62. (nreverse r)))
  63. (t specs)))
  64. (put 'org-compatible-face 'lisp-indent-function 1)
  65. ;;;; Emacs/XEmacs compatibility
  66. ;; Overlay compatibility functions
  67. (defun org-make-overlay (beg end &optional buffer)
  68. (if (featurep 'xemacs)
  69. (make-extent beg end buffer)
  70. (make-overlay beg end buffer)))
  71. (defun org-delete-overlay (ovl)
  72. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  73. (defun org-detach-overlay (ovl)
  74. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  75. (defun org-move-overlay (ovl beg end &optional buffer)
  76. (if (featurep 'xemacs)
  77. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  78. (move-overlay ovl beg end buffer)))
  79. (defun org-overlay-put (ovl prop value)
  80. (if (featurep 'xemacs)
  81. (set-extent-property ovl prop value)
  82. (overlay-put ovl prop value)))
  83. (defun org-overlay-display (ovl text &optional face evap)
  84. "Make overlay OVL display TEXT with face FACE."
  85. (if (featurep 'xemacs)
  86. (let ((gl (make-glyph text)))
  87. (and face (set-glyph-face gl face))
  88. (set-extent-property ovl 'invisible t)
  89. (set-extent-property ovl 'end-glyph gl))
  90. (overlay-put ovl 'display text)
  91. (if face (overlay-put ovl 'face face))
  92. (if evap (overlay-put ovl 'evaporate t))))
  93. (defun org-overlay-before-string (ovl text &optional face evap)
  94. "Make overlay OVL display TEXT with face FACE."
  95. (if (featurep 'xemacs)
  96. (let ((gl (make-glyph text)))
  97. (and face (set-glyph-face gl face))
  98. (set-extent-property ovl 'begin-glyph gl))
  99. (if face (org-add-props text nil 'face face))
  100. (overlay-put ovl 'before-string text)
  101. (if evap (overlay-put ovl 'evaporate t))))
  102. (defun org-overlay-get (ovl prop)
  103. (if (featurep 'xemacs)
  104. (extent-property ovl prop)
  105. (overlay-get ovl prop)))
  106. (defun org-overlays-at (pos)
  107. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  108. (defun org-overlays-in (&optional start end)
  109. (if (featurep 'xemacs)
  110. (extent-list nil start end)
  111. (overlays-in start end)))
  112. (defun org-overlay-start (o)
  113. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  114. (defun org-overlay-end (o)
  115. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  116. (defun org-find-overlays (prop &optional pos delete)
  117. "Find all overlays specifying PROP at POS or point.
  118. If DELETE is non-nil, delete all those overlays."
  119. (let ((overlays (org-overlays-at (or pos (point))))
  120. ov found)
  121. (while (setq ov (pop overlays))
  122. (if (org-overlay-get ov prop)
  123. (if delete (org-delete-overlay ov) (push ov found))))
  124. found))
  125. (defun org-add-hook (hook function &optional append local)
  126. "Add-hook, compatible with both Emacsen."
  127. (if (and local (featurep 'xemacs))
  128. (add-local-hook hook function append)
  129. (add-hook hook function append local)))
  130. (defun org-add-props (string plist &rest props)
  131. "Add text properties to entire string, from beginning to end.
  132. PLIST may be a list of properties, PROPS are individual properties and values
  133. that will be added to PLIST. Returns the string that was modified."
  134. (add-text-properties
  135. 0 (length string) (if props (append plist props) plist) string)
  136. string)
  137. (put 'org-add-props 'lisp-indent-function 2)
  138. ;; Region compatibility
  139. (defvar org-ignore-region nil
  140. "To temporarily disable the active region.")
  141. (defun org-region-active-p ()
  142. "Is `transient-mark-mode' on and the region active?
  143. Works on both Emacs and XEmacs."
  144. (if org-ignore-region
  145. nil
  146. (if (featurep 'xemacs)
  147. (and zmacs-regions (region-active-p))
  148. (if (fboundp 'use-region-p)
  149. (use-region-p)
  150. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  151. ;; Invisibility compatibility
  152. (defun org-add-to-invisibility-spec (arg)
  153. "Add elements to `buffer-invisibility-spec'.
  154. See documentation for `buffer-invisibility-spec' for the kind of elements
  155. that can be added."
  156. (cond
  157. ((fboundp 'add-to-invisibility-spec)
  158. (add-to-invisibility-spec arg))
  159. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  160. (setq buffer-invisibility-spec (list arg)))
  161. (t
  162. (setq buffer-invisibility-spec
  163. (cons arg buffer-invisibility-spec)))))
  164. (defun org-remove-from-invisibility-spec (arg)
  165. "Remove elements from `buffer-invisibility-spec'."
  166. (if (fboundp 'remove-from-invisibility-spec)
  167. (remove-from-invisibility-spec arg)
  168. (if (consp buffer-invisibility-spec)
  169. (setq buffer-invisibility-spec
  170. (delete arg buffer-invisibility-spec)))))
  171. (defun org-in-invisibility-spec-p (arg)
  172. "Is ARG a member of `buffer-invisibility-spec'?"
  173. (if (consp buffer-invisibility-spec)
  174. (member arg buffer-invisibility-spec)
  175. nil))
  176. (provide 'org-compat)
  177. ;;; org-compat.el ends here