org-compat.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. ;;; org-compat.el --- Compatibility Code for Older Emacsen -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2004-2018 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 <https://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. (require 'cl-lib)
  26. (require 'org-macs)
  27. (declare-function org-at-table.el-p "org" ())
  28. (declare-function org-element-at-point "org-element" ())
  29. (declare-function org-element-type "org-element" (element))
  30. (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
  31. (declare-function org-link-set-parameters "org" (type &rest rest))
  32. (declare-function org-table-end "org-table" (&optional table-type))
  33. (declare-function outline-next-heading "outline" ())
  34. (declare-function table--at-cell-p "table" (position &optional object at-column))
  35. (defvar org-table-any-border-regexp)
  36. (defvar org-table-dataline-regexp)
  37. (defvar org-table-tab-recognizes-table.el)
  38. (defvar org-table1-hline-regexp)
  39. ;;; Emacs < 25.1 compatibility
  40. (when (< emacs-major-version 25)
  41. (defalias 'outline-hide-entry 'hide-entry)
  42. (defalias 'outline-hide-sublevels 'hide-sublevels)
  43. (defalias 'outline-hide-subtree 'hide-subtree)
  44. (defalias 'outline-show-all 'show-all)
  45. (defalias 'outline-show-branches 'show-branches)
  46. (defalias 'outline-show-children 'show-children)
  47. (defalias 'outline-show-entry 'show-entry)
  48. (defalias 'outline-show-subtree 'show-subtree)
  49. (defalias 'xref-find-definitions 'find-tag)
  50. (defalias 'format-message 'format)
  51. (defalias 'gui-get-selection 'x-get-selection))
  52. (defun org-decode-time (&optional time zone)
  53. "Backward-compatible function for `decode-time'."
  54. (if (< emacs-major-version 25)
  55. (decode-time time)
  56. (decode-time time zone)))
  57. (unless (fboundp 'directory-name-p)
  58. (defun directory-name-p (name)
  59. "Return non-nil if NAME ends with a directory separator character."
  60. (let ((len (length name))
  61. (lastc ?.))
  62. (if (> len 0)
  63. (setq lastc (aref name (1- len))))
  64. (or (= lastc ?/)
  65. (and (memq system-type '(windows-nt ms-dos))
  66. (= lastc ?\\))))))
  67. (unless (fboundp 'directory-files-recursively)
  68. (defun directory-files-recursively (dir regexp &optional include-directories)
  69. "Return list of all files under DIR that have file names matching REGEXP.
  70. This function works recursively. Files are returned in \"depth first\"
  71. order, and files from each directory are sorted in alphabetical order.
  72. Each file name appears in the returned list in its absolute form.
  73. Optional argument INCLUDE-DIRECTORIES non-nil means also include in the
  74. output directories whose names match REGEXP."
  75. (let ((result nil)
  76. (files nil)
  77. ;; When DIR is "/", remote file names like "/method:" could
  78. ;; also be offered. We shall suppress them.
  79. (tramp-mode (and tramp-mode (file-remote-p (expand-file-name dir)))))
  80. (dolist (file (sort (file-name-all-completions "" dir)
  81. 'string<))
  82. (unless (member file '("./" "../"))
  83. (if (directory-name-p file)
  84. (let* ((leaf (substring file 0 (1- (length file))))
  85. (full-file (expand-file-name leaf dir)))
  86. ;; Don't follow symlinks to other directories.
  87. (unless (file-symlink-p full-file)
  88. (setq result
  89. (nconc result (directory-files-recursively
  90. full-file regexp include-directories))))
  91. (when (and include-directories
  92. (string-match regexp leaf))
  93. (setq result (nconc result (list full-file)))))
  94. (when (string-match regexp file)
  95. (push (expand-file-name file dir) files)))))
  96. (nconc result (nreverse files)))))
  97. ;;; Obsolete aliases (remove them after the next major release).
  98. ;;;; XEmacs compatibility, now removed.
  99. (define-obsolete-function-alias 'org-activate-mark 'activate-mark)
  100. (define-obsolete-function-alias 'org-add-hook 'add-hook "Org 9.0")
  101. (define-obsolete-function-alias 'org-bound-and-true-p 'bound-and-true-p "Org 9.0")
  102. (define-obsolete-function-alias 'org-decompose-region 'decompose-region "Org 9.0")
  103. (define-obsolete-function-alias 'org-defvaralias 'defvaralias "Org 9.0")
  104. (define-obsolete-function-alias 'org-detach-overlay 'delete-overlay "Org 9.0")
  105. (define-obsolete-function-alias 'org-file-equal-p 'file-equal-p "Org 9.0")
  106. (define-obsolete-function-alias 'org-float-time 'float-time "Org 9.0")
  107. (define-obsolete-function-alias 'org-indent-line-to 'indent-line-to "Org 9.0")
  108. (define-obsolete-function-alias 'org-indent-to-column 'indent-to-column "Org 9.0")
  109. (define-obsolete-function-alias 'org-looking-at-p 'looking-at-p "Org 9.0")
  110. (define-obsolete-function-alias 'org-looking-back 'looking-back "Org 9.0")
  111. (define-obsolete-function-alias 'org-match-string-no-properties 'match-string-no-properties "Org 9.0")
  112. (define-obsolete-function-alias 'org-propertize 'propertize "Org 9.0")
  113. (define-obsolete-function-alias 'org-select-frame-set-input-focus 'select-frame-set-input-focus "Org 9.0")
  114. (defmacro org-re (s)
  115. "Replace posix classes in regular expression S."
  116. (declare (debug (form))
  117. (obsolete "you can safely remove it." "Org 9.0"))
  118. s)
  119. ;;;; Functions from cl-lib that Org used to have its own implementation of.
  120. (define-obsolete-function-alias 'org-count 'cl-count "Org 9.0")
  121. (define-obsolete-function-alias 'org-every 'cl-every "Org 9.0")
  122. (define-obsolete-function-alias 'org-find-if 'cl-find-if "Org 9.0")
  123. (define-obsolete-function-alias 'org-reduce 'cl-reduce "Org 9.0")
  124. (define-obsolete-function-alias 'org-remove-if 'cl-remove-if "Org 9.0")
  125. (define-obsolete-function-alias 'org-remove-if-not 'cl-remove-if-not "Org 9.0")
  126. (define-obsolete-function-alias 'org-some 'cl-some "Org 9.0")
  127. (define-obsolete-function-alias 'org-floor* 'cl-floor "Org 9.0")
  128. (defun org-sublist (list start end)
  129. "Return a section of LIST, from START to END.
  130. Counting starts at 1."
  131. (cl-subseq list (1- start) end))
  132. (make-obsolete 'org-sublist
  133. "use cl-subseq (note the 0-based counting)."
  134. "Org 9.0")
  135. ;;;; Functions available since Emacs 24.3
  136. (define-obsolete-function-alias 'org-buffer-narrowed-p 'buffer-narrowed-p "Org 9.0")
  137. (define-obsolete-function-alias 'org-called-interactively-p 'called-interactively-p "Org 9.0")
  138. (define-obsolete-function-alias 'org-char-to-string 'char-to-string "Org 9.0")
  139. (define-obsolete-function-alias 'org-delete-directory 'delete-directory "Org 9.0")
  140. (define-obsolete-function-alias 'org-format-seconds 'format-seconds "Org 9.0")
  141. (define-obsolete-function-alias 'org-link-escape-browser 'url-encode-url "Org 9.0")
  142. (define-obsolete-function-alias 'org-no-warnings 'with-no-warnings "Org 9.0")
  143. (define-obsolete-function-alias 'org-number-sequence 'number-sequence "Org 9.0")
  144. (define-obsolete-function-alias 'org-pop-to-buffer-same-window 'pop-to-buffer-same-window "Org 9.0")
  145. (define-obsolete-function-alias 'org-string-match-p 'string-match-p "Org 9.0")
  146. ;;;; Functions and variables from previous releases now obsolete.
  147. (define-obsolete-function-alias 'org-element-remove-indentation
  148. 'org-remove-indentation "Org 9.0")
  149. (define-obsolete-variable-alias 'org-latex-create-formula-image-program
  150. 'org-preview-latex-default-process "Org 9.0")
  151. (define-obsolete-variable-alias 'org-latex-preview-ltxpng-directory
  152. 'org-preview-latex-image-directory "Org 9.0")
  153. (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
  154. (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 9.0")
  155. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp "Org 8.3")
  156. (define-obsolete-function-alias 'org-image-file-name-regexp
  157. 'image-file-name-regexp "Org 9.0")
  158. (define-obsolete-function-alias 'org-completing-read-no-i
  159. 'completing-read "Org 9.0")
  160. (define-obsolete-function-alias 'org-icompleting-read
  161. 'completing-read "Org 9.0")
  162. (define-obsolete-function-alias 'org-iread-file-name 'read-file-name "Org 9.0")
  163. (define-obsolete-function-alias 'org-days-to-time
  164. 'org-time-stamp-to-now "Org 8.2")
  165. (define-obsolete-variable-alias 'org-agenda-ignore-drawer-properties
  166. 'org-agenda-ignore-properties "Org 9.0")
  167. (define-obsolete-function-alias 'org-preview-latex-fragment
  168. 'org-toggle-latex-fragment "Org 8.3")
  169. (define-obsolete-function-alias 'org-export-get-genealogy
  170. 'org-element-lineage "Org 9.0")
  171. (define-obsolete-variable-alias 'org-latex-with-hyperref
  172. 'org-latex-hyperref-template "Org 9.0")
  173. (define-obsolete-variable-alias 'hfy-optimisations 'hfy-optimizations "Org 9.0")
  174. (define-obsolete-variable-alias 'org-export-htmlized-org-css-url
  175. 'org-org-htmlized-css-url "Org 8.2")
  176. (define-obsolete-function-alias 'org-list-parse-list 'org-list-to-lisp "Org 9.0")
  177. (define-obsolete-function-alias 'org-agenda-todayp
  178. 'org-agenda-today-p "Org 9.0")
  179. (define-obsolete-function-alias 'org-babel-examplize-region
  180. 'org-babel-examplify-region "Org 9.0")
  181. (define-obsolete-variable-alias 'org-babel-capitalize-example-region-markers
  182. 'org-babel-uppercase-example-markers "Org 9.1")
  183. (define-obsolete-function-alias 'org-babel-trim 'org-trim "Org 9.0")
  184. (define-obsolete-variable-alias 'org-html-style 'org-html-head "24.4")
  185. (define-obsolete-function-alias 'org-insert-columns-dblock
  186. 'org-columns-insert-dblock "Org 9.0")
  187. (define-obsolete-variable-alias 'org-export-babel-evaluate
  188. 'org-export-use-babel "Org 9.1")
  189. (define-obsolete-function-alias 'org-activate-bracket-links
  190. 'org-activate-links "Org 9.0")
  191. (define-obsolete-function-alias 'org-activate-plain-links 'ignore "Org 9.0")
  192. (define-obsolete-function-alias 'org-activate-angle-links 'ignore "Org 9.0")
  193. (defun org-in-fixed-width-region-p ()
  194. "Non-nil if point in a fixed-width region."
  195. (save-match-data
  196. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  197. (make-obsolete 'org-in-fixed-width-region-p
  198. "use `org-element' library"
  199. "Org 9.0")
  200. (defun org-compatible-face (inherits specs)
  201. "Make a compatible face specification.
  202. If INHERITS is an existing face and if the Emacs version supports
  203. it, just inherit the face. If INHERITS is not given and SPECS
  204. is, use SPECS to define the face."
  205. (declare (indent 1))
  206. (if (facep inherits)
  207. (list (list t :inherit inherits))
  208. specs))
  209. (make-obsolete 'org-compatible-face "you can remove it." "Org 9.0")
  210. (defun org-add-link-type (type &optional follow export)
  211. "Add a new TYPE link.
  212. FOLLOW and EXPORT are two functions.
  213. FOLLOW should take the link path as the single argument and do whatever
  214. is necessary to follow the link, for example find a file or display
  215. a mail message.
  216. EXPORT should format the link path for export to one of the export formats.
  217. It should be a function accepting three arguments:
  218. path the path of the link, the text after the prefix (like \"http:\")
  219. desc the description of the link, if any
  220. format the export format, a symbol like `html' or `latex' or `ascii'.
  221. The function may use the FORMAT information to return different values
  222. depending on the format. The return value will be put literally into
  223. the exported file. If the return value is nil, this means Org should
  224. do what it normally does with links which do not have EXPORT defined.
  225. Org mode has a built-in default for exporting links. If you are happy with
  226. this default, there is no need to define an export function for the link
  227. type. For a simple example of an export function, see `org-bbdb.el'.
  228. If TYPE already exists, update it with the arguments.
  229. See `org-link-parameters' for documentation on the other parameters."
  230. (org-link-set-parameters type :follow follow :export export)
  231. (message "Created %s link." type))
  232. (make-obsolete 'org-add-link-type "use `org-link-set-parameters' instead." "Org 9.0")
  233. (defun org-table-recognize-table.el ()
  234. "If there is a table.el table nearby, recognize it and move into it."
  235. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  236. (beginning-of-line)
  237. (unless (or (looking-at org-table-dataline-regexp)
  238. (not (looking-at org-table1-hline-regexp)))
  239. (forward-line)
  240. (when (looking-at org-table-any-border-regexp)
  241. (forward-line -2)))
  242. (if (re-search-forward "|" (org-table-end t) t)
  243. (progn
  244. (require 'table)
  245. (if (table--at-cell-p (point)) t
  246. (message "recognizing table.el table...")
  247. (table-recognize-table)
  248. (message "recognizing table.el table...done")))
  249. (error "This should not happen"))))
  250. ;; Not used by Org core since commit 6d1e3082, Feb 2010.
  251. (make-obsolete 'org-table-recognize-table.el
  252. "please notify the org mailing list if you use this function."
  253. "Org 9.0")
  254. (defun org-remove-angle-brackets (s)
  255. (org-unbracket-string "<" ">" s))
  256. (make-obsolete 'org-remove-angle-brackets 'org-unbracket-string "Org 9.0")
  257. (defun org-remove-double-quotes (s)
  258. (org-unbracket-string "\"" "\"" s))
  259. (make-obsolete 'org-remove-double-quotes 'org-unbracket-string "Org 9.0")
  260. (defcustom org-publish-sitemap-file-entry-format "%t"
  261. "Format string for site-map file entry.
  262. You could use brackets to delimit on what part the link will be.
  263. %t is the title.
  264. %a is the author.
  265. %d is the date formatted using `org-publish-sitemap-date-format'."
  266. :group 'org-export-publish
  267. :type 'string)
  268. (make-obsolete-variable
  269. 'org-publish-sitemap-file-entry-format
  270. "set `:sitemap-format-entry' in `org-publish-project-alist' instead."
  271. "Org 9.1")
  272. (defvar org-agenda-skip-regexp)
  273. (defun org-agenda-skip-entry-when-regexp-matches ()
  274. "Check if the current entry contains match for `org-agenda-skip-regexp'.
  275. If yes, it returns the end position of this entry, causing agenda commands
  276. to skip the entry but continuing the search in the subtree. This is a
  277. function that can be put into `org-agenda-skip-function' for the duration
  278. of a command."
  279. (declare (obsolete "use `org-agenda-skip-if' instead." "Org 9.1"))
  280. (let ((end (save-excursion (org-end-of-subtree t)))
  281. skip)
  282. (save-excursion
  283. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  284. (and skip end)))
  285. (defun org-agenda-skip-subtree-when-regexp-matches ()
  286. "Check if the current subtree contains match for `org-agenda-skip-regexp'.
  287. If yes, it returns the end position of this tree, causing agenda commands
  288. to skip this subtree. This is a function that can be put into
  289. `org-agenda-skip-function' for the duration of a command."
  290. (declare (obsolete "use `org-agenda-skip-if' instead." "Org 9.1"))
  291. (let ((end (save-excursion (org-end-of-subtree t)))
  292. skip)
  293. (save-excursion
  294. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  295. (and skip end)))
  296. (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
  297. "Check if the current subtree contains match for `org-agenda-skip-regexp'.
  298. If yes, it returns the end position of the current entry (NOT the tree),
  299. causing agenda commands to skip the entry but continuing the search in
  300. the subtree. This is a function that can be put into
  301. `org-agenda-skip-function' for the duration of a command. An important
  302. use of this function is for the stuck project list."
  303. (declare (obsolete "use `org-agenda-skip-if' instead." "Org 9.1"))
  304. (let ((end (save-excursion (org-end-of-subtree t)))
  305. (entry-end (save-excursion (outline-next-heading) (1- (point))))
  306. skip)
  307. (save-excursion
  308. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  309. (and skip entry-end)))
  310. (define-obsolete-function-alias 'org-minutes-to-clocksum-string
  311. 'org-duration-from-minutes "Org 9.1")
  312. (define-obsolete-function-alias 'org-hh:mm-string-to-minutes
  313. 'org-duration-to-minutes "Org 9.1")
  314. (define-obsolete-function-alias 'org-duration-string-to-minutes
  315. 'org-duration-to-minutes "Org 9.1")
  316. (make-obsolete-variable 'org-time-clocksum-format
  317. "set `org-duration-format' instead." "Org 9.1")
  318. (make-obsolete-variable 'org-time-clocksum-use-fractional
  319. "set `org-duration-format' instead." "Org 9.1")
  320. (make-obsolete-variable 'org-time-clocksum-fractional-format
  321. "set `org-duration-format' instead." "Org 9.1")
  322. (make-obsolete-variable 'org-time-clocksum-use-effort-durations
  323. "set `org-duration-units' instead." "Org 9.1")
  324. (define-obsolete-function-alias 'org-babel-number-p
  325. 'org-babel--string-to-number "Org 9.0")
  326. (define-obsolete-variable-alias 'org-usenet-links-prefer-google
  327. 'org-gnus-prefer-web-links "Org 9.1")
  328. (define-obsolete-variable-alias 'org-texinfo-def-table-markup
  329. 'org-texinfo-table-default-markup "Org 9.1")
  330. ;;; The function was made obsolete by commit 65399674d5 of 2013-02-22.
  331. ;;; This make-obsolete call was added 2016-09-01.
  332. (make-obsolete 'org-capture-import-remember-templates
  333. "use the `org-capture-templates' variable instead."
  334. "Org 9.0")
  335. ;;;; Obsolete link types
  336. (eval-after-load 'org
  337. '(progn
  338. (org-link-set-parameters "file+emacs") ;since Org 9.0
  339. (org-link-set-parameters "file+sys"))) ;since Org 9.0
  340. ;;; Miscellaneous functions
  341. (defun org-version-check (version feature level)
  342. (let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
  343. (v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
  344. (rmaj (or (nth 0 v1) 99))
  345. (rmin (or (nth 1 v1) 99))
  346. (rbld (or (nth 2 v1) 99))
  347. (maj (or (nth 0 v2) 0))
  348. (min (or (nth 1 v2) 0))
  349. (bld (or (nth 2 v2) 0)))
  350. (if (or (< maj rmaj)
  351. (and (= maj rmaj)
  352. (< min rmin))
  353. (and (= maj rmaj)
  354. (= min rmin)
  355. (< bld rbld)))
  356. (if (eq level :predicate)
  357. ;; just return if we have the version
  358. nil
  359. (let ((msg (format "Emacs %s or greater is recommended for %s"
  360. version feature)))
  361. (display-warning 'org msg level)
  362. t))
  363. t)))
  364. (defun org-get-x-clipboard (value)
  365. "Get the value of the X or Windows clipboard."
  366. (cond ((and (eq window-system 'x)
  367. (fboundp 'gui-get-selection)) ;Silence byte-compiler.
  368. (org-no-properties
  369. (ignore-errors
  370. (or (gui-get-selection value 'UTF8_STRING)
  371. (gui-get-selection value 'COMPOUND_TEXT)
  372. (gui-get-selection value 'STRING)
  373. (gui-get-selection value 'TEXT)))))
  374. ((and (eq window-system 'w32) (fboundp 'w32-get-clipboard-data))
  375. (w32-get-clipboard-data))))
  376. (defun org-add-props (string plist &rest props)
  377. "Add text properties to entire string, from beginning to end.
  378. PLIST may be a list of properties, PROPS are individual properties and values
  379. that will be added to PLIST. Returns the string that was modified."
  380. (add-text-properties
  381. 0 (length string) (if props (append plist props) plist) string)
  382. string)
  383. (put 'org-add-props 'lisp-indent-function 2)
  384. (defun org-fit-window-to-buffer (&optional window max-height min-height
  385. shrink-only)
  386. "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
  387. WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
  388. passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
  389. `shrink-window-if-larger-than-buffer' instead, the height limit is
  390. ignored in this case."
  391. (cond ((if (fboundp 'window-full-width-p)
  392. (not (window-full-width-p window))
  393. ;; do nothing if another window would suffer
  394. (> (frame-width) (window-width window))))
  395. ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
  396. (fit-window-to-buffer window max-height min-height))
  397. ((fboundp 'shrink-window-if-larger-than-buffer)
  398. (shrink-window-if-larger-than-buffer window)))
  399. (or window (selected-window)))
  400. ;; `set-transient-map' is only in Emacs >= 24.4
  401. (defalias 'org-set-transient-map
  402. (if (fboundp 'set-transient-map)
  403. 'set-transient-map
  404. 'set-temporary-overlay-map))
  405. ;;; Region compatibility
  406. (defvar org-ignore-region nil
  407. "Non-nil means temporarily disable the active region.")
  408. (defun org-region-active-p ()
  409. "Non-nil when the region active.
  410. Unlike to `use-region-p', this function also checks
  411. `org-ignore-region'."
  412. (and (not org-ignore-region) (use-region-p)))
  413. (defun org-cursor-to-region-beginning ()
  414. (when (and (org-region-active-p)
  415. (> (point) (region-beginning)))
  416. (exchange-point-and-mark)))
  417. ;;; Invisibility compatibility
  418. (defun org-remove-from-invisibility-spec (arg)
  419. "Remove elements from `buffer-invisibility-spec'."
  420. (if (fboundp 'remove-from-invisibility-spec)
  421. (remove-from-invisibility-spec arg)
  422. (if (consp buffer-invisibility-spec)
  423. (setq buffer-invisibility-spec
  424. (delete arg buffer-invisibility-spec)))))
  425. (defun org-in-invisibility-spec-p (arg)
  426. "Is ARG a member of `buffer-invisibility-spec'?"
  427. (if (consp buffer-invisibility-spec)
  428. (member arg buffer-invisibility-spec)))
  429. (defun org-move-to-column (column &optional force _buffer)
  430. "Move to column COLUMN.
  431. Pass COLUMN and FORCE to `move-to-column'."
  432. (let ((buffer-invisibility-spec
  433. (if (listp buffer-invisibility-spec)
  434. (remove '(org-filtered) buffer-invisibility-spec)
  435. buffer-invisibility-spec)))
  436. (move-to-column column force)))
  437. (defmacro org-find-library-dir (library)
  438. `(file-name-directory (or (locate-library ,library) "")))
  439. (defun org-count-lines (s)
  440. "How many lines in string S?"
  441. (let ((start 0) (n 1))
  442. (while (string-match "\n" s start)
  443. (setq start (match-end 0) n (1+ n)))
  444. (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
  445. (setq n (1- n)))
  446. n))
  447. (defun org-kill-new (string &rest args)
  448. (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
  449. string)
  450. (apply 'kill-new string args))
  451. ;; `font-lock-ensure' is only available from 24.4.50 on
  452. (defalias 'org-font-lock-ensure
  453. (if (fboundp 'font-lock-ensure)
  454. #'font-lock-ensure
  455. (lambda (&optional _beg _end)
  456. (with-no-warnings (font-lock-fontify-buffer)))))
  457. ;; `file-local-name' was added in Emacs 26.1.
  458. (defalias 'org-babel-local-file-name
  459. (if (fboundp 'file-local-name)
  460. 'file-local-name
  461. (lambda (file)
  462. "Return the local name component of FILE."
  463. (or (file-remote-p file 'localname) file))))
  464. (defmacro org-no-popups (&rest body)
  465. "Suppress popup windows.
  466. Let-bind some variables to nil around BODY to achieve the desired
  467. effect, which variables to use depends on the Emacs version."
  468. (if (org-version-check "24.2.50" "" :predicate)
  469. `(let (pop-up-frames display-buffer-alist)
  470. ,@body)
  471. `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
  472. ,@body)))
  473. ;;;###autoload
  474. (defmacro org-check-version ()
  475. "Try very hard to provide sensible version strings."
  476. (let* ((org-dir (org-find-library-dir "org"))
  477. (org-version.el (concat org-dir "org-version.el"))
  478. (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
  479. (if (require 'org-version org-version.el 'noerror)
  480. '(progn
  481. (autoload 'org-release "org-version.el")
  482. (autoload 'org-git-version "org-version.el"))
  483. (if (require 'org-fixup org-fixup.el 'noerror)
  484. '(org-fixup)
  485. ;; provide fallback definitions and complain
  486. (warn "Could not define org version correctly. Check installation!")
  487. '(progn
  488. (defun org-release () "N/A")
  489. (defun org-git-version () "N/A !!check installation!!"))))))
  490. (defmacro org-with-silent-modifications (&rest body)
  491. (if (fboundp 'with-silent-modifications)
  492. `(with-silent-modifications ,@body)
  493. `(org-unmodified ,@body)))
  494. (def-edebug-spec org-with-silent-modifications (body))
  495. ;; Functions for Emacs < 24.4 compatibility
  496. (defun org-define-error (name message)
  497. "Define NAME as a new error signal.
  498. MESSAGE is a string that will be output to the echo area if such
  499. an error is signaled without being caught by a `condition-case'.
  500. Implements `define-error' for older emacsen."
  501. (if (fboundp 'define-error) (define-error name message)
  502. (put name 'error-conditions
  503. (copy-sequence (cons name (get 'error 'error-conditions))))))
  504. (unless (fboundp 'string-suffix-p)
  505. ;; From Emacs subr.el.
  506. (defun string-suffix-p (suffix string &optional ignore-case)
  507. "Return non-nil if SUFFIX is a suffix of STRING.
  508. If IGNORE-CASE is non-nil, the comparison is done without paying
  509. attention to case differences."
  510. (let ((start-pos (- (length string) (length suffix))))
  511. (and (>= start-pos 0)
  512. (eq t (compare-strings suffix nil nil
  513. string start-pos nil ignore-case))))))
  514. (provide 'org-compat)
  515. ;;; org-compat.el ends here