1

我运行以下代码来打印i暂存缓冲区和 ielm repl 中的 15 个连续值:

    (defvar i 0)
    (while (< i 15)
       (print i)
       (setq i (+ i 1)))`

我在暂存缓冲区和 repl 中注意到的是,它们都只显示了 sexp 的结果值。然后将打印的值i发送到Messages缓冲区。

  1. 至少对于 repl,我怎样才能得到irepl 中打印的值?
  2. 如果您有其他对您有效的解决方案,请告诉我!

请注意,我通过终端和 Ubuntu 12.04 LTS 使用 emacs 24.3。感谢所有的帮助!

此外,根据print我们的文档:

print is a built-in function in `C source code'.

(print OBJECT &optional PRINTCHARFUN)

Output the printed representation of OBJECT, with newlines around it.
Quoting characters are printed when needed to make output that `read'
can handle, whenever this is possible.  For complex objects, the behavior
is controlled by `print-level' and `print-length', which see.

OBJECT is any of the Lisp data types: a number, a string, a symbol,
a list, a buffer, a window, a frame, etc.

A printed representation of an object is text which describes that object.

Optional argument PRINTCHARFUN is the output stream, which can be one
of these:

   - a buffer, in which case output is inserted into that buffer at point;
   - a marker, in which case output is inserted at marker's position;
   - a function, in which case that function is called once for each
     character of OBJECT's printed representation;
   - a symbol, in which case that symbol's function definition is called; or
   - t, in which case the output is displayed in the echo area.

If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
is used instead.
  1. 由于我对 lisp 的实际方面不熟悉,如何打印到不同的缓冲区?
4

2 回答 2

3

format这是我编写的 Emacs Lisp 的一个小扩展,其执行类似于 Common Lisp format,但%用作控制字符:

http://code.google.com/p/formatting-el/source/browse/trunk/formatting.el

因此,例如,如果您想将带有换行符的数字列表打印到字符串中,您可以这样做:

(cl-format "%{%s%^\n%}" (cl-loop for i from 0 below 10 collect i))
"0
1
2
3
4
5
6
7
8
9"

实现此目的的另一种方法是使用以下内容:

(mapconcat #'number-to-string (cl-loop for i from 0 below 10 collect i) "\n")
"0
1
2
3
4
5
6
7
8
9"
于 2013-08-25T10:18:00.640 回答
2

with-output-to-string如果你真的想要,你可以使用:

(with-output-to-string
    (setq i 0)
    (while (< i 15)
      (princ i)
      (setq i (+ i 1))))
于 2013-08-24T12:50:35.783 回答