Gocode+auto-complete to build Emacs's Go language auto-complete function

Source: Internet
Author: User
Tags git clone gocode lenovo

The previous essay recorded the Simple programming environment (push door) used by Go-mode and Goflymake to build the go language in Emacs, today to record the auto-complete function of the go language in Emacs using Gocode+auto-complete configuration. Take a look first??, my Emacs configuration can refer to myemacs-conf.

About Gocode

Gocode is a tool written by NSF to provide auto-complete function of Go language for various editors. Refer to the README.MD in GitHub to install the Gocode.

$ go get-u github.com/nsf/gocode

After installing Gocode, run the command below to set Gocode's Propose-builtins (default is False, does not automatically associate the go language built-in types, constants and methods, etc.),

$ gocode Set Propose-builtins True

Set up Emacs below to start.

Emacs Configuration Auto-complete

You can configure Auto-complete and Company-mode in Emacs to combine gocode with the Go Language auto-complete feature, and the Gocode library also provides guidelines for both configurations. They are configured to experience a bit, or feel auto-complete to use the experience better. Here is a record of the configuration of the Auto-complete, about Company-mode configuration can refer to Emacs-company configuration.

1. Installing Auto-complete

Installation methods are provided in the official user's Manual of Auto-complete. I install it by manual installation, first download the Auto-complete code base,

$ git clone https://github.com/auto-complete/auto-complete.git

Then go to the Auto-complete directory and install it automatically via Etc/install.el.

$ cd auto-complete/-batch-l etc/install.el

The Install command provides "install to:", which allows you to enter the target directory to install, such as input ~/.emacs.d. After the installation is successful, you will be prompted with the following:

Successfully installed! ADD the following code to your emacs: (add-to-list ' Load-path "~/.EMACS.D") (    require ' auto-complete-config) ( Add-to-list ' Ac-dictionary-directories "~/.emacs.d/ac-dict") (Ac-config-default)

Follow the prompts to add the relevant configuration to the ~/.emacs configuration file.

Here's a question: After the installation is complete, when I load Auto-complete-mode, I will be prompted to find a popup. So you need to download the popop.el file yourself, and back to the Auto-complete.el directory.

2. Install and configure the Go-autocomplete

Copy the Emacs/go-autocomplete.ele file from the Gocode release package or the Gocode source repository to the ~/.EMACS.D directory and add the following configuration to the ~/.emacs configuration file.

(Require ' go-autocomplete) (Require ' auto-complete-config) (Ac-config-default)

As a result, Emacs's Go language auto-complete function can be used. However, the default configuration needs to be modified to provide ease of use.

3. Enhanced configuration I. Lenovo ignores uppercase and lowercase

Auto-complete can control whether Lenovo ignores case by ac-ignore-case variables,

;; Set to T to ignore case, set to nil to indicate case sensitivity;; Smart, by default, indicates that if the input string does not contain uppercase characters, the case is ignored (setq ac-ignore-case t)
Ii. shortcut keys for selecting a Lenovo item up and down

Auto-complete provides tab, up, down, m-p, and m-n to move up and down to select Auto-Lenovo items. For Emacs users are accustomed to use c-p and c-n to move up and down, and in Mac OS x System m-p and m-n shortcut keys is really not good to press.

(setq ac-use-menu-map T) (Define-key ac-menu-map "\c-n" ' Ac-next) (Define-key ac-menu-map "\c-p" ' ac-previous)
Iii. configuration of the legend source in Go-mode mode

By default, the auto-complement plenary in the go language has the following problem: MD5 and Md5inst's Lenovo items recur.

In this buffer of Emacs run M: Ac-sources looked, ac-sources (about ac-sources is what, simply said Auto-complete auto-Lenovo show the data from where, more details please refer to the Official document) the value of ( Ac-source-go Ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers). The problem is in the two ac-source-go and Ac-source-words-in-same-mode-buffers. Auto-complete the analysis data provided by the Gocode obtained by Ac-source-go (the first two items), and the words that appear in the file are obtained through Ac-source-words-in-same-mode-buffers (the following three items), The result is a repetition. We removed the ac-source-words-in-same-mode-buffers by modifying the Go-autocomplete.el file.

(Add-hook ' Go-mode-hook # ' (Lambda () (setq ac-sources ' (ac-source-go ac-source-abbrev ac-source-dictionary)))

Now the effect is as follows, no duplicates are present,

Auto-complete Some instructions for use

For detailed instructions on how to use Auto-complete, please refer to the official documentation. Here are just a few simple instructions to use.

1. Selection of Lenovo items <TAB> use of keys

After the auto-Lenovo list is displayed, the,<tab> key is temporarily assigned the ability to select a legend. The following three scenarios exist:

    • When there is only one legend, the <TAB> is used to automatically complete the association;
    • There are multiple associative items, and all associative items have a common prefix, then the common prefix is automatically complement;
    • Otherwise, it is used to loop the selection in the list of Lenovo items;
Use of <RET> keys

The <RET> key is used to automatically complete the selected items, with the following effects:

    • Immediately automatically fill in the currently selected Lenovo items;
    • If the selected legend contains an action, the action is performed.
Other selection keys
    • Available <up> or m-p Select the previous legend,<down> or M-n to select the next legend;
    • Use m-<digit> to quickly use the top 10 Lenovo items in the Lenovo list to auto-complete.
2. Lenovo Help Information

The help information of the so-called Lenovo item is a detailed documentation of the legend. There are two ways to help display auto-complete:

    • Quick help: The so-called quick Assist is the selection of a legend, on the right side of the association to display the information of the popup;
    • buffer Help: The so-called buffer helper is a buffer in which to display assistance information. When the help information for a Lenovo item is too long, you can choose the buffer helper mode when the Quick Assist mode shows that it is inconvenient to view. After selecting a legend from the upper and lower keys, the key will display the buffer help and then scroll up and down through the c-m-v or c-m-s-v, and the other keys may cause buffer to automatically close.

For quick help, as shown in the yellow area, when you select a Lenovo item up and down, auto-complete defaults to the document description for that legend, if one exists.

If there is too much help information for a particular legend, the Quick assist display is not easy to see, and you can press F1 to open the buffer to see it, as shown in. You can use the C-M-V or c-m-s-v shortcut keys to scroll up and down the buffer help buffer to see the aid information, and other shortcut keys will cause the buffer assist buffers to close automatically.

3. Using dictionaries to provide Lenovo items

A dictionary is a list of strings, and Auto-complete can provide the configuration of an auto-associative item through a dictionary. Auto-complete supports three types of dictionaries:

    • User defined dictionary: a custom dictionary that includes two lists ac-user-dictionary and Ac-user-dictionary-files. Where ac-user-dictionary is used to add a list of strings for a legend, the modification takes effect immediately, and Ac-user-dictionary-files is a list of custom dictionary files, all of which are series of associative items separated by newline characters. ~/.dict is also a custom dictionary file by default (the custom dictionary file uses the cache, so the modifications do not take effect automatically and you can run Ac-clear-disctionary-cache to make them effective). Note: User-defined dictionaries can be used for all buffers.
    • Major mode Dictionary: the dictionary with the Emacs Main mode command (Major mode) will only take effect in the main mode buffer, for example, a dictionary named C++-mode takes effect only in the buffer of C++-mode mode. The main mode dictionary file is loaded from the list of directories specified by Ac-dictionary-directories. The Dict directory of the Auto-complete GitHub Library provides a dictionary file of main emacs main mode. (Note: Once modified or added, you also need to run Ac-clear-disctionary-cache to clear the cache)
    • Extension Dictionary: extension dictionary, from the name we can see that it is for the file extension to take effect. For example, a dictionary file named CPP takes effect only in the *.cpp buffer. The extension dictionary is also looked up from the list of directories specified by Ac-dictionary-directories. It also exists in the cache.

Reference list:

http://dominik.honnef.co/posts/2013/03/emacs-go-1/

Https://github.com/nsf/gocode

Http://auto-complete.org/doc/manual.html

***************

* Welcome reprint, but please specify the source Oh *

***************

Gocode+auto-complete to build Emacs's Go language auto-complete function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.