diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index 437cb618..e97d4ed9 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -11,6 +11,7 @@ import ( "time" "github.com/mitchellh/go-homedir" + "github.com/yuin/gopher-lua" "github.com/zyedidia/clipboard" "github.com/zyedidia/tcell" ) @@ -619,6 +620,14 @@ func (v *View) Save() bool { v.GoSave() } } + if err := L.CallByParam(lua.P{ + Fn: L.GetGlobal("onSave"), + NRet: 1, + Protect: true, + }); err != nil { + // The function isn't defined by this plugin + return true + } return true } @@ -633,7 +642,7 @@ func (v *View) GoSave() { } else { messenger.Message("Saved " + v.buf.path) } - v.reOpen() + v.ReOpen() } else if settings["gofmt"] == true { messenger.Message("Running gofmt...") err := gofmt(v.buf.path) @@ -642,7 +651,7 @@ func (v *View) GoSave() { } else { messenger.Message("Saved " + v.buf.path) } - v.reOpen() + v.ReOpen() return } diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index a05761fb..d9241215 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -7,8 +7,10 @@ import ( "os" "github.com/go-errors/errors" + "github.com/layeh/gopher-luar" "github.com/mattn/go-isatty" "github.com/mitchellh/go-homedir" + "github.com/yuin/gopher-lua" "github.com/zyedidia/tcell" "github.com/zyedidia/tcell/encoding" ) @@ -41,6 +43,9 @@ var ( // Is the help screen open helpOpen = false + + // L is the lua state + L *lua.LState ) // LoadInput loads the file input for the editor @@ -175,6 +180,13 @@ func main() { os.Exit(1) } + L = lua.NewState() + defer L.Close() + + if err := L.DoFile("plugin.lua"); err != nil { + panic(err) + } + encoding.Register() tcell.SetEncodingFallback(tcell.EncodingFallbackASCII) @@ -207,6 +219,10 @@ func main() { messenger = new(Messenger) view := NewView(buf) + L.SetGlobal("view", luar.New(L, view)) + L.SetGlobal("settings", luar.New(L, &settings)) + L.SetGlobal("messenger", luar.New(L, messenger)) + for { // Display everything Redraw(view) diff --git a/cmd/micro/plugin.lua b/cmd/micro/plugin.lua new file mode 100644 index 00000000..c6fab31c --- /dev/null +++ b/cmd/micro/plugin.lua @@ -0,0 +1,8 @@ +function onSave() + local handle = io.popen("goimports -w view.go") + local result = handle:read("*a") + handle:close() + + view:ReOpen() + messenger:Message(result) +end diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 175799f6..5ff868da 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -172,8 +172,8 @@ func (v *View) OpenBuffer(buf *Buffer) { v.lastClickTime = time.Time{} } -// Close and Re-open the current file. -func (v *View) reOpen() { +// ReOpen reloads the current buffer +func (v *View) ReOpen() { if v.CanClose("Continue? (yes, no, save) ") { file, err := ioutil.ReadFile(v.buf.path) filename := v.buf.name