testing/README: Fix instructions
[org-mode.git] / lisp / org-macs.el
blob974a36634f76b00ebce7f3823f8fe0d751e2cb01
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 (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 ;; Copied from bookmark.el
130 (defmacro org-with-buffer-modified-unmodified (&rest body)
131 "Run BODY while preserving the buffer's `buffer-modified-p' state."
132 (org-with-gensyms (was-modified)
133 `(let ((,was-modified (buffer-modified-p)))
134 (unwind-protect
135 (progn ,@body)
136 (set-buffer-modified-p ,was-modified)))))
138 (defmacro org-without-partial-completion (&rest body)
139 `(if (and (boundp 'partial-completion-mode)
140 partial-completion-mode
141 (fboundp 'partial-completion-mode))
142 (unwind-protect
143 (progn
144 (partial-completion-mode -1)
145 ,@body)
146 (partial-completion-mode 1))
147 ,@body))
148 (def-edebug-spec org-without-partial-completion (body))
150 ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
151 (defmacro org-maybe-intangible (props)
152 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
153 In Emacs 21, invisible text is not avoided by the command loop, so the
154 intangible property is needed to make sure point skips this text.
155 In Emacs 22, this is not necessary. The intangible text property has
156 led to problems with flyspell. These problems are fixed in flyspell.el,
157 but we still avoid setting the property in Emacs 22 and later.
158 We use a macro so that the test can happen at compilation time."
159 (if (< emacs-major-version 22)
160 `(append '(intangible t) ,props)
161 props))
163 (defmacro org-with-point-at (pom &rest body)
164 "Move to buffer and point of point-or-marker POM for the duration of BODY."
165 (org-with-gensyms (mpom)
166 `(let ((,mpom ,pom))
167 (save-excursion
168 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
169 (save-excursion
170 (goto-char (or ,mpom (point)))
171 ,@body)))))
172 (def-edebug-spec org-with-point-at (form body))
173 (put 'org-with-point-at 'lisp-indent-function 1)
175 (defmacro org-no-warnings (&rest body)
176 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
177 (def-edebug-spec org-no-warnings (body))
179 (defmacro org-if-unprotected (&rest body)
180 "Execute BODY if there is no `org-protected' text property at point."
181 `(unless (get-text-property (point) 'org-protected)
182 ,@body))
183 (def-edebug-spec org-if-unprotected (body))
185 (defmacro org-if-unprotected-1 (&rest body)
186 "Execute BODY if there is no `org-protected' text property at point-1."
187 `(unless (get-text-property (1- (point)) 'org-protected)
188 ,@body))
189 (def-edebug-spec org-if-unprotected-1 (body))
191 (defmacro org-if-unprotected-at (pos &rest body)
192 "Execute BODY if there is no `org-protected' text property at POS."
193 `(unless (get-text-property ,pos 'org-protected)
194 ,@body))
195 (def-edebug-spec org-if-unprotected-at (form body))
196 (put 'org-if-unprotected-at 'lisp-indent-function 1)
198 (defun org-re-search-forward-unprotected (&rest args)
199 "Like re-search-forward, but stop only in unprotected places."
200 (catch 'exit
201 (while t
202 (unless (apply 're-search-forward args)
203 (throw 'exit nil))
204 (unless (get-text-property (match-beginning 0) 'org-protected)
205 (throw 'exit (point))))))
207 ;; FIXME: Normalize argument names
208 (defmacro org-with-remote-undo (_buffer &rest _body)
209 "Execute BODY while recording undo information in two buffers."
210 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
211 `(let ((,cline (org-current-line))
212 (,cmd this-command)
213 (,buf1 (current-buffer))
214 (,buf2 ,_buffer)
215 (,undo1 buffer-undo-list)
216 (,undo2 (with-current-buffer ,_buffer buffer-undo-list))
217 ,c1 ,c2)
218 ,@_body
219 (when org-agenda-allow-remote-undo
220 (setq ,c1 (org-verify-change-for-undo
221 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
222 ,c2 (org-verify-change-for-undo
223 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
224 (when (or ,c1 ,c2)
225 ;; make sure there are undo boundaries
226 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
227 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
228 ;; remember which buffer to undo
229 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
230 org-agenda-undo-list))))))
231 (def-edebug-spec org-with-remote-undo (form body))
232 (put 'org-with-remote-undo 'lisp-indent-function 1)
234 (defmacro org-no-read-only (&rest body)
235 "Inhibit read-only for BODY."
236 `(let ((inhibit-read-only t)) ,@body))
237 (def-edebug-spec org-no-read-only (body))
239 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
240 rear-nonsticky t mouse-map t fontified t
241 org-emphasis t)
242 "Properties to remove when a string without properties is wanted.")
244 (defsubst org-match-string-no-properties (num &optional string)
245 (if (featurep 'xemacs)
246 (let ((s (match-string num string)))
247 (and s (remove-text-properties 0 (length s) org-rm-props s))
249 (match-string-no-properties num string)))
251 (defsubst org-no-properties (s &optional restricted)
252 "Remove all text properties from string S.
253 When RESTRICTED is non-nil, only remove the properties listed
254 in `org-rm-props'."
255 (if (fboundp 'set-text-properties)
256 (set-text-properties 0 (length s) nil s)
257 (if restricted
258 (remove-text-properties 0 (length s) org-rm-props s)
259 (set-text-properties 0 (length s) nil s)))
262 (defsubst org-get-alist-option (option key)
263 (cond ((eq key t) t)
264 ((eq option t) t)
265 ((assoc key option) (cdr (assoc key option)))
266 (t (cdr (assq 'default option)))))
268 (defsubst org-check-external-command (cmd &optional use no-error)
269 "Check if external program CMD for USE exists, error if not.
270 When the program does exist, return its path.
271 When it does not exist and NO-ERROR is set, return nil.
272 Otherwise, throw an error. The optional argument USE can describe what this
273 program is needed for, so that the error message can be more informative."
274 (or (executable-find cmd)
275 (if no-error
277 (error "Can't find `%s'%s" cmd
278 (if use (format " (%s)" use) "")))))
280 (defsubst org-inhibit-invisibility ()
281 "Modified `buffer-invisibility-spec' for Emacs 21.
282 Some ops with invisible text do not work correctly on Emacs 21. For these
283 we turn off invisibility temporarily. Use this in a `let' form."
284 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
286 (defsubst org-set-local (var value)
287 "Make VAR local in current buffer and set it to VALUE."
288 (set (make-local-variable var) value))
290 (defsubst org-last (list)
291 "Return the last element of LIST."
292 (car (last list)))
294 (defun org-let (list &rest body)
295 (eval (cons 'let (cons list body))))
296 (put 'org-let 'lisp-indent-function 1)
298 (defun org-let2 (list1 list2 &rest body)
299 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
300 (put 'org-let2 'lisp-indent-function 2)
302 (defsubst org-call-with-arg (command arg)
303 "Call COMMAND interactively, but pretend prefix arg was ARG."
304 (let ((current-prefix-arg arg)) (call-interactively command)))
306 (defsubst org-current-line (&optional pos)
307 (save-excursion
308 (and pos (goto-char pos))
309 ;; works also in narrowed buffer, because we start at 1, not point-min
310 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
312 (defsubst org-goto-line (N)
313 (save-restriction
314 (widen)
315 (goto-char (point-min))
316 (forward-line (1- N))))
318 (defsubst org-current-line-string (&optional to-here)
319 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
321 (defsubst org-pos-in-match-range (pos n)
322 (and (match-beginning n)
323 (<= (match-beginning n) pos)
324 (>= (match-end n) pos)))
326 (defun org-autoload (file functions)
327 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
328 (let ((d (format "Documentation will be available after `%s.el' is loaded."
329 file))
331 (while (setq f (pop functions))
332 (or (fboundp f) (autoload f file d t)))))
334 (defun org-match-line (re)
335 "Looking-at at the beginning of the current line."
336 (save-excursion
337 (goto-char (point-at-bol))
338 (looking-at re)))
340 (defun org-plist-delete (plist property)
341 "Delete PROPERTY from PLIST.
342 This is in contrast to merely setting it to 0."
343 (let (p)
344 (while plist
345 (if (not (eq property (car plist)))
346 (setq p (plist-put p (car plist) (nth 1 plist))))
347 (setq plist (cddr plist)))
350 (defun org-replace-match-keep-properties (newtext &optional fixedcase
351 literal string)
352 "Like `replace-match', but add the text properties found original text."
353 (setq newtext (org-add-props newtext (text-properties-at
354 (match-beginning 0) string)))
355 (replace-match newtext fixedcase literal string))
357 (defmacro org-save-outline-visibility (use-markers &rest body)
358 "Save and restore outline visibility around BODY.
359 If USE-MARKERS is non-nil, use markers for the positions.
360 This means that the buffer may change while running BODY,
361 but it also means that the buffer should stay alive
362 during the operation, because otherwise all these markers will
363 point nowhere."
364 (declare (indent 1))
365 (org-with-gensyms (data rtn)
366 `(let ((,data (org-outline-overlay-data ,use-markers))
367 ,rtn)
368 (unwind-protect
369 (progn
370 (setq ,rtn (progn ,@body))
371 (org-set-outline-overlay-data ,data))
372 (when ,use-markers
373 (mapc (lambda (c)
374 (and (markerp (car c)) (move-marker (car c) nil))
375 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
376 ,data)))
377 ,rtn)))
378 (def-edebug-spec org-save-outline-visibility (form body))
380 (defmacro org-with-wide-buffer (&rest body)
381 "Execute body while temporarily widening the buffer."
382 `(save-excursion
383 (save-restriction
384 (widen)
385 ,@body)))
386 (def-edebug-spec org-with-wide-buffer (body))
388 (defmacro org-with-limited-levels (&rest body)
389 "Execute BODY with limited number of outline levels."
390 `(let* ((org-called-with-limited-levels t)
391 (org-outline-regexp (org-get-limited-outline-regexp))
392 (outline-regexp org-outline-regexp)
393 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
394 ,@body))
395 (def-edebug-spec org-with-limited-levels (body))
397 (defvar org-outline-regexp) ; defined in org.el
398 (defvar org-odd-levels-only) ; defined in org.el
399 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
400 (defun org-get-limited-outline-regexp ()
401 "Return outline-regexp with limited number of levels.
402 The number of levels is controlled by `org-inlinetask-min-level'"
403 (if (or (not (derived-mode-p 'org-mode)) (not (featurep 'org-inlinetask)))
404 org-outline-regexp
405 (let* ((limit-level (1- org-inlinetask-min-level))
406 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
407 (format "\\*\\{1,%d\\} " nstars))))
409 (defun org-format-seconds (string seconds)
410 "Compatibility function replacing format-seconds."
411 (if (fboundp 'format-seconds)
412 (format-seconds string seconds)
413 (format-time-string string (seconds-to-time seconds))))
415 (defmacro org-eval-in-environment (environment form)
416 `(eval (list 'let ,environment ',form)))
417 (def-edebug-spec org-eval-in-environment (form form))
418 (put 'org-eval-in-environment 'lisp-indent-function 1)
420 (defun org-make-parameter-alist (flat)
421 "Return alist based on FLAT.
422 FLAT is a list with alternating symbol names and values. The
423 returned alist is a list of lists with the symbol name in car and
424 the value in cdr."
425 (when flat
426 (cons (list (car flat) (cadr flat))
427 (org-make-parameter-alist (cddr flat)))))
429 ;;;###autoload
430 (defmacro org-load-noerror-mustsuffix (file)
431 "Load FILE with optional arguments NOERROR and MUSTSUFFIX. Drop the MUSTSUFFIX argument for XEmacs, which doesn't recognize it."
432 (if (featurep 'xemacs)
433 `(load ,file 'noerror)
434 `(load ,file 'noerror nil nil 'mustsuffix)))
436 (provide 'org-macs)
438 ;;; org-macs.el ends here