ob-exp.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. ;;; ob-exp.el --- Exportation of org-babel source blocks
  2. ;; Copyright (C) 2009-2013 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. (require 'org-exp-blocks)
  21. (eval-when-compile
  22. (require 'cl))
  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-heading-components "org" ())
  30. (declare-function org-link-search "org" (s &optional type avoid-pos stealth))
  31. (declare-function org-fill-template "org" (template alist))
  32. (declare-function org-in-verbatim-emphasis "org" ())
  33. (declare-function org-in-block-p "org" (names))
  34. (declare-function org-between-regexps-p "org" (start-re end-re &optional lim-up lim-down))
  35. (add-to-list 'org-export-interblocks '(src org-babel-exp-non-block-elements))
  36. (org-export-blocks-add-block '(src org-babel-exp-src-block nil))
  37. (defcustom org-export-babel-evaluate t
  38. "Switch controlling code evaluation during export.
  39. When set to nil no code will be evaluated as part of the export
  40. process."
  41. :group 'org-babel
  42. :version "24.1"
  43. :type 'boolean)
  44. (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
  45. (defun org-babel-exp-get-export-buffer ()
  46. "Return the current export buffer if possible."
  47. (cond
  48. ((bufferp org-current-export-file) org-current-export-file)
  49. (org-current-export-file (get-file-buffer org-current-export-file))
  50. ('otherwise
  51. (error "Requested export buffer when `org-current-export-file' is nil"))))
  52. (defmacro org-babel-exp-in-export-file (lang &rest body)
  53. (declare (indent 1))
  54. `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
  55. (heading (nth 4 (ignore-errors (org-heading-components))))
  56. (export-buffer (current-buffer))
  57. (original-buffer (org-babel-exp-get-export-buffer)) results)
  58. (when original-buffer
  59. ;; resolve parameters in the original file so that
  60. ;; headline and file-wide parameters are included, attempt
  61. ;; to go to the same heading in the original file
  62. (set-buffer original-buffer)
  63. (save-restriction
  64. (when heading
  65. (condition-case nil
  66. (let ((org-link-search-inhibit-query t))
  67. (org-link-search heading))
  68. (error (when heading
  69. (goto-char (point-min))
  70. (re-search-forward (regexp-quote heading) nil t)))))
  71. (setq results ,@body))
  72. (set-buffer export-buffer)
  73. results)))
  74. (def-edebug-spec org-babel-exp-in-export-file (form body))
  75. (defun org-babel-exp-src-block (body &rest headers)
  76. "Process source block for export.
  77. Depending on the 'export' headers argument in replace the source
  78. code block with...
  79. both ---- display the code and the results
  80. code ---- the default, display the code inside the block but do
  81. not process
  82. results - just like none only the block is run on export ensuring
  83. that it's results are present in the org-mode buffer
  84. none ----- do not display either code or results upon export"
  85. (interactive)
  86. (unless noninteractive (message "org-babel-exp processing..."))
  87. (save-excursion
  88. (goto-char (match-beginning 0))
  89. (let* ((info (org-babel-get-src-block-info 'light))
  90. (lang (nth 0 info))
  91. (raw-params (nth 2 info)) hash)
  92. ;; bail if we couldn't get any info from the block
  93. (when info
  94. ;; if we're actually going to need the parameters
  95. (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
  96. (org-babel-exp-in-export-file lang
  97. (setf (nth 2 info)
  98. (org-babel-process-params
  99. (org-babel-merge-params
  100. org-babel-default-header-args
  101. (org-babel-params-from-properties lang)
  102. (if (boundp lang-headers) (eval lang-headers) nil)
  103. raw-params))))
  104. (setf hash (org-babel-sha1-hash info)))
  105. (org-babel-exp-do-export info 'block hash)))))
  106. (defcustom org-babel-exp-call-line-template
  107. ""
  108. "Template used to export call lines.
  109. This template may be customized to include the call line name
  110. with any export markup. The template is filled out using
  111. `org-fill-template', and the following %keys may be used.
  112. line --- call line
  113. An example value would be \"\\n: call: %line\" to export the call line
  114. wrapped in a verbatim environment.
  115. Note: the results are inserted separately after the contents of
  116. this template."
  117. :group 'org-babel
  118. :type 'string)
  119. (defvar org-babel-default-lob-header-args)
  120. (defun org-babel-exp-non-block-elements (start end)
  121. "Process inline source and call lines between START and END for export."
  122. (interactive)
  123. (save-excursion
  124. (goto-char start)
  125. (unless (markerp end)
  126. (let ((m (make-marker)))
  127. (set-marker m end (current-buffer))
  128. (setq end m)))
  129. (let ((rx (concat "\\(" org-babel-inline-src-block-regexp
  130. "\\|" org-babel-lob-one-liner-regexp "\\)")))
  131. (while (and (< (point) (marker-position end))
  132. (re-search-forward rx end t))
  133. (if (save-excursion
  134. (goto-char (match-beginning 0))
  135. (looking-at org-babel-inline-src-block-regexp))
  136. (progn
  137. (forward-char 1)
  138. (let* ((info (save-match-data
  139. (org-babel-parse-inline-src-block-match)))
  140. (params (nth 2 info)))
  141. (save-match-data
  142. (goto-char (match-beginning 2))
  143. (unless (org-babel-in-example-or-verbatim)
  144. ;; expand noweb references in the original file
  145. (setf (nth 1 info)
  146. (if (and (cdr (assoc :noweb params))
  147. (string= "yes" (cdr (assoc :noweb params))))
  148. (org-babel-expand-noweb-references
  149. info (org-babel-exp-get-export-buffer))
  150. (nth 1 info)))
  151. (let ((code-replacement (save-match-data
  152. (org-babel-exp-do-export
  153. info 'inline))))
  154. (if code-replacement
  155. (progn (replace-match code-replacement nil nil nil 1)
  156. (delete-char 1))
  157. (org-babel-examplize-region (match-beginning 1)
  158. (match-end 1))
  159. (forward-char 2)))))))
  160. (unless (org-babel-in-example-or-verbatim)
  161. (let* ((lob-info (org-babel-lob-get-info))
  162. (inlinep (match-string 11))
  163. (inline-start (match-end 11))
  164. (inline-end (match-end 0))
  165. (results (save-match-data
  166. (org-babel-exp-do-export
  167. (list "emacs-lisp" "results"
  168. (org-babel-merge-params
  169. org-babel-default-header-args
  170. org-babel-default-lob-header-args
  171. (org-babel-params-from-properties)
  172. (org-babel-parse-header-arguments
  173. (org-no-properties
  174. (concat ":var results="
  175. (mapconcat #'identity
  176. (butlast lob-info)
  177. " ")))))
  178. "" nil (car (last lob-info)))
  179. 'lob)))
  180. (rep (org-fill-template
  181. org-babel-exp-call-line-template
  182. `(("line" . ,(nth 0 lob-info))))))
  183. (if inlinep
  184. (save-excursion
  185. (goto-char inline-start)
  186. (delete-region inline-start inline-end)
  187. (insert rep))
  188. (replace-match rep t t)))))))))
  189. (defun org-babel-in-example-or-verbatim ()
  190. "Return true if point is in example or verbatim code.
  191. Example and verbatim code include escaped portions of
  192. an org-mode buffer code that should be treated as normal
  193. org-mode text."
  194. (or (save-match-data
  195. (save-excursion
  196. (goto-char (point-at-bol))
  197. (looking-at "[ \t]*:[ \t]")))
  198. (org-in-verbatim-emphasis)
  199. (org-in-block-p org-list-forbidden-blocks)
  200. (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  201. (defun org-babel-exp-do-export (info type &optional hash)
  202. "Return a string with the exported content of a code block.
  203. The function respects the value of the :exports header argument."
  204. (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
  205. (when (not (and session (equal "none" session)))
  206. (org-babel-exp-results info type 'silent)))))
  207. (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info)))))
  208. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  209. ('none (funcall silently) (funcall clean) "")
  210. ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
  211. ('results (org-babel-exp-results info type nil hash) "")
  212. ('both (org-babel-exp-results info type nil hash)
  213. (org-babel-exp-code info)))))
  214. (defcustom org-babel-exp-code-template
  215. "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
  216. "Template used to export the body of code blocks.
  217. This template may be customized to include additional information
  218. such as the code block name, or the values of particular header
  219. arguments. The template is filled out using `org-fill-template',
  220. and the following %keys may be used.
  221. lang ------ the language of the code block
  222. name ------ the name of the code block
  223. body ------ the body of the code block
  224. flags ----- the flags passed to the code block
  225. In addition to the keys mentioned above, every header argument
  226. defined for the code block may be used as a key and will be
  227. replaced with its value."
  228. :group 'org-babel
  229. :type 'string)
  230. (defun org-babel-exp-code (info)
  231. "Return the original code block formatted for export."
  232. (setf (nth 1 info)
  233. (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
  234. (replace-regexp-in-string
  235. (org-babel-noweb-wrap) "" (nth 1 info))
  236. (if (org-babel-noweb-p (nth 2 info) :export)
  237. (org-babel-expand-noweb-references
  238. info (org-babel-exp-get-export-buffer))
  239. (nth 1 info))))
  240. (org-fill-template
  241. org-babel-exp-code-template
  242. `(("lang" . ,(nth 0 info))
  243. ("body" . ,(if (string= (nth 0 info) "org")
  244. (replace-regexp-in-string "^" "," (nth 1 info))
  245. (nth 1 info)))
  246. ,@(mapcar (lambda (pair)
  247. (cons (substring (symbol-name (car pair)) 1)
  248. (format "%S" (cdr pair))))
  249. (nth 2 info))
  250. ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
  251. ("name" . ,(or (nth 4 info) "")))))
  252. (defun org-babel-exp-results (info type &optional silent hash)
  253. "Evaluate and return the results of the current code block for export.
  254. Results are prepared in a manner suitable for export by org-mode.
  255. This function is called by `org-babel-exp-do-export'. The code
  256. block will be evaluated. Optional argument SILENT can be used to
  257. inhibit insertion of results into the buffer."
  258. (when (and org-export-babel-evaluate
  259. (not (and hash (equal hash (org-babel-current-result-hash)))))
  260. (let ((lang (nth 0 info))
  261. (body (if (org-babel-noweb-p (nth 2 info) :eval)
  262. (org-babel-expand-noweb-references
  263. info (org-babel-exp-get-export-buffer))
  264. (nth 1 info)))
  265. (info (copy-sequence info)))
  266. ;; skip code blocks which we can't evaluate
  267. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  268. (org-babel-eval-wipe-error-buffer)
  269. (prog1 nil
  270. (setf (nth 1 info) body)
  271. (setf (nth 2 info)
  272. (org-babel-exp-in-export-file lang
  273. (org-babel-process-params
  274. (org-babel-merge-params
  275. (nth 2 info)
  276. `((:results . ,(if silent "silent" "replace")))))))
  277. (cond
  278. ((equal type 'block)
  279. (org-babel-execute-src-block nil info))
  280. ((equal type 'inline)
  281. ;; position the point on the inline source block allowing
  282. ;; `org-babel-insert-result' to check that the block is
  283. ;; inline
  284. (re-search-backward "[ \f\t\n\r\v]" nil t)
  285. (re-search-forward org-babel-inline-src-block-regexp nil t)
  286. (re-search-backward "src_" nil t)
  287. (org-babel-execute-src-block nil info))
  288. ((equal type 'lob)
  289. (save-excursion
  290. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  291. (org-babel-execute-src-block nil info)))))))))
  292. (provide 'ob-exp)
  293. ;;; ob-exp.el ends here