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 chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/"
58 return chosen, suggestions
61 // CommandComplete autocompletes commands
62 func CommandComplete(input string) (string, []string) {
63 var suggestions []string
64 for cmd := range commands {
65 if strings.HasPrefix(cmd, input) {
66 suggestions = append(suggestions, cmd)
71 if len(suggestions) == 1 {
72 chosen = suggestions[0]
74 return chosen, suggestions
77 // HelpComplete autocompletes help topics
78 func HelpComplete(input string) (string, []string) {
79 var suggestions []string
81 for _, topic := range helpFiles {
82 if strings.HasPrefix(topic, input) {
84 suggestions = append(suggestions, topic)
89 if len(suggestions) == 1 {
90 chosen = suggestions[0]
92 return chosen, suggestions
95 // OptionComplete autocompletes options
96 func OptionComplete(input string) (string, []string) {
97 var suggestions []string
98 localSettings := DefaultLocalSettings()
99 for option := range globalSettings {
100 if strings.HasPrefix(option, input) {
101 suggestions = append(suggestions, option)
104 for option := range localSettings {
105 if strings.HasPrefix(option, input) {
106 suggestions = append(suggestions, option)
111 if len(suggestions) == 1 {
112 chosen = suggestions[0]
114 return chosen, suggestions