Subject: Re: reading symbols preserving case
From: rpw3@rpw3.org (Rob Warnock)
Date: Thu, 02 Apr 2009 06:07:47 -0500
Newsgroups: comp.lang.lisp
Message-ID: <kMidneQb1uUeAUnUnZ2dnUVZ_hSdnZ2d@speakeasy.net>
Francisco Vides Fernández  <fvides@dedaloingenieros.com> wrote:
+---------------
| I'm trying to write something like this:
| (case-preserving-context
|   (a list of symbols))
| => 
| (|a| |list| |of| |symbols|)
...
| Which basically setf the readtable-case of *readtable* to :preserve ...
| but I'd like something cleaner, if possible. Some suggestions?
+---------------

Others have given you good answers, but depending on exactly what
you're trying to accomplish, you might want to take a close look
at :INVERT rather than :PRESERVE. Assuming your source code is all
lowercase, you can (setf (readtable-case *readtable*) :invert) and
*leave* it that way, and still preserve CamelCase:

    > (readtable-case *readtable*)

    :UPCASE
    > 'foo

    FOO
    > (setf (readtable-case *readtable*) :invert)

    :invert
    > 'foo

    foo
    > '(A List Of Symbols such as FOO & BAR)

    (A List Of Symbols such as FOO & BAR)
    > (mapcar #'symbol-name *)	; Show the "real" internal case

    ("a" "List" "Of" "Symbols" "SUCH" "AS" "foo" "&" "bar")
    > (eq 'foo (intern "FOO"))	; So lowercase source code works

    t
    > (eq 'FOO (intern "FOO"))	;   but uppercase source doesn't,

    nil
    > (eq 'FOO (intern "foo"))	;   unless you really do want lowercase names.

    t
    >

When readtable-case is :INVERT, the printer also inverts on output
by default. If all you need is read/print invariance of case, :INVERT
may be your friend.


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607