ob-exp.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. ;;; ob-exp.el --- Exportation of org-babel source blocks
  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. (eval-when-compile
  21. (require 'cl))
  22. (declare-function org-babel-lob-get-info "ob-lob" (&optional datum))
  23. (declare-function org-element-at-point "org-element" ())
  24. (declare-function org-element-context "org-element" (&optional element))
  25. (declare-function org-element-property "org-element" (property element))
  26. (declare-function org-element-type "org-element" (element))
  27. (declare-function org-export-copy-buffer "ox" ())
  28. (declare-function org-fill-template "org" (template alist))
  29. (declare-function org-get-indentation "org" (&optional line))
  30. (declare-function org-heading-components "org" ())
  31. (declare-function org-in-commented-heading-p "org" (&optional no-inheritance))
  32. (defvar org-src-preserve-indentation)
  33. (defcustom org-export-babel-evaluate t
  34. "Switch controlling code evaluation during export.
  35. When set to nil no code will be evaluated as part of the export
  36. process. When set to `inline-only', only inline code blocks will
  37. be executed."
  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 (lambda (x) (eq x nil)))
  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) (eval lang-headers))
  88. (append (org-babel-params-from-properties lang)
  89. (list raw-params)))))))
  90. (setf hash (org-babel-sha1-hash info)))
  91. (org-babel-exp-do-export info 'block hash)))))
  92. (defcustom org-babel-exp-call-line-template
  93. ""
  94. "Template used to export call lines.
  95. This template may be customized to include the call line name
  96. with any export markup. The template is filled out using
  97. `org-fill-template', and the following %keys may be used.
  98. line --- call line
  99. An example value would be \"\\n: call: %line\" to export the call line
  100. wrapped in a verbatim environment.
  101. Note: the results are inserted separately after the contents of
  102. this template."
  103. :group 'org-babel
  104. :type 'string)
  105. (defun org-babel-exp-process-buffer ()
  106. "Execute all Babel blocks in current buffer."
  107. (interactive)
  108. (when org-export-babel-evaluate
  109. (save-window-excursion
  110. (let ((case-fold-search t)
  111. (regexp (if (eq org-export-babel-evaluate 'inline-only)
  112. "\\(call\\|src\\)_"
  113. "\\(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 (org-in-commented-heading-p))
  139. (let* ((element (save-match-data (org-element-context)))
  140. (type (org-element-type element))
  141. (begin
  142. (copy-marker (org-element-property :begin element)))
  143. (end
  144. (copy-marker
  145. (save-excursion
  146. (goto-char (org-element-property :end element))
  147. (skip-chars-backward " \r\t\n")
  148. (point)))))
  149. (case type
  150. (inline-src-block
  151. (let* ((info
  152. (org-babel-get-src-block-info nil element))
  153. (params (nth 2 info)))
  154. (setf (nth 1 info)
  155. (if (and (cdr (assoc :noweb params))
  156. (string= "yes"
  157. (cdr (assq :noweb params))))
  158. (org-babel-expand-noweb-references
  159. info org-babel-exp-reference-buffer)
  160. (nth 1 info)))
  161. (goto-char begin)
  162. (let ((replacement
  163. (org-babel-exp-do-export info 'inline)))
  164. (if (equal replacement "")
  165. ;; Replacement code is empty: remove
  166. ;; inline source block, including extra
  167. ;; white space that might have been
  168. ;; created when inserting results.
  169. (delete-region begin
  170. (progn (goto-char end)
  171. (skip-chars-forward " \t")
  172. (point)))
  173. ;; Otherwise: remove inline src block but
  174. ;; preserve following white spaces. Then
  175. ;; insert value.
  176. (delete-region begin end)
  177. (insert replacement)))))
  178. ((babel-call inline-babel-call)
  179. (let ((results (org-babel-exp-do-export
  180. (org-babel-lob-get-info element)
  181. 'lob))
  182. (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. (lang
  210. (or (org-element-property :language element)
  211. (user-error
  212. "No language for src block: %s"
  213. (or (org-element-property :name element)
  214. "(unnamed)")))))
  215. ;; Take care of matched block: compute
  216. ;; replacement string. In particular, a nil
  217. ;; REPLACEMENT means the block is left as-is
  218. ;; while an empty string removes the block.
  219. (let ((replacement
  220. (progn (goto-char match-start)
  221. (org-babel-exp-src-block))))
  222. (cond ((not replacement) (goto-char end))
  223. ((equal replacement "")
  224. (goto-char end)
  225. (skip-chars-forward " \r\t\n")
  226. (beginning-of-line)
  227. (delete-region begin (point)))
  228. (t
  229. (goto-char match-start)
  230. (delete-region (point)
  231. (save-excursion
  232. (goto-char end)
  233. (line-end-position)))
  234. (insert replacement)
  235. (if (or org-src-preserve-indentation
  236. (org-element-property
  237. :preserve-indent element))
  238. ;; Indent only code block
  239. ;; markers.
  240. (save-excursion
  241. (skip-chars-backward " \r\t\n")
  242. (indent-line-to ind)
  243. (goto-char match-start)
  244. (indent-line-to ind))
  245. ;; Indent everything.
  246. (indent-rigidly
  247. match-start (point) ind)))))
  248. (set-marker match-start nil))))
  249. (set-marker begin nil)
  250. (set-marker end nil)))))
  251. (kill-buffer org-babel-exp-reference-buffer)
  252. (remove-text-properties (point-min) (point-max) '(org-reference)))))))
  253. (defun org-babel-exp-do-export (info type &optional hash)
  254. "Return a string with the exported content of a code block.
  255. The function respects the value of the :exports header argument."
  256. (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
  257. (unless (equal "none" session)
  258. (org-babel-exp-results info type 'silent)))))
  259. (clean (lambda () (if (eq type 'inline)
  260. (org-babel-remove-inline-result)
  261. (org-babel-remove-result info)))))
  262. (pcase (or (cdr (assq :exports (nth 2 info))) "code")
  263. ("none" (funcall silently) (funcall clean) "")
  264. ("code" (funcall silently) (funcall clean) (org-babel-exp-code info type))
  265. ("results" (org-babel-exp-results info type nil hash) "")
  266. ("both"
  267. (org-babel-exp-results info type nil hash)
  268. (org-babel-exp-code info type)))))
  269. (defcustom org-babel-exp-code-template
  270. "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
  271. "Template used to export the body of code blocks.
  272. This template may be customized to include additional information
  273. such as the code block name, or the values of particular header
  274. arguments. The template is filled out using `org-fill-template',
  275. and the following %keys may be used.
  276. lang ------ the language of the code block
  277. name ------ the name of the code block
  278. body ------ the body of the code block
  279. switches -- the switches associated to the code block
  280. flags ----- the flags passed to the code block
  281. In addition to the keys mentioned above, every header argument
  282. defined for the code block may be used as a key and will be
  283. replaced with its value."
  284. :group 'org-babel
  285. :type 'string)
  286. (defcustom org-babel-exp-inline-code-template
  287. "src_%lang[%switches%flags]{%body}"
  288. "Template used to export the body of inline code blocks.
  289. This template may be customized to include additional information
  290. such as the code block name, or the values of particular header
  291. arguments. The template is filled out using `org-fill-template',
  292. and the following %keys may be used.
  293. lang ------ the language of the code block
  294. name ------ the name of the code block
  295. body ------ the body of the code block
  296. switches -- the switches associated to the code block
  297. flags ----- the flags passed to the code block
  298. In addition to the keys mentioned above, every header argument
  299. defined for the code block may be used as a key and will be
  300. replaced with its value."
  301. :group 'org-babel
  302. :type 'string
  303. :version "25.1"
  304. :package-version '(Org . "8.3"))
  305. (defun org-babel-exp-code (info type)
  306. "Return the original code block formatted for export."
  307. (setf (nth 1 info)
  308. (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
  309. (replace-regexp-in-string
  310. (org-babel-noweb-wrap) "" (nth 1 info))
  311. (if (org-babel-noweb-p (nth 2 info) :export)
  312. (org-babel-expand-noweb-references
  313. info org-babel-exp-reference-buffer)
  314. (nth 1 info))))
  315. (org-fill-template
  316. (if (eq type 'inline)
  317. org-babel-exp-inline-code-template
  318. org-babel-exp-code-template)
  319. `(("lang" . ,(nth 0 info))
  320. ("body" . ,(nth 1 info))
  321. ("switches" . ,(let ((f (nth 3 info)))
  322. (and (org-string-nw-p f) (concat " " f))))
  323. ("flags" . ,(let ((f (assq :flags (nth 2 info))))
  324. (and f (concat " " (cdr f)))))
  325. ,@(mapcar (lambda (pair)
  326. (cons (substring (symbol-name (car pair)) 1)
  327. (format "%S" (cdr pair))))
  328. (nth 2 info))
  329. ("name" . ,(or (nth 4 info) "")))))
  330. (defun org-babel-exp-results (info type &optional silent hash)
  331. "Evaluate and return the results of the current code block for export.
  332. Results are prepared in a manner suitable for export by Org mode.
  333. This function is called by `org-babel-exp-do-export'. The code
  334. block will be evaluated. Optional argument SILENT can be used to
  335. inhibit insertion of results into the buffer."
  336. (unless (and hash (equal hash (org-babel-current-result-hash)))
  337. (let ((lang (nth 0 info))
  338. (body (if (org-babel-noweb-p (nth 2 info) :eval)
  339. (org-babel-expand-noweb-references
  340. info org-babel-exp-reference-buffer)
  341. (nth 1 info)))
  342. (info (copy-sequence info))
  343. (org-babel-current-src-block-location (point-marker)))
  344. ;; Skip code blocks which we can't evaluate.
  345. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  346. (org-babel-eval-wipe-error-buffer)
  347. (setf (nth 1 info) body)
  348. (setf (nth 2 info)
  349. (org-babel-exp--at-source
  350. (org-babel-process-params
  351. (org-babel-merge-params
  352. (nth 2 info)
  353. `((:results . ,(if silent "silent" "replace")))))))
  354. (pcase type
  355. (`block (org-babel-execute-src-block nil info))
  356. (`inline
  357. ;; Position the point on the inline source block
  358. ;; allowing `org-babel-insert-result' to check that the
  359. ;; block is inline.
  360. (goto-char (nth 5 info))
  361. (org-babel-execute-src-block nil info))
  362. (`lob
  363. (save-excursion
  364. (goto-char (nth 5 info))
  365. (let (org-confirm-babel-evaluate)
  366. (org-babel-execute-src-block nil info)))))))))
  367. (provide 'ob-exp)
  368. ;;; ob-exp.el ends here