ob-exp.el 10 KB

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