前回までで比較対象となる previous msgid (コメント欄の #|) と msgid をそれぞれバッファーに入れる事ができました。
しかし、 msgid は unquote してあるのに比べて previous msgid は 抜き出したままで、 これでは msgid との比較には使いにくいです。
そこで、 previous msgid も msgid と同様に unquote 等してみます。
処理の流れ
- 一時バッファーを作って previous msgid からコピー
- そこで処理した結果を文字列として返します
プログラム
(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))
とりあえず動いているようなのでヨシッ
今回はここまで。