org-babel-exp.el 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. ;;; org-babel-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 'org-babel)
  26. (require 'org-exp-blocks)
  27. (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil))
  28. (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
  29. (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
  30. (defvar org-babel-function-def-export-keyword "function"
  31. "When exporting a source block function, this keyword will
  32. appear in the exported version in the place of source name
  33. line. A source block is considered to be a source block function
  34. if the source name is present and is followed by a parenthesized
  35. argument list. The parentheses may be empty or contain
  36. whitespace. An example is the following which generates n random
  37. (uniform) numbers.
  38. #+source: rand(n)
  39. #+begin_src R
  40. runif(n)
  41. #+end_src
  42. ")
  43. (defvar org-babel-function-def-export-indent 4
  44. "When exporting a source block function, the block contents
  45. will be indented by this many characters. See
  46. `org-babel-function-def-export-name' for the definition of a
  47. source block function.")
  48. (defvar obe-marker nil)
  49. (defun org-babel-exp-src-blocks (body &rest headers)
  50. "Process src block for export. Depending on the 'export'
  51. headers argument in replace the source code block with...
  52. both ---- display the code and the results
  53. code ---- the default, display the code inside the block but do
  54. not process
  55. results - just like none only the block is run on export ensuring
  56. that it's results are present in the org-mode buffer
  57. none ----- do not display either code or results upon export"
  58. (interactive)
  59. (message "org-babel-exp processing...")
  60. (when (member (first headers) org-babel-interpreters)
  61. (save-excursion
  62. (goto-char (match-beginning 0))
  63. (org-babel-exp-do-export (org-babel-get-src-block-info) 'block))))
  64. (defun org-babel-exp-inline-src-blocks (start end)
  65. "Process inline src blocks between START and END for export.
  66. See `org-babel-exp-src-blocks' for export options, currently the
  67. options and are taken from `org-babel-defualt-inline-header-args'."
  68. (interactive)
  69. (save-excursion
  70. (goto-char start)
  71. (while (and (< (point) end)
  72. (re-search-forward org-babel-inline-src-block-regexp end t))
  73. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  74. (replacement (save-match-data
  75. (org-babel-exp-do-export info 'inline))))
  76. (setq end (+ end (- (length replacement) (length (match-string 1)))))
  77. (replace-match replacement t t nil 1)))))
  78. (defun org-babel-exp-lob-one-liners (start end)
  79. "Process #+lob (Library of Babel) calls between START and END for export.
  80. See `org-babel-exp-src-blocks' for export options. Currently the
  81. options are taken from `org-babel-default-header-args'."
  82. (interactive)
  83. (let (replacement)
  84. (save-excursion
  85. (goto-char start)
  86. (while (and (< (point) end)
  87. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  88. (setq replacement
  89. (save-match-data
  90. (org-babel-exp-do-export
  91. (list "emacs-lisp" "results"
  92. (org-babel-merge-params
  93. org-babel-default-header-args
  94. (org-babel-parse-header-arguments
  95. (org-babel-clean-text-properties
  96. (concat ":var results="
  97. (mapconcat #'identity
  98. (org-babel-lob-get-info) " "))))))
  99. 'lob)))
  100. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  101. (replace-match replacement t t)))))
  102. (defun org-babel-exp-do-export (info type)
  103. "Return a string containing the exported content of the current
  104. code block respecting the value of the :exports header argument."
  105. (case (intern (or (cdr (assoc :exports (third info))) "code"))
  106. ('none "")
  107. ('code (org-babel-exp-code info type))
  108. ('results (org-babel-exp-results info type))
  109. ('both (concat (org-babel-exp-code info type)
  110. "\n\n"
  111. (org-babel-exp-results info type)))))
  112. (defun org-babel-exp-code (info type)
  113. "Return the code the current code block in a manner suitable
  114. for exportation by org-mode. This function is called by
  115. `org-babel-exp-do-export'."
  116. (let ((lang (first info))
  117. (body (second info))
  118. (switches (fourth info))
  119. (name (fifth info))
  120. (args (mapcar
  121. #'cdr
  122. (remove-if-not (lambda (el) (eq :var (car el))) (third info)))))
  123. (case type
  124. ('inline (format "=%s=" body))
  125. ('block
  126. (let ((str
  127. (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
  128. (if (and body (string-match "\n$" body))
  129. "" "\n"))))
  130. (when name
  131. (add-text-properties
  132. 0 (length str)
  133. (list 'org-caption
  134. (format "%s(%s)"
  135. name
  136. (mapconcat #'identity args ", ")))
  137. str))
  138. str))
  139. ('lob
  140. (let ((call-line (and (string-match "results=" (car args))
  141. (substring (car args) (match-end 0)))))
  142. (cond
  143. ((eq backend 'html)
  144. (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
  145. call-line))
  146. ((t (format ": %s\n" call-line)))))))))
  147. (defun org-babel-exp-results (info type)
  148. "Return the results of the current code block in a manner
  149. suitable for exportation by org-mode. This function is called by
  150. `org-babel-exp-do-export'."
  151. (let ((lang (first info))
  152. (body (second info))
  153. (params
  154. ;; lets ensure that we lookup references in the original file
  155. (mapcar
  156. (lambda (pair)
  157. (if (and org-current-export-file
  158. (eq (car pair) :var)
  159. (string-match org-babel-ref-split-regexp (cdr pair))
  160. (null (org-babel-ref-literal (match-string 2 (cdr pair)))))
  161. `(:var . ,(concat (match-string 1 (cdr pair))
  162. "=" org-current-export-file
  163. ":" (match-string 2 (cdr pair))))
  164. pair))
  165. (third info))))
  166. (case type
  167. ('inline
  168. (let ((raw (org-babel-execute-src-block
  169. nil info '((:results . "silent"))))
  170. (result-params (split-string (cdr (assoc :results params)))))
  171. (cond ;; respect the value of the :results header argument
  172. ((member "file" result-params)
  173. (org-babel-result-to-file raw))
  174. ((or (member "raw" result-params) (member "org" result-params))
  175. (format "%s" raw))
  176. ((member "code" result-params)
  177. (format "src_%s{%s}" lang raw))
  178. (t
  179. (if (stringp raw)
  180. (if (= 0 (length raw)) "=(no results)="
  181. (format "=%s=" raw))
  182. (format "=%S=" raw))))))
  183. ('block
  184. (org-babel-execute-src-block
  185. nil nil (org-babel-merge-params params '((:results . "replace"))))
  186. "")
  187. ('lob
  188. (save-excursion
  189. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  190. (org-babel-execute-src-block
  191. nil (list lang body (org-babel-merge-params
  192. params '((:results . "replace"))))) "")))))
  193. (provide 'org-babel-exp)
  194. ;;; org-babel-exp.el ends here