[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
 decode-coding-regin() with utf-8
- From: Hideyuki SHIRAI (
白井秀行
) <shirai@xxxxxxxxxxx>
 
- Date: Mon, 14 Mar 2005 19:13:33 +0900 (JST)
 
- X-ml-name: emacs-w3m
 
- X-mail-count: 07888
 
白井です。
いつからかはわかりませんが、最近の Emacs CVS Head の CJK モード
を使って、utf-8 (や utf-16) を扱う場合、Emacs 立ち上げ後、最初に
utf-8 を使うときに subst-jis などをロードしますが、そのときの関
数が decode-codeing-region() だと decode 結果が化けるようです。
例えば、
(let ((file (expand-file-name "/tmp/utf8.txt"))
      (coding-system-for-write 'utf-8))
  (with-temp-buffer
    (insert "朝日新聞社")
    (write-region (point-min) (point-max) file)))
とでもして、utf-8 なファイルを作っておき、
% emacs -q
で立ち上げた Emacs 上で、
(let ((file (expand-file-name "/tmp/utf8.txt"))
      (buf1 (get-buffer-create "*tmp1*"))
      (coding-system-for-read 'raw-text))
  (pop-to-buffer buf1)
  (erase-buffer)
  (insert-file-contents file)
  (set-buffer-multibyte nil)
  (decode-coding-region (point-min) (point-max) 'utf-8)
  (set-buffer-multibyte t))
を動かすと、「朝×××社」のように化けます。
# × の中身は省略
だけど、上記をもう一回動かしたり、最初に動かすものが以下のような
ものでは、ちゃんと「朝日新聞社」となります。
(let ((file (expand-file-name "/tmp/utf8.txt"))
      (coding-system-for-read 'raw-text))
  (decode-coding-string
   (with-temp-buffer
     (insert-file-contents file)
     (buffer-string))
   'utf-8))
とか、
(let ((file (expand-file-name "/tmp/utf8.txt"))
      (buf1 (get-buffer-create "*tmp1*"))
      (coding-system-for-read 'raw-text))
  (encode-coding-string (string 53794) 'utf-8) ;; この行追加
  (pop-to-buffer buf1)
  (erase-buffer)
  (insert-file-contents file)
  (set-buffer-multibyte nil)
  (decode-coding-region (point-min) (point-max) 'utf-8)
  (set-buffer-multibyte t))
とか、
(let ((file (expand-file-name "/tmp/utf8.txt"))
      (buf1 (get-buffer-create "*tmp1*"))
      (coding-system-for-read 'utf-8))
  (pop-to-buffer buf1)
  (erase-buffer)
  (insert-file-contents file))
は化けません。
報告だけで申し訳ありませんが、よろしくお願いします。
-- 
白井秀行 (mailto:shirai@meadowy.org)