org-compat.el 12 KB

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