Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to use anything but double semicolons. #131

Closed
jordwalke opened this issue May 11, 2015 · 13 comments

Comments

Projects
None yet
4 participants
@jordwalke
Copy link

commented May 11, 2015

Is there a way to use anything other than double semicolons to tell the top level to try to parse a top level phrase? It would be great if you could configure <control+cr> or <shift+cr>.

@whitequark

This comment has been minimized.

Copy link
Collaborator

commented May 11, 2015

No. This is not really a part of utop but of OCaml's grammar. (You can also use ;; in source files.)

@jordwalke

This comment has been minimized.

Copy link
Author

commented May 11, 2015

@whitequark: I'm aware of toplevel_phrase. I also looked at how m17n customized utop with a custom parser/lexer. I know I can create a custom parser/lexer, but I'm wondering if there's any other hacks I can do in .lambdaterm.conf such as triggering the insertion of two semis (and a return). Otherwise, I suppose I'll just have to create a custom parser.

@whitequark

This comment has been minimized.

Copy link
Collaborator

commented May 11, 2015

Oh. The sequences in your bugreport did not initially render. (I have fixed them.)

@jordwalke

This comment has been minimized.

Copy link
Author

commented May 11, 2015

Okay. Then do you suppose it might be possible via configuring lambdaterm?

@whitequark

This comment has been minimized.

Copy link
Collaborator

commented May 11, 2015

@diml

This comment has been minimized.

Copy link
Collaborator

commented May 11, 2015

Yeah you could have this in your ~/.lambda-termrc:

[read-line]
C-enter: goto-eot, insert(;), insert(;), accept

However there seems to be a rendering bug in lambda-term.

Another solution, if you want it even when you just press enter is to add this to your ~/.ocamlinit:

let orig_parse = !UTop.parse_toplevel_phrase in
UTop.parse_toplevel_phrase := (fun str eos_is_error -> orig_parse (str ^ ";;") eos_is_error);;

This solution might be better as what will be added to the history is exactly what you typed. Note that if you are using camlp4 you need to run this after #camlp4o.

@hcarty

This comment has been minimized.

Copy link
Contributor

commented May 11, 2015

A quick and dirty function you could add to .ocamlinit to toggle between requiring semicolons (ex. for pasting a block of code) and not (for simpler interactive use):

let toggle_semi =
  let original = !UTop.parse_toplevel_phrase in
  let no_semi str eos_is_error = original (str ^ ";;") eos_is_error in
  let semi = ref true in
  fun () ->
    UTop.parse_toplevel_phrase := if !semi then no_semi else original;
    semi := not !semi

I put this in a gist for future reference.

@jordwalke

This comment has been minimized.

Copy link
Author

commented May 11, 2015

Cool, good suggestions.

@diml diml closed this in 92fafe1 May 12, 2015

@jordwalke

This comment has been minimized.

Copy link
Author

commented Aug 5, 2015

I think one issue that I see with some of these suggestions that automatically submit upon hitting <Enter>, is that there doesn't seem to be a way to not submit on <Shift+Enter>, which is how must REPLs work (enter submits, shift+enter does multiline input).

@diml

This comment has been minimized.

Copy link
Collaborator

commented Aug 6, 2015

@jordwalke, with 92fafe1 you can do both: you can bind enter to automatically add ;; and something else to always insert a newline. In fact <Meta+Enter> is already bound to insert a newline. It is not <Shift+Enter> as this combination is not available in the terminal (you can't distinguish it from just <Enter>).

@jordwalke

This comment has been minimized.

Copy link
Author

commented Aug 7, 2015

@diml Thanks for reminding me. With your gist on utop1.18:

#require "lambda-term";;
LTerm_read_line.bind
  [ { control = false; meta = false; shift = false; code = Enter } ]
  [ UTop.end_and_accept_current_phrase ]

...it seems that hitting enter during a <c-r> history search seems to fail with the error:

Fatal error: exception File "src/lib/uTop.cppo.ml", line 81, characters 26-32: Assertion failed

If I had to guess, I'd say the intended behavior is for end_and_accept_current_phrase to not complete the selected history, and instead append the ;; to whatever they have manually typed, but instead, it just fatals.

@jordwalke

This comment has been minimized.

Copy link
Author

commented Aug 7, 2015

I also notice that with your suggested keybinding above, the following fatals utop:

  let x = List.map (fun x -> x) [<Enter>

Instead of fataling utop, it should probably gracefully show a parsing error of some kind.

@diml

This comment has been minimized.

Copy link
Collaborator

commented Aug 7, 2015

@jordwalke I fixed the first failure but I can't reproduce the second one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.