Release 6.21a
[org-mode/org-tableheadings.git] / lisp / org-macs.el
blob399d14d8ebbdc6b2d155b7aa18858d0fbe794e2c
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.21a
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-with-remote-undo (_buffer &rest _body)
114 "Execute BODY while recording undo information in two buffers."
115 `(let ((_cline (org-current-line))
116 (_cmd this-command)
117 (_buf1 (current-buffer))
118 (_buf2 ,_buffer)
119 (_undo1 buffer-undo-list)
120 (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
121 _c1 _c2)
122 ,@_body
123 (when org-agenda-allow-remote-undo
124 (setq _c1 (org-verify-change-for-undo
125 _undo1 (with-current-buffer _buf1 buffer-undo-list))
126 _c2 (org-verify-change-for-undo
127 _undo2 (with-current-buffer _buf2 buffer-undo-list)))
128 (when (or _c1 _c2)
129 ;; make sure there are undo boundaries
130 (and _c1 (with-current-buffer _buf1 (undo-boundary)))
131 (and _c2 (with-current-buffer _buf2 (undo-boundary)))
132 ;; remember which buffer to undo
133 (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
134 org-agenda-undo-list)))))
136 (defmacro org-no-read-only (&rest body)
137 "Inhibit read-only for BODY."
138 `(let ((inhibit-read-only t)) ,@body))
140 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
141 rear-nonsticky t mouse-map t fontified t)
142 "Properties to remove when a string without properties is wanted.")
144 (defsubst org-match-string-no-properties (num &optional string)
145 (if (featurep 'xemacs)
146 (let ((s (match-string num string)))
147 (and s (remove-text-properties 0 (length s) org-rm-props s))
149 (match-string-no-properties num string)))
151 (defsubst org-no-properties (s)
152 (if (fboundp 'set-text-properties)
153 (set-text-properties 0 (length s) nil s)
154 (remove-text-properties 0 (length s) org-rm-props s))
157 (defsubst org-get-alist-option (option key)
158 (cond ((eq key t) t)
159 ((eq option t) t)
160 ((assoc key option) (cdr (assoc key option)))
161 (t (cdr (assq 'default option)))))
163 (defsubst org-inhibit-invisibility ()
164 "Modified `buffer-invisibility-spec' for Emacs 21.
165 Some ops with invisible text do not work correctly on Emacs 21. For these
166 we turn off invisibility temporarily. Use this in a `let' form."
167 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
169 (defsubst org-set-local (var value)
170 "Make VAR local in current buffer and set it to VALUE."
171 (set (make-variable-buffer-local var) value))
173 (defsubst org-mode-p ()
174 "Check if the current buffer is in Org-mode."
175 (eq major-mode 'org-mode))
177 (defsubst org-last (list)
178 "Return the last element of LIST."
179 (car (last list)))
181 (defun org-let (list &rest body)
182 (eval (cons 'let (cons list body))))
183 (put 'org-let 'lisp-indent-function 1)
185 (defun org-let2 (list1 list2 &rest body)
186 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
187 (put 'org-let2 'lisp-indent-function 2)
189 (defsubst org-call-with-arg (command arg)
190 "Call COMMAND interactively, but pretend prefix arg was ARG."
191 (let ((current-prefix-arg arg)) (call-interactively command)))
193 (defsubst org-current-line (&optional pos)
194 (save-excursion
195 (and pos (goto-char pos))
196 ;; works also in narrowed buffer, because we start at 1, not point-min
197 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
199 (defsubst org-current-line-string (&optional to-here)
200 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
202 (defsubst org-pos-in-match-range (pos n)
203 (and (match-beginning n)
204 (<= (match-beginning n) pos)
205 (>= (match-end n) pos)))
207 (defun org-autoload (file functions)
208 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
209 (let ((d (format "Documentation will be available after `%s.el' is loaded."
210 file))
212 (while (setq f (pop functions))
213 (or (fboundp f) (autoload f file d t)))))
215 (defun org-match-line (re)
216 "Looking-at at the beginning of the current line."
217 (save-excursion
218 (goto-char (point-at-bol))
219 (looking-at re)))
221 (defun org-plist-delete (plist property)
222 "Delete PROPERTY from PLIST.
223 This is in contrast to merely setting it to 0."
224 (let (p)
225 (while plist
226 (if (not (eq property (car plist)))
227 (setq p (plist-put p (car plist) (nth 1 plist))))
228 (setq plist (cddr plist)))
231 (provide 'org-macs)
233 ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
235 ;;; org-macs.el ends here