org-compat.el 49 KB

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