From a7f159bddce3b54f16969de6522fb5eeefdb69f1 Mon Sep 17 00:00:00 2001 From: boombuler Date: Tue, 13 Sep 2016 08:53:20 +0200 Subject: [PATCH 1/2] Load help files when needed --- cmd/micro/autocomplete.go | 3 +-- cmd/micro/help.go | 36 ++++++++++++++++-------------------- cmd/micro/micro.go | 3 --- cmd/micro/view.go | 17 ++++++++++------- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/cmd/micro/autocomplete.go b/cmd/micro/autocomplete.go index 941f0fc5..84904252 100644 --- a/cmd/micro/autocomplete.go +++ b/cmd/micro/autocomplete.go @@ -81,9 +81,8 @@ func CommandComplete(input string) (string, []string) { func HelpComplete(input string) (string, []string) { var suggestions []string - for _, topic := range helpFiles { + for topic, _ := range helpPages { if strings.HasPrefix(topic, input) { - suggestions = append(suggestions, topic) } } diff --git a/cmd/micro/help.go b/cmd/micro/help.go index 4cced6ad..eaf9f0ee 100644 --- a/cmd/micro/help.go +++ b/cmd/micro/help.go @@ -1,25 +1,21 @@ package main -var helpPages map[string]string - -var helpFiles = []string{ - "help", - "keybindings", - "plugins", - "colors", - "options", - "commands", - "tutorial", +type HelpPage interface { + HelpFile() ([]byte, error) } -// LoadHelp loads the help text from inside the binary -func LoadHelp() { - helpPages = make(map[string]string) - for _, file := range helpFiles { - data, err := Asset("runtime/help/" + file + ".md") - if err != nil { - TermMessage("Unable to load help text", file) - } - helpPages[file] = string(data) - } +var helpPages map[string]HelpPage = map[string]HelpPage{ + "help": assetHelpPage("help"), + "keybindings": assetHelpPage("keybindings"), + "plugins": assetHelpPage("plugins"), + "colors": assetHelpPage("colors"), + "options": assetHelpPage("options"), + "commands": assetHelpPage("commands"), + "tutorial": assetHelpPage("tutorial"), +} + +type assetHelpPage string + +func (file assetHelpPage) HelpFile() ([]byte, error) { + return Asset("runtime/help/" + string(file) + ".md") } diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 71076089..2081fe2f 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -242,9 +242,6 @@ func main() { // Load the syntax files, including the colorscheme LoadSyntaxFiles() - // Load the help files - LoadHelp() - // Start the screen InitScreen() diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 889bbf9b..dff2b11b 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -529,15 +529,18 @@ func (v *View) ClearAllGutterMessages() { // Opens the given help page in a new horizontal split func (v *View) openHelp(helpPage string) { - if v.Help { - helpBuffer := NewBuffer([]byte(helpPages[helpPage]), helpPage+".md") - helpBuffer.Name = "Help" - v.OpenBuffer(helpBuffer) + if data, err := helpPages[helpPage].HelpFile(); err != nil { + TermMessage("Unable to load help text", helpPage, "\n", err) } else { - helpBuffer := NewBuffer([]byte(helpPages[helpPage]), helpPage+".md") + helpBuffer := NewBuffer(data, helpPage+".md") helpBuffer.Name = "Help" - v.HSplit(helpBuffer) - CurView().Help = true + + if v.Help { + v.OpenBuffer(helpBuffer) + } else { + v.HSplit(helpBuffer) + CurView().Help = true + } } } From d250b9d7b03b1db0d6f3a5d24b20e77d0e75891f Mon Sep 17 00:00:00 2001 From: boombuler Date: Tue, 13 Sep 2016 09:06:06 +0200 Subject: [PATCH 2/2] 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