org-compat.el 39 KB

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