org-compat.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. ;;; org-compat.el --- Compatibility code for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.23
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  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.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file contains code needed for compatibility with XEmacs and older
  25. ;; versions of GNU Emacs.
  26. ;;; Code:
  27. (require 'org-macs)
  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) (progn (delete-extent ovl) nil) (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-overlay-buffer (o)
  117. (if (featurep 'xemacs) (extent-buffer o) (overlay-buffer o)))
  118. (defun org-find-overlays (prop &optional pos delete)
  119. "Find all overlays specifying PROP at POS or point.
  120. If DELETE is non-nil, delete all those overlays."
  121. (let ((overlays (org-overlays-at (or pos (point))))
  122. ov found)
  123. (while (setq ov (pop overlays))
  124. (if (org-overlay-get ov prop)
  125. (if delete (org-delete-overlay ov) (push ov found))))
  126. found))
  127. (defun org-add-hook (hook function &optional append local)
  128. "Add-hook, compatible with both Emacsen."
  129. (if (and local (featurep 'xemacs))
  130. (add-local-hook hook function append)
  131. (add-hook hook function append local)))
  132. (defun org-add-props (string plist &rest props)
  133. "Add text properties to entire string, from beginning to end.
  134. PLIST may be a list of properties, PROPS are individual properties and values
  135. that will be added to PLIST. Returns the string that was modified."
  136. (add-text-properties
  137. 0 (length string) (if props (append plist props) plist) string)
  138. string)
  139. (put 'org-add-props 'lisp-indent-function 2)
  140. (defun org-fit-window-to-buffer (&optional window max-height min-height
  141. shrink-only)
  142. "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
  143. WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
  144. passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
  145. `shrink-window-if-larger-than-buffer' instead, the hight limit are
  146. ignored in this case."
  147. (cond ((if (fboundp 'window-full-width-p)
  148. (not (window-full-width-p window))
  149. (> (frame-width) (window-width window)))
  150. ;; do nothing if another window would suffer
  151. )
  152. ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
  153. (fit-window-to-buffer window max-height min-height))
  154. ((fboundp 'shrink-window-if-larger-than-buffer)
  155. (shrink-window-if-larger-than-buffer window)))
  156. (or window (selected-window)))
  157. ;; Region compatibility
  158. (defvar org-ignore-region nil
  159. "To temporarily disable the active region.")
  160. (defun org-region-active-p ()
  161. "Is `transient-mark-mode' on and the region active?
  162. Works on both Emacs and XEmacs."
  163. (if org-ignore-region
  164. nil
  165. (if (featurep 'xemacs)
  166. (and zmacs-regions (region-active-p))
  167. (if (fboundp 'use-region-p)
  168. (use-region-p)
  169. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  170. (defun org-cursor-to-region-beginning ()
  171. (when (and (org-region-active-p)
  172. (> (point) (region-beginning)))
  173. (exchange-point-and-mark)))
  174. ;; Invisibility compatibility
  175. (defun org-add-to-invisibility-spec (arg)
  176. "Add elements to `buffer-invisibility-spec'.
  177. See documentation for `buffer-invisibility-spec' for the kind of elements
  178. that can be added."
  179. (cond
  180. ((fboundp 'add-to-invisibility-spec)
  181. (add-to-invisibility-spec arg))
  182. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  183. (setq buffer-invisibility-spec (list arg)))
  184. (t
  185. (setq buffer-invisibility-spec
  186. (cons arg buffer-invisibility-spec)))))
  187. (defun org-remove-from-invisibility-spec (arg)
  188. "Remove elements from `buffer-invisibility-spec'."
  189. (if (fboundp 'remove-from-invisibility-spec)
  190. (remove-from-invisibility-spec arg)
  191. (if (consp buffer-invisibility-spec)
  192. (setq buffer-invisibility-spec
  193. (delete arg buffer-invisibility-spec)))))
  194. (defun org-in-invisibility-spec-p (arg)
  195. "Is ARG a member of `buffer-invisibility-spec'?"
  196. (if (consp buffer-invisibility-spec)
  197. (member arg buffer-invisibility-spec)
  198. nil))
  199. (defun org-indent-to-column (column &optional minimum buffer)
  200. "Work around a bug with extents with invisibility in XEmacs."
  201. (if (featurep 'xemacs)
  202. (let ((ext-inv (extent-list
  203. nil (point-at-bol) (point-at-eol)
  204. 'all-extents-closed-open 'invisible))
  205. ext-inv-specs)
  206. (dolist (ext ext-inv)
  207. (when (extent-property ext 'invisible)
  208. (add-to-list 'ext-inv-specs (list ext (extent-property
  209. ext 'invisible)))
  210. (set-extent-property ext 'invisible nil)))
  211. (indent-to-column column minimum buffer)
  212. (dolist (ext-inv-spec ext-inv-specs)
  213. (set-extent-property (car ext-inv-spec) 'invisible
  214. (cadr ext-inv-spec))))
  215. (indent-to-column column minimum)))
  216. (defun org-indent-line-to (column)
  217. "Work around a bug with extents with invisibility in XEmacs."
  218. (if (featurep 'xemacs)
  219. (let ((ext-inv (extent-list
  220. nil (point-at-bol) (point-at-eol)
  221. 'all-extents-closed-open 'invisible))
  222. ext-inv-specs)
  223. (dolist (ext ext-inv)
  224. (when (extent-property ext 'invisible)
  225. (add-to-list 'ext-inv-specs (list ext (extent-property
  226. ext 'invisible)))
  227. (set-extent-property ext 'invisible nil)))
  228. (indent-line-to column)
  229. (dolist (ext-inv-spec ext-inv-specs)
  230. (set-extent-property (car ext-inv-spec) 'invisible
  231. (cadr ext-inv-spec))))
  232. (indent-line-to column)))
  233. (defun org-move-to-column (column &optional force buffer)
  234. (if (featurep 'xemacs)
  235. (let ((ext-inv (extent-list
  236. nil (point-at-bol) (point-at-eol)
  237. 'all-extents-closed-open 'invisible))
  238. ext-inv-specs)
  239. (dolist (ext ext-inv)
  240. (when (extent-property ext 'invisible)
  241. (add-to-list 'ext-inv-specs (list ext (extent-property ext
  242. 'invisible)))
  243. (set-extent-property ext 'invisible nil)))
  244. (move-to-column column force buffer)
  245. (dolist (ext-inv-spec ext-inv-specs)
  246. (set-extent-property (car ext-inv-spec) 'invisible
  247. (cadr ext-inv-spec))))
  248. (move-to-column column force)))
  249. (defun org-get-x-clipboard-compat (value)
  250. "Get the clipboard value on XEmacs or Emacs 21"
  251. (cond (org-xemacs-p (org-no-warnings (get-selection-no-error value)))
  252. ((fboundp 'x-get-selection)
  253. (condition-case nil
  254. (or (x-get-selection value 'UTF8_STRING)
  255. (x-get-selection value 'COMPOUND_TEXT)
  256. (x-get-selection value 'STRING)
  257. (x-get-selection value 'TEXT))
  258. (error nil)))))
  259. (defun org-propertize (string &rest properties)
  260. (if (featurep 'xemacs)
  261. (progn
  262. (add-text-properties 0 (length string) properties string)
  263. string)
  264. (apply 'propertize string properties)))
  265. (defun org-substring-no-properties (string &optional from to)
  266. (if (featurep 'xemacs)
  267. (org-no-properties (substring string (or from 0) to))
  268. (substring-no-properties string from to)))
  269. (defun org-count-lines (s)
  270. "How many lines in string S?"
  271. (let ((start 0) (n 1))
  272. (while (string-match "\n" s start)
  273. (setq start (match-end 0) n (1+ n)))
  274. (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
  275. (setq n (1- n)))
  276. n))
  277. (provide 'org-compat)
  278. ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
  279. ;;; org-compat.el ends here