为什么我的 Emacs 正则表达式使用 ^ 来匹配行的开头不起作用?

发布于 2024-10-01 21:12:40 字数 397 浏览 0 评论 0原文

我正在尝试设置 .emacs ,以便任何以字母 makefile 开头的文件将其设置为 makefile 模式。例如 makefile-something 应该处于 makefile 模式。

这行不通:

(setq auto-mode-alist (cons '("^makefile" . makefile-mode) auto-mode-alist))

但这行得通:

(setq auto-mode-alist (cons '("makefile" . makefile-mode) auto-mode-alist))

任何人都可以解释为什么吗?

I am trying to set up .emacs so that any file which begins with the letters makefile sets it to makefile mode. E.g. makefile-something should be in makefile mode.

This doesn't work:

(setq auto-mode-alist (cons '("^makefile" . makefile-mode) auto-mode-alist))

But this does:

(setq auto-mode-alist (cons '("makefile" . makefile-mode) auto-mode-alist))

Can anyone explain why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画骨成沙 2024-10-08 21:12:40

这是因为文件名前面有一个路径组件,尝试:

"/makefile[^/]*$"

参见 http://www.gnu.org/s/emacs/manual/html_node/elisp/Auto-Major-Mode.html(页面底部)

EDI:根据 Sean 评论更正了正则表达式

It is because there is a path component in front of the filename, try:

"/makefile[^/]*$"

see http://www.gnu.org/s/emacs/manual/html_node/elisp/Auto-Major-Mode.html (bottom of page)

EDI: corrected regexp according to Sean comment

哆啦不做梦 2024-10-08 21:12:40

所以这只是一个有根据的猜测,但我注意到 auto-mode-alist 中想要匹配文件名开头的其他正则表达式不使用 ^,他们使用/。例如:(

("/\\.?\\(?:gnokiirc\\|kde.*rc\\|mime\\.types\\|wgetrc\\)\\'" . conf-mode)

来自 Emacs 23.2.1)基于此,我认为正则表达式可能应用于文件的完整路径名,而不是基本名称。所以尝试

(setq auto-mode-alist (cons '("/makefile" . makefile-mode) auto-mode-alist))

一下吧。

So this is only an educated guess, but I notice that other regexps in auto-mode-alist that want to match the beginning of the file name don't use ^, they use /. For instance:

("/\\.?\\(?:gnokiirc\\|kde.*rc\\|mime\\.types\\|wgetrc\\)\\'" . conf-mode)

(from Emacs 23.2.1) Based on that, I'm thinking maybe the regexp is applied to the full pathname of the file, not the basename. So try

(setq auto-mode-alist (cons '("/makefile" . makefile-mode) auto-mode-alist))

instead.

梦里°也失望 2024-10-08 21:12:40

只是为了方便,尝试一下

 (add-to-list 'auto-mode-alist ...

Just for convenience's sake, try

 (add-to-list 'auto-mode-alist ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文