org-list: Use export back-ends to transform radio lists
[org-mode.git] / lisp / org-macs.el
blob1c9b8ee2696754dee22efc916ec53ae958c9d6e0
1 ;;; org-macs.el --- Top-level definitions for Org-mode
3 ;; Copyright (C) 2004-2013 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 (if (>= emacs-major-version 23)
38 (defsubst org-char-to-string(c)
39 "Defsubst to decode UTF-8 character values in emacs 23 and beyond."
40 (char-to-string c))
41 (defsubst org-char-to-string (c)
42 "Defsubst to decode UTF-8 character values in emacs 22."
43 (string (decode-char 'ucs c)))))
45 (declare-function org-add-props "org-compat" (string plist &rest props))
46 (declare-function org-string-match-p "org-compat" (&rest args))
48 (defmacro org-with-gensyms (symbols &rest body)
49 `(let ,(mapcar (lambda (s)
50 `(,s (make-symbol (concat "--" (symbol-name ',s))))) symbols)
51 ,@body))
52 (def-edebug-spec org-with-gensyms (sexp body))
53 (put 'org-with-gensyms 'lisp-indent-function 1)
55 (defmacro org-called-interactively-p (&optional kind)
56 (if (featurep 'xemacs)
57 `(interactive-p)
58 (if (or (> emacs-major-version 23)
59 (and (>= emacs-major-version 23)
60 (>= emacs-minor-version 2)))
61 ;; defined with no argument in <=23.1
62 `(with-no-warnings (called-interactively-p ,kind))
63 `(interactive-p))))
64 (def-edebug-spec org-called-interactively-p (&optional ("quote" symbolp)))
66 (defmacro org-bound-and-true-p (var)
67 "Return the value of symbol VAR if it is bound, else nil."
68 `(and (boundp (quote ,var)) ,var))
69 (def-edebug-spec org-bound-and-true-p (symbolp))
71 (defun org-string-nw-p (s)
72 "Is S a string with a non-white character?"
73 (and (stringp s)
74 (org-string-match-p "\\S-" s)
75 s))
77 (defun org-not-nil (v)
78 "If V not nil, and also not the string \"nil\", then return V.
79 Otherwise return nil."
80 (and v (not (equal v "nil")) v))
82 (defun org-substitute-posix-classes (re)
83 "Substitute posix classes in regular expression RE."
84 (let ((ss re))
85 (save-match-data
86 (while (string-match "\\[:alnum:\\]" ss)
87 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
88 (while (string-match "\\[:word:\\]" ss)
89 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
90 (while (string-match "\\[:alpha:\\]" ss)
91 (setq ss (replace-match "a-zA-Z" t t ss)))
92 (while (string-match "\\[:punct:\\]" ss)
93 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
94 ss)))
96 (defmacro org-re (s)
97 "Replace posix classes in regular expression."
98 (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
99 (def-edebug-spec org-re (form))
101 (defmacro org-preserve-lc (&rest body)
102 (org-with-gensyms (line col)
103 `(let ((,line (org-current-line))
104 (,col (current-column)))
105 (unwind-protect
106 (progn ,@body)
107 (org-goto-line ,line)
108 (org-move-to-column ,col)))))
109 (def-edebug-spec org-preserve-lc (body))
111 ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
112 ;; `org-unmodified' to ignore real text modifications
113 (defmacro org-unmodified (&rest body)
114 "Run BODY while preserving the buffer's `buffer-modified-p' state."
115 (org-with-gensyms (was-modified)
116 `(let ((,was-modified (buffer-modified-p)))
117 (unwind-protect
118 (let ((buffer-undo-list t)
119 (inhibit-modification-hooks t))
120 ,@body)
121 (set-buffer-modified-p ,was-modified)))))
122 (def-edebug-spec org-unmodified (body))
124 (defmacro org-without-partial-completion (&rest body)
125 `(if (and (boundp 'partial-completion-mode)
126 partial-completion-mode
127 (fboundp 'partial-completion-mode))
128 (unwind-protect
129 (progn
130 (partial-completion-mode -1)
131 ,@body)
132 (partial-completion-mode 1))
133 ,@body))
134 (def-edebug-spec org-without-partial-completion (body))
136 ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
137 (defmacro org-maybe-intangible (props)
138 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
139 In Emacs 21, invisible text is not avoided by the command loop, so the
140 intangible property is needed to make sure point skips this text.
141 In Emacs 22, this is not necessary. The intangible text property has
142 led to problems with flyspell. These problems are fixed in flyspell.el,
143 but we still avoid setting the property in Emacs 22 and later.
144 We use a macro so that the test can happen at compilation time."
145 (if (< emacs-major-version 22)
146 `(append '(intangible t) ,props)
147 props))
149 (defmacro org-with-point-at (pom &rest body)
150 "Move to buffer and point of point-or-marker POM for the duration of BODY."
151 (org-with-gensyms (mpom)
152 `(let ((,mpom ,pom))
153 (save-excursion
154 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
155 (save-excursion
156 (goto-char (or ,mpom (point)))
157 ,@body)))))
158 (def-edebug-spec org-with-point-at (form body))
159 (put 'org-with-point-at 'lisp-indent-function 1)
161 (defmacro org-no-warnings (&rest body)
162 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
163 (def-edebug-spec org-no-warnings (body))
165 ;; FIXME: Normalize argument names
166 (defmacro org-with-remote-undo (_buffer &rest _body)
167 "Execute BODY while recording undo information in two buffers."
168 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
169 `(let ((,cline (org-current-line))
170 (,cmd this-command)
171 (,buf1 (current-buffer))
172 (,buf2 ,_buffer)
173 (,undo1 buffer-undo-list)
174 (,undo2 (with-current-buffer ,_buffer buffer-undo-list))
175 ,c1 ,c2)
176 ,@_body
177 (when org-agenda-allow-remote-undo
178 (setq ,c1 (org-verify-change-for-undo
179 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
180 ,c2 (org-verify-change-for-undo
181 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
182 (when (or ,c1 ,c2)
183 ;; make sure there are undo boundaries
184 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
185 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
186 ;; remember which buffer to undo
187 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
188 org-agenda-undo-list))))))
189 (def-edebug-spec org-with-remote-undo (form body))
190 (put 'org-with-remote-undo 'lisp-indent-function 1)
192 (defmacro org-no-read-only (&rest body)
193 "Inhibit read-only for BODY."
194 `(let ((inhibit-read-only t)) ,@body))
195 (def-edebug-spec org-no-read-only (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-autoload (file functions)
286 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
287 (let ((d (format "Documentation will be available after `%s.el' is loaded."
288 file))
290 (while (setq f (pop functions))
291 (or (fboundp f) (autoload f file d t)))))
293 (defun org-match-line (re)
294 "Looking-at at the beginning of the current line."
295 (save-excursion
296 (goto-char (point-at-bol))
297 (looking-at re)))
299 (defun org-plist-delete (plist property)
300 "Delete PROPERTY from PLIST.
301 This is in contrast to merely setting it to 0."
302 (let (p)
303 (while plist
304 (if (not (eq property (car plist)))
305 (setq p (plist-put p (car plist) (nth 1 plist))))
306 (setq plist (cddr plist)))
309 (defun org-replace-match-keep-properties (newtext &optional fixedcase
310 literal string)
311 "Like `replace-match', but add the text properties found original text."
312 (setq newtext (org-add-props newtext (text-properties-at
313 (match-beginning 0) string)))
314 (replace-match newtext fixedcase literal string))
316 (defmacro org-save-outline-visibility (use-markers &rest body)
317 "Save and restore outline visibility around BODY.
318 If USE-MARKERS is non-nil, use markers for the positions.
319 This means that the buffer may change while running BODY,
320 but it also means that the buffer should stay alive
321 during the operation, because otherwise all these markers will
322 point nowhere."
323 (declare (indent 1))
324 (org-with-gensyms (data rtn)
325 `(let ((,data (org-outline-overlay-data ,use-markers))
326 ,rtn)
327 (unwind-protect
328 (progn
329 (setq ,rtn (progn ,@body))
330 (org-set-outline-overlay-data ,data))
331 (when ,use-markers
332 (mapc (lambda (c)
333 (and (markerp (car c)) (move-marker (car c) nil))
334 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
335 ,data)))
336 ,rtn)))
337 (def-edebug-spec org-save-outline-visibility (form body))
339 (defmacro org-with-wide-buffer (&rest body)
340 "Execute body while temporarily widening the buffer."
341 `(save-excursion
342 (save-restriction
343 (widen)
344 ,@body)))
345 (def-edebug-spec org-with-wide-buffer (body))
347 (defmacro org-with-limited-levels (&rest body)
348 "Execute BODY with limited number of outline levels."
349 `(let* ((org-called-with-limited-levels t)
350 (org-outline-regexp (org-get-limited-outline-regexp))
351 (outline-regexp org-outline-regexp)
352 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
353 ,@body))
354 (def-edebug-spec org-with-limited-levels (body))
356 (defvar org-outline-regexp) ; defined in org.el
357 (defvar org-odd-levels-only) ; defined in org.el
358 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
359 (defun org-get-limited-outline-regexp ()
360 "Return outline-regexp with limited number of levels.
361 The number of levels is controlled by `org-inlinetask-min-level'"
362 (if (or (not (derived-mode-p 'org-mode)) (not (featurep 'org-inlinetask)))
363 org-outline-regexp
364 (let* ((limit-level (1- org-inlinetask-min-level))
365 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
366 (format "\\*\\{1,%d\\} " nstars))))
368 (defun org-format-seconds (string seconds)
369 "Compatibility function replacing format-seconds."
370 (if (fboundp 'format-seconds)
371 (format-seconds string seconds)
372 (format-time-string string (seconds-to-time seconds))))
374 (defmacro org-eval-in-environment (environment form)
375 `(eval (list 'let ,environment ',form)))
376 (def-edebug-spec org-eval-in-environment (form form))
377 (put 'org-eval-in-environment 'lisp-indent-function 1)
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