package edit

import (
	

	
	
	
	
	
)

//elvdoc:var history:binding
//
// Binding table for the history mode.

//elvdoc:fn history:start
//
// Starts the history mode.

//elvdoc:fn history:up
//
// Walks to the previous entry in history mode.

//elvdoc:fn history:down
//
// Walks to the next entry in history mode.

//elvdoc:fn history:down-or-quit
//
// Walks to the next entry in history mode, or quit the history mode if already
// at the newest entry.

//elvdoc:fn history:fast-forward
//
// Import command history entries that happened after the current session
// started.

func ( *Editor,  *eval.Evaler,  *histStore,  eval.NsBuilder) {
	 := newBindingVar(emptyBindingsMap)
	 := newMapBindings(, , )
	 := .app
	.AddNs("history",
		eval.NsBuilder{
			"binding": ,
		}.AddGoFns("<edit:history>", map[string]interface{}{
			"start": func() { notifyError(, histwalkStart(, , )) },
			"up":    func() { notifyError(, histwalkDo(, mode.Histwalk.Prev)) },

			"down": func() { notifyError(, histwalkDo(, mode.Histwalk.Next)) },
			"down-or-quit": func() {
				 := histwalkDo(, mode.Histwalk.Next)
				if  == histutil.ErrEndOfHistory {
					.SetAddon(nil, false)
				} else {
					notifyError(, )
				}
			},
			// TODO: Remove these builtins in favor of two universal accept and
			// close builtins
			"accept": func() { .SetAddon(nil, true) },

			"fast-forward": .FastForward,
		}).Ns())
}

func ( cli.App,  *histStore,  tk.Bindings) error {
	 := .CodeArea().CopyState().Buffer
	,  := mode.NewHistwalk(, mode.HistwalkSpec{
		Bindings: , Store: , Prefix: .Content[:.Dot]})
	if  != nil {
		.SetAddon(, false)
	}
	return 
}

var errNotInHistoryMode = errors.New("not in history mode")

func ( cli.App,  func(mode.Histwalk) error) error {
	,  := .CopyState().Addon.(mode.Histwalk)
	if ! {
		return errNotInHistoryMode
	}
	return ()
}

func ( cli.App,  error) {
	if  != nil {
		.Notify(.Error())
	}
}