ob-exp.el 11 KB

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