読者です 読者をやめる 読者になる 読者になる

ruby-modeのマジックコメント

たぶんemacsの24.2辺りから自動でutf-8になったんじゃないかと思う。それまでは下記のruby-modeにあるように、coding-sysntem-for-writebuffer-file-coding-systemが設定されていないと

# -*- coding: undefined -*-

みたいになってC-x RET f utf-8 RETのようにしてUTF-8であることを明示しないといけずに面倒だった。まあ今は余り考えなくてもよくなったっぽい

(defun ruby-mode-set-encoding ()
  (save-excursion
    (widen)
    (goto-char (point-min))
    (when (re-search-forward "[^\0-\177]" nil t)
      (goto-char (point-min))
      (let ((coding-system
             (or coding-system-for-write
                 buffer-file-coding-system)))
        (if coding-system
            (setq coding-system
                  (or (coding-system-get coding-system 'mime-charset)
                      (coding-system-change-eol-conversion coding-system nil))))
        (setq coding-system
              (if coding-system
                  (symbol-name
                   (or (and ruby-use-encoding-map
                            (cdr (assq coding-system ruby-encoding-map)))
                       coding-system))
                "ascii-8bit"))
        (if (looking-at "^#!") (beginning-of-line 2))
        (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
               (unless (string= (match-string 2) coding-system)
                 (goto-char (match-beginning 2))
                 (delete-region (point) (match-end 2))
                 (and (looking-at "-\*-")
                      (let ((n (skip-chars-backward " ")))
                        (cond ((= n 0) (insert "  ") (backward-char))
                              ((= n -1) (insert " "))
                              ((forward-char)))))
                 (insert coding-system)))
              ((looking-at "\\s *#.*coding\\s *[:=]"))
              (t (insert "# -*- coding: " coding-system " -*-\n"))
              )))))

今はこんな設定。

(set-language-environment       "Japanese")
(prefer-coding-system           'utf-8-unix)
(setq default-buffer-file-coding-system 'utf-8)
(setq coding-system-for-write   'utf-8)
(set-buffer-file-coding-system  'utf-8)
(set-terminal-coding-system     'utf-8)
(set-keyboard-coding-system     'utf-8)
(set-clipboard-coding-system    'utf-8)
(set-language-environment 'utf-8)
(set-default-coding-systems 'utf-8)