github.com/elves/elvish@v0.15.0/pkg/edit/histwalk.go (about)

     1  package edit
     2  
     3  import (
     4  	"github.com/elves/elvish/pkg/cli"
     5  	"github.com/elves/elvish/pkg/cli/addons/histwalk"
     6  	"github.com/elves/elvish/pkg/cli/histutil"
     7  	"github.com/elves/elvish/pkg/eval"
     8  )
     9  
    10  //elvdoc:fn history:fast-forward
    11  //
    12  // Import command history entries that happened after the current session
    13  // started.
    14  
    15  func initHistWalk(ed *Editor, ev *eval.Evaler, hs *histStore, nb eval.NsBuilder) {
    16  	bindingVar := newBindingVar(EmptyBindingMap)
    17  	binding := newMapBinding(ed, ev, bindingVar)
    18  	app := ed.app
    19  	nb.AddNs("history",
    20  		eval.NsBuilder{
    21  			"binding": bindingVar,
    22  		}.AddGoFns("<edit:history>", map[string]interface{}{
    23  			"start": func() { histWalkStart(app, hs, binding) },
    24  			"up":    func() { notifyIfError(app, histwalk.Prev(app)) },
    25  			"down":  func() { notifyIfError(app, histwalk.Next(app)) },
    26  			"down-or-quit": func() {
    27  				err := histwalk.Next(app)
    28  				if err == histutil.ErrEndOfHistory {
    29  					histwalk.Close(app)
    30  				} else {
    31  					notifyIfError(app, err)
    32  				}
    33  			},
    34  			"accept": func() { histwalk.Accept(app) },
    35  			"close":  func() { histwalk.Close(app) },
    36  
    37  			"fast-forward": hs.FastForward,
    38  		}).Ns())
    39  }
    40  
    41  func histWalkStart(app cli.App, hs *histStore, binding cli.Handler) {
    42  	buf := app.CodeArea().CopyState().Buffer
    43  	histwalk.Start(app, histwalk.Config{
    44  		Binding: binding, Store: hs, Prefix: buf.Content[:buf.Dot]})
    45  }
    46  
    47  func notifyIfError(app cli.App, err error) {
    48  	if err != nil {
    49  		app.Notify(err.Error())
    50  	}
    51  }