4

Emacsをrubyモードで自動的に開いてもらいたいファイルとファイル拡張子の長いリストがあります。Googleを使用することで、機能する最も基本的なソリューションは次のとおりです。

(setq auto-mode-alist (cons '("\.rake$"    . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\.thor$"    . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("Gemfile$"   . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("Rakefile$"  . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("Crushfile$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("Capfile$"   . ruby-mode) auto-mode-alist))

これは私には繰り返しのようです。ペアのリストを一度定義して、それを直接ループまたはコンスする方法はありますauto-mode-alistか?私はもう試した

(cons '(("\\.rake" . ruby-mode)
         ("\\.thor" . ruby-mode)) auto-mode-alist)

しかし、それはうまくいかないようです。助言がありますか?

4

3 に答える 3

6

auto-mode-alistこれらすべてのオプションに一致するために必要な正規表現は1つ(したがって、へのエントリ)だけであり、それregexp-optを構築する作業を自分で行うことができます。

(let* ((ruby-files '(".rake" ".thor" "Gemfile" "Rakefile" "Crushfile" "Capfile"))
       (ruby-regexp (concat (regexp-opt ruby-files t) "\\'")))
  (add-to-list 'auto-mode-alist (cons ruby-regexp 'ruby-mode)))

特に個別のエントリが必要な場合は、次のようにします。

(mapc
 (lambda (file)
   (add-to-list 'auto-mode-alist
                (cons (concat (regexp-quote file) "\\'") 'ruby-mode)))
 '(".rake" ".thor" "Gemfile" "Rakefile" "Crushfile" "Capfile"))
于 2012-06-14T09:17:16.940 に答える
1

consアイテムとリストを受け取り、そのアイテムを先頭にした新しいリストを返します。(たとえば、(cons 1 '(2 3))を与える'(1 2 3)

あなたがしたいのは、リストとリストとappendそれらを一緒にすることです

(setq auto-mode-alist
  (append '(("\\.rake" . ruby-mode)
            ("\\.thor" . ruby-mode))
   auto-mode-alist))
于 2012-06-14T06:37:28.870 に答える
1

私のお気に入りは

(push '("\\(\\.\\(rake\\|thor\\)\\|\\(Gem\\|Rake\\|Crush\\|Cap\\)file\\)\\'" . ruby-mode) auto-mode-alist)
于 2012-06-19T01:43:26.783 に答える