1

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?

0

Try this: (format "(#o%o, #x%x, ?\\%s)" 10 10 (single-key-description 10))

You might need to use (princ (format "(#o%o, #x%x, ?\\%s)" 10 10 (single-key-description 10)), depending on what you're doing with it.

You can get this yourself by looking at the code that implements M-:: command eval-expression. You'll see that it uses this, for a character:

(eval-expression-print-format 10)

That's an equivalent answer to what I show above.

(To see eval-expression at work, step by step, you can use M-x debug-on-entry.)

| improve this answer | |
  • Thanks a lot, it works! – jcp Jul 2 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.