dotfiles/.emacs.d/conf.d/004-go-lang.el

93 lines
3.2 KiB
EmacsLisp

(defvar go-root (let (root (getenv "GOROOT"))
(if root root
(concat (getenv "HOME") "/sandbox/apps/golang"))))
(defvar go-path (concat (getenv "HOME") "/lib/go"))
(let ((oracle-file
(concat go-path "/src/golang.org/x/tools/cmd/oracle/oracle.el")))
(message (concat "Attempting to load: " oracle-file))
(when (file-exists-p oracle-file)
(message (concat "loading: " oracle-file))
(load-file oracle-file)))
;; Required to make gocode work correctly with the lib-paths.
(setenv "GOPATH" (let (path (getenv "GOPATH"))
(if path path
go-path)))
(defvar jwall-set-go-path-history nil
"History for the jwall:set-go-path command")
(defun jwall:set-go-path (go-path)
"Sets the go path for emacs to whatever you want."
(interactive
(progn (list (read-shell-command "GOPATH: " (getenv "GOPATH") 'jwall-set-go-path-history))))
(setenv "GOPATH" go-path))
(require 'company)
(require 'flycheck)
(require 'go-mode)
(add-to-list 'load-path (concat go-path "/src/github.com/nsf/gocode/emacs-company"))
(require 'company-go)
;(setq company-begin-commands '(self-insert-command))
;(setq company-minimum-prefix-length 0)
;; Mostly stolen from https://gist.github.com/5975784
(defun goimports ()
"Formats the current buffer according to the goimports tool."
(interactive)
(let ((tmpfile (make-temp-file "goimports" nil ".go"))
(patchbuf (get-buffer-create "*Goimport patch*"))
(errbuf (get-buffer-create "*Goimport Errors*"))
(coding-system-for-read 'utf-8)
(coding-system-for-write 'utf-8))
(with-current-buffer errbuf
(setq buffer-read-only nil)
(erase-buffer))
(with-current-buffer patchbuf
(erase-buffer))
(write-region nil nil tmpfile)
;; We're using errbuf for the mixed stdout and stderr output. This
;; is not an issue because gofmt -w does not produce any stdout
;; output in case of success.
(if (zerop (call-process "goimports" nil errbuf nil "-w" tmpfile))
(if (zerop (call-process-region (point-min) (point-max) "diff" nil patchbuf nil "-n" "-" tmpfile))
(progn
(kill-buffer errbuf)
(message "Buffer is already goimportsed"))
(go--apply-rcs-patch patchbuf)
(kill-buffer errbuf)
(message "Applied goimports"))
(message "Could not apply goimports. Check errors for details")
(gofmt--process-errors (buffer-file-name) tmpfile errbuf))
(kill-buffer patchbuf)
(delete-file tmpfile)))
(defun maybe-use-goimports ()
(if (eq major-mode 'go-mode)
(dolist (path exec-path)
(when (file-exists-p (concat path "/goimports"))
(goimports)))
(gofmt-before-save)))
(defun jwall:go-mode-hook ()
(add-hook 'before-save-hook 'maybe-use-goimports)
(set (make-local-variable 'company-backends) '(company-go))
(company-mode)
(setq-local company-idle-delay 0)
(setq-local company-auto-complete-chars ".")
;(setq-local flycheck-idle-change-delay .)
(flycheck-mode)
(go-eldoc-setup)
;(eldoc-mode)
(setq-local tab-wdith 4))
; wire up my hooks yay.
(add-hook 'go-mode-hook 'jwall:go-mode-hook)