次: Output Functions, 前: Input Functions, 上: Read and Print
出力ストリームは表示で生成した文字群をどのように扱うかを指定します。 ほとんどの表示関数は省略可能な引数として出力ストリームを受け付けます。 出力ストリームとして使える型は以下のとおりです。
tnilnilを指定すると、
standard-outputの値をかわりに使うことを意味する。
その値はデフォルト出力ストリームであり、
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は与えられた文字を受け取り、
それをリストlast-outputの先頭にコンスします
(see Building Lists)。
終了後には、リストがすべての出力文字を保持していますが逆順です。
(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を呼び出してリストを文字列に変換し、
内容を読みやすいようにしました。