如何才能使emacs自动插入闭式大括号 [英] How can can I get emacs to insert closing braces automatically

查看:162
本文介绍了如何才能使emacs自动插入闭式大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用emacs,而且有一个我真正喜欢的功能,搜索有点无果。我希望别人这样做,因为我不想再学习elisp。

  void foo()< cursor> ; 

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

  void foo(){
< cursor>
}

我希望这只能在cc模式下发生,只有在结束一行,当不在一个字符串/评论/ etc



首先要想到的是重新绑定{来做这个(我可以弄清楚如何自己这样做),但是很难让它只在正确的时间发生。



任何提示都将不胜感激。

解决方案

这样做:

 (defun my- c-mode-insert-lcurly()
(interactive)
(插入{)
(let((pps(syntax-ppss)))
(eolp)(不是(或(nth 3 pps)(nth 4 pps))));; EOL而不是字符串或注释
(c-indent-line)
(插入\\\

(c-indent-line)
(forward-line -1)
(c-indent-line))))

(define-key c-mode-base-map{'my-c-mode-insert-lcurly)


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>
}

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.

any hints would be appreciated.

解决方案

This will do it:

(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)

这篇关于如何才能使emacs自动插入闭式大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆