Release 6.35b
[org-mode/org-tableheadings.git] / lisp / org-macs.el
blobd46af092f518d75e9390475bd106a5d873b19fbe
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.35b
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))))
40 (declare-function org-add-props "org-compat" (string plist &rest props))
42 (defmacro org-bound-and-true-p (var)
43 "Return the value of symbol VAR if it is bound, else nil."
44 `(and (boundp (quote ,var)) ,var))
46 (defmacro org-unmodified (&rest body)
47 "Execute body without changing `buffer-modified-p'.
48 Also, do not record undo information."
49 `(set-buffer-modified-p
50 (prog1 (buffer-modified-p)
51 (let ((buffer-undo-list t)
52 before-change-functions after-change-functions)
53 ,@body))))
55 (defmacro org-re (s)
56 "Replace posix classes in regular expression."
57 (if (featurep 'xemacs)
58 (let ((ss s))
59 (save-match-data
60 (while (string-match "\\[:alnum:\\]" ss)
61 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
62 (while (string-match "\\[:word:\\]" ss)
63 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
64 (while (string-match "\\[:alpha:\\]" ss)
65 (setq ss (replace-match "a-zA-Z" t t ss)))
66 (while (string-match "\\[:punct:\\]" ss)
67 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
68 ss))
69 s))
71 (defmacro org-preserve-lc (&rest body)
72 `(let ((_line (org-current-line))
73 (_col (current-column)))
74 (unwind-protect
75 (progn ,@body)
76 (org-goto-line _line)
77 (org-move-to-column _col))))
79 (defmacro org-without-partial-completion (&rest body)
80 `(let ((pc-mode (and (boundp 'partial-completion-mode)
81 partial-completion-mode)))
82 (unwind-protect
83 (progn
84 (if pc-mode (partial-completion-mode -1))
85 ,@body)
86 (if pc-mode (partial-completion-mode 1)))))
88 (defmacro org-maybe-intangible (props)
89 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
90 In emacs 21, invisible text is not avoided by the command loop, so the
91 intangible property is needed to make sure point skips this text.
92 In Emacs 22, this is not necessary. The intangible text property has
93 led to problems with flyspell. These problems are fixed in flyspell.el,
94 but we still avoid setting the property in Emacs 22 and later.
95 We use a macro so that the test can happen at compilation time."
96 (if (< emacs-major-version 22)
97 `(append '(intangible t) ,props)
98 props))
100 (defmacro org-with-point-at (pom &rest body)
101 "Move to buffer and point of point-or-marker POM for the duration of BODY."
102 `(save-excursion
103 (if (markerp ,pom) (set-buffer (marker-buffer ,pom)))
104 (save-excursion
105 (goto-char (or ,pom (point)))
106 ,@body)))
107 (put 'org-with-point-at 'lisp-indent-function 1)
109 (defmacro org-no-warnings (&rest body)
110 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
112 (defmacro org-if-unprotected (&rest body)
113 "Execute BODY if there is no `org-protected' text property at point."
114 `(unless (get-text-property (point) 'org-protected)
115 ,@body))
117 (defmacro org-if-unprotected-1 (&rest body)
118 "Execute BODY if there is no `org-protected' text property at point-1."
119 `(unless (get-text-property (1- (point)) 'org-protected)
120 ,@body))
122 (defmacro org-if-unprotected-at (pos &rest body)
123 "Execute BODY if there is no `org-protected' text property at POS."
124 `(unless (get-text-property ,pos 'org-protected)
125 ,@body))
126 (put 'org-if-unprotected-at 'lisp-indent-function 1)
128 (defun org-re-search-forward-unprotected (&rest args)
129 "Like re-search-forward, but stop only in unprotected places."
130 (catch 'exit
131 (while t
132 (unless (apply 're-search-forward args)
133 (throw 'exit nil))
134 (unless (get-text-property (match-beginning 0) 'org-protected)
135 (throw 'exit (point))))))
137 (defmacro org-with-remote-undo (_buffer &rest _body)
138 "Execute BODY while recording undo information in two buffers."
139 `(let ((_cline (org-current-line))
140 (_cmd this-command)
141 (_buf1 (current-buffer))
142 (_buf2 ,_buffer)
143 (_undo1 buffer-undo-list)
144 (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
145 _c1 _c2)
146 ,@_body
147 (when org-agenda-allow-remote-undo
148 (setq _c1 (org-verify-change-for-undo
149 _undo1 (with-current-buffer _buf1 buffer-undo-list))
150 _c2 (org-verify-change-for-undo
151 _undo2 (with-current-buffer _buf2 buffer-undo-list)))
152 (when (or _c1 _c2)
153 ;; make sure there are undo boundaries
154 (and _c1 (with-current-buffer _buf1 (undo-boundary)))
155 (and _c2 (with-current-buffer _buf2 (undo-boundary)))
156 ;; remember which buffer to undo
157 (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
158 org-agenda-undo-list)))))
160 (defmacro org-no-read-only (&rest body)
161 "Inhibit read-only for BODY."
162 `(let ((inhibit-read-only t)) ,@body))
164 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
165 rear-nonsticky t mouse-map t fontified t)
166 "Properties to remove when a string without properties is wanted.")
168 (defsubst org-match-string-no-properties (num &optional string)
169 (if (featurep 'xemacs)
170 (let ((s (match-string num string)))
171 (and s (remove-text-properties 0 (length s) org-rm-props s))
173 (match-string-no-properties num string)))
175 (defsubst org-no-properties (s)
176 (if (fboundp 'set-text-properties)
177 (set-text-properties 0 (length s) nil s)
178 (remove-text-properties 0 (length s) org-rm-props s))
181 (defsubst org-get-alist-option (option key)
182 (cond ((eq key t) t)
183 ((eq option t) t)
184 ((assoc key option) (cdr (assoc key option)))
185 (t (cdr (assq 'default option)))))
187 (defsubst org-check-external-command (cmd &optional use no-error)
188 "Check if external program CMD for USE exists, error if not.
189 When the program does exist, return its path.
190 When it does not exist and NO-ERROR is set, return nil.
191 Otherwise, throw an error. The optional argument USE can describe what this
192 program is needed for, so that the error message can be more informative."
193 (or (executable-find cmd)
194 (if no-error
196 (error "Can't find `%s'%s" cmd
197 (if use (format " (%s)" use) "")))))
199 (defsubst org-inhibit-invisibility ()
200 "Modified `buffer-invisibility-spec' for Emacs 21.
201 Some ops with invisible text do not work correctly on Emacs 21. For these
202 we turn off invisibility temporarily. Use this in a `let' form."
203 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
205 (defsubst org-set-local (var value)
206 "Make VAR local in current buffer and set it to VALUE."
207 (set (make-local-variable var) value))
209 (defsubst org-mode-p ()
210 "Check if the current buffer is in Org-mode."
211 (eq major-mode 'org-mode))
213 (defsubst org-last (list)
214 "Return the last element of LIST."
215 (car (last list)))
217 (defun org-let (list &rest body)
218 (eval (cons 'let (cons list body))))
219 (put 'org-let 'lisp-indent-function 1)
221 (defun org-let2 (list1 list2 &rest body)
222 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
223 (put 'org-let2 'lisp-indent-function 2)
225 (defsubst org-call-with-arg (command arg)
226 "Call COMMAND interactively, but pretend prefix arg was ARG."
227 (let ((current-prefix-arg arg)) (call-interactively command)))
229 (defsubst org-current-line (&optional pos)
230 (save-excursion
231 (and pos (goto-char pos))
232 ;; works also in narrowed buffer, because we start at 1, not point-min
233 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
235 (defsubst org-goto-line (N)
236 (save-restriction
237 (widen)
238 (goto-char (point-min))
239 (forward-line (1- N))))
241 (defsubst org-current-line-string (&optional to-here)
242 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
244 (defsubst org-pos-in-match-range (pos n)
245 (and (match-beginning n)
246 (<= (match-beginning n) pos)
247 (>= (match-end n) pos)))
249 (defun org-autoload (file functions)
250 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
251 (let ((d (format "Documentation will be available after `%s.el' is loaded."
252 file))
254 (while (setq f (pop functions))
255 (or (fboundp f) (autoload f file d t)))))
257 (defun org-match-line (re)
258 "Looking-at at the beginning of the current line."
259 (save-excursion
260 (goto-char (point-at-bol))
261 (looking-at re)))
263 (defun org-plist-delete (plist property)
264 "Delete PROPERTY from PLIST.
265 This is in contrast to merely setting it to 0."
266 (let (p)
267 (while plist
268 (if (not (eq property (car plist)))
269 (setq p (plist-put p (car plist) (nth 1 plist))))
270 (setq plist (cddr plist)))
274 (defun org-replace-match-keep-properties (newtext &optional fixedcase
275 literal string)
276 "Like `replace-match', but add the text properties found original text."
277 (setq newtext (org-add-props newtext (text-properties-at
278 (match-beginning 0) string)))
279 (replace-match newtext fixedcase literal string))
281 (defmacro org-with-limited-levels (&rest body)
282 "Execute BODY with limited number of outline levels."
283 `(let* ((outline-regexp (org-get-limited-outline-regexp)))
284 ,@body))
286 (defvar org-odd-levels-only) ; defined in org.el
287 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
288 (defun org-get-limited-outline-regexp ()
289 "Return outline-regexp with limited number of levels.
290 The number of levels is controlled by "
291 (if (or (not (org-mode-p)) (not (featurep 'org-inlinetask)))
293 outline-regexp
294 (let* ((limit-level (1- org-inlinetask-min-level))
295 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
296 (format "\\*\\{1,%d\\} " nstars))))
298 (provide 'org-macs)
300 ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
302 ;;; org-macs.el ends here