2019-12-18 18:38:21 +08:00
|
|
|
package edit
|
2019-12-08 08:29:19 +08:00
|
|
|
|
|
|
|
import (
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/cli"
|
|
|
|
"github.com/elves/elvish/pkg/cli/addons/instant"
|
|
|
|
"github.com/elves/elvish/pkg/eval"
|
2020-04-26 01:26:17 +08:00
|
|
|
"github.com/elves/elvish/pkg/parse"
|
2019-12-08 08:29:19 +08:00
|
|
|
)
|
|
|
|
|
2019-12-27 00:31:24 +08:00
|
|
|
//elvdoc:var -instant:binding
|
|
|
|
//
|
|
|
|
// Binding for the instant mode.
|
|
|
|
|
|
|
|
//elvdoc:fn -instant:start
|
|
|
|
//
|
|
|
|
// Starts the instant mode. In instant mode, any text entered at the command
|
|
|
|
// line is evaluated immediately, with the output displayed.
|
|
|
|
//
|
|
|
|
// **WARNING**: Beware of unintended consequences when using destructive
|
|
|
|
// commands. For example, if you type `sudo rm -rf /tmp/*` in the instant mode,
|
|
|
|
// Elvish will attempt to evaluate `sudo rm -rf /` when you typed that far.
|
|
|
|
|
2020-12-25 01:39:51 +08:00
|
|
|
func initInstant(ed *Editor, ev *eval.Evaler, nb eval.NsBuilder) {
|
2019-12-08 08:29:19 +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("-instant",
|
|
|
|
eval.NsBuilder{
|
2019-12-08 08:29:19 +08:00
|
|
|
"binding": bindingVar,
|
2019-12-18 07:33:00 +08:00
|
|
|
}.AddGoFns("<edit:-instant>:", map[string]interface{}{
|
2020-05-03 02:51:12 +08:00
|
|
|
"start": func() { instantStart(ed.app, ev, binding) },
|
2020-12-25 01:39:51 +08:00
|
|
|
}).Ns())
|
2019-12-08 08:29:19 +08:00
|
|
|
}
|
|
|
|
|
2019-12-26 08:52:22 +08:00
|
|
|
func instantStart(app cli.App, ev *eval.Evaler, binding cli.Handler) {
|
2019-12-08 08:29:19 +08:00
|
|
|
execute := func(code string) ([]string, error) {
|
2021-01-02 10:39:03 +08:00
|
|
|
outPort, collect, err := eval.CaptureStringPort()
|
2019-12-08 08:29:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-02 10:24:12 +08:00
|
|
|
err = ev.Eval(
|
|
|
|
parse.Source{Name: "[instant]", Code: code},
|
|
|
|
eval.EvalCfg{
|
|
|
|
Ports: []*eval.Port{nil, outPort},
|
|
|
|
Interrupt: eval.ListenInterrupts})
|
|
|
|
return collect(), err
|
|
|
|
}
|
|
|
|
instant.Start(app, instant.Config{Binding: binding, Execute: execute})
|
|
|
|
}
|