org-babel-exp.el 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. (defun org-babel-exp-src-blocks (body &rest headers)
  31. "Process src block for export. Depending on the 'export'
  32. headers argument in replace the source code block with...
  33. both ---- display the code and the results
  34. code ---- the default, display the code inside the block but do
  35. not process
  36. results - just like none only the block is run on export ensuring
  37. that it's results are present in the org-mode buffer
  38. none ----- do not display either code or results upon export"
  39. (interactive)
  40. (unless headers (error "org-babel can't process a source block without knowing the source code"))
  41. (message "org-babel-exp processing...")
  42. (let* ((lang (car headers))
  43. (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
  44. (switches (cdr headers)) params)
  45. (while (and (cadr headers) (not (string-match "[ \t]*:" (cadr headers))))
  46. (pop headers))
  47. (setq params (cdr headers))
  48. (setf (cdr headers) nil)
  49. (setq switches (mapconcat #'identity switches " "))
  50. (setq params (org-babel-merge-params
  51. org-babel-default-header-args
  52. (if (boundp lang-headers) (eval lang-headers) nil)
  53. (org-babel-params-from-properties)
  54. (org-babel-parse-header-arguments
  55. (mapconcat #'identity params " "))))
  56. (org-babel-exp-do-export (list lang body params switches) 'block)))
  57. (defun org-babel-exp-inline-src-blocks (start end)
  58. "Process inline src blocks between START and END for export.
  59. See `org-babel-exp-src-blocks' for export options, currently the
  60. options and are taken from `org-babel-defualt-inline-header-args'."
  61. (interactive)
  62. (save-excursion
  63. (goto-char start)
  64. (while (and (< (point) end)
  65. (re-search-forward org-babel-inline-src-block-regexp end t))
  66. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  67. (replacement (save-match-data
  68. (org-babel-exp-do-export info 'inline))))
  69. (setq end (+ end (- (length replacement) (length (match-string 1)))))
  70. (replace-match replacement t t nil 1)))))
  71. (defun org-babel-exp-lob-one-liners (start end)
  72. "Process #+lob (Library of Babel) calls between START and END for export.
  73. See `org-babel-exp-src-blocks' for export options. Currently the
  74. options are taken from `org-babel-default-header-args'."
  75. (interactive)
  76. (let (replacement)
  77. (save-excursion
  78. (goto-char start)
  79. (while (and (< (point) end)
  80. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  81. (setq replacement
  82. (save-match-data
  83. (org-babel-exp-do-export
  84. (list "emacs-lisp" "results"
  85. (org-babel-merge-params
  86. org-babel-default-header-args
  87. (org-babel-parse-header-arguments
  88. (org-babel-clean-text-properties
  89. (concat ":var results="
  90. (mapconcat #'identity (org-babel-lob-get-info) " "))))))
  91. 'lob)))
  92. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  93. (replace-match replacement t t)))))
  94. (defun org-babel-exp-do-export (info type)
  95. (case (intern (or (cdr (assoc :exports (third info))) "code"))
  96. ('none "")
  97. ('code (org-babel-exp-code info type))
  98. ('results (org-babel-exp-results info type))
  99. ('both (concat (org-babel-exp-code info type)
  100. "\n\n"
  101. (org-babel-exp-results info type)))))
  102. (defun org-babel-exp-code (info type)
  103. (let ((lang (first info))
  104. (body (second info))
  105. (switches (fourth info)))
  106. (case type
  107. ('inline (format "=%s=" (second info)))
  108. ('block (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC" lang switches body
  109. (if (string-match "\n$" body) "" "\n")))
  110. ('lob (save-excursion
  111. (re-search-backward org-babel-lob-one-liner-regexp)
  112. (format "#+BEGIN_SRC org-babel-lob\n%s\n#+END_SRC"
  113. (first (org-babel-lob-get-info))))))))
  114. (defun org-babel-exp-results (info type)
  115. (let ((lang (first info))
  116. (body (second info))
  117. (params
  118. ;; lets ensure that we lookup references in the original file
  119. (mapcar (lambda (pair)
  120. (if (and org-current-export-file
  121. (eq (car pair) :var)
  122. (string-match org-babel-ref-split-regexp (cdr pair)))
  123. `(:var . ,(concat (match-string 1 (cdr pair))
  124. "=" org-current-export-file
  125. ":" (match-string 2 (cdr pair))))
  126. pair))
  127. (third info))))
  128. (case type
  129. ('inline
  130. (let ((raw (org-babel-execute-src-block
  131. nil info '((:results . "silent"))))
  132. (result-params (split-string (cdr (assoc :results params)))))
  133. (cond ;; respect the value of the :results header argument
  134. ((member "file" result-params)
  135. (org-babel-result-to-file raw))
  136. ((or (member "raw" result-params) (member "org" result-params))
  137. raw)
  138. ((member "code" result-params)
  139. (format "src_%s{%s}" lang raw))
  140. (t
  141. (if (stringp raw)
  142. (if (= 0 (length raw)) "=(no results)="
  143. (format "=%s=" raw))
  144. (format "=%S=" raw))))))
  145. ('block
  146. (save-excursion ;; org-exp-blocks places us at the end of the block
  147. (re-search-backward org-babel-src-block-regexp nil t)
  148. (org-babel-execute-src-block
  149. nil nil (org-babel-merge-params params '((:results . "replace")))) ""))
  150. ('lob
  151. (save-excursion
  152. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  153. (org-babel-execute-src-block
  154. nil (list lang body (org-babel-merge-params params '((:results . "replace"))))) "")))))
  155. (provide 'org-babel-exp)
  156. ;;; org-babel-exp.el ends here