Interface with plugin.lua file

This commit is contained in:
Zachary Yedidia
2016-04-24 19:52:02 -04:00
parent 87d9221a73
commit e05e993e25
4 changed files with 37 additions and 4 deletions

View File

@@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"github.com/yuin/gopher-lua"
"github.com/zyedidia/clipboard" "github.com/zyedidia/clipboard"
"github.com/zyedidia/tcell" "github.com/zyedidia/tcell"
) )
@@ -619,6 +620,14 @@ func (v *View) Save() bool {
v.GoSave() 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 return true
} }
@@ -633,7 +642,7 @@ func (v *View) GoSave() {
} else { } else {
messenger.Message("Saved " + v.buf.path) messenger.Message("Saved " + v.buf.path)
} }
v.reOpen() v.ReOpen()
} else if settings["gofmt"] == true { } else if settings["gofmt"] == true {
messenger.Message("Running gofmt...") messenger.Message("Running gofmt...")
err := gofmt(v.buf.path) err := gofmt(v.buf.path)
@@ -642,7 +651,7 @@ func (v *View) GoSave() {
} else { } else {
messenger.Message("Saved " + v.buf.path) messenger.Message("Saved " + v.buf.path)
} }
v.reOpen() v.ReOpen()
return return
} }

View File

@@ -7,8 +7,10 @@ import (
"os" "os"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/layeh/gopher-luar"
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"github.com/yuin/gopher-lua"
"github.com/zyedidia/tcell" "github.com/zyedidia/tcell"
"github.com/zyedidia/tcell/encoding" "github.com/zyedidia/tcell/encoding"
) )
@@ -41,6 +43,9 @@ var (
// Is the help screen open // Is the help screen open
helpOpen = false helpOpen = false
// L is the lua state
L *lua.LState
) )
// LoadInput loads the file input for the editor // LoadInput loads the file input for the editor
@@ -175,6 +180,13 @@ func main() {
os.Exit(1) os.Exit(1)
} }
L = lua.NewState()
defer L.Close()
if err := L.DoFile("plugin.lua"); err != nil {
panic(err)
}
encoding.Register() encoding.Register()
tcell.SetEncodingFallback(tcell.EncodingFallbackASCII) tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
@@ -207,6 +219,10 @@ func main() {
messenger = new(Messenger) messenger = new(Messenger)
view := NewView(buf) 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 { for {
// Display everything // Display everything
Redraw(view) Redraw(view)

8
cmd/micro/plugin.lua Normal file
View File

@@ -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

View File

@@ -172,8 +172,8 @@ func (v *View) OpenBuffer(buf *Buffer) {
v.lastClickTime = time.Time{} v.lastClickTime = time.Time{}
} }
// Close and Re-open the current file. // ReOpen reloads the current buffer
func (v *View) reOpen() { func (v *View) ReOpen() {
if v.CanClose("Continue? (yes, no, save) ") { if v.CanClose("Continue? (yes, no, save) ") {
file, err := ioutil.ReadFile(v.buf.path) file, err := ioutil.ReadFile(v.buf.path)
filename := v.buf.name filename := v.buf.name