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 // OptionValueComplete completes values for various options
146 func OptionValueComplete(inputOpt, input string) (string, []string) {
147 inputOpt = strings.TrimSpace(inputOpt)
148 var suggestions []string
149 localSettings := DefaultLocalSettings()
150 var optionVal interface{}
151 for k, option := range globalSettings {
156 for k, option := range localSettings {
162 switch optionVal.(type) {
164 if strings.HasPrefix("on", input) {
165 suggestions = append(suggestions, "on")
166 } else if strings.HasPrefix("true", input) {
167 suggestions = append(suggestions, "true")
169 if strings.HasPrefix("off", input) {
170 suggestions = append(suggestions, "off")
171 } else if strings.HasPrefix("false", input) {
172 suggestions = append(suggestions, "false")
177 _, suggestions = ColorschemeComplete(input)
179 if strings.HasPrefix("unix", input) {
180 suggestions = append(suggestions, "unix")
182 if strings.HasPrefix("dos", input) {
183 suggestions = append(suggestions, "dos")
186 if strings.HasPrefix("sudo", input) {
187 suggestions = append(suggestions, "sudo")
189 if strings.HasPrefix("doas", input) {
190 suggestions = append(suggestions, "doas")
196 if len(suggestions) == 1 {
197 chosen = suggestions[0]
199 return chosen, suggestions
202 // MakeCompletion registers a function from a plugin for autocomplete commands
203 func MakeCompletion(function string) Completion {
204 pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function))
205 return Completion(-len(pluginCompletions))
208 // PluginComplete autocompletes from plugin function
209 func PluginComplete(complete Completion, input string) (chosen string, suggestions []string) {
210 idx := int(-complete) - 1
212 if len(pluginCompletions) <= idx {
215 suggestions = pluginCompletions[idx](input)
217 if len(suggestions) == 1 {
218 chosen = suggestions[0]
223 // PluginCmdComplete completes with possible choices for the `> plugin` command
224 func PluginCmdComplete(input string) (chosen string, suggestions []string) {
225 for _, cmd := range []string{"install", "remove", "search", "update", "list"} {
226 if strings.HasPrefix(cmd, input) {
227 suggestions = append(suggestions, cmd)
231 if len(suggestions) == 1 {
232 chosen = suggestions[0]
234 return chosen, suggestions
237 // PluginnameComplete completes with the names of loaded plugins
238 func PluginNameComplete(input string) (chosen string, suggestions []string) {
239 for _, pp := range GetAllPluginPackages() {
240 if strings.HasPrefix(pp.Name, input) {
241 suggestions = append(suggestions, pp.Name)
245 if len(suggestions) == 1 {
246 chosen = suggestions[0]
248 return chosen, suggestions