From 4915e411a22eeefc9561639f5cde2bbdf1731c00 Mon Sep 17 00:00:00 2001 From: Jackson Ray Hamilton Date: Sat, 7 Mar 2015 18:01:05 -0800 Subject: [PATCH] New indentation option for js-mode * lisp/progmodes/js.el (js--proper-indentation): Add and use custom option `js-dynamic-multi-line-declaration-indentation'. --- lisp/ChangeLog | 5 +++++ lisp/progmodes/js.el | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 0b277c7..2609693 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,8 @@ +2015-03-07 Jackson Ray Hamilton + + * lisp/progmodes/js.el (js--proper-indentation): Add and use new + custom option `js-dynamic-multi-line-declaration-indentation'. + 2015-03-07 Stefan Monnier * battery.el (battery-echo-area-format): Simplify default. diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index d7712e4..2f00da0 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -509,6 +509,37 @@ getting timeout messages." :type 'integer :group 'js) +(defcustom js-dynamic-multi-line-declaration-indentation nil + "Non-nil to indent the first function, array or object literal +in a multi-line variable declaration specially. + +Normally, the first item is unindented, and subsequent items have +their identifiers lined up against the first: + + var o = { + foo: 3 + }; + + var o = { + foo: 3 + }, + bar = 2; + +With this option enabled, the first declaration will be indented +if there are more declarations, such that the first function, +array or object lines up nicely with the remaining declarations: + + var o = { + foo: 3 + }; + + var o = { + foo: 3 + }, + bar = 2;" + :type 'boolean + :group 'js) + ;;; KeyMap (defvar js-mode-map @@ -1884,13 +1915,34 @@ In particular, return the buffer position of the first `for' kwd." ;; "case" and "default". (let ((same-indent-p (looking-at "[]})]")) (switch-keyword-p (looking-at "default\\_>\\|case\\_>[^:]")) - (continued-expr-p (js--continued-expression-p))) + (continued-expr-p (js--continued-expression-p)) + declaration-keyword-end) (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) + (when (and js-dynamic-multi-line-declaration-indentation + ;; Check the condition and + ;; simultaneously preserve the match + ;; data. + (when (looking-at js--declaration-keyword-re) + (setq declaration-keyword-end (match-end 0))) + ;; Determine if the declaration + ;; statement has multiple declarations + ;; (implied if at least one is separated + ;; by a ","). + (save-excursion + (goto-char (nth 1 parse-status)) + (when (condition-case nil + (progn + (forward-sexp) + t) + (error nil)) + (while (forward-comment 1)) + (looking-at ",")))) + (goto-char (1+ declaration-keyword-end))) (let* ((in-switch-p (unless same-indent-p (looking-at "\\_"))) (same-indent-p (or same-indent-p -- 1.9.1