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
24 func BeginSearch(searchStr string) {
25 searchHistory = append(searchHistory, "")
26 messenger.historyNum = len(searchHistory) - 1
28 messenger.response = searchStr
29 messenger.cursorx = Count(searchStr)
30 messenger.Message("Find: ")
31 messenger.hasPrompt = true
34 // EndSearch stops the current search
36 searchHistory[len(searchHistory)-1] = messenger.response
38 messenger.hasPrompt = false
42 messenger.Message("^P Previous ^N Next")
46 // exit the search mode, reset active search phrase, and clear status bar
47 func ExitSearch(v *View) {
50 messenger.hasPrompt = false
53 v.Cursor.ResetSelection()
56 // HandleSearchEvent takes an event and a view and will do a real time match from the messenger's output
57 // to the current buffer. It searches down the buffer.
58 func HandleSearchEvent(event tcell.Event, v *View) {
59 switch e := event.(type) {
63 // Exit the search mode
66 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEnter:
73 messenger.HandleEvent(event, searchHistory)
75 if messenger.cursorx < 0 {
81 if messenger.response == "" {
82 v.Cursor.ResetSelection()
83 // We don't end the search though
87 Search(messenger.response, v, true)
92 // Search searches in the view for the given regex. The down bool
93 // specifies whether it should search down from the searchStart position
95 func Search(searchStr string, v *View, down bool) {
101 text := v.Buf.String()
103 str = string([]rune(text)[searchStart:])
104 charPos = searchStart
106 str = string([]rune(text)[:searchStart])
108 r, err := regexp.Compile(searchStr)
109 if v.Buf.Settings["ignorecase"].(bool) {
110 r, err = regexp.Compile("(?i)" + searchStr)
115 matches := r.FindAllStringIndex(str, -1)
118 // Search the entire buffer now
119 matches = r.FindAllStringIndex(text, -1)
122 v.Cursor.ResetSelection()
127 match = matches[len(matches)-1]
135 match = matches[len(matches)-1]
140 if match[0] == match[1] {
144 v.Cursor.SetSelectionStart(FromCharPos(charPos+runePos(match[0], str), v.Buf))
145 v.Cursor.SetSelectionEnd(FromCharPos(charPos+runePos(match[1], str), v.Buf))
146 v.Cursor.Loc = v.Cursor.CurSelection[1]
147 lastSearch = searchStr