4 "github.com/gdamore/tcell"
9 // What was the last search
12 // Where should we start the search down from (or up from)
15 // Is there currently a search in progress
19 // BeginSearch starts a search
22 messenger.hasPrompt = true
23 messenger.Message("Find: ")
26 // EndSearch stops the current search
29 messenger.hasPrompt = false
34 // HandleSearchEvent takes an event and a view and will do a real time match from the messenger's output
35 // to the current buffer. It searches down the buffer.
36 func HandleSearchEvent(event tcell.Event, v *View) {
37 switch e := event.(type) {
40 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape, tcell.KeyEnter:
47 messenger.HandleEvent(event)
49 if messenger.cursorx < 0 {
55 if messenger.response == "" {
56 v.cursor.ResetSelection()
57 // We don't end the search though
61 Search(messenger.response, v, true)
66 // Search searches in the view for the given regex. The down bool
67 // specifies whether it should search down from the searchStart position
69 func Search(searchStr string, v *View, down bool) {
76 str = v.buf.text[searchStart:]
79 str = v.buf.text[:searchStart]
81 r, err := regexp.Compile(searchStr)
85 matches := r.FindAllStringIndex(str, -1)
88 // Search the entire buffer now
89 matches = r.FindAllStringIndex(v.buf.text, -1)
92 v.cursor.ResetSelection()
97 match = matches[len(matches)-1]
104 match = matches[len(matches)-1]
109 v.cursor.curSelection[0] = charPos + match[0]
110 v.cursor.curSelection[1] = charPos + match[1]
111 v.cursor.x, v.cursor.y = FromCharPos(charPos+match[1]-1, v.buf)
115 lastSearch = searchStr