2013-08-24 108 views
1

我跑到下面的代碼在臨時緩衝區打印的i兩個15個連續值和ielm REPL:的Emacs Lisp打印輸出

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

我在這兩個臨時緩衝區和REPL注意到什麼,是他們都只顯示sexp的結果值。然後將i的打印值發送到Messages緩衝區。

  1. 至少對於repl來說,我怎樣才能得到打印在repl中的i的值?
  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. 由於我是新來的口齒不清的實際問題,我怎麼打印到不同的緩衝區?

回答

3

這裏有一個小小的擴展的Emacs Lisp format我寫來完成類似的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" 
+0

無法從我的'.emacs'文件加載common-lisp功能? – CodeKingPlusPlus

+1

@CodeKingPlusPlus我不太清楚你在問什麼...... Common Lisp是一種獨立的編程語言,然而,在Emacs Lisp中有一個'cl'包,它提供的功能類似於其原型中的功能。然而,這個軟件包沒有類似於Common Lisp的'format',我的'cl-format'功能是爲了補償這種遺漏。 – 2013-08-25 16:31:48

+0

這就是我所問的。謝謝! – CodeKingPlusPlus

2

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

(with-output-to-string 
    (setq i 0) 
    (while (< i 15) 
     (princ i) 
     (setq i (+ i 1)))) 
+0

如何將打印'i'的值之間的換行?有沒有某種格式的字符串? – CodeKingPlusPlus

+0

如果你想換行,使用'print'而不是'princ' –

+0

如果我這樣做,那麼'帶輸出到字符串'給我的值'i'與換行符連接'\ n' – CodeKingPlusPlus