[OCaml] A basic OCaml Plugin for TeXmacs

Hello,
Here’s a basic OCaml plugin.
Thank to @jeroen for his remarks, debug and interest !!!
Cheers, Nicolas

Tools -> Update -> Plugins
Insert -> Session -> OCaml

Result inside TeXmacs:

Hello
Ocaml]  1+1;;
   - : int = 2
Ocaml]  let a = 10;;
   val a : int = 10
Ocaml]  let b = "bbb";;
   val b : string = "bbb"
Ocaml]  Printf.printf("Coucou");;
   Coucou
   - : unit = ()
Ocaml]  

Here are the files:

$TEXMACS_HOME_PATH/plugins/ocaml/src/tmrepl.ml

#use "topfind";;

let () = Toploop.initialize_toplevel_env ();;

let eval s =
  let lexbuf = Lexing.from_string s in
  let phrase = !Toploop.parse_toplevel_phrase lexbuf in
  let success = Toploop.execute_phrase true Format.err_formatter phrase in
  ignore success;;

let dDATA_BEGIN  = Char.chr  2;;
let dDATA_END    = Char.chr  5;;
let dDATA_ESCAPE = Char.chr 27;;

print_char dDATA_BEGIN;;
Printf.printf "verbatim:";;
Printf.printf "Hello";;
print_char dDATA_END;;
flush stdout;;

while true do
  let str = read_line () in
  eval str;
  print_char dDATA_BEGIN;
  Printf.printf "verbatim:";
  print_char dDATA_END;
  flush stdout;
done

$TEXMACS_HOME_PATH/plugins/ocaml/bin/tm_ocaml

#!/bin/bash
### REMOVE THIS LINE (from @jeroen): echo -ne "\002verbatim:"

# start OCaml for plugin session
ocaml $TEXMACS_HOME_PATH/plugins/ocaml/src/tmrepl.ml

$TEXMACS_HOME_PATH/plugins/ocaml/progs/init-ocaml.scm

(plugin-configure ocaml
  (:require (url-exists-in-path? "ocaml"))
  (:launch "tm_ocaml")
  (:session "OCaml"))
1 Like

Hi @Nicolas. It’s great to see a new plugin being developed!

It seems you are sending the “banner” message “hello” twice, once in the shell script tm_ocaml and once in tmrepl.ml. If you comment out the second line in tm_ocaml, it works for me. You don’t really need that script anyway, you can just start ocaml from init-ocaml.scm.

It would be nice if you could put this into a git repository, that makes it easier to test.

Yes, it work now !!!
Thank @jeroen
I was tricked by the Matlab’s plugin file tm_matlab
(line: echo -ne "\002verbatim:").
I modified my code above
Nicolas

Regarding the REPL functionality, there seems to be a Jupiter kernel for OCAML. Perhaps you can check how they evaluate expressions.

ok, I found:
https://github.com/akabe/ocaml-jupyter/tree/master/jupyter/src/repl


The code is complicated, I will try to do a much simpler version if possible.
I will try something this evening … DONE !!!
OCaml user’s group is not as friendly as the TeXmacs one.
I can’t expect much help from them !!!