2019-12-18 18:38:21 +08:00
|
|
|
package edit
|
2019-12-18 07:43:00 +08:00
|
|
|
|
|
|
|
import (
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/cli"
|
2021-02-12 02:49:00 +08:00
|
|
|
"src.elv.sh/pkg/cli/mode"
|
|
|
|
"src.elv.sh/pkg/cli/tk"
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval"
|
|
|
|
"src.elv.sh/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) {
|
2021-02-11 09:50:11 +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
|
|
|
}
|
|
|
|
|
2021-02-12 02:49: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,
|
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
|
2021-02-12 02:49:00 +08:00
|
|
|
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
|
2020-04-26 02:22:38 +08:00
|
|
|
src := parse.Source{Name: "[minibuf]", Code: code}
|
2020-05-03 06:11:50 +08:00
|
|
|
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}
|
2021-01-02 08:10:26 +08:00
|
|
|
err := ev.Eval(src, eval.EvalCfg{Ports: ports})
|
2019-12-18 07:43:00 +08:00
|
|
|
if err != nil {
|
|
|
|
app.Notify(err.Error())
|
|
|
|
}
|
|
|
|
}
|