On this page:
typeset-code
code
current-code-font
current-code-tt
current-code-line-sep
current-comment-color
current-keyword-color
current-id-color
current-literal-color
current-const-color
current-base-color
current-reader-forms
code-align
current-keyword-list
current-const-list
current-literal-list
mzscheme-const-list
code-colorize-enabled
code-colorize-quote-enabled
code-italic-underscore-enabled
code-scripts-enabled
define-code
define-exec-code
define-exec-code/ scale
comment-color
keyword-color
id-color
literal-color
code-pict-bottom-line-pict
pict->code-pict

4 Typesetting Racket Code

The slideshow/code library provides utilities for typesetting Racket code as a pict.

(typeset-code stx)  pict?
  stx : syntax?
Produces a pict for code in the given syntax object. The source-location information of the syntax object determines the line breaks, line indenting, and space within a row. Empty rows are ignored.

Beware that if you use read-syntax on a file port, you may have to turn on line counting via port-count-lines! for the code to typeset properly. Also beware that when a source file containing a syntax or quote-syntax form is compiled, source location information is omitted from the compiled syntax object.

Normally, typeset-code is used through the code syntactic form, which works properly with compilation, and that escapes to pict-producing code via unsyntax. See also define-code.

Embedded picts within stx are used directly. Row elements are combined using and operator like htl-append, so use code-align (see below) as necessary to add an ascent to ascentless picts. More precisely, creation of a line of code uses pict-last to determine the end point of the element most recently added to a line; the main effect is that closing parentheses are attached in the right place when a multi-line pict is embedded in stx.

An identifier that starts with _ is italicized in the pict, and the _ is dropped, unless the code-italic-underscore-enabled parameter is set to #f. Also, unless code-scripts-enabled is set to #f, _ and ^ in the middle of a word create superscripts and subscripts, respectively (like TeX); for example foo^4_ok is displayed as the identifier foo with a 4 superscript and an ok subscript.

Further, uses of certain identifiers in stx typeset specially:

(code datum ...)
The macro form of typeset-code. Within a datum, unsyntax can be used to escape to an expression.

For more information, see typeset-code and define-code, since code is defined as

  (define-code code typeset-code)

Parameter for a base font used to typeset text. The default is `(bold . modern). For even more control, see current-code-tt.

(current-code-tt)  (string? . -> . pict?)
(current-code-tt proc)  void?
  proc : (string? . -> . pict?)
Parameter for a one-argument procedure to turn a string into a pict, used to typeset text. The default is

  (lambda (s) (text s (current-code-font) (current-font-size)))

This procedure is not used to typeset subscripts or other items that require font changes, where current-code-font is used directly.

A parameter that determines the spacing between lines of typeset code. The default is 2.

A parameter for the color of comments.

A parameter for the color of syntactic-form names. See current-keyword-list.

(current-id-color)  (or/c string? (is-a?/c color%))
(current-id-color color)  void?
  color : (or/c string? (is-a?/c color%))
A parameter for the color of identifiers that are not syntactic form names or constants.

A parameter for the color of literal values, such as strings and numbers. See also current-literal-list

A parameter for the color of constant names. See current-const-list.

A parameter for the color of everything else.

Parameter for a list of symbols indicating which built-in reader forms should be used. The default is ''quasiquote. Remove a symbol to suppress the corresponding reader output.

(code-align pict)  pict?
  pict : pict?
Adjusts the ascent of pict so that its bottom aligns with the baseline for text; use this function when pict has no ascent.

A list of strings to color as syntactic-form names. The default includes most of the forms provided by racket/base.

(current-const-list)  (listof string?)
(current-const-list names)  void?
  names : (listof string?)
A list of strings to color as constant names. The default is null.

A list of strings to color as literals, in addition to literals such as strings. The default is null.

A list of strings that could be used to initialize the current-const-list parameter.

(code-colorize-enabled)  boolean?
(code-colorize-enabled on?)  void?
  on? : any/c
A parameter to enable or disable all code coloring. The default is #t.

A parameter to control whether under a quote is colorized as a literal (as in this documentation). The default is #t.

A parameter to control whether _-prefixed identifiers are italicized (dropping the _). The default is #t.

(code-scripts-enabled)  boolean?
(code-scripts-enabled on?)  void?
  on? : any/c
A parameter to control whether TeX-style subscripts and subscripts are recognized in an identifier.

(define-code code-id typeset-code-id)
(define-code code-id typeset-code-id escape-id)
Defines code-id as a macro that uses typeset-code-id, which is a function with the same input as typeset-code. The escape-id form defaults to unsyntax.

The resulting code-id syntactic form takes a sequence of datums:

  (code-id datum ...)

It produces a pict that typesets the sequence. Source-location information for the datum determines the layout of code in the resulting pict. The code-id is expanded in such a way that source location is preserved during compilation (so typeset-code-id receives a syntax object with source locations intact).

If a datum contains (escape-id expr) – perhaps as #,expr when escape-id is unsyntax – then the expr is evaluated and the result datum is spliced in place of the escape-id form in datum. If the result is not a syntax object, it is given the source location of the escape-id form. A pict value intected this way as a datum is rendered as itself.

(define-exec-code (pict-id runnable-id string-id)
  datum ...)
Binds pict-id to the result of (code datum ...), except that if an identifier _ appears anywhere in a datum, then the identifier and the following expression are not included for code.

Meanwhile, runnable-id is bound to a syntax object that wraps the datums in a begin. In this case, _s are removed from the datums, but not the following expression. Thus, an _ identifier is used to comment out an expression from the pict, but have it present in the syntax object for evaluation.

The string-id is bound to a string representation of the code that is in the pict. This string is useful for copying to the clipboard with (send the-clipboard set-clipboard-string string-id 0).

(define-exec-code/scale scale-expr (pict-id runnable-id string-id)
  datum ...)
Like define-exec-code, but with a scale to use via scale/improve-new-text when generating the pict.

For backward compatibility, the default values for current-comment-color, etc.

(code-pict-bottom-line-pict pict)  (or/c pict? #f)
  pict : pict?
The same as pict-last, provided for backward compatibility.

(pict->code-pict pict bl-pict)  pict?
  pict : pict?
  bl-pict : (or/c pict? #f)
Mainly for backward compatibility: returns (if bl-pict (use-last pict (or (pict-last bl-pict) bl-pict))).