Code to generate format functions. It is relatively flexible, supporting inheritance and allowing you to extend your formatters. It is accompanied by an implementation of Common Lisp's format function which is thread safe and extendible (meaning you could define new format characters).

Create and return a procedure that can be used as a format function. The resulting procedure receives a port, a format string and optional arguments. It parses the format string according to the formatters parameter and outputs a result to the port specified. If, instead of a port, #t is specified, the results will be sent to the current output port. If, on the other hand, #f is passed, the procedure will store its output on a new string and return it.

The returned procedure parses the format string looking for occurrences of the escape character (usually #\~); when one is found, a function from the formatters argument is called.

formatters is a list of formatters. Each formatter is itself a list of (char function) pairs, where char is the character for the escape sequence being defined and function the function to call when the character, preceeded by escape, is found in the format string.

To produce the functions included in the formatters, use (formatter-function proc), where proc is a procedure that receives the following parameters:

state

A structure with internal information. You won't normally inspect this directly but rather pass it to other functions that require it (such as out-char and out-string).

start

The position in the format string where the escape sequence was found. You'll normally just ignore this.

params

A list with paramters that occur between escape and char (for example, in a format string such as ~23,33,12A).

colon, atsign

Booleans indicating whether those characters occur between escape and char.

To output info at the current position, the formatter functions should use the out-char and out-string functions. The receive the state parameter (as passed to the formatter) and a character and string respectively.