ob-exp.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 (org-babel-lob-get-info element)
  197. 'lob)
  198. (let ((rep
  199. (org-fill-template
  200. org-babel-exp-call-line-template
  201. `(("line" .
  202. ,(org-element-property :value element))))))
  203. ;; If replacement is empty, completely remove
  204. ;; the object/element, including any extra
  205. ;; white space that might have been created
  206. ;; when including results.
  207. (if (equal rep "")
  208. (delete-region
  209. begin
  210. (progn (goto-char end)
  211. (if (not (eq type 'babel-call))
  212. (progn (skip-chars-forward " \t")
  213. (point))
  214. (skip-chars-forward " \r\t\n")
  215. (line-beginning-position))))
  216. ;; Otherwise, preserve trailing
  217. ;; spaces/newlines and then, insert
  218. ;; replacement string.
  219. (goto-char begin)
  220. (delete-region begin end)
  221. (insert rep))))
  222. (`src-block
  223. (let ((match-start (copy-marker (match-beginning 0)))
  224. (ind (current-indentation)))
  225. ;; Take care of matched block: compute
  226. ;; replacement string. In particular, a nil
  227. ;; REPLACEMENT means the block is left as-is
  228. ;; while an empty string removes the block.
  229. (let ((replacement
  230. (progn (goto-char match-start)
  231. (org-babel-exp-src-block))))
  232. (cond ((not replacement) (goto-char end))
  233. ((equal replacement "")
  234. (goto-char end)
  235. (skip-chars-forward " \r\t\n")
  236. (beginning-of-line)
  237. (delete-region begin (point)))
  238. (t
  239. (goto-char match-start)
  240. (delete-region (point)
  241. (save-excursion
  242. (goto-char end)
  243. (line-end-position)))
  244. (insert replacement)
  245. (if (or org-src-preserve-indentation
  246. (org-element-property
  247. :preserve-indent element))
  248. ;; Indent only code block
  249. ;; markers.
  250. (save-excursion
  251. (skip-chars-backward " \r\t\n")
  252. (indent-line-to ind)
  253. (goto-char match-start)
  254. (indent-line-to ind))
  255. ;; Indent everything.
  256. (indent-rigidly
  257. match-start (point) ind)))))
  258. (set-marker match-start nil))))
  259. (set-marker begin nil)
  260. (set-marker end nil)))))
  261. (kill-buffer org-babel-exp-reference-buffer)
  262. (remove-text-properties (point-min) (point-max)
  263. '(org-reference nil)))))))
  264. (defun org-babel-exp-do-export (info type &optional hash)
  265. "Return a string with the exported content of a code block.
  266. The function respects the value of the :exports header argument."
  267. (let ((silently (lambda () (let ((session (cdr (assq :session (nth 2 info)))))
  268. (unless (equal "none" session)
  269. (org-babel-exp-results info type 'silent)))))
  270. (clean (lambda () (if (eq type 'inline)
  271. (org-babel-remove-inline-result)
  272. (org-babel-remove-result info)))))
  273. (pcase (or (cdr (assq :exports (nth 2 info))) "code")
  274. ("none" (funcall silently) (funcall clean) "")
  275. ("code" (funcall silently) (funcall clean) (org-babel-exp-code info type))
  276. ("results" (org-babel-exp-results info type nil hash) "")
  277. ("both"
  278. (org-babel-exp-results info type nil hash)
  279. (org-babel-exp-code info type)))))
  280. (defcustom org-babel-exp-code-template
  281. "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
  282. "Template used to export the body of code blocks.
  283. This template may be customized to include additional information
  284. such as the code block name, or the values of particular header
  285. arguments. The template is filled out using `org-fill-template',
  286. and the following %keys may be used.
  287. lang ------ the language of the code block
  288. name ------ the name of the code block
  289. body ------ the body of the code block
  290. switches -- the switches associated to the code block
  291. flags ----- the flags passed to the code block
  292. In addition to the keys mentioned above, every header argument
  293. defined for the code block may be used as a key and will be
  294. replaced with its value."
  295. :group 'org-babel
  296. :type 'string)
  297. (defcustom org-babel-exp-inline-code-template
  298. "src_%lang[%switches%flags]{%body}"
  299. "Template used to export the body of inline code blocks.
  300. This template may be customized to include additional information
  301. such as the code block name, or the values of particular header
  302. arguments. The template is filled out using `org-fill-template',
  303. and the following %keys may be used.
  304. lang ------ the language of the code block
  305. name ------ the name of the code block
  306. body ------ the body of the code block
  307. switches -- the switches associated to the code block
  308. flags ----- the flags passed to the code block
  309. In addition to the keys mentioned above, every header argument
  310. defined for the code block may be used as a key and will be
  311. replaced with its value."
  312. :group 'org-babel
  313. :type 'string
  314. :version "26.1"
  315. :package-version '(Org . "8.3"))
  316. (defun org-babel-exp-code (info type)
  317. "Return the original code block formatted for export."
  318. (setf (nth 1 info)
  319. (if (string= "strip-export" (cdr (assq :noweb (nth 2 info))))
  320. (replace-regexp-in-string
  321. (org-babel-noweb-wrap) "" (nth 1 info))
  322. (if (org-babel-noweb-p (nth 2 info) :export)
  323. (org-babel-expand-noweb-references
  324. info org-babel-exp-reference-buffer)
  325. (nth 1 info))))
  326. (org-fill-template
  327. (if (eq type 'inline)
  328. org-babel-exp-inline-code-template
  329. org-babel-exp-code-template)
  330. `(("lang" . ,(nth 0 info))
  331. ("body" . ,(org-escape-code-in-string (nth 1 info)))
  332. ("switches" . ,(let ((f (nth 3 info)))
  333. (and (org-string-nw-p f) (concat " " f))))
  334. ("flags" . ,(let ((f (assq :flags (nth 2 info))))
  335. (and f (concat " " (cdr f)))))
  336. ,@(mapcar (lambda (pair)
  337. (cons (substring (symbol-name (car pair)) 1)
  338. (format "%S" (cdr pair))))
  339. (nth 2 info))
  340. ("name" . ,(or (nth 4 info) "")))))
  341. (defun org-babel-exp-results (info type &optional silent hash)
  342. "Evaluate and return the results of the current code block for export.
  343. Results are prepared in a manner suitable for export by Org mode.
  344. This function is called by `org-babel-exp-do-export'. The code
  345. block will be evaluated. Optional argument SILENT can be used to
  346. inhibit insertion of results into the buffer."
  347. (unless (and hash (equal hash (org-babel-current-result-hash)))
  348. (let ((lang (nth 0 info))
  349. (body (if (org-babel-noweb-p (nth 2 info) :eval)
  350. (org-babel-expand-noweb-references
  351. info org-babel-exp-reference-buffer)
  352. (nth 1 info)))
  353. (info (copy-sequence info))
  354. (org-babel-current-src-block-location (point-marker)))
  355. ;; Skip code blocks which we can't evaluate.
  356. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  357. (org-babel-eval-wipe-error-buffer)
  358. (setf (nth 1 info) body)
  359. (setf (nth 2 info)
  360. (org-babel-exp--at-source
  361. (org-babel-process-params
  362. (org-babel-merge-params
  363. (nth 2 info)
  364. `((:results . ,(if silent "silent" "replace")))))))
  365. (pcase type
  366. (`block (org-babel-execute-src-block nil info))
  367. (`inline
  368. ;; Position the point on the inline source block
  369. ;; allowing `org-babel-insert-result' to check that the
  370. ;; block is inline.
  371. (goto-char (nth 5 info))
  372. (org-babel-execute-src-block nil info))
  373. (`lob
  374. (save-excursion
  375. (goto-char (nth 5 info))
  376. (org-babel-execute-src-block nil info))))))))
  377. (provide 'ob-exp)
  378. ;;; ob-exp.el ends here