ob-exp.el 15 KB

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