0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Emacs Lisp 超初心者がコマンドを作ってみる 〜一時バッファーを使ってみる〜

Posted at

前回までで比較対象となる previous msgid (コメント欄の #|) と msgid をそれぞれバッファーに入れる事ができました。

しかし、 msgid は unquote してあるのに比べて previous msgid は 抜き出したままで、 これでは msgid との比較には使いにくいです。

そこで、 previous msgid も msgid と同様に unquote 等してみます。

処理の流れ

  1. 一時バッファーを作って previous msgid からコピー
  2. そこで処理した結果を文字列として返します

プログラム

(defun po-extract-previous-msgid (buffer start end)
  "Delete '#|' marker and unquote text from BUFFER START END.
delete '^#| ' each line.  then unquote.
return is String with property."
  (with-temp-buffer
    (insert-buffer-substring buffer start end)
    (goto-char (point-min))
    (while (re-search-forward "^#\\(~\\)?|[ \t]*" nil t)
      (replace-match "" t t))
    (po-extract-unquoted (current-buffer) (point-min) (point-max))
    )
  )

with-temp-buffer は with-current-buffer に似ていて、 with-current-buffer が指定のバッファーに対して、 with-temp-buffer は with-current-buffer 自身が勝手に一時的なバッファーを作り、そちらに切り替えます。 with-current-buffer を抜けると元に戻して一時的なバッファーは破棄(kill)します。

with-temp-buffer カレント・バッファーが切り替わり、 with-temp-buffer が勝手に作った謎のバッファーになっています。元のバッファーにアクセスするには、save-current-buffer や with-current-buffer と同様にそれらの外で oldbuf などとシンボルに代入(バインド)してから参照する必要があります。

実際の処理部分は正規表現マッチングで、マッチした部分を "" に置換しています

 (while (re-search-forward "^#\\(~\\)?|[ \t]*" nil t)
      (replace-match "" t t))

po-extract-unquoted は msgid の unquote をする関数で、上記正規表現置換でテキストは msgid と同じ形になっているのでコレを使います。

ためしに動かしてみる

前回で作成された pepm-previous-msgid バッファで試してみます。

M-: Eval:(po-extract-previous-msgid (current-buffer) (point-min) (point-max))

po-extract-previous-msgid.png

実行結果(参考)
result-po-extract-previous-msgid.png

とりあえず動いているようなのでヨシッ

今回はここまで。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?