Search code examples
syntaxemacscharacterelisp

What does ? mean in (insert "hello" ?\s "world" ?\n)


The functions insert's example is demonstrated as:

(with-temp-buffer
  (insert "hello" ?\s "world" ?\n)
  (buffer-string))

What does ? mean here?


Solution

  • ?\s and ?\n are read syntax for characters. The first is read syntax for the space character; the second for the character NEWLINE (also called Control J, and which sometimes appears as ^J, where the ^J is a newline character.

    In Emacs Lisp characters are positive integers. They can be written as numerals for their ASCII codes (values) and they can be read by the Lisp reader when written that way: 32 (for \s) and 10 (for \n). But Elisp also offers a character read syntax introduced by ?, and these two characters have their own ? syntax. Other, more ordinary characters can just be prefixed by ?: ?s is read as the character s, and ?@ is read as the character @.

    The ? read syntax is more readable for humans than using just integers: 115 (for ?s) and 64 for ?@. But you can always use just the integers -- characters are integers.

    Your example: (insert "hello" ?\s "world" ?\n). Function insert accepts strings and chars as its arguments. In this case you pass string "hello", character ?\s (space char), string "world", and character ?\n (newline char, Control J).

    See the Elisp manual, node Basic Char Syntax.