ob-exp.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. ;;; ob-exp.el --- Exportation of Babel Source Blocks -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2021 Free Software Foundation, Inc.
  3. ;; Authors: Eric Schulte
  4. ;; Dan Davison
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: https://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Code:
  19. (require 'ob-core)
  20. (declare-function org-babel-lob-get-info "ob-lob" (&optional datum))
  21. (declare-function org-element-at-point "org-element" ())
  22. (declare-function org-element-context "org-element" (&optional element))
  23. (declare-function org-element-property "org-element" (property element))
  24. (declare-function org-element-type "org-element" (element))
  25. (declare-function org-escape-code-in-string "org-src" (s))
  26. (declare-function org-export-copy-buffer "ox" ())
  27. (declare-function org-in-commented-heading-p "org" (&optional no-inheritance))
  28. (declare-function org-in-archived-heading-p "org" (&optional no-inheritance))
  29. (defvar org-src-preserve-indentation)
  30. (defcustom org-export-use-babel t
  31. "Switch controlling code evaluation and header processing during export.
  32. When set to nil no code will be evaluated as part of the export
  33. process and no header arguments will be obeyed. Users who wish
  34. to avoid evaluating code on export should use the header argument
  35. `:eval never-export'."
  36. :group 'org-babel
  37. :version "24.1"
  38. :type '(choice (const :tag "Never" nil)
  39. (const :tag "Always" t))
  40. :safe #'null)
  41. (defmacro org-babel-exp--at-source (&rest body)
  42. "Evaluate BODY at the source of the Babel block at point.
  43. Source is located in `org-babel-exp-reference-buffer'. The value
  44. returned is the value of the last form in BODY. Assume that
  45. point is at the beginning of the Babel block."
  46. (declare (indent 1) (debug body))
  47. `(let ((source (get-text-property (point) 'org-reference)))
  48. ;; Source blocks created during export process (e.g., by other
  49. ;; source blocks) are not referenced. In this case, do not move
  50. ;; point at all.
  51. (with-current-buffer (if source org-babel-exp-reference-buffer
  52. (current-buffer))
  53. (org-with-wide-buffer
  54. (when source (goto-char source))
  55. ,@body))))
  56. (defun org-babel-exp-src-block ()
  57. "Process source block for export.
  58. Depending on the \":export\" header argument, replace the source
  59. code block like this:
  60. both ---- display the code and the results
  61. code ---- the default, display the code inside the block but do
  62. not process
  63. results - just like none only the block is run on export ensuring
  64. that its results are present in the Org mode buffer
  65. none ---- do not display either code or results upon export
  66. Assume point is at block opening line."
  67. (interactive)
  68. (save-excursion
  69. (let* ((info (org-babel-get-src-block-info))
  70. (lang (nth 0 info))
  71. (raw-params (nth 2 info))
  72. hash)
  73. ;; bail if we couldn't get any info from the block
  74. (unless noninteractive
  75. (message "org-babel-exp process %s at position %d..."
  76. lang
  77. (line-beginning-position)))
  78. (when info
  79. ;; if we're actually going to need the parameters
  80. (when (member (cdr (assq :exports (nth 2 info))) '("both" "results"))
  81. (let ((lang-headers (intern (concat "org-babel-default-header-args:"
  82. lang))))
  83. (org-babel-exp--at-source
  84. (setf (nth 2 info)
  85. (org-babel-process-params
  86. (apply #'org-babel-merge-params
  87. org-babel-default-header-args
  88. (and (boundp lang-headers)
  89. (symbol-value lang-headers))
  90. (append (org-babel-params-from-properties lang)
  91. (list raw-params)))))))
  92. (setf hash (org-babel-sha1-hash info :export)))
  93. (org-babel-exp-do-export info 'block hash)))))
  94. (defcustom org-babel-exp-call-line-template
  95. ""
  96. "Template used to export call lines.
  97. This template may be customized to include the call line name
  98. with any export markup. The template is filled out using
  99. `org-fill-template', and the following %keys may be used.
  100. line --- call line
  101. An example value would be \"\\n: call: %line\" to export the call line
  102. wrapped in a verbatim environment.
  103. Note: the results are inserted separately after the contents of
  104. this template."
  105. :group 'org-babel
  106. :type 'string)
  107. (defun org-babel-exp-process-buffer ()
  108. "Execute all Babel blocks in current buffer."
  109. (interactive)
  110. (when org-export-use-babel
  111. (save-window-excursion
  112. (let ((case-fold-search t)
  113. (regexp "\\(call\\|src\\)_\\|^[ \t]*#\\+\\(BEGIN_SRC\\|CALL:\\)")
  114. ;; Get a pristine copy of current buffer so Babel
  115. ;; references are properly resolved and source block
  116. ;; context is preserved.
  117. (org-babel-exp-reference-buffer (org-export-copy-buffer)))
  118. (unwind-protect
  119. (save-excursion
  120. ;; First attach to every source block their original
  121. ;; position, so that they can be retrieved within
  122. ;; `org-babel-exp-reference-buffer', even after heavy
  123. ;; modifications on current buffer.
  124. ;;
  125. ;; False positives are harmless, so we don't check if
  126. ;; we're really at some Babel object. Moreover,
  127. ;; `line-end-position' ensures that we propertize
  128. ;; a noticeable part of the object, without affecting
  129. ;; multiple objects on the same line.
  130. (goto-char (point-min))
  131. (while (re-search-forward regexp nil t)
  132. (let ((s (match-beginning 0)))
  133. (put-text-property s (line-end-position) 'org-reference s)))
  134. ;; Evaluate from top to bottom every Babel block
  135. ;; encountered.
  136. (goto-char (point-min))
  137. (while (re-search-forward regexp nil t)
  138. (unless (save-match-data (or (org-in-commented-heading-p)
  139. (org-in-archived-heading-p)))
  140. (let* ((object? (match-end 1))
  141. (element (save-match-data
  142. (if object? (org-element-context)
  143. ;; No deep inspection if we're
  144. ;; just looking for an element.
  145. (org-element-at-point))))
  146. (type
  147. (pcase (org-element-type element)
  148. ;; Discard block elements if we're looking
  149. ;; for inline objects. False results
  150. ;; happen when, e.g., "call_" syntax is
  151. ;; located within affiliated keywords:
  152. ;;
  153. ;; #+name: call_src
  154. ;; #+begin_src ...
  155. ((and (or `babel-call `src-block) (guard object?))
  156. nil)
  157. (type type)))
  158. (begin
  159. (copy-marker (org-element-property :begin element)))
  160. (end
  161. (copy-marker
  162. (save-excursion
  163. (goto-char (org-element-property :end element))
  164. (skip-chars-backward " \r\t\n")
  165. (point)))))
  166. (pcase type
  167. (`inline-src-block
  168. (let* ((info
  169. (org-babel-get-src-block-info nil element))
  170. (params (nth 2 info)))
  171. (setf (nth 1 info)
  172. (if (and (cdr (assq :noweb params))
  173. (string= "yes"
  174. (cdr (assq :noweb params))))
  175. (org-babel-expand-noweb-references
  176. info org-babel-exp-reference-buffer)
  177. (nth 1 info)))
  178. (goto-char begin)
  179. (let ((replacement
  180. (org-babel-exp-do-export info 'inline)))
  181. (if (equal replacement "")
  182. ;; Replacement code is empty: remove
  183. ;; inline source block, including extra
  184. ;; white space that might have been
  185. ;; created when inserting results.
  186. (delete-region begin
  187. (progn (goto-char end)
  188. (skip-chars-forward " \t")
  189. (point)))
  190. ;; Otherwise: remove inline source block
  191. ;; but preserve following white spaces.
  192. ;; Then insert value.
  193. (delete-region begin end)
  194. (insert replacement)))))
  195. ((or `babel-call `inline-babel-call)
  196. (org-babel-exp-do-export
  197. (or (org-babel-lob-get-info element)
  198. (user-error "Unknown Babel reference: %s"
  199. (org-element-property :call element)))
  200. 'lob)
  201. (let ((rep
  202. (org-fill-template
  203. org-babel-exp-call-line-template
  204. `(("line" .
  205. ,(org-element-property :value element))))))
  206. ;; If replacement is empty, completely remove
  207. ;; the object/element, including any extra
  208. ;; white space that might have been created
  209. ;; when including results.
  210. (if (equal rep "")
  211. (delete-region
  212. begin
  213. (progn (goto-char end)
  214. (if (not (eq type 'babel-call))
  215. (progn (skip-chars-forward " \t")
  216. (point))
  217. (skip-chars-forward " \r\t\n")
  218. (line-beginning-position))))
  219. ;; Otherwise, preserve trailing
  220. ;; spaces/newlines and then, insert
  221. ;; replacement string.
  222. (goto-char begin)
  223. (delete-region begin end)
  224. (insert rep))))
  225. (`src-block
  226. (let ((match-start (copy-marker (match-beginning 0)))
  227. (ind (current-indentation)))
  228. ;; Take care of matched block: compute
  229. ;; replacement string. In particular, a nil
  230. ;; REPLACEMENT means the block is left as-is
  231. ;; while an empty string removes the block.
  232. (let ((replacement
  233. (progn (goto-char match-start)
  234. (org-babel-exp-src-block))))
  235. (cond ((not replacement) (goto-char end))
  236. ((equal replacement "")
  237. (goto-char end)
  238. (skip-chars-forward " \r\t\n")
  239. (beginning-of-line)
  240. (delete-region begin (point)))
  241. (t
  242. (goto-char match-start)
  243. (delete-region (point)
  244. (save-excursion
  245. (goto-char end)
  246. (line-end-position)))
  247. (insert replacement)
  248. (if (or org-src-preserve-indentation
  249. (org-element-property
  250. :preserve-indent element))
  251. ;; Indent only code block
  252. ;; markers.
  253. (save-excursion
  254. (skip-chars-backward " \r\t\n")
  255. (indent-line-to ind)
  256. (goto-char match-start)
  257. (indent-line-to ind))
  258. ;; Indent everything.
  259. (indent-rigidly
  260. match-start (point) ind)))))
  261. (set-marker match-start nil))))
  262. (set-marker begin nil)
  263. (set-marker end nil)))))
  264. (kill-buffer org-babel-exp-reference-buffer)
  265. (remove-text-properties (point-min) (point-max)
  266. '(org-reference nil)))))))
  267. (defun org-babel-exp-do-export (info type &optional hash)
  268. "Return a string with the exported content of a code block.
  269. The function respects the value of the :exports header argument."
  270. (let ((silently (lambda () (let ((session (cdr (assq :session (nth 2 info)))))
  271. (unless (equal "none" session)
  272. (org-babel-exp-results info type 'silent)))))
  273. (clean (lambda () (if (eq type 'inline)
  274. (org-babel-remove-inline-result)
  275. (org-babel-remove-result info)))))
  276. (pcase (or (cdr (assq :exports (nth 2 info))) "code")
  277. ("none" (funcall silently) (funcall clean) "")
  278. ("code" (funcall silently) (funcall clean) (org-babel-exp-code info type))
  279. ("results" (org-babel-exp-results info type nil hash) "")
  280. ("both"
  281. (org-babel-exp-results info type nil hash)
  282. (org-babel-exp-code info type)))))
  283. (defcustom org-babel-exp-code-template
  284. "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
  285. "Template used to export the body of code blocks.
  286. This template may be customized to include additional information
  287. such as the code block name, or the values of particular header
  288. arguments. The template is filled out using `org-fill-template',
  289. and the following %keys may be used.
  290. lang ------ the language of the code block
  291. name ------ the name of the code block
  292. body ------ the body of the code block
  293. switches -- the switches associated to the code block
  294. flags ----- the flags passed to the code block
  295. In addition to the keys mentioned above, every header argument
  296. defined for the code block may be used as a key and will be
  297. replaced with its value."
  298. :group 'org-babel
  299. :type 'string)
  300. (defcustom org-babel-exp-inline-code-template
  301. "src_%lang[%switches%flags]{%body}"
  302. "Template used to export the body of inline code blocks.
  303. This template may be customized to include additional information
  304. such as the code block name, or the values of particular header
  305. arguments. The template is filled out using `org-fill-template',
  306. and the following %keys may be used.
  307. lang ------ the language of the code block
  308. name ------ the name of the code block
  309. body ------ the body of the code block
  310. switches -- the switches associated to the code block
  311. flags ----- the flags passed to the code block
  312. In addition to the keys mentioned above, every header argument
  313. defined for the code block may be used as a key and will be
  314. replaced with its value."
  315. :group 'org-babel
  316. :type 'string
  317. :version "26.1"
  318. :package-version '(Org . "8.3"))
  319. (defun org-babel-exp-code (info type)
  320. "Return the original code block formatted for export."
  321. (setf (nth 1 info)
  322. (if (string= "strip-export" (cdr (assq :noweb (nth 2 info))))
  323. (replace-regexp-in-string
  324. (org-babel-noweb-wrap) "" (nth 1 info))
  325. (if (org-babel-noweb-p (nth 2 info) :export)
  326. (org-babel-expand-noweb-references
  327. info org-babel-exp-reference-buffer)
  328. (nth 1 info))))
  329. (org-fill-template
  330. (if (eq type 'inline)
  331. org-babel-exp-inline-code-template
  332. org-babel-exp-code-template)
  333. `(("lang" . ,(nth 0 info))
  334. ("body" . ,(org-escape-code-in-string (nth 1 info)))
  335. ("switches" . ,(let ((f (nth 3 info)))
  336. (and (org-string-nw-p f) (concat " " f))))
  337. ("flags" . ,(let ((f (assq :flags (nth 2 info))))
  338. (and f (concat " " (cdr f)))))
  339. ,@(mapcar (lambda (pair)
  340. (cons (substring (symbol-name (car pair)) 1)
  341. (format "%S" (cdr pair))))
  342. (nth 2 info))
  343. ("name" . ,(or (nth 4 info) "")))))
  344. (defun org-babel-exp-results (info type &optional silent hash)
  345. "Evaluate and return the results of the current code block for export.
  346. Results are prepared in a manner suitable for export by Org mode.
  347. This function is called by `org-babel-exp-do-export'. The code
  348. block will be evaluated. Optional argument SILENT can be used to
  349. inhibit insertion of results into the buffer."
  350. (unless (and hash (equal hash (org-babel-current-result-hash)))
  351. (let ((lang (nth 0 info))
  352. (body (if (org-babel-noweb-p (nth 2 info) :eval)
  353. (org-babel-expand-noweb-references
  354. info org-babel-exp-reference-buffer)
  355. (nth 1 info)))
  356. (info (copy-sequence info))
  357. (org-babel-current-src-block-location (point-marker)))
  358. ;; Skip code blocks which we can't evaluate.
  359. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  360. (org-babel-eval-wipe-error-buffer)
  361. (setf (nth 1 info) body)
  362. (setf (nth 2 info)
  363. (org-babel-exp--at-source
  364. (org-babel-process-params
  365. (org-babel-merge-params
  366. (nth 2 info)
  367. `((:results . ,(if silent "silent" "replace")))))))
  368. (pcase type
  369. (`block (org-babel-execute-src-block nil info))
  370. (`inline
  371. ;; Position the point on the inline source block
  372. ;; allowing `org-babel-insert-result' to check that the
  373. ;; block is inline.
  374. (goto-char (nth 5 info))
  375. (org-babel-execute-src-block nil info))
  376. (`lob
  377. (save-excursion
  378. (goto-char (nth 5 info))
  379. (org-babel-execute-src-block nil info))))))))
  380. (provide 'ob-exp)
  381. ;;; ob-exp.el ends here