[racket] Reading drracket files into text%

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Tue Aug 31 15:22:12 EDT 2010

On Tue, Aug 31, 2010 at 2:14 PM, Matthias Felleisen
<matthias at ccs.neu.edu> wrote:
>
> Would this help:
>
> ;; String (instance Text%) -> Void
> ;; load the content of file f into text editor t,
> ;; strip meta-data in first three lines
> (define (load f t)
>  (send t load-file f)
>  (define txt (send t get-text))
>  (define inp (open-input-string txt))

You can use open-input-text-editor here.

But even better to just use the paragraph methods of the text, ie:

  (send t get-text (send t paragraph-start-position 1) (send t
paragraph-end-position 1))

gets the second line of text in the editor.

>  (define fst (read-line inp))
>  (define res
>    (if (string<=? ";;" fst)
>        (call-with-output-string
>         (lambda (op)
>           (read-line inp)
>           (read-line inp)
>           (copy-port inp op)))
>        fst))
>  (send t select-all)
>  (send t delete)
>  (send t insert res))

This is where things break if there was an image in the file.

You can just use the delete method with number argument to remove
stuff from the file.

Robby

> No test, just a use case:
>
>  (define f (new frame% [label "hello world"][width 500][height 100]))
>  (define t (new text%))
>  (define e (new editor-canvas% [parent f][editor t]))
>  (load "intermediate.rkt" t)
>  (send f show #t))
>
>
>
>
> On Aug 31, 2010, at 2:49 PM, Ryan Golbeck wrote:
>
>> What's the proper way to read a file produced by DrRacket into a text% object?
>>
>> Using load-file produces problems when DrRacket has created a file
>> where the first three lines contain meta data information about the
>> language to use.
>>
>> Thanks,
>> -ryan
>> _________________________________________________
>>  For list-related administrative tasks:
>>  http://lists.racket-lang.org/listinfo/users
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>


Posted on the users mailing list.