org-compat.el 13 KB

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