23 NumTypes = 5 // How many filetypes are there
28 // RuntimeFile allows the program to read runtime data like colorschemes or syntax files
29 type RuntimeFile interface {
30 // Name returns a name of the file without paths or extensions
32 // Data returns the content of the file.
33 Data() ([]byte, error)
36 // allFiles contains all available files, mapped by filetype
37 var allFiles [][]RuntimeFile
38 var realFiles [][]RuntimeFile
41 allFiles = make([][]RuntimeFile, NumTypes)
42 realFiles = make([][]RuntimeFile, NumTypes)
45 // NewRTFiletype creates a new RTFiletype
46 func NewRTFiletype() int {
48 allFiles = append(allFiles, []RuntimeFile{})
49 realFiles = append(realFiles, []RuntimeFile{})
53 // some file on filesystem
59 // some file on filesystem but with a different name
60 type namedFile struct {
65 // a file with the data stored in memory
66 type memoryFile struct {
71 func (mf memoryFile) Name() string {
74 func (mf memoryFile) Data() ([]byte, error) {
78 func (rf realFile) Name() string {
79 fn := filepath.Base(string(rf))
80 return fn[:len(fn)-len(filepath.Ext(fn))]
83 func (rf realFile) Data() ([]byte, error) {
84 return ioutil.ReadFile(string(rf))
87 func (af assetFile) Name() string {
88 fn := path.Base(string(af))
89 return fn[:len(fn)-len(path.Ext(fn))]
92 func (af assetFile) Data() ([]byte, error) {
93 return Asset(string(af))
96 func (nf namedFile) Name() string {
100 // AddRuntimeFile registers a file for the given filetype
101 func AddRuntimeFile(fileType RTFiletype, file RuntimeFile) {
102 allFiles[fileType] = append(allFiles[fileType], file)
105 // AddRealRuntimeFile registers a file for the given filetype
106 func AddRealRuntimeFile(fileType RTFiletype, file RuntimeFile) {
107 allFiles[fileType] = append(allFiles[fileType], file)
108 realFiles[fileType] = append(realFiles[fileType], file)
111 // AddRuntimeFilesFromDirectory registers each file from the given directory for
112 // the filetype which matches the file-pattern
113 func AddRuntimeFilesFromDirectory(fileType RTFiletype, directory, pattern string) {
114 files, _ := ioutil.ReadDir(directory)
115 for _, f := range files {
116 if ok, _ := filepath.Match(pattern, f.Name()); !f.IsDir() && ok {
117 fullPath := filepath.Join(directory, f.Name())
118 AddRealRuntimeFile(fileType, realFile(fullPath))
123 // AddRuntimeFilesFromAssets registers each file from the given asset-directory for
124 // the filetype which matches the file-pattern
125 func AddRuntimeFilesFromAssets(fileType RTFiletype, directory, pattern string) {
126 files, err := AssetDir(directory)
130 for _, f := range files {
131 if ok, _ := path.Match(pattern, f); ok {
132 AddRuntimeFile(fileType, assetFile(path.Join(directory, f)))
137 // FindRuntimeFile finds a runtime file of the given filetype and name
138 // will return nil if no file was found
139 func FindRuntimeFile(fileType RTFiletype, name string) RuntimeFile {
140 for _, f := range ListRuntimeFiles(fileType) {
141 if f.Name() == name {
148 // ListRuntimeFiles lists all known runtime files for the given filetype
149 func ListRuntimeFiles(fileType RTFiletype) []RuntimeFile {
150 return allFiles[fileType]
153 // ListRealRuntimeFiles lists all real runtime files (on disk) for a filetype
154 // these runtime files will be ones defined by the user and loaded from the config directory
155 func ListRealRuntimeFiles(fileType RTFiletype) []RuntimeFile {
156 return realFiles[fileType]
159 // InitRuntimeFiles initializes all assets file and the config directory
160 func InitRuntimeFiles() {
161 add := func(fileType RTFiletype, dir, pattern string) {
162 AddRuntimeFilesFromDirectory(fileType, filepath.Join(ConfigDir, dir), pattern)
163 AddRuntimeFilesFromAssets(fileType, path.Join("runtime", dir), pattern)
166 add(RTColorscheme, "colorschemes", "*.micro")
167 add(RTSyntax, "syntax", "*.yaml")
168 add(RTSyntaxHeader, "syntax", "*.hdr")
169 add(RTHelp, "help", "*.md")
171 initlua := filepath.Join(ConfigDir, "init.lua")
172 if _, err := os.Stat(initlua); !os.IsNotExist(err) {
175 p.DirName = "initlua"
176 p.Srcs = append(p.Srcs, realFile(initlua))
177 Plugins = append(Plugins, p)
180 // Search ConfigDir for plugin-scripts
181 plugdir := filepath.Join(ConfigDir, "plug")
182 files, _ := ioutil.ReadDir(plugdir)
184 isID := regexp.MustCompile(`^[_A-Za-z0-9]+$`).MatchString
186 for _, d := range files {
188 srcs, _ := ioutil.ReadDir(filepath.Join(plugdir, d.Name()))
192 for _, f := range srcs {
193 if strings.HasSuffix(f.Name(), ".lua") {
194 p.Srcs = append(p.Srcs, realFile(filepath.Join(plugdir, d.Name(), f.Name())))
195 } else if strings.HasSuffix(f.Name(), ".json") {
196 data, err := ioutil.ReadFile(filepath.Join(plugdir, d.Name(), f.Name()))
200 p.Info, err = NewPluginInfo(data)
209 if !isID(p.Name) || len(p.Srcs) <= 0 {
210 log.Println(p.Name, "is not a plugin")
213 Plugins = append(Plugins, p)
217 plugdir = filepath.Join("runtime", "plugins")
218 if files, err := AssetDir(plugdir); err == nil {
219 for _, d := range files {
220 if srcs, err := AssetDir(filepath.Join(plugdir, d)); err == nil {
225 for _, f := range srcs {
226 if strings.HasSuffix(f, ".lua") {
227 p.Srcs = append(p.Srcs, assetFile(filepath.Join(plugdir, d, f)))
228 } else if strings.HasSuffix(f, ".json") {
229 data, err := Asset(filepath.Join(plugdir, d, f))
233 p.Info, err = NewPluginInfo(data)
241 if !isID(p.Name) || len(p.Srcs) <= 0 {
242 log.Println(p.Name, "is not a plugin")
245 Plugins = append(Plugins, p)
251 // PluginReadRuntimeFile allows plugin scripts to read the content of a runtime file
252 func PluginReadRuntimeFile(fileType RTFiletype, name string) string {
253 if file := FindRuntimeFile(fileType, name); file != nil {
254 if data, err := file.Data(); err == nil {
261 // PluginListRuntimeFiles allows plugins to lists all runtime files of the given type
262 func PluginListRuntimeFiles(fileType RTFiletype) []string {
263 files := ListRuntimeFiles(fileType)
264 result := make([]string, len(files))
265 for i, f := range files {
271 // PluginAddRuntimeFile adds a file to the runtime files for a plugin
272 func PluginAddRuntimeFile(plugin string, filetype RTFiletype, filePath string) error {
273 pl := FindPlugin(plugin)
275 return errors.New("Plugin " + plugin + " does not exist")
278 fullpath := filepath.Join(ConfigDir, "plug", pldir, filePath)
279 if _, err := os.Stat(fullpath); err == nil {
280 AddRealRuntimeFile(filetype, realFile(fullpath))
282 fullpath = path.Join("runtime", "plugins", pldir, filePath)
283 AddRuntimeFile(filetype, assetFile(fullpath))
288 // PluginAddRuntimeFilesFromDirectory adds files from a directory to the runtime files for a plugin
289 func PluginAddRuntimeFilesFromDirectory(plugin string, filetype RTFiletype, directory, pattern string) error {
290 pl := FindPlugin(plugin)
292 return errors.New("Plugin " + plugin + " does not exist")
295 fullpath := filepath.Join(ConfigDir, "plug", pldir, directory)
296 if _, err := os.Stat(fullpath); err == nil {
297 AddRuntimeFilesFromDirectory(filetype, fullpath, pattern)
299 fullpath = path.Join("runtime", "plugins", pldir, directory)
300 AddRuntimeFilesFromAssets(filetype, fullpath, pattern)
305 // PluginAddRuntimeFileFromMemory adds a file to the runtime files for a plugin from a given string
306 func PluginAddRuntimeFileFromMemory(filetype RTFiletype, filename, data string) {
307 AddRealRuntimeFile(filetype, memoryFile{filename, []byte(data)})