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 // MakeCompletion registers a function from a plugin for autocomplete commands
152 func MakeCompletion(function string) Completion {
153 pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function))
154 return Completion(-len(pluginCompletions))
157 // PluginComplete autocompletes from plugin function
158 func PluginComplete(complete Completion, input string) (chosen string, suggestions []string) {
159 idx := int(-complete) - 1
161 if len(pluginCompletions) <= idx {
164 suggestions = pluginCompletions[idx](input)
166 if len(suggestions) == 1 {
167 chosen = suggestions[0]
172 func PluginCmdComplete(input string) (chosen string, suggestions []string) {
173 for _, cmd := range []string{"install", "remove", "search", "update", "list"} {
174 if strings.HasPrefix(cmd, input) {
175 suggestions = append(suggestions, cmd)
179 if len(suggestions) == 1 {
180 chosen = suggestions[0]
182 return chosen, suggestions
185 func PluginNameComplete(input string) (chosen string, suggestions []string) {
186 for _, pp := range GetAllPluginPackages() {
187 if strings.HasPrefix(pp.Name, input) {
188 suggestions = append(suggestions, pp.Name)
192 if len(suggestions) == 1 {
193 chosen = suggestions[0]
195 return chosen, suggestions