Checkboxes: Enforce sequential processing with ORDERED property
[org-mode.git] / lisp / org-macs.el
blob86496b67d1275ded3ff213e4c63956e7310368d3
1 ;;; org-macs.el --- Top-level definitions for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
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: 6.22trans
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 (defmacro org-bound-and-true-p (var)
37 "Return the value of symbol VAR if it is bound, else nil."
38 `(and (boundp (quote ,var)) ,var))
40 (defmacro org-unmodified (&rest body)
41 "Execute body without changing `buffer-modified-p'."
42 `(set-buffer-modified-p
43 (prog1 (buffer-modified-p) ,@body)))
45 (defmacro org-re (s)
46 "Replace posix classes in regular expression."
47 (if (featurep 'xemacs)
48 (let ((ss s))
49 (save-match-data
50 (while (string-match "\\[:alnum:\\]" ss)
51 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
52 (while (string-match "\\[:word:\\]" ss)
53 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
54 (while (string-match "\\[:alpha:\\]" ss)
55 (setq ss (replace-match "a-zA-Z" t t ss)))
56 ss))
57 s))
59 (defmacro org-preserve-lc (&rest body)
60 `(let ((_line (org-current-line))
61 (_col (current-column)))
62 (unwind-protect
63 (progn ,@body)
64 (goto-line _line)
65 (org-move-to-column _col))))
67 (defmacro org-without-partial-completion (&rest body)
68 `(let ((pc-mode (and (boundp 'partial-completion-mode)
69 partial-completion-mode)))
70 (unwind-protect
71 (progn
72 (if pc-mode (partial-completion-mode -1))
73 ,@body)
74 (if pc-mode (partial-completion-mode 1)))))
76 (eval-and-compile
77 (unless (fboundp 'declare-function)
78 (defmacro declare-function (fn file &optional arglist fileonly))))
80 (defmacro org-maybe-intangible (props)
81 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
82 In emacs 21, invisible text is not avoided by the command loop, so the
83 intangible property is needed to make sure point skips this text.
84 In Emacs 22, this is not necessary. The intangible text property has
85 led to problems with flyspell. These problems are fixed in flyspell.el,
86 but we still avoid setting the property in Emacs 22 and later.
87 We use a macro so that the test can happen at compilation time."
88 (if (< emacs-major-version 22)
89 `(append '(intangible t) ,props)
90 props))
92 (defmacro org-with-point-at (pom &rest body)
93 "Move to buffer and point of point-or-marker POM for the duration of BODY."
94 `(save-excursion
95 (if (markerp ,pom) (set-buffer (marker-buffer ,pom)))
96 (save-excursion
97 (goto-char (or ,pom (point)))
98 ,@body)))
100 (defmacro org-no-warnings (&rest body)
101 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
103 (defmacro org-if-unprotected (&rest body)
104 "Execute BODY if there is no `org-protected' text property at point."
105 `(unless (get-text-property (point) 'org-protected)
106 ,@body))
108 (defmacro org-if-unprotected-1 (&rest body)
109 "Execute BODY if there is no `org-protected' text property at point-1."
110 `(unless (get-text-property (1- (point)) 'org-protected)
111 ,@body))
113 (defmacro org-if-unprotected-at (pos &rest body)
114 "Execute BODY if there is no `org-protected' text property at point-1."
115 `(unless (get-text-property ,pos 'org-protected)
116 ,@body))
118 (defmacro org-with-remote-undo (_buffer &rest _body)
119 "Execute BODY while recording undo information in two buffers."
120 `(let ((_cline (org-current-line))
121 (_cmd this-command)
122 (_buf1 (current-buffer))
123 (_buf2 ,_buffer)
124 (_undo1 buffer-undo-list)
125 (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
126 _c1 _c2)
127 ,@_body
128 (when org-agenda-allow-remote-undo
129 (setq _c1 (org-verify-change-for-undo
130 _undo1 (with-current-buffer _buf1 buffer-undo-list))
131 _c2 (org-verify-change-for-undo
132 _undo2 (with-current-buffer _buf2 buffer-undo-list)))
133 (when (or _c1 _c2)
134 ;; make sure there are undo boundaries
135 (and _c1 (with-current-buffer _buf1 (undo-boundary)))
136 (and _c2 (with-current-buffer _buf2 (undo-boundary)))
137 ;; remember which buffer to undo
138 (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
139 org-agenda-undo-list)))))
141 (defmacro org-no-read-only (&rest body)
142 "Inhibit read-only for BODY."
143 `(let ((inhibit-read-only t)) ,@body))
145 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
146 rear-nonsticky t mouse-map t fontified t)
147 "Properties to remove when a string without properties is wanted.")
149 (defsubst org-match-string-no-properties (num &optional string)
150 (if (featurep 'xemacs)
151 (let ((s (match-string num string)))
152 (and s (remove-text-properties 0 (length s) org-rm-props s))
154 (match-string-no-properties num string)))
156 (defsubst org-no-properties (s)
157 (if (fboundp 'set-text-properties)
158 (set-text-properties 0 (length s) nil s)
159 (remove-text-properties 0 (length s) org-rm-props s))
162 (defsubst org-get-alist-option (option key)
163 (cond ((eq key t) t)
164 ((eq option t) t)
165 ((assoc key option) (cdr (assoc key option)))
166 (t (cdr (assq 'default option)))))
168 (defsubst org-inhibit-invisibility ()
169 "Modified `buffer-invisibility-spec' for Emacs 21.
170 Some ops with invisible text do not work correctly on Emacs 21. For these
171 we turn off invisibility temporarily. Use this in a `let' form."
172 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
174 (defsubst org-set-local (var value)
175 "Make VAR local in current buffer and set it to VALUE."
176 (set (make-variable-buffer-local var) value))
178 (defsubst org-mode-p ()
179 "Check if the current buffer is in Org-mode."
180 (eq major-mode 'org-mode))
182 (defsubst org-last (list)
183 "Return the last element of LIST."
184 (car (last list)))
186 (defun org-let (list &rest body)
187 (eval (cons 'let (cons list body))))
188 (put 'org-let 'lisp-indent-function 1)
190 (defun org-let2 (list1 list2 &rest body)
191 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
192 (put 'org-let2 'lisp-indent-function 2)
194 (defsubst org-call-with-arg (command arg)
195 "Call COMMAND interactively, but pretend prefix arg was ARG."
196 (let ((current-prefix-arg arg)) (call-interactively command)))
198 (defsubst org-current-line (&optional pos)
199 (save-excursion
200 (and pos (goto-char pos))
201 ;; works also in narrowed buffer, because we start at 1, not point-min
202 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
204 (defsubst org-current-line-string (&optional to-here)
205 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
207 (defsubst org-pos-in-match-range (pos n)
208 (and (match-beginning n)
209 (<= (match-beginning n) pos)
210 (>= (match-end n) pos)))
212 (defun org-autoload (file functions)
213 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
214 (let ((d (format "Documentation will be available after `%s.el' is loaded."
215 file))
217 (while (setq f (pop functions))
218 (or (fboundp f) (autoload f file d t)))))
220 (defun org-match-line (re)
221 "Looking-at at the beginning of the current line."
222 (save-excursion
223 (goto-char (point-at-bol))
224 (looking-at re)))
226 (defun org-plist-delete (plist property)
227 "Delete PROPERTY from PLIST.
228 This is in contrast to merely setting it to 0."
229 (let (p)
230 (while plist
231 (if (not (eq property (car plist)))
232 (setq p (plist-put p (car plist) (nth 1 plist))))
233 (setq plist (cddr plist)))
236 (provide 'org-macs)
238 ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
240 ;;; org-macs.el ends here