org-babel-exp.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; org-babel-exp.el --- Exportation of org-babel source blocks
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  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. (defun org-babel-exp-src-blocks (body &rest headers)
  30. "Process src block for export. Depending on the 'export'
  31. headers argument in replace the source code block with...
  32. both ---- display the code and the results
  33. code ---- the default, display the code inside the block but do
  34. not process
  35. results - just like none only the block is run on export ensuring
  36. that it's results are present in the org-mode buffer
  37. none ----- do not display either code or results upon export"
  38. (interactive)
  39. (unless headers (error "org-babel can't process a source block without knowing the source code"))
  40. (message "org-babel-exp processing...")
  41. (let* ((lang (car headers))
  42. (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
  43. (params (org-babel-merge-params
  44. org-babel-default-header-args
  45. (if (boundp lang-headers) (eval lang-headers) nil)
  46. (org-babel-params-from-properties)
  47. (org-babel-parse-header-arguments
  48. (mapconcat #'identity (cdr headers) " ")))))
  49. (org-babel-exp-do-export lang body params)))
  50. (defun org-babel-exp-inline-src-blocks (start end)
  51. "Process inline src blocks between START and END for export.
  52. See `org-babel-exp-src-blocks' for export options, currently the
  53. options and are taken from `org-babel-defualt-inline-header-args'."
  54. (interactive)
  55. (save-excursion
  56. (goto-char start)
  57. (while (and (< (point) end)
  58. (re-search-forward org-babel-inline-src-block-regexp end t))
  59. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  60. (replacement (save-match-data
  61. (org-babel-exp-do-export
  62. (first info) (second info) (third info) t))))
  63. ;; (message "%s -> %s" (second info) replacement) ;; debugging
  64. (setf end (+ end (- (length replacement)
  65. (+ 6 (length (first info)) (length (second info))))))
  66. (replace-match replacement t t)))))
  67. (defun org-babel-exp-do-export (lang body params &optional inline)
  68. (case (intern (or (cdr (assoc :exports params)) "code"))
  69. ('none "")
  70. ('code (org-babel-exp-code body lang params inline))
  71. ('results (org-babel-exp-results body lang params inline))
  72. ('both (concat (org-babel-exp-code body lang params inline)
  73. "\n\n"
  74. (org-babel-exp-results body lang params inline)))))
  75. (defun org-babel-exp-code (body lang params &optional inline)
  76. (if inline
  77. (format "=%s=" body)
  78. (format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body
  79. (if (string-match "\n$" body) "" "\n"))))
  80. (defun org-babel-exp-results (body lang params &optional inline)
  81. (let ((params
  82. ;; lets ensure that we lookup references in the original file
  83. (mapcar (lambda (pair)
  84. (if (and (eq (car pair) :var)
  85. (string-match org-babel-ref-split-regexp (cdr pair)))
  86. `(:var . ,(concat (match-string 1 (cdr pair))
  87. "=" org-current-export-file
  88. ":" (match-string 2 (cdr pair))))
  89. pair)) params)))
  90. (if inline
  91. (let ((raw (org-babel-execute-src-block
  92. nil (list lang body params) '((:results . "silent"))))
  93. (result-params (split-string (cdr (assoc :results params)))))
  94. (cond ;; respect the value of the :results header argument
  95. ((member "file" result-params)
  96. (org-babel-result-to-file raw))
  97. ((or (member "raw" result-params) (member "org" result-params))
  98. raw)
  99. ((member "code" result-params)
  100. (format "src_%s{%s}" lang raw))
  101. (t
  102. (if (and (stringp raw) (= 0 (length raw)))
  103. "=(no results)=" (format "=%S=" raw)))))
  104. (save-excursion ;; org-exp-blocks places us at the end of the block
  105. (re-search-backward org-babel-src-block-regexp nil t)
  106. (org-babel-execute-src-block
  107. nil nil (org-babel-merge-params params '((:results . "replace")))) ""))))
  108. (provide 'org-babel-exp)
  109. ;;; org-babel-exp.el ends here