EmacsWiki: Java Script Mode (original) (raw)
JS mode (formerly Espresso)
For more general information on working with JavaScript in Emacs, see the JavaScript page.
For a long while there was debate and back-and-forth about JavaScript support in Emacs. There were a number of different options, in various stages of maturity and completion. As of Emacs 23.2, there is a standard major mode for editing javascript: js-mode. It is derived and updated from Espresso-mode.
Espresso-mode itself is a fork of Karl Landstrom’s JavaScript mode (described below), but with additional features and robustness improvements. For the legacy page, see http://www.nongnu.org/espresso. For the current js-mode, just get a current Emacs (23.2 or later). If you have a back-rev of Emacs, see EmacsDevelopmentSources for how to get js-mode.
js-mode includes:
- Support for cc-mode’s comment wrapping.
- C preprocessor support – yes, people use this with JavaScript.
- Full suite of movement commands, including
beginning-of-defun,end-of-defun, and so on. - Configurable recognition of class definitions. JavaScript, being a prototype-based language, has no “classes” per se, but there are constructions used to mean the same thing.
- Hierarchical
imenusupport using the aforementioned class definition recognition. In addition to recognizing top-level functions,imenuwill recognize methods. - Improved fontification of regular expression literals
JavaScript mode
Anything with the name “javascript-mode” or “javascript” is likely deprecated – and decrepit. The following links are kept around for information, but if you are using JavaScript, you want js-mode or Js2Mode.
Usually this name refers to Karl Landstrom’s implementation: https://web.archive.org/web/20130313070358/http://www.karllandstrom.se/downloads/emacs/javascript.el
Another obsolete implementation by Peter Kruse: https://web.archive.org/web/20190406114129/http://wal.sh/code/elisp/trunk/javascript-kruse.el
The Mewde Project contain XEmacs’ javascript mode while adding Mozilla Spider Monkey Project Javascript-Shell support. It works inside Emacs, but only primitively.
Fix for Greasemonkey scripts
The following fix from Sylecn addresses a bug where the metadata block in Greasemonkey scripts confuses the syntax highlighter:
(eval-after-load 'js
'(progn
(setq js--regexp-literal-fix
"[^=][=(,:]\\(?:\\s-\\|\n\\)*\\(/\\)\\(?:\\\\.\\|[^/*\\]\\)\\(?:\\\\.\\|[^/\\]\\)*\\(/\\)")
(setq js-font-lock-syntactic-keywords-fix
`((,js--regexp-literal-fix (1 "|") (2 "|"))))
(setq js-font-lock-syntactic-keywords js-font-lock-syntactic-keywords-fix)))