如何在Emacs Lisp中使用ielm打印字符串?

4
我想在ielm中打印一个字符串。我不想打印已经被表示的内容,我想要这个字符串本身。我希望得到以下结果:
ELISP> (some-unknown-function "a\nb\n")
a
b
ELISP>

我看不出有任何方法可以做到这一点。显然的函数是printprinc,但它们只会给我可打印的表示形式:

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

我玩过pppp-escape-newlines,但它们仍然会转义其他字符:
ELISP> (setq pp-escape-newlines nil)
nil
ELISP> (pp "a\n")
"\"a
\""

这可行吗?对于检查大型字符串,message并不足够。
3个回答

8
直接插入缓冲区怎么样?
(defun p (x) (move-end-of-line 0) (insert (format "\n%s" x)))

这将为您带来以下结果:
ELISP> (p "a\nb\n")
a
b

nil
ELISP> 

编辑:使用format可以打印除字符串以外的其他内容。


聪明的技巧,我喜欢它! :) - James Porter

2
;;; 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使用(pp-to-string ielm-result)(因此绑定pp-escape-newlines通常会产生影响),但如果您想完全绕过pp,那么IELM不提供该功能,因此我认为Sean的答案是最佳选择。

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

1

如果您想将字符串作为会话的一部分显示,@Sean的答案是正确的。

然而,您说您想检查大字符串。另一种方法是将字符串放在单独的窗口中。您可以使用with-output-to-temp-buffer来实现这一点。例如:

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

一个新窗口将弹出(如果已经存在,则其输出将被更改)。它处于帮助模式下,因此是只读的,可以用q关闭。
如果您想在输出缓冲区中进行一些更复杂的操作,可以使用with-temp-buffer-window,如下所示:
(with-temp-buffer-window "*string-inspector*"
                         #'temp-buffer-show-function
                         nil
  (insert "hello, world!!"))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接