ob-exp.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. ;;; ob-exp.el --- Exportation of org-babel source blocks
  2. ;; Copyright (C) 2009-2012 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)
  20. (eval-when-compile
  21. (require 'cl))
  22. (defvar obe-marker nil)
  23. (defvar org-current-export-file)
  24. (defvar org-babel-lob-one-liner-regexp)
  25. (defvar org-babel-ref-split-regexp)
  26. (defvar org-list-forbidden-blocks)
  27. (declare-function org-babel-lob-get-info "ob-lob" ())
  28. (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
  29. (declare-function org-between-regexps-p "org"
  30. (start-re end-re &optional lim-up lim-down))
  31. (declare-function org-get-indentation "org" (&optional line))
  32. (declare-function org-heading-components "org" ())
  33. (declare-function org-in-block-p "org" (names))
  34. (declare-function org-in-verbatim-emphasis "org" ())
  35. (declare-function org-link-search "org" (s &optional type avoid-pos stealth))
  36. (declare-function org-fill-template "org" (template alist))
  37. (declare-function org-split-string "org" (string &optional separators))
  38. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  39. (declare-function org-element-context "org-element" ())
  40. (declare-function org-element-property "org-element" (property element))
  41. (declare-function org-element-type "org-element" (element))
  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."
  46. :group 'org-babel
  47. :version "24.1"
  48. :type 'boolean)
  49. (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
  50. (defun org-babel-exp-get-export-buffer ()
  51. "Return the current export buffer if possible."
  52. (cond
  53. ((bufferp org-current-export-file) org-current-export-file)
  54. (org-current-export-file (get-file-buffer org-current-export-file))
  55. ('otherwise
  56. (error "Requested export buffer when `org-current-export-file' is nil"))))
  57. (defmacro org-babel-exp-in-export-file (lang &rest body)
  58. (declare (indent 1))
  59. `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
  60. (heading (nth 4 (ignore-errors (org-heading-components))))
  61. (export-buffer (current-buffer))
  62. (original-buffer (org-babel-exp-get-export-buffer)) results)
  63. (when original-buffer
  64. ;; resolve parameters in the original file so that
  65. ;; headline and file-wide parameters are included, attempt
  66. ;; to go to the same heading in the original file
  67. (set-buffer original-buffer)
  68. (save-restriction
  69. (when heading
  70. (condition-case nil
  71. (let ((org-link-search-inhibit-query t))
  72. (org-link-search heading))
  73. (error (when heading
  74. (goto-char (point-min))
  75. (re-search-forward (regexp-quote heading) 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 in replace the source
  83. code block with...
  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 it's 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. (unless noninteractive (message "org-babel-exp processing..."))
  93. (save-excursion
  94. (let* ((info (org-babel-get-src-block-info 'light))
  95. (lang (nth 0 info))
  96. (raw-params (nth 2 info)) hash)
  97. ;; bail if we couldn't get any info from the block
  98. (when info
  99. ;; if we're actually going to need the parameters
  100. (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
  101. (org-babel-exp-in-export-file lang
  102. (setf (nth 2 info)
  103. (org-babel-process-params
  104. (org-babel-merge-params
  105. org-babel-default-header-args
  106. (org-babel-params-from-properties lang)
  107. (if (boundp lang-headers) (eval lang-headers) nil)
  108. raw-params))))
  109. (setf hash (org-babel-sha1-hash info)))
  110. (org-babel-exp-do-export info 'block hash)))))
  111. (defcustom org-babel-exp-call-line-template
  112. ""
  113. "Template used to export call lines.
  114. This template may be customized to include the call line name
  115. with any export markup. The template is filled out using
  116. `org-fill-template', and the following %keys may be used.
  117. line --- call line
  118. An example value would be \"\\n: call: %line\" to export the call line
  119. wrapped in a verbatim environment.
  120. Note: the results are inserted separately after the contents of
  121. this template."
  122. :group 'org-babel
  123. :type 'string)
  124. (defvar org-babel-default-lob-header-args)
  125. (defun org-babel-exp-non-block-elements (start end)
  126. "Process inline source and call lines between START and END for export."
  127. (interactive)
  128. (save-excursion
  129. (goto-char start)
  130. (unless (markerp end)
  131. (let ((m (make-marker)))
  132. (set-marker m end (current-buffer))
  133. (setq end m)))
  134. (let ((rx (concat "\\(?:" org-babel-inline-src-block-regexp
  135. "\\|" org-babel-lob-one-liner-regexp "\\)")))
  136. (while (re-search-forward rx end t)
  137. (let* ((element (save-match-data (org-element-context)))
  138. (type (org-element-type element)))
  139. (cond
  140. ((not (memq type '(babel-call inline-babel-call inline-src-block))))
  141. ((eq type 'inline-src-block)
  142. (let* ((beg (org-element-property :begin element))
  143. (end (save-excursion
  144. (goto-char (org-element-property :end element))
  145. (skip-chars-forward " \t")
  146. (point)))
  147. (info (org-babel-parse-inline-src-block-match))
  148. (params (nth 2 info)))
  149. ;; Expand noweb references in the original file.
  150. (setf (nth 1 info)
  151. (if (and (cdr (assoc :noweb params))
  152. (string= "yes" (cdr (assoc :noweb params))))
  153. (org-babel-expand-noweb-references
  154. info (org-babel-exp-get-export-buffer))
  155. (nth 1 info)))
  156. (let ((code-replacement
  157. (save-match-data (org-babel-exp-do-export info 'inline))))
  158. (if code-replacement
  159. (progn
  160. (delete-region
  161. (progn (goto-char beg)
  162. (skip-chars-backward " \t")
  163. (point))
  164. end)
  165. (insert code-replacement))
  166. (org-babel-examplize-region beg end)
  167. (forward-char 2)))))
  168. (t (let* ((lob-info (org-babel-lob-get-info))
  169. (inlinep (match-string 11))
  170. (inline-start (match-end 11))
  171. (inline-end (match-end 0))
  172. (results (save-match-data
  173. (org-babel-exp-do-export
  174. (list "emacs-lisp" "results"
  175. (org-babel-merge-params
  176. org-babel-default-header-args
  177. org-babel-default-lob-header-args
  178. (org-babel-params-from-properties)
  179. (org-babel-parse-header-arguments
  180. (org-no-properties
  181. (concat ":var results="
  182. (mapconcat #'identity
  183. (butlast lob-info)
  184. " ")))))
  185. "" nil (car (last lob-info)))
  186. 'lob)))
  187. (rep (org-fill-template
  188. org-babel-exp-call-line-template
  189. `(("line" . ,(nth 0 lob-info))))))
  190. (if inlinep
  191. (save-excursion
  192. (goto-char inline-start)
  193. (delete-region inline-start inline-end)
  194. (insert rep))
  195. (replace-match rep t t))))))))))
  196. (defvar org-src-preserve-indentation) ; From org-src.el
  197. (defun org-export-blocks-preprocess ()
  198. "Execute all blocks in visible part of buffer."
  199. (interactive)
  200. (save-window-excursion
  201. (let ((case-fold-search t)
  202. (pos (point-min)))
  203. (goto-char pos)
  204. (while (re-search-forward "^[ \t]*#\\+BEGIN_SRC" nil t)
  205. (let ((element (save-match-data (org-element-at-point))))
  206. (when (eq (org-element-type element) 'src-block)
  207. (let* ((match-start (copy-marker (match-beginning 0)))
  208. (begin (copy-marker (org-element-property :begin element)))
  209. (end (copy-marker (org-element-property :end element)))
  210. ;; Make sure we don't remove any blank lines after
  211. ;; the block when replacing it.
  212. (block-end (save-excursion
  213. (goto-char end)
  214. (skip-chars-backward " \r\t\n")
  215. (copy-marker (line-end-position))))
  216. (ind (org-get-indentation))
  217. (headers
  218. (cons
  219. (org-element-property :language element)
  220. (let ((params (org-element-property :parameters element)))
  221. (and params (org-split-string params "[ \t]+")))))
  222. (preserve-indent
  223. (or org-src-preserve-indentation
  224. (org-element-property :preserve-indent element))))
  225. ;; Execute all non-block elements between POS and
  226. ;; current block.
  227. (org-babel-exp-non-block-elements pos begin)
  228. ;; Take care of matched block: compute replacement
  229. ;; string. In particular, a nil REPLACEMENT means the
  230. ;; block should be left as-is while an empty string
  231. ;; should remove the block.
  232. (let ((replacement (progn (goto-char match-start)
  233. (org-babel-exp-src-block headers))))
  234. (cond ((not replacement) (goto-char end))
  235. ((equal replacement "") (delete-region begin end))
  236. (t
  237. (goto-char match-start)
  238. (delete-region (point) block-end)
  239. (insert replacement)
  240. (if preserve-indent
  241. ;; Indent only the code block markers.
  242. (save-excursion (skip-chars-backward " \r\t\n")
  243. (indent-line-to ind)
  244. (goto-char match-start)
  245. (indent-line-to ind))
  246. ;; Indent everything.
  247. (indent-code-rigidly match-start (point) ind)))))
  248. (setq pos (point))
  249. ;; Cleanup markers.
  250. (set-marker match-start nil)
  251. (set-marker begin nil)
  252. (set-marker end nil)
  253. (set-marker block-end nil)))))
  254. ;; Eventually execute all non-block Babel elements between last
  255. ;; src-block and end of buffer.
  256. (org-babel-exp-non-block-elements pos (point-max)))))
  257. (defun org-babel-in-example-or-verbatim ()
  258. "Return true if point is in example or verbatim code.
  259. Example and verbatim code include escaped portions of
  260. an org-mode buffer code that should be treated as normal
  261. org-mode text."
  262. (or (save-match-data
  263. (save-excursion
  264. (goto-char (point-at-bol))
  265. (looking-at "[ \t]*:[ \t]")))
  266. (org-in-verbatim-emphasis)
  267. (org-in-block-p org-list-forbidden-blocks)
  268. (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  269. (defun org-babel-exp-do-export (info type &optional hash)
  270. "Return a string with the exported content of a code block.
  271. The function respects the value of the :exports header argument."
  272. (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
  273. (when (not (and session (equal "none" session)))
  274. (org-babel-exp-results info type 'silent)))))
  275. (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info)))))
  276. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  277. ('none (funcall silently) (funcall clean) "")
  278. ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
  279. ('results (org-babel-exp-results info type nil hash) "")
  280. ('both (org-babel-exp-results info type nil hash)
  281. (org-babel-exp-code info)))))
  282. (defcustom org-babel-exp-code-template
  283. "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
  284. "Template used to export the body of code blocks.
  285. This template may be customized to include additional information
  286. such as the code block name, or the values of particular header
  287. arguments. The template is filled out using `org-fill-template',
  288. and the following %keys may be used.
  289. lang ------ the language of the code block
  290. name ------ the name of the code block
  291. body ------ the body of 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. (defun org-babel-exp-code (info)
  299. "Return the original code block formatted for export."
  300. (setf (nth 1 info)
  301. (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
  302. (replace-regexp-in-string
  303. (org-babel-noweb-wrap) "" (nth 1 info))
  304. (if (org-babel-noweb-p (nth 2 info) :export)
  305. (org-babel-expand-noweb-references
  306. info (org-babel-exp-get-export-buffer))
  307. (nth 1 info))))
  308. (org-fill-template
  309. org-babel-exp-code-template
  310. `(("lang" . ,(nth 0 info))
  311. ("body" . ,(if (string= (nth 0 info) "org")
  312. (replace-regexp-in-string "^" "," (nth 1 info))
  313. (nth 1 info)))
  314. ,@(mapcar (lambda (pair)
  315. (cons (substring (symbol-name (car pair)) 1)
  316. (format "%S" (cdr pair))))
  317. (nth 2 info))
  318. ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
  319. ("name" . ,(or (nth 4 info) "")))))
  320. (defun org-babel-exp-results (info type &optional silent hash)
  321. "Evaluate and return the results of the current code block for export.
  322. Results are prepared in a manner suitable for export by org-mode.
  323. This function is called by `org-babel-exp-do-export'. The code
  324. block will be evaluated. Optional argument SILENT can be used to
  325. inhibit insertion of results into the buffer."
  326. (when (and org-export-babel-evaluate
  327. (not (and hash (equal hash (org-babel-current-result-hash)))))
  328. (let ((lang (nth 0 info))
  329. (body (if (org-babel-noweb-p (nth 2 info) :eval)
  330. (org-babel-expand-noweb-references
  331. info (org-babel-exp-get-export-buffer))
  332. (nth 1 info)))
  333. (info (copy-sequence info)))
  334. ;; skip code blocks which we can't evaluate
  335. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  336. (org-babel-eval-wipe-error-buffer)
  337. (prog1 nil
  338. (setf (nth 1 info) body)
  339. (setf (nth 2 info)
  340. (org-babel-exp-in-export-file lang
  341. (org-babel-process-params
  342. (org-babel-merge-params
  343. (nth 2 info)
  344. `((:results . ,(if silent "silent" "replace")))))))
  345. (cond
  346. ((equal type 'block)
  347. (org-babel-execute-src-block nil info))
  348. ((equal type 'inline)
  349. ;; position the point on the inline source block allowing
  350. ;; `org-babel-insert-result' to check that the block is
  351. ;; inline
  352. (re-search-backward "[ \f\t\n\r\v]" nil t)
  353. (re-search-forward org-babel-inline-src-block-regexp nil t)
  354. (re-search-backward "src_" nil t)
  355. (org-babel-execute-src-block nil info))
  356. ((equal type 'lob)
  357. (save-excursion
  358. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  359. (org-babel-execute-src-block nil info)))))))))
  360. (provide 'ob-exp)
  361. ;;; ob-exp.el ends here