This commit is contained in:
Zachary Yedidia
2016-12-22 16:38:24 -05:00
10 changed files with 70 additions and 37 deletions

View File

@@ -13,7 +13,7 @@ import (
// PreActionCall executes the lua pre callback if possible
func PreActionCall(funcName string, view *View) bool {
executeAction := true
for _, pl := range loadedPlugins {
for pl := range loadedPlugins {
ret, err := Call(pl+".pre"+funcName, view)
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
TermMessage(err)
@@ -29,7 +29,7 @@ func PreActionCall(funcName string, view *View) bool {
// PostActionCall executes the lua plugin callback if possible
func PostActionCall(funcName string, view *View) bool {
relocate := true
for _, pl := range loadedPlugins {
for pl := range loadedPlugins {
ret, err := Call(pl+".on"+funcName, view)
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
TermMessage(err)
@@ -1651,7 +1651,7 @@ func (v *View) PlayMacro(usePlugin bool) bool {
v.Buf.Insert(v.Cursor.Loc, string(t))
v.Cursor.Right()
for _, pl := range loadedPlugins {
for pl := range loadedPlugins {
_, err := Call(pl+".onRune", string(t), v)
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
TermMessage(err)

View File

@@ -9,6 +9,7 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
@@ -283,6 +284,18 @@ func (b *Buffer) SaveAs(filename string) error {
b.UpdateRules()
dir, _ := homedir.Dir()
b.Path = strings.Replace(filename, "~", dir, 1)
if b.Settings["rmtrailingws"].(bool) {
r, _ := regexp.Compile(`[ \t]+$`)
for lineNum, line := range b.Lines(0, b.NumLines) {
indices := r.FindStringIndex(line)
if indices == nil {
continue
}
startLoc := Loc{indices[0], lineNum}
b.deleteToEnd(startLoc)
}
b.Cursor.Relocate()
}
if b.Settings["eofnewline"].(bool) {
end := b.End()
if b.RuneAt(Loc{end.X - 1, end.Y}) != '\n' {
@@ -362,6 +375,11 @@ func (b *Buffer) remove(start, end Loc) string {
b.Update()
return sub
}
func (b *Buffer) deleteToEnd(start Loc) {
b.IsModified = true
b.LineArray.DeleteToEnd(start)
b.Update()
}
// Start returns the location of the first character in the buffer
func (b *Buffer) Start() Loc {

View File

@@ -133,12 +133,10 @@ func PluginCmd(args []string) {
removed := ""
for _, plugin := range args[1:] {
// check if the plugin exists.
for _, lp := range loadedPlugins {
if lp == plugin {
UninstallPlugin(plugin)
removed += plugin + " "
continue
}
if _, ok := loadedPlugins[plugin]; ok {
UninstallPlugin(plugin)
removed += plugin + " "
continue
}
}
if !IsSpaces(removed) {

View File

@@ -117,7 +117,7 @@ func (eh *EventHandler) Execute(t *TextEvent) {
}
eh.UndoStack.Push(t)
for _, pl := range loadedPlugins {
for pl := range loadedPlugins {
ret, err := Call(pl+".onBeforeTextEvent", t)
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
TermMessage(err)

View File

@@ -385,7 +385,7 @@ func main() {
for _, v := range t.views {
v.Buf.FindFileType()
v.Buf.UpdateRules()
for _, pl := range loadedPlugins {
for pl := range loadedPlugins {
_, err := Call(pl+".onViewOpen", v)
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
TermMessage(err)

View File

@@ -10,7 +10,7 @@ import (
"github.com/yuin/gopher-lua"
)
var loadedPlugins []string
var loadedPlugins map[string]string
// Call calls the lua function 'function'
// If it does not exist nothing happens, if there is an error,
@@ -112,32 +112,39 @@ func LuaFunctionJob(function string) func(string, ...string) {
}
}
// luaPluginName convert a human-friendly plugin name into a valid lua variable name.
func luaPluginName(name string) string {
return strings.Replace(name, "-", "_", -1)
}
// LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
func LoadPlugins() {
loadedPlugins = make(map[string]string)
for _, plugin := range ListRuntimeFiles(RTPlugin) {
alreadyExists := false
pluginName := plugin.Name()
for _, pl := range loadedPlugins {
if pl == pluginName {
alreadyExists = true
break
}
if _, ok := loadedPlugins[pluginName]; ok {
continue
}
if !alreadyExists {
data, err := plugin.Data()
if err != nil {
TermMessage("Error loading plugin: " + pluginName)
continue
}
pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n"
if err := L.DoString(pluginDef + string(data)); err != nil {
TermMessage(err)
continue
}
loadedPlugins = append(loadedPlugins, pluginName)
data, err := plugin.Data()
if err != nil {
TermMessage("Error loading plugin: " + pluginName)
continue
}
pluginLuaName := luaPluginName(pluginName)
pluginDef := "\nlocal P = {}\n" + pluginLuaName + " = P\nsetmetatable(" + pluginLuaName + ", {__index = _G})\nsetfenv(1, P)\n"
if err := L.DoString(pluginDef + string(data)); err != nil {
TermMessage(err)
continue
}
loadedPlugins[pluginName] = pluginLuaName
}
if _, err := os.Stat(configDir + "/init.lua"); err == nil {
@@ -146,6 +153,6 @@ func LoadPlugins() {
if err := L.DoString(pluginDef + string(data)); err != nil {
TermMessage(err)
}
loadedPlugins = append(loadedPlugins, "init")
loadedPlugins["init"] = "init"
}
}

View File

@@ -358,8 +358,8 @@ func GetInstalledVersions(withCore bool) PluginVersions {
result = append(result, newStaticPluginVersion(CorePluginName, Version))
}
for _, name := range loadedPlugins {
version := GetInstalledPluginVersion(name)
for name, lpname := range loadedPlugins {
version := GetInstalledPluginVersion(lpname)
if pv := newStaticPluginVersion(name, version); pv != nil {
result = append(result, pv)
}
@@ -561,7 +561,9 @@ func (pv PluginVersions) install() {
func UninstallPlugin(name string) {
if err := os.RemoveAll(filepath.Join(configDir, "plugins", name)); err != nil {
messenger.Error(err)
return
}
delete(loadedPlugins, name)
}
// Install installs the plugin
@@ -582,7 +584,9 @@ func (pl PluginPackage) Install() {
func UpdatePlugins(plugins []string) {
// if no plugins are specified, update all installed plugins.
if len(plugins) == 0 {
plugins = loadedPlugins
for name := range loadedPlugins {
plugins = append(plugins, name)
}
}
messenger.AddLog("Checking for plugin updates")

View File

@@ -181,6 +181,7 @@ func DefaultGlobalSettings() map[string]interface{} {
"colorscheme": "default",
"cursorline": true,
"eofnewline": false,
"rmtrailingws": false,
"ignorecase": false,
"indentchar": " ",
"infobar": true,
@@ -212,6 +213,7 @@ func DefaultLocalSettings() map[string]interface{} {
"colorcolumn": float64(0),
"cursorline": true,
"eofnewline": false,
"rmtrailingws": false,
"filetype": "Unknown",
"ignorecase": false,
"indentchar": " ",

View File

@@ -120,7 +120,7 @@ func NewViewWidthHeight(buf *Buffer, w, h int) *View {
v.Height--
}
for _, pl := range loadedPlugins {
for pl := range loadedPlugins {
_, err := Call(pl+".onViewOpen", v)
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
TermMessage(err)
@@ -484,7 +484,7 @@ func (v *View) HandleEvent(event tcell.Event) {
v.Buf.Insert(v.Cursor.Loc, string(e.Rune()))
v.Cursor.Right()
for _, pl := range loadedPlugins {
for pl := range loadedPlugins {
_, err := Call(pl+".onRune", string(e.Rune()), v)
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
TermMessage(err)