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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
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."
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-called-interactively-p
(&optional kind
)
51 `(if (featurep 'xemacs
)
53 (if (or (> emacs-major-version
23)
54 (and (>= emacs-major-version
23)
55 (>= emacs-minor-version
2)))
56 (with-no-warnings (called-interactively-p ,kind
)) ;; defined with no argument in <=23.1
59 (if (and (not (fboundp 'with-silent-modifications
))
60 (or (< emacs-major-version
23)
61 (and (= emacs-major-version
23)
62 (< emacs-minor-version
2))))
63 (defmacro with-silent-modifications
(&rest body
)
64 `(org-unmodified ,@body
)))
66 (defmacro org-bound-and-true-p
(var)
67 "Return the value of symbol VAR if it is bound, else nil."
68 `(and (boundp (quote ,var
)) ,var
))
70 (defun org-string-nw-p (s)
71 "Is S a string with a non-white character?"
73 (org-string-match-p "\\S-" s
)
76 (defun org-not-nil (v)
77 "If V not nil, and also not the string \"nil\", then return V.
78 Otherwise return nil."
79 (and v
(not (equal v
"nil")) v
))
81 (defmacro org-unmodified
(&rest body
)
82 "Execute body without changing `buffer-modified-p'.
83 Also, do not record undo information."
84 `(set-buffer-modified-p
85 (prog1 (buffer-modified-p)
86 (let ((buffer-undo-list t
)
87 before-change-functions after-change-functions
)
91 "Replace posix classes in regular expression."
92 (if (featurep 'xemacs
)
95 (while (string-match "\\[:alnum:\\]" ss
)
96 (setq ss
(replace-match "a-zA-Z0-9" t t ss
)))
97 (while (string-match "\\[:word:\\]" ss
)
98 (setq ss
(replace-match "a-zA-Z0-9" t t ss
)))
99 (while (string-match "\\[:alpha:\\]" ss
)
100 (setq ss
(replace-match "a-zA-Z" t t ss
)))
101 (while (string-match "\\[:punct:\\]" ss
)
102 (setq ss
(replace-match "\001-@[-`{-~" t t ss
)))
106 (defmacro org-preserve-lc
(&rest body
)
107 `(let ((_line (org-current-line))
108 (_col (current-column)))
111 (org-goto-line _line
)
112 (org-move-to-column _col
))))
114 (defmacro org-without-partial-completion
(&rest body
)
115 `(let ((pc-mode (and (boundp 'partial-completion-mode
)
116 partial-completion-mode
)))
119 (if pc-mode
(partial-completion-mode -
1))
121 (if pc-mode
(partial-completion-mode 1)))))
123 (defmacro org-maybe-intangible
(props)
124 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
125 In Emacs 21, invisible text is not avoided by the command loop, so the
126 intangible property is needed to make sure point skips this text.
127 In Emacs 22, this is not necessary. The intangible text property has
128 led to problems with flyspell. These problems are fixed in flyspell.el,
129 but we still avoid setting the property in Emacs 22 and later.
130 We use a macro so that the test can happen at compilation time."
131 (if (< emacs-major-version
22)
132 `(append '(intangible t
) ,props
)
135 (defmacro org-with-point-at
(pom &rest body
)
136 "Move to buffer and point of point-or-marker POM for the duration of BODY."
138 (if (markerp ,pom
) (set-buffer (marker-buffer ,pom
)))
140 (goto-char (or ,pom
(point)))
142 (put 'org-with-point-at
'lisp-indent-function
1)
144 (defmacro org-no-warnings
(&rest body
)
145 (cons (if (fboundp 'with-no-warnings
) 'with-no-warnings
'progn
) body
))
147 (defmacro org-if-unprotected
(&rest body
)
148 "Execute BODY if there is no `org-protected' text property at point."
149 `(unless (get-text-property (point) 'org-protected
)
152 (defmacro org-if-unprotected-1
(&rest body
)
153 "Execute BODY if there is no `org-protected' text property at point-1."
154 `(unless (get-text-property (1- (point)) 'org-protected
)
157 (defmacro org-if-unprotected-at
(pos &rest body
)
158 "Execute BODY if there is no `org-protected' text property at POS."
159 `(unless (get-text-property ,pos
'org-protected
)
161 (put 'org-if-unprotected-at
'lisp-indent-function
1)
163 (defun org-re-search-forward-unprotected (&rest args
)
164 "Like re-search-forward, but stop only in unprotected places."
167 (unless (apply 're-search-forward args
)
169 (unless (get-text-property (match-beginning 0) 'org-protected
)
170 (throw 'exit
(point))))))
172 (defmacro org-with-remote-undo
(_buffer &rest _body
)
173 "Execute BODY while recording undo information in two buffers."
174 `(let ((_cline (org-current-line))
176 (_buf1 (current-buffer))
178 (_undo1 buffer-undo-list
)
179 (_undo2 (with-current-buffer ,_buffer buffer-undo-list
))
182 (when org-agenda-allow-remote-undo
183 (setq _c1
(org-verify-change-for-undo
184 _undo1
(with-current-buffer _buf1 buffer-undo-list
))
185 _c2
(org-verify-change-for-undo
186 _undo2
(with-current-buffer _buf2 buffer-undo-list
)))
188 ;; make sure there are undo boundaries
189 (and _c1
(with-current-buffer _buf1
(undo-boundary)))
190 (and _c2
(with-current-buffer _buf2
(undo-boundary)))
191 ;; remember which buffer to undo
192 (push (list _cmd _cline _buf1 _c1 _buf2 _c2
)
193 org-agenda-undo-list
)))))
195 (defmacro org-no-read-only
(&rest body
)
196 "Inhibit read-only for BODY."
197 `(let ((inhibit-read-only t
)) ,@body
))
199 (defconst org-rm-props
'(invisible t face t keymap t intangible t mouse-face t
200 rear-nonsticky t mouse-map t fontified t
202 "Properties to remove when a string without properties is wanted.")
204 (defsubst org-match-string-no-properties
(num &optional string
)
205 (if (featurep 'xemacs
)
206 (let ((s (match-string num string
)))
207 (and s
(remove-text-properties 0 (length s
) org-rm-props s
))
209 (match-string-no-properties num string
)))
211 (defsubst org-no-properties
(s)
212 (if (fboundp 'set-text-properties
)
213 (set-text-properties 0 (length s
) nil s
)
214 (remove-text-properties 0 (length s
) org-rm-props s
))
217 (defsubst org-get-alist-option
(option key
)
220 ((assoc key option
) (cdr (assoc key option
)))
221 (t (cdr (assq 'default option
)))))
223 (defsubst org-check-external-command
(cmd &optional use no-error
)
224 "Check if external program CMD for USE exists, error if not.
225 When the program does exist, return its path.
226 When it does not exist and NO-ERROR is set, return nil.
227 Otherwise, throw an error. The optional argument USE can describe what this
228 program is needed for, so that the error message can be more informative."
229 (or (executable-find cmd
)
232 (error "Can't find `%s'%s" cmd
233 (if use
(format " (%s)" use
) "")))))
235 (defsubst org-inhibit-invisibility
()
236 "Modified `buffer-invisibility-spec' for Emacs 21.
237 Some ops with invisible text do not work correctly on Emacs 21. For these
238 we turn off invisibility temporarily. Use this in a `let' form."
239 (if (< emacs-major-version
22) nil buffer-invisibility-spec
))
241 (defsubst org-set-local
(var value
)
242 "Make VAR local in current buffer and set it to VALUE."
243 (set (make-local-variable var
) value
))
245 (defsubst org-mode-p
()
246 "Check if the current buffer is in Org-mode."
247 (eq major-mode
'org-mode
))
249 (defsubst org-last
(list)
250 "Return the last element of LIST."
253 (defun org-let (list &rest body
)
254 (eval (cons 'let
(cons list body
))))
255 (put 'org-let
'lisp-indent-function
1)
257 (defun org-let2 (list1 list2
&rest body
)
258 (eval (cons 'let
(cons list1
(list (cons 'let
(cons list2 body
)))))))
259 (put 'org-let2
'lisp-indent-function
2)
261 (defsubst org-call-with-arg
(command arg
)
262 "Call COMMAND interactively, but pretend prefix arg was ARG."
263 (let ((current-prefix-arg arg
)) (call-interactively command
)))
265 (defsubst org-current-line
(&optional pos
)
267 (and pos
(goto-char pos
))
268 ;; works also in narrowed buffer, because we start at 1, not point-min
269 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
271 (defsubst org-goto-line
(N)
274 (goto-char (point-min))
275 (forward-line (1- N
))))
277 (defsubst org-current-line-string
(&optional to-here
)
278 (buffer-substring (point-at-bol) (if to-here
(point) (point-at-eol))))
280 (defsubst org-pos-in-match-range
(pos n
)
281 (and (match-beginning n
)
282 (<= (match-beginning n
) pos
)
283 (>= (match-end n
) pos
)))
285 (defun org-autoload (file functions
)
286 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
287 (let ((d (format "Documentation will be available after `%s.el' is loaded."
290 (while (setq f
(pop functions
))
291 (or (fboundp f
) (autoload f file d t
)))))
293 (defun org-match-line (re)
294 "Looking-at at the beginning of the current line."
296 (goto-char (point-at-bol))
299 (defun org-plist-delete (plist property
)
300 "Delete PROPERTY from PLIST.
301 This is in contrast to merely setting it to 0."
304 (if (not (eq property
(car plist
)))
305 (setq p
(plist-put p
(car plist
) (nth 1 plist
))))
306 (setq plist
(cddr plist
)))
309 (defun org-replace-match-keep-properties (newtext &optional fixedcase
311 "Like `replace-match', but add the text properties found original text."
312 (setq newtext
(org-add-props newtext
(text-properties-at
313 (match-beginning 0) string
)))
314 (replace-match newtext fixedcase literal string
))
316 (defmacro org-save-outline-visibility
(use-markers &rest body
)
317 "Save and restore outline visibility around BODY.
318 If USE-MARKERS is non-nil, use markers for the positions.
319 This means that the buffer may change while running BODY,
320 but it also means that the buffer should stay alive
321 during the operation, because otherwise all these markers will
324 `(let ((data (org-outline-overlay-data ,use-markers
)))
328 (org-set-outline-overlay-data data
))
331 (and (markerp (car c
)) (move-marker (car c
) nil
))
332 (and (markerp (cdr c
)) (move-marker (cdr c
) nil
)))
335 (defmacro org-with-limited-levels
(&rest body
)
336 "Execute BODY with limited number of outline levels."
337 `(let* ((outline-regexp (org-get-limited-outline-regexp)))
340 (defvar org-odd-levels-only
) ; defined in org.el
341 (defvar org-inlinetask-min-level
) ; defined in org-inlinetask.el
342 (defun org-get-limited-outline-regexp ()
343 "Return outline-regexp with limited number of levels.
344 The number of levels is controlled by `org-inlinetask-min-level'"
345 (if (or (not (org-mode-p)) (not (featurep 'org-inlinetask
)))
348 (let* ((limit-level (1- org-inlinetask-min-level
))
349 (nstars (if org-odd-levels-only
(1- (* limit-level
2)) limit-level
)))
350 (format "\\*\\{1,%d\\} " nstars
))))
354 ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
356 ;;; org-macs.el ends here