From d250b9d7b03b1db0d6f3a5d24b20e77d0e75891f Mon Sep 17 00:00:00 2001 From: boombuler Date: Tue, 13 Sep 2016 09:06:06 +0200 Subject: [PATCH] allow plugins to have a help file --- cmd/micro/help.go | 17 +++++++++++++++++ cmd/micro/plugin.go | 6 +++++- runtime/help/plugins.md | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cmd/micro/help.go b/cmd/micro/help.go index eaf9f0ee..caaad5b5 100644 --- a/cmd/micro/help.go +++ b/cmd/micro/help.go @@ -1,5 +1,9 @@ package main +import ( + "io/ioutil" +) + type HelpPage interface { HelpFile() ([]byte, error) } @@ -19,3 +23,16 @@ type assetHelpPage string func (file assetHelpPage) HelpFile() ([]byte, error) { return Asset("runtime/help/" + string(file) + ".md") } + +type fileHelpPage string + +func (file fileHelpPage) HelpFile() ([]byte, error) { + return ioutil.ReadFile(string(file)) +} + +func AddPluginHelp(name, file string) { + if _, exists := helpPages[name]; exists { + return + } + helpPages[name] = fileHelpPage(file) +} diff --git a/cmd/micro/plugin.go b/cmd/micro/plugin.go index 607eb2b6..0c84ae62 100644 --- a/cmd/micro/plugin.go +++ b/cmd/micro/plugin.go @@ -4,6 +4,7 @@ import ( "errors" "io/ioutil" "os" + "path/filepath" "strings" "github.com/layeh/gopher-luar" @@ -126,8 +127,9 @@ func LoadPlugins() { 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(configDir + "/plugins/" + pluginName + "/" + f.Name()) + 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 { @@ -135,6 +137,8 @@ func LoadPlugins() { continue } loadedPlugins = append(loadedPlugins, pluginName) + } else if f.Name() == "help.md" { + AddPluginHelp(pluginName, fullPath) } } } diff --git a/runtime/help/plugins.md b/runtime/help/plugins.md index e4142b17..25f83ae3 100644 --- a/runtime/help/plugins.md +++ b/runtime/help/plugins.md @@ -4,6 +4,8 @@ Micro supports creating plugins with a simple Lua system. Every plugin has a main script which is run at startup which should be placed in `~/.config/micro/plugins/pluginName/pluginName.lua`. +If you want to add a help page for your plugin, place a markdown file in `~/.config/micro/plugins/pluginName/help.md`. + There are a number of callback functions which you can create in your plugin to run code at times other than startup. The naming scheme is `onAction(view)`. For example a function which is run every time the user saves