package main
import (
- "github.com/atotto/clipboard"
- "github.com/gdamore/tcell"
"io/ioutil"
"strconv"
"strings"
"time"
+
+ "github.com/atotto/clipboard"
+ "github.com/gdamore/tcell"
+ "github.com/mitchellh/go-homedir"
)
// The View struct stores information about a view into a buffer.
matches SyntaxMatches
// The matches from the last frame
lastMatches SyntaxMatches
-
- // This is the range of lines that should have their syntax highlighting updated
- updateLines [2]int
}
// NewView returns a new fullscreen view
func NewViewWidthHeight(buf *Buffer, w, h int) *View {
v := new(View)
- v.buf = buf
-
v.widthPercent = w
v.heightPercent = h
v.Resize(screen.Size())
- v.topline = 0
- // Put the cursor at the first spot
- v.cursor = Cursor{
- x: 0,
- y: 0,
- v: v,
- }
- v.cursor.ResetSelection()
+ v.OpenBuffer(buf)
v.eh = NewEventHandler(v)
view: v,
}
- // Update the syntax highlighting for the entire buffer at the start
- v.UpdateLines(v.topline, v.topline+v.height)
- v.matches = Match(v)
-
- // Set mouseReleased to true because we assume the mouse is not being pressed when
- // the editor is opened
- v.mouseReleased = true
- v.lastClickTime = time.Time{}
-
return v
}
-// UpdateLines sets the values for v.updateLines
-func (v *View) UpdateLines(start, end int) {
- v.updateLines[0] = start
- v.updateLines[1] = end + 1
-}
-
// 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
if !canceled {
if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
return true
+ } else if strings.ToLower(quit) == "save" || strings.ToLower(quit) == "s" {
+ v.Save()
+ return true
}
}
} else {
v.cursor.y = 0
}
+// 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.leftCol = 0
+ // Put the cursor at the first spot
+ v.cursor = Cursor{
+ x: 0,
+ y: 0,
+ v: v,
+ }
+ v.cursor.ResetSelection()
+
+ v.eh = NewEventHandler(v)
+ v.matches = Match(v)
+
+ // Set mouseReleased to true because we assume the mouse is not being pressed when
+ // the editor is opened
+ v.mouseReleased = true
+ v.lastClickTime = time.Time{}
+}
+
// OpenFile opens a new file in the current view
// It makes sure that the current buffer can be closed first (unsaved changes)
func (v *View) OpenFile() {
- if v.CanClose("Continue? ") {
+ if v.CanClose("Continue? (yes, no, save) ") {
filename, canceled := messenger.Prompt("File to open: ")
if canceled {
return
}
+ home, _ := homedir.Dir()
+ filename = strings.Replace(filename, "~", home, 1)
file, err := ioutil.ReadFile(filename)
if err != nil {
messenger.Error(err.Error())
return
}
- v.buf = NewBuffer(string(file), filename)
+ buf := NewBuffer(string(file), filename)
+ v.OpenBuffer(buf)
}
}
if y >= len(v.buf.lines) {
y = len(v.buf.lines) - 1
}
+ if y < 0 {
+ y = 0
+ }
if x < 0 {
x = 0
}
// By default it's true because most events should cause a relocate
relocate := true
- // By default we don't update and syntax highlighting
- v.UpdateLines(-2, 0)
switch e := event.(type) {
case *tcell.EventResize:
// Window resized
v.cursor.DeleteSelection()
v.cursor.ResetSelection()
}
+
v.eh.Insert(v.cursor.Loc(), "\n")
+ ws := GetLeadingWhitespace(v.buf.lines[v.cursor.y])
v.cursor.Right()
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
+
+ if settings.AutoIndent {
+ v.eh.Insert(v.cursor.Loc(), ws)
+ for i := 0; i < len(ws); i++ {
+ v.cursor.Right()
+ }
+ }
v.cursor.lastVisualX = v.cursor.GetVisualX()
- // v.UpdateLines(v.cursor.y-1, v.cursor.y)
case tcell.KeySpace:
// Insert a space
if v.cursor.HasSelection() {
}
v.eh.Insert(v.cursor.Loc(), " ")
v.cursor.Right()
- v.UpdateLines(v.cursor.y, v.cursor.y)
- case tcell.KeyBackspace2:
+ case tcell.KeyBackspace2, tcell.KeyBackspace:
// Delete a character
if v.cursor.HasSelection() {
v.cursor.DeleteSelection()
v.cursor.ResetSelection()
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
} else if v.cursor.Loc() > 0 {
// We have to do something a bit hacky here because we want to
// delete the line by first moving left and then deleting backwards
// but the undo redo would place the cursor in the wrong place
// So instead we move left, save the position, move back, delete
// and restore the position
- v.cursor.Left()
- cx, cy := v.cursor.x, v.cursor.y
- v.cursor.Right()
- loc := v.cursor.Loc()
- v.eh.Remove(loc-1, loc)
- v.cursor.x, v.cursor.y = cx, cy
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
- // v.UpdateLines(v.cursor.y, v.cursor.y+1)
+
+ // If the user is using spaces instead of tabs and they are deleting
+ // whitespace at the start of the line, we should delete as if its a
+ // tab (tabSize number of spaces)
+ lineStart := v.buf.lines[v.cursor.y][:v.cursor.x]
+ if settings.TabsToSpaces && IsSpaces(lineStart) && len(lineStart) != 0 && len(lineStart)%settings.TabSize == 0 {
+ loc := v.cursor.Loc()
+ v.cursor.SetLoc(loc - settings.TabSize)
+ cx, cy := v.cursor.x, v.cursor.y
+ v.cursor.SetLoc(loc)
+ v.eh.Remove(loc-settings.TabSize, loc)
+ v.cursor.x, v.cursor.y = cx, cy
+ } else {
+ v.cursor.Left()
+ cx, cy := v.cursor.x, v.cursor.y
+ v.cursor.Right()
+ loc := v.cursor.Loc()
+ v.eh.Remove(loc-1, loc)
+ v.cursor.x, v.cursor.y = cx, cy
+ }
}
v.cursor.lastVisualX = v.cursor.GetVisualX()
case tcell.KeyTab:
v.cursor.DeleteSelection()
v.cursor.ResetSelection()
}
- v.eh.Insert(v.cursor.Loc(), "\t")
- v.cursor.Right()
- v.UpdateLines(v.cursor.y, v.cursor.y)
+ if settings.TabsToSpaces {
+ v.eh.Insert(v.cursor.Loc(), Spaces(settings.TabSize))
+ for i := 0; i < settings.TabSize; i++ {
+ v.cursor.Right()
+ }
+ } else {
+ v.eh.Insert(v.cursor.Loc(), "\t")
+ v.cursor.Right()
+ }
case tcell.KeyCtrlS:
v.Save()
case tcell.KeyCtrlF:
Search(lastSearch, v, false)
case tcell.KeyCtrlZ:
v.eh.Undo()
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
case tcell.KeyCtrlY:
v.eh.Redo()
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
case tcell.KeyCtrlC:
v.Copy()
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
case tcell.KeyCtrlX:
v.Cut()
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
case tcell.KeyCtrlV:
v.Paste()
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
case tcell.KeyCtrlA:
v.SelectAll()
case tcell.KeyCtrlO:
v.OpenFile()
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
+ case tcell.KeyHome:
+ v.topline = 0
+ relocate = false
+ case tcell.KeyEnd:
+ if v.height > len(v.buf.lines) {
+ v.topline = 0
+ } else {
+ v.topline = len(v.buf.lines) - v.height
+ }
+ relocate = false
case tcell.KeyPgUp:
v.PageUp()
relocate = false
case tcell.KeyCtrlD:
v.HalfPageDown()
relocate = false
+ case tcell.KeyCtrlR:
+ if settings.Ruler == false {
+ settings.Ruler = true
+ } else {
+ settings.Ruler = false
+ }
case tcell.KeyRune:
// Insert a character
if v.cursor.HasSelection() {
v.cursor.DeleteSelection()
v.cursor.ResetSelection()
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
- } else {
- v.UpdateLines(v.cursor.y, v.cursor.y)
}
v.eh.Insert(v.cursor.Loc(), string(e.Rune()))
v.cursor.Right()
x, y := e.Position()
x -= v.lineNumOffset - v.leftCol
y += v.topline
- // Position always seems to be off by one
- x--
- y--
button := e.Buttons()
v.ScrollUp(2)
// We don't want to relocate if the user is scrolling
relocate = false
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
case tcell.WheelDown:
// Scroll down two lines
v.ScrollDown(2)
// We don't want to relocate if the user is scrolling
relocate = false
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
}
}
// DisplayView renders the view to the screen
func (v *View) DisplayView() {
- // matches := make(SyntaxMatches, len(v.buf.lines))
- //
- // viewStart := v.topline
- // viewEnd := v.topline + v.height
- // if viewEnd > len(v.buf.lines) {
- // viewEnd = len(v.buf.lines)
- // }
- //
- // lines := v.buf.lines[viewStart:viewEnd]
- // for i, line := range lines {
- // matches[i] = make([]tcell.Style, len(line))
- // }
-
// The character number of the character in the top left of the screen
-
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(len(v.buf.lines)))
// + 1 for the little space after the line number
- v.lineNumOffset = maxLineLength + 1
-
+ if settings.Ruler == true {
+ v.lineNumOffset = maxLineLength + 1
+ } else {
+ v.lineNumOffset = 0
+ }
var highlightStyle tcell.Style
for lineN := 0; lineN < v.height; lineN++ {
lineNumStyle = style
}
// Write the spaces before the line number if necessary
- lineNum := strconv.Itoa(lineN + v.topline + 1)
- for i := 0; i < maxLineLength-len(lineNum); i++ {
- screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
- x++
- }
- // Write the actual line number
- for _, ch := range lineNum {
- screen.SetContent(x, lineN, ch, nil, lineNumStyle)
- x++
- }
- // Write the extra space
- screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
- x++
+ var lineNum string
+ if settings.Ruler == true {
+ lineNum = strconv.Itoa(lineN + v.topline + 1)
+ for i := 0; i < maxLineLength-len(lineNum); i++ {
+ screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
+ x++
+ }
+ // Write the actual line number
+ for _, ch := range lineNum {
+ screen.SetContent(x, lineN, ch, nil, lineNumStyle)
+ x++
+ }
+ if settings.Ruler == true {
+ // Write the extra space
+ screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
+ x++
+ }
+ }
// Write the line
tabchars := 0
runes := []rune(line)
}
ch := runes[colN]
var lineStyle tcell.Style
- // Does the current character need to be syntax highlighted?
-
- // if lineN >= v.updateLines[0] && lineN < v.updateLines[1] {
if settings.Syntax {
+ // Syntax highlighting is enabled
highlightStyle = v.matches[lineN][colN]
}
- // } else if lineN < len(v.lastMatches) && colN < len(v.lastMatches[lineN]) {
- // highlightStyle = v.lastMatches[lineN][colN]
- // } else {
- // highlightStyle = defStyle
- // }
if v.cursor.HasSelection() &&
(charNum >= v.cursor.curSelection[0] && charNum < v.cursor.curSelection[1] ||
} else {
lineStyle = highlightStyle
}
- // matches[lineN][colN] = highlightStyle
if ch == '\t' {
screen.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)