0

When trying to evaluate an interactive elisp-function which needs an argument in a yasnippet, it gives the error wrong number of arguments. I assume, the reason is, that the function is interactive and thus needs an interactive argument which is not supplied, since it is inside the snippet. This leads to the question:

How can I evaluate an interactive function inside a yasnippet?

.

Simple example snippet:

# name: xxx
# key: xxx
# type: command
# --
(cdlatex-math-modify)

When activating the snippet, I get the error message wrong number or arguments.

1 Answer 1

1

In general, you can use call-interactively to invoke an interactive command from Lisp code, drawing the required arguments from whatever sources are specified in the (interactive) declaration at the top of the command's function definition. It's as though you invoked the same command with M-x or a keybinding.

# name: math-modify
# key: mod
# type: command
# --
(call-interactively #'cdlatex-math-modify)

This even handles prefix arguments correctly. If you want to pass a prefix arg to cdlatex-math-modify when calling it this way, just type C-u [arg] immediately before invoking yas/expand. For instance, after you've entered the template key ("mod" here), type C-u 2 TAB to make cdlatex-math-modify operate on the last two words before point. It will percolate down.

Now, I don't know if this is the function you actually intend to create a snippet for, or if you just picked it as an example. But in this particular case, there's a problem with using a snippet triggered by a character sequence. Specifically, cdlatex-math-modify operates on the character before point, but yasnippet only recognizes template key sequences when it sees them as whole words – that is, when they're preceded by a word separator or at the beginning of a buffer. As a result, this snippet only works to modify word-boundary characters, because that's the only kind of preceding character that can be there when it gets expanded. (You can do C-u 1 TAB as described above, but then it grabs the whole last word and the word boundary following it.) I'd just use a regular keybinding for this purpose.

Thanks for bringing cdlatex-mode to my attention; I wasn't aware of it.

3
  • 2
    FYI see this: github.com/tecosaur/LaTeX-auto-activating-snippets
    – Ian
    Sep 6, 2022 at 8:08
  • 1
    This worked perfectly! Thank you! Yes the plain cdlatex-math-modify was just an example, but one which is very close to my usecase. I wrote a function which f.e. pushes the keystroke > to the cdlatex-math-modify-function, and thus, when i type vec my snippet does the same, that would happen, if i had pressed ' >, and vec is much more convenient on my keyboard. With (setq yas-key-syntaxes '(yas-longest-key-from-whitespace "w_.()" "w_." "w_" "w")) in my init file the Yasnippet also gets expanded, when there is a character in front of it.
    – Sinthoras
    Sep 6, 2022 at 14:19
  • To Ian: This looks interesting, thanks! Right now, I am more or less happy with my set-up using YaSnippet, (which is a slightly modified version of, what this blogpost presented karthinks.com/software/latex-input-for-impatient-scholars) but I have not found a solution for f.e. the subscripts. Maybe I'll try to use just the subscript part of this, if I can figure it out.
    – Sinthoras
    Sep 6, 2022 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.