Common Lisp/Printer/Formatted Output

In order to format a string with several optional values for inclusion into the output, rather than building the string with several strcat calls it can be made much simpler by defining the CL format function. The following explains this function.

format <function>

Takes a string and a list of values for inclusion into the string at specified positions and formatting.


This function has 3 required arguments:

  • destination - The destination to which the output must be sent.
    • Can be nil, in which case the formatted string is simply returned to the calling environment.
    • If this is T then the output needs to be sent to the standard output as well as returned to the calling environment. Since AutoLisp doesn't have a "standard output" (per say), it will be printed to the command line through a call to princ.
    • If this is a file object it will be appended to the file through a call to princ. The result will still be passed to the calling environment.
  • control-string - This is the original string including formatting codes as explained below.
  • args - A list containing the values to be included in the output.

Control String Codes

The control string should contain control codes in the positions where the values need to be input into the string. Each of these codes will be prefixed with a tilde [~] character. The codes are case insensitive, thus ~C has the same meaning as ~c. For a complete listing and description of these codes see the Formatted Output/Codes page

Code thus far

The AutoLisp implementation of the format function.