Release 7.3
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-macs.el
blob5a5612387fda7edf3c7fc82d0efaaf651c2e83ef
1 ;;; org-macs.el --- Top-level definitions for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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: 7.3
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))
41 (declare-function org-string-match-p "org-compat" (&rest args))
43 (defmacro org-called-interactively-p (&optional kind)
44 `(if (featurep 'xemacs)
45 (interactive-p)
46 (if (or (> emacs-major-version 23)
47 (and (>= emacs-major-version 23)
48 (>= emacs-minor-version 2)))
49 (called-interactively-p ,kind)
50 (interactive-p))))
52 (defmacro org-bound-and-true-p (var)
53 "Return the value of symbol VAR if it is bound, else nil."
54 `(and (boundp (quote ,var)) ,var))
56 (defun org-string-nw-p (s)
57 "Is S a string with a non-white character?"
58 (and (stringp s)
59 (org-string-match-p "\\S-" s)
60 s))
62 (defun org-not-nil (v)
63 "If V not nil, and also not the string \"nil\", then return V.
64 Otherwise return nil."
65 (and v (not (equal v "nil")) v))
67 (defmacro org-unmodified (&rest body)
68 "Execute body without changing `buffer-modified-p'.
69 Also, do not record undo information."
70 `(set-buffer-modified-p
71 (prog1 (buffer-modified-p)
72 (let ((buffer-undo-list t)
73 before-change-functions after-change-functions)
74 ,@body))))
76 (defmacro org-re (s)
77 "Replace posix classes in regular expression."
78 (if (featurep 'xemacs)
79 (let ((ss s))
80 (save-match-data
81 (while (string-match "\\[:alnum:\\]" ss)
82 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
83 (while (string-match "\\[:word:\\]" ss)
84 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
85 (while (string-match "\\[:alpha:\\]" ss)
86 (setq ss (replace-match "a-zA-Z" t t ss)))
87 (while (string-match "\\[:punct:\\]" ss)
88 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
89 ss))
90 s))
92 (defmacro org-preserve-lc (&rest body)
93 `(let ((_line (org-current-line))
94 (_col (current-column)))
95 (unwind-protect
96 (progn ,@body)
97 (org-goto-line _line)
98 (org-move-to-column _col))))
100 (defmacro org-without-partial-completion (&rest body)
101 `(let ((pc-mode (and (boundp 'partial-completion-mode)
102 partial-completion-mode)))
103 (unwind-protect
104 (progn
105 (if pc-mode (partial-completion-mode -1))
106 ,@body)
107 (if pc-mode (partial-completion-mode 1)))))
109 (defmacro org-maybe-intangible (props)
110 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
111 In Emacs 21, invisible text is not avoided by the command loop, so the
112 intangible property is needed to make sure point skips this text.
113 In Emacs 22, this is not necessary. The intangible text property has
114 led to problems with flyspell. These problems are fixed in flyspell.el,
115 but we still avoid setting the property in Emacs 22 and later.
116 We use a macro so that the test can happen at compilation time."
117 (if (< emacs-major-version 22)
118 `(append '(intangible t) ,props)
119 props))
121 (defmacro org-with-point-at (pom &rest body)
122 "Move to buffer and point of point-or-marker POM for the duration of BODY."
123 `(save-excursion
124 (if (markerp ,pom) (set-buffer (marker-buffer ,pom)))
125 (save-excursion
126 (goto-char (or ,pom (point)))
127 ,@body)))
128 (put 'org-with-point-at 'lisp-indent-function 1)
130 (defmacro org-no-warnings (&rest body)
131 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
133 (defmacro org-if-unprotected (&rest body)
134 "Execute BODY if there is no `org-protected' text property at point."
135 `(unless (get-text-property (point) 'org-protected)
136 ,@body))
138 (defmacro org-if-unprotected-1 (&rest body)
139 "Execute BODY if there is no `org-protected' text property at point-1."
140 `(unless (get-text-property (1- (point)) 'org-protected)
141 ,@body))
143 (defmacro org-if-unprotected-at (pos &rest body)
144 "Execute BODY if there is no `org-protected' text property at POS."
145 `(unless (get-text-property ,pos 'org-protected)
146 ,@body))
147 (put 'org-if-unprotected-at 'lisp-indent-function 1)
149 (defun org-re-search-forward-unprotected (&rest args)
150 "Like re-search-forward, but stop only in unprotected places."
151 (catch 'exit
152 (while t
153 (unless (apply 're-search-forward args)
154 (throw 'exit nil))
155 (unless (get-text-property (match-beginning 0) 'org-protected)
156 (throw 'exit (point))))))
158 (defmacro org-with-remote-undo (_buffer &rest _body)
159 "Execute BODY while recording undo information in two buffers."
160 `(let ((_cline (org-current-line))
161 (_cmd this-command)
162 (_buf1 (current-buffer))
163 (_buf2 ,_buffer)
164 (_undo1 buffer-undo-list)
165 (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
166 _c1 _c2)
167 ,@_body
168 (when org-agenda-allow-remote-undo
169 (setq _c1 (org-verify-change-for-undo
170 _undo1 (with-current-buffer _buf1 buffer-undo-list))
171 _c2 (org-verify-change-for-undo
172 _undo2 (with-current-buffer _buf2 buffer-undo-list)))
173 (when (or _c1 _c2)
174 ;; make sure there are undo boundaries
175 (and _c1 (with-current-buffer _buf1 (undo-boundary)))
176 (and _c2 (with-current-buffer _buf2 (undo-boundary)))
177 ;; remember which buffer to undo
178 (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
179 org-agenda-undo-list)))))
181 (defmacro org-no-read-only (&rest body)
182 "Inhibit read-only for BODY."
183 `(let ((inhibit-read-only t)) ,@body))
185 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
186 rear-nonsticky t mouse-map t fontified t
187 org-emphasis t)
188 "Properties to remove when a string without properties is wanted.")
190 (defsubst org-match-string-no-properties (num &optional string)
191 (if (featurep 'xemacs)
192 (let ((s (match-string num string)))
193 (and s (remove-text-properties 0 (length s) org-rm-props s))
195 (match-string-no-properties num string)))
197 (defsubst org-no-properties (s)
198 (if (fboundp 'set-text-properties)
199 (set-text-properties 0 (length s) nil s)
200 (remove-text-properties 0 (length s) org-rm-props s))
203 (defsubst org-get-alist-option (option key)
204 (cond ((eq key t) t)
205 ((eq option t) t)
206 ((assoc key option) (cdr (assoc key option)))
207 (t (cdr (assq 'default option)))))
209 (defsubst org-check-external-command (cmd &optional use no-error)
210 "Check if external program CMD for USE exists, error if not.
211 When the program does exist, return its path.
212 When it does not exist and NO-ERROR is set, return nil.
213 Otherwise, throw an error. The optional argument USE can describe what this
214 program is needed for, so that the error message can be more informative."
215 (or (executable-find cmd)
216 (if no-error
218 (error "Can't find `%s'%s" cmd
219 (if use (format " (%s)" use) "")))))
221 (defsubst org-inhibit-invisibility ()
222 "Modified `buffer-invisibility-spec' for Emacs 21.
223 Some ops with invisible text do not work correctly on Emacs 21. For these
224 we turn off invisibility temporarily. Use this in a `let' form."
225 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
227 (defsubst org-set-local (var value)
228 "Make VAR local in current buffer and set it to VALUE."
229 (set (make-local-variable var) value))
231 (defsubst org-mode-p ()
232 "Check if the current buffer is in Org-mode."
233 (eq major-mode 'org-mode))
235 (defsubst org-last (list)
236 "Return the last element of LIST."
237 (car (last list)))
239 (defun org-let (list &rest body)
240 (eval (cons 'let (cons list body))))
241 (put 'org-let 'lisp-indent-function 1)
243 (defun org-let2 (list1 list2 &rest body)
244 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
245 (put 'org-let2 'lisp-indent-function 2)
247 (defsubst org-call-with-arg (command arg)
248 "Call COMMAND interactively, but pretend prefix arg was ARG."
249 (let ((current-prefix-arg arg)) (call-interactively command)))
251 (defsubst org-current-line (&optional pos)
252 (save-excursion
253 (and pos (goto-char pos))
254 ;; works also in narrowed buffer, because we start at 1, not point-min
255 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
257 (defsubst org-goto-line (N)
258 (save-restriction
259 (widen)
260 (goto-char (point-min))
261 (forward-line (1- N))))
263 (defsubst org-current-line-string (&optional to-here)
264 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
266 (defsubst org-pos-in-match-range (pos n)
267 (and (match-beginning n)
268 (<= (match-beginning n) pos)
269 (>= (match-end n) pos)))
271 (defun org-autoload (file functions)
272 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
273 (let ((d (format "Documentation will be available after `%s.el' is loaded."
274 file))
276 (while (setq f (pop functions))
277 (or (fboundp f) (autoload f file d t)))))
279 (defun org-match-line (re)
280 "Looking-at at the beginning of the current line."
281 (save-excursion
282 (goto-char (point-at-bol))
283 (looking-at re)))
285 (defun org-plist-delete (plist property)
286 "Delete PROPERTY from PLIST.
287 This is in contrast to merely setting it to 0."
288 (let (p)
289 (while plist
290 (if (not (eq property (car plist)))
291 (setq p (plist-put p (car plist) (nth 1 plist))))
292 (setq plist (cddr plist)))
295 (defun org-replace-match-keep-properties (newtext &optional fixedcase
296 literal string)
297 "Like `replace-match', but add the text properties found original text."
298 (setq newtext (org-add-props newtext (text-properties-at
299 (match-beginning 0) string)))
300 (replace-match newtext fixedcase literal string))
302 (defmacro org-save-outline-visibility (use-markers &rest body)
303 "Save and restore outline visibility around BODY.
304 If USE-MARKERS is non-nil, use markers for the positions.
305 This means that the buffer may change while running BODY,
306 but it also means that the buffer should stay alive
307 during the operation, because otherwise all these markers will
308 point nowhere."
309 (declare (indent 1))
310 `(let ((data (org-outline-overlay-data ,use-markers)))
311 (unwind-protect
312 (progn
313 ,@body
314 (org-set-outline-overlay-data data))
315 (when ,use-markers
316 (mapc (lambda (c)
317 (and (markerp (car c)) (move-marker (car c) nil))
318 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
319 data)))))
321 (defmacro org-with-limited-levels (&rest body)
322 "Execute BODY with limited number of outline levels."
323 `(let* ((outline-regexp (org-get-limited-outline-regexp)))
324 ,@body))
326 (defvar org-odd-levels-only) ; defined in org.el
327 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
328 (defun org-get-limited-outline-regexp ()
329 "Return outline-regexp with limited number of levels.
330 The number of levels is controlled by `org-inlinetask-min-level'"
331 (if (or (not (org-mode-p)) (not (featurep 'org-inlinetask)))
333 outline-regexp
334 (let* ((limit-level (1- org-inlinetask-min-level))
335 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
336 (format "\\*\\{1,%d\\} " nstars))))
338 (provide 'org-macs)
340 ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
342 ;;; org-macs.el ends here