• このエントリーをはてなブックマークに追加
  • このエントリをクリップ! このエントリをクリップ!
ss-20090820-00 on Flickr

はじめに

YAMLデータの記述には,emacsのyaml-mode(0.0.4)を利用しています.

yaml-mode,便利ではあるのですが,ときどきインデントにタブが入ることがあります,(以前のバージョンでもそうでしたが.)この状態でYAML::Syckとかに通すと,読み込みに失敗してしまい,とても残念な気持ちになります.

本日,その残念な部分を修正しているっぽいものを見つけたので,以下,紹介とか修正方法とか.

なお,相変わらずemacs lispについては無知なのです.

yaml-mode.el いくつか

私は現在,次のyaml-mode.elを使っています.

で,今回見つけたのが次のページです.

Original yaml-mode.el has incorrect regexp for YAML comments which makes it treat almost everything as comment (at least on my Emacs 23.0.60). This yaml-mode.el fixes it.

As of 2009/07/07, this yaml-mode.el also enforces spaces instead of TABs in yaml-mode, regardless of emacs configuration.
EmacsWiki: Yaml Mode

この中で紹介(?)されているのが次のyaml-mode.elです.

バージョンは0.0.3となっていますが,はてさて...

本エントリでは,前者をyaml-mode-0.0.4.el,後者をyaml-mode-0.0.3x.elとしています.

差分をとってみる

どこが違うのかな,違うところだらけだったらやる気なくすな,と,二者間の差分をとってみたのが以下です.

% diff -u yaml-mode-0.0.3x.el yaml-mode-0.0.4.el
--- yaml-mode-0.0.3x.el 2009-08-20 10:56:03.000000000 +0900
+++ yaml-mode-0.0.4.el  2009-06-13 01:44:05.000000000 +0900
@@ -5,7 +5,7 @@
 ;; Author: Yoshiki Kurihara <kurihara@cpan.org>
 ;;         Marshall T. Vandegrift <llasram@gmail.com>
 ;; Keywords: data yaml
-;; Version: 0.0.3
+;; Version: 0.0.4
 
 ;; This file is not part of Emacs
 
@@ -107,12 +107,12 @@
 
 ;; Constants
 
-(defconst yaml-mode-version "0.0.3" "Version of `yaml-mode.'")
+(defconst yaml-mode-version "0.0.4" "Version of `yaml-mode.'")
 
 (defconst yaml-blank-line-re "^ *$"
   "Regexp matching a line containing only (valid) whitespace.")
 
-(defconst yaml-comment-re "\\(#+.*\\)"
+(defconst yaml-comment-re "\\(?:^\\|\\s-+\\)\\(#.*\\)"
   "Regexp matching a line containing a YAML comment or delimiter.")
 
 (defconst yaml-directive-re "^\\(?:--- \\)? *%\\(\\w+\\)"
@@ -218,9 +218,7 @@
   (set (make-local-variable 'font-lock-defaults)
        '(yaml-font-lock-keywords
          nil nil nil nil
-         (font-lock-syntactic-keywords . yaml-font-lock-syntactic-keywords)))
-  ;; TABs are not allowed in YAML
-  (set (make-local-variable 'indent-tabs-mode) nil))
+         (font-lock-syntactic-keywords . yaml-font-lock-syntactic-keywords))))
 
 
 ;; Font-lock support
%

変更個所はごくわずかでよかったよかった.

修正

先の差分の中で「TAB」の文字が入っている次の個所がいかにもって感じです.

@@ -218,9 +218,7 @@
   (set (make-local-variable 'font-lock-defaults)
        '(yaml-font-lock-keywords
          nil nil nil nil
-         (font-lock-syntactic-keywords . yaml-font-lock-syntactic-keywords)))
-  ;; TABs are not allowed in YAML
-  (set (make-local-variable 'indent-tabs-mode) nil))
+         (font-lock-syntactic-keywords . yaml-font-lock-syntactic-keywords))))

これを参考に,yaml-mode-0.0.4.elを修正します,手動で... (差分を部分的に適用する方法があればどなたかご教授くださいw)

このファイルを以下,yaml-mode-0.0.4x.elとします.

バイトコンパイルして完了

修正したyaml-mode-0.0.4x.elをバイトコンパイルします.

emacsを再起動してYAMLファイルを読み込んで,タブインデントができるような操作を再現してみたところ,無事,スペースのみのインデントができるようになりました.

最終的なパッチ

diff -u yaml-mode-0.0.4.el yaml-mode-0.0.4x.elの結果を以下に載せておきます.

--- yaml-mode-0.0.4.el  2009-06-13 01:44:05.000000000 +0900
+++ yaml-mode-0.0.4x.el 2009-08-20 12:23:59.000000000 +0900
@@ -218,7 +218,9 @@
   (set (make-local-variable 'font-lock-defaults)
        '(yaml-font-lock-keywords
          nil nil nil nil
-         (font-lock-syntactic-keywords . yaml-font-lock-syntactic-keywords))))
+         (font-lock-syntactic-keywords . yaml-font-lock-syntactic-keywords)))
+  ;; TABs are not allowed in YAML
+  (set (make-local-variable 'indent-tabs-mode) nil))
 
 
 ;; Font-lock support

おわりに

以上,emacsのyaml-modeで残念なタブインデントができるのをなくすための一方法を紹介しました.

YAMLライフの幸せ向上につながれば幸いです.

2 Responses to “[emacs] yaml-modeにおける残念なタブインデントをなくす”

  1. kei10in より:
     (setq-default indent-tabs-mode nil)

    じゃダメですか??

  2. 匿名 より:

    .emacs とかに

    (add-hook ‘yaml-mode-hook
    (function
    (lambda ()
    (set (make-local-variable ‘indent-tabs-mode) nil)
    )))

    でよさそう

コメントをドウゾ

will not be published.

  • twitter
  • facebook
  • github
  • hatena bookmark
  • SlideShare
  • flickr
  • foursquare
  • ustream
  • tumblr
  • friendfeed
  • mixi
  • Apple
  • WordPress
  • come icons are powered by komodomedia!