Search code examples
emacsyasnippet

Yasnippet: How to expand on " or" but not on "-or"


Title says it, is it possible to alter yasnippets behavior so that it is not possible to expand on "-or" but it is possible to expand on "[newline]or" or "[tab]or" only?

I would like this because I am trying to expand my yasnippets automatically when expansion is possible, and there is only ever confliction when I am writing variable names.


Solution

  • I got the solution here: http://ergoemacs.org/emacs/emacs_tip_yasnippet_expand_whole_hyphenated_word.html

    There is variable (defvar yas-key-syntaxes (list "w" "w_" "w_." "w_.()" "^ ")..), which controls kinds of expansions. "w" means that `"-or" is expanded as separate "or".

    If

    (setq yas-key-syntaxes '("w_" "w_." "w_.()" "^ "))
    

    then "-or" is expanded as complex expression "<some>-or" only. But " or" is expanded as usually.

    So, "buffer-substring" will be expanded only if there is file buffer-substring.yasnippet with content

    # contributor: Xah Lee (XahLee.org)
    # name: buffer-substring
    # key: buffer-substring
    # --
    (buffer-substring START$0 END)
    

    But with "w" keyword "buffer-substring" will be expanded as substring.yasnippet.

    If there is additional string # key: bs in file buffer-substring.yasnippet then "buffer-substring" won't be expanded at all. (For Version: 0.8.0)