我已经开始根据http://www.emacswiki.org/emacs/EmacsScripts上给出的指示编写Emacs脚本,这基本上说你的脚本应该从以下开始:
:;exec emacs --script "$0" $@
Run Code Online (Sandbox Code Playgroud)
现在我想自定义auto-mode-interpreter-regexp' accordingly, to make Emacs scripts automatically loaded withemacs-lisp-mode'.
最初的`auto-mode-interpreter-regexp'用于匹配:
#! /bin/bash
#! /usr/bin/env perl
Run Code Online (Sandbox Code Playgroud)
等等,因此就是这个:
"\\(?:#![ ]?\\([^ \n]*/bin/env[ ]\\)?\\([^ \n]+\\)\\)"
Run Code Online (Sandbox Code Playgroud)
我尝试添加新的正则表达式作为替代方案:
(setq auto-mode-interpreter-regexp
(concat ;; match "#! /bin/bash", "#! /usr/bin/env perl", etc.
"\\(?:#![ ]?\\([^ \n]*/bin/env[ ]\\)?\\([^ \n]+\\)\\)"
;; or
"\\|"
;; match ":;exec emacs "
"\\(?::;[ ]?\\(exec\\)[ ]+\\([^ \n]+\\)[ ]*\\)"))
Run Code Online (Sandbox Code Playgroud)
但是这一个在匹配整个字符串时,无法捕获其子匹配,尤其是检测解释器所需的第二个子匹配.因此,我混合了正则表达式以同时匹配两个标题:
(setq auto-mode-interpreter-regexp
(concat ;; match "#!" or ":;"
"\\(?:#!\\|:;\\)"
;; optional spaces
"[ ]?"
;; match "/bin/bash", "/usr/bin/env" or "exec"
"\\(\\[^ \n]*/bin/env[ ]\\|exec[ ]\\)?"
;; match interpreter
"\\([^ \n]+\\)"))
Run Code Online (Sandbox Code Playgroud)
我可以做得更好吗?谢谢.
Emacs 中的正则表达式支持使用“显式编号组”构造来为任何子匹配分配组编号。请参阅Elisp 手册 34.3.1.3 正则表达式中的反斜杠结构。
\n\n语法为\xe2\x80\x98(?num: ... )\xe2\x80\x99,其中num是所选组号。