org-compat.el 37 KB

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