elvish/pkg/edit/histwalk.go
Qi Xiao d4c434334f website: Use elvdoc for the ref/edit.md.
All existing docs that are not in elvdoc form are added to corresponding files
in pkg/edit.
2019-12-26 15:23:40 +00:00

51 lines
1.4 KiB
Go

package edit
import (
"github.com/elves/elvish/pkg/cli"
"github.com/elves/elvish/pkg/cli/addons/histwalk"
"github.com/elves/elvish/pkg/cli/histutil"
"github.com/elves/elvish/pkg/eval"
)
//elvdoc:fn history:fast-forward
//
// Import command history entries that happened after the current session
// started.
func initHistWalk(app cli.App, ev *eval.Evaler, ns eval.Ns, fuser *histutil.Fuser) {
bindingVar := newBindingVar(EmptyBindingMap)
binding := newMapBinding(app, ev, bindingVar)
ns.AddNs("history",
eval.Ns{
"binding": bindingVar,
}.AddGoFns("<edit:history>", map[string]interface{}{
"start": func() { histWalkStart(app, fuser, binding) },
"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)
}
},
"accept": func() { histwalk.Accept(app) },
"close": func() { histwalk.Close(app) },
"fast-forward": fuser.FastForward,
}))
}
func histWalkStart(app cli.App, fuser *histutil.Fuser, binding cli.Handler) {
buf := app.CodeArea().CopyState().Buffer
walker := fuser.Walker(buf.Content[:buf.Dot])
histwalk.Start(app, histwalk.Config{Binding: binding, Walker: walker})
}
func notifyIfError(app cli.App, err error) {
if err != nil {
app.Notify(err.Error())
}
}