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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
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."
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
)
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
)
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
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?"
81 (org-string-match-p "\\S-" 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
)
97 (def-edebug-spec org-unmodified
(body))
99 (defun org-substitute-posix-classes (re)
100 "Substitute posix classes in regular expression RE."
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
)))
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)))
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
))
134 (partial-completion-mode -
1)
136 (partial-completion-mode 1))
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
)
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)
158 (if (markerp ,mpom
) (set-buffer (marker-buffer ,mpom
)))
160 (goto-char (or ,mpom
(point)))
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
)
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
)
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
)
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."
192 (unless (apply 're-search-forward args
)
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))
203 (,buf1
(current-buffer))
205 (,undo1 buffer-undo-list
)
206 (,undo2
(with-current-buffer ,_buffer buffer-undo-list
))
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
)))
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
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
)
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
)
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-mode-p
()
276 "Check if the current buffer is in Org-mode."
277 (eq major-mode
'org-mode
))
279 (defsubst org-last
(list)
280 "Return the last element of LIST."
283 (defun org-let (list &rest body
)
284 (eval (cons 'let
(cons list body
))))
285 (put 'org-let
'lisp-indent-function
1)
287 (defun org-let2 (list1 list2
&rest body
)
288 (eval (cons 'let
(cons list1
(list (cons 'let
(cons list2 body
)))))))
289 (put 'org-let2
'lisp-indent-function
2)
291 (defsubst org-call-with-arg
(command arg
)
292 "Call COMMAND interactively, but pretend prefix arg was ARG."
293 (let ((current-prefix-arg arg
)) (call-interactively command
)))
295 (defsubst org-current-line
(&optional pos
)
297 (and pos
(goto-char pos
))
298 ;; works also in narrowed buffer, because we start at 1, not point-min
299 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
301 (defsubst org-goto-line
(N)
304 (goto-char (point-min))
305 (forward-line (1- N
))))
307 (defsubst org-current-line-string
(&optional to-here
)
308 (buffer-substring (point-at-bol) (if to-here
(point) (point-at-eol))))
310 (defsubst org-pos-in-match-range
(pos n
)
311 (and (match-beginning n
)
312 (<= (match-beginning n
) pos
)
313 (>= (match-end n
) pos
)))
315 (defun org-autoload (file functions
)
316 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
317 (let ((d (format "Documentation will be available after `%s.el' is loaded."
320 (while (setq f
(pop functions
))
321 (or (fboundp f
) (autoload f file d t
)))))
323 (defun org-match-line (re)
324 "Looking-at at the beginning of the current line."
326 (goto-char (point-at-bol))
329 (defun org-plist-delete (plist property
)
330 "Delete PROPERTY from PLIST.
331 This is in contrast to merely setting it to 0."
334 (if (not (eq property
(car plist
)))
335 (setq p
(plist-put p
(car plist
) (nth 1 plist
))))
336 (setq plist
(cddr plist
)))
339 (defun org-replace-match-keep-properties (newtext &optional fixedcase
341 "Like `replace-match', but add the text properties found original text."
342 (setq newtext
(org-add-props newtext
(text-properties-at
343 (match-beginning 0) string
)))
344 (replace-match newtext fixedcase literal string
))
346 (defmacro org-save-outline-visibility
(use-markers &rest body
)
347 "Save and restore outline visibility around BODY.
348 If USE-MARKERS is non-nil, use markers for the positions.
349 This means that the buffer may change while running BODY,
350 but it also means that the buffer should stay alive
351 during the operation, because otherwise all these markers will
354 (org-with-gensyms (data rtn
)
355 `(let ((,data
(org-outline-overlay-data ,use-markers
))
359 (setq ,rtn
(progn ,@body
))
360 (org-set-outline-overlay-data ,data
))
363 (and (markerp (car c
)) (move-marker (car c
) nil
))
364 (and (markerp (cdr c
)) (move-marker (cdr c
) nil
)))
367 (def-edebug-spec org-save-outline-visibility
(form body
))
369 (defmacro org-with-wide-buffer
(&rest body
)
370 "Execute body while temporarily widening the buffer."
375 (def-edebug-spec org-with-wide-buffer
(body))
377 (defmacro org-with-limited-levels
(&rest body
)
378 "Execute BODY with limited number of outline levels."
379 `(let* ((org-outline-regexp (org-get-limited-outline-regexp))
380 (outline-regexp org-outline-regexp
)
381 (org-outline-regexp-at-bol (concat "^" org-outline-regexp
)))
383 (def-edebug-spec org-with-limited-levels
(body))
385 (defvar org-outline-regexp
) ; defined in org.el
386 (defvar org-odd-levels-only
) ; defined in org.el
387 (defvar org-inlinetask-min-level
) ; defined in org-inlinetask.el
388 (defun org-get-limited-outline-regexp ()
389 "Return outline-regexp with limited number of levels.
390 The number of levels is controlled by `org-inlinetask-min-level'"
391 (if (or (not (org-mode-p)) (not (featurep 'org-inlinetask
)))
393 (let* ((limit-level (1- org-inlinetask-min-level
))
394 (nstars (if org-odd-levels-only
(1- (* limit-level
2)) limit-level
)))
395 (format "\\*\\{1,%d\\} " nstars
))))
397 (defun org-format-seconds (string seconds
)
398 "Compatibility function replacing format-seconds"
399 (if (fboundp 'format-seconds
)
400 (format-seconds string seconds
)
401 (format-time-string string
(seconds-to-time seconds
))))
403 (defmacro org-eval-in-environment
(environment form
)
404 `(eval (list 'let
,environment
',form
)))
405 (def-edebug-spec org-eval-in-environment
(form form
))
406 (put 'org-eval-in-environment
'lisp-indent-function
1)
408 (defun org-make-parameter-alist (flat)
409 "Return alist based on FLAT.
410 FLAT is a list with alternating symbol names and values. The
411 returned alist is a list of lists with the symbol name in car and
414 (cons (list (car flat
) (cadr flat
))
415 (org-make-parameter-alist (cddr flat
)))))
419 ;;; org-macs.el ends here