[racket] slideshow/pict: typeset-code on programmatically generated syntax objects?

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Mon Sep 16 11:17:16 EDT 2013

I'm creating a picture of a program that has one form in it that I want to highlight. I'm reading the program in with read-syntax with parameterization [port-count-lines-enabled #t], and the form I want to introduce gets spliced in the middle after reading, as the result of the code macro.

This form is not on its own line; instead it appears right at the end of the previous form.
Question: How can I make it render on its own line?

The code follows, where proctime.rkt is just used as a long enough file to make my point visually.

(define (dinky) 
  (code (code:line)
        (code:line (define (dinky-helper-function x lst)
                     (map (λ (y) (- y x)) lst)))))

(define (a-program)
  (parameterize ([port-count-lines-enabled #t])
   (with-input-from-file "proctime.rkt"
     (λ ()
        (read-line)
        (define all-forms-rev
          (for/fold ([acc '()])
              ([form (in-port read-syntax)]
               [i (in-naturals)])
            (define stuff
              (list 'code
                    (syntax-line form)
                    (syntax-column form)
                    (syntax-position form)
                    (syntax-span form)))
            (cons (datum->syntax #f
                                 (list #'code:line form)
                                 stuff) acc)))
        (define len (length all-forms-rev))
        (typeset-code
         (datum->syntax
          #f
          (for/fold ([acc '()]) ([form (in-list all-forms-rev)]
                                 [i (in-naturals)])
            (cond
             [(= i (quotient len 2))
              (list* form (tag-pict (dinky) 'helper) acc)]
             [else (cons form acc)]))))))))


Posted on the users mailing list.