micro/cmd/micro/view.go

478 lines
12 KiB
Go
Raw Normal View History

2016-03-17 21:27:57 +00:00
package main
import (
2016-04-24 01:48:51 +00:00
"io/ioutil"
2016-03-19 13:24:04 +00:00
"strconv"
2016-03-23 15:41:04 +00:00
"strings"
2016-03-26 20:32:19 +00:00
"time"
2016-04-22 20:02:26 +00:00
"github.com/gdamore/tcell"
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 stores information about the cursor, and the viewport
// 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-26 20:32:19 +00:00
// This stores when the last click was
// This is useful for detecting double and triple clicks
lastClickTime time.Time
2016-04-24 01:29:09 +00:00
// lastCutTime stores when the last ctrl+k was issued.
// It is used for clearing the clipboard to replace it with fresh cut lines.
lastCutTime time.Time
// freshClip returns true if the clipboard has never been pasted.
freshClip bool
2016-03-26 20:32:19 +00:00
// Was the last mouse event actually a double click?
// Useful for detecting triple clicks -- if a double click is detected
// but the last mouse event was actually a double click, it's a triple click
doubleClick bool
// Same here, just to keep track for mouse move events
tripleClick 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
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)
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
v.OpenBuffer(buf)
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
}
2016-03-17 21:27:57 +00:00
return v
}
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++
}
}
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
2016-04-19 17:16:08 +00:00
} else if strings.ToLower(quit) == "save" || strings.ToLower(quit) == "s" {
v.Save()
2016-04-19 17:16:08 +00:00
return true
2016-03-25 16:14:22 +00:00
}
}
} else {
return true
}
return false
}
// 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{}
}
2016-04-24 01:48:51 +00:00
// Close and Re-open the current file.
func (v *View) reOpen() {
if v.CanClose("Continue? (yes, no, save) ") {
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.matches = Match(v)
2016-04-24 21:26:42 +00:00
v.cursor.Relocate()
2016-04-24 01:48:51 +00:00
v.Relocate()
}
}
2016-03-25 16:14:22 +00:00
// 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() bool {
ret := false
2016-03-25 16:14:22 +00:00
cy := v.cursor.y
if cy < v.topline {
v.topline = cy
ret = true
2016-03-25 16:14:22 +00:00
}
if cy > v.topline+v.height-1 {
v.topline = cy - v.height + 1
ret = true
2016-03-25 16:14:22 +00:00
}
2016-04-04 18:03:17 +00:00
cx := v.cursor.GetVisualX()
if cx < v.leftCol {
v.leftCol = cx
ret = true
2016-04-04 18:03:17 +00:00
}
if cx+v.lineNumOffset+1 > v.leftCol+v.width {
v.leftCol = cx - v.width + v.lineNumOffset + 1
ret = true
2016-04-04 18:03:17 +00:00
}
return ret
2016-03-25 16:14:22 +00:00
}
// 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 y < 0 {
y = 0
}
2016-03-25 16:14:22 +00:00
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-04-04 18:09:24 +00:00
v.cursor.lastVisualX = v.cursor.GetVisualX()
2016-03-25 16:14:22 +00:00
}
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
2016-04-10 21:27:02 +00:00
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:
if e.Key() == 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()
} else {
for key, action := range bindings {
if e.Key() == key {
relocate = action(v)
}
}
2016-03-17 21:27:57 +00:00
}
case *tcell.EventMouse:
x, y := e.Position()
2016-04-04 22:05:34 +00:00
x -= v.lineNumOffset - v.leftCol
2016-03-17 21:27:57 +00:00
y += v.topline
button := e.Buttons()
switch button {
case tcell.Button1:
// Left click
2016-03-26 20:32:19 +00:00
origX, origY := v.cursor.x, v.cursor.y
2016-03-25 16:14:22 +00:00
v.MoveToMouseClick(x, y)
2016-03-17 21:27:57 +00:00
if v.mouseReleased {
2016-03-26 20:32:19 +00:00
if (time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold) &&
(origX == v.cursor.x && origY == v.cursor.y) {
if v.doubleClick {
// Triple click
v.lastClickTime = time.Now()
2016-03-28 12:43:08 +00:00
2016-03-26 20:32:19 +00:00
v.tripleClick = true
v.doubleClick = false
2016-03-28 12:43:08 +00:00
2016-03-26 20:32:19 +00:00
v.cursor.SelectLine()
} else {
// Double click
2016-03-28 12:43:08 +00:00
v.lastClickTime = time.Now()
2016-03-26 20:32:19 +00:00
v.doubleClick = true
v.tripleClick = false
2016-03-28 12:43:08 +00:00
v.cursor.SelectWord()
2016-03-26 20:32:19 +00:00
}
} else {
v.doubleClick = false
v.tripleClick = false
v.lastClickTime = time.Now()
loc := v.cursor.Loc()
2016-03-27 20:35:54 +00:00
v.cursor.curSelection[0] = loc
v.cursor.curSelection[1] = loc
2016-03-26 20:32:19 +00:00
}
} else {
if v.tripleClick {
v.cursor.AddLineToSelection()
} else if v.doubleClick {
2016-03-28 12:43:08 +00:00
v.cursor.AddWordToSelection()
2016-03-26 20:32:19 +00:00
} else {
v.cursor.curSelection[1] = v.cursor.Loc()
2016-03-26 20:32:19 +00:00
}
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
2016-03-27 20:35:54 +00:00
if !v.doubleClick && !v.tripleClick {
v.MoveToMouseClick(x, y)
v.cursor.curSelection[1] = 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-04-10 22:02:58 +00:00
if settings.Syntax {
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() {
// 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
2016-04-22 20:02:26 +00:00
if settings.Ruler == true {
v.lineNumOffset = maxLineLength + 1
} else {
v.lineNumOffset = 0
}
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 := defStyle
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
2016-04-22 20:02:26 +00:00
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++
}
2016-03-19 13:24:04 +00:00
2016-04-22 20:02:26 +00:00
if settings.Ruler == true {
// Write the extra space
screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
x++
}
}
2016-03-19 13:24:04 +00:00
// Write the line
2016-03-19 00:01:05 +00:00
tabchars := 0
2016-04-22 22:50:01 +00:00
for colN, ch := range line {
2016-03-20 19:24:40 +00:00
var lineStyle tcell.Style
2016-04-22 22:50:01 +00:00
2016-04-10 22:02:58 +00:00
if settings.Syntax {
// Syntax highlighting is enabled
2016-04-10 22:02:58 +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-04-22 23:31:50 +00:00
(charNum >= v.cursor.curSelection[0] && charNum < v.cursor.curSelection[1] ||
charNum < v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
2016-04-22 22:50:01 +00:00
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)
tabSize := settings.TabSize
for i := 0; i < tabSize-1; i++ {
tabchars++
2016-04-04 18:03:17 +00:00
if x-v.leftCol+tabchars >= v.lineNumOffset {
screen.SetContent(x-v.leftCol+tabchars, lineN, ' ', nil, lineStyle)
}
}
} else {
2016-04-04 18:03:17 +00:00
if x-v.leftCol+tabchars >= v.lineNumOffset {
screen.SetContent(x-v.leftCol+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.curSelection[0] && charNum < v.cursor.curSelection[1] ||
charNum < v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
2016-03-23 21:15:10 +00:00
selectStyle := defStyle.Reverse(true)
2016-03-23 21:15:10 +00:00
if style, ok := colorscheme["selection"]; ok {
selectStyle = style
2016-03-23 21:15:10 +00:00
}
2016-04-04 18:03:17 +00:00
screen.SetContent(x-v.leftCol+tabchars, lineN, ' ', nil, selectStyle)
2016-03-23 21:15:10 +00:00
}
2016-03-17 21:27:57 +00:00
charNum++
}
}
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()
}