GNU Emacs 32.0.50 (9008bf64c3) のマニュアルを PLaMo 翻訳で機械翻訳したものです。誤訳を含むことがあります。原文: https://www.gnu.org/software/emacs/manual/html_node/elisp/Output-Streams.html ・ emacs-doc.conao3.com

20.4 出力ストリーム

出力ストリームは、印刷によって生成された文字をどのように処理するかを指定します。ほとんどの印刷関数は、オプション引数として出力ストリームを受け取ります。出力ストリームとして指定可能な型は以下の通りです。

buffer

出力文字は、ポイント位置の buffer に挿入されます。文字が挿入されるにつれて、ポイントが進みます。

marker

出力文字は、marker が指すバッファのマーカー位置に挿入されます。文字が挿入されるにつれて、マーカー位置は前進します。ストリームがマーカーである場合、バッファ内の point の値は出力に影響せず、この種の出力では point は移動しません(ただし、マーカーが point の位置またはそれより前の位置を指している場合は、通常通り、point は周囲のテキストとともに前進します)。

function

The output characters are passed to function, which is responsible for storing them away. It is called with a single character as argument, as many times as there are characters to be output, and is responsible for storing the characters wherever you want to put them.

t

出力文字はエコー領域に表示されます。Emacs がバッチモードで動作している場合(バッチモードを参照)、出力は代わりに標準出力ディスクリプタに書き出されます。

nil

出力ストリームとして nil が指定された場合は、代わりに standard-output 変数の値を使用することになります。その値は デフォルトの出力ストリーム であり、nil であってはなりません。

symbol

出力ストリームとしてのシンボルは、そのシンボルの関数定義(もしあれば)と同等である。

有効な出力ストリームの多くは、入力ストリームとしても有効です。したがって、入力ストリームと出力ストリームの違いは、オブジェクトの種類というよりも、むしろ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 ----------

ここでは、マーカーを出力ストリームとして使用する例を示します。最初、マーカーは単語 ‘the’ 内の ‘t’ と ‘h’ の間にあるバッファ foo に位置しています。最後には、マーカーが挿入されたテキストを越えて移動し、同じ ‘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 の先頭に cons します(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 を呼び出すとリストが文字列に変換されるため、内容をより明確に確認できます。

関数: external-debugging-output character

この関数は、デバッグ時の出力ストリームとして有用です。標準エラーストリームに character を書き込みます。

例えば

(print "This is the output" #'external-debugging-output)
⊣ This is the output
⇒ "This is the output"