package edit

import (
	
	

	
	
	
	
	
	
)

var errStoreOffline = errors.New("store offline")

//elvdoc:fn command-history
//
// Outputs the entire command history as a stream of maps. Each map has a `id`
// key that identifies the sequence number of the entry, and a `cmd` key that
// identifies the content.
//
// Use indexing to extract individual entries. For example, to extract the
// content of the last command, do this:
//
// ```elvish
// edit:command-history | put [(all)][-1][cmd]
// ```

func ( histutil.Store,  chan<- interface{}) error {
	if  == nil {
		return errStoreOffline
	}
	,  := .AllCmds()
	if  != nil {
		return 
	}
	for ,  := range  {
		 <- vals.MakeMap("id", strconv.Itoa(.Seq), "cmd", .Text)
	}
	return nil
}

//elvdoc:fn insert-last-word
//
// Inserts the last word of the last command.

func ( cli.App,  histutil.Store) error {
	 := .Cursor("")
	.Prev()
	,  := .Get()
	if  != nil {
		return 
	}
	 := parseutil.Wordify(.Text)
	if len() > 0 {
		.CodeArea().MutateState(func( *tk.CodeAreaState) {
			.Buffer.InsertAtDot([len()-1])
		})
	}
	return nil
}

func ( cli.App,  eval.NsBuilder,  histutil.Store) {
	.AddGoFns("<edit>", map[string]interface{}{
		"command-history": func( *eval.Frame) error {
			return commandHistory(, .OutputChan())
		},
		"insert-last-word": func() { insertLastWord(, ) },
	})
}