micro/src/view.go

585 lines
15 KiB
Go
Raw Normal View History

2016-03-17 21:27:57 +00:00
package main
import (
"github.com/atotto/clipboard"
2016-03-23 20:37:49 +00:00
"github.com/gdamore/tcell"
2016-03-24 01:09:27 +00:00
"io/ioutil"
2016-03-19 13:24:04 +00:00
"strconv"
2016-03-23 15:41:04 +00:00
"strings"
2016-03-17 21:27:57 +00:00
)
2016-03-19 00:40:00 +00:00
// The View struct stores information about a view into a buffer.
// It has a value for the cursor, and the window that the user sees
// the buffer from.
2016-03-17 21:27:57 +00:00
type View struct {
2016-03-25 16:14:22 +00:00
cursor Cursor
// The topmost line, used for vertical scrolling
2016-03-21 21:43:53 +00:00
topline int
2016-03-25 16:14:22 +00:00
// The leftmost column, used for horizontal scrolling
2016-03-21 21:43:53 +00:00
leftCol int
2016-03-25 16:14:22 +00:00
// Percentage of the terminal window that this view takes up (from 0 to 100)
widthPercent int
heightPercent int
// Actual with and height
width int
height int
2016-03-21 21:43:53 +00:00
// How much to offset because of line numbers
2016-03-19 13:24:04 +00:00
lineNumOffset int
2016-03-17 22:20:07 +00:00
2016-03-25 16:14:22 +00:00
// The eventhandler for undo/redo
2016-03-19 22:16:10 +00:00
eh *EventHandler
2016-03-25 16:14:22 +00:00
// The buffer
2016-03-17 22:20:07 +00:00
buf *Buffer
2016-03-25 16:14:22 +00:00
// The statusline
sline Statusline
2016-03-17 21:27:57 +00:00
2016-03-25 16:14:22 +00:00
// Since tcell doesn't differentiate between a mouse release event
// and a mouse move event with no keys pressed, we need to keep
// track of whether or not the mouse was pressed (or not released) last event to determine
// mouse release events
2016-03-17 21:27:57 +00:00
mouseReleased bool
2016-03-25 18:56:29 +00:00
// Syntax highlighting matches
2016-03-25 16:14:22 +00:00
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
2016-03-17 21:27:57 +00:00
}
2016-03-25 16:14:22 +00:00
// NewView returns a new fullscreen view
2016-03-26 14:54:18 +00:00
func NewView(buf *Buffer) *View {
return NewViewWidthHeight(buf, 100, 100)
2016-03-17 22:20:07 +00:00
}
2016-03-21 21:43:53 +00:00
// NewViewWidthHeight returns a new view with the specified width and height percentages
2016-03-25 16:14:22 +00:00
// Note that w and h are percentages not actual values
2016-03-26 14:54:18 +00:00
func NewViewWidthHeight(buf *Buffer, w, h int) *View {
2016-03-17 21:27:57 +00:00
v := new(View)
v.buf = buf
2016-03-21 21:43:53 +00:00
v.widthPercent = w
v.heightPercent = h
2016-03-25 16:14:22 +00:00
v.Resize(screen.Size())
2016-03-21 21:43:53 +00:00
2016-03-17 21:27:57 +00:00
v.topline = 0
2016-03-25 16:14:22 +00:00
// Put the cursor at the first spot
2016-03-17 21:27:57 +00:00
v.cursor = Cursor{
2016-03-26 21:23:52 +00:00
x: 0,
y: 0,
v: v,
2016-03-17 21:27:57 +00:00
}
2016-03-19 22:16:10 +00:00
v.eh = NewEventHandler(v)
2016-03-25 16:14:22 +00:00
v.sline = Statusline{
view: v,
2016-03-17 22:20:07 +00:00
}
// Update the syntax highlighting for the entire buffer at the start
v.UpdateLines(v.topline, v.topline+v.height)
2016-03-25 23:18:29 +00:00
v.matches = Match(v)
2016-03-25 17:42:41 +00:00
// Set mouseReleased to true because we assume the mouse is not being pressed when
// the editor is opened
v.mouseReleased = true
2016-03-17 21:27:57 +00:00
return v
}
// UpdateLines sets the values for v.updateLines
func (v *View) UpdateLines(start, end int) {
v.updateLines[0] = start
v.updateLines[1] = end
}
2016-03-25 16:14:22 +00:00
// 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
2016-03-21 21:43:53 +00:00
func (v *View) Resize(w, h int) {
2016-03-25 16:14:22 +00:00
// Always include 1 line for the command line at the bottom
2016-03-21 21:43:53 +00:00
h--
2016-03-25 16:14:22 +00:00
v.width = int(float32(w) * float32(v.widthPercent) / 100)
// We subtract 1 for the statusline
v.height = int(float32(h)*float32(v.heightPercent)/100) - 1
2016-03-21 21:43:53 +00:00
}
2016-03-19 00:40:00 +00:00
// 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--
}
}
2016-03-19 00:40:00 +00:00
// 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 <= len(v.buf.lines)-v.height {
v.topline += n
} else if v.topline < len(v.buf.lines)-v.height {
v.topline++
}
}
// PageUp scrolls the view up a page
func (v *View) PageUp() {
if v.topline > v.height {
v.ScrollUp(v.height)
} else {
v.topline = 0
}
}
// PageDown scrolls the view down a page
func (v *View) PageDown() {
if len(v.buf.lines)-(v.topline+v.height) > v.height {
v.ScrollDown(v.height)
} else {
v.topline = len(v.buf.lines) - v.height
}
}
// HalfPageUp scrolls the view up half a page
func (v *View) HalfPageUp() {
if v.topline > v.height/2 {
v.ScrollUp(v.height / 2)
} else {
v.topline = 0
}
}
// HalfPageDown scrolls the view down half a page
func (v *View) HalfPageDown() {
if len(v.buf.lines)-(v.topline+v.height) > v.height/2 {
v.ScrollDown(v.height / 2)
} else {
v.topline = len(v.buf.lines) - v.height
}
}
2016-03-25 16:14:22 +00:00
// CanClose returns whether or not the view can be closed
// If there are unsaved changes, the user will be asked if the view can be closed
// 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() {
2016-03-26 14:54:18 +00:00
quit, canceled := messenger.Prompt("You have unsaved changes. " + msg)
2016-03-25 16:14:22 +00:00
if !canceled {
if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
return true
}
}
} else {
return true
}
return false
}
// Save the buffer to disk
func (v *View) Save() {
// If this is an empty buffer, ask for a filename
if v.buf.path == "" {
2016-03-26 14:54:18 +00:00
filename, canceled := messenger.Prompt("Filename: ")
2016-03-25 16:14:22 +00:00
if !canceled {
v.buf.path = filename
v.buf.name = filename
} else {
return
}
}
err := v.buf.Save()
if err != nil {
2016-03-26 14:54:18 +00:00
messenger.Error(err.Error())
2016-03-25 16:14:22 +00:00
}
}
// Copy the selection to the system clipboard
func (v *View) Copy() {
if v.cursor.HasSelection() {
if !clipboard.Unsupported {
clipboard.WriteAll(v.cursor.GetSelection())
} else {
2016-03-26 14:54:18 +00:00
messenger.Error("Clipboard is not supported on your system")
2016-03-25 16:14:22 +00:00
}
}
}
// Cut the selection to the system clipboard
func (v *View) Cut() {
if v.cursor.HasSelection() {
if !clipboard.Unsupported {
clipboard.WriteAll(v.cursor.GetSelection())
v.cursor.DeleteSelection()
v.cursor.ResetSelection()
} else {
2016-03-26 14:54:18 +00:00
messenger.Error("Clipboard is not supported on your system")
2016-03-25 16:14:22 +00:00
}
}
}
// Paste whatever is in the system clipboard into the buffer
// Delete and paste if the user has a selection
func (v *View) Paste() {
if !clipboard.Unsupported {
if v.cursor.HasSelection() {
v.cursor.DeleteSelection()
v.cursor.ResetSelection()
}
clip, _ := clipboard.ReadAll()
2016-03-26 21:23:52 +00:00
v.eh.Insert(v.cursor.Loc(), clip)
2016-03-25 16:14:22 +00:00
// This is a bit weird... Not sure if there's a better way
for i := 0; i < Count(clip); i++ {
v.cursor.Right()
}
} else {
2016-03-26 14:54:18 +00:00
messenger.Error("Clipboard is not supported on your system")
2016-03-25 16:14:22 +00:00
}
}
// SelectAll selects the entire buffer
func (v *View) SelectAll() {
v.cursor.selectionEnd = 0
v.cursor.selectionStart = v.buf.Len()
// Put the cursor at the beginning
v.cursor.x = 0
v.cursor.y = 0
}
// 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? ") {
2016-03-26 14:54:18 +00:00
filename, canceled := messenger.Prompt("File to open: ")
2016-03-25 16:14:22 +00:00
if canceled {
return
}
file, err := ioutil.ReadFile(filename)
if err != nil {
2016-03-26 14:54:18 +00:00
messenger.Error(err.Error())
2016-03-25 16:14:22 +00:00
return
}
v.buf = NewBuffer(string(file), filename)
}
}
// Relocate moves the view window so that the cursor is in view
// This is useful if the user has scrolled far away, and then starts typing
func (v *View) Relocate() {
cy := v.cursor.y
if cy < v.topline {
v.topline = cy
}
if cy > v.topline+v.height-1 {
v.topline = cy - v.height + 1
}
}
// 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 {
v.ScrollDown(1)
y = v.height + v.topline - 1
}
if y >= len(v.buf.lines) {
y = len(v.buf.lines) - 1
}
if x < 0 {
x = 0
}
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
}
2016-03-19 00:40:00 +00:00
// HandleEvent handles an event passed by the main loop
2016-03-25 16:14:22 +00:00
func (v *View) HandleEvent(event tcell.Event) {
// This bool determines whether the view is relocated at the end of the function
// 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)
2016-03-17 21:27:57 +00:00
switch e := event.(type) {
2016-03-21 21:43:53 +00:00
case *tcell.EventResize:
// Window resized
2016-03-21 21:43:53 +00:00
v.Resize(e.Size())
2016-03-17 21:27:57 +00:00
case *tcell.EventKey:
switch e.Key() {
case tcell.KeyUp:
// Cursor up
2016-03-19 00:40:00 +00:00
v.cursor.Up()
2016-03-17 21:27:57 +00:00
case tcell.KeyDown:
// Cursor down
2016-03-19 00:40:00 +00:00
v.cursor.Down()
2016-03-17 21:27:57 +00:00
case tcell.KeyLeft:
// Cursor left
2016-03-19 00:40:00 +00:00
v.cursor.Left()
2016-03-17 21:27:57 +00:00
case tcell.KeyRight:
// Cursor right
2016-03-19 00:40:00 +00:00
v.cursor.Right()
2016-03-17 21:27:57 +00:00
case tcell.KeyEnter:
// Insert a newline
2016-03-26 21:23:52 +00:00
v.eh.Insert(v.cursor.Loc(), "\n")
2016-03-19 00:40:00 +00:00
v.cursor.Right()
v.UpdateLines(v.cursor.y-1, v.cursor.y)
2016-03-17 22:20:07 +00:00
case tcell.KeySpace:
// Insert a space
2016-03-26 21:23:52 +00:00
v.eh.Insert(v.cursor.Loc(), " ")
2016-03-19 00:40:00 +00:00
v.cursor.Right()
v.UpdateLines(v.cursor.y, v.cursor.y)
2016-03-17 21:27:57 +00:00
case tcell.KeyBackspace2:
// Delete a character
2016-03-19 00:40:00 +00:00
if v.cursor.HasSelection() {
v.cursor.DeleteSelection()
2016-03-19 00:40:00 +00:00
v.cursor.ResetSelection()
// Rehighlight the entire buffer
v.UpdateLines(v.topline, v.topline+v.height)
2016-03-26 21:23:52 +00:00
} else if v.cursor.Loc() > 0 {
2016-03-19 22:16:10 +00:00
// 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
2016-03-19 00:40:00 +00:00
v.cursor.Left()
2016-03-26 21:23:52 +00:00
cx, cy := v.cursor.x, v.cursor.y
2016-03-19 22:16:10 +00:00
v.cursor.Right()
2016-03-26 21:23:52 +00:00
loc := v.cursor.Loc()
v.eh.Remove(loc-1, loc)
v.cursor.x, v.cursor.y = cx, cy
v.UpdateLines(v.cursor.y, v.cursor.y+1)
2016-03-17 21:27:57 +00:00
}
case tcell.KeyTab:
// Insert a tab
2016-03-26 21:23:52 +00:00
v.eh.Insert(v.cursor.Loc(), "\t")
2016-03-19 00:40:00 +00:00
v.cursor.Right()
v.UpdateLines(v.cursor.y, v.cursor.y)
2016-03-17 22:20:07 +00:00
case tcell.KeyCtrlS:
2016-03-25 16:14:22 +00:00
v.Save()
2016-03-19 22:16:10 +00:00
case tcell.KeyCtrlZ:
v.eh.Undo()
// Rehighlight the entire buffer
v.UpdateLines(v.topline, v.topline+v.height)
2016-03-19 22:16:10 +00:00
case tcell.KeyCtrlY:
v.eh.Redo()
// Rehighlight the entire buffer
v.UpdateLines(v.topline, v.topline+v.height)
case tcell.KeyCtrlC:
2016-03-25 16:14:22 +00:00
v.Copy()
// Rehighlight the entire buffer
v.UpdateLines(v.topline, v.topline+v.height)
case tcell.KeyCtrlX:
2016-03-25 16:14:22 +00:00
v.Cut()
// Rehighlight the entire buffer
v.UpdateLines(v.topline, v.topline+v.height)
case tcell.KeyCtrlV:
2016-03-25 16:14:22 +00:00
v.Paste()
// Rehighlight the entire buffer
v.UpdateLines(v.topline, v.topline+v.height)
case tcell.KeyCtrlA:
2016-03-25 16:14:22 +00:00
v.SelectAll()
2016-03-24 01:09:27 +00:00
case tcell.KeyCtrlO:
2016-03-25 16:14:22 +00:00
v.OpenFile()
// Rehighlight the entire buffer
v.UpdateLines(v.topline, v.topline+v.height)
case tcell.KeyPgUp:
v.PageUp()
2016-03-25 23:18:29 +00:00
relocate = false
case tcell.KeyPgDn:
v.PageDown()
2016-03-25 23:18:29 +00:00
relocate = false
case tcell.KeyCtrlU:
v.HalfPageUp()
2016-03-25 23:18:29 +00:00
relocate = false
case tcell.KeyCtrlD:
v.HalfPageDown()
2016-03-25 23:18:29 +00:00
relocate = false
2016-03-17 21:27:57 +00:00
case tcell.KeyRune:
// Insert a character
2016-03-19 00:40:00 +00:00
if v.cursor.HasSelection() {
v.cursor.DeleteSelection()
2016-03-19 00:40:00 +00:00
v.cursor.ResetSelection()
2016-03-19 00:01:05 +00:00
}
2016-03-26 21:23:52 +00:00
v.eh.Insert(v.cursor.Loc(), string(e.Rune()))
2016-03-19 00:40:00 +00:00
v.cursor.Right()
v.UpdateLines(v.cursor.y, v.cursor.y)
2016-03-17 21:27:57 +00:00
}
case *tcell.EventMouse:
x, y := e.Position()
2016-03-19 13:24:04 +00:00
x -= v.lineNumOffset
2016-03-17 21:27:57 +00:00
y += v.topline
// Position always seems to be off by one
x--
y--
button := e.Buttons()
switch button {
case tcell.Button1:
// Left click
2016-03-25 16:14:22 +00:00
v.MoveToMouseClick(x, y)
2016-03-17 21:27:57 +00:00
2016-03-26 21:23:52 +00:00
loc := v.cursor.Loc()
2016-03-17 21:27:57 +00:00
if v.mouseReleased {
2016-03-26 21:23:52 +00:00
v.cursor.selectionStart = loc
2016-03-17 21:27:57 +00:00
}
2016-03-26 21:23:52 +00:00
v.cursor.selectionEnd = loc
2016-03-17 21:27:57 +00:00
v.mouseReleased = false
case tcell.ButtonNone:
// Mouse event with no click
2016-03-25 16:14:22 +00:00
if !v.mouseReleased {
// Mouse was just released
// Relocating here isn't really necessary because the cursor will
// be in the right place from the last mouse event
// However, if we are running in a terminal that doesn't support mouse motion
// events, this still allows the user to make selections, except only after they
// release the mouse
v.MoveToMouseClick(x, y)
2016-03-26 21:23:52 +00:00
v.cursor.selectionEnd = v.cursor.Loc()
2016-03-25 16:14:22 +00:00
v.mouseReleased = true
}
// We don't want to relocate because otherwise the view will be relocated
2016-03-25 18:56:29 +00:00
// every time the user moves the cursor
2016-03-25 16:14:22 +00:00
relocate = false
2016-03-17 21:27:57 +00:00
case tcell.WheelUp:
// Scroll up two lines
2016-03-19 00:40:00 +00:00
v.ScrollUp(2)
2016-03-25 16:14:22 +00:00
// We don't want to relocate if the user is scrolling
relocate = false
2016-03-17 21:27:57 +00:00
case tcell.WheelDown:
// Scroll down two lines
2016-03-19 00:40:00 +00:00
v.ScrollDown(2)
2016-03-25 16:14:22 +00:00
// We don't want to relocate if the user is scrolling
relocate = false
2016-03-17 21:27:57 +00:00
}
}
2016-03-25 16:14:22 +00:00
if relocate {
v.Relocate()
2016-03-17 21:27:57 +00:00
}
2016-03-25 23:18:29 +00:00
v.matches = Match(v)
2016-03-17 21:27:57 +00:00
}
2016-03-25 16:14:22 +00:00
// DisplayView renders the view to the screen
func (v *View) DisplayView() {
2016-03-25 23:18:29 +00:00
matches := make(SyntaxMatches, len(v.matches))
2016-03-25 16:14:22 +00:00
// The character number of the character in the top left of the screen
2016-03-26 21:23:52 +00:00
charNum := ToCharPos(0, v.topline, v.buf)
2016-03-19 13:24:04 +00:00
// 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
2016-03-20 19:24:40 +00:00
var highlightStyle tcell.Style
2016-03-20 15:28:41 +00:00
2016-03-17 22:20:07 +00:00
for lineN := 0; lineN < v.height; lineN++ {
2016-03-25 16:14:22 +00:00
var x int
// If the buffer is smaller than the view height
// and we went too far, break
2016-03-17 21:27:57 +00:00
if lineN+v.topline >= len(v.buf.lines) {
break
}
line := v.buf.lines[lineN+v.topline]
2016-03-19 13:24:04 +00:00
// Write the line number
lineNumStyle := tcell.StyleDefault
if style, ok := colorscheme["line-number"]; ok {
lineNumStyle = style
}
2016-03-19 13:24:04 +00:00
// Write the spaces before the line number if necessary
lineNum := strconv.Itoa(lineN + v.topline + 1)
for i := 0; i < maxLineLength-len(lineNum); i++ {
2016-03-25 16:14:22 +00:00
screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
2016-03-19 13:24:04 +00:00
x++
}
// Write the actual line number
for _, ch := range lineNum {
2016-03-25 16:14:22 +00:00
screen.SetContent(x, lineN, ch, nil, lineNumStyle)
2016-03-19 13:24:04 +00:00
x++
}
// Write the extra space
2016-03-25 16:14:22 +00:00
screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
2016-03-19 13:24:04 +00:00
x++
// Write the line
2016-03-19 00:01:05 +00:00
tabchars := 0
2016-03-25 23:18:29 +00:00
for colN, ch := range line {
2016-03-20 19:24:40 +00:00
var lineStyle tcell.Style
2016-03-25 16:14:22 +00:00
// Does the current character need to be syntax highlighted?
2016-03-25 23:18:29 +00:00
highlightStyle = v.matches[lineN][colN]
2016-03-20 15:28:41 +00:00
2016-03-19 00:40:00 +00:00
if v.cursor.HasSelection() &&
2016-03-19 00:01:05 +00:00
(charNum >= v.cursor.selectionStart && charNum <= v.cursor.selectionEnd ||
charNum <= v.cursor.selectionStart && charNum >= v.cursor.selectionEnd) {
lineStyle = tcell.StyleDefault.Reverse(true)
if style, ok := colorscheme["selection"]; ok {
lineStyle = style
}
2016-03-20 19:24:40 +00:00
} else {
lineStyle = highlightStyle
2016-03-17 21:27:57 +00:00
}
if ch == '\t' {
2016-03-25 16:14:22 +00:00
screen.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)
2016-03-26 14:54:18 +00:00
tabSize := options["tabsize"].(int)
for i := 0; i < tabSize-1; i++ {
tabchars++
2016-03-25 16:14:22 +00:00
screen.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)
}
} else {
2016-03-25 16:14:22 +00:00
screen.SetContent(x+tabchars, lineN, ch, nil, lineStyle)
}
2016-03-17 21:27:57 +00:00
charNum++
2016-03-19 13:24:04 +00:00
x++
2016-03-17 21:27:57 +00:00
}
2016-03-25 16:14:22 +00:00
// Here we are at a newline
// The newline may be selected, in which case we should draw the selection style
// with a space to represent it
2016-03-23 21:15:10 +00:00
if v.cursor.HasSelection() &&
(charNum >= v.cursor.selectionStart && charNum <= v.cursor.selectionEnd ||
charNum <= v.cursor.selectionStart && charNum >= v.cursor.selectionEnd) {
selectStyle := tcell.StyleDefault.Reverse(true)
if style, ok := colorscheme["selection"]; ok {
selectStyle = style
2016-03-23 21:15:10 +00:00
}
2016-03-25 16:14:22 +00:00
screen.SetContent(x+tabchars, lineN, ' ', nil, selectStyle)
2016-03-23 21:15:10 +00:00
}
2016-03-17 21:27:57 +00:00
charNum++
}
v.lastMatches = matches
2016-03-17 21:27:57 +00:00
}
2016-03-25 16:14:22 +00:00
// Display renders the view, the cursor, and statusline
func (v *View) Display() {
v.DisplayView()
v.cursor.Display()
v.sline.Display()
}