org-compat.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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: 7.6
  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. ;; The following constant is for backward compatibility. We do not use
  33. ;; it in org-mode, because the Byte compiler evaluates (featurep 'xemacs)
  34. ;; at compilation time and can therefore optimize code better.
  35. (defconst org-xemacs-p (featurep 'xemacs))
  36. (defconst org-format-transports-properties-p
  37. (let ((x "a"))
  38. (add-text-properties 0 1 '(test t) x)
  39. (get-text-property 0 'test (format "%s" x)))
  40. "Does format transport text properties?")
  41. (defun org-compatible-face (inherits specs)
  42. "Make a compatible face specification.
  43. If INHERITS is an existing face and if the Emacs version supports it,
  44. just inherit the face. If INHERITS is set and the Emacs version does
  45. not support it, copy the face specification from the inheritance face.
  46. If INHERITS is not given and SPECS is, use SPECS to define the face.
  47. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  48. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  49. to the top of the list. The `min-colors' attribute will be removed from
  50. any other entries, and any resulting duplicates will be removed entirely."
  51. (when (and inherits (facep inherits) (not specs))
  52. (setq specs (or specs
  53. (get inherits 'saved-face)
  54. (get inherits 'face-defface-spec))))
  55. (cond
  56. ((and inherits (facep inherits)
  57. (not (featurep 'xemacs))
  58. (>= emacs-major-version 22)
  59. ;; do not inherit outline faces before Emacs 23
  60. (or (>= emacs-major-version 23)
  61. (not (string-match "\\`outline-[0-9]+"
  62. (symbol-name inherits)))))
  63. (list (list t :inherit inherits)))
  64. ((or (featurep 'xemacs) (< emacs-major-version 22))
  65. ;; These do not understand the `min-colors' attribute.
  66. (let (r e a)
  67. (while (setq e (pop specs))
  68. (cond
  69. ((memq (car e) '(t default)) (push e r))
  70. ((setq a (member '(min-colors 8) (car e)))
  71. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  72. (cdr e)))))
  73. ((setq a (assq 'min-colors (car e)))
  74. (setq e (cons (delq a (car e)) (cdr e)))
  75. (or (assoc (car e) r) (push e r)))
  76. (t (or (assoc (car e) r) (push e r)))))
  77. (nreverse r)))
  78. (t specs)))
  79. (put 'org-compatible-face 'lisp-indent-function 1)
  80. (defun org-version-check (version feature level)
  81. (let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
  82. (v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
  83. (rmaj (or (nth 0 v1) 99))
  84. (rmin (or (nth 1 v1) 99))
  85. (rbld (or (nth 2 v1) 99))
  86. (maj (or (nth 0 v2) 0))
  87. (min (or (nth 1 v2) 0))
  88. (bld (or (nth 2 v2) 0)))
  89. (if (or (< maj rmaj)
  90. (and (= maj rmaj)
  91. (< min rmin))
  92. (and (= maj rmaj)
  93. (= min rmin)
  94. (< bld rbld)))
  95. (if (eq level :predicate)
  96. ;; just return if we have the version
  97. nil
  98. (let ((msg (format "Emacs %s or greater is recommended for %s"
  99. version feature)))
  100. (display-warning 'org msg level)
  101. t))
  102. t)))
  103. ;;;; Emacs/XEmacs compatibility
  104. ;; Keys
  105. (defconst org-xemacs-key-equivalents
  106. '(([mouse-1] . [button1])
  107. ([mouse-2] . [button2])
  108. ([mouse-3] . [button3])
  109. ([C-mouse-4] . [(control mouse-4)])
  110. ([C-mouse-5] . [(control mouse-5)]))
  111. "Translation alist for a couple of keys.")
  112. ;; Overlay compatibility functions
  113. (defun org-detach-overlay (ovl)
  114. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  115. (defun org-overlay-display (ovl text &optional face evap)
  116. "Make overlay OVL display TEXT with face FACE."
  117. (if (featurep 'xemacs)
  118. (let ((gl (make-glyph text)))
  119. (and face (set-glyph-face gl face))
  120. (set-extent-property ovl 'invisible t)
  121. (set-extent-property ovl 'end-glyph gl))
  122. (overlay-put ovl 'display text)
  123. (if face (overlay-put ovl 'face face))
  124. (if evap (overlay-put ovl 'evaporate t))))
  125. (defun org-overlay-before-string (ovl text &optional face evap)
  126. "Make overlay OVL display TEXT with face FACE."
  127. (if (featurep 'xemacs)
  128. (let ((gl (make-glyph text)))
  129. (and face (set-glyph-face gl face))
  130. (set-extent-property ovl 'begin-glyph gl))
  131. (if face (org-add-props text nil 'face face))
  132. (overlay-put ovl 'before-string text)
  133. (if evap (overlay-put ovl 'evaporate t))))
  134. (defun org-find-overlays (prop &optional pos delete)
  135. "Find all overlays specifying PROP at POS or point.
  136. If DELETE is non-nil, delete all those overlays."
  137. (let ((overlays (overlays-at (or pos (point))))
  138. ov found)
  139. (while (setq ov (pop overlays))
  140. (if (overlay-get ov prop)
  141. (if delete (delete-overlay ov) (push ov found))))
  142. found))
  143. (defun org-get-x-clipboard (value)
  144. "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
  145. (if (eq window-system 'x)
  146. (let ((x (org-get-x-clipboard-compat value)))
  147. (if x (org-no-properties x)))))
  148. (defsubst org-decompose-region (beg end)
  149. "Decompose from BEG to END."
  150. (if (featurep 'xemacs)
  151. (let ((modified-p (buffer-modified-p))
  152. (buffer-read-only nil))
  153. (remove-text-properties beg end '(composition nil))
  154. (set-buffer-modified-p modified-p))
  155. (decompose-region beg end)))
  156. ;; Miscellaneous functions
  157. (defun org-add-hook (hook function &optional append local)
  158. "Add-hook, compatible with both Emacsen."
  159. (if (and local (featurep 'xemacs))
  160. (add-local-hook hook function append)
  161. (add-hook hook function append local)))
  162. (defun org-add-props (string plist &rest props)
  163. "Add text properties to entire string, from beginning to end.
  164. PLIST may be a list of properties, PROPS are individual properties and values
  165. that will be added to PLIST. Returns the string that was modified."
  166. (add-text-properties
  167. 0 (length string) (if props (append plist props) plist) string)
  168. string)
  169. (put 'org-add-props 'lisp-indent-function 2)
  170. (defun org-fit-window-to-buffer (&optional window max-height min-height
  171. shrink-only)
  172. "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
  173. WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
  174. passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
  175. `shrink-window-if-larger-than-buffer' instead, the height limit is
  176. ignored in this case."
  177. (cond ((if (fboundp 'window-full-width-p)
  178. (not (window-full-width-p window))
  179. (> (frame-width) (window-width window)))
  180. ;; do nothing if another window would suffer
  181. )
  182. ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
  183. (fit-window-to-buffer window max-height min-height))
  184. ((fboundp 'shrink-window-if-larger-than-buffer)
  185. (shrink-window-if-larger-than-buffer window)))
  186. (or window (selected-window)))
  187. (defun org-number-sequence (from &optional to inc)
  188. "Call `number-sequence or emulate it."
  189. (if (fboundp 'number-sequence)
  190. (number-sequence from to inc)
  191. (if (or (not to) (= from to))
  192. (list from)
  193. (or inc (setq inc 1))
  194. (when (zerop inc) (error "The increment can not be zero"))
  195. (let (seq (n 0) (next from))
  196. (if (> inc 0)
  197. (while (<= next to)
  198. (setq seq (cons next seq)
  199. n (1+ n)
  200. next (+ from (* n inc))))
  201. (while (>= next to)
  202. (setq seq (cons next seq)
  203. n (1+ n)
  204. next (+ from (* n inc)))))
  205. (nreverse seq)))))
  206. ;; Region compatibility
  207. (defvar org-ignore-region nil
  208. "To temporarily disable the active region.")
  209. (defun org-region-active-p ()
  210. "Is `transient-mark-mode' on and the region active?
  211. Works on both Emacs and XEmacs."
  212. (if org-ignore-region
  213. nil
  214. (if (featurep 'xemacs)
  215. (and zmacs-regions (region-active-p))
  216. (if (fboundp 'use-region-p)
  217. (use-region-p)
  218. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  219. (defun org-cursor-to-region-beginning ()
  220. (when (and (org-region-active-p)
  221. (> (point) (region-beginning)))
  222. (exchange-point-and-mark)))
  223. ;; Emacs 22 misses `activate-mark'
  224. (if (fboundp 'activate-mark)
  225. (defalias 'org-activate-mark 'activate-mark)
  226. (defun org-activate-mark ()
  227. (when (mark t)
  228. (setq mark-active t)
  229. (unless transient-mark-mode
  230. (setq transient-mark-mode 'lambda)))))
  231. ;; Invisibility compatibility
  232. (defun org-remove-from-invisibility-spec (arg)
  233. "Remove elements from `buffer-invisibility-spec'."
  234. (if (fboundp 'remove-from-invisibility-spec)
  235. (remove-from-invisibility-spec arg)
  236. (if (consp buffer-invisibility-spec)
  237. (setq buffer-invisibility-spec
  238. (delete arg buffer-invisibility-spec)))))
  239. (defun org-in-invisibility-spec-p (arg)
  240. "Is ARG a member of `buffer-invisibility-spec'?"
  241. (if (consp buffer-invisibility-spec)
  242. (member arg buffer-invisibility-spec)
  243. nil))
  244. (defmacro org-xemacs-without-invisibility (&rest body)
  245. "Turn off exents with invisibility while executing BODY."
  246. `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
  247. 'all-extents-closed-open 'invisible))
  248. ext-inv-specs)
  249. (dolist (ext ext-inv)
  250. (when (extent-property ext 'invisible)
  251. (add-to-list 'ext-inv-specs (list ext (extent-property
  252. ext 'invisible)))
  253. (set-extent-property ext 'invisible nil)))
  254. ,@body
  255. (dolist (ext-inv-spec ext-inv-specs)
  256. (set-extent-property (car ext-inv-spec) 'invisible
  257. (cadr ext-inv-spec)))))
  258. (defun org-indent-to-column (column &optional minimum buffer)
  259. "Work around a bug with extents with invisibility in XEmacs."
  260. (if (featurep 'xemacs)
  261. (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
  262. (indent-to-column column minimum)))
  263. (defun org-indent-line-to (column)
  264. "Work around a bug with extents with invisibility in XEmacs."
  265. (if (featurep 'xemacs)
  266. (org-xemacs-without-invisibility (indent-line-to column))
  267. (indent-line-to column)))
  268. (defun org-move-to-column (column &optional force buffer)
  269. (if (featurep 'xemacs)
  270. (org-xemacs-without-invisibility (move-to-column column force buffer))
  271. (move-to-column column force)))
  272. (defun org-get-x-clipboard-compat (value)
  273. "Get the clipboard value on XEmacs or Emacs 21."
  274. (cond ((featurep 'xemacs)
  275. (org-no-warnings (get-selection-no-error value)))
  276. ((fboundp 'x-get-selection)
  277. (condition-case nil
  278. (or (x-get-selection value 'UTF8_STRING)
  279. (x-get-selection value 'COMPOUND_TEXT)
  280. (x-get-selection value 'STRING)
  281. (x-get-selection value 'TEXT))
  282. (error nil)))))
  283. (defun org-propertize (string &rest properties)
  284. (if (featurep 'xemacs)
  285. (progn
  286. (add-text-properties 0 (length string) properties string)
  287. string)
  288. (apply 'propertize string properties)))
  289. (defun org-substring-no-properties (string &optional from to)
  290. (if (featurep 'xemacs)
  291. (org-no-properties (substring string (or from 0) to))
  292. (substring-no-properties string from to)))
  293. (defun org-find-library-name (library)
  294. (if (fboundp 'find-library-name)
  295. (file-name-directory (find-library-name library))
  296. ; XEmacs does not have `find-library-name'
  297. (flet ((find-library-name-helper (filename ignored-codesys)
  298. filename)
  299. (find-library-name (library)
  300. (find-library library nil 'find-library-name-helper)))
  301. (file-name-directory (find-library-name library)))))
  302. (defun org-count-lines (s)
  303. "How many lines in string S?"
  304. (let ((start 0) (n 1))
  305. (while (string-match "\n" s start)
  306. (setq start (match-end 0) n (1+ n)))
  307. (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
  308. (setq n (1- n)))
  309. n))
  310. (defun org-kill-new (string &rest args)
  311. (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
  312. string)
  313. (apply 'kill-new string args))
  314. (defun org-select-frame-set-input-focus (frame)
  315. "Select FRAME, raise it, and set input focus, if possible."
  316. (cond ((featurep 'xemacs)
  317. (if (fboundp 'select-frame-set-input-focus)
  318. (select-frame-set-input-focus frame)
  319. (raise-frame frame)
  320. (select-frame frame)
  321. (focus-frame frame)))
  322. ;; `select-frame-set-input-focus' defined in Emacs 21 will not
  323. ;; set the input focus.
  324. ((>= emacs-major-version 22)
  325. (select-frame-set-input-focus frame))
  326. (t
  327. (raise-frame frame)
  328. (select-frame frame)
  329. (cond ((memq window-system '(x ns mac))
  330. (x-focus-frame frame))
  331. ((eq window-system 'w32)
  332. (w32-focus-frame frame)))
  333. (when focus-follows-mouse
  334. (set-mouse-position frame (1- (frame-width frame)) 0)))))
  335. (defun org-float-time (&optional time)
  336. "Convert time value TIME to a floating point number.
  337. TIME defaults to the current time."
  338. (if (featurep 'xemacs)
  339. (time-to-seconds (or time (current-time)))
  340. (float-time time)))
  341. (if (fboundp 'string-match-p)
  342. (defalias 'org-string-match-p 'string-match-p)
  343. (defun org-string-match-p (regexp string &optional start)
  344. (save-match-data
  345. (funcall 'string-match regexp string start))))
  346. (if (fboundp 'looking-at-p)
  347. (defalias 'org-looking-at-p 'looking-at-p)
  348. (defun org-looking-at-p (&rest args)
  349. (save-match-data
  350. (apply 'looking-at args))))
  351. ; XEmacs does not have `looking-back'.
  352. (if (fboundp 'looking-back)
  353. (defalias 'org-looking-back 'looking-back)
  354. (defun org-looking-back (regexp &optional limit greedy)
  355. "Return non-nil if text before point matches regular expression REGEXP.
  356. Like `looking-at' except matches before point, and is slower.
  357. LIMIT if non-nil speeds up the search by specifying a minimum
  358. starting position, to avoid checking matches that would start
  359. before LIMIT.
  360. If GREEDY is non-nil, extend the match backwards as far as
  361. possible, stopping when a single additional previous character
  362. cannot be part of a match for REGEXP. When the match is
  363. extended, its starting position is allowed to occur before
  364. LIMIT."
  365. (let ((start (point))
  366. (pos
  367. (save-excursion
  368. (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
  369. (point)))))
  370. (if (and greedy pos)
  371. (save-restriction
  372. (narrow-to-region (point-min) start)
  373. (while (and (> pos (point-min))
  374. (save-excursion
  375. (goto-char pos)
  376. (backward-char 1)
  377. (looking-at (concat "\\(?:" regexp "\\)\\'"))))
  378. (setq pos (1- pos)))
  379. (save-excursion
  380. (goto-char pos)
  381. (looking-at (concat "\\(?:" regexp "\\)\\'")))))
  382. (not (null pos)))))
  383. (defun org-floor* (x &optional y)
  384. "Return a list of the floor of X and the fractional part of X.
  385. With two arguments, return floor and remainder of their quotient."
  386. (let ((q (floor x y)))
  387. (list q (- x (if y (* y q) q)))))
  388. (provide 'org-compat)
  389. ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe
  390. ;;; org-compat.el ends here