mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-17 06:17:12 +09:00
Implement better search functionality (not finished)
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var lastSearch string
|
||||
|
||||
// BeginSearch starts a search
|
||||
func BeginSearch() {
|
||||
searching = true
|
||||
@@ -48,14 +50,33 @@ func HandleSearchEvent(event tcell.Event, v *View) {
|
||||
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)
|
||||
Search(messenger.response, v)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Search searches for a given regular expression inside a view
|
||||
// It also moves the cursor and highlights the search result
|
||||
func Search(searchStr string, v *View) {
|
||||
lines := v.buf.lines[v.cursor.y:]
|
||||
var charPos int
|
||||
if v.cursor.HasSelection() {
|
||||
x, y := FromCharPos(v.cursor.curSelection[1]-1, v.buf)
|
||||
lines = v.buf.lines[y:]
|
||||
lines[0] = lines[0][x:]
|
||||
charPos = ToCharPos(x, y, v.buf)
|
||||
} else {
|
||||
lines[0] = lines[0][v.cursor.x:]
|
||||
charPos = ToCharPos(v.cursor.x, v.cursor.y, v.buf)
|
||||
}
|
||||
str := strings.Join(lines, "\n")
|
||||
r, err := regexp.Compile(searchStr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
match := r.FindStringIndex(str)
|
||||
if match == nil {
|
||||
// FIXME
|
||||
str = strings.Join(v.buf.lines[:v.cursor.y], "\n")
|
||||
match = r.FindStringIndex(str)
|
||||
charPos = 0
|
||||
@@ -70,5 +91,5 @@ func HandleSearchEvent(event tcell.Event, v *View) {
|
||||
if v.Relocate() {
|
||||
v.matches = Match(v)
|
||||
}
|
||||
return
|
||||
lastSearch = searchStr
|
||||
}
|
||||
|
||||
@@ -428,6 +428,8 @@ func (v *View) HandleEvent(event tcell.Event) {
|
||||
v.Save()
|
||||
case tcell.KeyCtrlF:
|
||||
BeginSearch()
|
||||
case tcell.KeyCtrlN:
|
||||
Search(lastSearch, v)
|
||||
case tcell.KeyCtrlZ:
|
||||
v.eh.Undo()
|
||||
// Rehighlight the entire buffer
|
||||
|
||||
Reference in New Issue
Block a user