diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 0ce90310..0befdbee 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -243,9 +243,6 @@ func main() { InitCommands() InitBindings() - // Load the syntax files, including the colorscheme - LoadSyntaxFiles() - // Start the screen InitScreen() @@ -330,11 +327,14 @@ func main() { L.SetGlobal("ListRuntimeFiles", luar.New(L, PluginListRuntimeFiles)) L.SetGlobal("AddRuntimeFile", luar.New(L, PluginAddRuntimeFile)) - LoadPlugins() - jobs = make(chan JobFunction, 100) events = make(chan tcell.Event, 100) + LoadPlugins() + + // Load the syntax files, including the colorscheme + LoadSyntaxFiles() + for _, t := range tabs { for _, v := range t.views { for _, pl := range loadedPlugins { diff --git a/cmd/micro/plugin.go b/cmd/micro/plugin.go index e728c3dd..d77baea9 100644 --- a/cmd/micro/plugin.go +++ b/cmd/micro/plugin.go @@ -4,7 +4,6 @@ import ( "errors" "io/ioutil" "os" - "path/filepath" "strings" "github.com/layeh/gopher-luar" @@ -13,12 +12,6 @@ import ( var loadedPlugins []string -var preInstalledPlugins = []string{ - "go", - "linter", - "autoclose", -} - // Call calls the lua function 'function' // If it does not exist nothing happens, if there is an error, // the error is returned @@ -121,48 +114,28 @@ func LuaFunctionJob(function string) func(string, ...string) { // LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins func LoadPlugins() { - files, _ := ioutil.ReadDir(configDir + "/plugins") - for _, plugin := range files { - if plugin.IsDir() { - pluginName := plugin.Name() - files, _ := ioutil.ReadDir(configDir + "/plugins/" + pluginName) - for _, f := range files { - fullPath := filepath.Join(configDir, "plugins", pluginName, f.Name()) - if f.Name() == pluginName+".lua" { - data, _ := ioutil.ReadFile(fullPath) - pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n" - - if err := L.DoString(pluginDef + string(data)); err != nil { - TermMessage(err) - continue - } - loadedPlugins = append(loadedPlugins, pluginName) - } - } - } - } - - for _, pluginName := range preInstalledPlugins { + for _, plugin := range ListRuntimeFiles(RTPlugin) { alreadyExists := false + pluginName := plugin.Name() for _, pl := range loadedPlugins { if pl == pluginName { alreadyExists = true break } } + if !alreadyExists { - plugin := "runtime/plugins/" + pluginName + "/" + pluginName + ".lua" - data, err := Asset(plugin) + data, err := plugin.Data() if err != nil { - TermMessage("Error loading pre-installed plugin: " + pluginName) + TermMessage("Error loading plugin: " + pluginName) continue } pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n" + if err := L.DoString(pluginDef + string(data)); err != nil { TermMessage(err) continue } - loadedPlugins = append(loadedPlugins, pluginName) } } diff --git a/cmd/micro/rtfiles.go b/cmd/micro/rtfiles.go index 0e537dee..91bafa08 100644 --- a/cmd/micro/rtfiles.go +++ b/cmd/micro/rtfiles.go @@ -11,6 +11,7 @@ const ( RTColorscheme = "colorscheme" RTSyntax = "syntax" RTHelp = "help" + RTPlugin = "plugin" ) // RuntimeFile allows the program to read runtime data like colorschemes or syntax files @@ -121,6 +122,26 @@ func InitRuntimeFiles() { add(RTColorscheme, "colorschemes", "*.micro") add(RTSyntax, "syntax", "*.micro") add(RTHelp, "help", "*.md") + + // Search configDir for plugin-scripts + files, _ := ioutil.ReadDir(filepath.Join(configDir, "plugins")) + for _, f := range files { + if f.IsDir() { + scriptPath := filepath.Join(configDir, "plugins", f.Name(), f.Name()+".lua") + if _, err := os.Stat(scriptPath); err == nil { + AddRuntimeFile(RTPlugin, realFile(scriptPath)) + } + } + } + + if files, err := AssetDir("runtime/plugins"); err == nil { + for _, f := range files { + scriptPath := path.Join("runtime/plugins", f, f+".lua") + if _, err := AssetInfo(scriptPath); err == nil { + AddRuntimeFile(RTPlugin, assetFile(scriptPath)) + } + } + } } // PluginReadRuntimeFile allows plugin scripts to read the content of a runtime file