You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm writing some code to auto insert a snippet into files by major-mode. I have an alist for major-mode -> snippet name, but it seems like the snippet keys are a more straightforward way to identify a snippet... except I can't find a function to lookup a snippet by key. Is there such a thing, something like yas-lookup-snippet?
Also if someone's already done this, feel free to point me in that direction.
FYI, here's my current code:
;; Define an alist mapping file extensions to yasnippet names (*not* keys!)
(defvarmy-extension-snippet-alist
'(("cpp"."C++ CPP source file header")
("cxx"."C++ CPP source file header")
("h"."C++ CPP header file header")
("hpp"."C++ CPP header file header")
("hxx"."C++ CPP header file header"))
"Alist mapping file extensions to yasnippet names.")
(defunmy-auto-insert-snippet ()
"Automatically insert a specific yasnippet when creating a new file based on its extension."
(when (and buffer-file-name
(zerop (buffer-size)))
(let* ((file-extension (file-name-extension buffer-file-name))
(snippet-name (assoc-default file-extension my-extension-snippet-alist)))
(condition-case err
(when snippet-name
(yas-expand-snippet (yas-lookup-snippet snippet-name))) ;;; here's where I'd like to look up by key instead
(error;; maybe no such snippet
(message (error-message-string err))
nil)
))))
(add-hook'find-file-hook'my-auto-insert-snippet)
The text was updated successfully, but these errors were encountered:
I'm writing some code to auto insert a snippet into files by major-mode. I have an alist for major-mode -> snippet name, but it seems like the snippet keys are a more straightforward way to identify a snippet... except I can't find a function to lookup a snippet by key. Is there such a thing, something like
yas-lookup-snippet
?Also if someone's already done this, feel free to point me in that direction.
FYI, here's my current code:
The text was updated successfully, but these errors were encountered: