diff --git a/src/cursor.go b/src/cursor.go index 7b96f9f7..b7c655cb 100644 --- a/src/cursor.go +++ b/src/cursor.go @@ -6,12 +6,19 @@ import ( // FromCharPos converts from a character position to an x, y position func FromCharPos(loc int, buf *Buffer) (int, int) { - charNum := 0 - x, y := 0, 0 + return FromCharPosStart(0, 0, 0, loc, buf) +} - for charNum+Count(buf.lines[y])+1 <= loc { - charNum += Count(buf.lines[y]) + 1 +// FromCharPosStart converts from a character position to an x, y position, starting at the specified character location +func FromCharPosStart(startLoc, startX, startY, loc int, buf *Buffer) (int, int) { + charNum := startLoc + x, y := startX, startY + + lineLen := Count(buf.lines[y]) + 1 + for charNum+lineLen <= loc { + charNum += lineLen y++ + lineLen = Count(buf.lines[y]) + 1 } x = loc - charNum diff --git a/src/highlighter.go b/src/highlighter.go index 59e6f2de..731d79dc 100644 --- a/src/highlighter.go +++ b/src/highlighter.go @@ -296,15 +296,20 @@ func Match(v *View) SyntaxMatches { str := strings.Join(buf.lines[totalStart:totalEnd], "\n") startNum := ToCharPos(0, totalStart, v.buf) + toplineNum := ToCharPos(0, v.topline, v.buf) + bottomlineNum := ToCharPos(0, v.topline+v.height, v.buf) + for _, rule := range rules { if rule.startend { - if rule.regex.MatchString(str) { - indicies := rule.regex.FindAllStringIndex(str, -1) + if indicies := rule.regex.FindAllStringIndex(str, -1); indicies != nil { for _, value := range indicies { value[0] += startNum value[1] += startNum for i := value[0]; i < value[1]; i++ { - colNum, lineNum := FromCharPos(i, buf) + if i < toplineNum || i > bottomlineNum { + continue + } + colNum, lineNum := FromCharPosStart(toplineNum, 0, v.topline, i, buf) if lineNum == -1 || colNum == -1 { continue } @@ -317,8 +322,7 @@ func Match(v *View) SyntaxMatches { } } else { for lineN, line := range lines { - if rule.regex.MatchString(line) { - indicies := rule.regex.FindAllStringIndex(line, -1) + if indicies := rule.regex.FindAllStringIndex(line, -1); indicies != nil { for _, value := range indicies { for i := value[0]; i < value[1]; i++ { // matches[lineN+updateStart][i] = rule.style diff --git a/src/micro.go b/src/micro.go index 47828d8c..967e33da 100644 --- a/src/micro.go +++ b/src/micro.go @@ -22,6 +22,8 @@ var screen tcell.Screen // Object to send messages and prompts to the user var messenger *Messenger +var redrawStatus int + // LoadInput loads the file input for the editor func LoadInput() (string, []byte, error) { // There are a number of ways micro should start given its input diff --git a/src/view.go b/src/view.go index 94b40559..15f33036 100644 --- a/src/view.go +++ b/src/view.go @@ -257,10 +257,7 @@ func (v *View) Paste() { } clip, _ := clipboard.ReadAll() v.eh.Insert(v.cursor.Loc(), clip) - // This is a bit weird... Not sure if there's a better way - for i := 0; i < Count(clip); i++ { - v.cursor.Right() - } + v.cursor.SetLoc(v.cursor.Loc() + Count(clip)) } else { messenger.Error("Clipboard is not supported on your system") } @@ -343,6 +340,7 @@ func (v *View) HandleEvent(event tcell.Event) { relocate := true // By default we don't update and syntax highlighting v.UpdateLines(-2, 0) + rematch := true switch e := event.(type) { case *tcell.EventResize: // Window resized @@ -352,21 +350,26 @@ func (v *View) HandleEvent(event tcell.Event) { case tcell.KeyUp: // Cursor up v.cursor.Up() + rematch = false case tcell.KeyDown: // Cursor down v.cursor.Down() + rematch = false case tcell.KeyLeft: // Cursor left v.cursor.Left() + rematch = false case tcell.KeyRight: // Cursor right v.cursor.Right() + rematch = false case tcell.KeyEnter: // Insert a newline v.eh.Insert(v.cursor.Loc(), "\n") v.cursor.Right() // Rehighlight the entire buffer v.UpdateLines(v.topline, v.topline+v.height) + v.cursor.lastVisualX = v.cursor.GetVisualX() // v.UpdateLines(v.cursor.y-1, v.cursor.y) case tcell.KeySpace: // Insert a space @@ -396,6 +399,7 @@ func (v *View) HandleEvent(event tcell.Event) { v.UpdateLines(v.topline, v.topline+v.height) // v.UpdateLines(v.cursor.y, v.cursor.y+1) } + v.cursor.lastVisualX = v.cursor.GetVisualX() case tcell.KeyTab: // Insert a tab v.eh.Insert(v.cursor.Loc(), "\t") @@ -415,6 +419,7 @@ func (v *View) HandleEvent(event tcell.Event) { v.Copy() // Rehighlight the entire buffer v.UpdateLines(v.topline, v.topline+v.height) + rematch = false case tcell.KeyCtrlX: v.Cut() // Rehighlight the entire buffer @@ -509,6 +514,7 @@ func (v *View) HandleEvent(event tcell.Event) { } } v.mouseReleased = false + rematch = false case tcell.ButtonNone: // Mouse event with no click @@ -530,6 +536,7 @@ func (v *View) HandleEvent(event tcell.Event) { // We don't want to relocate because otherwise the view will be relocated // every time the user moves the cursor relocate = false + rematch = false case tcell.WheelUp: // Scroll up two lines v.ScrollUp(2) @@ -551,7 +558,9 @@ func (v *View) HandleEvent(event tcell.Event) { v.Relocate() } - v.matches = Match(v) + if rematch { + v.matches = Match(v) + } } // DisplayView renders the view to the screen