ob-exp.el 16 KB

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