[racket] current-output encoding
On Mon, Apr 1, 2013 at 12:31 AM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> At Tue, 26 Mar 2013 18:29:00 +0100, Pierpaolo Bernardi wrote:
> > On Tue, Mar 26, 2013 at 5:25 PM, Pierpaolo Bernardi <olopierpa at gmail.com>
> wrote:
> > > On Tue, Mar 26, 2013 at 5:00 PM, Matthew Flatt <mflatt at cs.utah.edu>
> wrote:
> > >
> > >> I think you want `reencode-output-port', probably using "" (which
> > >> represents the user's default encoding) as the target encoding.
> > >
> > > I tried the following:
> > >
> > > ====
> > > #lang racket
> > >
> > > (current-output-port (reencode-output-port (current-output-port) ""))
> > >
> > > (display "à")
> > > ====
> > >
> > > but this displays nothing both in DrRacket and in the console.
> > > Also "program >tempfile" from the console produces a file of length 0.
> >
> > Adding a (flush-output) after the display makes it work from inside
> DrRacket.
> >
> > In the console there's no change, and redirecting the output to a file
> > still produces a 0-length file, though.
>
> Ok, while `reencode-output-port' might be the right direction, there
> are several complications:
>
> * On Windows, Racket uses UTF-8 for "". So, reencoding to "" doesn't
> do anything.
>
> That's the right choice for DrRacket, through, since DrRacket's
> interaction window uses UTF-8.
>
> * In a Windows console, the right encoding depends on how the console
> is configured. Use `chcp' to find out your code page. It will be a
> number N, and then the locale you want is "Windows-N".
>
I discovered that this would be too simple for microsoft. 8^)
The codepage that my system uses is 850, and the magic word is "ibm850"
(from:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx
)
In the end, this works in my console:
====
#lang racket
(current-output-port
(reencode-output-port (current-output-port) "ibm850"))
(display "à")
(flush-output)
====
But then, of course, it does not work in the DrRacket listener.
You could also set your console to UTF-8 with `chcp 65001', and then
> the console and DrRacket will work the same (as long as you also pick a
> suitable font in the console).
>
This would be the most rational solution (even microsoft agrees, in the
page above!), but after 'chcp 65001' the console acts weirdly (lines are
skipped at random, strings are displayed twice...), and it seems there's
not an easy way to change the default console cp (here there are some
infos, but not very reassuring:
http://superuser.com/questions/269818/change-default-code-page-of-windows-console-to-utf-8
)
It seems that for now is still better to stick to ASCII if we need to write
to the console. :(
Thanks!
P.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130405/378ec336/attachment.html>