编写包含 Emacs Lisp 代码的片段时出现问题

发布于 2024-10-09 09:05:12 字数 716 浏览 0 评论 0原文

我一直在尝试利用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻之岛 2024-10-16 09:05:12

来自编写片段

yas/缩进线

变量 yas/indent-line 控制缩进。默认情况下它绑定到“auto”,这会导致您的代码片段根据插入的缓冲区模式进行缩进。

另一个变量 yas/also-auto-indent-first-line,当非 nil 时正是这样做的:-)。

要在代码片段模板中使用硬编码缩进,请将此变量设置为固定。

要控制每个代码段的缩进,另请参阅“编写代码段”中的 #expand-env: 指令。

为了向后兼容早期版本的 YASnippet,您还可以将 $>在您的代码片段中,将执行 (indent-according-to-mode) 来缩进该行。仅当 yas/indent-line 设置为“auto”以外的其他值时,此操作才会生效。

for (${int i = 0}; ${i < 10}; ${++i})
{
gt;
$0
gt;
}
gt;

From Writing snippets:

yas/indent-line

The variable yas/indent-line controls the indenting. It is bound to 'auto by default, which causes your snippet to be indented according to the mode of the buffer it was inserted in.

Another variable yas/also-auto-indent-first-line, when non-nil does exactly that :-).

To use the hard-coded indentation in your snippet template, set this variable to fixed.

To control indentation on a per-snippet basis, see also the directive # expand-env: in Writing Snippets.

For backward compatibility with earlier versions of YASnippet, you can also place a $> in your snippet, an (indent-according-to-mode) will be executed there to indent the line. This only takes effect when yas/indent-line is set to something other than 'auto.

for (${int i = 0}; ${i < 10}; ${++i})
{
gt;
$0
gt;
}
gt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文