Is there a way to conditionally select the mode on which yasnippet depends?

67 views
Skip to first unread message

Patrick Seebauer

unread,
Oct 27, 2014, 10:33:22 AM10/27/14
to smart-...@googlegroups.com
I'm using web-mode and I want yasnippet to use the modes according to (web-mode-language-at-pos)?

is that possible?

This is what I tried (in .emacs):

(defun expand-for-web-mode ()
 
(when (equal mode-name "Web") ; not sure if that's the preffered way
    (setq yas-extra-modes
      (let ((web-lang (web-mode-language-at-pos)))
        (when (equal web-lang "html")       '
(html-mode))
       
(when (equal web-lang "css")        '(css-mode))
        (when (equal web-lang "javascript") '
(js-mode) '(js2-mode))
        ))
    ))
(add-hook '
yas-before-expand-snippet-hook 'expand-for-web-mode)


it only works the first time a snippet is expanded, is there some other hook I haven't seen, or should I use another keybinding for <TAB> in web-mode and set the variable there (and then call yasnippet/expand-snippet)?

Patrick Seebauer

unread,
Oct 28, 2014, 10:50:01 AM10/28/14
to smart-...@googlegroups.com
I realized that I need to use (cond) here, but the problem still stands, here is my updated code:


(defun expand-for-web-mode ()
  (when (equal mode-name "Web")
    (make-local-variable 'yas-extra-modes)

    (setq yas-extra-modes
      (let ((web-lang (web-mode-language-at-pos)))
        (cond
         ((equal web-lang "html")       '(html-mode))
         ((equal web-lang "css")        '(css-mode))
         ((equal web-lang "javascript") '(javascript-mode))
         )))))
(add-hook 'yas-before-expand-snippet-hook 'expand-for-web-mode)


Am Montag, 27. Oktober 2014 15:33:22 UTC+1 schrieb Patrick Seebauer:
I'm using web-mode and I want yasnippet to use the modes according to (web-mode-language-at-pos)?

is that possible?



  … faulty code 
Reply all
Reply to author
Forward
0 new messages