When Emacs shows an integer value in the minibuffer, or as an output to IELM, it shows three representations of it -- octal, hex and as character:
ELISP> 10
10 (#o12, #xa, ?\C-j)
This is really nice, and I'd like to use something like that in my programs. Now, while I can use format
to show an integer as octal (with %o
) or with hex (with %x
), I don't know how to get the nice formatted character as the above. %c
will actually print the character without escaping, and will, for example, print a line feed for 10.
I have tried the print-escape-*
variables, without success:
ELISP> (let ((print-escape-newlines t)
(print-escape-nonascii t)
(print-escape-multibyte t)
(print-escape-control-characters t))
(concat "char: " (string 10)))
"char:
"
ELISP> (let ((print-escape-newlines t)
(print-escape-nonascii t)
(print-escape-multibyte t)
(print-escape-control-characters t))
(format "char: %s" (string 10)))
"char:
"
ELISP> (let ((print-escape-newlines t)
(print-escape-nonascii t)
(print-escape-multibyte t)
(print-escape-control-characters t))
(format "char: %s" 10))
"char: 10"
ELISP> (let ((print-escape-newlines t)
(print-escape-nonascii t)
(print-escape-multibyte t)
(print-escape-control-characters t))
(format "char: %c" 10))
"char:
"
What I want is to get a string with the escaped character, to later print with princ
.
I've tried looking into the source for IELM and the minibuffer, but they seem to delegate printing to other parts of Emacs, and I couldn't find how it's done.
How can I get those printed the way I described?