org-compat.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. ;;; org-compat.el --- Compatibility code for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  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.35trans
  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-detach-overlay (ovl)
  80. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  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-overlays-at (pos)
  101. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  102. (defun org-overlays-in (&optional start end)
  103. (if (featurep 'xemacs)
  104. (extent-list nil start end)
  105. (overlays-in start end)))
  106. (defun org-find-overlays (prop &optional pos delete)
  107. "Find all overlays specifying PROP at POS or point.
  108. If DELETE is non-nil, delete all those overlays."
  109. (let ((overlays (org-overlays-at (or pos (point))))
  110. ov found)
  111. (while (setq ov (pop overlays))
  112. (if (overlay-get ov prop)
  113. (if delete (delete-overlay ov) (push ov found))))
  114. found))
  115. (defun org-add-hook (hook function &optional append local)
  116. "Add-hook, compatible with both Emacsen."
  117. (if (and local (featurep 'xemacs))
  118. (add-local-hook hook function append)
  119. (add-hook hook function append local)))
  120. (defun org-add-props (string plist &rest props)
  121. "Add text properties to entire string, from beginning to end.
  122. PLIST may be a list of properties, PROPS are individual properties and values
  123. that will be added to PLIST. Returns the string that was modified."
  124. (add-text-properties
  125. 0 (length string) (if props (append plist props) plist) string)
  126. string)
  127. (put 'org-add-props 'lisp-indent-function 2)
  128. (defun org-fit-window-to-buffer (&optional window max-height min-height
  129. shrink-only)
  130. "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
  131. WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
  132. passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
  133. `shrink-window-if-larger-than-buffer' instead, the hight limit are
  134. ignored in this case."
  135. (cond ((if (fboundp 'window-full-width-p)
  136. (not (window-full-width-p window))
  137. (> (frame-width) (window-width window)))
  138. ;; do nothing if another window would suffer
  139. )
  140. ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
  141. (fit-window-to-buffer window max-height min-height))
  142. ((fboundp 'shrink-window-if-larger-than-buffer)
  143. (shrink-window-if-larger-than-buffer window)))
  144. (or window (selected-window)))
  145. ;; Region compatibility
  146. (defvar org-ignore-region nil
  147. "To temporarily disable the active region.")
  148. (defun org-region-active-p ()
  149. "Is `transient-mark-mode' on and the region active?
  150. Works on both Emacs and XEmacs."
  151. (if org-ignore-region
  152. nil
  153. (if (featurep 'xemacs)
  154. (and zmacs-regions (region-active-p))
  155. (if (fboundp 'use-region-p)
  156. (use-region-p)
  157. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  158. (defun org-cursor-to-region-beginning ()
  159. (when (and (org-region-active-p)
  160. (> (point) (region-beginning)))
  161. (exchange-point-and-mark)))
  162. ;; Invisibility compatibility
  163. (defun org-add-to-invisibility-spec (arg)
  164. "Add elements to `buffer-invisibility-spec'.
  165. See documentation for `buffer-invisibility-spec' for the kind of elements
  166. that can be added."
  167. (cond
  168. ((fboundp 'add-to-invisibility-spec)
  169. (add-to-invisibility-spec arg))
  170. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  171. (setq buffer-invisibility-spec (list arg)))
  172. (t
  173. (setq buffer-invisibility-spec
  174. (cons arg buffer-invisibility-spec)))))
  175. (defun org-remove-from-invisibility-spec (arg)
  176. "Remove elements from `buffer-invisibility-spec'."
  177. (if (fboundp 'remove-from-invisibility-spec)
  178. (remove-from-invisibility-spec arg)
  179. (if (consp buffer-invisibility-spec)
  180. (setq buffer-invisibility-spec
  181. (delete arg buffer-invisibility-spec)))))
  182. (defun org-in-invisibility-spec-p (arg)
  183. "Is ARG a member of `buffer-invisibility-spec'?"
  184. (if (consp buffer-invisibility-spec)
  185. (member arg buffer-invisibility-spec)
  186. nil))
  187. (defun org-indent-to-column (column &optional minimum buffer)
  188. "Work around a bug with extents with invisibility in XEmacs."
  189. (if (featurep 'xemacs)
  190. (let ((ext-inv (extent-list
  191. nil (point-at-bol) (point-at-eol)
  192. 'all-extents-closed-open 'invisible))
  193. ext-inv-specs)
  194. (dolist (ext ext-inv)
  195. (when (extent-property ext 'invisible)
  196. (add-to-list 'ext-inv-specs (list ext (extent-property
  197. ext 'invisible)))
  198. (set-extent-property ext 'invisible nil)))
  199. (indent-to-column column minimum buffer)
  200. (dolist (ext-inv-spec ext-inv-specs)
  201. (set-extent-property (car ext-inv-spec) 'invisible
  202. (cadr ext-inv-spec))))
  203. (indent-to-column column minimum)))
  204. (defun org-indent-line-to (column)
  205. "Work around a bug with extents with invisibility in XEmacs."
  206. (if (featurep 'xemacs)
  207. (let ((ext-inv (extent-list
  208. nil (point-at-bol) (point-at-eol)
  209. 'all-extents-closed-open 'invisible))
  210. ext-inv-specs)
  211. (dolist (ext ext-inv)
  212. (when (extent-property ext 'invisible)
  213. (add-to-list 'ext-inv-specs (list ext (extent-property
  214. ext 'invisible)))
  215. (set-extent-property ext 'invisible nil)))
  216. (indent-line-to column)
  217. (dolist (ext-inv-spec ext-inv-specs)
  218. (set-extent-property (car ext-inv-spec) 'invisible
  219. (cadr ext-inv-spec))))
  220. (indent-line-to column)))
  221. (defun org-move-to-column (column &optional force buffer)
  222. (if (featurep 'xemacs)
  223. (let ((ext-inv (extent-list
  224. nil (point-at-bol) (point-at-eol)
  225. 'all-extents-closed-open 'invisible))
  226. ext-inv-specs)
  227. (dolist (ext ext-inv)
  228. (when (extent-property ext 'invisible)
  229. (add-to-list 'ext-inv-specs (list ext (extent-property ext
  230. 'invisible)))
  231. (set-extent-property ext 'invisible nil)))
  232. (move-to-column column force buffer)
  233. (dolist (ext-inv-spec ext-inv-specs)
  234. (set-extent-property (car ext-inv-spec) 'invisible
  235. (cadr ext-inv-spec))))
  236. (move-to-column column force)))
  237. (defun org-get-x-clipboard-compat (value)
  238. "Get the clipboard value on XEmacs or Emacs 21"
  239. (cond (org-xemacs-p (org-no-warnings (get-selection-no-error value)))
  240. ((fboundp 'x-get-selection)
  241. (condition-case nil
  242. (or (x-get-selection value 'UTF8_STRING)
  243. (x-get-selection value 'COMPOUND_TEXT)
  244. (x-get-selection value 'STRING)
  245. (x-get-selection value 'TEXT))
  246. (error nil)))))
  247. (defun org-propertize (string &rest properties)
  248. (if (featurep 'xemacs)
  249. (progn
  250. (add-text-properties 0 (length string) properties string)
  251. string)
  252. (apply 'propertize string properties)))
  253. (defun org-substring-no-properties (string &optional from to)
  254. (if (featurep 'xemacs)
  255. (org-no-properties (substring string (or from 0) to))
  256. (substring-no-properties string from to)))
  257. (defun org-find-library-name (library)
  258. (if (fboundp 'find-library-name)
  259. (file-name-directory (find-library-name library))
  260. ; XEmacs does not have `find-library-name'
  261. (flet ((find-library-name-helper (filename ignored-codesys)
  262. filename)
  263. (find-library-name (library)
  264. (find-library library nil 'find-library-name-helper)))
  265. (file-name-directory (find-library-name library)))))
  266. (defun org-count-lines (s)
  267. "How many lines in string S?"
  268. (let ((start 0) (n 1))
  269. (while (string-match "\n" s start)
  270. (setq start (match-end 0) n (1+ n)))
  271. (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
  272. (setq n (1- n)))
  273. n))
  274. (defun org-kill-new (string &rest args)
  275. (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
  276. string)
  277. (apply 'kill-new string args))
  278. (defun org-select-frame-set-input-focus (frame)
  279. "Select FRAME, raise it, and set input focus, if possible."
  280. (cond ((featurep 'xemacs)
  281. (if (fboundp 'select-frame-set-input-focus)
  282. (select-frame-set-input-focus frame)
  283. (raise-frame frame)
  284. (select-frame frame)
  285. (focus-frame frame)))
  286. ;; `select-frame-set-input-focus' defined in Emacs 21 will not
  287. ;; set the input focus.
  288. ((>= emacs-major-version 22)
  289. (select-frame-set-input-focus frame))
  290. (t
  291. (raise-frame frame)
  292. (select-frame frame)
  293. (cond ((memq window-system '(x ns mac))
  294. (x-focus-frame frame))
  295. ((eq window-system 'w32)
  296. (w32-focus-frame frame)))
  297. (when focus-follows-mouse
  298. (set-mouse-position frame (1- (frame-width frame)) 0)))))
  299. (defun org-float-time (&optional time)
  300. "Convert time value TIME to a floating point number.
  301. TIME defaults to the current time."
  302. (if (featurep 'xemacs)
  303. (time-to-seconds (or time (current-time)))
  304. (float-time time)))
  305. ; XEmacs does not have `looking-back'.
  306. (if (fboundp 'looking-back)
  307. (defalias 'org-looking-back 'looking-back)
  308. (defun org-looking-back (regexp &optional limit greedy)
  309. "Return non-nil if text before point matches regular expression REGEXP.
  310. Like `looking-at' except matches before point, and is slower.
  311. LIMIT if non-nil speeds up the search by specifying a minimum
  312. starting position, to avoid checking matches that would start
  313. before LIMIT.
  314. If GREEDY is non-nil, extend the match backwards as far as
  315. possible, stopping when a single additional previous character
  316. cannot be part of a match for REGEXP. When the match is
  317. extended, its starting position is allowed to occur before
  318. LIMIT."
  319. (let ((start (point))
  320. (pos
  321. (save-excursion
  322. (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
  323. (point)))))
  324. (if (and greedy pos)
  325. (save-restriction
  326. (narrow-to-region (point-min) start)
  327. (while (and (> pos (point-min))
  328. (save-excursion
  329. (goto-char pos)
  330. (backward-char 1)
  331. (looking-at (concat "\\(?:" regexp "\\)\\'"))))
  332. (setq pos (1- pos)))
  333. (save-excursion
  334. (goto-char pos)
  335. (looking-at (concat "\\(?:" regexp "\\)\\'")))))
  336. (not (null pos)))))
  337. (provide 'org-compat)
  338. ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
  339. ;;; org-compat.el ends here