caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] ocaml top-level line editing script
@ 2012-07-04 17:06 Aaron Bohannon
  2012-07-04 17:56 ` "Markus W. Weißmann"
  2012-07-04 18:24 ` rixed
  0 siblings, 2 replies; 6+ messages in thread
From: Aaron Bohannon @ 2012-07-04 17:06 UTC (permalink / raw)
  To: OCaml List

[-- Attachment #1: Type: text/plain, Size: 914 bytes --]

Hi,

One thing that is missing from the OCaml top level loop is line
editing features.  I used to use a wrapper program called "ledit" that
added readline support, but I never seem to have it installed on my
machine when I need it, and moreover, I have started to get used to
the more flexible line editing of zsh.

After some poking around, I found that it's fairly straightforward to
wrap any REPL with a zsh script and get zsh line editing capabilities,
which includes multi-line support (e.g., so you can fix a typo you
made three lines back).

I'm attaching the script I hacked together, in case it is useful to
anyone else.  It works great for me, but it was not written to be
generic and robust, so YMMV.  You must use ctrl-D to exit cleanly -- I
didn't bother adding support for "#quit".  I'm sure someone could make
many improvements to it if they had more patience for zsh scripting
than I do.

 - Aaron

[-- Attachment #2: zcaml --]
[-- Type: application/octet-stream, Size: 1173 bytes --]

#!/usr/bin/env zsh

# Simple wrapper function for "ocaml" that adds zsh line editing capabilities.
# Based on the "nslookup" example distributed with zsh.

setopt localoptions localtraps

local input output

zmodload -e zsh/zpty || zmodload -i zsh/zpty

trap 'return 130' INT
trap 'zpty -d ocaml' EXIT

zpty ocaml command ocaml

zpty -r ocaml output '*
# '
printf "%s" "$output"

bindkey -N viins-copy viins
bindkey -N vicmd-copy vicmd

# some mappings that I like
bindkey -M viins-copy "^A" vi-beginning-of-line
bindkey -M viins-copy "^E" vi-end-of-line
bindkey -M viins-copy "^P" history-beginning-search-backward
bindkey -M viins-copy "^N" history-beginning-search-forward

# use ctrl-\ to enter a newline -- afterward, you can still edit previous lines
bindkey -M viins-copy -s "^\\" "^V^J"

while input=''; vared -he -p '# ' -M viins-copy -m vicmd-copy input; do
  # add line to zsh history
  print -s "$input"
  # break on ctrl-D (I think?)
  [[ "$input" = EXIT ]] && break
  # ";;" is added because this loop will hang if we don't get back to a prompt
  zpty -w ocaml "$input;;"
  zpty -r ocaml output '(|*
)# '
  printf "%s" "$output"
done

zpty -w ocaml '#quit;;'

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] ocaml top-level line editing script
  2012-07-04 17:06 [Caml-list] ocaml top-level line editing script Aaron Bohannon
@ 2012-07-04 17:56 ` "Markus W. Weißmann"
  2012-07-05  1:46   ` Aaron Bohannon
  2012-07-04 18:24 ` rixed
  1 sibling, 1 reply; 6+ messages in thread
From: "Markus W. Weißmann" @ 2012-07-04 17:56 UTC (permalink / raw)
  To: Aaron Bohannon; +Cc: OCaml List

rlwrap? [1] Its probably already available through your favorite package manger.


-Markus

[1] http://utopia.knoware.nl/~hlub/uck/rlwrap/

On 4 Jul 2012, at 19:06, Aaron Bohannon wrote:

> Hi,
> 
> One thing that is missing from the OCaml top level loop is line
> editing features.  I used to use a wrapper program called "ledit" that
> added readline support, but I never seem to have it installed on my
> machine when I need it, and moreover, I have started to get used to
> the more flexible line editing of zsh.
> 
> After some poking around, I found that it's fairly straightforward to
> wrap any REPL with a zsh script and get zsh line editing capabilities,
> which includes multi-line support (e.g., so you can fix a typo you
> made three lines back).
> 
> I'm attaching the script I hacked together, in case it is useful to
> anyone else.  It works great for me, but it was not written to be
> generic and robust, so YMMV.  You must use ctrl-D to exit cleanly -- I
> didn't bother adding support for "#quit".  I'm sure someone could make
> many improvements to it if they had more patience for zsh scripting
> than I do.
> 
> - Aaron
> 
> -- 
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 
> <zcaml>

-- 
Markus Weißmann, M.Sc.
Technische Universität München
Institut für Informatik
Boltzmannstr. 3
D-85748 Garching
Germany
http://wwwknoll.in.tum.de/


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] ocaml top-level line editing script
  2012-07-04 17:06 [Caml-list] ocaml top-level line editing script Aaron Bohannon
  2012-07-04 17:56 ` "Markus W. Weißmann"
@ 2012-07-04 18:24 ` rixed
  2012-07-04 21:19   ` Anthony Tavener
  1 sibling, 1 reply; 6+ messages in thread
From: rixed @ 2012-07-04 18:24 UTC (permalink / raw)
  To: OCaml List

Interresting, although rlwrap gives you history, filename completion,
prompt coloring, syntax completion, brackets matching, use of an
external editor and easy configuration via .inputrc.

For instance I have this in my .zshrc:

alias ocaml='rlwrap --prompt-colour=green --multi-line --remember --complete-filenames --command-name=ocaml ocaml -rectypes'

And this in my .inputrc:

$if ocaml
    "\C-o": "()\C-b"
    "\C-n": ";;\n"
$endif

While the default C-^ launches vi (for multi-line edits)

I'm confident others have much more gorgeous inputrcs, though. Please share :-)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] ocaml top-level line editing script
  2012-07-04 18:24 ` rixed
@ 2012-07-04 21:19   ` Anthony Tavener
  0 siblings, 0 replies; 6+ messages in thread
From: Anthony Tavener @ 2012-07-04 21:19 UTC (permalink / raw)
  To: rixed; +Cc: OCaml List

[-- Attachment #1: Type: text/plain, Size: 1121 bytes --]

Oh geez... I've only been using: alias ocaml='rlwrap ocaml'. I didn't RTFM
on rlwrap.

A colored prompt helps, and multi-line edit! Thanks! I'll leave -rectypes
as a default for crazy people though. ;)


On Wed, Jul 4, 2012 at 12:24 PM, <rixed@happyleptic.org> wrote:

> Interresting, although rlwrap gives you history, filename completion,
> prompt coloring, syntax completion, brackets matching, use of an
> external editor and easy configuration via .inputrc.
>
> For instance I have this in my .zshrc:
>
> alias ocaml='rlwrap --prompt-colour=green --multi-line --remember
> --complete-filenames --command-name=ocaml ocaml -rectypes'
>
> And this in my .inputrc:
>
> $if ocaml
>     "\C-o": "()\C-b"
>     "\C-n": ";;\n"
> $endif
>
> While the default C-^ launches vi (for multi-line edits)
>
> I'm confident others have much more gorgeous inputrcs, though. Please
> share :-)
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>

[-- Attachment #2: Type: text/html, Size: 1841 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] ocaml top-level line editing script
  2012-07-04 17:56 ` "Markus W. Weißmann"
@ 2012-07-05  1:46   ` Aaron Bohannon
  2012-07-05  4:25     ` Aaron Bohannon
  0 siblings, 1 reply; 6+ messages in thread
From: Aaron Bohannon @ 2012-07-05  1:46 UTC (permalink / raw)
  To: Markus W. Weißmann; +Cc: OCaml List

Ah.  Thanks for the tip.  I figured there must have been a more
well-engineered way to do what I wanted to do....

 - Aaron

On Wed, Jul 4, 2012 at 1:56 PM, "Markus W. Weißmann"
<markus.weissmann@in.tum.de> wrote:
> rlwrap? [1] Its probably already available through your favorite package manger.
>
>
> -Markus
>
> [1] http://utopia.knoware.nl/~hlub/uck/rlwrap/
>
> On 4 Jul 2012, at 19:06, Aaron Bohannon wrote:
>
>> Hi,
>>
>> One thing that is missing from the OCaml top level loop is line
>> editing features.  I used to use a wrapper program called "ledit" that
>> added readline support, but I never seem to have it installed on my
>> machine when I need it, and moreover, I have started to get used to
>> the more flexible line editing of zsh.
>>
>> After some poking around, I found that it's fairly straightforward to
>> wrap any REPL with a zsh script and get zsh line editing capabilities,
>> which includes multi-line support (e.g., so you can fix a typo you
>> made three lines back).
>>
>> I'm attaching the script I hacked together, in case it is useful to
>> anyone else.  It works great for me, but it was not written to be
>> generic and robust, so YMMV.  You must use ctrl-D to exit cleanly -- I
>> didn't bother adding support for "#quit".  I'm sure someone could make
>> many improvements to it if they had more patience for zsh scripting
>> than I do.
>>
>> - Aaron
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa-roc.inria.fr/wws/info/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>
>> <zcaml>
>
> --
> Markus Weißmann, M.Sc.
> Technische Universität München
> Institut für Informatik
> Boltzmannstr. 3
> D-85748 Garching
> Germany
> http://wwwknoll.in.tum.de/
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] ocaml top-level line editing script
  2012-07-05  1:46   ` Aaron Bohannon
@ 2012-07-05  4:25     ` Aaron Bohannon
  0 siblings, 0 replies; 6+ messages in thread
From: Aaron Bohannon @ 2012-07-05  4:25 UTC (permalink / raw)
  To: Markus W. Weißmann; +Cc: OCaml List

Being able to edit the input in vim is really nice.  Unfortunately,
rlwrap seems to erase the line breaks when it records the input in
it's history (zsh remembers them).

 - Aaron
i
On Wed, Jul 4, 2012 at 9:46 PM, Aaron Bohannon <bohannon@seas.upenn.edu> wrote:
> Ah.  Thanks for the tip.  I figured there must have been a more
> well-engineered way to do what I wanted to do....
>
>  - Aaron
>
> On Wed, Jul 4, 2012 at 1:56 PM, "Markus W. Weißmann"
> <markus.weissmann@in.tum.de> wrote:
>> rlwrap? [1] Its probably already available through your favorite package manger.
>>
>>
>> -Markus
>>
>> [1] http://utopia.knoware.nl/~hlub/uck/rlwrap/
>>
>> On 4 Jul 2012, at 19:06, Aaron Bohannon wrote:
>>
>>> Hi,
>>>
>>> One thing that is missing from the OCaml top level loop is line
>>> editing features.  I used to use a wrapper program called "ledit" that
>>> added readline support, but I never seem to have it installed on my
>>> machine when I need it, and moreover, I have started to get used to
>>> the more flexible line editing of zsh.
>>>
>>> After some poking around, I found that it's fairly straightforward to
>>> wrap any REPL with a zsh script and get zsh line editing capabilities,
>>> which includes multi-line support (e.g., so you can fix a typo you
>>> made three lines back).
>>>
>>> I'm attaching the script I hacked together, in case it is useful to
>>> anyone else.  It works great for me, but it was not written to be
>>> generic and robust, so YMMV.  You must use ctrl-D to exit cleanly -- I
>>> didn't bother adding support for "#quit".  I'm sure someone could make
>>> many improvements to it if they had more patience for zsh scripting
>>> than I do.
>>>
>>> - Aaron
>>>
>>> --
>>> Caml-list mailing list.  Subscription management and archives:
>>> https://sympa-roc.inria.fr/wws/info/caml-list
>>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>>
>>> <zcaml>
>>
>> --
>> Markus Weißmann, M.Sc.
>> Technische Universität München
>> Institut für Informatik
>> Boltzmannstr. 3
>> D-85748 Garching
>> Germany
>> http://wwwknoll.in.tum.de/
>>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-07-05  4:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-04 17:06 [Caml-list] ocaml top-level line editing script Aaron Bohannon
2012-07-04 17:56 ` "Markus W. Weißmann"
2012-07-05  1:46   ` Aaron Bohannon
2012-07-05  4:25     ` Aaron Bohannon
2012-07-04 18:24 ` rixed
2012-07-04 21:19   ` Anthony Tavener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).