word-count-modeでバイト数を表示

『ページ 1500 文字』といった原稿依頼の場合、たいていの場合「1 文字」は2 バイト文字の 1 文字です。つまり、『ページ 1500 文字』は『ページ 3000バイト』ということになります。

で、Emacs用にword-count-modeというものがあります。

これはリージョンや専用マーカとカーソルの間の文字数/単語数/行数をモードラインに表示してくれるもので、文字数制限のある原稿を書くときに重宝します。

ただ、最近の Emacs だとマルチバイトの 1 文字をちゃんと 1 文字と数えるので、word-count-mode でも文字数≠バイト数になってしまいます。なのでword-count-mode の「文字数」を『ページ 3000 バイト』と比較することができません。

そこで word-count-mode の表示を『文字数/単語数/行数』から『バイト数/文字数/行数』に変えるパッチを作ってみました。

--- word-count.el.orig	2004-06-04 16:58:17.000000000 +0900
+++ word-count.el	2006-03-30 12:16:23.000000000 +0900
@@ -424,8 +424,8 @@
 (defun word-count-CWL-string (string)
   (setq string (word-count-preremove-string string))
   (list
+   (word-count-byte-string       string t)
    (word-count-characters-string string t)
-   (word-count-words-string      string t)
    (word-count-lines-string      string t)
    ))
 
@@ -435,6 +435,9 @@
 (defun word-count-words-region (&optional start end)
   (word-count-words-string (word-count-buffer-substring start end)))
 
+(defun word-count-byte-region (&optional start end)
+  (string-bytes (word-count-buffer-substring start end)))
+
 (defun word-count-lines-region (&optional start end)
   (word-count-lines-string (word-count-buffer-substring start end)))
 
@@ -451,6 +454,15 @@
      (mell-match-count-string word-count-non-character-regexp string)
      ))
 
+;;; encode-coding-string で buffer-file-coding-system にすると、*-dos
+;;; な文字コードの場合に改行で1バイトカウントされてしまうので、
+;;; shift_jis-unix 固定にしている。
+(defun word-count-byte-string (string &optional nopreremove)
+  (or nopreremove
+      (setq string (word-count-preremove-string string)))
+  (- (length (encode-coding-string string 'shift_jis-unix))
+     (mell-match-count-string word-count-non-character-regexp string)))
+
 (defun word-count-words-string (string &optional nopreremove)
   (or nopreremove
       (setq string (word-count-preremove-string string)))