Erase `org-insert-labeled-timestamps-at-point'
[org-mode.git] / lisp / ob-ocaml.el
blob24391504eb0009f7970da1e5cbab71a0d797c85e
1 ;;; ob-ocaml.el --- org-babel functions for ocaml evaluation
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating ocaml source code. This one will
27 ;; be sort of tricky because ocaml programs must be compiled before
28 ;; they can be run, but ocaml code can also be run through an
29 ;; interactive interpreter.
31 ;; For now lets only allow evaluation using the ocaml interpreter.
33 ;;; Requirements:
35 ;; - tuareg-mode :: http://www-rocq.inria.fr/~acohen/tuareg/
37 ;;; Code:
38 (require 'ob)
39 (require 'comint)
40 (eval-when-compile (require 'cl))
42 (declare-function tuareg-run-caml "ext:tuareg" ())
43 (declare-function tuareg-run-ocaml "ext:tuareg" ())
44 (declare-function tuareg-interactive-send-input "ext:tuareg" ())
46 (defvar org-babel-tangle-lang-exts)
47 (add-to-list 'org-babel-tangle-lang-exts '("ocaml" . "ml"))
49 (defvar org-babel-default-header-args:ocaml '())
51 (defvar org-babel-ocaml-eoe-indicator "\"org-babel-ocaml-eoe\";;")
52 (defvar org-babel-ocaml-eoe-output "org-babel-ocaml-eoe")
54 (defcustom org-babel-ocaml-command "ocaml"
55 "Name of the command for executing Ocaml code."
56 :version "24.4"
57 :package-version '(Org . "8.0")
58 :group 'org-babel
59 :type 'string)
61 (defun org-babel-execute:ocaml (body params)
62 "Execute a block of Ocaml code with Babel."
63 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
64 (full-body (org-babel-expand-body:generic
65 body params
66 (org-babel-variable-assignments:ocaml params)))
67 (session (org-babel-prep-session:ocaml
68 (cdr (assoc :session params)) params))
69 (raw (org-babel-comint-with-output
70 (session org-babel-ocaml-eoe-output t full-body)
71 (insert
72 (concat
73 (org-babel-chomp full-body) ";;\n"
74 org-babel-ocaml-eoe-indicator))
75 (tuareg-interactive-send-input)))
76 (clean
77 (car (let ((re (regexp-quote org-babel-ocaml-eoe-output)) out)
78 (delq nil (mapcar (lambda (line)
79 (if out
80 (progn (setq out nil) line)
81 (when (string-match re line)
82 (progn (setq out t) nil))))
83 (mapcar #'org-babel-trim (reverse raw)))))))
84 (raw (org-babel-trim clean))
85 (result-params (cdr (assoc :result-params params)))
86 (parsed
87 (string-match
88 "\\(\\(.*\n\\)*\\)[^:\n]+ : \\([^=\n]+\\) =\\(\n\\| \\)\\(.+\\)$"
89 raw))
90 (output (match-string 1 raw))
91 (type (match-string 3 raw))
92 (value (match-string 5 raw)))
93 (org-babel-reassemble-table
94 (org-babel-result-cond result-params
95 (cond
96 ((member "verbatim" result-params) raw)
97 ((member "output" result-params) output)
98 (t raw))
99 (if (and value type)
100 (org-babel-ocaml-parse-output value type)
101 raw))
102 (org-babel-pick-name
103 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
104 (org-babel-pick-name
105 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
107 (defvar tuareg-interactive-buffer-name)
108 (defun org-babel-prep-session:ocaml (session params)
109 "Prepare SESSION according to the header arguments in PARAMS."
110 (require 'tuareg)
111 (let ((tuareg-interactive-buffer-name (if (and (not (string= session "none"))
112 (not (string= session "default"))
113 (stringp session))
114 session
115 tuareg-interactive-buffer-name)))
116 (save-window-excursion (if (fboundp 'tuareg-run-process-if-needed)
117 (tuareg-run-process-if-needed org-babel-ocaml-command)
118 (tuareg-run-caml)))
119 (get-buffer tuareg-interactive-buffer-name)))
121 (defun org-babel-variable-assignments:ocaml (params)
122 "Return list of ocaml statements assigning the block's variables."
123 (mapcar
124 (lambda (pair) (format "let %s = %s;;" (car pair)
125 (org-babel-ocaml-elisp-to-ocaml (cdr pair))))
126 (mapcar #'cdr (org-babel-get-header params :var))))
128 (defun org-babel-ocaml-elisp-to-ocaml (val)
129 "Return a string of ocaml code which evaluates to VAL."
130 (if (listp val)
131 (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml val "; ") "|]")
132 (format "%S" val)))
134 (defun org-babel-ocaml-parse-output (value type)
135 "Parse VALUE of type TYPE.
136 VALUE and TYPE are string output from an ocaml process."
137 (cond
138 ((string= "string" type)
139 (org-babel-read value))
140 ((or (string= "int" type)
141 (string= "float" type))
142 (string-to-number value))
143 ((string-match "list" type)
144 (org-babel-ocaml-read-list value))
145 ((string-match "array" type)
146 (org-babel-ocaml-read-array value))
147 (t (message "don't recognize type %s" type) value)))
149 (defun org-babel-ocaml-read-list (results)
150 "Convert RESULTS into an elisp table or string.
151 If the results look like a table, then convert them into an
152 Emacs-lisp table, otherwise return the results as a string."
153 (org-babel-script-escape (replace-regexp-in-string ";" "," results)))
155 (defun org-babel-ocaml-read-array (results)
156 "Convert RESULTS into an elisp table or string.
157 If the results look like a table, then convert them into an
158 Emacs-lisp table, otherwise return the results as a string."
159 (org-babel-script-escape
160 (replace-regexp-in-string
161 "\\[|" "[" (replace-regexp-in-string
162 "|\\]" "]" (replace-regexp-in-string
163 "; " "," results)))))
165 (provide 'ob-ocaml)
169 ;;; ob-ocaml.el ends here