ob-exp.el 10 KB

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