Ledit is an OCaml readline replacement, capable of being used either as a standalone wrapper for line-based interactive programs, or included in your program as a library. This tutorial describes the latter use.

download the ledit sources

Get the ledit sources from the main FTP site, or use your distribution's package if one is provided.

fix the makefile and make

~ $  cd ledit-1.11
~/ledit-1.11 $  perl -pi -e 's/-o pa_local.ppo/-o pa_local.ppo -loc loc/' Makefile
~/ledit-1.11 $  make
~/ledit-1.11 $  make ledit.cmx # make the .cmx library too if desired

adding ledit to your project

Now let's write a simple readline program to demonstrate the use of the library.

let readline prompt =
  Ledit.set_prompt prompt;
  let buf = Buffer.create 256 in
  let rec loop c = match c with
  | '\n' -> Buffer.contents buf
  | _    -> Buffer.add_char buf c; loop (Ledit.input_char stdin)
  loop (Ledit.input_char stdin);;

let quit_loop = ref false in
print_endline "Ledit demo. Enter '#quit' or Ctrl-D to exit";
print_endline "-------------------------------------------";
flush stdout;
try while not !quit_loop do
  let str = readline "Say something> " in
  if str = "#quit" then
    quit_loop := true
with End_of_file -> print_newline ();;
~/readline $ ocamlopt unix.cmxa cursor.cmx ledit.cmx readline.ml -o readline
~/readline $ ./readlineLedit demo. Enter '#quit' or Ctrl-D to exit
Say something> fdgdfg
Say something> dfgsdfgs
Say something> fdb
Say something> #quit
~/readline $

Automating the build process

With a few small changes, Ledit can be made to compile and install automatically using OCamlMakefile (Compiling with GNU make). See Ledit with OCamlMakefile for details.