Created
November 27, 2011 23:39
-
-
Save travisjeffery/1398457 to your computer and use it in GitHub Desktop.
nodejs, javascript indentation for emacs
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 this to your Emacs configuration to have proper Nodejs | |
;; indentation with javascript when using `js-mode'. —Travis Jeffery | |
(add-hook 'js-mode-hook (lambda () | |
(defun js--proper-indentation (parse-status) | |
"Return the proper indentation for the current line." | |
(save-excursion | |
(back-to-indentation) | |
(cond | |
((looking-at ",") | |
(let ((spos | |
(save-excursion | |
(while (looking-back "}[\t\n ]*") | |
(backward-sexp) | |
(if (looking-back ",[\t\n ]*") | |
(re-search-backward ",[\t\n ]*"))) | |
(cond | |
((looking-back "\\(,\\|(\\)[ \t]*\\<\\([^:=\n\t ]+\\)[ \t\n]*") | |
(re-search-backward "\\(,\\|(\\)[ \t]*\\<\\([^:=\n\t ]+\\)[ \t\n]*" (point-min) t) | |
(+ (current-column) 2)) | |
((re-search-backward "\\<\\([^:=\n\t ]+\\)[ \t]*\\(:\\|=\\)" (point-min) t) | |
(current-column)) | |
(t | |
nil))))) | |
(if spos | |
(- spos 2) | |
(+ js-indent-level js-expr-indent-offset)))) | |
((nth 4 parse-status) | |
(js--get-c-offset 'c (nth 8 parse-status))) | |
((nth 8 parse-status) 0) ; inside string | |
((js--ctrl-statement-indentation)) | |
((eq (char-after) ?#) 0) | |
((save-excursion (js--beginning-of-macro)) 4) | |
((nth 1 parse-status) | |
;; A single closing paren/bracket should be indented at the | |
;; same level as the opening statement. Same goes for | |
;; "case" and "default". | |
(let ((same-indent-p (looking-at | |
"[]})]\\|\\_<case\\_>\\|\\_<default\\_>")) | |
(continued-expr-p (js--continued-expression-p))) | |
(goto-char (nth 1 parse-status)) ; go to the opening char | |
(if (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)") | |
(progn ; nothing following the opening paren/bracket | |
(skip-syntax-backward " ") | |
(when (eq (char-before) ?\)) (backward-list)) | |
(back-to-indentation) | |
(cond (same-indent-p | |
(current-column)) | |
(continued-expr-p | |
(+ (current-column) (* 2 js-indent-level) | |
js-expr-indent-offset)) | |
(t | |
(+ (current-column) js-indent-level | |
(case (char-after (nth 1 parse-status)) | |
(?\( js-paren-indent-offset) | |
(?\[ js-square-indent-offset) | |
(?\{ js-curly-indent-offset)))))) | |
;; If there is something following the opening | |
;; paren/bracket, everything else should be indented at | |
;; the same level. | |
(unless same-indent-p | |
(forward-char) | |
(skip-chars-forward " \t")) | |
(current-column)))) | |
((js--continued-expression-p) | |
(+ js-indent-level js-expr-indent-offset)) | |
(t 0)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment