Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-complete with go-mode

Tags:

emacs

go

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?

like image 894
code_martial Avatar asked Feb 19 '23 06:02

code_martial


2 Answers

Here is workaround for now:

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

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

  • Add go-mode to ac-modes
  • Add go-mode dictionary
like image 85
m2ym Avatar answered Feb 22 '23 22:02

m2ym


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

(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.

like image 22
Thomas Avatar answered Feb 22 '23 22:02

Thomas