Finished editing Babel docstrings
[org-mode.git] / lisp / ob-ocaml.el
blob5ca9b7e930ca4e4a021986d44cb4960fc1e38554
1 ;;; ob-ocaml.el --- org-babel functions for ocaml evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating ocaml source code. This one will
28 ;; be sort of tricky because ocaml programs must be compiled before
29 ;; they can be run, but ocaml code can also be run through an
30 ;; interactive interpreter.
32 ;; For now lets only allow evaluation using the ocaml interpreter.
34 ;;; Requirements:
36 ;; - tuareg-mode :: http://www-rocq.inria.fr/~acohen/tuareg/
38 ;;; Code:
39 (require 'ob)
40 (require 'ob-comint)
41 (require 'comint)
42 (eval-when-compile (require 'cl))
44 (declare-function tuareg-run-caml "ext:taureg" ())
46 (add-to-list 'org-babel-tangle-lang-exts '("ocaml" . "ml"))
48 (defvar org-babel-default-header-args:ocaml '())
50 (defvar org-babel-ocaml-eoe-indicator "\"org-babel-ocaml-eoe\";;")
51 (defvar org-babel-ocaml-eoe-output "org-babel-ocaml-eoe")
53 (defun org-babel-expand-body:ocaml (body params &optional processed-params)
54 "Expand BODY according to PARAMS, return the expanded body."
55 (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
56 (concat
57 (mapconcat
58 (lambda (pair) (format "let %s = %s;" (car pair) (cdr pair)))
59 vars "\n") "\n" body "\n")))
61 (defun org-babel-execute:ocaml (body params)
62 "Execute a block of Ocaml code with Babel."
63 (let* ((processed-params (org-babel-process-params params))
64 (vars (nth 1 processed-params))
65 (full-body (org-babel-expand-body:ocaml body params processed-params))
66 (session (org-babel-prep-session:ocaml
67 (cdr (assoc :session params)) params))
68 (raw (org-babel-comint-with-output
69 (session org-babel-ocaml-eoe-output t full-body)
70 (insert (concat (org-babel-chomp full-body) " ;;"))
71 (comint-send-input nil t)
72 (insert org-babel-ocaml-eoe-indicator)
73 (comint-send-input nil t))))
74 (org-babel-reassemble-table
75 (org-babel-ocaml-parse-output (org-babel-trim (car raw)))
76 (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
77 (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))))
79 (defvar tuareg-interactive-buffer-name)
80 (defun org-babel-prep-session:ocaml (session params)
81 "Prepare SESSION according to the header arguments in PARAMS."
82 (require 'tuareg)
83 (let ((tuareg-interactive-buffer-name (if (and (not (string= session "none"))
84 (not (string= session "default"))
85 (stringp session))
86 session
87 tuareg-interactive-buffer-name)))
88 (save-window-excursion (tuareg-run-caml)
89 (get-buffer tuareg-interactive-buffer-name))))
91 (defun org-babel-ocaml-parse-output (output)
92 "Parse OUTPUT.
93 OUTPUT is string output from an ocaml process."
94 (let ((regexp "%s = \\(.+\\)$"))
95 (cond
96 ((string-match (format regexp "string") output)
97 (org-babel-read (match-string 1 output)))
98 ((or (string-match (format regexp "int") output)
99 (string-match (format regexp "float") output))
100 (string-to-number (match-string 1 output)))
101 ((string-match (format regexp "list") output)
102 (org-babel-ocaml-read-list (match-string 1 output)))
103 ((string-match (format regexp "array") output)
104 (org-babel-ocaml-read-array (match-string 1 output)))
105 (t (message "don't recognize type of %s" output) output))))
107 (defun org-babel-ocaml-read-list (results)
108 "Convert RESULTS into an elisp table or string.
109 If the results look like a table, then convert them into an
110 Emacs-lisp table, otherwise return the results as a string."
111 (org-babel-read
112 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
113 (org-babel-read
114 (replace-regexp-in-string
115 "\\[" "(" (replace-regexp-in-string
116 "\\]" ")" (replace-regexp-in-string
117 "; " " " (replace-regexp-in-string
118 "'" "\"" results)))))
119 results)))
121 (defun org-babel-ocaml-read-array (results)
122 "Convert RESULTS into an elisp table or string.
123 If the results look like a table, then convert them into an
124 Emacs-lisp table, otherwise return the results as a string."
125 (org-babel-read
126 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
127 (org-babel-read
128 (replace-regexp-in-string
129 "\\[|" "(" (replace-regexp-in-string
130 "|\\]" ")" (replace-regexp-in-string
131 "; " " " (replace-regexp-in-string
132 "'" "\"" results)))))
133 results)))
135 (provide 'ob-ocaml)
137 ;; arch-tag: 2e815f4d-365e-4d69-b1df-dd17fdd7b7b7
139 ;;; ob-ocaml.el ends here