Emacs Lisp: Character Type
In emacs lisp, character is represented as integer of the character's Codepoint .
For example, the char “a” in elisp is just 97, because its codepoint is 97.
Note: elisp “Character Type” is not technically a “type” of value in the sense of most programing languages, because there is no way to distinguish integer from char. There is no function that returns true/false on whether a value is a character type. Whether a integer is a character depends on programer's intention.
Char Syntax
Char can also be represented like this ?a
for easy reading. ?a
means the character “a”.
You can also represent char by
(string-to-char "a")
(equal 97 ?a ) ;; t (equal 97 (string-to-char "a")) ;; t
Find a Char's Codepoint
Useful Functions on Character
char-before
-
(char-before)
return the unicode codepoint (integer) of character before cursor. char-after
-
(char-after)
return the unicode codepoint (integer) of character after cursor. char-to-string
-
(char-to-string CHAR)
convert a CHAR (unicode codepoint (integer)) to string of single character. string-to-char
-
(string-to-char STRING)
return the first char in string. (return a integer that's the char's unicode codepoint) char-equal
-
(char-equal C1 C2)
.Return t if two characters match, but dependent on a Buffer Local Variable case-fold-search.
Case is ignored if case-fold-search is non-nil in the current buffer.
Warning: when at beginning of buffer,
char-before
return nil. Usingchar-equal
result error because argument needs to be integer. So, in general, better useeq
. [see Emacs Lisp: Test Equality];; check if char before is a newline (char-equal (char-before ) 10)
ASCII Control Chars and Backslash
Syntax of the form ?\char
may have special meaning, depending what char is.
They either represent a ASCII control character, or just the character char.
For example, ?\n
is the newline char. ?\\
is backslash char.
Here's a list of special meaning with the backslash:
syntax | codepoint | name | input |
---|---|---|---|
?\a | 7 | bell | C-g |
?\b | 8 | backspace | C-h |
?\t | 9 | horizontal | C-i |
?\n | 10 | line feed | C-j |
?\v | 11 | vertical tab | C-k |
?\f | 12 | formfeed | C-l |
?\r | 13 | carriage return | C-m |
?\e | 27 | escape | C-[ |
?\s | 32 | space | SPC |
?\\ | 92 | backslash | \ |
?\d | 127 | delete character | DEL |
[see ASCII Characters]