Add find previous and documentation

This commit is contained in:
Zachary Yedidia 2016-04-17 14:30:49 -04:00
parent 8fdf99854e
commit 1648afd226
5 changed files with 59 additions and 18 deletions

View File

@ -80,6 +80,8 @@ You can move the cursor around with the arrow keys and mouse.
* Ctrl-z: Undo
* Ctrl-y: Redo
* Ctrl-f: Find
* Ctrl-n: Find next
* Ctrl-p: Find previous
* Ctrl-a: Select all
* Ctrl-c: Copy
* Ctrl-x: Cut

View File

@ -17,6 +17,8 @@ Ctrl-z: Undo
Ctrl-y: Redo
Ctrl-f: Find
Ctrl-n: Find next
Ctrl-p: Find previous
Ctrl-a: Select all

View File

@ -23,9 +23,6 @@ var (
// Object to send messages and prompts to the user
messenger *Messenger
// Is there currently a search in progress
searching bool
// The default style
defStyle tcell.Style
)

View File

@ -5,8 +5,16 @@ import (
"regexp"
)
var lastSearch string
var searchStart int
var (
// What was the last search
lastSearch string
// Where should we start the search down from (or up from)
searchStart int
// Is there currently a search in progress
searching bool
)
// BeginSearch starts a search
func BeginSearch() {
@ -50,30 +58,54 @@ func HandleSearchEvent(event tcell.Event, v *View) {
return
}
Search(messenger.response, v)
Search(messenger.response, v, true)
return
}
func Search(searchStr string, v *View) {
str := v.buf.text[searchStart:]
// Search searches in the view for the given regex. The down bool
// specifies whether it should search down from the searchStart position
// or up from there
func Search(searchStr string, v *View, down bool) {
var str string
var charPos int
if down {
str = v.buf.text[searchStart:]
charPos = searchStart
} else {
str = v.buf.text[:searchStart]
}
r, err := regexp.Compile(searchStr)
if err != nil {
return
}
match := r.FindStringIndex(str)
if match == nil {
matches := r.FindAllStringIndex(str, -1)
var match []int
if matches == nil {
// Search the entire buffer now
match = r.FindStringIndex(v.buf.text)
searchStart = 0
if match == nil {
matches = r.FindAllStringIndex(v.buf.text, -1)
charPos = 0
if matches == nil {
v.cursor.ResetSelection()
return
}
if !down {
match = matches[len(matches)-1]
} else {
match = matches[0]
}
}
v.cursor.curSelection[0] = searchStart + match[0]
v.cursor.curSelection[1] = searchStart + match[1]
v.cursor.x, v.cursor.y = FromCharPos(searchStart+match[1]-1, v.buf)
if !down {
match = matches[len(matches)-1]
} else {
match = matches[0]
}
v.cursor.curSelection[0] = charPos + match[0]
v.cursor.curSelection[1] = charPos + match[1]
v.cursor.x, v.cursor.y = FromCharPos(charPos+match[1]-1, v.buf)
if v.Relocate() {
v.matches = Match(v)
}

View File

@ -439,8 +439,16 @@ func (v *View) HandleEvent(event tcell.Event) {
} else {
searchStart = ToCharPos(v.cursor.x, v.cursor.y, v.buf)
}
messenger.Message("Search for " + lastSearch)
Search(lastSearch, v)
messenger.Message("Find: " + lastSearch)
Search(lastSearch, v, true)
case tcell.KeyCtrlP:
if v.cursor.HasSelection() {
searchStart = v.cursor.curSelection[0]
} else {
searchStart = ToCharPos(v.cursor.x, v.cursor.y, v.buf)
}
messenger.Message("Find: " + lastSearch)
Search(lastSearch, v, false)
case tcell.KeyCtrlZ:
v.eh.Undo()
// Rehighlight the entire buffer