关于 emacs 中的自动完成和 yasnippet [英] About auto complete and yasnippet in emacs
问题描述
我在 Emacs 中使用自动完成和 yasnippet,我对它们的设置感到困惑.我将以下代码放在我的 .emacs
中:
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)
我正在尝试将 yasnippet 设置为自动完成弹出菜单中的第一个候选.但是,如下例所示,这不适用于我当前的设置:当我输入单词 for
时,formatItem
位于第一个位置,for
秒.formatItem
只是当前缓冲区中的一个本地函数.
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|
----------------
所以我的问题是:如何让 yasnippet 成为自动完成的第一个候选?我的 .emacs
配置中是否缺少某些内容?
So my question is: how can I make yasnippet the first candidate in auto-complete? And is there something missing in my .emacs
config?
感谢任何帮助.
推荐答案
ac-config-default
安装钩子来设置合理的默认 ac-sources
值.这些挂钩(尤其是 ac-common-setup
)可能会干扰您的设置.
ac-config-default
installs hooks to setup sensible default ac-sources
values. These hooks (especially ac-common-setup
) might interfere with your settings.
您可以通过查看 中
缓冲区,通过与您的自定义值进行比较来查看它是否已被修改.ac-sources
(Ch vac-sources
) 的实际值来检查这一点js-mode
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:
- 停止使用
ac-config-default
并指定所有autocomplete
设置, - 建议有问题的钩子在运行后将
ac-source-yasnippet
放在ac-sources
的开头.假设 ac-common-setup 确实是麻烦制造者,这看起来像:
- stop using
ac-config-default
and specifiy allautocomplete
settings, - advise the faulty hook to put
ac-source-yasnippet
at the beginning ofac-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))
这篇关于关于 emacs 中的自动完成和 yasnippet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!