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 dirs := strings.Split(input, "/")
20 var files []os.FileInfo
23 home, _ := homedir.Dir()
25 directories := strings.Join(dirs[:len(dirs)-1], "/")
26 if strings.HasPrefix(directories, "~") {
27 directories = strings.Replace(directories, "~", home, 1)
29 files, err = ioutil.ReadDir(directories)
31 files, err = ioutil.ReadDir(".")
33 var suggestions []string
35 return "", suggestions
37 for _, f := range files {
42 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
43 suggestions = append(suggestions, name)
48 if len(suggestions) == 1 {
50 chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/" + suggestions[0]
52 chosen = suggestions[0]
56 chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/"
60 return chosen, suggestions
63 // CommandComplete autocompletes commands
64 func CommandComplete(input string) (string, []string) {
65 var suggestions []string
66 for cmd := range commands {
67 if strings.HasPrefix(cmd, input) {
68 suggestions = append(suggestions, cmd)
73 if len(suggestions) == 1 {
74 chosen = suggestions[0]
76 return chosen, suggestions
79 // HelpComplete autocompletes help topics
80 func HelpComplete(input string) (string, []string) {
81 var suggestions []string
83 for _, topic := range helpFiles {
84 if strings.HasPrefix(topic, input) {
86 suggestions = append(suggestions, topic)
91 if len(suggestions) == 1 {
92 chosen = suggestions[0]
94 return chosen, suggestions
97 func contains(s []string, e string) bool {
106 // OptionComplete autocompletes options
107 func OptionComplete(input string) (string, []string) {
108 var suggestions []string
109 localSettings := DefaultLocalSettings()
110 for option := range globalSettings {
111 if strings.HasPrefix(option, input) {
112 suggestions = append(suggestions, option)
115 for option := range localSettings {
116 if strings.HasPrefix(option, input) && !contains(suggestions, option) {
117 suggestions = append(suggestions, option)
122 if len(suggestions) == 1 {
123 chosen = suggestions[0]
125 return chosen, suggestions
128 // MakeCompletion registeres a function from a plugin for autocomplete commands
129 func MakeCompletion(function string) Completion {
130 pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function))
131 return Completion(-len(pluginCompletions))
134 // PluginComplete autocompletes from plugin function
135 func PluginComplete(complete Completion, input string) (chosen string, suggestions []string) {
136 idx := int(-complete) - 1
138 if len(pluginCompletions) <= idx {
141 suggestions = pluginCompletions[idx](input)
143 if len(suggestions) == 1 {
144 chosen = suggestions[0]