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