Emacs 在web模式下-如何将右大括号和圆括号拉至开口线的缩进级别
在大多数emacs模式中,当在块的末尾键入右大括号时,该大括号将自动拉回到其各自开口线的缩进级别。例如,在键入以下内容后:Emacs 在web模式下-如何将右大括号和圆括号拉至开口线的缩进级别,emacs,indentation,Emacs,Indentation,在大多数emacs模式中,当在块的末尾键入右大括号时,该大括号将自动拉回到其各自开口线的缩进级别。例如,在键入以下内容后: int main(int argc, char* argv[]) { // stuff } 将闭合撑杆拉回左侧: int main(int argc, char* argv[]) { // stuff } // <---- pulled back with no additional input 因此,似乎要在web模式下获得此功能,我
int main(int argc, char* argv[]) {
// stuff
}
将闭合撑杆拉回左侧:
int main(int argc, char* argv[]) {
// stuff
} // <---- pulled back with no additional input
因此,似乎要在web模式下获得此功能,我需要学习一些lisp并提交PR。这就是我将要做的,如果/当我完成时,将在此处提交答案。同时,我仍然希望有更多了解这一点的人提供意见。使用
web模式中的defun
。el
尝试:
(local-set-key (kbd "RET") 'newline-and-indent)
因此,我最终提交了一份带有此功能的PR,但维护人员显然也有同样的想法,并将其合并到自己身上。它从v14.0.36(提交)起就开始运行 正如我在对我的问题的编辑中所述,c-electric-brace是一个关键绑定,因此效果立竿见影。在很容易启用/禁用的情况下,这更难实现,因此在web模式下,它被添加为post命令挂钩。因此,如果启用,则在闪烁匹配的括号/大括号/括号后发生。因此有一个短暂的延迟 默认情况下,
web模式启用自动缩进
设置为t
时,启用此效果。以下是相关代码(合并后的附加代码,不是我的):
不幸的是,这并不能解决问题,因为web模式已经纠正了RET上的缩进,所以更改RET上的绑定不会改变任何东西。不过,你的回答让我陷入了学习emacs键绑定到函数的兔子洞(两个小时前,我对lisp一无所知,它变化得很快),所以我正在进一步了解我想要什么。所以我会投票,尽管我现在的代表不允许,因为我指出了正确的方向。我会更新我的问题。
(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."
(local-set-key (kbd "RET") 'newline-and-indent)
(when (and web-mode-enable-auto-indentation
(member this-command '(self-insert-command))
(member (get-text-property (point) 'part-side) '(javascript jsx))
(looking-back "^[ \t]+[]})]"))
(indent-according-to-mode)
;;(message "%S" (point))
(when (and web-mode-change-end (> web-mode-change-end (point-max)))
(message "post-command: enlarge web-mode-change-end")
(setq web-mode-change-end (point-max))
)
)