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