我怎样才能让emacs自动插入关闭括号

Bwm*_*mat 6 emacs curly-braces cc-mode

我刚刚开始使用emacs,并且有一个我非常喜欢的功能,并且搜索一下也没用.我希望其他人这样做是因为我还不想学习elisp.

void foo()<cursor>
Run Code Online (Sandbox Code Playgroud)

我想输入一个"{"来导致这种情况发生

void foo(){
    <cursor>
}
Run Code Online (Sandbox Code Playgroud)

我希望这只发生在cc模式中,并且只在不在字符串/ comment/etc中的行尾

我想到的第一件事是重新绑定"{"来做到这一点(我可以弄清楚如何自己做),但很难让它只在恰当的时间发生.

任何提示将不胜感激.

Chm*_*nah 9

在最新的emacs上你可以使用:

electric-pair-mode is an interactive compiled Lisp function.

(electric-pair-mode &optional ARG)

Automatically pair-up parens when inserting an open paren.
Run Code Online (Sandbox Code Playgroud)

这是集成在Emacs 24.1(实际上是CVS)中


sco*_*zer 5

这样做:

(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 "\n\n}")
      (c-indent-line)
      (forward-line -1)
      (c-indent-line))))

(define-key c-mode-base-map "{" 'my-c-mode-insert-lcurly)
Run Code Online (Sandbox Code Playgroud)