ob-exp.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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-id-get "org-id" (&optional pom create prefix))
  32. (declare-function org-in-commented-heading-p "org" (&optional no-inheritance))
  33. (declare-function org-link-search "org" (s &optional avoid-pos stealth))
  34. (declare-function org-split-string "org" (string &optional separators))
  35. (defvar org-src-preserve-indentation)
  36. (defcustom org-export-babel-evaluate t
  37. "Switch controlling code evaluation during export.
  38. When set to nil no code will be evaluated as part of the export
  39. process. When set to `inline-only', only inline code blocks will
  40. be executed."
  41. :group 'org-babel
  42. :version "24.1"
  43. :type '(choice (const :tag "Never" nil)
  44. (const :tag "Only inline code" inline-only)
  45. (const :tag "Always" t)))
  46. (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
  47. (defvar org-link-search-inhibit-query)
  48. (defmacro org-babel-exp-in-export-file (lang &rest body)
  49. (declare (indent 1))
  50. `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
  51. (heading-query (or (org-id-get)
  52. ;; CUSTOM_IDs don't work, maybe they are
  53. ;; stripped, or maybe they resolve too
  54. ;; late in `org-link-search'.
  55. ;; (org-entry-get nil "CUSTOM_ID")
  56. (nth 4 (ignore-errors (org-heading-components)))))
  57. (export-buffer (current-buffer))
  58. results)
  59. (when org-babel-exp-reference-buffer
  60. ;; Resolve parameters in the original file so that headline and
  61. ;; file-wide parameters are included, attempt to go to the same
  62. ;; heading in the original file
  63. (set-buffer org-babel-exp-reference-buffer)
  64. (save-restriction
  65. (when heading-query
  66. (condition-case nil
  67. (let ((org-link-search-inhibit-query t))
  68. ;; TODO: When multiple headings have the same title,
  69. ;; this returns the first, which is not always
  70. ;; the right heading. Consider a better way to
  71. ;; find the proper heading.
  72. (org-link-search heading-query))
  73. (error (when heading-query
  74. (goto-char (point-min))
  75. (re-search-forward (regexp-quote heading-query) nil t)))))
  76. (setq results ,@body))
  77. (set-buffer export-buffer)
  78. results)))
  79. (def-edebug-spec org-babel-exp-in-export-file (form body))
  80. (defun org-babel-exp-src-block (&rest headers)
  81. "Process source block for export.
  82. Depending on the `export' headers argument, replace the source
  83. code block like this:
  84. both ---- display the code and the results
  85. code ---- the default, display the code inside the block but do
  86. not process
  87. results - just like none only the block is run on export ensuring
  88. that its results are present in the org-mode buffer
  89. none ---- do not display either code or results upon export
  90. Assume point is at the beginning of block's starting line."
  91. (interactive)
  92. (save-excursion
  93. (let* ((info (org-babel-get-src-block-info 'light))
  94. (lang (nth 0 info))
  95. (raw-params (nth 2 info)) hash)
  96. ;; bail if we couldn't get any info from the block
  97. (unless noninteractive
  98. (message "org-babel-exp process %s at position %d..."
  99. lang (line-beginning-position)))
  100. (when info
  101. ;; if we're actually going to need the parameters
  102. (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
  103. (org-babel-exp-in-export-file lang
  104. (setf (nth 2 info)
  105. (org-babel-process-params
  106. (apply #'org-babel-merge-params
  107. org-babel-default-header-args
  108. (if (boundp lang-headers) (eval lang-headers) nil)
  109. (append (org-babel-params-from-properties lang)
  110. (list raw-params))))))
  111. (setf hash (org-babel-sha1-hash info)))
  112. (org-babel-exp-do-export info 'block hash)))))
  113. (defcustom org-babel-exp-call-line-template
  114. ""
  115. "Template used to export call lines.
  116. This template may be customized to include the call line name
  117. with any export markup. The template is filled out using
  118. `org-fill-template', and the following %keys may be used.
  119. line --- call line
  120. An example value would be \"\\n: call: %line\" to export the call line
  121. wrapped in a verbatim environment.
  122. Note: the results are inserted separately after the contents of
  123. this template."
  124. :group 'org-babel
  125. :type 'string)
  126. (defun org-babel-exp-process-buffer ()
  127. "Execute all Babel blocks in current buffer."
  128. (interactive)
  129. (when org-export-babel-evaluate
  130. (save-window-excursion
  131. (save-excursion
  132. (let ((case-fold-search t)
  133. (regexp
  134. (if (eq org-export-babel-evaluate 'inline-only)
  135. "\\(call\\|src\\)_"
  136. "\\(call\\|src\\)_\\|^[ \t]*#\\+\\(BEGIN_SRC\\|CALL:\\)"))
  137. ;; Get a pristine copy of current buffer so Babel
  138. ;; references are properly resolved and source block
  139. ;; context is preserved.
  140. (org-babel-exp-reference-buffer (org-export-copy-buffer)))
  141. (goto-char (point-min))
  142. (unwind-protect
  143. (while (re-search-forward regexp nil t)
  144. (unless (save-match-data (org-in-commented-heading-p))
  145. (let* ((element (save-match-data (org-element-context)))
  146. (type (org-element-type element))
  147. (begin
  148. (copy-marker (org-element-property :begin element)))
  149. (end
  150. (copy-marker
  151. (save-excursion
  152. (goto-char (org-element-property :end element))
  153. (skip-chars-backward " \r\t\n")
  154. (point)))))
  155. (case type
  156. (inline-src-block
  157. (let* ((info
  158. (org-babel-get-src-block-info nil element))
  159. (params (nth 2 info)))
  160. (setf (nth 1 info)
  161. (if (and (cdr (assoc :noweb params))
  162. (string= "yes"
  163. (cdr (assq :noweb params))))
  164. (org-babel-expand-noweb-references
  165. info org-babel-exp-reference-buffer)
  166. (nth 1 info)))
  167. (goto-char begin)
  168. (let ((replacement
  169. (org-babel-exp-do-export info 'inline)))
  170. (if (equal replacement "")
  171. ;; Replacement code is empty: remove
  172. ;; inline source block, including extra
  173. ;; white space that might have been
  174. ;; created when inserting results.
  175. (delete-region begin
  176. (progn (goto-char end)
  177. (skip-chars-forward " \t")
  178. (point)))
  179. ;; Otherwise: remove inline src block but
  180. ;; preserve following white spaces. Then
  181. ;; insert value.
  182. (delete-region begin end)
  183. (insert replacement)))))
  184. ((babel-call inline-babel-call)
  185. (let ((results (org-babel-exp-do-export
  186. (org-babel-lob-get-info element)
  187. 'lob))
  188. (rep
  189. (org-fill-template
  190. org-babel-exp-call-line-template
  191. `(("line" .
  192. ,(org-element-property :value element))))))
  193. ;; If replacement is empty, completely remove
  194. ;; the object/element, including any extra
  195. ;; white space that might have been created
  196. ;; when including results.
  197. (if (equal rep "")
  198. (delete-region
  199. begin
  200. (progn (goto-char end)
  201. (if (not (eq type 'babel-call))
  202. (progn (skip-chars-forward " \t")
  203. (point))
  204. (skip-chars-forward " \r\t\n")
  205. (line-beginning-position))))
  206. ;; Otherwise, preserve trailing
  207. ;; spaces/newlines and then, insert
  208. ;; replacement string.
  209. (goto-char begin)
  210. (delete-region begin end)
  211. (insert rep))))
  212. (src-block
  213. (let* ((match-start (copy-marker (match-beginning 0)))
  214. (ind (org-get-indentation))
  215. (lang
  216. (or (org-element-property :language element)
  217. (user-error
  218. "No language for src block: %s"
  219. (or (org-element-property :name element)
  220. "(unnamed)"))))
  221. (headers
  222. (cons lang
  223. (let ((params
  224. (org-element-property
  225. :parameters element)))
  226. (and params
  227. (org-split-string params))))))
  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 headers))))
  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. (defun org-babel-exp-do-export (info type &optional hash)
  266. "Return a string with the exported content of a code block.
  267. The function respects the value of the :exports header argument."
  268. (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
  269. (unless (equal "none" session)
  270. (org-babel-exp-results info type 'silent)))))
  271. (clean (lambda () (if (eq type 'inline)
  272. (org-babel-remove-inline-result)
  273. (org-babel-remove-result info)))))
  274. (pcase (or (cdr (assq :exports (nth 2 info))) "code")
  275. ("none" (funcall silently) (funcall clean) "")
  276. ("code" (funcall silently) (funcall clean) (org-babel-exp-code info type))
  277. ("results" (org-babel-exp-results info type nil hash) "")
  278. ("both"
  279. (org-babel-exp-results info type nil hash)
  280. (org-babel-exp-code info type)))))
  281. (defcustom org-babel-exp-code-template
  282. "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
  283. "Template used to export the body of code blocks.
  284. This template may be customized to include additional information
  285. such as the code block name, or the values of particular header
  286. arguments. The template is filled out using `org-fill-template',
  287. and the following %keys may be used.
  288. lang ------ the language of the code block
  289. name ------ the name of the code block
  290. body ------ the body of the code block
  291. switches -- the switches associated to the code block
  292. flags ----- the flags passed to the code block
  293. In addition to the keys mentioned above, every header argument
  294. defined for the code block may be used as a key and will be
  295. replaced with its value."
  296. :group 'org-babel
  297. :type 'string)
  298. (defcustom org-babel-exp-inline-code-template
  299. "src_%lang[%switches%flags]{%body}"
  300. "Template used to export the body of inline code blocks.
  301. This template may be customized to include additional information
  302. such as the code block name, or the values of particular header
  303. arguments. The template is filled out using `org-fill-template',
  304. and the following %keys may be used.
  305. lang ------ the language of the code block
  306. name ------ the name of the code block
  307. body ------ the body of the code block
  308. switches -- the switches associated to the code block
  309. flags ----- the flags passed to the code block
  310. In addition to the keys mentioned above, every header argument
  311. defined for the code block may be used as a key and will be
  312. replaced with its value."
  313. :group 'org-babel
  314. :type 'string
  315. :version "25.1"
  316. :package-version '(Org . "8.3"))
  317. (defun org-babel-exp-code (info type)
  318. "Return the original code block formatted for export."
  319. (setf (nth 1 info)
  320. (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
  321. (replace-regexp-in-string
  322. (org-babel-noweb-wrap) "" (nth 1 info))
  323. (if (org-babel-noweb-p (nth 2 info) :export)
  324. (org-babel-expand-noweb-references
  325. info org-babel-exp-reference-buffer)
  326. (nth 1 info))))
  327. (org-fill-template
  328. (if (eq type 'inline)
  329. org-babel-exp-inline-code-template
  330. org-babel-exp-code-template)
  331. `(("lang" . ,(nth 0 info))
  332. ("body" . ,(nth 1 info))
  333. ("switches" . ,(let ((f (nth 3 info)))
  334. (and (org-string-nw-p f) (concat " " f))))
  335. ("flags" . ,(let ((f (assq :flags (nth 2 info))))
  336. (and f (concat " " (cdr f)))))
  337. ,@(mapcar (lambda (pair)
  338. (cons (substring (symbol-name (car pair)) 1)
  339. (format "%S" (cdr pair))))
  340. (nth 2 info))
  341. ("name" . ,(or (nth 4 info) "")))))
  342. (defun org-babel-exp-results (info type &optional silent hash)
  343. "Evaluate and return the results of the current code block for export.
  344. Results are prepared in a manner suitable for export by Org mode.
  345. This function is called by `org-babel-exp-do-export'. The code
  346. block will be evaluated. Optional argument SILENT can be used to
  347. inhibit insertion of results into the buffer."
  348. (unless (and hash (equal hash (org-babel-current-result-hash)))
  349. (let ((lang (nth 0 info))
  350. (body (if (org-babel-noweb-p (nth 2 info) :eval)
  351. (org-babel-expand-noweb-references
  352. info org-babel-exp-reference-buffer)
  353. (nth 1 info)))
  354. (info (copy-sequence info))
  355. (org-babel-current-src-block-location (point-marker)))
  356. ;; Skip code blocks which we can't evaluate.
  357. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  358. (org-babel-eval-wipe-error-buffer)
  359. (prog1 nil
  360. (setf (nth 1 info) body)
  361. (setf (nth 2 info)
  362. (org-babel-exp-in-export-file lang
  363. (org-babel-process-params
  364. (org-babel-merge-params
  365. (nth 2 info)
  366. `((:results . ,(if silent "silent" "replace")))))))
  367. (pcase type
  368. (`block (org-babel-execute-src-block nil info))
  369. (`inline
  370. ;; Position the point on the inline source block
  371. ;; allowing `org-babel-insert-result' to check that the
  372. ;; block is inline.
  373. (goto-char (nth 5 info))
  374. (org-babel-execute-src-block nil info))
  375. (`lob
  376. (save-excursion
  377. (goto-char (nth 5 info))
  378. (let (org-confirm-babel-evaluate)
  379. (org-babel-execute-src-block nil info))))))))))
  380. (provide 'ob-exp)
  381. ;;; ob-exp.el ends here