]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/view.go
Fix some issues with unicode handling
[micro.git] / cmd / micro / view.go
index 710583063ea1de5f456a90909a684d0ecd32c3ac..4080fa1642dd2f109cf73b3fae3259b828942c1f 100644 (file)
@@ -1,14 +1,11 @@
 package main
 
 import (
-       "reflect"
-       "runtime"
        "strconv"
        "strings"
        "time"
 
        "github.com/mattn/go-runewidth"
-       "github.com/yuin/gopher-lua"
        "github.com/zyedidia/tcell"
 )
 
@@ -83,15 +80,13 @@ type View struct {
        // The matches from the last frame
        lastMatches SyntaxMatches
 
-       splitParent         *View
-       splitChild          *View
-       splitOrigDimensions [2]int
-       splitOrigPos        [2]int
+       splitNode *LeafNode
 }
 
 // NewView returns a new fullscreen view
 func NewView(buf *Buffer) *View {
-       return NewViewWidthHeight(buf, 100, 100)
+       screenW, screenH := screen.Size()
+       return NewViewWidthHeight(buf, screenW, screenH-1)
 }
 
 // NewViewWidthHeight returns a new view with the specified width and height percentages
@@ -101,9 +96,10 @@ func NewViewWidthHeight(buf *Buffer, w, h int) *View {
 
        v.x, v.y = 0, 0
 
-       v.widthPercent = w
-       v.heightPercent = h
-       v.Resize(screen.Size())
+       v.width = w
+       v.height = h
+
+       v.ToggleTabbar()
 
        v.OpenBuffer(buf)
 
@@ -113,41 +109,34 @@ func NewViewWidthHeight(buf *Buffer, w, h int) *View {
                view: v,
        }
 
+       if settings["statusline"].(bool) {
+               v.height--
+       }
+
        return v
 }
 
-// Resize recalculates the actual width and height of the view from the width and height
-// percentages
-// This is usually called when the window is resized, or when a split has been added and
-// the percentages have changed
-func (v *View) Resize(w, h int) {
-       // Always include 1 line for the command line at the bottom
-       h--
+func (v *View) ToggleStatusLine() {
+       if settings["statusline"].(bool) {
+               v.height--
+       } else {
+               v.height++
+       }
+}
+
+func (v *View) ToggleTabbar() {
        if len(tabs) > 1 {
                if v.y == 0 {
                        // Include one line for the tab bar at the top
-                       h--
+                       v.height--
                        v.y = 1
                }
        } else {
                if v.y == 1 {
                        v.y = 0
+                       v.height++
                }
        }
-       v.width = int(float32(w) * float32(v.widthPercent) / 100)
-       // We subtract 1 for the statusline
-       v.height = int(float32(h) * float32(v.heightPercent) / 100)
-       if w%2 == 0 && v.x > 1 && v.widthPercent < 100 {
-               v.width++
-       }
-
-       if h%2 == 1 && v.y > 1 && v.heightPercent < 100 {
-               v.height++
-       }
-       if settings["statusline"].(bool) {
-               // Make room for the status line if it is enabled
-               v.height--
-       }
 }
 
 // ScrollUp scrolls the view up n lines (if possible)
@@ -181,7 +170,7 @@ func (v *View) CanClose(msg string) bool {
                        if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
                                return true
                        } else if strings.ToLower(quit) == "save" || strings.ToLower(quit) == "s" {
-                               v.Save()
+                               v.Save(true)
                                return true
                        }
                }
@@ -231,57 +220,15 @@ func (v *View) ReOpen() {
 
 // HSplit opens a horizontal split with the given buffer
 func (v *View) HSplit(buf *Buffer) bool {
-       origDimensions := [2]int{v.widthPercent, v.heightPercent}
-       origPos := [2]int{v.x, v.y}
-
-       v.heightPercent /= 2
-       v.Resize(screen.Size())
-
-       newView := NewViewWidthHeight(buf, v.widthPercent, v.heightPercent)
-
-       v.splitOrigDimensions = origDimensions
-       v.splitOrigPos = origPos
-       newView.splitOrigDimensions = origDimensions
-       newView.splitOrigPos = origPos
-
-       newView.TabNum = v.TabNum
-       newView.y = v.y + v.height + 1
-       newView.x = v.x
-       tab := tabs[v.TabNum]
-       tab.curView++
-       newView.Num = len(tab.views)
-       newView.splitParent = v
-       v.splitChild = newView
-       tab.views = append(tab.views, newView)
-       newView.Resize(screen.Size())
+       v.splitNode.HSplit(buf)
+       tabs[v.TabNum].Resize()
        return false
 }
 
 // VSplit opens a vertical split with the given buffer
 func (v *View) VSplit(buf *Buffer) bool {
-       origDimensions := [2]int{v.widthPercent, v.heightPercent}
-       origPos := [2]int{v.x, v.y}
-
-       v.widthPercent /= 2
-       v.Resize(screen.Size())
-
-       newView := NewViewWidthHeight(buf, v.widthPercent, v.heightPercent)
-
-       v.splitOrigDimensions = origDimensions
-       v.splitOrigPos = origPos
-       newView.splitOrigDimensions = origDimensions
-       newView.splitOrigPos = origPos
-
-       newView.TabNum = v.TabNum
-       newView.y = v.y
-       newView.x = v.x + v.width
-       tab := tabs[v.TabNum]
-       tab.curView++
-       newView.Num = len(tab.views)
-       newView.splitParent = v
-       v.splitChild = newView
-       tab.views = append(tab.views, newView)
-       newView.Resize(screen.Size())
+       v.splitNode.VSplit(buf)
+       tabs[v.TabNum].Resize()
        return false
 }
 
@@ -355,7 +302,7 @@ func (v *View) HandleEvent(event tcell.Event) {
        switch e := event.(type) {
        case *tcell.EventResize:
                // Window resized
-               v.Resize(e.Size())
+               tabs[v.TabNum].Resize()
        case *tcell.EventKey:
                if e.Key() == tcell.KeyRune && (e.Modifiers() == 0 || e.Modifiers() == tcell.ModShift) {
                        // Insert a character
@@ -383,28 +330,7 @@ func (v *View) HandleEvent(event tcell.Event) {
                                        if e.Modifiers() == key.modifiers {
                                                relocate = false
                                                for _, action := range actions {
-                                                       executeAction := true
-                                                       funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(action).Pointer()).Name(), ".")
-                                                       for _, pl := range loadedPlugins {
-                                                               ret, err := Call(pl+".pre"+funcName[len(funcName)-1], nil)
-                                                               if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
-                                                                       TermMessage(err)
-                                                                       continue
-                                                               }
-                                                               if ret == lua.LFalse {
-                                                                       executeAction = false
-                                                               }
-                                                       }
-                                                       if executeAction {
-                                                               relocate = action(v) || relocate
-                                                               for _, pl := range loadedPlugins {
-                                                                       _, err := Call(pl+".on"+funcName[len(funcName)-1], nil)
-                                                                       if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
-                                                                               TermMessage(err)
-                                                                               continue
-                                                                       }
-                                                               }
-                                                       }
+                                                       relocate = action(v, true) || relocate
                                                }
                                        }
                                }
@@ -538,6 +464,20 @@ func (v *View) ClearAllGutterMessages() {
        }
 }
 
+// Opens the given help page in a new horizontal split
+func (v *View) openHelp(helpPage string) {
+       if v.Help {
+               helpBuffer := NewBuffer([]byte(helpPages[helpPage]), helpPage+".md")
+               helpBuffer.Name = "Help"
+               v.OpenBuffer(helpBuffer)
+       } else {
+               helpBuffer := NewBuffer([]byte(helpPages[helpPage]), helpPage+".md")
+               helpBuffer.Name = "Help"
+               v.HSplit(helpBuffer)
+               CurView().Help = true
+       }
+}
+
 func (v *View) drawCell(x, y int, ch rune, combc []rune, style tcell.Style) {
        if x >= v.x && x < v.x+v.width && y >= v.y && y < v.y+v.height {
                screen.SetContent(x, y, ch, combc, style)
@@ -592,7 +532,7 @@ func (v *View) DisplayView() {
 
                if v.x != 0 {
                        // Draw the split divider
-                       v.drawCell(screenX, screenY, ' ', nil, defStyle.Reverse(true))
+                       v.drawCell(screenX, screenY, '|', nil, defStyle.Reverse(true))
                        screenX++
                }
 
@@ -656,12 +596,13 @@ func (v *View) DisplayView() {
                if settings["ruler"] == true {
                        // Write the line number
                        lineNumStyle := defStyle
-                       curLineNumStyle := defStyle
                        if style, ok := colorscheme["line-number"]; ok {
                                lineNumStyle = style
                        }
                        if style, ok := colorscheme["current-line-number"]; ok {
-                               curLineNumStyle = style
+                               if curLineN == v.Cursor.Y {
+                                       lineNumStyle = style
+                               }
                        }
 
                        lineNum := strconv.Itoa(curLineN + 1)
@@ -673,11 +614,7 @@ func (v *View) DisplayView() {
                        }
                        // Write the actual line number
                        for _, ch := range lineNum {
-                               if curLineN == v.Cursor.Y {
-                                       v.drawCell(screenX, screenY, ch, nil, curLineNumStyle)
-                               } else {
-                                       v.drawCell(screenX, screenY, ch, nil, lineNumStyle)
-                               }
+                               v.drawCell(screenX, screenY, ch, nil, lineNumStyle)
                                screenX++
                        }
 
@@ -687,7 +624,8 @@ func (v *View) DisplayView() {
                }
 
                // Now we actually draw the line
-               for colN, ch := range line {
+               colN := 0
+               for _, ch := range line {
                        lineStyle := defStyle
 
                        if settings["syntax"].(bool) {
@@ -772,6 +710,7 @@ func (v *View) DisplayView() {
                        }
                        charNum = charNum.Move(1, v.Buf)
                        screenX++
+                       colN++
                }
                // Here we are at a newline
 
@@ -800,7 +739,7 @@ func (v *View) DisplayView() {
                                        lineStyle = lineStyle.Background(fg)
                                }
                        }
-                       if screenX-v.x-v.leftCol >= v.lineNumOffset {
+                       if screenX-v.x-v.leftCol+i >= v.lineNumOffset {
                                v.drawCell(screenX-v.leftCol+i, screenY, ' ', nil, lineStyle)
                        }
                }
@@ -823,7 +762,12 @@ func (v *View) Display() {
        if v.Num == tabs[curTab].curView {
                v.DisplayCursor()
        }
+       _, screenH := screen.Size()
        if settings["statusline"].(bool) {
                v.sline.Display()
+       } else if (v.y + v.height) != screenH-1 {
+               for x := 0; x < v.width; x++ {
+                       screen.SetContent(v.x+x, v.y+v.height, '-', nil, defStyle.Reverse(true))
+               }
        }
 }