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
))))
40 (declare-function org-add-props
"org-compat" (string plist
&rest props
))
42 (defmacro org-bound-and-true-p
(var)
43 "Return the value of symbol VAR if it is bound, else nil."
44 `(and (boundp (quote ,var
)) ,var
))
46 (defun org-not-nil (v)
47 "If V not nil, and also not the string \"nil\", then return V.
48 Otherwise return nil."
49 (and v
(not (equal v
"nil")) v
))
51 (defmacro org-unmodified
(&rest body
)
52 "Execute body without changing `buffer-modified-p'.
53 Also, do not record undo information."
54 `(set-buffer-modified-p
55 (prog1 (buffer-modified-p)
56 (let ((buffer-undo-list t
)
57 before-change-functions after-change-functions
)
61 "Replace posix classes in regular expression."
62 (if (featurep 'xemacs
)
65 (while (string-match "\\[:alnum:\\]" ss
)
66 (setq ss
(replace-match "a-zA-Z0-9" t t ss
)))
67 (while (string-match "\\[:word:\\]" ss
)
68 (setq ss
(replace-match "a-zA-Z0-9" t t ss
)))
69 (while (string-match "\\[:alpha:\\]" ss
)
70 (setq ss
(replace-match "a-zA-Z" t t ss
)))
71 (while (string-match "\\[:punct:\\]" ss
)
72 (setq ss
(replace-match "\001-@[-`{-~" t t ss
)))
76 (defmacro org-preserve-lc
(&rest body
)
77 `(let ((_line (org-current-line))
78 (_col (current-column)))
82 (org-move-to-column _col
))))
84 (defmacro org-without-partial-completion
(&rest body
)
85 `(let ((pc-mode (and (boundp 'partial-completion-mode
)
86 partial-completion-mode
)))
89 (if pc-mode
(partial-completion-mode -
1))
91 (if pc-mode
(partial-completion-mode 1)))))
93 (defmacro org-maybe-intangible
(props)
94 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
95 In Emacs 21, invisible text is not avoided by the command loop, so the
96 intangible property is needed to make sure point skips this text.
97 In Emacs 22, this is not necessary. The intangible text property has
98 led to problems with flyspell. These problems are fixed in flyspell.el,
99 but we still avoid setting the property in Emacs 22 and later.
100 We use a macro so that the test can happen at compilation time."
101 (if (< emacs-major-version
22)
102 `(append '(intangible t
) ,props
)
105 (defmacro org-with-point-at
(pom &rest body
)
106 "Move to buffer and point of point-or-marker POM for the duration of BODY."
108 (if (markerp ,pom
) (set-buffer (marker-buffer ,pom
)))
110 (goto-char (or ,pom
(point)))
112 (put 'org-with-point-at
'lisp-indent-function
1)
114 (defmacro org-no-warnings
(&rest body
)
115 (cons (if (fboundp 'with-no-warnings
) 'with-no-warnings
'progn
) body
))
117 (defmacro org-if-unprotected
(&rest body
)
118 "Execute BODY if there is no `org-protected' text property at point."
119 `(unless (get-text-property (point) 'org-protected
)
122 (defmacro org-if-unprotected-1
(&rest body
)
123 "Execute BODY if there is no `org-protected' text property at point-1."
124 `(unless (get-text-property (1- (point)) 'org-protected
)
127 (defmacro org-if-unprotected-at
(pos &rest body
)
128 "Execute BODY if there is no `org-protected' text property at POS."
129 `(unless (get-text-property ,pos
'org-protected
)
131 (put 'org-if-unprotected-at
'lisp-indent-function
1)
133 (defun org-re-search-forward-unprotected (&rest args
)
134 "Like re-search-forward, but stop only in unprotected places."
137 (unless (apply 're-search-forward args
)
139 (unless (get-text-property (match-beginning 0) 'org-protected
)
140 (throw 'exit
(point))))))
142 (defmacro org-with-remote-undo
(_buffer &rest _body
)
143 "Execute BODY while recording undo information in two buffers."
144 `(let ((_cline (org-current-line))
146 (_buf1 (current-buffer))
148 (_undo1 buffer-undo-list
)
149 (_undo2 (with-current-buffer ,_buffer buffer-undo-list
))
152 (when org-agenda-allow-remote-undo
153 (setq _c1
(org-verify-change-for-undo
154 _undo1
(with-current-buffer _buf1 buffer-undo-list
))
155 _c2
(org-verify-change-for-undo
156 _undo2
(with-current-buffer _buf2 buffer-undo-list
)))
158 ;; make sure there are undo boundaries
159 (and _c1
(with-current-buffer _buf1
(undo-boundary)))
160 (and _c2
(with-current-buffer _buf2
(undo-boundary)))
161 ;; remember which buffer to undo
162 (push (list _cmd _cline _buf1 _c1 _buf2 _c2
)
163 org-agenda-undo-list
)))))
165 (defmacro org-no-read-only
(&rest body
)
166 "Inhibit read-only for BODY."
167 `(let ((inhibit-read-only t
)) ,@body
))
169 (defconst org-rm-props
'(invisible t face t keymap t intangible t mouse-face t
170 rear-nonsticky t mouse-map t fontified t
172 "Properties to remove when a string without properties is wanted.")
174 (defsubst org-match-string-no-properties
(num &optional string
)
175 (if (featurep 'xemacs
)
176 (let ((s (match-string num string
)))
177 (and s
(remove-text-properties 0 (length s
) org-rm-props s
))
179 (match-string-no-properties num string
)))
181 (defsubst org-no-properties
(s)
182 (if (fboundp 'set-text-properties
)
183 (set-text-properties 0 (length s
) nil s
)
184 (remove-text-properties 0 (length s
) org-rm-props s
))
187 (defsubst org-get-alist-option
(option key
)
190 ((assoc key option
) (cdr (assoc key option
)))
191 (t (cdr (assq 'default option
)))))
193 (defsubst org-check-external-command
(cmd &optional use no-error
)
194 "Check if external program CMD for USE exists, error if not.
195 When the program does exist, return its path.
196 When it does not exist and NO-ERROR is set, return nil.
197 Otherwise, throw an error. The optional argument USE can describe what this
198 program is needed for, so that the error message can be more informative."
199 (or (executable-find cmd
)
202 (error "Can't find `%s'%s" cmd
203 (if use
(format " (%s)" use
) "")))))
205 (defsubst org-inhibit-invisibility
()
206 "Modified `buffer-invisibility-spec' for Emacs 21.
207 Some ops with invisible text do not work correctly on Emacs 21. For these
208 we turn off invisibility temporarily. Use this in a `let' form."
209 (if (< emacs-major-version
22) nil buffer-invisibility-spec
))
211 (defsubst org-set-local
(var value
)
212 "Make VAR local in current buffer and set it to VALUE."
213 (set (make-local-variable var
) value
))
215 (defsubst org-mode-p
()
216 "Check if the current buffer is in Org-mode."
217 (eq major-mode
'org-mode
))
219 (defsubst org-last
(list)
220 "Return the last element of LIST."
223 (defun org-let (list &rest body
)
224 (eval (cons 'let
(cons list body
))))
225 (put 'org-let
'lisp-indent-function
1)
227 (defun org-let2 (list1 list2
&rest body
)
228 (eval (cons 'let
(cons list1
(list (cons 'let
(cons list2 body
)))))))
229 (put 'org-let2
'lisp-indent-function
2)
231 (defsubst org-call-with-arg
(command arg
)
232 "Call COMMAND interactively, but pretend prefix arg was ARG."
233 (let ((current-prefix-arg arg
)) (call-interactively command
)))
235 (defsubst org-current-line
(&optional pos
)
237 (and pos
(goto-char pos
))
238 ;; works also in narrowed buffer, because we start at 1, not point-min
239 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
241 (defsubst org-goto-line
(N)
244 (goto-char (point-min))
245 (forward-line (1- N
))))
247 (defsubst org-current-line-string
(&optional to-here
)
248 (buffer-substring (point-at-bol) (if to-here
(point) (point-at-eol))))
250 (defsubst org-pos-in-match-range
(pos n
)
251 (and (match-beginning n
)
252 (<= (match-beginning n
) pos
)
253 (>= (match-end n
) pos
)))
255 (defun org-autoload (file functions
)
256 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
257 (let ((d (format "Documentation will be available after `%s.el' is loaded."
260 (while (setq f
(pop functions
))
261 (or (fboundp f
) (autoload f file d t
)))))
263 (defun org-match-line (re)
264 "Looking-at at the beginning of the current line."
266 (goto-char (point-at-bol))
269 (defun org-plist-delete (plist property
)
270 "Delete PROPERTY from PLIST.
271 This is in contrast to merely setting it to 0."
274 (if (not (eq property
(car plist
)))
275 (setq p
(plist-put p
(car plist
) (nth 1 plist
))))
276 (setq plist
(cddr plist
)))
279 (defun org-replace-match-keep-properties (newtext &optional fixedcase
281 "Like `replace-match', but add the text properties found original text."
282 (setq newtext
(org-add-props newtext
(text-properties-at
283 (match-beginning 0) string
)))
284 (replace-match newtext fixedcase literal string
))
286 (defmacro org-with-limited-levels
(&rest body
)
287 "Execute BODY with limited number of outline levels."
288 `(let* ((outline-regexp (org-get-limited-outline-regexp)))
291 (defvar org-odd-levels-only
) ; defined in org.el
292 (defvar org-inlinetask-min-level
) ; defined in org-inlinetask.el
293 (defun org-get-limited-outline-regexp ()
294 "Return outline-regexp with limited number of levels.
295 The number of levels is controlled by `org-inlinetask-min-level'"
296 (if (or (not (org-mode-p)) (not (featurep 'org-inlinetask
)))
299 (let* ((limit-level (1- org-inlinetask-min-level
))
300 (nstars (if org-odd-levels-only
(1- (* limit-level
2)) limit-level
)))
301 (format "\\*\\{1,%d\\} " nstars
))))
304 ;;; Saving and restoring visibility
306 (defun org-outline-overlay-data (&optional use-markers
)
307 "Return a list of the locations of all outline overlays.
308 The are overlays with the `invisible' property value `outline'.
309 The return values is a list of cons cells, with start and stop
310 positions for each overlay.
311 If USE-MARKERS is set, return the positions as markers."
318 (when (eq (overlay-get o
'invisible
) 'outline
)
319 (setq beg
(overlay-start o
)
321 (and beg end
(> end beg
)
323 (cons (move-marker (make-marker) beg
)
324 (move-marker (make-marker) end
))
326 (overlays-in (point-min) (point-max))))))))
328 (autoload 'show-all
"outline" nil t
)
330 (defun org-set-outline-overlay-data (data)
331 "Create visibility overlays for all positions in DATA.
332 DATA should have been made by `org-outline-overlay-data'."
339 (setq o
(make-overlay (car c
) (cdr c
)))
340 (overlay-put o
'invisible
'outline
))
343 (defmacro org-save-outline-visibility
(use-markers &rest body
)
344 "Save and restore outline visibility around BODY.
345 If USE-MARKERS is non-nil, use markers for the positions.
346 This means that the buffer may change while running BODY,
347 but it also means that the buffer should stay alive
348 during the operation, because otherwise all these markers will
351 `(let ((data (org-outline-overlay-data ,use-markers
)))
355 (org-set-outline-overlay-data data
))
358 (and (markerp (car c
)) (move-marker (car c
) nil
))
359 (and (markerp (cdr c
)) (move-marker (cdr c
) nil
)))
365 ;; arch-tag: 7e6a73ce-aac9-4fc0-9b30-ce6f89dc6668
367 ;;; org-macs.el ends here