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