Skip to content

Commit

Permalink
Non-modal search; fixes zyedidia#1150
Browse files Browse the repository at this point in the history
Also fixes part of zyedidia#941 (can scroll; can't select yet)
  • Loading branch information
supbish committed Aug 28, 2018
1 parent a92b256 commit a55b7fe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
10 changes: 2 additions & 8 deletions cmd/micro/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1075,14 +1075,13 @@ func (v *View) FindNext(usePlugin bool) bool {

if v.Cursor.HasSelection() {
searchStart = v.Cursor.CurSelection[1]
// lastSearch = v.Cursor.GetSelection()
} else {
searchStart = v.Cursor.Loc
}
if lastSearch == "" {
return true
}
messenger.Message("Finding: " + lastSearch)
messenger.Message("Searched next: " + lastSearch)
Search(lastSearch, v, true)

if usePlugin {
Expand All @@ -1102,7 +1101,7 @@ func (v *View) FindPrevious(usePlugin bool) bool {
} else {
searchStart = v.Cursor.Loc
}
messenger.Message("Finding: " + lastSearch)
messenger.Message("Searched previous: " + lastSearch)
Search(lastSearch, v, false)

if usePlugin {
Expand Down Expand Up @@ -1819,11 +1818,6 @@ func (v *View) ToggleOverwriteMode(usePlugin bool) bool {
// Escape leaves current mode
func (v *View) Escape(usePlugin bool) bool {
if v.mainCursor() {
// 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
Expand Down
7 changes: 7 additions & 0 deletions cmd/micro/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var mouseBindings map[Key][]func(*View, bool, *tcell.EventMouse) bool
var helpBinding string
var kmenuBinding string

var reverseBindingKeys map[tcell.Key]string

var mouseBindingActions = map[string]func(*View, bool, *tcell.EventMouse) bool{
"MousePress": (*View).MousePress,
"MouseMultiCursor": (*View).MouseMultiCursor,
Expand Down Expand Up @@ -279,6 +281,11 @@ func InitBindings() {
bindingsStr = make(map[string]string)
mouseBindings = make(map[Key][]func(*View, bool, *tcell.EventMouse) bool)

reverseBindingKeys = make(map[tcell.Key]string)
for k, v := range bindingKeys {
reverseBindingKeys[v] = k
}

var parsed map[string]string
defaults := DefaultBindings()

Expand Down
4 changes: 2 additions & 2 deletions cmd/micro/micro.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ func main() {
t.Resize()
}
case *tcell.EventMouse:
if !searching {
// if !searching {
if e.Buttons() == tcell.Button1 {
// If the user left clicked we check a couple things
_, h := screen.Size()
Expand Down Expand Up @@ -557,7 +557,7 @@ func main() {
didAction = true
}
}
}
// }
}

if !didAction {
Expand Down
20 changes: 19 additions & 1 deletion cmd/micro/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func EndSearch() {

// ExitSearch exits the search mode, reset active search phrase, and clear status bar
func ExitSearch(v *View) {
lastSearch = ""
searching = false
messenger.hasPrompt = false
messenger.Clear()
Expand All @@ -59,6 +58,21 @@ func ExitSearch(v *View) {
func HandleSearchEvent(event tcell.Event, v *View) {
switch e := event.(type) {
case *tcell.EventKey:

// Handle FindNext and FindPrevious while composing search
switch bindingsStr[reverseBindingKeys[e.Key()]] {
case "Find", "FindNext":
lastSearch = messenger.response
v.FindNext(true)
v.Relocate()
return
case "FindPrevious":
lastSearch = messenger.response
v.FindPrevious(true)
v.Relocate()
return
}

switch e.Key() {
case tcell.KeyEscape:
// Exit the search mode
Expand All @@ -74,6 +88,10 @@ func HandleSearchEvent(event tcell.Event, v *View) {
EndSearch()
return
}
// Do nothing on mouse events.
// Prevents relocating on mouse motion, so we can scroll the view while searching.
case *tcell.EventMouse:
return
}

messenger.HandleEvent(event, searchHistory)
Expand Down

0 comments on commit a55b7fe

Please sign in to comment.