org-macs.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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-not-nil (v)
  40. "If V not nil, and also not the string \"nil\", then return V.
  41. Otherwise return nil."
  42. (and v (not (equal v "nil")) v))
  43. (defmacro org-preserve-lc (&rest body)
  44. (declare (debug (body)))
  45. (org-with-gensyms (line col)
  46. `(let ((,line (org-current-line))
  47. (,col (current-column)))
  48. (unwind-protect
  49. (progn ,@body)
  50. (org-goto-line ,line)
  51. (org-move-to-column ,col)))))
  52. ;; Use `org-with-silent-modifications' to ignore cosmetic changes and
  53. ;; `org-unmodified' to ignore real text modifications
  54. (defmacro org-unmodified (&rest body)
  55. "Run BODY while preserving the buffer's `buffer-modified-p' state."
  56. (declare (debug (body)))
  57. (org-with-gensyms (was-modified)
  58. `(let ((,was-modified (buffer-modified-p)))
  59. (unwind-protect
  60. (let ((buffer-undo-list t)
  61. (inhibit-modification-hooks t))
  62. ,@body)
  63. (set-buffer-modified-p ,was-modified)))))
  64. (defmacro org-without-partial-completion (&rest body)
  65. (declare (debug (body)))
  66. `(if (and (boundp 'partial-completion-mode)
  67. partial-completion-mode
  68. (fboundp 'partial-completion-mode))
  69. (unwind-protect
  70. (progn
  71. (partial-completion-mode -1)
  72. ,@body)
  73. (partial-completion-mode 1))
  74. ,@body))
  75. (defmacro org-with-point-at (pom &rest body)
  76. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  77. (declare (debug (form body)) (indent 1))
  78. (org-with-gensyms (mpom)
  79. `(let ((,mpom ,pom))
  80. (save-excursion
  81. (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
  82. (org-with-wide-buffer
  83. (goto-char (or ,mpom (point)))
  84. ,@body)))))
  85. (defmacro org-with-remote-undo (buffer &rest body)
  86. "Execute BODY while recording undo information in two buffers."
  87. (declare (debug (form body)) (indent 1))
  88. (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
  89. `(let ((,cline (org-current-line))
  90. (,cmd this-command)
  91. (,buf1 (current-buffer))
  92. (,buf2 ,buffer)
  93. (,undo1 buffer-undo-list)
  94. (,undo2 (with-current-buffer ,buffer buffer-undo-list))
  95. ,c1 ,c2)
  96. ,@body
  97. (when org-agenda-allow-remote-undo
  98. (setq ,c1 (org-verify-change-for-undo
  99. ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
  100. ,c2 (org-verify-change-for-undo
  101. ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
  102. (when (or ,c1 ,c2)
  103. ;; make sure there are undo boundaries
  104. (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
  105. (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
  106. ;; remember which buffer to undo
  107. (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
  108. org-agenda-undo-list))))))
  109. (defmacro org-no-read-only (&rest body)
  110. "Inhibit read-only for BODY."
  111. (declare (debug (body)))
  112. `(let ((inhibit-read-only t)) ,@body))
  113. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  114. rear-nonsticky t mouse-map t fontified t
  115. org-emphasis t)
  116. "Properties to remove when a string without properties is wanted.")
  117. (defsubst org-no-properties (s &optional restricted)
  118. "Remove all text properties from string S.
  119. When RESTRICTED is non-nil, only remove the properties listed
  120. in `org-rm-props'."
  121. (if restricted (remove-text-properties 0 (length s) org-rm-props s)
  122. (set-text-properties 0 (length s) nil s))
  123. s)
  124. (defsubst org-get-alist-option (option key)
  125. (cond ((eq key t) t)
  126. ((eq option t) t)
  127. ((assoc key option) (cdr (assoc key option)))
  128. (t (let ((r (cdr (assq 'default option))))
  129. (if (listp r) (delq nil r) r)))))
  130. (defsubst org-check-external-command (cmd &optional use no-error)
  131. "Check if external program CMD for USE exists, error if not.
  132. When the program does exist, return its path.
  133. When it does not exist and NO-ERROR is set, return nil.
  134. Otherwise, throw an error. The optional argument USE can describe what this
  135. program is needed for, so that the error message can be more informative."
  136. (or (executable-find cmd)
  137. (if no-error
  138. nil
  139. (error "Can't find `%s'%s" cmd
  140. (if use (format " (%s)" use) "")))))
  141. (defsubst org-last (list)
  142. "Return the last element of LIST."
  143. (car (last list)))
  144. (defun org-let (list &rest body)
  145. (eval (cons 'let (cons list body))))
  146. (put 'org-let 'lisp-indent-function 1)
  147. (defun org-let2 (list1 list2 &rest body)
  148. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  149. (put 'org-let2 'lisp-indent-function 2)
  150. (defsubst org-call-with-arg (command arg)
  151. "Call COMMAND interactively, but pretend prefix arg was ARG."
  152. (let ((current-prefix-arg arg)) (call-interactively command)))
  153. (defsubst org-current-line (&optional pos)
  154. (save-excursion
  155. (and pos (goto-char pos))
  156. ;; works also in narrowed buffer, because we start at 1, not point-min
  157. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  158. (defsubst org-goto-line (N)
  159. (save-restriction
  160. (widen)
  161. (goto-char (point-min))
  162. (forward-line (1- N))))
  163. (defsubst org-current-line-string (&optional to-here)
  164. (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
  165. (defsubst org-pos-in-match-range (pos n)
  166. (and (match-beginning n)
  167. (<= (match-beginning n) pos)
  168. (>= (match-end n) pos)))
  169. (defun org-match-line (regexp)
  170. "Match REGEXP at the beginning of the current line."
  171. (save-excursion
  172. (beginning-of-line)
  173. (looking-at regexp)))
  174. (defun org-plist-delete (plist property)
  175. "Delete PROPERTY from PLIST.
  176. This is in contrast to merely setting it to 0."
  177. (let (p)
  178. (while plist
  179. (if (not (eq property (car plist)))
  180. (setq p (plist-put p (car plist) (nth 1 plist))))
  181. (setq plist (cddr plist)))
  182. p))
  183. (defmacro org-save-outline-visibility (use-markers &rest body)
  184. "Save and restore outline visibility around BODY.
  185. If USE-MARKERS is non-nil, use markers for the positions.
  186. This means that the buffer may change while running BODY,
  187. but it also means that the buffer should stay alive
  188. during the operation, because otherwise all these markers will
  189. point nowhere."
  190. (declare (debug (form body)) (indent 1))
  191. (org-with-gensyms (data)
  192. `(let ((,data (org-outline-overlay-data ,use-markers)))
  193. (unwind-protect
  194. (prog1 (progn ,@body)
  195. (org-set-outline-overlay-data ,data))
  196. (when ,use-markers
  197. (dolist (c ,data)
  198. (when (markerp (car c)) (move-marker (car c) nil))
  199. (when (markerp (cdr c)) (move-marker (cdr c) nil))))))))
  200. (defmacro org-with-wide-buffer (&rest body)
  201. "Execute body while temporarily widening the buffer."
  202. (declare (debug (body)))
  203. `(save-excursion
  204. (save-restriction
  205. (widen)
  206. ,@body)))
  207. (defmacro org-with-limited-levels (&rest body)
  208. "Execute BODY with limited number of outline levels."
  209. (declare (debug (body)))
  210. `(progn
  211. (defvar org-called-with-limited-levels)
  212. (defvar org-outline-regexp)
  213. (defvar outline-regexp)
  214. (defvar org-outline-regexp-bol)
  215. (let* ((org-called-with-limited-levels t)
  216. (org-outline-regexp (org-get-limited-outline-regexp))
  217. (outline-regexp org-outline-regexp)
  218. (org-outline-regexp-bol (concat "^" org-outline-regexp)))
  219. ,@body)))
  220. (defvar org-outline-regexp) ; defined in org.el
  221. (defvar org-odd-levels-only) ; defined in org.el
  222. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  223. (defun org-get-limited-outline-regexp ()
  224. "Return outline-regexp with limited number of levels.
  225. The number of levels is controlled by `org-inlinetask-min-level'"
  226. (cond ((not (derived-mode-p 'org-mode))
  227. outline-regexp)
  228. ((not (featurep 'org-inlinetask))
  229. org-outline-regexp)
  230. (t
  231. (let* ((limit-level (1- org-inlinetask-min-level))
  232. (nstars (if org-odd-levels-only
  233. (1- (* limit-level 2))
  234. limit-level)))
  235. (format "\\*\\{1,%d\\} " nstars)))))
  236. (defmacro org-eval-in-environment (environment form)
  237. (declare (debug (form form)) (indent 1))
  238. `(eval (list 'let ,environment ',form)))
  239. (defun org-make-parameter-alist (flat)
  240. "Return alist based on FLAT.
  241. FLAT is a list with alternating symbol names and values. The
  242. returned alist is a list of lists with the symbol name in car and
  243. the value in cdr."
  244. (when flat
  245. (cons (list (car flat) (cadr flat))
  246. (org-make-parameter-alist (cddr flat)))))
  247. ;;;###autoload
  248. (defmacro org-load-noerror-mustsuffix (file)
  249. "Load FILE with optional arguments NOERROR and MUSTSUFFIX."
  250. `(load ,file 'noerror nil nil 'mustsuffix))
  251. (defun org-unbracket-string (pre post string)
  252. "Remove PRE/POST from the beginning/end of STRING.
  253. Both PRE and POST must be pre-/suffixes of STRING, or neither is
  254. removed."
  255. (if (and (string-prefix-p pre string)
  256. (string-suffix-p post string))
  257. (substring string (length pre) (- (length post)))
  258. string))
  259. (defun org-read-function (prompt &optional allow-empty?)
  260. "Prompt for a function.
  261. If ALLOW-EMPTY? is non-nil, return nil rather than raising an
  262. error when the user input is empty."
  263. (let ((func (completing-read prompt obarray #'fboundp t)))
  264. (cond ((not (string= func ""))
  265. (intern func))
  266. (allow-empty? nil)
  267. (t (user-error "Empty input is not valid")))))
  268. (provide 'org-macs)
  269. ;;; org-macs.el ends here