2014-08-27 74 views
1

我想在注释和字符串中禁用YASnippet扩展(例如,“if”),但没有找到如何以通用的方式做到这一点。如何永远扩展评论和字符串中的yasnippets

http://capitaomorte.github.io/yasnippet/snippet-expansion.html#sec-2-2上,他们说如何为Python做到这一点,但我希望能够一次为所有编程模式工作,并且我不知道任何测试“在字符串/注释”中的函数,与语言无关。

还有办法吗?

+1

也许你可以尝试测试评估'(第n个8(语法PPSS))'在其它模式下,看它是否返回't'或'零'在字符串和评论?我不知道这是否会起作用,但它似乎是你所链接的功能所做的。 – lawlist 2014-08-27 16:01:30

+0

另请参阅http://stackoverflow.com/questions/12815781/emacs-lisp-and-c-mode-when-am-i-in-a-comment-region/12820339#12820339 – Stefan 2014-08-27 17:18:38

回答

2

使用lawlist的建议,并将其添加到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) 
+0

这的确行得通。太感谢了! – user3341592 2014-08-28 09:59:50