8 "github.com/mitchellh/go-homedir"
11 var pluginCompletions []func(string) []string
13 // This file is meant (for now) for autocompletion in command mode, not
14 // while coding. This helps micro autocomplete commands and then filenames
15 // for example with `vsplit filename`.
17 // FileComplete autocompletes filenames
18 func FileComplete(input string) (string, []string) {
19 var sep string = string(os.PathSeparator)
20 dirs := strings.Split(input, sep)
22 var files []os.FileInfo
25 home, _ := homedir.Dir()
27 directories := strings.Join(dirs[:len(dirs)-1], sep) + sep
29 if strings.HasPrefix(directories, "~") {
30 directories = strings.Replace(directories, "~", home, 1)
32 files, err = ioutil.ReadDir(directories)
34 files, err = ioutil.ReadDir(".")
37 var suggestions []string
39 return "", suggestions
41 for _, f := range files {
46 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
47 suggestions = append(suggestions, name)
52 if len(suggestions) == 1 {
54 chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep + suggestions[0]
56 chosen = suggestions[0]
60 chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep
64 return chosen, suggestions
67 // CommandComplete autocompletes commands
68 func CommandComplete(input string) (string, []string) {
69 var suggestions []string
70 for cmd := range commands {
71 if strings.HasPrefix(cmd, input) {
72 suggestions = append(suggestions, cmd)
77 if len(suggestions) == 1 {
78 chosen = suggestions[0]
80 return chosen, suggestions
83 // HelpComplete autocompletes help topics
84 func HelpComplete(input string) (string, []string) {
85 var suggestions []string
87 for _, file := range ListRuntimeFiles(RTHelp) {
89 if strings.HasPrefix(topic, input) {
90 suggestions = append(suggestions, topic)
95 if len(suggestions) == 1 {
96 chosen = suggestions[0]
98 return chosen, suggestions
101 // ColorschemeComplete tab-completes names of colorschemes.
102 func ColorschemeComplete(input string) (string, []string) {
103 var suggestions []string
104 files := ListRuntimeFiles(RTColorscheme)
106 for _, f := range files {
107 if strings.HasPrefix(f.Name(), input) {
108 suggestions = append(suggestions, f.Name())
113 if len(suggestions) == 1 {
114 chosen = suggestions[0]
117 return chosen, suggestions
120 func contains(s []string, e string) bool {
121 for _, a := range s {
129 // OptionComplete autocompletes options
130 func OptionComplete(input string) (string, []string) {
131 var suggestions []string
132 localSettings := DefaultLocalSettings()
133 for option := range globalSettings {
134 if strings.HasPrefix(option, input) {
135 suggestions = append(suggestions, option)
138 for option := range localSettings {
139 if strings.HasPrefix(option, input) && !contains(suggestions, option) {
140 suggestions = append(suggestions, option)
145 if len(suggestions) == 1 {
146 chosen = suggestions[0]
148 return chosen, suggestions
151 func OptionValueComplete(inputOpt, input string) (string, []string) {
152 inputOpt = strings.TrimSpace(inputOpt)
153 var suggestions []string
154 localSettings := DefaultLocalSettings()
155 var optionVal interface{}
156 for k, option := range globalSettings {
161 for k, option := range localSettings {
167 switch optionVal.(type) {
169 if strings.HasPrefix("on", input) {
170 suggestions = append(suggestions, "on")
171 } else if strings.HasPrefix("true", input) {
172 suggestions = append(suggestions, "true")
174 if strings.HasPrefix("off", input) {
175 suggestions = append(suggestions, "off")
176 } else if strings.HasPrefix("false", input) {
177 suggestions = append(suggestions, "false")
182 _, suggestions = ColorschemeComplete(input)
184 if strings.HasPrefix("unix", input) {
185 suggestions = append(suggestions, "unix")
187 if strings.HasPrefix("dos", input) {
188 suggestions = append(suggestions, "dos")
194 if len(suggestions) == 1 {
195 chosen = suggestions[0]
197 return chosen, suggestions
200 // MakeCompletion registers a function from a plugin for autocomplete commands
201 func MakeCompletion(function string) Completion {
202 pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function))
203 return Completion(-len(pluginCompletions))
206 // PluginComplete autocompletes from plugin function
207 func PluginComplete(complete Completion, input string) (chosen string, suggestions []string) {
208 idx := int(-complete) - 1
210 if len(pluginCompletions) <= idx {
213 suggestions = pluginCompletions[idx](input)
215 if len(suggestions) == 1 {
216 chosen = suggestions[0]
221 func PluginCmdComplete(input string) (chosen string, suggestions []string) {
222 for _, cmd := range []string{"install", "remove", "search", "update", "list"} {
223 if strings.HasPrefix(cmd, input) {
224 suggestions = append(suggestions, cmd)
228 if len(suggestions) == 1 {
229 chosen = suggestions[0]
231 return chosen, suggestions
234 func PluginNameComplete(input string) (chosen string, suggestions []string) {
235 for _, pp := range GetAllPluginPackages() {
236 if strings.HasPrefix(pp.Name, input) {
237 suggestions = append(suggestions, pp.Name)
241 if len(suggestions) == 1 {
242 chosen = suggestions[0]
244 return chosen, suggestions