7 // "github.com/mitchellh/go-homedir"
10 // This file is meant (for now) for autocompletion in command mode, not
11 // while coding. This helps micro autocomplete commands and then filenames
12 // for example with `vsplit filename`.
14 // FileComplete autocompletes filenames
15 func FileComplete(input string) (string, []string) {
16 dirs := strings.Split(input, "/")
17 var files []os.FileInfo
20 // home, _ := homedir.Dir()
22 // directories := strings.Join(dirs[:len(dirs)-1], "/")
23 // if strings.HasPrefix(directories, "~") {
24 // directories = strings.Replace(directories, "~", home)
26 files, err = ioutil.ReadDir(strings.Join(dirs[:len(dirs)-1], "/"))
28 files, err = ioutil.ReadDir(".")
30 var suggestions []string
32 return "", suggestions
34 for _, f := range files {
39 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
40 suggestions = append(suggestions, name)
45 if len(suggestions) == 1 {
47 chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/" + suggestions[0]
49 chosen = suggestions[0]
53 return chosen, suggestions
56 // CommandComplete autocompletes commands
57 func CommandComplete(input string) (string, []string) {
58 var suggestions []string
59 for cmd := range commands {
60 if strings.HasPrefix(cmd, input) {
61 suggestions = append(suggestions, cmd)
66 if len(suggestions) == 1 {
67 chosen = suggestions[0]
69 return chosen, suggestions
72 // HelpComplete autocompletes help topics
73 func HelpComplete(input string) (string, []string) {
74 var suggestions []string
76 for _, topic := range helpFiles {
77 if strings.HasPrefix(topic, input) {
79 suggestions = append(suggestions, topic)
84 if len(suggestions) == 1 {
85 chosen = suggestions[0]
87 return chosen, suggestions
90 // OptionComplete autocompletes options
91 func OptionComplete(input string) (string, []string) {
92 var suggestions []string
93 for option := range settings {
94 if strings.HasPrefix(option, input) {
95 suggestions = append(suggestions, option)
100 if len(suggestions) == 1 {
101 chosen = suggestions[0]
103 return chosen, suggestions