org-babel-exp.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. (add-to-list 'org-export-blocks '(src org-babel-exp-src-blocks))
  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 ---- the default, display the code and the results
  33. code ---- display the code inside the block but do not process
  34. results - process the block and replace it with the results of
  35. execution
  36. none ----- do not display either code or results upon export"
  37. (interactive)
  38. (unless headers (error "org-babel can't process a source block without knowing the source code"))
  39. (message "org-babel processing...")
  40. (let ((lang (car headers))
  41. (params (org-babel-parse-header-arguments (mapconcat #'identity (cdr headers) " "))))
  42. (org-babel-exp-do-export lang body params)))
  43. (defun org-babel-exp-inline-src-blocks (start end)
  44. "Process inline src blocks between START and END for export.
  45. See `org-babel-exp-src-blocks' for export options, currently the
  46. options and are taken from `org-babel-defualt-inline-header-args'."
  47. (interactive)
  48. (save-excursion
  49. (goto-char start)
  50. (while (and (< (point) end) (re-search-forward org-babel-inline-src-block-regexp end t))
  51. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  52. (replacement (save-match-data
  53. (org-babel-exp-do-export (first info) (second info) (third info) t))))
  54. (setf end (+ end (- (length replacement)
  55. (+ 6 (length (first info)) (length (second info))))))
  56. (replace-match replacement t t)))))
  57. (defun org-babel-exp-do-export (lang body params &optional inline)
  58. (case (intern (or (cdr (assoc :exports params)) "code"))
  59. ('none "")
  60. ('code (org-babel-exp-code body lang params inline))
  61. ('results (org-babel-exp-results body lang params inline))
  62. ('both (concat (org-babel-exp-code body lang params inline)
  63. "\n\n"
  64. (org-babel-exp-results body lang params inline)))))
  65. (defun org-babel-exp-code (body lang params &optional inline)
  66. (if inline
  67. (format "=%s=" body)
  68. (format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body
  69. (if (string-match "\n$" body) "" "\n"))))
  70. (defun org-babel-exp-results (body lang params &optional inline)
  71. ;; I expect there's a good reason why not, but would it be possible
  72. ;; to use org-babel-execute-src-block here? [ded]
  73. (let* ((cmd (intern (concat "org-babel-execute:" lang)))
  74. (result
  75. (multiple-value-bind (session vars result-params result-type)
  76. (org-babel-process-params params) (funcall cmd body params)))
  77. (result-as-org (org-babel-result-to-org-string result)))
  78. (if inline
  79. (format "=%s=" result)
  80. (if (stringp result)
  81. (format "#+BEGIN_EXAMPLE\n%s%s\n#+END_EXAMPLE" result
  82. (if (string-match "\n$" body) "" "\n"))
  83. result-as-org))))
  84. (provide 'org-babel-exp)
  85. ;;; org-babel-exp.el ends here