mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
171 lines
No EOL
173 KiB
HTML
171 lines
No EOL
173 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>CCFormat (containers.CCFormat)</title><link rel="stylesheet" href="../../_odoc-theme/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.0"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> – <a href="../index.html">containers</a> » CCFormat</nav><header class="odoc-preamble"><h1>Module <code><span>CCFormat</span></code></h1><p>Helpers for Format</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.8</li></ul></header><nav class="odoc-toc"><ul><li><a href="#introduction">Introduction</a></li><li><a href="#formatters">Formatters</a></li><li><a href="#boxes">Pretty-printing boxes</a></li><li><a href="#formatting-functions">Formatting functions</a></li><li><a href="#breaks">Break hints</a></li><li><a href="#pretty-printing-termination">Pretty-printing termination</a></li><li><a href="#margin">Margin</a></li><li><a href="#maxindent">Maximum indentation limit</a></li><li><a href="#geometry">Geometry</a></li><li><a href="#maximum-formatting-depth">Maximum formatting depth</a></li><li><a href="#tabulation-boxes">Tabulation boxes</a></li><li><a href="#ellipsis">Ellipsis</a></li><li><a href="#tags">Semantic tags</a></li><li><a href="#meaning">Redefining formatter output</a><ul><li><a href="#redefining-output-functions">Redefining output functions</a></li></ul></li><li><a href="#tagsmeaning">Redefining semantic tag operations</a></li><li><a href="#formatter">Defining formatters</a><ul><li><a href="#symbolic">Symbolic pretty-printing</a></li></ul></li><li><a href="#convenience-formatting-functions.">Convenience formatting functions.</a></li><li><a href="#fpp">Formatted pretty-printing</a></li><li><a href="#deprecated">Deprecated</a><ul><li><a href="#string-tags">String tags</a></li><li><a href="#combinators">Combinators</a></li><li><a href="#ansi-codes">ANSI codes</a></li><li><a href="#io">IO</a></li><li><a href="#dump">Dump</a></li></ul></li></ul></nav><div class="odoc-content"><div class="odoc-spec"><div class="spec type anchored" id="type-iter"><a href="#type-iter" class="anchor"></a><code><span><span class="keyword">type</span> <span>'a iter</span></span><span> = <span><span>(<span><span class="type-var">'a</span> <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span> unit</span></code></div></div><h2 id="introduction"><a href="#introduction" class="anchor"></a>Introduction</h2><p>You may consider this module as providing an extension to the <code>printf</code> facility to provide automatic line splitting. The addition of pretty-printing annotations to your regular <code>printf</code> format strings gives you fancy indentation and line breaks. Pretty-printing annotations are described below in the documentation of the function <code>Format</code>.fprintf.</p><p>You may also use the explicit pretty-printing box management and printing functions provided by this module. This style is more basic but more verbose than the concise <code>fprintf</code> format strings.</p><p>For instance, the sequence <code>open_box 0; print_string "x ="; print_space ();
|
||
print_int 1; close_box (); print_newline ()</code> that prints <code>x = 1</code> within a pretty-printing box, can be abbreviated as <code>printf "@[%s@ %i@]@." "x =" 1</code>, or even shorter <code>printf "@[x =@ %i@]@." 1</code>.</p><p>Rule of thumb for casual users of this library:</p><ul><li>use simple pretty-printing boxes (as obtained by <code>open_box 0</code>);</li><li>use simple break hints as obtained by <code>print_cut ()</code> that outputs a simple break hint, or by <code>print_space ()</code> that outputs a space indicating a break hint;</li><li>once a pretty-printing box is open, display its material with basic printing functions (e. g. <code>print_int</code> and <code>print_string</code>);</li><li>when the material for a pretty-printing box has been printed, call <code>close_box ()</code> to close the box;</li><li>at the end of pretty-printing, flush the pretty-printer to display all the remaining material, e.g. evaluate <code>print_newline ()</code>.</li></ul><p>The behavior of pretty-printing commands is unspecified if there is no open pretty-printing box. Each box opened by one of the <code>open_</code> functions below must be closed using <code>close_box</code> for proper formatting. Otherwise, some of the material printed in the boxes may not be output, or may be formatted incorrectly.</p><p>In case of interactive use, each phrase is executed in the initial state of the standard pretty-printer: after each phrase execution, the interactive system closes all open pretty-printing boxes, flushes all pending text, and resets the standard pretty-printer.</p><p>Warning: mixing calls to pretty-printing functions of this module with calls to <a href="../../ocaml/Stdlib/index.html"><code>Stdlib</code></a> low level output functions is error prone.</p><p>The pretty-printing functions output material that is delayed in the pretty-printer queue and stacks in order to compute proper line splitting. In contrast, basic I/O output functions write directly in their output device. As a consequence, the output of a basic I/O function may appear before the output of a pretty-printing function that has been called before. For instance, <code>
|
||
Stdlib.print_string "<";
|
||
Format.print_string "PRETTY";
|
||
Stdlib.print_string ">";
|
||
Format.print_string "TEXT";
|
||
</code> leads to output <code><>PRETTYTEXT</code>.</p><h2 id="formatters"><a href="#formatters" class="anchor"></a>Formatters</h2><div class="odoc-spec"><div class="spec type anchored" id="type-formatter"><a href="#type-formatter" class="anchor"></a><code><span><span class="keyword">type</span> formatter</span><span> = <a href="../../ocaml/Stdlib/Format/index.html#type-formatter">Stdlib.Format.formatter</a></span></code></div><div class="spec-doc"><p>Abstract data corresponding to a pretty-printer (also called a formatter) and all its machinery. See also <a href="#formatter">Defining formatters</a>.</p></div></div><h2 id="boxes"><a href="#boxes" class="anchor"></a>Pretty-printing boxes</h2><p>The pretty-printing engine uses the concepts of pretty-printing box and break hint to drive indentation and line splitting behavior of the pretty-printer.</p><p>Each different pretty-printing box kind introduces a specific line splitting policy:</p><ul><li>within an <em>horizontal</em> box, break hints never split the line (but the line may be split in a box nested deeper),</li><li>within a <em>vertical</em> box, break hints always split the line,</li><li>within an <em>horizontal/vertical</em> box, if the box fits on the current line then break hints never split the line, otherwise break hint always split the line,</li><li>within a <em>compacting</em> box, a break hint never splits the line, unless there is no more room on the current line.</li></ul><p>Note that line splitting policy is box specific: the policy of a box does not rule the policy of inner boxes. For instance, if a vertical box is nested in an horizontal box, all break hints within the vertical box will split the line.</p><p>Moreover, opening a box after the <a href="#maxindent">maximum indentation limit</a> splits the line whether or not the box would end up fitting on the line.</p><div class="odoc-spec"><div class="spec value anchored" id="val-pp_open_box"><a href="#val-pp_open_box" class="anchor"></a><code><span><span class="keyword">val</span> pp_open_box : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-open_box"><a href="#val-open_box" class="anchor"></a><code><span><span class="keyword">val</span> open_box : <span>int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_open_box ppf d</code> opens a new compacting pretty-printing box with offset <code>d</code> in the formatter <code>ppf</code>.</p><p>Within this box, the pretty-printer prints as much as possible material on every line.</p><p>A break hint splits the line if there is no more room on the line to print the remainder of the box.</p><p>Within this box, the pretty-printer emphasizes the box structure: if a structural box does not fit fully on a simple line, a break hint also splits the line if the splitting ``moves to the left'' (i.e. the new line gets an indentation smaller than the one of the current line).</p><p>This box is the general purpose pretty-printing box.</p><p>If the pretty-printer splits the line in the box, offset <code>d</code> is added to the current indentation.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_close_box"><a href="#val-pp_close_box" class="anchor"></a><code><span><span class="keyword">val</span> pp_close_box : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-close_box"><a href="#val-close_box" class="anchor"></a><code><span><span class="keyword">val</span> close_box : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Closes the most recently open pretty-printing box.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_open_hbox"><a href="#val-pp_open_hbox" class="anchor"></a><code><span><span class="keyword">val</span> pp_open_hbox : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-open_hbox"><a href="#val-open_hbox" class="anchor"></a><code><span><span class="keyword">val</span> open_hbox : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_open_hbox ppf ()</code> opens a new 'horizontal' pretty-printing box.</p><p>This box prints material on a single line.</p><p>Break hints in a horizontal box never split the line. (Line splitting may still occur inside boxes nested deeper).</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_open_vbox"><a href="#val-pp_open_vbox" class="anchor"></a><code><span><span class="keyword">val</span> pp_open_vbox : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-open_vbox"><a href="#val-open_vbox" class="anchor"></a><code><span><span class="keyword">val</span> open_vbox : <span>int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_open_vbox ppf d</code> opens a new 'vertical' pretty-printing box with offset <code>d</code>.</p><p>This box prints material on as many lines as break hints in the box.</p><p>Every break hint in a vertical box splits the line.</p><p>If the pretty-printer splits the line in the box, <code>d</code> is added to the current indentation.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_open_hvbox"><a href="#val-pp_open_hvbox" class="anchor"></a><code><span><span class="keyword">val</span> pp_open_hvbox : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-open_hvbox"><a href="#val-open_hvbox" class="anchor"></a><code><span><span class="keyword">val</span> open_hvbox : <span>int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_open_hvbox ppf d</code> opens a new 'horizontal/vertical' pretty-printing box with offset <code>d</code>.</p><p>This box behaves as an horizontal box if it fits on a single line, otherwise it behaves as a vertical box.</p><p>If the pretty-printer splits the line in the box, <code>d</code> is added to the current indentation.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_open_hovbox"><a href="#val-pp_open_hovbox" class="anchor"></a><code><span><span class="keyword">val</span> pp_open_hovbox : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-open_hovbox"><a href="#val-open_hovbox" class="anchor"></a><code><span><span class="keyword">val</span> open_hovbox : <span>int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_open_hovbox ppf d</code> opens a new 'horizontal-or-vertical' pretty-printing box with offset <code>d</code>.</p><p>This box prints material as much as possible on every line.</p><p>A break hint splits the line if there is no more room on the line to print the remainder of the box.</p><p>If the pretty-printer splits the line in the box, <code>d</code> is added to the current indentation.</p></div></div><h2 id="formatting-functions"><a href="#formatting-functions" class="anchor"></a>Formatting functions</h2><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_string"><a href="#val-pp_print_string" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_string : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>string <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_string"><a href="#val-print_string" class="anchor"></a><code><span><span class="keyword">val</span> print_string : <span>string <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_print_string ppf s</code> prints <code>s</code> in the current pretty-printing box.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_bytes"><a href="#val-pp_print_bytes" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_bytes : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>bytes <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_bytes"><a href="#val-print_bytes" class="anchor"></a><code><span><span class="keyword">val</span> print_bytes : <span>bytes <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_print_bytes ppf b</code> prints <code>b</code> in the current pretty-printing box.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.13.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_as"><a href="#val-pp_print_as" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_as : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>string <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_as"><a href="#val-print_as" class="anchor"></a><code><span><span class="keyword">val</span> print_as : <span>int <span class="arrow">-></span></span> <span>string <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_print_as ppf len s</code> prints <code>s</code> in the current pretty-printing box. The pretty-printer formats <code>s</code> as if it were of length <code>len</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_int"><a href="#val-pp_print_int" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_int : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_int"><a href="#val-print_int" class="anchor"></a><code><span><span class="keyword">val</span> print_int : <span>int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Print an integer in the current pretty-printing box.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_float"><a href="#val-pp_print_float" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_float : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_float"><a href="#val-print_float" class="anchor"></a><code><span><span class="keyword">val</span> print_float : <span>float <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Print a floating point number in the current pretty-printing box.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_char"><a href="#val-pp_print_char" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_char : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>char <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_char"><a href="#val-print_char" class="anchor"></a><code><span><span class="keyword">val</span> print_char : <span>char <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Print a character in the current pretty-printing box.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_bool"><a href="#val-pp_print_bool" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_bool : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>bool <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_bool"><a href="#val-print_bool" class="anchor"></a><code><span><span class="keyword">val</span> print_bool : <span>bool <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Print a boolean in the current pretty-printing box.</p></div></div><h2 id="breaks"><a href="#breaks" class="anchor"></a>Break hints</h2><p>A 'break hint' tells the pretty-printer to output some space or split the line whichever way is more appropriate to the current pretty-printing box splitting rules.</p><p>Break hints are used to separate printing items and are mandatory to let the pretty-printer correctly split lines and indent items.</p><p>Simple break hints are:</p><ul><li>the 'space': output a space or split the line if appropriate,</li><li>the 'cut': split the line if appropriate.</li></ul><p>Note: the notions of space and line splitting are abstract for the pretty-printing engine, since those notions can be completely redefined by the programmer. However, in the pretty-printer default setting, ``output a space'' simply means printing a space character (ASCII code 32) and ``split the line'' means printing a newline character (ASCII code 10).</p><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_space"><a href="#val-pp_print_space" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_space : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_space"><a href="#val-print_space" class="anchor"></a><code><span><span class="keyword">val</span> print_space : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_print_space ppf ()</code> emits a 'space' break hint: the pretty-printer may split the line at this point, otherwise it prints one space.</p><p><code>pp_print_space ppf ()</code> is equivalent to <code>pp_print_break ppf 1 0</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_cut"><a href="#val-pp_print_cut" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_cut : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_cut"><a href="#val-print_cut" class="anchor"></a><code><span><span class="keyword">val</span> print_cut : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_print_cut ppf ()</code> emits a 'cut' break hint: the pretty-printer may split the line at this point, otherwise it prints nothing.</p><p><code>pp_print_cut ppf ()</code> is equivalent to <code>pp_print_break ppf 0 0</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_break"><a href="#val-pp_print_break" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_break : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_break"><a href="#val-print_break" class="anchor"></a><code><span><span class="keyword">val</span> print_break : <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_print_break ppf nspaces offset</code> emits a 'full' break hint: the pretty-printer may split the line at this point, otherwise it prints <code>nspaces</code> spaces.</p><p>If the pretty-printer splits the line, <code>offset</code> is added to the current indentation.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_custom_break"><a href="#val-pp_print_custom_break" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_custom_break :
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span>fits:<span>(string * int * string)</span> <span class="arrow">-></span></span>
|
||
<span>breaks:<span>(string * int * string)</span> <span class="arrow">-></span></span>
|
||
unit</span></code></div><div class="spec-doc"><p><code>pp_print_custom_break ppf ~fits:(s1, n, s2) ~breaks:(s3, m, s4)</code> emits a custom break hint: the pretty-printer may split the line at this point.</p><p>If it does not split the line, then the <code>s1</code> is emitted, then <code>n</code> spaces, then <code>s2</code>.</p><p>If it splits the line, then it emits the <code>s3</code> string, then an indent (according to the box rules), then an offset of <code>m</code> spaces, then the <code>s4</code> string.</p><p>While <code>n</code> and <code>m</code> are handled by <code>formatter_out_functions.out_indent</code>, the strings will be handled by <code>formatter_out_functions.out_string</code>. This allows for a custom formatter that handles indentation distinctly, for example, outputs <code><br/></code> tags or <code>&nbsp;</code> entities.</p><p>The custom break is useful if you want to change which visible (non-whitespace) characters are printed in case of break or no break. For example, when printing a list <code> [a; b; c] </code>, you might want to add a trailing semicolon when it is printed vertically:</p><pre class="language-ocaml"><code>[
|
||
a;
|
||
b;
|
||
c;
|
||
]</code></pre><p>You can do this as follows:</p><pre class="language-ocaml"><code>printf "@[<v 0>[@;<0 2>@[<v 0>a;@,b;@,c@]%t]@]@\n"
|
||
(pp_print_custom_break ~fits:("", 0, "") ~breaks:(";", 0, ""))</code></pre><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_force_newline"><a href="#val-pp_force_newline" class="anchor"></a><code><span><span class="keyword">val</span> pp_force_newline : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-force_newline"><a href="#val-force_newline" class="anchor"></a><code><span><span class="keyword">val</span> force_newline : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Force a new line in the current pretty-printing box.</p><p>The pretty-printer must split the line at this point,</p><p>Not the normal way of pretty-printing, since imperative line splitting may interfere with current line counters and box size calculation. Using break hints within an enclosing vertical box is a better alternative.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_if_newline"><a href="#val-pp_print_if_newline" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_if_newline : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_if_newline"><a href="#val-print_if_newline" class="anchor"></a><code><span><span class="keyword">val</span> print_if_newline : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Execute the next formatting command if the preceding line has just been split. Otherwise, ignore the next formatting command.</p></div></div><h2 id="pretty-printing-termination"><a href="#pretty-printing-termination" class="anchor"></a>Pretty-printing termination</h2><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_flush"><a href="#val-pp_print_flush" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_flush : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_flush"><a href="#val-print_flush" class="anchor"></a><code><span><span class="keyword">val</span> print_flush : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>End of pretty-printing: resets the pretty-printer to initial state.</p><p>All open pretty-printing boxes are closed, all pending text is printed. In addition, the pretty-printer low level output device is flushed to ensure that all pending text is really displayed.</p><p>Note: never use <code>print_flush</code> in the normal course of a pretty-printing routine, since the pretty-printer uses a complex buffering machinery to properly indent the output; manually flushing those buffers at random would conflict with the pretty-printer strategy and result to poor rendering.</p><p>Only consider using <code>print_flush</code> when displaying all pending material is mandatory (for instance in case of interactive use when you want the user to read some text) and when resetting the pretty-printer state will not disturb further pretty-printing.</p><p>Warning: If the output device of the pretty-printer is an output channel, repeated calls to <code>print_flush</code> means repeated calls to <a href="../../ocaml/Stdlib/index.html#val-flush"><code>Stdlib.flush</code></a> to flush the out channel; these explicit flush calls could foil the buffering strategy of output channels and could dramatically impact efficiency.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_newline"><a href="#val-pp_print_newline" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_newline : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_newline"><a href="#val-print_newline" class="anchor"></a><code><span><span class="keyword">val</span> print_newline : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>End of pretty-printing: resets the pretty-printer to initial state.</p><p>All open pretty-printing boxes are closed, all pending text is printed.</p><p>Equivalent to <a href="#val-print_flush"><code>print_flush</code></a> followed by a new line. See corresponding words of caution for <a href="#val-print_flush"><code>print_flush</code></a>.</p><p>Note: this is not the normal way to output a new line; the preferred method is using break hints within a vertical pretty-printing box.</p></div></div><h2 id="margin"><a href="#margin" class="anchor"></a>Margin</h2><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_margin"><a href="#val-pp_set_margin" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_margin : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_margin"><a href="#val-set_margin" class="anchor"></a><code><span><span class="keyword">val</span> set_margin : <span>int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_set_margin ppf d</code> sets the right margin to <code>d</code> (in characters): the pretty-printer splits lines that overflow the right margin according to the break hints given. Setting the margin to <code>d</code> means that the formatting engine aims at printing at most <code>d-1</code> characters per line. Nothing happens if <code>d</code> is smaller than 2. If <code>d</code> is too large, the right margin is set to the maximum admissible value (which is greater than <code>10 ^ 9</code>). If <code>d</code> is less than the current maximum indentation limit, the maximum indentation limit is decreased while trying to preserve a minimal ratio <code>max_indent/margin>=50%</code> and if possible the current difference <code>margin - max_indent</code>.</p><p>See also <a href="#val-pp_set_geometry"><code>pp_set_geometry</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_margin"><a href="#val-pp_get_margin" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_margin : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> int</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_margin"><a href="#val-get_margin" class="anchor"></a><code><span><span class="keyword">val</span> get_margin : <span>unit <span class="arrow">-></span></span> int</span></code></div><div class="spec-doc"><p>Returns the position of the right margin.</p></div></div><h2 id="maxindent"><a href="#maxindent" class="anchor"></a>Maximum indentation limit</h2><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_max_indent"><a href="#val-pp_set_max_indent" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_max_indent : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_max_indent"><a href="#val-set_max_indent" class="anchor"></a><code><span><span class="keyword">val</span> set_max_indent : <span>int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_set_max_indent ppf d</code> sets the maximum indentation limit of lines to <code>d</code> (in characters): once this limit is reached, new pretty-printing boxes are rejected to the left, unless the enclosing box fully fits on the current line. As an illustration,</p><pre class="language-ocaml"><code>set_margin 10; set_max_indent 5; printf "@[123456@[7@]89A@]@." </code></pre><p>yields</p><pre class="language-ocaml"><code>123456
|
||
789A</code></pre><p>because the nested box <code>"@[7@]"</code> is opened after the maximum indentation limit (<code>7>5</code>) and its parent box does not fit on the current line. Either decreasing the length of the parent box to make it fit on a line:</p><pre class="language-ocaml"><code>printf "@[123456@[7@]89@]@." </code></pre><p>or opening an intermediary box before the maximum indentation limit which fits on the current line</p><pre class="language-ocaml"><code>printf "@[123@[456@[7@]89@]A@]@." </code></pre><p>avoids the rejection to the left of the inner boxes and print respectively <code>"123456789"</code> and <code>"123456789A"</code> . Note also that vertical boxes never fit on a line whereas horizontal boxes always fully fit on the current line. Opening a box may split a line whereas the contents may have fit. If this behavior is problematic, it can be curtailed by setting the maximum indentation limit to <code>margin - 1</code>. Note that setting the maximum indentation limit to <code>margin</code> is invalid.</p><p>Nothing happens if <code>d</code> is smaller than 2.</p><p>If <code>d</code> is too large, the limit is set to the maximum admissible value (which is greater than <code>10 ^ 9</code>).</p><p>If <code>d</code> is greater or equal than the current margin, it is ignored, and the current maximum indentation limit is kept.</p><p>See also <a href="#val-pp_set_geometry"><code>pp_set_geometry</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_max_indent"><a href="#val-pp_get_max_indent" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_max_indent : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> int</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_max_indent"><a href="#val-get_max_indent" class="anchor"></a><code><span><span class="keyword">val</span> get_max_indent : <span>unit <span class="arrow">-></span></span> int</span></code></div><div class="spec-doc"><p>Return the maximum indentation limit (in characters).</p></div></div><h2 id="geometry"><a href="#geometry" class="anchor"></a>Geometry</h2><p>Geometric functions can be used to manipulate simultaneously the coupled variables, margin and maxixum indentation limit.</p><div class="odoc-spec"><div class="spec type anchored" id="type-geometry"><a href="#type-geometry" class="anchor"></a><code><span><span class="keyword">type</span> geometry</span><span> = <a href="../../ocaml/Stdlib/Format/index.html#type-geometry">Stdlib.Format.geometry</a></span><span> = </span><span>{</span></code><ol><li id="type-geometry.max_indent" class="def record field anchored"><a href="#type-geometry.max_indent" class="anchor"></a><code><span>max_indent : int;</span></code></li><li id="type-geometry.margin" class="def record field anchored"><a href="#type-geometry.margin" class="anchor"></a><code><span>margin : int;</span></code></li></ol><code><span>}</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-check_geometry"><a href="#val-check_geometry" class="anchor"></a><code><span><span class="keyword">val</span> check_geometry : <span><a href="#type-geometry">geometry</a> <span class="arrow">-></span></span> bool</span></code></div><div class="spec-doc"><p>Check if the formatter geometry is valid: <code>1 < max_indent < margin</code></p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_geometry"><a href="#val-pp_set_geometry" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_geometry : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>max_indent:int <span class="arrow">-></span></span> <span>margin:int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_geometry"><a href="#val-set_geometry" class="anchor"></a><code><span><span class="keyword">val</span> set_geometry : <span>max_indent:int <span class="arrow">-></span></span> <span>margin:int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_safe_set_geometry"><a href="#val-pp_safe_set_geometry" class="anchor"></a><code><span><span class="keyword">val</span> pp_safe_set_geometry : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>max_indent:int <span class="arrow">-></span></span> <span>margin:int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-safe_set_geometry"><a href="#val-safe_set_geometry" class="anchor"></a><code><span><span class="keyword">val</span> safe_set_geometry : <span>max_indent:int <span class="arrow">-></span></span> <span>margin:int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_set_geometry ppf ~max_indent ~margin</code> sets both the margin and maximum indentation limit for <code>ppf</code>.</p><p>When <code>1 < max_indent < margin</code>, <code>pp_set_geometry ppf ~max_indent ~margin</code> is equivalent to <code>pp_set_margin ppf margin; pp_set_max_indent ppf max_indent</code>; and avoids the subtly incorrect <code>pp_set_max_indent ppf max_indent; pp_set_margin ppf margin</code>;</p><p>Outside of this domain, <code>pp_set_geometry</code> raises an invalid argument exception whereas <code>pp_safe_set_geometry</code> does nothing.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_update_geometry"><a href="#val-pp_update_geometry" class="anchor"></a><code><span><span class="keyword">val</span> pp_update_geometry : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><span>(<span><a href="#type-geometry">geometry</a> <span class="arrow">-></span></span> <a href="#type-geometry">geometry</a>)</span> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_update_geometry ppf (fun geo -> { geo with ... })</code> lets you update a formatter's geometry in a way that is robust to extension of the <code>geometry</code> record with new fields.</p><p>Raises an invalid argument exception if the returned geometry does not satisfy <a href="#val-check_geometry"><code>check_geometry</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.11.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-update_geometry"><a href="#val-update_geometry" class="anchor"></a><code><span><span class="keyword">val</span> update_geometry : <span><span>(<span><a href="#type-geometry">geometry</a> <span class="arrow">-></span></span> <a href="#type-geometry">geometry</a>)</span> <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_geometry"><a href="#val-pp_get_geometry" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_geometry : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> <a href="#type-geometry">geometry</a></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_geometry"><a href="#val-get_geometry" class="anchor"></a><code><span><span class="keyword">val</span> get_geometry : <span>unit <span class="arrow">-></span></span> <a href="#type-geometry">geometry</a></span></code></div><div class="spec-doc"><p>Return the current geometry of the formatter</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><h2 id="maximum-formatting-depth"><a href="#maximum-formatting-depth" class="anchor"></a>Maximum formatting depth</h2><p>The maximum formatting depth is the maximum number of pretty-printing boxes simultaneously open.</p><p>Material inside boxes nested deeper is printed as an ellipsis (more precisely as the text returned by <a href="#val-get_ellipsis_text"><code>get_ellipsis_text</code></a> <code>()</code>).</p><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_max_boxes"><a href="#val-pp_set_max_boxes" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_max_boxes : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_max_boxes"><a href="#val-set_max_boxes" class="anchor"></a><code><span><span class="keyword">val</span> set_max_boxes : <span>int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_set_max_boxes ppf max</code> sets the maximum number of pretty-printing boxes simultaneously open.</p><p>Material inside boxes nested deeper is printed as an ellipsis (more precisely as the text returned by <a href="#val-get_ellipsis_text"><code>get_ellipsis_text</code></a> <code>()</code>).</p><p>Nothing happens if <code>max</code> is smaller than 2.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_max_boxes"><a href="#val-pp_get_max_boxes" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_max_boxes : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> int</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_max_boxes"><a href="#val-get_max_boxes" class="anchor"></a><code><span><span class="keyword">val</span> get_max_boxes : <span>unit <span class="arrow">-></span></span> int</span></code></div><div class="spec-doc"><p>Returns the maximum number of pretty-printing boxes allowed before ellipsis.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_over_max_boxes"><a href="#val-pp_over_max_boxes" class="anchor"></a><code><span><span class="keyword">val</span> pp_over_max_boxes : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> bool</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-over_max_boxes"><a href="#val-over_max_boxes" class="anchor"></a><code><span><span class="keyword">val</span> over_max_boxes : <span>unit <span class="arrow">-></span></span> bool</span></code></div><div class="spec-doc"><p>Tests if the maximum number of pretty-printing boxes allowed have already been opened.</p></div></div><h2 id="tabulation-boxes"><a href="#tabulation-boxes" class="anchor"></a>Tabulation boxes</h2><p>A <em>tabulation box</em> prints material on lines divided into cells of fixed length. A tabulation box provides a simple way to display vertical columns of left adjusted text.</p><p>This box features command <code>set_tab</code> to define cell boundaries, and command <code>print_tab</code> to move from cell to cell and split the line when there is no more cells to print on the line.</p><p>Note: printing within tabulation box is line directed, so arbitrary line splitting inside a tabulation box leads to poor rendering. Yet, controlled use of tabulation boxes allows simple printing of columns within module <code>Format</code>.</p><div class="odoc-spec"><div class="spec value anchored" id="val-pp_open_tbox"><a href="#val-pp_open_tbox" class="anchor"></a><code><span><span class="keyword">val</span> pp_open_tbox : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-open_tbox"><a href="#val-open_tbox" class="anchor"></a><code><span><span class="keyword">val</span> open_tbox : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>open_tbox ()</code> opens a new tabulation box.</p><p>This box prints lines separated into cells of fixed width.</p><p>Inside a tabulation box, special <em>tabulation markers</em> defines points of interest on the line (for instance to delimit cell boundaries). Function <code>Format</code>.set_tab sets a tabulation marker at insertion point.</p><p>A tabulation box features specific <em>tabulation breaks</em> to move to next tabulation marker or split the line. Function <code>Format</code>.print_tbreak prints a tabulation break.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_close_tbox"><a href="#val-pp_close_tbox" class="anchor"></a><code><span><span class="keyword">val</span> pp_close_tbox : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-close_tbox"><a href="#val-close_tbox" class="anchor"></a><code><span><span class="keyword">val</span> close_tbox : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Closes the most recently opened tabulation box.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_tab"><a href="#val-pp_set_tab" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_tab : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_tab"><a href="#val-set_tab" class="anchor"></a><code><span><span class="keyword">val</span> set_tab : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Sets a tabulation marker at current insertion point.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_tab"><a href="#val-pp_print_tab" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_tab : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_tab"><a href="#val-print_tab" class="anchor"></a><code><span><span class="keyword">val</span> print_tab : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>print_tab ()</code> emits a 'next' tabulation break hint: if not already set on a tabulation marker, the insertion point moves to the first tabulation marker on the right, or the pretty-printer splits the line and insertion point moves to the leftmost tabulation marker.</p><p>It is equivalent to <code>print_tbreak 0 0</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_tbreak"><a href="#val-pp_print_tbreak" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_tbreak : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-print_tbreak"><a href="#val-print_tbreak" class="anchor"></a><code><span><span class="keyword">val</span> print_tbreak : <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>print_tbreak nspaces offset</code> emits a 'full' tabulation break hint.</p><p>If not already set on a tabulation marker, the insertion point moves to the first tabulation marker on the right and the pretty-printer prints <code>nspaces</code> spaces.</p><p>If there is no next tabulation marker on the right, the pretty-printer splits the line at this point, then insertion point moves to the leftmost tabulation marker of the box.</p><p>If the pretty-printer splits the line, <code>offset</code> is added to the current indentation.</p></div></div><h2 id="ellipsis"><a href="#ellipsis" class="anchor"></a>Ellipsis</h2><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_ellipsis_text"><a href="#val-pp_set_ellipsis_text" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_ellipsis_text : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>string <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_ellipsis_text"><a href="#val-set_ellipsis_text" class="anchor"></a><code><span><span class="keyword">val</span> set_ellipsis_text : <span>string <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Set the text of the ellipsis printed when too many pretty-printing boxes are open (a single dot, <code>.</code>, by default).</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_ellipsis_text"><a href="#val-pp_get_ellipsis_text" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_ellipsis_text : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> string</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_ellipsis_text"><a href="#val-get_ellipsis_text" class="anchor"></a><code><span><span class="keyword">val</span> get_ellipsis_text : <span>unit <span class="arrow">-></span></span> string</span></code></div><div class="spec-doc"><p>Return the text of the ellipsis.</p></div></div><h2 id="tags"><a href="#tags" class="anchor"></a>Semantic tags</h2><div class="odoc-spec"><div class="spec type anchored" id="type-stag"><a href="#type-stag" class="anchor"></a><code><span><span class="keyword">type</span> stag</span><span> = <a href="../../ocaml/Stdlib/Format/index.html#type-stag">Stdlib.Format.stag</a></span><span> = </span><span>..</span></code></div><div class="spec-doc"><p><i>Semantic tags</i> (or simply <em>tags</em>) are user's defined annotations to associate user's specific operations to printed entities.</p><p>Common usage of semantic tags is text decoration to get specific font or text size rendering for a display device, or marking delimitation of entities (e.g. HTML or TeX elements or terminal escape sequences). More sophisticated usage of semantic tags could handle dynamic modification of the pretty-printer behavior to properly print the material within some specific tags. For instance, we can define an RGB tag like so:</p><pre class="language-ocaml"><code>type stag += RGB of {r:int;g:int;b:int}</code></pre><p>In order to properly delimit printed entities, a semantic tag must be opened before and closed after the entity. Semantic tags must be properly nested like parentheses using <a href="#val-pp_open_stag"><code>pp_open_stag</code></a> and <a href="#val-pp_close_stag"><code>pp_close_stag</code></a>.</p><p>Tag specific operations occur any time a tag is opened or closed, At each occurrence, two kinds of operations are performed <em>tag-marking</em> and <em>tag-printing</em>:</p><ul><li>The tag-marking operation is the simpler tag specific operation: it simply writes a tag specific string into the output device of the formatter. Tag-marking does not interfere with line-splitting computation.</li><li>The tag-printing operation is the more involved tag specific operation: it can print arbitrary material to the formatter. Tag-printing is tightly linked to the current pretty-printer operations.</li></ul><p>Roughly speaking, tag-marking is commonly used to get a better rendering of texts in the rendering device, while tag-printing allows fine tuning of printing routines to print the same entity differently according to the semantic tags (i.e. print additional material or even omit parts of the output).</p><p>More precisely: when a semantic tag is opened or closed then both and successive 'tag-printing' and 'tag-marking' operations occur:</p><ul><li>Tag-printing a semantic tag means calling the formatter specific function <code>print_open_stag</code> (resp. <code>print_close_stag</code>) with the name of the tag as argument: that tag-printing function can then print any regular material to the formatter (so that this material is enqueued as usual in the formatter queue for further line splitting computation).</li><li>Tag-marking a semantic tag means calling the formatter specific function <code>mark_open_stag</code> (resp. <code>mark_close_stag</code>) with the name of the tag as argument: that tag-marking function can then return the 'tag-opening marker' (resp. `tag-closing marker') for direct output into the output device of the formatter.</li></ul><p>Being written directly into the output device of the formatter, semantic tag marker strings are not considered as part of the printing material that drives line splitting (in other words, the length of the strings corresponding to tag markers is considered as zero for line splitting).</p><p>Thus, semantic tag handling is in some sense transparent to pretty-printing and does not interfere with usual indentation. Hence, a single pretty-printing routine can output both simple 'verbatim' material or richer decorated output depending on the treatment of tags. By default, tags are not active, hence the output is not decorated with tag information. Once <code>set_tags</code> is set to <code>true</code>, the pretty-printer engine honors tags and decorates the output accordingly.</p><p>Default tag-marking functions behave the HTML way: <a href="#type-tag">string tags</a> are enclosed in "<" and ">" while other tags are ignored; hence, opening marker for tag string <code>"t"</code> is <code>"<t>"</code> and closing marker is <code>"</t>"</code>.</p><p>Default tag-printing functions just do nothing.</p><p>Tag-marking and tag-printing functions are user definable and can be set by calling <a href="#val-set_formatter_stag_functions"><code>set_formatter_stag_functions</code></a>.</p><p>Semantic tag operations may be set on or off with <a href="#val-set_tags"><code>set_tags</code></a>. Tag-marking operations may be set on or off with <a href="#val-set_mark_tags"><code>set_mark_tags</code></a>. Tag-printing operations may be set on or off with <a href="#val-set_print_tags"><code>set_print_tags</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-tag"><a href="#type-tag" class="anchor"></a><code><span><span class="keyword">type</span> tag</span><span> = string</span></code></div></div><div class="odoc-spec"><div class="spec type extension anchored" id="extension-decl-String_tag"><a href="#extension-decl-String_tag" class="anchor"></a><code><span><span class="keyword">type</span> <a href="#type-stag">stag</a> += </span></code><ol><li id="extension-String_tag" class="def extension anchored"><a href="#extension-String_tag" class="anchor"></a><code><span>| </span><span><span class="extension">String_tag</span> <span class="keyword">of</span> <a href="#type-tag">tag</a></span></code><div class="def-doc"><span class="comment-delim">(*</span><p><code>String_tag s</code> is a string tag <code>s</code>. String tags can be inserted either by explicitly using the constructor <code>String_tag</code> or by using the dedicated format syntax <code>"@{<s> ... @}"</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul><span class="comment-delim">*)</span></div></li></ol></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_open_stag"><a href="#val-pp_open_stag" class="anchor"></a><code><span><span class="keyword">val</span> pp_open_stag : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><a href="#type-stag">stag</a> <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-open_stag"><a href="#val-open_stag" class="anchor"></a><code><span><span class="keyword">val</span> open_stag : <span><a href="#type-stag">stag</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_open_stag ppf t</code> opens the semantic tag named <code>t</code>.</p><p>The <code>print_open_stag</code> tag-printing function of the formatter is called with <code>t</code> as argument; then the opening tag marker for <code>t</code>, as given by <code>mark_open_stag t</code>, is written into the output device of the formatter.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_close_stag"><a href="#val-pp_close_stag" class="anchor"></a><code><span><span class="keyword">val</span> pp_close_stag : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-close_stag"><a href="#val-close_stag" class="anchor"></a><code><span><span class="keyword">val</span> close_stag : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_close_stag ppf ()</code> closes the most recently opened semantic tag <code>t</code>.</p><p>The closing tag marker, as given by <code>mark_close_stag t</code>, is written into the output device of the formatter; then the <code>print_close_stag</code> tag-printing function of the formatter is called with <code>t</code> as argument.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_tags"><a href="#val-pp_set_tags" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_tags : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>bool <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_tags"><a href="#val-set_tags" class="anchor"></a><code><span><span class="keyword">val</span> set_tags : <span>bool <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_set_tags ppf b</code> turns on or off the treatment of semantic tags (default is off).</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_print_tags"><a href="#val-pp_set_print_tags" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_print_tags : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>bool <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_print_tags"><a href="#val-set_print_tags" class="anchor"></a><code><span><span class="keyword">val</span> set_print_tags : <span>bool <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_set_print_tags ppf b</code> turns on or off the tag-printing operations.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_mark_tags"><a href="#val-pp_set_mark_tags" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_mark_tags : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>bool <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_mark_tags"><a href="#val-set_mark_tags" class="anchor"></a><code><span><span class="keyword">val</span> set_mark_tags : <span>bool <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_set_mark_tags ppf b</code> turns on or off the tag-marking operations.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_print_tags"><a href="#val-pp_get_print_tags" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_print_tags : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> bool</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_print_tags"><a href="#val-get_print_tags" class="anchor"></a><code><span><span class="keyword">val</span> get_print_tags : <span>unit <span class="arrow">-></span></span> bool</span></code></div><div class="spec-doc"><p>Return the current status of tag-printing operations.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_mark_tags"><a href="#val-pp_get_mark_tags" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_mark_tags : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> bool</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_mark_tags"><a href="#val-get_mark_tags" class="anchor"></a><code><span><span class="keyword">val</span> get_mark_tags : <span>unit <span class="arrow">-></span></span> bool</span></code></div><div class="spec-doc"><p>Return the current status of tag-marking operations.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_formatter_out_channel"><a href="#val-pp_set_formatter_out_channel" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_formatter_out_channel : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><a href="../../ocaml/Stdlib/index.html#type-out_channel">out_channel</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Redirecting the standard formatter output</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_formatter_out_channel"><a href="#val-set_formatter_out_channel" class="anchor"></a><code><span><span class="keyword">val</span> set_formatter_out_channel : <span><a href="../../ocaml/Stdlib/index.html#type-out_channel">out_channel</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Redirect the standard 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.)</p><p><code>set_formatter_out_channel</code> is equivalent to <a href="#val-pp_set_formatter_out_channel"><code>pp_set_formatter_out_channel</code></a> <code>std_formatter</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_formatter_output_functions"><a href="#val-pp_set_formatter_output_functions" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_formatter_output_functions :
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span><span>(<span>string <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><span>(<span>unit <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_formatter_output_functions"><a href="#val-set_formatter_output_functions" class="anchor"></a><code><span><span class="keyword">val</span> set_formatter_output_functions :
|
||
<span><span>(<span>string <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><span>(<span>unit <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
unit</span></code></div><div class="spec-doc"><p><code>pp_set_formatter_output_functions ppf out flush</code> redirects the standard pretty-printer output functions to the functions <code>out</code> and <code>flush</code>.</p><p>The <code>out</code> function performs all the pretty-printer string output. It is called with a string <code>s</code>, a start position <code>p</code>, and a number of characters <code>n</code>; it is supposed to output characters <code>p</code> to <code>p + n - 1</code> of <code>s</code>.</p><p>The <code>flush</code> function is called whenever the pretty-printer is flushed (via conversion <code>%!</code>, or pretty-printing indications <code>@?</code> or <code>@.</code>, or using low level functions <code>print_flush</code> or <code>print_newline</code>).</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_formatter_output_functions"><a href="#val-pp_get_formatter_output_functions" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_formatter_output_functions :
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span>unit <span class="arrow">-></span></span>
|
||
<span>(<span>string <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit)</span> * <span>(<span>unit <span class="arrow">-></span></span> unit)</span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_formatter_output_functions"><a href="#val-get_formatter_output_functions" class="anchor"></a><code><span><span class="keyword">val</span> get_formatter_output_functions :
|
||
<span>unit <span class="arrow">-></span></span>
|
||
<span>(<span>string <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit)</span> * <span>(<span>unit <span class="arrow">-></span></span> unit)</span></span></code></div><div class="spec-doc"><p>Return the current output functions of the standard pretty-printer.</p></div></div><h2 id="meaning"><a href="#meaning" class="anchor"></a>Redefining formatter output</h2><p>The <code>Format</code> module is versatile enough to let you completely redefine the meaning of pretty-printing output: you may provide your own functions to define how to handle indentation, line splitting, and even printing of all the characters that have to be printed!</p><h3 id="redefining-output-functions"><a href="#redefining-output-functions" class="anchor"></a>Redefining output functions</h3><div class="odoc-spec"><div class="spec type anchored" id="type-formatter_out_functions"><a href="#type-formatter_out_functions" class="anchor"></a><code><span><span class="keyword">type</span> formatter_out_functions</span><span> = <a href="../../ocaml/Stdlib/Format/index.html#type-formatter_out_functions">Stdlib.Format.formatter_out_functions</a></span><span> = </span><span>{</span></code><ol><li id="type-formatter_out_functions.out_string" class="def record field anchored"><a href="#type-formatter_out_functions.out_string" class="anchor"></a><code><span>out_string : <span>string <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit;</span></code></li><li id="type-formatter_out_functions.out_flush" class="def record field anchored"><a href="#type-formatter_out_functions.out_flush" class="anchor"></a><code><span>out_flush : <span>unit <span class="arrow">-></span></span> unit;</span></code></li><li id="type-formatter_out_functions.out_newline" class="def record field anchored"><a href="#type-formatter_out_functions.out_newline" class="anchor"></a><code><span>out_newline : <span>unit <span class="arrow">-></span></span> unit;</span></code></li><li id="type-formatter_out_functions.out_spaces" class="def record field anchored"><a href="#type-formatter_out_functions.out_spaces" class="anchor"></a><code><span>out_spaces : <span>int <span class="arrow">-></span></span> unit;</span></code></li><li id="type-formatter_out_functions.out_indent" class="def record field anchored"><a href="#type-formatter_out_functions.out_indent" class="anchor"></a><code><span>out_indent : <span>int <span class="arrow">-></span></span> unit;</span></code><div class="def-doc"><span class="comment-delim">(*</span><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.06.0</li></ul><span class="comment-delim">*)</span></div></li></ol><code><span>}</span></code></div><div class="spec-doc"><p>The set of output functions specific to a formatter:</p><ul><li>the <code>out_string</code> function performs all the pretty-printer string output. It is called with a string <code>s</code>, a start position <code>p</code>, and a number of characters <code>n</code>; it is supposed to output characters <code>p</code> to <code>p + n - 1</code> of <code>s</code>.</li><li>the <code>out_flush</code> function flushes the pretty-printer output device.</li><li><code>out_newline</code> is called to open a new line when the pretty-printer splits the line.</li><li>the <code>out_spaces</code> function outputs spaces when a break hint leads to spaces instead of a line split. It is called with the number of spaces to output.</li><li>the <code>out_indent</code> function performs new line indentation when the pretty-printer splits the line. It is called with the indentation value of the new line.</li></ul><p>By default:</p><ul><li>fields <code>out_string</code> and <code>out_flush</code> are output device specific; (e.g. <a href="../../ocaml/Stdlib/index.html#val-output_string"><code>Stdlib.output_string</code></a> and <a href="../../ocaml/Stdlib/index.html#val-flush"><code>Stdlib.flush</code></a> for a <a href="../../ocaml/Stdlib/index.html#type-out_channel"><code>Stdlib.out_channel</code></a> device, or <code>Buffer.add_substring</code> and <a href="../../ocaml/Stdlib/index.html#val-ignore"><code>Stdlib.ignore</code></a> for a <code>Buffer.t</code> output device),</li><li>field <code>out_newline</code> is equivalent to <code>out_string "\n" 0 1</code>;</li><li>fields <code>out_spaces</code> and <code>out_indent</code> are equivalent to <code>out_string (String.make n ' ') 0 n</code>.</li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.01.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_formatter_out_functions"><a href="#val-pp_set_formatter_out_functions" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_formatter_out_functions :
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span><a href="#type-formatter_out_functions">formatter_out_functions</a> <span class="arrow">-></span></span>
|
||
unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_formatter_out_functions"><a href="#val-set_formatter_out_functions" class="anchor"></a><code><span><span class="keyword">val</span> set_formatter_out_functions : <span><a href="#type-formatter_out_functions">formatter_out_functions</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_set_formatter_out_functions ppf out_funs</code> Set all the pretty-printer output functions of <code>ppf</code> to those of argument <code>out_funs</code>,</p><p>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).</p><p>Reasonable defaults for functions <code>out_spaces</code> and <code>out_newline</code> are respectively <code>out_funs.out_string (String.make n ' ') 0 n</code> and <code>out_funs.out_string "\n" 0 1</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.01.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_formatter_out_functions"><a href="#val-pp_get_formatter_out_functions" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_formatter_out_functions :
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span>unit <span class="arrow">-></span></span>
|
||
<a href="#type-formatter_out_functions">formatter_out_functions</a></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_formatter_out_functions"><a href="#val-get_formatter_out_functions" class="anchor"></a><code><span><span class="keyword">val</span> get_formatter_out_functions : <span>unit <span class="arrow">-></span></span> <a href="#type-formatter_out_functions">formatter_out_functions</a></span></code></div><div class="spec-doc"><p>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.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.01.0</li></ul></div></div><h2 id="tagsmeaning"><a href="#tagsmeaning" class="anchor"></a>Redefining semantic tag operations</h2><div class="odoc-spec"><div class="spec type anchored" id="type-formatter_stag_functions"><a href="#type-formatter_stag_functions" class="anchor"></a><code><span><span class="keyword">type</span> formatter_stag_functions</span><span> = <a href="../../ocaml/Stdlib/Format/index.html#type-formatter_stag_functions">Stdlib.Format.formatter_stag_functions</a></span><span> = </span><span>{</span></code><ol><li id="type-formatter_stag_functions.mark_open_stag" class="def record field anchored"><a href="#type-formatter_stag_functions.mark_open_stag" class="anchor"></a><code><span>mark_open_stag : <span><a href="#type-stag">stag</a> <span class="arrow">-></span></span> string;</span></code></li><li id="type-formatter_stag_functions.mark_close_stag" class="def record field anchored"><a href="#type-formatter_stag_functions.mark_close_stag" class="anchor"></a><code><span>mark_close_stag : <span><a href="#type-stag">stag</a> <span class="arrow">-></span></span> string;</span></code></li><li id="type-formatter_stag_functions.print_open_stag" class="def record field anchored"><a href="#type-formatter_stag_functions.print_open_stag" class="anchor"></a><code><span>print_open_stag : <span><a href="#type-stag">stag</a> <span class="arrow">-></span></span> unit;</span></code></li><li id="type-formatter_stag_functions.print_close_stag" class="def record field anchored"><a href="#type-formatter_stag_functions.print_close_stag" class="anchor"></a><code><span>print_close_stag : <span><a href="#type-stag">stag</a> <span class="arrow">-></span></span> unit;</span></code></li></ol><code><span>}</span></code></div><div class="spec-doc"><p>The semantic tag handling functions specific to a formatter: <code>mark</code> versions are the 'tag-marking' functions that associate a string marker to a tag in order for the pretty-printing engine to write those markers as 0 length tokens in the output device of the formatter. <code>print</code> versions are the 'tag-printing' functions that can perform regular printing when a tag is closed or opened.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_formatter_stag_functions"><a href="#val-pp_set_formatter_stag_functions" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_formatter_stag_functions :
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span><a href="#type-formatter_stag_functions">formatter_stag_functions</a> <span class="arrow">-></span></span>
|
||
unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_formatter_stag_functions"><a href="#val-set_formatter_stag_functions" class="anchor"></a><code><span><span class="keyword">val</span> set_formatter_stag_functions : <span><a href="#type-formatter_stag_functions">formatter_stag_functions</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_set_formatter_stag_functions ppf tag_funs</code> changes the meaning of opening and closing semantic tag operations to use the functions in <code>tag_funs</code> when printing on <code>ppf</code>.</p><p>When opening a semantic tag with name <code>t</code>, the string <code>t</code> is passed to the opening tag-marking function (the <code>mark_open_stag</code> field of the record <code>tag_funs</code>), that must return the opening tag marker for that name. When the next call to <code>close_stag ()</code> happens, the semantic tag name <code>t</code> is sent back to the closing tag-marking function (the <code>mark_close_stag</code> field of record <code>tag_funs</code>), that must return a closing tag marker for that name.</p><p>The <code>print_</code> field of the record contains the tag-printing functions that are called at tag opening and tag closing time, to output regular material in the pretty-printer queue.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_formatter_stag_functions"><a href="#val-pp_get_formatter_stag_functions" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_formatter_stag_functions :
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span>unit <span class="arrow">-></span></span>
|
||
<a href="#type-formatter_stag_functions">formatter_stag_functions</a></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_formatter_stag_functions"><a href="#val-get_formatter_stag_functions" class="anchor"></a><code><span><span class="keyword">val</span> get_formatter_stag_functions : <span>unit <span class="arrow">-></span></span> <a href="#type-formatter_stag_functions">formatter_stag_functions</a></span></code></div><div class="spec-doc"><p>Return the current semantic tag operation functions of the standard pretty-printer.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><h2 id="formatter"><a href="#formatter" class="anchor"></a>Defining formatters</h2><p>Defining new formatters permits unrelated output of material in parallel on several output devices. All the parameters of a formatter are local to the formatter: right margin, maximum indentation limit, maximum number of pretty-printing boxes simultaneously open, ellipsis, and so on, are specific to each formatter and may be fixed independently.</p><p>For instance, given a <code>Buffer</code>.t buffer <code>b</code>, <a href="#val-formatter_of_buffer"><code>formatter_of_buffer</code></a> <code>b</code> returns a new formatter using buffer <code>b</code> as its output device. Similarly, given a <a href="../../ocaml/Stdlib/index.html#type-out_channel"><code>Stdlib.out_channel</code></a> output channel <code>oc</code>, <a href="#val-formatter_of_out_channel"><code>formatter_of_out_channel</code></a> <code>oc</code> returns a new formatter using channel <code>oc</code> as its output device.</p><p>Alternatively, given <code>out_funs</code>, a complete set of output functions for a formatter, then <a href="#val-formatter_of_out_functions"><code>formatter_of_out_functions</code></a> <code>out_funs</code> computes a new formatter using those functions for output.</p><div class="odoc-spec"><div class="spec value anchored" id="val-formatter_of_out_channel"><a href="#val-formatter_of_out_channel" class="anchor"></a><code><span><span class="keyword">val</span> formatter_of_out_channel : <span><a href="../../ocaml/Stdlib/index.html#type-out_channel">out_channel</a> <span class="arrow">-></span></span> <a href="#type-formatter">formatter</a></span></code></div><div class="spec-doc"><p><code>formatter_of_out_channel oc</code> returns a new formatter writing to the corresponding output channel <code>oc</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-std_formatter"><a href="#val-std_formatter" class="anchor"></a><code><span><span class="keyword">val</span> std_formatter : <a href="#type-formatter">formatter</a></span></code></div><div class="spec-doc"><p>The standard formatter to write to standard output.</p><p>It is defined as <a href="#val-formatter_of_out_channel"><code>formatter_of_out_channel</code></a> <a href="../../ocaml/Stdlib/index.html#val-stdout"><code>Stdlib.stdout</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-err_formatter"><a href="#val-err_formatter" class="anchor"></a><code><span><span class="keyword">val</span> err_formatter : <a href="#type-formatter">formatter</a></span></code></div><div class="spec-doc"><p>A formatter to write to standard error.</p><p>It is defined as <a href="#val-formatter_of_out_channel"><code>formatter_of_out_channel</code></a> <a href="../../ocaml/Stdlib/index.html#val-stderr"><code>Stdlib.stderr</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-formatter_of_buffer"><a href="#val-formatter_of_buffer" class="anchor"></a><code><span><span class="keyword">val</span> formatter_of_buffer : <span><a href="../../ocaml/Stdlib/Buffer/index.html#type-t">Stdlib.Buffer.t</a> <span class="arrow">-></span></span> <a href="#type-formatter">formatter</a></span></code></div><div class="spec-doc"><p><code>formatter_of_buffer b</code> returns a new formatter writing to buffer <code>b</code>. At the end of pretty-printing, the formatter must be flushed using <a href="#val-pp_print_flush"><code>pp_print_flush</code></a> or <a href="#val-pp_print_newline"><code>pp_print_newline</code></a>, to print all the pending material into the buffer.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-stdbuf"><a href="#val-stdbuf" class="anchor"></a><code><span><span class="keyword">val</span> stdbuf : <a href="../../ocaml/Stdlib/Buffer/index.html#type-t">Stdlib.Buffer.t</a></span></code></div><div class="spec-doc"><p>The string buffer in which <code>str_formatter</code> writes.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-str_formatter"><a href="#val-str_formatter" class="anchor"></a><code><span><span class="keyword">val</span> str_formatter : <a href="#type-formatter">formatter</a></span></code></div><div class="spec-doc"><p>A formatter to output to the <a href="#val-stdbuf"><code>stdbuf</code></a> string buffer.</p><p><code>str_formatter</code> is defined as <a href="#val-formatter_of_buffer"><code>formatter_of_buffer</code></a> <a href="#val-stdbuf"><code>stdbuf</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-flush_str_formatter"><a href="#val-flush_str_formatter" class="anchor"></a><code><span><span class="keyword">val</span> flush_str_formatter : <span>unit <span class="arrow">-></span></span> string</span></code></div><div class="spec-doc"><p>Returns the material printed with <code>str_formatter</code>, flushes the formatter and resets the corresponding buffer.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-make_formatter"><a href="#val-make_formatter" class="anchor"></a><code><span><span class="keyword">val</span> make_formatter :
|
||
<span><span>(<span>string <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><span>(<span>unit <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<a href="#type-formatter">formatter</a></span></code></div><div class="spec-doc"><p><code>make_formatter out flush</code> returns a new formatter that outputs with function <code>out</code>, and flushes with function <code>flush</code>.</p><p>For instance,</p><pre class="language-ocaml"><code>make_formatter
|
||
(Stdlib.output oc)
|
||
(fun () -> Stdlib.flush oc)</code></pre><p>returns a formatter to the <a href="../../ocaml/Stdlib/index.html#type-out_channel"><code>Stdlib.out_channel</code></a> <code>oc</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-formatter_of_out_functions"><a href="#val-formatter_of_out_functions" class="anchor"></a><code><span><span class="keyword">val</span> formatter_of_out_functions : <span><a href="#type-formatter_out_functions">formatter_out_functions</a> <span class="arrow">-></span></span> <a href="#type-formatter">formatter</a></span></code></div><div class="spec-doc"><p><code>formatter_of_out_functions out_funs</code> returns a new formatter that writes with the set of output functions <code>out_funs</code>.</p><p>See definition of type <a href="#type-formatter_out_functions"><code>formatter_out_functions</code></a> for the meaning of argument <code>out_funs</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.06.0</li></ul></div></div><h3 id="symbolic"><a href="#symbolic" class="anchor"></a>Symbolic pretty-printing</h3><p>Symbolic pretty-printing is pretty-printing using a symbolic formatter, i.e. a formatter that outputs symbolic pretty-printing items.</p><p>When using a symbolic formatter, all regular pretty-printing activities occur but output material is symbolic and stored in a buffer of output items. At the end of pretty-printing, flushing the output buffer allows post-processing of symbolic output before performing low level output operations.</p><p>In practice, first define a symbolic output buffer <code>b</code> using:</p><ul><li><code>let sob = make_symbolic_output_buffer ()</code>. Then define a symbolic formatter with:</li><li><code>let ppf = formatter_of_symbolic_output_buffer sob</code></li></ul><p>Use symbolic formatter <code>ppf</code> as usual, and retrieve symbolic items at end of pretty-printing by flushing symbolic output buffer <code>sob</code> with:</p><ul><li><code>flush_symbolic_output_buffer sob</code>.</li></ul><div class="odoc-spec"><div class="spec type anchored" id="type-symbolic_output_item"><a href="#type-symbolic_output_item" class="anchor"></a><code><span><span class="keyword">type</span> symbolic_output_item</span><span> = <a href="../../ocaml/Stdlib/Format/index.html#type-symbolic_output_item">Stdlib.Format.symbolic_output_item</a></span><span> = </span></code><ol><li id="type-symbolic_output_item.Output_flush" class="def variant constructor anchored"><a href="#type-symbolic_output_item.Output_flush" class="anchor"></a><code><span>| </span><span><span class="constructor">Output_flush</span></span></code><div class="def-doc"><span class="comment-delim">(*</span><p>symbolic flush command</p><span class="comment-delim">*)</span></div></li><li id="type-symbolic_output_item.Output_newline" class="def variant constructor anchored"><a href="#type-symbolic_output_item.Output_newline" class="anchor"></a><code><span>| </span><span><span class="constructor">Output_newline</span></span></code><div class="def-doc"><span class="comment-delim">(*</span><p>symbolic newline command</p><span class="comment-delim">*)</span></div></li><li id="type-symbolic_output_item.Output_string" class="def variant constructor anchored"><a href="#type-symbolic_output_item.Output_string" class="anchor"></a><code><span>| </span><span><span class="constructor">Output_string</span> <span class="keyword">of</span> string</span></code><div class="def-doc"><span class="comment-delim">(*</span><p><code>Output_string s</code>: symbolic output for string <code>s</code></p><span class="comment-delim">*)</span></div></li><li id="type-symbolic_output_item.Output_spaces" class="def variant constructor anchored"><a href="#type-symbolic_output_item.Output_spaces" class="anchor"></a><code><span>| </span><span><span class="constructor">Output_spaces</span> <span class="keyword">of</span> int</span></code><div class="def-doc"><span class="comment-delim">(*</span><p><code>Output_spaces n</code>: symbolic command to output <code>n</code> spaces</p><span class="comment-delim">*)</span></div></li><li id="type-symbolic_output_item.Output_indent" class="def variant constructor anchored"><a href="#type-symbolic_output_item.Output_indent" class="anchor"></a><code><span>| </span><span><span class="constructor">Output_indent</span> <span class="keyword">of</span> int</span></code><div class="def-doc"><span class="comment-delim">(*</span><p><code>Output_indent i</code>: symbolic indentation of size <code>i</code></p><span class="comment-delim">*)</span></div></li></ol></div><div class="spec-doc"><p>Items produced by symbolic pretty-printers</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.06.0</li></ul></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-symbolic_output_buffer"><a href="#type-symbolic_output_buffer" class="anchor"></a><code><span><span class="keyword">type</span> symbolic_output_buffer</span><span> = <a href="../../ocaml/Stdlib/Format/index.html#type-symbolic_output_buffer">Stdlib.Format.symbolic_output_buffer</a></span></code></div><div class="spec-doc"><p>The output buffer of a symbolic pretty-printer.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.06.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-make_symbolic_output_buffer"><a href="#val-make_symbolic_output_buffer" class="anchor"></a><code><span><span class="keyword">val</span> make_symbolic_output_buffer : <span>unit <span class="arrow">-></span></span> <a href="#type-symbolic_output_buffer">symbolic_output_buffer</a></span></code></div><div class="spec-doc"><p><code>make_symbolic_output_buffer ()</code> returns a fresh buffer for symbolic output.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.06.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-clear_symbolic_output_buffer"><a href="#val-clear_symbolic_output_buffer" class="anchor"></a><code><span><span class="keyword">val</span> clear_symbolic_output_buffer : <span><a href="#type-symbolic_output_buffer">symbolic_output_buffer</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>clear_symbolic_output_buffer sob</code> resets buffer <code>sob</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.06.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_symbolic_output_buffer"><a href="#val-get_symbolic_output_buffer" class="anchor"></a><code><span><span class="keyword">val</span> get_symbolic_output_buffer :
|
||
<span><a href="#type-symbolic_output_buffer">symbolic_output_buffer</a> <span class="arrow">-></span></span>
|
||
<span><a href="#type-symbolic_output_item">symbolic_output_item</a> list</span></span></code></div><div class="spec-doc"><p><code>get_symbolic_output_buffer sob</code> returns the contents of buffer <code>sob</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.06.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-flush_symbolic_output_buffer"><a href="#val-flush_symbolic_output_buffer" class="anchor"></a><code><span><span class="keyword">val</span> flush_symbolic_output_buffer :
|
||
<span><a href="#type-symbolic_output_buffer">symbolic_output_buffer</a> <span class="arrow">-></span></span>
|
||
<span><a href="#type-symbolic_output_item">symbolic_output_item</a> list</span></span></code></div><div class="spec-doc"><p><code>flush_symbolic_output_buffer sob</code> returns the contents of buffer <code>sob</code> and resets buffer <code>sob</code>. <code>flush_symbolic_output_buffer sob</code> is equivalent to <code>let items = get_symbolic_output_buffer sob in
|
||
clear_symbolic_output_buffer sob; items</code></p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.06.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-add_symbolic_output_item"><a href="#val-add_symbolic_output_item" class="anchor"></a><code><span><span class="keyword">val</span> add_symbolic_output_item :
|
||
<span><a href="#type-symbolic_output_buffer">symbolic_output_buffer</a> <span class="arrow">-></span></span>
|
||
<span><a href="#type-symbolic_output_item">symbolic_output_item</a> <span class="arrow">-></span></span>
|
||
unit</span></code></div><div class="spec-doc"><p><code>add_symbolic_output_item sob itm</code> adds item <code>itm</code> to buffer <code>sob</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.06.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-formatter_of_symbolic_output_buffer"><a href="#val-formatter_of_symbolic_output_buffer" class="anchor"></a><code><span><span class="keyword">val</span> formatter_of_symbolic_output_buffer : <span><a href="#type-symbolic_output_buffer">symbolic_output_buffer</a> <span class="arrow">-></span></span> <a href="#type-formatter">formatter</a></span></code></div><div class="spec-doc"><p><code>formatter_of_symbolic_output_buffer sob</code> returns a symbolic formatter that outputs to <code>symbolic_output_buffer</code> <code>sob</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.06.0</li></ul></div></div><h2 id="convenience-formatting-functions."><a href="#convenience-formatting-functions." class="anchor"></a>Convenience formatting functions.</h2><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_list"><a href="#val-pp_print_list" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_list :
|
||
<span>?pp_sep:<span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span><span><span class="type-var">'a</span> list</span> <span class="arrow">-></span></span>
|
||
unit</span></code></div><div class="spec-doc"><p><code>pp_print_list ?pp_sep pp_v ppf l</code> prints items of list <code>l</code>, using <code>pp_v</code> to print each item, and calling <code>pp_sep</code> between items (<code>pp_sep</code> defaults to <a href="#val-pp_print_cut"><code>pp_print_cut</code></a>. Does nothing on empty lists.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.02.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_seq"><a href="#val-pp_print_seq" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_seq :
|
||
<span>?pp_sep:<span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span><span><span class="type-var">'a</span> <a href="../../ocaml/Stdlib/Seq/index.html#type-t">Stdlib.Seq.t</a></span> <span class="arrow">-></span></span>
|
||
unit</span></code></div><div class="spec-doc"><p><code>pp_print_seq ?pp_sep pp_v ppf s</code> prints items of sequence <code>s</code>, using <code>pp_v</code> to print each item, and calling <code>pp_sep</code> between items (<code>pp_sep</code> defaults to <a href="#val-pp_print_cut"><code>pp_print_cut</code></a>. Does nothing on empty sequences.</p><p>This function does not terminate on infinite sequences.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.12</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_text"><a href="#val-pp_print_text" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_text : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>string <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>pp_print_text ppf s</code> prints <code>s</code> with spaces and newlines respectively printed using <a href="#val-pp_print_space"><code>pp_print_space</code></a> and <a href="#val-pp_force_newline"><code>pp_force_newline</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.02.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_option"><a href="#val-pp_print_option" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_option :
|
||
<span>?none:<span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span><span><span class="type-var">'a</span> option</span> <span class="arrow">-></span></span>
|
||
unit</span></code></div><div class="spec-doc"><p><code>pp_print_option ?none pp_v ppf o</code> prints <code>o</code> on <code>ppf</code> using <code>pp_v</code> if <code>o</code> is <code>Some v</code> and <code>none</code> if it is <code>None</code>. <code>none</code> prints nothing by default.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_result"><a href="#val-pp_print_result" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_result :
|
||
<span>ok:<span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span>error:<span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><span class="type-var">'e</span> <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'e</span>)</span> <a href="../../ocaml/Stdlib/index.html#type-result">result</a></span> <span class="arrow">-></span></span>
|
||
unit</span></code></div><div class="spec-doc"><p><code>pp_print_result ~ok ~error ppf r</code> prints <code>r</code> on <code>ppf</code> using <code>ok</code> if <code>r</code> is <code>Ok _</code> and <code>error</code> if <code>r</code> is <code>Error _</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_print_either"><a href="#val-pp_print_either" class="anchor"></a><code><span><span class="keyword">val</span> pp_print_either :
|
||
<span>left:<span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span>right:<span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><span class="type-var">'b</span> <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="../../ocaml/Stdlib/Either/index.html#type-t">Stdlib.Either.t</a></span> <span class="arrow">-></span></span>
|
||
unit</span></code></div><div class="spec-doc"><p><code>pp_print_either ~left ~right ppf e</code> prints <code>e</code> on <code>ppf</code> using <code>left</code> if <code>e</code> is <code>Either.Left _</code> and <code>right</code> if <code>e</code> is <code>Either.Right _</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.13</li></ul></div></div><h2 id="fpp"><a href="#fpp" class="anchor"></a>Formatted pretty-printing</h2><p>Module <code>Format</code> provides a complete set of <code>printf</code> like functions for pretty-printing using format string specifications.</p><p>Specific annotations may be added in the format strings to give pretty-printing commands to the pretty-printing engine.</p><p>Those annotations are introduced in the format strings using the <code>@</code> character. For instance, <code>@ </code> means a space break, <code>@,</code> means a cut, <code>@[</code> opens a new box, and <code>@]</code> closes the last open box.</p><p><code>fprintf ff fmt arg1 ... argN</code> formats the arguments <code>arg1</code> to <code>argN</code> according to the format string <code>fmt</code>, and outputs the resulting string on the formatter <code>ff</code>.</p><p>The format string <code>fmt</code> is a character string which contains three types of objects: plain characters and conversion specifications as specified in the <code>Printf</code> module, and pretty-printing indications specific to the <code>Format</code> module.</p><p>The pretty-printing indication characters are introduced by a <code>@</code> character, and their meanings are:</p><ul><li><code>@[</code>: open a pretty-printing box. The type and offset of the box may be optionally specified with the following syntax: the <code><</code> character, followed by an optional box type indication, then an optional integer offset, and the closing <code>></code> character. Pretty-printing box type is one of <code>h</code>, <code>v</code>, <code>hv</code>, <code>b</code>, or <code>hov</code>. '<code>h</code>' stands for an 'horizontal' pretty-printing box, '<code>v</code>' stands for a 'vertical' pretty-printing box, '<code>hv</code>' stands for an 'horizontal/vertical' pretty-printing box, '<code>b</code>' stands for an 'horizontal-or-vertical' pretty-printing box demonstrating indentation, '<code>hov</code>' stands a simple 'horizontal-or-vertical' pretty-printing box. For instance, <code>@[<hov 2></code> opens an 'horizontal-or-vertical' pretty-printing box with indentation 2 as obtained with <code>open_hovbox 2</code>. For more details about pretty-printing boxes, see the various box opening functions <code>open_*box</code>.</li><li><code>@]</code>: close the most recently opened pretty-printing box.</li><li><code>@,</code>: output a 'cut' break hint, as with <code>print_cut ()</code>.</li><li><code>@ </code>: output a 'space' break hint, as with <code>print_space ()</code>.</li><li><code>@;</code>: output a 'full' break hint as with <code>print_break</code>. The <code>nspaces</code> and <code>offset</code> parameters of the break hint may be optionally specified with the following syntax: the <code><</code> character, followed by an integer <code>nspaces</code> value, then an integer <code>offset</code>, and a closing <code>></code> character. If no parameters are provided, the good break defaults to a 'space' break hint.</li><li><code>@.</code>: flush the pretty-printer and split the line, as with <code>print_newline ()</code>.</li><li><code>@<n></code>: print the following item as if it were of length <code>n</code>. Hence, <code>printf "@<0>%s" arg</code> prints <code>arg</code> as a zero length string. If <code>@<n></code> is not followed by a conversion specification, then the following character of the format is printed as if it were of length <code>n</code>.</li><li><code>@\{</code>: open a semantic tag. The name of the tag may be optionally specified with the following syntax: the <code><</code> character, followed by an optional string specification, and the closing <code>></code> character. The string specification is any character string that does not contain the closing character <code>'>'</code>. If omitted, the tag name defaults to the empty string. For more details about semantic tags, see the functions <a href="#val-open_stag"><code>open_stag</code></a> and <a href="#val-close_stag"><code>close_stag</code></a>.</li><li><code>@\}</code>: close the most recently opened semantic tag.</li><li><code>@?</code>: flush the pretty-printer as with <code>print_flush ()</code>. This is equivalent to the conversion <code>%!</code>.</li><li><code>@\n</code>: force a newline, as with <code>force_newline ()</code>, not the normal way of pretty-printing, you should prefer using break hints inside a vertical pretty-printing box.</li></ul><p>Note: To prevent the interpretation of a <code>@</code> character as a pretty-printing indication, escape it with a <code>%</code> character. Old quotation mode <code>@@</code> is deprecated since it is not compatible with formatted input interpretation of character <code>'@'</code>.</p><p>Example: <code>printf "@[%s@ %d@]@." "x =" 1</code> is equivalent to <code>open_box (); print_string "x ="; print_space ();
|
||
print_int 1; close_box (); print_newline ()</code>. It prints <code>x = 1</code> within a pretty-printing 'horizontal-or-vertical' box.</p><div class="odoc-spec"><div class="spec value anchored" id="val-printf"><a href="#val-printf" class="anchor"></a><code><span><span class="keyword">val</span> printf : <span><span><span>(<span class="type-var">'a</span>, <a href="#type-formatter">formatter</a>, unit)</span> <a href="../../ocaml/Stdlib/index.html#type-format">format</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <code>fprintf</code> above, but output on <code>std_formatter</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-eprintf"><a href="#val-eprintf" class="anchor"></a><code><span><span class="keyword">val</span> eprintf : <span><span><span>(<span class="type-var">'a</span>, <a href="#type-formatter">formatter</a>, unit)</span> <a href="../../ocaml/Stdlib/index.html#type-format">format</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <code>fprintf</code> above, but output on <code>err_formatter</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-asprintf"><a href="#val-asprintf" class="anchor"></a><code><span><span class="keyword">val</span> asprintf : <span><span><span>(<span class="type-var">'a</span>, <a href="#type-formatter">formatter</a>, unit, string)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <code>printf</code> above, but instead of printing on a formatter, returns a string containing the result of formatting the arguments. The type of <code>asprintf</code> is general enough to interact nicely with <code>%a</code> conversions.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.01.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-dprintf"><a href="#val-dprintf" class="anchor"></a><code><span><span class="keyword">val</span> dprintf : <span><span><span>(<span class="type-var">'a</span>, <a href="#type-formatter">formatter</a>, unit, <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> unit)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <a href="#val-fprintf"><code>fprintf</code></a>, except the formatter is the last argument. <code>dprintf "..." a b c</code> is a function of type <code>formatter -> unit</code> which can be given to a format specifier <code>%t</code>.</p><p>This can be used as a replacement for <a href="#val-asprintf"><code>asprintf</code></a> to delay formatting decisions. Using the string returned by <a href="#val-asprintf"><code>asprintf</code></a> in a formatting context forces formatting decisions to be taken in isolation, and the final string may be created prematurely. <a href="#val-dprintf"><code>dprintf</code></a> allows delay of formatting decisions until the final formatting context is known. For example:</p><pre class="language-ocaml"><code>let t = Format.dprintf "%i@ %i@ %i" 1 2 3 in
|
||
...
|
||
Format.printf "@[<v>%t@]" t</code></pre><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-ifprintf"><a href="#val-ifprintf" class="anchor"></a><code><span><span class="keyword">val</span> ifprintf : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><span><span>(<span class="type-var">'a</span>, <a href="#type-formatter">formatter</a>, unit)</span> <a href="../../ocaml/Stdlib/index.html#type-format">format</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Same as <code>fprintf</code> above, but does not print anything. Useful to ignore some material when conditionally printing.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.10.0</li></ul></div></div><p>Formatted Pretty-Printing with continuations.</p><div class="odoc-spec"><div class="spec value anchored" id="val-kfprintf"><a href="#val-kfprintf" class="anchor"></a><code><span><span class="keyword">val</span> kfprintf :
|
||
<span><span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span class="type-var">'a</span>)</span> <span class="arrow">-></span></span>
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span><span><span>(<span class="type-var">'b</span>, <a href="#type-formatter">formatter</a>, unit, <span class="type-var">'a</span>)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span>
|
||
<span class="type-var">'b</span></span></code></div><div class="spec-doc"><p>Same as <code>fprintf</code> above, but instead of returning immediately, passes the formatter to its first argument at the end of printing.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-kdprintf"><a href="#val-kdprintf" class="anchor"></a><code><span><span class="keyword">val</span> kdprintf :
|
||
<span><span>(<span><span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span> <span class="type-var">'a</span>)</span> <span class="arrow">-></span></span>
|
||
<span><span><span>(<span class="type-var">'b</span>, <a href="#type-formatter">formatter</a>, unit, <span class="type-var">'a</span>)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span>
|
||
<span class="type-var">'b</span></span></code></div><div class="spec-doc"><p>Same as <a href="#val-dprintf"><code>dprintf</code></a> above, but instead of returning immediately, passes the suspended printer to its first argument at the end of printing.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-ikfprintf"><a href="#val-ikfprintf" class="anchor"></a><code><span><span class="keyword">val</span> ikfprintf :
|
||
<span><span>(<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span class="type-var">'a</span>)</span> <span class="arrow">-></span></span>
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span><span><span>(<span class="type-var">'b</span>, <a href="#type-formatter">formatter</a>, unit, <span class="type-var">'a</span>)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span>
|
||
<span class="type-var">'b</span></span></code></div><div class="spec-doc"><p>Same as <code>kfprintf</code> above, but does not print anything. Useful to ignore some material when conditionally printing.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.12.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-kasprintf"><a href="#val-kasprintf" class="anchor"></a><code><span><span class="keyword">val</span> kasprintf : <span><span>(<span>string <span class="arrow">-></span></span> <span class="type-var">'a</span>)</span> <span class="arrow">-></span></span> <span><span><span>(<span class="type-var">'b</span>, <a href="#type-formatter">formatter</a>, unit, <span class="type-var">'a</span>)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span> <span class="type-var">'b</span></span></code></div><div class="spec-doc"><p>Same as <code>asprintf</code> above, but instead of returning the string, passes it to the first argument.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.03</li></ul></div></div><h2 id="deprecated"><a href="#deprecated" class="anchor"></a>Deprecated</h2><div class="odoc-spec"><div class="spec value anchored" id="val-bprintf"><a href="#val-bprintf" class="anchor"></a><code><span><span class="keyword">val</span> bprintf : <span><a href="../../ocaml/Stdlib/Buffer/index.html#type-t">Stdlib.Buffer.t</a> <span class="arrow">-></span></span> <span><span><span>(<span class="type-var">'a</span>, <a href="#type-formatter">formatter</a>, unit)</span> <a href="../../ocaml/Stdlib/index.html#type-format">format</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>This function is error prone. Do not use it. This function is neither compositional nor incremental, since it flushes the pretty-printer queue at each call.</p><p>If you need to print to some buffer <code>b</code>, you must first define a formatter writing to <code>b</code>, using <code>let to_b = formatter_of_buffer b</code>; then use regular calls to <code>Format.fprintf</code> with formatter <code>to_b</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-kprintf"><a href="#val-kprintf" class="anchor"></a><code><span><span class="keyword">val</span> kprintf : <span><span>(<span>string <span class="arrow">-></span></span> <span class="type-var">'a</span>)</span> <span class="arrow">-></span></span> <span><span><span>(<span class="type-var">'b</span>, unit, string, <span class="type-var">'a</span>)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span> <span class="type-var">'b</span></span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>An alias for <code>ksprintf</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_all_formatter_output_functions"><a href="#val-set_all_formatter_output_functions" class="anchor"></a><code><span><span class="keyword">val</span> set_all_formatter_output_functions :
|
||
<span>out:<span>(<span>string <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span>flush:<span>(<span>unit <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span>newline:<span>(<span>unit <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span>spaces:<span>(<span>int <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
unit</span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <code>set_formatter_out_functions</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_all_formatter_output_functions"><a href="#val-get_all_formatter_output_functions" class="anchor"></a><code><span><span class="keyword">val</span> get_all_formatter_output_functions :
|
||
<span>unit <span class="arrow">-></span></span>
|
||
<span>(<span>string <span class="arrow">-></span></span>
|
||
<span>int <span class="arrow">-></span></span>
|
||
<span>int <span class="arrow">-></span></span>
|
||
unit)</span>
|
||
* <span>(<span>unit <span class="arrow">-></span></span>
|
||
unit)</span>
|
||
* <span>(<span>unit <span class="arrow">-></span></span>
|
||
unit)</span>
|
||
* <span>(<span>int <span class="arrow">-></span></span>
|
||
unit)</span></span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <code>get_formatter_out_functions</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_all_formatter_output_functions"><a href="#val-pp_set_all_formatter_output_functions" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_all_formatter_output_functions :
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span>out:<span>(<span>string <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span>flush:<span>(<span>unit <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span>newline:<span>(<span>unit <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span>spaces:<span>(<span>int <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
unit</span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <code>pp_set_formatter_out_functions</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_all_formatter_output_functions"><a href="#val-pp_get_all_formatter_output_functions" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_all_formatter_output_functions :
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span>unit <span class="arrow">-></span></span>
|
||
<span>(<span>string <span class="arrow">-></span></span>
|
||
<span>int <span class="arrow">-></span></span>
|
||
<span>int <span class="arrow">-></span></span>
|
||
unit)</span>
|
||
* <span>(<span>unit <span class="arrow">-></span></span>
|
||
unit)</span>
|
||
* <span>(<span>unit <span class="arrow">-></span></span>
|
||
unit)</span>
|
||
* <span>(<span>int <span class="arrow">-></span></span>
|
||
unit)</span></span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <code>pp_get_formatter_out_functions</code>.</p></li></ul></div></div><h3 id="string-tags"><a href="#string-tags" class="anchor"></a>String tags</h3><div class="odoc-spec"><div class="spec value anchored" id="val-pp_open_tag"><a href="#val-pp_open_tag" class="anchor"></a><code><span><span class="keyword">val</span> pp_open_tag : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span><a href="#type-tag">tag</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <a href="#val-pp_open_stag"><code>pp_open_stag</code></a>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-open_tag"><a href="#val-open_tag" class="anchor"></a><code><span><span class="keyword">val</span> open_tag : <span><a href="#type-tag">tag</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <a href="#val-open_stag"><code>open_stag</code></a>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_close_tag"><a href="#val-pp_close_tag" class="anchor"></a><code><span><span class="keyword">val</span> pp_close_tag : <span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span> <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <a href="#val-pp_close_stag"><code>pp_close_stag</code></a>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-close_tag"><a href="#val-close_tag" class="anchor"></a><code><span><span class="keyword">val</span> close_tag : <span>unit <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <a href="#val-close_stag"><code>close_stag</code></a>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-formatter_tag_functions"><a href="#type-formatter_tag_functions" class="anchor"></a><code><span><span class="keyword">type</span> formatter_tag_functions</span><span> = <a href="../../ocaml/Stdlib/Format/index.html#type-formatter_tag_functions">Stdlib.Format.formatter_tag_functions</a></span><span> = </span><span>{</span></code><ol><li id="type-formatter_tag_functions.mark_open_tag" class="def record field anchored"><a href="#type-formatter_tag_functions.mark_open_tag" class="anchor"></a><code><span>mark_open_tag : <span><a href="#type-tag">tag</a> <span class="arrow">-></span></span> string;</span></code></li><li id="type-formatter_tag_functions.mark_close_tag" class="def record field anchored"><a href="#type-formatter_tag_functions.mark_close_tag" class="anchor"></a><code><span>mark_close_tag : <span><a href="#type-tag">tag</a> <span class="arrow">-></span></span> string;</span></code></li><li id="type-formatter_tag_functions.print_open_tag" class="def record field anchored"><a href="#type-formatter_tag_functions.print_open_tag" class="anchor"></a><code><span>print_open_tag : <span><a href="#type-tag">tag</a> <span class="arrow">-></span></span> unit;</span></code></li><li id="type-formatter_tag_functions.print_close_tag" class="def record field anchored"><a href="#type-formatter_tag_functions.print_close_tag" class="anchor"></a><code><span>print_close_tag : <span><a href="#type-tag">tag</a> <span class="arrow">-></span></span> unit;</span></code></li></ol><code><span>}</span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <a href="#type-formatter_stag_functions"><code>formatter_stag_functions</code></a>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_set_formatter_tag_functions"><a href="#val-pp_set_formatter_tag_functions" class="anchor"></a><code><span><span class="keyword">val</span> pp_set_formatter_tag_functions :
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span><a href="#type-formatter_tag_functions">formatter_tag_functions</a> <span class="arrow">-></span></span>
|
||
unit</span></code></div><div class="spec-doc"><p>This function will erase non-string tag formatting functions.</p><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <a href="#val-pp_set_formatter_stag_functions"><code>pp_set_formatter_stag_functions</code></a>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_formatter_tag_functions"><a href="#val-set_formatter_tag_functions" class="anchor"></a><code><span><span class="keyword">val</span> set_formatter_tag_functions : <span><a href="#type-formatter_tag_functions">formatter_tag_functions</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <a href="#val-set_formatter_stag_functions"><code>set_formatter_stag_functions</code></a>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp_get_formatter_tag_functions"><a href="#val-pp_get_formatter_tag_functions" class="anchor"></a><code><span><span class="keyword">val</span> pp_get_formatter_tag_functions :
|
||
<span><a href="#type-formatter">formatter</a> <span class="arrow">-></span></span>
|
||
<span>unit <span class="arrow">-></span></span>
|
||
<a href="#type-formatter_tag_functions">formatter_tag_functions</a></span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <a href="#val-pp_get_formatter_stag_functions"><code>pp_get_formatter_stag_functions</code></a>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_formatter_tag_functions"><a href="#val-get_formatter_tag_functions" class="anchor"></a><code><span><span class="keyword">val</span> get_formatter_tag_functions : <span>unit <span class="arrow">-></span></span> <a href="#type-formatter_tag_functions">formatter_tag_functions</a></span></code></div><div class="spec-doc"><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> <p>Subsumed by <a href="#val-get_formatter_stag_functions"><code>get_formatter_stag_functions</code></a>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-t"><a href="#type-t" class="anchor"></a><code><span><span class="keyword">type</span> t</span><span> = <a href="../../ocaml/Stdlib/Format/index.html#type-formatter">Stdlib.Format.formatter</a></span></code></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-printer"><a href="#type-printer" class="anchor"></a><code><span><span class="keyword">type</span> <span>-'a printer</span></span><span> = <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> unit</span></code></div></div><h3 id="combinators"><a href="#combinators" class="anchor"></a>Combinators</h3><div class="odoc-spec"><div class="spec value anchored" id="val-silent"><a href="#val-silent" class="anchor"></a><code><span><span class="keyword">val</span> silent : <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Prints nothing.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-unit"><a href="#val-unit" class="anchor"></a><code><span><span class="keyword">val</span> unit : <span>unit <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Prints "()".</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-int"><a href="#val-int" class="anchor"></a><code><span><span class="keyword">val</span> int : <span>int <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-string"><a href="#val-string" class="anchor"></a><code><span><span class="keyword">val</span> string : <span>string <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-bool"><a href="#val-bool" class="anchor"></a><code><span><span class="keyword">val</span> bool : <span>bool <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-float3"><a href="#val-float3" class="anchor"></a><code><span><span class="keyword">val</span> float3 : <span>float <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-float"><a href="#val-float" class="anchor"></a><code><span><span class="keyword">val</span> float : <span>float <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-exn"><a href="#val-exn" class="anchor"></a><code><span><span class="keyword">val</span> exn : <span>exn <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Printer using <code>Printexc</code>.to_string.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-space"><a href="#val-space" class="anchor"></a><code><span><span class="keyword">val</span> space : <span>unit <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Alias to <a href="#val-pp_print_space"><code>pp_print_space</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-cut"><a href="#val-cut" class="anchor"></a><code><span><span class="keyword">val</span> cut : <span>unit <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Alias to <a href="#val-pp_print_cut"><code>pp_print_cut</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-break"><a href="#val-break" class="anchor"></a><code><span><span class="keyword">val</span> break : <span><span>(int * int)</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Tuple-ized <a href="#type-printer"><code>printer</code></a> form of <a href="#val-pp_print_break"><code>pp_print_break</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-newline"><a href="#val-newline" class="anchor"></a><code><span><span class="keyword">val</span> newline : <span>unit <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Force newline (see <code>Format</code>.pp_force_newline).</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-substring"><a href="#val-substring" class="anchor"></a><code><span><span class="keyword">val</span> substring : <span><span>(string * int * int)</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>substring (s,i,len)</code> prints the substring <code>(s,i,len)</code>, where <code>i</code> is the offset in <code>s</code> and <code>len</code> the number of bytes in the substring.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if the triple <code>(s,i,len)</code> does not describe a proper substring.</p></li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-text"><a href="#val-text" class="anchor"></a><code><span><span class="keyword">val</span> text : <span>string <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Print string, but replacing spaces with breaks and newlines with <a href="#val-newline"><code>newline</code></a>. See <code>pp_print_text</code> on recent versions of OCaml.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-string_lines"><a href="#val-string_lines" class="anchor"></a><code><span><span class="keyword">val</span> string_lines : <span>string <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>string_lines out s</code> prints <code>s</code> with all newlines (<code>'\n'</code>) replaced by a cut, in a vertical box. It does <b>NOT</b> insert breakable spaces in place of spaces, unlike <a href="#val-text"><code>text</code></a>. This means an already formatted string can be displayed inside another formatter without mangling the indentation.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.3</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-char"><a href="#val-char" class="anchor"></a><code><span><span class="keyword">val</span> char : <span>char <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.14</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-int32"><a href="#val-int32" class="anchor"></a><code><span><span class="keyword">val</span> int32 : <span>int32 <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.14</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-int64"><a href="#val-int64" class="anchor"></a><code><span><span class="keyword">val</span> int64 : <span>int64 <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.14</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-nativeint"><a href="#val-nativeint" class="anchor"></a><code><span><span class="keyword">val</span> nativeint : <span>nativeint <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.14</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-flush"><a href="#val-flush" class="anchor"></a><code><span><span class="keyword">val</span> flush : <span>unit <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Alias to <code>Format</code>.pp_print_flush.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-string_quoted"><a href="#val-string_quoted" class="anchor"></a><code><span><span class="keyword">val</span> string_quoted : <span>string <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Similar to <code>CCString</code>.print.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.14</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-list"><a href="#val-list" class="anchor"></a><code><span><span class="keyword">val</span> list : <span>?sep:<span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> list</span> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-array"><a href="#val-array" class="anchor"></a><code><span><span class="keyword">val</span> array : <span>?sep:<span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> array</span> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-arrayi"><a href="#val-arrayi" class="anchor"></a><code><span><span class="keyword">val</span> arrayi : <span>?sep:<span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span>(int * <span class="type-var">'a</span>)</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> array</span> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-seq"><a href="#val-seq" class="anchor"></a><code><span><span class="keyword">val</span> seq : <span>?sep:<span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="../../ocaml/Stdlib/Seq/index.html#type-t">Stdlib.Seq.t</a></span> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-iter"><a href="#val-iter" class="anchor"></a><code><span><span class="keyword">val</span> iter : <span>?sep:<span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-iter">iter</a></span> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-opt"><a href="#val-opt" class="anchor"></a><code><span><span class="keyword">val</span> opt : <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> option</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>opt pp</code> prints options as follows:</p><ul><li><code>Some x</code> will become "some foo" if <code>pp x ---> "foo"</code>.</li><li><code>None</code> will become "none".</li></ul></div></div><p>In the tuple printers, the <code>sep</code> argument is only available.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.17</li></ul><div class="odoc-spec"><div class="spec value anchored" id="val-pair"><a href="#val-pair" class="anchor"></a><code><span><span class="keyword">val</span> pair : <span>?sep:<span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'b</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span>(<span class="type-var">'a</span> * <span class="type-var">'b</span>)</span> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-triple"><a href="#val-triple" class="anchor"></a><code><span><span class="keyword">val</span> triple :
|
||
<span>?sep:<span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span>
|
||
<span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span>
|
||
<span><span><span class="type-var">'b</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span>
|
||
<span><span><span class="type-var">'c</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span>
|
||
<span><span>(<span class="type-var">'a</span> * <span class="type-var">'b</span> * <span class="type-var">'c</span>)</span> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-quad"><a href="#val-quad" class="anchor"></a><code><span><span class="keyword">val</span> quad :
|
||
<span>?sep:<span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span>
|
||
<span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span>
|
||
<span><span><span class="type-var">'b</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span>
|
||
<span><span><span class="type-var">'c</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span>
|
||
<span><span><span class="type-var">'d</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span>
|
||
<span><span>(<span class="type-var">'a</span> * <span class="type-var">'b</span> * <span class="type-var">'c</span> * <span class="type-var">'d</span>)</span> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-append"><a href="#val-append" class="anchor"></a><code><span><span class="keyword">val</span> append : <span><span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span>unit <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>append ppa ppb</code> first prints <code>ppa ()</code>, then prints <code>ppb ()</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-append_l"><a href="#val-append_l" class="anchor"></a><code><span><span class="keyword">val</span> append_l : <span><span><span>unit <a href="#type-printer">printer</a></span> list</span> <span class="arrow">-></span></span> <span>unit <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>append_l pps</code> runs the printers in <code>pps</code> sequentially.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-within"><a href="#val-within" class="anchor"></a><code><span><span class="keyword">val</span> within : <span>string <span class="arrow">-></span></span> <span>string <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>within a b p</code> wraps <code>p</code> inside the strings <code>a</code> and <code>b</code>. Convenient, for instances, for brackets, parenthesis, quotes, etc.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.17</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-map"><a href="#val-map" class="anchor"></a><code><span><span class="keyword">val</span> map : <span><span>(<span><span class="type-var">'a</span> <span class="arrow">-></span></span> <span class="type-var">'b</span>)</span> <span class="arrow">-></span></span> <span><span><span class="type-var">'b</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-vbox"><a href="#val-vbox" class="anchor"></a><code><span><span class="keyword">val</span> vbox : <span>?i:int <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Wrap the printer in a vertical box.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">i</span> <p>level of indentation within the box (default 0).</p></li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.16</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-hvbox"><a href="#val-hvbox" class="anchor"></a><code><span><span class="keyword">val</span> hvbox : <span>?i:int <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Wrap the printer in a horizontal/vertical box.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">i</span> <p>level of indentation within the box (default 0).</p></li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.16</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-hovbox"><a href="#val-hovbox" class="anchor"></a><code><span><span class="keyword">val</span> hovbox : <span>?i:int <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Wrap the printer in a horizontal or vertical box.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">i</span> <p>level of indentation within the box (default 0).</p></li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.16</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-hbox"><a href="#val-hbox" class="anchor"></a><code><span><span class="keyword">val</span> hbox : <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Wrap the printer in an horizontal box.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.16</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-return"><a href="#val-return" class="anchor"></a><code><span><span class="keyword">val</span> return : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">_</span>, <span class="type-var">_</span>, <span class="type-var">'a</span>)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span> <span>unit <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>return "some_format_string"</code> takes a argument-less format string and returns a printer actionable by <code>()</code>. Examples:</p><ul><li><code>return ",@ "</code></li><li><code>return "@{<Red>and then@}@,"</code></li><li><code>return "@[<v>a@ b@]"</code></li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_to_string"><a href="#val-of_to_string" class="anchor"></a><code><span><span class="keyword">val</span> of_to_string : <span><span>(<span><span class="type-var">'a</span> <span class="arrow">-></span></span> string)</span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>of_to_string f</code> converts its input to a string using <code>f</code>, then prints the string.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-const"><a href="#val-const" class="anchor"></a><code><span><span class="keyword">val</span> const : <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> <span>unit <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>const pp x</code> is a unit printer that uses <code>pp</code> on <code>x</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-some"><a href="#val-some" class="anchor"></a><code><span><span class="keyword">val</span> some : <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> option</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>some pp</code> will print options as follows:</p><ul><li><code>Some x</code> is printed using <code>pp</code> on <code>x</code></li><li><code>None</code> is not printed at all</li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-const_string"><a href="#val-const_string" class="anchor"></a><code><span><span class="keyword">val</span> const_string : <span>string <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>const_string s</code> is a printer that ignores its input and always prints <code>s</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.5</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-opaque"><a href="#val-opaque" class="anchor"></a><code><span><span class="keyword">val</span> opaque : <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>opaque</code> is <code>const_string "opaque"</code>. The exact string used is not stable.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.5</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-lazy_force"><a href="#val-lazy_force" class="anchor"></a><code><span><span class="keyword">val</span> lazy_force : <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> lazy_t</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>lazy_force pp out x</code> forces <code>x</code> and prints the result with <code>pp</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 2.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-lazy_or"><a href="#val-lazy_or" class="anchor"></a><code><span><span class="keyword">val</span> lazy_or : <span>?default:<span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> lazy_t</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>lazy_or ?default pp out x</code> prints <code>default</code> if <code>x</code> is not evaluated yet, or uses <code>pp</code> otherwise.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 2.0</li></ul></div></div><h3 id="ansi-codes"><a href="#ansi-codes" class="anchor"></a>ANSI codes</h3><p>Use ANSI escape codes https://en.wikipedia.org/wiki/ANSI_escape_code to put some colors on the terminal.</p><p>This uses <b>tags</b> in format strings to specify the style. Current styles are the following:</p><ul><li>"reset" resets style</li><li>"black"</li><li>"red"</li><li>"green"</li><li>"yellow"</li><li>"blue"</li><li>"magenta"</li><li>"cyan"</li><li>"white"</li><li>"bold" bold font</li><li>"Black" bold black</li><li>"Red" bold red</li><li>"Green" bold green</li><li>"Yellow" bold yellow</li><li>"Blue" bold blue</li><li>"Magenta" bold magenta</li><li>"Cyan" bold cyan</li><li>"White" bold white</li></ul><p>Example:</p><pre class="language-ocaml"><code>set_color_default true;;
|
||
|
||
Format.printf
|
||
"what is your @{<White>favorite color@}? @{<blue>blue@}! No, @{<red>red@}! Ahhhhhhh@.";;</code></pre><p><b>status: unstable</b></p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.15</li></ul><div class="odoc-spec"><div class="spec value anchored" id="val-set_color_tag_handling"><a href="#val-set_color_tag_handling" class="anchor"></a><code><span><span class="keyword">val</span> set_color_tag_handling : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Add functions to support color tags to the given formatter.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.15</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_color_default"><a href="#val-set_color_default" class="anchor"></a><code><span><span class="keyword">val</span> set_color_default : <span>bool <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>set_color_default b</code> enables color handling on the standard formatters (stdout, stderr) if <code>b = true</code> as well as on <a href="#val-sprintf"><code>sprintf</code></a> formatters; it disables the color handling if <code>b = false</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_color"><a href="#val-with_color" class="anchor"></a><code><span><span class="keyword">val</span> with_color : <span>string <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>with_color "Blue" pp</code> behaves like the printer <code>pp</code>, but with the given style.</p><p><b>status: unstable</b></p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.16</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_colorf"><a href="#val-with_colorf" class="anchor"></a><code><span><span class="keyword">val</span> with_colorf : <span>string <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><span><span>(<span class="type-var">'a</span>, <a href="#type-t">t</a>, unit, unit)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>with_colorf "Blue" out "%s %d" "yolo" 42</code> will behave like <code>Format</code>.fprintf, but wrapping the content with the given style.</p><p><b>status: unstable</b></p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.16</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_color_sf"><a href="#val-with_color_sf" class="anchor"></a><code><span><span class="keyword">val</span> with_color_sf : <span>string <span class="arrow">-></span></span> <span><span><span>(<span class="type-var">'a</span>, <a href="#type-t">t</a>, unit, string)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>with_color_sf "Blue" out "%s %d" "yolo" 42</code> will behave like <a href="#val-sprintf"><code>sprintf</code></a>, but wrapping the content with the given style.</p><p>Example:</p><pre class="language-ocaml"><code>CCFormat.with_color_sf "red" "%a" CCFormat.Dump.(list int) [1;2;3] |> print_endline;;</code></pre><p><b>status: unstable</b></p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.21</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_color_ksf"><a href="#val-with_color_ksf" class="anchor"></a><code><span><span class="keyword">val</span> with_color_ksf :
|
||
<span>f:<span>(<span>string <span class="arrow">-></span></span> <span class="type-var">'b</span>)</span> <span class="arrow">-></span></span>
|
||
<span>string <span class="arrow">-></span></span>
|
||
<span><span><span>(<span class="type-var">'a</span>, <a href="#type-t">t</a>, unit, <span class="type-var">'b</span>)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span>
|
||
<span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>with_color_ksf "Blue" ~f "%s %d" "yolo" 42</code> will behave like <a href="#val-ksprintf"><code>ksprintf</code></a>, but wrapping the content with the given style.</p><p>Example: the following with raise <code>Failure</code> with a colored message</p><pre class="language-ocaml"><code>CCFormat.with_color_ksf "red" ~f:failwith "%a" CCFormat.Dump.(list int) [1;2;3];;</code></pre><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-ANSI_codes"><a href="#module-ANSI_codes" class="anchor"></a><code><span><span class="keyword">module</span> <a href="ANSI_codes/index.html">ANSI_codes</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div><div class="spec-doc"><p>ANSI escape codes. This contains lower level functions for them.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-styling"><a href="#val-styling" class="anchor"></a><code><span><span class="keyword">val</span> styling : <span><span><a href="ANSI_codes/index.html#type-style">ANSI_codes.style</a> list</span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p><code>styling st p</code> is the same printer as <code>p</code>, except it locally sets the style <code>st</code>.</p><p>Example:</p><pre class="language-ocaml"><code>open CCFormat;
|
||
set_color_default true;
|
||
sprintf
|
||
"what is your %a? %a! No, %a! Ahhhhhhh@."
|
||
(styling [`FG `White; `Bold] string) "favorite color"
|
||
(styling [`FG `Blue] string) "blue"
|
||
(styling [`FG `Red] string) "red"</code></pre><p>Available only on OCaml >= 4.08.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.7</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_styling"><a href="#val-with_styling" class="anchor"></a><code><span><span class="keyword">val</span> with_styling : <span><span><a href="ANSI_codes/index.html#type-style">ANSI_codes.style</a> list</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><span>(<span>unit <span class="arrow">-></span></span> <span class="type-var">'a</span>)</span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>with_styling style fmt f</code> sets the given style on <code>fmt</code>, calls <code>f()</code>, then restores the previous style. It is useful in imperative-style printers (a sequence of "print a; print b; …").</p><p>Available only on OCaml >= 4.08.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.7</li></ul></div></div><h3 id="io"><a href="#io" class="anchor"></a>IO</h3><div class="odoc-spec"><div class="spec value anchored" id="val-output"><a href="#val-output" class="anchor"></a><code><span><span class="keyword">val</span> output : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-to_string"><a href="#val-to_string" class="anchor"></a><code><span><span class="keyword">val</span> to_string : <span><span><span class="type-var">'a</span> <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> string</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_chan"><a href="#val-of_chan" class="anchor"></a><code><span><span class="keyword">val</span> of_chan : <span><a href="../../ocaml/Stdlib/index.html#type-out_channel">out_channel</a> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Alias to <code>Format</code>.formatter_of_out_channel.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_out_chan"><a href="#val-with_out_chan" class="anchor"></a><code><span><span class="keyword">val</span> with_out_chan : <span><a href="../../ocaml/Stdlib/index.html#type-out_channel">out_channel</a> <span class="arrow">-></span></span> <span><span>(<span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span class="type-var">'a</span>)</span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>with_out_chan oc f</code> turns <code>oc</code> into a formatter <code>fmt</code>, and call <code>f fmt</code>. Behaves like <code>f fmt</code> from then on, but whether the call to <code>f</code> fails or returns, <code>fmt</code> is flushed before the call terminates.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-stdout"><a href="#val-stdout" class="anchor"></a><code><span><span class="keyword">val</span> stdout : <a href="#type-t">t</a></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-stderr"><a href="#val-stderr" class="anchor"></a><code><span><span class="keyword">val</span> stderr : <a href="#type-t">t</a></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-tee"><a href="#val-tee" class="anchor"></a><code><span><span class="keyword">val</span> tee : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>tee a b</code> makes a new formatter that writes in both <code>a</code> and <code>b</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-sprintf"><a href="#val-sprintf" class="anchor"></a><code><span><span class="keyword">val</span> sprintf : <span><span><span>(<span class="type-var">'a</span>, <a href="#type-t">t</a>, unit, string)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Print into a string any format string that would usually be compatible with <a href="#val-fprintf"><code>fprintf</code></a>. Like <code>Format</code>.asprintf.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-sprintf_no_color"><a href="#val-sprintf_no_color" class="anchor"></a><code><span><span class="keyword">val</span> sprintf_no_color : <span><span><span>(<span class="type-var">'a</span>, <a href="#type-t">t</a>, unit, string)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Like <a href="#val-sprintf"><code>sprintf</code></a> but never prints colors.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.16</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-sprintf_dyn_color"><a href="#val-sprintf_dyn_color" class="anchor"></a><code><span><span class="keyword">val</span> sprintf_dyn_color : <span>colors:bool <span class="arrow">-></span></span> <span><span><span>(<span class="type-var">'a</span>, <a href="#type-t">t</a>, unit, string)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Like <a href="#val-sprintf"><code>sprintf</code></a> but enable/disable colors depending on <code>colors</code>.</p><p>Example:</p><pre class="language-ocaml"><code>(* with colors *)
|
||
CCFormat.sprintf_dyn_color ~colors:true "@{<Red>%a@}"
|
||
CCFormat.Dump.(list int) [1;2;3] |> print_endline;;
|
||
|
||
(* without colors *)
|
||
CCFormat.sprintf_dyn_color ~colors:false "@{<Red>%a@}"
|
||
CCFormat.Dump.(list int) [1;2;3] |> print_endline;;</code></pre><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.21</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-fprintf"><a href="#val-fprintf" class="anchor"></a><code><span><span class="keyword">val</span> fprintf : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><span><span>(<span class="type-var">'a</span>, <a href="#type-t">t</a>, unit)</span> <a href="../../ocaml/Stdlib/index.html#type-format">format</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Alias to <code>Format</code>.fprintf.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.14</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-fprintf_dyn_color"><a href="#val-fprintf_dyn_color" class="anchor"></a><code><span><span class="keyword">val</span> fprintf_dyn_color : <span>colors:bool <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><span><span>(<span class="type-var">'a</span>, <a href="#type-t">t</a>, unit)</span> <a href="../../ocaml/Stdlib/index.html#type-format">format</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Like <a href="#val-fprintf"><code>fprintf</code></a> but enable/disable colors depending on <code>colors</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.21</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-ksprintf"><a href="#val-ksprintf" class="anchor"></a><code><span><span class="keyword">val</span> ksprintf :
|
||
<span>?margin:int <span class="arrow">-></span></span>
|
||
<span>f:<span>(<span>string <span class="arrow">-></span></span> <span class="type-var">'b</span>)</span> <span class="arrow">-></span></span>
|
||
<span><span><span>(<span class="type-var">'a</span>, <a href="../../ocaml/Stdlib/Format/index.html#type-formatter">Stdlib.Format.formatter</a>, unit, <span class="type-var">'b</span>)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span>
|
||
<span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>ksprintf fmt ~f</code> formats using <code>fmt</code>, in a way similar to <a href="#val-sprintf"><code>sprintf</code></a>, and then calls <code>f</code> on the resulting string.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">margin</span> <p>set margin (since 2.1)</p></li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.14</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-to_file"><a href="#val-to_file" class="anchor"></a><code><span><span class="keyword">val</span> to_file : <span>string <span class="arrow">-></span></span> <span><span><span>(<span class="type-var">'a</span>, <a href="#type-t">t</a>, unit, unit)</span> <a href="../../ocaml/Stdlib/index.html#type-format4">format4</a></span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Print to the given file.</p></div></div><h3 id="dump"><a href="#dump" class="anchor"></a>Dump</h3><p>Print structures as OCaml values, so that they can be parsed back by OCaml (typically, in the toplevel, for debugging).</p><p>Example:</p><pre class="language-ocaml"><code>Format.printf "%a@." CCFormat.Dump.(list int) CCList.(1 -- 200);;
|
||
|
||
Format.printf "%a@." CCFormat.Dump.(array (list (pair int bool)))
|
||
[| [1, true; 2, false]; []; [42, false] |];;</code></pre><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.21</li></ul><div class="odoc-spec"><div class="spec module anchored" id="module-Dump"><a href="#module-Dump" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Dump/index.html">Dump</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Infix"><a href="#module-Infix" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Infix/index.html">Infix</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-include"><details open="open"><summary class="spec include"><code><span><span class="keyword">include</span> <span class="keyword">module</span> <span class="keyword">type</span> <span class="keyword">of</span> <a href="Infix/index.html">Infix</a></span></code></summary><div class="odoc-spec"><div class="spec value anchored" id="val-(++)"><a href="#val-(++)" class="anchor"></a><code><span><span class="keyword">val</span> (++) : <span><span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span><span>unit <a href="#type-printer">printer</a></span> <span class="arrow">-></span></span> <span>unit <a href="#type-printer">printer</a></span></span></code></div><div class="spec-doc"><p>Alias to <a href="#val-append"><code>append</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.2</li></ul></div></div></details></div></div></body></html> |