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