experimental patches to utop
Go to file
Christian Lindig c0054c41b2 Document utop(1) options in manual page
Add all options as decribed by the output by --help to the manual page.
This was done manually and some automation would be nicer.

Signed-off-by: Christian Lindig <christian.lindig@citrix.com>
2021-12-02 17:47:48 -05:00
.github Setup code owners 2018-08-20 14:31:13 +01:00
examples port build from jbuilder to dune 2019-01-15 09:50:54 +00:00
man Document utop(1) options in manual page 2021-12-02 17:47:48 -05:00
src Add support for OCaml 4.14 2021-11-18 10:10:53 -05:00
.gitignore ignore vim swapfiles 2019-01-31 10:32:13 +00:00
.ocp-indent Switch to jbuilder 2017-04-04 13:36:28 +01:00
.travis.yml Test OCaml 4.11 in CI 2020-05-09 09:38:47 +08:00
CHANGES.md update CHANGES and fixes to opam file to pass lint 2021-06-18 09:31:46 +01:00
LICENSE fix LICENSE date 2011-08-25 10:22:13 +02:00
Makefile remove `release` target as `dune-release` can be used now 2019-01-31 10:31:59 +00:00
README.md update README 2020-06-01 12:10:01 +08:00
dune port build from jbuilder to dune 2019-01-15 09:50:54 +00:00
dune-project Add dune-project 2018-08-29 18:46:43 +01:00
dune-workspace.dev port build from jbuilder to dune 2019-01-15 09:50:54 +00:00
screenshot.png Add screenshot (#259) 2018-09-24 11:48:27 -05:00
utop.opam update CHANGES and fixes to opam file to pass lint 2021-06-18 09:31:46 +01:00
utoprc-dark fix startup files autoloading and allow to disable it 2013-11-26 13:22:36 +00:00
utoprc-light fix startup files autoloading and allow to disable it 2013-11-26 13:22:36 +00:00

README.md

utop — a universal toplevel (i.e., REPL) for OCaml

utop is an improved toplevel (i.e., Read-Eval-Print Loop) for OCaml. It can run in a terminal or in Emacs. It supports line editing, history, real-time and context sensitive completion, colors, and more.

It integrates with the Tuareg and typerex modes in Emacs.

Travis build Status

Screenshot

Installation via opam

The easiest and recommended way of installing utop is via opam:

$ opam install utop

If you want to build it manually, you should install all the dependencies listed in the next section.

Dependencies

Installation from sources

To build and install utop:

$ make
$ make install

Documentation and manual pages (optional)

To build the documentation (currently broken):

$ make doc

It will then be installed by make install.

Tests (optional)

To build and execute tests (currently broken):

$ make test

Usage

To use utop, simply run:

$ utop

utop display a bar after the prompt which is used to show possible completions in real-time. You can navigate in it using M-left and M-right, and select one completion using M-tab. The M denotes the meta key, which is Alt most of the time.

Customization

Colors

To add colors to utop, copy one of the files utoprc-dark or utoprc-light to ~/.utoprc. utoprc-dark is for terminals with dark colors (such as white on black) and utoprc-light is for terminals with light colors (such as black on white).

Prompt

You can customize the prompt of utop by setting the reference UTop.prompt.

To turn off all colors and remove the line above the prompt that lists time, etc., add this to ~/.config/utop/init.ml:

#utop_prompt_dummy;;

To turn off the line of boxes listing possible completions that appears under the prompt, add this to ~/.config/utop/init.ml:

UTop.set_show_box false

Key bindings

Key bindings in the terminal can be changed by writing a ~/.lambda-term-inputrc file. For example:

[read-line]
C-left: complete-bar-prev
C-right: complete-bar-next
C-down: complete-bar

If manual pages are correctly installed you can see a description of this file by executing:

$ man 5 lambda-term-inputrc

Vi edit mode

You can turn on the vi edit mode by #edit_mode_vi. It currently supports three vi modes: normal, insert, visual mode, and you can get/set content with vim-like registers.

This special edit mode is evolving rapidly, see the CHANGES of lambda-term for the rapidly changing information.

UTop API

UTop exposes several more settings through its API; see documentation.

Integration with emacs

Main setup

To use utop in emacs, first you need to make sure emacs can find the command utop and the file utop.el. utop.el is available through melpa, so M-x package-install RET utop RET should do.

If this doesn't work and you installed utop via opam, you can add this to your ~/.emacs:

;; Add the opam lisp dir to the emacs load path
(add-to-list
 'load-path
 (replace-regexp-in-string
  "\n" "/share/emacs/site-lisp"
  (shell-command-to-string "opam config var prefix")))

;; Automatically load utop.el
(autoload 'utop "utop" "Toplevel for OCaml" t)

In any case, if you installed utop via opam you should add this to your ~/.emacs:

;; Use the opam installed utop
(setq utop-command "opam config exec -- utop -emacs")

If you use dune and want to launch dune utop in emacs, you should add this to your ~/.emacs:

(setq utop-command "opam config exec -- dune utop . -- -emacs")

This was tested with opam 1.2. For older versions of opam, you can copy&paste this to your ~/.emacs:

;; Setup environment variables using opam
(dolist (var (car (read-from-string (shell-command-to-string "opam config env --sexp"))))
  (setenv (car var) (cadr var)))

;; Update the emacs path
(setq exec-path (append (parse-colon-path (getenv "PATH"))
                        (list exec-directory)))

;; Update the emacs load path
(add-to-list 'load-path (expand-file-name "../../share/emacs/site-lisp"
                                          (getenv "OCAML_TOPLEVEL_PATH")))

;; Automatically load utop.el
(autoload 'utop "utop" "Toplevel for OCaml" t)

Usage

Then you can execute utop inside emacs with: M-x utop.

utop also ships with a minor mode that has the following key-bindings

key-binding function Description
C-c C-s utop Start a utop buffer
C-x C-e utop-eval-phrase Evaluate the current phrase
C-x C-r utop-eval-region Evaluate the selected region
C-c C-b utop-eval-buffer Evaluate the current buffer
C-c C-k utop-kill Kill a running utop process

You can enable the minor mode using M-x utop-minor-mode, or you can have it enabled by default with the following configuration:

(autoload 'utop-minor-mode "utop" "Minor mode for utop" t)
(add-hook 'tuareg-mode-hook 'utop-minor-mode)

If you plan to use utop with another major-mode than tuareg, replace tuareg-mode-hook by the appropriate hook. The utop minor mode will work out of the box with these modes: tuareg-mode, caml-mode and typerex-mode. For other modes you will need to set the following three variables:

  • utop-skip-blank-and-comments
  • utop-skip-to-end-of-phrase
  • utop-discover-phrase

You can also complete text in a buffer using the environment of the toplevel. For that bind the function utop-edit-complete to the key you want.

Common error

If you get this error when running utop in a terminal or in emacs this means that the environment variable CAML_LD_LIBRARY_PATH is not set correctly:

Fatal error: cannot load shared library dlllwt-unix_stubs
Reason: dlopen(dlllwt-unix_stubs.so, 138): image not found

It shall point to the directory stublibs inside your ocaml installation.

Automatically installing toplevel printers

Utop will automatically install toplevel printers for custom types if their interface file is marked with an [@@ocaml.toplevel_printer] attribute. Adding this annotation to your libraries will remove the need to have a separate top package to install the printers.

For example, in the uri library, the old printing function for Uri.t was:

val pp_hum : Format.formatter -> t -> unit

Just adding this annotation results in Uri.t values being automatically pretty printed in this version of utop.

val pp_hum : Format.formatter -> t -> unit [@@ocaml.toplevel_printer]

There should be no downsides to adding this attribute to your libraries, so we encourage community library maintainers to use this attribute to improve the out-of-the-box experience for users of their libraries within utop.

Creating a custom utop-enabled toplevel

With Dune

The recommended way to build a custom utop toplevel is via Dune. The entry point of the custom utop must call UTop_main.main. For instance write the following myutop.ml file:

let () = UTop_main.main ()

and the following dune file:

(executable
 (name myutop)
 (link_flags -linkall)
 (libraries utop))

then to build the toplevel, run:

$ dune myutop.bc

Note the -linkall in the link flags. By default OCaml doesn't link unused modules, however for a toplevel you don't know in advance what the user is going to use so you must link everything.

If you want to include more libraries in your custom utop, simply add them to the (libraries ...) field.

Additionally, if you want to install this toplevel, add the two following fields to the executable stanza:

  (public_name myutop)
  (modes byte)

The (modes ...) field is to tell dune to install the byte-code version of the executable, as currently native toplevels are not fully suported.

Manually, with ocamlfind

This section describe methods using ocamlfind. These are no longer tested, so there is no guarantee they still work.

If you want to create a custom toplevel with utop instead of the classic one you need to link it with utop and its dependencies and call UTop_main.main in the last linked unit. You also need to pass the -thread switch when linking the toplevel.

The easiest way to do that is by using ocamlfind:

$ ocamlfind ocamlmktop -o myutop -thread -linkpkg -package utop myutop_main.cmo

Where myutop_main.ml contains:

let () = UTop_main.main ()

You can also use the ocamlc sub-command instead of ocamlmktop, in this case you need to pass these thee extra arguments:

  • -linkall to be sure all units are linked into the produced toplevel
  • -package compiler-libs.toplevel
  • -predicates create_toploop

With the last option ocamlfind will generate a small ocaml unit, linked just before myutop_main.cmo, which will register at startup packages already linked in the toplevel so they are not loaded again by the #require directive. It does the same with the ocamlmktop sub-command.

For example:

$ ocamlfind ocamlc -o myutop -thread -linkpkg -linkall -predicates create_toploop \
    -package compiler-libs.toplevel,utop myutop.cmo

Note that if you are not using ocamlfind, you will need to do that yourself. You have to call Topfind.don't_load with the list of all packages linked with the toplevel.

A full example using ocamlbuild is provided in the examples/custom-utop directory.