-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrfi-166.html
1963 lines (1644 loc) · 75.2 KB
/
srfi-166.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>SRFI 166: Monadic Formatting</title>
<link href="/favicon.png" rel="icon" sizes="192x192" type="image/png">
<link rel="stylesheet" href="https://srfi.schemers.org/srfi.css" type="text/css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords" content="Scheme, programming language, SRFI, format">
<style>
body {
max-width: 7.6in;
margin: 30pt;
}
dt {
font-weight: bold;
color: rgb(120,0,120);
}
.code-example {
background-color: rgb(241,241,241);
}
.output-example {
background-color: beige;
font-size: smaller;
}
var {
font-weight: bold;
color: rgb(20,20,120);
}
</style>
</head>
<body>
<h1><a href="https://srfi.schemers.org/"><img class="srfi-logo" src="https://srfi.schemers.org/srfi-logo.svg" alt="SRFI logo" /></a>166: Monadic Formatting</h1>
<p>by Alex Shinn</p>
<h2><a id="Status">Status</a></h2>
<p>This SRFI is currently in <em>final</em> status. Here is <a href="https://srfi.schemers.org/srfi-process.html">an explanation</a> of each status that a SRFI can hold. To provide input on this SRFI, please send email to <code><a href="mailto:srfi+minus+166+at+srfi+dotschemers+dot+org">srfi-166@<span class="antispam">nospam</span>srfi.schemers.org</a></code>. To subscribe to the list, follow <a href="https://srfi.schemers.org/srfi-list-subscribe.html">these instructions</a>. You can access previous messages via the mailing list <a href="https://srfi-email.schemers.org/srfi-166">archive</a>.</p>
<ul>
<li>Received: 2019/3/27</li>
<li>Draft #1 published: 2019-03-27</li>
<li>Draft #2 published: 2020-04-22</li>
<li>Draft #3 published: 2020-06-08</li>
<li>Draft #4 published: 2020-07-03</li>
<li>Draft #5 published: 2020-07-20</li>
<li>Finalized: 2020-07-30</li>
</ul>
<h2>Table of Contents</h2>
<ul id="toc-table">
<li><a href="#Abstract">Abstract</a></li>
<li><a href="#Diffs">Summary of differences from SRFI 159</a></li>
<li><a href="#Rationale">Rationale</a></li>
<li><a href="#Types-and-Naming-Conventions">Types and Naming Conventions</a></li>
<li><a href="#Specification">Specification</a></li>
<li><ul>
<li><a href="#Usage">Usage</a></li>
<li><a href="#Formatting-Objects">Formatting Objects</a></li>
<li><a href="#Formatting-Numbers">Formatting Numbers</a></li>
<li><a href="#Formatting-Space">Formatting Space</a></li>
<li><a href="#Concatenation">Concatenation</a></li>
<li><a href="#Padding-and-Trimming">Padding and Trimming</a></li>
<li><a href="#Pretty-Printing">Pretty Printing</a></li>
<li><a href="#Columnar-Formatting">Columnar Formatting</a></li>
<li><a href="#Formatting-with-Color">Formatting with Color</a></li>
<li><a href="#Unicode">Unicode</a></li>
<li><a href="#Higher-Order-Formatters-and-State">Higher Order Formatters and State</a></li>
<li><a href="#State-Variables">State Variables</a></li>
</ul></li>
<li><a href="#Implementation">Implementation</a></li>
<li><a href="#Acknowledgements">Acknowledgements</a></li>
<li><a href="#References">References</a></li>
</ul>
<h2><a id="Abstract">Abstract</a></h2>
<p>
A library of procedures for formatting Scheme objects to text in
various ways, and for easily concatenating, composing and extending
these formatters efficiently without resorting to capturing and
manipulating intermediate strings.
<p>
This SRFI is an updated version of SRFI 159, primarily with the
difference that state variables are hygienic.
<h2><a id="Diffs">Summary of differences from SRFI 159:</a></h2>
<ul>
<li>State variables are first class and hygienic</li>
<li>Added <code>written-shared</code>, <code>pretty-shared</code></li>
<li>Added <code>as-italic</code>, <code>as-color</code>, <code>as-true-color</code>, <code>on-<i>color</i></code> background variants, and <code>pretty-with-color</code></li>
<li>Added <code>ambiguous-is-wide?</code> state variable and <code>string-terminal-width/wide</code> utility</li>
<li>Added <code>substring/width</code> state var for width-aware substring operations, with <code>substring-terminal-width(/wide)</code> utilities</li>
<li>Added <code>substring/preserve</code> state var used in trimming, with <code>substring-terminal-preserve</code> utility</li>
<li>Added <code>pretty-environment</code> state variable</li>
<li>Renamed <code>as-unicode</code> to <code>terminal-aware</code></li>
<li>Restored non-uniform comma rules as needed in India</li>
<li>Restored <code>upcased</code> and <code>downcased</code></li>
<li>Several clarifications and more examples</li>
</ul>
<h2><a id="Rationale">Rationale</a></h2>
<p>
There are several approaches to text formatting. Concatenating
strings to display is not acceptable, since it doesn't scale to very
large output. The simplest realistic idea, and what people resort to
in typical portable Scheme, is to interleave <code>display</code> and <code>write</code> and
manual loops, but this is both extremely verbose and doesn't compose
well. A simple concept such as padding space can't be achieved
directly without somehow capturing intermediate output.
<p>
The traditional approach in other languages is to use templates -
typically strings, though in theory any object could be used and
indeed Emacs's mode-line format templates allow arbitrary
sexps. Templates can use either escape sequences (as in C's printf
and Common Lisp's format) or pattern matching (as in Visual Basic's
Format, Perl6's form, and SQL date formats). The primary
disadvantage of templates is the relative difficulty (usually
impossibility) of extending them, their opaqueness, and the
unreadability that arises with complex formats. Templates are not
without their advantages, but they are already addressed by other
libraries such as SRFI 28 and SRFI 48.
<p>
Another important aspect of formatting is state. Common Lisp format
provides a "fresh-line" format spec which outputs a newline only if
the output stream is not already at the beginning of a line. C++
iostreams allow changing the radix and floating-point precision for
numeric output, not just for a single value but as a persistent
setting for all future output. Custom formatters which could
manipulate their own state would allow for many new possibilities.
<p>
This SRFI takes a combinator approach to solving both problems.
Formatters are defined, which are called to produce their output as
needed, composed with other formatters, and refer to and update
arbitrary state. The primary goal of this SRFI is to have a
maximally expressive and extensible formatting library. The next
most important goal is scalability — to be able to handle
arbitrarily large output and not build intermediate results except
where necessary. The third goal is brevity and ease of use.
<h2>Index</h2>
<pre>
<b>Base</b>
<a href="#proc-show">show</a> <a href="#proc-each">each</a> <a href="#proc-each-in-list">each-in-list</a>
<a href="#proc-displayed">displayed</a> <a href="#proc-written">written</a> <a href="#proc-written-shared">written-shared</a> <a href="#proc-written-simply">written-simply</a>
<a href="#proc-escaped">escaped</a> <a href="#proc-maybe-escaped">maybe-escaped</a>
<a href="#proc-numeric">numeric</a> <a href="#proc-numeric_2fcomma">numeric/comma</a> <a href="#proc-numeric_2fsi">numeric/si</a> <a href="#proc-numeric_2ffitted">numeric/fitted</a>
<a href="#proc-nl">nl</a> <a href="#proc-fl">fl</a> <a href="#proc-space-to">space-to</a> <a href="#proc-tab-to">tab-to</a> <a href="#proc-nothing">nothing</a>
<a href="#proc-joined">joined</a> <a href="#proc-joined_2fprefix">joined/prefix</a> <a href="#proc-joined_2fsuffix">joined/suffix</a>
<a href="#proc-joined_2flast">joined/last</a> <a href="#proc-joined_2fdot">joined/dot</a> <a href="#proc-joined_2frange">joined/range</a>
<a href="#proc-padded">padded</a> <a href="#proc-padded_2fright">padded/right</a> <a href="#proc-padded_2fboth">padded/both</a>
<a href="#proc-trimmed">trimmed</a> <a href="#proc-trimmed_2fright">trimmed/right</a> <a href="#proc-trimmed_2fboth">trimmed/both</a>
<a href="#proc-trimmed_2flazy">trimmed/lazy</a> <a href="#proc-fitted">fitted</a> <a href="#proc-fitted_2fright">fitted/right</a> <a href="#proc-fitted_2fboth">fitted/both</a>
<a href="#proc-fn">fn</a> <a href="#proc-with">with</a> <a href="#proc-with!">with!</a> <a href="#proc-forked">forked</a> <a href="#proc-call-with-output">call-with-output</a>
<a href="#proc-make-state-variable">make-state-variable</a>
<a href="#proc-port">port</a> <a href="#proc-row">row</a> <a href="#proc-col">col</a> <a href="#proc-width">width</a> <a href="#proc-output">output</a> <a href="#proc-writer">writer</a>
<a href="#proc-string-width">string-width</a> <a href="#proc-substring_2fwidth">substring/width</a>
<a href="#proc-substring_2fpreserve">substring/preserve</a>
<a href="#proc-pad-char">pad-char</a> <a href="#proc-ellipsis">ellipsis</a>
<a href="#proc-radix">radix</a> <a href="#proc-precision">precision</a> <a href="#proc-decimal-sep">decimal-sep</a> <a href="#proc-decimal-align">decimal-align</a>
<a href="#proc-sign-rule">sign-rule</a> <a href="#proc-comma-rule">comma-rule</a> <a href="#proc-comma-sep">comma-sep</a>
<a href="#proc-word-separator?">word-separator?</a> <a href="#proc-ambiguous-is-wide?">ambiguous-is-wide?</a>
<b>Pretty</b>
<a href="#proc-pretty">pretty</a> <a href="#proc-pretty-shared">pretty-shared</a> <a href="#proc-pretty-simply">pretty-simply</a> <a href="#proc-pretty-with-color">pretty-with-color</a>
<b>Columnar</b>
<a href="#proc-columnar">columnar</a> <a href="#proc-tabular">tabular</a> <a href="#proc-wrapped">wrapped</a> <a href="#proc-wrapped_2flist">wrapped/list</a> <a href="#proc-wrapped_2fchar">wrapped/char</a>
<a href="#proc-justified">justified</a> <a href="#proc-from-file">from-file</a> <a href="#proc-line-numbers">line-numbers</a>
<b>Unicode</b>
<a href="#proc-terminal-aware">terminal-aware</a>
<a href="#proc-string-terminal-width">string-terminal-width</a> <a href="#proc-string-terminal-width_2fwide">string-terminal-width/wide</a>
<a href="#proc-substring-terminal-width">substring-terminal-width</a> <a href="#proc-substring-terminal-width_2fwide">substring-terminal-width/wide</a>
<a href="#proc-substring-terminal-preserve">substring-terminal-preserve</a>
<a href="#proc-upcased">upcased</a> <a href="#proc-downcased">downcased</a>
<b>Color</b>
<a href="#proc-as-red">as-red</a> <a href="#proc-as-blue">as-blue</a> <a href="#proc-as-green">as-green</a> <a href="#proc-as-cyan">as-cyan</a>
<a href="#proc-as-yellow">as-yellow</a> <a href="#proc-as-magenta">as-magenta</a> <a href="#proc-as-white">as-white</a> <a href="#proc-as-black">as-black</a>
<a href="#proc-as-bold">as-bold</a> <a href="#proc-as-italic">as-italic</a> <a href="#proc-as-underline">as-underline</a>
<a href="#proc-as-color">as-color</a> <a href="#proc-as-true-color">as-true-color</a>
<a href="#proc-on-red">on-red</a> <a href="#proc-on-blue">on-blue</a> <a href="#proc-on-green">on-green</a> <a href="#proc-on-cyan">on-cyan</a>
<a href="#proc-on-yellow">on-yellow</a> <a href="#proc-on-magenta">on-magenta</a> <a href="#proc-on-white">on-white</a> <a href="#proc-on-black">on-black</a>
<a href="#proc-on-color">on-color</a> <a href="#proc-on-true-color">on-true-color</a>
</pre>
<h2><a id="Types-and-Naming-Conventions">Types and Naming Conventions</a></h2>
<p>
We introduce two new types, <code>formatters</code>, which are
disjoint from any type except possibly procedures, and <code>state
variables</code>, which are distinct from any type except possibly
SRFI 39 parameters. These are in fact identical to the SRFI 165
computations and computation environment variables, respectively,
though knowledge of SRFI 165 is not required to use this SRFI.
<p>
In the prototypes below the following naming conventions imply type
restrictions:
<ul>
<li><var>fmt</var>: either a formatter, or a string or char coerced to a formatter with <code>displayed</code>
<li><var>formatter</var>: a formatter
<li><var>mapper</var>: a procedure of one argument which returns a formatter
<li><var>num</var>: a number
<li><var>state-var</var>: a state variable
<p>
The naming of formatters and mappers is generally chosen such that
they read as adjectives or adverbs describing how the objects they
act on are formatted. This provides a natural reading of the code,
and allows for a simple mapping between standard operations and
their formatting counterparts:
<li><code>write</code>: <code>written</code>
<li><code>display</code>: <code>displayed</code>
<li><code>string-pad</code>: <code>padded</code>
<li><code>string-trim</code>: <code>trimmed</code>
<li><code>string-join</code>: <code>joined</code>
</ul>
<h2><a id="Specification">Specification</a></h2>
<p>
The SRFI is divided into a core implementation and three utility
libraries, which could be defined portably in terms of the core but
are provided as convenience extensions. The libraries are as follows:
<pre>
(srfi 166) ; composite of all of the following
(srfi 166 base) ; all bindings not in one of the following
(srfi 166 pretty) ; all bindings in <a href="#Pretty-Printing">Pretty Printing</a>
(srfi 166 columnar) ; all bindings in <a href="#Columnar-Formatting">Columnar Formatting</a>
(srfi 166 unicode) ; all bindings in <a href="#Unicode">Unicode</a>
(srfi 166 color) ; all bindings in <a href="#Formatting-with-Color">Formatting with Color</a>
</pre>
<h3><a id="Usage">Usage</a></h3>
<dl>
<dt>(<a id="proc-show"><code class="proc-def">show</code></a> <var>output-dest</var> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<p>
The entry point for all formatting. Applies the <var>fmt</var> formatters in
sequence, accumulating the output to <var>output-dest</var>. As with SRFI
28 <code>format</code>, <var>output-dest</var> can be an output port, <code>#t</code> to
indicate the current output port, or <code>#f</code> to accumulate the
output into a string and return that as the result of <code>show</code>.
<p>
Each <var>fmt</var> should be a formatter as discussed below. As a
convenience, non-formatter arguments are also allowed and are
formatted as if wrapped with <code>displayed</code>, described below, so
that
<pre class="code-example">
(show #f "π = " (with ((precision 2)) (acos -1)) nl)
</pre>
would return the string <code>"π = 3.14\n"</code>.
<p>
As mentioned, formatters are an opaque type and cannot directly be
applied outside of <code>show</code>. Custom formatters are built on the
existing formatters, and as first-class objects may be named or
computed dynamically, so that:
<pre class="code-example">
(let ((~.2f (lambda (x) (with ((precision 2)) x))))
(show #f "π = " (~.2f (acos -1)) nl))
</pre>
produces the same result. For typical uses you only need to combine
the existing high-level formatters described in the succeeding
sections, but see the section <a href="#Higher-Order-Formatters-and-State">Higher Order Formatters and State</a>
for control flow and state-manipulation primitives.
<p>
The return value of <code>show</code> is the accumulated string if
<var>output-dest</var> is <code>#f</code> and unspecified otherwise.
</dd></dl>
<h3><a id="Formatting-Objects">Formatting Objects</a></h3>
<dl>
<dt>(<a id="proc-displayed"><code class="proc-def">displayed</code></a> <var>obj</var>)
<dd class="proc-def">
<p>
If <var>obj</var> is a formatter, returns <var>obj</var> as is.
Otherwise, outputs <var>obj</var> using <code>display</code> semantics.
Specifically, strings are output as if by <code>write-string</code> and
characters are written as if by <code>write-char</code>. Other objects
are output as with <code>written</code> (including nested strings and chars
inside <var>obj</var>). This is the default behavior for top-level formats
in <code>show</code>, <code>each</code> and most other high-level formatters.
<p>
It is an error if <var>obj</var> is a procedure which is not a formatter.
</dd>
</dl>
<dl>
<dt>(<a id="proc-written"><code class="proc-def">written</code></a> <var>obj</var>)
<dd class="proc-def">
<p>
Outputs <var>obj</var> using <code>write</code> semantics. Uses the current
<code>numeric</code> formatting settings to the extent that the written
result can still
be passed to <code>read</code>, possibly with loss of precision.
Specifically,
the current <var>radix</var> is used if set to any of 2, 8, 10 or 16, and the
fixed-point <var>precision</var> is used if specified and the
<var>radix</var> is 10.
<pre class="code-example">
(show #f (written (cons 0 1)))
=> "(0 . 1)"
</pre>
<pre class="code-example">
(show #f 1.5 " " (with ((precision 0)) 1.5))
=> "1.5 2"
</pre>
<pre class="code-example">
(show #f 1/7 " " (with ((precision 3)) 1/7)
" " (with ((precision 20)) 1/7))
=> "1/7 0.143 0.14285714285714285714"
</pre>
<p>
Implementations should allow arbitrary precision for exact rational
numbers. For example:
<pre class="code-example">
(show #f (with ((precision 50)) 1/3))
=> "0.33333333333333333333333333333333333333333333333333"
</pre>
<p>
As a less obvious example, using <code>string-segment</code> from
SRFI 152, the following code returns the first 100 Fibonacci
numbers:
<pre class="code-example">
(map string->number
(string-segment
(show #f (with ((precision 2500))
(/ 1000 (- #e1e50 #e1e25 1))))
25))
</pre>
<p>
If you don't know the type of an object and want to print it out
for debugging purposes, you should always wrap it with <code>written</code>
or one of its variants, in case the object is itself a formatter.
Note that, for debugging, a convenient idiom is to wrap the
object(s) in a quasiquote list:
<pre class="code-example">
(define (add x y)
(show #t `(add x: ,x y: ,y) nl)
(+ x y))
</pre>
</dd>
<dt>(<a id="proc-written-shared"><code class="proc-def">written-shared</code></a> <var>obj</var>)
<dd class="proc-def">
<p>
Like <code>written</code>, but using data labels for shared structures among all pairs
and vectors, analogous to <code>write-shared</code>.
</dd>
<dt>(<a id="proc-written-simply"><code class="proc-def">written-simply</code></a> <var>obj</var>)
<dd class="proc-def">
<p>
Like <code>written</code>, but doesn't handle shared structures, analogous to
<code>write-simply</code>. Infinite loops can still be avoided if
used inside a formatter that truncates data (see
<code>trimmed-lazy</code> below).
</dd>
<dt>(<a id="proc-escaped"><code class="proc-def">escaped</code></a> <var>str</var> <var>[quote-ch</var> <var>esc-ch</var> <var>renamer]</var>)
<dd class="proc-def">
<p>
Outputs the string <var>str</var>, escaping any quote or escape characters.
If <var>esc-ch</var>, which defaults to <code>#\\</code>, is <code>#f</code>,
escapes only the <var>quote-ch</var>, which defaults to <code>#\"</code>,
by doubling it, as in SQL strings and CSV values. If <var>renamer</var> is
provided, it should be a procedure of one character which maps that
character to its escape value, e.g. <code>#\newline => #\n</code>,
or <code>#f</code> if there is no escape value.
<pre class="code-example">
(show #f (escaped "hi, bob!"))
=> "hi, bob!"
(show #f (escaped "hi, \"bob!\""))
=> "hi, \"bob!\""
</pre>
</dd>
<dt>(<a id="proc-maybe-escaped"><code class="proc-def">maybe-escaped</code></a> <var>str</var> <var>pred</var> <var>[quote-ch</var> <var>esc-ch</var> <var>renamer]</var>)
<dd class="proc-def">
<p>
Like <code>escaped</code>, but first checks if any quoting is required (by
the existence of either any quote or escape characters, or any
character matching <code>pred</code>), and if so outputs the string in
quotes and with escapes. Otherwise outputs the string as is. This
is useful for quoting symbols and CSV output, etc.
<pre class="code-example">
(show #f (maybe-escaped "foo" char-whitespace? #\"))
=> "foo"
</pre>
<pre class="code-example">
(show #f (maybe-escaped "foo bar" char-whitespace? #\"))
=> "\"foo bar\""
</pre>
<pre class="code-example">
(show #f (maybe-escaped "foo\"bar\"baz" char-whitespace? #\"))
=> "\"foo\"bar\"baz\""
</pre>
</dd></dl>
<h3><a id="Formatting-Numbers">Formatting Numbers</a></h3>
<dl>
<dt>(<a id="proc-numeric"><code class="proc-def">numeric</code></a> <var>num</var> <var>[radix</var> <var>precision</var> <var>sign-rule</var> <var>comma-rule</var> <var>comma-sep</var> <var>decimal-sep]</var>)
<dd class="proc-def">
<p>
Formats a single number <var>num</var>. You can optionally specify any <var>radix</var>
from 2 to 36 (even if <var>num</var> isn't an integer). <var>precision</var> forces a
fixed-point format.
<p>
A <var>sign-rule</var> of <code>#t</code> indicates to output a plus sign (+) for
positive integers. However, if <var>sign-rule</var> is a pair of two strings, it
means to wrap negative numbers with the two strings. For example,
<code>("(" . ")")</code> prints negative numbers in parentheses, financial
style: <code>-1.99 => (1.99)</code>.
<p>
<var>comma-rule</var> is an integer specifying the number of digits
between commas, or a list of integers representing the number of
digits between each successive comma, with the first being the least
significant digits and the last repeating.
<p>
<var>comma-sep</var> is the character to use for commas, defaulting to
<code>#\,</code>.
<p>
<var>decimal-sep</var> is the character to use for decimals, defaulting
to <code>#\.</code>, or to <code>#\,</code> (European style) if <var>comma-sep</var> is
already <code>#\.</code>.
<p>
These parameters may seem unwieldy, but they can also take their
defaults from state variables, described below, if any are omitted.
</dd>
<dt>(<a id="proc-numeric_2fcomma"><code class="proc-def">numeric/comma</code></a> <var>num</var> <var>[comma-rule</var> <var>radix</var> <var>precision</var> <var>sign-rule]</var>)
<dd class="proc-def">
<p>
Shortcut for <code>numeric</code> to print with commas.
<pre class="code-example">
(show #f (numeric/comma 123456789))
=> "123,456,789"
(show #f (numeric/comma 123456789 2))
=> "1,23,45,67,89"
(show #f (numeric/comma 123456789 '(3 2)))
=> "12,34,56,789"
</pre>
</dd>
<dt>(<a id="proc-numeric_2fsi"><code class="proc-def">numeric/si</code></a> <var>num</var> <var>[base</var> <var>separator]</var>)
<dd class="proc-def">
<p>
Abbreviates <var>num</var> with an SI suffix as in the -h or --si
option to many GNU commands. The base defaults to 1000, using
suffix names k, M, G, etc. If the base is 1024, the suffixes are
Ki, Mi, Gi, etc. (note the capital "Ki" in this case). It is an
error to specify a base other than 1000 or 1024. If
<var>separator</var> is provided, it is inserted after the number,
before any suffix, for example to allow a space.
<pre class="code-example">
(show #f (numeric/si 608))
=> "608"
</pre>
<pre class="code-example">
(show #f (numeric/si 608) "B")
=> "608B"
</pre>
<pre class="code-example">
(show #f (numeric/si 608 1000 " ") "B")
=> "608 B"
</pre>
<pre class="code-example">
(show #f (numeric/si 3986))
=> "4k"
</pre>
<pre class="code-example">
(show #f (numeric/si 3986 1024) "B")
=> "3.9KiB"
</pre>
<pre class="code-example">
(show #f (numeric/si 1.23e-6) "m")
=> "1.2µm"
</pre>
<pre class="code-example">
(show #f (numeric/si 1.23e-6 1000 " ") "m")
=> "1.2 µm"
</pre>
<p>
See <a href="https://en.wikipedia.org/wiki/Metric_prefix">https://en.wikipedia.org/wiki/Metric_prefix</a> for the complete
list of abbreviations.
</dd>
<dt>(<a id="proc-numeric_2ffitted"><code class="proc-def">numeric/fitted</code></a> <var>width</var> <var>n</var> <var>.</var> <var>args</var>)
<dd class="proc-def">
<p>
Like <code>numeric</code>, but if the result doesn't fit in
<var>width</var> using the current <code>precision</code>, output
instead a string of hashes rather than showing an incorrectly
truncated number. For example
<pre class="code-example">
(show #f (with ((precision 2)) (numeric/fitted 4 1.25)))
=> "1.25"
</pre>
<pre class="code-example">
(show #f (with ((precision 2)) (numeric/fitted 4 12.345)))
=> "#.##"
</pre>
<pre class="code-example">
(show #f (with ((precision 0)) (numeric/fitted 2 123.45)))
=> "##"
</pre>
</dd>
</dl>
<h3><a id="Formatting-Space">Formatting Space</a></h3>
<dl>
<dt><a id="proc-nl"><code class="proc-def">nl</code></a>
<dd class="proc-def">
<p>
Outputs a newline.
<pre class="code-example">
(show #f nl)
=> "\n"
</pre>
</dd>
<dt><a id="proc-fl"><code class="proc-def">fl</code></a>
<dd class="proc-def">
<p>
Short for "fresh line," outputs a newline only if we're not already
at the start of a line.
<pre class="code-example">
(show #f fl)
=> ""
</pre>
<pre class="code-example">
(show #f "hi" fl)
=> "hi\n"
</pre>
<pre class="code-example">
(show #f "hi" nl fl)
=> "hi\n"
</pre>
</dd>
<dt>(<a id="proc-space-to"><code class="proc-def">space-to</code></a> <var>column</var>)
<dd class="proc-def">
<p>
Outputs spaces up to the given <var>column</var>. If the current column is
already >= <var>column</var>, does nothing. The character used for spacing
is the current value of <var>pad-char</var>, described below, which defaults
to space. Columns are zero-based.
<pre class="code-example">
(show #f "a" (space-to 5) "b")
=> "a b"
</pre>
<pre class="code-example">
(show #f "a" (space-to 0) "b")
=> "ab"
</pre>
</dd>
<dt>(<a id="proc-tab-to"><code class="proc-def">tab-to</code></a> <var>[tab-width]</var>)
<dd class="proc-def">
<p>
Outputs spaces up to the next tab stop, using tab stops of width
<var>tab-width</var>, which defaults to 8. If already on a tab stop,
does nothing. If you want to ensure you always tab at least one
space, you can use <code>(each " " (tab-to width))</code>. Columns
are zero-based.
<pre class="code-example">
(show #f (tab-to 5) "b")
=> "b"
</pre>
<pre class="code-example">
(show #f "a" (tab-to 5) "b")
=> "a b"
</pre>
<pre class="code-example">
(show #f "abcdefghi" (tab-to 5) "b")
=> "abcdefghi b"
</pre>
</dd>
<dt><a id="proc-nothing"><code class="proc-def">nothing</code></a>
<dd class="proc-def">
<p>
Outputs nothing (useful in combinators and as a default noop in
conditionals).
<pre class="code-example">
(show #f "a" nothing "b")
=> "ab"
</pre>
</dd></dl>
<h3><a id="Concatenation">Concatenation</a></h3>
<dl>
<dt>(<a id="proc-each"><code class="proc-def">each</code></a> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<p>
Applies each <var>fmt</var> in sequence, as in the top-level of show.
<pre class="code-example">
(show #f (each "a" "b"))
=> "ab"
</pre>
</dd>
<dt>(<a id="proc-each-in-list"><code class="proc-def">each-in-list</code></a> <var>list-of-fmts</var>)
<dd class="proc-def">
<p>
Equivalent to <code>(apply each list-of-fmts)</code> but may be more efficient.
</dd>
<dt>(<a id="proc-joined"><code class="proc-def">joined</code></a> <var>mapper</var> <var>list</var> <var>[sep]</var>)
<dd class="proc-def">
<p>
Formats each element <var>elt</var> of <var>list</var> with <code>(mapper elt)</code>,
inserting <var>sep</var> in between. <var>sep</var> defaults to the empty string, but
can be any format or string.
<pre class="code-example">
(show #f (joined displayed '(a b c) ", "))
=> "a, b, c"
</pre>
</dd>
<dt>(<a id="proc-joined_2fprefix"><code class="proc-def">joined/prefix</code></a> <var>mapper</var> <var>list</var> <var>[sep]</var>)
<dd class="proc-def"></dd>
<dt>(<a id="proc-joined_2fsuffix"><code class="proc-def">joined/suffix</code></a> <var>mapper</var> <var>list</var> <var>[sep]</var>)
<dd class="proc-def">
<pre class="code-example">
(show #f (joined/prefix displayed '(usr local bin) "/"))
=> "/usr/local/bin"
</pre>
<pre class="code-example">
(show #f (joined/suffix displayed '(1 2 3) nl))
=> "1\n2\n3\n"
</pre>
Like <code>joined</code>, but inserts <var>sep</var> before/after every element.
</dd>
<dt>(<a id="proc-joined_2flast"><code class="proc-def">joined/last</code></a> <var>mapper</var> <var>last-mapper</var> <var>list</var> <var>[sep]</var>)
<dd class="proc-def">
<p>
Like <code>joined</code>, but the last element of the list is formatted with
<var>last-mapper</var> instead.
<pre class="code-example">
(show #f (joined/last displayed
(lambda (last) (each "and " last))
'(lions tigers bears)
", "))
=> "lions, tigers, and bears"
</pre>
</dd>
<dt>(<a id="proc-joined_2fdot"><code class="proc-def">joined/dot</code></a> <var>mapper</var> <var>dot-mapper</var> <var>list</var> <var>[sep]</var>)
<dd class="proc-def">
<p>
Like <code>joined</code>, but if the list is a dotted list, then formats the
dotted value with <var>dot-mapper</var> instead.
<pre class="code-example">
(show #f
"("
(joined/dot displayed
(lambda (dot) (each ". " dot))
'(1 2 . 3)
" ")
")")
=> "(1 2 . 3)"
</pre>
</dd>
<dt>(<a id="proc-joined_2frange"><code class="proc-def">joined/range</code></a> <var>mapper</var> <var>start</var> <var>[end</var> <var>sep]</var>)
<dd class="proc-def">
<p>
Like <code>joined</code>, but counts from <var>start</var> (inclusive) to <var>end</var>
(exclusive), formatting each integer in the range with <var>mapper</var>. If
<var>end</var> is <code>#f</code> or unspecified, produces an infinite stream of
output.
<pre class="code-example">
(show #f (joined/range displayed 0 5 " "))
=> "0 1 2 3 4"
</pre>
</dd></dl>
<h3><a id="Padding-and-Trimming">Padding and Trimming</a></h3>
Formatters for ensuring output expands to, does not exceed, or fits
exactly within a specified width. Width is measured with the
<code>string-width</code> state variable, and trimming is then done by
calling <code>substring/width</code> state variable on the desired left
and right <i>widths</i>. The default values for these are
<code>string-length</code> and <code>substring</code>, indicating the
widths are are equivalent to string indexes, however this may have
different semantics as in <code>terminal-aware</code> discussed below,
and other extensions could be imagined such as enforcing trimming on
word boundaries.
<dl>
<dt>(<a id="proc-padded"><code class="proc-def">padded</code></a> <var>width</var> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<dt>(<a id="proc-padded_2fright"><code class="proc-def">padded/right</code></a> <var>width</var> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<dt>(<a id="proc-padded_2fboth"><code class="proc-def">padded/both</code></a> <var>width</var> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<p>
Analogs of SRFI-13 <code>string-pad</code>, these add extra space to the
left, right or both sides of the output generated by the <var>fmt</var>s
to pad it to <var>width</var>. If <var>width</var> is exceeded, has no effect.
<code>padded/both</code> will include one more extra space on the right
side of the output if the difference is odd.
<p>
<code>padded/right</code> is guaranteed not to accumulate any intermediate
data.
<p>
The padding can be controlled with the <code>pad-char</code> state
variable described below, defaulting to space.
<p>
Note these are column-oriented padders, so won't necessarily work
with multi-line output (padding doesn't seem a likely operation for
multi-line output).
<pre class="code-example">
(show #f (padded 5 "abc"))
=> " abc"
</pre>
<pre class="code-example">
(show #f (padded/right 5 "abc"))
=> "abc "
</pre>
<pre class="code-example">
(show #f (padded/both 5 "abc"))
=> " abc "
</pre>
</dd>
<dt>(<a id="proc-trimmed"><code class="proc-def">trimmed</code></a> <var>width</var> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<dt>(<a id="proc-trimmed_2fright"><code class="proc-def">trimmed/right</code></a> <var>width</var> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<dt>(<a id="proc-trimmed_2fboth"><code class="proc-def">trimmed/both</code></a> <var>width</var> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<p>
Analogs of SRFI-13 <code>string-trim</code>, truncates the output of the
<var>fmt</var>s to force it in under <var>width</var> columns.
<code>trimmed</code> truncates on the left, <code>trimmed/right</code>
on the right, and <code>trimmed/both</code> truncates on both the left
and right, truncating 1 more on the right if the <var>width</var> isn't
even.
If <var>width</var> is not exceeded, is equivalent to <code>each</code>.
<p>
If a truncation <var>ellipsis</var> is set, then when any truncation occurs,
<code>trimmed</code> and <code>trimmed/right</code> will prepend and append the
ellipsis, respectively. <code>trimmed/both</code> will both prepend and
append. The length of the <var>ellipsis</var> will be considered when
truncating the original string, so that the total width will never
be longer than <var>width</var>. It is an error if <var>width</var> is
less than the length of <var>ellipsis</var>, or double the length for /both.
<p>
If the state variable <code>substring/preserve</code> is not
<code>#f</code>, then this is called on the left and/or right
portions of the output which have been excluded by trim, and the
result of this is output in place of the trimmed text. The default
is <code>#f</code> (do nothing), but it can be useful to override
this to preserve control sequences which are zero-width regardless
but affect the state of the output stream. In particular
<code>substring-terminal/preserve</code> as enabled in
<code>terminal-aware</code> preserves ANSI control sequences and
bidirectional overrides.
<p>
For example, consider
<pre class="code-example">
(show #f (with ((ellipsis "…")) (trimmed/both 5 "abcdef")))
=> "…bcd…"
</pre>
Here we note that the output width of 6 exceeds the requested width
of 5 and trimming must be done. Since the ellipsis width is 1, we
must split the trimming to remove one character from the left and
two from the right. Thus, the output is computed as:
<pre class="code-example">
(let ((str "abcdef"))
(each (substring/preserve (substring/width str -1 1))
ellipsis
(substring/width str 1 4)
ellipsis
(substring/preserve (substring/width str 4 6))))
</pre>
<p>
Additional examples:
<pre class="code-example">
(show #f (trimmed 5 "abcde"))
=> "abcde"
</pre>
<pre class="code-example">
(show #f (trimmed 5 "abcdef"))
=> "bcdef"
</pre>
<pre class="code-example">
(show #f (trimmed/right 5 "abcdef"))
=> "abcde"
</pre>
<pre class="code-example">
(show #f (trimmed/both 5 "abcdef"))
=> "abcde"
</pre>
<pre class="code-example">
(show #f (trimmed/both 4 "abcdef"))
=> "bcde"
</pre>
<pre class="code-example">
(show #f (with ((ellipsis "...")) (trimmed 5 "abcdef")))
=> "...ef"
</pre>
<pre class="code-example">
(show #f (with ((ellipsis "...")) (trimmed/right 5 "abcdef")))
=> "ab..."
</pre>
<pre class="code-example">
(show #f (with ((ellipsis "_")) (trimmed/both 5 "abcdef")))
=> "_bcd_"
</pre>
<pre class="code-example">
(show #f (trimmed/right 2 "日本語"))
=> "日本"
</pre>
<pre class="code-example">
(show #f (terminal-aware (trimmed/right 2 "日本語")))
=> "日"
</pre>
</dd>
<dt>(<a id="proc-trimmed_2flazy"><code class="proc-def">trimmed/lazy</code></a> <var>width</var> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<p>
A variant of <code>trimmed</code> which generates each <var>fmt</var> in
left-to-right order, and truncates and terminates
immediately if more than <var>width</var> characters are generated.
It does not output <var>ellipsis</var>.
Thus this is safe to use with an infinite amount of output,
e.g. from <code>written-simply</code> on an infinite list.
</dd>
<dt>(<a id="proc-fitted"><code class="proc-def">fitted</code></a> <var>width</var> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<dt>(<a id="proc-fitted_2fright"><code class="proc-def">fitted/right</code></a> <var>width</var> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<dt>(<a id="proc-fitted_2fboth"><code class="proc-def">fitted/both</code></a> <var>width</var> <var>fmt</var> <var>...</var>)
<dd class="proc-def">
<p>
A combination of <code>padded</code> and <code>trimmed</code>, ensures the output
width is exactly <var>width</var>, truncating if it goes over and padding if
it goes under.
</dd></dl>
<h3><a id="Pretty-Printing">Pretty Printing</a></h3>
The homoiconic nature of Lisp makes pretty printing an indispensable
utility for presenting code in a readable format.
<dl>
<dt>(<a id="proc-pretty"><code class="proc-def">pretty</code></a> <var>obj</var>)
<dd class="proc-def">
<p>
Pretty-prints <var>obj</var>. The result should be identical to
<code>written</code> except possibly for differences in whitespace to make
the output resemble formatted source code. Implementations should
print vectors and data lists (lists that don't begin with a (nested)
symbol) in a tabular format when possible to reduce vertical space.
As with <code>written</code>, cyclic structure must be detected and
represented with datum labels.</p>
</dd>
<dt>(<a id="proc-pretty-shared"><code class="proc-def">pretty-shared</code></a> <var>obj</var>)
<dd class="proc-def">
<p>
The same as <code>pretty</code> but using data labels for shared structures among all pairs
and vectors, analogous to <code>write-shared</code>.
</dd>
<dt>(<a id="proc-pretty-simply"><code class="proc-def">pretty-simply</code></a> <var>obj</var>)
<dd class="proc-def">
<p>
The same as <code>pretty</code>, but without using any datum labels.
</dd>
<dt>(<a id="proc-pretty-with-color"><code class="proc-def">pretty-with-color</code></a> <var>obj</var>)
<dd class="proc-def">
<p>
Equivalent to <code>pretty</code>, but may optionally include ANSI control