About auto complete and yasnippet in emacs

久未见 提交于 2019-11-30 03:51:43

问题


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.


回答1:


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))


来源:https://stackoverflow.com/questions/9869178/about-auto-complete-and-yasnippet-in-emacs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!