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)
21 var files []os.FileInfo
24 home, _ := homedir.Dir()
26 directories := strings.Join(dirs[:len(dirs)-1], sep)
27 if strings.HasPrefix(directories, "~") {
28 directories = strings.Replace(directories, "~", home, 1)
30 files, err = ioutil.ReadDir(directories)
32 files, err = ioutil.ReadDir(".")
34 var suggestions []string
36 return "", suggestions
38 for _, f := range files {
43 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
44 suggestions = append(suggestions, name)
49 if len(suggestions) == 1 {
51 chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep + suggestions[0]
53 chosen = suggestions[0]
57 chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep
61 return chosen, suggestions
64 // CommandComplete autocompletes commands
65 func CommandComplete(input string) (string, []string) {
66 var suggestions []string
67 for cmd := range commands {
68 if strings.HasPrefix(cmd, input) {
69 suggestions = append(suggestions, cmd)
74 if len(suggestions) == 1 {
75 chosen = suggestions[0]
77 return chosen, suggestions
80 // HelpComplete autocompletes help topics
81 func HelpComplete(input string) (string, []string) {
82 var suggestions []string
84 for _, topic := range helpFiles {
85 if strings.HasPrefix(topic, input) {
87 suggestions = append(suggestions, topic)
92 if len(suggestions) == 1 {
93 chosen = suggestions[0]
95 return chosen, suggestions
98 func contains(s []string, e string) bool {
107 // OptionComplete autocompletes options
108 func OptionComplete(input string) (string, []string) {
109 var suggestions []string
110 localSettings := DefaultLocalSettings()
111 for option := range globalSettings {
112 if strings.HasPrefix(option, input) {
113 suggestions = append(suggestions, option)
116 for option := range localSettings {
117 if strings.HasPrefix(option, input) && !contains(suggestions, option) {
118 suggestions = append(suggestions, option)
123 if len(suggestions) == 1 {
124 chosen = suggestions[0]
126 return chosen, suggestions
129 // MakeCompletion registeres a function from a plugin for autocomplete commands
130 func MakeCompletion(function string) Completion {
131 pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function))
132 return Completion(-len(pluginCompletions))
135 // PluginComplete autocompletes from plugin function
136 func PluginComplete(complete Completion, input string) (chosen string, suggestions []string) {
137 idx := int(-complete) - 1
139 if len(pluginCompletions) <= idx {
142 suggestions = pluginCompletions[idx](input)
144 if len(suggestions) == 1 {
145 chosen = suggestions[0]