Solution 1
(add-hook 'yas-after-exit-snippet-hook
(lambda () (if (buffer-substring yas-snippet-beg yas-snippet-end)
(bm-toggle))))
The yas-after-exit-snippet-hook
runs in an environment where yas-snippet-beg
and yas-snippet-end
are defined. These give the beginning and end points (i.e. region) of the inserted snippet.
The function buffer-substring
returns the contents of a given region within the current buffer. We can use this with yas-snippet-beg
and yas-snippet-end
. If the snippet worked, then there will be text in the region. We expect the snippet to either work or fail gracefully, not defining the snippet region or running the hook. Anything non-nil evaluates to t
. So, when we insert a snippet, the conditional will trigger and run the toggle.
Since it's unlikely we'd use this functionality anywhere else, there's no point defining an explicit function. The use of lambda
creates an anonymous function. This function is added to the hook. When the hook is called, our function is created, called, and destroyed.
Solution 2
Another possibility is to search for the string explicitly.
(add-hook 'yas-after-exit-snippet-hook
(lambda () (if (string-match-p (regexp-quote "import ipdb; ipdb.set_trace()") (thing-at-point 'line t))
(bm-toggle))))
This code checks for the thing-at-point
on the current line. A quirk is that (thing-at-point 'line)
returns a string with a newline at the end. So, instead of matching against the snippet "character-for-character", we can match it with a regex and accept any line containing the snippet.
yas-after-exit-snippet-hook
should be sufficient if you can recognize the snippet from its expansion.