LoginSignup
5

More than 5 years have passed since last update.

emacs+editorconfigでマルチバイト文字のエンコードエラーを解決する

Posted at

emacs 24.5.1
editorconfig.el 20150831.1054 (melpa)

状況

editorconfigを入れてから、バッファにマルチバイト文字が含まれる際、
保存時にエンコードエラーが出るようになった。

sample
These default coding systems were tried to encode text
in the buffer `xxxxx.txt':
  (undecided (123 . 45678))

ちなみに文字コードの設定はこうなっている。

(prefer-coding-system 'utf-8-unix)

追ってみる

*-coding-systemを触っているのはこの辺り

(defun edconf-set-line-ending (end-of-line)
  "Set line ending style to CR, LF, or CRLF"
  (set-buffer-file-coding-system
   (cond
    ((equal end-of-line "lf") 'undecided-unix)
    ((equal end-of-line "cr") 'undecided-mac)
    ((equal end-of-line "crlf") 'undecided-dos)
    (t 'undecided))
   nil t))

edconfig-set-line-endingはfind-fileされる度に呼ばれる。

;;;###autoload
(defun edconf-find-file-hook ()
  (when (executable-find edconf-exec-path)
    (let ((props (edconf-parse-properties (edconf-get-properties))))
      (edconf-set-indentation (gethash 'indent_style props)
                              (gethash 'indent_size props)
                              (gethash 'tab_width props))
      (edconf-set-line-ending (gethash 'end_of_line props))
      (edconf-set-trailing-nl (gethash 'insert_final_newline props))
      (edconf-set-trailing-ws (gethash 'trim_trailing_whitespace props))
      (edconf-set-line-length (gethash 'max_line_length props)))))

つまり

  1. end_of_lineが設定されていないとundecided
  2. 設定されていると対応するOSに合わせてundecided-xxx

がemacs側で設定される。

解決方法

  1. .editorconfigにend_of_lineをちゃんと設定する
  2. editorconfig.elの該当する行を書き換える

(色々試してみたが上手くいかなかったので最終的に力技で解決。。)

.editorconfig
# 略
end_of_line = lf
# 略
--- editorconfig-20150831.1054.el   2015-10-17 17:26:06.000000000 +0900
+++ modified.el 2015-10-17 20:22:17.000000000 +0900
@@ -213,9 +213,9 @@
   "Set line ending style to CR, LF, or CRLF"
   (set-buffer-file-coding-system
    (cond
-    ((equal end-of-line "lf") 'undecided-unix)
-    ((equal end-of-line "cr") 'undecided-mac)
-    ((equal end-of-line "crlf") 'undecided-dos)
+    ((equal end-of-line "lf") 'utf-8-unix)
+    ((equal end-of-line "cr") 'utf-8-mac)
+    ((equal end-of-line "crlf") 'utf-8-dos)
     (t 'undecided))
    nil t))

デフォルトの値がundecidedになっているのは、
どんな環境でも動くようになのかな。

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
5