与 AUCTeX 一起使用时,在 $0 之后以 % 结束行的 yasnippets 行为很奇怪

发布于 2024-12-14 07:32:31 字数 587 浏览 5 评论 0原文

Yasnippet 片段有一个百分号 %,以片段的最后一个点 $0 结束一行,百分号之前的行为很奇怪,因为光标被放置在之后百分号而不是百分号之前。我想知道如何才能避免这种奇怪的行为。

考虑以下代码片段:

# -*- mode: snippet -*-
# name: test snippet
# key: ts
# --
{
$0%
}

我认为当它被激活时,它应该插入三行,其中第一行包含{,最后一行包含},第二行 % 并将光标放在第二行的 % 之前,如下例所示:

{
[cursor]%
}

但会发生以下情况:

{
  % [cursor]
}

如何使代码片段表现良好正如我认为应该的那样?

我的猜测是,这是由于 AUCTeX 中的某些原因造成的,因为它发生在 AUCTeX 激活的情况下,但不是在主要模式 Lisp Interaction 中。

Yasnippet snippets that has a percent sign, %, ending a line with the last point of the snippet, $0, before the percent sign acts strange in that the cursor gets placed after the percent sign and not before it. I wonder how I can avoid this strange behavior.

Consider the following snippet:

# -*- mode: snippet -*-
# name: test snippet
# key: ts
# --
{
$0%
}

I take it that as it's activated it should insert three lines where the first contains {, the last line } and the second line % and place the cursor before % on the second line as in the following example:

{
[cursor]%
}

But what happens is the following:

{
  % [cursor]
}

How can I make it so that the snippet behaves as I think it should?

My guess is that this is due to something in AUCTeX because it happens with AUCTeX activated but not in the major mode Lisp Interaction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

输什么也不输骨气 2024-12-21 07:32:31

它适用于我的配置,但我怀疑它与自动缩进有关(我的配置是经过大量定制的,因此可能有所不同)。 您仍然会看到问题吗

# expand-env: ((yas/indent-line 'fixed))

如果将或

# expand-env: ((yas/indent-line t))

添加到代码片段的标题中, ?您还可以尝试将 $> 添加到您想要缩进的行中,看看这是否会产生影响(如果确实如此,则会大大缩小范围)。 yasnippet 代码中有一条关于标记更改位置的一些问题的注释,但看起来几年前已修复。

您还应该检查 indent-line-function 是否具有正确的值,即 LaTeX-indent-line

您可以在 yas/indent-according-to-mode 的定义中添加一些 sit-for 来查看不同阶段的点。例如,将以下内容放入临时缓冲区中,将光标置于其末尾并键入 Cx Ce。然后照常插入您的代码片段,代码中每当您看到 (sit-for 1) 时,它就会暂停 1 秒。因此,如果光标从错误的位置开始,那么您就知道问题出在缩进之前,等等。您必须监视缩进的每一行,因此您可能希望通过 < 关闭有问题的行之外的缩进代码>$>。添加或删除 sit-for 可以让您缩小范围。

(defun yas/indent-according-to-mode (snippet-markers)
  "Indent current line according to mode, preserving
SNIPPET-MARKERS."
  (sit-for 1)
  (goto-char (yas/real-line-beginning))
  (sit-for 1)
  (let ((trouble-markers (remove-if-not #'(lambda (marker)
                                            (= marker (point)))
                                        snippet-markers)))
    (save-restriction
      (widen)
      (sit-for 1)
      (condition-case err
          (indent-according-to-mode)
        (error (message "[yas] warning: yas/indent-according-to-mode habing problems running %s" indent-line-function)
               nil)))
    (sit-for 1)
    (mapc #'(lambda (marker)
              (set-marker marker (point)))
          trouble-markers)))

It works right with my configuration, but I suspect it has to do with auto indenting (mine is heavily customized so that may be the difference). Do you still see the problem if you add

# expand-env: ((yas/indent-line 'fixed))

or

# expand-env: ((yas/indent-line t))

to the snippet's header? You can also try adding $> to the line(s) that you want indented to see if that makes a difference (if it does that would narrow things down a lot). There is a note in the yasnippet code about some problems with markers changing places, but that looks like it was fixed a few years ago.

You should also check that indent-line-function has the proper value namely LaTeX-indent-line.

You could add some sit-for's to the definition of yas/indent-according-to-mode to see where point is at different stages. For example put the following in a scratch buffer, position your cursor after the end of it and type C-x C-e. Then insert your snippet as usual and it will pause for 1 second every where in the code you see a (sit-for 1). So if the cursor starts out in the wrong place, then you know the problem is before indentation, etc. You will have to watch it for every line that is indented, so you may wish to turn off indentation except for the problematic line via $>. Adding or removing sit-for's will allow you to narrow it down.

(defun yas/indent-according-to-mode (snippet-markers)
  "Indent current line according to mode, preserving
SNIPPET-MARKERS."
  (sit-for 1)
  (goto-char (yas/real-line-beginning))
  (sit-for 1)
  (let ((trouble-markers (remove-if-not #'(lambda (marker)
                                            (= marker (point)))
                                        snippet-markers)))
    (save-restriction
      (widen)
      (sit-for 1)
      (condition-case err
          (indent-according-to-mode)
        (error (message "[yas] warning: yas/indent-according-to-mode habing problems running %s" indent-line-function)
               nil)))
    (sit-for 1)
    (mapc #'(lambda (marker)
              (set-marker marker (point)))
          trouble-markers)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文