0

Just FYI, I am new to the .emacs file.

I would lik to set up my .emacs file to auto-indent and auto-pair a certain way to make writing code a little faster. I have found some info as to how to do these things independently but I'm not sure how to put it all together for the emacs version that I have. Ultimately, I would like to set up these definitions specific to which ever language I am coding in. Just to get me started I will use java as an example.

Obviously auto-pairing for ", (, ' are pretty straigforward. I would just like it to auto insert a closing ", ), ' and place the cursor in the middle.

For {, I would like it auto insert two newlines and the closing } whith the cursor in the middle.

Example

while (true) {
  <--- cursor would be here with auto-indent of 2 spaces
}

I would also like this to work for nested curly braces which the appropriate indentation.

Example

while(true) {
  if (...) {

  }
}

Here is what I have so far in my .emacs file:

(defun java-autoindent ()
  (when (and (eq major-mode 'java-mode) (looking-back "[{;]"))
    (newline-and-indent)))
(add-hook 'post-self-insert-hook 'java-autoindent)

Obviously this just inserts a line and auto indents, but I also want the closed } to be included on the line below. I also tried using electric-pair but that didn't work.

My wish list may be a little unrealistic. I'm not even sure that this is possible, but I would be happy with the closest that I could get. Any help to get me going in the right direction would be greatly appreciated.

Drew
  • 29,895
  • 7
  • 74
  • 104
Quinn V.
  • 31
  • 3
  • "The didn't work" doesn't give us much info to help you out. You might also like to try `electric-layout-mode` to auto-insert the LFs after { and before }. – Stefan Mar 06 '16 at 15:54
  • One more thing: modes from the CC-modes family (Java, C, C++) may behave slightly different in this respect (because they offered similar functionality in a different way, long before a global feature was introduced), so try those electric-*-mode thingies in some other modes first. – Stefan Mar 06 '16 at 15:55

1 Answers1

0

Emacs defines modes for each type of language you code in. Some modes are derived from others and there is a mode called prog-mode which most programming modes are derived from.

The mode for a language is where things like indentation are defined because these tend to be language specific. The rules for indentation can be quite complicated, which is why people often use a mode with similar indentation style as the parent and derive a new mode from that.

Have a look at modes and derived mode in the emacs elisp manual.

With respect to adding matching/closing delimiters, have a look at electric-pair-mode (I think it was in emacs 24.4 - I'm running 25 and forget when it was introduced).

With respect to your requirement to enter some code, some newlines and position the cursor in a specific place, you probably want to look at one of emacs' template solutions. yasnippet is a popular choice and it is easy to define new templates in it. There are also many existing packaged yasnippet templates you can download/install. If you don't like yasnippet, google emacs template and have a look there are quitre a few frameworks.

Tim X
  • 4,158
  • 1
  • 20
  • 26