test: tests for expanding noweb references
[org-mode/org-jambu.git] / lisp / org-macs.el
blob27e1d8808a7ce39227b5fb768f165b0eec9b69f4
1 ;;; org-macs.el --- Top-level definitions for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.5
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains macro definitions, defsubst definitions, other
30 ;; stuff needed for compilation and top-level forms in Org-mode, as well
31 ;; lots of small functions that are not org-mode specific but simply
32 ;; generally useful stuff.
34 ;;; Code:
36 (eval-and-compile
37 (unless (fboundp 'declare-function)
38 (defmacro declare-function (fn file &optional arglist fileonly)))
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-called-interactively-p (&optional kind)
51 `(if (featurep 'xemacs)
52 (interactive-p)
53 (if (or (> emacs-major-version 23)
54 (and (>= emacs-major-version 23)
55 (>= emacs-minor-version 2)))
56 (with-no-warnings (called-interactively-p ,kind)) ;; defined with no argument in <=23.1
57 (interactive-p))))
59 (if (and (not (fboundp 'with-silent-modifications))
60 (or (< emacs-major-version 23)
61 (and (= emacs-major-version 23)
62 (< emacs-minor-version 2))))
63 (defmacro with-silent-modifications (&rest body)
64 `(org-unmodified ,@body)))
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))
70 (defun org-string-nw-p (s)
71 "Is S a string with a non-white character?"
72 (and (stringp s)
73 (org-string-match-p "\\S-" s)
74 s))
76 (defun org-not-nil (v)
77 "If V not nil, and also not the string \"nil\", then return V.
78 Otherwise return nil."
79 (and v (not (equal v "nil")) v))
81 (defmacro org-unmodified (&rest body)
82 "Execute body without changing `buffer-modified-p'.
83 Also, do not record undo information."
84 `(set-buffer-modified-p
85 (prog1 (buffer-modified-p)
86 (let ((buffer-undo-list t)
87 before-change-functions after-change-functions)
88 ,@body))))
90 (defmacro org-re (s)
91 "Replace posix classes in regular expression."
92 (if (featurep 'xemacs)
93 (let ((ss s))
94 (save-match-data
95 (while (string-match "\\[:alnum:\\]" ss)
96 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
97 (while (string-match "\\[:word:\\]" ss)
98 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
99 (while (string-match "\\[:alpha:\\]" ss)
100 (setq ss (replace-match "a-zA-Z" t t ss)))
101 (while (string-match "\\[:punct:\\]" ss)
102 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
103 ss))
106 (defmacro org-preserve-lc (&rest body)
107 `(let ((_line (org-current-line))
108 (_col (current-column)))
109 (unwind-protect
110 (progn ,@body)
111 (org-goto-line _line)
112 (org-move-to-column _col))))
114 (defmacro org-without-partial-completion (&rest body)
115 `(let ((pc-mode (and (boundp 'partial-completion-mode)
116 'partial-completion-mode)))
117 (unwind-protect
118 (progn
119 (when pc-mode (funcall pc-mode -1))
120 ,@body)
121 (when pc-mode (funcall pc-mode 1)))))
123 (defmacro org-maybe-intangible (props)
124 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
125 In Emacs 21, invisible text is not avoided by the command loop, so the
126 intangible property is needed to make sure point skips this text.
127 In Emacs 22, this is not necessary. The intangible text property has
128 led to problems with flyspell. These problems are fixed in flyspell.el,
129 but we still avoid setting the property in Emacs 22 and later.
130 We use a macro so that the test can happen at compilation time."
131 (if (< emacs-major-version 22)
132 `(append '(intangible t) ,props)
133 props))
135 (defmacro org-with-point-at (pom &rest body)
136 "Move to buffer and point of point-or-marker POM for the duration of BODY."
137 `(let ((pom ,pom))
138 (save-excursion
139 (if (markerp pom) (set-buffer (marker-buffer pom)))
140 (save-excursion
141 (goto-char (or pom (point)))
142 ,@body))))
143 (put 'org-with-point-at 'lisp-indent-function 1)
145 (defmacro org-no-warnings (&rest body)
146 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
148 (defmacro org-if-unprotected (&rest body)
149 "Execute BODY if there is no `org-protected' text property at point."
150 `(unless (get-text-property (point) 'org-protected)
151 ,@body))
153 (defmacro org-if-unprotected-1 (&rest body)
154 "Execute BODY if there is no `org-protected' text property at point-1."
155 `(unless (get-text-property (1- (point)) 'org-protected)
156 ,@body))
158 (defmacro org-if-unprotected-at (pos &rest body)
159 "Execute BODY if there is no `org-protected' text property at POS."
160 `(unless (get-text-property ,pos 'org-protected)
161 ,@body))
162 (put 'org-if-unprotected-at 'lisp-indent-function 1)
164 (defun org-re-search-forward-unprotected (&rest args)
165 "Like re-search-forward, but stop only in unprotected places."
166 (catch 'exit
167 (while t
168 (unless (apply 're-search-forward args)
169 (throw 'exit nil))
170 (unless (get-text-property (match-beginning 0) 'org-protected)
171 (throw 'exit (point))))))
173 (defmacro org-with-remote-undo (_buffer &rest _body)
174 "Execute BODY while recording undo information in two buffers."
175 `(let ((_cline (org-current-line))
176 (_cmd this-command)
177 (_buf1 (current-buffer))
178 (_buf2 ,_buffer)
179 (_undo1 buffer-undo-list)
180 (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
181 _c1 _c2)
182 ,@_body
183 (when org-agenda-allow-remote-undo
184 (setq _c1 (org-verify-change-for-undo
185 _undo1 (with-current-buffer _buf1 buffer-undo-list))
186 _c2 (org-verify-change-for-undo
187 _undo2 (with-current-buffer _buf2 buffer-undo-list)))
188 (when (or _c1 _c2)
189 ;; make sure there are undo boundaries
190 (and _c1 (with-current-buffer _buf1 (undo-boundary)))
191 (and _c2 (with-current-buffer _buf2 (undo-boundary)))
192 ;; remember which buffer to undo
193 (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
194 org-agenda-undo-list)))))
195 (put 'org-with-remote-undo 'lisp-indent-function 1)
197 (defmacro org-no-read-only (&rest body)
198 "Inhibit read-only for BODY."
199 `(let ((inhibit-read-only t)) ,@body))
201 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
202 rear-nonsticky t mouse-map t fontified t
203 org-emphasis t)
204 "Properties to remove when a string without properties is wanted.")
206 (defsubst org-match-string-no-properties (num &optional string)
207 (if (featurep 'xemacs)
208 (let ((s (match-string num string)))
209 (and s (remove-text-properties 0 (length s) org-rm-props s))
211 (match-string-no-properties num string)))
213 (defsubst org-no-properties (s)
214 (if (fboundp 'set-text-properties)
215 (set-text-properties 0 (length s) nil s)
216 (remove-text-properties 0 (length s) org-rm-props 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 (cdr (assq 'default option)))))
225 (defsubst org-check-external-command (cmd &optional use no-error)
226 "Check if external program CMD for USE exists, error if not.
227 When the program does exist, return its path.
228 When it does not exist and NO-ERROR is set, return nil.
229 Otherwise, throw an error. The optional argument USE can describe what this
230 program is needed for, so that the error message can be more informative."
231 (or (executable-find cmd)
232 (if no-error
234 (error "Can't find `%s'%s" cmd
235 (if use (format " (%s)" use) "")))))
237 (defsubst org-inhibit-invisibility ()
238 "Modified `buffer-invisibility-spec' for Emacs 21.
239 Some ops with invisible text do not work correctly on Emacs 21. For these
240 we turn off invisibility temporarily. Use this in a `let' form."
241 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
243 (defsubst org-set-local (var value)
244 "Make VAR local in current buffer and set it to VALUE."
245 (set (make-local-variable var) value))
247 (defsubst org-mode-p ()
248 "Check if the current buffer is in Org-mode."
249 (eq major-mode 'org-mode))
251 (defsubst org-last (list)
252 "Return the last element of LIST."
253 (car (last list)))
255 (defun org-let (list &rest body)
256 (eval (cons 'let (cons list body))))
257 (put 'org-let 'lisp-indent-function 1)
259 (defun org-let2 (list1 list2 &rest body)
260 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
261 (put 'org-let2 'lisp-indent-function 2)
263 (defsubst org-call-with-arg (command arg)
264 "Call COMMAND interactively, but pretend prefix arg was ARG."
265 (let ((current-prefix-arg arg)) (call-interactively command)))
267 (defsubst org-current-line (&optional pos)
268 (save-excursion
269 (and pos (goto-char pos))
270 ;; works also in narrowed buffer, because we start at 1, not point-min
271 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
273 (defsubst org-goto-line (N)
274 (save-restriction
275 (widen)
276 (goto-char (point-min))
277 (forward-line (1- N))))
279 (defsubst org-current-line-string (&optional to-here)
280 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
282 (defsubst org-pos-in-match-range (pos n)
283 (and (match-beginning n)
284 (<= (match-beginning n) pos)
285 (>= (match-end n) pos)))
287 (defun org-autoload (file functions)
288 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
289 (let ((d (format "Documentation will be available after `%s.el' is loaded."
290 file))
292 (while (setq f (pop functions))
293 (or (fboundp f) (autoload f file d t)))))
295 (defun org-match-line (re)
296 "Looking-at at the beginning of the current line."
297 (save-excursion
298 (goto-char (point-at-bol))
299 (looking-at re)))
301 (defun org-plist-delete (plist property)
302 "Delete PROPERTY from PLIST.
303 This is in contrast to merely setting it to 0."
304 (let (p)
305 (while plist
306 (if (not (eq property (car plist)))
307 (setq p (plist-put p (car plist) (nth 1 plist))))
308 (setq plist (cddr plist)))
311 (defun org-replace-match-keep-properties (newtext &optional fixedcase
312 literal string)
313 "Like `replace-match', but add the text properties found original text."
314 (setq newtext (org-add-props newtext (text-properties-at
315 (match-beginning 0) string)))
316 (replace-match newtext fixedcase literal string))
318 (defmacro org-save-outline-visibility (use-markers &rest body)
319 "Save and restore outline visibility around BODY.
320 If USE-MARKERS is non-nil, use markers for the positions.
321 This means that the buffer may change while running BODY,
322 but it also means that the buffer should stay alive
323 during the operation, because otherwise all these markers will
324 point nowhere."
325 (declare (indent 1))
326 `(let ((data (org-outline-overlay-data ,use-markers))
327 rtn)
328 (unwind-protect
329 (progn
330 (setq rtn (progn ,@body))
331 (org-set-outline-overlay-data data))
332 (when ,use-markers
333 (mapc (lambda (c)
334 (and (markerp (car c)) (move-marker (car c) nil))
335 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
336 data)))
337 rtn))
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)))
346 (defmacro org-with-limited-levels (&rest body)
347 "Execute BODY with limited number of outline levels."
348 `(let* ((outline-regexp (org-get-limited-outline-regexp)))
349 ,@body))
351 (defvar org-odd-levels-only) ; defined in org.el
352 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
353 (defun org-get-limited-outline-regexp ()
354 "Return outline-regexp with limited number of levels.
355 The number of levels is controlled by `org-inlinetask-min-level'"
356 (if (or (not (org-mode-p)) (not (featurep 'org-inlinetask)))
357 outline-regexp
358 (let* ((limit-level (1- org-inlinetask-min-level))
359 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
360 (format "\\*\\{1,%d\\} " nstars))))
362 (provide 'org-macs)
364 ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
366 ;;; org-macs.el ends here