org-compat.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. ;;; org-compat.el --- Compatibility code for Org-mode
  2. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file contains code needed for compatibility with XEmacs and older
  23. ;; versions of GNU Emacs.
  24. ;;; Code:
  25. (eval-when-compile
  26. (require 'cl))
  27. (require 'org-macs)
  28. (declare-function w32-focus-frame "term/w32-win" (frame))
  29. ;; The following constant is for backward compatibility. We do not use
  30. ;; it in org-mode, because the Byte compiler evaluates (featurep 'xemacs)
  31. ;; at compilation time and can therefore optimize code better.
  32. (defconst org-xemacs-p (featurep 'xemacs))
  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. (defun org-version-check (version feature level)
  78. (let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
  79. (v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
  80. (rmaj (or (nth 0 v1) 99))
  81. (rmin (or (nth 1 v1) 99))
  82. (rbld (or (nth 2 v1) 99))
  83. (maj (or (nth 0 v2) 0))
  84. (min (or (nth 1 v2) 0))
  85. (bld (or (nth 2 v2) 0)))
  86. (if (or (< maj rmaj)
  87. (and (= maj rmaj)
  88. (< min rmin))
  89. (and (= maj rmaj)
  90. (= min rmin)
  91. (< bld rbld)))
  92. (if (eq level :predicate)
  93. ;; just return if we have the version
  94. nil
  95. (let ((msg (format "Emacs %s or greater is recommended for %s"
  96. version feature)))
  97. (display-warning 'org msg level)
  98. t))
  99. t)))
  100. ;;;; Emacs/XEmacs compatibility
  101. (eval-and-compile
  102. (when (and (not (boundp 'user-emacs-directory))
  103. (boundp 'user-init-directory))
  104. (defvaralias 'user-emacs-directory 'user-init-directory)))
  105. ;; Keys
  106. (defconst org-xemacs-key-equivalents
  107. '(([mouse-1] . [button1])
  108. ([mouse-2] . [button2])
  109. ([mouse-3] . [button3])
  110. ([C-mouse-4] . [(control mouse-4)])
  111. ([C-mouse-5] . [(control mouse-5)]))
  112. "Translation alist for a couple of keys.")
  113. ;; Overlay compatibility functions
  114. (defun org-detach-overlay (ovl)
  115. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  116. (defun org-overlay-display (ovl text &optional face evap)
  117. "Make overlay OVL display TEXT with face FACE."
  118. (if (featurep 'xemacs)
  119. (let ((gl (make-glyph text)))
  120. (and face (set-glyph-face gl face))
  121. (set-extent-property ovl 'invisible t)
  122. (set-extent-property ovl 'end-glyph gl))
  123. (overlay-put ovl 'display text)
  124. (if face (overlay-put ovl 'face face))
  125. (if evap (overlay-put ovl 'evaporate t))))
  126. (defun org-overlay-before-string (ovl text &optional face evap)
  127. "Make overlay OVL display TEXT with face FACE."
  128. (if (featurep 'xemacs)
  129. (let ((gl (make-glyph text)))
  130. (and face (set-glyph-face gl face))
  131. (set-extent-property ovl 'begin-glyph gl))
  132. (if face (org-add-props text nil 'face face))
  133. (overlay-put ovl 'before-string text)
  134. (if evap (overlay-put ovl 'evaporate t))))
  135. (defun org-find-overlays (prop &optional pos delete)
  136. "Find all overlays specifying PROP at POS or point.
  137. If DELETE is non-nil, delete all those overlays."
  138. (let ((overlays (overlays-at (or pos (point))))
  139. ov found)
  140. (while (setq ov (pop overlays))
  141. (if (overlay-get ov prop)
  142. (if delete (delete-overlay ov) (push ov found))))
  143. found))
  144. (defun org-get-x-clipboard (value)
  145. "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
  146. (if (eq window-system 'x)
  147. (let ((x (org-get-x-clipboard-compat value)))
  148. (if x (org-no-properties x)))))
  149. (defsubst org-decompose-region (beg end)
  150. "Decompose from BEG to END."
  151. (if (featurep 'xemacs)
  152. (let ((modified-p (buffer-modified-p))
  153. (buffer-read-only nil))
  154. (remove-text-properties beg end '(composition nil))
  155. (set-buffer-modified-p modified-p))
  156. (decompose-region beg end)))
  157. ;; Miscellaneous functions
  158. (defun org-add-hook (hook function &optional append local)
  159. "Add-hook, compatible with both Emacsen."
  160. (if (and local (featurep 'xemacs))
  161. (add-local-hook hook function append)
  162. (add-hook hook function append local)))
  163. (defun org-add-props (string plist &rest props)
  164. "Add text properties to entire string, from beginning to end.
  165. PLIST may be a list of properties, PROPS are individual properties and values
  166. that will be added to PLIST. Returns the string that was modified."
  167. (add-text-properties
  168. 0 (length string) (if props (append plist props) plist) string)
  169. string)
  170. (put 'org-add-props 'lisp-indent-function 2)
  171. (defun org-fit-window-to-buffer (&optional window max-height min-height
  172. shrink-only)
  173. "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
  174. WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
  175. passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
  176. `shrink-window-if-larger-than-buffer' instead, the height limit is
  177. ignored in this case."
  178. (cond ((if (fboundp 'window-full-width-p)
  179. (not (window-full-width-p window))
  180. ;; do nothing if another window would suffer
  181. (> (frame-width) (window-width window))))
  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. (when (and (boundp 'transient-mark-mode)
  230. (not transient-mark-mode))
  231. (setq transient-mark-mode 'lambda))
  232. (when (boundp 'zmacs-regions)
  233. (setq zmacs-regions t)))))
  234. ;; Invisibility compatibility
  235. (defun org-remove-from-invisibility-spec (arg)
  236. "Remove elements from `buffer-invisibility-spec'."
  237. (if (fboundp 'remove-from-invisibility-spec)
  238. (remove-from-invisibility-spec arg)
  239. (if (consp buffer-invisibility-spec)
  240. (setq buffer-invisibility-spec
  241. (delete arg buffer-invisibility-spec)))))
  242. (defun org-in-invisibility-spec-p (arg)
  243. "Is ARG a member of `buffer-invisibility-spec'?"
  244. (if (consp buffer-invisibility-spec)
  245. (member arg buffer-invisibility-spec)
  246. nil))
  247. (defmacro org-xemacs-without-invisibility (&rest body)
  248. "Turn off extents with invisibility while executing BODY."
  249. `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
  250. 'all-extents-closed-open 'invisible))
  251. ext-inv-specs)
  252. (dolist (ext ext-inv)
  253. (when (extent-property ext 'invisible)
  254. (add-to-list 'ext-inv-specs (list ext (extent-property
  255. ext 'invisible)))
  256. (set-extent-property ext 'invisible nil)))
  257. ,@body
  258. (dolist (ext-inv-spec ext-inv-specs)
  259. (set-extent-property (car ext-inv-spec) 'invisible
  260. (cadr ext-inv-spec)))))
  261. (def-edebug-spec org-xemacs-without-invisibility (body))
  262. (defun org-indent-to-column (column &optional minimum buffer)
  263. "Work around a bug with extents with invisibility in XEmacs."
  264. (if (featurep 'xemacs)
  265. (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
  266. (indent-to-column column minimum)))
  267. (defun org-indent-line-to (column)
  268. "Work around a bug with extents with invisibility in XEmacs."
  269. (if (featurep 'xemacs)
  270. (org-xemacs-without-invisibility (indent-line-to column))
  271. (indent-line-to column)))
  272. (defun org-move-to-column (column &optional force buffer)
  273. (if (featurep 'xemacs)
  274. (org-xemacs-without-invisibility (move-to-column column force buffer))
  275. (move-to-column column force)))
  276. (defun org-get-x-clipboard-compat (value)
  277. "Get the clipboard value on XEmacs or Emacs 21."
  278. (cond ((featurep 'xemacs)
  279. (org-no-warnings (get-selection-no-error value)))
  280. ((fboundp 'x-get-selection)
  281. (condition-case nil
  282. (or (x-get-selection value 'UTF8_STRING)
  283. (x-get-selection value 'COMPOUND_TEXT)
  284. (x-get-selection value 'STRING)
  285. (x-get-selection value 'TEXT))
  286. (error nil)))))
  287. (defun org-propertize (string &rest properties)
  288. (if (featurep 'xemacs)
  289. (progn
  290. (add-text-properties 0 (length string) properties string)
  291. string)
  292. (apply 'propertize string properties)))
  293. (defmacro org-find-library-dir (library)
  294. `(file-name-directory (or (locate-library ,library) "")))
  295. (defun org-count-lines (s)
  296. "How many lines in string S?"
  297. (let ((start 0) (n 1))
  298. (while (string-match "\n" s start)
  299. (setq start (match-end 0) n (1+ n)))
  300. (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
  301. (setq n (1- n)))
  302. n))
  303. (defun org-kill-new (string &rest args)
  304. (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
  305. string)
  306. (apply 'kill-new string args))
  307. (defun org-select-frame-set-input-focus (frame)
  308. "Select FRAME, raise it, and set input focus, if possible."
  309. (cond ((featurep 'xemacs)
  310. (if (fboundp 'select-frame-set-input-focus)
  311. (select-frame-set-input-focus frame)
  312. (raise-frame frame)
  313. (select-frame frame)
  314. (focus-frame frame)))
  315. ;; `select-frame-set-input-focus' defined in Emacs 21 will not
  316. ;; set the input focus.
  317. ((>= emacs-major-version 22)
  318. (select-frame-set-input-focus frame))
  319. (t
  320. (raise-frame frame)
  321. (select-frame frame)
  322. (cond ((memq window-system '(x ns mac))
  323. (x-focus-frame frame))
  324. ((eq window-system 'w32)
  325. (w32-focus-frame frame)))
  326. (when focus-follows-mouse
  327. (set-mouse-position frame (1- (frame-width frame)) 0)))))
  328. (defun org-float-time (&optional time)
  329. "Convert time value TIME to a floating point number.
  330. TIME defaults to the current time."
  331. (if (featurep 'xemacs)
  332. (time-to-seconds (or time (current-time)))
  333. (float-time time)))
  334. ;; `user-error' is only available from 24.2.50 on
  335. (unless (fboundp 'user-error)
  336. (defalias 'user-error 'error))
  337. (defmacro org-no-popups (&rest body)
  338. "Suppress popup windows.
  339. Let-bind some variables to nil around BODY to achieve the desired
  340. effect, which variables to use depends on the Emacs version."
  341. (if (org-version-check "24.2.50" "" :predicate)
  342. `(let (pop-up-frames display-buffer-alist)
  343. ,@body)
  344. `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
  345. ,@body)))
  346. (if (fboundp 'string-match-p)
  347. (defalias 'org-string-match-p 'string-match-p)
  348. (defun org-string-match-p (regexp string &optional start)
  349. (save-match-data
  350. (funcall 'string-match regexp string start))))
  351. (if (fboundp 'looking-at-p)
  352. (defalias 'org-looking-at-p 'looking-at-p)
  353. (defun org-looking-at-p (&rest args)
  354. (save-match-data
  355. (apply 'looking-at args))))
  356. ;; XEmacs does not have `looking-back'.
  357. (if (fboundp 'looking-back)
  358. (defalias 'org-looking-back 'looking-back)
  359. (defun org-looking-back (regexp &optional limit greedy)
  360. "Return non-nil if text before point matches regular expression REGEXP.
  361. Like `looking-at' except matches before point, and is slower.
  362. LIMIT if non-nil speeds up the search by specifying a minimum
  363. starting position, to avoid checking matches that would start
  364. before LIMIT.
  365. If GREEDY is non-nil, extend the match backwards as far as
  366. possible, stopping when a single additional previous character
  367. cannot be part of a match for REGEXP. When the match is
  368. extended, its starting position is allowed to occur before
  369. LIMIT."
  370. (let ((start (point))
  371. (pos
  372. (save-excursion
  373. (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
  374. (point)))))
  375. (if (and greedy pos)
  376. (save-restriction
  377. (narrow-to-region (point-min) start)
  378. (while (and (> pos (point-min))
  379. (save-excursion
  380. (goto-char pos)
  381. (backward-char 1)
  382. (looking-at (concat "\\(?:" regexp "\\)\\'"))))
  383. (setq pos (1- pos)))
  384. (save-excursion
  385. (goto-char pos)
  386. (looking-at (concat "\\(?:" regexp "\\)\\'")))))
  387. (not (null pos)))))
  388. (defun org-floor* (x &optional y)
  389. "Return a list of the floor of X and the fractional part of X.
  390. With two arguments, return floor and remainder of their quotient."
  391. (let ((q (floor x y)))
  392. (list q (- x (if y (* y q) q)))))
  393. ;; `pop-to-buffer-same-window' has been introduced in Emacs 24.1.
  394. (defun org-pop-to-buffer-same-window
  395. (&optional buffer-or-name norecord label)
  396. "Pop to buffer specified by BUFFER-OR-NAME in the selected window."
  397. (if (fboundp 'pop-to-buffer-same-window)
  398. (funcall
  399. 'pop-to-buffer-same-window buffer-or-name norecord)
  400. (funcall 'switch-to-buffer buffer-or-name norecord)))
  401. ;; RECURSIVE has been introduced with Emacs 23.2.
  402. ;; This is copying and adapted from `tramp-compat-delete-directory'
  403. (defun org-delete-directory (directory &optional recursive)
  404. "Compatibility function for `delete-directory'."
  405. (if (null recursive)
  406. (delete-directory directory)
  407. (condition-case nil
  408. (funcall 'delete-directory directory recursive)
  409. ;; This Emacs version does not support the RECURSIVE flag. We
  410. ;; use the implementation from Emacs 23.2.
  411. (wrong-number-of-arguments
  412. (setq directory (directory-file-name (expand-file-name directory)))
  413. (if (not (file-symlink-p directory))
  414. (mapc (lambda (file)
  415. (if (eq t (car (file-attributes file)))
  416. (org-delete-directory file recursive)
  417. (delete-file file)))
  418. (directory-files
  419. directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
  420. (delete-directory directory)))))
  421. ;;;###autoload
  422. (defmacro org-check-version ()
  423. "Try very hard to provide sensible version strings."
  424. (let* ((org-dir (org-find-library-dir "org"))
  425. (org-version.el (concat org-dir "org-version.el"))
  426. (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
  427. (if (require 'org-version org-version.el 'noerror)
  428. '(progn
  429. (autoload 'org-release "org-version.el")
  430. (autoload 'org-git-version "org-version.el"))
  431. (if (require 'org-fixup org-fixup.el 'noerror)
  432. '(org-fixup)
  433. ;; provide fallback definitions and complain
  434. (warn "Could not define org version correctly. Check installation!")
  435. '(progn
  436. (defun org-release () "N/A")
  437. (defun org-git-version () "N/A !!check installation!!"))))))
  438. (provide 'org-compat)
  439. ;;; org-compat.el ends here