Fix handling of author and email in koma exporter.
[org-mode.git] / lisp / org-macs.el
blobcc837d0bb1e2378c93f17d54ccc543f7a368de77
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 (defmacro org-with-remote-undo (buffer &rest body)
166 "Execute BODY while recording undo information in two buffers."
167 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
168 `(let ((,cline (org-current-line))
169 (,cmd this-command)
170 (,buf1 (current-buffer))
171 (,buf2 ,buffer)
172 (,undo1 buffer-undo-list)
173 (,undo2 (with-current-buffer ,buffer buffer-undo-list))
174 ,c1 ,c2)
175 ,@body
176 (when org-agenda-allow-remote-undo
177 (setq ,c1 (org-verify-change-for-undo
178 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
179 ,c2 (org-verify-change-for-undo
180 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
181 (when (or ,c1 ,c2)
182 ;; make sure there are undo boundaries
183 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
184 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
185 ;; remember which buffer to undo
186 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
187 org-agenda-undo-list))))))
188 (def-edebug-spec org-with-remote-undo (form body))
189 (put 'org-with-remote-undo 'lisp-indent-function 1)
191 (defmacro org-no-read-only (&rest body)
192 "Inhibit read-only for BODY."
193 `(let ((inhibit-read-only t)) ,@body))
194 (def-edebug-spec org-no-read-only (body))
196 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
197 rear-nonsticky t mouse-map t fontified t
198 org-emphasis t)
199 "Properties to remove when a string without properties is wanted.")
201 (defsubst org-match-string-no-properties (num &optional string)
202 (if (featurep 'xemacs)
203 (let ((s (match-string num string)))
204 (and s (remove-text-properties 0 (length s) org-rm-props s))
206 (match-string-no-properties num string)))
208 (defsubst org-no-properties (s &optional restricted)
209 "Remove all text properties from string S.
210 When RESTRICTED is non-nil, only remove the properties listed
211 in `org-rm-props'."
212 (if (fboundp 'set-text-properties)
213 (set-text-properties 0 (length s) nil s)
214 (if restricted
215 (remove-text-properties 0 (length s) org-rm-props s)
216 (set-text-properties 0 (length s) nil s)))
219 (defsubst org-get-alist-option (option key)
220 (cond ((eq key t) t)
221 ((eq option t) t)
222 ((assoc key option) (cdr (assoc key option)))
223 (t (let ((r (cdr (assq 'default option))))
224 (if (listp r) (delq nil r) r)))))
226 (defsubst org-check-external-command (cmd &optional use no-error)
227 "Check if external program CMD for USE exists, error if not.
228 When the program does exist, return its path.
229 When it does not exist and NO-ERROR is set, return nil.
230 Otherwise, throw an error. The optional argument USE can describe what this
231 program is needed for, so that the error message can be more informative."
232 (or (executable-find cmd)
233 (if no-error
235 (error "Can't find `%s'%s" cmd
236 (if use (format " (%s)" use) "")))))
238 (defsubst org-inhibit-invisibility ()
239 "Modified `buffer-invisibility-spec' for Emacs 21.
240 Some ops with invisible text do not work correctly on Emacs 21. For these
241 we turn off invisibility temporarily. Use this in a `let' form."
242 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
244 (defsubst org-set-local (var value)
245 "Make VAR local in current buffer and set it to VALUE."
246 (set (make-local-variable var) value))
248 (defsubst org-last (list)
249 "Return the last element of LIST."
250 (car (last list)))
252 (defun org-let (list &rest body)
253 (eval (cons 'let (cons list body))))
254 (put 'org-let 'lisp-indent-function 1)
256 (defun org-let2 (list1 list2 &rest body)
257 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
258 (put 'org-let2 'lisp-indent-function 2)
260 (defsubst org-call-with-arg (command arg)
261 "Call COMMAND interactively, but pretend prefix arg was ARG."
262 (let ((current-prefix-arg arg)) (call-interactively command)))
264 (defsubst org-current-line (&optional pos)
265 (save-excursion
266 (and pos (goto-char pos))
267 ;; works also in narrowed buffer, because we start at 1, not point-min
268 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
270 (defsubst org-goto-line (N)
271 (save-restriction
272 (widen)
273 (goto-char (point-min))
274 (forward-line (1- N))))
276 (defsubst org-current-line-string (&optional to-here)
277 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
279 (defsubst org-pos-in-match-range (pos n)
280 (and (match-beginning n)
281 (<= (match-beginning n) pos)
282 (>= (match-end n) pos)))
284 (defun org-autoload (file functions)
285 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
286 (let ((d (format "Documentation will be available after `%s.el' is loaded."
287 file))
289 (while (setq f (pop functions))
290 (or (fboundp f) (autoload f file d t)))))
292 (defun org-match-line (re)
293 "Looking-at at the beginning of the current line."
294 (save-excursion
295 (goto-char (point-at-bol))
296 (looking-at re)))
298 (defun org-plist-delete (plist property)
299 "Delete PROPERTY from PLIST.
300 This is in contrast to merely setting it to 0."
301 (let (p)
302 (while plist
303 (if (not (eq property (car plist)))
304 (setq p (plist-put p (car plist) (nth 1 plist))))
305 (setq plist (cddr plist)))
308 (defun org-replace-match-keep-properties (newtext &optional fixedcase
309 literal string)
310 "Like `replace-match', but add the text properties found original text."
311 (setq newtext (org-add-props newtext (text-properties-at
312 (match-beginning 0) string)))
313 (replace-match newtext fixedcase literal string))
315 (defmacro org-save-outline-visibility (use-markers &rest body)
316 "Save and restore outline visibility around BODY.
317 If USE-MARKERS is non-nil, use markers for the positions.
318 This means that the buffer may change while running BODY,
319 but it also means that the buffer should stay alive
320 during the operation, because otherwise all these markers will
321 point nowhere."
322 (declare (indent 1))
323 (org-with-gensyms (data rtn)
324 `(let ((,data (org-outline-overlay-data ,use-markers))
325 ,rtn)
326 (unwind-protect
327 (progn
328 (setq ,rtn (progn ,@body))
329 (org-set-outline-overlay-data ,data))
330 (when ,use-markers
331 (mapc (lambda (c)
332 (and (markerp (car c)) (move-marker (car c) nil))
333 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
334 ,data)))
335 ,rtn)))
336 (def-edebug-spec org-save-outline-visibility (form body))
338 (defmacro org-with-wide-buffer (&rest body)
339 "Execute body while temporarily widening the buffer."
340 `(save-excursion
341 (save-restriction
342 (widen)
343 ,@body)))
344 (def-edebug-spec org-with-wide-buffer (body))
346 (defmacro org-with-limited-levels (&rest body)
347 "Execute BODY with limited number of outline levels."
348 `(let* ((org-called-with-limited-levels t)
349 (org-outline-regexp (org-get-limited-outline-regexp))
350 (outline-regexp org-outline-regexp)
351 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
352 ,@body))
353 (def-edebug-spec org-with-limited-levels (body))
355 (defvar org-outline-regexp) ; defined in org.el
356 (defvar org-odd-levels-only) ; defined in org.el
357 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
358 (defun org-get-limited-outline-regexp ()
359 "Return outline-regexp with limited number of levels.
360 The number of levels is controlled by `org-inlinetask-min-level'"
361 (if (or (not (derived-mode-p 'org-mode)) (not (featurep 'org-inlinetask)))
362 org-outline-regexp
363 (let* ((limit-level (1- org-inlinetask-min-level))
364 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
365 (format "\\*\\{1,%d\\} " nstars))))
367 (defun org-format-seconds (string seconds)
368 "Compatibility function replacing format-seconds."
369 (if (fboundp 'format-seconds)
370 (format-seconds string seconds)
371 (format-time-string string (seconds-to-time seconds))))
373 (defmacro org-eval-in-environment (environment form)
374 `(eval (list 'let ,environment ',form)))
375 (def-edebug-spec org-eval-in-environment (form form))
376 (put 'org-eval-in-environment 'lisp-indent-function 1)
378 (defun org-make-parameter-alist (flat)
379 "Return alist based on FLAT.
380 FLAT is a list with alternating symbol names and values. The
381 returned alist is a list of lists with the symbol name in car and
382 the value in cdr."
383 (when flat
384 (cons (list (car flat) (cadr flat))
385 (org-make-parameter-alist (cddr flat)))))
387 ;;;###autoload
388 (defmacro org-load-noerror-mustsuffix (file)
389 "Load FILE with optional arguments NOERROR and MUSTSUFFIX. Drop the MUSTSUFFIX argument for XEmacs, which doesn't recognize it."
390 (if (featurep 'xemacs)
391 `(load ,file 'noerror)
392 `(load ,file 'noerror nil nil 'mustsuffix)))
394 (provide 'org-macs)
396 ;;; org-macs.el ends here