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