6
votes

I recently upgraded from emacs 23 to emacs 24. Working with JavaScript using the default js-mode, I've noticed a minor but annoying difference between js-mode in emacs23 vs. emacs24. To make sure it wasn't one of my settings I started emacs23 and emacs24 with the --no-init-file option and I see the same problem, namely:

In emacs23 in js-mode, when you type a closing curly brace, }, it temporarily jumps the cursor back to the match opening curly brace and then indents the closing brace to the appropriate level of indentation.

When I do it in emacs24, the temp cursor jump to the matching opening curly brace occurs as expected, but it does not indent the closing brace, making me hit the tab key to finish the job.

Is anyone else seeing this discrepancy? If so, any ideas on how to fix it? Is there is nice "indent-on-closing-curly" type of variable to set somewhere in my init.el?

I am using emacs on Xubuntu Linux (and I installed from source). I haven't compared 23 vs. 24 on other platforms yet.

Note: someone might want to suggest using Yegge's js2-mode instead, but I generally don't like using it and would like to get js-mode working right.

2

2 Answers

11
votes

Since I've found a satisfactory answer and haven't heard from anyone else, I'll post my answer and accept it in case it helps anybody else.

I didn't find the exact reason why js-mode behaves differently in emacs 23 vs. emacs 24, but setting electric-indent-mode in your .emacs or init.el gives the behavior I want. With this, it actually indents before you type the closing brace, but that works for me. This is a new setting in emacs 24. Your .emacs entry would be:

(electric-indent-mode t)

One problem I encountered is that org-mode doesn't like electric-indent-mode, so you can either specifically set electric-indent-mode for javascript/java/c/etc. modes or specifically unset it for org-mode. I chose the latter with this:

(defun my-org-mode-hooks ()
  (electric-indent-mode -1))

(add-hook 'org-mode-hook 'my-org-mode-hooks)
0
votes

Thank you, midpeter444. (electric-indent-mode t) works like a charm. I had googled and somebody suggest to set js-auto-indent-flag to non-nil, it didn't work on my Emacs 24.3, even there do is a js-auto-indent-flag variable can be customized through 'customize-group js', it takes no effect. I found java.el does not use it though it is declared in source.