caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] ocaml toplevel: is it possible to separate what is sent on stdout from the answers from the toplevel?
@ 2014-03-24 13:17 Alan Schmitt
  2014-03-24 14:47 ` Jeremie Dimino
  0 siblings, 1 reply; 3+ messages in thread
From: Alan Schmitt @ 2014-03-24 13:17 UTC (permalink / raw)
  To: OCaml Mailing List

Hello,

I'm looking into extending the OCaml support in org-mode, and to do so
I would like to distinguish what is printed out as replies from the
toplevel from the data printed from the program.

To give a precise example, I would like to split "foo" from "val x : int
= 3" in the following interaction:

--8<---------------cut here---------------start------------->8---
# let x = print_endline "foo"; 3;;
foo
val x : int = 3
--8<---------------cut here---------------end--------------->8---

Is there a way to do it?

Thanks,

Alan

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

* Re: [Caml-list] ocaml toplevel: is it possible to separate what is sent on stdout from the answers from the toplevel?
  2014-03-24 13:17 [Caml-list] ocaml toplevel: is it possible to separate what is sent on stdout from the answers from the toplevel? Alan Schmitt
@ 2014-03-24 14:47 ` Jeremie Dimino
  2014-03-29 11:02   ` Alan Schmitt
  0 siblings, 1 reply; 3+ messages in thread
From: Jeremie Dimino @ 2014-03-24 14:47 UTC (permalink / raw)
  To: Alan Schmitt; +Cc: OCaml Mailing List

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

The best way is to create a custom toplevel and map the replies.

"val x : int = 3" is printed using [Toploop.print_out_phrase] which is
a reference, so you can replace by a function adding markers around
the text.

This won't catch error/warning messages. If you also want to
distinguish these, you can redirect the standard formatters. The
toplevel prints replies and error messages on the formatter passed to
[Toploop.loop], which is [Format.std_formatter] by default. So you can
either:
(1) override [Format.std_formatter] callbacks
(2) call [Toploop.loop] with a custom formatter

You can do (1) with [Format.pp_set_all_formatter_output_functions] and
(2) by setting the startup hook:

    Toploop.toplevel_startup_hook := (fun () -> Toploop.loop my_formatter;
exit 0)

Warnings are always printed on [Format.err_formatter] so you'll need
to override its callbacks.

If you can't create a custom toplevel, I guess you can also send the
code to the toplevel at the beginning of the session.



On Mon, Mar 24, 2014 at 1:17 PM, Alan Schmitt <
alan.schmitt@polytechnique.org> wrote:

> Hello,
>
> I'm looking into extending the OCaml support in org-mode, and to do so
> I would like to distinguish what is printed out as replies from the
> toplevel from the data printed from the program.
>
> To give a precise example, I would like to split "foo" from "val x : int
> = 3" in the following interaction:
>
> --8<---------------cut here---------------start------------->8---
> # let x = print_endline "foo"; 3;;
> foo
> val x : int = 3
> --8<---------------cut here---------------end--------------->8---
>
> Is there a way to do it?
>
> Thanks,
>
> Alan
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>



-- 
Jeremie

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

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

* Re: [Caml-list] ocaml toplevel: is it possible to separate what is sent on stdout from the answers from the toplevel?
  2014-03-24 14:47 ` Jeremie Dimino
@ 2014-03-29 11:02   ` Alan Schmitt
  0 siblings, 0 replies; 3+ messages in thread
From: Alan Schmitt @ 2014-03-29 11:02 UTC (permalink / raw)
  To: Jeremie Dimino; +Cc: OCaml Mailing List

Hello Jeremie,

Jeremie Dimino <jdimino@janestreet.com> writes:

> The best way is to create a custom toplevel and map the replies.
>
> "val x : int = 3" is printed using [Toploop.print_out_phrase] which is
> a reference, so you can replace by a function adding markers around
> the text.
>
> This won't catch error/warning messages. If you also want to
> distinguish these, you can redirect the standard formatters. The
> toplevel prints replies and error messages on the formatter passed to
> [Toploop.loop], which is [Format.std_formatter] by default. So you can
> either:
> (1) override [Format.std_formatter] callbacks
> (2) call [Toploop.loop] with a custom formatter
>
> You can do (1) with [Format.pp_set_all_formatter_output_functions] and
> (2) by setting the startup hook:
>
> Toploop.toplevel_startup_hook := (fun () -> Toploop.loop my_formatter;
> exit 0)
>
> Warnings are always printed on [Format.err_formatter] so you'll need
> to override its callbacks.
>
> If you can't create a custom toplevel, I guess you can also send the
> code to the toplevel at the beginning of the session.

Thanks a lot, this was most helpful. This is what I ended up doing. The
following, when evaluated, will send all the information from the
toplevel to some file, and only output in the toplevel things that are
explicitly printed.

#+begin_src ocaml
let _,_,newline,space = 
  Format.pp_get_all_formatter_output_functions Format.std_formatter ();;

let outfile = open_out "/Users/schmitta/tmp/testoutml.txt";;

let myout s p n = Printf.fprintf outfile "%s" (String.sub s p n);;

let myflush () = flush outfile;;

Format.pp_set_all_formatter_output_functions 
  Format.std_formatter 
  myout 
  myflush 
  newline 
  space;;
#+end_src

Alan


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

end of thread, other threads:[~2014-03-29 11:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-24 13:17 [Caml-list] ocaml toplevel: is it possible to separate what is sent on stdout from the answers from the toplevel? Alan Schmitt
2014-03-24 14:47 ` Jeremie Dimino
2014-03-29 11:02   ` Alan Schmitt

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