Auto-commit of generated files.
[emacs.git] / lisp / org / org-macs.el
blobdc413f4d9939b993d40e6b2f524121e39cd4a870
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
8 ;; Version: 7.7
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file contains macro definitions, defsubst definitions, other
29 ;; stuff needed for compilation and top-level forms in Org-mode, as well
30 ;; lots of small functions that are not org-mode specific but simply
31 ;; generally useful stuff.
33 ;;; Code:
35 (eval-and-compile
36 (unless (fboundp 'declare-function)
37 (defmacro declare-function (fn file &optional arglist fileonly)))
38 (if (>= emacs-major-version 23)
39 (defsubst org-char-to-string(c)
40 "Defsubst to decode UTF-8 character values in emacs 23 and beyond."
41 (char-to-string c))
42 (defsubst org-char-to-string (c)
43 "Defsubst to decode UTF-8 character values in emacs 22."
44 (string (decode-char 'ucs c)))))
46 (declare-function org-add-props "org-compat" (string plist &rest props))
47 (declare-function org-string-match-p "org-compat" (&rest args))
49 (defmacro org-called-interactively-p (&optional kind)
50 (if (featurep 'xemacs)
51 `(interactive-p)
52 (if (or (> emacs-major-version 23)
53 (and (>= emacs-major-version 23)
54 (>= emacs-minor-version 2)))
55 `(with-no-warnings (called-interactively-p ,kind)) ;; defined with no argument in <=23.1
56 `(interactive-p))))
58 (if (and (not (fboundp 'with-silent-modifications))
59 (or (< emacs-major-version 23)
60 (and (= emacs-major-version 23)
61 (< emacs-minor-version 2))))
62 (defmacro with-silent-modifications (&rest body)
63 `(org-unmodified ,@body)))
65 (defmacro org-bound-and-true-p (var)
66 "Return the value of symbol VAR if it is bound, else nil."
67 `(and (boundp (quote ,var)) ,var))
69 (defun org-string-nw-p (s)
70 "Is S a string with a non-white character?"
71 (and (stringp s)
72 (org-string-match-p "\\S-" s)
73 s))
75 (defun org-not-nil (v)
76 "If V not nil, and also not the string \"nil\", then return V.
77 Otherwise return nil."
78 (and v (not (equal v "nil")) v))
80 (defmacro org-unmodified (&rest body)
81 "Execute body without changing `buffer-modified-p'.
82 Also, do not record undo information."
83 `(set-buffer-modified-p
84 (prog1 (buffer-modified-p)
85 (let ((buffer-undo-list t)
86 before-change-functions after-change-functions)
87 ,@body))))
89 (defmacro org-re (s)
90 "Replace posix classes in regular expression."
91 (if (featurep 'xemacs)
92 (let ((ss s))
93 (save-match-data
94 (while (string-match "\\[:alnum:\\]" ss)
95 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
96 (while (string-match "\\[:word:\\]" ss)
97 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
98 (while (string-match "\\[:alpha:\\]" ss)
99 (setq ss (replace-match "a-zA-Z" t t ss)))
100 (while (string-match "\\[:punct:\\]" ss)
101 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
102 ss))
105 (defmacro org-preserve-lc (&rest body)
106 `(let ((_line (org-current-line))
107 (_col (current-column)))
108 (unwind-protect
109 (progn ,@body)
110 (org-goto-line _line)
111 (org-move-to-column _col))))
113 (defmacro org-without-partial-completion (&rest body)
114 `(if (and (boundp 'partial-completion-mode)
115 partial-completion-mode
116 (fboundp 'partial-completion-mode))
117 (unwind-protect
118 (progn
119 (partial-completion-mode -1)
120 ,@body)
121 (partial-completion-mode 1))
122 ,@body))
124 (defmacro org-maybe-intangible (props)
125 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
126 In Emacs 21, invisible text is not avoided by the command loop, so the
127 intangible property is needed to make sure point skips this text.
128 In Emacs 22, this is not necessary. The intangible text property has
129 led to problems with flyspell. These problems are fixed in flyspell.el,
130 but we still avoid setting the property in Emacs 22 and later.
131 We use a macro so that the test can happen at compilation time."
132 (if (< emacs-major-version 22)
133 `(append '(intangible t) ,props)
134 props))
136 (defmacro org-with-point-at (pom &rest body)
137 "Move to buffer and point of point-or-marker POM for the duration of BODY."
138 `(let ((pom ,pom))
139 (save-excursion
140 (if (markerp pom) (set-buffer (marker-buffer pom)))
141 (save-excursion
142 (goto-char (or pom (point)))
143 ,@body))))
144 (put 'org-with-point-at 'lisp-indent-function 1)
146 (defmacro org-no-warnings (&rest body)
147 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
149 (defmacro org-if-unprotected (&rest body)
150 "Execute BODY if there is no `org-protected' text property at point."
151 `(unless (get-text-property (point) 'org-protected)
152 ,@body))
154 (defmacro org-if-unprotected-1 (&rest body)
155 "Execute BODY if there is no `org-protected' text property at point-1."
156 `(unless (get-text-property (1- (point)) 'org-protected)
157 ,@body))
159 (defmacro org-if-unprotected-at (pos &rest body)
160 "Execute BODY if there is no `org-protected' text property at POS."
161 `(unless (get-text-property ,pos 'org-protected)
162 ,@body))
163 (put 'org-if-unprotected-at 'lisp-indent-function 1)
165 (defun org-re-search-forward-unprotected (&rest args)
166 "Like re-search-forward, but stop only in unprotected places."
167 (catch 'exit
168 (while t
169 (unless (apply 're-search-forward args)
170 (throw 'exit nil))
171 (unless (get-text-property (match-beginning 0) 'org-protected)
172 (throw 'exit (point))))))
174 (defmacro org-with-remote-undo (_buffer &rest _body)
175 "Execute BODY while recording undo information in two buffers."
176 `(let ((_cline (org-current-line))
177 (_cmd this-command)
178 (_buf1 (current-buffer))
179 (_buf2 ,_buffer)
180 (_undo1 buffer-undo-list)
181 (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
182 _c1 _c2)
183 ,@_body
184 (when org-agenda-allow-remote-undo
185 (setq _c1 (org-verify-change-for-undo
186 _undo1 (with-current-buffer _buf1 buffer-undo-list))
187 _c2 (org-verify-change-for-undo
188 _undo2 (with-current-buffer _buf2 buffer-undo-list)))
189 (when (or _c1 _c2)
190 ;; make sure there are undo boundaries
191 (and _c1 (with-current-buffer _buf1 (undo-boundary)))
192 (and _c2 (with-current-buffer _buf2 (undo-boundary)))
193 ;; remember which buffer to undo
194 (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
195 org-agenda-undo-list)))))
196 (put 'org-with-remote-undo 'lisp-indent-function 1)
198 (defmacro org-no-read-only (&rest body)
199 "Inhibit read-only for BODY."
200 `(let ((inhibit-read-only t)) ,@body))
202 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
203 rear-nonsticky t mouse-map t fontified t
204 org-emphasis t)
205 "Properties to remove when a string without properties is wanted.")
207 (defsubst org-match-string-no-properties (num &optional string)
208 (if (featurep 'xemacs)
209 (let ((s (match-string num string)))
210 (and s (remove-text-properties 0 (length s) org-rm-props s))
212 (match-string-no-properties num string)))
214 (defsubst org-no-properties (s)
215 (if (fboundp 'set-text-properties)
216 (set-text-properties 0 (length s) nil s)
217 (remove-text-properties 0 (length s) org-rm-props s))
220 (defsubst org-get-alist-option (option key)
221 (cond ((eq key t) t)
222 ((eq option t) t)
223 ((assoc key option) (cdr (assoc key option)))
224 (t (cdr (assq 'default option)))))
226 (defsubst org-check-external-command (cmd &optional use no-error)
227 "Check if external program CMD for USE exists, error if not.
228 When the program does exist, return its path.
229 When it does not exist and NO-ERROR is set, return nil.
230 Otherwise, throw an error. The optional argument USE can describe what this
231 program is needed for, so that the error message can be more informative."
232 (or (executable-find cmd)
233 (if no-error
235 (error "Can't find `%s'%s" cmd
236 (if use (format " (%s)" use) "")))))
238 (defsubst org-inhibit-invisibility ()
239 "Modified `buffer-invisibility-spec' for Emacs 21.
240 Some ops with invisible text do not work correctly on Emacs 21. For these
241 we turn off invisibility temporarily. Use this in a `let' form."
242 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
244 (defsubst org-set-local (var value)
245 "Make VAR local in current buffer and set it to VALUE."
246 (set (make-local-variable var) value))
248 (defsubst org-mode-p ()
249 "Check if the current buffer is in Org-mode."
250 (eq major-mode 'org-mode))
252 (defsubst org-last (list)
253 "Return the last element of LIST."
254 (car (last list)))
256 (defun org-let (list &rest body)
257 (eval (cons 'let (cons list body))))
258 (put 'org-let 'lisp-indent-function 1)
260 (defun org-let2 (list1 list2 &rest body)
261 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
262 (put 'org-let2 'lisp-indent-function 2)
264 (defsubst org-call-with-arg (command arg)
265 "Call COMMAND interactively, but pretend prefix arg was ARG."
266 (let ((current-prefix-arg arg)) (call-interactively command)))
268 (defsubst org-current-line (&optional pos)
269 (save-excursion
270 (and pos (goto-char pos))
271 ;; works also in narrowed buffer, because we start at 1, not point-min
272 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
274 (defsubst org-goto-line (N)
275 (save-restriction
276 (widen)
277 (goto-char (point-min))
278 (forward-line (1- N))))
280 (defsubst org-current-line-string (&optional to-here)
281 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
283 (defsubst org-pos-in-match-range (pos n)
284 (and (match-beginning n)
285 (<= (match-beginning n) pos)
286 (>= (match-end n) pos)))
288 (defun org-autoload (file functions)
289 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
290 (let ((d (format "Documentation will be available after `%s.el' is loaded."
291 file))
293 (while (setq f (pop functions))
294 (or (fboundp f) (autoload f file d t)))))
296 (defun org-match-line (re)
297 "Looking-at at the beginning of the current line."
298 (save-excursion
299 (goto-char (point-at-bol))
300 (looking-at re)))
302 (defun org-plist-delete (plist property)
303 "Delete PROPERTY from PLIST.
304 This is in contrast to merely setting it to 0."
305 (let (p)
306 (while plist
307 (if (not (eq property (car plist)))
308 (setq p (plist-put p (car plist) (nth 1 plist))))
309 (setq plist (cddr plist)))
312 (defun org-replace-match-keep-properties (newtext &optional fixedcase
313 literal string)
314 "Like `replace-match', but add the text properties found original text."
315 (setq newtext (org-add-props newtext (text-properties-at
316 (match-beginning 0) string)))
317 (replace-match newtext fixedcase literal string))
319 (defmacro org-save-outline-visibility (use-markers &rest body)
320 "Save and restore outline visibility around BODY.
321 If USE-MARKERS is non-nil, use markers for the positions.
322 This means that the buffer may change while running BODY,
323 but it also means that the buffer should stay alive
324 during the operation, because otherwise all these markers will
325 point nowhere."
326 (declare (indent 1))
327 `(let ((data (org-outline-overlay-data ,use-markers))
328 rtn)
329 (unwind-protect
330 (progn
331 (setq rtn (progn ,@body))
332 (org-set-outline-overlay-data data))
333 (when ,use-markers
334 (mapc (lambda (c)
335 (and (markerp (car c)) (move-marker (car c) nil))
336 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
337 data)))
338 rtn))
340 (defmacro org-with-wide-buffer (&rest body)
341 "Execute body while temporarily widening the buffer."
342 `(save-excursion
343 (save-restriction
344 (widen)
345 ,@body)))
347 (defmacro org-with-limited-levels (&rest body)
348 "Execute BODY with limited number of outline levels."
349 `(let* ((org-outline-regexp (org-get-limited-outline-regexp))
350 (outline-regexp org-outline-regexp)
351 (org-outline-regexp-at-bol (concat "^" org-outline-regexp)))
352 ,@body))
354 (defvar org-outline-regexp) ; defined in org.el
355 (defvar org-odd-levels-only) ; defined in org.el
356 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
357 (defun org-get-limited-outline-regexp ()
358 "Return outline-regexp with limited number of levels.
359 The number of levels is controlled by `org-inlinetask-min-level'"
360 (if (or (not (org-mode-p)) (not (featurep 'org-inlinetask)))
361 org-outline-regexp
362 (let* ((limit-level (1- org-inlinetask-min-level))
363 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
364 (format "\\*\\{1,%d\\} " nstars))))
366 (defun org-format-seconds (string seconds)
367 "Compatibility function replacing format-seconds"
368 (if (fboundp 'format-seconds)
369 (format-seconds string seconds)
370 (format-time-string string (seconds-to-time seconds))))
372 (provide 'org-macs)
376 ;;; org-macs.el ends here