org-compat.el 37 KB

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