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

5.2.5 insert-buffer における let 式

変数 buffer が単なるバッファ名ではなく、バッファそのものを参照していることを確認した上で、insert-buffer 関数は let 式へと進みます。ここでは3つのローカル変数 start、end、newmark を指定し、それらを初期値 nil にバインドします。これらの変数は let の残りの部分で使用され、let の終わりまで、Emacs 内で同名の他の変数の出現を一時的に隠します。

let の本体には、2つの save-excursion 式が含まれています。まず、内側の save-excursion 式を詳しく見ていきましょう。この式は次のようになっています。

(save-excursion
  (set-buffer buffer)
  (setq start (point-min) end (point-max)))

式 (set-buffer buffer) は、Emacs のフォーカスを現在のバッファから、テキストをコピー元とするバッファへと切り替えます。そのバッファでは、コマンド point-min と point-max を用いて、変数 start と end にバッファの先頭と末尾が設定されます。ここでは、setq が同一の式内で2つの変数を設定できる仕組みが示されている点に注目してください。setq の第1引数には第2引数の値が、第3引数には第4引数の値がそれぞれ設定されます。

内部のsave-excursionの本体が評価された後、save-excursionは元のバッファを復元しますが、startとendは、テキストがコピーされるバッファの先頭と末尾の値に設定されたままとなります。

外側の save-excursion 式は次のようになります。

(save-excursion
  (inner-save-excursion-expression
     (go-to-new-buffer-and-set-start-and-end)
  (insert-buffer-substring buffer start end)
  (setq newmark (point)))

The insert-buffer-substring function copies the text into the current buffer from the region indicated by start and end in buffer. Since the whole of the second buffer lies between start and end, the whole of the second buffer is copied into the buffer you are editing. Next, the value of point, which will be at the end of the inserted text, is recorded in the variable newmark.

外側のsave-excursionの本体が評価された後、pointは元の位置に再配置される。

ただし、新たに挿入されたテキストの末尾にマークを置き、ポイントをその先頭に配置すると便利です。newmark 変数は、挿入されたテキストの末尾を記録します。let 式の最終行において、(push-mark newmark) 式関数がこの位置にマークを設定します。(マークの以前の位置には依然としてアクセス可能であり、マークリングに記録されているため、C-u C-SPC でそこに戻ることができます。)一方、ポイントは挿入されたテキストの先頭に配置されます。そこは insert 関数を呼び出す前の位置であり、その位置は最初の save-excursion によって保存されています。

let 式全体は次のようになります。

(let (start end newmark)
  (save-excursion
    (save-excursion
      (set-buffer buffer)
      (setq start (point-min) end (point-max)))
    (insert-buffer-substring buffer start end)
    (setq newmark (point)))
  (push-mark newmark))

append-to-buffer関数と同様に、insert-buffer関数はlet、save-excursion、set-bufferを使用します。さらに、この関数はorの使い方の一例を示しています。これらの関数はすべて、繰り返し利用することになる構成要素です。