elvish/pkg/edit/minibuf.go

54 lines
1.4 KiB
Go
Raw Normal View History

package edit
2019-12-18 07:43:00 +08:00
import (
2019-12-24 04:00:59 +08:00
"github.com/elves/elvish/pkg/cli"
"github.com/elves/elvish/pkg/eval"
"github.com/elves/elvish/pkg/parse"
2019-12-18 07:43:00 +08:00
)
func initMinibuf(ed *Editor, ev *eval.Evaler, nb eval.NsBuilder) {
2019-12-18 07:43:00 +08:00
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 cli.Handler) {
2019-12-26 08:52:22 +08:00
w := cli.NewCodeArea(cli.CodeAreaSpec{
Prompt: cli.ModePrompt(" 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
2019-12-26 08:52:22 +08:00
codeArea, ok := cli.Addon(app).(cli.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}
op, err := ev.ParseAndCompile(src, nil)
2019-12-18 07:43:00 +08:00
if err != nil {
app.Notify(err.Error())
return
}
notifyPort, cleanup := makeNotifyPort(ed)
2019-12-18 07:43:00 +08:00
defer cleanup()
ports := []*eval.Port{eval.DevNullClosedChan, notifyPort, notifyPort}
err = ev.Eval(op, eval.EvalCfg{Ports: ports})
2019-12-18 07:43:00 +08:00
if err != nil {
app.Notify(err.Error())
}
}