]> code.delx.au - gnu-emacs/blob - lisp/org/org-macs.el
Update copyright notices for 2013.
[gnu-emacs] / lisp / org / org-macs.el
1 ;;; org-macs.el --- Top-level definitions for Org-mode
2
3 ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
4
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.
10 ;;
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.
15
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.
20
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24 ;;
25 ;;; Commentary:
26
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.
31
32 ;;; Code:
33
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)))))
44
45 (declare-function org-add-props "org-compat" (string plist &rest props))
46 (declare-function org-string-match-p "org-compat" (&rest args))
47
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)
54
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)))
65
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)))
73
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))
78
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))
84
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))
89
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 before-change-functions after-change-functions)
97 ,@body))))
98 (def-edebug-spec org-unmodified (body))
99
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)))
113
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))
118
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))
128
129 (defmacro org-without-partial-completion (&rest body)
130 `(if (and (boundp 'partial-completion-mode)
131 partial-completion-mode
132 (fboundp 'partial-completion-mode))
133 (unwind-protect
134 (progn
135 (partial-completion-mode -1)
136 ,@body)
137 (partial-completion-mode 1))
138 ,@body))
139 (def-edebug-spec org-without-partial-completion (body))
140
141 ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
142 (defmacro org-maybe-intangible (props)
143 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
144 In Emacs 21, invisible text is not avoided by the command loop, so the
145 intangible property is needed to make sure point skips this text.
146 In Emacs 22, this is not necessary. The intangible text property has
147 led to problems with flyspell. These problems are fixed in flyspell.el,
148 but we still avoid setting the property in Emacs 22 and later.
149 We use a macro so that the test can happen at compilation time."
150 (if (< emacs-major-version 22)
151 `(append '(intangible t) ,props)
152 props))
153
154 (defmacro org-with-point-at (pom &rest body)
155 "Move to buffer and point of point-or-marker POM for the duration of BODY."
156 (org-with-gensyms (mpom)
157 `(let ((,mpom ,pom))
158 (save-excursion
159 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
160 (save-excursion
161 (goto-char (or ,mpom (point)))
162 ,@body)))))
163 (def-edebug-spec org-with-point-at (form body))
164 (put 'org-with-point-at 'lisp-indent-function 1)
165
166 (defmacro org-no-warnings (&rest body)
167 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
168 (def-edebug-spec org-no-warnings (body))
169
170 (defmacro org-if-unprotected (&rest body)
171 "Execute BODY if there is no `org-protected' text property at point."
172 `(unless (get-text-property (point) 'org-protected)
173 ,@body))
174 (def-edebug-spec org-if-unprotected (body))
175
176 (defmacro org-if-unprotected-1 (&rest body)
177 "Execute BODY if there is no `org-protected' text property at point-1."
178 `(unless (get-text-property (1- (point)) 'org-protected)
179 ,@body))
180 (def-edebug-spec org-if-unprotected-1 (body))
181
182 (defmacro org-if-unprotected-at (pos &rest body)
183 "Execute BODY if there is no `org-protected' text property at POS."
184 `(unless (get-text-property ,pos 'org-protected)
185 ,@body))
186 (def-edebug-spec org-if-unprotected-at (form body))
187 (put 'org-if-unprotected-at 'lisp-indent-function 1)
188
189 (defun org-re-search-forward-unprotected (&rest args)
190 "Like re-search-forward, but stop only in unprotected places."
191 (catch 'exit
192 (while t
193 (unless (apply 're-search-forward args)
194 (throw 'exit nil))
195 (unless (get-text-property (match-beginning 0) 'org-protected)
196 (throw 'exit (point))))))
197
198 ;; FIXME: Normalize argument names
199 (defmacro org-with-remote-undo (_buffer &rest _body)
200 "Execute BODY while recording undo information in two buffers."
201 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
202 `(let ((,cline (org-current-line))
203 (,cmd this-command)
204 (,buf1 (current-buffer))
205 (,buf2 ,_buffer)
206 (,undo1 buffer-undo-list)
207 (,undo2 (with-current-buffer ,_buffer buffer-undo-list))
208 ,c1 ,c2)
209 ,@_body
210 (when org-agenda-allow-remote-undo
211 (setq ,c1 (org-verify-change-for-undo
212 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
213 ,c2 (org-verify-change-for-undo
214 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
215 (when (or ,c1 ,c2)
216 ;; make sure there are undo boundaries
217 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
218 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
219 ;; remember which buffer to undo
220 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
221 org-agenda-undo-list))))))
222 (def-edebug-spec org-with-remote-undo (form body))
223 (put 'org-with-remote-undo 'lisp-indent-function 1)
224
225 (defmacro org-no-read-only (&rest body)
226 "Inhibit read-only for BODY."
227 `(let ((inhibit-read-only t)) ,@body))
228 (def-edebug-spec org-no-read-only (body))
229
230 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
231 rear-nonsticky t mouse-map t fontified t
232 org-emphasis t)
233 "Properties to remove when a string without properties is wanted.")
234
235 (defsubst org-match-string-no-properties (num &optional string)
236 (if (featurep 'xemacs)
237 (let ((s (match-string num string)))
238 (and s (remove-text-properties 0 (length s) org-rm-props s))
239 s)
240 (match-string-no-properties num string)))
241
242 (defsubst org-no-properties (s &optional restricted)
243 "Remove all text properties from string S.
244 When RESTRICTED is non-nil, only remove the properties listed
245 in `org-rm-props'."
246 (if (fboundp 'set-text-properties)
247 (set-text-properties 0 (length s) nil s)
248 (if restricted
249 (remove-text-properties 0 (length s) org-rm-props s)
250 (set-text-properties 0 (length s) nil s)))
251 s)
252
253 (defsubst org-get-alist-option (option key)
254 (cond ((eq key t) t)
255 ((eq option t) t)
256 ((assoc key option) (cdr (assoc key option)))
257 (t (cdr (assq 'default option)))))
258
259 (defsubst org-check-external-command (cmd &optional use no-error)
260 "Check if external program CMD for USE exists, error if not.
261 When the program does exist, return its path.
262 When it does not exist and NO-ERROR is set, return nil.
263 Otherwise, throw an error. The optional argument USE can describe what this
264 program is needed for, so that the error message can be more informative."
265 (or (executable-find cmd)
266 (if no-error
267 nil
268 (error "Can't find `%s'%s" cmd
269 (if use (format " (%s)" use) "")))))
270
271 (defsubst org-inhibit-invisibility ()
272 "Modified `buffer-invisibility-spec' for Emacs 21.
273 Some ops with invisible text do not work correctly on Emacs 21. For these
274 we turn off invisibility temporarily. Use this in a `let' form."
275 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
276
277 (defsubst org-set-local (var value)
278 "Make VAR local in current buffer and set it to VALUE."
279 (set (make-local-variable var) value))
280
281 (defsubst org-last (list)
282 "Return the last element of LIST."
283 (car (last list)))
284
285 (defun org-let (list &rest body)
286 (eval (cons 'let (cons list body))))
287 (put 'org-let 'lisp-indent-function 1)
288
289 (defun org-let2 (list1 list2 &rest body)
290 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
291 (put 'org-let2 'lisp-indent-function 2)
292
293 (defsubst org-call-with-arg (command arg)
294 "Call COMMAND interactively, but pretend prefix arg was ARG."
295 (let ((current-prefix-arg arg)) (call-interactively command)))
296
297 (defsubst org-current-line (&optional pos)
298 (save-excursion
299 (and pos (goto-char pos))
300 ;; works also in narrowed buffer, because we start at 1, not point-min
301 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
302
303 (defsubst org-goto-line (N)
304 (save-restriction
305 (widen)
306 (goto-char (point-min))
307 (forward-line (1- N))))
308
309 (defsubst org-current-line-string (&optional to-here)
310 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
311
312 (defsubst org-pos-in-match-range (pos n)
313 (and (match-beginning n)
314 (<= (match-beginning n) pos)
315 (>= (match-end n) pos)))
316
317 (defun org-autoload (file functions)
318 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
319 (let ((d (format "Documentation will be available after `%s.el' is loaded."
320 file))
321 f)
322 (while (setq f (pop functions))
323 (or (fboundp f) (autoload f file d t)))))
324
325 (defun org-match-line (re)
326 "Looking-at at the beginning of the current line."
327 (save-excursion
328 (goto-char (point-at-bol))
329 (looking-at re)))
330
331 (defun org-plist-delete (plist property)
332 "Delete PROPERTY from PLIST.
333 This is in contrast to merely setting it to 0."
334 (let (p)
335 (while plist
336 (if (not (eq property (car plist)))
337 (setq p (plist-put p (car plist) (nth 1 plist))))
338 (setq plist (cddr plist)))
339 p))
340
341 (defun org-replace-match-keep-properties (newtext &optional fixedcase
342 literal string)
343 "Like `replace-match', but add the text properties found original text."
344 (setq newtext (org-add-props newtext (text-properties-at
345 (match-beginning 0) string)))
346 (replace-match newtext fixedcase literal string))
347
348 (defmacro org-save-outline-visibility (use-markers &rest body)
349 "Save and restore outline visibility around BODY.
350 If USE-MARKERS is non-nil, use markers for the positions.
351 This means that the buffer may change while running BODY,
352 but it also means that the buffer should stay alive
353 during the operation, because otherwise all these markers will
354 point nowhere."
355 (declare (indent 1))
356 (org-with-gensyms (data rtn)
357 `(let ((,data (org-outline-overlay-data ,use-markers))
358 ,rtn)
359 (unwind-protect
360 (progn
361 (setq ,rtn (progn ,@body))
362 (org-set-outline-overlay-data ,data))
363 (when ,use-markers
364 (mapc (lambda (c)
365 (and (markerp (car c)) (move-marker (car c) nil))
366 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
367 ,data)))
368 ,rtn)))
369 (def-edebug-spec org-save-outline-visibility (form body))
370
371 (defmacro org-with-wide-buffer (&rest body)
372 "Execute body while temporarily widening the buffer."
373 `(save-excursion
374 (save-restriction
375 (widen)
376 ,@body)))
377 (def-edebug-spec org-with-wide-buffer (body))
378
379 (defmacro org-with-limited-levels (&rest body)
380 "Execute BODY with limited number of outline levels."
381 `(let* ((org-called-with-limited-levels t)
382 (org-outline-regexp (org-get-limited-outline-regexp))
383 (outline-regexp org-outline-regexp)
384 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
385 ,@body))
386 (def-edebug-spec org-with-limited-levels (body))
387
388 (defvar org-outline-regexp) ; defined in org.el
389 (defvar org-odd-levels-only) ; defined in org.el
390 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
391 (defun org-get-limited-outline-regexp ()
392 "Return outline-regexp with limited number of levels.
393 The number of levels is controlled by `org-inlinetask-min-level'"
394 (if (or (not (derived-mode-p 'org-mode)) (not (featurep 'org-inlinetask)))
395 org-outline-regexp
396 (let* ((limit-level (1- org-inlinetask-min-level))
397 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
398 (format "\\*\\{1,%d\\} " nstars))))
399
400 (defun org-format-seconds (string seconds)
401 "Compatibility function replacing format-seconds."
402 (if (fboundp 'format-seconds)
403 (format-seconds string seconds)
404 (format-time-string string (seconds-to-time seconds))))
405
406 (defmacro org-eval-in-environment (environment form)
407 `(eval (list 'let ,environment ',form)))
408 (def-edebug-spec org-eval-in-environment (form form))
409 (put 'org-eval-in-environment 'lisp-indent-function 1)
410
411 (defun org-make-parameter-alist (flat)
412 "Return alist based on FLAT.
413 FLAT is a list with alternating symbol names and values. The
414 returned alist is a list of lists with the symbol name in car and
415 the value in cdr."
416 (when flat
417 (cons (list (car flat) (cadr flat))
418 (org-make-parameter-alist (cddr flat)))))
419
420 (provide 'org-macs)
421
422 ;;; org-macs.el ends here