Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print a string in Emacs lisp with ielm?

I'd like to print a string in ielm. I don't want to print the printed representation, I want the string itself. I'd like this result:

ELISP> (some-unknown-function "a\nb\n")
a
b
ELISP>

I can't see any way to do this. The obvious functions are print and princ, but these give me the printable representation:

ELISP> (print "* first\n* second\n* third\n")
"* first\n* second\n* third\n"

I've played with pp and pp-escape-newlines, but these still escape other characters:

ELISP> (setq pp-escape-newlines nil)
nil
ELISP> (pp "a\n")
"\"a
\""

Is this possible? For inspecting large strings, message doesn't cut it.

like image 506
Wilfred Hughes Avatar asked Dec 27 '22 01:12

Wilfred Hughes


2 Answers

How about inserting directly into the buffer?

(defun p (x) (move-end-of-line 0) (insert (format "\n%s" x)))

That gets you:

ELISP> (p "a\nb\n")
a
b

nil
ELISP> 

EDIT: Use format to be able to print things other than strings.

like image 88
Sean Avatar answered Jan 06 '23 04:01

Sean


;;; Commentary:

;; Provides a nice interface to evaluating Emacs Lisp expressions.
;; Input is handled by the comint package, and output is passed
;; through the pretty-printer.

IELM uses (pp-to-string ielm-result) (so binding pp-escape-newlines has an effect in general), but if you want to bypass pp altogether then IELM doesn't provide for that, so I suspect Sean's answer is your best option.

ELISP> (setq pp-escape-newlines nil)
nil
ELISP> "foo\nbar"
"foo
bar"
like image 35
phils Avatar answered Jan 06 '23 03:01

phils