org-compat.el 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. ;;; org-compat.el --- Compatibility Code for Older Emacsen -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2004-2021 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten.dominik@gmail.com>
  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-set-restriction-lock "org-agenda" (&optional type))
  30. (declare-function org-agenda-remove-restriction-lock "org-agenda" (&optional noupdate))
  31. (declare-function org-calendar-goto-agenda "org-agenda" ())
  32. (declare-function org-align-tags "org" (&optional all))
  33. (declare-function org-at-heading-p "org" (&optional ignored))
  34. (declare-function org-at-table.el-p "org" ())
  35. (declare-function org-element-at-point "org-element" ())
  36. (declare-function org-element-context "org-element" (&optional element))
  37. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  38. (declare-function org-element-type "org-element" (element))
  39. (declare-function org-element-property "org-element" (property element))
  40. (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
  41. (declare-function org-get-heading "org" (&optional no-tags no-todo no-priority no-comment))
  42. (declare-function org-get-tags "org" (&optional pos local))
  43. (declare-function org-hide-block-toggle "org" (&optional force no-error element))
  44. (declare-function org-link-display-format "ol" (s))
  45. (declare-function org-link-set-parameters "ol" (type &rest rest))
  46. (declare-function org-log-into-drawer "org" ())
  47. (declare-function org-make-tag-string "org" (tags))
  48. (declare-function org-reduced-level "org" (l))
  49. (declare-function org-return "org" (&optional indent arg interactive))
  50. (declare-function org-show-context "org" (&optional key))
  51. (declare-function org-table-end "org-table" (&optional table-type))
  52. (declare-function outline-next-heading "outline" ())
  53. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  54. (declare-function table--at-cell-p "table" (position &optional object at-column))
  55. (defvar calendar-mode-map)
  56. (defvar org-complex-heading-regexp)
  57. (defvar org-agenda-diary-file)
  58. (defvar org-agenda-overriding-restriction)
  59. (defvar org-agenda-restriction-lock-overlay)
  60. (defvar org-table-any-border-regexp)
  61. (defvar org-table-dataline-regexp)
  62. (defvar org-table-tab-recognizes-table.el)
  63. (defvar org-table1-hline-regexp)
  64. ;;; Emacs < 28.1 compatibility
  65. (if (fboundp 'length>)
  66. (defalias 'org-length> #'length>)
  67. (defun org-length> (sequence length)
  68. "Return non-nil if SEQUENCE is longer than LENGTH."
  69. (> (length sequence) length)))
  70. (if (fboundp 'file-name-concat)
  71. (defalias 'org-file-name-concat #'file-name-concat)
  72. (defun org-file-name-concat (directory &rest components)
  73. "Append COMPONENTS to DIRECTORY and return the resulting string.
  74. Elements in COMPONENTS must be a string or nil.
  75. DIRECTORY or the non-final elements in COMPONENTS may or may not end
  76. with a slash -- if they don't end with a slash, a slash will be
  77. inserted before contatenating."
  78. (save-match-data
  79. (mapconcat
  80. #'identity
  81. (delq nil
  82. (mapcar
  83. (lambda (str)
  84. (when (and str (not (string-empty-p str))
  85. (string-match "\\(.+\\)/?" str))
  86. (match-string 1 str)))
  87. (cons directory components)))
  88. "/"))))
  89. (if (fboundp 'directory-empty-p)
  90. (defalias 'org-directory-empty-p #'directory-empty-p)
  91. (defun org-directory-empty-p (dir)
  92. "Return t if DIR names an existing directory containing no other files."
  93. (and (file-directory-p dir)
  94. (null (directory-files dir nil directory-files-no-dot-files-regexp t)))))
  95. ;;; Emacs < 27.1 compatibility
  96. (unless (fboundp 'proper-list-p)
  97. ;; `proper-list-p' was added in Emacs 27.1. The function below is
  98. ;; taken from Emacs subr.el 200195e824b^.
  99. (defun proper-list-p (object)
  100. "Return OBJECT's length if it is a proper list, nil otherwise.
  101. A proper list is neither circular nor dotted (i.e., its last cdr
  102. is nil)."
  103. (and (listp object) (ignore-errors (length object)))))
  104. (if (fboundp 'xor)
  105. ;; `xor' was added in Emacs 27.1.
  106. (defalias 'org-xor #'xor)
  107. (defsubst org-xor (a b)
  108. "Exclusive `or'."
  109. (if a (not b) b)))
  110. (unless (fboundp 'pcomplete-uniquify-list)
  111. ;; The misspelled variant was made obsolete in Emacs 27.1
  112. (defalias 'pcomplete-uniquify-list 'pcomplete-uniqify-list))
  113. (if (fboundp 'time-convert)
  114. (progn
  115. (defsubst org-time-convert-to-integer (time)
  116. (time-convert time 'integer))
  117. (defsubst org-time-convert-to-list (time)
  118. (time-convert time 'list)))
  119. (defun org-time-convert-to-integer (time)
  120. (floor (float-time time)))
  121. (defun org-time-convert-to-list (time)
  122. (seconds-to-time (float-time time))))
  123. ;; `newline-and-indent' did not take a numeric argument before 27.1.
  124. (if (version< emacs-version "27")
  125. (defsubst org-newline-and-indent (&optional _arg)
  126. (newline-and-indent))
  127. (defalias 'org-newline-and-indent #'newline-and-indent))
  128. (defun org--set-faces-extend (faces extend-p)
  129. "Set the :extend attribute of FACES to EXTEND-P.
  130. This is a no-op for Emacs versions lower than 27, since face
  131. extension beyond end of line was not controllable."
  132. (when (fboundp 'set-face-extend)
  133. (mapc (lambda (f) (set-face-extend f extend-p)) faces)))
  134. (if (fboundp 'string-distance)
  135. (defalias 'org-string-distance 'string-distance)
  136. (defun org-string-distance (s1 s2)
  137. "Return the edit (levenshtein) distance between strings S1 S2."
  138. (let* ((l1 (length s1))
  139. (l2 (length s2))
  140. (dist (vconcat (mapcar (lambda (_) (make-vector (1+ l2) nil))
  141. (number-sequence 1 (1+ l1)))))
  142. (in (lambda (i j) (aref (aref dist i) j))))
  143. (setf (aref (aref dist 0) 0) 0)
  144. (dolist (j (number-sequence 1 l2))
  145. (setf (aref (aref dist 0) j) j))
  146. (dolist (i (number-sequence 1 l1))
  147. (setf (aref (aref dist i) 0) i)
  148. (dolist (j (number-sequence 1 l2))
  149. (setf (aref (aref dist i) j)
  150. (min
  151. (1+ (funcall in (1- i) j))
  152. (1+ (funcall in i (1- j)))
  153. (+ (if (equal (aref s1 (1- i)) (aref s2 (1- j))) 0 1)
  154. (funcall in (1- i) (1- j)))))))
  155. (funcall in l1 l2))))
  156. (define-obsolete-function-alias 'org-babel-edit-distance 'org-string-distance
  157. "9.5")
  158. ;;; Emacs < 26.1 compatibility
  159. (if (fboundp 'line-number-display-width)
  160. (defalias 'org-line-number-display-width 'line-number-display-width)
  161. (defun org-line-number-display-width (&rest _) 0))
  162. (if (fboundp 'buffer-hash)
  163. (defalias 'org-buffer-hash 'buffer-hash)
  164. (defun org-buffer-hash () (md5 (current-buffer))))
  165. (unless (fboundp 'file-attribute-modification-time)
  166. (defsubst file-attribute-modification-time (attributes)
  167. "The modification time in ATTRIBUTES returned by `file-attributes'.
  168. This is the time of the last change to the file's contents, and
  169. is a list of integers (HIGH LOW USEC PSEC) in the same style
  170. as (current-time)."
  171. (nth 5 attributes)))
  172. (unless (fboundp 'file-attribute-size)
  173. (defsubst file-attribute-size (attributes)
  174. "The size (in bytes) in ATTRIBUTES returned by `file-attributes'.
  175. This is a floating point number if the size is too large for an integer."
  176. (nth 7 attributes)))
  177. ;;; Obsolete aliases (remove them after the next major release).
  178. ;;;; XEmacs compatibility, now removed.
  179. (define-obsolete-function-alias 'org-activate-mark 'activate-mark "9.0")
  180. (define-obsolete-function-alias 'org-add-hook 'add-hook "9.0")
  181. (define-obsolete-function-alias 'org-bound-and-true-p 'bound-and-true-p "9.0")
  182. (define-obsolete-function-alias 'org-decompose-region 'decompose-region "9.0")
  183. (define-obsolete-function-alias 'org-defvaralias 'defvaralias "9.0")
  184. (define-obsolete-function-alias 'org-detach-overlay 'delete-overlay "9.0")
  185. (define-obsolete-function-alias 'org-file-equal-p 'file-equal-p "9.0")
  186. (define-obsolete-function-alias 'org-float-time 'float-time "9.0")
  187. (define-obsolete-function-alias 'org-indent-line-to 'indent-line-to "9.0")
  188. (define-obsolete-function-alias 'org-indent-to-column 'indent-to-column "9.0")
  189. (define-obsolete-function-alias 'org-looking-at-p 'looking-at-p "9.0")
  190. (define-obsolete-function-alias 'org-looking-back 'looking-back "9.0")
  191. (define-obsolete-function-alias 'org-match-string-no-properties 'match-string-no-properties "9.0")
  192. (define-obsolete-function-alias 'org-propertize 'propertize "9.0")
  193. (define-obsolete-function-alias 'org-select-frame-set-input-focus 'select-frame-set-input-focus "9.0")
  194. (define-obsolete-function-alias 'org-file-remote-p 'file-remote-p "9.2")
  195. (defmacro org-re (s)
  196. "Replace posix classes in regular expression S."
  197. (declare (debug (form))
  198. (obsolete "you can safely remove it." "9.0"))
  199. s)
  200. ;;;; Functions from cl-lib that Org used to have its own implementation of.
  201. (define-obsolete-function-alias 'org-count 'cl-count "9.0")
  202. (define-obsolete-function-alias 'org-every 'cl-every "9.0")
  203. (define-obsolete-function-alias 'org-find-if 'cl-find-if "9.0")
  204. (define-obsolete-function-alias 'org-reduce 'cl-reduce "9.0")
  205. (define-obsolete-function-alias 'org-remove-if 'cl-remove-if "9.0")
  206. (define-obsolete-function-alias 'org-remove-if-not 'cl-remove-if-not "9.0")
  207. (define-obsolete-function-alias 'org-some 'cl-some "9.0")
  208. (define-obsolete-function-alias 'org-floor* 'cl-floor "9.0")
  209. (defun org-sublist (list start end)
  210. "Return a section of LIST, from START to END.
  211. Counting starts at 1."
  212. (cl-subseq list (1- start) end))
  213. (make-obsolete 'org-sublist
  214. "use cl-subseq (note the 0-based counting)."
  215. "9.0")
  216. ;;;; Functions available since Emacs 25.1
  217. (define-obsolete-function-alias 'org-string-collate-lessp 'string-collate-lessp "9.6")
  218. (define-obsolete-function-alias 'org-decode-time 'decode-time "9.6")
  219. (define-obsolete-function-alias 'org-format-time-string 'format-time-string "9.6")
  220. (define-obsolete-function-alias 'org-time-add 'time-add "9.6")
  221. (define-obsolete-function-alias 'org-time-subtract 'time-subtract "9.6")
  222. (define-obsolete-function-alias 'org-time-since 'time-since "9.6")
  223. (define-obsolete-function-alias 'org-time-less-p 'time-less-p "9.6")
  224. ;;;; Functions available since Emacs 24.3
  225. (define-obsolete-function-alias 'org-buffer-narrowed-p 'buffer-narrowed-p "9.0")
  226. (define-obsolete-function-alias 'org-called-interactively-p 'called-interactively-p "9.0")
  227. (define-obsolete-function-alias 'org-char-to-string 'char-to-string "9.0")
  228. (define-obsolete-function-alias 'org-delete-directory 'delete-directory "9.0")
  229. (define-obsolete-function-alias 'org-format-seconds 'format-seconds "9.0")
  230. (define-obsolete-function-alias 'org-link-escape-browser 'url-encode-url "9.0")
  231. (define-obsolete-function-alias 'org-no-warnings 'with-no-warnings "9.0")
  232. (define-obsolete-function-alias 'org-number-sequence 'number-sequence "9.0")
  233. (define-obsolete-function-alias 'org-pop-to-buffer-same-window 'pop-to-buffer-same-window "9.0")
  234. (define-obsolete-function-alias 'org-string-match-p 'string-match-p "9.0")
  235. ;;;; Functions and variables from previous releases now obsolete.
  236. (define-obsolete-function-alias 'org-element-remove-indentation
  237. 'org-remove-indentation "9.0")
  238. (define-obsolete-variable-alias 'org-latex-create-formula-image-program
  239. 'org-preview-latex-default-process "9.0")
  240. (define-obsolete-variable-alias 'org-latex-preview-ltxpng-directory
  241. 'org-preview-latex-image-directory "9.0")
  242. (define-obsolete-function-alias 'org-table-p 'org-at-table-p "9.0")
  243. (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "9.0")
  244. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp "8.3")
  245. (define-obsolete-function-alias 'org-image-file-name-regexp
  246. 'image-file-name-regexp "9.0")
  247. (define-obsolete-function-alias 'org-completing-read-no-i
  248. 'completing-read "9.0")
  249. (define-obsolete-function-alias 'org-icompleting-read
  250. 'completing-read "9.0")
  251. (define-obsolete-function-alias 'org-iread-file-name 'read-file-name "9.0")
  252. (define-obsolete-function-alias 'org-days-to-time
  253. 'org-time-stamp-to-now "8.2")
  254. (define-obsolete-variable-alias 'org-agenda-ignore-drawer-properties
  255. 'org-agenda-ignore-properties "9.0")
  256. (define-obsolete-function-alias 'org-preview-latex-fragment
  257. 'org-toggle-latex-fragment "8.3")
  258. (define-obsolete-function-alias 'org-export-get-genealogy
  259. 'org-element-lineage "9.0")
  260. (define-obsolete-variable-alias 'org-latex-with-hyperref
  261. 'org-latex-hyperref-template "9.0")
  262. (define-obsolete-variable-alias 'hfy-optimisations 'hfy-optimizations "9.0")
  263. (define-obsolete-variable-alias 'org-export-htmlized-org-css-url
  264. 'org-org-htmlized-css-url "8.2")
  265. (define-obsolete-function-alias 'org-list-parse-list 'org-list-to-lisp "9.0")
  266. (define-obsolete-function-alias 'org-agenda-todayp
  267. 'org-agenda-today-p "9.0")
  268. (define-obsolete-function-alias 'org-babel-examplize-region
  269. 'org-babel-examplify-region "9.0")
  270. (define-obsolete-variable-alias 'org-babel-capitalize-example-region-markers
  271. 'org-babel-uppercase-example-markers "9.1")
  272. (define-obsolete-function-alias 'org-babel-trim 'org-trim "9.0")
  273. (define-obsolete-variable-alias 'org-html-style 'org-html-head "24.4")
  274. (define-obsolete-function-alias 'org-insert-columns-dblock
  275. 'org-columns-insert-dblock "9.0")
  276. (define-obsolete-variable-alias 'org-export-babel-evaluate
  277. 'org-export-use-babel "9.1")
  278. (define-obsolete-function-alias 'org-activate-bracket-links
  279. 'org-activate-links "9.0")
  280. (define-obsolete-function-alias 'org-activate-plain-links 'ignore "9.0")
  281. (define-obsolete-function-alias 'org-activate-angle-links 'ignore "9.0")
  282. (define-obsolete-function-alias 'org-remove-double-quotes 'org-strip-quotes "9.0")
  283. (define-obsolete-function-alias 'org-get-indentation
  284. 'current-indentation "9.2")
  285. (define-obsolete-function-alias 'org-capture-member 'org-capture-get "9.2")
  286. (define-obsolete-function-alias 'org-remove-from-invisibility-spec
  287. 'remove-from-invisibility-spec "9.2")
  288. (define-obsolete-variable-alias 'org-effort-durations 'org-duration-units
  289. "9.2")
  290. (define-obsolete-function-alias 'org-toggle-latex-fragment 'org-latex-preview
  291. "9.3")
  292. (define-obsolete-function-alias 'org-remove-latex-fragment-image-overlays
  293. 'org-clear-latex-preview "9.3")
  294. (define-obsolete-variable-alias 'org-attach-directory
  295. 'org-attach-id-dir "9.3")
  296. (make-obsolete 'org-attach-store-link "No longer used" "9.4")
  297. (make-obsolete 'org-attach-expand-link "No longer used" "9.4")
  298. (define-obsolete-function-alias 'org-file-url-p 'org-url-p "9.5")
  299. (defun org-in-fixed-width-region-p ()
  300. "Non-nil if point in a fixed-width region."
  301. (save-match-data
  302. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  303. (make-obsolete 'org-in-fixed-width-region-p
  304. "use `org-element' library"
  305. "9.0")
  306. (defun org-compatible-face (inherits specs)
  307. "Make a compatible face specification.
  308. If INHERITS is an existing face and if the Emacs version supports
  309. it, just inherit the face. If INHERITS is not given and SPECS
  310. is, use SPECS to define the face."
  311. (declare (indent 1))
  312. (if (facep inherits)
  313. (list (list t :inherit inherits))
  314. specs))
  315. (make-obsolete 'org-compatible-face "you can remove it." "9.0")
  316. (defun org-add-link-type (type &optional follow export)
  317. "Add a new TYPE link.
  318. FOLLOW and EXPORT are two functions.
  319. FOLLOW should take the link path as the single argument and do whatever
  320. is necessary to follow the link, for example find a file or display
  321. a mail message.
  322. EXPORT should format the link path for export to one of the export formats.
  323. It should be a function accepting three arguments:
  324. path the path of the link, the text after the prefix (like \"http:\")
  325. desc the description of the link, if any
  326. format the export format, a symbol like `html' or `latex' or `ascii'.
  327. The function may use the FORMAT information to return different values
  328. depending on the format. The return value will be put literally into
  329. the exported file. If the return value is nil, this means Org should
  330. do what it normally does with links which do not have EXPORT defined.
  331. Org mode has a built-in default for exporting links. If you are happy with
  332. this default, there is no need to define an export function for the link
  333. type. For a simple example of an export function, see `org-bbdb.el'.
  334. If TYPE already exists, update it with the arguments.
  335. See `org-link-parameters' for documentation on the other parameters."
  336. (org-link-set-parameters type :follow follow :export export)
  337. (message "Created %s link." type))
  338. (make-obsolete 'org-add-link-type "use `org-link-set-parameters' instead." "9.0")
  339. ;;;; Functions unused in Org core.
  340. (defun org-table-recognize-table.el ()
  341. "If there is a table.el table nearby, recognize it and move into it."
  342. (when (org-at-table.el-p)
  343. (beginning-of-line)
  344. (unless (or (looking-at org-table-dataline-regexp)
  345. (not (looking-at org-table1-hline-regexp)))
  346. (forward-line)
  347. (when (looking-at org-table-any-border-regexp)
  348. (forward-line -2)))
  349. (if (re-search-forward "|" (org-table-end t) t)
  350. (progn
  351. (require 'table)
  352. (if (table--at-cell-p (point)) t
  353. (message "recognizing table.el table...")
  354. (table-recognize-table)
  355. (message "recognizing table.el table...done")))
  356. (error "This should not happen"))))
  357. ;; Not used since commit 6d1e3082, Feb 2010.
  358. (make-obsolete 'org-table-recognize-table.el
  359. "please notify Org mailing list if you use this function."
  360. "9.0")
  361. (defmacro org-preserve-lc (&rest body)
  362. (declare (debug (body))
  363. (obsolete "please notify Org mailing list if you use this function."
  364. "9.2"))
  365. (org-with-gensyms (line col)
  366. `(let ((,line (org-current-line))
  367. (,col (current-column)))
  368. (unwind-protect
  369. (progn ,@body)
  370. (org-goto-line ,line)
  371. (org-move-to-column ,col)))))
  372. (defun org-version-check (version &rest _)
  373. "Non-nil if VERSION is lower (older) than `emacs-version'."
  374. (declare (obsolete "use `version<' or `fboundp' instead."
  375. "9.2"))
  376. (version< version emacs-version))
  377. (defun org-remove-angle-brackets (s)
  378. (org-unbracket-string "<" ">" s))
  379. (make-obsolete 'org-remove-angle-brackets 'org-unbracket-string "9.0")
  380. (defcustom org-publish-sitemap-file-entry-format "%t"
  381. "Format string for site-map file entry.
  382. You could use brackets to delimit on what part the link will be.
  383. %t is the title.
  384. %a is the author.
  385. %d is the date formatted using `org-publish-sitemap-date-format'."
  386. :group 'org-export-publish
  387. :type 'string)
  388. (make-obsolete-variable
  389. 'org-publish-sitemap-file-entry-format
  390. "set `:sitemap-format-entry' in `org-publish-project-alist' instead."
  391. "9.1")
  392. (defvar org-agenda-skip-regexp)
  393. (defun org-agenda-skip-entry-when-regexp-matches ()
  394. "Check if the current entry contains match for `org-agenda-skip-regexp'.
  395. If yes, it returns the end position of this entry, causing agenda commands
  396. to skip the entry but continuing the search in the subtree. This is a
  397. function that can be put into `org-agenda-skip-function' for the duration
  398. of a command."
  399. (declare (obsolete "use `org-agenda-skip-if' instead." "9.1"))
  400. (let ((end (save-excursion (org-end-of-subtree t)))
  401. skip)
  402. (save-excursion
  403. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  404. (and skip end)))
  405. (defun org-agenda-skip-subtree-when-regexp-matches ()
  406. "Check if the current subtree contains match for `org-agenda-skip-regexp'.
  407. If yes, it returns the end position of this tree, causing agenda commands
  408. to skip this subtree. This is a function that can be put into
  409. `org-agenda-skip-function' for the duration of a command."
  410. (declare (obsolete "use `org-agenda-skip-if' instead." "9.1"))
  411. (let ((end (save-excursion (org-end-of-subtree t)))
  412. skip)
  413. (save-excursion
  414. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  415. (and skip end)))
  416. (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
  417. "Check if the current subtree contains match for `org-agenda-skip-regexp'.
  418. If yes, it returns the end position of the current entry (NOT the tree),
  419. causing agenda commands to skip the entry but continuing the search in
  420. the subtree. This is a function that can be put into
  421. `org-agenda-skip-function' for the duration of a command. An important
  422. use of this function is for the stuck project list."
  423. (declare (obsolete "use `org-agenda-skip-if' instead." "9.1"))
  424. (let ((end (save-excursion (org-end-of-subtree t)))
  425. (entry-end (save-excursion (outline-next-heading) (1- (point))))
  426. skip)
  427. (save-excursion
  428. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  429. (and skip entry-end)))
  430. (define-obsolete-function-alias 'org-minutes-to-clocksum-string
  431. 'org-duration-from-minutes "9.1")
  432. (define-obsolete-function-alias 'org-hh:mm-string-to-minutes
  433. 'org-duration-to-minutes "9.1")
  434. (define-obsolete-function-alias 'org-duration-string-to-minutes
  435. 'org-duration-to-minutes "9.1")
  436. (make-obsolete-variable 'org-time-clocksum-format
  437. "set `org-duration-format' instead." "9.1")
  438. (make-obsolete-variable 'org-time-clocksum-use-fractional
  439. "set `org-duration-format' instead." "9.1")
  440. (make-obsolete-variable 'org-time-clocksum-fractional-format
  441. "set `org-duration-format' instead." "9.1")
  442. (make-obsolete-variable 'org-time-clocksum-use-effort-durations
  443. "set `org-duration-units' instead." "9.1")
  444. (define-obsolete-function-alias 'org-babel-number-p
  445. 'org-babel--string-to-number "9.0")
  446. (define-obsolete-variable-alias 'org-usenet-links-prefer-google
  447. 'org-gnus-prefer-web-links "9.1")
  448. (define-obsolete-variable-alias 'org-texinfo-def-table-markup
  449. 'org-texinfo-table-default-markup "9.1")
  450. (define-obsolete-variable-alias 'org-agenda-overriding-columns-format
  451. 'org-overriding-columns-format "9.2.2")
  452. (define-obsolete-variable-alias 'org-doi-server-url
  453. 'org-link-doi-server-url "9.3")
  454. (define-obsolete-variable-alias 'org-email-link-description-format
  455. 'org-link-email-description-format "9.3")
  456. (define-obsolete-variable-alias 'org-make-link-description-function
  457. 'org-link-make-description-function "9.3")
  458. (define-obsolete-variable-alias 'org-from-is-user-regexp
  459. 'org-link-from-user-regexp "9.3")
  460. (define-obsolete-variable-alias 'org-descriptive-links
  461. 'org-link-descriptive "9.3")
  462. (define-obsolete-variable-alias 'org-context-in-file-links
  463. 'org-link-context-for-files "9.3")
  464. (define-obsolete-variable-alias 'org-keep-stored-link-after-insertion
  465. 'org-link-keep-stored-after-insertion "9.3")
  466. (define-obsolete-variable-alias 'org-display-internal-link-with-indirect-buffer
  467. 'org-link-use-indirect-buffer-for-internals "9.3")
  468. (define-obsolete-variable-alias 'org-confirm-shell-link-function
  469. 'org-link-shell-confirm-function "9.3")
  470. (define-obsolete-variable-alias 'org-confirm-shell-link-not-regexp
  471. 'org-link-shell-skip-confirm-regexp "9.3")
  472. (define-obsolete-variable-alias 'org-confirm-elisp-link-function
  473. 'org-link-elisp-confirm-function "9.3")
  474. (define-obsolete-variable-alias 'org-confirm-elisp-link-not-regexp
  475. 'org-link-elisp-skip-confirm-regexp "9.3")
  476. (define-obsolete-function-alias 'org-file-complete-link
  477. 'org-link-complete-file "9.3")
  478. (define-obsolete-function-alias 'org-email-link-description
  479. 'org-link-email-description "9.3")
  480. (define-obsolete-function-alias 'org-make-link-string
  481. 'org-link-make-string "9.3")
  482. (define-obsolete-function-alias 'org-store-link-props
  483. 'org-link-store-props "9.3")
  484. (define-obsolete-function-alias 'org-add-link-props
  485. 'org-link-add-props "9.3")
  486. (define-obsolete-function-alias 'org-make-org-heading-search-string
  487. 'org-link-heading-search-string "9.3")
  488. (define-obsolete-function-alias 'org-make-link-regexps
  489. 'org-link-make-regexps "9.3")
  490. (define-obsolete-function-alias 'org-property-global-value
  491. 'org-property-global-or-keyword-value "9.3")
  492. (make-obsolete-variable 'org-file-properties 'org-keyword-properties "9.3")
  493. (define-obsolete-variable-alias 'org-angle-link-re
  494. 'org-link-angle-re "9.3")
  495. (define-obsolete-variable-alias 'org-plain-link-re
  496. 'org-link-plain-re "9.3")
  497. (define-obsolete-variable-alias 'org-bracket-link-regexp
  498. 'org-link-bracket-re "9.3")
  499. (define-obsolete-variable-alias 'org-bracket-link-analytic-regexp
  500. 'org-link-bracket-re "9.3")
  501. (define-obsolete-variable-alias 'org-any-link-re
  502. 'org-link-any-re "9.3")
  503. (define-obsolete-function-alias 'org-open-link-from-string
  504. 'org-link-open-from-string "9.3")
  505. (define-obsolete-function-alias 'org-add-angle-brackets
  506. 'org-link-add-angle-brackets "9.3")
  507. ;; The function was made obsolete by commit 65399674d5 of 2013-02-22.
  508. ;; This make-obsolete call was added 2016-09-01.
  509. (make-obsolete 'org-capture-import-remember-templates
  510. "use the `org-capture-templates' variable instead."
  511. "9.0")
  512. (defun org-show-block-all ()
  513. "Unfold all blocks in the current buffer."
  514. (interactive)
  515. (remove-overlays nil nil 'invisible 'org-hide-block))
  516. (make-obsolete 'org-show-block-all
  517. "use `org-show-all' instead."
  518. "9.2")
  519. (define-obsolete-function-alias 'org-get-tags-at 'org-get-tags "9.2")
  520. (defun org-get-local-tags ()
  521. "Get a list of tags defined in the current headline."
  522. (declare (obsolete "use `org-get-tags' instead." "9.2"))
  523. (org-get-tags nil 'local))
  524. (defun org-get-local-tags-at (&optional pos)
  525. "Get a list of tags defined in the current headline."
  526. (declare (obsolete "use `org-get-tags' instead." "9.2"))
  527. (org-get-tags pos 'local))
  528. (defun org-get-tags-string ()
  529. "Get the TAGS string in the current headline."
  530. (declare (obsolete "use `org-make-tag-string' instead." "9.2"))
  531. (org-make-tag-string (org-get-tags nil t)))
  532. (define-obsolete-function-alias 'org-set-tags-to 'org-set-tags "9.2")
  533. (defun org-align-all-tags ()
  534. "Align the tags in all headings."
  535. (declare (obsolete "use `org-align-tags' instead." "9.2"))
  536. (org-align-tags t))
  537. (define-obsolete-function-alias
  538. 'org-at-property-block-p 'org-at-property-drawer-p "9.4")
  539. (defun org-flag-drawer (flag &optional element beg end)
  540. "When FLAG is non-nil, hide the drawer we are at.
  541. Otherwise make it visible.
  542. When optional argument ELEMENT is a parsed drawer, as returned by
  543. `org-element-at-point', hide or show that drawer instead.
  544. When buffer positions BEG and END are provided, hide or show that
  545. region as a drawer without further ado."
  546. (declare (obsolete "use `org-hide-drawer-toggle' instead." "9.4"))
  547. (if (and beg end) (org-flag-region beg end flag 'outline)
  548. (let ((drawer
  549. (or element
  550. (and (save-excursion
  551. (beginning-of-line)
  552. (looking-at-p "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"))
  553. (org-element-at-point)))))
  554. (when (memq (org-element-type drawer) '(drawer property-drawer))
  555. (let ((post (org-element-property :post-affiliated drawer)))
  556. (org-flag-region
  557. (save-excursion (goto-char post) (line-end-position))
  558. (save-excursion (goto-char (org-element-property :end drawer))
  559. (skip-chars-backward " \t\n")
  560. (line-end-position))
  561. flag 'outline)
  562. ;; When the drawer is hidden away, make sure point lies in
  563. ;; a visible part of the buffer.
  564. (when (invisible-p (max (1- (point)) (point-min)))
  565. (goto-char post)))))))
  566. (defun org-hide-block-toggle-maybe ()
  567. "Toggle visibility of block at point.
  568. Unlike to `org-hide-block-toggle', this function does not throw
  569. an error. Return a non-nil value when toggling is successful."
  570. (declare (obsolete "use `org-hide-block-toggle' instead." "9.4"))
  571. (interactive)
  572. (org-hide-block-toggle nil t))
  573. (defun org-hide-block-toggle-all ()
  574. "Toggle the visibility of all blocks in the current buffer."
  575. (declare (obsolete "please notify Org mailing list if you use this function."
  576. "9.4"))
  577. (let ((start (point-min))
  578. (end (point-max)))
  579. (save-excursion
  580. (goto-char start)
  581. (while (and (< (point) end)
  582. (re-search-forward "^[ \t]*#\\+begin_?\
  583. \\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$" end t))
  584. (save-excursion
  585. (save-match-data
  586. (goto-char (match-beginning 0))
  587. (org-hide-block-toggle)))))))
  588. (defun org-return-indent ()
  589. "Goto next table row or insert a newline and indent.
  590. Calls `org-table-next-row' or `newline-and-indent', depending on
  591. context. See the individual commands for more information."
  592. (declare (obsolete "use `org-return' with INDENT set to t instead."
  593. "9.4"))
  594. (interactive)
  595. (org-return t))
  596. (defmacro org-with-silent-modifications (&rest body)
  597. (declare (obsolete "use `with-silent-modifications' instead." "9.2")
  598. (debug (body)))
  599. `(with-silent-modifications ,@body))
  600. (define-obsolete-function-alias 'org-babel-strip-quotes
  601. 'org-strip-quotes "9.2")
  602. (define-obsolete-variable-alias 'org-sort-agenda-notime-is-late
  603. 'org-agenda-sort-notime-is-late "9.4")
  604. (define-obsolete-variable-alias 'org-sort-agenda-noeffort-is-high
  605. 'org-agenda-sort-noeffort-is-high "9.4")
  606. (defconst org-maybe-keyword-time-regexp
  607. (concat "\\(\\<\\(\\(?:CLO\\(?:CK\\|SED\\)\\|DEADLINE\\|SCHEDULED\\):\\)\\)?"
  608. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*[]>]"
  609. "\\|"
  610. "<%%([^\r\n>]*>\\)")
  611. "Matches a timestamp, possibly preceded by a keyword.")
  612. (make-obsolete-variable
  613. 'org-maybe-keyword-time-regexp
  614. "use `org-planning-line-re', followed by `org-ts-regexp-both' instead."
  615. "9.4")
  616. (define-obsolete-function-alias 'org-copy 'org-refile-copy "9.4")
  617. (define-obsolete-function-alias 'org-get-last-sibling 'org-get-previous-sibling "9.4")
  618. ;;;; Obsolete link types
  619. (eval-after-load 'ol
  620. '(progn
  621. (org-link-set-parameters "file+emacs") ;since Org 9.0
  622. (org-link-set-parameters "file+sys"))) ;since Org 9.0
  623. ;;; Miscellaneous functions
  624. (defun org-get-x-clipboard (value)
  625. "Get the value of the X or Windows clipboard."
  626. (cond ((and (eq window-system 'x)
  627. (fboundp 'gui-get-selection)) ;Silence byte-compiler.
  628. (org-no-properties
  629. (ignore-errors
  630. (or (gui-get-selection value 'UTF8_STRING)
  631. (gui-get-selection value 'COMPOUND_TEXT)
  632. (gui-get-selection value 'STRING)
  633. (gui-get-selection value 'TEXT)))))
  634. ((and (eq window-system 'w32) (fboundp 'w32-get-clipboard-data))
  635. (w32-get-clipboard-data))))
  636. ;; `set-transient-map' is only in Emacs >= 24.4
  637. (defalias 'org-set-transient-map
  638. (if (fboundp 'set-transient-map)
  639. 'set-transient-map
  640. 'set-temporary-overlay-map))
  641. ;;; Region compatibility
  642. (defvar org-ignore-region nil
  643. "Non-nil means temporarily disable the active region.")
  644. (defun org-region-active-p ()
  645. "Non-nil when the region active.
  646. Unlike to `use-region-p', this function also checks
  647. `org-ignore-region'."
  648. (and (not org-ignore-region) (use-region-p)))
  649. (defun org-cursor-to-region-beginning ()
  650. (when (and (org-region-active-p)
  651. (> (point) (region-beginning)))
  652. (exchange-point-and-mark)))
  653. ;;; Invisibility compatibility
  654. (defun org-in-invisibility-spec-p (arg)
  655. "Is ARG a member of `buffer-invisibility-spec'?"
  656. (when (consp buffer-invisibility-spec)
  657. (member arg buffer-invisibility-spec)))
  658. (defun org-move-to-column (column &optional force _buffer)
  659. "Move to column COLUMN.
  660. Pass COLUMN and FORCE to `move-to-column'."
  661. (let ((buffer-invisibility-spec
  662. (if (listp buffer-invisibility-spec)
  663. (remove '(org-filtered) buffer-invisibility-spec)
  664. buffer-invisibility-spec)))
  665. (move-to-column column force)))
  666. (defmacro org-find-library-dir (library)
  667. `(file-name-directory (or (locate-library ,library) "")))
  668. (defun org-count-lines (s)
  669. "How many lines in string S?"
  670. (let ((start 0) (n 1))
  671. (while (string-match "\n" s start)
  672. (setq start (match-end 0) n (1+ n)))
  673. (when (and (> (length s) 0) (= (aref s (1- (length s))) ?\n))
  674. (setq n (1- n)))
  675. n))
  676. (defun org-kill-new (string &rest args)
  677. (remove-text-properties 0 (length string) '(line-prefix t wrap-prefix t)
  678. string)
  679. (apply 'kill-new string args))
  680. ;; `font-lock-ensure' is only available from 24.4.50 on
  681. (defalias 'org-font-lock-ensure
  682. (if (fboundp 'font-lock-ensure)
  683. #'font-lock-ensure
  684. (lambda (&optional _beg _end)
  685. (with-no-warnings (font-lock-fontify-buffer)))))
  686. ;; `file-local-name' was added in Emacs 26.1.
  687. (defalias 'org-babel-local-file-name
  688. (if (fboundp 'file-local-name)
  689. 'file-local-name
  690. (lambda (file)
  691. "Return the local name component of FILE."
  692. (or (file-remote-p file 'localname) file))))
  693. ;;;###autoload
  694. (defmacro org-check-version ()
  695. "Try very hard to provide sensible version strings."
  696. (let* ((org-dir (org-find-library-dir "org"))
  697. (org-version.el (concat org-dir "org-version.el"))
  698. (org-fixup.el (concat org-dir "../mk/org-fixup.el")))
  699. (if (require 'org-version org-version.el 'noerror)
  700. '(progn
  701. (autoload 'org-release "org-version.el")
  702. (autoload 'org-git-version "org-version.el"))
  703. (if (require 'org-fixup org-fixup.el 'noerror)
  704. '(org-fixup)
  705. ;; provide fallback definitions and complain
  706. (warn "Could not define org version correctly. Check installation!")
  707. '(progn
  708. (defun org-release () "N/A")
  709. (defun org-git-version () "N/A !!check installation!!"))))))
  710. ;;; Functions for Emacs < 24.4 compatibility
  711. (defun org-define-error (name message)
  712. "Define NAME as a new error signal.
  713. MESSAGE is a string that will be output to the echo area if such
  714. an error is signaled without being caught by a `condition-case'.
  715. Implements `define-error' for older emacsen."
  716. (if (fboundp 'define-error) (define-error name message)
  717. (put name 'error-conditions
  718. (copy-sequence (cons name (get 'error 'error-conditions))))))
  719. (unless (fboundp 'string-suffix-p)
  720. ;; From Emacs subr.el.
  721. (defun string-suffix-p (suffix string &optional ignore-case)
  722. "Return non-nil if SUFFIX is a suffix of STRING.
  723. If IGNORE-CASE is non-nil, the comparison is done without paying
  724. attention to case differences."
  725. (let ((start-pos (- (length string) (length suffix))))
  726. (and (>= start-pos 0)
  727. (eq t (compare-strings suffix nil nil
  728. string start-pos nil ignore-case))))))
  729. ;;; Integration with and fixes for other packages
  730. (defgroup org-imenu-and-speedbar nil
  731. "Options concerning imenu and speedbar in Org mode."
  732. :tag "Org Imenu and Speedbar"
  733. :group 'org-structure)
  734. (defcustom org-imenu-depth 2
  735. "The maximum level for Imenu access to Org headlines.
  736. This also applied for speedbar access."
  737. :group 'org-imenu-and-speedbar
  738. :type 'integer)
  739. ;;;; Imenu
  740. (defvar-local org-imenu-markers nil
  741. "All markers currently used by Imenu.")
  742. (defun org-imenu-get-tree ()
  743. "Produce the index for Imenu."
  744. (dolist (x org-imenu-markers) (move-marker x nil))
  745. (setq org-imenu-markers nil)
  746. (org-with-wide-buffer
  747. (goto-char (point-max))
  748. (let* ((re (concat "^" (org-get-limited-outline-regexp)))
  749. (subs (make-vector (1+ org-imenu-depth) nil))
  750. (last-level 0))
  751. (while (re-search-backward re nil t)
  752. (let ((level (org-reduced-level (funcall outline-level)))
  753. (headline (org-no-properties
  754. (org-link-display-format (org-get-heading t t t t)))))
  755. (when (and (<= level org-imenu-depth) (org-string-nw-p headline))
  756. (let* ((m (point-marker))
  757. (item (propertize headline 'org-imenu-marker m 'org-imenu t)))
  758. (push m org-imenu-markers)
  759. (if (>= level last-level)
  760. (push (cons item m) (aref subs level))
  761. (push (cons item
  762. (cl-mapcan #'identity (cl-subseq subs (1+ level))))
  763. (aref subs level))
  764. (cl-loop for i from (1+ level) to org-imenu-depth
  765. do (aset subs i nil)))
  766. (setq last-level level)))))
  767. (aref subs 1))))
  768. (eval-after-load 'imenu
  769. '(progn
  770. (add-hook 'imenu-after-jump-hook
  771. (lambda ()
  772. (when (derived-mode-p 'org-mode)
  773. (org-show-context 'org-goto))))
  774. (add-hook 'org-mode-hook
  775. (lambda ()
  776. (setq imenu-create-index-function 'org-imenu-get-tree)))))
  777. ;;;; Speedbar
  778. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  779. "Overlay marking the agenda restriction line in speedbar.")
  780. (overlay-put org-speedbar-restriction-lock-overlay
  781. 'face 'org-agenda-restriction-lock)
  782. (overlay-put org-speedbar-restriction-lock-overlay
  783. 'help-echo "Agendas are currently limited to this item.")
  784. (delete-overlay org-speedbar-restriction-lock-overlay)
  785. (defun org-speedbar-set-agenda-restriction ()
  786. "Restrict future agenda commands to the location at point in speedbar.
  787. If there is already a restriction lock at the location, remove it.
  788. To get rid of the restriction, use `\\[org-agenda-remove-restriction-lock]'."
  789. (interactive)
  790. (require 'org-agenda)
  791. (let (p m tp np dir txt)
  792. (cond
  793. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  794. 'org-imenu t))
  795. (setq m (get-text-property p 'org-imenu-marker))
  796. (with-current-buffer (marker-buffer m)
  797. (goto-char m)
  798. (if (and org-agenda-overriding-restriction
  799. (member org-agenda-restriction-lock-overlay
  800. (overlays-at (point))))
  801. (org-agenda-remove-restriction-lock 'noupdate)
  802. (org-agenda-set-restriction-lock 'subtree))))
  803. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  804. 'speedbar-function 'speedbar-find-file))
  805. (setq tp (previous-single-property-change
  806. (1+ p) 'speedbar-function)
  807. np (next-single-property-change
  808. tp 'speedbar-function)
  809. dir (speedbar-line-directory)
  810. txt (buffer-substring-no-properties (or tp (point-min))
  811. (or np (point-max))))
  812. (with-current-buffer (find-file-noselect
  813. (let ((default-directory dir))
  814. (expand-file-name txt)))
  815. (unless (derived-mode-p 'org-mode)
  816. (user-error "Cannot restrict to non-Org mode file"))
  817. (org-agenda-set-restriction-lock 'file)))
  818. (t (user-error "Don't know how to restrict Org mode agenda")))
  819. (move-overlay org-speedbar-restriction-lock-overlay
  820. (point-at-bol) (point-at-eol))
  821. (setq current-prefix-arg nil)
  822. (org-agenda-maybe-redo)))
  823. (defvar speedbar-file-key-map)
  824. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  825. (eval-after-load 'speedbar
  826. '(progn
  827. (speedbar-add-supported-extension ".org")
  828. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  829. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  830. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  831. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  832. (add-hook 'speedbar-visiting-tag-hook
  833. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  834. ;;;; Add Log
  835. (defun org-add-log-current-headline ()
  836. "Return current headline or nil.
  837. This function ignores inlinetasks. It is meant to be used as
  838. `add-log-current-defun-function' value."
  839. (org-with-limited-levels (org-get-heading t t t t)))
  840. ;;;; Flyspell
  841. (defun org--flyspell-object-check-p (element)
  842. "Non-nil when Flyspell can check object at point.
  843. ELEMENT is the element at point."
  844. (let ((object (save-excursion
  845. (when (looking-at-p "\\>") (backward-char))
  846. (org-element-context element))))
  847. (cl-case (org-element-type object)
  848. ;; Prevent checks in links due to keybinding conflict with
  849. ;; Flyspell.
  850. ((code entity export-snippet inline-babel-call
  851. inline-src-block line-break latex-fragment link macro
  852. statistics-cookie target timestamp verbatim)
  853. nil)
  854. (footnote-reference
  855. ;; Only in inline footnotes, within the definition.
  856. (and (eq (org-element-property :type object) 'inline)
  857. (< (save-excursion
  858. (goto-char (org-element-property :begin object))
  859. (search-forward ":" nil t 2))
  860. (point))))
  861. (otherwise t))))
  862. (defun org-mode-flyspell-verify ()
  863. "Function used for `flyspell-generic-check-word-predicate'."
  864. (if (org-at-heading-p)
  865. ;; At a headline or an inlinetask, check title only.
  866. (and (save-excursion (beginning-of-line)
  867. (and (let ((case-fold-search t))
  868. (not (looking-at-p "\\*+ END[ \t]*$")))
  869. (let ((case-fold-search nil))
  870. (looking-at org-complex-heading-regexp))))
  871. (match-beginning 4)
  872. (>= (point) (match-beginning 4))
  873. (or (not (match-beginning 5))
  874. (< (point) (match-beginning 5)))
  875. ;; Ignore checks in code, verbatim and others.
  876. (org--flyspell-object-check-p (org-element-at-point-no-context)))
  877. (let* ((element (org-element-at-point-no-context))
  878. (post-affiliated (org-element-property :post-affiliated element)))
  879. (cond
  880. ;; Ignore checks in all affiliated keywords but captions.
  881. ((< (point) post-affiliated)
  882. (and (save-excursion
  883. (beginning-of-line)
  884. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  885. (> (point) (match-end 0))
  886. (org--flyspell-object-check-p element)))
  887. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  888. ((let ((log (org-log-into-drawer)))
  889. (and log
  890. (let ((drawer (org-element-lineage element '(drawer))))
  891. (and drawer
  892. (eq (compare-strings
  893. log nil nil
  894. (org-element-property :drawer-name drawer) nil nil t)
  895. t)))))
  896. nil)
  897. (t
  898. (cl-case (org-element-type element)
  899. ((comment quote-section) t)
  900. (comment-block
  901. ;; Allow checks between block markers, not on them.
  902. (and (> (line-beginning-position) post-affiliated)
  903. (save-excursion
  904. (end-of-line)
  905. (skip-chars-forward " \r\t\n")
  906. (< (point) (org-element-property :end element)))))
  907. ;; Arbitrary list of keywords where checks are meaningful.
  908. ;; Make sure point is on the value part of the element.
  909. (keyword
  910. (and (member (org-element-property :key element)
  911. '("DESCRIPTION" "TITLE"))
  912. (save-excursion
  913. (search-backward ":" (line-beginning-position) t))))
  914. ;; Check is globally allowed in paragraphs verse blocks and
  915. ;; table rows (after affiliated keywords) but some objects
  916. ;; must not be affected.
  917. ((paragraph table-row verse-block)
  918. (let ((cbeg (org-element-property :contents-begin element))
  919. (cend (org-element-property :contents-end element)))
  920. (and cbeg (>= (point) cbeg) (< (point) cend)
  921. (org--flyspell-object-check-p element))))))))))
  922. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  923. (defun org-remove-flyspell-overlays-in (beg end)
  924. "Remove flyspell overlays in region."
  925. (and (bound-and-true-p flyspell-mode)
  926. (fboundp 'flyspell-delete-region-overlays)
  927. (flyspell-delete-region-overlays beg end)))
  928. (defvar flyspell-delayed-commands)
  929. (eval-after-load 'flyspell
  930. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  931. ;;;; Bookmark
  932. (defun org-bookmark-jump-unhide ()
  933. "Unhide the current position, to show the bookmark location."
  934. (and (derived-mode-p 'org-mode)
  935. (or (org-invisible-p)
  936. (save-excursion (goto-char (max (point-min) (1- (point))))
  937. (org-invisible-p)))
  938. (org-show-context 'bookmark-jump)))
  939. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  940. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  941. ;;;; Calendar
  942. (defcustom org-calendar-to-agenda-key 'default
  943. "Key to be installed in `calendar-mode-map' for switching to the agenda.
  944. The command `org-calendar-goto-agenda' will be bound to this key.
  945. When set to `default', bind the function to `c', but only if it is
  946. available in the Calendar keymap. This is the default choice because
  947. `c' can then be used to switch back and forth between agenda and calendar.
  948. When nil, `org-calendar-goto-agenda' is not bound to any key."
  949. :group 'org-agenda
  950. :type '(choice
  951. (const :tag "Bind to `c' if available" default)
  952. (key-sequence :tag "Other binding")
  953. (const :tag "No binding" nil))
  954. :safe (lambda (v) (or (symbolp v) (stringp v)))
  955. :package-version '(Org . "9.2"))
  956. (defcustom org-calendar-insert-diary-entry-key [?i]
  957. "The key to be installed in `calendar-mode-map' for adding diary entries.
  958. This option is irrelevant until `org-agenda-diary-file' has been configured
  959. to point to an Org file. When that is the case, the command
  960. `org-agenda-diary-entry' will be bound to the key given here, by default
  961. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  962. if you want to continue doing this, you need to change this to a different
  963. key."
  964. :group 'org-agenda
  965. :type 'sexp)
  966. (defun org--setup-calendar-bindings ()
  967. "Bind Org functions in Calendar keymap."
  968. (pcase org-calendar-to-agenda-key
  969. (`nil nil)
  970. ((and key (pred stringp))
  971. (local-set-key (kbd key) #'org-calendar-goto-agenda))
  972. ((guard (not (lookup-key calendar-mode-map "c")))
  973. (local-set-key "c" #'org-calendar-goto-agenda))
  974. (_ nil))
  975. (when (and (boundp 'org-agenda-diary-file)
  976. (not (eq org-agenda-diary-file 'diary-file)))
  977. (local-set-key org-calendar-insert-diary-entry-key
  978. #'org-agenda-diary-entry)))
  979. (eval-after-load 'calendar
  980. '(add-hook 'calendar-mode-hook #'org--setup-calendar-bindings))
  981. ;;;; Saveplace
  982. ;; Make sure saveplace shows the location if it was hidden
  983. (eval-after-load 'saveplace
  984. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  985. "Make the position visible."
  986. (org-bookmark-jump-unhide)))
  987. ;;;; Ecb
  988. ;; Make sure ecb shows the location if it was hidden
  989. (eval-after-load 'ecb
  990. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  991. "Make hierarchy visible when jumping into location from ECB tree buffer."
  992. (when (derived-mode-p 'org-mode)
  993. (org-show-context))))
  994. ;;;; Simple
  995. (defun org-mark-jump-unhide ()
  996. "Make the point visible with `org-show-context' after jumping to the mark."
  997. (when (and (derived-mode-p 'org-mode)
  998. (org-invisible-p))
  999. (org-show-context 'mark-goto)))
  1000. (eval-after-load 'simple
  1001. '(defadvice pop-to-mark-command (after org-make-visible activate)
  1002. "Make the point visible with `org-show-context'."
  1003. (org-mark-jump-unhide)))
  1004. (eval-after-load 'simple
  1005. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  1006. "Make the point visible with `org-show-context'."
  1007. (org-mark-jump-unhide)))
  1008. (eval-after-load 'simple
  1009. '(defadvice pop-global-mark (after org-make-visible activate)
  1010. "Make the point visible with `org-show-context'."
  1011. (org-mark-jump-unhide)))
  1012. ;;;; Session
  1013. ;; Make "session.el" ignore our circular variable.
  1014. (defvar session-globals-exclude)
  1015. (eval-after-load 'session
  1016. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  1017. ;;;; Speed commands
  1018. (make-obsolete-variable 'org-speed-commands-user
  1019. "configure `org-speed-commands' instead." "9.5")
  1020. (provide 'org-compat)
  1021. ;; Local variables:
  1022. ;; generated-autoload-file: "org-loaddefs.el"
  1023. ;; End:
  1024. ;;; org-compat.el ends here