2019-12-18 18:38:21 +08:00
|
|
|
package edit
|
2019-10-27 07:17:43 +08:00
|
|
|
|
|
|
|
import (
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/cli"
|
|
|
|
"src.elv.sh/pkg/cli/histutil"
|
2021-02-12 02:49:00 +08:00
|
|
|
"src.elv.sh/pkg/cli/mode/histwalk"
|
|
|
|
"src.elv.sh/pkg/cli/tk"
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval"
|
2019-10-27 07:17:43 +08:00
|
|
|
)
|
|
|
|
|
2019-12-26 23:23:40 +08:00
|
|
|
//elvdoc:fn history:fast-forward
|
|
|
|
//
|
|
|
|
// Import command history entries that happened after the current session
|
|
|
|
// started.
|
|
|
|
|
2020-12-25 01:39:51 +08:00
|
|
|
func initHistWalk(ed *Editor, ev *eval.Evaler, hs *histStore, 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)
|
|
|
|
app := ed.app
|
2020-12-25 01:39:51 +08:00
|
|
|
nb.AddNs("history",
|
|
|
|
eval.NsBuilder{
|
2019-10-27 07:17:43 +08:00
|
|
|
"binding": bindingVar,
|
|
|
|
}.AddGoFns("<edit:history>", map[string]interface{}{
|
2020-07-14 06:35:19 +08:00
|
|
|
"start": func() { histWalkStart(app, hs, binding) },
|
2019-11-20 18:21:25 +08:00
|
|
|
"up": func() { notifyIfError(app, histwalk.Prev(app)) },
|
|
|
|
"down": func() { notifyIfError(app, histwalk.Next(app)) },
|
|
|
|
"down-or-quit": func() {
|
|
|
|
err := histwalk.Next(app)
|
|
|
|
if err == histutil.ErrEndOfHistory {
|
|
|
|
histwalk.Close(app)
|
|
|
|
} else {
|
|
|
|
notifyIfError(app, err)
|
|
|
|
}
|
|
|
|
},
|
2019-11-04 03:18:24 +08:00
|
|
|
"accept": func() { histwalk.Accept(app) },
|
|
|
|
"close": func() { histwalk.Close(app) },
|
2019-11-20 18:24:01 +08:00
|
|
|
|
2020-07-14 06:35:19 +08:00
|
|
|
"fast-forward": hs.FastForward,
|
2020-12-25 01:39:51 +08:00
|
|
|
}).Ns())
|
2019-10-27 07:17:43 +08:00
|
|
|
}
|
2019-10-29 06:48:16 +08:00
|
|
|
|
2021-02-12 02:49:00 +08:00
|
|
|
func histWalkStart(app cli.App, hs *histStore, binding tk.Handler) {
|
2019-11-04 07:22:36 +08:00
|
|
|
buf := app.CodeArea().CopyState().Buffer
|
2020-07-14 06:35:19 +08:00
|
|
|
histwalk.Start(app, histwalk.Config{
|
|
|
|
Binding: binding, Store: hs, Prefix: buf.Content[:buf.Dot]})
|
2019-10-29 06:48:16 +08:00
|
|
|
}
|
2019-11-20 18:21:25 +08:00
|
|
|
|
|
|
|
func notifyIfError(app cli.App, err error) {
|
|
|
|
if err != nil {
|
|
|
|
app.Notify(err.Error())
|
|
|
|
}
|
|
|
|
}
|