mirror of
https://github.com/Hopiu/micro.git
synced 2026-05-16 18:51:06 +00:00
71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/gdamore/tcell"
|
||
|
|
"regexp"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
func BeginSearch() {
|
||
|
|
searching = true
|
||
|
|
messenger.hasPrompt = true
|
||
|
|
messenger.Message("Find: ")
|
||
|
|
}
|
||
|
|
|
||
|
|
func EndSearch() {
|
||
|
|
searching = false
|
||
|
|
messenger.hasPrompt = false
|
||
|
|
messenger.Clear()
|
||
|
|
messenger.Reset()
|
||
|
|
}
|
||
|
|
|
||
|
|
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:
|
||
|
|
// Done
|
||
|
|
EndSearch()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
messenger.HandleEvent(event)
|
||
|
|
|
||
|
|
if messenger.cursorx < 0 {
|
||
|
|
// Done
|
||
|
|
EndSearch()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if messenger.response == "" {
|
||
|
|
v.cursor.ResetSelection()
|
||
|
|
// We don't end the search though
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
str := strings.Join(v.buf.lines[v.cursor.y:], "\n")
|
||
|
|
charPos := ToCharPos(0, v.cursor.y, v.buf)
|
||
|
|
r, err := regexp.Compile(messenger.response)
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
match := r.FindStringIndex(str)
|
||
|
|
if match == nil {
|
||
|
|
str = strings.Join(v.buf.lines[:v.cursor.y], "\n")
|
||
|
|
match = r.FindStringIndex(str)
|
||
|
|
charPos = 0
|
||
|
|
if match == nil {
|
||
|
|
v.cursor.ResetSelection()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
v.cursor.curSelection[0] = charPos + match[0]
|
||
|
|
v.cursor.curSelection[1] = charPos + match[1]
|
||
|
|
v.cursor.x, v.cursor.y = FromCharPos(charPos+match[1]-1, v.buf)
|
||
|
|
if v.Relocate() {
|
||
|
|
v.matches = Match(v)
|
||
|
|
}
|
||
|
|
return
|
||
|
|
}
|