[plt-scheme] encoding: latin1 and utf

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun May 1 00:53:02 EDT 2005

At Thu, 28 Apr 2005 14:38:11 -0300, Pedro Kröger wrote:
> how can I use latin1 strings with plt scheme 299.100?

If you simply want MzScheme (or other stdio-based tools) to work with
Latin-1-encoded stdio, then

 (require (lib "port.ss"))
  (reencode-input-port (current-input-port) "" #"?" #t))
 (let ([re-out (lambda (port buffer-mode)
                 (reencode-output-port port "" #"?" #t 
                  (object-name port) buffer-mode))])
   (current-output-port (re-out (current-output-port) 'line))
   (current-error-port (re-out (current-error-port) 'none)))

may do the trick.

If you mean to have Latin-1-encoded program text to be processed by
various PLT tools, I see one possibility, at least with v299.104. You
could prefix any S-expression --- especially a module --- with

 #reader "latin-1-reader.ss"

where the module for "latin-1-reader.ss" is below. (You might put it in
a collection and use a `lib' path instead of a literal string after

I'm not sure I like this approach, but it works with all tools that
process a program only through S-expressions. It doesn't work for
tools, like DrScheme's editor, that directly manipulate the text of a

BTW, you're using readline, right? The error message in

>  (display "não\n")
> I get:
> bytes->string/utf-8: string is not a well-formed UTF-8 encoding: #"(display 
> \"n\343o\\n\")"

is a readline problem that we'll work on.



(module latin-1-reader mzscheme
  (require (lib "port.ss"))

  (define (convert-port port)
    (let ([re-port (reencode-input-port port "LATIN-1")])
      ;; If line counting was on, enable line counting on
      ;;  the new port, and shift it's location to match
      ;;  the current location of the original port
      (let-values ([(l c p) (port-next-location port)])
	(if (and l c p)
	      (port-count-lines! re-port)
	      (let ([re-port (relocate-input-port re-port l c p)])
		(port-count-lines! re-port)

  (define (my-read port)
    (read (convert-port port)))

  (define (my-read-syntax name port)
    (read-syntax name (convert-port port)))
  (provide (rename my-read read)
	   (rename my-read-syntax read-syntax)))

Posted on the users mailing list.