org-compat.el 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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-agenda-maybe-redo "org-agenda" ())
  28. (declare-function org-at-heading-p "org" (&optional ignored))
  29. (declare-function org-at-table.el-p "org" ())
  30. (declare-function org-element-at-point "org-element" ())
  31. (declare-function org-element-context "org-element" (&optional element))
  32. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  33. (declare-function org-element-type "org-element" (element))
  34. (declare-function org-element-property "org-element" (property element))
  35. (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
  36. (declare-function org-invisible-p "org" (&optional pos))
  37. (declare-function org-link-display-format "org" (s))
  38. (declare-function org-link-set-parameters "org" (type &rest rest))
  39. (declare-function org-log-into-drawer "org" ())
  40. (declare-function org-reduced-level "org" (l))
  41. (declare-function org-show-context "org" (&optional key))
  42. (declare-function org-table-end "org-table" (&optional table-type))
  43. (declare-function outline-next-heading "outline" ())
  44. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  45. (declare-function table--at-cell-p "table" (position &optional object at-column))
  46. (defvar org-complex-heading-regexp)
  47. (defvar org-table-any-border-regexp)
  48. (defvar org-table-dataline-regexp)
  49. (defvar org-table-tab-recognizes-table.el)
  50. (defvar org-table1-hline-regexp)
  51. ;;; Emacs < 25.1 compatibility
  52. (when (< emacs-major-version 25)
  53. (defalias 'outline-hide-entry 'hide-entry)
  54. (defalias 'outline-hide-sublevels 'hide-sublevels)
  55. (defalias 'outline-hide-subtree 'hide-subtree)
  56. (defalias 'outline-show-branches 'show-branches)
  57. (defalias 'outline-show-children 'show-children)
  58. (defalias 'outline-show-entry 'show-entry)
  59. (defalias 'outline-show-subtree 'show-subtree)
  60. (defalias 'xref-find-definitions 'find-tag)
  61. (defalias 'format-message 'format)
  62. (defalias 'gui-get-selection 'x-get-selection))
  63. (defun org-decode-time (&optional time zone)
  64. "Backward-compatible function for `decode-time'."
  65. (if (< emacs-major-version 25)
  66. (decode-time time)
  67. (decode-time time zone)))
  68. (unless (fboundp 'directory-name-p)
  69. (defun directory-name-p (name)
  70. "Return non-nil if NAME ends with a directory separator character."
  71. (let ((len (length name))
  72. (lastc ?.))
  73. (if (> len 0)
  74. (setq lastc (aref name (1- len))))
  75. (or (= lastc ?/)
  76. (and (memq system-type '(windows-nt ms-dos))
  77. (= lastc ?\\))))))
  78. (unless (fboundp 'directory-files-recursively)
  79. (defun directory-files-recursively (dir regexp &optional include-directories)
  80. "Return list of all files under DIR that have file names matching REGEXP.
  81. This function works recursively. Files are returned in \"depth first\"
  82. order, and files from each directory are sorted in alphabetical order.
  83. Each file name appears in the returned list in its absolute form.
  84. Optional argument INCLUDE-DIRECTORIES non-nil means also include in the
  85. output directories whose names match REGEXP."
  86. (let ((result nil)
  87. (files nil)
  88. ;; When DIR is "/", remote file names like "/method:" could
  89. ;; also be offered. We shall suppress them.
  90. (tramp-mode (and tramp-mode (file-remote-p (expand-file-name dir)))))
  91. (dolist (file (sort (file-name-all-completions "" dir)
  92. 'string<))
  93. (unless (member file '("./" "../"))
  94. (if (directory-name-p file)
  95. (let* ((leaf (substring file 0 (1- (length file))))
  96. (full-file (expand-file-name leaf dir)))
  97. ;; Don't follow symlinks to other directories.
  98. (unless (file-symlink-p full-file)
  99. (setq result
  100. (nconc result (directory-files-recursively
  101. full-file regexp include-directories))))
  102. (when (and include-directories
  103. (string-match regexp leaf))
  104. (setq result (nconc result (list full-file)))))
  105. (when (string-match regexp file)
  106. (push (expand-file-name file dir) files)))))
  107. (nconc result (nreverse files)))))
  108. ;;; Obsolete aliases (remove them after the next major release).
  109. ;;;; XEmacs compatibility, now removed.
  110. (define-obsolete-function-alias 'org-activate-mark 'activate-mark)
  111. (define-obsolete-function-alias 'org-add-hook 'add-hook "Org 9.0")
  112. (define-obsolete-function-alias 'org-bound-and-true-p 'bound-and-true-p "Org 9.0")
  113. (define-obsolete-function-alias 'org-decompose-region 'decompose-region "Org 9.0")
  114. (define-obsolete-function-alias 'org-defvaralias 'defvaralias "Org 9.0")
  115. (define-obsolete-function-alias 'org-detach-overlay 'delete-overlay "Org 9.0")
  116. (define-obsolete-function-alias 'org-file-equal-p 'file-equal-p "Org 9.0")
  117. (define-obsolete-function-alias 'org-float-time 'float-time "Org 9.0")
  118. (define-obsolete-function-alias 'org-indent-line-to 'indent-line-to "Org 9.0")
  119. (define-obsolete-function-alias 'org-indent-to-column 'indent-to-column "Org 9.0")
  120. (define-obsolete-function-alias 'org-looking-at-p 'looking-at-p "Org 9.0")
  121. (define-obsolete-function-alias 'org-looking-back 'looking-back "Org 9.0")
  122. (define-obsolete-function-alias 'org-match-string-no-properties 'match-string-no-properties "Org 9.0")
  123. (define-obsolete-function-alias 'org-propertize 'propertize "Org 9.0")
  124. (define-obsolete-function-alias 'org-select-frame-set-input-focus 'select-frame-set-input-focus "Org 9.0")
  125. (defmacro org-re (s)
  126. "Replace posix classes in regular expression S."
  127. (declare (debug (form))
  128. (obsolete "you can safely remove it." "Org 9.0"))
  129. s)
  130. ;;;; Functions from cl-lib that Org used to have its own implementation of.
  131. (define-obsolete-function-alias 'org-count 'cl-count "Org 9.0")
  132. (define-obsolete-function-alias 'org-every 'cl-every "Org 9.0")
  133. (define-obsolete-function-alias 'org-find-if 'cl-find-if "Org 9.0")
  134. (define-obsolete-function-alias 'org-reduce 'cl-reduce "Org 9.0")
  135. (define-obsolete-function-alias 'org-remove-if 'cl-remove-if "Org 9.0")
  136. (define-obsolete-function-alias 'org-remove-if-not 'cl-remove-if-not "Org 9.0")
  137. (define-obsolete-function-alias 'org-some 'cl-some "Org 9.0")
  138. (define-obsolete-function-alias 'org-floor* 'cl-floor "Org 9.0")
  139. (defun org-sublist (list start end)
  140. "Return a section of LIST, from START to END.
  141. Counting starts at 1."
  142. (cl-subseq list (1- start) end))
  143. (make-obsolete 'org-sublist
  144. "use cl-subseq (note the 0-based counting)."
  145. "Org 9.0")
  146. ;;;; Functions available since Emacs 24.3
  147. (define-obsolete-function-alias 'org-buffer-narrowed-p 'buffer-narrowed-p "Org 9.0")
  148. (define-obsolete-function-alias 'org-called-interactively-p 'called-interactively-p "Org 9.0")
  149. (define-obsolete-function-alias 'org-char-to-string 'char-to-string "Org 9.0")
  150. (define-obsolete-function-alias 'org-delete-directory 'delete-directory "Org 9.0")
  151. (define-obsolete-function-alias 'org-format-seconds 'format-seconds "Org 9.0")
  152. (define-obsolete-function-alias 'org-link-escape-browser 'url-encode-url "Org 9.0")
  153. (define-obsolete-function-alias 'org-no-warnings 'with-no-warnings "Org 9.0")
  154. (define-obsolete-function-alias 'org-number-sequence 'number-sequence "Org 9.0")
  155. (define-obsolete-function-alias 'org-pop-to-buffer-same-window 'pop-to-buffer-same-window "Org 9.0")
  156. (define-obsolete-function-alias 'org-string-match-p 'string-match-p "Org 9.0")
  157. ;;;; Functions and variables from previous releases now obsolete.
  158. (define-obsolete-function-alias 'org-element-remove-indentation
  159. 'org-remove-indentation "Org 9.0")
  160. (define-obsolete-variable-alias 'org-latex-create-formula-image-program
  161. 'org-preview-latex-default-process "Org 9.0")
  162. (define-obsolete-variable-alias 'org-latex-preview-ltxpng-directory
  163. 'org-preview-latex-image-directory "Org 9.0")
  164. (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
  165. (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 9.0")
  166. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp "Org 8.3")
  167. (define-obsolete-function-alias 'org-image-file-name-regexp
  168. 'image-file-name-regexp "Org 9.0")
  169. (define-obsolete-function-alias 'org-completing-read-no-i
  170. 'completing-read "Org 9.0")
  171. (define-obsolete-function-alias 'org-icompleting-read
  172. 'completing-read "Org 9.0")
  173. (define-obsolete-function-alias 'org-iread-file-name 'read-file-name "Org 9.0")
  174. (define-obsolete-function-alias 'org-days-to-time
  175. 'org-time-stamp-to-now "Org 8.2")
  176. (define-obsolete-variable-alias 'org-agenda-ignore-drawer-properties
  177. 'org-agenda-ignore-properties "Org 9.0")
  178. (define-obsolete-function-alias 'org-preview-latex-fragment
  179. 'org-toggle-latex-fragment "Org 8.3")
  180. (define-obsolete-function-alias 'org-export-get-genealogy
  181. 'org-element-lineage "Org 9.0")
  182. (define-obsolete-variable-alias 'org-latex-with-hyperref
  183. 'org-latex-hyperref-template "Org 9.0")
  184. (define-obsolete-variable-alias 'hfy-optimisations 'hfy-optimizations "Org 9.0")
  185. (define-obsolete-variable-alias 'org-export-htmlized-org-css-url
  186. 'org-org-htmlized-css-url "Org 8.2")
  187. (define-obsolete-function-alias 'org-list-parse-list 'org-list-to-lisp "Org 9.0")
  188. (define-obsolete-function-alias 'org-agenda-todayp
  189. 'org-agenda-today-p "Org 9.0")
  190. (define-obsolete-function-alias 'org-babel-examplize-region
  191. 'org-babel-examplify-region "Org 9.0")
  192. (define-obsolete-variable-alias 'org-babel-capitalize-example-region-markers
  193. 'org-babel-uppercase-example-markers "Org 9.1")
  194. (define-obsolete-function-alias 'org-babel-trim 'org-trim "Org 9.0")
  195. (define-obsolete-variable-alias 'org-html-style 'org-html-head "24.4")
  196. (define-obsolete-function-alias 'org-insert-columns-dblock
  197. 'org-columns-insert-dblock "Org 9.0")
  198. (define-obsolete-variable-alias 'org-export-babel-evaluate
  199. 'org-export-use-babel "Org 9.1")
  200. (define-obsolete-function-alias 'org-activate-bracket-links
  201. 'org-activate-links "Org 9.0")
  202. (define-obsolete-function-alias 'org-activate-plain-links 'ignore "Org 9.0")
  203. (define-obsolete-function-alias 'org-activate-angle-links 'ignore "Org 9.0")
  204. (defun org-in-fixed-width-region-p ()
  205. "Non-nil if point in a fixed-width region."
  206. (save-match-data
  207. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  208. (make-obsolete 'org-in-fixed-width-region-p
  209. "use `org-element' library"
  210. "Org 9.0")
  211. (defun org-compatible-face (inherits specs)
  212. "Make a compatible face specification.
  213. If INHERITS is an existing face and if the Emacs version supports
  214. it, just inherit the face. If INHERITS is not given and SPECS
  215. is, use SPECS to define the face."
  216. (declare (indent 1))
  217. (if (facep inherits)
  218. (list (list t :inherit inherits))
  219. specs))
  220. (make-obsolete 'org-compatible-face "you can remove it." "Org 9.0")
  221. (defun org-add-link-type (type &optional follow export)
  222. "Add a new TYPE link.
  223. FOLLOW and EXPORT are two functions.
  224. FOLLOW should take the link path as the single argument and do whatever
  225. is necessary to follow the link, for example find a file or display
  226. a mail message.
  227. EXPORT should format the link path for export to one of the export formats.
  228. It should be a function accepting three arguments:
  229. path the path of the link, the text after the prefix (like \"http:\")
  230. desc the description of the link, if any
  231. format the export format, a symbol like `html' or `latex' or `ascii'.
  232. The function may use the FORMAT information to return different values
  233. depending on the format. The return value will be put literally into
  234. the exported file. If the return value is nil, this means Org should
  235. do what it normally does with links which do not have EXPORT defined.
  236. Org mode has a built-in default for exporting links. If you are happy with
  237. this default, there is no need to define an export function for the link
  238. type. For a simple example of an export function, see `org-bbdb.el'.
  239. If TYPE already exists, update it with the arguments.
  240. See `org-link-parameters' for documentation on the other parameters."
  241. (org-link-set-parameters type :follow follow :export export)
  242. (message "Created %s link." type))
  243. (make-obsolete 'org-add-link-type "use `org-link-set-parameters' instead." "Org 9.0")
  244. (defun org-table-recognize-table.el ()
  245. "If there is a table.el table nearby, recognize it and move into it."
  246. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  247. (beginning-of-line)
  248. (unless (or (looking-at org-table-dataline-regexp)
  249. (not (looking-at org-table1-hline-regexp)))
  250. (forward-line)
  251. (when (looking-at org-table-any-border-regexp)
  252. (forward-line -2)))
  253. (if (re-search-forward "|" (org-table-end t) t)
  254. (progn
  255. (require 'table)
  256. (if (table--at-cell-p (point)) t
  257. (message "recognizing table.el table...")
  258. (table-recognize-table)
  259. (message "recognizing table.el table...done")))
  260. (error "This should not happen"))))
  261. ;; Not used by Org core since commit 6d1e3082, Feb 2010.
  262. (make-obsolete 'org-table-recognize-table.el
  263. "please notify the org mailing list if you use this function."
  264. "Org 9.0")
  265. (defun org-remove-angle-brackets (s)
  266. (org-unbracket-string "<" ">" s))
  267. (make-obsolete 'org-remove-angle-brackets 'org-unbracket-string "Org 9.0")
  268. (defun org-remove-double-quotes (s)
  269. (org-unbracket-string "\"" "\"" s))
  270. (make-obsolete 'org-remove-double-quotes 'org-unbracket-string "Org 9.0")
  271. (defcustom org-publish-sitemap-file-entry-format "%t"
  272. "Format string for site-map file entry.
  273. You could use brackets to delimit on what part the link will be.
  274. %t is the title.
  275. %a is the author.
  276. %d is the date formatted using `org-publish-sitemap-date-format'."
  277. :group 'org-export-publish
  278. :type 'string)
  279. (make-obsolete-variable
  280. 'org-publish-sitemap-file-entry-format
  281. "set `:sitemap-format-entry' in `org-publish-project-alist' instead."
  282. "Org 9.1")
  283. (defvar org-agenda-skip-regexp)
  284. (defun org-agenda-skip-entry-when-regexp-matches ()
  285. "Check if the current entry contains match for `org-agenda-skip-regexp'.
  286. If yes, it returns the end position of this entry, causing agenda commands
  287. to skip the entry but continuing the search in the subtree. This is a
  288. function that can be put into `org-agenda-skip-function' for the duration
  289. 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-subtree-when-regexp-matches ()
  297. "Check if the current subtree contains match for `org-agenda-skip-regexp'.
  298. If yes, it returns the end position of this tree, causing agenda commands
  299. to skip this subtree. This is a function that can be put into
  300. `org-agenda-skip-function' for the duration of a command."
  301. (declare (obsolete "use `org-agenda-skip-if' instead." "Org 9.1"))
  302. (let ((end (save-excursion (org-end-of-subtree t)))
  303. skip)
  304. (save-excursion
  305. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  306. (and skip end)))
  307. (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
  308. "Check if the current subtree contains match for `org-agenda-skip-regexp'.
  309. If yes, it returns the end position of the current entry (NOT the tree),
  310. causing agenda commands to skip the entry but continuing the search in
  311. the subtree. This is a function that can be put into
  312. `org-agenda-skip-function' for the duration of a command. An important
  313. use of this function is for the stuck project list."
  314. (declare (obsolete "use `org-agenda-skip-if' instead." "Org 9.1"))
  315. (let ((end (save-excursion (org-end-of-subtree t)))
  316. (entry-end (save-excursion (outline-next-heading) (1- (point))))
  317. skip)
  318. (save-excursion
  319. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  320. (and skip entry-end)))
  321. (define-obsolete-function-alias 'org-minutes-to-clocksum-string
  322. 'org-duration-from-minutes "Org 9.1")
  323. (define-obsolete-function-alias 'org-hh:mm-string-to-minutes
  324. 'org-duration-to-minutes "Org 9.1")
  325. (define-obsolete-function-alias 'org-duration-string-to-minutes
  326. 'org-duration-to-minutes "Org 9.1")
  327. (make-obsolete-variable 'org-time-clocksum-format
  328. "set `org-duration-format' instead." "Org 9.1")
  329. (make-obsolete-variable 'org-time-clocksum-use-fractional
  330. "set `org-duration-format' instead." "Org 9.1")
  331. (make-obsolete-variable 'org-time-clocksum-fractional-format
  332. "set `org-duration-format' instead." "Org 9.1")
  333. (make-obsolete-variable 'org-time-clocksum-use-effort-durations
  334. "set `org-duration-units' instead." "Org 9.1")
  335. (define-obsolete-function-alias 'org-babel-number-p
  336. 'org-babel--string-to-number "Org 9.0")
  337. (define-obsolete-variable-alias 'org-usenet-links-prefer-google
  338. 'org-gnus-prefer-web-links "Org 9.1")
  339. (define-obsolete-variable-alias 'org-texinfo-def-table-markup
  340. 'org-texinfo-table-default-markup "Org 9.1")
  341. ;; The function was made obsolete by commit 65399674d5 of 2013-02-22.
  342. ;; This make-obsolete call was added 2016-09-01.
  343. (make-obsolete 'org-capture-import-remember-templates
  344. "use the `org-capture-templates' variable instead."
  345. "Org 9.0")
  346. (defun org-show-block-all ()
  347. "Unfold all blocks in the current buffer."
  348. (interactive)
  349. (remove-overlays nil nil 'invisible 'org-hide-block))
  350. (make-obsolete 'org-show-block-all
  351. "use `org-show-all' instead."
  352. "Org 9.2")
  353. ;;;; Obsolete link types
  354. (eval-after-load 'org
  355. '(progn
  356. (org-link-set-parameters "file+emacs") ;since Org 9.0
  357. (org-link-set-parameters "file+sys"))) ;since Org 9.0
  358. ;;; Miscellaneous functions
  359. (defun org-version-check (version feature level)
  360. (let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
  361. (v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
  362. (rmaj (or (nth 0 v1) 99))
  363. (rmin (or (nth 1 v1) 99))
  364. (rbld (or (nth 2 v1) 99))
  365. (maj (or (nth 0 v2) 0))
  366. (min (or (nth 1 v2) 0))
  367. (bld (or (nth 2 v2) 0)))
  368. (if (or (< maj rmaj)
  369. (and (= maj rmaj)
  370. (< min rmin))
  371. (and (= maj rmaj)
  372. (= min rmin)
  373. (< bld rbld)))
  374. (if (eq level :predicate)
  375. ;; just return if we have the version
  376. nil
  377. (let ((msg (format "Emacs %s or greater is recommended for %s"
  378. version feature)))
  379. (display-warning 'org msg level)
  380. t))
  381. t)))
  382. (defun org-get-x-clipboard (value)
  383. "Get the value of the X or Windows clipboard."
  384. (cond ((and (eq window-system 'x)
  385. (fboundp 'gui-get-selection)) ;Silence byte-compiler.
  386. (org-no-properties
  387. (ignore-errors
  388. (or (gui-get-selection value 'UTF8_STRING)
  389. (gui-get-selection value 'COMPOUND_TEXT)
  390. (gui-get-selection value 'STRING)
  391. (gui-get-selection value 'TEXT)))))
  392. ((and (eq window-system 'w32) (fboundp 'w32-get-clipboard-data))
  393. (w32-get-clipboard-data))))
  394. (defun org-fit-window-to-buffer (&optional window max-height min-height
  395. shrink-only)
  396. "Fit WINDOW to the buffer, but only if it is not a side-by-side window.
  397. WINDOW defaults to the selected window. MAX-HEIGHT and MIN-HEIGHT are
  398. passed through to `fit-window-to-buffer'. If SHRINK-ONLY is set, call
  399. `shrink-window-if-larger-than-buffer' instead, the height limit is
  400. ignored in this case."
  401. (cond ((if (fboundp 'window-full-width-p)
  402. (not (window-full-width-p window))
  403. ;; do nothing if another window would suffer
  404. (> (frame-width) (window-width window))))
  405. ((and (fboundp 'fit-window-to-buffer) (not shrink-only))
  406. (fit-window-to-buffer window max-height min-height))
  407. ((fboundp 'shrink-window-if-larger-than-buffer)
  408. (shrink-window-if-larger-than-buffer window)))
  409. (or window (selected-window)))
  410. ;; `set-transient-map' is only in Emacs >= 24.4
  411. (defalias 'org-set-transient-map
  412. (if (fboundp 'set-transient-map)
  413. 'set-transient-map
  414. 'set-temporary-overlay-map))
  415. ;;; Region compatibility
  416. (defvar org-ignore-region nil
  417. "Non-nil means temporarily disable the active region.")
  418. (defun org-region-active-p ()
  419. "Non-nil when the region active.
  420. Unlike to `use-region-p', this function also checks
  421. `org-ignore-region'."
  422. (and (not org-ignore-region) (use-region-p)))
  423. (defun org-cursor-to-region-beginning ()
  424. (when (and (org-region-active-p)
  425. (> (point) (region-beginning)))
  426. (exchange-point-and-mark)))
  427. ;;; Invisibility compatibility
  428. (defun org-remove-from-invisibility-spec (arg)
  429. "Remove elements from `buffer-invisibility-spec'."
  430. (if (fboundp 'remove-from-invisibility-spec)
  431. (remove-from-invisibility-spec arg)
  432. (if (consp buffer-invisibility-spec)
  433. (setq buffer-invisibility-spec
  434. (delete arg buffer-invisibility-spec)))))
  435. (defun org-in-invisibility-spec-p (arg)
  436. "Is ARG a member of `buffer-invisibility-spec'?"
  437. (if (consp buffer-invisibility-spec)
  438. (member arg buffer-invisibility-spec)))
  439. (defun org-move-to-column (column &optional force _buffer)
  440. "Move to column COLUMN.
  441. Pass COLUMN and FORCE to `move-to-column'."
  442. (let ((buffer-invisibility-spec
  443. (if (listp buffer-invisibility-spec)
  444. (remove '(org-filtered) buffer-invisibility-spec)
  445. buffer-invisibility-spec)))
  446. (move-to-column column force)))
  447. (defmacro org-find-library-dir (library)
  448. `(file-name-directory (or (locate-library ,library) "")))
  449. (defun org-count-lines (s)
  450. "How many lines in string S?"
  451. (let ((start 0) (n 1))
  452. (while (string-match "\n" s start)
  453. (setq start (match-end 0) n (1+ n)))
  454. (if (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
  455. (setq n (1- n)))
  456. n))
  457. (defun org-kill-new (string &rest args)
  458. (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
  459. string)
  460. (apply 'kill-new string args))
  461. ;; `font-lock-ensure' is only available from 24.4.50 on
  462. (defalias 'org-font-lock-ensure
  463. (if (fboundp 'font-lock-ensure)
  464. #'font-lock-ensure
  465. (lambda (&optional _beg _end)
  466. (with-no-warnings (font-lock-fontify-buffer)))))
  467. ;; `file-local-name' was added in Emacs 26.1.
  468. (defalias 'org-babel-local-file-name
  469. (if (fboundp 'file-local-name)
  470. 'file-local-name
  471. (lambda (file)
  472. "Return the local name component of FILE."
  473. (or (file-remote-p file 'localname) file))))
  474. (defmacro org-no-popups (&rest body)
  475. "Suppress popup windows.
  476. Let-bind some variables to nil around BODY to achieve the desired
  477. effect, which variables to use depends on the Emacs version."
  478. (if (org-version-check "24.2.50" "" :predicate)
  479. `(let (pop-up-frames display-buffer-alist)
  480. ,@body)
  481. `(let (pop-up-frames special-display-buffer-names special-display-regexps special-display-function)
  482. ,@body)))
  483. ;;;###autoload
  484. (defmacro org-check-version ()
  485. "Try very hard to provide sensible version strings."
  486. (let* ((org-dir (org-find-library-dir "org"))
  487. (org-version.el (concat org-dir "org-version.el"))
  488. (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
  489. (if (require 'org-version org-version.el 'noerror)
  490. '(progn
  491. (autoload 'org-release "org-version.el")
  492. (autoload 'org-git-version "org-version.el"))
  493. (if (require 'org-fixup org-fixup.el 'noerror)
  494. '(org-fixup)
  495. ;; provide fallback definitions and complain
  496. (warn "Could not define org version correctly. Check installation!")
  497. '(progn
  498. (defun org-release () "N/A")
  499. (defun org-git-version () "N/A !!check installation!!"))))))
  500. (defmacro org-with-silent-modifications (&rest body)
  501. (if (fboundp 'with-silent-modifications)
  502. `(with-silent-modifications ,@body)
  503. `(org-unmodified ,@body)))
  504. (def-edebug-spec org-with-silent-modifications (body))
  505. ;;; Functions for Emacs < 24.4 compatibility
  506. (defun org-define-error (name message)
  507. "Define NAME as a new error signal.
  508. MESSAGE is a string that will be output to the echo area if such
  509. an error is signaled without being caught by a `condition-case'.
  510. Implements `define-error' for older emacsen."
  511. (if (fboundp 'define-error) (define-error name message)
  512. (put name 'error-conditions
  513. (copy-sequence (cons name (get 'error 'error-conditions))))))
  514. (unless (fboundp 'string-suffix-p)
  515. ;; From Emacs subr.el.
  516. (defun string-suffix-p (suffix string &optional ignore-case)
  517. "Return non-nil if SUFFIX is a suffix of STRING.
  518. If IGNORE-CASE is non-nil, the comparison is done without paying
  519. attention to case differences."
  520. (let ((start-pos (- (length string) (length suffix))))
  521. (and (>= start-pos 0)
  522. (eq t (compare-strings suffix nil nil
  523. string start-pos nil ignore-case))))))
  524. ;;; Integration with and fixes for other packages
  525. (defgroup org-imenu-and-speedbar nil
  526. "Options concerning imenu and speedbar in Org mode."
  527. :tag "Org Imenu and Speedbar"
  528. :group 'org-structure)
  529. (defcustom org-imenu-depth 2
  530. "The maximum level for Imenu access to Org headlines.
  531. This also applied for speedbar access."
  532. :group 'org-imenu-and-speedbar
  533. :type 'integer)
  534. ;;;; Imenu
  535. (defvar-local org-imenu-markers nil
  536. "All markers currently used by Imenu.")
  537. (defun org-imenu-new-marker (&optional pos)
  538. "Return a new marker for use by Imenu, and remember the marker."
  539. (let ((m (make-marker)))
  540. (move-marker m (or pos (point)))
  541. (push m org-imenu-markers)
  542. m))
  543. (defun org-imenu-get-tree ()
  544. "Produce the index for Imenu."
  545. (dolist (x org-imenu-markers) (move-marker x nil))
  546. (setq org-imenu-markers nil)
  547. (let* ((case-fold-search nil)
  548. (n org-imenu-depth)
  549. (re (concat "^" (org-get-limited-outline-regexp)))
  550. (subs (make-vector (1+ n) nil))
  551. (last-level 0)
  552. m level head0 head)
  553. (org-with-wide-buffer
  554. (goto-char (point-max))
  555. (while (re-search-backward re nil t)
  556. (setq level (org-reduced-level (funcall outline-level)))
  557. (when (and (<= level n)
  558. (looking-at org-complex-heading-regexp)
  559. (setq head0 (match-string-no-properties 4)))
  560. (setq head (org-link-display-format head0)
  561. m (org-imenu-new-marker))
  562. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  563. (if (>= level last-level)
  564. (push (cons head m) (aref subs level))
  565. (push (cons head (aref subs (1+ level))) (aref subs level))
  566. (cl-loop for i from (1+ level) to n do (aset subs i nil)))
  567. (setq last-level level))))
  568. (aref subs 1)))
  569. (eval-after-load "imenu"
  570. '(progn
  571. (add-hook 'imenu-after-jump-hook
  572. (lambda ()
  573. (when (derived-mode-p 'org-mode)
  574. (org-show-context 'org-goto))))))
  575. ;;;; Speedbar
  576. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  577. "Overlay marking the agenda restriction line in speedbar.")
  578. (overlay-put org-speedbar-restriction-lock-overlay
  579. 'face 'org-agenda-restriction-lock)
  580. (overlay-put org-speedbar-restriction-lock-overlay
  581. 'help-echo "Agendas are currently limited to this item.")
  582. (delete-overlay org-speedbar-restriction-lock-overlay)
  583. (defun org-speedbar-set-agenda-restriction ()
  584. "Restrict future agenda commands to the location at point in speedbar.
  585. To get rid of the restriction, use `\\[org-agenda-remove-restriction-lock]'."
  586. (interactive)
  587. (require 'org-agenda)
  588. (let (p m tp np dir txt)
  589. (cond
  590. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  591. 'org-imenu t))
  592. (setq m (get-text-property p 'org-imenu-marker))
  593. (with-current-buffer (marker-buffer m)
  594. (goto-char m)
  595. (org-agenda-set-restriction-lock 'subtree)))
  596. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  597. 'speedbar-function 'speedbar-find-file))
  598. (setq tp (previous-single-property-change
  599. (1+ p) 'speedbar-function)
  600. np (next-single-property-change
  601. tp 'speedbar-function)
  602. dir (speedbar-line-directory)
  603. txt (buffer-substring-no-properties (or tp (point-min))
  604. (or np (point-max))))
  605. (with-current-buffer (find-file-noselect
  606. (let ((default-directory dir))
  607. (expand-file-name txt)))
  608. (unless (derived-mode-p 'org-mode)
  609. (user-error "Cannot restrict to non-Org mode file"))
  610. (org-agenda-set-restriction-lock 'file)))
  611. (t (user-error "Don't know how to restrict Org mode agenda")))
  612. (move-overlay org-speedbar-restriction-lock-overlay
  613. (point-at-bol) (point-at-eol))
  614. (setq current-prefix-arg nil)
  615. (org-agenda-maybe-redo)))
  616. (defvar speedbar-file-key-map)
  617. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  618. (eval-after-load "speedbar"
  619. '(progn
  620. (speedbar-add-supported-extension ".org")
  621. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  622. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  623. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  624. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  625. (add-hook 'speedbar-visiting-tag-hook
  626. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  627. ;;;; Flyspell
  628. (defun org--flyspell-object-check-p (element)
  629. "Non-nil when Flyspell can check object at point.
  630. ELEMENT is the element at point."
  631. (let ((object (save-excursion
  632. (when (looking-at-p "\\>") (backward-char))
  633. (org-element-context element))))
  634. (cl-case (org-element-type object)
  635. ;; Prevent checks in links due to keybinding conflict with
  636. ;; Flyspell.
  637. ((code entity export-snippet inline-babel-call
  638. inline-src-block line-break latex-fragment link macro
  639. statistics-cookie target timestamp verbatim)
  640. nil)
  641. (footnote-reference
  642. ;; Only in inline footnotes, within the definition.
  643. (and (eq (org-element-property :type object) 'inline)
  644. (< (save-excursion
  645. (goto-char (org-element-property :begin object))
  646. (search-forward ":" nil t 2))
  647. (point))))
  648. (otherwise t))))
  649. (defun org-mode-flyspell-verify ()
  650. "Function used for `flyspell-generic-check-word-predicate'."
  651. (if (org-at-heading-p)
  652. ;; At a headline or an inlinetask, check title only. This is
  653. ;; faster than relying on `org-element-at-point'.
  654. (and (save-excursion (beginning-of-line)
  655. (and (let ((case-fold-search t))
  656. (not (looking-at-p "\\*+ END[ \t]*$")))
  657. (let ((case-fold-search nil))
  658. (looking-at org-complex-heading-regexp))))
  659. (match-beginning 4)
  660. (>= (point) (match-beginning 4))
  661. (or (not (match-beginning 5))
  662. (< (point) (match-beginning 5))))
  663. (let* ((element (org-element-at-point))
  664. (post-affiliated (org-element-property :post-affiliated element)))
  665. (cond
  666. ;; Ignore checks in all affiliated keywords but captions.
  667. ((< (point) post-affiliated)
  668. (and (save-excursion
  669. (beginning-of-line)
  670. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  671. (> (point) (match-end 0))
  672. (org--flyspell-object-check-p element)))
  673. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  674. ((let ((log (org-log-into-drawer)))
  675. (and log
  676. (let ((drawer (org-element-lineage element '(drawer))))
  677. (and drawer
  678. (eq (compare-strings
  679. log nil nil
  680. (org-element-property :drawer-name drawer) nil nil t)
  681. t)))))
  682. nil)
  683. (t
  684. (cl-case (org-element-type element)
  685. ((comment quote-section) t)
  686. (comment-block
  687. ;; Allow checks between block markers, not on them.
  688. (and (> (line-beginning-position) post-affiliated)
  689. (save-excursion
  690. (end-of-line)
  691. (skip-chars-forward " \r\t\n")
  692. (< (point) (org-element-property :end element)))))
  693. ;; Arbitrary list of keywords where checks are meaningful.
  694. ;; Make sure point is on the value part of the element.
  695. (keyword
  696. (and (member (org-element-property :key element)
  697. '("DESCRIPTION" "TITLE"))
  698. (save-excursion
  699. (search-backward ":" (line-beginning-position) t))))
  700. ;; Check is globally allowed in paragraphs verse blocks and
  701. ;; table rows (after affiliated keywords) but some objects
  702. ;; must not be affected.
  703. ((paragraph table-row verse-block)
  704. (let ((cbeg (org-element-property :contents-begin element))
  705. (cend (org-element-property :contents-end element)))
  706. (and cbeg (>= (point) cbeg) (< (point) cend)
  707. (org--flyspell-object-check-p element))))))))))
  708. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  709. (defun org-remove-flyspell-overlays-in (beg end)
  710. "Remove flyspell overlays in region."
  711. (and (bound-and-true-p flyspell-mode)
  712. (fboundp 'flyspell-delete-region-overlays)
  713. (flyspell-delete-region-overlays beg end)))
  714. (defvar flyspell-delayed-commands)
  715. (eval-after-load "flyspell"
  716. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  717. ;;;; Bookmark
  718. (defun org-bookmark-jump-unhide ()
  719. "Unhide the current position, to show the bookmark location."
  720. (and (derived-mode-p 'org-mode)
  721. (or (org-invisible-p)
  722. (save-excursion (goto-char (max (point-min) (1- (point))))
  723. (org-invisible-p)))
  724. (org-show-context 'bookmark-jump)))
  725. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  726. (eval-after-load "bookmark"
  727. '(if (boundp 'bookmark-after-jump-hook)
  728. ;; We can use the hook
  729. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  730. ;; Hook not available, use advice
  731. (defadvice bookmark-jump (after org-make-visible activate)
  732. "Make the position visible."
  733. (org-bookmark-jump-unhide))))
  734. ;;;; Saveplace
  735. ;; Make sure saveplace shows the location if it was hidden
  736. (eval-after-load "saveplace"
  737. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  738. "Make the position visible."
  739. (org-bookmark-jump-unhide)))
  740. ;;;; Ecb
  741. ;; Make sure ecb shows the location if it was hidden
  742. (eval-after-load "ecb"
  743. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  744. "Make hierarchy visible when jumping into location from ECB tree buffer."
  745. (when (derived-mode-p 'org-mode)
  746. (org-show-context))))
  747. ;;;; Simple
  748. (defun org-mark-jump-unhide ()
  749. "Make the point visible with `org-show-context' after jumping to the mark."
  750. (when (and (derived-mode-p 'org-mode)
  751. (org-invisible-p))
  752. (org-show-context 'mark-goto)))
  753. (eval-after-load "simple"
  754. '(defadvice pop-to-mark-command (after org-make-visible activate)
  755. "Make the point visible with `org-show-context'."
  756. (org-mark-jump-unhide)))
  757. (eval-after-load "simple"
  758. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  759. "Make the point visible with `org-show-context'."
  760. (org-mark-jump-unhide)))
  761. (eval-after-load "simple"
  762. '(defadvice pop-global-mark (after org-make-visible activate)
  763. "Make the point visible with `org-show-context'."
  764. (org-mark-jump-unhide)))
  765. ;;;; Session
  766. ;; Make "session.el" ignore our circular variable.
  767. (defvar session-globals-exclude)
  768. (eval-after-load "session"
  769. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  770. (provide 'org-compat)
  771. ;;; org-compat.el ends here