experimental patches to utop
Go to file
Jeremie Dimino 9e41bf85da Better check for async rewrite
Resolve the types Async.Std.Deferred.t instead of hard-coding a list
of aliases.

Fix #137
2016-01-07 10:30:53 +00:00
examples/custom-utop Fix build of the custom-utop example 2015-11-23 10:38:40 +00:00
man allow to configure the external editor 2015-10-29 10:07:28 +00:00
src Better check for async rewrite 2016-01-07 10:30:53 +00:00
.gitignore move to git 2012-10-17 11:32:11 +02:00
CHANGES.md update CHANGES 2015-06-23 09:57:34 +01:00
LICENSE fix LICENSE date 2011-08-25 10:22:13 +02:00
Makefile Expose API documentation on github pages. 2014-10-28 03:40:23 +03:00
README.md Fix cppo link in readme (closes #133) 2015-05-24 01:52:42 +03:00
_oasis bump version to 1.18 2015-06-23 09:57:51 +01:00
_tags build with trunk 2015-03-18 16:47:29 +00:00
configure Add a Makefile 2012-10-17 11:36:33 +02:00
dist Remove --force from git add options in dist. 2014-10-22 18:47:05 +04:00
myocamlbuild.ml Use tuple comparisons, present since cppo 1.1.2. 2014-11-17 17:58:12 +03:00
opam Add base-unix and base-threads to opam depends. 2014-12-20 22:02:16 +03:00
setup.ml Add a Makefile 2012-10-17 11:36:33 +02:00
style.css Expose API documentation on github pages. 2014-10-28 03:40:23 +03: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 for OCaml

utop is an improved toplevel for OCaml. It can run in a terminal or in Emacs. It supports line edition, history, real-time and context sensitive completion, colors, and more.

It integrates with the tuareg and typerex modes in Emacs.

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

For building the development version, you also need to install oasis (>= 0.4.0).

Installation from sources

To build and install utop:

$ ./configure
$ make
$ make install

If you want to be able to use camlp4, rather use:

$ ./configure --enable-camlp4

Documentation and manual pages (optional)

To build the documentation:

$ make doc

It will then be installed by make install.

Tests (optional)

To build and execute tests:

$ ./configure --enable-tests
$ 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.

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

UTop API

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

Integration with emacs

To use utop in emacs, first you need to make sure emacs can find the command utop and the file utop.el. If you installed utop via opam you can copy-paste this code into you ~/.emacs file:

;; 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)

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.

Creating a custom utop-enabled toplevel

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.