Using js-mode's indent logic in js2-mode

Steve Yegge's js2-mode is a sweet major mode for working with JavaScript in Emacs, but its auto-indentation logic is notoriously frustrating.

Here's a somewhat hack-y workaround: switch to javascript-mode before calling indent-region and then switch back.

;; use js-mode's indent logic, by pressing C-M-| (C-M-S-\)
(defun rlw/js-indent-region () (interactive) (js-mode) (indent-region (region-beginning) (region-end)) (js2-mode) )
(define-key js2-mode-map (kbd "C-M-|") 'rlw/js-indent-region)

PS: I haven't yet had a chance to sort these out, but there are at least four or five JavaScript modes:

The first two are supposed to address js2-mode's indentation problems (among other enhancements).

Published 7 May 2013
Tagged emacs, elisp and javascript.

 

This page was generated at 9:56 PM on 15 Jan 2016.
Copyright © 1999 - 2016 Rodney Waldhoff.