ob-exp.el 12 KB

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