* lisp/international/characters.el (standard-case-table): Set syntax of ?ยป
[emacs.git] / lisp / org / org-macs.el
blob57b2d8a577e9ed3573181e737091aed081bc7883
1 ;;; org-macs.el --- Top-level definitions for Org-mode
3 ;; Copyright (C) 2004-2013 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 ;; defined with no argument in <=23.1
62 `(with-no-warnings (called-interactively-p ,kind))
63 `(interactive-p))))
64 (def-edebug-spec org-called-interactively-p (&optional ("quote" symbolp)))
66 (when (and (not (fboundp 'with-silent-modifications))
67 (or (< emacs-major-version 23)
68 (and (= emacs-major-version 23)
69 (< emacs-minor-version 2))))
70 (defmacro with-silent-modifications (&rest body)
71 `(org-unmodified ,@body))
72 (def-edebug-spec with-silent-modifications (body)))
74 (defmacro org-bound-and-true-p (var)
75 "Return the value of symbol VAR if it is bound, else nil."
76 `(and (boundp (quote ,var)) ,var))
77 (def-edebug-spec org-bound-and-true-p (symbolp))
79 (defun org-string-nw-p (s)
80 "Is S a string with a non-white character?"
81 (and (stringp s)
82 (org-string-match-p "\\S-" s)
83 s))
85 (defun org-not-nil (v)
86 "If V not nil, and also not the string \"nil\", then return V.
87 Otherwise return nil."
88 (and v (not (equal v "nil")) v))
90 (defmacro org-unmodified (&rest body)
91 "Execute body without changing `buffer-modified-p'.
92 Also, do not record undo information."
93 `(set-buffer-modified-p
94 (prog1 (buffer-modified-p)
95 (let ((buffer-undo-list t)
96 (inhibit-modification-hooks t))
97 ,@body))))
98 (def-edebug-spec org-unmodified (body))
100 (defun org-substitute-posix-classes (re)
101 "Substitute posix classes in regular expression RE."
102 (let ((ss re))
103 (save-match-data
104 (while (string-match "\\[:alnum:\\]" ss)
105 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
106 (while (string-match "\\[:word:\\]" ss)
107 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
108 (while (string-match "\\[:alpha:\\]" ss)
109 (setq ss (replace-match "a-zA-Z" t t ss)))
110 (while (string-match "\\[:punct:\\]" ss)
111 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
112 ss)))
114 (defmacro org-re (s)
115 "Replace posix classes in regular expression."
116 (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
117 (def-edebug-spec org-re (form))
119 (defmacro org-preserve-lc (&rest body)
120 (org-with-gensyms (line col)
121 `(let ((,line (org-current-line))
122 (,col (current-column)))
123 (unwind-protect
124 (progn ,@body)
125 (org-goto-line ,line)
126 (org-move-to-column ,col)))))
127 (def-edebug-spec org-preserve-lc (body))
129 ;; Copied from bookmark.el
130 (defmacro org-with-buffer-modified-unmodified (&rest body)
131 "Run BODY while preserving the buffer's `buffer-modified-p' state."
132 (org-with-gensyms (was-modified)
133 `(let ((,was-modified (buffer-modified-p)))
134 (unwind-protect
135 (progn ,@body)
136 (set-buffer-modified-p ,was-modified)))))
138 (defmacro org-without-partial-completion (&rest body)
139 `(if (and (boundp 'partial-completion-mode)
140 partial-completion-mode
141 (fboundp 'partial-completion-mode))
142 (unwind-protect
143 (progn
144 (partial-completion-mode -1)
145 ,@body)
146 (partial-completion-mode 1))
147 ,@body))
148 (def-edebug-spec org-without-partial-completion (body))
150 ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
151 (defmacro org-maybe-intangible (props)
152 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
153 In Emacs 21, invisible text is not avoided by the command loop, so the
154 intangible property is needed to make sure point skips this text.
155 In Emacs 22, this is not necessary. The intangible text property has
156 led to problems with flyspell. These problems are fixed in flyspell.el,
157 but we still avoid setting the property in Emacs 22 and later.
158 We use a macro so that the test can happen at compilation time."
159 (if (< emacs-major-version 22)
160 `(append '(intangible t) ,props)
161 props))
163 (defmacro org-with-point-at (pom &rest body)
164 "Move to buffer and point of point-or-marker POM for the duration of BODY."
165 (org-with-gensyms (mpom)
166 `(let ((,mpom ,pom))
167 (save-excursion
168 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
169 (save-excursion
170 (goto-char (or ,mpom (point)))
171 ,@body)))))
172 (def-edebug-spec org-with-point-at (form body))
173 (put 'org-with-point-at 'lisp-indent-function 1)
175 (defmacro org-no-warnings (&rest body)
176 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
177 (def-edebug-spec org-no-warnings (body))
179 (defmacro org-if-unprotected (&rest body)
180 "Execute BODY if there is no `org-protected' text property at point."
181 `(unless (get-text-property (point) 'org-protected)
182 ,@body))
183 (def-edebug-spec org-if-unprotected (body))
185 (defmacro org-if-unprotected-1 (&rest body)
186 "Execute BODY if there is no `org-protected' text property at point-1."
187 `(unless (get-text-property (1- (point)) 'org-protected)
188 ,@body))
189 (def-edebug-spec org-if-unprotected-1 (body))
191 (defmacro org-if-unprotected-at (pos &rest body)
192 "Execute BODY if there is no `org-protected' text property at POS."
193 `(unless (get-text-property ,pos 'org-protected)
194 ,@body))
195 (def-edebug-spec org-if-unprotected-at (form body))
196 (put 'org-if-unprotected-at 'lisp-indent-function 1)
198 (defun org-re-search-forward-unprotected (&rest args)
199 "Like re-search-forward, but stop only in unprotected places."
200 (catch 'exit
201 (while t
202 (unless (apply 're-search-forward args)
203 (throw 'exit nil))
204 (unless (get-text-property (match-beginning 0) 'org-protected)
205 (throw 'exit (point))))))
207 ;; FIXME: Normalize argument names
208 (defmacro org-with-remote-undo (_buffer &rest _body)
209 "Execute BODY while recording undo information in two buffers."
210 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
211 `(let ((,cline (org-current-line))
212 (,cmd this-command)
213 (,buf1 (current-buffer))
214 (,buf2 ,_buffer)
215 (,undo1 buffer-undo-list)
216 (,undo2 (with-current-buffer ,_buffer buffer-undo-list))
217 ,c1 ,c2)
218 ,@_body
219 (when org-agenda-allow-remote-undo
220 (setq ,c1 (org-verify-change-for-undo
221 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
222 ,c2 (org-verify-change-for-undo
223 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
224 (when (or ,c1 ,c2)
225 ;; make sure there are undo boundaries
226 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
227 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
228 ;; remember which buffer to undo
229 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
230 org-agenda-undo-list))))))
231 (def-edebug-spec org-with-remote-undo (form body))
232 (put 'org-with-remote-undo 'lisp-indent-function 1)
234 (defmacro org-no-read-only (&rest body)
235 "Inhibit read-only for BODY."
236 `(let ((inhibit-read-only t)) ,@body))
237 (def-edebug-spec org-no-read-only (body))
239 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
240 rear-nonsticky t mouse-map t fontified t
241 org-emphasis t)
242 "Properties to remove when a string without properties is wanted.")
244 (defsubst org-match-string-no-properties (num &optional string)
245 (if (featurep 'xemacs)
246 (let ((s (match-string num string)))
247 (and s (remove-text-properties 0 (length s) org-rm-props s))
249 (match-string-no-properties num string)))
251 (defsubst org-no-properties (s &optional restricted)
252 "Remove all text properties from string S.
253 When RESTRICTED is non-nil, only remove the properties listed
254 in `org-rm-props'."
255 (if (fboundp 'set-text-properties)
256 (set-text-properties 0 (length s) nil s)
257 (if restricted
258 (remove-text-properties 0 (length s) org-rm-props s)
259 (set-text-properties 0 (length s) nil s)))
262 (defsubst org-get-alist-option (option key)
263 (cond ((eq key t) t)
264 ((eq option t) t)
265 ((assoc key option) (cdr (assoc key option)))
266 (t (let ((r (cdr (assq 'default option))))
267 (if (listp r) (delq nil r) r)))))
269 (defsubst org-check-external-command (cmd &optional use no-error)
270 "Check if external program CMD for USE exists, error if not.
271 When the program does exist, return its path.
272 When it does not exist and NO-ERROR is set, return nil.
273 Otherwise, throw an error. The optional argument USE can describe what this
274 program is needed for, so that the error message can be more informative."
275 (or (executable-find cmd)
276 (if no-error
278 (error "Can't find `%s'%s" cmd
279 (if use (format " (%s)" use) "")))))
281 (defsubst org-inhibit-invisibility ()
282 "Modified `buffer-invisibility-spec' for Emacs 21.
283 Some ops with invisible text do not work correctly on Emacs 21. For these
284 we turn off invisibility temporarily. Use this in a `let' form."
285 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
287 (defsubst org-set-local (var value)
288 "Make VAR local in current buffer and set it to VALUE."
289 (set (make-local-variable var) value))
291 (defsubst org-last (list)
292 "Return the last element of LIST."
293 (car (last list)))
295 (defun org-let (list &rest body)
296 (eval (cons 'let (cons list body))))
297 (put 'org-let 'lisp-indent-function 1)
299 (defun org-let2 (list1 list2 &rest body)
300 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
301 (put 'org-let2 'lisp-indent-function 2)
303 (defsubst org-call-with-arg (command arg)
304 "Call COMMAND interactively, but pretend prefix arg was ARG."
305 (let ((current-prefix-arg arg)) (call-interactively command)))
307 (defsubst org-current-line (&optional pos)
308 (save-excursion
309 (and pos (goto-char pos))
310 ;; works also in narrowed buffer, because we start at 1, not point-min
311 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
313 (defsubst org-goto-line (N)
314 (save-restriction
315 (widen)
316 (goto-char (point-min))
317 (forward-line (1- N))))
319 (defsubst org-current-line-string (&optional to-here)
320 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
322 (defsubst org-pos-in-match-range (pos n)
323 (and (match-beginning n)
324 (<= (match-beginning n) pos)
325 (>= (match-end n) pos)))
327 (defun org-autoload (file functions)
328 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
329 (let ((d (format "Documentation will be available after `%s.el' is loaded."
330 file))
332 (while (setq f (pop functions))
333 (or (fboundp f) (autoload f file d t)))))
335 (defun org-match-line (re)
336 "Looking-at at the beginning of the current line."
337 (save-excursion
338 (goto-char (point-at-bol))
339 (looking-at re)))
341 (defun org-plist-delete (plist property)
342 "Delete PROPERTY from PLIST.
343 This is in contrast to merely setting it to 0."
344 (let (p)
345 (while plist
346 (if (not (eq property (car plist)))
347 (setq p (plist-put p (car plist) (nth 1 plist))))
348 (setq plist (cddr plist)))
351 (defun org-replace-match-keep-properties (newtext &optional fixedcase
352 literal string)
353 "Like `replace-match', but add the text properties found original text."
354 (setq newtext (org-add-props newtext (text-properties-at
355 (match-beginning 0) string)))
356 (replace-match newtext fixedcase literal string))
358 (defmacro org-save-outline-visibility (use-markers &rest body)
359 "Save and restore outline visibility around BODY.
360 If USE-MARKERS is non-nil, use markers for the positions.
361 This means that the buffer may change while running BODY,
362 but it also means that the buffer should stay alive
363 during the operation, because otherwise all these markers will
364 point nowhere."
365 (declare (indent 1))
366 (org-with-gensyms (data rtn)
367 `(let ((,data (org-outline-overlay-data ,use-markers))
368 ,rtn)
369 (unwind-protect
370 (progn
371 (setq ,rtn (progn ,@body))
372 (org-set-outline-overlay-data ,data))
373 (when ,use-markers
374 (mapc (lambda (c)
375 (and (markerp (car c)) (move-marker (car c) nil))
376 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
377 ,data)))
378 ,rtn)))
379 (def-edebug-spec org-save-outline-visibility (form body))
381 (defmacro org-with-wide-buffer (&rest body)
382 "Execute body while temporarily widening the buffer."
383 `(save-excursion
384 (save-restriction
385 (widen)
386 ,@body)))
387 (def-edebug-spec org-with-wide-buffer (body))
389 (defmacro org-with-limited-levels (&rest body)
390 "Execute BODY with limited number of outline levels."
391 `(let* ((org-called-with-limited-levels t)
392 (org-outline-regexp (org-get-limited-outline-regexp))
393 (outline-regexp org-outline-regexp)
394 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
395 ,@body))
396 (def-edebug-spec org-with-limited-levels (body))
398 (defvar org-outline-regexp) ; defined in org.el
399 (defvar org-odd-levels-only) ; defined in org.el
400 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
401 (defun org-get-limited-outline-regexp ()
402 "Return outline-regexp with limited number of levels.
403 The number of levels is controlled by `org-inlinetask-min-level'"
404 (if (or (not (derived-mode-p 'org-mode)) (not (featurep 'org-inlinetask)))
405 org-outline-regexp
406 (let* ((limit-level (1- org-inlinetask-min-level))
407 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
408 (format "\\*\\{1,%d\\} " nstars))))
410 (defun org-format-seconds (string seconds)
411 "Compatibility function replacing format-seconds."
412 (if (fboundp 'format-seconds)
413 (format-seconds string seconds)
414 (format-time-string string (seconds-to-time seconds))))
416 (defmacro org-eval-in-environment (environment form)
417 `(eval (list 'let ,environment ',form)))
418 (def-edebug-spec org-eval-in-environment (form form))
419 (put 'org-eval-in-environment 'lisp-indent-function 1)
421 (defun org-make-parameter-alist (flat)
422 "Return alist based on FLAT.
423 FLAT is a list with alternating symbol names and values. The
424 returned alist is a list of lists with the symbol name in car and
425 the value in cdr."
426 (when flat
427 (cons (list (car flat) (cadr flat))
428 (org-make-parameter-alist (cddr flat)))))
430 (provide 'org-macs)
432 ;;; org-macs.el ends here