Created org-clock.el as split-off from org.el.
[org-mode.git] / lisp / org-macs.el
blobb8337db5961306ce4d72f46bc078d14f3b70d596
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.00pre-2
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, or (at your option)
15 ;; 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
30 ;; This file contains macro definitions, defsubst definitions, and
31 ;; oder stuff needed for compilation and top-level forms in Org-mode.
33 ;;; 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 "\\[:alpha:\\]" ss)
53 (setq ss (replace-match "a-zA-Z" t t ss)))
54 ss))
55 s))
57 (defmacro org-preserve-lc (&rest body)
58 `(let ((_line (org-current-line))
59 (_col (current-column)))
60 (unwind-protect
61 (progn ,@body)
62 (goto-line _line)
63 (move-to-column _col))))
65 (defmacro org-without-partial-completion (&rest body)
66 `(let ((pc-mode (and (boundp 'partial-completion-mode)
67 partial-completion-mode)))
68 (unwind-protect
69 (progn
70 (if pc-mode (partial-completion-mode -1))
71 ,@body)
72 (if pc-mode (partial-completion-mode 1)))))
74 (eval-and-compile
75 (unless (fboundp 'declare-function)
76 (defmacro declare-function (fn file &optional arglist fileonly))))
78 (defmacro org-maybe-intangible (props)
79 "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
80 In emacs 21, invisible text is not avoided by the command loop, so the
81 intangible property is needed to make sure point skips this text.
82 In Emacs 22, this is not necessary. The intangible text property has
83 led to problems with flyspell. These problems are fixed in flyspell.el,
84 but we still avoid setting the property in Emacs 22 and later.
85 We use a macro so that the test can happen at compilation time."
86 (if (< emacs-major-version 22)
87 `(append '(intangible t) ,props)
88 props))
90 (defmacro org-with-point-at (pom &rest body)
91 "Move to buffer and point of point-or-marker POM for the duration of BODY."
92 (declare (indent 1) (debug t))
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 (declare (debug t))
105 `(unless (get-text-property (point) 'org-protected)
106 ,@body))
108 (defmacro org-with-remote-undo (_buffer &rest _body)
109 "Execute BODY while recording undo information in two buffers."
110 (declare (indent 1) (debug t))
111 `(let ((_cline (org-current-line))
112 (_cmd this-command)
113 (_buf1 (current-buffer))
114 (_buf2 ,_buffer)
115 (_undo1 buffer-undo-list)
116 (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
117 _c1 _c2)
118 ,@_body
119 (when org-agenda-allow-remote-undo
120 (setq _c1 (org-verify-change-for-undo
121 _undo1 (with-current-buffer _buf1 buffer-undo-list))
122 _c2 (org-verify-change-for-undo
123 _undo2 (with-current-buffer _buf2 buffer-undo-list)))
124 (when (or _c1 _c2)
125 ;; make sure there are undo boundaries
126 (and _c1 (with-current-buffer _buf1 (undo-boundary)))
127 (and _c2 (with-current-buffer _buf2 (undo-boundary)))
128 ;; remember which buffer to undo
129 (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
130 org-agenda-undo-list)))))
132 (defmacro org-no-read-only (&rest body)
133 "Inhibit read-only for BODY."
134 `(let ((inhibit-read-only t)) ,@body))
136 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
137 rear-nonsticky t mouse-map t fontified t)
138 "Properties to remove when a string without properties is wanted.")
140 (defsubst org-match-string-no-properties (num &optional string)
141 (if (featurep 'xemacs)
142 (let ((s (match-string num string)))
143 (remove-text-properties 0 (length s) org-rm-props s)
145 (match-string-no-properties num string)))
147 (defsubst org-no-properties (s)
148 (if (fboundp 'set-text-properties)
149 (set-text-properties 0 (length s) nil s)
150 (remove-text-properties 0 (length s) org-rm-props s))
153 (defsubst org-get-alist-option (option key)
154 (cond ((eq key t) t)
155 ((eq option t) t)
156 ((assoc key option) (cdr (assoc key option)))
157 (t (cdr (assq 'default option)))))
159 (defsubst org-inhibit-invisibility ()
160 "Modified `buffer-invisibility-spec' for Emacs 21.
161 Some ops with invisible text do not work correctly on Emacs 21. For these
162 we turn off invisibility temporarily. Use this in a `let' form."
163 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
165 (defsubst org-set-local (var value)
166 "Make VAR local in current buffer and set it to VALUE."
167 (set (make-variable-buffer-local var) value))
169 (defsubst org-mode-p ()
170 "Check if the current buffer is in Org-mode."
171 (eq major-mode 'org-mode))
173 (defsubst org-last (list)
174 "Return the last element of LIST."
175 (car (last list)))
177 (defun org-let (list &rest body)
178 (eval (cons 'let (cons list body))))
179 (put 'org-let 'lisp-indent-function 1)
181 (defun org-let2 (list1 list2 &rest body)
182 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
183 (put 'org-let2 'lisp-indent-function 2)
185 (defsubst org-call-with-arg (command arg)
186 "Call COMMAND interactively, but pretend prefix are was ARG."
187 (let ((current-prefix-arg arg)) (call-interactively command)))
189 (defsubst org-current-line (&optional pos)
190 (save-excursion
191 (and pos (goto-char pos))
192 ;; works also in narrowed buffer, because we start at 1, not point-min
193 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
195 (defsubst org-pos-in-match-range (pos n)
196 (and (match-beginning n)
197 (<= (match-beginning n) pos)
198 (>= (match-end n) pos)))
200 (defun org-autoload (file functions)
201 "Establish autoload for all FUNCTIONS in FILE, if not boutd already."
202 (declare (indent 1) (debug t))
203 (let ((d (format "Documentation will be available after `%s.el' is loaded."
204 file))
206 (while (setq f (pop functions))
207 (or (fboundp f) (autoload f file d t)))))
209 (defmacro org-match-line (re)
210 "Looking-at at the beginning of the current line."
211 (save-excursion
212 (goto-char (point-at-bol))
213 (looking-at re)))
215 (provide 'org-macs)
217 ;;; org-macs.el ends here