5

I have added a few lines to my .ocamlinit to make the utop prompt less of an eyesore:

#require "react";;
#require "lambda-term";;
UTop.prompt := fst (React.S.create LTerm_text.(eval [
    S "\n";
    B_bold true;
    B_fg (LTerm_style.cyan);
    S "# ";
]));;

(I don't understand everything that is going on there... I just found it posted online and tweaked it.)

But now if I use the regular ocaml toplevel I get:

Error: Unbound module UTop

Can I add a test to .ocamlinit to skip those lines unless I'm running utop? Can I put them in an additional file that is only loaded by utop?

Tobia
  • 17,856
  • 6
  • 74
  • 93

1 Answers1

1

What if you add #require "utop";; ?

Pierre G.
  • 4,346
  • 1
  • 12
  • 25
  • I see this error: `Exception: Invalid_argument "The ocamltoplevel.cma library from compiler-libs cannot be loaded inside the OCaml toplevel".` and then `File ".ocamlinit", line 1: Error: Reference to undefined global Thread` (I skipped a bunch of lines like `/usr/local/lib/ocaml/unix.cma: loaded`). That was my first guess as well :) – Anton Trunov Jun 27 '17 at 13:52
  • hmmm, it works on my side (updated .ocamlinit like you). I am using ocaml 4.04.0. – Pierre G. Jun 27 '17 at 13:54
  • I'm on OCaml 4.04.1. I brushed my config a bit, but I still see `/usr/local/lib/ocaml/compiler-libs/ocamltoplevel.cma: loaded Exception: Invalid_argument "The ocamltoplevel.cma library from compiler-libs cannot be loaded inside the OCaml toplevel".` when starting the standard toplevel, but utop starts fine. – Anton Trunov Jun 27 '17 at 14:17
  • On my system it does work. But I was hoping to avoid loading 14 additional modules* when I don't need them, such as when running scripts. * camomile, ocamlcommon, ocamlbytecomp, ocamltoplevel, findlib, findlib_top, lwt, lwt-log, lwt-unix, react, lwt_react, zed, lambda_term, uTop – Tobia Jun 27 '17 at 19:04
  • 2
    @Tobia (1) A workaround (not a very nice though) is to divide your `.ocamlinit` into two files: one for utop and another for the standard toplevel and e.g. make an alias for `ocaml` passing `-init ` to the original toplevel. – Anton Trunov Jun 27 '17 at 20:09
  • 2
    @Tobia (2) In principle we could test `Sys.executable_name` in OCaml code, but there is the problem with `#require` and other directives (this is not valid OCaml code). – Anton Trunov Jun 27 '17 at 20:14
  • @AntonTrunov I see. I will keep the #require and I will make a different init file if I ever need to. Thanks – Tobia Jun 27 '17 at 21:14
  • 2
    Ideally, utop would use a different init file thank ocaml--let's call it .utoprc--or look for .utoprc before loading .ocamlinit. People who wanted the same configuration in both could use come kind of include command to run another file from inside .utoprc. – Mars Jun 29 '17 at 15:04