Release 6.28
[org-mode.git] / lisp / org-macs.el
blob5f6395093ba6ae02285a0b37ab9f974f27adb4aa
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.28
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 `(set-buffer-modified-p
49 (prog1 (buffer-modified-p) ,@body)))
51 (defmacro org-re (s)
52 "Replace posix classes in regular expression."
53 (if (featurep 'xemacs)
54 (let ((ss s))
55 (save-match-data
56 (while (string-match "\\[:alnum:\\]" ss)
57 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
58 (while (string-match "\\[:word:\\]" ss)
59 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
60 (while (string-match "\\[:alpha:\\]" ss)
61 (setq ss (replace-match "a-zA-Z" t t ss)))
62 ss))
63 s))
65 (defmacro org-preserve-lc (&rest body)
66 `(let ((_line (org-current-line))
67 (_col (current-column)))
68 (unwind-protect
69 (progn ,@body)
70 (goto-line _line)
71 (org-move-to-column _col))))
73 (defmacro org-without-partial-completion (&rest body)
74 `(let ((pc-mode (and (boundp 'partial-completion-mode)
75 partial-completion-mode)))
76 (unwind-protect
77 (progn
78 (if pc-mode (partial-completion-mode -1))
79 ,@body)
80 (if pc-mode (partial-completion-mode 1)))))
82 (defmacro org-maybe-intangible (props)
83 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
84 In emacs 21, invisible text is not avoided by the command loop, so the
85 intangible property is needed to make sure point skips this text.
86 In Emacs 22, this is not necessary. The intangible text property has
87 led to problems with flyspell. These problems are fixed in flyspell.el,
88 but we still avoid setting the property in Emacs 22 and later.
89 We use a macro so that the test can happen at compilation time."
90 (if (< emacs-major-version 22)
91 `(append '(intangible t) ,props)
92 props))
94 (defmacro org-with-point-at (pom &rest body)
95 "Move to buffer and point of point-or-marker POM for the duration of BODY."
96 `(save-excursion
97 (if (markerp ,pom) (set-buffer (marker-buffer ,pom)))
98 (save-excursion
99 (goto-char (or ,pom (point)))
100 ,@body)))
102 (defmacro org-no-warnings (&rest body)
103 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
105 (defmacro org-if-unprotected (&rest body)
106 "Execute BODY if there is no `org-protected' text property at point."
107 `(unless (get-text-property (point) 'org-protected)
108 ,@body))
110 (defmacro org-if-unprotected-1 (&rest body)
111 "Execute BODY if there is no `org-protected' text property at point-1."
112 `(unless (get-text-property (1- (point)) 'org-protected)
113 ,@body))
115 (defmacro org-if-unprotected-at (pos &rest body)
116 "Execute BODY if there is no `org-protected' text property at point-1."
117 `(unless (get-text-property ,pos 'org-protected)
118 ,@body))
120 (defmacro org-with-remote-undo (_buffer &rest _body)
121 "Execute BODY while recording undo information in two buffers."
122 `(let ((_cline (org-current-line))
123 (_cmd this-command)
124 (_buf1 (current-buffer))
125 (_buf2 ,_buffer)
126 (_undo1 buffer-undo-list)
127 (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
128 _c1 _c2)
129 ,@_body
130 (when org-agenda-allow-remote-undo
131 (setq _c1 (org-verify-change-for-undo
132 _undo1 (with-current-buffer _buf1 buffer-undo-list))
133 _c2 (org-verify-change-for-undo
134 _undo2 (with-current-buffer _buf2 buffer-undo-list)))
135 (when (or _c1 _c2)
136 ;; make sure there are undo boundaries
137 (and _c1 (with-current-buffer _buf1 (undo-boundary)))
138 (and _c2 (with-current-buffer _buf2 (undo-boundary)))
139 ;; remember which buffer to undo
140 (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
141 org-agenda-undo-list)))))
143 (defmacro org-no-read-only (&rest body)
144 "Inhibit read-only for BODY."
145 `(let ((inhibit-read-only t)) ,@body))
147 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
148 rear-nonsticky t mouse-map t fontified t)
149 "Properties to remove when a string without properties is wanted.")
151 (defsubst org-match-string-no-properties (num &optional string)
152 (if (featurep 'xemacs)
153 (let ((s (match-string num string)))
154 (and s (remove-text-properties 0 (length s) org-rm-props s))
156 (match-string-no-properties num string)))
158 (defsubst org-no-properties (s)
159 (if (fboundp 'set-text-properties)
160 (set-text-properties 0 (length s) nil s)
161 (remove-text-properties 0 (length s) org-rm-props s))
164 (defsubst org-get-alist-option (option key)
165 (cond ((eq key t) t)
166 ((eq option t) t)
167 ((assoc key option) (cdr (assoc key option)))
168 (t (cdr (assq 'default option)))))
170 (defsubst org-check-external-command (cmd &optional use no-error)
171 "Check if external progam CMD for USE exists, error if not.
172 When the program does exist, return it's path.
173 When it does not exist and NO-ERROR is set, return nil.
174 Otherwise, throw an error. The optional argument USE can describe what this
175 program is needed for, so that the error message can be more informative."
176 (or (executable-find cmd)
177 (if no-error
179 (error "Can't find `%s'%s" cmd
180 (if use (format " (%s)" use) "")))))
182 (defsubst org-inhibit-invisibility ()
183 "Modified `buffer-invisibility-spec' for Emacs 21.
184 Some ops with invisible text do not work correctly on Emacs 21. For these
185 we turn off invisibility temporarily. Use this in a `let' form."
186 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
188 (defsubst org-set-local (var value)
189 "Make VAR local in current buffer and set it to VALUE."
190 (set (make-variable-buffer-local var) value))
192 (defsubst org-mode-p ()
193 "Check if the current buffer is in Org-mode."
194 (eq major-mode 'org-mode))
196 (defsubst org-last (list)
197 "Return the last element of LIST."
198 (car (last list)))
200 (defun org-let (list &rest body)
201 (eval (cons 'let (cons list body))))
202 (put 'org-let 'lisp-indent-function 1)
204 (defun org-let2 (list1 list2 &rest body)
205 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
206 (put 'org-let2 'lisp-indent-function 2)
208 (defsubst org-call-with-arg (command arg)
209 "Call COMMAND interactively, but pretend prefix arg was ARG."
210 (let ((current-prefix-arg arg)) (call-interactively command)))
212 (defsubst org-current-line (&optional pos)
213 (save-excursion
214 (and pos (goto-char pos))
215 ;; works also in narrowed buffer, because we start at 1, not point-min
216 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
218 (defsubst org-current-line-string (&optional to-here)
219 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
221 (defsubst org-pos-in-match-range (pos n)
222 (and (match-beginning n)
223 (<= (match-beginning n) pos)
224 (>= (match-end n) pos)))
226 (defun org-autoload (file functions)
227 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
228 (let ((d (format "Documentation will be available after `%s.el' is loaded."
229 file))
231 (while (setq f (pop functions))
232 (or (fboundp f) (autoload f file d t)))))
234 (defun org-match-line (re)
235 "Looking-at at the beginning of the current line."
236 (save-excursion
237 (goto-char (point-at-bol))
238 (looking-at re)))
240 (defun org-plist-delete (plist property)
241 "Delete PROPERTY from PLIST.
242 This is in contrast to merely setting it to 0."
243 (let (p)
244 (while plist
245 (if (not (eq property (car plist)))
246 (setq p (plist-put p (car plist) (nth 1 plist))))
247 (setq plist (cddr plist)))
251 (defun org-replace-match-keep-properties (newtext &optional fixedcase
252 literal string)
253 "Like `replace-match', but add the text properties found original text."
254 (setq newtext (org-add-props newtext (text-properties-at
255 (match-beginning 0) string)))
256 (replace-match newtext fixedcase literal string))
259 (provide 'org-macs)
261 ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
263 ;;; org-macs.el ends here