ob-exp.el 11 KB

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