org-macro.el: Allow modification-time from vc.el
[org-mode.git] / lisp / org-macs.el
blobfd4791f54c3510d1d91bc8069db9bd3000336a32
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 (featurep 'xemacs)
61 `(interactive-p)
62 (if (or (> emacs-major-version 23)
63 (and (>= emacs-major-version 23)
64 (>= emacs-minor-version 2)))
65 ;; defined with no argument in <=23.1
66 `(with-no-warnings (called-interactively-p ,kind))
67 `(interactive-p))))
69 (defmacro org-bound-and-true-p (var)
70 "Return the value of symbol VAR if it is bound, else nil."
71 (declare (debug (symbolp)))
72 `(and (boundp (quote ,var)) ,var))
74 (defun org-string-nw-p (s)
75 "Return S if S is a string containing a non-blank character.
76 Otherwise, return nil."
77 (and (stringp s)
78 (org-string-match-p "[^ \r\t\n]" s)
79 s))
81 (defun org-not-nil (v)
82 "If V not nil, and also not the string \"nil\", then return V.
83 Otherwise return nil."
84 (and v (not (equal v "nil")) v))
86 (defun org-substitute-posix-classes (re)
87 "Substitute posix classes in regular expression RE."
88 (let ((ss re))
89 (save-match-data
90 (while (string-match "\\[:alnum:\\]" ss)
91 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
92 (while (string-match "\\[:word:\\]" ss)
93 (setq ss (replace-match "a-zA-Z0-9" t t ss)))
94 (while (string-match "\\[:alpha:\\]" ss)
95 (setq ss (replace-match "a-zA-Z" t t ss)))
96 (while (string-match "\\[:punct:\\]" ss)
97 (setq ss (replace-match "\001-@[-`{-~" t t ss)))
98 ss)))
100 (defmacro org-re (s)
101 "Replace posix classes in regular expression."
102 (declare (debug (form)))
103 (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
105 (defmacro org-preserve-lc (&rest body)
106 (declare (debug (body)))
107 (org-with-gensyms (line col)
108 `(let ((,line (org-current-line))
109 (,col (current-column)))
110 (unwind-protect
111 (progn ,@body)
112 (org-goto-line ,line)
113 (org-move-to-column ,col)))))
115 ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
116 ;; `org-unmodified' to ignore real text modifications
117 (defmacro org-unmodified (&rest body)
118 "Run BODY while preserving the buffer's `buffer-modified-p' state."
119 (declare (debug (body)))
120 (org-with-gensyms (was-modified)
121 `(let ((,was-modified (buffer-modified-p)))
122 (unwind-protect
123 (let ((buffer-undo-list t)
124 (inhibit-modification-hooks t))
125 ,@body)
126 (set-buffer-modified-p ,was-modified)))))
128 (defmacro org-without-partial-completion (&rest body)
129 (declare (debug (body)))
130 `(if (and (boundp 'partial-completion-mode)
131 partial-completion-mode
132 (fboundp 'partial-completion-mode))
133 (unwind-protect
134 (progn
135 (partial-completion-mode -1)
136 ,@body)
137 (partial-completion-mode 1))
138 ,@body))
140 ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
141 (defmacro org-maybe-intangible (props)
142 "Add \\='(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
143 In Emacs 21, invisible text is not avoided by the command loop, so the
144 intangible property is needed to make sure point skips this text.
145 In Emacs 22, this is not necessary. The intangible text property has
146 led to problems with flyspell. These problems are fixed in flyspell.el,
147 but we still avoid setting the property in Emacs 22 and later.
148 We use a macro so that the test can happen at compilation time."
149 (if (< emacs-major-version 22)
150 `(append '(intangible t) ,props)
151 props))
153 (defmacro org-with-point-at (pom &rest body)
154 "Move to buffer and point of point-or-marker POM for the duration of BODY."
155 (declare (debug (form body)) (indent 1))
156 (org-with-gensyms (mpom)
157 `(let ((,mpom ,pom))
158 (save-excursion
159 (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
160 (org-with-wide-buffer
161 (goto-char (or ,mpom (point)))
162 ,@body)))))
164 (defmacro org-no-warnings (&rest body)
165 (declare (debug (body)))
166 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
168 (defmacro org-with-remote-undo (buffer &rest body)
169 "Execute BODY while recording undo information in two buffers."
170 (declare (debug (form body)) (indent 1))
171 (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
172 `(let ((,cline (org-current-line))
173 (,cmd this-command)
174 (,buf1 (current-buffer))
175 (,buf2 ,buffer)
176 (,undo1 buffer-undo-list)
177 (,undo2 (with-current-buffer ,buffer buffer-undo-list))
178 ,c1 ,c2)
179 ,@body
180 (when org-agenda-allow-remote-undo
181 (setq ,c1 (org-verify-change-for-undo
182 ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
183 ,c2 (org-verify-change-for-undo
184 ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
185 (when (or ,c1 ,c2)
186 ;; make sure there are undo boundaries
187 (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
188 (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
189 ;; remember which buffer to undo
190 (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
191 org-agenda-undo-list))))))
193 (defmacro org-no-read-only (&rest body)
194 "Inhibit read-only for BODY."
195 (declare (debug (body)))
196 `(let ((inhibit-read-only t)) ,@body))
198 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
199 rear-nonsticky t mouse-map t fontified t
200 org-emphasis t)
201 "Properties to remove when a string without properties is wanted.")
203 (defsubst org-match-string-no-properties (num &optional string)
204 (if (featurep 'xemacs)
205 (let ((s (match-string num string)))
206 (and s (remove-text-properties 0 (length s) org-rm-props s))
208 (match-string-no-properties num string)))
210 (defsubst org-no-properties (s &optional restricted)
211 "Remove all text properties from string S.
212 When RESTRICTED is non-nil, only remove the properties listed
213 in `org-rm-props'."
214 (if (fboundp 'set-text-properties)
215 (set-text-properties 0 (length s) nil s)
216 (if restricted
217 (remove-text-properties 0 (length s) org-rm-props s)
218 (set-text-properties 0 (length s) nil s)))
221 (defsubst org-get-alist-option (option key)
222 (cond ((eq key t) t)
223 ((eq option t) t)
224 ((assoc key option) (cdr (assoc key option)))
225 (t (let ((r (cdr (assq 'default option))))
226 (if (listp r) (delq nil r) r)))))
228 (defsubst org-check-external-command (cmd &optional use no-error)
229 "Check if external program CMD for USE exists, error if not.
230 When the program does exist, return its path.
231 When it does not exist and NO-ERROR is set, return nil.
232 Otherwise, throw an error. The optional argument USE can describe what this
233 program is needed for, so that the error message can be more informative."
234 (or (executable-find cmd)
235 (if no-error
237 (error "Can't find `%s'%s" cmd
238 (if use (format " (%s)" use) "")))))
240 (defsubst org-inhibit-invisibility ()
241 "Modified `buffer-invisibility-spec' for Emacs 21.
242 Some ops with invisible text do not work correctly on Emacs 21. For these
243 we turn off invisibility temporarily. Use this in a `let' form."
244 (if (< emacs-major-version 22) nil buffer-invisibility-spec))
246 (defsubst org-last (list)
247 "Return the last element of LIST."
248 (car (last list)))
250 (defun org-let (list &rest body)
251 (eval (cons 'let (cons list body))))
252 (put 'org-let 'lisp-indent-function 1)
254 (defun org-let2 (list1 list2 &rest body)
255 (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
256 (put 'org-let2 'lisp-indent-function 2)
258 (defsubst org-call-with-arg (command arg)
259 "Call COMMAND interactively, but pretend prefix arg was ARG."
260 (let ((current-prefix-arg arg)) (call-interactively command)))
262 (defsubst org-current-line (&optional pos)
263 (save-excursion
264 (and pos (goto-char pos))
265 ;; works also in narrowed buffer, because we start at 1, not point-min
266 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
268 (defsubst org-goto-line (N)
269 (save-restriction
270 (widen)
271 (goto-char (point-min))
272 (forward-line (1- N))))
274 (defsubst org-current-line-string (&optional to-here)
275 (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
277 (defsubst org-pos-in-match-range (pos n)
278 (and (match-beginning n)
279 (<= (match-beginning n) pos)
280 (>= (match-end n) pos)))
282 (defun org-match-line (re)
283 "Looking-at at the beginning of the current line."
284 (save-excursion
285 (goto-char (point-at-bol))
286 (looking-at re)))
288 (defun org-plist-delete (plist property)
289 "Delete PROPERTY from PLIST.
290 This is in contrast to merely setting it to 0."
291 (let (p)
292 (while plist
293 (if (not (eq property (car plist)))
294 (setq p (plist-put p (car plist) (nth 1 plist))))
295 (setq plist (cddr plist)))
298 (defun org-replace-match-keep-properties (newtext &optional fixedcase
299 literal string)
300 "Like `replace-match', but add the text properties found original text."
301 (setq newtext (org-add-props newtext (text-properties-at
302 (match-beginning 0) string)))
303 (replace-match newtext fixedcase literal string))
305 (defmacro org-save-outline-visibility (use-markers &rest body)
306 "Save and restore outline visibility around BODY.
307 If USE-MARKERS is non-nil, use markers for the positions.
308 This means that the buffer may change while running BODY,
309 but it also means that the buffer should stay alive
310 during the operation, because otherwise all these markers will
311 point nowhere."
312 (declare (debug (form body)) (indent 1))
313 (org-with-gensyms (data rtn)
314 `(let ((,data (org-outline-overlay-data ,use-markers))
315 ,rtn)
316 (unwind-protect
317 (progn
318 (setq ,rtn (progn ,@body))
319 (org-set-outline-overlay-data ,data))
320 (when ,use-markers
321 (mapc (lambda (c)
322 (and (markerp (car c)) (move-marker (car c) nil))
323 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
324 ,data)))
325 ,rtn)))
327 (defmacro org-with-wide-buffer (&rest body)
328 "Execute body while temporarily widening the buffer."
329 (declare (debug (body)))
330 `(save-excursion
331 (save-restriction
332 (widen)
333 ,@body)))
335 (defmacro org-with-limited-levels (&rest body)
336 "Execute BODY with limited number of outline levels."
337 (declare (debug (body)))
338 `(progn
339 (defvar org-called-with-limited-levels)
340 (defvar org-outline-regexp)
341 (defvar outline-regexp)
342 (defvar org-outline-regexp-bol)
343 (let* ((org-called-with-limited-levels t)
344 (org-outline-regexp (org-get-limited-outline-regexp))
345 (outline-regexp org-outline-regexp)
346 (org-outline-regexp-bol (concat "^" org-outline-regexp)))
347 ,@body)))
349 (defvar org-outline-regexp) ; defined in org.el
350 (defvar org-odd-levels-only) ; defined in org.el
351 (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
352 (defun org-get-limited-outline-regexp ()
353 "Return outline-regexp with limited number of levels.
354 The number of levels is controlled by `org-inlinetask-min-level'"
355 (cond ((not (derived-mode-p 'org-mode))
356 outline-regexp)
357 ((not (featurep 'org-inlinetask))
358 org-outline-regexp)
360 (let* ((limit-level (1- org-inlinetask-min-level))
361 (nstars (if org-odd-levels-only
362 (1- (* limit-level 2))
363 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 (defmacro org-eval-in-environment (environment form)
373 (declare (debug (form form)) (indent 1))
374 `(eval (list 'let ,environment ',form)))
376 (defun org-make-parameter-alist (flat)
377 "Return alist based on FLAT.
378 FLAT is a list with alternating symbol names and values. The
379 returned alist is a list of lists with the symbol name in car and
380 the value in cdr."
381 (when flat
382 (cons (list (car flat) (cadr flat))
383 (org-make-parameter-alist (cddr flat)))))
385 ;;;###autoload
386 (defmacro org-load-noerror-mustsuffix (file)
387 "Load FILE with optional arguments NOERROR and MUSTSUFFIX. Drop the MUSTSUFFIX argument for XEmacs, which doesn't recognize it."
388 (if (featurep 'xemacs)
389 `(load ,file 'noerror)
390 `(load ,file 'noerror nil nil 'mustsuffix)))
392 (provide 'org-macs)
394 ;;; org-macs.el ends here