yasnippet 0.7.0和auto-complete-mode一起使用

I will only talk about how to set up yasnippet 0.7.0 or higher version.

Table of Contents

  • 1 If you follow `normal install` way, setup is simple
  • 2 If you use elpa package system, setup is even simpler
  • 3 The real world setup is NOT simple
    • 3.1 Two snippets may share the same key, so I need activate `yas/dropdown-prompt'
    • 3.2 `yas/dropdown-prompt' is not perfect
    • 3.3 Yasnippets conflicts with other plugins
  • 4 My final yasnippet setup

1 If you follow `normal install` way, setup is simple

See its official documentation.

Install the yasnippet into somewhere and add following code into your .emacs:

(add-to-list 'load-path
              "~/.emacs.d/plugins/yasnippet-x.y.z")
(require 'yasnippet) ;; not yasnippet-bundle
(yas/initialize)
(yas/load-directory "~/.emacs.d/plugins/yasnippet-x.y.z/snippets")

2 If you use elpa package system, setup is even simpler

After installation, you only need two lines in .emacs.

(require 'yasnippet)
(yas/initialize)

Yasnippet 0.7.0 have already defined two locations for the snippets, "~/.emacs.d/yasnippet-install-path/yasnippet-x.y.z/snippets" and "~/.emacs.d/snippets". Yasnippet will load snippets in BOTH directories at startup.

So you only put your own snippets in "~/.emacs.d/snippets" and done. No need to tweak .emacs at all. To verify my claim, you can `C-h v yas/snippet-dirs' to check value of "yas/snippet-dirs". Please note "yas/root-directory" is the alias of "yas/snippet-dirs".

3 The real world setup is NOT simple

I will explain the reasons at first and give my complete yasnippet configuration code at the end of the this post.

3.1 Two snippets may share the same key, so I need activate `yas/dropdown-prompt'

One issue is I need a user-friendly dropdown window popped up when the key I input has several candidates. For example, when I type "inc" in C code. There are two candicates `#include "…"' and `#include <…>' available. A handy dropdown popup will help me to choose one of them as efficient as possible.

The good news is such fancy popup is a standard component of yasnippet. It's called `yas/dropdown-prompt'. Yasnippet's default algorithm will activate it at highest prority.

The bad news is for some wierd reason yasnippet won't load that dropdown-list by default. So you need manually load that component by one line of elisp code `(require 'dropdown-list)'.

3.2 `yas/dropdown-prompt' is not perfect

I cannot scroll down the dropdown window when there are more candidates it can display. That's especially annoying when calling `yas/insert-snippet'. In this case, we need use `yas/completing-prompt' instead. I will show the fix at the end of this article.

3.3 Yasnippets conflicts with other plugins

I use Auto Complete Mode (version 20120327 in elpa). There are two issuses when using it with yasnippets.

First, it use TAB key to do the auto-complete thing while yasnippet also uses TAB key. So I need re-configure hotkeys of yasnippets.

Second, yansippet changed its API `yas/get-snippet-tables' since version 0.7.0. This make the auto-complete cannot use yasnippet at all. This issue is reported and fixed by tkf. Actually all you need do is simple:

cd auto-complete-install-dir
rm auto-complete-config.elc
curl -L https://raw.github.com/tkf/auto-complete/337caa2ccc254a79f615bb2417f0d2fb9552b547/auto-complete-config.el > auto-complete-config.el

4 My final yasnippet setup

(require 'yasnippet)
(yas/initialize)
;; default TAB key is occupied by auto-complete
(global-set-key (kbd "C-c ; u") 'yas/expand)
;; default hotkey `C-c & C-s` is still valid
(global-set-key (kbd "C-c ; s") 'yas/insert-snippet)
;; give yas/dropdown-prompt in yas/prompt-functions a chance
(require 'dropdown-list)
;; use yas/completing-prompt when ONLY when `M-x yas/insert-snippet'
;; thanks to capitaomorte for providing the trick.
(defadvice yas/insert-snippet (around use-completing-prompt activate)
     "Use `yas/completing-prompt' for `yas/prompt-functions' but only here..."
       (let ((yas/prompt-functions '(yas/completing-prompt)))
             ad-do-it))

你可能感兴趣的:(yasnippet 0.7.0和auto-complete-mode一起使用)