Emacs - 找不到关于电子布局模式的信息

时间:2013-06-17 04:31:22

标签: emacs elisp

我对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)
}

但是,我找不到有关electric-layout-rules的足够信息,无法在我的.emacs文件中使用它。我启用了electric-layout-mode没有问题,因为在自定义缓冲区中有一个条目。

我查看了“电子布局规则”的帮助条目,但我无法理解它,并且我注意到它的语法类似于C Auto Newline的c-hanging-braces-alist的语法,我徒劳地试图模仿。

的语法

长话短说,我会很感激electric-layout-rules的某种使用示例,我可能会把它放到我的.emacs文件中。


编辑:几周前我曾在SuperUser上问过similar, less detailed version of this question。我不知道如何解决问题,但我想我可以保持开放,直到这个问题得到解答,或者如果有人建议我现在删除它,以防任何相关内容。

这个Electric Layout Mode Manual Page与另一个问题相关联,但我没有通过electric-layout-rules自定义行为的任​​何内容,而且它明确地说明了JavaScript。答案中的代码和electric-layout-mode在编辑C文件时不起作用。

1 个答案:

答案 0 :(得分:5)

正如您所见, C-h v 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))

e.g:

(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-h i g (elisp) Basic Char Syntax RET
  • C-h i g (elisp) Dotted Pair Notation RET
  • C-h i g (elisp) Association Lists RET
相关问题