我是 Emacs 的新手,我很难找到有关 electric-layout-mode
的信息,特别是 electric-layout-rules
。
我现在使用的是c-toggle-auto-newline
,但是我正在尝试将其替换为Electric Layout,希望它能与Electric Pair Mode配合使用,这样我就可以结合electric-indent-mode
的自动缩进与 Electric Pair Mode 的括号行为。
换句话说,我希望它会在按下“{”时给我这种行为:
int main() <- (Ideally autonewline here, as C Auto Newline does)
{
(point)
}
但是,我找不到足够的关于 electric-layout-rules
的信息来让它在我的 .emacs 文件中工作。我毫无问题地启用了 electric-layout-mode
,因为在自定义缓冲区中有一个条目。
我查看了“electric-layout-rules”的帮助条目,但我很难理解它,我注意到它的语法类似于 c-hanging-braces-alist
的 C Auto Newline,我试图模拟其语法,但没有成功。
长话短说,我希望能为 electric-layout-rules
提供一些使用示例,我可以将其放入我的 .emacs 文件中。
编辑:我问了一个 similar, less detailed version of this question几周前在 SuperUser 上。我不知道如何移动问题,但我想我可能会把它打开直到这个问题得到回答,或者如果有人建议我现在删除它,以防它与这里相关。
这Electric Layout Mode Manual Page在另一个问题中有链接,但我没有任何关于通过 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.
这意味着我们可以通过以下模式添加新规则:
(add-to-list 'electric-layout-rules '(CHAR . WHERE))
例如:
(add-to-list 'electric-layout-rules '(?{ . around))
会导致在 {
前后自动插入换行符,无论何时我们键入它。
我尝试组合布局和配对选项,它并没有完全复制您所希望的,但是 FWIW:
(require 'electric)
(add-to-list 'electric-layout-rules '(?{ . around))
(add-to-list 'electric-pair-pairs '(?{ . ?}))
(electric-layout-mode 1)
(electric-pair-mode 1)
它似乎对启用这两种模式的顺序很敏感。为右大括号添加布局规则没有帮助,因为这些规则显然只会在手动输入的字符上触发。
进一步阅读:
- C-hig
(elisp) 基本字符语法
RET< - C-hig
(elisp) 点对表示法
RET< - C-hig
(elisp) 关联列表
RET
关于Emacs - 找不到关于电子布局模式的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17140442/