Load plugins from ~/.config/micro/plugins

This commit is contained in:
Zachary Yedidia 2016-04-26 16:20:26 -04:00
parent eba820a9c7
commit 603cec9d81
4 changed files with 36 additions and 41 deletions

View file

@ -620,14 +620,15 @@ func (v *View) Save() bool {
v.GoSave()
}
}
if err := L.CallByParam(lua.P{
Fn: L.GetGlobal("onSave"),
NRet: 0,
Protect: true,
}); err != nil {
// The function isn't defined by this plugin
messenger.Error(err)
return true
for _, pl := range loadedPlugins {
if err := L.CallByParam(lua.P{
Fn: L.GetGlobal(pl + "_onSave"),
NRet: 0,
Protect: true,
}); err != nil {
// The function isn't defined by this plugin
messenger.Error(err)
}
}
return true
}

View file

@ -183,10 +183,6 @@ func main() {
L = lua.NewState()
defer L.Close()
if err := L.DoFile("plugin.lua"); err != nil {
panic(err)
}
encoding.Register()
tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
@ -199,6 +195,7 @@ func main() {
LoadSyntaxFiles()
// Load the help files
LoadHelp()
LoadPlugins()
buf := NewBuffer(string(input), filename)

26
cmd/micro/plugin.go Normal file
View file

@ -0,0 +1,26 @@
package main
import (
"io/ioutil"
)
var loadedPlugins []string
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 {
if f.Name() == pluginName+".lua" {
if err := L.DoFile(configDir + "/plugins/" + pluginName + "/" + f.Name()); err != nil {
TermMessage(err)
continue
}
loadedPlugins = append(loadedPlugins, pluginName)
}
}
}
}
}

View file

@ -1,29 +0,0 @@
go = {}
function onSave()
if settings.GoImports then
messenger:Message("Running goimports...")
go.goimports()
elseif settings.GoFmt then
messenger:Message("Running gofmt...")
go.gofmt()
end
end
function go.gofmt()
local handle = io.popen("gofmt -w " .. view.Buf.Path)
local result = handle:read("*a")
handle:close()
view:ReOpen()
messenger:Message(result)
end
function go.goimports()
local handle = io.popen("goimports -w " .. view.Buf.Path)
local result = handle:read("*a")
handle:close()
view:ReOpen()
messenger:Message(result)
end