elvish/pkg/edit/minibuf.go

50 lines
1.3 KiB
Go
Raw Normal View History

package edit
2019-12-18 07:43:00 +08:00
import (
2021-09-04 03:44:04 +08:00
"src.elv.sh/pkg/cli/modes"
"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(emptyBindingsMap)
bindings := newMapBindings(ed, ev, bindingVar)
nb.AddNs("minibuf",
eval.BuildNsNamed("edit:minibuf").
AddVar("binding", bindingVar).
AddGoFns(map[string]any{
"start": func() { minibufStart(ed, ev, bindings) },
}))
2019-12-18 07:43:00 +08:00
}
func minibufStart(ed *Editor, ev *eval.Evaler, bindings tk.Bindings) {
w := tk.NewCodeArea(tk.CodeAreaSpec{
2021-09-04 03:44:04 +08:00
Prompt: modes.Prompt(" MINIBUF ", true),
Bindings: bindings,
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.
})
ed.app.PushAddon(w)
ed.app.Redraw()
2019-12-18 07:43:00 +08:00
}
func minibufSubmit(ed *Editor, ev *eval.Evaler) {
app := ed.app
codeArea, ok := app.ActiveWidget().(tk.CodeArea)
2019-12-18 07:43:00 +08:00
if !ok {
return
}
ed.app.PopAddon()
2019-12-18 07:43:00 +08:00
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(modes.ErrorText(err))
2019-12-18 07:43:00 +08:00
}
}