summaryrefslogtreecommitdiff
path: root/devdocs/elisp/basic-char-syntax.html
blob: d6c9c2cec4027977ab7ab00cf0f987eca0b53a25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 <h4 class="subsubsection">Basic Char Syntax</h4>      <p>Since characters are really integers, the printed representation of a character is a decimal number. This is also a possible read syntax for a character, but writing characters that way in Lisp programs is not clear programming. You should <em>always</em> use the special read syntax formats that Emacs Lisp provides for characters. These syntax formats start with a question mark. </p> <p>The usual read syntax for alphanumeric characters is a question mark followed by the character; thus, ‘<samp>?A</samp>’ for the character <kbd>A</kbd>, ‘<samp>?B</samp>’ for the character <kbd>B</kbd>, and ‘<samp>?a</samp>’ for the character <kbd>a</kbd>. </p> <p>For example: </p> <div class="example"> <pre class="example">?Q ⇒ 81     ?q ⇒ 113
</pre>
</div> <p>You can use the same syntax for punctuation characters. However, if the punctuation character has a special syntactic meaning in Lisp, you must quote it with a ‘<samp>\</samp>’. For example, ‘<samp>?\(</samp>’ is the way to write the open-paren character. Likewise, if the character is ‘<samp>\</samp>’, you must use a second ‘<samp>\</samp>’ to quote it: ‘<samp>?\\</samp>’. </p>                    <p>You can express the characters control-g, backspace, tab, newline, vertical tab, formfeed, space, return, del, and escape as ‘<samp>?\a</samp>’, ‘<samp>?\b</samp>’, ‘<samp>?\t</samp>’, ‘<samp>?\n</samp>’, ‘<samp>?\v</samp>’, ‘<samp>?\f</samp>’, ‘<samp>?\s</samp>’, ‘<samp>?\r</samp>’, ‘<samp>?\d</samp>’, and ‘<samp>?\e</samp>’, respectively. (‘<samp>?\s</samp>’ followed by a dash has a different meaning—it applies the Super modifier to the following character.) Thus, </p> <div class="example"> <pre class="example">?\a ⇒ 7                 ; <span class="roman">control-g, <kbd>C-g</kbd></span>
?\b ⇒ 8                 ; <span class="roman">backspace, <tt class="key">BS</tt>, <kbd>C-h</kbd></span>
?\t ⇒ 9                 ; <span class="roman">tab, <tt class="key">TAB</tt>, <kbd>C-i</kbd></span>
?\n ⇒ 10                ; <span class="roman">newline, <kbd>C-j</kbd></span>
?\v ⇒ 11                ; <span class="roman">vertical tab, <kbd>C-k</kbd></span>
?\f ⇒ 12                ; <span class="roman">formfeed character, <kbd>C-l</kbd></span>
?\r ⇒ 13                ; <span class="roman">carriage return, <tt class="key">RET</tt>, <kbd>C-m</kbd></span>
?\e ⇒ 27                ; <span class="roman">escape character, <tt class="key">ESC</tt>, <kbd>C-[</kbd></span>
?\s ⇒ 32                ; <span class="roman">space character, <tt class="key">SPC</tt></span>
?\\ ⇒ 92                ; <span class="roman">backslash character, <kbd>\</kbd></span>
?\d ⇒ 127               ; <span class="roman">delete character, <tt class="key">DEL</tt></span>
</pre>
</div>  <p>These sequences which start with backslash are also known as <em>escape sequences</em>, because backslash plays the role of an escape character; this has nothing to do with the character <tt class="key">ESC</tt>. ‘<samp>\s</samp>’ is meant for use in character constants; in string constants, just write the space. </p> <p>A backslash is allowed, and harmless, preceding any character without a special escape meaning; thus, ‘<samp>?\+</samp>’ is equivalent to ‘<samp>?+</samp>’. There is no reason to add a backslash before most characters. However, you must add a backslash before any of the characters ‘<samp>()[]\;"</samp>’, and you should add a backslash before any of the characters ‘<samp>|'`#.,</samp>’ to avoid confusing the Emacs commands for editing Lisp code. You should also add a backslash before Unicode characters which resemble the previously mentioned <acronym>ASCII</acronym> ones, to avoid confusing people reading your code. Emacs will highlight some non-escaped commonly confused characters such as ‘<samp>‘</samp>’ to encourage this. You can also add a backslash before whitespace characters such as space, tab, newline and formfeed. However, it is cleaner to use one of the easily readable escape sequences, such as ‘<samp>\t</samp>’ or ‘<samp>\s</samp>’, instead of an actual whitespace character such as a tab or a space. (If you do write backslash followed by a space, you should write an extra space after the character constant to separate it from the following text.) </p><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
    <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Basic-Char-Syntax.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Basic-Char-Syntax.html</a>
  </p>
</div>