我有electric-pair-mode
开启(这并不是特别相关,因为这可能适用于任何自动配对模式甚至是手动配对),但简而言之,我希望它能够在我有:
function foo() {|}
(其中|
是标记)
如果我按回车键,我想自动转到
function foo() {
|
}
这也意味着
function foo(|) {}
会变成
function foo(
|
){}
我已经有了处理缩进的事情了,但是我不知道如何说“如果我在任何一对空的匹配括号内,当我按下返回时,实际插入两条新线并把我放在第一个”。
谢谢!
答案 0 :(得分:5)
以下是我在init文件中的内容,我是从Magnar Sveen的.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
。行
(or (and (looking-back "{") (looking-at "}"))
(and (looking-back ">") (looking-at "<"))
(and (looking-back "(") (looking-at ")"))
(and (looking-back "\\[") (looking-at "\\]")))
检查光标是否在{|}
,[|]
,(|)
或>|<
(html)。
答案 1 :(得分:1)
您可能还想查看smartparens
。具体来说,请参阅insertion hooks上的页面。
这是我个人使用的配置:
(with-eval-after-load 'smartparens
(sp-with-modes
'(c++-mode objc-mode c-mode)
(sp-local-pair "{" nil :post-handlers '(:add ("||\n[i]" "RET")))))
这还有一个额外的好处,就是自动缩进当前行。如果您愿意,可以很容易地将其推广到更多模式(对全局对使用sp-pair
)和paren类型(只需复制代码)。