11 RTColorscheme = "colorscheme"
16 // RuntimeFile allows the program to read runtime data like colorschemes or syntax files
17 type RuntimeFile interface {
18 // Name returns a name of the file without paths or extensions
20 // Data returns the content of the file.
21 Data() ([]byte, error)
24 // allFiles contains all available files, mapped by filetype
25 var allFiles map[string][]RuntimeFile
27 // some file on filesystem
33 // some file on filesystem but with a different name
34 type namedFile struct {
39 func (rf realFile) Name() string {
40 fn := filepath.Base(string(rf))
41 return fn[:len(fn)-len(filepath.Ext(fn))]
44 func (rf realFile) Data() ([]byte, error) {
45 return ioutil.ReadFile(string(rf))
48 func (af assetFile) Name() string {
49 fn := path.Base(string(af))
50 return fn[:len(fn)-len(path.Ext(fn))]
53 func (af assetFile) Data() ([]byte, error) {
54 return Asset(string(af))
57 func (nf namedFile) Name() string {
61 // AddRuntimeFile registers a file for the given filetype
62 func AddRuntimeFile(fileType string, file RuntimeFile) {
64 allFiles = make(map[string][]RuntimeFile)
66 allFiles[fileType] = append(allFiles[fileType], file)
69 // AddRuntimeFilesFromDirectory registers each file from the given directory for
70 // the filetype which matches the file-pattern
71 func AddRuntimeFilesFromDirectory(fileType, directory, pattern string) {
72 files, _ := ioutil.ReadDir(directory)
73 for _, f := range files {
74 if ok, _ := filepath.Match(pattern, f.Name()); !f.IsDir() && ok {
75 fullPath := filepath.Join(directory, f.Name())
76 AddRuntimeFile(fileType, realFile(fullPath))
81 // AddRuntimeFilesFromAssets registers each file from the given asset-directory for
82 // the filetype which matches the file-pattern
83 func AddRuntimeFilesFromAssets(fileType, directory, pattern string) {
84 files, err := AssetDir(directory)
88 for _, f := range files {
89 if ok, _ := path.Match(pattern, f); ok {
90 AddRuntimeFile(fileType, assetFile(path.Join(directory, f)))
95 // FindRuntimeFile finds a runtime file of the given filetype and name
96 // will return nil if no file was found
97 func FindRuntimeFile(fileType, name string) RuntimeFile {
98 for _, f := range ListRuntimeFiles(fileType) {
106 // ListRuntimeFiles lists all known runtime files for the given filetype
107 func ListRuntimeFiles(fileType string) []RuntimeFile {
108 if files, ok := allFiles[fileType]; ok {
111 return []RuntimeFile{}
114 // InitRuntimeFiles initializes all assets file and the config directory
115 func InitRuntimeFiles() {
116 add := func(fileType, dir, pattern string) {
117 AddRuntimeFilesFromDirectory(fileType, filepath.Join(configDir, dir), pattern)
118 AddRuntimeFilesFromAssets(fileType, path.Join("runtime", dir), pattern)
121 add(RTColorscheme, "colorschemes", "*.micro")
122 add(RTSyntax, "syntax", "*.micro")
123 add(RTHelp, "help", "*.md")
126 // PluginReadRuntimeFile allows plugin scripts to read the content of a runtime file
127 func PluginReadRuntimeFile(fileType, name string) string {
128 if file := FindRuntimeFile(fileType, name); file != nil {
129 if data, err := file.Data(); err == nil {
136 // PluginListRuntimeFiles allows plugins to lists all runtime files of the given type
137 func PluginListRuntimeFiles(fileType string) []string {
138 files := ListRuntimeFiles(fileType)
139 result := make([]string, len(files))
140 for i, f := range files {
146 // PluginAddRuntimeFile adds a file to the runtime files for a plugin
147 func PluginAddRuntimeFile(plugin, filetype, path string) {
148 fullpath := configDir + "/plugins/" + plugin + "/" + path
149 if _, err := os.Stat(fullpath); err == nil {
150 AddRuntimeFile(filetype, realFile(fullpath))
152 fullpath = "runtime/plugins/" + plugin + "/" + path
153 AddRuntimeFile(filetype, assetFile(fullpath))
157 // PluginAddRuntimeFilesFromDirectory adds files from a directory to the runtime files for a plugin
158 func PluginAddRuntimeFilesFromDirectory(plugin, filetype, directory, pattern string) {
159 fullpath := filepath.Join(configDir, "plugins", plugin, directory)
160 if _, err := os.Stat(fullpath); err == nil {
161 AddRuntimeFilesFromDirectory(filetype, fullpath, pattern)
163 fullpath = path.Join("runtime", "plugins", plugin, directory)
164 AddRuntimeFilesFromAssets(filetype, fullpath, pattern)