ob-exp.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. (start (point-min)))
  203. (goto-char start)
  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* ((block-start (copy-marker (match-beginning 0)))
  208. (match-start (copy-marker
  209. (org-element-property :begin element)))
  210. ;; Make sure we don't remove any blank lines after
  211. ;; the block when replacing it.
  212. (match-end (save-excursion
  213. (goto-char (org-element-property :end element))
  214. (skip-chars-backward " \r\t\n")
  215. (copy-marker (line-end-position))))
  216. (indentation (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 (or org-src-preserve-indentation
  223. (org-element-property :preserve-indent
  224. element))))
  225. ;; Execute all non-block elements between START and
  226. ;; MATCH-START.
  227. (org-babel-exp-non-block-elements start match-start)
  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 block-start)
  233. (org-babel-exp-src-block headers))))
  234. (cond ((not replacement) (goto-char match-end))
  235. ((equal replacement "")
  236. (delete-region (org-element-property :begin element)
  237. (org-element-property :end element)))
  238. (t
  239. (goto-char match-start)
  240. (delete-region (point) match-end)
  241. (insert replacement)
  242. (if preserve-indent
  243. ;; Indent only the code block markers.
  244. (save-excursion (skip-chars-backward " \r\t\n")
  245. (indent-line-to indentation)
  246. (goto-char match-start)
  247. (indent-line-to indentation))
  248. ;; Indent everything.
  249. (indent-code-rigidly match-start (point) indentation)))))
  250. (setq start (point))
  251. ;; Cleanup markers.
  252. (set-marker block-start nil)
  253. (set-marker match-start nil)
  254. (set-marker match-end nil)))))
  255. ;; Eventually execute all non-block Babel elements between last
  256. ;; src-block and end of buffer.
  257. (org-babel-exp-non-block-elements start (point-max)))))
  258. (defun org-babel-in-example-or-verbatim ()
  259. "Return true if point is in example or verbatim code.
  260. Example and verbatim code include escaped portions of
  261. an org-mode buffer code that should be treated as normal
  262. org-mode text."
  263. (or (save-match-data
  264. (save-excursion
  265. (goto-char (point-at-bol))
  266. (looking-at "[ \t]*:[ \t]")))
  267. (org-in-verbatim-emphasis)
  268. (org-in-block-p org-list-forbidden-blocks)
  269. (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  270. (defun org-babel-exp-do-export (info type &optional hash)
  271. "Return a string with the exported content of a code block.
  272. The function respects the value of the :exports header argument."
  273. (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
  274. (when (not (and session (equal "none" session)))
  275. (org-babel-exp-results info type 'silent)))))
  276. (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info)))))
  277. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  278. ('none (funcall silently) (funcall clean) "")
  279. ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
  280. ('results (org-babel-exp-results info type nil hash) "")
  281. ('both (org-babel-exp-results info type nil hash)
  282. (org-babel-exp-code info)))))
  283. (defcustom org-babel-exp-code-template
  284. "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
  285. "Template used to export the body of code blocks.
  286. This template may be customized to include additional information
  287. such as the code block name, or the values of particular header
  288. arguments. The template is filled out using `org-fill-template',
  289. and the following %keys may be used.
  290. lang ------ the language of the code block
  291. name ------ the name of the code block
  292. body ------ the body of the code block
  293. flags ----- the flags passed to the code block
  294. In addition to the keys mentioned above, every header argument
  295. defined for the code block may be used as a key and will be
  296. replaced with its value."
  297. :group 'org-babel
  298. :type 'string)
  299. (defun org-babel-exp-code (info)
  300. "Return the original code block formatted for export."
  301. (setf (nth 1 info)
  302. (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
  303. (replace-regexp-in-string
  304. (org-babel-noweb-wrap) "" (nth 1 info))
  305. (if (org-babel-noweb-p (nth 2 info) :export)
  306. (org-babel-expand-noweb-references
  307. info (org-babel-exp-get-export-buffer))
  308. (nth 1 info))))
  309. (org-fill-template
  310. org-babel-exp-code-template
  311. `(("lang" . ,(nth 0 info))
  312. ("body" . ,(if (string= (nth 0 info) "org")
  313. (replace-regexp-in-string "^" "," (nth 1 info))
  314. (nth 1 info)))
  315. ,@(mapcar (lambda (pair)
  316. (cons (substring (symbol-name (car pair)) 1)
  317. (format "%S" (cdr pair))))
  318. (nth 2 info))
  319. ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
  320. ("name" . ,(or (nth 4 info) "")))))
  321. (defun org-babel-exp-results (info type &optional silent hash)
  322. "Evaluate and return the results of the current code block for export.
  323. Results are prepared in a manner suitable for export by org-mode.
  324. This function is called by `org-babel-exp-do-export'. The code
  325. block will be evaluated. Optional argument SILENT can be used to
  326. inhibit insertion of results into the buffer."
  327. (when (and org-export-babel-evaluate
  328. (not (and hash (equal hash (org-babel-current-result-hash)))))
  329. (let ((lang (nth 0 info))
  330. (body (if (org-babel-noweb-p (nth 2 info) :eval)
  331. (org-babel-expand-noweb-references
  332. info (org-babel-exp-get-export-buffer))
  333. (nth 1 info)))
  334. (info (copy-sequence info)))
  335. ;; skip code blocks which we can't evaluate
  336. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  337. (org-babel-eval-wipe-error-buffer)
  338. (prog1 nil
  339. (setf (nth 1 info) body)
  340. (setf (nth 2 info)
  341. (org-babel-exp-in-export-file lang
  342. (org-babel-process-params
  343. (org-babel-merge-params
  344. (nth 2 info)
  345. `((:results . ,(if silent "silent" "replace")))))))
  346. (cond
  347. ((equal type 'block)
  348. (org-babel-execute-src-block nil info))
  349. ((equal type 'inline)
  350. ;; position the point on the inline source block allowing
  351. ;; `org-babel-insert-result' to check that the block is
  352. ;; inline
  353. (re-search-backward "[ \f\t\n\r\v]" nil t)
  354. (re-search-forward org-babel-inline-src-block-regexp nil t)
  355. (re-search-backward "src_" nil t)
  356. (org-babel-execute-src-block nil info))
  357. ((equal type 'lob)
  358. (save-excursion
  359. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  360. (org-babel-execute-src-block nil info)))))))))
  361. (provide 'ob-exp)
  362. ;;; ob-exp.el ends here