Release 6.19
[org-mode.git] / lisp / org-macs.el
blobd79bc253ff4149994829de8d2f4fed3273664cd3
1 ;;; org-macs.el --- Top-level definitions for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 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 ;; Version: 6.19
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file contains macro definitions, defsubst definitions, other
29 ;; stuff needed for compilation and top-level forms in Org-mode, as well
30 ;; lots of small functions that are not org-mode specific but simply
31 ;; generally useful stuff.
33 ;;; Code:
35 (defmacro org-bound-and-true-p (var)
36 "Return the value of symbol VAR if it is bound, else nil."
37 `(and (boundp (quote ,var)) ,var))
39 (defmacro org-unmodified (&rest body)
40 "Execute body without changing `buffer-modified-p'."
41 `(set-buffer-modified-p
42 (prog1 (buffer-modified-p) ,@body)))
44 (defmacro org-re (s)
45 "Replace posix classes in regular expression."
46 (if (featurep 'xemacs)
47 (let ((ss s))
48 (save-match-data
49 (while (string-match "\\[:alnum:\\]" ss)
50 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
51 (while (string-match "\\[:word:\\]" ss)
52 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
53 (while (string-match "\\[:alpha:\\]" ss)
54 (setq ss (replace-match "a-zA-Z" t t ss)))
55 ss))
56 s))
58 (defmacro org-preserve-lc (&rest body)
59 `(let ((_line (org-current-line))
60 (_col (current-column)))
61 (unwind-protect
62 (progn ,@body)
63 (goto-line _line)
64 (org-move-to-column _col))))
66 (defmacro org-without-partial-completion (&rest body)
67 `(let ((pc-mode (and (boundp 'partial-completion-mode)
68 partial-completion-mode)))
69 (unwind-protect
70 (progn
71 (if pc-mode (partial-completion-mode -1))
72 ,@body)
73 (if pc-mode (partial-completion-mode 1)))))
75 (eval-and-compile
76 (unless (fboundp 'declare-function)
77 (defmacro declare-function (fn file &optional arglist fileonly))))
79 (defmacro org-maybe-intangible (props)
80 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
81 In emacs 21, invisible text is not avoided by the command loop, so the
82 intangible property is needed to make sure point skips this text.
83 In Emacs 22, this is not necessary. The intangible text property has
84 led to problems with flyspell. These problems are fixed in flyspell.el,
85 but we still avoid setting the property in Emacs 22 and later.
86 We use a macro so that the test can happen at compilation time."
87 (if (< emacs-major-version 22)
88 `(append '(intangible t) ,props)
89 props))
91 (defmacro org-with-point-at (pom &rest body)
92 "Move to buffer and point of point-or-marker POM for the duration of BODY."
93 `(save-excursion
94 (if (markerp ,pom) (set-buffer (marker-buffer ,pom)))
95 (save-excursion
96 (goto-char (or ,pom (point)))
97 ,@body)))
99 (defmacro org-no-warnings (&rest body)
100 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
102 (defmacro org-if-unprotected (&rest body)
103 "Execute BODY if there is no `org-protected' text property at point."
104 `(unless (get-text-property (point) 'org-protected)
105 ,@body))
107 (defmacro org-if-unprotected-1 (&rest body)
108 "Execute BODY if there is no `org-protected' text property at point-1."
109 `(unless (get-text-property (1- (point)) 'org-protected)
110 ,@body))
112 (defmacro org-with-remote-undo (_buffer &rest _body)
113 "Execute BODY while recording undo information in two buffers."
114 `(let ((_cline (org-current-line))
115 (_cmd this-command)
116 (_buf1 (current-buffer))
117 (_buf2 ,_buffer)
118 (_undo1 buffer-undo-list)
119 (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
120 _c1 _c2)
121 ,@_body
122 (when org-agenda-allow-remote-undo
123 (setq _c1 (org-verify-change-for-undo
124 _undo1 (with-current-buffer _buf1 buffer-undo-list))
125 _c2 (org-verify-change-for-undo
126 _undo2 (with-current-buffer _buf2 buffer-undo-list)))
127 (when (or _c1 _c2)
128 ;; make sure there are undo boundaries
129 (and _c1 (with-current-buffer _buf1 (undo-boundary)))
130 (and _c2 (with-current-buffer _buf2 (undo-boundary)))
131 ;; remember which buffer to undo
132 (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
133 org-agenda-undo-list)))))
135 (defmacro org-no-read-only (&rest body)
136 "Inhibit read-only for BODY."
137 `(let ((inhibit-read-only t)) ,@body))
139 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
140 rear-nonsticky t mouse-map t fontified t)
141 "Properties to remove when a string without properties is wanted.")
143 (defsubst org-match-string-no-properties (num &optional string)
144 (if (featurep 'xemacs)
145 (let ((s (match-string num string)))
146 (and s (remove-text-properties 0 (length s) org-rm-props s))
148 (match-string-no-properties num string)))
150 (defsubst org-no-properties (s)
151 (if (fboundp 'set-text-properties)
152 (set-text-properties 0 (length s) nil s)
153 (remove-text-properties 0 (length s) org-rm-props s))
156 (defsubst org-get-alist-option (option key)
157 (cond ((eq key t) t)
158 ((eq option t) t)
159 ((assoc key option) (cdr (assoc key option)))
160 (t (cdr (assq 'default option)))))
162 (defsubst org-inhibit-invisibility ()
163 "Modified `buffer-invisibility-spec' for Emacs 21.
164 Some ops with invisible text do not work correctly on Emacs 21. For these
165 we turn off invisibility temporarily. Use this in a `let' form."
166 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
168 (defsubst org-set-local (var value)
169 "Make VAR local in current buffer and set it to VALUE."
170 (set (make-variable-buffer-local var) value))
172 (defsubst org-mode-p ()
173 "Check if the current buffer is in Org-mode."
174 (eq major-mode 'org-mode))
176 (defsubst org-last (list)
177 "Return the last element of LIST."
178 (car (last list)))
180 (defun org-let (list &rest body)
181 (eval (cons 'let (cons list body))))
182 (put 'org-let 'lisp-indent-function 1)
184 (defun org-let2 (list1 list2 &rest body)
185 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
186 (put 'org-let2 'lisp-indent-function 2)
188 (defsubst org-call-with-arg (command arg)
189 "Call COMMAND interactively, but pretend prefix arg was ARG."
190 (let ((current-prefix-arg arg)) (call-interactively command)))
192 (defsubst org-current-line (&optional pos)
193 (save-excursion
194 (and pos (goto-char pos))
195 ;; works also in narrowed buffer, because we start at 1, not point-min
196 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
198 (defsubst org-current-line-string (&optional to-here)
199 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
201 (defsubst org-pos-in-match-range (pos n)
202 (and (match-beginning n)
203 (<= (match-beginning n) pos)
204 (>= (match-end n) pos)))
206 (defun org-autoload (file functions)
207 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
208 (let ((d (format "Documentation will be available after `%s.el' is loaded."
209 file))
211 (while (setq f (pop functions))
212 (or (fboundp f) (autoload f file d t)))))
214 (defun org-match-line (re)
215 "Looking-at at the beginning of the current line."
216 (save-excursion
217 (goto-char (point-at-bol))
218 (looking-at re)))
220 (defun org-plist-delete (plist property)
221 "Delete PROPERTY from PLIST.
222 This is in contrast to merely setting it to 0."
223 (let (p)
224 (while plist
225 (if (not (eq property (car plist)))
226 (setq p (plist-put p (car plist) (nth 1 plist))))
227 (setq plist (cddr plist)))
230 (provide 'org-macs)
232 ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
234 ;;; org-macs.el ends here