More small fixes on `C-u C-c C-c' and checkboxes.
[org-mode/org-kjn.git] / lisp / org-macs.el
blob521f5fd8633e3628c551a8ee0c4e20de7348e645
1 ;;; org-macs.el --- Top-level definitions for Org-mode
3 ;; Copyright (C) 2004-2011 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 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains macro definitions, defsubst definitions, other
28 ;; stuff needed for compilation and top-level forms in Org-mode, as well
29 ;; lots of small functions that are not org-mode specific but simply
30 ;; generally useful stuff.
32 ;;; Code:
34 (eval-and-compile
35 (unless (fboundp 'declare-function)
36 (defmacro declare-function (fn file &optional arglist fileonly)))
37 (if (>= emacs-major-version 23)
38 (defsubst org-char-to-string(c)
39 "Defsubst to decode UTF-8 character values in emacs 23 and beyond."
40 (char-to-string c))
41 (defsubst org-char-to-string (c)
42 "Defsubst to decode UTF-8 character values in emacs 22."
43 (string (decode-char 'ucs c)))))
45 (declare-function org-add-props "org-compat" (string plist &rest props))
46 (declare-function org-string-match-p "org-compat" (&rest args))
48 (defmacro org-with-gensyms (symbols &rest body)
49 `(let ,(mapcar (lambda (s)
50 `(,s (make-symbol (concat "--" (symbol-name ',s))))) symbols)
51 ,@body))
52 (def-edebug-spec org-with-gensyms (sexp body))
53 (put 'org-with-gensyms 'lisp-indent-function 1)
55 (defmacro org-called-interactively-p (&optional kind)
56 (if (featurep 'xemacs)
57 `(interactive-p)
58 (if (or (> emacs-major-version 23)
59 (and (>= emacs-major-version 23)
60 (>= emacs-minor-version 2)))
61 `(with-no-warnings (called-interactively-p ,kind)) ;; defined with no argument in <=23.1
62 `(interactive-p))))
63 (def-edebug-spec org-called-interactively-p (&optional ("quote" symbolp)))
65 (when (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))
71 (def-edebug-spec with-silent-modifications (body)))
73 (defmacro org-bound-and-true-p (var)
74 "Return the value of symbol VAR if it is bound, else nil."
75 `(and (boundp (quote ,var)) ,var))
76 (def-edebug-spec org-bound-and-true-p (symbolp))
78 (defun org-string-nw-p (s)
79 "Is S a string with a non-white character?"
80 (and (stringp s)
81 (org-string-match-p "\\S-" s)
82 s))
84 (defun org-not-nil (v)
85 "If V not nil, and also not the string \"nil\", then return V.
86 Otherwise return nil."
87 (and v (not (equal v "nil")) v))
89 (defmacro org-unmodified (&rest body)
90 "Execute body without changing `buffer-modified-p'.
91 Also, do not record undo information."
92 `(set-buffer-modified-p
93 (prog1 (buffer-modified-p)
94 (let ((buffer-undo-list t)
95 before-change-functions after-change-functions)
96 ,@body))))
97 (def-edebug-spec org-unmodified (body))
99 (defun org-substitute-posix-classes (re)
100 "Substitute posix classes in regular expression RE."
101 (let ((ss re))
102 (save-match-data
103 (while (string-match "\\[:alnum:\\]" ss)
104 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
105 (while (string-match "\\[:word:\\]" ss)
106 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
107 (while (string-match "\\[:alpha:\\]" ss)
108 (setq ss (replace-match "a-zA-Z" t t ss)))
109 (while (string-match "\\[:punct:\\]" ss)
110 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
111 ss)))
113 (defmacro org-re (s)
114 "Replace posix classes in regular expression."
115 (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
116 (def-edebug-spec org-re (form))
118 (defmacro org-preserve-lc (&rest body)
119 (org-with-gensyms (line col)
120 `(let ((,line (org-current-line))
121 (,col (current-column)))
122 (unwind-protect
123 (progn ,@body)
124 (org-goto-line ,line)
125 (org-move-to-column ,col)))))
126 (def-edebug-spec org-preserve-lc (body))
128 (defmacro org-without-partial-completion (&rest body)
129 `(if (and (boundp 'partial-completion-mode)
130 partial-completion-mode
131 (fboundp 'partial-completion-mode))
132 (unwind-protect
133 (progn
134 (partial-completion-mode -1)
135 ,@body)
136 (partial-completion-mode 1))
137 ,@body))
138 (def-edebug-spec org-without-partial-completion (body))
140 ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
141 (defmacro org-maybe-intangible (props)
142 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
143 In Emacs 21, invisible text is not avoided by the command loop, so the
144 intangible property is needed to make sure point skips this text.
145 In Emacs 22, this is not necessary. The intangible text property has
146 led to problems with flyspell. These problems are fixed in flyspell.el,
147 but we still avoid setting the property in Emacs 22 and later.
148 We use a macro so that the test can happen at compilation time."
149 (if (< emacs-major-version 22)
150 `(append '(intangible t) ,props)
151 props))
153 (defmacro org-with-point-at (pom &rest body)
154 "Move to buffer and point of point-or-marker POM for the duration of BODY."
155 (org-with-gensyms (mpom)
156 `(let ((,mpom ,pom))
157 (save-excursion
158 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
159 (save-excursion
160 (goto-char (or ,mpom (point)))
161 ,@body)))))
162 (def-edebug-spec org-with-point-at (form body))
163 (put 'org-with-point-at 'lisp-indent-function 1)
165 (defmacro org-no-warnings (&rest body)
166 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
167 (def-edebug-spec org-no-warnings (body))
169 (defmacro org-if-unprotected (&rest body)
170 "Execute BODY if there is no `org-protected' text property at point."
171 `(unless (get-text-property (point) 'org-protected)
172 ,@body))
173 (def-edebug-spec org-if-unprotected (body))
175 (defmacro org-if-unprotected-1 (&rest body)
176 "Execute BODY if there is no `org-protected' text property at point-1."
177 `(unless (get-text-property (1- (point)) 'org-protected)
178 ,@body))
179 (def-edebug-spec org-if-unprotected-1 (body))
181 (defmacro org-if-unprotected-at (pos &rest body)
182 "Execute BODY if there is no `org-protected' text property at POS."
183 `(unless (get-text-property ,pos 'org-protected)
184 ,@body))
185 (def-edebug-spec org-if-unprotected-at (form body))
186 (put 'org-if-unprotected-at 'lisp-indent-function 1)
188 (defun org-re-search-forward-unprotected (&rest args)
189 "Like re-search-forward, but stop only in unprotected places."
190 (catch 'exit
191 (while t
192 (unless (apply 're-search-forward args)
193 (throw 'exit nil))
194 (unless (get-text-property (match-beginning 0) 'org-protected)
195 (throw 'exit (point))))))
197 ;; FIXME: Normalize argument names
198 (defmacro org-with-remote-undo (_buffer &rest _body)
199 "Execute BODY while recording undo information in two buffers."
200 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
201 `(let ((,cline (org-current-line))
202 (,cmd this-command)
203 (,buf1 (current-buffer))
204 (,buf2 ,_buffer)
205 (,undo1 buffer-undo-list)
206 (,undo2 (with-current-buffer ,_buffer buffer-undo-list))
207 ,c1 ,c2)
208 ,@_body
209 (when org-agenda-allow-remote-undo
210 (setq ,c1 (org-verify-change-for-undo
211 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
212 ,c2 (org-verify-change-for-undo
213 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
214 (when (or ,c1 ,c2)
215 ;; make sure there are undo boundaries
216 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
217 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
218 ;; remember which buffer to undo
219 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
220 org-agenda-undo-list))))))
221 (def-edebug-spec org-with-remote-undo (form body))
222 (put 'org-with-remote-undo 'lisp-indent-function 1)
224 (defmacro org-no-read-only (&rest body)
225 "Inhibit read-only for BODY."
226 `(let ((inhibit-read-only t)) ,@body))
227 (def-edebug-spec org-no-read-only (body))
229 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
230 rear-nonsticky t mouse-map t fontified t
231 org-emphasis t)
232 "Properties to remove when a string without properties is wanted.")
234 (defsubst org-match-string-no-properties (num &optional string)
235 (if (featurep 'xemacs)
236 (let ((s (match-string num string)))
237 (and s (remove-text-properties 0 (length s) org-rm-props s))
239 (match-string-no-properties num string)))
241 (defsubst org-no-properties (s)
242 (if (fboundp 'set-text-properties)
243 (set-text-properties 0 (length s) nil s)
244 (remove-text-properties 0 (length s) org-rm-props s))
247 (defsubst org-get-alist-option (option key)
248 (cond ((eq key t) t)
249 ((eq option t) t)
250 ((assoc key option) (cdr (assoc key option)))
251 (t (cdr (assq 'default option)))))
253 (defsubst org-check-external-command (cmd &optional use no-error)
254 "Check if external program CMD for USE exists, error if not.
255 When the program does exist, return its path.
256 When it does not exist and NO-ERROR is set, return nil.
257 Otherwise, throw an error. The optional argument USE can describe what this
258 program is needed for, so that the error message can be more informative."
259 (or (executable-find cmd)
260 (if no-error
262 (error "Can't find `%s'%s" cmd
263 (if use (format " (%s)" use) "")))))
265 (defsubst org-inhibit-invisibility ()
266 "Modified `buffer-invisibility-spec' for Emacs 21.
267 Some ops with invisible text do not work correctly on Emacs 21. For these
268 we turn off invisibility temporarily. Use this in a `let' form."
269 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
271 (defsubst org-set-local (var value)
272 "Make VAR local in current buffer and set it to VALUE."
273 (set (make-local-variable var) value))
275 (defsubst org-last (list)
276 "Return the last element of LIST."
277 (car (last list)))
279 (defun org-let (list &rest body)
280 (eval (cons 'let (cons list body))))
281 (put 'org-let 'lisp-indent-function 1)
283 (defun org-let2 (list1 list2 &rest body)
284 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
285 (put 'org-let2 'lisp-indent-function 2)
287 (defsubst org-call-with-arg (command arg)
288 "Call COMMAND interactively, but pretend prefix arg was ARG."
289 (let ((current-prefix-arg arg)) (call-interactively command)))
291 (defsubst org-current-line (&optional pos)
292 (save-excursion
293 (and pos (goto-char pos))
294 ;; works also in narrowed buffer, because we start at 1, not point-min
295 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
297 (defsubst org-goto-line (N)
298 (save-restriction
299 (widen)
300 (goto-char (point-min))
301 (forward-line (1- N))))
303 (defsubst org-current-line-string (&optional to-here)
304 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
306 (defsubst org-pos-in-match-range (pos n)
307 (and (match-beginning n)
308 (<= (match-beginning n) pos)
309 (>= (match-end n) pos)))
311 (defun org-autoload (file functions)
312 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
313 (let ((d (format "Documentation will be available after `%s.el' is loaded."
314 file))
316 (while (setq f (pop functions))
317 (or (fboundp f) (autoload f file d t)))))
319 (defun org-match-line (re)
320 "Looking-at at the beginning of the current line."
321 (save-excursion
322 (goto-char (point-at-bol))
323 (looking-at re)))
325 (defun org-plist-delete (plist property)
326 "Delete PROPERTY from PLIST.
327 This is in contrast to merely setting it to 0."
328 (let (p)
329 (while plist
330 (if (not (eq property (car plist)))
331 (setq p (plist-put p (car plist) (nth 1 plist))))
332 (setq plist (cddr plist)))
335 (defun org-replace-match-keep-properties (newtext &optional fixedcase
336 literal string)
337 "Like `replace-match', but add the text properties found original text."
338 (setq newtext (org-add-props newtext (text-properties-at
339 (match-beginning 0) string)))
340 (replace-match newtext fixedcase literal string))
342 (defmacro org-save-outline-visibility (use-markers &rest body)
343 "Save and restore outline visibility around BODY.
344 If USE-MARKERS is non-nil, use markers for the positions.
345 This means that the buffer may change while running BODY,
346 but it also means that the buffer should stay alive
347 during the operation, because otherwise all these markers will
348 point nowhere."
349 (declare (indent 1))
350 (org-with-gensyms (data rtn)
351 `(let ((,data (org-outline-overlay-data ,use-markers))
352 ,rtn)
353 (unwind-protect
354 (progn
355 (setq ,rtn (progn ,@body))
356 (org-set-outline-overlay-data ,data))
357 (when ,use-markers
358 (mapc (lambda (c)
359 (and (markerp (car c)) (move-marker (car c) nil))
360 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
361 ,data)))
362 ,rtn)))
363 (def-edebug-spec org-save-outline-visibility (form body))
365 (defmacro org-with-wide-buffer (&rest body)
366 "Execute body while temporarily widening the buffer."
367 `(save-excursion
368 (save-restriction
369 (widen)
370 ,@body)))
371 (def-edebug-spec org-with-wide-buffer (body))
373 (defmacro org-with-limited-levels (&rest body)
374 "Execute BODY with limited number of outline levels."
375 `(let* ((org-outline-regexp (org-get-limited-outline-regexp))
376 (outline-regexp org-outline-regexp)
377 (org-outline-regexp-at-bol (concat "^" org-outline-regexp)))
378 ,@body))
379 (def-edebug-spec org-with-limited-levels (body))
381 (defvar org-outline-regexp) ; defined in org.el
382 (defvar org-odd-levels-only) ; defined in org.el
383 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
384 (defun org-get-limited-outline-regexp ()
385 "Return outline-regexp with limited number of levels.
386 The number of levels is controlled by `org-inlinetask-min-level'"
387 (if (or (not (eq major-mode 'org-mode)) (not (featurep 'org-inlinetask)))
388 org-outline-regexp
389 (let* ((limit-level (1- org-inlinetask-min-level))
390 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
391 (format "\\*\\{1,%d\\} " nstars))))
393 (defun org-format-seconds (string seconds)
394 "Compatibility function replacing format-seconds"
395 (if (fboundp 'format-seconds)
396 (format-seconds string seconds)
397 (format-time-string string (seconds-to-time seconds))))
399 (defmacro org-eval-in-environment (environment form)
400 `(eval (list 'let ,environment ',form)))
401 (def-edebug-spec org-eval-in-environment (form form))
402 (put 'org-eval-in-environment 'lisp-indent-function 1)
404 (defun org-make-parameter-alist (flat)
405 "Return alist based on FLAT.
406 FLAT is a list with alternating symbol names and values. The
407 returned alist is a list of lists with the symbol name in car and
408 the value in cdr."
409 (when flat
410 (cons (list (car flat) (cadr flat))
411 (org-make-parameter-alist (cddr flat)))))
413 (provide 'org-macs)
415 ;;; org-macs.el ends here