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.
So as you've seen, C-hv electric-layout-rules RET tells us:
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.
which means that we can add new rules via the following pattern:
(add-to-list 'electric-layout-rules '(CHAR . WHERE))
e.g.:
(add-to-list 'electric-layout-rules '(?{ . around))
would cause newlines to be automatically inserted before and after a {, whenever we type it.
I tried combining the layout and pairs options, and it doesn't quite replicate what you were hoping for, but FWIW:
(require 'electric)
(add-to-list 'electric-layout-rules '(?{ . around))
(add-to-list 'electric-pair-pairs '(?{ . ?}))
(electric-layout-mode 1)
(electric-pair-mode 1)
It seems to be sensitive to the order in which you enable those two modes. Adding a layout rule for the closing brace didn't help, as those evidentially only trigger on manually-typed characters.
Further reading:
(elisp) Basic Char Syntax RET(elisp) Dotted Pair Notation RET(elisp) Association Lists RETIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With