ob-exp.el 12 KB

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