org-compat.el 38 KB

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