caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] How can I change the margin size in the toplevel?
@ 2015-09-21 14:20 Alan Schmitt
  2015-09-22 17:14 ` Emilio Jesús Gallego Arias
  0 siblings, 1 reply; 10+ messages in thread
From: Alan Schmitt @ 2015-09-21 14:20 UTC (permalink / raw)
  To: OCaml Mailing List

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

Hello,

I’m writing some slides for an ocaml course that contain the output sent
by the toplevel, such as the following:

# let x = 1 + "toto";;
Error: This expression has type string but an expression was expected of type
         int

As the messages are too wide to fit on the width of the slide, they are
put in an environment that wraps them. The results look like this:

Characters 12-18:
  let x = 1 + "toto";;
              ^^^^^^
Error: This expression has type string but an expression was expected of
      type
         int

I’m trying to get rid of the line break before “int”, and looking at the
code, I see that the toplevel uses Format with break hints for these
messages, so I should be able to do this by setting the margin to a big
number. Unfortunately I don’t seem to be able to change the margin of
the formatter used in the toplevel. I tried both:

Format.pp_set_margin Format.err_formatter 200

and

Format.pp_set_margin Format.std_formatter 200

inside the toplevel but it does not change the formatter for these
messages.

Is there a way to access the formatter used by the toplevel? And if not,
is it possible to change the column at which word wrap happens?

Thanks,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Last week athmospheric CO₂ average (Updated September 13, 2015, Mauna Loa Obs.):
397.77 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: [Caml-list] How can I change the margin size in the toplevel?
  2015-09-21 14:20 [Caml-list] How can I change the margin size in the toplevel? Alan Schmitt
@ 2015-09-22 17:14 ` Emilio Jesús Gallego Arias
  2015-09-23  6:48   ` Alan Schmitt
  0 siblings, 1 reply; 10+ messages in thread
From: Emilio Jesús Gallego Arias @ 2015-09-22 17:14 UTC (permalink / raw)
  To: caml-list

Hi Alan,

Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> Format.pp_set_margin Format.err_formatter 200
>
> and
>
> Format.pp_set_margin Format.std_formatter 200
>
> inside the toplevel but it does not change the formatter for these
> messages.

I recall having some trouble with Format margins too, this snippet
worked for me(TM) in a custom application:

  let set_pp fmt =
    Format.pp_set_ellipsis_text fmt "[...]";
    Format.pp_set_margin fmt (fmt_margin + 1); (* Don't ever ask *)
    Format.pp_set_max_indent fmt fmt_margin    in

  set_pp Format.std_formatter;
  set_pp Format.err_formatter;

> Is there a way to access the formatter used by the toplevel? And if not,
> is it possible to change the column at which word wrap happens?

Best regards,
Emilio


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

* Re: [Caml-list] How can I change the margin size in the toplevel?
  2015-09-22 17:14 ` Emilio Jesús Gallego Arias
@ 2015-09-23  6:48   ` Alan Schmitt
  2015-09-23  7:26     ` David MENTRE
  2015-09-23 10:25     ` Emilio Jesús Gallego Arias
  0 siblings, 2 replies; 10+ messages in thread
From: Alan Schmitt @ 2015-09-23  6:48 UTC (permalink / raw)
  To: Emilio Jesús Gallego Arias; +Cc: caml-list

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

Hi Emilio,

On 2015-09-22 19:14, gallego@cri.ensmp.fr (Emilio Jesús Gallego Arias) writes:

> Alan Schmitt <alan.schmitt@polytechnique.org> writes:
>
>> Format.pp_set_margin Format.err_formatter 200
>>
>> and
>>
>> Format.pp_set_margin Format.std_formatter 200
>>
>> inside the toplevel but it does not change the formatter for these
>> messages.
>
> I recall having some trouble with Format margins too, this snippet
> worked for me(TM) in a custom application:
>
>   let set_pp fmt =
>     Format.pp_set_ellipsis_text fmt "[...]";
>     Format.pp_set_margin fmt (fmt_margin + 1); (* Don't ever ask *)
>     Format.pp_set_max_indent fmt fmt_margin    in
>
>   set_pp Format.std_formatter;
>   set_pp Format.err_formatter;

Thanks a lot for this suggestion. Unfortunately it does not seem to work
in the toplevel: it’s as if the toplevel was not using
Format.std_formatter for its output. I don’t know how to get hold of the
formatter used in the toplevel to change its options…

Thanks again,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Athmospheric CO₂ (Updated September 13, 2015, Mauna Loa Obs.): 397.77 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: [Caml-list] How can I change the margin size in the toplevel?
  2015-09-23  6:48   ` Alan Schmitt
@ 2015-09-23  7:26     ` David MENTRE
  2015-09-23 12:23       ` Alan Schmitt
  2015-09-23 10:25     ` Emilio Jesús Gallego Arias
  1 sibling, 1 reply; 10+ messages in thread
From: David MENTRE @ 2015-09-23  7:26 UTC (permalink / raw)
  To: caml-list

Hello Alan,

Le 23/09/2015 08:48, Alan Schmitt a écrit :
> Unfortunately it does not seem to work
> in the toplevel: it’s as if the toplevel was not using
> Format.std_formatter for its output. I don’t know how to get hold of the
> formatter used in the toplevel to change its options…

Probably stupid and brute force suggestion: have you considered 
recompiling the toplevel, modifying the constant defining the line width?

Best regards,
david


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

* Re: [Caml-list] How can I change the margin size in the toplevel?
  2015-09-23  6:48   ` Alan Schmitt
  2015-09-23  7:26     ` David MENTRE
@ 2015-09-23 10:25     ` Emilio Jesús Gallego Arias
  2015-09-23 11:34       ` Gerd Stolpmann
  1 sibling, 1 reply; 10+ messages in thread
From: Emilio Jesús Gallego Arias @ 2015-09-23 10:25 UTC (permalink / raw)
  To: caml-list

Hi Alan,

Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> Thanks a lot for this suggestion. Unfortunately it does not seem to work
> in the toplevel: it’s as if the toplevel was not using
> Format.std_formatter for its output. I don’t know how to get hold of the
> formatter used in the toplevel to change its options…

Oh I see, indeed the particular problem here seems that typing errors
are first formatted to a fresh buffer formatter, which won't inherit
stderr's margin.

The code seems to have changed in the last months (likely due to color
support), but the corresponding code in trunk is at parsing/location.ml:335.

Indeed, this little hack works for me(TM):

8<--------------------------------------------------------------------8<
Index: parsing/location.ml
===================================================================
--- parsing/location.ml	(revision 16441)
+++ parsing/location.ml	(working copy)
@@ -333,6 +333,7 @@
 let pp_ksprintf ?before k fmt =
   let buf = Buffer.create 64 in
   let ppf = Format.formatter_of_buffer buf in
+  Format.pp_set_margin ppf (200 + 1);
   Misc.Color.set_color_tag_handling ppf;
   begin match before with
     | None -> ()
8<--------------------------------------------------------------------8<

I don't know what would be the proper way to integrate it upstream.

Best regards,
Emilio


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

* Re: [Caml-list] How can I change the margin size in the toplevel?
  2015-09-23 10:25     ` Emilio Jesús Gallego Arias
@ 2015-09-23 11:34       ` Gerd Stolpmann
  2015-09-23 12:16         ` Alan Schmitt
  0 siblings, 1 reply; 10+ messages in thread
From: Gerd Stolpmann @ 2015-09-23 11:34 UTC (permalink / raw)
  To: Emilio Jesús Gallego Arias; +Cc: caml-list

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

Am Mittwoch, den 23.09.2015, 12:25 +0200 schrieb Emilio Jesús Gallego
Arias:
> Hi Alan,
> 
> Alan Schmitt <alan.schmitt@polytechnique.org> writes:
> 
> > Thanks a lot for this suggestion. Unfortunately it does not seem to work
> > in the toplevel: it’s as if the toplevel was not using
> > Format.std_formatter for its output. I don’t know how to get hold of the
> > formatter used in the toplevel to change its options…
> 
> Oh I see, indeed the particular problem here seems that typing errors
> are first formatted to a fresh buffer formatter, which won't inherit
> stderr's margin.
> 
> The code seems to have changed in the last months (likely due to color
> support), but the corresponding code in trunk is at parsing/location.ml:335.
> 
> Indeed, this little hack works for me(TM):
> 
> 8<--------------------------------------------------------------------8<
> Index: parsing/location.ml
> ===================================================================
> --- parsing/location.ml	(revision 16441)
> +++ parsing/location.ml	(working copy)
> @@ -333,6 +333,7 @@
>  let pp_ksprintf ?before k fmt =
>    let buf = Buffer.create 64 in
>    let ppf = Format.formatter_of_buffer buf in
> +  Format.pp_set_margin ppf (200 + 1);
>    Misc.Color.set_color_tag_handling ppf;
>    begin match before with
>      | None -> ()
> 8<--------------------------------------------------------------------8<
> 
> I don't know what would be the proper way to integrate it upstream.

Maybe to propagate the margin?

Format.pp_set_margin ppf (Format.pp_get_margin Format.std_formatter())

Don't know whether std_formatter or err_formatter is the right one.

Gerd


> Best regards,
> Emilio
> 
> 

-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
My OCaml site:          http://www.camlcity.org
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

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

* Re: [Caml-list] How can I change the margin size in the toplevel?
  2015-09-23 11:34       ` Gerd Stolpmann
@ 2015-09-23 12:16         ` Alan Schmitt
  0 siblings, 0 replies; 10+ messages in thread
From: Alan Schmitt @ 2015-09-23 12:16 UTC (permalink / raw)
  To: Gerd Stolpmann; +Cc: Emilio Jesús Gallego Arias, caml-list

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

Hello,

On 2015-09-23 13:34, Gerd Stolpmann <info@gerd-stolpmann.de> writes:

> Am Mittwoch, den 23.09.2015, 12:25 +0200 schrieb Emilio Jesús Gallego
> Arias:
>> Hi Alan,
>> 
>> The code seems to have changed in the last months (likely due to color
>> support), but the corresponding code in trunk is at parsing/location.ml:335.
>> 
>> Indeed, this little hack works for me(TM):
>> 
>> 8<--------------------------------------------------------------------8<
>> Index: parsing/location.ml
>> ===================================================================
>> --- parsing/location.ml	(revision 16441)
>> +++ parsing/location.ml	(working copy)
>> @@ -333,6 +333,7 @@
>>  let pp_ksprintf ?before k fmt =
>>    let buf = Buffer.create 64 in
>>    let ppf = Format.formatter_of_buffer buf in
>> +  Format.pp_set_margin ppf (200 + 1);
>>    Misc.Color.set_color_tag_handling ppf;
>>    begin match before with
>>      | None -> ()
>> 8<--------------------------------------------------------------------8<
>> 
>> I don't know what would be the proper way to integrate it upstream.

Thank you for tracking down where the formatter is created. It makes
sense now why I was not able to change these margins.

> Maybe to propagate the margin?
>
> Format.pp_set_margin ppf (Format.pp_get_margin Format.std_formatter())
>
> Don't know whether std_formatter or err_formatter is the right one.

It’s not clear. The formatter for warnings is set by default to the
error formatter
(https://github.com/ocaml/ocaml/blob/b860d631458802c8685aa59c9d7b66a9225fcedb/parsing/location.ml#L309)
but the toplevel loop sets it to the std formatter (the loop is called
with std_formatter,
https://github.com/ocaml/ocaml/blob/b860d631458802c8685aa59c9d7b66a9225fcedb/parsing/location.ml#L309).
So a solution could be to reuse its value:

Format.pp_set_margin ppf (Format.pp_get_margin !formatter_for_warnings ())

Thanks again,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Athmospheric CO₂ (Updated September 22, 2015, Mauna Loa Obs.): 397.77 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: [Caml-list] How can I change the margin size in the toplevel?
  2015-09-23  7:26     ` David MENTRE
@ 2015-09-23 12:23       ` Alan Schmitt
  2015-09-23 16:05         ` Alan Schmitt
  0 siblings, 1 reply; 10+ messages in thread
From: Alan Schmitt @ 2015-09-23 12:23 UTC (permalink / raw)
  To: David MENTRE, Nicolas Ojeda Bar; +Cc: caml-list

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

Hi Nicolas and David,

> This is rather heavy-handed, but you could compile your own custom
> toplevel directly from source to do this.

> Probably stupid and brute force suggestion: have you considered recompiling
> the toplevel, modifying the constant defining the line width?

Thank you for the suggestion. I could do this as a last resort now that
I know where to change it, but I’d rather not.

Is there a guide somewhere that explains how to create a new opam switch
From local (modified) ocaml sources?

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Athmospheric CO₂ (Updated September 22, 2015, Mauna Loa Obs.): 397.77 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: [Caml-list] How can I change the margin size in the toplevel?
  2015-09-23 12:23       ` Alan Schmitt
@ 2015-09-23 16:05         ` Alan Schmitt
  2015-09-23 16:39           ` Gabriel Scherer
  0 siblings, 1 reply; 10+ messages in thread
From: Alan Schmitt @ 2015-09-23 16:05 UTC (permalink / raw)
  To: David MENTRE; +Cc: Nicolas Ojeda Bar, caml-list

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

On 2015-09-23 14:23, Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> Is there a guide somewhere that explains how to create a new opam switch
> From local (modified) ocaml sources?

A very helpful colleague gave me a crash course on nix and I was able to
compile a patched version (where I tweaked error_of_printer in
parsing/location.ml as follows):

#+begin_src ocaml
let error_of_printer loc print x =
  let buf = Buffer.create 64 in
  let ppf = Format.formatter_of_buffer buf in
  Format.pp_set_margin ppf (Format.pp_get_margin !formatter_for_warnings ());
  pp_print_string ppf "Error: ";
  print ppf x;
  pp_print_flush ppf ();
  let msg = Buffer.contents buf in
  errorf ~loc "%s" msg
#+end_src

This works great: I can now change the margin of the error messages
simply by doing a "Format.set_margin" in the toplevel.

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Athmospheric CO₂ (Updated September 22, 2015, Mauna Loa Obs.): 397.77 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: [Caml-list] How can I change the margin size in the toplevel?
  2015-09-23 16:05         ` Alan Schmitt
@ 2015-09-23 16:39           ` Gabriel Scherer
  0 siblings, 0 replies; 10+ messages in thread
From: Gabriel Scherer @ 2015-09-23 16:39 UTC (permalink / raw)
  To: Alan Schmitt; +Cc: David MENTRE, Nicolas Ojeda Bar, caml users

For the record, Simon Cruanes is trying to find a good fix for the
issue, with a preliminary patch proposal in
  https://github.com/ocaml/ocaml/pull/207
Maybe we could discuss the implementation there (or in a new Mantis
ticket) rather than on the mailing-list directly.

(There are wonderful people that prepare summaries of mailing-list
discussions, you know, they probably have to read all the messages,
let's not create too much work.)

On Wed, Sep 23, 2015 at 6:05 PM, Alan Schmitt
<alan.schmitt@polytechnique.org> wrote:
> On 2015-09-23 14:23, Alan Schmitt <alan.schmitt@polytechnique.org> writes:
>
>> Is there a guide somewhere that explains how to create a new opam switch
>> From local (modified) ocaml sources?
>
> A very helpful colleague gave me a crash course on nix and I was able to
> compile a patched version (where I tweaked error_of_printer in
> parsing/location.ml as follows):
>
> #+begin_src ocaml
> let error_of_printer loc print x =
>   let buf = Buffer.create 64 in
>   let ppf = Format.formatter_of_buffer buf in
>   Format.pp_set_margin ppf (Format.pp_get_margin !formatter_for_warnings ());
>   pp_print_string ppf "Error: ";
>   print ppf x;
>   pp_print_flush ppf ();
>   let msg = Buffer.contents buf in
>   errorf ~loc "%s" msg
> #+end_src
>
> This works great: I can now change the margin of the error messages
> simply by doing a "Format.set_margin" in the toplevel.
>
> Alan
>
> --
> OpenPGP Key ID : 040D0A3B4ED2E5C7
> Athmospheric CO₂ (Updated September 22, 2015, Mauna Loa Obs.): 397.77 ppm

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

end of thread, other threads:[~2015-09-23 16:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-21 14:20 [Caml-list] How can I change the margin size in the toplevel? Alan Schmitt
2015-09-22 17:14 ` Emilio Jesús Gallego Arias
2015-09-23  6:48   ` Alan Schmitt
2015-09-23  7:26     ` David MENTRE
2015-09-23 12:23       ` Alan Schmitt
2015-09-23 16:05         ` Alan Schmitt
2015-09-23 16:39           ` Gabriel Scherer
2015-09-23 10:25     ` Emilio Jesús Gallego Arias
2015-09-23 11:34       ` Gerd Stolpmann
2015-09-23 12:16         ` 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).