How to print a string in Emacs lisp using ielm?

I want to print a line in ielm. I do not want to print the printed view, I want the string itself. I need this result:

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

I see no way to do this. The obvious functions are print and princ , but they give me a printed representation:

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

I played with pp and pp-escape-newlines , but they still come out of other characters:

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

Is it possible? To check for large strings, message does not cut it.

+4
source share
3 answers

How to paste directly into the buffer?

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

This will help you:

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

EDIT: use format to print objects other than lines.

+7
source
 ;;; 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 the pp-escape-newlines has an effect at all), but if you want to get around pp at all, IELM does not provide for this, so I suspect Sean's answer is your best option .

 ELISP> (setq pp-escape-newlines nil) nil ELISP> "foo\nbar" "foo bar" 
+2
source

@Sean's answer is correct if you want to display the string as part of your session.

However, you say you want to check for large strings. An alternative approach would be to put the string in a separate window. You can use with-output-to-temp-buffer for this. For instance:

 (with-output-to-temp-buffer "*string-inspector*" (print "Hello, world!") nil) 

A new window will appear (or if it already exists, its output will be changed). It is in help mode, so it is read-only and can be closed with q .

If you want to make even more complex stuff in your output buffer, you can use with-temp-buffer-window instead:

 (with-temp-buffer-window "*string-inspector*" #'temp-buffer-show-function nil (insert "hello, world!!")) 
+1
source

Source: https://habr.com/ru/post/1486324/


All Articles