Skip to main content

readtable-case

readtable-case Accessor

Syntax:

readtable-case readtable → mode

(setf (readtable-case readtable) mode**)**

Arguments and Values:

readtable—a readtable.

mode—a case sensitivity mode.

Description:

Accesses the readtable case of readtable, which affects the way in which the Lisp Reader reads symbols and the way in which the Lisp Printer writes symbols.

Examples:

See Section 23.1.2.1 (Examples of Effect of Readtable Case on the Lisp Reader) and Section 22.1.3.3.2.1 (Examples of Effect of Readtable Case on the Lisp Printer). 

Exceptional Situations:

Should signal an error of type type-error if readtable is not a readtable. Should signal an error of type type-error if mode is not a case sensitivity mode.

See Also:

*readtable*, *print-escape*, Section 2.2 (Reader Algorithm), Section 23.1.2 (Effect of Readtable Case on the Lisp Reader), Section 22.1.3.3.2 (Effect of Readtable Case on the Lisp Printer)

Notes:

copy-readtable copies the readtable case of the readtable.

Expanded Reference: readtable-case

Querying the current readtable case

readtable-case returns the case sensitivity mode of a readtable. The standard readtable uses :upcase, which converts unescaped lowercase letters to uppercase during reading.

(readtable-case *readtable*)
=> :UPCASE

The four readtable case modes

The valid modes are :upcase, :downcase, :preserve, and :invert.

;; :UPCASE (default) - unescaped letters are uppercased
(let ((*readtable* (copy-readtable)))
(setf (readtable-case *readtable*) :upcase)
(read-from-string "hello"))
=> HELLO
=> 5

;; :DOWNCASE - unescaped letters are lowercased
(let ((*readtable* (copy-readtable)))
(setf (readtable-case *readtable*) :downcase)
(read-from-string "HELLO"))
=> |hello|
=> 5

;; :PRESERVE - letters are not changed
(let ((*readtable* (copy-readtable)))
(setf (readtable-case *readtable*) :preserve)
(symbol-name (read-from-string "Hello")))
=> "Hello"

;; :INVERT - if all unescaped letters in a token have the same case,
;; that case is inverted; mixed case is preserved
(let ((*readtable* (copy-readtable)))
(setf (readtable-case *readtable*) :invert)
(list (symbol-name (read-from-string "hello"))
(symbol-name (read-from-string "HELLO"))
(symbol-name (read-from-string "Hello"))))
=> ("HELLO" "hello" "Hello")

Setting readtable case with SETF

readtable-case is a setf-able accessor.

(let ((*readtable* (copy-readtable)))
(setf (readtable-case *readtable*) :preserve)
(readtable-case *readtable*))
=> :PRESERVE

Readtable case affects symbol printing too

The readtable case influences how the printer writes symbols, ensuring round-trip consistency.

(let ((*readtable* (copy-readtable)))
(setf (readtable-case *readtable*) :invert)
;; Standard symbols (all uppercase internally) print as lowercase
(format nil "~A" 'hello))
=> "hello"