2014-10-23 16:44:23 +02:00
|
|
|
;;==============================================================================
|
|
|
|
;; Go
|
|
|
|
;;==============================================================================
|
2016-06-02 22:28:00 +02:00
|
|
|
;; Packages: go-rename,
|
|
|
|
;; Optional packages: go-autocomplete, go-guru
|
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-02 22:28:00 +02:00
|
|
|
(defun go-file-in-gopath-p ()
|
|
|
|
(let ((dir (expand-file-name (file-name-directory buffer-file-name))) (looping t) (gopath (getenv "GOPATH")))
|
|
|
|
(while (progn
|
|
|
|
(if (string= dir gopath)
|
|
|
|
(setq looping nil)
|
|
|
|
(setq dir (expand-file-name ".." dir)))
|
|
|
|
(and looping (not (string= dir "/")))))
|
|
|
|
(if (string= dir "/") nil t)))
|
|
|
|
|
2014-10-23 16:44:23 +02:00
|
|
|
(add-hook-and-eval
|
|
|
|
'go-mode-hook
|
|
|
|
(lambda ()
|
2016-06-02 22:28:00 +02:00
|
|
|
(setq gofmt-command "goimports")
|
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)
|
2015-12-09 01:02:23 +01:00
|
|
|
(local-set-key (kbd "C-c d") 'godoc)
|
|
|
|
(local-set-key (kbd "M-.") #'godef-jump)
|
|
|
|
(local-set-key (kbd "C-<f10>") 'go-eval-buffer)
|
2016-06-02 22:28:00 +02:00
|
|
|
(set (make-local-variable 'compile-command) (if (go-file-in-gopath-p) "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)
|