Move `org-unlogged-message' to "org-macs.el"
[org-mode.git] / lisp / org-macs.el
blob5fe322b52918a8e3a6bc648be7877067b0eee6f8
1 ;;; org-macs.el --- Top-level Definitions for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://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 macro definitions, defsubst definitions, other
28 ;; stuff needed for compilation and top-level forms in Org mode, as
29 ;; well lots of small functions that are not Org mode specific but
30 ;; simply generally useful stuff.
32 ;;; Code:
34 (declare-function org-defkey "org" (keymap key def))
38 ;;; Macros
40 (defmacro org-with-gensyms (symbols &rest body)
41 (declare (debug (sexp body)) (indent 1))
42 `(let ,(mapcar (lambda (s)
43 `(,s (make-symbol (concat "--" (symbol-name ',s)))))
44 symbols)
45 ,@body))
47 (defmacro org-preserve-lc (&rest body)
48 (declare (debug (body)))
49 (org-with-gensyms (line col)
50 `(let ((,line (org-current-line))
51 (,col (current-column)))
52 (unwind-protect
53 (progn ,@body)
54 (org-goto-line ,line)
55 (org-move-to-column ,col)))))
57 ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
58 ;; `org-unmodified' to ignore real text modifications
59 (defmacro org-unmodified (&rest body)
60 "Run BODY while preserving the buffer's `buffer-modified-p' state."
61 (declare (debug (body)))
62 (org-with-gensyms (was-modified)
63 `(let ((,was-modified (buffer-modified-p)))
64 (unwind-protect
65 (let ((buffer-undo-list t)
66 (inhibit-modification-hooks t))
67 ,@body)
68 (set-buffer-modified-p ,was-modified)))))
70 (defmacro org-without-partial-completion (&rest body)
71 (declare (debug (body)))
72 `(if (and (boundp 'partial-completion-mode)
73 partial-completion-mode
74 (fboundp 'partial-completion-mode))
75 (unwind-protect
76 (progn
77 (partial-completion-mode -1)
78 ,@body)
79 (partial-completion-mode 1))
80 ,@body))
82 (defmacro org-with-point-at (pom &rest body)
83 "Move to buffer and point of point-or-marker POM for the duration of BODY."
84 (declare (debug (form body)) (indent 1))
85 (org-with-gensyms (mpom)
86 `(let ((,mpom ,pom))
87 (save-excursion
88 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
89 (org-with-wide-buffer
90 (goto-char (or ,mpom (point)))
91 ,@body)))))
93 (defmacro org-with-remote-undo (buffer &rest body)
94 "Execute BODY while recording undo information in two buffers."
95 (declare (debug (form body)) (indent 1))
96 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
97 `(let ((,cline (org-current-line))
98 (,cmd this-command)
99 (,buf1 (current-buffer))
100 (,buf2 ,buffer)
101 (,undo1 buffer-undo-list)
102 (,undo2 (with-current-buffer ,buffer buffer-undo-list))
103 ,c1 ,c2)
104 ,@body
105 (when org-agenda-allow-remote-undo
106 (setq ,c1 (org-verify-change-for-undo
107 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
108 ,c2 (org-verify-change-for-undo
109 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
110 (when (or ,c1 ,c2)
111 ;; make sure there are undo boundaries
112 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
113 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
114 ;; remember which buffer to undo
115 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
116 org-agenda-undo-list))))))
118 (defmacro org-no-read-only (&rest body)
119 "Inhibit read-only for BODY."
120 (declare (debug (body)))
121 `(let ((inhibit-read-only t)) ,@body))
123 (defmacro org-save-outline-visibility (use-markers &rest body)
124 "Save and restore outline visibility around BODY.
125 If USE-MARKERS is non-nil, use markers for the positions.
126 This means that the buffer may change while running BODY,
127 but it also means that the buffer should stay alive
128 during the operation, because otherwise all these markers will
129 point nowhere."
130 (declare (debug (form body)) (indent 1))
131 (org-with-gensyms (data)
132 `(let ((,data (org-outline-overlay-data ,use-markers)))
133 (unwind-protect
134 (prog1 (progn ,@body)
135 (org-set-outline-overlay-data ,data))
136 (when ,use-markers
137 (dolist (c ,data)
138 (when (markerp (car c)) (move-marker (car c) nil))
139 (when (markerp (cdr c)) (move-marker (cdr c) nil))))))))
141 (defmacro org-with-wide-buffer (&rest body)
142 "Execute body while temporarily widening the buffer."
143 (declare (debug (body)))
144 `(save-excursion
145 (save-restriction
146 (widen)
147 ,@body)))
149 (defmacro org-with-limited-levels (&rest body)
150 "Execute BODY with limited number of outline levels."
151 (declare (debug (body)))
152 `(progn
153 (defvar org-called-with-limited-levels)
154 (defvar org-outline-regexp)
155 (defvar outline-regexp)
156 (defvar org-outline-regexp-bol)
157 (let* ((org-called-with-limited-levels t)
158 (org-outline-regexp (org-get-limited-outline-regexp))
159 (outline-regexp org-outline-regexp)
160 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
161 ,@body)))
163 (defmacro org-eval-in-environment (environment form)
164 (declare (debug (form form)) (indent 1))
165 `(eval (list 'let ,environment ',form)))
167 ;;;###autoload
168 (defmacro org-load-noerror-mustsuffix (file)
169 "Load FILE with optional arguments NOERROR and MUSTSUFFIX."
170 `(load ,file 'noerror nil nil 'mustsuffix))
174 ;;; Buffer
176 (defun org-base-buffer (buffer)
177 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
178 (if (not buffer)
179 buffer
180 (or (buffer-base-buffer buffer)
181 buffer)))
183 (defun org-find-base-buffer-visiting (file)
184 "Like `find-buffer-visiting' but always return the base buffer and
185 not an indirect buffer."
186 (let ((buf (or (get-file-buffer file)
187 (find-buffer-visiting file))))
188 (if buf
189 (or (buffer-base-buffer buf) buf)
190 nil)))
194 ;;; Input
196 (defun org-read-function (prompt &optional allow-empty?)
197 "Prompt for a function.
198 If ALLOW-EMPTY? is non-nil, return nil rather than raising an
199 error when the user input is empty."
200 (let ((func (completing-read prompt obarray #'fboundp t)))
201 (cond ((not (string= func ""))
202 (intern func))
203 (allow-empty? nil)
204 (t (user-error "Empty input is not valid")))))
206 (defun org-completing-read (&rest args)
207 "Completing-read with SPACE being a normal character."
208 (let ((enable-recursive-minibuffers t)
209 (minibuffer-local-completion-map
210 (copy-keymap minibuffer-local-completion-map)))
211 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
212 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
213 (org-defkey minibuffer-local-completion-map (kbd "C-c !")
214 'org-time-stamp-inactive)
215 (apply #'completing-read args)))
219 ;;; Logic
221 (defsubst org-xor (a b)
222 "Exclusive `or'."
223 (if a (not b) b))
227 ;;; Overlays
229 (defun org-overlay-display (ovl text &optional face evap)
230 "Make overlay OVL display TEXT with face FACE."
231 (overlay-put ovl 'display text)
232 (if face (overlay-put ovl 'face face))
233 (if evap (overlay-put ovl 'evaporate t)))
235 (defun org-overlay-before-string (ovl text &optional face evap)
236 "Make overlay OVL display TEXT with face FACE."
237 (if face (org-add-props text nil 'face face))
238 (overlay-put ovl 'before-string text)
239 (if evap (overlay-put ovl 'evaporate t)))
241 (defun org-find-overlays (prop &optional pos delete)
242 "Find all overlays specifying PROP at POS or point.
243 If DELETE is non-nil, delete all those overlays."
244 (let (found)
245 (dolist (ov (overlays-at (or pos (point))) found)
246 (cond ((not (overlay-get ov prop)))
247 (delete (delete-overlay ov))
248 (t (push ov found))))))
252 ;;; String manipulation
254 (defsubst org-trim (s &optional keep-lead)
255 "Remove whitespace at the beginning and the end of string S.
256 When optional argument KEEP-LEAD is non-nil, removing blank lines
257 at the beginning of the string does not affect leading indentation."
258 (replace-regexp-in-string
259 (if keep-lead "\\`\\([ \t]*\n\\)+" "\\`[ \t\n\r]+") ""
260 (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
262 (defun org-string-nw-p (s)
263 "Return S if S is a string containing a non-blank character.
264 Otherwise, return nil."
265 (and (stringp s)
266 (string-match-p "[^ \r\t\n]" s)
269 (defun org-reverse-string (string)
270 "Return the reverse of STRING."
271 (apply #'string (nreverse (string-to-list string))))
273 (defun org-split-string (string &optional separators)
274 "Splits STRING into substrings at SEPARATORS.
276 SEPARATORS is a regular expression. When nil, it defaults to
277 \"[ \f\t\n\r\v]+\".
279 Unlike `split-string', matching SEPARATORS at the beginning and
280 end of string are ignored."
281 (let ((separators (or separators "[ \f\t\n\r\v]+")))
282 (when (string-match (concat "\\`" separators) string)
283 (setq string (replace-match "" nil nil string)))
284 (when (string-match (concat separators "\\'") string)
285 (setq string (replace-match "" nil nil string)))
286 (split-string string separators)))
288 (defun org-string-display (string)
289 "Return STRING as it is displayed in the current buffer.
290 This function takes into consideration `invisible' and `display'
291 text properties."
292 (let* ((build-from-parts
293 (lambda (s property filter)
294 ;; Build a new string out of string S. On every group of
295 ;; contiguous characters with the same PROPERTY value,
296 ;; call FILTER on the properties list at the beginning of
297 ;; the group. If it returns a string, replace the
298 ;; characters in the group with it. Otherwise, preserve
299 ;; those characters.
300 (let ((len (length s))
301 (new "")
302 (i 0)
303 (cursor 0))
304 (while (setq i (text-property-not-all i len property nil s))
305 (let ((end (next-single-property-change i property s len))
306 (value (funcall filter (text-properties-at i s))))
307 (when value
308 (setq new (concat new (substring s cursor i) value))
309 (setq cursor end))
310 (setq i end)))
311 (concat new (substring s cursor)))))
312 (prune-invisible
313 (lambda (s)
314 (funcall build-from-parts s 'invisible
315 (lambda (props)
316 ;; If `invisible' property in PROPS means text
317 ;; is to be invisible, return the empty string.
318 ;; Otherwise return nil so that the part is
319 ;; skipped.
320 (and (or (eq t buffer-invisibility-spec)
321 (assoc-string (plist-get props 'invisible)
322 buffer-invisibility-spec))
323 "")))))
324 (replace-display
325 (lambda (s)
326 (funcall build-from-parts s 'display
327 (lambda (props)
328 ;; If there is any string specification in
329 ;; `display' property return it. Also attach
330 ;; other text properties on the part to that
331 ;; string (face...).
332 (let* ((display (plist-get props 'display))
333 (value (if (stringp display) display
334 (cl-some #'stringp display))))
335 (when value
336 (apply #'propertize
337 ;; Displayed string could contain
338 ;; invisible parts, but no nested
339 ;; display.
340 (funcall prune-invisible value)
341 'display
342 (and (not (stringp display))
343 (cl-remove-if #'stringp display))
344 props))))))))
345 ;; `display' property overrides `invisible' one. So we first
346 ;; replace characters with `display' property. Then we remove
347 ;; invisible characters.
348 (funcall prune-invisible (funcall replace-display string))))
350 (defun org-string-width (string)
351 "Return width of STRING when displayed in the current buffer.
352 Unlike `string-width', this function takes into consideration
353 `invisible' and `display' text properties."
354 (string-width (org-string-display string)))
356 (defun org-not-nil (v)
357 "If V not nil, and also not the string \"nil\", then return V.
358 Otherwise return nil."
359 (and v (not (equal v "nil")) v))
361 (defun org-unbracket-string (pre post string)
362 "Remove PRE/POST from the beginning/end of STRING.
363 Both PRE and POST must be pre-/suffixes of STRING, or neither is
364 removed."
365 (if (and (string-prefix-p pre string)
366 (string-suffix-p post string))
367 (substring string (length pre) (- (length post)))
368 string))
370 (defsubst org-current-line-string (&optional to-here)
371 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
373 (defun org-shorten-string (s maxlength)
374 "Shorten string S so that it is no longer than MAXLENGTH characters.
375 If the string is shorter or has length MAXLENGTH, just return the
376 original string. If it is longer, the functions finds a space in the
377 string, breaks this string off at that locations and adds three dots
378 as ellipsis. Including the ellipsis, the string will not be longer
379 than MAXLENGTH. If finding a good breaking point in the string does
380 not work, the string is just chopped off in the middle of a word
381 if necessary."
382 (if (<= (length s) maxlength)
384 (let* ((n (max (- maxlength 4) 1))
385 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
386 (if (string-match re s)
387 (concat (match-string 1 s) "...")
388 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
390 (defun org-remove-tabs (s &optional width)
391 "Replace tabulators in S with spaces.
392 Assumes that s is a single line, starting in column 0."
393 (setq width (or width tab-width))
394 (while (string-match "\t" s)
395 (setq s (replace-match
396 (make-string
397 (- (* width (/ (+ (match-beginning 0) width) width))
398 (match-beginning 0)) ?\ )
399 t t s)))
402 (defun org-wrap (string &optional width lines)
403 "Wrap string to either a number of lines, or a width in characters.
404 If WIDTH is non-nil, the string is wrapped to that width, however many lines
405 that costs. If there is a word longer than WIDTH, the text is actually
406 wrapped to the length of that word.
407 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
408 many lines, whatever width that takes.
409 The return value is a list of lines, without newlines at the end."
410 (let* ((words (split-string string))
411 (maxword (apply 'max (mapcar 'org-string-width words)))
412 w ll)
413 (cond (width
414 (org--do-wrap words (max maxword width)))
415 (lines
416 (setq w maxword)
417 (setq ll (org--do-wrap words maxword))
418 (if (<= (length ll) lines)
420 (setq ll words)
421 (while (> (length ll) lines)
422 (setq w (1+ w))
423 (setq ll (org--do-wrap words w)))
424 ll))
425 (t (error "Cannot wrap this")))))
427 (defun org--do-wrap (words width)
428 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
429 (let (lines line)
430 (while words
431 (setq line (pop words))
432 (while (and words (< (+ (length line) (length (car words))) width))
433 (setq line (concat line " " (pop words))))
434 (setq lines (push line lines)))
435 (nreverse lines)))
437 (defun org-remove-indentation (code &optional n)
438 "Remove maximum common indentation in string CODE and return it.
439 N may optionally be the number of columns to remove. Return CODE
440 as-is if removal failed."
441 (with-temp-buffer
442 (insert code)
443 (if (org-do-remove-indentation n) (buffer-string) code)))
445 (defun org-do-remove-indentation (&optional n)
446 "Remove the maximum common indentation from the buffer.
447 When optional argument N is a positive integer, remove exactly
448 that much characters from indentation, if possible. Return nil
449 if it fails."
450 (catch :exit
451 (goto-char (point-min))
452 ;; Find maximum common indentation, if not specified.
453 (let ((n (or n
454 (let ((min-ind (point-max)))
455 (save-excursion
456 (while (re-search-forward "^[ \t]*\\S-" nil t)
457 (let ((ind (1- (current-column))))
458 (if (zerop ind) (throw :exit nil)
459 (setq min-ind (min min-ind ind))))))
460 min-ind))))
461 (if (zerop n) (throw :exit nil)
462 ;; Remove exactly N indentation, but give up if not possible.
463 (while (not (eobp))
464 (let ((ind (progn (skip-chars-forward " \t") (current-column))))
465 (cond ((eolp) (delete-region (line-beginning-position) (point)))
466 ((< ind n) (throw :exit nil))
467 (t (indent-line-to (- ind n))))
468 (forward-line)))
469 ;; Signal success.
470 t))))
474 ;;; List manipulation
476 (defsubst org-get-alist-option (option key)
477 (cond ((eq key t) t)
478 ((eq option t) t)
479 ((assoc key option) (cdr (assoc key option)))
480 (t (let ((r (cdr (assq 'default option))))
481 (if (listp r) (delq nil r) r)))))
483 (defsubst org-last (list)
484 "Return the last element of LIST."
485 (car (last list)))
487 (defsubst org-uniquify (list)
488 "Non-destructively remove duplicate elements from LIST."
489 (let ((res (copy-sequence list))) (delete-dups res)))
491 (defun org-uniquify-alist (alist)
492 "Merge elements of ALIST with the same key.
494 For example, in this alist:
496 \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
497 => \\='((a 1 3) (b 2))
499 merge (a 1) and (a 3) into (a 1 3).
501 The function returns the new ALIST."
502 (let (rtn)
503 (dolist (e alist rtn)
504 (let (n)
505 (if (not (assoc (car e) rtn))
506 (push e rtn)
507 (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
508 (setq rtn (assq-delete-all (car e) rtn))
509 (push n rtn))))))
511 (defun org-delete-all (elts list)
512 "Remove all elements in ELTS from LIST.
513 Comparison is done with `equal'. It is a destructive operation
514 that may remove elements by altering the list structure."
515 (while elts
516 (setq list (delete (pop elts) list)))
517 list)
519 (defun org-plist-delete (plist property)
520 "Delete PROPERTY from PLIST.
521 This is in contrast to merely setting it to 0."
522 (let (p)
523 (while plist
524 (if (not (eq property (car plist)))
525 (setq p (plist-put p (car plist) (nth 1 plist))))
526 (setq plist (cddr plist)))
529 (defun org-combine-plists (&rest plists)
530 "Create a single property list from all plists in PLISTS.
531 The process starts by copying the first list, and then setting properties
532 from the other lists. Settings in the last list are the most significant
533 ones and overrule settings in the other lists."
534 (let ((rtn (copy-sequence (pop plists)))
535 p v ls)
536 (while plists
537 (setq ls (pop plists))
538 (while ls
539 (setq p (pop ls) v (pop ls))
540 (setq rtn (plist-put rtn p v))))
541 rtn))
545 ;;; Regexp matching
547 (defsubst org-pos-in-match-range (pos n)
548 (and (match-beginning n)
549 (<= (match-beginning n) pos)
550 (>= (match-end n) pos)))
552 (defun org-skip-whitespace ()
553 "Skip over space, tabs and newline characters."
554 (skip-chars-forward " \t\n\r"))
556 (defun org-match-line (regexp)
557 "Match REGEXP at the beginning of the current line."
558 (save-excursion
559 (beginning-of-line)
560 (looking-at regexp)))
562 (defun org-in-regexp (regexp &optional nlines visually)
563 "Check if point is inside a match of REGEXP.
565 Normally only the current line is checked, but you can include
566 NLINES extra lines around point into the search. If VISUALLY is
567 set, require that the cursor is not after the match but really
568 on, so that the block visually is on the match.
570 Return nil or a cons cell (BEG . END) where BEG and END are,
571 respectively, the positions at the beginning and the end of the
572 match."
573 (catch :exit
574 (let ((pos (point))
575 (eol (line-end-position (if nlines (1+ nlines) 1))))
576 (save-excursion
577 (beginning-of-line (- 1 (or nlines 0)))
578 (while (and (re-search-forward regexp eol t)
579 (<= (match-beginning 0) pos))
580 (let ((end (match-end 0)))
581 (when (or (> end pos) (and (= end pos) (not visually)))
582 (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
584 (defun org-point-in-group (point group &optional context)
585 "Check if POINT is in match-group GROUP.
586 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
587 match. If the match group does not exist or point is not inside it,
588 return nil."
589 (and (match-beginning group)
590 (>= point (match-beginning group))
591 (<= point (match-end group))
592 (if context
593 (list context (match-beginning group) (match-end group))
594 t)))
598 ;;; Motion
600 (defsubst org-goto-line (N)
601 (save-restriction
602 (widen)
603 (goto-char (point-min))
604 (forward-line (1- N))))
606 (defsubst org-current-line (&optional pos)
607 (save-excursion
608 (and pos (goto-char pos))
609 ;; works also in narrowed buffer, because we start at 1, not point-min
610 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
614 ;;; Text properties
616 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
617 rear-nonsticky t mouse-map t fontified t
618 org-emphasis t)
619 "Properties to remove when a string without properties is wanted.")
621 (defsubst org-no-properties (s &optional restricted)
622 "Remove all text properties from string S.
623 When RESTRICTED is non-nil, only remove the properties listed
624 in `org-rm-props'."
625 (if restricted (remove-text-properties 0 (length s) org-rm-props s)
626 (set-text-properties 0 (length s) nil s))
628 (defun org-add-props (string plist &rest props)
629 "Add text properties to entire string, from beginning to end.
630 PLIST may be a list of properties, PROPS are individual properties and values
631 that will be added to PLIST. Returns the string that was modified."
632 (declare (indent 2))
633 (add-text-properties
634 0 (length string) (if props (append plist props) plist) string)
635 string)
637 (defun org-make-parameter-alist (flat)
638 "Return alist based on FLAT.
639 FLAT is a list with alternating symbol names and values. The
640 returned alist is a list of lists with the symbol name in car and
641 the value in cdr."
642 (when flat
643 (cons (list (car flat) (cadr flat))
644 (org-make-parameter-alist (cddr flat)))))
646 (defsubst org-get-at-bol (property)
647 "Get text property PROPERTY at the beginning of line."
648 (get-text-property (point-at-bol) property))
650 (defun org-get-at-eol (property n)
651 "Get text property PROPERTY at the end of line less N characters."
652 (get-text-property (- (point-at-eol) n) property))
654 (defun org-find-text-property-in-string (prop s)
655 "Return the first non-nil value of property PROP in string S."
656 (or (get-text-property 0 prop s)
657 (get-text-property (or (next-single-property-change 0 prop s) 0)
658 prop s)))
660 (defun org-invisible-p (&optional pos)
661 "Non-nil if the character after POS is invisible.
662 If POS is nil, use `point' instead."
663 (get-char-property (or pos (point)) 'invisible))
665 (defun org-truely-invisible-p ()
666 "Check if point is at a character currently not visible.
667 This version does not only check the character property, but also
668 `visible-mode'."
669 (unless (bound-and-true-p visible-mode)
670 (org-invisible-p)))
672 (defun org-invisible-p2 ()
673 "Check if point is at a character currently not visible.
674 If the point is at EOL (and not at the beginning of a buffer too),
675 move it back by one char before doing this check."
676 (save-excursion
677 (when (and (eolp) (not (bobp)))
678 (backward-char 1))
679 (org-invisible-p)))
683 ;;; Local variables
685 (defconst org-unique-local-variables
686 '(org-element--cache
687 org-element--cache-objects
688 org-element--cache-sync-keys
689 org-element--cache-sync-requests
690 org-element--cache-sync-timer)
691 "List of local variables that cannot be transferred to another buffer.")
693 (defun org-get-local-variables ()
694 "Return a list of all local variables in an Org mode buffer."
695 (delq nil
696 (mapcar
697 (lambda (x)
698 (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
699 (name (car binding)))
700 (and (not (get name 'org-state))
701 (not (memq name org-unique-local-variables))
702 (string-match-p
703 "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
704 auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
705 (symbol-name name))
706 binding)))
707 (with-temp-buffer
708 (org-mode)
709 (buffer-local-variables)))))
711 (defun org-clone-local-variables (from-buffer &optional regexp)
712 "Clone local variables from FROM-BUFFER.
713 Optional argument REGEXP selects variables to clone."
714 (dolist (pair (buffer-local-variables from-buffer))
715 (pcase pair
716 (`(,name . ,value) ;ignore unbound variables
717 (when (and (not (memq name org-unique-local-variables))
718 (or (null regexp) (string-match-p regexp (symbol-name name))))
719 (ignore-errors (set (make-local-variable name) value)))))))
723 ;;; Miscellaneous
725 (defsubst org-call-with-arg (command arg)
726 "Call COMMAND interactively, but pretend prefix arg was ARG."
727 (let ((current-prefix-arg arg)) (call-interactively command)))
729 (defsubst org-check-external-command (cmd &optional use no-error)
730 "Check if external program CMD for USE exists, error if not.
731 When the program does exist, return its path.
732 When it does not exist and NO-ERROR is set, return nil.
733 Otherwise, throw an error. The optional argument USE can describe what this
734 program is needed for, so that the error message can be more informative."
735 (or (executable-find cmd)
736 (if no-error
738 (error "Can't find `%s'%s" cmd
739 (if use (format " (%s)" use) "")))))
741 (defun org-display-warning (message)
742 "Display the given MESSAGE as a warning."
743 (display-warning 'org message :warning))
745 (defun org-unlogged-message (&rest args)
746 "Display a message, but avoid logging it in the *Messages* buffer."
747 (let ((message-log-max nil))
748 (apply #'message args)))
750 (defun org-let (list &rest body)
751 (eval (cons 'let (cons list body))))
752 (put 'org-let 'lisp-indent-function 1)
754 (defun org-let2 (list1 list2 &rest body)
755 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
756 (put 'org-let2 'lisp-indent-function 2)
758 (defun org-eval (form)
759 "Eval FORM and return result."
760 (condition-case error
761 (eval form)
762 (error (format "%%![Error: %s]" error))))
764 (defvar org-outline-regexp) ; defined in org.el
765 (defvar org-odd-levels-only) ; defined in org.el
766 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
767 (defun org-get-limited-outline-regexp ()
768 "Return outline-regexp with limited number of levels.
769 The number of levels is controlled by `org-inlinetask-min-level'"
770 (cond ((not (derived-mode-p 'org-mode))
771 outline-regexp)
772 ((not (featurep 'org-inlinetask))
773 org-outline-regexp)
775 (let* ((limit-level (1- org-inlinetask-min-level))
776 (nstars (if org-odd-levels-only
777 (1- (* limit-level 2))
778 limit-level)))
779 (format "\\*\\{1,%d\\} " nstars)))))
782 (provide 'org-macs)
784 ;;; org-macs.el ends here