Go语言开放工具之Emacs
原文放在@astaxie 写的“build web application with golang”书里面。
下面只把Emacs里面的拿出来,如果需要参考vim,liteide和Sublime text的请一步 https://github.com/astaxie/build-web-application-with-golang/blob/master/1.4.md
Emacs主要是使用了gocode, speedbar, 并把一些go的命令融合进来。
## Emacs
Emacs传说中的神器,她不仅仅是一个编辑器,它是一个整合环境,或可称它为集成开发环境,这些功能如让使用者置身于全功能的操作系统中
1. 配置Emacs高亮显示
cp $GOROOT/misc/emacs/* ~/.emacs.d/
2. 安装[Gocode](https://github.com/nsf/gocode/)
go get -u github.com/nsf/gocode
gocode默认安装到$GOPATH/bin里面下面,需要把$GOPATH/bin路径设置到系统PATH里面。
3. 配置[Gocode](https://github.com/nsf/gocode/)
~ cd $GOPATH/src/github.com/nsf/gocode/emacs
~ cp go-autocomplete.el ~/.emacs.d/
~ gocode set propose-builtins true
propose-builtins true
~ gocode set lib-path “/home/border/gocode/pkg/linux_amd64″ // 换为你自己的路径
lib-path “/home/border/gocode/pkg/linux_amd64″
~ gocode set
propose-builtins true
lib-path “/home/border/gocode/pkg/linux_amd64″
4. 需要安装 [Auto Completion](http://www.emacswiki.org/emacs/AutoComplete)
下载AutoComplete并解压
~ make install DIR=$HOME/.emacs.d/auto-complete
配置~/.emacs文件
;;auto-complete
(require ‘auto-complete-config)
(add-to-list ‘ac-dictionary-directories “~/.emacs.d/auto-complete/ac-dict”)
(ac-config-default)
(local-set-key (kbd “M-/”) ‘semantic-complete-analyze-inline)
(local-set-key “.” ‘semantic-complete-self-insert)
(local-set-key “>” ‘semantic-complete-self-insert)
详细信息参考: http://www.emacswiki.org/emacs/AutoComplete
5. 配置.emacs
;; golang mode
(require ‘go-mode-load)
(require ‘go-autocomplete)
;; speedbar
;; (speedbar 1)
(speedbar-add-supported-extension “.go”)
(add-hook
‘go-mode-hook
‘(lambda ()
;; gocode
(auto-complete-mode 1)
(setq ac-sources ‘(ac-source-go))
;; Imenu & Speedbar
(setq imenu-generic-expression
‘((“type” “^type *\\([^ \t\n\r\f]*\\)” 1)
(“func” “^func *\\(.*\\) {” 1)))
(imenu-add-to-menubar “Index”)
;; Outline mode
(make-local-variable ‘outline-regexp)
(setq outline-regexp “//\\.\\|//[^\r\n\f][^\r\n\f]\\|pack\\|func\\|impo\\|cons\\|var.\\|type\\|\t\t*….”)
(outline-minor-mode 1)
(local-set-key “\M-a” ‘outline-previous-visible-heading)
(local-set-key “\M-e” ‘outline-next-visible-heading)
;; Menu bar
(require ‘easymenu)
(defconst go-hooked-menu
‘(“Go tools”
["Go run buffer" go t]
["Go reformat buffer" go-fmt-buffer t]
["Go check buffer" go-fix-buffer t]))
(easy-menu-define
go-added-menu
(current-local-map)
“Go tools”
go-hooked-menu)
;; Other
(setq show-trailing-whitespace t)
))
;; helper function
(defun go ()
“run current buffer”
(interactive)
(compile (concat “go run ” (buffer-file-name))))
;; helper function
(defun go-fmt-buffer ()
“run gofmt on current buffer”
(interactive)
(if buffer-read-only
(progn
(ding)
(message “Buffer is read only”))
(let ((p (line-number-at-pos))
(filename (buffer-file-name))
(old-max-mini-window-height max-mini-window-height))
(show-all)
(if (get-buffer “*Go Reformat Errors*”)
(progn
(delete-windows-on “*Go Reformat Errors*”)
(kill-buffer “*Go Reformat Errors*”)))
(setq max-mini-window-height 1)
(if (= 0 (shell-command-on-region (point-min) (point-max) “gofmt” “*Go Reformat Output*” nil “*Go Reformat Errors*” t))
(progn
(erase-buffer)
(insert-buffer-substring “*Go Reformat Output*”)
(goto-char (point-min))
(forward-line (1- p)))
(with-current-buffer “*Go Reformat Errors*”
(progn
(goto-char (point-min))
(while (re-search-forward “
(replace-match filename))
(goto-char (point-min))
(compilation-mode))))
(setq max-mini-window-height old-max-mini-window-height)
(delete-windows-on “*Go Reformat Output*”)
(kill-buffer “*Go Reformat Output*”))))
;; helper function
(defun go-fix-buffer ()
“run gofix on current buffer”
(interactive)
(show-all)
(shell-command-on-region (point-min) (point-max) “go tool fix -diff”))
6. 恭喜你,你现在可以体验在神器中开发Go的乐趣. 默认speedbar是关闭的,如果打开需要把 ;; (speedbar 1) 前面的注释去掉,或者也可以通过 *M-x speedbar* 手动开启。
参考:
1. Go & Emacs http://www.let.rug.nl/~kleiweg/go/go-emacs.html
2. https://github.com/border/emacs
–EOF–

![[Google]]( http://wifihack.net/blog/wp-content/plugins/easy-adsense-lite/google-light.gif)
Recent Comments