9 var pluginCompletions []func(string) []string
11 // This file is meant (for now) for autocompletion in command mode, not
12 // while coding. This helps micro autocomplete commands and then filenames
13 // for example with `vsplit filename`.
15 // FileComplete autocompletes filenames
16 func FileComplete(input string) (string, []string) {
17 var sep string = string(os.PathSeparator)
18 dirs := strings.Split(input, sep)
20 var files []os.FileInfo
23 directories := strings.Join(dirs[:len(dirs)-1], sep) + sep
25 directories = ReplaceHome(directories)
26 files, err = ioutil.ReadDir(directories)
28 files, err = ioutil.ReadDir(".")
31 var suggestions []string
33 return "", suggestions
35 for _, f := range files {
40 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
41 suggestions = append(suggestions, name)
46 if len(suggestions) == 1 {
48 chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep + suggestions[0]
50 chosen = suggestions[0]
54 chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep
58 return chosen, suggestions
61 // CommandComplete autocompletes commands
62 func CommandComplete(input string) (string, []string) {
63 var suggestions []string
64 for cmd := range commands {
65 if strings.HasPrefix(cmd, input) {
66 suggestions = append(suggestions, cmd)
71 if len(suggestions) == 1 {
72 chosen = suggestions[0]
74 return chosen, suggestions
77 // HelpComplete autocompletes help topics
78 func HelpComplete(input string) (string, []string) {
79 var suggestions []string
81 for _, file := range ListRuntimeFiles(RTHelp) {
83 if strings.HasPrefix(topic, input) {
84 suggestions = append(suggestions, topic)
89 if len(suggestions) == 1 {
90 chosen = suggestions[0]
92 return chosen, suggestions
95 // ColorschemeComplete tab-completes names of colorschemes.
96 func ColorschemeComplete(input string) (string, []string) {
97 var suggestions []string
98 files := ListRuntimeFiles(RTColorscheme)
100 for _, f := range files {
101 if strings.HasPrefix(f.Name(), input) {
102 suggestions = append(suggestions, f.Name())
107 if len(suggestions) == 1 {
108 chosen = suggestions[0]
111 return chosen, suggestions
114 func contains(s []string, e string) bool {
115 for _, a := range s {
123 // OptionComplete autocompletes options
124 func OptionComplete(input string) (string, []string) {
125 var suggestions []string
126 localSettings := DefaultLocalSettings()
127 for option := range globalSettings {
128 if strings.HasPrefix(option, input) {
129 suggestions = append(suggestions, option)
132 for option := range localSettings {
133 if strings.HasPrefix(option, input) && !contains(suggestions, option) {
134 suggestions = append(suggestions, option)
139 if len(suggestions) == 1 {
140 chosen = suggestions[0]
142 return chosen, suggestions
145 func OptionValueComplete(inputOpt, input string) (string, []string) {
146 inputOpt = strings.TrimSpace(inputOpt)
147 var suggestions []string
148 localSettings := DefaultLocalSettings()
149 var optionVal interface{}
150 for k, option := range globalSettings {
155 for k, option := range localSettings {
161 switch optionVal.(type) {
163 if strings.HasPrefix("on", input) {
164 suggestions = append(suggestions, "on")
165 } else if strings.HasPrefix("true", input) {
166 suggestions = append(suggestions, "true")
168 if strings.HasPrefix("off", input) {
169 suggestions = append(suggestions, "off")
170 } else if strings.HasPrefix("false", input) {
171 suggestions = append(suggestions, "false")
176 _, suggestions = ColorschemeComplete(input)
178 if strings.HasPrefix("unix", input) {
179 suggestions = append(suggestions, "unix")
181 if strings.HasPrefix("dos", input) {
182 suggestions = append(suggestions, "dos")
188 if len(suggestions) == 1 {
189 chosen = suggestions[0]
191 return chosen, suggestions
194 // MakeCompletion registers a function from a plugin for autocomplete commands
195 func MakeCompletion(function string) Completion {
196 pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function))
197 return Completion(-len(pluginCompletions))
200 // PluginComplete autocompletes from plugin function
201 func PluginComplete(complete Completion, input string) (chosen string, suggestions []string) {
202 idx := int(-complete) - 1
204 if len(pluginCompletions) <= idx {
207 suggestions = pluginCompletions[idx](input)
209 if len(suggestions) == 1 {
210 chosen = suggestions[0]
215 func PluginCmdComplete(input string) (chosen string, suggestions []string) {
216 for _, cmd := range []string{"install", "remove", "search", "update", "list"} {
217 if strings.HasPrefix(cmd, input) {
218 suggestions = append(suggestions, cmd)
222 if len(suggestions) == 1 {
223 chosen = suggestions[0]
225 return chosen, suggestions
228 func PluginNameComplete(input string) (chosen string, suggestions []string) {
229 for _, pp := range GetAllPluginPackages() {
230 if strings.HasPrefix(pp.Name, input) {
231 suggestions = append(suggestions, pp.Name)
235 if len(suggestions) == 1 {
236 chosen = suggestions[0]
238 return chosen, suggestions