org-compat.el 36 KB

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