Merge branch 'maint'
[org-mode.git] / lisp / ob-ocaml.el
blobfd0ddf8ab7fef7103e97b3e74d2faaea4c1a5cd2
1 ;;; ob-ocaml.el --- Babel Functions for Ocaml -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2017 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 <https://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)
41 (declare-function tuareg-run-caml "ext:tuareg" ())
42 (declare-function tuareg-run-ocaml "ext:tuareg" ())
43 (declare-function tuareg-interactive-send-input "ext:tuareg" ())
44 (declare-function org-trim "org" (s &optional keep-lead))
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* ((full-body (org-babel-expand-body:generic
64 body params
65 (org-babel-variable-assignments:ocaml params)))
66 (session (org-babel-prep-session:ocaml
67 (cdr (assq :session params)) params))
68 (raw (org-babel-comint-with-output
69 (session org-babel-ocaml-eoe-output nil full-body)
70 (insert
71 (concat
72 (org-babel-chomp full-body) ";;\n"
73 org-babel-ocaml-eoe-indicator))
74 (tuareg-interactive-send-input)))
75 (clean
76 (car (let ((re (regexp-quote org-babel-ocaml-eoe-output)) out)
77 (delq nil (mapcar (lambda (line)
78 (if out
79 (progn (setq out nil) line)
80 (when (string-match re line)
81 (progn (setq out t) nil))))
82 (mapcar #'org-trim (reverse raw)))))))
83 (raw (org-trim clean))
84 (result-params (cdr (assq :result-params params))))
85 (string-match
86 "\\(\\(.*\n\\)*\\)[^:\n]+ : \\([^=\n]+\\) =\\(\n\\| \\)\\(.+\\)$"
87 raw)
88 (let ((output (match-string 1 raw))
89 (type (match-string 3 raw))
90 (value (match-string 5 raw)))
91 (org-babel-reassemble-table
92 (org-babel-result-cond result-params
93 (cond
94 ((member "verbatim" result-params) raw)
95 ((member "output" result-params) output)
96 (t raw))
97 (if (and value type)
98 (org-babel-ocaml-parse-output value type)
99 raw))
100 (org-babel-pick-name
101 (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
102 (org-babel-pick-name
103 (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))))
105 (defvar tuareg-interactive-buffer-name)
106 (defun org-babel-prep-session:ocaml (session _params)
107 "Prepare SESSION according to the header arguments in PARAMS."
108 (require 'tuareg)
109 (let ((tuareg-interactive-buffer-name (if (and (not (string= session "none"))
110 (not (string= session "default"))
111 (stringp session))
112 session
113 tuareg-interactive-buffer-name)))
114 (save-window-excursion (if (fboundp 'tuareg-run-process-if-needed)
115 (tuareg-run-process-if-needed org-babel-ocaml-command)
116 (tuareg-run-caml)))
117 (get-buffer tuareg-interactive-buffer-name)))
119 (defun org-babel-variable-assignments:ocaml (params)
120 "Return list of ocaml statements assigning the block's variables."
121 (mapcar
122 (lambda (pair) (format "let %s = %s;;" (car pair)
123 (org-babel-ocaml-elisp-to-ocaml (cdr pair))))
124 (org-babel--get-vars params)))
126 (defun org-babel-ocaml-elisp-to-ocaml (val)
127 "Return a string of ocaml code which evaluates to VAL."
128 (if (listp val)
129 (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml val "; ") "|]")
130 (format "%S" val)))
132 (defun org-babel-ocaml-parse-output (value type)
133 "Parse VALUE of type TYPE.
134 VALUE and TYPE are string output from an ocaml process."
135 (cond
136 ((string= "string" type)
137 (org-babel-read value))
138 ((or (string= "int" type)
139 (string= "float" type))
140 (string-to-number value))
141 ((string-match "list" type)
142 (org-babel-ocaml-read-list value))
143 ((string-match "array" type)
144 (org-babel-ocaml-read-array value))
145 (t (message "don't recognize type %s" type) value)))
147 (defun org-babel-ocaml-read-list (results)
148 "Convert RESULTS into an elisp table or string.
149 If the results look like a table, then convert them into an
150 Emacs-lisp table, otherwise return the results as a string."
151 ;; XXX: This probably does not behave as expected when a semicolon
152 ;; is in a string in a list. The same comment applies to
153 ;; `org-babel-ocaml-read-array' below (with even more failure
154 ;; modes).
155 (org-babel-script-escape (replace-regexp-in-string ";" "," results)))
157 (defun org-babel-ocaml-read-array (results)
158 "Convert RESULTS into an elisp table or string.
159 If the results look like a table, then convert them into an
160 Emacs-lisp table, otherwise return the results as a string."
161 (org-babel-script-escape
162 (replace-regexp-in-string
163 "\\[|" "[" (replace-regexp-in-string
164 "|\\]" "]" (replace-regexp-in-string
165 "; " "," results)))))
167 (provide 'ob-ocaml)
171 ;;; ob-ocaml.el ends here