]> git.lizzy.rs Git - micro.git/blob - cmd/micro/autocomplete.go
Add local settings for each buffer
[micro.git] / cmd / micro / autocomplete.go
1 package main
2
3 import (
4         "io/ioutil"
5         "os"
6         "strings"
7
8         "github.com/mitchellh/go-homedir"
9 )
10
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`.
14
15 // FileComplete autocompletes filenames
16 func FileComplete(input string) (string, []string) {
17         dirs := strings.Split(input, "/")
18         var files []os.FileInfo
19         var err error
20         if len(dirs) > 1 {
21                 home, _ := homedir.Dir()
22
23                 directories := strings.Join(dirs[:len(dirs)-1], "/")
24                 if strings.HasPrefix(directories, "~") {
25                         directories = strings.Replace(directories, "~", home, 1)
26                 }
27                 files, err = ioutil.ReadDir(directories)
28         } else {
29                 files, err = ioutil.ReadDir(".")
30         }
31         var suggestions []string
32         if err != nil {
33                 return "", suggestions
34         }
35         for _, f := range files {
36                 name := f.Name()
37                 if f.IsDir() {
38                         name += "/"
39                 }
40                 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
41                         suggestions = append(suggestions, name)
42                 }
43         }
44
45         var chosen string
46         if len(suggestions) == 1 {
47                 if len(dirs) > 1 {
48                         chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/" + suggestions[0]
49                 } else {
50                         chosen = suggestions[0]
51                 }
52         }
53
54         return chosen, suggestions
55 }
56
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)
63                 }
64         }
65
66         var chosen string
67         if len(suggestions) == 1 {
68                 chosen = suggestions[0]
69         }
70         return chosen, suggestions
71 }
72
73 // HelpComplete autocompletes help topics
74 func HelpComplete(input string) (string, []string) {
75         var suggestions []string
76
77         for _, topic := range helpFiles {
78                 if strings.HasPrefix(topic, input) {
79
80                         suggestions = append(suggestions, topic)
81                 }
82         }
83
84         var chosen string
85         if len(suggestions) == 1 {
86                 chosen = suggestions[0]
87         }
88         return chosen, suggestions
89 }
90
91 // OptionComplete autocompletes options
92 func OptionComplete(input string) (string, []string) {
93         var suggestions []string
94         for option := range globalSettings {
95                 if strings.HasPrefix(option, input) {
96                         suggestions = append(suggestions, option)
97                 }
98         }
99
100         var chosen string
101         if len(suggestions) == 1 {
102                 chosen = suggestions[0]
103         }
104         return chosen, suggestions
105 }