【发布时间】:2017-01-01 18:58:59
【问题描述】:
在大多数 emacs 模式中,当在块的末尾键入右大括号时,大括号会自动拉回到其相应开头行的缩进级别。例如,输入以下内容后:
int main(int argc, char* argv[]) {
// stuff
}
右大括号被拉回左侧:
int main(int argc, char* argv[]) {
// stuff
} // <---- pulled back with no additional input
但是,当使用 web 模式时,情况并非如此,至少在默认设置下是这样。直到有某些额外的输入,例如当光标位于右大括号的行上时,按 Tab 或 Enter。这不适用于在添加任何块内容之前添加右大括号的编码风格,这在 emacs 中用 C 编码时效果很好(具体来说,我的意思是输入“{ enter enter } C-p”)。在这个序列中,没有什么可以触发右大括号拉回一个缩进级别,所以我的代码如下所示:
class Header extends React.Component {
render() {
return (
<div></div>
);
}
}
除非我在该序列中添加一个制表键(“{ enter enter } tab C-p”)。
我知道这只是一个键,我可以改变我的习惯,但是有没有办法改变 emacs 的行为呢?我在 web 模式的 documentation 中没有看到任何相关变量需要更改,但我是否遗漏了什么?
编辑:在学习了超出我想象的更多关于 emacs 的知识后,我意识到我正在寻找的实际上是 cc-mode 的一个特定功能。在 cc-mode 中,} 绑定到 c-electric-brace。来源:
(defun c-electric-brace (arg)
"Insert a brace.
If `c-electric-flag' is non-nil, the brace is not inside a literal and a
numeric ARG hasn't been supplied, the command performs several electric
actions:
\(a) If the auto-newline feature is turned on (indicated by \"/la\" on
the mode line) newlines are inserted before and after the brace as
directed by the settings in `c-hanging-braces-alist'.
\(b) Any auto-newlines are indented. The original line is also
reindented unless `c-syntactic-indentation' is nil.
\(c) If auto-newline is turned on, various newline cleanups based on the
settings of `c-cleanup-list' are done."
所以看来要在 web 模式下获得这个功能,我需要学习一些 lisp 并提交 PR。这就是我要做的,如果/当我完成时,我会在这里提交答案。与此同时,我仍然希望了解更多相关信息的任何人提供意见。
【问题讨论】:
标签: emacs indentation