diff --git a/cmd/micro/eventhandler.go b/cmd/micro/eventhandler.go index eed62484..efeca821 100644 --- a/cmd/micro/eventhandler.go +++ b/cmd/micro/eventhandler.go @@ -1,9 +1,11 @@ package main import ( + "strings" "time" dmp "github.com/sergi/go-diff/diffmatchpatch" + "github.com/yuin/gopher-lua" ) const ( @@ -114,6 +116,17 @@ func (eh *EventHandler) Execute(t *TextEvent) { eh.RedoStack = new(Stack) } eh.UndoStack.Push(t) + + for _, pl := range loadedPlugins { + ret, err := Call(pl+".onBeforeTextEvent", t) + if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") { + TermMessage(err) + } + if val, ok := ret.(lua.LBool); ok && val == lua.LFalse { + return + } + } + ExecuteTextEvent(t, eh.buf) } diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 4930aef0..0ce90310 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" "runtime" "strings" @@ -310,6 +311,14 @@ func main() { L.SetGlobal("GetLeadingWhitespace", luar.New(L, GetLeadingWhitespace)) L.SetGlobal("MakeCompletion", luar.New(L, MakeCompletion)) L.SetGlobal("NewBuffer", luar.New(L, NewBuffer)) + L.SetGlobal("RuneStr", luar.New(L, func(r rune) string { + return string(r) + })) + L.SetGlobal("Loc", luar.New(L, func(x, y int) Loc { + return Loc{x, y} + })) + L.SetGlobal("JoinPaths", luar.New(L, filepath.Join)) + L.SetGlobal("configDir", luar.New(L, configDir)) // Used for asynchronous jobs L.SetGlobal("JobStart", luar.New(L, JobStart)) diff --git a/cmd/micro/rtfiles.go b/cmd/micro/rtfiles.go index 3d0ea574..0e537dee 100644 --- a/cmd/micro/rtfiles.go +++ b/cmd/micro/rtfiles.go @@ -144,12 +144,12 @@ func PluginListRuntimeFiles(fileType string) []string { } // PluginAddRuntimeFile adds a file to the runtime files for a plugin -func PluginAddRuntimeFile(plugin, filetype, path string) { - fullpath := configDir + "/plugins/" + plugin + "/" + path +func PluginAddRuntimeFile(plugin, filetype, filePath string) { + fullpath := filepath.Join(configDir, "plugins", plugin, filePath) if _, err := os.Stat(fullpath); err == nil { AddRuntimeFile(filetype, realFile(fullpath)) } else { - fullpath = "runtime/plugins/" + plugin + "/" + path + fullpath = path.Join("runtime", "plugins", plugin, filePath) AddRuntimeFile(filetype, assetFile(fullpath)) } } diff --git a/runtime/help/plugins.md b/runtime/help/plugins.md index 7feb3c5b..efdcec5b 100644 --- a/runtime/help/plugins.md +++ b/runtime/help/plugins.md @@ -43,12 +43,20 @@ for functions are given using Go's type system): * `OS`: variable which gives the OS micro is currently running on (this is the same as Go's GOOS variable, so `darwin`, `windows`, `linux`, `freebsd`...) +* `configDir`: contains the path to the micro configuration files + * `tabs`: a list of all the tabs currently in use * `curTab`: the index of the current tabs in the tabs list * `messenger`: lets you send messages to the user or create prompts +* `RuneStr(r rune) string`: returns a string containing the given rune + +* `Loc(x, y int) Loc`: returns a new `Loc` struct + +* `JoinPaths(dir... string) string` combines multiple directories to a full path + * `GetOption(name string)`: returns the value of the requested option * `AddOption(name string, value interface{})`: sets the given option with the given