diff --git a/cmd/micro/actions.go b/cmd/micro/actions.go index 68f71952..3e92682a 100644 --- a/cmd/micro/actions.go +++ b/cmd/micro/actions.go @@ -775,12 +775,15 @@ func (v *View) Find(usePlugin bool) bool { return false } + searchStr := "" if v.Cursor.HasSelection() { searchStart = ToCharPos(v.Cursor.CurSelection[1], v.Buf) + searchStart = ToCharPos(v.Cursor.CurSelection[1], v.Buf) + searchStr = v.Cursor.GetSelection() } else { searchStart = ToCharPos(v.Cursor.Loc, v.Buf) } - BeginSearch() + BeginSearch(searchStr) if usePlugin { return PostActionCall("Find", v) @@ -796,9 +799,15 @@ func (v *View) FindNext(usePlugin bool) bool { if v.Cursor.HasSelection() { searchStart = ToCharPos(v.Cursor.CurSelection[1], v.Buf) + if lastSearch == "" { // or always? FIXME + lastSearch = v.Cursor.GetSelection() + } } else { searchStart = ToCharPos(v.Cursor.Loc, v.Buf) } + if lastSearch == "" { + return true + } messenger.Message("Finding: " + lastSearch) Search(lastSearch, v, true) @@ -1384,6 +1393,21 @@ func (v *View) CommandMode(usePlugin bool) bool { return false } +// Escape leaves current mode / quits the editor +func (v *View) Escape(usePlugin bool) bool { + // check if user is searching, or the last search is still active + if searching || lastSearch != "" { + ExitSearch(v) + return true + } + // check if a prompt is shown, hide it and don't quit + if messenger.hasPrompt { + messenger.Reset() // FIXME + return true + } + return v.Quit(usePlugin) +} + // Quit quits the editor // This behavior needs to be changed and should really only quit the editor if this // is the last view diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index f2c21568..fadad29c 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -74,6 +74,7 @@ var bindingActions = map[string]func(*View, bool) bool{ "ClearStatus": (*View).ClearStatus, "ShellMode": (*View).ShellMode, "CommandMode": (*View).CommandMode, + "Escape": (*View).Escape, "Quit": (*View).Quit, "QuitAll": (*View).QuitAll, "AddTab": (*View).AddTab, @@ -433,6 +434,6 @@ func DefaultBindings() map[string]string { "F4": "Quit", "F7": "Find", "F10": "Quit", - "Esc": "Quit", + "Esc": "Escape", } } diff --git a/cmd/micro/search.go b/cmd/micro/search.go index 96618471..c16e7547 100644 --- a/cmd/micro/search.go +++ b/cmd/micro/search.go @@ -21,10 +21,12 @@ var ( ) // BeginSearch starts a search -func BeginSearch() { +func BeginSearch(searchStr string) { searchHistory = append(searchHistory, "") messenger.historyNum = len(searchHistory) - 1 searching = true + messenger.response = searchStr + messenger.cursorx = Count(searchStr) messenger.hasPrompt = true messenger.Message("Find: ") } @@ -41,13 +43,27 @@ func EndSearch() { } } +// exit the search mode, reset active search phrase, and clear status bar +func ExitSearch(v *View) { + lastSearch = "" + searching = false + messenger.hasPrompt = false + messenger.Clear() + messenger.Reset() + v.Cursor.ResetSelection() +} + // HandleSearchEvent takes an event and a view and will do a real time match from the messenger's output // to the current buffer. It searches down the buffer. func HandleSearchEvent(event tcell.Event, v *View) { switch e := event.(type) { case *tcell.EventKey: switch e.Key() { - case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape, tcell.KeyEnter: + case tcell.KeyEscape: + // Exit the search mode + ExitSearch(v) + return + case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEnter: // Done EndSearch() return