Merge pull request #354 from boombuler/help

Help
This commit is contained in:
Zachary Yedidia
2016-09-14 17:13:46 -04:00
committed by GitHub
6 changed files with 49 additions and 31 deletions

View File

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

View File

@@ -1,25 +1,38 @@
package main
var helpPages map[string]string
import (
"io/ioutil"
)
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")
}
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)
}

View File

@@ -242,9 +242,6 @@ func main() {
// Load the syntax files, including the colorscheme
LoadSyntaxFiles()
// Load the help files
LoadHelp()
// Start the screen
InitScreen()

View 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)
}
}
}

View File

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

View File

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