org-babel-ocaml.el 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ;;; org-babel-ocaml.el --- org-babel functions for ocaml evaluation
  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. ;; Org-Babel support for evaluating ocaml source code. This one will
  24. ;; be sort of tricky because ocaml programs must be compiled before
  25. ;; they can be run, but ocaml code can also be run through an
  26. ;; interactive interpreter.
  27. ;;
  28. ;; For now lets only allow evaluation using the ocaml interpreter.
  29. ;;; Requirements:
  30. ;; - tuareg-mode :: http://www-rocq.inria.fr/~acohen/tuareg/
  31. ;;; Code:
  32. (require 'org-babel)
  33. (require 'tuareg)
  34. (org-babel-add-interpreter "ocaml")
  35. (add-to-list 'org-babel-tangle-langs '("ocaml" "ml"))
  36. (defvar org-babel-ocaml-eoe-indicator "\"org-babel-ocaml-eoe\";;")
  37. (defvar org-babel-ocaml-eoe-output "- : string = \"org-babel-ocaml-eoe\"")
  38. (defun org-babel-execute:ocaml (body params)
  39. "Execute a block of Ocaml code with org-babel. This function
  40. is called by `org-babel-execute-src-block' with the following
  41. variables pre-set using `multiple-value-bind'.
  42. (session vars result-params result-type)"
  43. (message "executing Ocaml source code block")
  44. (let* ((full-body (concat
  45. (mapconcat
  46. (lambda (pair) (format "let %s = %s;" (car pair) (cdr pair)))
  47. vars "\n") "\n" body "\n"))
  48. (session (get-buffer (org-babel-prep-session:ocaml session params)))
  49. (raw (org-babel-comint-with-output session org-babel-ocaml-eoe-output t
  50. (insert full-body)
  51. (insert ";;")
  52. (comint-send-input nil t)
  53. (insert org-babel-ocaml-eoe-indicator)
  54. (comint-send-input nil t))))
  55. (org-babel-trim (car raw))))
  56. (defun org-babel-prep-session:ocaml (session params)
  57. "Prepare SESSION according to the header arguments specified in PARAMS."
  58. (tuareg-run-caml) tuareg-interactive-buffer-name)
  59. (provide 'org-babel-ocaml)
  60. ;;; org-babel-ocaml.el ends here