Emacs 23.2 中包含了 js-mode,这是一个重命名和改进过的 Espresso。我刚刚在你那可笑的代码上执行了 "indent-region",得到了这个:
var o = { foo : 'bar'
, baz : 'foo'
}
, p
, q = new Squash( o
, { start: 0
, limit: 50
}
)
我猜那不是你想要的。逗号的偏移可能与你期望的不同。
(天哪,那太丑了。)
编辑
好的,我查看了 js-mode 并且缩进似乎是由 js--proper-indentation
完成的。它查看当前行,然后尝试做出关于如何缩进的决策。
它有许多条件进行测试,用于各种语法模式。我只是加入了一个以逗号开头的行的检查,并得到了这些结果:
var o = { foo : 'bar'
, baz : 'foo'
}
, p
, q = new Squash( o
, { start: 0
, limit: 50
}
)
我认为这就是你想要的,但在我看来它仍然完全无法使用。无论如何,这是我的做法。
将此条件插入到js--proper-indentation
列表的顶部:
(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))))
....
一定要保留defun中的其他条件 - 我不知道它们的作用,但它们可能很重要。
我不确定这是否牢固可靠,除了您的一个测试用例外,我没有进行过其他测试。但这应该可以帮助您入门。