doumei7420 2016-02-22 23:27
浏览 206

emacs自动完成go语言

I have been trying to combine company mode and gocode to setup auto complete in emacs. I have followed this link. But the result is not what is mentioned in gocode's github page. The menu doesn't appear on its own and the function declaration is not displayed in mini-buffer.

This is my .emacs file (gocode and company mode setup is at last):

                                                     ;(invert-face 'default)   ;;weird with nw mode
                                                     ;(blink-cursor-mode 0)
(load-file "/home/sudeep/.emacs.d/shortcuts.el")
;; (defadvice ido-find-file (after find-file-sudo activate)
;;   "Find file as root if necessary."
;;   (unless (and buffer-file-name
;;                (file-writable-p buffer-file-name))
;;     (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
                                                     ;(setq debug-on-error t)

(setq custom-file "/home/sudeep/.emacs.d/custom.el")
(load custom-file 'noerror)

(iswitchb-mode 1)
                                                     ;disables scroll bar
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(menu-bar-mode -1)
(tool-bar-mode -1)


(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))    ;.h files are treated as c files instaed of c++ in emacs. Remove it!

;; -- laoding images in org mode
;; enable image mode first
(setq-default iimage-mode t)


;Splitted window switching with C-ESC

(global-set-key (kbd "<C-escape>") 'other-window)
;     

;hiding code(hiding the contents of a function)C-c @ C-h and C-c @ C-s


(add-hook 'c++-mode-common-hook #'hs-minor-mode)
(add-hook 'c-mode-common-hook #'hs-minor-mode)
(add-hook 'python-mode-common-hook #'hs-minor-mode)
(add-hook 'elisp-mode-common-hook #'hs-minor-mode)
(add-hook 'java-mode-common-hook #'hs-minor-mode)


(blink-cursor-mode -1)

;miscellaneous
(setq max-mini-window-height .70)
(which-function-mode)




(setq locale-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8-unix)
(set-keyboard-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(prefer-coding-system 'utf-8)




;; (load-file "~/.emacs.d/tabbar.el")
;; (require 'cl)
;; (when (require 'tabbar nil t)
;;   (setq tabbar-buffer-groups-function
;;        (lambda (b) (list "All Buffers")))
;;   (setq tabbar-buffer-list-function
;;        (lambda ()
;;           (remove-if
;;            (lambda(buffer)
;;               (find (aref (buffer-name buffer) 0) " *"))
;;            (buffer-list))))
;;   (tabbar-mode))

;; (setq tabbar-buffer-groups-function
;;      (lambda ()
;;        (list "All")))
;; (require 'tabbar)
;; (tabbar-mode 1)
;; (global-set-key [(C-S-iso-lefttab)] 'tabbar-backward)
;; (global-set-key (kbd "C-<tab>") 'tabbar-forward)



                                                     ;bookmarks
(global-set-key (kbd "C-c C-z") 'bookmark-set)
(global-set-key (kbd "C-c C-j") 'bookmark-jump)
(global-set-key (kbd "C-c C-q") 'bookmark-delete)
;(global-set-key (kbd "C-!") 'gtags-pop-stack)

(defadvice bookmark-jump (after bookmark-jump activate)
  (let ((latest (bookmark-get-bookmark bookmark)))
    (setq bookmark-alist (delq latest bookmark-alist))
    (add-to-list 'bookmark-alist latest)))

(display-time-mode 1)

;highlighting  todo,fixme, bug

(add-hook 'prog-mode-common-hook
             (lambda ()
                (font-lock-add-keywords nil
                                                '(("\\<\\(FIXME\\|TODO\\|BUG\\):" 1 font-lock-warning-face t)))))


;; (load-file "/home/sudeep/.emacs.d/sr-speedbar.el")
;; (require 'sr-speedbar)
;; (make-face 'speedbar-face)
;; ;(set-face-font 'speedbar-face "8")
;; (setq speedbar-mode-hook '(lambda () (buffer-face-set 'speedbar-face)))


;; (speedbar-add-supported-extension ".c")
;; (speedbar-add-supported-extension ".c++")
;; (speedbar-add-supported-extension ".s")
;; (speedbar-add-supported-extension ".asm")
;; (speedbar-add-supported-extension ".java")
;; (speedbar-add-supported-extension ".py")

(setq c-default-style "linux"
        c-basic-offset 3)

                                                     ;indentation customization(replacing tabs by 4 spaces)
(setq-default c-basic-offset 3
                  tab-width 3
                  indent-tabs-mode t)


(defun dos2unix ()
  "Replace DOS eolns CR LF with Unix eolns CR"
  (interactive)
    (goto-char (point-min))
      (while (search-forward "" nil t) (replace-match "")))


;(set-face-attribute 'default nil :height 105)

(setq url-proxy-services
   '(("no_proxy" . "^\\(localhost\\|10.*\\)")
     ("http" . "10.3.100.207:8080")
     ("https" . "10.3.100.207:8080")
      ("ftp" . "10.3.100.207:8080")
      ("socks" . "10.3.100.207:8080")))



(require 'package)
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
                         ("marmalade" . "http://marmalade-repo.org/packages/")
                         ("melpa" . "http://melpa.milkbox.net/packages/")
                                 ("elpa" . "http://tromey.com/elpa/")))
(package-initialize)
(when (not package-archive-contents)
  (package-refresh-contents))
(add-to-list 'load-path "/home/sudeep/.emacs.d/")



(defun djcb-opacity-modify (&optional dec)
  "modify the transparency of the emacs frame; if DEC is t,
    decrease the transparency, otherwise increase it in 10%-steps"
  (let* ((alpha-or-nil (frame-parameter nil 'alpha)) ; nil before setting
          (oldalpha (if alpha-or-nil alpha-or-nil 100))
          (newalpha (if dec (- oldalpha 10) (+ oldalpha 10))))
    (when (and (>= newalpha frame-alpha-lower-limit) (<= newalpha 100))
      (modify-frame-parameters nil (list (cons 'alpha newalpha))))))

 ;; C-8 will increase opacity (== decrease transparency)
 ;; C-9 will decrease opacity (== increase transparency
 ;; C-0 will returns the state to normal
(global-set-key (kbd "C-8") '(lambda()(interactive)(djcb-opacity-modify)))
(global-set-key (kbd "C-9") '(lambda()(interactive)(djcb-opacity-modify t)))
(global-set-key (kbd "C-0") '(lambda()(interactive)
                               (modify-frame-parameters nil `((alpha . 10)))))



(setq large-file-warning-threshold 100000000)


;(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
;(load-theme 'junio t)
;(load-theme 'zenburn t)

;(load-file "~/.emacs.d/arjen-theme.el")

                                                     ;(load-file "~/.emacs.d/sanityinc-tomorrow-night-theme.el")
                                                     ;(set-background-color "black")
                                                     ;(require 'color-theme-sanityinc-tomorrow)
                                                     ;(load-theme 'color-theme-sanityinc-tomorrow-black t)
                                                     ;(load-theme 'sanityinc-solarized-dark)


(defun electric-pair ()
  "If at end of line, insert character pair without surrounding spaces.
   Otherwise, just insert the typed character."
  (interactive)
  (if (eolp) (let (parens-require-spaces) (insert-pair)) 
    (self-insert-command 1)))

(add-hook 'js-mode-hook
             (lambda ()
                (define-key js-mode-map "(" 'electric-pair)
                (define-key js-mode-map "[" 'electric-pair)
                (define-key js-mode-map "{" 'electric-pair)))



;;js2-mode setup
                                                     ;(semantic-mode)
                                                     ;(require 'icicles)
                                                     ;(icy-mode t)


;; (add-to-list 'load-path "/home/sudeep/.emacs.d/elpa/auto-complete-20160107.8/")  ; Load the default configuration
;; (require 'auto-complete-config)   ; Make sure we can find the dictionaries
;; (add-to-list 'ac-dictionary-directories "/home/sudeep/.emacs.d/elpa/auto-complete-20160107.8/dict")          ; Usedictionaries by default
;; (setq-default ac-sources (add-to-list 'ac-sources 'ac-source-dictionary))
;; (global-auto-complete-mode t)
;;                                                   ; Start auto-completion after 2 characters of a word
;; (setq ac-auto-start 2)
;;                                                   ; case sensitivity is important when finding matches
;; (setq ac-ignore-case nil)

;;                                                   ;(autoload 'js2-mode "js2" nil t) 

;; (ac-config-default)

;; (set-default 'ac-sources
;;               '(ac-source-abbrev
;;                  ac-source-dictionary
;;                  ac-source-yasnippet
;;                  ac-source-words-in-buffer
;;                  ac-source-words-in-same-mode-buffers
;;                  ac-source-semantic))

;; (dolist (m '(c-mode c++-mode java-mode go-mode))
;;   (add-to-list 'ac-modes m))

;; (global-auto-complete-mode t)
;; ;;yasnippet
;; (add-to-list 'load-path
;;               "~/.emacs.d/plugins/yasnippet")
;; (require 'yasnippet)
;;                                                   ;(yas-global-mode 1)

(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))

(js2-imenu-extras-mode)


;; yes or no becomes y or n
(defalias 'yes-or-no-p 'y-or-n-p)

;; ;;yasnippet
;; (yas-global-mode 1)

;; ;; auto-complete
;; (require 'auto-complete-config)
;; (add-to-list 'ac-dictionary-directories "~/.emacs.d/elpa/auto-complete/dict")

;; (set-default 'ac-sources
;;              '(ac-source-abbrev
;;                ac-source-dictionary
;;                ac-source-yasnippet
;;                ac-source-words-in-buffer
;;                ac-source-words-in-same-mode-buffers
;;                ac-source-semantic))

;; (ac-config-default)

;; (dolist (m '(c-mode c++-mode java-mode emacs-lisp-mode))
;;   (add-to-list 'ac-modes m))

;; (global-auto-complete-mode t)





(global-aggressive-indent-mode 1)
(add-to-list 'aggressive-indent-excluded-modes 'html-mode)
(ido-mode 1)


;;go-mode
                                                     ;(require 'go-autocomplete)


(require 'company)                                   ; load company mode
                                                     ;(require 'company-go)                                                  ; load company mode go backend
(add-hook 'go-mode-hook (lambda ()
                                  (set (make-local-variable 'company-backends) '(company-go))
                                  (company-mode)))
(setq company-tooltip-limit 20)                      ; bigger popup window
(setq company-idle-delay .1)                         ; decrease delay before autocompletion popup shows
(setq company-echo-delay 0)                          ; remove annoying blinking
(setq company-begin-commands '(self-insert-command)) ; start autocompletion only after typing

(global-company-mode)

;; (eval-after-load "go-mode"
;;   '(progn
;;      (flycheck-declare-checker go-gofmt
;;                                       "A Go syntax and style checker using the gofmt utility."
;;                                       :command '("gofmt" source-inplace)
;;                                       :error-patterns '(("^\\(?1:.*\\):\\(?2:[0-9]+\\):\\(?3:[0-9]+\\): \\(?4:.*\\)$" error))
;;                                       :modes 'go-mode)
;;      (add-to-list 'flycheck-checkers 'go-gofmt)))
                                                     ;(ac-go-expand-arguments-into-snippets)
                                                     ;(add-hook 'go-mode-hook 'flycheck-mode)

Please help me to understand and fix the missing stuff.

Here is the debug message when I execute company-complete after typing some letters:

Debugger entered--Lisp error: (error "GOCODE PANIC: Please check your code by \"go build\"")
  signal(error ("GOCODE PANIC: Please check your code by \"go build\""))
  error("GOCODE PANIC: Please check your code by \"go build\"")
  company-go--candidates()
  company-go(candidates #("Clien" 0 5 (fontified t)))
  apply(company-go (candidates #("Clien" 0 5 (fontified t))))
  #[0 "\302 !\203\303   \300\"\207\303\304  \300#\207" [(candidates #("Clien" 0 5 (fontified t))) company-backend functionp apply company--multi-backend-adapter] 4 "

(fn)"]()
  company-call-backend-raw(candidates #("Clien" 0 5 (fontified t)))
  apply(company-call-backend-raw (candidates #("Clien" 0 5 (fontified t))))
  company--force-sync(company-call-backend-raw (candidates #("Clien" 0 5 (fontified t))) company-go)
  company-call-backend(candidates #("Clien" 0 5 (fontified t)))
  company--fetch-candidates(#("Clien" 0 5 (fontified t)))
  company-calculate-candidates(#("Clien" 0 5 (fontified t)))
  #[0 "
\203   
C\202
\211\205\233\211@\3009\204\306!\203<9\2036\307N\310=\2046\307N?\205@\311!\205@\312!\202@\313\312\"\240\210\300@\203\224\314\300@!\203\216\315\300@!\211\301\316\f!\240\210\301@:\204w
\203i\317\320!\210\301@\310=\203\216\321\322!\210\202\216
\203\300@\323\301@!\210\324\325\326 \"\210\327\330!\210\331\332\301@\"\210A\266\202\202
\207" [((#("Clien" 0 5 (fontified t)) . t)) (nil) company-backend company-backends company-prefix company--manual-action functionp company-init t company-init-backend prefix company--multi-backend-adapter company--good-prefix-p company--prefix-str company-calculate-candidates message "No completion found" company-cancel unique company-update-candidates run-hook-with-args company-completion-started-hook company-explicit-action-p company-call-frontends show throw --cl-block-nil-- company--manual-prefix] 6 "

(fn)"]()
  funcall(#[0 "
\203   
C\202
\211\205\233\211@\3009\204\306!\203<9\2036\307N\310=\2046\307N?\205@\311!\205@\312!\202@\313\312\"\240\210\300@\203\224\314\300@!\203\216\315\300@!\211\301\316\f!\240\210\301@:\204w
\203i\317\320!\210\301@\310=\203\216\321\322!\210\202\216
\203\300@\323\301@!\210\324\325\326 \"\210\327\330!\210\331\332\301@\"\210A\266\202\202
\207" [((#("Clien" 0 5 (fontified t)) . t)) (nil) company-backend company-backends company-prefix company--manual-action functionp company-init t company-init-backend prefix company--multi-backend-adapter company--good-prefix-p company--prefix-str company-calculate-candidates message "No completion found" company-cancel unique company-update-candidates run-hook-with-args company-completion-started-hook company-explicit-action-p company-call-frontends show throw --cl-block-nil-- company--manual-prefix] 6 "

(fn)"])
  company--begin-new()
  company--perform()
  #[0 "\301 \210\207" [company-candidates company--perform] 1 "

(fn)"]()
  company-auto-begin()
  company-manual-begin()
  company-complete()
  call-interactively(company-complete record nil)
  command-execute(company-complete record)
  execute-extended-command(nil "company-complete")
  call-interactively(execute-extended-command nil nil)

EDIT Adding (require 'cl) at the start solved the problem.

  • 写回答

2条回答 默认 最新

  • douketangyouzh5219 2016-02-23 00:58
    关注

    You need only company package. company-go is not required. The following config works for me:

      ;; enable company mode for autocompletion
      (add-hook 'go-mode-hook (lambda ()
                                (set (make-local-variable 'company-backends) '(company-go))
                                (company-mode)))
    

    Flycheck is also highly recommended (go support works out of the box):

      ;; enable flycheck
      (add-hook 'go-mode-hook 'flycheck-mode)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算