org-compat.el 47 KB

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