.TH "Format" 3o source: 2019-01-25 OCamldoc "OCaml library"
.SH NAME
Format \- Pretty printing.
.SH Module
Module Format
.SH Documentation
.sp
Module
.BI "Format"
:
.B sig end
.sp
Pretty printing\&.
.sp
This module implements a pretty\-printing facility to format values
within \&'pretty\-printing boxes\&'\&. The pretty\-printer splits lines
at specified break hints, and indents lines according to the box
structure\&.
.sp
For a gentle introduction to the basics of pretty\-printing using
.B Format
, read
http://caml\&.inria\&.fr/resources/doc/guides/format\&.en\&.html\&.
.sp
You may consider this module as providing an extension to the
.B printf
facility to provide automatic line splitting\&. The addition of
pretty\-printing annotations to your regular
.B printf
formats gives you
fancy indentation and line breaks\&.
Pretty\-printing annotations are described below in the documentation of
the function
.B Format\&.fprintf
\&.
.sp
You may also use the explicit box management and printing functions
provided by this module\&. This style is more basic but more verbose
than the
.B fprintf
concise formats\&.
.sp
For instance, the sequence
.B open_box 0; print_string "x ="; print_space ();
.B print_int 1; close_box (); print_newline ()
that prints
.B x = 1
within a pretty\-printing box, can be
abbreviated as
.B printf "@[%s@ %i@]@\&." "x =" 1
, or even shorter
.B printf "@[x =@ %i@]@\&." 1
\&.
.sp
Rule of thumb for casual users of this library:
.sp
\-use simple boxes (as obtained by
.B open_box 0
);
.sp
\-use simple break hints (as obtained by
.B print_cut ()
that outputs a
simple break hint, or by
.B print_space ()
that outputs a space
indicating a break hint);
.sp
\-once a box is opened, display its material with basic printing
functions (e\&. g\&.
.B print_int
and
.B print_string
);
.sp
\-when the material for a box has been printed, call
.B close_box ()
to
close the box;
.sp
\-at the end of your routine, flush the pretty\-printer to display all the
remaining material, e\&.g\&. evaluate
.B print_newline ()
\&.
The behaviour of pretty\-printing commands is unspecified
if there is no opened pretty\-printing box\&. Each box opened via
one of the
.B open_
functions below must be closed using
.B close_box
for proper formatting\&. Otherwise, some of the material printed in the
boxes may not be output, or may be formatted incorrectly\&.
.sp
In case of interactive use, the system closes all opened boxes and
flushes all pending text (as with the
.B print_newline
function)
after each phrase\&. Each phrase is therefore executed in the initial
state of the pretty\-printer\&.
.sp
Warning: the material output by the following functions is delayed
in the pretty\-printer queue in order to compute the proper line
splitting\&. Hence, you should not mix calls to the printing functions
of the basic I/O system with calls to the functions of this module:
this could result in some strange output seemingly unrelated with
the evaluation order of printing commands\&.
.sp
.sp
.sp
.PP
.B ===
.B Boxes
.B ===
.PP
.I val open_box
:
.B int -> unit
.sp
.B open_box d
opens a new pretty\-printing box
with offset
.B d
\&.
.sp
This box prints material as much as possible on every line\&.
.sp
A break hint splits the line if there is no more room on the line to
print the remainder of the box\&.
A break hint also splits the line if the splitting ``moves to the left\&'\&'
(i\&.e\&. it gives an indentation smaller than the one of the current line)\&.
.sp
This box is the general purpose pretty\-printing box\&.
.sp
If the pretty\-printer splits the line in the box, offset
.B d
is added to
the current indentation\&.
.sp
.I val close_box
:
.B unit -> unit
.sp
Closes the most recently opened pretty\-printing box\&.
.sp
.PP
.B ===
.B Formatting functions
.B ===
.PP
.I val print_string
:
.B string -> unit
.sp
.B print_string str
prints
.B str
in the current box\&.
.sp
.I val print_as
:
.B int -> string -> unit
.sp
.B print_as len str
prints
.B str
in the
current box\&. The pretty\-printer formats
.B str
as if
it were of length
.B len
\&.
.sp
.I val print_int
:
.B int -> unit
.sp
Prints an integer in the current box\&.
.sp
.I val print_float
:
.B float -> unit
.sp
Prints a floating point number in the current box\&.
.sp
.I val print_char
:
.B char -> unit
.sp
Prints a character in the current box\&.
.sp
.I val print_bool
:
.B bool -> unit
.sp
Prints a boolean in the current box\&.
.sp
.PP
.B ===
.B Break hints
.B ===
.PP
.PP
.B === A \&'break hint\&' tells the pretty\-printer to output some space or split the
.B line whichever way is more appropriate to the current box splitting rules\&.
.B
.B Break hints are used to separate printing items and are mandatory to let
.B the pretty\-printer correctly split lines and indent items\&.
.B
.B Simple break hints are:
.B \- the \&'space\&': output a space or split the line if appropriate,
.B \- the \&'cut\&': split the line if appropriate\&.
.B
.B Note: the notions of space and line splitting are abstract for the
.B pretty\-printing engine, since those notions can be completely defined
.B by the programmer\&.
.B However, in the pretty\-printer default setting, ``output a space\&'\&' simply
.B means printing a space character (ASCII code 32) and ``split the line\&'\&'
.B is printing a newline character (ASCII code 10)\&. ===
.PP
.I val print_space
:
.B unit -> unit
.sp
.B print_space ()
the \&'space\&' break hint:
the pretty\-printer may split the line at this
point, otherwise it prints one space\&.
It is equivalent to
.B print_break 1 0
\&.
.sp
.I val print_cut
:
.B unit -> unit
.sp
.B print_cut ()
the \&'cut\&' break hint:
the pretty\-printer may split the line at this
point, otherwise it prints nothing\&.
It is equivalent to
.B print_break 0 0
\&.
.sp
.I val print_break
:
.B int -> int -> unit
.sp
.B print_break nspaces offset
the \&'full\&' break hint:
the pretty\-printer may split the line at this
point, otherwise it prints
.B nspaces
spaces\&.
.sp
If the pretty\-printer splits the line,
.B offset
is added to
the current indentation\&.
.sp
.I val print_flush
:
.B unit -> unit
.sp
Flushes the pretty printer: all opened boxes are closed,
and all pending text is displayed\&.
.sp
.I val print_newline
:
.B unit -> unit
.sp
Equivalent to
.B print_flush
followed by a new line\&.
.sp
.I val force_newline
:
.B unit -> unit
.sp
Forces a new line in the current box\&.
Not the normal way of pretty\-printing, since the new line does not reset
the current line count\&.
You should prefer using break hints within a vertcal box\&.
.sp
.I val print_if_newline
:
.B unit -> unit
.sp
Executes the next formatting command if the preceding line
has just been split\&. Otherwise, ignore the next formatting
command\&.
.sp
.PP
.B ===
.B Margin
.B ===
.PP
.I val set_margin
:
.B int -> unit
.sp
.B set_margin d
sets the right margin to
.B d
(in characters):
the pretty\-printer splits lines that overflow the right margin according to
the break hints given\&.
Nothing happens if
.B d
is smaller than 2\&.
If
.B d
is too large, the right margin is set to the maximum
admissible value (which is greater than
.B 10^9
)\&.
.sp
.I val get_margin
:
.B unit -> int
.sp
Returns the position of the right margin\&.
.sp
.PP
.B ===
.B Maximum indentation limit
.B ===
.PP
.I val set_max_indent
:
.B int -> unit
.sp
.B set_max_indent d
sets the maximum indentation limit of lines to
.B d
(in
characters):
once this limit is reached, new boxes are rejected to the left,
if they do not fit on the current line\&.
Nothing happens if
.B d
is smaller than 2\&.
If
.B d
is too large, the limit is set to the maximum
admissible value (which is greater than
.B 10 ^ 9
)\&.
.sp
.I val get_max_indent
:
.B unit -> int
.sp
Return the maximum indentation limit (in characters)\&.
.sp
.PP
.B ===
.B Formatting depth: maximum number of boxes allowed before ellipsis
.B ===
.PP
.I val set_max_boxes
:
.B int -> unit
.sp
.B set_max_boxes max
sets the maximum number of boxes simultaneously
opened\&.
Material inside boxes nested deeper is printed as an ellipsis (more
precisely as the text returned by
.B get_ellipsis_text ()
)\&.
Nothing happens if
.B max
is smaller than 2\&.
.sp
.I val get_max_boxes
:
.B unit -> int
.sp
Returns the maximum number of boxes allowed before ellipsis\&.
.sp
.I val over_max_boxes
:
.B unit -> bool
.sp
Tests if the maximum number of boxes allowed have already been opened\&.
.sp
.PP
.B ===
.B Advanced formatting
.B ===
.PP
.I val open_hbox
:
.B unit -> unit
.sp
.B open_hbox ()
opens a new \&'horizontal\&' pretty\-printing box\&.
.sp
This box prints material on a single line\&.
.sp
Break hints in a horizontal box never split the line\&.
(Line splitting may still occur inside boxes nested deeper)\&.
.sp
.I val open_vbox
:
.B int -> unit
.sp
.B open_vbox d
opens a new \&'vertical\&' pretty\-printing box
with offset
.B d
\&.
.sp
This box prints material on as many lines as break hints in the box\&.
.sp
Every break hint in a vertical box splits the line\&.
.sp
If the pretty\-printer splits the line in the box,
.B d
is added to the
current indentation\&.
.sp
.I val open_hvbox
:
.B int -> unit
.sp
.B open_hvbox d
opens a new \&'horizontal\-vertical\&' pretty\-printing box
with offset
.B d
\&.
.sp
This box behaves as an horizontal box if it fits on a single line,
otherwise it behaves as a vertical box\&.
.sp
If the pretty\-printer splits the line in the box,
.B d
is added to the
current indentation\&.
.sp
.I val open_hovbox
:
.B int -> unit
.sp
.B open_hovbox d
opens a new \&'horizontal\-or\-vertical\&' pretty\-printing box
with offset
.B d
\&.
.sp
This box prints material as much as possible on every line\&.
.sp
A break hint splits the line if there is no more room on the line to
print the remainder of the box\&.
.sp
If the pretty\-printer splits the line in the box,
.B d
is added to the
current indentation\&.
.sp
.PP
.B ===
.B Ellipsis
.B ===
.PP
.I val set_ellipsis_text
:
.B string -> unit
.sp
Set the text of the ellipsis printed when too many boxes
are opened (a single dot,
.B \&.
, by default)\&.
.sp
.I val get_ellipsis_text
:
.B unit -> string
.sp
Return the text of the ellipsis\&.
.sp
.PP
.B ===
.B Semantic Tags
.B ===
.PP
.I type tag
=
.B string
.sp
.sp
.PP
.B === Semantic tags (or simply tags) are used to decorate printed
.B entities for user\&'s defined purposes, e\&.g\&. setting font and giving size
.B indications for a display device, or marking delimitation of semantic
.B entities (e\&.g\&. HTML or TeX elements or terminal escape sequences)\&.
.B
.B By default, those tags do not influence line splitting calculation:
.B the tag \&'markers\&' are not considered as part of the printing
.B material that drives line splitting (in other words, the length of
.B those strings is considered as zero for line splitting)\&.
.B
.B Thus, tag handling is in some sense transparent to pretty\-printing
.B and does not interfere with usual indentation\&. Hence, a single
.B pretty printing routine can output both simple \&'verbatim\&'
.B material or richer decorated output depending on the treatment of
.B tags\&. By default, tags are not active, hence the output is not
.B decorated with tag information\&. Once set_tags is set to true,
.B the pretty printer engine honours tags and decorates the output
.B accordingly\&.
.B
.B When a tag has been opened (or closed), it is both and successively
.B \&'printed\&' and \&'marked\&'\&. Printing a tag means calling a
.B formatter specific function with the name of the tag as argument:
.B that \&'tag printing\&' function can then print any regular material
.B to the formatter (so that this material is enqueued as usual in the
.B formatter queue for further line splitting computation)\&. Marking a
.B tag means to output an arbitrary string (the \&'tag marker\&'),
.B directly into the output device of the formatter\&. Hence, the
.B formatter specific \&'tag marking\&' function must return the tag
.B marker string associated to its tag argument\&. Being flushed
.B directly into the output device of the formatter, tag marker
.B strings are not considered as part of the printing material that
.B drives line splitting (in other words, the length of the strings
.B corresponding to tag markers is considered as zero for line
.B splitting)\&. In addition, advanced users may take advantage of
.B the specificity of tag markers to be precisely output when the
.B pretty printer has already decided where to split the lines, and
.B precisely when the queue is flushed into the output device\&.
.B
.B In the spirit of HTML tags, the default tag marking functions
.B output tags enclosed in "<" and ">": hence, the opening marker of
.B tag t is "" and the closing marker ""\&.
.B
.B Default tag printing functions just do nothing\&.
.B
.B Tag marking and tag printing functions are user definable and can
.B be set by calling set_formatter_tag_functions\&. ===
.PP
.I val open_tag
:
.B tag -> unit
.sp
.B open_tag t
opens the tag named
.B t
; the
.B print_open_tag
function of the formatter is called with
.B t
as argument;
the tag marker
.B mark_open_tag t
will be flushed into the output
device of the formatter\&.
.sp
.I val close_tag
:
.B unit -> unit
.sp
.B close_tag ()
closes the most recently opened tag
.B t
\&.
In addition, the
.B print_close_tag
function of the formatter is called
with
.B t
as argument\&. The marker
.B mark_close_tag t
will be flushed
into the output device of the formatter\&.
.sp
.I val set_tags
:
.B bool -> unit
.sp
.B set_tags b
turns on or off the treatment of tags (default is off)\&.
.sp
.I val set_print_tags
:
.B bool -> unit
.sp
.B set_print_tags b
turns on or off the printing of tags\&.
.sp
.I val set_mark_tags
:
.B bool -> unit
.sp
.B set_mark_tags b
turns on or off the output of tag markers\&.
.sp
.I val get_print_tags
:
.B unit -> bool
.sp
Return the current status of tags printing\&.
.sp
.I val get_mark_tags
:
.B unit -> bool
.sp
Return the current status of tags marking\&.
.sp
.PP
.B ===
.B Redirecting the standard formatter output
.B ===
.PP
.I val set_formatter_out_channel
:
.B Pervasives.out_channel -> unit
.sp
Redirect the pretty\-printer output to the given channel\&.
(All the output functions of the standard formatter are set to the
default output functions printing to the given channel\&.)
.sp
.I val set_formatter_output_functions
:
.B (string -> int -> int -> unit) -> (unit -> unit) -> unit
.sp
.B set_formatter_output_functions out flush
redirects the
pretty\-printer output functions to the functions
.B out
and
.B flush
\&.
.sp
The
.B out
function performs all the pretty\-printer string output\&.
It is called with a string
.B s
, a start position
.B p
, and a number of
characters
.B n
; it is supposed to output characters
.B p
to
.B p + n \- 1
of
.B s
\&.
.sp
The
.B flush
function is called whenever the pretty\-printer is flushed
(via conversion
.B %!
, or pretty\-printing indications
.B @?
or
.B @\&.
, or
using low level functions
.B print_flush
or
.B print_newline
)\&.
.sp
.I val get_formatter_output_functions
:
.B unit -> (string -> int -> int -> unit) * (unit -> unit)
.sp
Return the current output functions of the pretty\-printer\&.
.sp
.PP
.B ===
.B Changing the meaning of standard formatter pretty printing
.B ===
.PP
.PP
.B === The Format module is versatile enough to let you completely redefine
.B the meaning of pretty printing: you may provide your own functions to define
.B how to handle indentation, line splitting, and even printing of all the
.B characters that have to be printed! ===
.PP
.I type formatter_out_functions
= {
out_string :
.B string -> int -> int -> unit
;
out_flush :
.B unit -> unit
;
out_newline :
.B unit -> unit
;
out_spaces :
.B int -> unit
;
}
.sp
.B "Since"
4.01.0
.sp
.I val set_formatter_out_functions
:
.B formatter_out_functions -> unit
.sp
.B set_formatter_out_functions f
Redirect the pretty\-printer output to the functions
.B f\&.out_string
and
.B f\&.out_flush
as described in
.B set_formatter_output_functions
\&. In addition, the pretty\-printer function
that outputs a newline is set to the function
.B f\&.out_newline
and
the function that outputs indentation spaces is set to the function
.B f\&.out_spaces
\&.
.sp
This way, you can change the meaning of indentation (which can be
something else than just printing space characters) and the meaning of new
lines opening (which can be connected to any other action needed by the
application at hand)\&. The two functions
.B f\&.out_spaces
and
.B f\&.out_newline
are normally connected to
.B f\&.out_string
and
.B f\&.out_flush
: respective
default values for
.B f\&.out_space
and
.B f\&.out_newline
are
.B f\&.out_string (String\&.make n \&' \&') 0 n
and
.B f\&.out_string "\(rsn" 0 1
\&.
.sp
.B "Since"
4.01.0
.sp
.I val get_formatter_out_functions
:
.B unit -> formatter_out_functions
.sp
Return the current output functions of the pretty\-printer,
including line splitting and indentation functions\&. Useful to record the
current setting and restore it afterwards\&.
.sp
.B "Since"
4.01.0
.sp
.PP
.B ===
.B Changing the meaning of printing semantic tags
.B ===
.PP
.I type formatter_tag_functions
= {
mark_open_tag :
.B tag -> string
;
mark_close_tag :
.B tag -> string
;
print_open_tag :
.B tag -> unit
;
print_close_tag :
.B tag -> unit
;
}
.sp
The tag handling functions specific to a formatter:
.B mark
versions are the \&'tag marking\&' functions that associate a string
marker to a tag in order for the pretty\-printing engine to flush
those markers as 0 length tokens in the output device of the formatter\&.
.B print
versions are the \&'tag printing\&' functions that can perform
regular printing when a tag is closed or opened\&.
.sp
.I val set_formatter_tag_functions
:
.B formatter_tag_functions -> unit
.sp
.B set_formatter_tag_functions tag_funs
changes the meaning of
opening and closing tags to use the functions in
.B tag_funs
\&.
.sp
When opening a tag name
.B t
, the string
.B t
is passed to the
opening tag marking function (the
.B mark_open_tag
field of the
record
.B tag_funs
), that must return the opening tag marker for
that name\&. When the next call to
.B close_tag ()
happens, the tag
name
.B t
is sent back to the closing tag marking function (the
.B mark_close_tag
field of record
.B tag_funs
), that must return a
closing tag marker for that name\&.
.sp
The
.B print_
field of the record contains the functions that are
called at tag opening and tag closing time, to output regular
material in the pretty\-printer queue\&.
.sp
.I val get_formatter_tag_functions
:
.B unit -> formatter_tag_functions
.sp
Return the current tag functions of the pretty\-printer\&.
.sp
.PP
.B ===
.B Multiple formatted output
.B ===
.PP
.I type formatter
.sp
Abstract data corresponding to a pretty\-printer (also called a
formatter) and all its machinery\&.
.sp
Defining new pretty\-printers permits unrelated output of material in
parallel on several output channels\&.
All the parameters of a pretty\-printer are local to a formatter:
margin, maximum indentation limit, maximum number of boxes
simultaneously opened, ellipsis, and so on, are specific to
each pretty\-printer and may be fixed independently\&.
Given a
.B Pervasives\&.out_channel
output channel
.B oc
, a new formatter
writing to that channel is simply obtained by calling
.B formatter_of_out_channel oc
\&.
Alternatively, the
.B make_formatter
function allocates a new
formatter with explicit output and flushing functions
(convenient to output material to strings for instance)\&.
.sp
.I val formatter_of_out_channel
:
.B Pervasives.out_channel -> formatter
.sp
.B formatter_of_out_channel oc
returns a new formatter that
writes to the corresponding channel
.B oc
\&.
.sp
.I val std_formatter
:
.B formatter
.sp
The standard formatter used by the formatting functions
above\&. It is defined as
.B formatter_of_out_channel stdout
\&.
.sp
.I val err_formatter
:
.B formatter
.sp
A formatter to use with formatting functions below for
output to standard error\&. It is defined as
.B formatter_of_out_channel stderr
\&.
.sp
.I val formatter_of_buffer
:
.B Buffer.t -> formatter
.sp
.B formatter_of_buffer b
returns a new formatter writing to
buffer
.B b
\&. As usual, the formatter has to be flushed at
the end of pretty printing, using
.B pp_print_flush
or
.B pp_print_newline
, to display all the pending material\&.
.sp
.I val stdbuf
:
.B Buffer.t
.sp
The string buffer in which
.B str_formatter
writes\&.
.sp
.I val str_formatter
:
.B formatter
.sp
A formatter to use with formatting functions below for
output to the
.B stdbuf
string buffer\&.
.B str_formatter
is defined as
.B formatter_of_buffer stdbuf
\&.
.sp
.I val flush_str_formatter
:
.B unit -> string
.sp
Returns the material printed with
.B str_formatter
, flushes
the formatter and resets the corresponding buffer\&.
.sp
.I val make_formatter
:
.B (string -> int -> int -> unit) -> (unit -> unit) -> formatter
.sp
.B make_formatter out flush
returns a new formatter that writes according
to the output function
.B out
, and the flushing function
.B flush
\&. For
instance, a formatter to the
.B Pervasives\&.out_channel
.B oc
is returned by
.B make_formatter (Pervasives\&.output oc) (fun () \-> Pervasives\&.flush oc)
\&.
.sp
.PP
.B ===
.B Basic functions to use with formatters
.B ===
.PP
.I val pp_open_hbox
:
.B formatter -> unit -> unit
.sp
.sp
.I val pp_open_vbox
:
.B formatter -> int -> unit
.sp
.sp
.I val pp_open_hvbox
:
.B formatter -> int -> unit
.sp
.sp
.I val pp_open_hovbox
:
.B formatter -> int -> unit
.sp
.sp
.I val pp_open_box
:
.B formatter -> int -> unit
.sp
.sp
.I val pp_close_box
:
.B formatter -> unit -> unit
.sp
.sp
.I val pp_open_tag
:
.B formatter -> string -> unit
.sp
.sp
.I val pp_close_tag
:
.B formatter -> unit -> unit
.sp
.sp
.I val pp_print_string
:
.B formatter -> string -> unit
.sp
.sp
.I val pp_print_as
:
.B formatter -> int -> string -> unit
.sp
.sp
.I val pp_print_int
:
.B formatter -> int -> unit
.sp
.sp
.I val pp_print_float
:
.B formatter -> float -> unit
.sp
.sp
.I val pp_print_char
:
.B formatter -> char -> unit
.sp
.sp
.I val pp_print_bool
:
.B formatter -> bool -> unit
.sp
.sp
.I val pp_print_break
:
.B formatter -> int -> int -> unit
.sp
.sp
.I val pp_print_cut
:
.B formatter -> unit -> unit
.sp
.sp
.I val pp_print_space
:
.B formatter -> unit -> unit
.sp
.sp
.I val pp_force_newline
:
.B formatter -> unit -> unit
.sp
.sp
.I val pp_print_flush
:
.B formatter -> unit -> unit
.sp
.sp
.I val pp_print_newline
:
.B formatter -> unit -> unit
.sp
.sp
.I val pp_print_if_newline
:
.B formatter -> unit -> unit
.sp
.sp
.I val pp_set_tags
:
.B formatter -> bool -> unit
.sp
.sp
.I val pp_set_print_tags
:
.B formatter -> bool -> unit
.sp
.sp
.I val pp_set_mark_tags
:
.B formatter -> bool -> unit
.sp
.sp
.I val pp_get_print_tags
:
.B formatter -> unit -> bool
.sp
.sp
.I val pp_get_mark_tags
:
.B formatter -> unit -> bool
.sp
.sp
.I val pp_set_margin
:
.B formatter -> int -> unit
.sp
.sp
.I val pp_get_margin
:
.B formatter -> unit -> int
.sp
.sp
.I val pp_set_max_indent
:
.B formatter -> int -> unit
.sp
.sp
.I val pp_get_max_indent
:
.B formatter -> unit -> int
.sp
.sp
.I val pp_set_max_boxes
:
.B formatter -> int -> unit
.sp
.sp
.I val pp_get_max_boxes
:
.B formatter -> unit -> int
.sp
.sp
.I val pp_over_max_boxes
:
.B formatter -> unit -> bool
.sp
.sp
.I val pp_set_ellipsis_text
:
.B formatter -> string -> unit
.sp
.sp
.I val pp_get_ellipsis_text
:
.B formatter -> unit -> string
.sp
.sp
.I val pp_set_formatter_out_channel
:
.B formatter -> Pervasives.out_channel -> unit
.sp
.sp
.I val pp_set_formatter_output_functions
:
.B formatter -> (string -> int -> int -> unit) -> (unit -> unit) -> unit
.sp
.sp
.I val pp_get_formatter_output_functions
:
.B formatter -> unit -> (string -> int -> int -> unit) * (unit -> unit)
.sp
.sp
.I val pp_set_formatter_tag_functions
:
.B formatter -> formatter_tag_functions -> unit
.sp
.sp
.I val pp_get_formatter_tag_functions
:
.B formatter -> unit -> formatter_tag_functions
.sp
.sp
.I val pp_set_formatter_out_functions
:
.B formatter -> formatter_out_functions -> unit
.sp
.B "Since"
4.01.0
.sp
.I val pp_get_formatter_out_functions
:
.B formatter -> unit -> formatter_out_functions
.sp
These functions are the basic ones: usual functions
operating on the standard formatter are defined via partial
evaluation of these primitives\&. For instance,
.B print_string
is equal to
.B pp_print_string std_formatter
\&.
.sp
.B "Since"
4.01.0
.sp
.I val pp_flush_formatter
:
.B formatter -> unit
.sp
.B pp_flush_formatter fmt
flushes
.B fmt
\&'s internal queue, ensuring that all
the printing and flushing actions have been performed\&. In addition, this
operation will close all boxes and reset the state of the formatter\&.
.sp
This will not flush
.B fmt
\&'s output\&. In most cases, the user may want to use
.B Format\&.pp_print_flush
instead\&.
.sp
.B "Since"
4.04.0
.sp
.PP
.B ===
.B Convenience formatting functions\&.
.B ===
.PP
.I val pp_print_list
:
.B ?pp_sep:(formatter -> unit -> unit) ->
.B (formatter -> 'a -> unit) -> formatter -> 'a list -> unit
.sp
.B pp_print_list ?pp_sep pp_v ppf l
prints items of list
.B l
,
using
.B pp_v
to print each item, and calling
.B pp_sep
between items (
.B pp_sep
defaults to
.B Format\&.pp_print_cut
)\&.
Does nothing on empty lists\&.
.sp
.B "Since"
4.02.0
.sp
.I val pp_print_text
:
.B formatter -> string -> unit
.sp
.B pp_print_text ppf s
prints
.B s
with spaces and newlines
respectively printed with
.B Format\&.pp_print_space
and
.B Format\&.pp_force_newline
\&.
.sp
.B "Since"
4.02.0
.sp
.PP
.B ===
.B printf like functions for pretty\-printing\&.
.B ===
.PP
.I val fprintf
:
.B formatter -> ('a, formatter, unit) Pervasives.format -> 'a
.sp
.sp
.PP
.B === fprintf ff fmt arg1 \&.\&.\&. argN formats the arguments arg1 to argN
.B according to the format string fmt, and outputs the resulting string on
.B the formatter ff\&.
.B
.B The format fmt is a character string which contains three types of
.B objects: plain characters and conversion specifications as specified in
.B the Printf module, and pretty\-printing indications specific to the
.B Format module\&.
.B
.B The pretty\-printing indication characters are introduced by
.B a @ character, and their meanings are:
.B \- @[: open a pretty\-printing box\&. The type and offset of the
.B box may be optionally specified with the following syntax:
.B the < character, followed by an optional box type indication,
.B then an optional integer offset, and the closing > character\&.
.B Box type is one of h, v, hv, b, or hov\&.
.B \&'h\&' stands for an \&'horizontal\&' box,
.B \&'v\&' stands for a \&'vertical\&' box,
.B \&'hv\&' stands for an \&'horizontal\-vertical\&' box,
.B \&'b\&' stands for an \&'horizontal\-or\-vertical\&' box demonstrating indentation,
.B \&'hov\&' stands a simple \&'horizontal\-or\-vertical\&' box\&.
.B For instance, @[ opens an \&'horizontal\-or\-vertical\&'
.B box with indentation 2 as obtained with open_hovbox 2\&.
.B For more details about boxes, see the various box opening
.B functions open_*box\&.
.B \- @]: close the most recently opened pretty\-printing box\&.
.B \- @,: output a \&'cut\&' break hint, as with print_cut ()\&.
.B \- @ : output a \&'space\&' break hint, as with print_space ()\&.
.B \- @;: output a \&'full\&' break hint as with print_break\&. The
.B nspaces and offset parameters of the break hint may be
.B optionally specified with the following syntax:
.B the < character, followed by an integer nspaces value,
.B then an integer offset, and a closing > character\&.
.B If no parameters are provided, the good break defaults to a
.B \&'space\&' break hint\&.
.B \- @\&.: flush the pretty printer and split the line, as with
.B print_newline ()\&.
.B \- @: print the following item as if it were of length n\&.
.B Hence, printf "@<0>%s" arg prints arg as a zero length string\&.
.B If @ is not followed by a conversion specification,
.B then the following character of the format is printed as if
.B it were of length n\&.
.B \- @{: open a tag\&. The name of the tag may be optionally
.B specified with the following syntax:
.B the < character, followed by an optional string
.B specification, and the closing > character\&. The string
.B specification is any character string that does not contain the
.B closing character \&'>\&'\&. If omitted, the tag name defaults to the
.B empty string\&.
.B For more details about tags, see the functions open_tag and
.B close_tag\&.
.B \- @}: close the most recently opened tag\&.
.B \- @?: flush the pretty printer as with print_flush ()\&.
.B This is equivalent to the conversion %!\&.
.B \- @\(rsn: force a newline, as with force_newline (), not the normal way
.B of pretty\-printing, you should prefer using break hints inside a vertical
.B box\&.
.B
.B Note: If you need to prevent the interpretation of a @ character as a
.B pretty\-printing indication, you must escape it with a % character\&.
.B Old quotation mode @@ is deprecated since it is not compatible with
.B formatted input interpretation of character \&'@\&'\&.
.B
.B Example: printf "@[%s@ %d@]@\&." "x =" 1 is equivalent to
.B open_box (); print_string "x ="; print_space ();
.B print_int 1; close_box (); print_newline ()\&.
.B It prints x = 1 within a pretty\-printing \&'horizontal\-or\-vertical\&' box\&. ===
.PP
.I val printf
:
.B ('a, formatter, unit) Pervasives.format -> 'a
.sp
Same as
.B fprintf
above, but output on
.B std_formatter
\&.
.sp
.I val eprintf
:
.B ('a, formatter, unit) Pervasives.format -> 'a
.sp
Same as
.B fprintf
above, but output on
.B err_formatter
\&.
.sp
.I val sprintf
:
.B ('a, unit, string) Pervasives.format -> 'a
.sp
Same as
.B printf
above, but instead of printing on a formatter,
returns a string containing the result of formatting the arguments\&.
Note that the pretty\-printer queue is flushed at the end of each
call to
.B sprintf
\&.
.sp
In case of multiple and related calls to
.B sprintf
to output
material on a single string, you should consider using
.B fprintf
with the predefined formatter
.B str_formatter
and call
.B flush_str_formatter ()
to get the final result\&.
.sp
Alternatively, you can use
.B Format\&.fprintf
with a formatter writing to a
buffer of your own: flushing the formatter and the buffer at the end of
pretty\-printing returns the desired string\&.
.sp
.I val asprintf
:
.B ('a, formatter, unit, string) Pervasives.format4 -> 'a
.sp
Same as
.B printf
above, but instead of printing on a formatter,
returns a string containing the result of formatting the arguments\&.
The type of
.B asprintf
is general enough to interact nicely with
.B %a
conversions\&.
.sp
.B "Since"
4.01.0
.sp
.I val ifprintf
:
.B formatter -> ('a, formatter, unit) Pervasives.format -> 'a
.sp
Same as
.B fprintf
above, but does not print anything\&.
Useful to ignore some material when conditionally printing\&.
.sp
.B "Since"
3.10.0
.sp
.PP
.B === Formatted output functions with continuations\&. ===
.PP
.I val kfprintf
:
.B (formatter -> 'a) ->
.B formatter -> ('b, formatter, unit, 'a) Pervasives.format4 -> 'b
.sp
Same as
.B fprintf
above, but instead of returning immediately,
passes the formatter to its first argument at the end of printing\&.
.sp
.I val ikfprintf
:
.B (formatter -> 'a) ->
.B formatter -> ('b, formatter, unit, 'a) Pervasives.format4 -> 'b
.sp
Same as
.B kfprintf
above, but does not print anything\&.
Useful to ignore some material when conditionally printing\&.
.sp
.B "Since"
3.12.0
.sp
.I val ksprintf
:
.B (string -> 'a) -> ('b, unit, string, 'a) Pervasives.format4 -> 'b
.sp
Same as
.B sprintf
above, but instead of returning the string,
passes it to the first argument\&.
.sp
.I val kasprintf
:
.B (string -> 'a) -> ('b, formatter, unit, 'a) Pervasives.format4 -> 'b
.sp
Same as
.B asprintf
above, but instead of returning the string,
passes it to the first argument\&.
.sp
.B "Since"
4.03
.sp
.PP
.B ===
.B Deprecated
.B ===
.PP
.I val bprintf
:
.B Buffer.t -> ('a, formatter, unit) Pervasives.format -> 'a
.sp
.B "Deprecated."
This function is error prone\&. Do not use it\&.
.sp
If you need to print to some buffer
.B b
, you must first define a
formatter writing to
.B b
, using
.B let to_b = formatter_of_buffer b
; then
use regular calls to
.B Format\&.fprintf
on formatter
.B to_b
\&.
.sp
.I val kprintf
:
.B (string -> 'a) -> ('b, unit, string, 'a) Pervasives.format4 -> 'b
.sp
.B "Deprecated."
An alias for
.B ksprintf
\&.
.sp
.I val set_all_formatter_output_functions
:
.B out:(string -> int -> int -> unit) ->
.B flush:(unit -> unit) ->
.B newline:(unit -> unit) -> spaces:(int -> unit) -> unit
.sp
.B "Deprecated."
Subsumed by
.B set_formatter_out_functions
\&.
.sp
.I val get_all_formatter_output_functions
:
.B unit ->
.B (string -> int -> int -> unit) * (unit -> unit) * (unit -> unit) *
.B (int -> unit)
.sp
.B "Deprecated."
Subsumed by
.B get_formatter_out_functions
\&.
.sp
.I val pp_set_all_formatter_output_functions
:
.B formatter ->
.B out:(string -> int -> int -> unit) ->
.B flush:(unit -> unit) ->
.B newline:(unit -> unit) -> spaces:(int -> unit) -> unit
.sp
.B "Deprecated."
Subsumed by
.B pp_set_formatter_out_functions
\&.
.sp
.I val pp_get_all_formatter_output_functions
:
.B formatter ->
.B unit ->
.B (string -> int -> int -> unit) * (unit -> unit) * (unit -> unit) *
.B (int -> unit)
.sp
.B "Deprecated."
Subsumed by
.B pp_get_formatter_out_functions
\&.
.sp
.PP
.B === Tabulation boxes are deprecated\&. ===
.PP
.I val pp_open_tbox
:
.B formatter -> unit -> unit
.sp
.B "Deprecated."
since 4\&.03\&.0
.sp
.I val pp_close_tbox
:
.B formatter -> unit -> unit
.sp
.B "Deprecated."
since 4\&.03\&.0
.sp
.I val pp_print_tbreak
:
.B formatter -> int -> int -> unit
.sp
.B "Deprecated."
since 4\&.03\&.0
.sp
.I val pp_set_tab
:
.B formatter -> unit -> unit
.sp
.B "Deprecated."
since 4\&.03\&.0
.sp
.I val pp_print_tab
:
.B formatter -> unit -> unit
.sp
.B "Deprecated."
since 4\&.03\&.0
.sp
.I val open_tbox
:
.B unit -> unit
.sp
.B "Deprecated."
since 4\&.03\&.0
.sp
.I val close_tbox
:
.B unit -> unit
.sp
.B "Deprecated."
since 4\&.03\&.0
.sp
.I val print_tbreak
:
.B int -> int -> unit
.sp
.B "Deprecated."
since 4\&.03\&.0
.sp
.I val set_tab
:
.B unit -> unit
.sp
.B "Deprecated."
since 4\&.03\&.0
.sp
.I val print_tab
:
.B unit -> unit
.sp
.B "Deprecated."
since 4\&.03\&.0
.sp