From 44a26cd1075f39f12b1a0a248147a52c57d7686b Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Tue, 24 Aug 2010 10:50:06 -0700 Subject: [PATCH] Make the js-mode indentation more customizable. In particular, allow the user to customize how much additional indentation is used for expressions within braces of various types. Also better document js--proper-indentation. --- lisp/ChangeLog | 6 +++++ lisp/progmodes/js.el | 51 ++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index bb36d60..1bad604 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,9 @@ +2010-08-24 Nathan Weizenbaum + + * progmodes/js.el: Make indentation more customizable by adding + variables `js-paren-indent-offset', `js-square-indent-offset', and + `js-curly-indent-offset'. + 2010-08-23 Michael Albinus * net/dbus.el: Accept UNIX domain sockets as bus address. diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index d6feca4..9d9dda7 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -436,6 +436,24 @@ The value must be no less than minus `js-indent-level'." :type 'integer :group 'js) +(defcustom js-paren-indent-offset 0 + "Number of additional spaces used for indentation of expressions within parentheses. +The value must be no less than minus `js-indent-level'." + :type 'integer + :group 'js) + +(defcustom js-square-indent-offset 0 + "Number of additional spaces used for indentation of expressions within square braces. +The value must be no less than minus `js-indent-level'." + :type 'integer + :group 'js) + +(defcustom js-curly-indent-offset 0 + "Number of additional spaces used for indentation of expressions within curly braces. +The value must be no less than minus `js-indent-level'." + :type 'integer + :group 'js) + (defcustom js-auto-indent-flag t "Whether to automatically indent when typing punctuation characters. If non-nil, the characters {}();,: also indent the current line @@ -1762,21 +1780,24 @@ nil." "Return the proper indentation for the current line." (save-excursion (back-to-indentation) - (cond ((nth 4 parse-status) + (cond ((nth 4 parse-status) ; inside comment (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) - (let ((same-indent-p (looking-at + ((js--ctrl-statement-indentation)) ; inside braceless control structure + ((eq (char-after) ?#) 0) ; cpp directive + ((save-excursion (js--beginning-of-macro)) 4) ; cpp directive continuation + ((nth 1 parse-status) ; in parens or brackets + (let (;; A single closing paren/bracket should be indented at the + ;; same level as the opening statement. Same goes for "case" + ;; and "default". + (same-indent-p (looking-at "[]})]\\|\\_\\|\\_")) (continued-expr-p (js--continued-expression-p))) - (goto-char (nth 1 parse-status)) + (goto-char (nth 1 parse-status)) ; go to the opening char (if (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)") - (progn + (progn ; nothing following the opening paren/bracket (skip-syntax-backward " ") - (when (eq (char-before) ?\)) (backward-list)) + (when (eq (char-before) ?\)) (backward-list)) (back-to-indentation) (cond (same-indent-p (current-column)) @@ -1784,15 +1805,21 @@ nil." (+ (current-column) (* 2 js-indent-level) js-expr-indent-offset)) (t - (+ (current-column) js-indent-level)))) + (+ (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 as that. (unless same-indent-p (forward-char) (skip-chars-forward " \t")) (current-column)))) - ((js--continued-expression-p) + ((js--continued-expression-p) ; in a toplevel expression (+ js-indent-level js-expr-indent-offset)) - (t 0)))) + (t 0)))) ; at toplevel, outside any expressions (defun js-indent-line () "Indent the current line as JavaScript." -- 1.7.1