Public-Fan-8904 · 2022-04-18 12:00
Consider the following snippets:
`# -*- mode: snippet -*-
# name: tdp
# key: ^^
# group: math
# condition: (and (texmathp) 'auto)
# --
^{$1}$0
and
# -*- mode: snippet -*-
# name: set
# key: set
# group: math
# condition: (and (texmathp) 'auto)
# --
\\{ $1 \\}
I would like to expand on different conditions. By default Yasnippet only expands at word boundaries, so for example fooset doesn't expand, you have to type a space between the snippet that you want to expand. As you can see this is a problem with the first snippet because I would like to expand x\^\^3 to x\^{3}. I looked for a solution to this problem and found this
(setq yas-key-syntaxes '(yas-longest-key-from-whitespace "w_.()" "w_." "w_" "w"))
which let's me expand snippet on non-word boundaries. So now I can expand x\^\^3 to x\^{3}.
However now the problem I have is with the set snippet. I would like that the set snippet follows the default behavior because when I type \\subset the snippet is triggered when I type "set". Is there any way to tell yasnippet to expand the first snippet always and the second one on word boundaries?
redguardtoo · 2022-04-18 12:57
I'm not a yansnippet expert. But as I understand, the lisp function could be embedded inside the snippet. Like,
# -*- coding: utf-8; mode: snippet -*-
# name: console.log which function with var list from `kill-ring'
# key: lwf
# contributor: somebody
# --
`(my-whatever-snippet-function)`
You can write any code in `my-whatever-snippet-function`
Public-Fan-8904 · 2022-04-18 13:08
In which snippet should I introduce this code? Are you talking about '(yas-longest-key-from-whitespace "w\_.()" "w\_." "w\_" "w")? Like for example copying that in the beginning of the tdp snippet? Sorry I'm kind of a beginner in this.
redguardtoo · 2022-04-18 13:26
second snippet. you can check word boundary in `my-whatever-snippet-function` which is just a function defined in `~/.emacs.d/init.el`.
You could use builtin api like `insert`, `forward-char`, `backward-char`, `thing-at-point`, `save-excursion`, etc.
Public-Fan-8904 · 2022-04-18 15:04
I don't know if this would help because I need to apply a function to determine if the snippet should be expanded or not. A function in the snippet body would run after the snippet triggered.
redguardtoo · 2022-04-19 00:48
If you mean the `condition directive`, you can still use elisp function, like
# -*- coding: utf-8; mode: snippet -*-
# name: console.log(description,object)
# key: l1
# condition: (my-test)
# contributor: Chen Bin <chenbin DOT sh AT gmail>
# --
console.log('${1:object}=', ${1:$(yas/substr yas-text "[=:, ]*\\\\([^=:, ]*\\\\)" 1)});
`my-test` is just a function defined in `~/.emacs.d/init.el` by you.
You can also customize `yas-buffer-local-condition
` to set up condition per buffer. See https://joaotavora.github.io/yasnippet/snippet-development.html#org185b594 for documentation.
Public-Fan-8904 · 2022-04-19 13:49
I couldn't figure it out but thanks! I'll check the link you suggested