org-macs.el 12 KB

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