org-compat.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. ;; Keys
  102. (defconst org-xemacs-key-equivalents
  103. '(([mouse-1] . [button1])
  104. ([mouse-2] . [button2])
  105. ([mouse-3] . [button3])
  106. ([C-mouse-4] . [(control mouse-4)])
  107. ([C-mouse-5] . [(control mouse-5)]))
  108. "Translation alist for a couple of keys.")
  109. ;; Overlay compatibility functions
  110. (defun org-detach-overlay (ovl)
  111. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  112. (defun org-overlay-display (ovl text &optional face evap)
  113. "Make overlay OVL display TEXT with face FACE."
  114. (if (featurep 'xemacs)
  115. (let ((gl (make-glyph text)))
  116. (and face (set-glyph-face gl face))
  117. (set-extent-property ovl 'invisible t)
  118. (set-extent-property ovl 'end-glyph gl))
  119. (overlay-put ovl 'display text)
  120. (if face (overlay-put ovl 'face face))
  121. (if evap (overlay-put ovl 'evaporate t))))
  122. (defun org-overlay-before-string (ovl text &optional face evap)
  123. "Make overlay OVL display TEXT with face FACE."
  124. (if (featurep 'xemacs)
  125. (let ((gl (make-glyph text)))
  126. (and face (set-glyph-face gl face))
  127. (set-extent-property ovl 'begin-glyph gl))
  128. (if face (org-add-props text nil 'face face))
  129. (overlay-put ovl 'before-string text)
  130. (if evap (overlay-put ovl 'evaporate t))))
  131. (defun org-find-overlays (prop &optional pos delete)
  132. "Find all overlays specifying PROP at POS or point.
  133. If DELETE is non-nil, delete all those overlays."
  134. (let ((overlays (overlays-at (or pos (point))))
  135. ov found)
  136. (while (setq ov (pop overlays))
  137. (if (overlay-get ov prop)
  138. (if delete (delete-overlay ov) (push ov found))))
  139. found))
  140. (defun org-get-x-clipboard (value)
  141. "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
  142. (if (eq window-system 'x)
  143. (let ((x (org-get-x-clipboard-compat value)))
  144. (if x (org-no-properties x)))))
  145. (defsubst org-decompose-region (beg end)
  146. "Decompose from BEG to END."
  147. (if (featurep 'xemacs)
  148. (let ((modified-p (buffer-modified-p))
  149. (buffer-read-only nil))
  150. (remove-text-properties beg end '(composition nil))
  151. (set-buffer-modified-p modified-p))
  152. (decompose-region beg end)))
  153. (defmacro org-define-obsolete-function-alias (o-name c-name when &optional doc)
  154. "Reconcile the two-argument form of
  155. `define-obsolete-function-alias' in XEmacs/Emacs 22 with the 3-4
  156. argument form in Emacs 23 and later."
  157. (if (or (featurep 'xemacs)
  158. (< emacs-major-version 23))
  159. `(define-obsolete-function-alias ,o-name ,c-name)
  160. `(define-obsolete-function-alias ,o-name ,c-name ,when ,doc)))
  161. (defmacro org-define-obsolete-variable-alias (o-name c-name when &optional doc)
  162. "Reconcile the two-argument form of
  163. `define-obsolete-variable-alias' in XEmacs/Emacs 22 with the 3-4
  164. argument form in Emacs 23 and later."
  165. (if (or (featurep 'xemacs)
  166. (< emacs-major-version 23))
  167. `(define-obsolete-variable-alias ,o-name ,c-name)
  168. `(define-obsolete-variable-alias ,o-name ,c-name ,when ,doc)))
  169. ;; Miscellaneous functions
  170. (defun org-add-hook (hook function &optional append local)
  171. "Add-hook, compatible with both Emacsen."
  172. (if (and local (featurep 'xemacs))
  173. (add-local-hook hook function append)
  174. (add-hook hook function append local)))
  175. (defun org-add-props (string plist &rest props)
  176. "Add text properties to entire string, from beginning to end.
  177. PLIST may be a list of properties, PROPS are individual properties and values
  178. that will be added to PLIST. Returns the string that was modified."
  179. (add-text-properties
  180. 0 (length string) (if props (append plist props) plist) string)
  181. string)
  182. (put 'org-add-props 'lisp-indent-function 2)
  183. (defun org-fit-window-to-buffer (&optional window max-height min-height
  184. shrink-only)
  185. "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
  186. WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
  187. passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
  188. `shrink-window-if-larger-than-buffer' instead, the height limit is
  189. ignored in this case."
  190. (cond ((if (fboundp 'window-full-width-p)
  191. (not (window-full-width-p window))
  192. ;; do nothing if another window would suffer
  193. (> (frame-width) (window-width window))))
  194. ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
  195. (fit-window-to-buffer window max-height min-height))
  196. ((fboundp 'shrink-window-if-larger-than-buffer)
  197. (shrink-window-if-larger-than-buffer window)))
  198. (or window (selected-window)))
  199. (defun org-number-sequence (from &optional to inc)
  200. "Call `number-sequence or emulate it."
  201. (if (fboundp 'number-sequence)
  202. (number-sequence from to inc)
  203. (if (or (not to) (= from to))
  204. (list from)
  205. (or inc (setq inc 1))
  206. (when (zerop inc) (error "The increment can not be zero"))
  207. (let (seq (n 0) (next from))
  208. (if (> inc 0)
  209. (while (<= next to)
  210. (setq seq (cons next seq)
  211. n (1+ n)
  212. next (+ from (* n inc))))
  213. (while (>= next to)
  214. (setq seq (cons next seq)
  215. n (1+ n)
  216. next (+ from (* n inc)))))
  217. (nreverse seq)))))
  218. ;; Region compatibility
  219. (defvar org-ignore-region nil
  220. "To temporarily disable the active region.")
  221. (defun org-region-active-p ()
  222. "Is `transient-mark-mode' on and the region active?
  223. Works on both Emacs and XEmacs."
  224. (if org-ignore-region
  225. nil
  226. (if (featurep 'xemacs)
  227. (and zmacs-regions (region-active-p))
  228. (if (fboundp 'use-region-p)
  229. (use-region-p)
  230. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  231. (defun org-cursor-to-region-beginning ()
  232. (when (and (org-region-active-p)
  233. (> (point) (region-beginning)))
  234. (exchange-point-and-mark)))
  235. ;; Emacs 22 misses `activate-mark'
  236. (if (fboundp 'activate-mark)
  237. (defalias 'org-activate-mark 'activate-mark)
  238. (defun org-activate-mark ()
  239. (when (mark t)
  240. (setq mark-active t)
  241. (when (and (boundp 'transient-mark-mode)
  242. (not transient-mark-mode))
  243. (setq transient-mark-mode 'lambda))
  244. (when (boundp 'zmacs-regions)
  245. (setq zmacs-regions t)))))
  246. ;; Invisibility compatibility
  247. (defun org-remove-from-invisibility-spec (arg)
  248. "Remove elements from `buffer-invisibility-spec'."
  249. (if (fboundp 'remove-from-invisibility-spec)
  250. (remove-from-invisibility-spec arg)
  251. (if (consp buffer-invisibility-spec)
  252. (setq buffer-invisibility-spec
  253. (delete arg buffer-invisibility-spec)))))
  254. (defun org-in-invisibility-spec-p (arg)
  255. "Is ARG a member of `buffer-invisibility-spec'?"
  256. (if (consp buffer-invisibility-spec)
  257. (member arg buffer-invisibility-spec)
  258. nil))
  259. (defmacro org-xemacs-without-invisibility (&rest body)
  260. "Turn off extents with invisibility while executing BODY."
  261. `(let ((ext-inv (extent-list nil (point-at-bol) (point-at-eol)
  262. 'all-extents-closed-open 'invisible))
  263. ext-inv-specs)
  264. (dolist (ext ext-inv)
  265. (when (extent-property ext 'invisible)
  266. (add-to-list 'ext-inv-specs (list ext (extent-property
  267. ext 'invisible)))
  268. (set-extent-property ext 'invisible nil)))
  269. ,@body
  270. (dolist (ext-inv-spec ext-inv-specs)
  271. (set-extent-property (car ext-inv-spec) 'invisible
  272. (cadr ext-inv-spec)))))
  273. (def-edebug-spec org-xemacs-without-invisibility (body))
  274. (defun org-indent-to-column (column &optional minimum buffer)
  275. "Work around a bug with extents with invisibility in XEmacs."
  276. (if (featurep 'xemacs)
  277. (org-xemacs-without-invisibility (indent-to-column column minimum buffer))
  278. (indent-to-column column minimum)))
  279. (defun org-indent-line-to (column)
  280. "Work around a bug with extents with invisibility in XEmacs."
  281. (if (featurep 'xemacs)
  282. (org-xemacs-without-invisibility (indent-line-to column))
  283. (indent-line-to column)))
  284. (defun org-move-to-column (column &optional force buffer)
  285. (if (featurep 'xemacs)
  286. (org-xemacs-without-invisibility (move-to-column column force buffer))
  287. (move-to-column column force)))
  288. (defun org-get-x-clipboard-compat (value)
  289. "Get the clipboard value on XEmacs or Emacs 21."
  290. (cond ((featurep 'xemacs)
  291. (org-no-warnings (get-selection-no-error value)))
  292. ((fboundp 'x-get-selection)
  293. (condition-case nil
  294. (or (x-get-selection value 'UTF8_STRING)
  295. (x-get-selection value 'COMPOUND_TEXT)
  296. (x-get-selection value 'STRING)
  297. (x-get-selection value 'TEXT))
  298. (error nil)))))
  299. (defun org-propertize (string &rest properties)
  300. (if (featurep 'xemacs)
  301. (progn
  302. (add-text-properties 0 (length string) properties string)
  303. string)
  304. (apply 'propertize string properties)))
  305. (defmacro org-find-library-dir (library)
  306. `(file-name-directory (or (locate-library ,library) "")))
  307. (defun org-count-lines (s)
  308. "How many lines in string S?"
  309. (let ((start 0) (n 1))
  310. (while (string-match "\n" s start)
  311. (setq start (match-end 0) n (1+ n)))
  312. (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
  313. (setq n (1- n)))
  314. n))
  315. (defun org-kill-new (string &rest args)
  316. (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
  317. string)
  318. (apply 'kill-new string args))
  319. (defun org-select-frame-set-input-focus (frame)
  320. "Select FRAME, raise it, and set input focus, if possible."
  321. (cond ((featurep 'xemacs)
  322. (if (fboundp 'select-frame-set-input-focus)
  323. (select-frame-set-input-focus frame)
  324. (raise-frame frame)
  325. (select-frame frame)
  326. (focus-frame frame)))
  327. ;; `select-frame-set-input-focus' defined in Emacs 21 will not
  328. ;; set the input focus.
  329. ((>= emacs-major-version 22)
  330. (select-frame-set-input-focus frame))
  331. (t
  332. (raise-frame frame)
  333. (select-frame frame)
  334. (cond ((memq window-system '(x ns mac))
  335. (x-focus-frame frame))
  336. ((eq window-system 'w32)
  337. (w32-focus-frame frame)))
  338. (when focus-follows-mouse
  339. (set-mouse-position frame (1- (frame-width frame)) 0)))))
  340. (defun org-float-time (&optional time)
  341. "Convert time value TIME to a floating point number.
  342. TIME defaults to the current time."
  343. (if (featurep 'xemacs)
  344. (time-to-seconds (or time (current-time)))
  345. (float-time time)))
  346. ;; `user-error' is only available from 24.2.50 on
  347. (unless (fboundp 'user-error)
  348. (defalias 'user-error 'error))
  349. (defmacro org-no-popups (&rest body)
  350. "Suppress popup windows.
  351. Let-bind some variables to nil around BODY to achieve the desired
  352. effect, which variables to use depends on the Emacs version."
  353. (if (org-version-check "24.2.50" "" :predicate)
  354. `(let (pop-up-frames display-buffer-alist)
  355. ,@body)
  356. `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
  357. ,@body)))
  358. (if (fboundp 'string-match-p)
  359. (defalias 'org-string-match-p 'string-match-p)
  360. (defun org-string-match-p (regexp string &optional start)
  361. (save-match-data
  362. (funcall 'string-match regexp string start))))
  363. (if (fboundp 'looking-at-p)
  364. (defalias 'org-looking-at-p 'looking-at-p)
  365. (defun org-looking-at-p (&rest args)
  366. (save-match-data
  367. (apply 'looking-at args))))
  368. ;; XEmacs does not have `looking-back'.
  369. (if (fboundp 'looking-back)
  370. (defalias 'org-looking-back 'looking-back)
  371. (defun org-looking-back (regexp &optional limit greedy)
  372. "Return non-nil if text before point matches regular expression REGEXP.
  373. Like `looking-at' except matches before point, and is slower.
  374. LIMIT if non-nil speeds up the search by specifying a minimum
  375. starting position, to avoid checking matches that would start
  376. before LIMIT.
  377. If GREEDY is non-nil, extend the match backwards as far as
  378. possible, stopping when a single additional previous character
  379. cannot be part of a match for REGEXP. When the match is
  380. extended, its starting position is allowed to occur before
  381. LIMIT."
  382. (let ((start (point))
  383. (pos
  384. (save-excursion
  385. (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
  386. (point)))))
  387. (if (and greedy pos)
  388. (save-restriction
  389. (narrow-to-region (point-min) start)
  390. (while (and (> pos (point-min))
  391. (save-excursion
  392. (goto-char pos)
  393. (backward-char 1)
  394. (looking-at (concat "\\(?:" regexp "\\)\\'"))))
  395. (setq pos (1- pos)))
  396. (save-excursion
  397. (goto-char pos)
  398. (looking-at (concat "\\(?:" regexp "\\)\\'")))))
  399. (not (null pos)))))
  400. (defun org-floor* (x &optional y)
  401. "Return a list of the floor of X and the fractional part of X.
  402. With two arguments, return floor and remainder of their quotient."
  403. (let ((q (floor x y)))
  404. (list q (- x (if y (* y q) q)))))
  405. ;; `pop-to-buffer-same-window' has been introduced in Emacs 24.1.
  406. (defun org-pop-to-buffer-same-window
  407. (&optional buffer-or-name norecord label)
  408. "Pop to buffer specified by BUFFER-OR-NAME in the selected window."
  409. (if (fboundp 'pop-to-buffer-same-window)
  410. (funcall
  411. 'pop-to-buffer-same-window buffer-or-name norecord)
  412. (funcall 'switch-to-buffer buffer-or-name norecord)))
  413. ;; `condition-case-unless-debug' has been introduced in Emacs 24.1
  414. ;; `condition-case-no-debug' has been introduced in Emacs 23.1
  415. (defmacro org-condition-case-unless-debug (var bodyform &rest handlers)
  416. (declare (debug condition-case) (indent 2))
  417. (or (and (fboundp 'condition-case-unless-debug)
  418. `(condition-case-unless-debug ,var ,bodyform ,@handlers))
  419. (and (fboundp 'condition-case-no-debug)
  420. `(condition-case-no-debug ,var ,bodyform ,@handlers))
  421. `(condition-case ,var ,bodyform ,@handlers)))
  422. ;; RECURSIVE has been introduced with Emacs 23.2.
  423. ;; This is copying and adapted from `tramp-compat-delete-directory'
  424. (defun org-delete-directory (directory &optional recursive)
  425. "Compatibility function for `delete-directory'."
  426. (if (null recursive)
  427. (delete-directory directory)
  428. (condition-case nil
  429. (funcall 'delete-directory directory recursive)
  430. ;; This Emacs version does not support the RECURSIVE flag. We
  431. ;; use the implementation from Emacs 23.2.
  432. (wrong-number-of-arguments
  433. (setq directory (directory-file-name (expand-file-name directory)))
  434. (if (not (file-symlink-p directory))
  435. (mapc (lambda (file)
  436. (if (eq t (car (file-attributes file)))
  437. (org-delete-directory file recursive)
  438. (delete-file file)))
  439. (directory-files
  440. directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
  441. (delete-directory directory)))))
  442. ;;;###autoload
  443. (defmacro org-check-version ()
  444. "Try very hard to provide sensible version strings."
  445. (let* ((org-dir (org-find-library-dir "org"))
  446. (org-version.el (concat org-dir "org-version.el"))
  447. (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
  448. (if (require 'org-version org-version.el 'noerror)
  449. '(progn
  450. (autoload 'org-release "org-version.el")
  451. (autoload 'org-git-version "org-version.el"))
  452. (if (require 'org-fixup org-fixup.el 'noerror)
  453. '(org-fixup)
  454. ;; provide fallback definitions and complain
  455. (warn "Could not define org version correctly. Check installation!")
  456. '(progn
  457. (defun org-release () "N/A")
  458. (defun org-git-version () "N/A !!check installation!!"))))))
  459. (provide 'org-compat)
  460. ;;; org-compat.el ends here