ob-exp.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ;;; ob-exp.el --- Exportation of org-babel source blocks
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.01trans
  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. ;;; Commentary:
  19. ;; See the online documentation for more information
  20. ;;
  21. ;; http://orgmode.org/worg/org-contrib/babel/
  22. ;;; Code:
  23. (require 'ob)
  24. (require 'org-exp-blocks)
  25. (eval-when-compile
  26. (require 'cl))
  27. (defvar obe-marker nil)
  28. (defvar org-current-export-file)
  29. (defvar org-babel-lob-one-liner-regexp)
  30. (defvar org-babel-ref-split-regexp)
  31. (declare-function org-babel-lob-get-info "ob-lob" ())
  32. (declare-function org-babel-ref-literal "ob-ref" (ref))
  33. (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
  34. (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
  35. (add-hook 'org-export-blocks-postblock-hook 'org-exp-res/src-name-cleanup)
  36. (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil))
  37. (defcustom org-export-babel-evaluate t
  38. "Switch controlling code evaluation during export.
  39. When set to nil no code will be exported as part of the export
  40. process."
  41. :group 'org-babel
  42. :type 'boolean)
  43. (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
  44. (defvar org-babel-function-def-export-keyword "function"
  45. "The keyword to substitute for the source name line on export.
  46. When exporting a source block function, this keyword will
  47. appear in the exported version in the place of source name
  48. line. A source block is considered to be a source block function
  49. if the source name is present and is followed by a parenthesized
  50. argument list. The parentheses may be empty or contain
  51. whitespace. An example is the following which generates n random
  52. \(uniform) numbers.
  53. #+source: rand(n)
  54. #+begin_src R
  55. runif(n)
  56. #+end_src")
  57. (defvar org-babel-function-def-export-indent 4
  58. "Number of characters to indent a source block on export.
  59. When exporting a source block function, the block contents will
  60. be indented by this many characters. See
  61. `org-babel-function-def-export-name' for the definition of a
  62. source block function.")
  63. (defun org-babel-exp-src-blocks (body &rest headers)
  64. "Process source block for export.
  65. Depending on the 'export' headers argument in replace the source
  66. code block with...
  67. both ---- display the code and the results
  68. code ---- the default, display the code inside the block but do
  69. not process
  70. results - just like none only the block is run on export ensuring
  71. that it's results are present in the org-mode buffer
  72. none ----- do not display either code or results upon export"
  73. (interactive)
  74. (message "org-babel-exp processing...")
  75. (save-excursion
  76. (goto-char (match-beginning 0))
  77. (let* ((info (org-babel-get-src-block-info))
  78. (params (nth 2 info)))
  79. ;; bail if we couldn't get any info from the block
  80. (when info
  81. ;; expand noweb references in the original file
  82. (setf (nth 1 info)
  83. (if (and (cdr (assoc :noweb params))
  84. (string= "yes" (cdr (assoc :noweb params))))
  85. (org-babel-expand-noweb-references
  86. info (get-file-buffer org-current-export-file))
  87. (nth 1 info))))
  88. (org-babel-exp-do-export info 'block))))
  89. (defun org-babel-exp-inline-src-blocks (start end)
  90. "Process inline source blocks between START and END for export.
  91. See `org-babel-exp-src-blocks' for export options, currently the
  92. options and are taken from `org-babel-default-inline-header-args'."
  93. (interactive)
  94. (save-excursion
  95. (goto-char start)
  96. (while (and (< (point) end)
  97. (re-search-forward org-babel-inline-src-block-regexp end t))
  98. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  99. (params (nth 2 info))
  100. (replacement
  101. (save-match-data
  102. (if (org-babel-in-example-or-verbatim)
  103. (buffer-substring (match-beginning 0) (match-end 0))
  104. ;; expand noweb references in the original file
  105. (setf (nth 1 info)
  106. (if (and (cdr (assoc :noweb params))
  107. (string= "yes" (cdr (assoc :noweb params))))
  108. (org-babel-expand-noweb-references
  109. info (get-file-buffer org-current-export-file))
  110. (nth 1 info)))
  111. (org-babel-exp-do-export info 'inline)))))
  112. (setq end (+ end (- (length replacement) (length (match-string 1)))))
  113. (replace-match replacement t t nil 1)))))
  114. (defun org-exp-res/src-name-cleanup ()
  115. "Clean up #+results and #+srcname lines for export.
  116. This function should only be called after all block processing
  117. has taken place."
  118. (interactive)
  119. (save-excursion
  120. (goto-char (point-min))
  121. (while (org-re-search-forward-unprotected
  122. (concat
  123. "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
  124. nil t)
  125. (delete-region
  126. (progn (beginning-of-line) (point))
  127. (progn (end-of-line) (+ 1 (point)))))))
  128. (defun org-babel-in-example-or-verbatim ()
  129. "Return true if point is in example or verbatim code.
  130. Example and verbatim code include escaped portions of
  131. an org-mode buffer code that should be treated as normal
  132. org-mode text."
  133. (or (org-in-indented-comment-line)
  134. (save-excursion
  135. (save-match-data
  136. (goto-char (point-at-bol))
  137. (looking-at "[ \t]*:[ \t]")))
  138. (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  139. (defun org-babel-exp-lob-one-liners (start end)
  140. "Process Library of Babel calls between START and END for export.
  141. See `org-babel-exp-src-blocks' for export options. Currently the
  142. options are taken from `org-babel-default-header-args'."
  143. (interactive)
  144. (let (replacement)
  145. (save-excursion
  146. (goto-char start)
  147. (while (and (< (point) end)
  148. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  149. (setq replacement
  150. (let ((lob-info (org-babel-lob-get-info)))
  151. (save-match-data
  152. (org-babel-exp-do-export
  153. (list "emacs-lisp" "results"
  154. (org-babel-merge-params
  155. org-babel-default-header-args
  156. (org-babel-parse-header-arguments
  157. (org-babel-clean-text-properties
  158. (concat ":var results="
  159. (mapconcat #'identity
  160. (butlast lob-info) " ")))))
  161. (car (last lob-info)))
  162. 'lob))))
  163. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  164. (replace-match replacement t t)))))
  165. (defun org-babel-exp-do-export (info type)
  166. "Return a string with the exported content of a code block.
  167. The function respects the value of the :exports header argument."
  168. (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
  169. (when (and session
  170. (not (equal "none" session))
  171. (not (assoc :noeval (nth 2 info))))
  172. (org-babel-exp-results info type 'silent))))
  173. (clean () (org-babel-remove-result info)))
  174. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  175. ('none (silently) (clean) "")
  176. ('code (silently) (clean) (org-babel-exp-code info type))
  177. ('results (org-babel-exp-results info type))
  178. ('both (concat (org-babel-exp-code info type)
  179. "\n\n"
  180. (org-babel-exp-results info type))))))
  181. (defvar backend)
  182. (defun org-babel-exp-code (info type)
  183. "Prepare and return code in the current code block for export.
  184. Code is prepared in a manner suitable for exportat by
  185. org-mode. This function is called by `org-babel-exp-do-export'.
  186. The code block is not evaluated."
  187. (let ((lang (nth 0 info))
  188. (body (nth 1 info))
  189. (switches (nth 3 info))
  190. (name (nth 4 info))
  191. (args (mapcar
  192. #'cdr
  193. (org-remove-if-not (lambda (el) (eq :var (car el))) (nth 2 info)))))
  194. (case type
  195. ('inline (format "=%s=" body))
  196. ('block
  197. (let ((str
  198. (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
  199. (if (and body (string-match "\n$" body))
  200. "" "\n"))))
  201. (when name
  202. (add-text-properties
  203. 0 (length str)
  204. (list 'org-caption
  205. (format "%s(%s)"
  206. name
  207. (mapconcat #'identity args ", ")))
  208. str))
  209. str))
  210. ('lob
  211. (let ((call-line (and (string-match "results=" (car args))
  212. (substring (car args) (match-end 0)))))
  213. (cond
  214. ((eq backend 'html)
  215. (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
  216. call-line))
  217. ((format ": %s\n" call-line))))))))
  218. (defun org-babel-exp-results (info type &optional silent)
  219. "Evaluate and return the results of the current code block for export.
  220. Results are prepared in a manner suitable for export by org-mode.
  221. This function is called by `org-babel-exp-do-export'. The code
  222. block will be evaluated. Optional argument SILENT can be used to
  223. inhibit insertion of results into the buffer."
  224. (if org-export-babel-evaluate
  225. (let ((lang (nth 0 info))
  226. (body (nth 1 info))
  227. (params
  228. ;; lets ensure that we lookup references in the original file
  229. (mapcar
  230. (lambda (pair)
  231. (if (and org-current-export-file
  232. (eq (car pair) :var)
  233. (string-match org-babel-ref-split-regexp (cdr pair))
  234. (equal :ob-must-be-reference
  235. (org-babel-ref-literal
  236. (match-string 2 (cdr pair)))))
  237. `(:var . ,(concat (match-string 1 (cdr pair))
  238. "=" org-current-export-file
  239. ":" (match-string 2 (cdr pair))))
  240. pair))
  241. (nth 2 info))))
  242. ;; skip code blocks which we can't evaluate
  243. (if (fboundp (intern (concat "org-babel-execute:" lang)))
  244. (case type
  245. ('inline
  246. (let ((raw (org-babel-execute-src-block
  247. nil info '((:results . "silent"))))
  248. (result-params (split-string
  249. (cdr (assoc :results params)))))
  250. (unless silent
  251. (cond ;; respect the value of the :results header argument
  252. ((member "file" result-params)
  253. (org-babel-result-to-file raw))
  254. ((or (member "raw" result-params)
  255. (member "org" result-params))
  256. (format "%s" raw))
  257. ((member "code" result-params)
  258. (format "src_%s{%s}" lang raw))
  259. (t
  260. (if (stringp raw)
  261. (if (= 0 (length raw)) "=(no results)="
  262. (format "%s" raw))
  263. (format "%S" raw)))))))
  264. ('block
  265. (org-babel-execute-src-block
  266. nil info (org-babel-merge-params
  267. params
  268. `((:results . ,(if silent "silent" "replace")))))
  269. "")
  270. ('lob
  271. (save-excursion
  272. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  273. (org-babel-execute-src-block
  274. nil info (org-babel-merge-params
  275. params
  276. `((:results . ,(if silent "silent" "replace")))))
  277. "")))
  278. ""))
  279. ""))
  280. (provide 'ob-exp)
  281. ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
  282. ;;; ob-exp.el ends here