* lisp/mail/rmail.el (rmail-show-message-1): Use restore-buffer-modified-p.
[emacs.git] / lisp / org / org-macs.el
blob5bb868887393557189a8fd12767d92577083832d
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.4
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))))
39 (declare-function org-add-props "org-compat" (string plist &rest props))
40 (declare-function org-string-match-p "org-compat" (&rest args))
42 (defmacro org-called-interactively-p (&optional kind)
43 `(if (featurep 'xemacs)
44 (interactive-p)
45 (if (or (> emacs-major-version 23)
46 (and (>= emacs-major-version 23)
47 (>= emacs-minor-version 2)))
48 (with-no-warnings (called-interactively-p ,kind)) ;; defined with no argument in <=23.1
49 (interactive-p))))
51 (if (and (not (fboundp 'with-silent-modifications))
52 (or (< emacs-major-version 23)
53 (and (= emacs-major-version 23)
54 (< emacs-minor-version 2))))
55 (defmacro with-silent-modifications (&rest body)
56 `(org-unmodified ,@body)))
58 (defmacro org-bound-and-true-p (var)
59 "Return the value of symbol VAR if it is bound, else nil."
60 `(and (boundp (quote ,var)) ,var))
62 (defun org-string-nw-p (s)
63 "Is S a string with a non-white character?"
64 (and (stringp s)
65 (org-string-match-p "\\S-" s)
66 s))
68 (defun org-not-nil (v)
69 "If V not nil, and also not the string \"nil\", then return V.
70 Otherwise return nil."
71 (and v (not (equal v "nil")) v))
73 (defmacro org-unmodified (&rest body)
74 "Execute body without changing `buffer-modified-p'.
75 Also, do not record undo information."
76 `(set-buffer-modified-p
77 (prog1 (buffer-modified-p)
78 (let ((buffer-undo-list t)
79 before-change-functions after-change-functions)
80 ,@body))))
82 (defmacro org-re (s)
83 "Replace posix classes in regular expression."
84 (if (featurep 'xemacs)
85 (let ((ss s))
86 (save-match-data
87 (while (string-match "\\[:alnum:\\]" ss)
88 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
89 (while (string-match "\\[:word:\\]" ss)
90 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
91 (while (string-match "\\[:alpha:\\]" ss)
92 (setq ss (replace-match "a-zA-Z" t t ss)))
93 (while (string-match "\\[:punct:\\]" ss)
94 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
95 ss))
96 s))
98 (defmacro org-preserve-lc (&rest body)
99 `(let ((_line (org-current-line))
100 (_col (current-column)))
101 (unwind-protect
102 (progn ,@body)
103 (org-goto-line _line)
104 (org-move-to-column _col))))
106 (defmacro org-without-partial-completion (&rest body)
107 `(let ((pc-mode (and (boundp 'partial-completion-mode)
108 partial-completion-mode)))
109 (unwind-protect
110 (progn
111 (if pc-mode (partial-completion-mode -1))
112 ,@body)
113 (if pc-mode (partial-completion-mode 1)))))
115 (defmacro org-maybe-intangible (props)
116 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
117 In Emacs 21, invisible text is not avoided by the command loop, so the
118 intangible property is needed to make sure point skips this text.
119 In Emacs 22, this is not necessary. The intangible text property has
120 led to problems with flyspell. These problems are fixed in flyspell.el,
121 but we still avoid setting the property in Emacs 22 and later.
122 We use a macro so that the test can happen at compilation time."
123 (if (< emacs-major-version 22)
124 `(append '(intangible t) ,props)
125 props))
127 (defmacro org-with-point-at (pom &rest body)
128 "Move to buffer and point of point-or-marker POM for the duration of BODY."
129 `(save-excursion
130 (if (markerp ,pom) (set-buffer (marker-buffer ,pom)))
131 (save-excursion
132 (goto-char (or ,pom (point)))
133 ,@body)))
134 (put 'org-with-point-at 'lisp-indent-function 1)
136 (defmacro org-no-warnings (&rest body)
137 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
139 (defmacro org-if-unprotected (&rest body)
140 "Execute BODY if there is no `org-protected' text property at point."
141 `(unless (get-text-property (point) 'org-protected)
142 ,@body))
144 (defmacro org-if-unprotected-1 (&rest body)
145 "Execute BODY if there is no `org-protected' text property at point-1."
146 `(unless (get-text-property (1- (point)) 'org-protected)
147 ,@body))
149 (defmacro org-if-unprotected-at (pos &rest body)
150 "Execute BODY if there is no `org-protected' text property at POS."
151 `(unless (get-text-property ,pos 'org-protected)
152 ,@body))
153 (put 'org-if-unprotected-at 'lisp-indent-function 1)
155 (defun org-re-search-forward-unprotected (&rest args)
156 "Like re-search-forward, but stop only in unprotected places."
157 (catch 'exit
158 (while t
159 (unless (apply 're-search-forward args)
160 (throw 'exit nil))
161 (unless (get-text-property (match-beginning 0) 'org-protected)
162 (throw 'exit (point))))))
164 (defmacro org-with-remote-undo (_buffer &rest _body)
165 "Execute BODY while recording undo information in two buffers."
166 `(let ((_cline (org-current-line))
167 (_cmd this-command)
168 (_buf1 (current-buffer))
169 (_buf2 ,_buffer)
170 (_undo1 buffer-undo-list)
171 (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
172 _c1 _c2)
173 ,@_body
174 (when org-agenda-allow-remote-undo
175 (setq _c1 (org-verify-change-for-undo
176 _undo1 (with-current-buffer _buf1 buffer-undo-list))
177 _c2 (org-verify-change-for-undo
178 _undo2 (with-current-buffer _buf2 buffer-undo-list)))
179 (when (or _c1 _c2)
180 ;; make sure there are undo boundaries
181 (and _c1 (with-current-buffer _buf1 (undo-boundary)))
182 (and _c2 (with-current-buffer _buf2 (undo-boundary)))
183 ;; remember which buffer to undo
184 (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
185 org-agenda-undo-list)))))
187 (defmacro org-no-read-only (&rest body)
188 "Inhibit read-only for BODY."
189 `(let ((inhibit-read-only t)) ,@body))
191 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
192 rear-nonsticky t mouse-map t fontified t
193 org-emphasis t)
194 "Properties to remove when a string without properties is wanted.")
196 (defsubst org-match-string-no-properties (num &optional string)
197 (if (featurep 'xemacs)
198 (let ((s (match-string num string)))
199 (and s (remove-text-properties 0 (length s) org-rm-props s))
201 (match-string-no-properties num string)))
203 (defsubst org-no-properties (s)
204 (if (fboundp 'set-text-properties)
205 (set-text-properties 0 (length s) nil s)
206 (remove-text-properties 0 (length s) org-rm-props s))
209 (defsubst org-get-alist-option (option key)
210 (cond ((eq key t) t)
211 ((eq option t) t)
212 ((assoc key option) (cdr (assoc key option)))
213 (t (cdr (assq 'default option)))))
215 (defsubst org-check-external-command (cmd &optional use no-error)
216 "Check if external program CMD for USE exists, error if not.
217 When the program does exist, return its path.
218 When it does not exist and NO-ERROR is set, return nil.
219 Otherwise, throw an error. The optional argument USE can describe what this
220 program is needed for, so that the error message can be more informative."
221 (or (executable-find cmd)
222 (if no-error
224 (error "Can't find `%s'%s" cmd
225 (if use (format " (%s)" use) "")))))
227 (defsubst org-inhibit-invisibility ()
228 "Modified `buffer-invisibility-spec' for Emacs 21.
229 Some ops with invisible text do not work correctly on Emacs 21. For these
230 we turn off invisibility temporarily. Use this in a `let' form."
231 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
233 (defsubst org-set-local (var value)
234 "Make VAR local in current buffer and set it to VALUE."
235 (set (make-local-variable var) value))
237 (defsubst org-mode-p ()
238 "Check if the current buffer is in Org-mode."
239 (eq major-mode 'org-mode))
241 (defsubst org-last (list)
242 "Return the last element of LIST."
243 (car (last list)))
245 (defun org-let (list &rest body)
246 (eval (cons 'let (cons list body))))
247 (put 'org-let 'lisp-indent-function 1)
249 (defun org-let2 (list1 list2 &rest body)
250 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
251 (put 'org-let2 'lisp-indent-function 2)
253 (defsubst org-call-with-arg (command arg)
254 "Call COMMAND interactively, but pretend prefix arg was ARG."
255 (let ((current-prefix-arg arg)) (call-interactively command)))
257 (defsubst org-current-line (&optional pos)
258 (save-excursion
259 (and pos (goto-char pos))
260 ;; works also in narrowed buffer, because we start at 1, not point-min
261 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
263 (defsubst org-goto-line (N)
264 (save-restriction
265 (widen)
266 (goto-char (point-min))
267 (forward-line (1- N))))
269 (defsubst org-current-line-string (&optional to-here)
270 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
272 (defsubst org-pos-in-match-range (pos n)
273 (and (match-beginning n)
274 (<= (match-beginning n) pos)
275 (>= (match-end n) pos)))
277 (defun org-autoload (file functions)
278 "Establish autoload for all FUNCTIONS in FILE, if not bound already."
279 (let ((d (format "Documentation will be available after `%s.el' is loaded."
280 file))
282 (while (setq f (pop functions))
283 (or (fboundp f) (autoload f file d t)))))
285 (defun org-match-line (re)
286 "Looking-at at the beginning of the current line."
287 (save-excursion
288 (goto-char (point-at-bol))
289 (looking-at re)))
291 (defun org-plist-delete (plist property)
292 "Delete PROPERTY from PLIST.
293 This is in contrast to merely setting it to 0."
294 (let (p)
295 (while plist
296 (if (not (eq property (car plist)))
297 (setq p (plist-put p (car plist) (nth 1 plist))))
298 (setq plist (cddr plist)))
301 (defun org-replace-match-keep-properties (newtext &optional fixedcase
302 literal string)
303 "Like `replace-match', but add the text properties found original text."
304 (setq newtext (org-add-props newtext (text-properties-at
305 (match-beginning 0) string)))
306 (replace-match newtext fixedcase literal string))
308 (defmacro org-save-outline-visibility (use-markers &rest body)
309 "Save and restore outline visibility around BODY.
310 If USE-MARKERS is non-nil, use markers for the positions.
311 This means that the buffer may change while running BODY,
312 but it also means that the buffer should stay alive
313 during the operation, because otherwise all these markers will
314 point nowhere."
315 (declare (indent 1))
316 `(let ((data (org-outline-overlay-data ,use-markers)))
317 (unwind-protect
318 (progn
319 ,@body
320 (org-set-outline-overlay-data data))
321 (when ,use-markers
322 (mapc (lambda (c)
323 (and (markerp (car c)) (move-marker (car c) nil))
324 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
325 data)))))
327 (defmacro org-with-limited-levels (&rest body)
328 "Execute BODY with limited number of outline levels."
329 `(let* ((outline-regexp (org-get-limited-outline-regexp)))
330 ,@body))
332 (defvar org-odd-levels-only) ; defined in org.el
333 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
334 (defun org-get-limited-outline-regexp ()
335 "Return outline-regexp with limited number of levels.
336 The number of levels is controlled by `org-inlinetask-min-level'"
337 (if (or (not (org-mode-p)) (not (featurep 'org-inlinetask)))
339 outline-regexp
340 (let* ((limit-level (1- org-inlinetask-min-level))
341 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
342 (format "\\*\\{1,%d\\} " nstars))))
344 (provide 'org-macs)
347 ;;; org-macs.el ends here