package main
import (
- "reflect"
- "runtime"
"strconv"
"strings"
"time"
widthPercent int
heightPercent int
+ // Specifies whether or not this view holds a help buffer
+ Help bool
+
// Actual with and height
width int
height int
// Holds the list of gutter messages
messages map[string][]GutterMessage
- // Is the help text opened in this view
- helpOpen bool
-
// This is the index of this view in the views array
Num int
// What tab is this view stored in
TabNum int
- // Is this view modifiable?
- Modifiable bool
-
// The buffer
Buf *Buffer
- // This is the buffer that was last opened
- // This is used to open help, and then go back to the previously opened buffer
- lastBuffer *Buffer
// The statusline
sline Statusline
// 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())
+ 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
}
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
v.Cursor.Right()
for _, pl := range loadedPlugins {
- err := Call(pl+".onRune", []string{string(e.Rune())})
+ _, err := Call(pl+".onRune", []string{string(e.Rune())})
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
TermMessage(err)
}
if e.Modifiers() == key.modifiers {
relocate = false
for _, action := range actions {
- relocate = action(v) || relocate
- for _, pl := range loadedPlugins {
- funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(action).Pointer()).Name(), ".")
- err := Call(pl+".on"+funcName[len(funcName)-1], nil)
- if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
- TermMessage(err)
- }
- }
+ 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 v.x != 0 {
// Draw the split divider
- v.drawCell(screenX, screenY, ' ', nil, defStyle.Reverse(true))
+ v.drawCell(screenX, screenY, '|', nil, defStyle.Reverse(true))
screenX++
}
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
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)
}
}
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))
+ }
}
}