utop icon indicating copy to clipboard operation
utop copied to clipboard

provide an action to accept current input

Open bobzhang opened this issue 12 years ago • 7 comments

Is it possible to provide an action to send the current buffer into the server regardless of whether it is syntax correct or not?

For example, currently in the utop 3;; Enter the phrase will be sent to the toplevel, 3 Enter will result in a new line Is it possible to send a phrase only "3" ?

I tried the [accept] action, but it seems not to work, thanks!

bobzhang avatar Mar 01 '13 05:03 bobzhang

Yes, that's possible. But I need to change a little bit the way actions are handled.

ghost avatar Mar 01 '13 16:03 ghost

This makes sense to me, since I have changed the parser of toplevel, maybe other people benefit from this flexible design as well.

bobzhang avatar Mar 01 '13 19:03 bobzhang

I'll have a look

danmey avatar Apr 01 '13 14:04 danmey

Would mind explain a bit how utop works? Will it parse the string? I changed my parser to double semicolon, then utop accepts it happily, but it still gives me an paser error

let a = {:exp| 3 |};; Error: Syntax error

bobzhang avatar Apr 16 '13 21:04 bobzhang

utop does not use Toploop.parse_toplevel_phrase. It has its own UTop.parse_toplevel_phrase which works a bit differently. So if you want to use your own parser you need to use this one. The main difference is the way it reports errors, because utop wants to highlight the location itself.

For accepting current input the only problem is that it is currently not possible to define utop specific actions. I'd like to change how lambda-term handles editing actions so that it is possible to do so. But in the meantime a quick-workaround is to define a new action (for instance accept2 or terminate-and-accept ) in lambda-term and overload it to accepting the current input in utop.

I may have some time to have a look at it this week end but if you want to have a look, here is what need to be done:

  1. add a new constructor to LTerm_read_line.action
  2. update doc_of_action and the actions list
  3. make this new action behave the same way as Accept in LTerm_read_line, there are two places to update: engine#send_action and term#exec
  4. maybe provide a default binding for it, like Meta-Enter
  5. in UTop_main update read_phrase#exec to change the behavior of this new action: add the phrase as it to the history, then add UTop.phrase_terminator and parse it

ghost avatar Apr 17 '13 08:04 ghost

Thanks for your help! I got a roughly working version by mutating UTop.parse_toplevel_phrase. utop is an invaluable piece of software for ocaml programmers, it would be great that some internals are documented so that more people could contribute. Thanks again

bobzhang avatar Apr 18 '13 00:04 bobzhang

Yep, you are right. I intend to do so, I'll put some documentation on the wiki.

ghost avatar Apr 18 '13 08:04 ghost