输出流用于指定如何处理打印产生的字符。 大多数打印函数接受一个输出流作为可选参数。 输出流的可能类型如下:
输出字符会插入到 buffer 的光标位置(point)处。 随着字符插入,光标位置会向前移动。
输出字符会插入到 marker 所指向的缓冲区中,且位于标记的位置处。 标记位置会随字符插入向前移动。 当流为标记类型时,缓冲区中的光标位置值对打印无影响, 且此类打印操作不会移动光标(除非标记指向光标位置或其之前, 此时光标会像往常一样随周围文本向前移动)。
输出字符会传递给 function,由该函数负责存储这些字符。 该函数会以单个字符为参数被调用(输出多少字符就调用多少次), 并负责将字符存储到你指定的任意位置。
t ¶输出字符会显示在回显区(echo area)中。 若 Emacs 以批处理模式运行(see Batch Mode), 则输出会写入标准输出描述符(standard output descriptor)。
nil ¶指定 nil 作为输出流表示使用 standard-output 变量的值;
该值为 默认输出流(default output stream),且不能为 nil。
作为输出流的符号等价于该符号的函数定义(若存在)。
许多有效的输出流同时也可作为输入流使用。 因此,输入流与输出流的区别更多在于如何使用 Lisp 对象,而非对象类型的不同。
以下是将缓冲区用作输出流的示例。 初始时光标位于 ‘the’ 中 ‘h’ 之前的位置。 最终光标仍位于该 ‘h’ 之前的位置。
---------- Buffer: foo ---------- This is t∗he contents of foo. ---------- Buffer: foo ----------
(print "This is the output" (get-buffer "foo"))
⇒ "This is the output"
---------- Buffer: foo ---------- This is t "This is the output" ∗he contents of foo. ---------- Buffer: foo ----------
接下来展示将标记用作输出流的用法。
初始时,标记位于缓冲区 foo 中单词 ‘the’ 的 ‘t’ 和 ‘h’ 之间。
最终,标记会越过插入的文本向前移动,使其仍位于原 ‘h’ 之前。
注意,光标位置(按常规方式显示)对此无影响。
---------- Buffer: foo ---------- This is the ∗output ---------- Buffer: foo ----------
(setq m (copy-marker 10))
⇒ #<marker at 10 in foo>
(print "More output for foo." m)
⇒ "More output for foo."
---------- Buffer: foo ---------- This is t "More output for foo." he ∗output ---------- Buffer: foo ----------
m
⇒ #<marker at 34 in foo>
以下示例展示输出到回显区的用法:
(print "Echo Area output" t)
⇒ "Echo Area output"
---------- Echo Area ----------
"Echo Area output"
---------- Echo Area ----------
最后展示将函数用作输出流的用法。
函数 eat-output 接收传入的每个字符,并将其 cons 到列表 last-output 的前端(see 构建 cons 单元与列表)。
最终,该列表包含所有输出的字符,但顺序为逆序。
(setq last-output nil)
⇒ nil
(defun eat-output (c)
(setq last-output (cons c last-output)))
⇒ eat-output
(print "This is the output" #'eat-output)
⇒ "This is the output"
last-output
⇒ (10 34 116 117 112 116 117 111 32 101 104
116 32 115 105 32 115 105 104 84 34 10)
我们可通过反转列表将输出恢复为正确顺序:
(concat (nreverse last-output))
⇒ "
\"This is the output\"
"
调用 concat 可将列表转换为字符串,便于清晰查看其内容。
该函数在调试时可用作输出流,它会将 character 写入标准错误流(standard error stream)。
例如:
(print "This is the output" #'external-debugging-output) ⊣ This is the output ⇒ "This is the output"