8 "github.com/mitchellh/go-homedir"
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 dirs := strings.Split(input, "/")
18 var files []os.FileInfo
21 home, _ := homedir.Dir()
23 directories := strings.Join(dirs[:len(dirs)-1], "/")
24 if strings.HasPrefix(directories, "~") {
25 directories = strings.Replace(directories, "~", home, 1)
27 files, err = ioutil.ReadDir(directories)
29 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], "/") + "/" + suggestions[0]
50 chosen = suggestions[0]
54 return chosen, suggestions
57 // CommandComplete autocompletes commands
58 func CommandComplete(input string) (string, []string) {
59 var suggestions []string
60 for cmd := range commands {
61 if strings.HasPrefix(cmd, input) {
62 suggestions = append(suggestions, cmd)
67 if len(suggestions) == 1 {
68 chosen = suggestions[0]
70 return chosen, suggestions
73 // HelpComplete autocompletes help topics
74 func HelpComplete(input string) (string, []string) {
75 var suggestions []string
77 for _, topic := range helpFiles {
78 if strings.HasPrefix(topic, input) {
80 suggestions = append(suggestions, topic)
85 if len(suggestions) == 1 {
86 chosen = suggestions[0]
88 return chosen, suggestions
91 // OptionComplete autocompletes options
92 func OptionComplete(input string) (string, []string) {
93 var suggestions []string
94 for option := range settings {
95 if strings.HasPrefix(option, input) {
96 suggestions = append(suggestions, option)
101 if len(suggestions) == 1 {
102 chosen = suggestions[0]
104 return chosen, suggestions