package edit

import (
	

	
	
	
	
	
	
	
)

//elvdoc:var selected-file
//
// Name of the currently selected file in navigation mode. $nil if not in
// navigation mode.

//elvdoc:var navigation:binding
//
// Keybinding for the navigation mode.

//elvdoc:fn navigation:start
//
// Start the navigation mode.

//elvdoc:fn navigation:insert-selected
//
// Inserts the selected filename.

func ( cli.App) {
	,  := getNavigation()
	if ! {
		return
	}
	 := .SelectedName()
	if  == "" {
		// User pressed Alt-Enter or Enter in an empty directory with nothing
		// selected; don't do anything.
		return
	}

	.CodeArea().MutateState(func( *tk.CodeAreaState) {
		 := .Buffer.Dot
		if  != 0 && !strings.ContainsRune(" \n", rune(.Buffer.Content[-1])) {
			// The dot is not at the beginning of a buffer, and the previous
			// character is not a space or newline. Insert a space.
			.Buffer.InsertAtDot(" ")
		}
		// Insert the selected filename.
		.Buffer.InsertAtDot(parse.Quote())
	})
}

//elvdoc:fn navigation:insert-selected-and-quit
//
// Inserts the selected filename and closes the navigation addon.

func ( cli.App) {
	navInsertSelected()
	closeMode()
}

//elvdoc:fn navigation:trigger-filter
//
// Toggles the filtering status of the navigation addon.

//elvdoc:fn navigation:trigger-shown-hidden
//
// Toggles whether the navigation addon should be showing hidden files.

//elvdoc:var navigation:width-ratio
//
// A list of 3 integers, used for specifying the width ratio of the 3 columns in
// navigation mode.

func ( interface{}) [3]int {
	var (
		 []int
		  bool
	)
	vals.Iterate(, func( interface{}) bool {
		var  int
		 := vals.ScanToGo(, &)
		if  != nil {
			 = true
			return false
		}
		 = append(, )
		return true
	})
	if  || len() != 3 {
		// TODO: Handle the error.
		return [3]int{1, 3, 4}
	}
	var  [3]int
	copy([:], )
	return 
}

func ( *Editor,  *eval.Evaler,  eval.NsBuilder) {
	 := newBindingVar(emptyBindingsMap)
	 := newMapBindings(, , )
	 := newListVar(vals.MakeList(1.0, 3.0, 4.0))

	 := vars.FromGet(func() interface{} {
		if ,  := getNavigation(.app);  {
			return .SelectedName()
		}
		return nil
	})

	 := .app
	.Add("selected-file", )
	.AddNs("navigation",
		eval.NsBuilder{
			"binding":     ,
			"width-ratio": ,
		}.AddGoFns("<edit:navigation>", map[string]interface{}{
			"start": func() {
				 := mode.NewNavigation(, mode.NavigationSpec{
					Bindings: ,
					WidthRatio: func() [3]int {
						return convertNavWidthRatio(.Get())
					},
					Filter: filterSpec,
				})
				startMode(, , nil)
			},
			"left":  actOnNavigation(, mode.Navigation.Ascend),
			"right": actOnNavigation(, mode.Navigation.Descend),
			"up": actOnNavigation(,
				func( mode.Navigation) { .Select(tk.Prev) }),
			"down": actOnNavigation(,
				func( mode.Navigation) { .Select(tk.Next) }),
			"page-up": actOnNavigation(,
				func( mode.Navigation) { .Select(tk.PrevPage) }),
			"page-down": actOnNavigation(,
				func( mode.Navigation) { .Select(tk.NextPage) }),

			"file-preview-up": actOnNavigation(,
				func( mode.Navigation) { .ScrollPreview(-1) }),
			"file-preview-down": actOnNavigation(,
				func( mode.Navigation) { .ScrollPreview(1) }),

			"insert-selected":          func() { navInsertSelected() },
			"insert-selected-and-quit": func() { navInsertSelectedAndQuit() },

			"trigger-filter": actOnNavigation(,
				func( mode.Navigation) { .MutateFiltering(neg) }),
			"trigger-shown-hidden": actOnNavigation(,
				func( mode.Navigation) { .MutateShowHidden(neg) }),
		}).Ns())
}

func ( bool) bool { return ! }

func ( cli.App) (mode.Navigation, bool) {
	,  := .CopyState().Addon.(mode.Navigation)
	return , 
}

func ( cli.App,  func(mode.Navigation)) func() {
	return func() {
		if ,  := getNavigation();  {
			()
		}
	}
}