Emacs css-mode 自动关闭大括号并在冒号后自动插入分号(ala Textmate)
我正在寻找一种在 Emacs 中模仿 Textmate 的 CSS 编辑行为的方法。
在 Textmate 中,添加 CSS 属性时:
#element {} <-- Braces auto close.
#element {background: ;} <-- after typing the colon, the semi-colon is automatically inserted and the cursor is placed between them.
我查看了几种 css 模式(和 textmate.el),但看不到有人实现了此功能。
我对 emacs-lisp 的了解绝对为零,愿意尝试一下并自己写一些东西,但有人知道这是否已经完成了吗?
I'm looking for a way to mimic Textmate's CSS editing behavior in Emacs.
In Textmate, when adding a CSS property:
#element {} <-- Braces auto close.
#element {background: ;} <-- after typing the colon, the semi-colon is automatically inserted and the cursor is placed between them.
I have taken a look at several css-modes (and textmate.el) but cannot see that anyone has implemented this.
I have absolutely zero knowledge of emacs-lisp would be be willing to give it a shot and write something myself, but does anyone know if this has been done already?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想查看一些
electric
函数的示例(在键入特定可见字符时执行附加输入或格式化时使用的命名约定)。实现上没有什么特别的。有问题的键在模式的键映射内绑定到执行该工作的函数。除了必须处理键入的字符的插入这一事实之外,它就像任何其他键绑定一样。
cc-mode 有几个例子。基本方法如下所示:
诚然,c-electric-brace 是一个比您预期的复杂得多的函数,但拥有一个简单地插入相同数量的
}
的函数是微不足道的插入{
后。您可能会发现这有点简单,但是在某些情况下您不希望插入匹配的大括号。您可能还希望通过定义电动删除函数,在删除其中一个大括号时自动删除匹配的大括号。
你的电动冒号要求也不像大括号那么简单,因为它应该只出现在正确的上下文中(尽管在实践中你可能会逃避一个幼稚的实现,因为我不认为你会在 CSS 文件中键入冒号 。
如果您决定编写自己的解决方案,希望这能为您指明正确的方向
显然你想阅读一些关于 elisp 的教程,但是为了理解上面的代码,请注意你可以使用 Ch f (
describe-function
) 来阅读以下文档:任何 elisp 函数(或 Mxfind-function
查看代码)。You want to look at some examples of
electric
functions (the naming convention used when additional input or formatting is performed when particular visible characters are typed).There's nothing special about the implementation. The key in question is bound within the mode's keymap to a function which does the work. Aside from the fact that you must handle the insertion of the character typed, it's just like any other keybinding.
cc-mode has several examples. The basic approach looks like this:
Admittedly c-electric-brace is a far more complicated function than you might be expecting, but it would be trivial to have a function which simply inserted the same number of
}
s after inserting the{
s.You might find that is a little simplistic, and there are circumstances in which you don't want the matching brace to be inserted, however. You would probably also want to deal with automatically removing matching braces when you delete one of them, by defining electric delete functions.
Your electric colon requirement is also less trivial than the brace, as it should only occur in the right context (although in practice you might get away with a naive implementation, as I don't think you would be typing colons in a CSS file that were not in the correct context.)
Hopefully this points you in the right direction, if you decide to write your own solutions.
Obviously you would want to read some tutorials on elisp, but for understanding the code above, just note that you can use C-h f (
describe-function
) to read the documentation for any elisp function (or M-xfind-function
to view the code).对于各种牙套的通用自动配对等,您可能需要查看autopair-mode。
For general purpose autopairing of all sorts of braces, etc you might want to take a look at autopair-mode.