4

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?

2 Answers 2

5

Here is workaround for now:

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

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

1
  • I applied the changes in github that you've linked to in my source list and it worked. Thanks a ton! Sep 6, 2012 at 7:47
3

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.

2
  • I've tried that as well. Following is what the Go-Mode snippet looks like in my .emacs: ;; Load Go Mode (require 'go-mode-load) (add-hook 'go-mode-hook 'auto-complete-mode) 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. What's the best way to trigger a command just after a major mode is enabled for a buffer? Sep 6, 2012 at 4:26
  • Oops! No formatting support in comments so I'm going to put the same text in my question above. Sep 6, 2012 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.