Release 6.30d
[org-mode.git] / lisp / org-macs.el
blob6344d163a6eb5429b9cc0adf94dc0f5f48b9ad66
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.30d
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 ss))
67 s))
69 (defmacro org-preserve-lc (&rest body)
70 `(let ((_line (org-current-line))
71 (_col (current-column)))
72 (unwind-protect
73 (progn ,@body)
74 (org-goto-line _line)
75 (org-move-to-column _col))))
77 (defmacro org-without-partial-completion (&rest body)
78 `(let ((pc-mode (and (boundp 'partial-completion-mode)
79 partial-completion-mode)))
80 (unwind-protect
81 (progn
82 (if pc-mode (partial-completion-mode -1))
83 ,@body)
84 (if pc-mode (partial-completion-mode 1)))))
86 (defmacro org-maybe-intangible (props)
87 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
88 In emacs 21, invisible text is not avoided by the command loop, so the
89 intangible property is needed to make sure point skips this text.
90 In Emacs 22, this is not necessary. The intangible text property has
91 led to problems with flyspell. These problems are fixed in flyspell.el,
92 but we still avoid setting the property in Emacs 22 and later.
93 We use a macro so that the test can happen at compilation time."
94 (if (< emacs-major-version 22)
95 `(append '(intangible t) ,props)
96 props))
98 (defmacro org-with-point-at (pom &rest body)
99 "Move to buffer and point of point-or-marker POM for the duration of BODY."
100 `(save-excursion
101 (if (markerp ,pom) (set-buffer (marker-buffer ,pom)))
102 (save-excursion
103 (goto-char (or ,pom (point)))
104 ,@body)))
106 (defmacro org-no-warnings (&rest body)
107 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
109 (defmacro org-if-unprotected (&rest body)
110 "Execute BODY if there is no `org-protected' text property at point."
111 `(unless (get-text-property (point) 'org-protected)
112 ,@body))
114 (defmacro org-if-unprotected-1 (&rest body)
115 "Execute BODY if there is no `org-protected' text property at point-1."
116 `(unless (get-text-property (1- (point)) 'org-protected)
117 ,@body))
119 (defmacro org-if-unprotected-at (pos &rest body)
120 "Execute BODY if there is no `org-protected' text property at point-1."
121 `(unless (get-text-property ,pos 'org-protected)
122 ,@body))
124 (defmacro org-with-remote-undo (_buffer &rest _body)
125 "Execute BODY while recording undo information in two buffers."
126 `(let ((_cline (org-current-line))
127 (_cmd this-command)
128 (_buf1 (current-buffer))
129 (_buf2 ,_buffer)
130 (_undo1 buffer-undo-list)
131 (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
132 _c1 _c2)
133 ,@_body
134 (when org-agenda-allow-remote-undo
135 (setq _c1 (org-verify-change-for-undo
136 _undo1 (with-current-buffer _buf1 buffer-undo-list))
137 _c2 (org-verify-change-for-undo
138 _undo2 (with-current-buffer _buf2 buffer-undo-list)))
139 (when (or _c1 _c2)
140 ;; make sure there are undo boundaries
141 (and _c1 (with-current-buffer _buf1 (undo-boundary)))
142 (and _c2 (with-current-buffer _buf2 (undo-boundary)))
143 ;; remember which buffer to undo
144 (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
145 org-agenda-undo-list)))))
147 (defmacro org-no-read-only (&rest body)
148 "Inhibit read-only for BODY."
149 `(let ((inhibit-read-only t)) ,@body))
151 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
152 rear-nonsticky t mouse-map t fontified t)
153 "Properties to remove when a string without properties is wanted.")
155 (defsubst org-match-string-no-properties (num &optional string)
156 (if (featurep 'xemacs)
157 (let ((s (match-string num string)))
158 (and s (remove-text-properties 0 (length s) org-rm-props s))
160 (match-string-no-properties num string)))
162 (defsubst org-no-properties (s)
163 (if (fboundp 'set-text-properties)
164 (set-text-properties 0 (length s) nil s)
165 (remove-text-properties 0 (length s) org-rm-props s))
168 (defsubst org-get-alist-option (option key)
169 (cond ((eq key t) t)
170 ((eq option t) t)
171 ((assoc key option) (cdr (assoc key option)))
172 (t (cdr (assq 'default option)))))
174 (defsubst org-check-external-command (cmd &optional use no-error)
175 "Check if external progam CMD for USE exists, error if not.
176 When the program does exist, return its path.
177 When it does not exist and NO-ERROR is set, return nil.
178 Otherwise, throw an error. The optional argument USE can describe what this
179 program is needed for, so that the error message can be more informative."
180 (or (executable-find cmd)
181 (if no-error
183 (error "Can't find `%s'%s" cmd
184 (if use (format " (%s)" use) "")))))
186 (defsubst org-inhibit-invisibility ()
187 "Modified `buffer-invisibility-spec' for Emacs 21.
188 Some ops with invisible text do not work correctly on Emacs 21. For these
189 we turn off invisibility temporarily. Use this in a `let' form."
190 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
192 (defsubst org-set-local (var value)
193 "Make VAR local in current buffer and set it to VALUE."
194 (set (make-local-variable var) value))
196 (defsubst org-mode-p ()
197 "Check if the current buffer is in Org-mode."
198 (eq major-mode 'org-mode))
200 (defsubst org-last (list)
201 "Return the last element of LIST."
202 (car (last list)))
204 (defun org-let (list &rest body)
205 (eval (cons 'let (cons list body))))
206 (put 'org-let 'lisp-indent-function 1)
208 (defun org-let2 (list1 list2 &rest body)
209 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
210 (put 'org-let2 'lisp-indent-function 2)
212 (defsubst org-call-with-arg (command arg)
213 "Call COMMAND interactively, but pretend prefix arg was ARG."
214 (let ((current-prefix-arg arg)) (call-interactively command)))
216 (defsubst org-current-line (&optional pos)
217 (save-excursion
218 (and pos (goto-char pos))
219 ;; works also in narrowed buffer, because we start at 1, not point-min
220 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
222 (defsubst org-goto-line (N)
223 (save-restriction
224 (widen)
225 (goto-char (point-min))
226 (forward-line (1- N))))
228 (defsubst org-current-line-string (&optional to-here)
229 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
231 (defsubst org-pos-in-match-range (pos n)
232 (and (match-beginning n)
233 (<= (match-beginning n) pos)
234 (>= (match-end n) pos)))
236 (defun org-autoload (file functions)
237 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
238 (let ((d (format "Documentation will be available after `%s.el' is loaded."
239 file))
241 (while (setq f (pop functions))
242 (or (fboundp f) (autoload f file d t)))))
244 (defun org-match-line (re)
245 "Looking-at at the beginning of the current line."
246 (save-excursion
247 (goto-char (point-at-bol))
248 (looking-at re)))
250 (defun org-plist-delete (plist property)
251 "Delete PROPERTY from PLIST.
252 This is in contrast to merely setting it to 0."
253 (let (p)
254 (while plist
255 (if (not (eq property (car plist)))
256 (setq p (plist-put p (car plist) (nth 1 plist))))
257 (setq plist (cddr plist)))
261 (defun org-replace-match-keep-properties (newtext &optional fixedcase
262 literal string)
263 "Like `replace-match', but add the text properties found original text."
264 (setq newtext (org-add-props newtext (text-properties-at
265 (match-beginning 0) string)))
266 (replace-match newtext fixedcase literal string))
268 (defmacro org-with-limited-levels (&rest body)
269 "Execute BODY with limited number of outline levels."
270 `(let* ((outline-regexp (org-get-limited-outline-regexp)))
271 ,@body))
273 (defvar org-odd-levels-only) ; defined in org.el
274 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
275 (defun org-get-limited-outline-regexp ()
276 "Return outline-regexp with limited number of levels.
277 The number of levels is controlled by "
278 (if (or (not (org-mode-p)) (not (featurep 'org-inlinetask)))
280 outline-regexp
281 (let* ((limit-level (1- org-inlinetask-min-level))
282 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
283 (format "\\*\\{1,%d\\} " nstars))))
285 (provide 'org-macs)
287 ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
289 ;;; org-macs.el ends here