3

I'm very new to Emacs, and I'm having trouble finding information about electric-layout-mode, specifically electric-layout-rules.

I use c-toggle-auto-newline right now, but I'm trying to replace this with Electric Layout in the hopes that it will cooperate with Electric Pair Mode, so that I can combine the autoindentation of electric-indent-mode with Electric Pair Mode's bracket behavior.

In other words, I am hoping it will give me this behavior upon pressing "{":

int main() <- (Ideally autonewline here, as C Auto Newline does)
{
    (point)
}

However, I can't find enough information about electric-layout-rules to get it working in my .emacs file. I enabled electric-layout-mode without trouble, since there is an entry for it in the Customize buffer.

I looked at the Help entry for "electric-layout-rules", but I had trouble understanding it, and I noted that the syntax for it was similar to that of c-hanging-braces-alist of C Auto Newline, which I tried in vain to emulate the syntax of.

Long story short, I would appreciate some kind of use example for electric-layout-rules, something I might be able to put into my .emacs file.


EDIT: I had asked a similar, less detailed version of this question on SuperUser a couple of weeks ago. I don't know how to get questions moved, but I figured I might leave it open until this one is answered or if someone suggests that I delete it now, in case any of it is relevant here.

This Electric Layout Mode Manual Page was linked to in the other question, but I doesn't have anything on customizing the behavior through electric-layout-rules, and it explicitly says JavaScript on it. The code in the answer and electric-layout-mode didn't work when editing a C file.

4

1 回答 1

4

如您所见,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))

{每当我们键入它时,都会导致在 a 之前和之后自动插入换行符。

我尝试结合布局和对选项,它并不能完全复制你所希望的,但是 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) Basic Char Syntax RET
  • C-hig (elisp) Dotted Pair Notation RET
  • C-hig (elisp) Association Lists RET
于 2013-06-17T05:14:19.213 回答