6 "github.com/zyedidia/tcell"
10 // What was the last search
13 // Where should we start the search down from (or up from)
16 // Is there currently a search in progress
19 // Stores the history for searching
20 searchHistory []string
23 // BeginSearch starts a search
25 searchHistory = append(searchHistory, "")
26 messenger.historyNum = len(searchHistory) - 1
28 messenger.hasPrompt = true
29 messenger.Message("Find: ")
32 // EndSearch stops the current search
34 searchHistory[len(searchHistory)-1] = messenger.response
36 messenger.hasPrompt = false
40 messenger.Message("^P Previous ^N Next")
44 // HandleSearchEvent takes an event and a view and will do a real time match from the messenger's output
45 // to the current buffer. It searches down the buffer.
46 func HandleSearchEvent(event tcell.Event, v *View) {
47 switch e := event.(type) {
50 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape, tcell.KeyEnter:
57 messenger.HandleEvent(event, searchHistory)
59 if messenger.cursorx < 0 {
65 if messenger.response == "" {
66 v.Cursor.ResetSelection()
67 // We don't end the search though
71 Search(messenger.response, v, true)
76 // Search searches in the view for the given regex. The down bool
77 // specifies whether it should search down from the searchStart position
79 func Search(searchStr string, v *View, down bool) {
85 text := v.Buf.String()
87 str = string([]rune(text)[searchStart:])
90 str = string([]rune(text)[:searchStart])
92 r, err := regexp.Compile(searchStr)
93 if v.Buf.Settings["ignorecase"].(bool) {
94 r, err = regexp.Compile("(?i)" + searchStr)
99 matches := r.FindAllStringIndex(str, -1)
102 // Search the entire buffer now
103 matches = r.FindAllStringIndex(text, -1)
106 v.Cursor.ResetSelection()
111 match = matches[len(matches)-1]
119 match = matches[len(matches)-1]
124 if match[0] == match[1] {
128 v.Cursor.SetSelectionStart(FromCharPos(charPos+runePos(match[0], str), v.Buf))
129 v.Cursor.SetSelectionEnd(FromCharPos(charPos+runePos(match[1], str), v.Buf))
130 v.Cursor.Loc = v.Cursor.CurSelection[1]
134 lastSearch = searchStr