Question

I'm using auto-complete and yasnippet in Emacs and I am confused by their settings. I placed the following code in my .emacs:

(add-to-list 'load-path "~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas/global-mode 1)
(global-set-key (kbd "C-i") 'yas/expand)
(setq yas/also-auto-indent-first-line t)

(add-to-list 'load-path "~/.emacs.d/plugins/autocomplete/")
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/plugins/autocomplete/ac-dict")
(ac-config-default)
(setq ac-use-menu-map t)
(define-key ac-menu-map "\C-n" 'ac-next)
(define-key ac-menu-map "\C-p" 'ac-previous)

(defun ac-js-mode()
(setq ac-sources '(ac-source-yasnippet
                 ac-source-symbols
                 ac-source-words-in-buffer
                 ac-source-words-in-same-mode-buffers
                 ac-source-files-in-current-dir
                 )))
(add-hook 'js-mode-hook 'ac-js-mode)

I am trying to set yasnippet as the first candidate in the auto-complete popup menu. However, as the example below shows, this doesn't work with my current settings: when I type the word for, formatItem is in first position and for in second. formatItem is just a local function in current buffer.

 for_____________
|formatItem      |
|for            a|
|for            s|
|force          s|
|foreachv       s|
 ----------------

So my question is: how can I make yasnippet the first candidate in auto-complete? And is there something missing in my .emacs config?

Any help is appreciated.

Was it helpful?

Solution

ac-config-default installs hooks to setup sensible default ac-sources values. These hooks (especially ac-common-setup) might interfere with your settings.

You can check this by looking at the actual value of ac-sources (C-h vac-sources) in a js-mode buffer to see if it has been modified by comparison to your custom value.

If this is the case, I see two solutions:

  • stop using ac-config-default and specifiy all autocomplete settings,
  • advise the faulty hook to put ac-source-yasnippet at the beginning of ac-sources after it has run. Assuming ac-common-setup is indeed the troublemaker, this would look like:
(defadvice ac-common-setup (after give-yasnippet-highest-priority activate)
  (setq ac-sources (delq 'ac-source-yasnippet ac-sources))
  (add-to-list 'ac-sources 'ac-source-yasnippet))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top