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 func contains(s []string, e string) bool {
102 for _, a := range s {
110 // OptionComplete autocompletes options
111 func OptionComplete(input string) (string, []string) {
112 var suggestions []string
113 localSettings := DefaultLocalSettings()
114 for option := range globalSettings {
115 if strings.HasPrefix(option, input) {
116 suggestions = append(suggestions, option)
119 for option := range localSettings {
120 if strings.HasPrefix(option, input) && !contains(suggestions, option) {
121 suggestions = append(suggestions, option)
126 if len(suggestions) == 1 {
127 chosen = suggestions[0]
129 return chosen, suggestions
132 // MakeCompletion registeres a function from a plugin for autocomplete commands
133 func MakeCompletion(function string) Completion {
134 pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function))
135 return Completion(-len(pluginCompletions))
138 // PluginComplete autocompletes from plugin function
139 func PluginComplete(complete Completion, input string) (chosen string, suggestions []string) {
140 idx := int(-complete) - 1
142 if len(pluginCompletions) <= idx {
145 suggestions = pluginCompletions[idx](input)
147 if len(suggestions) == 1 {
148 chosen = suggestions[0]