我怎样才能让 emacs 自动插入右大括号 [英] How can can I get emacs to insert closing braces automatically
问题描述
我刚开始使用 emacs,有一个功能我非常喜欢,但四处搜索无果.我希望其他人已经这样做了,因为我现在还不想学习 elisp.
I've just started using emacs, and there's one feature I'd really like, and searching around a bit was fruitless. I hope someone else has done this because I don't want to learn elisp just yet.
void foo()<cursor>
我想输入一个{"来导致这种情况发生
I would like typing an "{" to cause this to happen
void foo(){
<cursor>
}
我希望这仅在 cc 模式下发生,并且仅在不在字符串/注释/等中时发生在行尾
I would like this to only happen in cc-mode, and only at the end of a line when not in a string/comment/etc
首先想到的是重新绑定{"以始终执行此操作(我自己可以弄清楚如何执行此操作),但是很难让它仅在正确的时间发生.
The first thing that came to mind was rebinding "{" to do this always(I could figure out how to do this myself), but it would be hard to make it only happen at the right time.
任何提示将不胜感激.
推荐答案
这样做:
(defun my-c-mode-insert-lcurly ()
(interactive)
(insert "{")
(let ((pps (syntax-ppss)))
(when (and (eolp) (not (or (nth 3 pps) (nth 4 pps)))) ;; EOL and not in string or comment
(c-indent-line)
(insert "
}")
(c-indent-line)
(forward-line -1)
(c-indent-line))))
(define-key c-mode-base-map "{" 'my-c-mode-insert-lcurly)
这篇关于我怎样才能让 emacs 自动插入右大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!