13.8 Printer Extension (original) (raw)
13.8 Printer Extension🔗ℹ
A write-proc method takes three arguments: the structure to be printed, the target port, and an argument that is #t for write mode, #f fordisplay mode, or 0 or 1 indicating the current quoting depth for print mode. The procedure should print the value to the given port usingwrite, display, print, fprintf,write-special, etc.
The port write handler, port display handler, and print handler are specially configured for a port given to a custom-write procedure. Printing to the port through display, write, or printprints a value recursively with sharing annotations. To avoid a recursive print (i.e., to print without regard to sharing with a value currently being printed), print instead to a string or pipe and transfer the result to the target port using write-string orwrite-special. To print recursively to a port other than the one given to the custom-write procedure, copy the given port’s write handler, display handler, and print handler to the other port.
The port given to write-proc is not necessarily the actual target port. In particular, to detect cycles, sharing, and quoting modes (in the case of print), the printer invokes a custom-write procedure with a port that records information about recursive prints, and does not retain any other output. This information-gathering phase needs the same objects (in theeq? sense) to be printed as later, so that the recorded information can be correlated with printed values.
Recursive print operations may trigger an escape from a call towrite-proc. For example, printing may escape during pretty-printing where a tentative print attempt overflows the line, or it may escape while printing error output that is constrained to a limited width.
The following example definition of a tuple type includes a write-proc procedure that prints the tuple’s list content using angle brackets in write and print mode and no brackets indisplay mode. Elements of the tuple are printed recursively, so that graph and cycle structure can be represented.
Examples:
The make-constructor-style-printer function can help in the implementation of a write-proc, as in this example:
Examples:
(require racket/struct) > (print (point 1 2)) (point 1 2) > (write (point 1 2)) #<point: 1 2>
Changed in version 8.7.0.5 of package base: Added a check so that omittingwrite-proc is now a syntax error.
Returns #t if v has the prop:custom-writeproperty, #f otherwise.
Returns the custom-write procedure associated with v.
A property and associated predicate and accessor. The property value is one of 'self, 'never, 'maybe, or'always. When a structure has this property in addition to aprop:custom-write property value, then the property value affects printing in print mode; see The Printer. When a value does not have the prop:custom-print-quotable, it is equivalent to having the 'self property value, which is suitable both for self-quoting forms and printed forms that are unreadable.