[plt-scheme] Trying to reliably turn a value into a list of snips

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Tue May 27 22:38:04 EDT 2008

Hi everyone,

I've been trying to write a module that takes a value and returns its 
representation as a list of snips.  It sorta works, except when it 
doesn't.  Here's the code I have so far:

(module value-snips mzscheme
   (require (lib "framework.ss" "framework")
            (lib "mred.ss" "mred")
            (lib "class.ss")
            (lib "contract.ss"))

   (provide/contract [value-snips (any/c . -> . (listof (is-a?/c snip%)))])

   ;; value-snips: value -> (listof snip)
   (define (value-snips a-value)
     (let ([es (make-eventspace)])
       (parameterize ([current-eventspace es])
         (let ([ch (make-channel)])
           (thread (lambda ()
                     (define a-text (new (text:ports-mixin 
                     (let ([port (send a-text get-out-port)])
                       (write a-value port)
                       (flush-output port)
                        (reverse (get-snips/rev a-text 0 (send a-text 
           (let ([result (sync ch)])

   (define (get-snips/rev text start end)
     (send text split-snip start)
     (send text split-snip end)
     (let loop ([snips/rev '()]
                 (send text find-snip start 'after-or-none)])
         [(or (not a-snip)
              (>= (send text get-snip-position a-snip)
          (loop (cons (send a-snip copy) snips/rev)
                (send a-snip next))]))))

Unfortunately, this breaks when I hit things like picts.  picts aren't 
snips.  It looks like they get converted implicitly by the REPL in 
DrScheme.  I see that picts are registered to the snip converter through 

I haven't yet found a good way of getting at that function without having 
to integrate tightly with DrScheme.  I suspect I need to use the 
render-value/format functions that work with DrScheme, but I hesitate in 
doing so: it means I can't easily test my module.  At the very least, I 
don't know of an effective way of testing a tool that depends on DrScheme.

Any suggestions would be greatly appreciated!

Posted on the users mailing list.