]> code.delx.au - gnu-emacs/blob - lisp/org/org-macs.el
Merge branch 'master' of git.sv.gnu.org:/srv/git/emacs
[gnu-emacs] / lisp / org / org-macs.el
1 ;;; org-macs.el --- Top-level definitions for Org-mode
2
3 ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
4
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
10 ;;
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.
15
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.
20
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24 ;;
25 ;;; Commentary:
26
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.
31
32 ;;; Code:
33
34 (eval-and-compile
35 (unless (fboundp 'declare-function)
36 (defmacro declare-function (fn file &optional _arglist _fileonly)
37 `(autoload ',fn ,file)))
38
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."
42 (char-to-string c))
43 (defsubst org-char-to-string (c)
44 "Defsubst to decode UTF-8 character values in emacs 22."
45 (string (decode-char 'ucs c)))))
46
47 (declare-function org-add-props "org-compat" (string plist &rest props))
48 (declare-function org-string-match-p "org-compat" (&rest args))
49
50 (defmacro org-with-gensyms (symbols &rest body)
51 (declare (debug (sexp body)) (indent 1))
52 `(let ,(mapcar (lambda (s)
53 `(,s (make-symbol (concat "--" (symbol-name ',s)))))
54 symbols)
55 ,@body))
56
57 (defmacro org-called-interactively-p (&optional kind)
58 (declare (debug (&optional ("quote" symbolp)))) ;Why not just t?
59 (if (featurep 'xemacs)
60 `(interactive-p)
61 (if (or (> emacs-major-version 23)
62 (and (>= emacs-major-version 23)
63 (>= emacs-minor-version 2)))
64 ;; defined with no argument in <=23.1
65 `(with-no-warnings (called-interactively-p ,kind))
66 `(interactive-p))))
67
68 (defmacro org-bound-and-true-p (var)
69 "Return the value of symbol VAR if it is bound, else nil."
70 (declare (debug (symbolp)))
71 `(and (boundp (quote ,var)) ,var))
72
73 (defun org-string-nw-p (s)
74 "Is S a string with a non-white character?"
75 (and (stringp s)
76 (org-string-match-p "\\S-" s)
77 s))
78
79 (defun org-not-nil (v)
80 "If V not nil, and also not the string \"nil\", then return V.
81 Otherwise return nil."
82 (and v (not (equal v "nil")) v))
83
84 (defun org-substitute-posix-classes (re)
85 "Substitute posix classes in regular expression RE."
86 (let ((ss re))
87 (save-match-data
88 (while (string-match "\\[:alnum:\\]" ss)
89 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
90 (while (string-match "\\[:word:\\]" ss)
91 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
92 (while (string-match "\\[:alpha:\\]" ss)
93 (setq ss (replace-match "a-zA-Z" t t ss)))
94 (while (string-match "\\[:punct:\\]" ss)
95 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
96 ss)))
97
98 (defmacro org-re (s)
99 "Replace posix classes in regular expression."
100 (declare (debug (form)))
101 (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
102
103 (defmacro org-preserve-lc (&rest body)
104 (declare (debug (body)))
105 (org-with-gensyms (line col)
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)))))
112
113 ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
114 ;; `org-unmodified' to ignore real text modifications
115 (defmacro org-unmodified (&rest body)
116 "Run BODY while preserving the buffer's `buffer-modified-p' state."
117 (declare (debug (body)))
118 (org-with-gensyms (was-modified)
119 `(let ((,was-modified (buffer-modified-p)))
120 (unwind-protect
121 (let ((buffer-undo-list t)
122 (inhibit-modification-hooks t))
123 ,@body)
124 (set-buffer-modified-p ,was-modified)))))
125
126 (defmacro org-without-partial-completion (&rest body)
127 (declare (debug (body)))
128 `(if (and (boundp 'partial-completion-mode)
129 partial-completion-mode
130 (fboundp 'partial-completion-mode))
131 (unwind-protect
132 (progn
133 (partial-completion-mode -1)
134 ,@body)
135 (partial-completion-mode 1))
136 ,@body))
137
138 ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
139 (defmacro org-maybe-intangible (props)
140 "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
141 In Emacs 21, invisible text is not avoided by the command loop, so the
142 intangible property is needed to make sure point skips this text.
143 In Emacs 22, this is not necessary. The intangible text property has
144 led to problems with flyspell. These problems are fixed in flyspell.el,
145 but we still avoid setting the property in Emacs 22 and later.
146 We use a macro so that the test can happen at compilation time."
147 (if (< emacs-major-version 22)
148 `(append '(intangible t) ,props)
149 props))
150
151 (defmacro org-with-point-at (pom &rest body)
152 "Move to buffer and point of point-or-marker POM for the duration of BODY."
153 (declare (debug (form body)) (indent 1))
154 (org-with-gensyms (mpom)
155 `(let ((,mpom ,pom))
156 (save-excursion
157 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
158 (org-with-wide-buffer
159 (goto-char (or ,mpom (point)))
160 ,@body)))))
161
162 (defmacro org-no-warnings (&rest body)
163 (declare (debug (body)))
164 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
165
166 (defmacro org-with-remote-undo (buffer &rest body)
167 "Execute BODY while recording undo information in two buffers."
168 (declare (debug (form body)) (indent 1))
169 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
170 `(let ((,cline (org-current-line))
171 (,cmd this-command)
172 (,buf1 (current-buffer))
173 (,buf2 ,buffer)
174 (,undo1 buffer-undo-list)
175 (,undo2 (with-current-buffer ,buffer buffer-undo-list))
176 ,c1 ,c2)
177 ,@body
178 (when org-agenda-allow-remote-undo
179 (setq ,c1 (org-verify-change-for-undo
180 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
181 ,c2 (org-verify-change-for-undo
182 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
183 (when (or ,c1 ,c2)
184 ;; make sure there are undo boundaries
185 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
186 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
187 ;; remember which buffer to undo
188 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
189 org-agenda-undo-list))))))
190
191 (defmacro org-no-read-only (&rest body)
192 "Inhibit read-only for BODY."
193 (declare (debug (body)))
194 `(let ((inhibit-read-only t)) ,@body))
195
196 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
197 rear-nonsticky t mouse-map t fontified t
198 org-emphasis t)
199 "Properties to remove when a string without properties is wanted.")
200
201 (defsubst org-match-string-no-properties (num &optional string)
202 (if (featurep 'xemacs)
203 (let ((s (match-string num string)))
204 (and s (remove-text-properties 0 (length s) org-rm-props s))
205 s)
206 (match-string-no-properties num string)))
207
208 (defsubst org-no-properties (s &optional restricted)
209 "Remove all text properties from string S.
210 When RESTRICTED is non-nil, only remove the properties listed
211 in `org-rm-props'."
212 (if (fboundp 'set-text-properties)
213 (set-text-properties 0 (length s) nil s)
214 (if restricted
215 (remove-text-properties 0 (length s) org-rm-props s)
216 (set-text-properties 0 (length s) nil s)))
217 s)
218
219 (defsubst org-get-alist-option (option key)
220 (cond ((eq key t) t)
221 ((eq option t) t)
222 ((assoc key option) (cdr (assoc key option)))
223 (t (let ((r (cdr (assq 'default option))))
224 (if (listp r) (delq nil r) r)))))
225
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
234 nil
235 (error "Can't find `%s'%s" cmd
236 (if use (format " (%s)" use) "")))))
237
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))
243
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))
247
248 (defsubst org-last (list)
249 "Return the last element of LIST."
250 (car (last list)))
251
252 (defun org-let (list &rest body)
253 (eval (cons 'let (cons list body))))
254 (put 'org-let 'lisp-indent-function 1)
255
256 (defun org-let2 (list1 list2 &rest body)
257 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
258 (put 'org-let2 'lisp-indent-function 2)
259
260 (defsubst org-call-with-arg (command arg)
261 "Call COMMAND interactively, but pretend prefix arg was ARG."
262 (let ((current-prefix-arg arg)) (call-interactively command)))
263
264 (defsubst org-current-line (&optional pos)
265 (save-excursion
266 (and pos (goto-char pos))
267 ;; works also in narrowed buffer, because we start at 1, not point-min
268 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
269
270 (defsubst org-goto-line (N)
271 (save-restriction
272 (widen)
273 (goto-char (point-min))
274 (forward-line (1- N))))
275
276 (defsubst org-current-line-string (&optional to-here)
277 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
278
279 (defsubst org-pos-in-match-range (pos n)
280 (and (match-beginning n)
281 (<= (match-beginning n) pos)
282 (>= (match-end n) pos)))
283
284 (defun org-match-line (re)
285 "Looking-at at the beginning of the current line."
286 (save-excursion
287 (goto-char (point-at-bol))
288 (looking-at re)))
289
290 (defun org-plist-delete (plist property)
291 "Delete PROPERTY from PLIST.
292 This is in contrast to merely setting it to 0."
293 (let (p)
294 (while plist
295 (if (not (eq property (car plist)))
296 (setq p (plist-put p (car plist) (nth 1 plist))))
297 (setq plist (cddr plist)))
298 p))
299
300 (defun org-replace-match-keep-properties (newtext &optional fixedcase
301 literal string)
302 "Like `replace-match', but add the text properties found original text."
303 (setq newtext (org-add-props newtext (text-properties-at
304 (match-beginning 0) string)))
305 (replace-match newtext fixedcase literal string))
306
307 (defmacro org-save-outline-visibility (use-markers &rest body)
308 "Save and restore outline visibility around BODY.
309 If USE-MARKERS is non-nil, use markers for the positions.
310 This means that the buffer may change while running BODY,
311 but it also means that the buffer should stay alive
312 during the operation, because otherwise all these markers will
313 point nowhere."
314 (declare (debug (form body)) (indent 1))
315 (org-with-gensyms (data rtn)
316 `(let ((,data (org-outline-overlay-data ,use-markers))
317 ,rtn)
318 (unwind-protect
319 (progn
320 (setq ,rtn (progn ,@body))
321 (org-set-outline-overlay-data ,data))
322 (when ,use-markers
323 (mapc (lambda (c)
324 (and (markerp (car c)) (move-marker (car c) nil))
325 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
326 ,data)))
327 ,rtn)))
328
329 (defmacro org-with-wide-buffer (&rest body)
330 "Execute body while temporarily widening the buffer."
331 (declare (debug (body)))
332 `(save-excursion
333 (save-restriction
334 (widen)
335 ,@body)))
336
337 (defmacro org-with-limited-levels (&rest body)
338 "Execute BODY with limited number of outline levels."
339 (declare (debug (body)))
340 `(progn
341 (defvar org-called-with-limited-levels)
342 (defvar org-outline-regexp)
343 (defvar outline-regexp)
344 (defvar org-outline-regexp-bol)
345 (let* ((org-called-with-limited-levels t)
346 (org-outline-regexp (org-get-limited-outline-regexp))
347 (outline-regexp org-outline-regexp)
348 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
349 ,@body)))
350
351 (defvar org-outline-regexp) ; defined in org.el
352 (defvar org-odd-levels-only) ; defined in org.el
353 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
354 (defun org-get-limited-outline-regexp ()
355 "Return outline-regexp with limited number of levels.
356 The number of levels is controlled by `org-inlinetask-min-level'"
357 (if (or (not (derived-mode-p 'org-mode)) (not (featurep 'org-inlinetask)))
358 org-outline-regexp
359 (let* ((limit-level (1- org-inlinetask-min-level))
360 (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
361 (format "\\*\\{1,%d\\} " nstars))))
362
363 (defun org-format-seconds (string seconds)
364 "Compatibility function replacing format-seconds."
365 (if (fboundp 'format-seconds)
366 (format-seconds string seconds)
367 (format-time-string string (seconds-to-time seconds))))
368
369 (defmacro org-eval-in-environment (environment form)
370 (declare (debug (form form)) (indent 1))
371 `(eval (list 'let ,environment ',form)))
372
373 (defun org-make-parameter-alist (flat)
374 "Return alist based on FLAT.
375 FLAT is a list with alternating symbol names and values. The
376 returned alist is a list of lists with the symbol name in car and
377 the value in cdr."
378 (when flat
379 (cons (list (car flat) (cadr flat))
380 (org-make-parameter-alist (cddr flat)))))
381
382 ;;;###autoload
383 (defmacro org-load-noerror-mustsuffix (file)
384 "Load FILE with optional arguments NOERROR and MUSTSUFFIX. Drop the MUSTSUFFIX argument for XEmacs, which doesn't recognize it."
385 (if (featurep 'xemacs)
386 `(load ,file 'noerror)
387 `(load ,file 'noerror nil nil 'mustsuffix)))
388
389 (provide 'org-macs)
390
391 ;;; org-macs.el ends here