org-compat.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. ;;; org-compat.el --- Compatibility code for Org-mode
  2. ;; Copyright (C) 2004-2016 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 older
  23. ;; versions of GNU Emacs.
  24. ;;; Code:
  25. (eval-when-compile
  26. (require 'cl))
  27. (require 'org-macs)
  28. ;; As of Emacs 25.1, `outline-mode' functions are under the 'outline-'
  29. ;; prefix, `find-tag' is replaced with `xref-find-definition' and
  30. ;; `x-get-selection' with `gui-get-selection'.
  31. (when (< emacs-major-version 25)
  32. (defalias 'outline-hide-entry 'hide-entry)
  33. (defalias 'outline-hide-sublevels 'hide-sublevels)
  34. (defalias 'outline-hide-subtree 'hide-subtree)
  35. (defalias 'outline-show-all 'show-all)
  36. (defalias 'outline-show-branches 'show-branches)
  37. (defalias 'outline-show-children 'show-children)
  38. (defalias 'outline-show-entry 'show-entry)
  39. (defalias 'outline-show-subtree 'show-subtree)
  40. (defalias 'xref-find-definitions 'find-tag))
  41. (eval-when-compile
  42. (when (< emacs-major-version 25)
  43. (defalias 'gui-get-selection 'x-get-selection)))
  44. (defun org-compatible-face (inherits specs)
  45. "Make a compatible face specification.
  46. If INHERITS is an existing face and if the Emacs version supports it,
  47. just inherit the face. If INHERITS is set and the Emacs version does
  48. not support it, copy the face specification from the inheritance face.
  49. If INHERITS is not given and SPECS is, use SPECS to define the face."
  50. (when (and inherits (facep inherits) (not specs))
  51. (setq specs (or specs
  52. (get inherits 'saved-face)
  53. (get inherits 'face-defface-spec))))
  54. (cond
  55. ((and inherits (facep inherits)
  56. (>= emacs-major-version 22)
  57. ;; do not inherit outline faces before Emacs 23
  58. (or (>= emacs-major-version 23)
  59. (not (string-match "\\`outline-[0-9]+"
  60. (symbol-name inherits)))))
  61. (list (list t :inherit inherits)))
  62. ((< emacs-major-version 22)
  63. ;; These do not understand the `min-colors' attribute.
  64. (let (r e a)
  65. (while (setq e (pop specs))
  66. (cond
  67. ((memq (car e) '(t default)) (push e r))
  68. ((setq a (member '(min-colors 8) (car e)))
  69. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  70. (cdr e)))))
  71. ((setq a (assq 'min-colors (car e)))
  72. (setq e (cons (delq a (car e)) (cdr e)))
  73. (or (assoc (car e) r) (push e r)))
  74. (t (or (assoc (car e) r) (push e r)))))
  75. (nreverse r)))
  76. (t specs)))
  77. (put 'org-compatible-face 'lisp-indent-function 1)
  78. (defun org-version-check (version feature level)
  79. (let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
  80. (v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
  81. (rmaj (or (nth 0 v1) 99))
  82. (rmin (or (nth 1 v1) 99))
  83. (rbld (or (nth 2 v1) 99))
  84. (maj (or (nth 0 v2) 0))
  85. (min (or (nth 1 v2) 0))
  86. (bld (or (nth 2 v2) 0)))
  87. (if (or (< maj rmaj)
  88. (and (= maj rmaj)
  89. (< min rmin))
  90. (and (= maj rmaj)
  91. (= min rmin)
  92. (< bld rbld)))
  93. (if (eq level :predicate)
  94. ;; just return if we have the version
  95. nil
  96. (let ((msg (format "Emacs %s or greater is recommended for %s"
  97. version feature)))
  98. (display-warning 'org msg level)
  99. t))
  100. t)))
  101. ;;; Emacs/XEmacs compatibility
  102. (eval-and-compile
  103. (defun org-defvaralias (new-alias base-variable &optional docstring)
  104. "Compatibility function for defvaralias.
  105. Don't do the aliasing when `defvaralias' is not bound."
  106. (declare (indent 1))
  107. (when (fboundp 'defvaralias)
  108. (defvaralias new-alias base-variable docstring)))
  109. (when (and (not (boundp 'user-emacs-directory))
  110. (boundp 'user-init-directory))
  111. (org-defvaralias 'user-emacs-directory 'user-init-directory)))
  112. (define-obsolete-function-alias 'org-add-hook 'add-hook "Org 9.0")
  113. (define-obsolete-function-alias 'org-decompose-region 'decompose-region "Org 9.0")
  114. (define-obsolete-function-alias 'org-detach-overlay 'delete-overlay "Org 9.0")
  115. (define-obsolete-function-alias 'org-file-equal-p 'file-equal-p "Org 9.0")
  116. (define-obsolete-function-alias 'org-float-time 'float-time "Org 9.0")
  117. (define-obsolete-function-alias 'org-indent-line-to 'indent-line-to "Org 9.0")
  118. (define-obsolete-function-alias 'org-indent-to-column 'indent-to-column "Org 9.0")
  119. (define-obsolete-function-alias 'org-looking-at-p 'looking-at-p "Org 9.0")
  120. (define-obsolete-function-alias 'org-looking-back 'looking-back "Org 9.0")
  121. (define-obsolete-function-alias 'org-match-string-no-properties 'match-string-properties "Org 9.0")
  122. (define-obsolete-function-alias 'org-propertize 'propertize "Org 9.0")
  123. (define-obsolete-function-alias 'org-select-frame-set-input-focus 'select-frame-set-input-focus "Org 9.0")
  124. (defmacro org-re (s)
  125. "Replace posix classes in regular expression."
  126. (declare (debug (form)))
  127. s)
  128. (make-obsolete 'org-re "It is now a no-op. Please remove it altogether." "Org 9.0")
  129. ;;; Miscellaneous functions
  130. (defun org-get-x-clipboard (value)
  131. "Get the value of the X or Windows clipboard."
  132. (cond ((eq window-system 'x)
  133. (org-no-properties
  134. (ignore-errors
  135. (or (gui-get-selection value 'UTF8_STRING)
  136. (gui-get-selection value 'COMPOUND_TEXT)
  137. (gui-get-selection value 'STRING)
  138. (gui-get-selection value 'TEXT)))))
  139. ((and (eq window-system 'w32) (fboundp 'w32-get-clipboard-data))
  140. (w32-get-clipboard-data))))
  141. (defun org-add-props (string plist &rest props)
  142. "Add text properties to entire string, from beginning to end.
  143. PLIST may be a list of properties, PROPS are individual properties and values
  144. that will be added to PLIST. Returns the string that was modified."
  145. (add-text-properties
  146. 0 (length string) (if props (append plist props) plist) string)
  147. string)
  148. (put 'org-add-props 'lisp-indent-function 2)
  149. (defun org-fit-window-to-buffer (&optional window max-height min-height
  150. shrink-only)
  151. "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
  152. WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
  153. passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
  154. `shrink-window-if-larger-than-buffer' instead, the height limit is
  155. ignored in this case."
  156. (cond ((if (fboundp 'window-full-width-p)
  157. (not (window-full-width-p window))
  158. ;; do nothing if another window would suffer
  159. (> (frame-width) (window-width window))))
  160. ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
  161. (fit-window-to-buffer window max-height min-height))
  162. ((fboundp 'shrink-window-if-larger-than-buffer)
  163. (shrink-window-if-larger-than-buffer window)))
  164. (or window (selected-window)))
  165. (defun org-number-sequence (from &optional to inc)
  166. "Call `number-sequence' or emulate it."
  167. (if (fboundp 'number-sequence)
  168. (number-sequence from to inc)
  169. (if (or (not to) (= from to))
  170. (list from)
  171. (or inc (setq inc 1))
  172. (when (zerop inc) (error "The increment can not be zero"))
  173. (let (seq (n 0) (next from))
  174. (if (> inc 0)
  175. (while (<= next to)
  176. (setq seq (cons next seq)
  177. n (1+ n)
  178. next (+ from (* n inc))))
  179. (while (>= next to)
  180. (setq seq (cons next seq)
  181. n (1+ n)
  182. next (+ from (* n inc)))))
  183. (nreverse seq)))))
  184. ;; `set-transient-map' is only in Emacs >= 24.4
  185. (defalias 'org-set-transient-map
  186. (if (fboundp 'set-transient-map)
  187. 'set-transient-map
  188. 'set-temporary-overlay-map))
  189. ;;; Region compatibility
  190. (defvar org-ignore-region nil
  191. "Non-nil means temporarily disable the active region.")
  192. (defun org-region-active-p ()
  193. "Is `transient-mark-mode' on and the region active?"
  194. (if org-ignore-region
  195. nil
  196. (if (fboundp 'use-region-p)
  197. (use-region-p)
  198. (and transient-mark-mode mark-active)))) ; Emacs 22 and before
  199. (defun org-cursor-to-region-beginning ()
  200. (when (and (org-region-active-p)
  201. (> (point) (region-beginning)))
  202. (exchange-point-and-mark)))
  203. ;;; Old alias for emacs 22 compatibility, now dropped
  204. (define-obsolete-function-alias 'org-activate-mark 'activate-mark)
  205. ;;; Invisibility compatibility
  206. (defun org-remove-from-invisibility-spec (arg)
  207. "Remove elements from `buffer-invisibility-spec'."
  208. (if (fboundp 'remove-from-invisibility-spec)
  209. (remove-from-invisibility-spec arg)
  210. (if (consp buffer-invisibility-spec)
  211. (setq buffer-invisibility-spec
  212. (delete arg buffer-invisibility-spec)))))
  213. (defun org-in-invisibility-spec-p (arg)
  214. "Is ARG a member of `buffer-invisibility-spec'?"
  215. (if (consp buffer-invisibility-spec)
  216. (member arg buffer-invisibility-spec)))
  217. (defun org-move-to-column (column &optional force buffer)
  218. "Move to column COLUMN.
  219. Pass COLUMN and FORCE to `move-to-column'."
  220. (let ((buffer-invisibility-spec
  221. (if (listp buffer-invisibility-spec)
  222. (remove '(org-filtered) buffer-invisibility-spec)
  223. buffer-invisibility-spec)))
  224. (move-to-column column force)))
  225. (defmacro org-find-library-dir (library)
  226. `(file-name-directory (or (locate-library ,library) "")))
  227. (defun org-count-lines (s)
  228. "How many lines in string S?"
  229. (let ((start 0) (n 1))
  230. (while (string-match "\n" s start)
  231. (setq start (match-end 0) n (1+ n)))
  232. (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
  233. (setq n (1- n)))
  234. n))
  235. (defun org-kill-new (string &rest args)
  236. (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
  237. string)
  238. (apply 'kill-new string args))
  239. ;; `user-error' is only available from 24.2.50 on
  240. (unless (fboundp 'user-error)
  241. (defalias 'user-error 'error))
  242. ;; ‘format-message’ is available only from 25 on
  243. (unless (fboundp 'format-message)
  244. (defalias 'format-message 'format))
  245. ;; `font-lock-ensure' is only available from 24.4.50 on
  246. (defalias 'org-font-lock-ensure
  247. (if (fboundp 'font-lock-ensure)
  248. #'font-lock-ensure
  249. (lambda (&optional _beg _end)
  250. (with-no-warnings (font-lock-fontify-buffer)))))
  251. (defmacro org-no-popups (&rest body)
  252. "Suppress popup windows.
  253. Let-bind some variables to nil around BODY to achieve the desired
  254. effect, which variables to use depends on the Emacs version."
  255. (if (org-version-check "24.2.50" "" :predicate)
  256. `(let (pop-up-frames display-buffer-alist)
  257. ,@body)
  258. `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
  259. ,@body)))
  260. (if (fboundp 'string-match-p)
  261. (defalias 'org-string-match-p 'string-match-p)
  262. (defun org-string-match-p (regexp string &optional start)
  263. (save-match-data
  264. (funcall 'string-match regexp string start))))
  265. (defun org-floor* (x &optional y)
  266. "Return a list of the floor of X and the fractional part of X.
  267. With two arguments, return floor and remainder of their quotient."
  268. (let ((q (floor x y)))
  269. (list q (- x (if y (* y q) q)))))
  270. ;; `pop-to-buffer-same-window' has been introduced in Emacs 24.1.
  271. (defun org-pop-to-buffer-same-window
  272. (&optional buffer-or-name norecord label)
  273. "Pop to buffer specified by BUFFER-OR-NAME in the selected window."
  274. (if (fboundp 'pop-to-buffer-same-window)
  275. (funcall
  276. 'pop-to-buffer-same-window buffer-or-name norecord)
  277. (funcall 'switch-to-buffer buffer-or-name norecord)))
  278. ;; RECURSIVE has been introduced with Emacs 23.2.
  279. ;; This is copying and adapted from `tramp-compat-delete-directory'
  280. (defun org-delete-directory (directory &optional recursive)
  281. "Compatibility function for `delete-directory'."
  282. (if (null recursive)
  283. (delete-directory directory)
  284. (condition-case nil
  285. (funcall 'delete-directory directory recursive)
  286. ;; This Emacs version does not support the RECURSIVE flag. We
  287. ;; use the implementation from Emacs 23.2.
  288. (wrong-number-of-arguments
  289. (setq directory (directory-file-name (expand-file-name directory)))
  290. (if (not (file-symlink-p directory))
  291. (mapc (lambda (file)
  292. (if (eq t (car (file-attributes file)))
  293. (org-delete-directory file recursive)
  294. (delete-file file)))
  295. (directory-files
  296. directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
  297. (delete-directory directory)))))
  298. ;;;###autoload
  299. (defmacro org-check-version ()
  300. "Try very hard to provide sensible version strings."
  301. (let* ((org-dir (org-find-library-dir "org"))
  302. (org-version.el (concat org-dir "org-version.el"))
  303. (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
  304. (if (require 'org-version org-version.el 'noerror)
  305. '(progn
  306. (autoload 'org-release "org-version.el")
  307. (autoload 'org-git-version "org-version.el"))
  308. (if (require 'org-fixup org-fixup.el 'noerror)
  309. '(org-fixup)
  310. ;; provide fallback definitions and complain
  311. (warn "Could not define org version correctly. Check installation!")
  312. '(progn
  313. (defun org-release () "N/A")
  314. (defun org-git-version () "N/A !!check installation!!"))))))
  315. ;; `buffer-narrowed-p' is available for Emacs >=24.3
  316. (defun org-buffer-narrowed-p ()
  317. "Compatibility function for `buffer-narrowed-p'."
  318. (if (fboundp 'buffer-narrowed-p)
  319. (buffer-narrowed-p)
  320. (/= (- (point-max) (point-min)) (buffer-size))))
  321. (defmacro org-with-silent-modifications (&rest body)
  322. (if (fboundp 'with-silent-modifications)
  323. `(with-silent-modifications ,@body)
  324. `(org-unmodified ,@body)))
  325. (def-edebug-spec org-with-silent-modifications (body))
  326. ;; Remove this when support for Emacs < 24.4 is dropped.
  327. (defun org-define-error (name message)
  328. "Define NAME as a new error signal.
  329. MESSAGE is a string that will be output to the echo area if such
  330. an error is signaled without being caught by a `condition-case'.
  331. Implements `define-error' for older emacsen."
  332. (if (fboundp 'define-error) (define-error name message)
  333. (put name 'error-conditions
  334. (copy-sequence (cons name (get 'error 'error-conditions))))))
  335. ;;; Functions from cl-lib that Org used to have its own implementation of
  336. (define-obsolete-function-alias 'org-count 'cl-count "Org 9.0")
  337. (define-obsolete-function-alias 'org-remove-if 'cl-remove-if "Org 9.0")
  338. (define-obsolete-function-alias 'org-remove-if-not 'cl-remove-if-not "Org 9.0")
  339. (define-obsolete-function-alias 'org-reduce 'cl-reduce "Org 9.0")
  340. (define-obsolete-function-alias 'org-every 'cl-every "Org 9.0")
  341. (define-obsolete-function-alias 'org-some 'cl-some "Org 9.0")
  342. (provide 'org-compat)
  343. ;;; org-compat.el ends here