LoginSignup
3
3

More than 5 years have passed since last update.

句読点の一括変換をEmacsで

Posted at

ふだんの句読点は「,.」ですが,本から書き出して,テキストデータにしたり,ブログで引用したりする際には,句読点を原文に合わせるようにしています.
句読点の組み合わせは,「、。」「,。」「,.」の3種類です.「、.」は見かけません.それと,句点は「。」「.」,読点は「、」「,」を指しますが,「テン・マル」「カンマ・マル」「カンマ・ピリオド」で呼び慣れているので,読点を先に書いています.
書き出す字数が少なければ,変換時に「,」を「、」にするのでもいいのですが,2文以上となると,まずEmacs上で書き出し,領域指定をしてから,自作関数で句読点変換を行っています.
自作関数は次のとおりです.

(defun replace-punctuation (a1 a2 b1 b2)
  "Replace periods and commas"
  (let ((s1 (if mark-active "選択領域" "バッファ全体"))
        (s2 (concat a2 b2))
        (b (if mark-active (region-beginning) (point-min)))
        (e (if mark-active (region-end) (point-max))))
    (if (y-or-n-p (concat s1 "の句読点を「" s2 "」にしますがよろしいですか?"))
        (progn
          (replace-string a1 a2 nil b e)
          (replace-string b1 b2 nil b e)))))
(defun replace-punctuation-ten-maru ()
  "選択領域またはバッファ全体の句読点を「、。」にします"
  (interactive)
  (replace-punctuation "," "、" "." "。"))
(defun replace-punctuation-comma-maru ()
  "選択領域またはバッファ全体の句読点を「,。」にします"
  (interactive)
  (replace-punctuation "、" "," "." "。"))
(defun replace-punctuation-comma-period ()
  "選択領域またはバッファ全体の句読点を「,.」にします"
  (interactive)
  (replace-punctuation "、" "," "。" "."))
(defalias 'replace-punctuation-o 'replace-punctuation-ten-maru)
(defalias 'replace-punctuation-\, 'replace-punctuation-comma-maru)
(defalias 'replace-punctuation-. 'replace-punctuation-comma-period)
(defalias 'tenmaru 'replace-punctuation-ten-maru)
(defalias 'commamaru 'replace-punctuation-comma-maru)
(defalias 'commaperiod 'replace-punctuation-comma-period)

M-x r-p-o RET で「、。」に,M-x r-p-, RET で「,。」に,M-x r-p-. RET で「,.」に,変換します(Windows/GNUPACKおよびUbuntuのGNU Emacs 24.3.1で動作確認しました).リージョン指定をしていなければ,バッファ全体を変換対象とします.変換前にy/nの質問がありますので,誤操作しても安心です.
この投稿のオリジナルは句読点変換の関数 - わさっきです.

3
3
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
3
3