I'm new to Emacs.
I'm trying to write an elisp function that works across all modes. Specifically, I want to write a function that inserts braces (a bit like insert-parentheses
) in the same way the following key sequence does for a dumb editor that only supports auto-indent:
"{" <ret> "}" <up-arrow> <end> <ret> <tab>
This key sequence works for both Java and C# (bsd) style indentation. I need it to work in all brace-y emacs modes, and also in plain text files - I have other formats that have no emacs modes but still use braces.
Here's my 12th attempt or so:
(defun insert-braces-macro ()
(interactive)
(insert "{")
(newline)
(indent-relative t)
(insert "}")
(forward-line -1)
(end-of-line)
(newline)
(indent-relative t)
(indent-relative nil))
Unfortunately, this doesn't work quite right. I don't think indent-relative
is the right function, because it doesn't indent correctly in Java-style:
f |
expands into:
f {
|
}
and in C mode:
somelongword another |
expands to:
somelongword another {
|
}
But indent-according-to-mode
isn't right either, because it will indent too much in C-ish modes (the trailing '}' is indented) and not at all in Fundamental mode.
What's the right way to handle this?