ob-exp.el 12 KB

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