我很新的Emacs的,而我无法找到有关的信息electric-layout-mode
,具体electric-layout-rules
.
我c-toggle-auto-newline
现在正在使用,但我正在尝试用Electric Layout替换它,希望它能配合电子对模式,这样我就可以将自动压缩electric-indent-mode
与电子对模式的支架行为结合起来.
换句话说,我希望在按下"{"时它会给我这种行为:
int main() <- (Ideally autonewline here, as C Auto Newline does)
{
(point)
}
Run Code Online (Sandbox Code Playgroud)
但是,我找不到有关electric-layout-rules
在.emacs文件中使用它的足够信息.我electric-layout-mode
没有遇到麻烦,因为在Customize缓冲区中有一个条目.
我查看了"电子布局规则"的帮助条目,但我无法理解它,我注意到它的语法类似于c-hanging-braces-alist
C Auto Newline的语法,我徒劳地试图模仿语法.
长话短说,我会很感激某些使用示例electric-layout-rules
,我可以将其放入我的.emacs文件中.
编辑:几周前,我在SuperUser上问了一个类似的,不那么详细的问题.我不知道如何移动问题,但我想我可以保持开放,直到这个问题得到解答,或者有人建议我现在删除它,以防其中任何相关内容.
这个电子布局模式手册页与其他问题相关联,但我没有任何关于自定义行为的内容electric-layout-rules
,它明确地说明了JavaScript.答案中的代码在electric-layout-mode
编辑C文件时不起作用.
如你所见,C-hv electric-layout-rules
RET告诉我们:
List of rules saying where to automatically insert newlines.
Each rule has the form (CHAR . WHERE) where CHAR is the char
that was just inserted and WHERE specifies where to insert newlines
and can be: nil, `before', `after', `around', or a function of no
arguments that returns one of those symbols.
Run Code Online (Sandbox Code Playgroud)
这意味着我们可以通过以下模式添加新规则:
(add-to-list 'electric-layout-rules '(CHAR . WHERE))
Run Code Online (Sandbox Code Playgroud)
例如:
(add-to-list 'electric-layout-rules '(?{ . around))
Run Code Online (Sandbox Code Playgroud)
{
每当我们输入换行符时,会导致在换行符之前和之后自动插入换行符.
我尝试将布局和配对选项结合起来,并没有完全复制你所希望的,但是FWIW:
(require 'electric)
(add-to-list 'electric-layout-rules '(?{ . around))
(add-to-list 'electric-pair-pairs '(?{ . ?}))
(electric-layout-mode 1)
(electric-pair-mode 1)
Run Code Online (Sandbox Code Playgroud)
它似乎对启用这两种模式的顺序很敏感.为结束括号添加布局规则没有帮助,因为那些显然只会触发手动键入的字符.
进一步阅读:
(elisp) Basic Char Syntax
RET(elisp) Dotted Pair Notation
RET(elisp) Association Lists
RET