Snippets are tiny notes I've collected for easy reference.
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:
- mooz's fork of js2-mode
- thomblake's js3-mode
- I think the defunct espresso-mode is now the built-in
js-mode
. - I'm not sure where that leaves
javascript-mode
. Also defunct I think. - Steve Yegge's js2-mode
The first two are supposed to address js2-mode's indentation problems (among other enhancements).
Published 7 May 2013
Snippets are tiny notes I've collected for easy reference.