ob-exp.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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: 0.01
  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. ;; expand noweb references in the original file
  80. (setf (nth 1 info)
  81. (if (and (cdr (assoc :noweb params))
  82. (string= "yes" (cdr (assoc :noweb params))))
  83. (org-babel-expand-noweb-references
  84. info (get-file-buffer org-current-export-file))
  85. (nth 1 info)))
  86. (org-babel-exp-do-export info 'block))))
  87. (defun org-babel-exp-inline-src-blocks (start end)
  88. "Process inline source blocks between START and END for export.
  89. See `org-babel-exp-src-blocks' for export options, currently the
  90. options and are taken from `org-babel-default-inline-header-args'."
  91. (interactive)
  92. (save-excursion
  93. (goto-char start)
  94. (while (and (< (point) end)
  95. (re-search-forward org-babel-inline-src-block-regexp end t))
  96. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  97. (params (nth 2 info))
  98. (replacement
  99. (save-match-data
  100. (if (org-babel-in-example-or-verbatim)
  101. (buffer-substring (match-beginning 0) (match-end 0))
  102. ;; expand noweb references in the original file
  103. (setf (nth 1 info)
  104. (if (and (cdr (assoc :noweb params))
  105. (string= "yes" (cdr (assoc :noweb params))))
  106. (org-babel-expand-noweb-references
  107. info (get-file-buffer org-current-export-file))
  108. (nth 1 info)))
  109. (org-babel-exp-do-export info 'inline)))))
  110. (setq end (+ end (- (length replacement) (length (match-string 1)))))
  111. (replace-match replacement t t nil 1)))))
  112. (defun org-exp-res/src-name-cleanup ()
  113. "Clean up #+results and #+srcname lines for export.
  114. This function should only be called after all block processing
  115. has taken place."
  116. (interactive)
  117. (save-excursion
  118. (goto-char (point-min))
  119. (while (org-re-search-forward-unprotected
  120. (concat
  121. "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
  122. nil t)
  123. (delete-region
  124. (progn (beginning-of-line) (point))
  125. (progn (end-of-line) (+ 1 (point)))))))
  126. (defun org-babel-in-example-or-verbatim ()
  127. "Return true if point is in example or verbatim code.
  128. Example and verbatim code include escaped portions of
  129. an org-mode buffer code that should be treated as normal
  130. org-mode text."
  131. (or (org-in-indented-comment-line)
  132. (save-excursion
  133. (save-match-data
  134. (goto-char (point-at-bol))
  135. (looking-at "[ \t]*:[ \t]")))
  136. (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  137. (defun org-babel-exp-lob-one-liners (start end)
  138. "Process Library of Babel calls between START and END for export.
  139. See `org-babel-exp-src-blocks' for export options. Currently the
  140. options are taken from `org-babel-default-header-args'."
  141. (interactive)
  142. (let (replacement)
  143. (save-excursion
  144. (goto-char start)
  145. (while (and (< (point) end)
  146. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  147. (setq replacement
  148. (let ((lob-info (org-babel-lob-get-info)))
  149. (save-match-data
  150. (org-babel-exp-do-export
  151. (list "emacs-lisp" "results"
  152. (org-babel-merge-params
  153. org-babel-default-header-args
  154. (org-babel-parse-header-arguments
  155. (org-babel-clean-text-properties
  156. (concat ":var results="
  157. (mapconcat #'identity
  158. (butlast lob-info) " ")))))
  159. (car (last lob-info)))
  160. 'lob))))
  161. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  162. (replace-match replacement t t)))))
  163. (defun org-babel-exp-do-export (info type)
  164. "Return a string with the exported content of a code block.
  165. The function respects the value of the :exports header argument."
  166. (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
  167. (when (and session
  168. (not (equal "none" session))
  169. (not (assoc :noeval (nth 2 info))))
  170. (org-babel-exp-results info type 'silent))))
  171. (clean () (org-babel-remove-result info)))
  172. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  173. ('none (silently) (clean) "")
  174. ('code (silently) (clean) (org-babel-exp-code info type))
  175. ('results (org-babel-exp-results info type))
  176. ('both (concat (org-babel-exp-code info type)
  177. "\n\n"
  178. (org-babel-exp-results info type))))))
  179. (defvar backend)
  180. (defun org-babel-exp-code (info type)
  181. "Prepare and return code in the current code block for export.
  182. Code is prepared in a manner suitable for exportat by
  183. org-mode. This function is called by `org-babel-exp-do-export'.
  184. The code block is not evaluated."
  185. (let ((lang (nth 0 info))
  186. (body (nth 1 info))
  187. (switches (nth 3 info))
  188. (name (nth 4 info))
  189. (args (mapcar
  190. #'cdr
  191. (org-remove-if-not (lambda (el) (eq :var (car el))) (nth 2 info)))))
  192. (case type
  193. ('inline (format "=%s=" body))
  194. ('block
  195. (let ((str
  196. (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
  197. (if (and body (string-match "\n$" body))
  198. "" "\n"))))
  199. (when name
  200. (add-text-properties
  201. 0 (length str)
  202. (list 'org-caption
  203. (format "%s(%s)"
  204. name
  205. (mapconcat #'identity args ", ")))
  206. str))
  207. str))
  208. ('lob
  209. (let ((call-line (and (string-match "results=" (car args))
  210. (substring (car args) (match-end 0)))))
  211. (cond
  212. ((eq backend 'html)
  213. (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
  214. call-line))
  215. ((format ": %s\n" call-line))))))))
  216. (defun org-babel-exp-results (info type &optional silent)
  217. "Evaluate and return the results of the current code block for export.
  218. Results are prepared in a manner suitable for export by org-mode.
  219. This function is called by `org-babel-exp-do-export'. The code
  220. block will be evaluated. Optional argument SILENT can be used to
  221. inhibit insertion of results into the buffer."
  222. (if org-export-babel-evaluate
  223. (let ((lang (nth 0 info))
  224. (body (nth 1 info))
  225. (params
  226. ;; lets ensure that we lookup references in the original file
  227. (mapcar
  228. (lambda (pair)
  229. (if (and org-current-export-file
  230. (eq (car pair) :var)
  231. (string-match org-babel-ref-split-regexp (cdr pair))
  232. (equal :ob-must-be-reference
  233. (org-babel-ref-literal
  234. (match-string 2 (cdr pair)))))
  235. `(:var . ,(concat (match-string 1 (cdr pair))
  236. "=" org-current-export-file
  237. ":" (match-string 2 (cdr pair))))
  238. pair))
  239. (nth 2 info))))
  240. ;; skip code blocks which we can't evaluate
  241. (if (fboundp (intern (concat "org-babel-execute:" lang)))
  242. (case type
  243. ('inline
  244. (let ((raw (org-babel-execute-src-block
  245. nil info '((:results . "silent"))))
  246. (result-params (split-string
  247. (cdr (assoc :results params)))))
  248. (unless silent
  249. (cond ;; respect the value of the :results header argument
  250. ((member "file" result-params)
  251. (org-babel-result-to-file raw))
  252. ((or (member "raw" result-params)
  253. (member "org" result-params))
  254. (format "%s" raw))
  255. ((member "code" result-params)
  256. (format "src_%s{%s}" lang raw))
  257. (t
  258. (if (stringp raw)
  259. (if (= 0 (length raw)) "=(no results)="
  260. (format "%s" raw))
  261. (format "%S" raw)))))))
  262. ('block
  263. (org-babel-execute-src-block
  264. nil info (org-babel-merge-params
  265. params
  266. `((:results . ,(if silent "silent" "replace")))))
  267. "")
  268. ('lob
  269. (save-excursion
  270. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  271. (org-babel-execute-src-block
  272. nil info (org-babel-merge-params
  273. params
  274. `((:results . ,(if silent "silent" "replace")))))
  275. "")))
  276. ""))
  277. ""))
  278. (provide 'ob-exp)
  279. ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
  280. ;;; ob-exp.el ends here