yasnppet 有办法让它每次展开的时候都执行一遍嵌入的elisp代码吗

默认yasnippet 只会在第一次展开的时候执行elisp 代码,怎么让他每次展开的时候都执行一遍啊

正常情况下应该就是每次展开的时候都执行的。

可能我理解错了你的意思,给个例子看看。

# -*- mode: snippet -*-
# name: NewLogicErr
# key: NL
# --

return NewLogicErr("`(concat (car (split-string (which-function) "(")) " failed")`", "$1", ${2:err}, ${3:code.ErrInterval})

比如这一段,我本意是每次展开都捕获当前所在代码块所在的函数名的,但是我发现除了第一次可以正确捕获函数名,后面再展开 函数名都是第一次展开时捕获的。

后面我看了官方文档,发现同一个buf 就只会执行一次嵌入的elisp代码。

# -*- mode: snippet -*-
# name: test
# key: test
# --
(message "`(concat (car (split-string (which-function) "(")) " failed")`")

Peek 2022-10-13 00-01

确定不是你配置的问题 ?

方便给个链接吗?我好像没找到。

https://joaotavora.github.io/yasnippet/snippet-development.html#orgcde188c

在Embedded lisp code 那一节

里面没有说吧?

Emacs-Lisp code can be embedded inside the template, written inside back-quotes (``` ). The lisp forms are evaluated when the snippet is being expanded. The evaluation is done in the same buffer as the snippet being expanded.

奇怪,为啥看你那边是可以的,我这边就有问题

而且,我记得我之前也是可以正常捕获的

我感觉是你理解错了,这说的就是每次展开时执行。

请排除自己配置的影响。

好烦,我这边就不得。。。

我这边又得了。。。。

我记得 yasnippet 有个缓存,下次可以删除下试试

多用 emacs -Q,少一些乌龙。

是which-func-mode 捕获函数名有问题,我开了which-func-mode 以后发现捕获的函数名是不对的

你说的 which-func-mode 的问题是不是这个?

(with-temp-buffer
  (emacs-lisp-mode)
- (insert "(defun foo ()\n(bar))")
+ (insert "(defun foo ()\n (bar))")
  (backward-char)
  (which-function)
- ) ;;=> nil
+ ) ;;=> "foo"

不是这个,是获取函数名会错误