如何将自己的对列表列入自动模式中? [英] How can I cons a list of pairs on to auto-mode-alist?

查看:84
本文介绍了如何将自己的对列表列入自动模式中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很长的文件和文件扩展名单,我想让Emacs在ruby模式下自动打开。使用谷歌,最基本的解决方案是这样的:

 (setq auto-mode-alist(cons' 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)
(\\thth,ruby-mode))auto-mode-alist)

但这似乎不起作用任何建议?

解决方案

您只需要一个正则表达式(因此在 auto-mode-alist )以匹配所有这些选项,您可以让 regexp-opt 为您建立它的工作。

 (let *((ruby-files'(.rake.thorGemfileRakefileCrushfile 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.thorGemfileRakefileCrushfileCapfile))


I have a long list of files and file extensions which I would like to have Emacs open automatically in ruby-mode. From using Google, the most basic solution that works is this:

(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))

Which seems way repetitive to me. Is there a way I could define the list of pairs once and either loop or cons it directly onto auto-mode-alist? I've tried

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

but that doesn't seem to work. Any suggestions?

解决方案

You only need a single regexp (and hence entry in auto-mode-alist) to match all those options, and you can let regexp-opt do the work of building it for you.

(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)))

If you especially want individual entries, you might do something like this:

(mapc
 (lambda (file)
   (add-to-list 'auto-mode-alist
                (cons (concat (regexp-quote file) "\\'") 'ruby-mode)))
 '(".rake" ".thor" "Gemfile" "Rakefile" "Crushfile" "Capfile"))

这篇关于如何将自己的对列表列入自动模式中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆