org-compat.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.13pre02
  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. (require 'org-macs)
  27. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  28. (defconst org-format-transports-properties-p
  29. (let ((x "a"))
  30. (add-text-properties 0 1 '(test t) x)
  31. (get-text-property 0 'test (format "%s" x)))
  32. "Does format transport text properties?")
  33. (defun org-compatible-face (inherits specs)
  34. "Make a compatible face specification.
  35. If INHERITS is an existing face and if the Emacs version supports it,
  36. just inherit the face. If not, use SPECS to define the face.
  37. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  38. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  39. to the top of the list. The `min-colors' attribute will be removed from
  40. any other entries, and any resulting duplicates will be removed entirely."
  41. (cond
  42. ((and inherits (facep inherits)
  43. (not (featurep 'xemacs)) (> emacs-major-version 22))
  44. ;; In Emacs 23, we use inheritance where possible.
  45. ;; We only do this in Emacs 23, because only there the outline
  46. ;; faces have been changed to the original org-mode-level-faces.
  47. (list (list t :inherit inherits)))
  48. ((or (featurep 'xemacs) (< emacs-major-version 22))
  49. ;; These do not understand the `min-colors' attribute.
  50. (let (r e a)
  51. (while (setq e (pop specs))
  52. (cond
  53. ((memq (car e) '(t default)) (push e r))
  54. ((setq a (member '(min-colors 8) (car e)))
  55. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  56. (cdr e)))))
  57. ((setq a (assq 'min-colors (car e)))
  58. (setq e (cons (delq a (car e)) (cdr e)))
  59. (or (assoc (car e) r) (push e r)))
  60. (t (or (assoc (car e) r) (push e r)))))
  61. (nreverse r)))
  62. (t specs)))
  63. (put 'org-compatible-face 'lisp-indent-function 1)
  64. ;;;; Emacs/XEmacs compatibility
  65. ;; Overlay compatibility functions
  66. (defun org-make-overlay (beg end &optional buffer)
  67. (if (featurep 'xemacs)
  68. (make-extent beg end buffer)
  69. (make-overlay beg end buffer)))
  70. (defun org-delete-overlay (ovl)
  71. (if (featurep 'xemacs) (progn (delete-extent ovl) nil) (delete-overlay ovl)))
  72. (defun org-detach-overlay (ovl)
  73. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  74. (defun org-move-overlay (ovl beg end &optional buffer)
  75. (if (featurep 'xemacs)
  76. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  77. (move-overlay ovl beg end buffer)))
  78. (defun org-overlay-put (ovl prop value)
  79. (if (featurep 'xemacs)
  80. (set-extent-property ovl prop value)
  81. (overlay-put ovl prop value)))
  82. (defun org-overlay-display (ovl text &optional face evap)
  83. "Make overlay OVL display TEXT with face FACE."
  84. (if (featurep 'xemacs)
  85. (let ((gl (make-glyph text)))
  86. (and face (set-glyph-face gl face))
  87. (set-extent-property ovl 'invisible t)
  88. (set-extent-property ovl 'end-glyph gl))
  89. (overlay-put ovl 'display text)
  90. (if face (overlay-put ovl 'face face))
  91. (if evap (overlay-put ovl 'evaporate t))))
  92. (defun org-overlay-before-string (ovl text &optional face evap)
  93. "Make overlay OVL display TEXT with face FACE."
  94. (if (featurep 'xemacs)
  95. (let ((gl (make-glyph text)))
  96. (and face (set-glyph-face gl face))
  97. (set-extent-property ovl 'begin-glyph gl))
  98. (if face (org-add-props text nil 'face face))
  99. (overlay-put ovl 'before-string text)
  100. (if evap (overlay-put ovl 'evaporate t))))
  101. (defun org-overlay-get (ovl prop)
  102. (if (featurep 'xemacs)
  103. (extent-property ovl prop)
  104. (overlay-get ovl prop)))
  105. (defun org-overlays-at (pos)
  106. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  107. (defun org-overlays-in (&optional start end)
  108. (if (featurep 'xemacs)
  109. (extent-list nil start end)
  110. (overlays-in start end)))
  111. (defun org-overlay-start (o)
  112. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  113. (defun org-overlay-end (o)
  114. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  115. (defun org-overlay-buffer (o)
  116. (if (featurep 'xemacs) (extent-buffer o) (overlay-buffer o)))
  117. (defun org-find-overlays (prop &optional pos delete)
  118. "Find all overlays specifying PROP at POS or point.
  119. If DELETE is non-nil, delete all those overlays."
  120. (let ((overlays (org-overlays-at (or pos (point))))
  121. ov found)
  122. (while (setq ov (pop overlays))
  123. (if (org-overlay-get ov prop)
  124. (if delete (org-delete-overlay ov) (push ov found))))
  125. found))
  126. (defun org-add-hook (hook function &optional append local)
  127. "Add-hook, compatible with both Emacsen."
  128. (if (and local (featurep 'xemacs))
  129. (add-local-hook hook function append)
  130. (add-hook hook function append local)))
  131. (defun org-add-props (string plist &rest props)
  132. "Add text properties to entire string, from beginning to end.
  133. PLIST may be a list of properties, PROPS are individual properties and values
  134. that will be added to PLIST. Returns the string that was modified."
  135. (add-text-properties
  136. 0 (length string) (if props (append plist props) plist) string)
  137. string)
  138. (put 'org-add-props 'lisp-indent-function 2)
  139. (defun org-fit-window-to-buffer (&optional window max-height min-height
  140. shrink-only)
  141. "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
  142. WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
  143. passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
  144. `shrink-window-if-larger-than-buffer' instead, the hight limit are
  145. ignored in this case."
  146. (cond ((> (frame-width) (window-width window))
  147. ;; do nothing if another window would suffer
  148. )
  149. ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
  150. (fit-window-to-buffer window max-height min-height))
  151. ((fboundp 'shrink-window-if-larger-than-buffer)
  152. (shrink-window-if-larger-than-buffer window)))
  153. (or window (selected-window)))
  154. ;; Region compatibility
  155. (defvar org-ignore-region nil
  156. "To temporarily disable the active region.")
  157. (defun org-region-active-p ()
  158. "Is `transient-mark-mode' on and the region active?
  159. Works on both Emacs and XEmacs."
  160. (if org-ignore-region
  161. nil
  162. (if (featurep 'xemacs)
  163. (and zmacs-regions (region-active-p))
  164. (if (fboundp 'use-region-p)
  165. (use-region-p)
  166. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  167. ;; Invisibility compatibility
  168. (defun org-add-to-invisibility-spec (arg)
  169. "Add elements to `buffer-invisibility-spec'.
  170. See documentation for `buffer-invisibility-spec' for the kind of elements
  171. that can be added."
  172. (cond
  173. ((fboundp 'add-to-invisibility-spec)
  174. (add-to-invisibility-spec arg))
  175. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  176. (setq buffer-invisibility-spec (list arg)))
  177. (t
  178. (setq buffer-invisibility-spec
  179. (cons arg buffer-invisibility-spec)))))
  180. (defun org-remove-from-invisibility-spec (arg)
  181. "Remove elements from `buffer-invisibility-spec'."
  182. (if (fboundp 'remove-from-invisibility-spec)
  183. (remove-from-invisibility-spec arg)
  184. (if (consp buffer-invisibility-spec)
  185. (setq buffer-invisibility-spec
  186. (delete arg buffer-invisibility-spec)))))
  187. (defun org-in-invisibility-spec-p (arg)
  188. "Is ARG a member of `buffer-invisibility-spec'?"
  189. (if (consp buffer-invisibility-spec)
  190. (member arg buffer-invisibility-spec)
  191. nil))
  192. (defun org-indent-to-column (column &optional minimum buffer)
  193. "Work around a bug with extents with invisibility in XEmacs."
  194. (if (featurep 'xemacs)
  195. (let ((ext-inv (extent-list
  196. nil (point-at-bol) (point-at-eol)
  197. 'all-extents-closed-open 'invisible))
  198. ext-inv-specs)
  199. (dolist (ext ext-inv)
  200. (when (extent-property ext 'invisible)
  201. (add-to-list 'ext-inv-specs (list ext (extent-property
  202. ext 'invisible)))
  203. (set-extent-property ext 'invisible nil)))
  204. (indent-to-column column minimum buffer)
  205. (dolist (ext-inv-spec ext-inv-specs)
  206. (set-extent-property (car ext-inv-spec) 'invisible
  207. (cadr ext-inv-spec))))
  208. (indent-to-column column minimum)))
  209. (defun org-indent-line-to (column)
  210. "Work around a bug with extents with invisibility in XEmacs."
  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
  219. ext 'invisible)))
  220. (set-extent-property ext 'invisible nil)))
  221. (indent-line-to column)
  222. (dolist (ext-inv-spec ext-inv-specs)
  223. (set-extent-property (car ext-inv-spec) 'invisible
  224. (cadr ext-inv-spec))))
  225. (indent-line-to column)))
  226. (defun org-move-to-column (column &optional force buffer)
  227. (if (featurep 'xemacs)
  228. (let ((ext-inv (extent-list
  229. nil (point-at-bol) (point-at-eol)
  230. 'all-extents-closed-open 'invisible))
  231. ext-inv-specs)
  232. (dolist (ext ext-inv)
  233. (when (extent-property ext 'invisible)
  234. (add-to-list 'ext-inv-specs (list ext (extent-property ext
  235. 'invisible)))
  236. (set-extent-property ext 'invisible nil)))
  237. (move-to-column column force buffer)
  238. (dolist (ext-inv-spec ext-inv-specs)
  239. (set-extent-property (car ext-inv-spec) 'invisible
  240. (cadr ext-inv-spec))))
  241. (move-to-column column force)))
  242. (defun org-get-x-clipboard-compat (value)
  243. "Get the clipboard value on XEmacs or Emacs 21"
  244. (cond (org-xemacs-p (org-no-warnings (get-selection-no-error value)))
  245. ((fboundp 'x-get-selection)
  246. (condition-case nil
  247. (or (x-get-selection value 'UTF8_STRING)
  248. (x-get-selection value 'COMPOUND_TEXT)
  249. (x-get-selection value 'STRING)
  250. (x-get-selection value 'TEXT))
  251. (error nil)))))
  252. (defun org-propertize (string &rest properties)
  253. (if (featurep 'xemacs)
  254. (add-text-properties 0 (length string) properties string)
  255. (apply 'propertize string properties)))
  256. (provide 'org-compat)
  257. ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
  258. ;;; org-compat.el ends here