org-compat.el 18 KB

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