Elisp: Character Type
What is Char Type
In emacs lisp, characters are represented as integers of the character's code point.
For example, the char “a” in elisp is just 97, because its code point is 97.
There is no dedicated char type
There is no actual character type in emacs lisp. Whether a integer is a character depends on programer's intention.
Readable Char Syntax
- Char can also be represented like this
?afor easy human reading. ?ameans the character “a”.?aeval to 97.
- Some characters, such as backslash, require a backslash. e.g.
?\\
You can also represent char by
(string-to-char "a")
(eq 97 ?a ) ;; t (eq 97 (string-to-char "a")) ;; t
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 | code point | 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〕
some other important special case
- SEMICOLON need backslash, else it became lisp comment. Correct syntax is
?\; - QUOTATION MARK need backslash, else it became start of string. Correct syntax is
?\"
- CIRCUMFLEX ACCENT cannot precede with backslash because it become control char. Correct syntax is
?^
Find a Char's code point Interactively
call
describe-char
Insert Char
insert-char-
(insert-char CHARACTER &optional COUNT INHERIT)insert character by its code point.
(insert-char 97 3) ;; aaa
Get Char from Buffer
char-before-
(char-before &optional POS)return the code point of character before cursor. If POS is out of range, return nil.
char-after-
return the code point of character after cursor. If POS is out of range, return nil.
preceding-char-
(preceding-char)return the code point of character before cursor. If point is at the beginning of the buffer, returns 0.
following-char-
(following-char)return the code point of character after cursor. If at the end of the buffer or accessible region, return 0.
Convert Char and String
char-to-string-
(char-to-string CHAR)convert a CHAR (code point) 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 code point)
string-
(string &rest CHARACTERS)convert chars to string.
(string 97) ;; "a" (string 97 98) "ab"
Equality Test, Check is Char or String
char-equal-
(char-equal C1 C2).Return
tif 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-beforereturnnil. Usingchar-equalresult error because argument needs to be integer. So, in general, better useeqif you don't care about letter case. 〔see Elisp: Equality Test〕;; check if char before is a newline (char-equal (char-before) 10) ;; depends on case-fold-search for letter characters ;; better use eq for single case characters, because char-before return nil if point is at beginning, and char-equal would error because it expect integer (eq (char-before) 10) char-or-string-p-
check if object is char or string.
Replace Chars
translate-regionsubst-char-in-stringsubst-char-in-region