org-compat.el 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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: 6.06b
  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 of the License, or
  13. ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file contains code needed for compatibility with XEmacs and older
  24. ;; versions of GNU Emacs.
  25. ;;; Code:
  26. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  27. (defconst org-format-transports-properties-p
  28. (let ((x "a"))
  29. (add-text-properties 0 1 '(test t) x)
  30. (get-text-property 0 'test (format "%s" x)))
  31. "Does format transport text properties?")
  32. (defun org-compatible-face (inherits specs)
  33. "Make a compatible face specification.
  34. If INHERITS is an existing face and if the Emacs version supports it,
  35. just inherit the face. If not, use SPECS to define the face.
  36. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  37. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  38. to the top of the list. The `min-colors' attribute will be removed from
  39. any other entries, and any resulting duplicates will be removed entirely."
  40. (cond
  41. ((and inherits (facep inherits)
  42. (not (featurep 'xemacs)) (> emacs-major-version 22))
  43. ;; In Emacs 23, we use inheritance where possible.
  44. ;; We only do this in Emacs 23, because only there the outline
  45. ;; faces have been changed to the original org-mode-level-faces.
  46. (list (list t :inherit inherits)))
  47. ((or (featurep 'xemacs) (< emacs-major-version 22))
  48. ;; These do not understand the `min-colors' attribute.
  49. (let (r e a)
  50. (while (setq e (pop specs))
  51. (cond
  52. ((memq (car e) '(t default)) (push e r))
  53. ((setq a (member '(min-colors 8) (car e)))
  54. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  55. (cdr e)))))
  56. ((setq a (assq 'min-colors (car e)))
  57. (setq e (cons (delq a (car e)) (cdr e)))
  58. (or (assoc (car e) r) (push e r)))
  59. (t (or (assoc (car e) r) (push e r)))))
  60. (nreverse r)))
  61. (t specs)))
  62. (put 'org-compatible-face 'lisp-indent-function 1)
  63. ;;;; Emacs/XEmacs compatibility
  64. ;; Overlay compatibility functions
  65. (defun org-make-overlay (beg end &optional buffer)
  66. (if (featurep 'xemacs)
  67. (make-extent beg end buffer)
  68. (make-overlay beg end buffer)))
  69. (defun org-delete-overlay (ovl)
  70. (if (featurep 'xemacs) (progn (delete-extent ovl) nil) (delete-overlay ovl)))
  71. (defun org-detach-overlay (ovl)
  72. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  73. (defun org-move-overlay (ovl beg end &optional buffer)
  74. (if (featurep 'xemacs)
  75. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  76. (move-overlay ovl beg end buffer)))
  77. (defun org-overlay-put (ovl prop value)
  78. (if (featurep 'xemacs)
  79. (set-extent-property ovl prop value)
  80. (overlay-put ovl prop value)))
  81. (defun org-overlay-display (ovl text &optional face evap)
  82. "Make overlay OVL display TEXT with face FACE."
  83. (if (featurep 'xemacs)
  84. (let ((gl (make-glyph text)))
  85. (and face (set-glyph-face gl face))
  86. (set-extent-property ovl 'invisible t)
  87. (set-extent-property ovl 'end-glyph gl))
  88. (overlay-put ovl 'display text)
  89. (if face (overlay-put ovl 'face face))
  90. (if evap (overlay-put ovl 'evaporate t))))
  91. (defun org-overlay-before-string (ovl text &optional face evap)
  92. "Make overlay OVL display TEXT with face FACE."
  93. (if (featurep 'xemacs)
  94. (let ((gl (make-glyph text)))
  95. (and face (set-glyph-face gl face))
  96. (set-extent-property ovl 'begin-glyph gl))
  97. (if face (org-add-props text nil 'face face))
  98. (overlay-put ovl 'before-string text)
  99. (if evap (overlay-put ovl 'evaporate t))))
  100. (defun org-overlay-get (ovl prop)
  101. (if (featurep 'xemacs)
  102. (extent-property ovl prop)
  103. (overlay-get ovl prop)))
  104. (defun org-overlays-at (pos)
  105. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  106. (defun org-overlays-in (&optional start end)
  107. (if (featurep 'xemacs)
  108. (extent-list nil start end)
  109. (overlays-in start end)))
  110. (defun org-overlay-start (o)
  111. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  112. (defun org-overlay-end (o)
  113. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  114. (defun org-overlay-buffer (o)
  115. (if (featurep 'xemacs) (extent-buffer o) (overlay-buffer 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. (defun org-indent-to-column (column &optional minimum buffer)
  177. "Work around a bug with extents with invisibility in XEmacs."
  178. (if (featurep 'xemacs)
  179. (let ((ext-inv (extent-list
  180. nil (point-at-bol) (point-at-eol)
  181. 'all-extents-closed-open 'invisible))
  182. ext-inv-specs)
  183. (dolist (ext ext-inv)
  184. (when (extent-property ext 'invisible)
  185. (add-to-list 'ext-inv-specs (list ext (extent-property
  186. ext 'invisible)))
  187. (set-extent-property ext 'invisible nil)))
  188. (indent-to-column column minimum buffer)
  189. (dolist (ext-inv-spec ext-inv-specs)
  190. (set-extent-property (car ext-inv-spec) 'invisible
  191. (cadr ext-inv-spec))))
  192. (indent-to-column column minimum)))
  193. (defun org-indent-line-to (column)
  194. "Work around a bug with extents with invisibility in XEmacs."
  195. (if (featurep 'xemacs)
  196. (let ((ext-inv (extent-list
  197. nil (point-at-bol) (point-at-eol)
  198. 'all-extents-closed-open 'invisible))
  199. ext-inv-specs)
  200. (dolist (ext ext-inv)
  201. (when (extent-property ext 'invisible)
  202. (add-to-list 'ext-inv-specs (list ext (extent-property
  203. ext 'invisible)))
  204. (set-extent-property ext 'invisible nil)))
  205. (indent-line-to column)
  206. (dolist (ext-inv-spec ext-inv-specs)
  207. (set-extent-property (car ext-inv-spec) 'invisible
  208. (cadr ext-inv-spec))))
  209. (indent-line-to column)))
  210. (defun org-move-to-column (column &optional force buffer)
  211. (if (featurep 'xemacs)
  212. (let ((ext-inv (extent-list
  213. nil (point-at-bol) (point-at-eol)
  214. 'all-extents-closed-open 'invisible))
  215. ext-inv-specs)
  216. (dolist (ext ext-inv)
  217. (when (extent-property ext 'invisible)
  218. (add-to-list 'ext-inv-specs (list ext (extent-property ext
  219. 'invisible)))
  220. (set-extent-property ext 'invisible nil)))
  221. (move-to-column column force buffer)
  222. (dolist (ext-inv-spec ext-inv-specs)
  223. (set-extent-property (car ext-inv-spec) 'invisible
  224. (cadr ext-inv-spec))))
  225. (move-to-column column force)))
  226. (provide 'org-compat)
  227. ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
  228. ;;; org-compat.el ends here