2014-10-23 16:44:23 +02:00
;;==============================================================================
;; Go
;;==============================================================================
2016-01-27 08:55:19 +01:00
2015-12-09 01:02:23 +01:00
( defun go-eval-buffer ( )
" Eval buffer with ` go run'. "
( interactive )
( let ( ( compile-command-backup compile-command ) )
( compile ( concat " go run " ( shell-quote-argument buffer-file-name ) ) )
( setq compile-command compile-command-backup ) ) )
2016-06-03 12:04:46 +02:00
( defun go-buffer-in-gopath-p ( )
2016-07-19 12:00:52 +02:00
( if ( not buffer-file-name )
nil
2016-10-05 06:57:29 +02:00
( let ( ( dir ( expand-file-name ( file-name-directory buffer-file-name ) ) ) ( looping t ) ( gopath ( split-string ( getenv " GOPATH " ) " : " ) ) )
2016-07-19 12:00:52 +02:00
( while ( progn
2016-10-05 06:57:29 +02:00
( if ( member dir gopath )
2016-07-19 12:00:52 +02:00
( setq looping nil )
( setq dir ( expand-file-name " .. " dir ) ) )
( and looping ( not ( string= dir " / " ) ) ) ) )
( if ( string= dir " / " ) nil t ) ) ) )
2016-06-02 22:28:00 +02:00
2016-10-06 14:37:53 +02:00
( when ( require 'go-guru nil t )
( unless ( executable-find " guru " )
; Requires `call-process-to-string' from `functions'."
( require 'functions )
( setq go-guru-command
( concat ( replace-regexp-in-string " \n $ " " " ( call-process-to-string " go " " env " " GOTOOLDIR " ) ) " /guru " ) ) ) )
2016-06-06 20:52:19 +02:00
( add-hook
'godoc-mode-hook
( lambda ( )
( setq tab-width 8 ) ) )
2016-06-09 18:20:38 +02:00
( add-hook-and-eval
2014-10-23 16:44:23 +02:00
'go-mode-hook
( lambda ( )
2016-10-06 14:37:53 +02:00
( when ( require 'go-eldoc nil t )
( go-eldoc-setup ) )
2016-10-06 14:38:30 +02:00
( when ( require 'company-go nil t )
( add-to-list 'company-backends 'company-go )
( company-mode )
( if ( fboundp 'helm-company )
( local-set-key ( kbd " M-TAB " ) 'helm-company )
( local-set-key ( kbd " M-TAB " ) 'company-complete ) ) )
2016-06-02 22:28:00 +02:00
( setq gofmt-command " goimports " )
2016-06-06 20:52:19 +02:00
( setq godoc-command " godoc -ex " )
( setq godoc-and-godef-command " godoc -ex " )
2014-10-23 16:44:23 +02:00
( add-hook 'before-save-hook #' gofmt-before-save nil t )
( local-set-key ( kbd " C-c m " ) 'go-main )
2016-06-06 20:52:19 +02:00
( local-set-key ( kbd " C-c D " ) 'godoc )
2016-06-08 23:37:21 +02:00
( when ( require 'helm-go-package nil t )
( local-set-key ( kbd " C-c D " ) 'helm-go-package ) )
2016-06-06 20:52:19 +02:00
( local-set-key ( kbd " C-c d " ) 'godoc-at-point )
2015-12-09 01:02:23 +01:00
( local-set-key ( kbd " M-. " ) #' godef-jump )
( local-set-key ( kbd " C-<f10> " ) 'go-eval-buffer )
2016-06-03 12:04:46 +02:00
( local-set-key ( kbd " <f9> " )
( lambda ( ) ( interactive )
( let ( ( compile-command " gometalinter --cyclo-over=20 --deadline=20s -e 'declaration of err shadows' -e 'error return value not checked \\ (.* \\ .Close \\ ( \\ )' " ) )
( compile compile-command ) ) ) )
2016-06-07 21:57:40 +02:00
( set ( make-local-variable 'compile-command )
( if ( go-buffer-in-gopath-p ) ( if ( string-match " _test.[gG][oO]$ " buffer-file-name ) " go test " " go install " ) " go build " ) ) ) )
2014-10-23 16:44:23 +02:00
( define-skeleton go-main
" Insert main function with basic includes. "
nil
2014-10-31 13:08:44 +01:00
> " package main " " \n " \n
" import ( " \n
" \" fmt \" " \n
" ) " > " \n " \n
2014-10-31 13:20:11 +01:00
" func main() { " \n
2014-10-31 13:08:44 +01:00
> @ _ \n
" } " > \n )
2014-10-23 16:44:23 +02:00
( provide 'mode-go )