2011年6月30日木曜日

emacs 23 (その6)

次は Emacs-23 で UTF-8 を使う場合や、文字コードの変換をする場合のマッピングの問題について考えます。マッピング問題は主に Microsoft の CP932 環境と、それ以外の環境で Unicode と JIS の間のマッピングが異なることが原因になっています。(他にも OS ごとに細々とした違いがあるのですが)。

Emacs-23 にはこの問題に対応するために japanese-ucs-jis-to-cp932-map, japanese-ucs-cp932-to-jis-map という二つの変換テーブルが定義されています。これをうまく使えば CP932 との変換表の違いを吸収することができます。

ただこの表はあくまで JIS X 0208 と Microsoft CP932 の違い定義しているようで、JIS X 0213 については考慮されておらず、そのまま使用してしまうと、JIS 第三水準にある「チルダ」が第1水準の「波ダッシュ」に変換されてしまうなど、いくつかの文字で不具合がでます。

ということで、この表も自分用に再定義することにします。ついでに GNU libc や Apple MacOS などで(過去に?)使用されていた変換表の差分も吸収しておきます。



;;
;; Japanese Compatible Table
;;
;;    compatibility for CP932 mapping
;;    compatibility for glibc alternative mapping
;;    compatibility for Apple alternative mapping
;;
(let ((table (make-translation-table)))
  (aset table #x2015 #x2014) ;Horizontal Bar              :Em Dash
  (aset table #x203e #x007e) ;Overline                    :Tilde
  (aset table #x2299 #x29bf) ;Circled Dot Operator        :Circled Bullet
  (aset table #x22ef #x2026) ;Midline Horizontal Ellipsis :Horizontal Ellipsis
  (aset table #xffe0 #x00a2) ;Fullwidth Cent Sign         :Cent Sign
  (aset table #xffe1 #x00a3) ;Fullwidth Pound Sign        :Pound Sign
  (aset table #xffe2 #x00ac) ;Fullwidth Not Sign          :Not Sign
  (aset table #xffe4 #x00a6) ;Fullwidth Broken Line       :Broken Line
  (define-translation-table 'ja-compatible-encode-map table))

;;
;; compatible map for japanese
;;
(coding-system-put 'compound-text :encode-translation-table
                    (get 'ja-compatible-encode-map 'translation-table))
(coding-system-put 'iso-2022-7bit :encode-translation-table
                    (get 'ja-compatible-encode-map 'translation-table))
(coding-system-put 'iso-2022-jp :encode-translation-table
                    (get 'ja-compatible-encode-map 'translation-table))
(coding-system-put 'iso-2022-jp-2 :encode-translation-table
                    (get 'ja-compatible-encode-map 'translation-table))
(coding-system-put 'iso-2022-jp-2004 :encode-translation-table
                    (get 'ja-compatible-encode-map 'translation-table))
(coding-system-put 'japanese-shift-jis :encode-translation-table
                    (get 'ja-compatible-encode-map 'translation-table))
(coding-system-put 'japanese-shift-jis-2004 :encode-translation-table
                    (get 'ja-compatible-encode-map 'translation-table))

上記のように変換テーブルは encoding 側にのみ設定しています。すなわち内部で保持している時には読み込んだままの unicode で保持しておき ISO-2022/SJIS系の文字コードで画面出力や、ファイルにセーブする時に対応する文字コードに変換するようにしています。

こうすることにより内容に影響を与えないため、そのまま UTF-8 や CP932 のファイルの読み書きすることもできます。

上記の一覧に EUC-JP系の文字コードがないのは、個人的にさらに別の処理を追加しているためです。その部分については次に説明する予定です。(続く)

0 件のコメント:

コメントを投稿