]> git.lizzy.rs Git - micro.git/blobdiff - internal/info/infobuffer.go
Allow aborting while opening a file with backup
[micro.git] / internal / info / infobuffer.go
index 89b926f28edbfe8959dfc285969e5910f0e73e1d..e88701a4aa5674ef2cd84504c9986a86b2e49594 100644 (file)
@@ -2,14 +2,8 @@ package info
 
 import (
        "fmt"
-       "strings"
 
-       "github.com/zyedidia/micro/internal/buffer"
-       luar "layeh.com/gopher-luar"
-
-       "github.com/zyedidia/micro/internal/config"
-       ulua "github.com/zyedidia/micro/internal/lua"
-       "github.com/zyedidia/micro/internal/screen"
+       "github.com/zyedidia/micro/v2/internal/buffer"
 )
 
 // The InfoBuf displays messages and other info at the bottom of the screen.
@@ -60,7 +54,7 @@ func (i *InfoBuf) Close() {
 func (i *InfoBuf) Message(msg ...interface{}) {
        // only display a new message if there isn't an active prompt
        // this is to prevent overwriting an existing prompt to the user
-       if i.HasPrompt == false {
+       if !i.HasPrompt {
                displayMessage := fmt.Sprint(msg...)
                // if there is no active prompt then style and display the message as normal
                i.Msg = displayMessage
@@ -84,7 +78,7 @@ func (i *InfoBuf) ClearGutter() {
 func (i *InfoBuf) Error(msg ...interface{}) {
        // only display a new message if there isn't an active prompt
        // this is to prevent overwriting an existing prompt to the user
-       if i.HasPrompt == false {
+       if !i.HasPrompt {
                // if there is no active prompt then style and display the message as normal
                i.Msg = fmt.Sprint(msg...)
                i.HasMessage, i.HasError = false, true
@@ -134,63 +128,6 @@ func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) {
        i.YNCallback = donecb
 }
 
-// PlugPrompt provides a plugin interface for calling "Prompt" with the appropriate Lua callbacks
-func (i *InfoBuf) PlugPrompt(prompt string, msg string, ptype string, eventcb string, donecb string) {
-       eventLuaFn := strings.Split(eventcb, ".")
-       doneLuaFn := strings.Split(donecb, ".")
-       var luaEventcb func(string)
-       var luaDonecb func(string, bool)
-
-       if len(eventLuaFn) == 2 {
-               plName, plFn := doneLuaFn[0], doneLuaFn[1]
-               pl := config.FindPlugin(plName)
-               if pl != nil {
-                       luaEventcb = func(resp string) {
-                               _, err := pl.Call(plFn, luar.New(ulua.L, resp))
-                               if err != nil && err != config.ErrNoSuchFunction {
-                                       screen.TermMessage(err)
-                               }
-                       }
-               }
-       }
-
-       if len(doneLuaFn) == 2 {
-               plName, plFn := doneLuaFn[0], doneLuaFn[1]
-               pl := config.FindPlugin(plName)
-               if pl != nil {
-                       luaDonecb = func(resp string, canceled bool) {
-                               _, err := pl.Call(plFn, luar.New(ulua.L, resp), luar.New(ulua.L, canceled))
-                               if err != nil && err != config.ErrNoSuchFunction {
-                                       screen.TermMessage(err)
-                               }
-                       }
-               }
-       }
-
-       i.Prompt(prompt, msg, ptype, luaEventcb, luaDonecb)
-}
-
-// PlugYNPrompt provides a plugin interface for calling "YNPrompt" with the appropriate Lua callbacks
-func (i *InfoBuf) PlugYNPrompt(prompt string, donecb string) {
-       doneLuaFn := strings.Split(donecb, ".")
-       var luaDonecb func(bool, bool)
-
-       if len(doneLuaFn) == 2 {
-               plName, plFn := doneLuaFn[0], doneLuaFn[1]
-               pl := config.FindPlugin(plName)
-               if pl != nil {
-                       luaDonecb = func(resp bool, canceled bool) {
-                               _, err := pl.Call(plFn, luar.New(ulua.L, resp), luar.New(ulua.L, canceled))
-                               if err != nil && err != config.ErrNoSuchFunction {
-                                       screen.TermMessage(err)
-                               }
-                       }
-               }
-       }
-
-       i.YNPrompt(prompt, luaDonecb)
-}
-
 // DonePrompt finishes the current prompt and indicates whether or not it was canceled
 func (i *InfoBuf) DonePrompt(canceled bool) {
        hadYN := i.HasYN
@@ -200,18 +137,27 @@ func (i *InfoBuf) DonePrompt(canceled bool) {
        if !hadYN {
                if i.PromptCallback != nil {
                        if canceled {
+                               i.Replace(i.Start(), i.End(), "")
                                i.PromptCallback("", true)
                                h := i.History[i.PromptType]
                                i.History[i.PromptType] = h[:len(h)-1]
                        } else {
                                resp := string(i.LineBytes(0))
+                               i.Replace(i.Start(), i.End(), "")
                                i.PromptCallback(resp, false)
                                h := i.History[i.PromptType]
                                h[len(h)-1] = resp
+
+                               // avoid duplicates
+                               for j := len(h) - 2; j >= 0; j-- {
+                                       if h[j] == h[len(h)-1] {
+                                               i.History[i.PromptType] = append(h[:j], h[j+1:]...)
+                                               break
+                                       }
+                               }
                        }
-                       i.PromptCallback = nil
+                       // i.PromptCallback = nil
                }
-               i.Replace(i.Start(), i.End(), "")
        }
        if i.YNCallback != nil && hadYN {
                i.YNCallback(i.YNResp, canceled)