Emacs and Go » History » Version 1
Tom Clegg, 03/17/2017 05:36 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 | |||
8 | <pre><code class="lisp"> |
||
9 | (setq-default indent-tabs-mode nil) |
||
10 | |||
11 | (require 'package) |
||
12 | (add-to-list 'package-archives |
||
13 | '("melpa" . "https://melpa.org/packages/")) |
||
14 | (add-to-list 'package-archives |
||
15 | '("melpa-stable" . "https://stable.melpa.org/packages/") t) |
||
16 | (when (< emacs-major-version 24) |
||
17 | ;; compatibility libraries like cl-lib |
||
18 | (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/"))) |
||
19 | (add-to-list 'package-archives |
||
20 | '("melpa" . "http://melpa.milkbox.net/packages/") t) |
||
21 | (package-initialize) |
||
22 | |||
23 | (define-key global-map (kbd "RET") 'newline-and-indent) |
||
24 | |||
25 | (setq package-list '(async auto-complete dash |
||
26 | go-add-tags go-autocomplete go-errcheck go-mode go-playground |
||
27 | go-rename gotest markdown-mode |
||
28 | yaml-mode)) |
||
29 | (dolist (package package-list) |
||
30 | (unless (package-installed-p package) |
||
31 | (message "installing %s..." package) |
||
32 | (package-install package))) |
||
33 | |||
34 | (defun arvados-gopath () |
||
35 | (let ((bfn (buffer-file-name))) |
||
36 | (setenv "GOPATH" |
||
37 | (with-temp-buffer |
||
38 | (call-process "bash" nil t nil "-c" |
||
39 | "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.curoverse.com; ln -sfn ${d} ${gp}/src/git.curoverse.com/arvados.git; break; fi; d=${d%/*}; done; mkdir -p ${gp}; echo -n ${gp}" |
||
40 | bfn) |
||
41 | (buffer-substring (point-min) (point-max)))))) |
||
42 | |||
43 | (defun go-mode-omnibus () |
||
44 | (let ((gopath (arvados-gopath))) |
||
45 | (make-local-variable 'process-environment) |
||
46 | (make-local-variable 'exec-path) |
||
47 | (add-to-list 'exec-path (concat gopath "/bin")) |
||
48 | (with-temp-buffer |
||
49 | (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)) |
||
50 | (auto-complete-mode 1) |
||
51 | ; call gofmt before saving |
||
52 | (add-hook 'before-save-hook 'gofmt-before-save) |
||
53 | (setq gofmt-command "goimports") |
||
54 | ; compile with go |
||
55 | (if (not (string-match "go" compile-command)) |
||
56 | (set (make-local-variable 'compile-command) |
||
57 | "go build -v && go test -v && go vet")) |
||
58 | ; godef-jump key bindings |
||
59 | (local-set-key (kbd "M-.") 'godef-jump) |
||
60 | (local-set-key (kbd "M-*") 'pop-tag-mark))) |
||
61 | (add-hook 'go-mode-hook 'go-mode-omnibus) |
||
62 | </code></pre> |