1Format(3) OCaml library Format(3)
2
3
4
6 Format - Pretty-printing.
7
9 Module Format
10
12 Module Format
13 : sig end
14
15
16 Pretty-printing.
17
18 This module implements a pretty-printing facility to format values
19 within Format.boxes and Format.tags combined with a set of Format.fpp .
20 The pretty-printer splits lines at specified Format.breaks , and in‐
21 dents lines according to the box structure. Similarly, Format.tags can
22 be used to decouple text presentation from its contents.
23
24 This pretty-printing facility is implemented as an overlay on top of
25 abstract Format.formatter which provide basic output functions. Some
26 formatters are predefined, notably:
27
28 - Format.std_formatter outputs to stdout
29
30
31 - Format.err_formatter outputs to stderr
32
33 Most functions in the Format module come in two variants: a short ver‐
34 sion that operates on the current domain's standard formatter as ob‐
35 tained using Format.get_std_formatter and the generic version prefixed
36 by pp_ that takes a formatter as its first argument. For the version
37 that operates on the current domain's standard formatter, the call to
38 Format.get_std_formatter is delayed until the last argument is re‐
39 ceived.
40
41 More formatters can be created with Format.formatter_of_out_channel ,
42 Format.formatter_of_buffer , Format.formatter_of_symbolic_output_buffer
43 or using Format.formatter .
44
45 Warning: Since Format.formatter contain mutable state, it is not
46 thread-safe to use the same formatter on multiple domains in parallel
47 without synchronization.
48
49 If multiple domains write to the same output channel using the prede‐
50 fined formatters (as obtained by Format.get_std_formatter or For‐
51 mat.get_err_formatter ), the output from the domains will be inter‐
52 leaved with each other at points where the formatters are flushed, such
53 as with Format.print_flush . This synchronization is not performed by
54 formatters obtained from Format.formatter_of_out_channel (on the stan‐
55 dard out channels or others).
56
57
58
59
60
61
62
63 Introduction
64 You may consider this module as providing an extension to the printf
65 facility to provide automatic line splitting. The addition of
66 pretty-printing annotations to your regular printf format strings gives
67 you fancy indentation and line breaks. Pretty-printing annotations are
68 described below in the documentation of the function Format.fprintf .
69
70 You may also use the explicit pretty-printing box management and print‐
71 ing functions provided by this module. This style is more basic but
72 more verbose than the concise fprintf format strings.
73
74 For instance, the sequence open_box 0; print_string "x ="; print_space
75 ();
76 print_int 1; close_box (); print_newline () that prints x = 1
77 within a pretty-printing box, can be abbreviated as printf "@[%s@
78 %i@]@." "x =" 1 , or even shorter printf "@[x =@ %i@]@." 1 .
79
80 Rule of thumb for casual users of this library:
81
82 -use simple pretty-printing boxes (as obtained by open_box 0 );
83
84 -use simple break hints as obtained by print_cut () that outputs a sim‐
85 ple break hint, or by print_space () that outputs a space indicating a
86 break hint;
87
88 -once a pretty-printing box is open, display its material with basic
89 printing functions (e. g. print_int and print_string );
90
91 -when the material for a pretty-printing box has been printed, call
92 close_box () to close the box;
93
94 -at the end of pretty-printing, flush the pretty-printer to display all
95 the remaining material, e.g. evaluate print_newline () .
96
97 The behavior of pretty-printing commands is unspecified if there is no
98 open pretty-printing box. Each box opened by one of the open_ functions
99 below must be closed using close_box for proper formatting. Otherwise,
100 some of the material printed in the boxes may not be output, or may be
101 formatted incorrectly.
102
103 In case of interactive use, each phrase is executed in the initial
104 state of the standard pretty-printer: after each phrase execution, the
105 interactive system closes all open pretty-printing boxes, flushes all
106 pending text, and resets the standard pretty-printer.
107
108 Warning: mixing calls to pretty-printing functions of this module with
109 calls to Stdlib low level output functions is error prone.
110
111 The pretty-printing functions output material that is delayed in the
112 pretty-printer queue and stacks in order to compute proper line split‐
113 ting. In contrast, basic I/O output functions write directly in their
114 output device. As a consequence, the output of a basic I/O function may
115 appear before the output of a pretty-printing function that has been
116 called before. For instance,
117 Stdlib.print_string "<";
118 Format.print_string "PRETTY";
119 Stdlib.print_string ">";
120 Format.print_string "TEXT";
121 leads to output <>PRETTYTEXT .
122
123 Formatters
124 type formatter
125
126
127 Abstract data corresponding to a pretty-printer (also called a format‐
128 ter) and all its machinery. See also Format.formatter .
129
130
131
132
133 Pretty-printing boxes
134 The pretty-printing engine uses the concepts of pretty-printing box and
135 break hint to drive indentation and line splitting behavior of the
136 pretty-printer.
137
138 Each different pretty-printing box kind introduces a specific line
139 splitting policy:
140
141
142 -within an horizontal box, break hints never split the line (but the
143 line may be split in a box nested deeper),
144
145 -within a vertical box, break hints always split the line,
146
147 -within an horizontal/vertical box, if the box fits on the current line
148 then break hints never split the line, otherwise break hint always
149 split the line,
150
151 -within a compacting box, a break hint never splits the line, unless
152 there is no more room on the current line.
153
154 Note that line splitting policy is box specific: the policy of a box
155 does not rule the policy of inner boxes. For instance, if a vertical
156 box is nested in an horizontal box, all break hints within the vertical
157 box will split the line.
158
159 Moreover, opening a box after the Format.maxindent splits the line
160 whether or not the box would end up fitting on the line.
161
162 val pp_open_box : formatter -> int -> unit
163
164
165
166
167 val open_box : int -> unit
168
169
170 pp_open_box ppf d opens a new compacting pretty-printing box with off‐
171 set d in the formatter ppf .
172
173 Within this box, the pretty-printer prints as much as possible material
174 on every line.
175
176 A break hint splits the line if there is no more room on the line to
177 print the remainder of the box.
178
179 Within this box, the pretty-printer emphasizes the box structure: if a
180 structural box does not fit fully on a simple line, a break hint also
181 splits the line if the splitting ``moves to the left'' (i.e. the new
182 line gets an indentation smaller than the one of the current line).
183
184 This box is the general purpose pretty-printing box.
185
186 If the pretty-printer splits the line in the box, offset d is added to
187 the current indentation.
188
189
190
191 val pp_close_box : formatter -> unit -> unit
192
193
194
195
196 val close_box : unit -> unit
197
198 Closes the most recently open pretty-printing box.
199
200
201
202 val pp_open_hbox : formatter -> unit -> unit
203
204
205
206
207 val open_hbox : unit -> unit
208
209
210 pp_open_hbox ppf () opens a new 'horizontal' pretty-printing box.
211
212 This box prints material on a single line.
213
214 Break hints in a horizontal box never split the line. (Line splitting
215 may still occur inside boxes nested deeper).
216
217
218
219 val pp_open_vbox : formatter -> int -> unit
220
221
222
223
224 val open_vbox : int -> unit
225
226
227 pp_open_vbox ppf d opens a new 'vertical' pretty-printing box with off‐
228 set d .
229
230 This box prints material on as many lines as break hints in the box.
231
232 Every break hint in a vertical box splits the line.
233
234 If the pretty-printer splits the line in the box, d is added to the
235 current indentation.
236
237
238
239 val pp_open_hvbox : formatter -> int -> unit
240
241
242
243
244 val open_hvbox : int -> unit
245
246
247 pp_open_hvbox ppf d opens a new 'horizontal/vertical' pretty-printing
248 box with offset d .
249
250 This box behaves as an horizontal box if it fits on a single line, oth‐
251 erwise it behaves as a vertical box.
252
253 If the pretty-printer splits the line in the box, d is added to the
254 current indentation.
255
256
257
258 val pp_open_hovbox : formatter -> int -> unit
259
260
261
262
263 val open_hovbox : int -> unit
264
265
266 pp_open_hovbox ppf d opens a new 'horizontal-or-vertical' pretty-print‐
267 ing box with offset d .
268
269 This box prints material as much as possible on every line.
270
271 A break hint splits the line if there is no more room on the line to
272 print the remainder of the box.
273
274 If the pretty-printer splits the line in the box, d is added to the
275 current indentation.
276
277
278
279
280 Formatting functions
281 val pp_print_string : formatter -> string -> unit
282
283
284
285
286 val print_string : string -> unit
287
288
289 pp_print_string ppf s prints s in the current pretty-printing box.
290
291
292
293 val pp_print_bytes : formatter -> bytes -> unit
294
295
296
297
298 val print_bytes : bytes -> unit
299
300
301 pp_print_bytes ppf b prints b in the current pretty-printing box.
302
303
304 Since 4.13.0
305
306
307
308 val pp_print_as : formatter -> int -> string -> unit
309
310
311
312
313 val print_as : int -> string -> unit
314
315
316 pp_print_as ppf len s prints s in the current pretty-printing box. The
317 pretty-printer formats s as if it were of length len .
318
319
320
321 val pp_print_int : formatter -> int -> unit
322
323
324
325
326 val print_int : int -> unit
327
328 Print an integer in the current pretty-printing box.
329
330
331
332 val pp_print_float : formatter -> float -> unit
333
334
335
336
337 val print_float : float -> unit
338
339 Print a floating point number in the current pretty-printing box.
340
341
342
343 val pp_print_char : formatter -> char -> unit
344
345
346
347
348 val print_char : char -> unit
349
350 Print a character in the current pretty-printing box.
351
352
353
354 val pp_print_bool : formatter -> bool -> unit
355
356
357
358
359 val print_bool : bool -> unit
360
361 Print a boolean in the current pretty-printing box.
362
363
364
365
366 Break hints
367 A 'break hint' tells the pretty-printer to output some space or split
368 the line whichever way is more appropriate to the current pretty-print‐
369 ing box splitting rules.
370
371 Break hints are used to separate printing items and are mandatory to
372 let the pretty-printer correctly split lines and indent items.
373
374 Simple break hints are:
375
376 -the 'space': output a space or split the line if appropriate,
377
378 -the 'cut': split the line if appropriate.
379
380 Note: the notions of space and line splitting are abstract for the
381 pretty-printing engine, since those notions can be completely redefined
382 by the programmer. However, in the pretty-printer default setting,
383 ``output a space'' simply means printing a space character (ASCII code
384 32) and ``split the line'' means printing a newline character (ASCII
385 code 10).
386
387 val pp_print_space : formatter -> unit -> unit
388
389
390
391
392 val print_space : unit -> unit
393
394
395 pp_print_space ppf () emits a 'space' break hint: the pretty-printer
396 may split the line at this point, otherwise it prints one space.
397
398
399 pp_print_space ppf () is equivalent to pp_print_break ppf 1 0 .
400
401
402
403 val pp_print_cut : formatter -> unit -> unit
404
405
406
407
408 val print_cut : unit -> unit
409
410
411 pp_print_cut ppf () emits a 'cut' break hint: the pretty-printer may
412 split the line at this point, otherwise it prints nothing.
413
414
415 pp_print_cut ppf () is equivalent to pp_print_break ppf 0 0 .
416
417
418
419 val pp_print_break : formatter -> int -> int -> unit
420
421
422
423
424 val print_break : int -> int -> unit
425
426
427 pp_print_break ppf nspaces offset emits a 'full' break hint: the
428 pretty-printer may split the line at this point, otherwise it prints
429 nspaces spaces.
430
431 If the pretty-printer splits the line, offset is added to the current
432 indentation.
433
434
435
436 val pp_print_custom_break : formatter -> fits:string * int * string ->
437 breaks:string * int * string -> unit
438
439
440 pp_print_custom_break ppf ~fits:(s1, n, s2) ~breaks:(s3, m, s4) emits a
441 custom break hint: the pretty-printer may split the line at this point.
442
443 If it does not split the line, then the s1 is emitted, then n spaces,
444 then s2 .
445
446 If it splits the line, then it emits the s3 string, then an indent (ac‐
447 cording to the box rules), then an offset of m spaces, then the s4
448 string.
449
450 While n and m are handled by formatter_out_functions.out_indent , the
451 strings will be handled by formatter_out_functions.out_string . This
452 allows for a custom formatter that handles indentation distinctly, for
453 example, outputs <br/> tags or entities.
454
455 The custom break is useful if you want to change which visible
456 (non-whitespace) characters are printed in case of break or no break.
457 For example, when printing a list [a; b; c] , you might want to add a
458 trailing semicolon when it is printed vertically:
459
460
461 [
462 a;
463 b;
464 c;
465 ]
466
467
468 You can do this as follows:
469 printf "@[<v 0>[@;<0 2>@[<v 0>a;@,b;@,c@]%t]@]@\n"
470 (pp_print_custom_break ~fits:("", 0, "") ~breaks:(";", 0, ""))
471
472
473
474 Since 4.08.0
475
476
477
478 val pp_force_newline : formatter -> unit -> unit
479
480
481
482
483 val force_newline : unit -> unit
484
485 Force a new line in the current pretty-printing box.
486
487 The pretty-printer must split the line at this point,
488
489 Not the normal way of pretty-printing, since imperative line splitting
490 may interfere with current line counters and box size calculation. Us‐
491 ing break hints within an enclosing vertical box is a better alterna‐
492 tive.
493
494
495
496 val pp_print_if_newline : formatter -> unit -> unit
497
498
499
500
501 val print_if_newline : unit -> unit
502
503 Execute the next formatting command if the preceding line has just been
504 split. Otherwise, ignore the next formatting command.
505
506
507
508
509 Pretty-printing termination
510 val pp_print_flush : formatter -> unit -> unit
511
512
513
514
515 val print_flush : unit -> unit
516
517 End of pretty-printing: resets the pretty-printer to initial state.
518
519 All open pretty-printing boxes are closed, all pending text is printed.
520 In addition, the pretty-printer low level output device is flushed to
521 ensure that all pending text is really displayed.
522
523 Note: never use print_flush in the normal course of a pretty-printing
524 routine, since the pretty-printer uses a complex buffering machinery to
525 properly indent the output; manually flushing those buffers at random
526 would conflict with the pretty-printer strategy and result to poor ren‐
527 dering.
528
529 Only consider using print_flush when displaying all pending material is
530 mandatory (for instance in case of interactive use when you want the
531 user to read some text) and when resetting the pretty-printer state
532 will not disturb further pretty-printing.
533
534 Warning: If the output device of the pretty-printer is an output chan‐
535 nel, repeated calls to print_flush means repeated calls to flush to
536 flush the out channel; these explicit flush calls could foil the
537 buffering strategy of output channels and could dramatically impact ef‐
538 ficiency.
539
540
541
542 val pp_print_newline : formatter -> unit -> unit
543
544
545
546
547 val print_newline : unit -> unit
548
549 End of pretty-printing: resets the pretty-printer to initial state.
550
551 All open pretty-printing boxes are closed, all pending text is printed.
552
553 Equivalent to Format.print_flush followed by a new line. See corre‐
554 sponding words of caution for Format.print_flush .
555
556 Note: this is not the normal way to output a new line; the preferred
557 method is using break hints within a vertical pretty-printing box.
558
559
560
561
562 Margin
563 val pp_set_margin : formatter -> int -> unit
564
565
566
567
568 val set_margin : int -> unit
569
570
571 pp_set_margin ppf d sets the right margin to d (in characters): the
572 pretty-printer splits lines that overflow the right margin according to
573 the break hints given. Setting the margin to d means that the format‐
574 ting engine aims at printing at most d-1 characters per line. Nothing
575 happens if d is smaller than 2. If d is too large, the right margin is
576 set to the maximum admissible value (which is greater than 10 ^ 9 ).
577 If d is less than the current maximum indentation limit, the maximum
578 indentation limit is decreased while trying to preserve a minimal ratio
579 max_indent/margin>=50% and if possible the current difference margin -
580 max_indent .
581
582 See also Format.pp_set_geometry .
583
584
585
586 val pp_get_margin : formatter -> unit -> int
587
588
589
590
591 val get_margin : unit -> int
592
593 Returns the position of the right margin.
594
595
596
597
598 Maximum indentation limit
599 val pp_set_max_indent : formatter -> int -> unit
600
601
602
603
604 val set_max_indent : int -> unit
605
606
607 pp_set_max_indent ppf d sets the maximum indentation limit of lines to
608 d (in characters): once this limit is reached, new pretty-printing
609 boxes are rejected to the left, unless the enclosing box fully fits on
610 the current line. As an illustration,
611 set_margin 10; set_max_indent 5; printf "@[123456@[7@]89A@]@."
612 yields
613 123456
614 789A
615
616 because the nested box "@[7@]" is opened after the maximum indentation
617 limit ( 7>5 ) and its parent box does not fit on the current line. Ei‐
618 ther decreasing the length of the parent box to make it fit on a line:
619 printf "@[123456@[7@]89@]@."
620 or opening an intermediary box before the maximum indentation limit
621 which fits on the current line
622 printf "@[123@[456@[7@]89@]A@]@."
623 avoids the rejection to the left of the inner boxes and print respec‐
624 tively "123456789" and "123456789A" . Note also that vertical boxes
625 never fit on a line whereas horizontal boxes always fully fit on the
626 current line. Opening a box may split a line whereas the contents may
627 have fit. If this behavior is problematic, it can be curtailed by set‐
628 ting the maximum indentation limit to margin - 1 . Note that setting
629 the maximum indentation limit to margin is invalid.
630
631 Nothing happens if d is smaller than 2.
632
633 If d is too large, the limit is set to the maximum admissible value
634 (which is greater than 10 ^ 9 ).
635
636 If d is greater or equal than the current margin, it is ignored, and
637 the current maximum indentation limit is kept.
638
639 See also Format.pp_set_geometry .
640
641
642
643 val pp_get_max_indent : formatter -> unit -> int
644
645
646
647
648 val get_max_indent : unit -> int
649
650 Return the maximum indentation limit (in characters).
651
652
653
654
655 Geometry
656 Geometric functions can be used to manipulate simultaneously the cou‐
657 pled variables, margin and maxixum indentation limit.
658
659 type geometry = {
660 max_indent : int ;
661 margin : int ;
662 }
663
664
665 Since 4.08
666
667
668
669 val check_geometry : geometry -> bool
670
671 Check if the formatter geometry is valid: 1 < max_indent < margin
672
673
674
675 Since 4.08
676
677
678
679 val pp_set_geometry : formatter -> max_indent:int -> margin:int -> unit
680
681
682
683
684 val set_geometry : max_indent:int -> margin:int -> unit
685
686
687
688
689 val pp_safe_set_geometry : formatter -> max_indent:int -> margin:int ->
690 unit
691
692
693
694
695 val safe_set_geometry : max_indent:int -> margin:int -> unit
696
697
698 pp_set_geometry ppf ~max_indent ~margin sets both the margin and maxi‐
699 mum indentation limit for ppf .
700
701 When 1 < max_indent < margin , pp_set_geometry ppf ~max_indent ~margin
702 is equivalent to pp_set_margin ppf margin; pp_set_max_indent ppf
703 max_indent ; and avoids the subtly incorrect pp_set_max_indent ppf
704 max_indent; pp_set_margin ppf margin ;
705
706 Outside of this domain, pp_set_geometry raises an invalid argument ex‐
707 ception whereas pp_safe_set_geometry does nothing.
708
709
710 Since 4.08.0
711
712
713
714 val pp_update_geometry : formatter -> (geometry -> geometry) -> unit
715
716
717 pp_update_geometry ppf (fun geo -> { geo with ... }) lets you update a
718 formatter's geometry in a way that is robust to extension of the geome‐
719 try record with new fields.
720
721 Raises an invalid argument exception if the returned geometry does not
722 satisfy Format.check_geometry .
723
724
725 Since 4.11.0
726
727
728
729 val update_geometry : (geometry -> geometry) -> unit
730
731
732
733
734 val pp_get_geometry : formatter -> unit -> geometry
735
736
737
738
739 val get_geometry : unit -> geometry
740
741 Return the current geometry of the formatter
742
743
744 Since 4.08.0
745
746
747
748
749 Maximum formatting depth
750 The maximum formatting depth is the maximum number of pretty-printing
751 boxes simultaneously open.
752
753 Material inside boxes nested deeper is printed as an ellipsis (more
754 precisely as the text returned by Format.get_ellipsis_text () ).
755
756 val pp_set_max_boxes : formatter -> int -> unit
757
758
759
760
761 val set_max_boxes : int -> unit
762
763
764 pp_set_max_boxes ppf max sets the maximum number of pretty-printing
765 boxes simultaneously open.
766
767 Material inside boxes nested deeper is printed as an ellipsis (more
768 precisely as the text returned by Format.get_ellipsis_text () ).
769
770 Nothing happens if max is smaller than 2.
771
772
773
774 val pp_get_max_boxes : formatter -> unit -> int
775
776
777
778
779 val get_max_boxes : unit -> int
780
781 Returns the maximum number of pretty-printing boxes allowed before el‐
782 lipsis.
783
784
785
786 val pp_over_max_boxes : formatter -> unit -> bool
787
788
789
790
791 val over_max_boxes : unit -> bool
792
793 Tests if the maximum number of pretty-printing boxes allowed have al‐
794 ready been opened.
795
796
797
798
799 Tabulation boxes
800 A tabulation box prints material on lines divided into cells of fixed
801 length. A tabulation box provides a simple way to display vertical col‐
802 umns of left adjusted text.
803
804 This box features command set_tab to define cell boundaries, and com‐
805 mand print_tab to move from cell to cell and split the line when there
806 is no more cells to print on the line.
807
808 Note: printing within tabulation box is line directed, so arbitrary
809 line splitting inside a tabulation box leads to poor rendering. Yet,
810 controlled use of tabulation boxes allows simple printing of columns
811 within module Format .
812
813 val pp_open_tbox : formatter -> unit -> unit
814
815
816
817
818 val open_tbox : unit -> unit
819
820
821 open_tbox () opens a new tabulation box.
822
823 This box prints lines separated into cells of fixed width.
824
825 Inside a tabulation box, special tabulation markers defines points of
826 interest on the line (for instance to delimit cell boundaries). Func‐
827 tion Format.set_tab sets a tabulation marker at insertion point.
828
829 A tabulation box features specific tabulation breaks to move to next
830 tabulation marker or split the line. Function Format.print_tbreak
831 prints a tabulation break.
832
833
834
835 val pp_close_tbox : formatter -> unit -> unit
836
837
838
839
840 val close_tbox : unit -> unit
841
842 Closes the most recently opened tabulation box.
843
844
845
846 val pp_set_tab : formatter -> unit -> unit
847
848
849
850
851 val set_tab : unit -> unit
852
853 Sets a tabulation marker at current insertion point.
854
855
856
857 val pp_print_tab : formatter -> unit -> unit
858
859
860
861
862 val print_tab : unit -> unit
863
864
865 print_tab () emits a 'next' tabulation break hint: if not already set
866 on a tabulation marker, the insertion point moves to the first tabula‐
867 tion marker on the right, or the pretty-printer splits the line and in‐
868 sertion point moves to the leftmost tabulation marker.
869
870 It is equivalent to print_tbreak 0 0 .
871
872
873
874 val pp_print_tbreak : formatter -> int -> int -> unit
875
876
877
878
879 val print_tbreak : int -> int -> unit
880
881
882 print_tbreak nspaces offset emits a 'full' tabulation break hint.
883
884 If not already set on a tabulation marker, the insertion point moves to
885 the first tabulation marker on the right and the pretty-printer prints
886 nspaces spaces.
887
888 If there is no next tabulation marker on the right, the pretty-printer
889 splits the line at this point, then insertion point moves to the left‐
890 most tabulation marker of the box.
891
892 If the pretty-printer splits the line, offset is added to the current
893 indentation.
894
895
896
897
898 Ellipsis
899 val pp_set_ellipsis_text : formatter -> string -> unit
900
901
902
903
904 val set_ellipsis_text : string -> unit
905
906 Set the text of the ellipsis printed when too many pretty-printing
907 boxes are open (a single dot, . , by default).
908
909
910
911 val pp_get_ellipsis_text : formatter -> unit -> string
912
913
914
915
916 val get_ellipsis_text : unit -> string
917
918 Return the text of the ellipsis.
919
920
921
922
923 Semantic tags
924 type stag = ..
925
926
927 Semantic tags (or simply tags) are user's defined annotations to asso‐
928 ciate user's specific operations to printed entities.
929
930 Common usage of semantic tags is text decoration to get specific font
931 or text size rendering for a display device, or marking delimitation of
932 entities (e.g. HTML or TeX elements or terminal escape sequences).
933 More sophisticated usage of semantic tags could handle dynamic modifi‐
934 cation of the pretty-printer behavior to properly print the material
935 within some specific tags. For instance, we can define an RGB tag like
936 so:
937 type stag += RGB of {r:int;g:int;b:int}
938
939
940 In order to properly delimit printed entities, a semantic tag must be
941 opened before and closed after the entity. Semantic tags must be prop‐
942 erly nested like parentheses using Format.pp_open_stag and For‐
943 mat.pp_close_stag .
944
945 Tag specific operations occur any time a tag is opened or closed, At
946 each occurrence, two kinds of operations are performed tag-marking and
947 tag-printing:
948
949 -The tag-marking operation is the simpler tag specific operation: it
950 simply writes a tag specific string into the output device of the for‐
951 matter. Tag-marking does not interfere with line-splitting computation.
952
953 -The tag-printing operation is the more involved tag specific opera‐
954 tion: it can print arbitrary material to the formatter. Tag-printing is
955 tightly linked to the current pretty-printer operations.
956
957 Roughly speaking, tag-marking is commonly used to get a better render‐
958 ing of texts in the rendering device, while tag-printing allows fine
959 tuning of printing routines to print the same entity differently ac‐
960 cording to the semantic tags (i.e. print additional material or even
961 omit parts of the output).
962
963 More precisely: when a semantic tag is opened or closed then both and
964 successive 'tag-printing' and 'tag-marking' operations occur:
965
966 -Tag-printing a semantic tag means calling the formatter specific func‐
967 tion print_open_stag (resp. print_close_stag ) with the name of the
968 tag as argument: that tag-printing function can then print any regular
969 material to the formatter (so that this material is enqueued as usual
970 in the formatter queue for further line splitting computation).
971
972 -Tag-marking a semantic tag means calling the formatter specific func‐
973 tion mark_open_stag (resp. mark_close_stag ) with the name of the tag
974 as argument: that tag-marking function can then return the 'tag-opening
975 marker' (resp. `tag-closing marker') for direct output into the output
976 device of the formatter.
977
978 Being written directly into the output device of the formatter, seman‐
979 tic tag marker strings are not considered as part of the printing mate‐
980 rial that drives line splitting (in other words, the length of the
981 strings corresponding to tag markers is considered as zero for line
982 splitting).
983
984 Thus, semantic tag handling is in some sense transparent to
985 pretty-printing and does not interfere with usual indentation. Hence, a
986 single pretty-printing routine can output both simple 'verbatim' mate‐
987 rial or richer decorated output depending on the treatment of tags. By
988 default, tags are not active, hence the output is not decorated with
989 tag information. Once set_tags is set to true , the pretty-printer en‐
990 gine honors tags and decorates the output accordingly.
991
992 Default tag-marking functions behave the HTML way: Format.tag are en‐
993 closed in "<" and ">" while other tags are ignored; hence, opening
994 marker for tag string "t" is "<t>" and closing marker is "</t>" .
995
996 Default tag-printing functions just do nothing.
997
998 Tag-marking and tag-printing functions are user definable and can be
999 set by calling Format.set_formatter_stag_functions .
1000
1001 Semantic tag operations may be set on or off with Format.set_tags .
1002 Tag-marking operations may be set on or off with Format.set_mark_tags .
1003 Tag-printing operations may be set on or off with Format.set_print_tags
1004 .
1005
1006
1007 Since 4.08.0
1008
1009
1010 type tag = string
1011
1012
1013
1014
1015 type stag +=
1016 | String_tag of tag (* String_tag s is a string tag s . String tags
1017 can be inserted either by explicitly using the constructor String_tag
1018 or by using the dedicated format syntax "@{<s> ... @}" .
1019
1020
1021 Since 4.08.0
1022 *)
1023
1024
1025
1026
1027
1028 val pp_open_stag : formatter -> stag -> unit
1029
1030
1031
1032
1033 val open_stag : stag -> unit
1034
1035
1036 pp_open_stag ppf t opens the semantic tag named t .
1037
1038 The print_open_stag tag-printing function of the formatter is called
1039 with t as argument; then the opening tag marker for t , as given by
1040 mark_open_stag t , is written into the output device of the formatter.
1041
1042
1043 Since 4.08.0
1044
1045
1046
1047 val pp_close_stag : formatter -> unit -> unit
1048
1049
1050
1051
1052 val close_stag : unit -> unit
1053
1054
1055 pp_close_stag ppf () closes the most recently opened semantic tag t .
1056
1057 The closing tag marker, as given by mark_close_stag t , is written into
1058 the output device of the formatter; then the print_close_stag
1059 tag-printing function of the formatter is called with t as argument.
1060
1061
1062 Since 4.08.0
1063
1064
1065
1066 val pp_set_tags : formatter -> bool -> unit
1067
1068
1069
1070
1071 val set_tags : bool -> unit
1072
1073
1074 pp_set_tags ppf b turns on or off the treatment of semantic tags (de‐
1075 fault is off).
1076
1077
1078
1079 val pp_set_print_tags : formatter -> bool -> unit
1080
1081
1082
1083
1084 val set_print_tags : bool -> unit
1085
1086
1087 pp_set_print_tags ppf b turns on or off the tag-printing operations.
1088
1089
1090
1091 val pp_set_mark_tags : formatter -> bool -> unit
1092
1093
1094
1095
1096 val set_mark_tags : bool -> unit
1097
1098
1099 pp_set_mark_tags ppf b turns on or off the tag-marking operations.
1100
1101
1102
1103 val pp_get_print_tags : formatter -> unit -> bool
1104
1105
1106
1107
1108 val get_print_tags : unit -> bool
1109
1110 Return the current status of tag-printing operations.
1111
1112
1113
1114 val pp_get_mark_tags : formatter -> unit -> bool
1115
1116
1117
1118
1119 val get_mark_tags : unit -> bool
1120
1121 Return the current status of tag-marking operations.
1122
1123
1124
1125 val pp_set_formatter_out_channel : formatter -> out_channel -> unit
1126
1127
1128 Redirecting the standard formatter output
1129 val set_formatter_out_channel : out_channel -> unit
1130
1131 Redirect the standard pretty-printer output to the given channel. (All
1132 the output functions of the standard formatter are set to the default
1133 output functions printing to the given channel.)
1134
1135
1136 set_formatter_out_channel is equivalent to Format.pp_set_format‐
1137 ter_out_channel std_formatter .
1138
1139
1140
1141 val pp_set_formatter_output_functions : formatter -> (string -> int ->
1142 int -> unit) -> (unit -> unit) -> unit
1143
1144
1145
1146
1147 val set_formatter_output_functions : (string -> int -> int -> unit) ->
1148 (unit -> unit) -> unit
1149
1150
1151 pp_set_formatter_output_functions ppf out flush redirects the standard
1152 pretty-printer output functions to the functions out and flush .
1153
1154 The out function performs all the pretty-printer string output. It is
1155 called with a string s , a start position p , and a number of charac‐
1156 ters n ; it is supposed to output characters p to p + n - 1 of s .
1157
1158 The flush function is called whenever the pretty-printer is flushed
1159 (via conversion %! , or pretty-printing indications @? or @. , or
1160 using low level functions print_flush or print_newline ).
1161
1162
1163
1164 val pp_get_formatter_output_functions : formatter -> unit -> (string ->
1165 int -> int -> unit) * (unit -> unit)
1166
1167
1168
1169
1170 val get_formatter_output_functions : unit -> (string -> int -> int ->
1171 unit) * (unit -> unit)
1172
1173 Return the current output functions of the standard pretty-printer.
1174
1175
1176
1177
1178 Redefining formatter output
1179 The Format module is versatile enough to let you completely redefine
1180 the meaning of pretty-printing output: you may provide your own func‐
1181 tions to define how to handle indentation, line splitting, and even
1182 printing of all the characters that have to be printed!
1183
1184 Redefining output functions
1185 type formatter_out_functions = {
1186 out_string : string -> int -> int -> unit ;
1187 out_flush : unit -> unit ;
1188 out_newline : unit -> unit ;
1189 out_spaces : int -> unit ;
1190 out_indent : int -> unit ; (* .B "Since" 4.06.0
1191 *)
1192 }
1193
1194
1195 The set of output functions specific to a formatter:
1196
1197 -the out_string function performs all the pretty-printer string output.
1198 It is called with a string s , a start position p , and a number of
1199 characters n ; it is supposed to output characters p to p + n - 1 of s
1200 .
1201
1202 -the out_flush function flushes the pretty-printer output device.
1203
1204 - out_newline is called to open a new line when the pretty-printer
1205 splits the line.
1206
1207 -the out_spaces function outputs spaces when a break hint leads to spa‐
1208 ces instead of a line split. It is called with the number of spaces to
1209 output.
1210
1211 -the out_indent function performs new line indentation when the
1212 pretty-printer splits the line. It is called with the indentation value
1213 of the new line.
1214
1215 By default:
1216
1217 -fields out_string and out_flush are output device specific; (e.g.
1218 output_string and flush for a out_channel device, or Buffer.add_sub‐
1219 string and ignore for a Buffer.t output device),
1220
1221 -field out_newline is equivalent to out_string "\n" 0 1 ;
1222
1223 -fields out_spaces and out_indent are equivalent to out_string
1224 (String.make n ' ') 0 n .
1225
1226
1227
1228 Since 4.01.0
1229
1230
1231
1232 val pp_set_formatter_out_functions : formatter -> formatter_out_func‐
1233 tions -> unit
1234
1235
1236
1237
1238 val set_formatter_out_functions : formatter_out_functions -> unit
1239
1240
1241 pp_set_formatter_out_functions ppf out_funs Set all the pretty-printer
1242 output functions of ppf to those of argument out_funs ,
1243
1244 This way, you can change the meaning of indentation (which can be some‐
1245 thing else than just printing space characters) and the meaning of new
1246 lines opening (which can be connected to any other action needed by the
1247 application at hand).
1248
1249 Reasonable defaults for functions out_spaces and out_newline are re‐
1250 spectively out_funs.out_string (String.make n ' ') 0 n and
1251 out_funs.out_string "\n" 0 1 .
1252
1253
1254 Since 4.01.0
1255
1256
1257
1258 val pp_get_formatter_out_functions : formatter -> unit -> format‐
1259 ter_out_functions
1260
1261
1262
1263
1264 val get_formatter_out_functions : unit -> formatter_out_functions
1265
1266 Return the current output functions of the pretty-printer, including
1267 line splitting and indentation functions. Useful to record the current
1268 setting and restore it afterwards.
1269
1270
1271 Since 4.01.0
1272
1273
1274
1275
1276 Redefining semantic tag operations
1277 type formatter_stag_functions = {
1278 mark_open_stag : stag -> string ;
1279 mark_close_stag : stag -> string ;
1280 print_open_stag : stag -> unit ;
1281 print_close_stag : stag -> unit ;
1282 }
1283
1284
1285 The semantic tag handling functions specific to a formatter: mark ver‐
1286 sions are the 'tag-marking' functions that associate a string marker to
1287 a tag in order for the pretty-printing engine to write those markers as
1288 0 length tokens in the output device of the formatter. print versions
1289 are the 'tag-printing' functions that can perform regular printing when
1290 a tag is closed or opened.
1291
1292
1293 Since 4.08.0
1294
1295
1296
1297 val pp_set_formatter_stag_functions : formatter -> formatter_stag_func‐
1298 tions -> unit
1299
1300
1301
1302
1303 val set_formatter_stag_functions : formatter_stag_functions -> unit
1304
1305
1306 pp_set_formatter_stag_functions ppf tag_funs changes the meaning of
1307 opening and closing semantic tag operations to use the functions in
1308 tag_funs when printing on ppf .
1309
1310 When opening a semantic tag with name t , the string t is passed to the
1311 opening tag-marking function (the mark_open_stag field of the record
1312 tag_funs ), that must return the opening tag marker for that name. When
1313 the next call to close_stag () happens, the semantic tag name t is sent
1314 back to the closing tag-marking function (the mark_close_stag field of
1315 record tag_funs ), that must return a closing tag marker for that name.
1316
1317 The print_ field of the record contains the tag-printing functions that
1318 are called at tag opening and tag closing time, to output regular mate‐
1319 rial in the pretty-printer queue.
1320
1321
1322 Since 4.08.0
1323
1324
1325
1326 val pp_get_formatter_stag_functions : formatter -> unit -> format‐
1327 ter_stag_functions
1328
1329
1330
1331
1332 val get_formatter_stag_functions : unit -> formatter_stag_functions
1333
1334 Return the current semantic tag operation functions of the standard
1335 pretty-printer.
1336
1337
1338 Since 4.08.0
1339
1340
1341
1342
1343 Defining formatters
1344 Defining new formatters permits unrelated output of material in paral‐
1345 lel on several output devices. All the parameters of a formatter are
1346 local to the formatter: right margin, maximum indentation limit, maxi‐
1347 mum number of pretty-printing boxes simultaneously open, ellipsis, and
1348 so on, are specific to each formatter and may be fixed independently.
1349
1350 For instance, given a Buffer.t buffer b , Format.formatter_of_buffer b
1351 returns a new formatter using buffer b as its output device. Simi‐
1352 larly, given a out_channel output channel oc , Format.format‐
1353 ter_of_out_channel oc returns a new formatter using channel oc as its
1354 output device.
1355
1356 Alternatively, given out_funs , a complete set of output functions for
1357 a formatter, then Format.formatter_of_out_functions out_funs computes a
1358 new formatter using those functions for output.
1359
1360 val formatter_of_out_channel : out_channel -> formatter
1361
1362
1363 formatter_of_out_channel oc returns a new formatter writing to the cor‐
1364 responding output channel oc .
1365
1366
1367
1368 val synchronized_formatter_of_out_channel : out_channel -> formatter
1369 Domain.DLS.key
1370
1371
1372 synchronized_formatter_of_out_channel oc returns the key to the do‐
1373 main-local state that holds the domain-local formatter for writing to
1374 the corresponding output channel oc .
1375
1376 When the formatter is used with multiple domains, the output from the
1377 domains will be interleaved with each other at points where the format‐
1378 ter is flushed, such as with Format.print_flush .
1379
1380
1381 Alert unstable.
1382
1383
1384
1385 val std_formatter : formatter
1386
1387 The initial domain's standard formatter to write to standard output.
1388
1389 It is defined as Format.formatter_of_out_channel stdout .
1390
1391
1392
1393 val get_std_formatter : unit -> formatter
1394
1395
1396 get_std_formatter () returns the current domain's standard formatter
1397 used to write to standard output.
1398
1399
1400 Since 5.0
1401
1402
1403
1404 val err_formatter : formatter
1405
1406 The initial domain's formatter to write to standard error.
1407
1408 It is defined as Format.formatter_of_out_channel stderr .
1409
1410
1411
1412 val get_err_formatter : unit -> formatter
1413
1414
1415 get_err_formatter () returns the current domain's formatter used to
1416 write to standard error.
1417
1418
1419 Since 5.0
1420
1421
1422
1423 val formatter_of_buffer : Buffer.t -> formatter
1424
1425
1426 formatter_of_buffer b returns a new formatter writing to buffer b . At
1427 the end of pretty-printing, the formatter must be flushed using For‐
1428 mat.pp_print_flush or Format.pp_print_newline , to print all the pend‐
1429 ing material into the buffer.
1430
1431
1432
1433 val stdbuf : Buffer.t
1434
1435 The initial domain's string buffer in which str_formatter writes.
1436
1437
1438
1439 val get_stdbuf : unit -> Buffer.t
1440
1441
1442 get_stdbuf () returns the current domain's string buffer in which the
1443 current domain's string formatter writes.
1444
1445
1446 Since 5.0
1447
1448
1449
1450 val str_formatter : formatter
1451
1452 The initial domain's formatter to output to the Format.stdbuf string
1453 buffer.
1454
1455
1456 str_formatter is defined as Format.formatter_of_buffer Format.stdbuf .
1457
1458
1459
1460 val get_str_formatter : unit -> formatter
1461
1462 The current domain's formatter to output to the current domains string
1463 buffer.
1464
1465
1466 Since 5.0
1467
1468
1469
1470 val flush_str_formatter : unit -> string
1471
1472 Returns the material printed with str_formatter of the current domain,
1473 flushes the formatter and resets the corresponding buffer.
1474
1475
1476
1477 val make_formatter : (string -> int -> int -> unit) -> (unit -> unit)
1478 -> formatter
1479
1480
1481 make_formatter out flush returns a new formatter that outputs with
1482 function out , and flushes with function flush .
1483
1484 For instance,
1485 make_formatter
1486 (Stdlib.output oc)
1487 (fun () -> Stdlib.flush oc)
1488
1489 returns a formatter to the out_channel oc .
1490
1491
1492
1493 val make_synchronized_formatter : (string -> int -> int -> unit) ->
1494 (unit -> unit) -> formatter Domain.DLS.key
1495
1496
1497 make_synchronized_formatter out flush returns the key to the domain-lo‐
1498 cal state that holds the domain-local formatter that outputs with func‐
1499 tion out , and flushes with function flush .
1500
1501 When the formatter is used with multiple domains, the output from the
1502 domains will be interleaved with each other at points where the format‐
1503 ter is flushed, such as with Format.print_flush .
1504
1505
1506 Since 5.0
1507
1508
1509 Alert unstable.
1510
1511
1512
1513 val formatter_of_out_functions : formatter_out_functions -> formatter
1514
1515
1516 formatter_of_out_functions out_funs returns a new formatter that writes
1517 with the set of output functions out_funs .
1518
1519 See definition of type Format.formatter_out_functions for the meaning
1520 of argument out_funs .
1521
1522
1523 Since 4.06.0
1524
1525
1526
1527
1528 Symbolic pretty-printing
1529 Symbolic pretty-printing is pretty-printing using a symbolic formatter,
1530 i.e. a formatter that outputs symbolic pretty-printing items.
1531
1532 When using a symbolic formatter, all regular pretty-printing activities
1533 occur but output material is symbolic and stored in a buffer of output
1534 items. At the end of pretty-printing, flushing the output buffer al‐
1535 lows post-processing of symbolic output before performing low level
1536 output operations.
1537
1538 In practice, first define a symbolic output buffer b using:
1539
1540 - let sob = make_symbolic_output_buffer () . Then define a symbolic
1541 formatter with:
1542
1543 - let ppf = formatter_of_symbolic_output_buffer sob
1544
1545 Use symbolic formatter ppf as usual, and retrieve symbolic items at end
1546 of pretty-printing by flushing symbolic output buffer sob with:
1547
1548 - flush_symbolic_output_buffer sob .
1549
1550
1551 type symbolic_output_item =
1552 | Output_flush (* symbolic flush command
1553 *)
1554 | Output_newline (* symbolic newline command
1555 *)
1556 | Output_string of string
1557 (* Output_string s : symbolic output for string s
1558
1559 *)
1560 | Output_spaces of int
1561 (* Output_spaces n : symbolic command to output n spaces
1562 *)
1563 | Output_indent of int
1564 (* Output_indent i : symbolic indentation of size i
1565
1566 *)
1567
1568
1569 Items produced by symbolic pretty-printers
1570
1571
1572 Since 4.06.0
1573
1574
1575 type symbolic_output_buffer
1576
1577
1578 The output buffer of a symbolic pretty-printer.
1579
1580
1581 Since 4.06.0
1582
1583
1584
1585 val make_symbolic_output_buffer : unit -> symbolic_output_buffer
1586
1587
1588 make_symbolic_output_buffer () returns a fresh buffer for symbolic out‐
1589 put.
1590
1591
1592 Since 4.06.0
1593
1594
1595
1596 val clear_symbolic_output_buffer : symbolic_output_buffer -> unit
1597
1598
1599 clear_symbolic_output_buffer sob resets buffer sob .
1600
1601
1602 Since 4.06.0
1603
1604
1605
1606 val get_symbolic_output_buffer : symbolic_output_buffer -> sym‐
1607 bolic_output_item list
1608
1609
1610 get_symbolic_output_buffer sob returns the contents of buffer sob .
1611
1612
1613 Since 4.06.0
1614
1615
1616
1617 val flush_symbolic_output_buffer : symbolic_output_buffer -> sym‐
1618 bolic_output_item list
1619
1620
1621 flush_symbolic_output_buffer sob returns the contents of buffer sob and
1622 resets buffer sob . flush_symbolic_output_buffer sob is equivalent to
1623 let items = get_symbolic_output_buffer sob in
1624 clear_symbolic_output_buffer sob; items
1625
1626
1627
1628 Since 4.06.0
1629
1630
1631
1632 val add_symbolic_output_item : symbolic_output_buffer -> symbolic_out‐
1633 put_item -> unit
1634
1635
1636 add_symbolic_output_item sob itm adds item itm to buffer sob .
1637
1638
1639 Since 4.06.0
1640
1641
1642
1643 val formatter_of_symbolic_output_buffer : symbolic_output_buffer ->
1644 formatter
1645
1646
1647 formatter_of_symbolic_output_buffer sob returns a symbolic formatter
1648 that outputs to symbolic_output_buffer sob .
1649
1650
1651 Since 4.06.0
1652
1653
1654
1655
1656 Convenience formatting functions.
1657 val pp_print_list : ?pp_sep:(formatter -> unit -> unit) -> (formatter
1658 -> 'a -> unit) -> formatter -> 'a list -> unit
1659
1660
1661 pp_print_list ?pp_sep pp_v ppf l prints items of list l , using pp_v to
1662 print each item, and calling pp_sep between items ( pp_sep defaults to
1663 Format.pp_print_cut . Does nothing on empty lists.
1664
1665
1666 Since 4.02.0
1667
1668
1669
1670 val pp_print_seq : ?pp_sep:(formatter -> unit -> unit) -> (formatter ->
1671 'a -> unit) -> formatter -> 'a Seq.t -> unit
1672
1673
1674 pp_print_seq ?pp_sep pp_v ppf s prints items of sequence s , using pp_v
1675 to print each item, and calling pp_sep between items ( pp_sep defaults
1676 to Format.pp_print_cut . Does nothing on empty sequences.
1677
1678 This function does not terminate on infinite sequences.
1679
1680
1681 Since 4.12
1682
1683
1684
1685 val pp_print_text : formatter -> string -> unit
1686
1687
1688 pp_print_text ppf s prints s with spaces and newlines respectively
1689 printed using Format.pp_print_space and Format.pp_force_newline .
1690
1691
1692 Since 4.02.0
1693
1694
1695
1696 val pp_print_option : ?none:(formatter -> unit -> unit) -> (formatter
1697 -> 'a -> unit) -> formatter -> 'a option -> unit
1698
1699
1700 pp_print_option ?none pp_v ppf o prints o on ppf using pp_v if o is
1701 Some v and none if it is None . none prints nothing by default.
1702
1703
1704 Since 4.08
1705
1706
1707
1708 val pp_print_result : ok:(formatter -> 'a -> unit) -> error:(formatter
1709 -> 'e -> unit) -> formatter -> ('a, 'e) result -> unit
1710
1711
1712 pp_print_result ~ok ~error ppf r prints r on ppf using ok if r is Ok _
1713 and error if r is Error _ .
1714
1715
1716 Since 4.08
1717
1718
1719
1720 val pp_print_either : left:(formatter -> 'a -> unit) -> right:(format‐
1721 ter -> 'b -> unit) -> formatter -> ('a, 'b) Either.t -> unit
1722
1723
1724 pp_print_either ~left ~right ppf e prints e on ppf using left if e is
1725 Either.Left _ and right if e is Either.Right _ .
1726
1727
1728 Since 4.13
1729
1730
1731
1732
1733 Formatted pretty-printing
1734 Module Format provides a complete set of printf like functions for
1735 pretty-printing using format string specifications.
1736
1737 Specific annotations may be added in the format strings to give
1738 pretty-printing commands to the pretty-printing engine.
1739
1740 Those annotations are introduced in the format strings using the @
1741 character. For instance, @ means a space break, @, means a cut, @[
1742 opens a new box, and @] closes the last open box.
1743
1744 val fprintf : formatter -> ('a, formatter, unit) format -> 'a
1745
1746
1747
1748
1749
1750 fprintf ff fmt arg1 ... argN formats the arguments arg1 to argN accord‐
1751 ing to the format string fmt , and outputs the resulting string on the
1752 formatter ff .
1753
1754 The format string fmt is a character string which contains three types
1755 of objects: plain characters and conversion specifications as specified
1756 in the Printf module, and pretty-printing indications specific to the
1757 Format module.
1758
1759 The pretty-printing indication characters are introduced by a @ charac‐
1760 ter, and their meanings are:
1761
1762 - @[ : open a pretty-printing box. The type and offset of the box may
1763 be optionally specified with the following syntax: the < character,
1764 followed by an optional box type indication, then an optional integer
1765 offset, and the closing > character. Pretty-printing box type is one
1766 of h , v , hv , b , or hov . ' h ' stands for an 'horizontal'
1767 pretty-printing box, ' v ' stands for a 'vertical' pretty-printing box,
1768 ' hv ' stands for an 'horizontal/vertical' pretty-printing box, ' b '
1769 stands for an 'horizontal-or-vertical' pretty-printing box demonstrat‐
1770 ing indentation, ' hov ' stands a simple 'horizontal-or-vertical'
1771 pretty-printing box. For instance, @[<hov 2> opens an 'horizon‐
1772 tal-or-vertical' pretty-printing box with indentation 2 as obtained
1773 with open_hovbox 2 . For more details about pretty-printing boxes, see
1774 the various box opening functions open_*box .
1775
1776 - @] : close the most recently opened pretty-printing box.
1777
1778 - @, : output a 'cut' break hint, as with print_cut () .
1779
1780 - @ : output a 'space' break hint, as with print_space () .
1781
1782 - @; : output a 'full' break hint as with print_break . The nspaces and
1783 offset parameters of the break hint may be optionally specified with
1784 the following syntax: the < character, followed by an integer nspaces
1785 value, then an integer offset , and a closing > character. If no pa‐
1786 rameters are provided, the good break defaults to a 'space' break hint.
1787
1788 - @. : flush the pretty-printer and split the line, as with print_new‐
1789 line () .
1790
1791 - @<n> : print the following item as if it were of length n . Hence,
1792 printf "@<0>%s" arg prints arg as a zero length string. If @<n> is not
1793 followed by a conversion specification, then the following character of
1794 the format is printed as if it were of length n .
1795
1796 - @{ : open a semantic tag. The name of the tag may be optionally spec‐
1797 ified with the following syntax: the < character, followed by an op‐
1798 tional string specification, and the closing > character. The string
1799 specification is any character string that does not contain the closing
1800 character '>' . If omitted, the tag name defaults to the empty string.
1801 For more details about semantic tags, see the functions For‐
1802 mat.open_stag and Format.close_stag .
1803
1804 - @} : close the most recently opened semantic tag.
1805
1806 - @? : flush the pretty-printer as with print_flush () . This is
1807 equivalent to the conversion %! .
1808
1809 - @\n : force a newline, as with force_newline () , not the normal way
1810 of pretty-printing, you should prefer using break hints inside a verti‐
1811 cal pretty-printing box.
1812
1813 Note: To prevent the interpretation of a @ character as a pretty-print‐
1814 ing indication, escape it with a % character. Old quotation mode @@ is
1815 deprecated since it is not compatible with formatted input interpreta‐
1816 tion of character '@' .
1817
1818 Example: printf "@[%s@ %d@]@." "x =" 1 is equivalent to open_box ();
1819 print_string "x ="; print_space ();
1820 print_int 1; close_box (); print_newline () . It prints x = 1
1821 within a pretty-printing 'horizontal-or-vertical' box.
1822
1823 val printf : ('a, formatter, unit) format -> 'a
1824
1825 Same as fprintf above, but output on get_std_formatter () .
1826
1827 It is defined similarly to fun fmt -> fprintf (get_std_formatter ())
1828 fmt but delays calling get_std_formatter until after the final argument
1829 required by the format is received. When used with multiple domains,
1830 the output from the domains will be interleaved with each other at
1831 points where the formatter is flushed, such as with Format.print_flush
1832 .
1833
1834
1835
1836 val eprintf : ('a, formatter, unit) format -> 'a
1837
1838 Same as fprintf above, but output on get_err_formatter () .
1839
1840 It is defined similarly to fun fmt -> fprintf (get_err_formatter ())
1841 fmt but delays calling get_err_formatter until after the final argument
1842 required by the format is received. When used with multiple domains,
1843 the output from the domains will be interleaved with each other at
1844 points where the formatter is flushed, such as with Format.print_flush
1845 .
1846
1847
1848
1849 val sprintf : ('a, unit, string) format -> 'a
1850
1851 Same as printf above, but instead of printing on a formatter, returns a
1852 string containing the result of formatting the arguments. Note that
1853 the pretty-printer queue is flushed at the end of each call to sprintf
1854 .
1855
1856 In case of multiple and related calls to sprintf to output material on
1857 a single string, you should consider using fprintf with the predefined
1858 formatter str_formatter and call flush_str_formatter () to get the fi‐
1859 nal result.
1860
1861 Alternatively, you can use Format.fprintf with a formatter writing to a
1862 buffer of your own: flushing the formatter and the buffer at the end of
1863 pretty-printing returns the desired string.
1864
1865
1866
1867 val asprintf : ('a, formatter, unit, string) format4 -> 'a
1868
1869 Same as printf above, but instead of printing on a formatter, returns a
1870 string containing the result of formatting the arguments. The type of
1871 asprintf is general enough to interact nicely with %a conversions.
1872
1873
1874 Since 4.01.0
1875
1876
1877
1878 val dprintf : ('a, formatter, unit, formatter -> unit) format4 -> 'a
1879
1880 Same as Format.fprintf , except the formatter is the last argument.
1881 dprintf "..." a b c is a function of type formatter -> unit which can
1882 be given to a format specifier %t .
1883
1884 This can be used as a replacement for Format.asprintf to delay format‐
1885 ting decisions. Using the string returned by Format.asprintf in a for‐
1886 matting context forces formatting decisions to be taken in isolation,
1887 and the final string may be created prematurely. Format.dprintf allows
1888 delay of formatting decisions until the final formatting context is
1889 known. For example:
1890 let t = Format.dprintf "%i@ %i@ %i" 1 2 3 in
1891 ...
1892 Format.printf "@[<v>%t@]" t
1893
1894
1895
1896 Since 4.08.0
1897
1898
1899
1900 val ifprintf : formatter -> ('a, formatter, unit) format -> 'a
1901
1902 Same as fprintf above, but does not print anything. Useful to ignore
1903 some material when conditionally printing.
1904
1905
1906 Since 3.10.0
1907
1908
1909
1910
1911 Formatted Pretty-Printing with continuations.
1912
1913 val kfprintf : (formatter -> 'a) -> formatter -> ('b, formatter, unit,
1914 'a) format4 -> 'b
1915
1916 Same as fprintf above, but instead of returning immediately, passes the
1917 formatter to its first argument at the end of printing.
1918
1919
1920
1921 val kdprintf : ((formatter -> unit) -> 'a) -> ('b, formatter, unit, 'a)
1922 format4 -> 'b
1923
1924 Same as Format.dprintf above, but instead of returning immediately,
1925 passes the suspended printer to its first argument at the end of print‐
1926 ing.
1927
1928
1929 Since 4.08.0
1930
1931
1932
1933 val ikfprintf : (formatter -> 'a) -> formatter -> ('b, formatter, unit,
1934 'a) format4 -> 'b
1935
1936 Same as kfprintf above, but does not print anything. Useful to ignore
1937 some material when conditionally printing.
1938
1939
1940 Since 3.12.0
1941
1942
1943
1944 val ksprintf : (string -> 'a) -> ('b, unit, string, 'a) format4 -> 'b
1945
1946 Same as sprintf above, but instead of returning the string, passes it
1947 to the first argument.
1948
1949
1950
1951 val kasprintf : (string -> 'a) -> ('b, formatter, unit, 'a) format4 ->
1952 'b
1953
1954 Same as asprintf above, but instead of returning the string, passes it
1955 to the first argument.
1956
1957
1958 Since 4.03
1959
1960
1961
1962
1963
1964OCamldoc 2023-07-20 Format(3)