编写包含 Emacs Lisp 代码的片段时出现问题
我一直在尝试利用 YASnippet 的一个很酷的功能:编写包含嵌入式 Emacs Lisp 代码的片段。 中的文本一样长
有一个用于 rst-mode 的代码片段,用“=”包围输入的文本,该文本与====
Text
= ===
基于这个片段,我决定稍微修改它(使用 Elisp),以便它根据您所处的主要模式注释掉这三行(我认为这样的片段对于组织源代码)。所以我写了这个:
${1:`(insert comment-start)`} ${2:$(make-string (string-width text) ?\-)}
$1 ${2:Text}
$1 ${2:$(make-string (string-width text) ?\-)}
$0
这段代码工作得相对很好,除了一个问题:这三行的缩进会混淆,具体取决于我所处的主要模式(例如,在 emacs-lisp-mode 中,第二行和第三行比第一行更向右移动)。
我认为问题的根源可能与第一行字符串 ${1:
后面的内容有关。如果我添加一个字符,就没有问题(即,所有三行在代码片段扩展的末尾都正确对齐)。如果我在此字符串后添加一个空格,则未对齐问题仍然存在。
所以我的问题是:你知道有什么方法可以重写这个片段,这样就不会出现这种错位吗?你知道这种行为的根源是什么吗?
干杯,
I've been trying to make use of a cool feature of YASnippet: write snippets containing embedded Emacs Lisp code. There is a snippet for rst-mode that surrounds the entered text with "=" that is as long as the text such as in
====
Text
====
Based on this snippet, I decided to slightly modify it (with Elisp) so that it comments out these three lines depending on the major mode you are in (I thought that such a snippet would be useful to organize the source code). So I wrote this:
${1:`(insert comment-start)`} ${2:$(make-string (string-width text) ?\-)}
$1 ${2:Text}
$1 ${2:$(make-string (string-width text) ?\-)}
$0
This code works relatively well except for one problem: the indentation of these three lines gets mixed up, depending on the major mode I'm in (e.g., in emacs-lisp-mode, the second and the third lines move more to the right than the first line).
I think the source of the problem might have something to do with what comes after the string ${1:
on the first line. If I add a character, I have no problem (i.e., all three lines are correctly aligned at the end of the snippet expansion). If I add a single space after this string, the misalignment problem still continues though.
So my question is: do you know of any way of rewriting this snippet so that this misalignment does not arise? Do you know what's the source of this behaviour?
Cheers,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自编写片段:
From Writing snippets: