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