org.el: Fix bug in `org-fill-template'
[org-mode.git] / lisp / org-macs.el
blob75c4013ed26cdd8f7f6f6bf2e5a4cf06db4703ae
1 ;;; org-macs.el --- Top-level definitions for Org-mode
3 ;; Copyright (C) 2004-2012 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 (when (and (not (fboundp 'with-silent-modifications))
67 (or (< emacs-major-version 23)
68 (and (= emacs-major-version 23)
69 (< emacs-minor-version 2))))
70 (defmacro with-silent-modifications (&rest body)
71 `(org-unmodified ,@body))
72 (def-edebug-spec with-silent-modifications (body)))
74 (defmacro org-bound-and-true-p (var)
75 "Return the value of symbol VAR if it is bound, else nil."
76 `(and (boundp (quote ,var)) ,var))
77 (def-edebug-spec org-bound-and-true-p (symbolp))
79 (defun org-string-nw-p (s)
80 "Is S a string with a non-white character?"
81 (and (stringp s)
82 (org-string-match-p "\\S-" s)
83 s))
85 (defun org-not-nil (v)
86 "If V not nil, and also not the string \"nil\", then return V.
87 Otherwise return nil."
88 (and v (not (equal v "nil")) v))
90 (defmacro org-unmodified (&rest body)
91 "Execute body without changing `buffer-modified-p'.
92 Also, do not record undo information."
93 `(set-buffer-modified-p
94 (prog1 (buffer-modified-p)
95 (let ((buffer-undo-list t)
96 before-change-functions after-change-functions)
97 ,@body))))
98 (def-edebug-spec org-unmodified (body))
100 (defun org-substitute-posix-classes (re)
101 "Substitute posix classes in regular expression RE."
102 (let ((ss re))
103 (save-match-data
104 (while (string-match "\\[:alnum:\\]" ss)
105 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
106 (while (string-match "\\[:word:\\]" ss)
107 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
108 (while (string-match "\\[:alpha:\\]" ss)
109 (setq ss (replace-match "a-zA-Z" t t ss)))
110 (while (string-match "\\[:punct:\\]" ss)
111 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
112 ss)))
114 (defmacro org-re (s)
115 "Replace posix classes in regular expression."
116 (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
117 (def-edebug-spec org-re (form))
119 (defmacro org-preserve-lc (&rest body)
120 (org-with-gensyms (line col)
121 `(let ((,line (org-current-line))
122 (,col (current-column)))
123 (unwind-protect
124 (progn ,@body)
125 (org-goto-line ,line)
126 (org-move-to-column ,col)))))
127 (def-edebug-spec org-preserve-lc (body))
129 (defmacro org-without-partial-completion (&rest body)
130 `(if (and (boundp 'partial-completion-mode)
131 partial-completion-mode
132 (fboundp 'partial-completion-mode))
133 (unwind-protect
134 (progn
135 (partial-completion-mode -1)
136 ,@body)
137 (partial-completion-mode 1))
138 ,@body))
139 (def-edebug-spec org-without-partial-completion (body))
141 ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
142 (defmacro org-maybe-intangible (props)
143 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
144 In Emacs 21, invisible text is not avoided by the command loop, so the
145 intangible property is needed to make sure point skips this text.
146 In Emacs 22, this is not necessary. The intangible text property has
147 led to problems with flyspell. These problems are fixed in flyspell.el,
148 but we still avoid setting the property in Emacs 22 and later.
149 We use a macro so that the test can happen at compilation time."
150 (if (< emacs-major-version 22)
151 `(append '(intangible t) ,props)
152 props))
154 (defmacro org-with-point-at (pom &rest body)
155 "Move to buffer and point of point-or-marker POM for the duration of BODY."
156 (org-with-gensyms (mpom)
157 `(let ((,mpom ,pom))
158 (save-excursion
159 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
160 (save-excursion
161 (goto-char (or ,mpom (point)))
162 ,@body)))))
163 (def-edebug-spec org-with-point-at (form body))
164 (put 'org-with-point-at 'lisp-indent-function 1)
166 (defmacro org-no-warnings (&rest body)
167 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
168 (def-edebug-spec org-no-warnings (body))
170 (defmacro org-if-unprotected (&rest body)
171 "Execute BODY if there is no `org-protected' text property at point."
172 `(unless (get-text-property (point) 'org-protected)
173 ,@body))
174 (def-edebug-spec org-if-unprotected (body))
176 (defmacro org-if-unprotected-1 (&rest body)
177 "Execute BODY if there is no `org-protected' text property at point-1."
178 `(unless (get-text-property (1- (point)) 'org-protected)
179 ,@body))
180 (def-edebug-spec org-if-unprotected-1 (body))
182 (defmacro org-if-unprotected-at (pos &rest body)
183 "Execute BODY if there is no `org-protected' text property at POS."
184 `(unless (get-text-property ,pos 'org-protected)
185 ,@body))
186 (def-edebug-spec org-if-unprotected-at (form body))
187 (put 'org-if-unprotected-at 'lisp-indent-function 1)
189 (defun org-re-search-forward-unprotected (&rest args)
190 "Like re-search-forward, but stop only in unprotected places."
191 (catch 'exit
192 (while t
193 (unless (apply 're-search-forward args)
194 (throw 'exit nil))
195 (unless (get-text-property (match-beginning 0) 'org-protected)
196 (throw 'exit (point))))))
198 ;; FIXME: Normalize argument names
199 (defmacro org-with-remote-undo (_buffer &rest _body)
200 "Execute BODY while recording undo information in two buffers."
201 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
202 `(let ((,cline (org-current-line))
203 (,cmd this-command)
204 (,buf1 (current-buffer))
205 (,buf2 ,_buffer)
206 (,undo1 buffer-undo-list)
207 (,undo2 (with-current-buffer ,_buffer buffer-undo-list))
208 ,c1 ,c2)
209 ,@_body
210 (when org-agenda-allow-remote-undo
211 (setq ,c1 (org-verify-change-for-undo
212 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
213 ,c2 (org-verify-change-for-undo
214 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
215 (when (or ,c1 ,c2)
216 ;; make sure there are undo boundaries
217 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
218 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
219 ;; remember which buffer to undo
220 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
221 org-agenda-undo-list))))))
222 (def-edebug-spec org-with-remote-undo (form body))
223 (put 'org-with-remote-undo 'lisp-indent-function 1)
225 (defmacro org-no-read-only (&rest body)
226 "Inhibit read-only for BODY."
227 `(let ((inhibit-read-only t)) ,@body))
228 (def-edebug-spec org-no-read-only (body))
230 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
231 rear-nonsticky t mouse-map t fontified t
232 org-emphasis t)
233 "Properties to remove when a string without properties is wanted.")
235 (defsubst org-match-string-no-properties (num &optional string)
236 (if (featurep 'xemacs)
237 (let ((s (match-string num string)))
238 (and s (remove-text-properties 0 (length s) org-rm-props s))
240 (match-string-no-properties num string)))
242 (defsubst org-no-properties (s)
243 (if (fboundp 'set-text-properties)
244 (set-text-properties 0 (length s) nil s)
245 (remove-text-properties 0 (length s) org-rm-props s))
248 (defsubst org-get-alist-option (option key)
249 (cond ((eq key t) t)
250 ((eq option t) t)
251 ((assoc key option) (cdr (assoc key option)))
252 (t (cdr (assq 'default option)))))
254 (defsubst org-check-external-command (cmd &optional use no-error)
255 "Check if external program CMD for USE exists, error if not.
256 When the program does exist, return its path.
257 When it does not exist and NO-ERROR is set, return nil.
258 Otherwise, throw an error. The optional argument USE can describe what this
259 program is needed for, so that the error message can be more informative."
260 (or (executable-find cmd)
261 (if no-error
263 (error "Can't find `%s'%s" cmd
264 (if use (format " (%s)" use) "")))))
266 (defsubst org-inhibit-invisibility ()
267 "Modified `buffer-invisibility-spec' for Emacs 21.
268 Some ops with invisible text do not work correctly on Emacs 21. For these
269 we turn off invisibility temporarily. Use this in a `let' form."
270 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
272 (defsubst org-set-local (var value)
273 "Make VAR local in current buffer and set it to VALUE."
274 (set (make-local-variable var) value))
276 (defsubst org-last (list)
277 "Return the last element of LIST."
278 (car (last list)))
280 (defun org-let (list &rest body)
281 (eval (cons 'let (cons list body))))
282 (put 'org-let 'lisp-indent-function 1)
284 (defun org-let2 (list1 list2 &rest body)
285 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
286 (put 'org-let2 'lisp-indent-function 2)
288 (defsubst org-call-with-arg (command arg)
289 "Call COMMAND interactively, but pretend prefix arg was ARG."
290 (let ((current-prefix-arg arg)) (call-interactively command)))
292 (defsubst org-current-line (&optional pos)
293 (save-excursion
294 (and pos (goto-char pos))
295 ;; works also in narrowed buffer, because we start at 1, not point-min
296 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
298 (defsubst org-goto-line (N)
299 (save-restriction
300 (widen)
301 (goto-char (point-min))
302 (forward-line (1- N))))
304 (defsubst org-current-line-string (&optional to-here)
305 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
307 (defsubst org-pos-in-match-range (pos n)
308 (and (match-beginning n)
309 (<= (match-beginning n) pos)
310 (>= (match-end n) pos)))
312 (defun org-autoload (file functions)
313 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
314 (let ((d (format "Documentation will be available after `%s.el' is loaded."
315 file))
317 (while (setq f (pop functions))
318 (or (fboundp f) (autoload f file d t)))))
320 (defun org-match-line (re)
321 "Looking-at at the beginning of the current line."
322 (save-excursion
323 (goto-char (point-at-bol))
324 (looking-at re)))
326 (defun org-plist-delete (plist property)
327 "Delete PROPERTY from PLIST.
328 This is in contrast to merely setting it to 0."
329 (let (p)
330 (while plist
331 (if (not (eq property (car plist)))
332 (setq p (plist-put p (car plist) (nth 1 plist))))
333 (setq plist (cddr plist)))
336 (defun org-replace-match-keep-properties (newtext &optional fixedcase
337 literal string)
338 "Like `replace-match', but add the text properties found original text."
339 (setq newtext (org-add-props newtext (text-properties-at
340 (match-beginning 0) string)))
341 (replace-match newtext fixedcase literal string))
343 (defmacro org-save-outline-visibility (use-markers &rest body)
344 "Save and restore outline visibility around BODY.
345 If USE-MARKERS is non-nil, use markers for the positions.
346 This means that the buffer may change while running BODY,
347 but it also means that the buffer should stay alive
348 during the operation, because otherwise all these markers will
349 point nowhere."
350 (declare (indent 1))
351 (org-with-gensyms (data rtn)
352 `(let ((,data (org-outline-overlay-data ,use-markers))
353 ,rtn)
354 (unwind-protect
355 (progn
356 (setq ,rtn (progn ,@body))
357 (org-set-outline-overlay-data ,data))
358 (when ,use-markers
359 (mapc (lambda (c)
360 (and (markerp (car c)) (move-marker (car c) nil))
361 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
362 ,data)))
363 ,rtn)))
364 (def-edebug-spec org-save-outline-visibility (form body))
366 (defmacro org-with-wide-buffer (&rest body)
367 "Execute body while temporarily widening the buffer."
368 `(save-excursion
369 (save-restriction
370 (widen)
371 ,@body)))
372 (def-edebug-spec org-with-wide-buffer (body))
374 (defmacro org-with-limited-levels (&rest body)
375 "Execute BODY with limited number of outline levels."
376 `(let* ((org-called-with-limited-levels t)
377 (org-outline-regexp (org-get-limited-outline-regexp))
378 (outline-regexp org-outline-regexp)
379 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
380 ,@body))
381 (def-edebug-spec org-with-limited-levels (body))
383 (defvar org-outline-regexp) ; defined in org.el
384 (defvar org-odd-levels-only) ; defined in org.el
385 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
386 (defun org-get-limited-outline-regexp ()
387 "Return outline-regexp with limited number of levels.
388 The number of levels is controlled by `org-inlinetask-min-level'"
389 (if (or (not (derived-mode-p 'org-mode)) (not (featurep 'org-inlinetask)))
390 org-outline-regexp
391 (let* ((limit-level (1- org-inlinetask-min-level))
392 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
393 (format "\\*\\{1,%d\\} " nstars))))
395 (defun org-format-seconds (string seconds)
396 "Compatibility function replacing format-seconds."
397 (if (fboundp 'format-seconds)
398 (format-seconds string seconds)
399 (format-time-string string (seconds-to-time seconds))))
401 (defmacro org-eval-in-environment (environment form)
402 `(eval (list 'let ,environment ',form)))
403 (def-edebug-spec org-eval-in-environment (form form))
404 (put 'org-eval-in-environment 'lisp-indent-function 1)
406 (defun org-make-parameter-alist (flat)
407 "Return alist based on FLAT.
408 FLAT is a list with alternating symbol names and values. The
409 returned alist is a list of lists with the symbol name in car and
410 the value in cdr."
411 (when flat
412 (cons (list (car flat) (cadr flat))
413 (org-make-parameter-alist (cddr flat)))))
415 (provide 'org-macs)
417 ;;; org-macs.el ends here