* bug#6914: Indentation improvements for js-mode
@ 2010-08-25 17:34 Nathan Weizenbaum
2010-08-26 15:12 ` Chong Yidong
0 siblings, 1 reply; 4+ messages in thread
From: Nathan Weizenbaum @ 2010-08-25 17:34 UTC (permalink / raw)
To: 6914
[-- Attachment #1.1: Type: text/plain, Size: 220 bytes --]
Package: emacs
Version: 24
Attached is a patch that the indentation for js-mode more customizable. In
particular, it adds variables for customizing the level of indentation
within different types of enclosing brackets.
[-- Attachment #1.2: Type: text/html, Size: 254 bytes --]
[-- Attachment #2: 0001-Make-the-js-mode-indentation-more-customizable.patch --]
[-- Type: text/x-patch, Size: 5113 bytes --]
From 44a26cd1075f39f12b1a0a248147a52c57d7686b Mon Sep 17 00:00:00 2001
From: Nathan Weizenbaum <nex342@gmail.com>
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 <nweiz@cressida.sea.corp.google.com>
+
+ * 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 <michael.albinus@gmx.de>
* 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
"[]})]\\|\\_<case\\_>\\|\\_<default\\_>"))
(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
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2010-08-26 20:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-25 17:34 bug#6914: Indentation improvements for js-mode Nathan Weizenbaum
2010-08-26 15:12 ` Chong Yidong
2010-08-26 18:07 ` Daniel Colascione
2010-08-26 20:10 ` Chong Yidong
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).