Next: , Previous: Near Point, Up: Text


31.2 バッファの内容を調べる

本節では、Lispプログラムでバッファ内の任意の部分のテキストを文字列に変換する ための2つの関数について述べます。

— Function: buffer-substring start end

この関数は、カレントバッファのstartendの位置で定義される 領域のテキストのコピーを含んだ文字列を返す。 引数がバッファの参照可能部分の内側の位置でないと、 buffer-substringはエラーargs-out-of-rangeを通知する。

startendより小さい必要はなく、引数の順番はどちらでもよい。 しかし、ほとんどの場合、小さい引数を先に書く。

コピーされるテキストにテキスト属性がある場合、 テキスト属性もそれが属する文字とともに文字列へコピーされる。 see Text Properties。 しかし、バッファのオーバレイ(see Overlays)とそれらの属性は 無視されコピーされない。

          ---------- Buffer: foo ----------
          This is the contents of buffer foo
          
          ---------- Buffer: foo ----------
          
          (buffer-substring 1 10)
           "This is t"
          (buffer-substring (point-max) 10)
           "he contents of buffer foo
          "
— Function: buffer-substring-no-properties start end

この関数はbuffer-substringと同様であるが、 テキスト属性をコピーせずに文字だけをコピーする点が異なる。 see Text Properties

— Function: buffer-string

この関数は、カレントバッファの参照可能部分全体の内容を文字列として返す。 これは、つぎと等価である。

          (buffer-substring (point-min) (point-max))
          ---------- Buffer: foo ----------
          This is the contents of buffer foo
          
          ---------- Buffer: foo ----------
          
          (buffer-string)
                "This is the contents of buffer foo
          "
— Function: thing-at-point thing

ポイントの周りやそのうしろにあるthingを文字列として返す。

引数thingは、構文上の要素の種類を指定するシンボルである。 可能な値は、symbollistsexpdefunfilenameurlwordsentencewhitespacelinepageなどである。

          ---------- Buffer: foo ----------
          Gentlemen may cry ``Pea-!-ce! Peace!,''
          but there is no peace.
          ---------- Buffer: foo ----------
          
          (thing-at-point 'word)
                "Peace"
          (thing-at-point 'line)
                "Gentlemen may cry ``Peace! Peace!,''\n"
          (thing-at-point 'whitespace)
                nil