Created
May 31, 2012 21:19
-
-
Save rawsyntax/2846385 to your computer and use it in GitHub Desktop.
js2-mode with better indentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode)) | |
(add-to-list 'auto-mode-alist '("\\.json$" . js2-mode)) | |
;; Use js-mode indentation in js2-mode, I don't like js2-mode's indentation | |
;; | |
;; thanks http://mihai.bazon.net/projects/editing-javascript-with-emacs-js2-mode | |
;; with my own modifications | |
;; | |
(defun my-js2-indent-function () | |
(interactive) | |
(save-restriction | |
(widen) | |
(let* ((inhibit-point-motion-hooks t) | |
(parse-status (save-excursion (syntax-ppss (point-at-bol)))) | |
(offset (- (current-column) (current-indentation))) | |
(indentation (js--proper-indentation parse-status)) | |
node) | |
(save-excursion | |
(back-to-indentation) | |
;; consecutive declarations in a var statement are nice if | |
;; properly aligned, i.e: | |
;; | |
;; var foo = "bar", | |
;; bar = "foo"; | |
(setq node (js2-node-at-point)) | |
(when (and node | |
(= js2-NAME (js2-node-type node)) | |
(= js2-VAR (js2-node-type (js2-node-parent node)))) | |
(setq indentation ( 4 indentation)))) | |
(indent-line-to indentation) | |
(when (> offset 0) (forward-char offset))))) | |
(defun my-indent-sexp () | |
(interactive) | |
(save-restriction | |
(save-excursion | |
(widen) | |
(let* ((inhibit-point-motion-hooks t) | |
(parse-status (syntax-ppss (point))) | |
(beg (nth 1 parse-status)) | |
(end-marker (make-marker)) | |
(end (progn (goto-char beg) (forward-list) (point))) | |
(ovl (make-overlay beg end))) | |
(set-marker end-marker end) | |
(overlay-put ovl 'face 'highlight) | |
(goto-char beg) | |
(while (< (point) (marker-position end-marker)) | |
;; don't reindent blank lines so we don't set the "buffer | |
;; modified" property for nothing | |
(beginning-of-line) | |
(unless (looking-at "\\s-*$") | |
(indent-according-to-mode)) | |
(forward-line)) | |
(run-with-timer 0.5 nil '(lambda(ovl) | |
(delete-overlay ovl)) ovl))))) | |
(defun my-js2-mode-hook () | |
(require 'js) | |
(setq js-indent-level 2 | |
indent-tabs-mode nil | |
c-basic-offset 2) | |
(c-toggle-auto-state 0) | |
(c-toggle-hungry-state 1) | |
(set (make-local-variable 'indent-line-function) 'my-js2-indent-function) | |
(define-key js2-mode-map [(meta control |)] 'cperl-lineup) | |
(define-key js2-mode-map [(meta control \;)] | |
'(lambda() | |
(interactive) | |
(insert "/* -----[ ") | |
(save-excursion | |
(insert " ]----- */")) | |
)) | |
(define-key js2-mode-map [(return)] 'newline-and-indent) | |
(define-key js2-mode-map [(backspace)] 'c-electric-backspace) | |
(define-key js2-mode-map [(control d)] 'c-electric-delete-forward) | |
(define-key js2-mode-map [(control meta q)] 'my-indent-sexp) | |
(if (featurep 'js2-highlight-vars) | |
(js2-highlight-vars-mode)) | |
(message "My JS2 hook")) | |
(add-hook 'js2-mode-hook 'my-js2-mode-hook) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment