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
20 // BeginSearch starts a search
23 messenger.hasPrompt = true
24 messenger.Message("Find: ")
27 // EndSearch stops the current search
30 messenger.hasPrompt = false
34 messenger.Message("^P Previous ^N Next")
38 // HandleSearchEvent takes an event and a view and will do a real time match from the messenger's output
39 // to the current buffer. It searches down the buffer.
40 func HandleSearchEvent(event tcell.Event, v *View) {
41 switch e := event.(type) {
44 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape, tcell.KeyEnter:
51 messenger.HandleEvent(event)
53 if messenger.cursorx < 0 {
59 if messenger.response == "" {
60 v.Cursor.ResetSelection()
61 // We don't end the search though
65 Search(messenger.response, v, true)
70 // Search searches in the view for the given regex. The down bool
71 // specifies whether it should search down from the searchStart position
73 func Search(searchStr string, v *View, down bool) {
79 text := v.Buf.String()
81 str = text[searchStart:]
84 str = text[:searchStart]
86 r, err := regexp.Compile(searchStr)
87 if settings["ignorecase"].(bool) {
88 r, err = regexp.Compile("(?i)" + searchStr)
93 matches := r.FindAllStringIndex(str, -1)
96 // Search the entire buffer now
97 matches = r.FindAllStringIndex(text, -1)
100 v.Cursor.ResetSelection()
105 match = matches[len(matches)-1]
112 match = matches[len(matches)-1]
117 if match[0] == match[1] {
121 v.Cursor.curSelection[0] = charPos + match[0]
122 v.Cursor.curSelection[1] = charPos + match[1]
123 v.Cursor.x, v.Cursor.y = FromCharPos(charPos+match[1]-1, v.Buf)
124 scrollmargin := int(settings["scrollmargin"].(float64))
125 if v.Relocate(scrollmargin) {
128 lastSearch = searchStr