11 RTColorscheme = "colorscheme"
17 // RuntimeFile allows the program to read runtime data like colorschemes or syntax files
18 type RuntimeFile interface {
19 // Name returns a name of the file without paths or extensions
21 // Data returns the content of the file.
22 Data() ([]byte, error)
25 // allFiles contains all available files, mapped by filetype
26 var allFiles map[string][]RuntimeFile
28 // some file on filesystem
34 // some file on filesystem but with a different name
35 type namedFile struct {
40 func (rf realFile) Name() string {
41 fn := filepath.Base(string(rf))
42 return fn[:len(fn)-len(filepath.Ext(fn))]
45 func (rf realFile) Data() ([]byte, error) {
46 return ioutil.ReadFile(string(rf))
49 func (af assetFile) Name() string {
50 fn := path.Base(string(af))
51 return fn[:len(fn)-len(path.Ext(fn))]
54 func (af assetFile) Data() ([]byte, error) {
55 return Asset(string(af))
58 func (nf namedFile) Name() string {
62 // AddRuntimeFile registers a file for the given filetype
63 func AddRuntimeFile(fileType string, file RuntimeFile) {
65 allFiles = make(map[string][]RuntimeFile)
67 allFiles[fileType] = append(allFiles[fileType], file)
70 // AddRuntimeFilesFromDirectory registers each file from the given directory for
71 // the filetype which matches the file-pattern
72 func AddRuntimeFilesFromDirectory(fileType, directory, pattern string) {
73 files, _ := ioutil.ReadDir(directory)
74 for _, f := range files {
75 if ok, _ := filepath.Match(pattern, f.Name()); !f.IsDir() && ok {
76 fullPath := filepath.Join(directory, f.Name())
77 AddRuntimeFile(fileType, realFile(fullPath))
82 // AddRuntimeFilesFromAssets registers each file from the given asset-directory for
83 // the filetype which matches the file-pattern
84 func AddRuntimeFilesFromAssets(fileType, directory, pattern string) {
85 files, err := AssetDir(directory)
89 for _, f := range files {
90 if ok, _ := path.Match(pattern, f); ok {
91 AddRuntimeFile(fileType, assetFile(path.Join(directory, f)))
96 // FindRuntimeFile finds a runtime file of the given filetype and name
97 // will return nil if no file was found
98 func FindRuntimeFile(fileType, name string) RuntimeFile {
99 for _, f := range ListRuntimeFiles(fileType) {
100 if f.Name() == name {
107 // ListRuntimeFiles lists all known runtime files for the given filetype
108 func ListRuntimeFiles(fileType string) []RuntimeFile {
109 if files, ok := allFiles[fileType]; ok {
112 return []RuntimeFile{}
115 // InitRuntimeFiles initializes all assets file and the config directory
116 func InitRuntimeFiles() {
117 add := func(fileType, dir, pattern string) {
118 AddRuntimeFilesFromDirectory(fileType, filepath.Join(configDir, dir), pattern)
119 AddRuntimeFilesFromAssets(fileType, path.Join("runtime", dir), pattern)
122 add(RTColorscheme, "colorschemes", "*.micro")
123 add(RTSyntax, "syntax", "*.micro")
124 add(RTHelp, "help", "*.md")
126 // Search configDir for plugin-scripts
127 files, _ := ioutil.ReadDir(filepath.Join(configDir, "plugins"))
128 for _, f := range files {
129 realpath, _ := filepath.EvalSymlinks(filepath.Join(configDir, "plugins", f.Name()))
130 realpathStat, _ := os.Stat(realpath)
131 if realpathStat.IsDir() {
132 scriptPath := filepath.Join(configDir, "plugins", f.Name(), f.Name()+".lua")
133 if _, err := os.Stat(scriptPath); err == nil {
134 AddRuntimeFile(RTPlugin, realFile(scriptPath))
139 if files, err := AssetDir("runtime/plugins"); err == nil {
140 for _, f := range files {
141 scriptPath := path.Join("runtime/plugins", f, f+".lua")
142 if _, err := AssetInfo(scriptPath); err == nil {
143 AddRuntimeFile(RTPlugin, assetFile(scriptPath))
149 // PluginReadRuntimeFile allows plugin scripts to read the content of a runtime file
150 func PluginReadRuntimeFile(fileType, name string) string {
151 if file := FindRuntimeFile(fileType, name); file != nil {
152 if data, err := file.Data(); err == nil {
159 // PluginListRuntimeFiles allows plugins to lists all runtime files of the given type
160 func PluginListRuntimeFiles(fileType string) []string {
161 files := ListRuntimeFiles(fileType)
162 result := make([]string, len(files))
163 for i, f := range files {
169 // PluginAddRuntimeFile adds a file to the runtime files for a plugin
170 func PluginAddRuntimeFile(plugin, filetype, filePath string) {
171 fullpath := filepath.Join(configDir, "plugins", plugin, filePath)
172 if _, err := os.Stat(fullpath); err == nil {
173 AddRuntimeFile(filetype, realFile(fullpath))
175 fullpath = path.Join("runtime", "plugins", plugin, filePath)
176 AddRuntimeFile(filetype, assetFile(fullpath))
180 // PluginAddRuntimeFilesFromDirectory adds files from a directory to the runtime files for a plugin
181 func PluginAddRuntimeFilesFromDirectory(plugin, filetype, directory, pattern string) {
182 fullpath := filepath.Join(configDir, "plugins", plugin, directory)
183 if _, err := os.Stat(fullpath); err == nil {
184 AddRuntimeFilesFromDirectory(filetype, fullpath, pattern)
186 fullpath = path.Join("runtime", "plugins", plugin, directory)
187 AddRuntimeFilesFromAssets(filetype, fullpath, pattern)