Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-macs.el
blob952f17da71a29d0a660fddb144affdbe815783f8
1 ;;; org-macs.el --- Top-level definitions for Org-mode
3 ;; Copyright (C) 2004-2016 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 ;;
9 ;; This file is part of GNU Emacs.
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.
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.
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
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.
32 ;;; Code:
34 (eval-and-compile
35 (unless (fboundp 'declare-function)
36 (defmacro declare-function (fn file &optional _arglist _fileonly)
37 `(autoload ',fn ,file)))
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)))))
47 (declare-function org-add-props "org-compat" (string plist &rest props))
48 (declare-function org-string-match-p "org-compat"
49 (regexp string &optional start))
51 (defmacro org-with-gensyms (symbols &rest body)
52 (declare (debug (sexp body)) (indent 1))
53 `(let ,(mapcar (lambda (s)
54 `(,s (make-symbol (concat "--" (symbol-name ',s)))))
55 symbols)
56 ,@body))
58 (defmacro org-called-interactively-p (&optional kind)
59 (declare (debug (&optional ("quote" symbolp)))) ;Why not just t?
60 (if (or (> emacs-major-version 23)
61 (and (>= emacs-major-version 23)
62 (>= emacs-minor-version 2)))
63 ;; defined with no argument in <=23.1
64 `(with-no-warnings (called-interactively-p ,kind))
65 `(interactive-p)))
67 (defmacro org-bound-and-true-p (var)
68 "Return the value of symbol VAR if it is bound, else nil."
69 (declare (debug (symbolp)))
70 `(and (boundp (quote ,var)) ,var))
72 (defun org-string-nw-p (s)
73 "Return S if S is a string containing a non-blank character.
74 Otherwise, return nil."
75 (and (stringp s)
76 (org-string-match-p "[^ \r\t\n]" s)
77 s))
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))
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)))
98 (defmacro org-preserve-lc (&rest body)
99 (declare (debug (body)))
100 (org-with-gensyms (line col)
101 `(let ((,line (org-current-line))
102 (,col (current-column)))
103 (unwind-protect
104 (progn ,@body)
105 (org-goto-line ,line)
106 (org-move-to-column ,col)))))
108 ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
109 ;; `org-unmodified' to ignore real text modifications
110 (defmacro org-unmodified (&rest body)
111 "Run BODY while preserving the buffer's `buffer-modified-p' state."
112 (declare (debug (body)))
113 (org-with-gensyms (was-modified)
114 `(let ((,was-modified (buffer-modified-p)))
115 (unwind-protect
116 (let ((buffer-undo-list t)
117 (inhibit-modification-hooks t))
118 ,@body)
119 (set-buffer-modified-p ,was-modified)))))
121 (defmacro org-without-partial-completion (&rest body)
122 (declare (debug (body)))
123 `(if (and (boundp 'partial-completion-mode)
124 partial-completion-mode
125 (fboundp 'partial-completion-mode))
126 (unwind-protect
127 (progn
128 (partial-completion-mode -1)
129 ,@body)
130 (partial-completion-mode 1))
131 ,@body))
133 ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
134 (defmacro org-maybe-intangible (props)
135 "Add \\='(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
136 In Emacs 21, invisible text is not avoided by the command loop, so the
137 intangible property is needed to make sure point skips this text.
138 In Emacs 22, this is not necessary. The intangible text property has
139 led to problems with flyspell. These problems are fixed in flyspell.el,
140 but we still avoid setting the property in Emacs 22 and later.
141 We use a macro so that the test can happen at compilation time."
142 (if (< emacs-major-version 22)
143 `(append '(intangible t) ,props)
144 props))
146 (defmacro org-with-point-at (pom &rest body)
147 "Move to buffer and point of point-or-marker POM for the duration of BODY."
148 (declare (debug (form body)) (indent 1))
149 (org-with-gensyms (mpom)
150 `(let ((,mpom ,pom))
151 (save-excursion
152 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
153 (org-with-wide-buffer
154 (goto-char (or ,mpom (point)))
155 ,@body)))))
157 (defmacro org-no-warnings (&rest body)
158 (declare (debug (body)))
159 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
161 (defmacro org-with-remote-undo (buffer &rest body)
162 "Execute BODY while recording undo information in two buffers."
163 (declare (debug (form body)) (indent 1))
164 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
165 `(let ((,cline (org-current-line))
166 (,cmd this-command)
167 (,buf1 (current-buffer))
168 (,buf2 ,buffer)
169 (,undo1 buffer-undo-list)
170 (,undo2 (with-current-buffer ,buffer buffer-undo-list))
171 ,c1 ,c2)
172 ,@body
173 (when org-agenda-allow-remote-undo
174 (setq ,c1 (org-verify-change-for-undo
175 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
176 ,c2 (org-verify-change-for-undo
177 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
178 (when (or ,c1 ,c2)
179 ;; make sure there are undo boundaries
180 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
181 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
182 ;; remember which buffer to undo
183 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
184 org-agenda-undo-list))))))
186 (defmacro org-no-read-only (&rest body)
187 "Inhibit read-only for BODY."
188 (declare (debug (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-no-properties (s &optional restricted)
197 "Remove all text properties from string S.
198 When RESTRICTED is non-nil, only remove the properties listed
199 in `org-rm-props'."
200 (if (fboundp 'set-text-properties)
201 (set-text-properties 0 (length s) nil s)
202 (if restricted
203 (remove-text-properties 0 (length s) org-rm-props s)
204 (set-text-properties 0 (length s) nil s)))
207 (defsubst org-get-alist-option (option key)
208 (cond ((eq key t) t)
209 ((eq option t) t)
210 ((assoc key option) (cdr (assoc key option)))
211 (t (let ((r (cdr (assq 'default option))))
212 (if (listp r) (delq nil r) r)))))
214 (defsubst org-check-external-command (cmd &optional use no-error)
215 "Check if external program CMD for USE exists, error if not.
216 When the program does exist, return its path.
217 When it does not exist and NO-ERROR is set, return nil.
218 Otherwise, throw an error. The optional argument USE can describe what this
219 program is needed for, so that the error message can be more informative."
220 (or (executable-find cmd)
221 (if no-error
223 (error "Can't find `%s'%s" cmd
224 (if use (format " (%s)" use) "")))))
226 (defsubst org-inhibit-invisibility ()
227 "Modified `buffer-invisibility-spec' for Emacs 21.
228 Some ops with invisible text do not work correctly on Emacs 21. For these
229 we turn off invisibility temporarily. Use this in a `let' form."
230 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
232 (defsubst org-last (list)
233 "Return the last element of LIST."
234 (car (last list)))
236 (defun org-let (list &rest body)
237 (eval (cons 'let (cons list body))))
238 (put 'org-let 'lisp-indent-function 1)
240 (defun org-let2 (list1 list2 &rest body)
241 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
242 (put 'org-let2 'lisp-indent-function 2)
244 (defsubst org-call-with-arg (command arg)
245 "Call COMMAND interactively, but pretend prefix arg was ARG."
246 (let ((current-prefix-arg arg)) (call-interactively command)))
248 (defsubst org-current-line (&optional pos)
249 (save-excursion
250 (and pos (goto-char pos))
251 ;; works also in narrowed buffer, because we start at 1, not point-min
252 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
254 (defsubst org-goto-line (N)
255 (save-restriction
256 (widen)
257 (goto-char (point-min))
258 (forward-line (1- N))))
260 (defsubst org-current-line-string (&optional to-here)
261 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
263 (defsubst org-pos-in-match-range (pos n)
264 (and (match-beginning n)
265 (<= (match-beginning n) pos)
266 (>= (match-end n) pos)))
268 (defun org-match-line (re)
269 "Looking-at at the beginning of the current line."
270 (save-excursion
271 (goto-char (point-at-bol))
272 (looking-at re)))
274 (defun org-plist-delete (plist property)
275 "Delete PROPERTY from PLIST.
276 This is in contrast to merely setting it to 0."
277 (let (p)
278 (while plist
279 (if (not (eq property (car plist)))
280 (setq p (plist-put p (car plist) (nth 1 plist))))
281 (setq plist (cddr plist)))
284 (defun org-replace-match-keep-properties (newtext &optional fixedcase
285 literal string)
286 "Like `replace-match', but add the text properties found original text."
287 (setq newtext (org-add-props newtext (text-properties-at
288 (match-beginning 0) string)))
289 (replace-match newtext fixedcase literal string))
291 (defmacro org-save-outline-visibility (use-markers &rest body)
292 "Save and restore outline visibility around BODY.
293 If USE-MARKERS is non-nil, use markers for the positions.
294 This means that the buffer may change while running BODY,
295 but it also means that the buffer should stay alive
296 during the operation, because otherwise all these markers will
297 point nowhere."
298 (declare (debug (form body)) (indent 1))
299 (org-with-gensyms (data rtn)
300 `(let ((,data (org-outline-overlay-data ,use-markers))
301 ,rtn)
302 (unwind-protect
303 (progn
304 (setq ,rtn (progn ,@body))
305 (org-set-outline-overlay-data ,data))
306 (when ,use-markers
307 (mapc (lambda (c)
308 (and (markerp (car c)) (move-marker (car c) nil))
309 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
310 ,data)))
311 ,rtn)))
313 (defmacro org-with-wide-buffer (&rest body)
314 "Execute body while temporarily widening the buffer."
315 (declare (debug (body)))
316 `(save-excursion
317 (save-restriction
318 (widen)
319 ,@body)))
321 (defmacro org-with-limited-levels (&rest body)
322 "Execute BODY with limited number of outline levels."
323 (declare (debug (body)))
324 `(progn
325 (defvar org-called-with-limited-levels)
326 (defvar org-outline-regexp)
327 (defvar outline-regexp)
328 (defvar org-outline-regexp-bol)
329 (let* ((org-called-with-limited-levels t)
330 (org-outline-regexp (org-get-limited-outline-regexp))
331 (outline-regexp org-outline-regexp)
332 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
333 ,@body)))
335 (defvar org-outline-regexp) ; defined in org.el
336 (defvar org-odd-levels-only) ; defined in org.el
337 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
338 (defun org-get-limited-outline-regexp ()
339 "Return outline-regexp with limited number of levels.
340 The number of levels is controlled by `org-inlinetask-min-level'"
341 (cond ((not (derived-mode-p 'org-mode))
342 outline-regexp)
343 ((not (featurep 'org-inlinetask))
344 org-outline-regexp)
346 (let* ((limit-level (1- org-inlinetask-min-level))
347 (nstars (if org-odd-levels-only
348 (1- (* limit-level 2))
349 limit-level)))
350 (format "\\*\\{1,%d\\} " nstars)))))
352 (defun org-format-seconds (string seconds)
353 "Compatibility function replacing format-seconds."
354 (if (fboundp 'format-seconds)
355 (format-seconds string seconds)
356 (format-time-string string (seconds-to-time seconds))))
358 (defmacro org-eval-in-environment (environment form)
359 (declare (debug (form form)) (indent 1))
360 `(eval (list 'let ,environment ',form)))
362 (defun org-make-parameter-alist (flat)
363 "Return alist based on FLAT.
364 FLAT is a list with alternating symbol names and values. The
365 returned alist is a list of lists with the symbol name in car and
366 the value in cdr."
367 (when flat
368 (cons (list (car flat) (cadr flat))
369 (org-make-parameter-alist (cddr flat)))))
371 ;;;###autoload
372 (defmacro org-load-noerror-mustsuffix (file)
373 "Load FILE with optional arguments NOERROR and MUSTSUFFIX."
374 `(load ,file 'noerror nil nil 'mustsuffix))
376 (provide 'org-macs)
378 ;;; org-macs.el ends here