mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-17 22:37:10 +09:00
allow plugins to have a help file
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user