[plt-scheme] Re: function for reading text from Scheme files

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Sun Nov 19 23:36:35 EST 2006

>    * This is not robust: I really would want to somehow get the editor%
>      that DrScheme is itself using, but haven't figured out how to pull
>      that out of Framework yet.

... Actually, I don't think we to do anything complicated after all! 
After more closely reading reading:

http://download.plt-scheme.org/doc/352/html/mred/mred-Z-H-623.html#node_sec_8.2.1

it sounds like we should be able to handle all different kinds of snips 
already, since the snips have a reliable encoding that auto-load their 
required snip classes.

Ah, in that case, it's just a matter of doing something like this:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module read-text mzscheme
   (require (lib "mred.ss" "mred")
            (lib "class.ss"))

   (provide read-text)

   ;; read-text: path -> string
   ;; Given a path, opens it and returns its text.  Images and other snips
   ;; are munged.
   (define (read-text path)
     (let ([editor (new text%)])
       (send editor load-file path)
       (send editor get-text))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Much simpler.  And if we wanted to get closer to the data, we can use 
OPEN-INPUT-GRAPHICAL-FILE which should be able to handle files with snips 
in them too.  My apologies about making things more complicated than they 
needed to be; I'm still learning the API.


Best of wishes!


Posted on the users mailing list.