Factor out function for substituting posix class in regular expression
[org-mode.git] / lisp / org-macs.el
blobbeb1f99731b4d2bbd1c976d71dea005487744d64
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.7
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)))
39 (if (>= emacs-major-version 23)
40 (defsubst org-char-to-string(c)
41 "Defsubst to decode UTF-8 character values in emacs 23 and beyond."
42 (char-to-string c))
43 (defsubst org-char-to-string (c)
44 "Defsubst to decode UTF-8 character values in emacs 22."
45 (string (decode-char 'ucs c)))))
47 (declare-function org-add-props "org-compat" (string plist &rest props))
48 (declare-function org-string-match-p "org-compat" (&rest args))
50 (defmacro org-with-gensyms (symbols &rest body)
51 `(let ,(mapcar (lambda (s)
52 `(,s (make-symbol (concat "--" (symbol-name ',s))))) symbols)
53 ,@body))
54 (put 'org-with-gensyms 'lisp-indent-function 1)
56 (defmacro org-called-interactively-p (&optional kind)
57 (if (featurep 'xemacs)
58 `(interactive-p)
59 (if (or (> emacs-major-version 23)
60 (and (>= emacs-major-version 23)
61 (>= emacs-minor-version 2)))
62 `(with-no-warnings (called-interactively-p ,kind)) ;; defined with no argument in <=23.1
63 `(interactive-p))))
65 (if (and (not (fboundp 'with-silent-modifications))
66 (or (< emacs-major-version 23)
67 (and (= emacs-major-version 23)
68 (< emacs-minor-version 2))))
69 (defmacro with-silent-modifications (&rest body)
70 `(org-unmodified ,@body)))
72 (defmacro org-bound-and-true-p (var)
73 "Return the value of symbol VAR if it is bound, else nil."
74 `(and (boundp (quote ,var)) ,var))
76 (defun org-string-nw-p (s)
77 "Is S a string with a non-white character?"
78 (and (stringp s)
79 (org-string-match-p "\\S-" s)
80 s))
82 (defun org-not-nil (v)
83 "If V not nil, and also not the string \"nil\", then return V.
84 Otherwise return nil."
85 (and v (not (equal v "nil")) v))
87 (defmacro org-unmodified (&rest body)
88 "Execute body without changing `buffer-modified-p'.
89 Also, do not record undo information."
90 `(set-buffer-modified-p
91 (prog1 (buffer-modified-p)
92 (let ((buffer-undo-list t)
93 before-change-functions after-change-functions)
94 ,@body))))
96 (defun org-substitute-posix-classes (re)
97 "Substitute posix classes in regular expression RE."
98 (let ((ss re))
99 (save-match-data
100 (while (string-match "\\[:alnum:\\]" ss)
101 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
102 (while (string-match "\\[:word:\\]" ss)
103 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
104 (while (string-match "\\[:alpha:\\]" ss)
105 (setq ss (replace-match "a-zA-Z" t t ss)))
106 (while (string-match "\\[:punct:\\]" ss)
107 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
108 ss)))
110 (defmacro org-re (s)
111 "Replace posix classes in regular expression."
112 (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
114 (defmacro org-preserve-lc (&rest body)
115 (org-with-gensyms (line col)
116 `(let ((,line (org-current-line))
117 (,col (current-column)))
118 (unwind-protect
119 (progn ,@body)
120 (org-goto-line ,line)
121 (org-move-to-column ,col)))))
123 (defmacro org-without-partial-completion (&rest body)
124 `(if (and (boundp 'partial-completion-mode)
125 partial-completion-mode
126 (fboundp 'partial-completion-mode))
127 (unwind-protect
128 (progn
129 (partial-completion-mode -1)
130 ,@body)
131 (partial-completion-mode 1))
132 ,@body))
134 (defmacro org-maybe-intangible (props)
135 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
136 In Emacs 21, invisible text is not avoided by the command loop, so the
137 intangible property is needed to make sure point skips this text.
138 In Emacs 22, this is not necessary. The intangible text property has
139 led to problems with flyspell. These problems are fixed in flyspell.el,
140 but we still avoid setting the property in Emacs 22 and later.
141 We use a macro so that the test can happen at compilation time."
142 (if (< emacs-major-version 22)
143 `(append '(intangible t) ,props)
144 props))
146 (defmacro org-with-point-at (pom &rest body)
147 "Move to buffer and point of point-or-marker POM for the duration of BODY."
148 (org-with-gensyms (mpom)
149 `(let ((,mpom ,pom))
150 (save-excursion
151 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
152 (save-excursion
153 (goto-char (or ,mpom (point)))
154 ,@body)))))
155 (put 'org-with-point-at 'lisp-indent-function 1)
157 (defmacro org-no-warnings (&rest body)
158 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
160 (defmacro org-if-unprotected (&rest body)
161 "Execute BODY if there is no `org-protected' text property at point."
162 `(unless (get-text-property (point) 'org-protected)
163 ,@body))
165 (defmacro org-if-unprotected-1 (&rest body)
166 "Execute BODY if there is no `org-protected' text property at point-1."
167 `(unless (get-text-property (1- (point)) 'org-protected)
168 ,@body))
170 (defmacro org-if-unprotected-at (pos &rest body)
171 "Execute BODY if there is no `org-protected' text property at POS."
172 `(unless (get-text-property ,pos 'org-protected)
173 ,@body))
174 (put 'org-if-unprotected-at 'lisp-indent-function 1)
176 (defun org-re-search-forward-unprotected (&rest args)
177 "Like re-search-forward, but stop only in unprotected places."
178 (catch 'exit
179 (while t
180 (unless (apply 're-search-forward args)
181 (throw 'exit nil))
182 (unless (get-text-property (match-beginning 0) 'org-protected)
183 (throw 'exit (point))))))
185 (defmacro org-with-remote-undo (_buffer &rest _body)
186 "Execute BODY while recording undo information in two buffers."
187 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
188 `(let ((,cline (org-current-line))
189 (,cmd this-command)
190 (,buf1 (current-buffer))
191 (,buf2 ,_buffer)
192 (,undo1 buffer-undo-list)
193 (,undo2 (with-current-buffer ,_buffer buffer-undo-list))
194 ,c1 ,c2)
195 ,@_body
196 (when org-agenda-allow-remote-undo
197 (setq ,c1 (org-verify-change-for-undo
198 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
199 ,c2 (org-verify-change-for-undo
200 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
201 (when (or ,c1 ,c2)
202 ;; make sure there are undo boundaries
203 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
204 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
205 ;; remember which buffer to undo
206 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
207 org-agenda-undo-list))))))
208 (put 'org-with-remote-undo 'lisp-indent-function 1)
210 (defmacro org-no-read-only (&rest body)
211 "Inhibit read-only for BODY."
212 `(let ((inhibit-read-only t)) ,@body))
214 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
215 rear-nonsticky t mouse-map t fontified t
216 org-emphasis t)
217 "Properties to remove when a string without properties is wanted.")
219 (defsubst org-match-string-no-properties (num &optional string)
220 (if (featurep 'xemacs)
221 (let ((s (match-string num string)))
222 (and s (remove-text-properties 0 (length s) org-rm-props s))
224 (match-string-no-properties num string)))
226 (defsubst org-no-properties (s)
227 (if (fboundp 'set-text-properties)
228 (set-text-properties 0 (length s) nil s)
229 (remove-text-properties 0 (length s) org-rm-props s))
232 (defsubst org-get-alist-option (option key)
233 (cond ((eq key t) t)
234 ((eq option t) t)
235 ((assoc key option) (cdr (assoc key option)))
236 (t (cdr (assq 'default option)))))
238 (defsubst org-check-external-command (cmd &optional use no-error)
239 "Check if external program CMD for USE exists, error if not.
240 When the program does exist, return its path.
241 When it does not exist and NO-ERROR is set, return nil.
242 Otherwise, throw an error. The optional argument USE can describe what this
243 program is needed for, so that the error message can be more informative."
244 (or (executable-find cmd)
245 (if no-error
247 (error "Can't find `%s'%s" cmd
248 (if use (format " (%s)" use) "")))))
250 (defsubst org-inhibit-invisibility ()
251 "Modified `buffer-invisibility-spec' for Emacs 21.
252 Some ops with invisible text do not work correctly on Emacs 21. For these
253 we turn off invisibility temporarily. Use this in a `let' form."
254 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
256 (defsubst org-set-local (var value)
257 "Make VAR local in current buffer and set it to VALUE."
258 (set (make-local-variable var) value))
260 (defsubst org-mode-p ()
261 "Check if the current buffer is in Org-mode."
262 (eq major-mode 'org-mode))
264 (defsubst org-last (list)
265 "Return the last element of LIST."
266 (car (last list)))
268 (defun org-let (list &rest body)
269 (eval (cons 'let (cons list body))))
270 (put 'org-let 'lisp-indent-function 1)
272 (defun org-let2 (list1 list2 &rest body)
273 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
274 (put 'org-let2 'lisp-indent-function 2)
276 (defsubst org-call-with-arg (command arg)
277 "Call COMMAND interactively, but pretend prefix arg was ARG."
278 (let ((current-prefix-arg arg)) (call-interactively command)))
280 (defsubst org-current-line (&optional pos)
281 (save-excursion
282 (and pos (goto-char pos))
283 ;; works also in narrowed buffer, because we start at 1, not point-min
284 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
286 (defsubst org-goto-line (N)
287 (save-restriction
288 (widen)
289 (goto-char (point-min))
290 (forward-line (1- N))))
292 (defsubst org-current-line-string (&optional to-here)
293 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
295 (defsubst org-pos-in-match-range (pos n)
296 (and (match-beginning n)
297 (<= (match-beginning n) pos)
298 (>= (match-end n) pos)))
300 (defun org-autoload (file functions)
301 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
302 (let ((d (format "Documentation will be available after `%s.el' is loaded."
303 file))
305 (while (setq f (pop functions))
306 (or (fboundp f) (autoload f file d t)))))
308 (defun org-match-line (re)
309 "Looking-at at the beginning of the current line."
310 (save-excursion
311 (goto-char (point-at-bol))
312 (looking-at re)))
314 (defun org-plist-delete (plist property)
315 "Delete PROPERTY from PLIST.
316 This is in contrast to merely setting it to 0."
317 (let (p)
318 (while plist
319 (if (not (eq property (car plist)))
320 (setq p (plist-put p (car plist) (nth 1 plist))))
321 (setq plist (cddr plist)))
324 (defun org-replace-match-keep-properties (newtext &optional fixedcase
325 literal string)
326 "Like `replace-match', but add the text properties found original text."
327 (setq newtext (org-add-props newtext (text-properties-at
328 (match-beginning 0) string)))
329 (replace-match newtext fixedcase literal string))
331 (defmacro org-save-outline-visibility (use-markers &rest body)
332 "Save and restore outline visibility around BODY.
333 If USE-MARKERS is non-nil, use markers for the positions.
334 This means that the buffer may change while running BODY,
335 but it also means that the buffer should stay alive
336 during the operation, because otherwise all these markers will
337 point nowhere."
338 (declare (indent 1))
339 (org-with-gensyms (data rtn)
340 `(let ((,data (org-outline-overlay-data ,use-markers))
341 ,rtn)
342 (unwind-protect
343 (progn
344 (setq ,rtn (progn ,@body))
345 (org-set-outline-overlay-data ,data))
346 (when ,use-markers
347 (mapc (lambda (c)
348 (and (markerp (car c)) (move-marker (car c) nil))
349 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
350 ,data)))
351 ,rtn)))
353 (defmacro org-with-wide-buffer (&rest body)
354 "Execute body while temporarily widening the buffer."
355 `(save-excursion
356 (save-restriction
357 (widen)
358 ,@body)))
360 (defmacro org-with-limited-levels (&rest body)
361 "Execute BODY with limited number of outline levels."
362 `(let* ((org-outline-regexp (org-get-limited-outline-regexp))
363 (outline-regexp org-outline-regexp)
364 (org-outline-regexp-at-bol (concat "^" org-outline-regexp)))
365 ,@body))
367 (defvar org-outline-regexp) ; defined in org.el
368 (defvar org-odd-levels-only) ; defined in org.el
369 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
370 (defun org-get-limited-outline-regexp ()
371 "Return outline-regexp with limited number of levels.
372 The number of levels is controlled by `org-inlinetask-min-level'"
373 (if (or (not (org-mode-p)) (not (featurep 'org-inlinetask)))
374 org-outline-regexp
375 (let* ((limit-level (1- org-inlinetask-min-level))
376 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
377 (format "\\*\\{1,%d\\} " nstars))))
379 (defun org-format-seconds (string seconds)
380 "Compatibility function replacing format-seconds"
381 (if (fboundp 'format-seconds)
382 (format-seconds string seconds)
383 (format-time-string string (seconds-to-time seconds))))
385 (defmacro org-eval-in-environment (environment form)
386 `(eval (list 'let ,environment ',form)))
387 (put 'org-eval-in-environment 'lisp-indent-function 1)
389 (provide 'org-macs)
391 ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
393 ;;; org-macs.el ends here