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