使用go-mode进行自动补全

huangapple go评论91阅读模式
英文:

Auto-complete with go-mode

问题

我试图在通过go-mode加载的.go文件时启用auto-complete-mode。如果我手动为Go源文件调用auto-complete-mode,它可以工作,但是当我尝试将其添加到.emacs文件中时,它不起作用:

(add-hook 'go-mode-hook 'auto-complete-mode)

我尝试了几个变化,但似乎都不起作用。以下是我.emacs文件中Go-Mode代码片段的当前样子:

;; 加载Go Mode
(require 'go-mode-load)
(add-hook 'go-mode-hook 'auto-complete-mode)

我尝试创建自己的hook函数,像这样:

;; 加载Go Mode
(require 'go-mode-load)
(defun auto-complete-for-go ()
  (auto-complete-mode 1))
(add-hook 'go-mode-hook 'auto-complete-for-go)

我还尝试将hook包含在go-mode-load.elgo-mode.el中,以及像这样调用auto-complete-mode

(auto-complete-mode t)
(provide 'go-mode)

无论哪种方式都不起作用。我还将go-mode-hook添加到auto-complete-default函数中,如下所示:

(defun ac-config-default ()
  (setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
  (add-hook 'go-mode-hook 'ac-common-setup)
  ;; 其他hooks
  (global-auto-complete-mode t))

这也不起作用。在启用缓冲区的主要模式后,触发命令的最佳方法是什么?

英文:

I'm trying to enable auto-complete-mode whenever a .go file is loaded through go-mode. It works if I invoke auto-complete-mode manually for Go source files, but when I tried adding it to .emacs as below, it doesn't work:

(add-hook 'go-mode-hook auto-complete-mode)

I've tried a few variations around it but none seem to work. Following is what the Go-Mode snippet currently looks like in my .emacs:

;; Load Go Mode
(require 'go-mode-load)
(add-hook 'go-mode-hook 'auto-complete-mode)

I tried creating my own hook function like this:

;; Load Go Mode
(require 'go-mode-load)
(defun auto-complete-for-go ()
  (auto-complete-mode 1))
(add-hook 'go-mode-hook 'auto-complete-for-go)

I also tried including the hook in go-mode-load.el and go-mode.el, as well as calling auto-complete-mode like this:

(auto-complete-mode t)
(provide 'go-mode)

Doesn't work either way. I also added the go-mode-hook to auto-complete-default function like so:

(defun ac-config-default ()
  (setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
  (add-hook 'go-mode-hook 'ac-common-setup)
  ;; Other hooks
  (global-auto-complete-mode t))

That doesn't work either. What's the best way to trigger a command just after a major mode is enabled for a buffer?

答案1

得分: 5

这是现在的解决方法:

(add-to-list 'ac-modes 'go-mode)

我在v1.4分支中通过以下提交修复了这个问题。

英文:

Here is workaround for now:

(add-to-list 'ac-modes 'go-mode)

I fixed the problem in v1.4 branch with the following commits.

答案2

得分: 3

Which variations have you tried? It should work if you add a single-quote in front of auto-complete-mode:

<!-- language: emacs-lisp -->

(add-hook 'go-mode-hook 'auto-complete-mode)

Without this quote, auto-complete-mode is interpreted as a variable and the value of that variable is added to go-mode-hook. For this to make sense, such a variable should contain a function reference as its value. Most likely though there will be no variable named auto-complete-mode and Emacs will complain.

By adding a quote, you tell Emacs that this is not a variable, but the actual function you want the hook to call. See also here and here.

英文:

Which variations have you tried? It should work if you add a single-quote in front of auto-complete-mode:

<!-- language: emacs-lisp -->

(add-hook &#39;go-mode-hook &#39;auto-complete-mode)

Without this quote, auto-complete-mode is interpreted as a variable and the value of that variable is added to go-mode-hook. For this to make sense, such a variable should contain a function reference as its value. Most likely though there will be no variable named auto-complete-mode and Emacs will complain.

By adding a quote, you tell Emacs that this is not a variable, but the actual function you want the hook to call. See also here and here.

huangapple
  • 本文由 发表于 2012年9月5日 18:05:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/12278990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定