1 ;;; org-macs.el --- Top-level Definitions for Org -*- lexical-binding: t; -*-
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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains macro definitions, defsubst definitions, other
28 ;; stuff needed for compilation and top-level forms in Org mode, as
29 ;; well lots of small functions that are not Org mode specific but
30 ;; simply generally useful stuff.
34 (defmacro org-with-gensyms
(symbols &rest body
)
35 (declare (debug (sexp body
)) (indent 1))
36 `(let ,(mapcar (lambda (s)
37 `(,s
(make-symbol (concat "--" (symbol-name ',s
)))))
41 (defmacro org-called-interactively-p
(&optional kind
)
42 (declare (debug (&optional
("quote" symbolp
)))) ;Why not just t?
43 (if (or (> emacs-major-version
23)
44 (and (>= emacs-major-version
23)
45 (>= emacs-minor-version
2)))
46 ;; defined with no argument in <=23.1
47 `(with-no-warnings (called-interactively-p ,kind
))
50 (defmacro org-bound-and-true-p
(var)
51 "Return the value of symbol VAR if it is bound, else nil."
52 (declare (debug (symbolp)))
53 `(and (boundp (quote ,var
)) ,var
))
55 (defun org-string-nw-p (s)
56 "Return S if S is a string containing a non-blank character.
57 Otherwise, return nil."
59 (string-match-p "[^ \r\t\n]" s
)
62 (defun org-not-nil (v)
63 "If V not nil, and also not the string \"nil\", then return V.
64 Otherwise return nil."
65 (and v
(not (equal v
"nil")) v
))
67 (defun org-substitute-posix-classes (re)
68 "Substitute posix classes in regular expression RE."
71 (while (string-match "\\[:alnum:\\]" ss
)
72 (setq ss
(replace-match "a-zA-Z0-9" t t ss
)))
73 (while (string-match "\\[:word:\\]" ss
)
74 (setq ss
(replace-match "a-zA-Z0-9" t t ss
)))
75 (while (string-match "\\[:alpha:\\]" ss
)
76 (setq ss
(replace-match "a-zA-Z" t t ss
)))
77 (while (string-match "\\[:punct:\\]" ss
)
78 (setq ss
(replace-match "\001-@[-`{-~" t t ss
)))
81 (defmacro org-preserve-lc
(&rest body
)
82 (declare (debug (body)))
83 (org-with-gensyms (line col
)
84 `(let ((,line
(org-current-line))
85 (,col
(current-column)))
89 (org-move-to-column ,col
)))))
91 ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
92 ;; `org-unmodified' to ignore real text modifications
93 (defmacro org-unmodified
(&rest body
)
94 "Run BODY while preserving the buffer's `buffer-modified-p' state."
95 (declare (debug (body)))
96 (org-with-gensyms (was-modified)
97 `(let ((,was-modified
(buffer-modified-p)))
99 (let ((buffer-undo-list t
)
100 (inhibit-modification-hooks t
))
102 (set-buffer-modified-p ,was-modified
)))))
104 (defmacro org-without-partial-completion
(&rest body
)
105 (declare (debug (body)))
106 `(if (and (boundp 'partial-completion-mode
)
107 partial-completion-mode
108 (fboundp 'partial-completion-mode
))
111 (partial-completion-mode -
1)
113 (partial-completion-mode 1))
116 ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
117 (defmacro org-maybe-intangible
(props)
118 "Add \\='(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
119 In Emacs 21, invisible text is not avoided by the command loop, so the
120 intangible property is needed to make sure point skips this text.
121 In Emacs 22, this is not necessary. The intangible text property has
122 led to problems with flyspell. These problems are fixed in flyspell.el,
123 but we still avoid setting the property in Emacs 22 and later.
124 We use a macro so that the test can happen at compilation time."
125 (if (< emacs-major-version
22)
126 `(append '(intangible t
) ,props
)
129 (defmacro org-with-point-at
(pom &rest body
)
130 "Move to buffer and point of point-or-marker POM for the duration of BODY."
131 (declare (debug (form body
)) (indent 1))
132 (org-with-gensyms (mpom)
135 (if (markerp ,mpom
) (set-buffer (marker-buffer ,mpom
)))
136 (org-with-wide-buffer
137 (goto-char (or ,mpom
(point)))
140 (defmacro org-no-warnings
(&rest body
)
141 (declare (debug (body)))
142 (cons (if (fboundp 'with-no-warnings
) 'with-no-warnings
'progn
) body
))
144 (defmacro org-with-remote-undo
(buffer &rest body
)
145 "Execute BODY while recording undo information in two buffers."
146 (declare (debug (form body
)) (indent 1))
147 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2
)
148 `(let ((,cline
(org-current-line))
150 (,buf1
(current-buffer))
152 (,undo1 buffer-undo-list
)
153 (,undo2
(with-current-buffer ,buffer buffer-undo-list
))
156 (when org-agenda-allow-remote-undo
157 (setq ,c1
(org-verify-change-for-undo
158 ,undo1
(with-current-buffer ,buf1 buffer-undo-list
))
159 ,c2
(org-verify-change-for-undo
160 ,undo2
(with-current-buffer ,buf2 buffer-undo-list
)))
162 ;; make sure there are undo boundaries
163 (and ,c1
(with-current-buffer ,buf1
(undo-boundary)))
164 (and ,c2
(with-current-buffer ,buf2
(undo-boundary)))
165 ;; remember which buffer to undo
166 (push (list ,cmd
,cline
,buf1
,c1
,buf2
,c2
)
167 org-agenda-undo-list
))))))
169 (defmacro org-no-read-only
(&rest body
)
170 "Inhibit read-only for BODY."
171 (declare (debug (body)))
172 `(let ((inhibit-read-only t
)) ,@body
))
174 (defconst org-rm-props
'(invisible t face t keymap t intangible t mouse-face t
175 rear-nonsticky t mouse-map t fontified t
177 "Properties to remove when a string without properties is wanted.")
179 (defsubst org-no-properties
(s &optional restricted
)
180 "Remove all text properties from string S.
181 When RESTRICTED is non-nil, only remove the properties listed
183 (if (fboundp 'set-text-properties
)
184 (set-text-properties 0 (length s
) nil s
)
186 (remove-text-properties 0 (length s
) org-rm-props s
)
187 (set-text-properties 0 (length s
) nil s
)))
190 (defsubst org-get-alist-option
(option key
)
193 ((assoc key option
) (cdr (assoc key option
)))
194 (t (let ((r (cdr (assq 'default option
))))
195 (if (listp r
) (delq nil r
) r
)))))
197 (defsubst org-check-external-command
(cmd &optional use no-error
)
198 "Check if external program CMD for USE exists, error if not.
199 When the program does exist, return its path.
200 When it does not exist and NO-ERROR is set, return nil.
201 Otherwise, throw an error. The optional argument USE can describe what this
202 program is needed for, so that the error message can be more informative."
203 (or (executable-find cmd
)
206 (error "Can't find `%s'%s" cmd
207 (if use
(format " (%s)" use
) "")))))
209 (defsubst org-inhibit-invisibility
()
210 "Modified `buffer-invisibility-spec' for Emacs 21.
211 Some ops with invisible text do not work correctly on Emacs 21. For these
212 we turn off invisibility temporarily. Use this in a `let' form."
213 (if (< emacs-major-version
22) nil buffer-invisibility-spec
))
215 (defsubst org-last
(list)
216 "Return the last element of LIST."
219 (defun org-let (list &rest body
)
220 (eval (cons 'let
(cons list body
))))
221 (put 'org-let
'lisp-indent-function
1)
223 (defun org-let2 (list1 list2
&rest body
)
224 (eval (cons 'let
(cons list1
(list (cons 'let
(cons list2 body
)))))))
225 (put 'org-let2
'lisp-indent-function
2)
227 (defsubst org-call-with-arg
(command arg
)
228 "Call COMMAND interactively, but pretend prefix arg was ARG."
229 (let ((current-prefix-arg arg
)) (call-interactively command
)))
231 (defsubst org-current-line
(&optional pos
)
233 (and pos
(goto-char pos
))
234 ;; works also in narrowed buffer, because we start at 1, not point-min
235 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
237 (defsubst org-goto-line
(N)
240 (goto-char (point-min))
241 (forward-line (1- N
))))
243 (defsubst org-current-line-string
(&optional to-here
)
244 (buffer-substring (point-at-bol) (if to-here
(point) (point-at-eol))))
246 (defsubst org-pos-in-match-range
(pos n
)
247 (and (match-beginning n
)
248 (<= (match-beginning n
) pos
)
249 (>= (match-end n
) pos
)))
251 (defun org-match-line (re)
252 "Looking-at at the beginning of the current line."
254 (goto-char (point-at-bol))
257 (defun org-plist-delete (plist property
)
258 "Delete PROPERTY from PLIST.
259 This is in contrast to merely setting it to 0."
262 (if (not (eq property
(car plist
)))
263 (setq p
(plist-put p
(car plist
) (nth 1 plist
))))
264 (setq plist
(cddr plist
)))
267 (defun org-replace-match-keep-properties (newtext &optional fixedcase
269 "Like `replace-match', but add the text properties found original text."
270 (setq newtext
(org-add-props newtext
(text-properties-at
271 (match-beginning 0) string
)))
272 (replace-match newtext fixedcase literal string
))
274 (defmacro org-save-outline-visibility
(use-markers &rest body
)
275 "Save and restore outline visibility around BODY.
276 If USE-MARKERS is non-nil, use markers for the positions.
277 This means that the buffer may change while running BODY,
278 but it also means that the buffer should stay alive
279 during the operation, because otherwise all these markers will
281 (declare (debug (form body
)) (indent 1))
282 (org-with-gensyms (data)
283 `(let ((,data
(org-outline-overlay-data ,use-markers
)))
285 (prog1 (progn ,@body
)
286 (org-set-outline-overlay-data ,data
))
289 (when (markerp (car c
)) (move-marker (car c
) nil
))
290 (when (markerp (cdr c
)) (move-marker (cdr c
) nil
))))))))
292 (defmacro org-with-wide-buffer
(&rest body
)
293 "Execute body while temporarily widening the buffer."
294 (declare (debug (body)))
300 (defmacro org-with-limited-levels
(&rest body
)
301 "Execute BODY with limited number of outline levels."
302 (declare (debug (body)))
304 (defvar org-called-with-limited-levels
)
305 (defvar org-outline-regexp
)
306 (defvar outline-regexp
)
307 (defvar org-outline-regexp-bol
)
308 (let* ((org-called-with-limited-levels t
)
309 (org-outline-regexp (org-get-limited-outline-regexp))
310 (outline-regexp org-outline-regexp
)
311 (org-outline-regexp-bol (concat "^" org-outline-regexp
)))
314 (defvar org-outline-regexp
) ; defined in org.el
315 (defvar org-odd-levels-only
) ; defined in org.el
316 (defvar org-inlinetask-min-level
) ; defined in org-inlinetask.el
317 (defun org-get-limited-outline-regexp ()
318 "Return outline-regexp with limited number of levels.
319 The number of levels is controlled by `org-inlinetask-min-level'"
320 (cond ((not (derived-mode-p 'org-mode
))
322 ((not (featurep 'org-inlinetask
))
325 (let* ((limit-level (1- org-inlinetask-min-level
))
326 (nstars (if org-odd-levels-only
327 (1- (* limit-level
2))
329 (format "\\*\\{1,%d\\} " nstars
)))))
331 (defun org-format-seconds (string seconds
)
332 "Compatibility function replacing format-seconds."
333 (if (fboundp 'format-seconds
)
334 (format-seconds string seconds
)
335 (format-time-string string
(seconds-to-time seconds
))))
337 (defmacro org-eval-in-environment
(environment form
)
338 (declare (debug (form form
)) (indent 1))
339 `(eval (list 'let
,environment
',form
)))
341 (defun org-make-parameter-alist (flat)
342 "Return alist based on FLAT.
343 FLAT is a list with alternating symbol names and values. The
344 returned alist is a list of lists with the symbol name in car and
347 (cons (list (car flat
) (cadr flat
))
348 (org-make-parameter-alist (cddr flat
)))))
351 (defmacro org-load-noerror-mustsuffix
(file)
352 "Load FILE with optional arguments NOERROR and MUSTSUFFIX."
353 `(load ,file
'noerror nil nil
'mustsuffix
))
357 ;;; org-macs.el ends here