]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/view.go
Fix: mouse clicking with softwrap
[micro.git] / cmd / micro / view.go
index 564c1dc26ad8f91b2dd4ca536cae3389c714b544..269acd43c0862991c1abe3438267e942c1e169bf 100644 (file)
@@ -1,7 +1,7 @@
 package main
 
 import (
-       "io/ioutil"
+       "os"
        "strconv"
        "strings"
        "time"
@@ -242,13 +242,14 @@ func (v *View) OpenBuffer(buf *Buffer) {
 func (v *View) Open(filename string) {
        home, _ := homedir.Dir()
        filename = strings.Replace(filename, "~", home, 1)
-       file, err := ioutil.ReadFile(filename)
+       file, err := os.Open(filename)
+       defer file.Close()
 
        var buf *Buffer
        if err != nil {
                messenger.Message(err.Error())
                // File does not exist -- create an empty buffer with that name
-               buf = NewBuffer([]byte{}, filename)
+               buf = NewBuffer(strings.NewReader(""), filename)
        } else {
                buf = NewBuffer(file, filename)
        }
@@ -417,9 +418,6 @@ func (v *View) MoveToMouseClick(x, y int) {
                v.ScrollDown(1)
                y = v.Height + v.Topline - 1
        }
-       if y >= v.Buf.NumLines {
-               y = v.Buf.NumLines - 1
-       }
        if y < 0 {
                y = 0
        }
@@ -630,7 +628,7 @@ func (v *View) openHelp(helpPage string) {
        if data, err := FindRuntimeFile(RTHelp, helpPage).Data(); err != nil {
                TermMessage("Unable to load help text", helpPage, "\n", err)
        } else {
-               helpBuffer := NewBuffer(data, helpPage+".md")
+               helpBuffer := NewBuffer(strings.NewReader(string(data)), helpPage+".md")
                helpBuffer.name = "Help"
 
                if v.Type == vtHelp {
@@ -778,7 +776,7 @@ func (v *View) DisplayView() {
                                lineNumStyle = style
                        }
                        if style, ok := colorscheme["current-line-number"]; ok {
-                               if curLineN == v.Cursor.Y && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() {
+                               if curLineN == v.Cursor.Y && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() {
                                        lineNumStyle = style
                                }
                        }
@@ -816,7 +814,7 @@ func (v *View) DisplayView() {
                                }
                        }
 
-                       if tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN && colN == v.Cursor.X {
+                       if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN && colN == v.Cursor.X {
                                v.DisplayCursor(screenX-v.leftCol, screenY)
                        }
 
@@ -842,7 +840,7 @@ func (v *View) DisplayView() {
 
                        // We need to display the background of the linestyle with the correct color if cursorline is enabled
                        // and this is the current view and there is no selection on this line and the cursor is on this line
-                       if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
+                       if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
                                if style, ok := colorscheme["cursor-line"]; ok {
                                        fg, _, _ := style.Decompose()
                                        lineStyle = lineStyle.Background(fg)
@@ -868,7 +866,7 @@ func (v *View) DisplayView() {
                                                lineIndentStyle = style
                                        }
                                }
-                               if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
+                               if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
                                        if style, ok := colorscheme["cursor-line"]; ok {
                                                fg, _, _ := style.Decompose()
                                                lineIndentStyle = lineIndentStyle.Background(fg)
@@ -888,6 +886,7 @@ func (v *View) DisplayView() {
                                                v.drawCell(screenX-v.leftCol, screenY, ' ', nil, lineStyle)
                                        }
                                }
+                               strWidth += remainder
                        } else if runewidth.RuneWidth(ch) > 1 {
                                if screenX-v.x-v.leftCol >= v.lineNumOffset {
                                        v.drawCell(screenX, screenY, ch, nil, lineStyle)
@@ -898,19 +897,20 @@ func (v *View) DisplayView() {
                                                v.drawCell(screenX-v.leftCol, screenY, '<', nil, lineStyle)
                                        }
                                }
+                               strWidth += StringWidth(string(ch), tabSize)
                        } else {
                                if screenX-v.x-v.leftCol >= v.lineNumOffset {
                                        v.drawCell(screenX-v.leftCol, screenY, ch, nil, lineStyle)
                                }
+                               strWidth += StringWidth(string(ch), tabSize)
                        }
                        charNum = charNum.Move(1, v.Buf)
                        screenX++
                        colN++
-                       strWidth += StringWidth(string(ch), tabSize)
                }
                // Here we are at a newline
 
-               if tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN && colN == v.Cursor.X {
+               if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN && colN == v.Cursor.X {
                        v.DisplayCursor(screenX-v.leftCol, screenY)
                }
 
@@ -933,7 +933,7 @@ func (v *View) DisplayView() {
 
                for i := 0; i < v.Width; i++ {
                        lineStyle := defStyle
-                       if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
+                       if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
                                if style, ok := colorscheme["cursor-line"]; ok {
                                        fg, _, _ := style.Decompose()
                                        lineStyle = lineStyle.Background(fg)