在括号对后将Emacs设置为智能自动线? [英] Set Emacs to smart auto-line after a parentheses pair?

查看:133
本文介绍了在括号对后将Emacs设置为智能自动线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有电对模式(这不是真正特别相关的,因为这可能适用于任何自动配对模式或甚至手动括号),但是简而言之,我想要这样,以便我有这样的情况:

I have electric-pair-mode on (which isn't really particularly relevant, as this could apply to any auto-pairing mode or even manual parens), but in a nutshell, I'd like it so that in the case I have:

function foo(){|}

(其中 | 是标记)

如果我按Enter键,我想自动进入

If I press enter, I would like to have it automatically go to

function foo() {
|
}

这也意味着

function foo(|){}

将成为

function foo(
|
){}

我已经有事情要照顾缩进,但我不知道如何说如果我在任何空的一对匹配的括号内,当我按回车,实际上插入两个新线,把我放在第一个。

I already have things to take care of the indentation, but I'm not sure how to say "if I'm inside any empty pair of matched parenthesis, when I press return, actually insert two new lines and put me at the first one".

谢谢!

推荐答案

这是我在init档案,我从Magnar Sveen的 .emacs.d

Here is what I have in my init file, I got this from Magnar Sveen's .emacs.d

(defun new-line-dwim ()
  (interactive)
  (let ((break-open-pair (or (and (looking-back "{") (looking-at "}"))
                             (and (looking-back ">") (looking-at "<"))
                             (and (looking-back "(") (looking-at ")"))
                             (and (looking-back "\\[") (looking-at "\\]")))))
    (newline)
    (when break-open-pair
      (save-excursion
        (newline)
        (indent-for-tab-command)))
    (indent-for-tab-command)))

您可以将其绑定到您选择的键。我已经将它绑定到 M-RET ,但如果需要,可以将其绑定到 RET 。线条

You can bind it to a key of your choice. I have bound it to M-RET but if you want, you can bind it to RET. The lines

(or (and (looking-back "{") (looking-at "}"))
    (and (looking-back ">") (looking-at "<"))
    (and (looking-back "(") (looking-at ")"))
    (and (looking-back "\\[") (looking-at "\\]")))

检查光标是否在 {|} [|] ($)$ / code>或> |< (html)

check if cursor is at {|}, [|], (|) or >|< (html).

这篇关于在括号对后将Emacs设置为智能自动线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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