Improvement: improve Search behaviour, and Escape key behaviour

This commit is contained in:
Saeed Rasooli
2016-10-12 11:41:45 +03:30
parent ea1de18326
commit 127ebc15b9
3 changed files with 45 additions and 4 deletions

View File

@@ -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

View File

@@ -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",
}
}

View File

@@ -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