Project

General

Profile

Emacs and Go » History » Version 4

Ward Vandewege, 08/03/2020 02:17 PM

1 1 Tom Clegg
h1. Emacs and Go
2
3
With this stuff in ~/.emacs, you should get the following features:
4
* (not really a feature) a strange delay the first time you open a Go file, while a bunch of dev tools get installed into GOPATH
5
* With point on an identifier, @M-.@ jumps to the identifier's definition (@M-*@ returns to original point)
6
* Gofmt and goimports get applied automatically when saving
7 3 Peter Amstutz
* Requires "gocode":https://github.com/nsf/gocode "goimports":https://godoc.org/golang.org/x/tools/cmd/goimports and "godef":https://godoc.org/github.com/rogpeppe/godef
8 1 Tom Clegg
9
<pre><code class="lisp">
10
(setq-default indent-tabs-mode nil)
11
12
(require 'package)
13
(add-to-list 'package-archives
14
             '("melpa" . "https://melpa.org/packages/"))
15
(add-to-list 'package-archives
16
             '("melpa-stable" . "https://stable.melpa.org/packages/") t)
17
(when (< emacs-major-version 24)
18
  ;; compatibility libraries like cl-lib                                                                                                                                                                                        
19
  (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/")))
20
(add-to-list 'package-archives
21
             '("melpa" . "http://melpa.milkbox.net/packages/") t)
22
(package-initialize)
23
24
(define-key global-map (kbd "RET") 'newline-and-indent)
25
26
(setq package-list '(async auto-complete dash
27
  go-add-tags go-autocomplete go-errcheck go-mode go-playground
28
  go-rename gotest markdown-mode
29
  yaml-mode))
30
(dolist (package package-list)
31
  (unless (package-installed-p package)
32
    (message "installing %s..." package)
33
    (package-install package)))
34
35
(defun arvados-gopath ()
36
  (let ((bfn (buffer-file-name)))
37
    (setenv "GOPATH"
38
            (with-temp-buffer
39
              (call-process "bash" nil t nil "-c"
40 4 Ward Vandewege
                            "gp=${GOPATH:-${HOME}/go}; d=${0}; while [[ ${d} != '' ]]; do if [[ -e ${d}/services/keep-web/. ]]; then gp=${d}/tmp/GOPATH; mkdir -p ${gp}/src/git.arvados.org; ln -sfn ${d} ${gp}/src/git.arvados.org/arvados.git; break; fi; d=${d%/*}; done; mkdir -p ${gp}; echo -n ${gp}"
41 1 Tom Clegg
                            bfn)
42
              (buffer-substring (point-min) (point-max))))))
43
44
(defun go-mode-omnibus ()
45
  (let ((gopath (arvados-gopath)))
46
    (make-local-variable 'process-environment)
47
    (make-local-variable 'exec-path)
48
    (add-to-list 'exec-path (concat gopath "/bin"))
49
    (with-temp-buffer
50
      (call-process "bash" nil t t "-c" "([[ -x ${1}/bin/gocode ]] && ${1}/bin/goimports -srcdir / /dev/null) || go get -u -v golang.org/x/tools/cmd/... github.com/rogpeppe/godef github.com/nsf/gocode" "-" gopath))
51
    (auto-complete-mode 1)
52
                                        ; call gofmt before saving                                                                                                                                                              
53
    (add-hook 'before-save-hook 'gofmt-before-save)
54
    (setq gofmt-command "goimports")
55
                                        ; compile with go                                                                                                                                                                       
56
    (if (not (string-match "go" compile-command))
57
        (set (make-local-variable 'compile-command)
58
             "go build -v && go test -v && go vet"))
59
                                        ; godef-jump key bindings                                                                                                                                                               
60
    (local-set-key (kbd "M-.") 'godef-jump)
61
    (local-set-key (kbd "M-*") 'pop-tag-mark)))
62
(add-hook 'go-mode-hook 'go-mode-omnibus)
63
</code></pre>