org-macs.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. ;;; org-macs.el --- Top-level Definitions for Org -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file contains macro definitions, defsubst definitions, other
  23. ;; stuff needed for compilation and top-level forms in Org mode, as
  24. ;; well lots of small functions that are not Org mode specific but
  25. ;; simply generally useful stuff.
  26. ;;; Code:
  27. (defmacro org-with-gensyms (symbols &rest body)
  28. (declare (debug (sexp body)) (indent 1))
  29. `(let ,(mapcar (lambda (s)
  30. `(,s (make-symbol (concat "--" (symbol-name ',s)))))
  31. symbols)
  32. ,@body))
  33. (defun org-string-nw-p (s)
  34. "Return S if S is a string containing a non-blank character.
  35. Otherwise, return nil."
  36. (and (stringp s)
  37. (string-match-p "[^ \r\t\n]" s)
  38. s))
  39. (defun org-split-string (string &optional separators)
  40. "Splits STRING into substrings at SEPARATORS.
  41. SEPARATORS is a regular expression. When nil, it defaults to
  42. \"[ \f\t\n\r\v]+\".
  43. Unlike to `split-string', matching SEPARATORS at the beginning
  44. and end of string are ignored."
  45. (let ((separators (or separators "[ \f\t\n\r\v]+")))
  46. (when (string-match (concat "\\`" separators) string)
  47. (setq string (replace-match "" nil nil string)))
  48. (when (string-match (concat separators "\\'") string)
  49. (setq string (replace-match "" nil nil string)))
  50. (split-string string separators)))
  51. (defun org-not-nil (v)
  52. "If V not nil, and also not the string \"nil\", then return V.
  53. Otherwise return nil."
  54. (and v (not (equal v "nil")) v))
  55. (defmacro org-preserve-lc (&rest body)
  56. (declare (debug (body)))
  57. (org-with-gensyms (line col)
  58. `(let ((,line (org-current-line))
  59. (,col (current-column)))
  60. (unwind-protect
  61. (progn ,@body)
  62. (org-goto-line ,line)
  63. (org-move-to-column ,col)))))
  64. ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
  65. ;; `org-unmodified' to ignore real text modifications
  66. (defmacro org-unmodified (&rest body)
  67. "Run BODY while preserving the buffer's `buffer-modified-p' state."
  68. (declare (debug (body)))
  69. (org-with-gensyms (was-modified)
  70. `(let ((,was-modified (buffer-modified-p)))
  71. (unwind-protect
  72. (let ((buffer-undo-list t)
  73. (inhibit-modification-hooks t))
  74. ,@body)
  75. (set-buffer-modified-p ,was-modified)))))
  76. (defmacro org-without-partial-completion (&rest body)
  77. (declare (debug (body)))
  78. `(if (and (boundp 'partial-completion-mode)
  79. partial-completion-mode
  80. (fboundp 'partial-completion-mode))
  81. (unwind-protect
  82. (progn
  83. (partial-completion-mode -1)
  84. ,@body)
  85. (partial-completion-mode 1))
  86. ,@body))
  87. (defmacro org-with-point-at (pom &rest body)
  88. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  89. (declare (debug (form body)) (indent 1))
  90. (org-with-gensyms (mpom)
  91. `(let ((,mpom ,pom))
  92. (save-excursion
  93. (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
  94. (org-with-wide-buffer
  95. (goto-char (or ,mpom (point)))
  96. ,@body)))))
  97. (defmacro org-with-remote-undo (buffer &rest body)
  98. "Execute BODY while recording undo information in two buffers."
  99. (declare (debug (form body)) (indent 1))
  100. (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
  101. `(let ((,cline (org-current-line))
  102. (,cmd this-command)
  103. (,buf1 (current-buffer))
  104. (,buf2 ,buffer)
  105. (,undo1 buffer-undo-list)
  106. (,undo2 (with-current-buffer ,buffer buffer-undo-list))
  107. ,c1 ,c2)
  108. ,@body
  109. (when org-agenda-allow-remote-undo
  110. (setq ,c1 (org-verify-change-for-undo
  111. ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
  112. ,c2 (org-verify-change-for-undo
  113. ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
  114. (when (or ,c1 ,c2)
  115. ;; make sure there are undo boundaries
  116. (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
  117. (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
  118. ;; remember which buffer to undo
  119. (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
  120. org-agenda-undo-list))))))
  121. (defmacro org-no-read-only (&rest body)
  122. "Inhibit read-only for BODY."
  123. (declare (debug (body)))
  124. `(let ((inhibit-read-only t)) ,@body))
  125. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  126. rear-nonsticky t mouse-map t fontified t
  127. org-emphasis t)
  128. "Properties to remove when a string without properties is wanted.")
  129. (defsubst org-no-properties (s &optional restricted)
  130. "Remove all text properties from string S.
  131. When RESTRICTED is non-nil, only remove the properties listed
  132. in `org-rm-props'."
  133. (if restricted (remove-text-properties 0 (length s) org-rm-props s)
  134. (set-text-properties 0 (length s) nil s))
  135. s)
  136. (defsubst org-get-alist-option (option key)
  137. (cond ((eq key t) t)
  138. ((eq option t) t)
  139. ((assoc key option) (cdr (assoc key option)))
  140. (t (let ((r (cdr (assq 'default option))))
  141. (if (listp r) (delq nil r) r)))))
  142. (defsubst org-check-external-command (cmd &optional use no-error)
  143. "Check if external program CMD for USE exists, error if not.
  144. When the program does exist, return its path.
  145. When it does not exist and NO-ERROR is set, return nil.
  146. Otherwise, throw an error. The optional argument USE can describe what this
  147. program is needed for, so that the error message can be more informative."
  148. (or (executable-find cmd)
  149. (if no-error
  150. nil
  151. (error "Can't find `%s'%s" cmd
  152. (if use (format " (%s)" use) "")))))
  153. (defsubst org-last (list)
  154. "Return the last element of LIST."
  155. (car (last list)))
  156. (defun org-let (list &rest body)
  157. (eval (cons 'let (cons list body))))
  158. (put 'org-let 'lisp-indent-function 1)
  159. (defun org-let2 (list1 list2 &rest body)
  160. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  161. (put 'org-let2 'lisp-indent-function 2)
  162. (defsubst org-call-with-arg (command arg)
  163. "Call COMMAND interactively, but pretend prefix arg was ARG."
  164. (let ((current-prefix-arg arg)) (call-interactively command)))
  165. (defsubst org-current-line (&optional pos)
  166. (save-excursion
  167. (and pos (goto-char pos))
  168. ;; works also in narrowed buffer, because we start at 1, not point-min
  169. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  170. (defsubst org-goto-line (N)
  171. (save-restriction
  172. (widen)
  173. (goto-char (point-min))
  174. (forward-line (1- N))))
  175. (defsubst org-current-line-string (&optional to-here)
  176. (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
  177. (defsubst org-pos-in-match-range (pos n)
  178. (and (match-beginning n)
  179. (<= (match-beginning n) pos)
  180. (>= (match-end n) pos)))
  181. (defun org-match-line (regexp)
  182. "Match REGEXP at the beginning of the current line."
  183. (save-excursion
  184. (beginning-of-line)
  185. (looking-at regexp)))
  186. (defun org-plist-delete (plist property)
  187. "Delete PROPERTY from PLIST.
  188. This is in contrast to merely setting it to 0."
  189. (let (p)
  190. (while plist
  191. (if (not (eq property (car plist)))
  192. (setq p (plist-put p (car plist) (nth 1 plist))))
  193. (setq plist (cddr plist)))
  194. p))
  195. (defmacro org-save-outline-visibility (use-markers &rest body)
  196. "Save and restore outline visibility around BODY.
  197. If USE-MARKERS is non-nil, use markers for the positions.
  198. This means that the buffer may change while running BODY,
  199. but it also means that the buffer should stay alive
  200. during the operation, because otherwise all these markers will
  201. point nowhere."
  202. (declare (debug (form body)) (indent 1))
  203. (org-with-gensyms (data)
  204. `(let ((,data (org-outline-overlay-data ,use-markers)))
  205. (unwind-protect
  206. (prog1 (progn ,@body)
  207. (org-set-outline-overlay-data ,data))
  208. (when ,use-markers
  209. (dolist (c ,data)
  210. (when (markerp (car c)) (move-marker (car c) nil))
  211. (when (markerp (cdr c)) (move-marker (cdr c) nil))))))))
  212. (defmacro org-with-wide-buffer (&rest body)
  213. "Execute body while temporarily widening the buffer."
  214. (declare (debug (body)))
  215. `(save-excursion
  216. (save-restriction
  217. (widen)
  218. ,@body)))
  219. (defmacro org-with-limited-levels (&rest body)
  220. "Execute BODY with limited number of outline levels."
  221. (declare (debug (body)))
  222. `(progn
  223. (defvar org-called-with-limited-levels)
  224. (defvar org-outline-regexp)
  225. (defvar outline-regexp)
  226. (defvar org-outline-regexp-bol)
  227. (let* ((org-called-with-limited-levels t)
  228. (org-outline-regexp (org-get-limited-outline-regexp))
  229. (outline-regexp org-outline-regexp)
  230. (org-outline-regexp-bol (concat "^" org-outline-regexp)))
  231. ,@body)))
  232. (defvar org-outline-regexp) ; defined in org.el
  233. (defvar org-odd-levels-only) ; defined in org.el
  234. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  235. (defun org-get-limited-outline-regexp ()
  236. "Return outline-regexp with limited number of levels.
  237. The number of levels is controlled by `org-inlinetask-min-level'"
  238. (cond ((not (derived-mode-p 'org-mode))
  239. outline-regexp)
  240. ((not (featurep 'org-inlinetask))
  241. org-outline-regexp)
  242. (t
  243. (let* ((limit-level (1- org-inlinetask-min-level))
  244. (nstars (if org-odd-levels-only
  245. (1- (* limit-level 2))
  246. limit-level)))
  247. (format "\\*\\{1,%d\\} " nstars)))))
  248. (defmacro org-eval-in-environment (environment form)
  249. (declare (debug (form form)) (indent 1))
  250. `(eval (list 'let ,environment ',form)))
  251. (defun org-make-parameter-alist (flat)
  252. "Return alist based on FLAT.
  253. FLAT is a list with alternating symbol names and values. The
  254. returned alist is a list of lists with the symbol name in car and
  255. the value in cdr."
  256. (when flat
  257. (cons (list (car flat) (cadr flat))
  258. (org-make-parameter-alist (cddr flat)))))
  259. ;;;###autoload
  260. (defmacro org-load-noerror-mustsuffix (file)
  261. "Load FILE with optional arguments NOERROR and MUSTSUFFIX."
  262. `(load ,file 'noerror nil nil 'mustsuffix))
  263. (defun org-unbracket-string (pre post string)
  264. "Remove PRE/POST from the beginning/end of STRING.
  265. Both PRE and POST must be pre-/suffixes of STRING, or neither is
  266. removed."
  267. (if (and (string-prefix-p pre string)
  268. (string-suffix-p post string))
  269. (substring string (length pre) (- (length post)))
  270. string))
  271. (defun org-read-function (prompt &optional allow-empty?)
  272. "Prompt for a function.
  273. If ALLOW-EMPTY? is non-nil, return nil rather than raising an
  274. error when the user input is empty."
  275. (let ((func (completing-read prompt obarray #'fboundp t)))
  276. (cond ((not (string= func ""))
  277. (intern func))
  278. (allow-empty? nil)
  279. (t (user-error "Empty input is not valid")))))
  280. (provide 'org-macs)
  281. ;;; org-macs.el ends here