2019-12-18 18:38:21 +08:00
|
|
|
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"
|
2020-04-26 01:26:17 +08:00
|
|
|
"github.com/elves/elvish/pkg/parse"
|
2019-12-18 07:43:00 +08:00
|
|
|
)
|
|
|
|
|
2020-12-25 01:39:51 +08:00
|
|
|
func initMinibuf(ed *Editor, ev *eval.Evaler, nb eval.NsBuilder) {
|
2019-12-18 07:43:00 +08:00
|
|
|
bindingVar := newBindingVar(EmptyBindingMap)
|
2020-05-03 02:51:12 +08:00
|
|
|
binding := newMapBinding(ed, ev, bindingVar)
|
2020-12-25 01:39:51 +08:00
|
|
|
nb.AddNs("minibuf",
|
|
|
|
eval.NsBuilder{
|
2019-12-18 07:43:00 +08:00
|
|
|
"binding": bindingVar,
|
|
|
|
}.AddGoFns("<edit:minibuf>:", map[string]interface{}{
|
2020-05-03 06:11:50 +08:00
|
|
|
"start": func() { minibufStart(ed, ev, binding) },
|
2020-12-25 01:39:51 +08:00
|
|
|
}).Ns())
|
2019-12-18 07:43:00 +08:00
|
|
|
}
|
|
|
|
|
2020-05-03 06:11:50 +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,
|
2020-05-03 06:11:50 +08:00
|
|
|
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.
|
|
|
|
})
|
2020-05-03 06:11:50 +08:00
|
|
|
cli.SetAddon(ed.app, w)
|
|
|
|
ed.app.Redraw()
|
2019-12-18 07:43:00 +08:00
|
|
|
}
|
|
|
|
|
2020-05-03 06:11:50 +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
|
2020-04-26 02:22:38 +08:00
|
|
|
src := parse.Source{Name: "[minibuf]", Code: code}
|
2020-04-26 20:14:51 +08:00
|
|
|
op, err := ev.ParseAndCompile(src, nil)
|
2019-12-18 07:43:00 +08:00
|
|
|
if err != nil {
|
|
|
|
app.Notify(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-05-03 06:11:50 +08:00
|
|
|
notifyPort, cleanup := makeNotifyPort(ed)
|
2019-12-18 07:43:00 +08:00
|
|
|
defer cleanup()
|
|
|
|
ports := []*eval.Port{eval.DevNullClosedChan, notifyPort, notifyPort}
|
2020-04-16 07:11:23 +08:00
|
|
|
err = ev.Eval(op, eval.EvalCfg{Ports: ports})
|
2019-12-18 07:43:00 +08:00
|
|
|
if err != nil {
|
|
|
|
app.Notify(err.Error())
|
|
|
|
}
|
|
|
|
}
|