Dan LaManna
Dan LaManna

Reputation: 3501

Properly handing failed snippet completions

I use TAB to expand snippets from yasnippet, when it doesn't expand a snippet, it usually falls back to indenting (the default command bound to TAB), this is referred to by yasnippets custom variable yas-fallback-behavior which can only be call-other-command or return-nil.

The desired functionality I want is upon hitting TAB:

  1. Tries to complete snippet, succeeds, or fails silently.
  2. Checks if we're in PHP mode, and calls php-complete-function, succeeds, or fails silently.
  3. Defaults to it's current indentation command (indent-for-tab-command).

It currently performs 1, then 3, perfectly. I was however, able to make it work for the most part by advising yas--fallback with this bit of code:

(defadvice yas--fallback (before try-php-expand (&optional arg from-trigger-key-p) activate)
  (when (string-equal mode-name "PHP")
    (php-complete-function)))

The only main issue that remains is that when trying to indent anything using TAB, php-complete-function does not fail silently, but instead spams the minibuffer with messages from it's multiple checks for a PHP completion.

So is there a way to disallow messaging from that function in this case, without doing all the same checks it does essentially re-programming the function in my advise? Alternatively, is there a better way to do this with yasnippet to begin with that I'm missing?

Upvotes: 2

Views: 109

Answers (2)

Stefan
Stefan

Reputation: 28531

You could try something like (guaranteed 100% untested):

(defvar my-inhibit-messages nil)
(defadvice message (around my-inhibit activate)
  (unless my-inhibit-messages ad-do-it))

(defadvice php-complete-function (around my-silence-php-messages activate)
  (let ((my-inhibit-messages t))
    ad-do-it))

Upvotes: 0

pmr
pmr

Reputation: 59811

You can turn of logging in any expression by setting message-log-max to nil.

(defadvice yas--fallback (before try-php-expand (&optional arg from-trigger-key-p) activate)
  (when (string-equal mode-name "PHP")
    (let (message-log-max)
      (php-complete-function))))

Upvotes: 0

Related Questions