Search code examples
emacsautocompleteexpansionyasnippet

How to never expand yasnippets in comments and strings


I'd like to disable YASnippet expansion (for example, if) in comments and strings, but don't find how to do that in a generic way.

On The condition system, they say how to do it for Python, but I'd like to get it working for all prog-modes at once, and I'm not aware of any function which tests "in string/comment", independently of the language.

Is there still a way to do so?


Solution

  • Using lawlist's suggestion and adding it to prog-mode-hook:

    (defun yas-no-expand-in-comment/string ()
      (setq yas-buffer-local-condition
            '(if (nth 8 (syntax-ppss)) ;; non-nil if in a string or comment
                 '(require-snippet-condition . force-in-comment)
               t)))
    (add-hook 'prog-mode-hook 'yas-no-expand-in-comment/string)