Deprecate `org-char-to-string' in favor of `char-to-string'
[org-mode/org-tableheadings.git] / lisp / org-macs.el
blob75997fc7b5dd3dc0b9b847d48315babea8e15ce2
1 ;;; org-macs.el --- Top-level Definitions for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2016 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 <http://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 (defmacro org-called-interactively-p (&optional kind)
42 (declare (debug (&optional ("quote" symbolp)))) ;Why not just t?
43 (if (or (> emacs-major-version 23)
44 (and (>= emacs-major-version 23)
45 (>= emacs-minor-version 2)))
46 ;; defined with no argument in <=23.1
47 `(with-no-warnings (called-interactively-p ,kind))
48 `(interactive-p)))
50 (defmacro org-bound-and-true-p (var)
51 "Return the value of symbol VAR if it is bound, else nil."
52 (declare (debug (symbolp)))
53 `(and (boundp (quote ,var)) ,var))
55 (defun org-string-nw-p (s)
56 "Return S if S is a string containing a non-blank character.
57 Otherwise, return nil."
58 (and (stringp s)
59 (string-match-p "[^ \r\t\n]" s)
60 s))
62 (defun org-not-nil (v)
63 "If V not nil, and also not the string \"nil\", then return V.
64 Otherwise return nil."
65 (and v (not (equal v "nil")) v))
67 (defun org-substitute-posix-classes (re)
68 "Substitute posix classes in regular expression RE."
69 (let ((ss re))
70 (save-match-data
71 (while (string-match "\\[:alnum:\\]" ss)
72 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
73 (while (string-match "\\[:word:\\]" ss)
74 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
75 (while (string-match "\\[:alpha:\\]" ss)
76 (setq ss (replace-match "a-zA-Z" t t ss)))
77 (while (string-match "\\[:punct:\\]" ss)
78 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
79 ss)))
81 (defmacro org-preserve-lc (&rest body)
82 (declare (debug (body)))
83 (org-with-gensyms (line col)
84 `(let ((,line (org-current-line))
85 (,col (current-column)))
86 (unwind-protect
87 (progn ,@body)
88 (org-goto-line ,line)
89 (org-move-to-column ,col)))))
91 ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
92 ;; `org-unmodified' to ignore real text modifications
93 (defmacro org-unmodified (&rest body)
94 "Run BODY while preserving the buffer's `buffer-modified-p' state."
95 (declare (debug (body)))
96 (org-with-gensyms (was-modified)
97 `(let ((,was-modified (buffer-modified-p)))
98 (unwind-protect
99 (let ((buffer-undo-list t)
100 (inhibit-modification-hooks t))
101 ,@body)
102 (set-buffer-modified-p ,was-modified)))))
104 (defmacro org-without-partial-completion (&rest body)
105 (declare (debug (body)))
106 `(if (and (boundp 'partial-completion-mode)
107 partial-completion-mode
108 (fboundp 'partial-completion-mode))
109 (unwind-protect
110 (progn
111 (partial-completion-mode -1)
112 ,@body)
113 (partial-completion-mode 1))
114 ,@body))
116 ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
117 (defmacro org-maybe-intangible (props)
118 "Add \\='(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
119 In Emacs 21, invisible text is not avoided by the command loop, so the
120 intangible property is needed to make sure point skips this text.
121 In Emacs 22, this is not necessary. The intangible text property has
122 led to problems with flyspell. These problems are fixed in flyspell.el,
123 but we still avoid setting the property in Emacs 22 and later.
124 We use a macro so that the test can happen at compilation time."
125 (if (< emacs-major-version 22)
126 `(append '(intangible t) ,props)
127 props))
129 (defmacro org-with-point-at (pom &rest body)
130 "Move to buffer and point of point-or-marker POM for the duration of BODY."
131 (declare (debug (form body)) (indent 1))
132 (org-with-gensyms (mpom)
133 `(let ((,mpom ,pom))
134 (save-excursion
135 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
136 (org-with-wide-buffer
137 (goto-char (or ,mpom (point)))
138 ,@body)))))
140 (defmacro org-no-warnings (&rest body)
141 (declare (debug (body)))
142 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
144 (defmacro org-with-remote-undo (buffer &rest body)
145 "Execute BODY while recording undo information in two buffers."
146 (declare (debug (form body)) (indent 1))
147 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
148 `(let ((,cline (org-current-line))
149 (,cmd this-command)
150 (,buf1 (current-buffer))
151 (,buf2 ,buffer)
152 (,undo1 buffer-undo-list)
153 (,undo2 (with-current-buffer ,buffer buffer-undo-list))
154 ,c1 ,c2)
155 ,@body
156 (when org-agenda-allow-remote-undo
157 (setq ,c1 (org-verify-change-for-undo
158 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
159 ,c2 (org-verify-change-for-undo
160 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
161 (when (or ,c1 ,c2)
162 ;; make sure there are undo boundaries
163 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
164 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
165 ;; remember which buffer to undo
166 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
167 org-agenda-undo-list))))))
169 (defmacro org-no-read-only (&rest body)
170 "Inhibit read-only for BODY."
171 (declare (debug (body)))
172 `(let ((inhibit-read-only t)) ,@body))
174 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
175 rear-nonsticky t mouse-map t fontified t
176 org-emphasis t)
177 "Properties to remove when a string without properties is wanted.")
179 (defsubst org-no-properties (s &optional restricted)
180 "Remove all text properties from string S.
181 When RESTRICTED is non-nil, only remove the properties listed
182 in `org-rm-props'."
183 (if (fboundp 'set-text-properties)
184 (set-text-properties 0 (length s) nil s)
185 (if restricted
186 (remove-text-properties 0 (length s) org-rm-props s)
187 (set-text-properties 0 (length s) nil s)))
190 (defsubst org-get-alist-option (option key)
191 (cond ((eq key t) t)
192 ((eq option t) t)
193 ((assoc key option) (cdr (assoc key option)))
194 (t (let ((r (cdr (assq 'default option))))
195 (if (listp r) (delq nil r) r)))))
197 (defsubst org-check-external-command (cmd &optional use no-error)
198 "Check if external program CMD for USE exists, error if not.
199 When the program does exist, return its path.
200 When it does not exist and NO-ERROR is set, return nil.
201 Otherwise, throw an error. The optional argument USE can describe what this
202 program is needed for, so that the error message can be more informative."
203 (or (executable-find cmd)
204 (if no-error
206 (error "Can't find `%s'%s" cmd
207 (if use (format " (%s)" use) "")))))
209 (defsubst org-inhibit-invisibility ()
210 "Modified `buffer-invisibility-spec' for Emacs 21.
211 Some ops with invisible text do not work correctly on Emacs 21. For these
212 we turn off invisibility temporarily. Use this in a `let' form."
213 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
215 (defsubst org-last (list)
216 "Return the last element of LIST."
217 (car (last list)))
219 (defun org-let (list &rest body)
220 (eval (cons 'let (cons list body))))
221 (put 'org-let 'lisp-indent-function 1)
223 (defun org-let2 (list1 list2 &rest body)
224 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
225 (put 'org-let2 'lisp-indent-function 2)
227 (defsubst org-call-with-arg (command arg)
228 "Call COMMAND interactively, but pretend prefix arg was ARG."
229 (let ((current-prefix-arg arg)) (call-interactively command)))
231 (defsubst org-current-line (&optional pos)
232 (save-excursion
233 (and pos (goto-char pos))
234 ;; works also in narrowed buffer, because we start at 1, not point-min
235 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
237 (defsubst org-goto-line (N)
238 (save-restriction
239 (widen)
240 (goto-char (point-min))
241 (forward-line (1- N))))
243 (defsubst org-current-line-string (&optional to-here)
244 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
246 (defsubst org-pos-in-match-range (pos n)
247 (and (match-beginning n)
248 (<= (match-beginning n) pos)
249 (>= (match-end n) pos)))
251 (defun org-match-line (re)
252 "Looking-at at the beginning of the current line."
253 (save-excursion
254 (goto-char (point-at-bol))
255 (looking-at re)))
257 (defun org-plist-delete (plist property)
258 "Delete PROPERTY from PLIST.
259 This is in contrast to merely setting it to 0."
260 (let (p)
261 (while plist
262 (if (not (eq property (car plist)))
263 (setq p (plist-put p (car plist) (nth 1 plist))))
264 (setq plist (cddr plist)))
267 (defun org-replace-match-keep-properties (newtext &optional fixedcase
268 literal string)
269 "Like `replace-match', but add the text properties found original text."
270 (setq newtext (org-add-props newtext (text-properties-at
271 (match-beginning 0) string)))
272 (replace-match newtext fixedcase literal string))
274 (defmacro org-save-outline-visibility (use-markers &rest body)
275 "Save and restore outline visibility around BODY.
276 If USE-MARKERS is non-nil, use markers for the positions.
277 This means that the buffer may change while running BODY,
278 but it also means that the buffer should stay alive
279 during the operation, because otherwise all these markers will
280 point nowhere."
281 (declare (debug (form body)) (indent 1))
282 (org-with-gensyms (data)
283 `(let ((,data (org-outline-overlay-data ,use-markers)))
284 (unwind-protect
285 (prog1 (progn ,@body)
286 (org-set-outline-overlay-data ,data))
287 (when ,use-markers
288 (dolist (c ,data)
289 (when (markerp (car c)) (move-marker (car c) nil))
290 (when (markerp (cdr c)) (move-marker (cdr c) nil))))))))
292 (defmacro org-with-wide-buffer (&rest body)
293 "Execute body while temporarily widening the buffer."
294 (declare (debug (body)))
295 `(save-excursion
296 (save-restriction
297 (widen)
298 ,@body)))
300 (defmacro org-with-limited-levels (&rest body)
301 "Execute BODY with limited number of outline levels."
302 (declare (debug (body)))
303 `(progn
304 (defvar org-called-with-limited-levels)
305 (defvar org-outline-regexp)
306 (defvar outline-regexp)
307 (defvar org-outline-regexp-bol)
308 (let* ((org-called-with-limited-levels t)
309 (org-outline-regexp (org-get-limited-outline-regexp))
310 (outline-regexp org-outline-regexp)
311 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
312 ,@body)))
314 (defvar org-outline-regexp) ; defined in org.el
315 (defvar org-odd-levels-only) ; defined in org.el
316 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
317 (defun org-get-limited-outline-regexp ()
318 "Return outline-regexp with limited number of levels.
319 The number of levels is controlled by `org-inlinetask-min-level'"
320 (cond ((not (derived-mode-p 'org-mode))
321 outline-regexp)
322 ((not (featurep 'org-inlinetask))
323 org-outline-regexp)
325 (let* ((limit-level (1- org-inlinetask-min-level))
326 (nstars (if org-odd-levels-only
327 (1- (* limit-level 2))
328 limit-level)))
329 (format "\\*\\{1,%d\\} " nstars)))))
331 (defun org-format-seconds (string seconds)
332 "Compatibility function replacing format-seconds."
333 (if (fboundp 'format-seconds)
334 (format-seconds string seconds)
335 (format-time-string string (seconds-to-time seconds))))
337 (defmacro org-eval-in-environment (environment form)
338 (declare (debug (form form)) (indent 1))
339 `(eval (list 'let ,environment ',form)))
341 (defun org-make-parameter-alist (flat)
342 "Return alist based on FLAT.
343 FLAT is a list with alternating symbol names and values. The
344 returned alist is a list of lists with the symbol name in car and
345 the value in cdr."
346 (when flat
347 (cons (list (car flat) (cadr flat))
348 (org-make-parameter-alist (cddr flat)))))
350 ;;;###autoload
351 (defmacro org-load-noerror-mustsuffix (file)
352 "Load FILE with optional arguments NOERROR and MUSTSUFFIX."
353 `(load ,file 'noerror nil nil 'mustsuffix))
355 (provide 'org-macs)
357 ;;; org-macs.el ends here