首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将配对列表添加到auto-mode- list?

如何将配对列表添加到auto-mode- list?
EN

Stack Overflow用户
提问于 2012-06-14 14:27:54
回答 3查看 663关注 0票数 4

我有一个很长的文件和文件扩展名列表,我希望Emacs在ruby模式下自动打开它们。通过使用Google,最基本的解决方案是:

代码语言:javascript
复制
(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上循环或反转它?我试过了

代码语言:javascript
复制
(cons '(("\\.rake" . ruby-mode)
         ("\\.thor" . ruby-mode)) auto-mode-alist)

但这似乎并不管用。有什么建议吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-06-14 17:17:17

您只需要一个正则表达式(以及auto-mode-alist中的条目)来匹配所有这些选项,您可以让regexp-opt为您构建它。

代码语言:javascript
复制
(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)))

如果你特别想要单独的条目,你可以这样做:

代码语言:javascript
复制
(mapc
 (lambda (file)
   (add-to-list 'auto-mode-alist
                (cons (concat (regexp-quote file) "\\'") 'ruby-mode)))
 '(".rake" ".thor" "Gemfile" "Rakefile" "Crushfile" "Capfile"))
票数 6
EN

Stack Overflow用户

发布于 2012-06-14 14:37:29

cons获取一个项目和一个列表,并返回一个新的列表,该列表的头部是该项目。(例如,(cons 1 '(2 3))给出了'(1 2 3))

您想要做的是获取一个列表和一个列表,并将它们append在一起

代码语言:javascript
复制
(setq auto-mode-alist
  (append '(("\\.rake" . ruby-mode)
            ("\\.thor" . ruby-mode))
   auto-mode-alist))
票数 1
EN

Stack Overflow用户

发布于 2012-06-19 09:43:27

我最喜欢的是

代码语言:javascript
复制
(push '("\\(\\.\\(rake\\|thor\\)\\|\\(Gem\\|Rake\\|Crush\\|Cap\\)file\\)\\'" . ruby-mode) auto-mode-alist)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11027783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档