13

I'm using js2-mode for working with javascript in emacs and for the most part it's very useful. However, the indenting methods are terribly frustrating when working with jQuery, closures, and JSON... for instance, code that I wish to be indented like this:

var foo = jQuery('#mycontainer ul li').each(function(el){
    var bar = el.html();
});

Turns out as:

var foo = jQuery('#mycontainer ul li').each(function(el){
                                          var bar = el.html();
                                       });

Is there a way I can just switch off all the indenting "helpers" and just have emacs insert N spaces when I hit the tab key? I know manual-indentation is a step backwards, but having readable code is, IMHO, more useful than a tool that doesn't work as expected.

Phillip B Oldham
  • 18,807
  • 20
  • 94
  • 134
  • That used to be a problem for old js2-mode. The problem is now gone with the latest GNU ELPA version of js2-mode which can be installed from `M-x list-packages`. With that version, C-M-\ or TAB on a region should indent the lines in a conventional way you expect and not in the Lisp-like way. – Jisang Yoo Mar 18 '13 at 13:35

7 Answers7

7

Not a direct answer to your question, but here is a fork of js2-mode that has improved indenting.

One of the improvements is that your example code is indented the way you ask here.

Dmitry
  • 3,625
  • 2
  • 30
  • 28
  • Now many improvements of the fork have been imported back to the original js2-mode and the ELPA version also has all those improvements. – Jisang Yoo Mar 18 '13 at 13:24
  • To be more accurate, the "fork" and ELPA have the same version, and the official page is lagging behind. – Dmitry Mar 18 '13 at 15:01
6

I guess I will make this a full answer instead of a comment; espresso-mode is included with Emacs, and is designed to be a Javascript mode for Emacs (instead of a Javascript mode that happens to run inside of Emacs). It works like regular programming modes, and also happens to indent things the way you like.

jrockway
  • 42,082
  • 9
  • 61
  • 86
  • I'm using emacs 22, but recently added espresso-mode and have found it *much* better than the default. Thanks! – Phillip B Oldham Aug 27 '10 at 09:00
  • 2
    I don't think it's worth throwing away all the nice features of js2 mode when you can use the link @AntonJ provided (http://mihai.bazon.net/projects/editing-javascript-with-emacs-js2-mode) to get the best of both worlds. I've been using that and have no regrets whatsoever. – Tikhon Jelvis Jun 04 '11 at 00:48
5

Check out this solution, maps indentation function in js2-mode to partially use indentation from esresso-mode (now known as js-mode included in emacs 23.2 and newer):

http://mihai.bazon.net/projects/editing-javascript-with-emacs-js2-mode

Works exactly as I expect indentation in emacs to work and you still get the parsing awesomeness from js2-mode.

antonj
  • 21,236
  • 6
  • 30
  • 20
4

Have you tried new versions of js2-mode? It looks like there's a fix out: http://code.google.com/p/js2-mode/issues/detail?id=94

friism
  • 19,068
  • 5
  • 80
  • 116
3

js2-mode supports "bounce" indenting; you can press tab multiple times to choose different likely indenting levels, so you might be able to get the effect you want that way:

(setq js2-bounce-indent-p t)
sanityinc
  • 15,002
  • 2
  • 49
  • 43
1

One other alternative is js3-mode. It indents like this by default, but there seems to be some options that might enable you to tweak it for your liking.

var foo = jQuery('#mycontainer ul li').each(function(el){
            var bar = el.html();
          });
grm
  • 5,197
  • 5
  • 29
  • 35
1

You can simply bind TAB to insert itself:

(add-hook 'js2-mode-hook 'my-js2-mode-hook)
(defun my-js2-mode-hook ()
  (define-key js2-mode-map [tab] 'self-insert-command))

(But the better solution would, of course, be to find out why the mode thinks it needs so much indentation for anonymous functions, and fix it.)

Kilian Foth
  • 13,904
  • 5
  • 39
  • 57