org-compat.el 36 KB

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