elvish/pkg/edit/minibuf.go

51 lines
1.3 KiB
Go
Raw Normal View History

package edit
2019-12-18 07:43:00 +08:00
import (
"src.elv.sh/pkg/cli"
"src.elv.sh/pkg/cli/mode"
"src.elv.sh/pkg/cli/tk"
"src.elv.sh/pkg/eval"
"src.elv.sh/pkg/parse"
2019-12-18 07:43:00 +08:00
)
func initMinibuf(ed *Editor, ev *eval.Evaler, nb eval.NsBuilder) {
bindingVar := newBindingVar(emptyBindingMap)
binding := newMapBinding(ed, ev, bindingVar)
nb.AddNs("minibuf",
eval.NsBuilder{
2019-12-18 07:43:00 +08:00
"binding": bindingVar,
}.AddGoFns("<edit:minibuf>:", map[string]interface{}{
"start": func() { minibufStart(ed, ev, binding) },
}).Ns())
2019-12-18 07:43:00 +08:00
}
func minibufStart(ed *Editor, ev *eval.Evaler, binding tk.Handler) {
w := tk.NewCodeArea(tk.CodeAreaSpec{
Prompt: mode.Prompt(" MINIBUF ", true),
2019-12-18 07:43:00 +08:00
OverlayHandler: binding,
OnSubmit: func() { minibufSubmit(ed, ev) },
2019-12-18 07:43:00 +08:00
// TODO: Add Highlighter. Right now the async highlighter is not
// directly usable.
})
cli.SetAddon(ed.app, w)
ed.app.Redraw()
2019-12-18 07:43:00 +08:00
}
func minibufSubmit(ed *Editor, ev *eval.Evaler) {
app := ed.app
codeArea, ok := cli.Addon(app).(tk.CodeArea)
2019-12-18 07:43:00 +08:00
if !ok {
return
}
cli.SetAddon(app, nil)
code := codeArea.CopyState().Buffer.Content
src := parse.Source{Name: "[minibuf]", Code: code}
notifyPort, cleanup := makeNotifyPort(ed)
2019-12-18 07:43:00 +08:00
defer cleanup()
2021-01-05 12:07:35 +08:00
ports := []*eval.Port{eval.DummyInputPort, notifyPort, notifyPort}
err := ev.Eval(src, eval.EvalCfg{Ports: ports})
2019-12-18 07:43:00 +08:00
if err != nil {
app.Notify(err.Error())
}
}