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 var sep string = string(os.PathSeparator)
18 dirs := strings.Split(input, sep)
19 var files []os.FileInfo
22 home, _ := homedir.Dir()
24 directories := strings.Join(dirs[:len(dirs)-1], sep)
25 if strings.HasPrefix(directories, "~") {
26 directories = strings.Replace(directories, "~", home, 1)
28 files, err = ioutil.ReadDir(directories)
30 files, err = ioutil.ReadDir(".")
32 var suggestions []string
34 return "", suggestions
36 for _, f := range files {
41 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
42 suggestions = append(suggestions, name)
47 if len(suggestions) == 1 {
49 chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep + suggestions[0]
51 chosen = suggestions[0]
55 chosen = strings.Join(dirs[:len(dirs)-1], sep) + sep
59 return chosen, suggestions
62 // CommandComplete autocompletes commands
63 func CommandComplete(input string) (string, []string) {
64 var suggestions []string
65 for cmd := range commands {
66 if strings.HasPrefix(cmd, input) {
67 suggestions = append(suggestions, cmd)
72 if len(suggestions) == 1 {
73 chosen = suggestions[0]
75 return chosen, suggestions
78 // HelpComplete autocompletes help topics
79 func HelpComplete(input string) (string, []string) {
80 var suggestions []string
82 for _, topic := range helpFiles {
83 if strings.HasPrefix(topic, input) {
85 suggestions = append(suggestions, topic)
90 if len(suggestions) == 1 {
91 chosen = suggestions[0]
93 return chosen, suggestions
96 func contains(s []string, e string) bool {
105 // OptionComplete autocompletes options
106 func OptionComplete(input string) (string, []string) {
107 var suggestions []string
108 localSettings := DefaultLocalSettings()
109 for option := range globalSettings {
110 if strings.HasPrefix(option, input) {
111 suggestions = append(suggestions, option)
114 for option := range localSettings {
115 if strings.HasPrefix(option, input) && !contains(suggestions, option) {
116 suggestions = append(suggestions, option)
121 if len(suggestions) == 1 {
122 chosen = suggestions[0]
124 return chosen, suggestions