Move `org-get-at-bol' into "org-macs.el"
[org-mode/org-tableheadings.git] / lisp / org-macs.el
blobd0005d7b676e83d7588dcbd125a421c09d8469ed
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:
35 ;;; Macros
37 (defmacro org-with-gensyms (symbols &rest body)
38 (declare (debug (sexp body)) (indent 1))
39 `(let ,(mapcar (lambda (s)
40 `(,s (make-symbol (concat "--" (symbol-name ',s)))))
41 symbols)
42 ,@body))
44 (defmacro org-preserve-lc (&rest body)
45 (declare (debug (body)))
46 (org-with-gensyms (line col)
47 `(let ((,line (org-current-line))
48 (,col (current-column)))
49 (unwind-protect
50 (progn ,@body)
51 (org-goto-line ,line)
52 (org-move-to-column ,col)))))
54 ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
55 ;; `org-unmodified' to ignore real text modifications
56 (defmacro org-unmodified (&rest body)
57 "Run BODY while preserving the buffer's `buffer-modified-p' state."
58 (declare (debug (body)))
59 (org-with-gensyms (was-modified)
60 `(let ((,was-modified (buffer-modified-p)))
61 (unwind-protect
62 (let ((buffer-undo-list t)
63 (inhibit-modification-hooks t))
64 ,@body)
65 (set-buffer-modified-p ,was-modified)))))
67 (defmacro org-without-partial-completion (&rest body)
68 (declare (debug (body)))
69 `(if (and (boundp 'partial-completion-mode)
70 partial-completion-mode
71 (fboundp 'partial-completion-mode))
72 (unwind-protect
73 (progn
74 (partial-completion-mode -1)
75 ,@body)
76 (partial-completion-mode 1))
77 ,@body))
79 (defmacro org-with-point-at (pom &rest body)
80 "Move to buffer and point of point-or-marker POM for the duration of BODY."
81 (declare (debug (form body)) (indent 1))
82 (org-with-gensyms (mpom)
83 `(let ((,mpom ,pom))
84 (save-excursion
85 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
86 (org-with-wide-buffer
87 (goto-char (or ,mpom (point)))
88 ,@body)))))
90 (defmacro org-with-remote-undo (buffer &rest body)
91 "Execute BODY while recording undo information in two buffers."
92 (declare (debug (form body)) (indent 1))
93 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
94 `(let ((,cline (org-current-line))
95 (,cmd this-command)
96 (,buf1 (current-buffer))
97 (,buf2 ,buffer)
98 (,undo1 buffer-undo-list)
99 (,undo2 (with-current-buffer ,buffer buffer-undo-list))
100 ,c1 ,c2)
101 ,@body
102 (when org-agenda-allow-remote-undo
103 (setq ,c1 (org-verify-change-for-undo
104 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
105 ,c2 (org-verify-change-for-undo
106 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
107 (when (or ,c1 ,c2)
108 ;; make sure there are undo boundaries
109 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
110 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
111 ;; remember which buffer to undo
112 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
113 org-agenda-undo-list))))))
115 (defmacro org-no-read-only (&rest body)
116 "Inhibit read-only for BODY."
117 (declare (debug (body)))
118 `(let ((inhibit-read-only t)) ,@body))
120 (defmacro org-save-outline-visibility (use-markers &rest body)
121 "Save and restore outline visibility around BODY.
122 If USE-MARKERS is non-nil, use markers for the positions.
123 This means that the buffer may change while running BODY,
124 but it also means that the buffer should stay alive
125 during the operation, because otherwise all these markers will
126 point nowhere."
127 (declare (debug (form body)) (indent 1))
128 (org-with-gensyms (data)
129 `(let ((,data (org-outline-overlay-data ,use-markers)))
130 (unwind-protect
131 (prog1 (progn ,@body)
132 (org-set-outline-overlay-data ,data))
133 (when ,use-markers
134 (dolist (c ,data)
135 (when (markerp (car c)) (move-marker (car c) nil))
136 (when (markerp (cdr c)) (move-marker (cdr c) nil))))))))
138 (defmacro org-with-wide-buffer (&rest body)
139 "Execute body while temporarily widening the buffer."
140 (declare (debug (body)))
141 `(save-excursion
142 (save-restriction
143 (widen)
144 ,@body)))
146 (defmacro org-with-limited-levels (&rest body)
147 "Execute BODY with limited number of outline levels."
148 (declare (debug (body)))
149 `(progn
150 (defvar org-called-with-limited-levels)
151 (defvar org-outline-regexp)
152 (defvar outline-regexp)
153 (defvar org-outline-regexp-bol)
154 (let* ((org-called-with-limited-levels t)
155 (org-outline-regexp (org-get-limited-outline-regexp))
156 (outline-regexp org-outline-regexp)
157 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
158 ,@body)))
160 (defmacro org-eval-in-environment (environment form)
161 (declare (debug (form form)) (indent 1))
162 `(eval (list 'let ,environment ',form)))
164 ;;;###autoload
165 (defmacro org-load-noerror-mustsuffix (file)
166 "Load FILE with optional arguments NOERROR and MUSTSUFFIX."
167 `(load ,file 'noerror nil nil 'mustsuffix))
171 ;;; String manipulation
173 (defsubst org-trim (s &optional keep-lead)
174 "Remove whitespace at the beginning and the end of string S.
175 When optional argument KEEP-LEAD is non-nil, removing blank lines
176 at the beginning of the string does not affect leading indentation."
177 (replace-regexp-in-string
178 (if keep-lead "\\`\\([ \t]*\n\\)+" "\\`[ \t\n\r]+") ""
179 (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
181 (defun org-string-nw-p (s)
182 "Return S if S is a string containing a non-blank character.
183 Otherwise, return nil."
184 (and (stringp s)
185 (string-match-p "[^ \r\t\n]" s)
188 (defun org-split-string (string &optional separators)
189 "Splits STRING into substrings at SEPARATORS.
191 SEPARATORS is a regular expression. When nil, it defaults to
192 \"[ \f\t\n\r\v]+\".
194 Unlike to `split-string', matching SEPARATORS at the beginning
195 and end of string are ignored."
196 (let ((separators (or separators "[ \f\t\n\r\v]+")))
197 (when (string-match (concat "\\`" separators) string)
198 (setq string (replace-match "" nil nil string)))
199 (when (string-match (concat separators "\\'") string)
200 (setq string (replace-match "" nil nil string)))
201 (split-string string separators)))
203 (defun org-string-display (string)
204 "Return STRING as it is displayed in the current buffer.
205 This function takes into consideration `invisible' and `display'
206 text properties."
207 (let* ((build-from-parts
208 (lambda (s property filter)
209 ;; Build a new string out of string S. On every group of
210 ;; contiguous characters with the same PROPERTY value,
211 ;; call FILTER on the properties list at the beginning of
212 ;; the group. If it returns a string, replace the
213 ;; characters in the group with it. Otherwise, preserve
214 ;; those characters.
215 (let ((len (length s))
216 (new "")
217 (i 0)
218 (cursor 0))
219 (while (setq i (text-property-not-all i len property nil s))
220 (let ((end (next-single-property-change i property s len))
221 (value (funcall filter (text-properties-at i s))))
222 (when value
223 (setq new (concat new (substring s cursor i) value))
224 (setq cursor end))
225 (setq i end)))
226 (concat new (substring s cursor)))))
227 (prune-invisible
228 (lambda (s)
229 (funcall build-from-parts s 'invisible
230 (lambda (props)
231 ;; If `invisible' property in PROPS means text
232 ;; is to be invisible, return the empty string.
233 ;; Otherwise return nil so that the part is
234 ;; skipped.
235 (and (or (eq t buffer-invisibility-spec)
236 (assoc-string (plist-get props 'invisible)
237 buffer-invisibility-spec))
238 "")))))
239 (replace-display
240 (lambda (s)
241 (funcall build-from-parts s 'display
242 (lambda (props)
243 ;; If there is any string specification in
244 ;; `display' property return it. Also attach
245 ;; other text properties on the part to that
246 ;; string (face...).
247 (let* ((display (plist-get props 'display))
248 (value (if (stringp display) display
249 (cl-some #'stringp display))))
250 (when value
251 (apply
252 #'propertize
253 ;; Displayed string could contain
254 ;; invisible parts, but no nested display.
255 (funcall prune-invisible value)
256 (plist-put props
257 'display
258 (and (not (stringp display))
259 (cl-remove-if #'stringp
260 display)))))))))))
261 ;; `display' property overrides `invisible' one. So we first
262 ;; replace characters with `display' property. Then we remove
263 ;; invisible characters.
264 (funcall prune-invisible (funcall replace-display string))))
266 (defun org-string-width (string)
267 "Return width of STRING when displayed in the current buffer.
268 Unlike to `string-width', this function takes into consideration
269 `invisible' and `display' text properties."
270 (string-width (org-string-display string)))
272 (defun org-not-nil (v)
273 "If V not nil, and also not the string \"nil\", then return V.
274 Otherwise return nil."
275 (and v (not (equal v "nil")) v))
277 (defun org-unbracket-string (pre post string)
278 "Remove PRE/POST from the beginning/end of STRING.
279 Both PRE and POST must be pre-/suffixes of STRING, or neither is
280 removed."
281 (if (and (string-prefix-p pre string)
282 (string-suffix-p post string))
283 (substring string (length pre) (- (length post)))
284 string))
286 (defsubst org-current-line-string (&optional to-here)
287 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
291 ;;; List manipulation
293 (defsubst org-get-alist-option (option key)
294 (cond ((eq key t) t)
295 ((eq option t) t)
296 ((assoc key option) (cdr (assoc key option)))
297 (t (let ((r (cdr (assq 'default option))))
298 (if (listp r) (delq nil r) r)))))
300 (defsubst org-last (list)
301 "Return the last element of LIST."
302 (car (last list)))
304 (defun org-plist-delete (plist property)
305 "Delete PROPERTY from PLIST.
306 This is in contrast to merely setting it to 0."
307 (let (p)
308 (while plist
309 (if (not (eq property (car plist)))
310 (setq p (plist-put p (car plist) (nth 1 plist))))
311 (setq plist (cddr plist)))
314 (defsubst org-uniquify (list)
315 "Non-destructively remove duplicate elements from LIST."
316 (let ((res (copy-sequence list))) (delete-dups res)))
320 ;;; Regexp matching
322 (defsubst org-pos-in-match-range (pos n)
323 (and (match-beginning n)
324 (<= (match-beginning n) pos)
325 (>= (match-end n) pos)))
327 (defun org-match-line (regexp)
328 "Match REGEXP at the beginning of the current line."
329 (save-excursion
330 (beginning-of-line)
331 (looking-at regexp)))
335 ;;; Motion
337 (defsubst org-goto-line (N)
338 (save-restriction
339 (widen)
340 (goto-char (point-min))
341 (forward-line (1- N))))
343 (defsubst org-current-line (&optional pos)
344 (save-excursion
345 (and pos (goto-char pos))
346 ;; works also in narrowed buffer, because we start at 1, not point-min
347 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
351 ;;; Text properties
353 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
354 rear-nonsticky t mouse-map t fontified t
355 org-emphasis t)
356 "Properties to remove when a string without properties is wanted.")
358 (defsubst org-no-properties (s &optional restricted)
359 "Remove all text properties from string S.
360 When RESTRICTED is non-nil, only remove the properties listed
361 in `org-rm-props'."
362 (if restricted (remove-text-properties 0 (length s) org-rm-props s)
363 (set-text-properties 0 (length s) nil s))
366 (defun org-make-parameter-alist (flat)
367 "Return alist based on FLAT.
368 FLAT is a list with alternating symbol names and values. The
369 returned alist is a list of lists with the symbol name in car and
370 the value in cdr."
371 (when flat
372 (cons (list (car flat) (cadr flat))
373 (org-make-parameter-alist (cddr flat)))))
375 (defsubst org-get-at-bol (property)
376 "Get text property PROPERTY at the beginning of line."
377 (get-text-property (point-at-bol) property))
381 ;;; Local variables
383 (defconst org-unique-local-variables
384 '(org-element--cache
385 org-element--cache-objects
386 org-element--cache-sync-keys
387 org-element--cache-sync-requests
388 org-element--cache-sync-timer)
389 "List of local variables that cannot be transferred to another buffer.")
391 (defun org-get-local-variables ()
392 "Return a list of all local variables in an Org mode buffer."
393 (delq nil
394 (mapcar
395 (lambda (x)
396 (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
397 (name (car binding)))
398 (and (not (get name 'org-state))
399 (not (memq name org-unique-local-variables))
400 (string-match-p
401 "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
402 auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
403 (symbol-name name))
404 binding)))
405 (with-temp-buffer
406 (org-mode)
407 (buffer-local-variables)))))
409 (defun org-clone-local-variables (from-buffer &optional regexp)
410 "Clone local variables from FROM-BUFFER.
411 Optional argument REGEXP selects variables to clone."
412 (dolist (pair (buffer-local-variables from-buffer))
413 (pcase pair
414 (`(,name . ,value) ;ignore unbound variables
415 (when (and (not (memq name org-unique-local-variables))
416 (or (null regexp) (string-match-p regexp (symbol-name name))))
417 (ignore-errors (set (make-local-variable name) value)))))))
421 ;;; Miscellaneous
423 (defsubst org-check-external-command (cmd &optional use no-error)
424 "Check if external program CMD for USE exists, error if not.
425 When the program does exist, return its path.
426 When it does not exist and NO-ERROR is set, return nil.
427 Otherwise, throw an error. The optional argument USE can describe what this
428 program is needed for, so that the error message can be more informative."
429 (or (executable-find cmd)
430 (if no-error
432 (error "Can't find `%s'%s" cmd
433 (if use (format " (%s)" use) "")))))
435 (defun org-let (list &rest body)
436 (eval (cons 'let (cons list body))))
437 (put 'org-let 'lisp-indent-function 1)
439 (defun org-let2 (list1 list2 &rest body)
440 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
441 (put 'org-let2 'lisp-indent-function 2)
443 (defsubst org-call-with-arg (command arg)
444 "Call COMMAND interactively, but pretend prefix arg was ARG."
445 (let ((current-prefix-arg arg)) (call-interactively command)))
447 (defvar org-outline-regexp) ; defined in org.el
448 (defvar org-odd-levels-only) ; defined in org.el
449 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
450 (defun org-get-limited-outline-regexp ()
451 "Return outline-regexp with limited number of levels.
452 The number of levels is controlled by `org-inlinetask-min-level'"
453 (cond ((not (derived-mode-p 'org-mode))
454 outline-regexp)
455 ((not (featurep 'org-inlinetask))
456 org-outline-regexp)
458 (let* ((limit-level (1- org-inlinetask-min-level))
459 (nstars (if org-odd-levels-only
460 (1- (* limit-level 2))
461 limit-level)))
462 (format "\\*\\{1,%d\\} " nstars)))))
464 (defun org-read-function (prompt &optional allow-empty?)
465 "Prompt for a function.
466 If ALLOW-EMPTY? is non-nil, return nil rather than raising an
467 error when the user input is empty."
468 (let ((func (completing-read prompt obarray #'fboundp t)))
469 (cond ((not (string= func ""))
470 (intern func))
471 (allow-empty? nil)
472 (t (user-error "Empty input is not valid")))))
475 (provide 'org-macs)
477 ;;; org-macs.el ends here