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.
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
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
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
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)