From 1648afd226f979f67acbb4acd4c62a3beed2e707 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 17 Apr 2016 14:30:49 -0400 Subject: [PATCH] Add find previous and documentation --- README.md | 2 ++ src/help.go | 2 ++ src/micro.go | 3 --- src/search.go | 58 +++++++++++++++++++++++++++++++++++++++------------ src/view.go | 12 +++++++++-- 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 96850987..7fd60388 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/help.go b/src/help.go index b8efab9e..5d2d3ed0 100644 --- a/src/help.go +++ b/src/help.go @@ -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 diff --git a/src/micro.go b/src/micro.go index 33cf6777..8733aa40 100644 --- a/src/micro.go +++ b/src/micro.go @@ -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 ) diff --git a/src/search.go b/src/search.go index 4a029aa0..2357c59b 100644 --- a/src/search.go +++ b/src/search.go @@ -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) } diff --git a/src/view.go b/src/view.go index 0b9dd5d1..347d50e3 100644 --- a/src/view.go +++ b/src/view.go @@ -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