package main
import (
- "reflect"
- "runtime"
"strconv"
"strings"
"time"
"github.com/mattn/go-runewidth"
- "github.com/yuin/gopher-lua"
"github.com/zyedidia/tcell"
)
// 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
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)
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)
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
}
}
// 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())
- newView.matches = Match(newView)
+ 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())
- newView.matches = Match(newView)
+ v.splitNode.VSplit(buf)
+ tabs[v.TabNum].Resize()
return false
}
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
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
}
}
}
}
}
+// 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)
if style, ok := colorscheme["line-number"]; ok {
lineNumStyle = style
}
+ if style, ok := colorscheme["current-line-number"]; ok {
+ if curLineN == v.Cursor.Y {
+ lineNumStyle = style
+ }
+ }
lineNum := strconv.Itoa(curLineN + 1)
}
// Now we actually draw the line
- for colN, ch := range line {
+ colN := 0
+ for _, ch := range line {
lineStyle := defStyle
if settings["syntax"].(bool) {
}
charNum = charNum.Move(1, v.Buf)
screenX++
+ colN++
}
// Here we are at a newline