ob-exp.el 15 KB

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