My favorites | Sign in
Project Logo
                
Search
for
Updated Apr 14, 2008 by steve.yegge
Labels: Featured
InstallationInstructions  

js2-mode instructions

Introduction

js2-mode is a new JavaScript editing mode for GNU Emacs. It aims to be more like an IDE (such as Eclipse or IntelliJ) than existing Emacs language modes. There is a lot of work left to do to achieve this goal, but it's got a pretty good start:

Most of the mode's features are customizable via M-x customize.

Details

To install js2-mode:

(autoload 'js2-mode "js2" nil t)
(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))

It will refuse to run unless you have byte-compiled it. You must byte-compile it with your version of Emacs because different versions of Emacs have different byte-compiled formats.

Usage Notes

Invoke M-x customize-group RET js2-mode RET to see the configuration options.

This mode does not have customizable indentation the way cc-engine modes do (e.g. java-mode, c-mode, objc-mode). It is a significant amount of work to create a customizable indenter.

Instead, js2-mode uses an indentation guesser (based on Karl Landström's "javascript.el" guesser), plus "bounce-indenting". When you hit TAB the first time to indent a line, it indents to what it thinks is the most likely indentation point, and it computes a set of other possibilities. If you hit TAB repeatedly, it cycles among these possibilities.

There are obvious downsides to this approach, but hopefully over time the indentation support will improve.

Installing from source code

As an alternate way to install the mode, you can download the source files from the svn repository (from the Source tab above), put them in your load-path, and byte-compile each of them. The mode runs without being byte-compiled in "developer mode" (i.e. from source), so be careful, since it's really really slow for large files.

If you're using the svn sources, you can get the latest updates between official releases.


Comment by glyphobet, Apr 29, 2008

Delete comment Comment by glyphobet, Today (moments ago)

I had no idea how to "put it in your Emacs load-path somewhere" and various Google searches were no help. Here's what eventually worked for me, in emacs:

1. make the directory ~/.emacs.d/js2/

2. put js2.el and js2.elc in ~/.emacs.d/js2/`

3. add these three lines to your ~/.emacs file:

(setq load-path (append (list (expand-file-name "~/.emacs.d/js2")) load-path))
(autoload 'js2-mode "js2" nil t)
(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))
Comment by j...@stonelake.fi, May 01, 2008

Thanks, got it now!

Comment by benluo, May 05, 2008

Is it possible to add some supports for Ajax module like JQuery, Dojo, YUI and so on?

Comment by mixandgo, May 15, 2008

Byte compiling throws an error in emacs-cvs from yesterday:

Compiling file /home/myusername/.elisp/js2.el at Thu May 15 15:09:23 2008 js2.el:6787:24:Error: Variable binding depth exceeds max-specpdl-size

Comment by k...@ollivier.co.nz, Aug 04, 2008

The path for load-paths is normally the lisp directory.

c:/emacs/lisp

for example

Comment by gopal418, Sep 19, 2008

@mixandgo, I was able to compile successfully from the command line:

shell> emacs --batch --eval '(byte-compile-file "js2-20080616a.el")'

I'm running Emacs 23.0.60.1

Comment by jcalas, Sep 21, 2008

I have the same problem with CVS emacs, I can compile it as the comment above sugested, but when I try to open a javascript file I get this error:

File mode specification error: (error "You must byte-compile js2-mode before using it.")

My js2.elc file is there.

emacs-version

GNU Emacs 23.0.60.1 (i686-pc-linux-gnu, GTK+ Version 2.12.11) of 2008-09-22
Comment by jesdisciple, Oct 21, 2008

This works on Ubuntu 8.04 (Hardy Heron): http://ubuntuforums.org/showpost.php?p=675916&postcount=3

Note that emacs must be opened with 'sudo' to have permission to write to "/usr/share/emacs/<version>/lisp".

Comment by ivan.fed...@gmail.com, Oct 26, 2008

If you have error message "You must byte-compile js2-mode before using it.", then check nXhtml package. It uses OLD js2 mode with name js2-new.el... :(

Comment by unlox775, Nov 24, 2008

Hey, I just got it installed, opened my first JS file to start editing... what is up with the behavior of TAB and auto-indenting?! Hitting tab mutiple times keeps re-indenting it to different levels! I differs from line-to-line, in loops like (0, 4, 0, 4, ...) or (0, 4, 8, 0, 4, 8, ...) or (12, 8, 4, 12, 8, 4, ...), or (16, 12, 0, 16, 12, 0, ...)

Syntax hiliting is great, but without smart auto-indenting, I'd rather use my javascript-mode. Am I missing something?

Comment by marktucks, Nov 27, 2008

It cycles through various TAB positions based on the previous line, bracket etc. (it's all on his blog), which is actually brilliant because keep hitting TAB in an IDE and it will just keep tabbing the line out, which is hardly ever what you want. Just keep hitting TAB until it's where you want. JS-2 mode also has a syntax checker, which I don't think javascript-mode has unless I'm misinformed.

Coupled with Ejacs (great name), JavaScript? shell, this is a great setup. (Thanks, Steve)

Comment by unlox775, Dec 02, 2008

But, all other code editing modes in emacs, Perl, PHP, java, C, etc have the same tab behavior, you hit tab (any number of times, wherever you are in a line) and it indents that line properly to the One place the indentation makes sense. Can't the syntax reader just read the surrounding code and pick the one place the indent should be? e.g the K&R style? Yeah, maybe it's a step up from other IDE's, but it's a major step back from the emacs usual behavior.


Sign in to add a comment
Hosted by Google Code