// It stores information about the cursor, and the viewport
// that the user sees the buffer from.
type View struct {
- cursor Cursor
+ Cursor Cursor
// The topmost line, used for vertical scrolling
- topline int
+ Topline int
// The leftmost column, used for horizontal scrolling
leftCol int
// The eventhandler for undo/redo
eh *EventHandler
+ // Holds the list of gutter messages
messages []GutterMessage
// The buffer
- buf *Buffer
+ Buf *Buffer
// The statusline
sline Statusline
// ScrollUp scrolls the view up n lines (if possible)
func (v *View) ScrollUp(n int) {
// Try to scroll by n but if it would overflow, scroll by 1
- if v.topline-n >= 0 {
- v.topline -= n
- } else if v.topline > 0 {
- v.topline--
+ if v.Topline-n >= 0 {
+ v.Topline -= n
+ } else if v.Topline > 0 {
+ v.Topline--
}
}
// ScrollDown scrolls the view down n lines (if possible)
func (v *View) ScrollDown(n int) {
// Try to scroll by n but if it would overflow, scroll by 1
- if v.topline+n <= v.buf.numLines-v.height {
- v.topline += n
- } else if v.topline < v.buf.numLines-v.height {
- v.topline++
+ if v.Topline+n <= v.Buf.NumLines-v.height {
+ v.Topline += n
+ } else if v.Topline < v.Buf.NumLines-v.height {
+ v.Topline++
}
}
// causing them to lose the unsaved changes
// The message is what to print after saying "You have unsaved changes. "
func (v *View) CanClose(msg string) bool {
- if v.buf.IsDirty() {
+ if v.Buf.IsDirty() {
quit, canceled := messenger.Prompt("You have unsaved changes. " + msg)
if !canceled {
if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
// OpenBuffer opens a new buffer in this view.
// This resets the topline, event handler and cursor.
func (v *View) OpenBuffer(buf *Buffer) {
- v.buf = buf
- v.topline = 0
+ v.Buf = buf
+ v.Topline = 0
v.leftCol = 0
// Put the cursor at the first spot
- v.cursor = Cursor{
+ v.Cursor = Cursor{
x: 0,
y: 0,
v: v,
}
- v.cursor.ResetSelection()
+ v.Cursor.ResetSelection()
v.eh = NewEventHandler(v)
v.matches = Match(v)
// ReOpen reloads the current buffer
func (v *View) ReOpen() {
if v.CanClose("Continue? (yes, no, save) ") {
- file, err := ioutil.ReadFile(v.buf.path)
- filename := v.buf.name
+ file, err := ioutil.ReadFile(v.Buf.Path)
+ filename := v.Buf.Name
if err != nil {
messenger.Error(err.Error())
return
}
buf := NewBuffer(string(file), filename)
- v.buf = buf
+ v.Buf = buf
v.matches = Match(v)
- v.cursor.Relocate()
+ v.Cursor.Relocate()
v.Relocate()
}
}
// This is useful if the user has scrolled far away, and then starts typing
func (v *View) Relocate() bool {
ret := false
- cy := v.cursor.y
- if cy < v.topline {
- v.topline = cy
+ cy := v.Cursor.y
+ if cy < v.Topline {
+ v.Topline = cy
ret = true
}
- if cy > v.topline+v.height-1 {
- v.topline = cy - v.height + 1
+ if cy > v.Topline+v.height-1 {
+ v.Topline = cy - v.height + 1
ret = true
}
- cx := v.cursor.GetVisualX()
+ cx := v.Cursor.GetVisualX()
if cx < v.leftCol {
v.leftCol = cx
ret = true
// MoveToMouseClick moves the cursor to location x, y assuming x, y were given
// by a mouse click
func (v *View) MoveToMouseClick(x, y int) {
- if y-v.topline > v.height-1 {
+ if y-v.Topline > v.height-1 {
v.ScrollDown(1)
- y = v.height + v.topline - 1
+ y = v.height + v.Topline - 1
}
- if y >= v.buf.numLines {
- y = v.buf.numLines - 1
+ if y >= v.Buf.NumLines {
+ y = v.Buf.NumLines - 1
}
if y < 0 {
y = 0
x = 0
}
- x = v.cursor.GetCharPosInLine(y, x)
- if x > Count(v.buf.lines[y]) {
- x = Count(v.buf.lines[y])
+ x = v.Cursor.GetCharPosInLine(y, x)
+ if x > Count(v.Buf.Lines[y]) {
+ x = Count(v.Buf.Lines[y])
}
- v.cursor.x = x
- v.cursor.y = y
- v.cursor.lastVisualX = v.cursor.GetVisualX()
+ v.Cursor.x = x
+ v.Cursor.y = y
+ v.Cursor.lastVisualX = v.Cursor.GetVisualX()
}
// HandleEvent handles an event passed by the main loop
case *tcell.EventKey:
if e.Key() == tcell.KeyRune {
// Insert a character
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
+ if v.Cursor.HasSelection() {
+ v.Cursor.DeleteSelection()
+ v.Cursor.ResetSelection()
}
- v.eh.Insert(v.cursor.Loc(), string(e.Rune()))
- v.cursor.Right()
+ v.eh.Insert(v.Cursor.Loc(), string(e.Rune()))
+ v.Cursor.Right()
} else {
for key, action := range bindings {
if e.Key() == key {
}
}
case *tcell.EventPaste:
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
+ if v.Cursor.HasSelection() {
+ v.Cursor.DeleteSelection()
+ v.Cursor.ResetSelection()
}
clip := e.Text()
- v.eh.Insert(v.cursor.Loc(), clip)
- v.cursor.SetLoc(v.cursor.Loc() + Count(clip))
+ v.eh.Insert(v.Cursor.Loc(), clip)
+ v.Cursor.SetLoc(v.Cursor.Loc() + Count(clip))
v.freshClip = false
case *tcell.EventMouse:
x, y := e.Position()
x -= v.lineNumOffset - v.leftCol
- y += v.topline
+ y += v.Topline
button := e.Buttons()
v.tripleClick = true
v.doubleClick = false
- v.cursor.SelectLine()
+ v.Cursor.SelectLine()
} else {
// Double click
v.lastClickTime = time.Now()
v.doubleClick = true
v.tripleClick = false
- v.cursor.SelectWord()
+ v.Cursor.SelectWord()
}
} else {
v.doubleClick = false
v.tripleClick = false
v.lastClickTime = time.Now()
- loc := v.cursor.Loc()
- v.cursor.origSelection[0] = loc
- v.cursor.curSelection[0] = loc
- v.cursor.curSelection[1] = loc
+ loc := v.Cursor.Loc()
+ v.Cursor.origSelection[0] = loc
+ v.Cursor.curSelection[0] = loc
+ v.Cursor.curSelection[1] = loc
}
v.mouseReleased = false
} else if !v.mouseReleased {
v.MoveToMouseClick(x, y)
if v.tripleClick {
- v.cursor.AddLineToSelection()
+ v.Cursor.AddLineToSelection()
} else if v.doubleClick {
- v.cursor.AddWordToSelection()
+ v.Cursor.AddWordToSelection()
} else {
- v.cursor.curSelection[1] = v.cursor.Loc()
+ v.Cursor.curSelection[1] = v.Cursor.Loc()
}
}
case tcell.ButtonNone:
if !v.doubleClick && !v.tripleClick {
v.MoveToMouseClick(x, y)
- v.cursor.curSelection[1] = v.cursor.Loc()
+ v.Cursor.curSelection[1] = v.Cursor.Loc()
}
v.mouseReleased = true
}
// DisplayView renders the view to the screen
func (v *View) DisplayView() {
// The character number of the character in the top left of the screen
- charNum := ToCharPos(0, v.topline, v.buf)
+ charNum := ToCharPos(0, v.Topline, v.Buf)
// Convert the length of buffer to a string, and get the length of the string
// We are going to have to offset by that amount
- maxLineLength := len(strconv.Itoa(v.buf.numLines))
+ maxLineLength := len(strconv.Itoa(v.Buf.NumLines))
// + 1 for the little space after the line number
if settings["ruler"] == true {
v.lineNumOffset = maxLineLength + 1
var x int
// If the buffer is smaller than the view height
// and we went too far, break
- if lineN+v.topline >= v.buf.numLines {
+ if lineN+v.Topline >= v.Buf.NumLines {
break
}
- line := v.buf.lines[lineN+v.topline]
+ line := v.Buf.Lines[lineN+v.Topline]
if len(v.messages) > 0 {
msgOnLine := false
for _, msg := range v.messages {
- if msg.lineNum == lineN+v.topline {
+ if msg.lineNum == lineN+v.Topline {
msgOnLine = true
gutterStyle := tcell.StyleDefault
switch msg.kind {
x++
screen.SetContent(x, lineN, '>', nil, gutterStyle)
x++
- if v.cursor.y == lineN {
+ if v.Cursor.y == lineN {
messenger.Message(msg.msg)
messenger.gutterMessage = true
}
x++
screen.SetContent(x, lineN, ' ', nil, tcell.StyleDefault)
x++
- if v.cursor.y == lineN && messenger.gutterMessage {
+ if v.Cursor.y == lineN && messenger.gutterMessage {
messenger.Reset()
messenger.gutterMessage = false
}
// Write the spaces before the line number if necessary
var lineNum string
if settings["ruler"] == true {
- lineNum = strconv.Itoa(lineN + v.topline + 1)
+ lineNum = strconv.Itoa(lineN + v.Topline + 1)
for i := 0; i < maxLineLength-len(lineNum); i++ {
screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
x++
highlightStyle = v.matches[lineN][colN]
}
- if v.cursor.HasSelection() &&
- (charNum >= v.cursor.curSelection[0] && charNum < v.cursor.curSelection[1] ||
- charNum < v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
+ if v.Cursor.HasSelection() &&
+ (charNum >= v.Cursor.curSelection[0] && charNum < v.Cursor.curSelection[1] ||
+ charNum < v.Cursor.curSelection[0] && charNum >= v.Cursor.curSelection[1]) {
lineStyle = tcell.StyleDefault.Reverse(true)
// The newline may be selected, in which case we should draw the selection style
// with a space to represent it
- if v.cursor.HasSelection() &&
- (charNum >= v.cursor.curSelection[0] && charNum < v.cursor.curSelection[1] ||
- charNum < v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
+ if v.Cursor.HasSelection() &&
+ (charNum >= v.Cursor.curSelection[0] && charNum < v.Cursor.curSelection[1] ||
+ charNum < v.Cursor.curSelection[0] && charNum >= v.Cursor.curSelection[1]) {
selectStyle := defStyle.Reverse(true)
// Display renders the view, the cursor, and statusline
func (v *View) Display() {
v.DisplayView()
- v.cursor.Display()
+ v.Cursor.Display()
v.sline.Display()
}