2016-03-17 21:27:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2016-11-29 18:44:30 +00:00
|
|
|
"os"
|
2017-02-25 02:29:22 +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
|
|
|
|
2016-04-26 01:23:03 +00:00
|
|
|
"github.com/zyedidia/tcell"
|
2016-03-17 21:27:57 +00:00
|
|
|
)
|
|
|
|
|
|
2017-10-01 16:42:23 +00:00
|
|
|
// The ViewType defines what kind of view this is
|
2017-02-13 21:11:57 +00:00
|
|
|
type ViewType struct {
|
2017-04-28 15:07:05 +00:00
|
|
|
kind int
|
2017-02-13 21:11:57 +00:00
|
|
|
readonly bool // The file cannot be edited
|
|
|
|
|
scratch bool // The file cannot be saved
|
|
|
|
|
}
|
2016-09-26 17:08:37 +00:00
|
|
|
|
2017-02-13 21:11:57 +00:00
|
|
|
var (
|
2017-04-28 15:07:05 +00:00
|
|
|
vtDefault = ViewType{0, false, false}
|
|
|
|
|
vtHelp = ViewType{1, true, true}
|
|
|
|
|
vtLog = ViewType{2, true, true}
|
|
|
|
|
vtScratch = ViewType{3, false, true}
|
2016-09-26 17:08:37 +00:00
|
|
|
)
|
|
|
|
|
|
2016-03-19 00:40:00 +00:00
|
|
|
// The View struct stores information about a view into a buffer.
|
2016-05-03 22:54:01 +00:00
|
|
|
// It stores information about the cursor, and the viewport
|
2016-04-16 21:33:13 +00:00
|
|
|
// that the user sees the buffer from.
|
2016-03-17 21:27:57 +00:00
|
|
|
type View struct {
|
2016-05-22 19:01:02 +00:00
|
|
|
// A pointer to the buffer's cursor for ease of access
|
|
|
|
|
Cursor *Cursor
|
2016-03-25 16:14:22 +00:00
|
|
|
|
|
|
|
|
// The topmost line, used for vertical scrolling
|
2016-10-26 16:29:23 +00:00
|
|
|
Topline int
|
2016-03-25 16:14:22 +00:00
|
|
|
// The leftmost column, used for horizontal scrolling
|
2017-03-22 16:28:02 +00:00
|
|
|
leftCol int
|
2016-03-21 21:43:53 +00:00
|
|
|
|
2016-07-14 17:01:02 +00:00
|
|
|
// Specifies whether or not this view holds a help buffer
|
2016-09-26 17:08:37 +00:00
|
|
|
Type ViewType
|
2016-07-14 17:01:02 +00:00
|
|
|
|
2016-09-28 20:40:48 +00:00
|
|
|
// Actual width and height
|
2016-11-29 01:23:22 +00:00
|
|
|
Width int
|
|
|
|
|
Height int
|
2016-03-21 21:43:53 +00:00
|
|
|
|
2016-11-29 01:23:22 +00:00
|
|
|
LockWidth bool
|
|
|
|
|
LockHeight bool
|
2016-11-16 17:36:48 +00:00
|
|
|
|
2016-06-03 20:41:09 +00:00
|
|
|
// Where this view is located
|
|
|
|
|
x, y 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-04-25 16:48:43 +00:00
|
|
|
// Holds the list of gutter messages
|
2016-04-29 00:43:45 +00:00
|
|
|
messages map[string][]GutterMessage
|
2016-04-27 15:22:57 +00:00
|
|
|
|
2016-06-03 20:41:09 +00:00
|
|
|
// This is the index of this view in the views array
|
|
|
|
|
Num int
|
2016-06-08 17:26:50 +00:00
|
|
|
// What tab is this view stored in
|
|
|
|
|
TabNum int
|
2016-06-03 20:41:09 +00:00
|
|
|
|
2016-03-25 16:14:22 +00:00
|
|
|
// The buffer
|
2016-04-25 16:48:43 +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
|
2017-09-17 22:31:32 +00:00
|
|
|
lastLoc Loc
|
2016-03-26 20:32:19 +00:00
|
|
|
|
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
|
|
|
|
|
|
2017-02-14 02:29:50 +00:00
|
|
|
cellview *CellView
|
|
|
|
|
|
2016-08-11 14:45:35 +00:00
|
|
|
splitNode *LeafNode
|
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 {
|
2016-08-11 14:45:35 +00:00
|
|
|
screenW, screenH := screen.Size()
|
2016-08-31 15:16:22 +00:00
|
|
|
return NewViewWidthHeight(buf, screenW, screenH)
|
2016-03-17 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-20 20:02:19 +00:00
|
|
|
// NewViewWidthHeight returns a new view with the specified width and height
|
|
|
|
|
// Note that w and h are raw column and row 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-06-08 17:29:24 +00:00
|
|
|
v.x, v.y = 0, 0
|
2016-06-03 20:41:09 +00:00
|
|
|
|
2016-11-29 01:28:40 +00:00
|
|
|
v.Width = w
|
|
|
|
|
v.Height = h
|
2017-02-14 02:29:50 +00:00
|
|
|
v.cellview = new(CellView)
|
2016-03-21 21:43:53 +00:00
|
|
|
|
2016-08-11 17:50:59 +00:00
|
|
|
v.ToggleTabbar()
|
|
|
|
|
|
2016-04-20 17:52:10 +00:00
|
|
|
v.OpenBuffer(buf)
|
2016-03-17 21:27:57 +00:00
|
|
|
|
2016-04-29 00:43:45 +00:00
|
|
|
v.messages = make(map[string][]GutterMessage)
|
|
|
|
|
|
2016-03-25 16:14:22 +00:00
|
|
|
v.sline = Statusline{
|
|
|
|
|
view: v,
|
2016-03-17 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-24 23:55:44 +00:00
|
|
|
if v.Buf.Settings["statusline"].(bool) {
|
2016-11-29 01:28:40 +00:00
|
|
|
v.Height--
|
2016-05-15 17:44:07 +00:00
|
|
|
}
|
2016-08-11 14:45:35 +00:00
|
|
|
|
2016-12-12 15:37:48 +00:00
|
|
|
for pl := range loadedPlugins {
|
2016-08-25 20:01:42 +00:00
|
|
|
_, err := Call(pl+".onViewOpen", v)
|
|
|
|
|
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
|
|
|
|
|
TermMessage(err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-11 14:45:35 +00:00
|
|
|
return v
|
2016-03-21 21:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-18 15:12:28 +00:00
|
|
|
// ToggleStatusLine creates an extra row for the statusline if necessary
|
2016-08-11 17:50:59 +00:00
|
|
|
func (v *View) ToggleStatusLine() {
|
2016-08-24 23:55:44 +00:00
|
|
|
if v.Buf.Settings["statusline"].(bool) {
|
2016-11-29 01:28:40 +00:00
|
|
|
v.Height--
|
2016-08-11 17:50:59 +00:00
|
|
|
} else {
|
2016-11-29 01:28:40 +00:00
|
|
|
v.Height++
|
2016-08-11 17:50:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 15:12:28 +00:00
|
|
|
// ToggleTabbar creates an extra row for the tabbar if necessary
|
2016-08-11 17:50:59 +00:00
|
|
|
func (v *View) ToggleTabbar() {
|
|
|
|
|
if len(tabs) > 1 {
|
|
|
|
|
if v.y == 0 {
|
|
|
|
|
// Include one line for the tab bar at the top
|
2016-11-29 01:28:40 +00:00
|
|
|
v.Height--
|
2016-08-11 17:50:59 +00:00
|
|
|
v.y = 1
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if v.y == 1 {
|
|
|
|
|
v.y = 0
|
2016-11-29 01:28:40 +00:00
|
|
|
v.Height++
|
2016-08-11 17:50:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 13:40:08 +00:00
|
|
|
func (v *View) paste(clip string) {
|
|
|
|
|
leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
|
|
|
|
|
|
|
|
|
|
if v.Cursor.HasSelection() {
|
|
|
|
|
v.Cursor.DeleteSelection()
|
|
|
|
|
v.Cursor.ResetSelection()
|
|
|
|
|
}
|
|
|
|
|
clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
|
|
|
|
|
v.Buf.Insert(v.Cursor.Loc, clip)
|
2017-06-15 22:52:51 +00:00
|
|
|
// v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
|
2016-09-02 13:40:08 +00:00
|
|
|
v.freshClip = false
|
|
|
|
|
messenger.Message("Pasted clipboard")
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 00:40:00 +00:00
|
|
|
// ScrollUp scrolls the view up n lines (if possible)
|
|
|
|
|
func (v *View) ScrollUp(n int) {
|
2016-03-18 16:18:54 +00:00
|
|
|
// Try to scroll by n but if it would overflow, scroll by 1
|
2016-04-25 16:48:43 +00:00
|
|
|
if v.Topline-n >= 0 {
|
|
|
|
|
v.Topline -= n
|
|
|
|
|
} else if v.Topline > 0 {
|
|
|
|
|
v.Topline--
|
2016-03-18 16:18:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 00:40:00 +00:00
|
|
|
// ScrollDown scrolls the view down n lines (if possible)
|
|
|
|
|
func (v *View) ScrollDown(n int) {
|
2016-03-18 16:18:54 +00:00
|
|
|
// Try to scroll by n but if it would overflow, scroll by 1
|
2017-02-25 21:58:49 +00:00
|
|
|
if v.Topline+n <= v.Buf.NumLines {
|
2016-04-25 16:48:43 +00:00
|
|
|
v.Topline += n
|
2017-02-25 21:58:49 +00:00
|
|
|
} else if v.Topline < v.Buf.NumLines-1 {
|
2016-04-25 16:48:43 +00:00
|
|
|
v.Topline++
|
2016-03-18 16:18:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2016-09-06 14:06:36 +00:00
|
|
|
func (v *View) CanClose() bool {
|
2017-09-18 03:33:18 +00:00
|
|
|
if v.Type == vtDefault && v.Buf.Modified() {
|
2017-05-10 14:02:17 +00:00
|
|
|
var choice bool
|
2016-09-28 17:07:05 +00:00
|
|
|
var canceled bool
|
|
|
|
|
if v.Buf.Settings["autosave"].(bool) {
|
2017-05-10 14:02:17 +00:00
|
|
|
choice = true
|
2016-09-28 17:07:05 +00:00
|
|
|
} else {
|
2017-05-10 14:02:17 +00:00
|
|
|
choice, canceled = messenger.YesNoPrompt("Save changes to " + v.Buf.GetName() + " before closing? (y,n,esc) ")
|
2016-09-28 17:07:05 +00:00
|
|
|
}
|
2016-03-25 16:14:22 +00:00
|
|
|
if !canceled {
|
2017-05-10 14:02:17 +00:00
|
|
|
//if char == 'y' {
|
|
|
|
|
if choice {
|
2016-08-17 18:16:27 +00:00
|
|
|
v.Save(true)
|
2016-03-25 16:14:22 +00:00
|
|
|
}
|
2017-10-04 16:11:20 +00:00
|
|
|
} else {
|
|
|
|
|
return false
|
2016-03-25 16:14:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
2017-10-01 16:42:23 +00:00
|
|
|
return true
|
2016-03-25 16:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-19 22:58:52 +00:00
|
|
|
// OpenBuffer opens a new buffer in this view.
|
|
|
|
|
// This resets the topline, event handler and cursor.
|
|
|
|
|
func (v *View) OpenBuffer(buf *Buffer) {
|
2016-05-31 23:25:32 +00:00
|
|
|
screen.Clear()
|
2016-05-28 21:29:49 +00:00
|
|
|
v.CloseBuffer()
|
2016-04-25 16:48:43 +00:00
|
|
|
v.Buf = buf
|
2016-05-22 19:01:02 +00:00
|
|
|
v.Cursor = &buf.Cursor
|
2016-04-25 16:48:43 +00:00
|
|
|
v.Topline = 0
|
2017-03-22 16:28:02 +00:00
|
|
|
v.leftCol = 0
|
2016-04-25 16:48:43 +00:00
|
|
|
v.Cursor.ResetSelection()
|
2016-06-03 20:41:09 +00:00
|
|
|
v.Relocate()
|
2016-08-25 00:03:02 +00:00
|
|
|
v.Center(false)
|
2016-04-29 00:43:45 +00:00
|
|
|
v.messages = make(map[string][]GutterMessage)
|
2016-04-19 22:58:52 +00:00
|
|
|
|
|
|
|
|
// 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-10-18 15:12:28 +00:00
|
|
|
// Open opens the given file in the view
|
2016-09-06 23:58:34 +00:00
|
|
|
func (v *View) Open(filename string) {
|
2017-09-24 00:56:08 +00:00
|
|
|
filename = ReplaceHome(filename)
|
2016-11-29 18:44:30 +00:00
|
|
|
file, err := os.Open(filename)
|
2017-03-09 17:56:24 +00:00
|
|
|
fileInfo, _ := os.Stat(filename)
|
|
|
|
|
|
|
|
|
|
if err == nil && fileInfo.IsDir() {
|
|
|
|
|
messenger.Error(filename, " is a directory")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 18:44:30 +00:00
|
|
|
defer file.Close()
|
2016-09-06 23:58:34 +00:00
|
|
|
|
|
|
|
|
var buf *Buffer
|
|
|
|
|
if err != nil {
|
|
|
|
|
messenger.Message(err.Error())
|
|
|
|
|
// File does not exist -- create an empty buffer with that name
|
2017-04-29 18:12:00 +00:00
|
|
|
buf = NewBufferFromString("", filename)
|
2016-09-06 23:58:34 +00:00
|
|
|
} else {
|
2017-04-29 18:12:00 +00:00
|
|
|
buf = NewBuffer(file, FSize(file), filename)
|
2016-09-06 23:58:34 +00:00
|
|
|
}
|
|
|
|
|
v.OpenBuffer(buf)
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 21:29:49 +00:00
|
|
|
// CloseBuffer performs any closing functions on the buffer
|
|
|
|
|
func (v *View) CloseBuffer() {
|
|
|
|
|
if v.Buf != nil {
|
|
|
|
|
v.Buf.Serialize()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-24 23:52:02 +00:00
|
|
|
// ReOpen reloads the current buffer
|
|
|
|
|
func (v *View) ReOpen() {
|
2016-09-06 14:06:36 +00:00
|
|
|
if v.CanClose() {
|
2016-05-31 23:25:32 +00:00
|
|
|
screen.Clear()
|
2016-05-30 21:48:33 +00:00
|
|
|
v.Buf.ReOpen()
|
2016-05-30 22:22:10 +00:00
|
|
|
v.Relocate()
|
2016-04-24 01:48:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-01 22:12:37 +00:00
|
|
|
// HSplit opens a horizontal split with the given buffer
|
2016-11-29 01:20:30 +00:00
|
|
|
func (v *View) HSplit(buf *Buffer) {
|
|
|
|
|
i := 0
|
2017-10-11 12:22:23 +00:00
|
|
|
if v.Buf.Settings["splitbottom"].(bool) {
|
2016-11-29 01:20:30 +00:00
|
|
|
i = 1
|
|
|
|
|
}
|
|
|
|
|
v.splitNode.HSplit(buf, v.Num+i)
|
2016-06-26 22:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-01 22:12:37 +00:00
|
|
|
// VSplit opens a vertical split with the given buffer
|
2016-11-29 01:20:30 +00:00
|
|
|
func (v *View) VSplit(buf *Buffer) {
|
|
|
|
|
i := 0
|
2017-10-11 12:22:23 +00:00
|
|
|
if v.Buf.Settings["splitright"].(bool) {
|
2016-11-29 01:20:30 +00:00
|
|
|
i = 1
|
|
|
|
|
}
|
|
|
|
|
v.splitNode.VSplit(buf, v.Num+i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HSplitIndex opens a horizontal split with the given buffer at the given index
|
|
|
|
|
func (v *View) HSplitIndex(buf *Buffer, splitIndex int) {
|
|
|
|
|
v.splitNode.HSplit(buf, splitIndex)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VSplitIndex opens a vertical split with the given buffer at the given index
|
|
|
|
|
func (v *View) VSplitIndex(buf *Buffer, splitIndex int) {
|
|
|
|
|
v.splitNode.VSplit(buf, splitIndex)
|
2016-06-15 22:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-18 15:12:28 +00:00
|
|
|
// GetSoftWrapLocation gets the location of a visual click on the screen and converts it to col,line
|
2016-10-13 18:26:45 +00:00
|
|
|
func (v *View) GetSoftWrapLocation(vx, vy int) (int, int) {
|
|
|
|
|
if !v.Buf.Settings["softwrap"].(bool) {
|
2016-12-26 15:33:41 +00:00
|
|
|
if vy >= v.Buf.NumLines {
|
|
|
|
|
vy = v.Buf.NumLines - 1
|
|
|
|
|
}
|
2016-10-13 21:09:15 +00:00
|
|
|
vx = v.Cursor.GetCharPosInLine(vy, vx)
|
2016-10-13 18:26:45 +00:00
|
|
|
return vx, vy
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
screenX, screenY := 0, v.Topline
|
2016-10-28 23:42:17 +00:00
|
|
|
for lineN := v.Topline; lineN < v.Bottomline(); lineN++ {
|
2016-10-13 18:26:45 +00:00
|
|
|
line := v.Buf.Line(lineN)
|
2016-12-26 15:33:41 +00:00
|
|
|
if lineN >= v.Buf.NumLines {
|
|
|
|
|
return 0, v.Buf.NumLines - 1
|
|
|
|
|
}
|
2016-10-13 18:26:45 +00:00
|
|
|
|
|
|
|
|
colN := 0
|
|
|
|
|
for _, ch := range line {
|
2016-11-29 01:28:40 +00:00
|
|
|
if screenX >= v.Width-v.lineNumOffset {
|
2016-10-13 18:26:45 +00:00
|
|
|
screenX = 0
|
|
|
|
|
screenY++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if screenX == vx && screenY == vy {
|
|
|
|
|
return colN, lineN
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ch == '\t' {
|
|
|
|
|
screenX += int(v.Buf.Settings["tabsize"].(float64)) - 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
screenX++
|
|
|
|
|
colN++
|
|
|
|
|
}
|
2016-10-13 21:09:15 +00:00
|
|
|
if screenY == vy {
|
|
|
|
|
return colN, lineN
|
|
|
|
|
}
|
2016-10-13 18:26:45 +00:00
|
|
|
screenX = 0
|
|
|
|
|
screenY++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0, 0
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-26 16:29:23 +00:00
|
|
|
func (v *View) Bottomline() int {
|
2016-11-16 17:36:48 +00:00
|
|
|
if !v.Buf.Settings["softwrap"].(bool) {
|
2016-11-29 01:28:40 +00:00
|
|
|
return v.Topline + v.Height
|
2016-11-16 17:36:48 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-26 16:29:23 +00:00
|
|
|
screenX, screenY := 0, 0
|
|
|
|
|
numLines := 0
|
2016-11-29 01:28:40 +00:00
|
|
|
for lineN := v.Topline; lineN < v.Topline+v.Height; lineN++ {
|
2016-10-26 16:29:23 +00:00
|
|
|
line := v.Buf.Line(lineN)
|
|
|
|
|
|
|
|
|
|
colN := 0
|
|
|
|
|
for _, ch := range line {
|
2016-11-29 01:28:40 +00:00
|
|
|
if screenX >= v.Width-v.lineNumOffset {
|
2016-10-26 16:29:23 +00:00
|
|
|
screenX = 0
|
|
|
|
|
screenY++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ch == '\t' {
|
|
|
|
|
screenX += int(v.Buf.Settings["tabsize"].(float64)) - 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
screenX++
|
|
|
|
|
colN++
|
|
|
|
|
}
|
|
|
|
|
screenX = 0
|
|
|
|
|
screenY++
|
|
|
|
|
numLines++
|
|
|
|
|
|
2016-11-29 01:28:40 +00:00
|
|
|
if screenY >= v.Height {
|
2016-10-26 16:29:23 +00:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return numLines + v.Topline
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2016-04-16 13:55:40 +00:00
|
|
|
func (v *View) Relocate() bool {
|
2016-10-26 16:29:23 +00:00
|
|
|
height := v.Bottomline() - v.Topline
|
2016-04-16 13:55:40 +00:00
|
|
|
ret := false
|
2016-05-28 21:29:49 +00:00
|
|
|
cy := v.Cursor.Y
|
2016-08-24 23:55:44 +00:00
|
|
|
scrollmargin := int(v.Buf.Settings["scrollmargin"].(float64))
|
2016-05-20 18:06:01 +00:00
|
|
|
if cy < v.Topline+scrollmargin && cy > scrollmargin-1 {
|
|
|
|
|
v.Topline = cy - scrollmargin
|
2016-04-16 13:55:40 +00:00
|
|
|
ret = true
|
2016-05-20 17:44:16 +00:00
|
|
|
} else if cy < v.Topline {
|
2016-04-25 16:48:43 +00:00
|
|
|
v.Topline = cy
|
2016-04-16 13:55:40 +00:00
|
|
|
ret = true
|
2016-03-25 16:14:22 +00:00
|
|
|
}
|
2016-10-12 20:24:00 +00:00
|
|
|
if cy > v.Topline+height-1-scrollmargin && cy < v.Buf.NumLines-scrollmargin {
|
|
|
|
|
v.Topline = cy - height + 1 + scrollmargin
|
2016-04-16 13:55:40 +00:00
|
|
|
ret = true
|
2016-10-12 20:24:00 +00:00
|
|
|
} else if cy >= v.Buf.NumLines-scrollmargin && cy > height {
|
|
|
|
|
v.Topline = v.Buf.NumLines - height
|
2016-05-22 21:52:42 +00:00
|
|
|
ret = true
|
2016-03-25 16:14:22 +00:00
|
|
|
}
|
2016-04-04 18:03:17 +00:00
|
|
|
|
2016-10-12 20:24:00 +00:00
|
|
|
if !v.Buf.Settings["softwrap"].(bool) {
|
|
|
|
|
cx := v.Cursor.GetVisualX()
|
|
|
|
|
if cx < v.leftCol {
|
2017-03-22 16:28:02 +00:00
|
|
|
v.leftCol = cx
|
2016-10-12 20:24:00 +00:00
|
|
|
ret = true
|
|
|
|
|
}
|
2016-11-29 01:28:40 +00:00
|
|
|
if cx+v.lineNumOffset+1 > v.leftCol+v.Width {
|
2017-03-22 16:28:02 +00:00
|
|
|
v.leftCol = cx - v.Width + v.lineNumOffset + 1
|
2016-10-12 20:24:00 +00:00
|
|
|
ret = true
|
|
|
|
|
}
|
2016-04-04 18:03:17 +00:00
|
|
|
}
|
2016-04-16 13:55:40 +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) {
|
2016-11-29 01:28:40 +00:00
|
|
|
if y-v.Topline > v.Height-1 {
|
2016-03-25 16:14:22 +00:00
|
|
|
v.ScrollDown(1)
|
2016-11-29 01:28:40 +00:00
|
|
|
y = v.Height + v.Topline - 1
|
2016-03-25 16:14:22 +00:00
|
|
|
}
|
2016-04-18 15:54:32 +00:00
|
|
|
if y < 0 {
|
|
|
|
|
y = 0
|
|
|
|
|
}
|
2016-03-25 16:14:22 +00:00
|
|
|
if x < 0 {
|
|
|
|
|
x = 0
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 18:26:45 +00:00
|
|
|
x, y = v.GetSoftWrapLocation(x, y)
|
|
|
|
|
// x = v.Cursor.GetCharPosInLine(y, x)
|
2016-06-07 15:43:28 +00:00
|
|
|
if x > Count(v.Buf.Line(y)) {
|
|
|
|
|
x = Count(v.Buf.Line(y))
|
2016-03-25 16:14:22 +00:00
|
|
|
}
|
2016-05-28 21:29:49 +00:00
|
|
|
v.Cursor.X = x
|
|
|
|
|
v.Cursor.Y = y
|
|
|
|
|
v.Cursor.LastVisualX = v.Cursor.GetVisualX()
|
2016-03-25 16:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 16:42:23 +00:00
|
|
|
// Execute actions executes the supplied actions
|
2017-06-11 21:49:59 +00:00
|
|
|
func (v *View) ExecuteActions(actions []func(*View, bool) bool) bool {
|
|
|
|
|
relocate := false
|
|
|
|
|
readonlyBindingsList := []string{"Delete", "Insert", "Backspace", "Cut", "Play", "Paste", "Move", "Add", "DuplicateLine", "Macro"}
|
|
|
|
|
for _, action := range actions {
|
|
|
|
|
readonlyBindingsResult := false
|
|
|
|
|
funcName := ShortFuncName(action)
|
|
|
|
|
if v.Type.readonly == true {
|
|
|
|
|
// check for readonly and if true only let key bindings get called if they do not change the contents.
|
|
|
|
|
for _, readonlyBindings := range readonlyBindingsList {
|
|
|
|
|
if strings.Contains(funcName, readonlyBindings) {
|
|
|
|
|
readonlyBindingsResult = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !readonlyBindingsResult {
|
|
|
|
|
// call the key binding
|
|
|
|
|
relocate = action(v, true) || relocate
|
|
|
|
|
// Macro
|
|
|
|
|
if funcName != "ToggleMacro" && funcName != "PlayMacro" {
|
|
|
|
|
if recordingMacro {
|
|
|
|
|
curMacro = append(curMacro, action)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return relocate
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 16:42:23 +00:00
|
|
|
// SetCursor sets the view's and buffer's cursor
|
2017-09-04 19:47:24 +00:00
|
|
|
func (v *View) SetCursor(c *Cursor) bool {
|
|
|
|
|
if c == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-06-17 02:19:33 +00:00
|
|
|
v.Cursor = c
|
|
|
|
|
v.Buf.curCursor = c.Num
|
2017-09-04 19:47:24 +00:00
|
|
|
|
|
|
|
|
return true
|
2017-06-17 02:19:33 +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-05-29 21:58:06 +00:00
|
|
|
v.Buf.CheckModTime()
|
|
|
|
|
|
2016-03-17 21:27:57 +00:00
|
|
|
switch e := event.(type) {
|
2017-10-06 18:03:35 +00:00
|
|
|
case *tcell.EventRaw:
|
|
|
|
|
for key, actions := range bindings {
|
|
|
|
|
if key.keyCode == -1 {
|
|
|
|
|
if e.EscapeCode() == key.escape {
|
|
|
|
|
for _, c := range v.Buf.cursors {
|
|
|
|
|
ok := v.SetCursor(c)
|
|
|
|
|
if !ok {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
relocate = false
|
|
|
|
|
relocate = v.ExecuteActions(actions) || relocate
|
|
|
|
|
}
|
|
|
|
|
v.SetCursor(&v.Buf.Cursor)
|
|
|
|
|
v.Buf.MergeCursors()
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-17 21:27:57 +00:00
|
|
|
case *tcell.EventKey:
|
2016-09-05 14:03:05 +00:00
|
|
|
// Check first if input is a key binding, if it is we 'eat' the input and don't insert a rune
|
|
|
|
|
isBinding := false
|
2017-06-12 00:40:11 +00:00
|
|
|
for key, actions := range bindings {
|
|
|
|
|
if e.Key() == key.keyCode {
|
|
|
|
|
if e.Key() == tcell.KeyRune {
|
|
|
|
|
if e.Rune() != key.r {
|
|
|
|
|
continue
|
2016-04-27 18:37:58 +00:00
|
|
|
}
|
2016-04-23 22:25:08 +00:00
|
|
|
}
|
2017-06-12 00:40:11 +00:00
|
|
|
if e.Modifiers() == key.modifiers {
|
2017-06-12 16:10:22 +00:00
|
|
|
for _, c := range v.Buf.cursors {
|
2017-09-04 19:47:24 +00:00
|
|
|
ok := v.SetCursor(c)
|
|
|
|
|
if !ok {
|
|
|
|
|
break
|
|
|
|
|
}
|
2017-06-12 16:10:22 +00:00
|
|
|
relocate = false
|
|
|
|
|
isBinding = true
|
|
|
|
|
relocate = v.ExecuteActions(actions) || relocate
|
|
|
|
|
}
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(&v.Buf.Cursor)
|
2017-06-17 14:43:14 +00:00
|
|
|
v.Buf.MergeCursors()
|
2017-06-12 00:40:11 +00:00
|
|
|
break
|
|
|
|
|
}
|
2016-04-23 22:25:08 +00:00
|
|
|
}
|
2016-03-17 21:27:57 +00:00
|
|
|
}
|
2016-09-05 14:03:05 +00:00
|
|
|
if !isBinding && e.Key() == tcell.KeyRune {
|
2017-05-29 18:18:10 +00:00
|
|
|
// Check viewtype if readonly don't insert a rune (readonly help and log view etc.)
|
|
|
|
|
if v.Type.readonly == false {
|
2017-06-12 16:10:22 +00:00
|
|
|
for _, c := range v.Buf.cursors {
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(c)
|
2016-09-05 14:03:05 +00:00
|
|
|
|
2017-06-12 16:10:22 +00:00
|
|
|
// Insert a character
|
|
|
|
|
if v.Cursor.HasSelection() {
|
|
|
|
|
v.Cursor.DeleteSelection()
|
|
|
|
|
v.Cursor.ResetSelection()
|
2017-05-29 18:18:10 +00:00
|
|
|
}
|
2017-06-12 16:10:22 +00:00
|
|
|
v.Buf.Insert(v.Cursor.Loc, string(e.Rune()))
|
2016-09-07 20:57:42 +00:00
|
|
|
|
2017-06-12 16:10:22 +00:00
|
|
|
for pl := range loadedPlugins {
|
|
|
|
|
_, err := Call(pl+".onRune", string(e.Rune()), v)
|
|
|
|
|
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
|
|
|
|
|
TermMessage(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if recordingMacro {
|
|
|
|
|
curMacro = append(curMacro, e.Rune())
|
|
|
|
|
}
|
2017-05-29 18:18:10 +00:00
|
|
|
}
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(&v.Buf.Cursor)
|
2016-09-07 20:57:42 +00:00
|
|
|
}
|
2016-09-05 14:03:05 +00:00
|
|
|
}
|
2016-05-02 15:16:03 +00:00
|
|
|
case *tcell.EventPaste:
|
2017-05-29 18:18:10 +00:00
|
|
|
// Check viewtype if readonly don't paste (readonly help and log view etc.)
|
|
|
|
|
if v.Type.readonly == false {
|
|
|
|
|
if !PreActionCall("Paste", v) {
|
|
|
|
|
break
|
|
|
|
|
}
|
2016-08-31 14:47:31 +00:00
|
|
|
|
2017-06-12 16:10:22 +00:00
|
|
|
for _, c := range v.Buf.cursors {
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(c)
|
2017-06-12 16:10:22 +00:00
|
|
|
v.paste(e.Text())
|
|
|
|
|
}
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(&v.Buf.Cursor)
|
2016-08-31 14:47:31 +00:00
|
|
|
|
2017-05-29 18:18:10 +00:00
|
|
|
PostActionCall("Paste", v)
|
|
|
|
|
}
|
2016-03-17 21:27:57 +00:00
|
|
|
case *tcell.EventMouse:
|
2016-05-17 17:39:27 +00:00
|
|
|
// Don't relocate for mouse events
|
2016-05-17 17:31:36 +00:00
|
|
|
relocate = false
|
2016-03-17 21:27:57 +00:00
|
|
|
|
|
|
|
|
button := e.Buttons()
|
|
|
|
|
|
2017-06-11 21:49:59 +00:00
|
|
|
for key, actions := range bindings {
|
2017-06-12 20:58:39 +00:00
|
|
|
if button == key.buttons && e.Modifiers() == key.modifiers {
|
2017-06-12 16:10:22 +00:00
|
|
|
for _, c := range v.Buf.cursors {
|
2017-09-04 19:47:24 +00:00
|
|
|
ok := v.SetCursor(c)
|
|
|
|
|
if !ok {
|
|
|
|
|
break
|
|
|
|
|
}
|
2017-06-12 16:10:22 +00:00
|
|
|
relocate = v.ExecuteActions(actions) || relocate
|
|
|
|
|
}
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(&v.Buf.Cursor)
|
2017-06-17 14:43:14 +00:00
|
|
|
v.Buf.MergeCursors()
|
2016-03-17 21:27:57 +00:00
|
|
|
}
|
2017-06-11 21:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for key, actions := range mouseBindings {
|
2017-06-12 20:58:39 +00:00
|
|
|
if button == key.buttons && e.Modifiers() == key.modifiers {
|
2017-06-11 21:49:59 +00:00
|
|
|
for _, action := range actions {
|
|
|
|
|
action(v, true, e)
|
|
|
|
|
}
|
2017-05-29 18:18:10 +00:00
|
|
|
}
|
2017-06-11 21:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch button {
|
2016-03-17 21:27:57 +00:00
|
|
|
case tcell.ButtonNone:
|
2016-03-24 00:58:42 +00:00
|
|
|
// Mouse event with no click
|
2016-03-25 16:14:22 +00:00
|
|
|
if !v.mouseReleased {
|
|
|
|
|
// Mouse was just released
|
|
|
|
|
|
2017-06-12 16:10:22 +00:00
|
|
|
x, y := e.Position()
|
|
|
|
|
x -= v.lineNumOffset - v.leftCol + v.x
|
|
|
|
|
y += v.Topline - v.y
|
|
|
|
|
|
2016-03-25 16:14:22 +00:00
|
|
|
// 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)
|
2016-09-05 01:19:14 +00:00
|
|
|
v.Cursor.SetSelectionEnd(v.Cursor.Loc)
|
2017-01-09 23:28:45 +00:00
|
|
|
v.Cursor.CopySelection("primary")
|
2016-03-27 20:35:54 +00:00
|
|
|
}
|
2016-03-25 16:14:22 +00:00
|
|
|
v.mouseReleased = true
|
|
|
|
|
}
|
2016-03-17 21:27:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 16:14:22 +00:00
|
|
|
if relocate {
|
|
|
|
|
v.Relocate()
|
2017-03-26 22:01:02 +00:00
|
|
|
// We run relocate again because there's a bug with relocating with softwrap
|
|
|
|
|
// when for example you jump to the bottom of the buffer and it tries to
|
|
|
|
|
// calculate where to put the topline so that the bottom line is at the bottom
|
|
|
|
|
// of the terminal and it runs into problems with visual lines vs real lines.
|
|
|
|
|
// This is (hopefully) a temporary solution
|
|
|
|
|
v.Relocate()
|
2016-03-17 21:27:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-17 15:05:23 +00:00
|
|
|
func (v *View) mainCursor() bool {
|
|
|
|
|
return v.Buf.curCursor == len(v.Buf.cursors)-1
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-27 16:33:33 +00:00
|
|
|
// GutterMessage creates a message in this view's gutter
|
2016-04-29 00:43:45 +00:00
|
|
|
func (v *View) GutterMessage(section string, lineN int, msg string, kind int) {
|
2016-04-27 18:12:32 +00:00
|
|
|
lineN--
|
2016-04-27 15:22:57 +00:00
|
|
|
gutterMsg := GutterMessage{
|
|
|
|
|
lineNum: lineN,
|
|
|
|
|
msg: msg,
|
|
|
|
|
kind: kind,
|
|
|
|
|
}
|
2016-04-29 00:43:45 +00:00
|
|
|
for _, v := range v.messages {
|
|
|
|
|
for _, gmsg := range v {
|
|
|
|
|
if gmsg.lineNum == lineN {
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-04-27 15:22:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-29 00:43:45 +00:00
|
|
|
messages := v.messages[section]
|
|
|
|
|
v.messages[section] = append(messages, gutterMsg)
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-30 18:30:42 +00:00
|
|
|
// ClearGutterMessages clears all gutter messages from a given section
|
2016-04-29 00:43:45 +00:00
|
|
|
func (v *View) ClearGutterMessages(section string) {
|
|
|
|
|
v.messages[section] = []GutterMessage{}
|
2016-04-27 15:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-05 15:31:53 +00:00
|
|
|
// ClearAllGutterMessages clears all the gutter messages
|
|
|
|
|
func (v *View) ClearAllGutterMessages() {
|
|
|
|
|
for k := range v.messages {
|
|
|
|
|
v.messages[k] = []GutterMessage{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 19:52:31 +00:00
|
|
|
// Opens the given help page in a new horizontal split
|
|
|
|
|
func (v *View) openHelp(helpPage string) {
|
2016-09-16 20:15:44 +00:00
|
|
|
if data, err := FindRuntimeFile(RTHelp, helpPage).Data(); err != nil {
|
2016-09-13 06:53:20 +00:00
|
|
|
TermMessage("Unable to load help text", helpPage, "\n", err)
|
2016-08-11 21:30:48 +00:00
|
|
|
} else {
|
2017-04-29 18:12:00 +00:00
|
|
|
helpBuffer := NewBufferFromString(string(data), helpPage+".md")
|
2016-11-20 00:07:51 +00:00
|
|
|
helpBuffer.name = "Help"
|
2016-09-13 06:53:20 +00:00
|
|
|
|
2016-09-26 17:08:37 +00:00
|
|
|
if v.Type == vtHelp {
|
2016-09-13 06:53:20 +00:00
|
|
|
v.OpenBuffer(helpBuffer)
|
|
|
|
|
} else {
|
|
|
|
|
v.HSplit(helpBuffer)
|
2016-09-26 17:08:37 +00:00
|
|
|
CurView().Type = vtHelp
|
2016-09-13 06:53:20 +00:00
|
|
|
}
|
2016-08-11 21:30:48 +00:00
|
|
|
}
|
2016-07-28 19:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 16:42:23 +00:00
|
|
|
// DisplayView draws the view to the screen
|
2017-02-25 02:29:22 +00:00
|
|
|
func (v *View) DisplayView() {
|
2017-05-03 00:12:37 +00:00
|
|
|
if v.Buf.Settings["softwrap"].(bool) && v.leftCol != 0 {
|
|
|
|
|
v.leftCol = 0
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-25 02:29:22 +00:00
|
|
|
if v.Type == vtLog {
|
|
|
|
|
// Log views should always follow the cursor...
|
|
|
|
|
v.Relocate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We need to know the string length of the largest line number
|
|
|
|
|
// so we can pad appropriately when displaying line numbers
|
|
|
|
|
maxLineNumLength := len(strconv.Itoa(v.Buf.NumLines))
|
|
|
|
|
|
|
|
|
|
if v.Buf.Settings["ruler"] == true {
|
|
|
|
|
// + 1 for the little space after the line number
|
|
|
|
|
v.lineNumOffset = maxLineNumLength + 1
|
|
|
|
|
} else {
|
|
|
|
|
v.lineNumOffset = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We need to add to the line offset if there are gutter messages
|
|
|
|
|
var hasGutterMessages bool
|
|
|
|
|
for _, v := range v.messages {
|
|
|
|
|
if len(v) > 0 {
|
|
|
|
|
hasGutterMessages = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if hasGutterMessages {
|
|
|
|
|
v.lineNumOffset += 2
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-05 14:40:49 +00:00
|
|
|
divider := 0
|
2017-02-25 02:29:22 +00:00
|
|
|
if v.x != 0 {
|
|
|
|
|
// One space for the extra split divider
|
|
|
|
|
v.lineNumOffset++
|
2017-05-05 14:40:49 +00:00
|
|
|
divider = 1
|
2017-02-25 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xOffset := v.x + v.lineNumOffset
|
|
|
|
|
yOffset := v.y
|
|
|
|
|
|
|
|
|
|
height := v.Height
|
|
|
|
|
width := v.Width
|
|
|
|
|
left := v.leftCol
|
|
|
|
|
top := v.Topline
|
|
|
|
|
|
2017-03-22 16:28:02 +00:00
|
|
|
v.cellview.Draw(v.Buf, top, height, left, width-v.lineNumOffset)
|
2017-02-25 02:29:22 +00:00
|
|
|
|
|
|
|
|
screenX := v.x
|
|
|
|
|
realLineN := top - 1
|
|
|
|
|
visualLineN := 0
|
|
|
|
|
var line []*Char
|
|
|
|
|
for visualLineN, line = range v.cellview.lines {
|
|
|
|
|
var firstChar *Char
|
|
|
|
|
if len(line) > 0 {
|
|
|
|
|
firstChar = line[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var softwrapped bool
|
|
|
|
|
if firstChar != nil {
|
|
|
|
|
if firstChar.realLoc.Y == realLineN {
|
|
|
|
|
softwrapped = true
|
|
|
|
|
}
|
|
|
|
|
realLineN = firstChar.realLoc.Y
|
|
|
|
|
} else {
|
|
|
|
|
realLineN++
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-18 17:33:19 +00:00
|
|
|
colorcolumn := int(v.Buf.Settings["colorcolumn"].(float64))
|
|
|
|
|
if colorcolumn != 0 {
|
|
|
|
|
style := GetColor("color-column")
|
|
|
|
|
fg, _, _ := style.Decompose()
|
|
|
|
|
st := defStyle.Background(fg)
|
|
|
|
|
screen.SetContent(xOffset+colorcolumn, yOffset+visualLineN, ' ', nil, st)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-25 02:29:22 +00:00
|
|
|
screenX = v.x
|
|
|
|
|
|
|
|
|
|
// If there are gutter messages we need to display the '>>' symbol here
|
|
|
|
|
if hasGutterMessages {
|
|
|
|
|
// msgOnLine stores whether or not there is a gutter message on this line in particular
|
|
|
|
|
msgOnLine := false
|
|
|
|
|
for k := range v.messages {
|
|
|
|
|
for _, msg := range v.messages[k] {
|
|
|
|
|
if msg.lineNum == realLineN {
|
|
|
|
|
msgOnLine = true
|
|
|
|
|
gutterStyle := defStyle
|
|
|
|
|
switch msg.kind {
|
|
|
|
|
case GutterInfo:
|
|
|
|
|
if style, ok := colorscheme["gutter-info"]; ok {
|
|
|
|
|
gutterStyle = style
|
|
|
|
|
}
|
|
|
|
|
case GutterWarning:
|
|
|
|
|
if style, ok := colorscheme["gutter-warning"]; ok {
|
|
|
|
|
gutterStyle = style
|
|
|
|
|
}
|
|
|
|
|
case GutterError:
|
|
|
|
|
if style, ok := colorscheme["gutter-error"]; ok {
|
|
|
|
|
gutterStyle = style
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-25 21:58:49 +00:00
|
|
|
screen.SetContent(screenX, yOffset+visualLineN, '>', nil, gutterStyle)
|
2017-02-25 02:29:22 +00:00
|
|
|
screenX++
|
2017-02-25 21:58:49 +00:00
|
|
|
screen.SetContent(screenX, yOffset+visualLineN, '>', nil, gutterStyle)
|
2017-02-25 02:29:22 +00:00
|
|
|
screenX++
|
|
|
|
|
if v.Cursor.Y == realLineN && !messenger.hasPrompt {
|
|
|
|
|
messenger.Message(msg.msg)
|
|
|
|
|
messenger.gutterMessage = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If there is no message on this line we just display an empty offset
|
|
|
|
|
if !msgOnLine {
|
2017-02-25 21:58:49 +00:00
|
|
|
screen.SetContent(screenX, yOffset+visualLineN, ' ', nil, defStyle)
|
2017-02-25 02:29:22 +00:00
|
|
|
screenX++
|
2017-02-25 21:58:49 +00:00
|
|
|
screen.SetContent(screenX, yOffset+visualLineN, ' ', nil, defStyle)
|
2017-02-25 02:29:22 +00:00
|
|
|
screenX++
|
|
|
|
|
if v.Cursor.Y == realLineN && messenger.gutterMessage {
|
|
|
|
|
messenger.Reset()
|
|
|
|
|
messenger.gutterMessage = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lineNumStyle := defStyle
|
|
|
|
|
if v.Buf.Settings["ruler"] == true {
|
|
|
|
|
// Write the line number
|
|
|
|
|
if style, ok := colorscheme["line-number"]; ok {
|
|
|
|
|
lineNumStyle = style
|
|
|
|
|
}
|
|
|
|
|
if style, ok := colorscheme["current-line-number"]; ok {
|
|
|
|
|
if realLineN == v.Cursor.Y && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() {
|
|
|
|
|
lineNumStyle = style
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lineNum := strconv.Itoa(realLineN + 1)
|
|
|
|
|
|
|
|
|
|
// Write the spaces before the line number if necessary
|
|
|
|
|
for i := 0; i < maxLineNumLength-len(lineNum); i++ {
|
2017-05-05 14:40:49 +00:00
|
|
|
screen.SetContent(screenX+divider, yOffset+visualLineN, ' ', nil, lineNumStyle)
|
2017-02-25 02:29:22 +00:00
|
|
|
screenX++
|
|
|
|
|
}
|
|
|
|
|
if softwrapped && visualLineN != 0 {
|
|
|
|
|
// Pad without the line number because it was written on the visual line before
|
|
|
|
|
for range lineNum {
|
2017-05-05 14:40:49 +00:00
|
|
|
screen.SetContent(screenX+divider, yOffset+visualLineN, ' ', nil, lineNumStyle)
|
2017-02-25 02:29:22 +00:00
|
|
|
screenX++
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Write the actual line number
|
|
|
|
|
for _, ch := range lineNum {
|
2017-05-05 14:40:49 +00:00
|
|
|
screen.SetContent(screenX+divider, yOffset+visualLineN, ch, nil, lineNumStyle)
|
2017-02-25 02:29:22 +00:00
|
|
|
screenX++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write the extra space
|
2017-05-05 14:40:49 +00:00
|
|
|
screen.SetContent(screenX+divider, yOffset+visualLineN, ' ', nil, lineNumStyle)
|
2017-02-25 02:29:22 +00:00
|
|
|
screenX++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lastChar *Char
|
2017-03-13 23:23:47 +00:00
|
|
|
cursorSet := false
|
2017-02-25 02:29:22 +00:00
|
|
|
for _, char := range line {
|
|
|
|
|
if char != nil {
|
|
|
|
|
lineStyle := char.style
|
|
|
|
|
|
2017-04-18 17:33:19 +00:00
|
|
|
colorcolumn := int(v.Buf.Settings["colorcolumn"].(float64))
|
|
|
|
|
if colorcolumn != 0 && char.visualLoc.X == colorcolumn {
|
|
|
|
|
style := GetColor("color-column")
|
|
|
|
|
fg, _, _ := style.Decompose()
|
|
|
|
|
lineStyle = lineStyle.Background(fg)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-25 02:29:22 +00:00
|
|
|
charLoc := char.realLoc
|
2017-06-12 16:10:22 +00:00
|
|
|
for _, c := range v.Buf.cursors {
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(c)
|
2017-06-12 16:10:22 +00:00
|
|
|
if v.Cursor.HasSelection() &&
|
|
|
|
|
(charLoc.GreaterEqual(v.Cursor.CurSelection[0]) && charLoc.LessThan(v.Cursor.CurSelection[1]) ||
|
|
|
|
|
charLoc.LessThan(v.Cursor.CurSelection[0]) && charLoc.GreaterEqual(v.Cursor.CurSelection[1])) {
|
|
|
|
|
// The current character is selected
|
|
|
|
|
lineStyle = defStyle.Reverse(true)
|
|
|
|
|
|
|
|
|
|
if style, ok := colorscheme["selection"]; ok {
|
|
|
|
|
lineStyle = style
|
|
|
|
|
}
|
2017-02-25 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(&v.Buf.Cursor)
|
2017-02-25 02:29:22 +00:00
|
|
|
|
|
|
|
|
if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num &&
|
|
|
|
|
!v.Cursor.HasSelection() && v.Cursor.Y == realLineN {
|
|
|
|
|
style := GetColor("cursor-line")
|
|
|
|
|
fg, _, _ := style.Decompose()
|
|
|
|
|
lineStyle = lineStyle.Background(fg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
screen.SetContent(xOffset+char.visualLoc.X, yOffset+char.visualLoc.Y, char.drawChar, nil, lineStyle)
|
|
|
|
|
|
2017-06-12 19:59:00 +00:00
|
|
|
for i, c := range v.Buf.cursors {
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(c)
|
2017-06-12 16:10:22 +00:00
|
|
|
if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
|
2017-06-12 19:59:00 +00:00
|
|
|
v.Cursor.Y == char.realLoc.Y && v.Cursor.X == char.realLoc.X && (!cursorSet || i != 0) {
|
|
|
|
|
ShowMultiCursor(xOffset+char.visualLoc.X, yOffset+char.visualLoc.Y, i)
|
|
|
|
|
cursorSet = true
|
2017-06-12 16:10:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(&v.Buf.Cursor)
|
2017-06-12 16:10:22 +00:00
|
|
|
|
2017-02-25 02:29:22 +00:00
|
|
|
lastChar = char
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastX := 0
|
|
|
|
|
var realLoc Loc
|
|
|
|
|
var visualLoc Loc
|
2017-04-14 01:37:13 +00:00
|
|
|
var cx, cy int
|
2017-02-25 02:29:22 +00:00
|
|
|
if lastChar != nil {
|
2017-03-13 23:23:47 +00:00
|
|
|
lastX = xOffset + lastChar.visualLoc.X + lastChar.width
|
2017-06-12 19:59:00 +00:00
|
|
|
for i, c := range v.Buf.cursors {
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(c)
|
2017-06-12 16:10:22 +00:00
|
|
|
if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
|
|
|
|
|
v.Cursor.Y == lastChar.realLoc.Y && v.Cursor.X == lastChar.realLoc.X+1 {
|
2017-06-12 19:59:00 +00:00
|
|
|
ShowMultiCursor(lastX, yOffset+lastChar.visualLoc.Y, i)
|
2017-06-12 16:10:22 +00:00
|
|
|
cx, cy = lastX, yOffset+lastChar.visualLoc.Y
|
|
|
|
|
}
|
2017-02-25 02:29:22 +00:00
|
|
|
}
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(&v.Buf.Cursor)
|
2017-06-12 19:49:21 +00:00
|
|
|
realLoc = Loc{lastChar.realLoc.X + 1, realLineN}
|
2017-02-25 02:29:22 +00:00
|
|
|
visualLoc = Loc{lastX - xOffset, lastChar.visualLoc.Y}
|
|
|
|
|
} else if len(line) == 0 {
|
2017-06-12 19:59:00 +00:00
|
|
|
for i, c := range v.Buf.cursors {
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(c)
|
2017-06-12 16:10:22 +00:00
|
|
|
if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
|
|
|
|
|
v.Cursor.Y == realLineN {
|
2017-06-12 19:59:00 +00:00
|
|
|
ShowMultiCursor(xOffset, yOffset+visualLineN, i)
|
2017-06-12 16:10:22 +00:00
|
|
|
cx, cy = xOffset, yOffset+visualLineN
|
|
|
|
|
}
|
2017-02-25 02:29:22 +00:00
|
|
|
}
|
2017-06-17 02:19:33 +00:00
|
|
|
v.SetCursor(&v.Buf.Cursor)
|
2017-02-25 02:29:22 +00:00
|
|
|
lastX = xOffset
|
|
|
|
|
realLoc = Loc{0, realLineN}
|
|
|
|
|
visualLoc = Loc{0, visualLineN}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v.Cursor.HasSelection() &&
|
|
|
|
|
(realLoc.GreaterEqual(v.Cursor.CurSelection[0]) && realLoc.LessThan(v.Cursor.CurSelection[1]) ||
|
|
|
|
|
realLoc.LessThan(v.Cursor.CurSelection[0]) && realLoc.GreaterEqual(v.Cursor.CurSelection[1])) {
|
|
|
|
|
// The current character is selected
|
|
|
|
|
selectStyle := defStyle.Reverse(true)
|
|
|
|
|
|
|
|
|
|
if style, ok := colorscheme["selection"]; ok {
|
|
|
|
|
selectStyle = style
|
|
|
|
|
}
|
|
|
|
|
screen.SetContent(xOffset+visualLoc.X, yOffset+visualLoc.Y, ' ', nil, selectStyle)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num &&
|
|
|
|
|
!v.Cursor.HasSelection() && v.Cursor.Y == realLineN {
|
2017-09-02 08:40:35 +00:00
|
|
|
for i := lastX; i < xOffset+v.Width-v.lineNumOffset; i++ {
|
2017-02-25 02:29:22 +00:00
|
|
|
style := GetColor("cursor-line")
|
|
|
|
|
fg, _, _ := style.Decompose()
|
|
|
|
|
style = style.Background(fg)
|
2017-04-14 01:37:13 +00:00
|
|
|
if !(tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && i == cx && yOffset+visualLineN == cy) {
|
|
|
|
|
screen.SetContent(i, yOffset+visualLineN, ' ', nil, style)
|
|
|
|
|
}
|
2017-02-25 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-05 14:40:49 +00:00
|
|
|
if divider != 0 {
|
2017-03-27 00:40:03 +00:00
|
|
|
dividerStyle := defStyle
|
|
|
|
|
if style, ok := colorscheme["divider"]; ok {
|
|
|
|
|
dividerStyle = style
|
|
|
|
|
}
|
2017-05-05 14:40:49 +00:00
|
|
|
for i := 0; i < v.Height; i++ {
|
2017-03-27 00:40:03 +00:00
|
|
|
screen.SetContent(v.x, yOffset+i, '|', nil, dividerStyle.Reverse(true))
|
2017-02-25 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-25 16:14:22 +00:00
|
|
|
|
2017-06-12 20:19:17 +00:00
|
|
|
// ShowMultiCursor will display a cursor at a location
|
|
|
|
|
// If i == 0 then the terminal cursor will be used
|
|
|
|
|
// Otherwise a fake cursor will be drawn at the position
|
2017-06-12 19:59:00 +00:00
|
|
|
func ShowMultiCursor(x, y, i int) {
|
|
|
|
|
if i == 0 {
|
|
|
|
|
screen.ShowCursor(x, y)
|
|
|
|
|
} else {
|
|
|
|
|
r, _, _, _ := screen.GetContent(x, y)
|
|
|
|
|
screen.SetContent(x, y, r, nil, defStyle.Reverse(true))
|
|
|
|
|
}
|
2017-06-12 16:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-25 16:14:22 +00:00
|
|
|
// Display renders the view, the cursor, and statusline
|
|
|
|
|
func (v *View) Display() {
|
2017-05-14 14:27:15 +00:00
|
|
|
if globalSettings["termtitle"].(bool) {
|
2017-04-29 20:01:09 +00:00
|
|
|
screen.SetTitle("micro: " + v.Buf.GetName())
|
|
|
|
|
}
|
2016-03-25 16:14:22 +00:00
|
|
|
v.DisplayView()
|
2016-10-12 20:24:00 +00:00
|
|
|
// Don't draw the cursor if it is out of the viewport or if it has a selection
|
2017-05-28 23:52:56 +00:00
|
|
|
if v.Num == tabs[curTab].CurView && (v.Cursor.Y-v.Topline < 0 || v.Cursor.Y-v.Topline > v.Height-1 || v.Cursor.HasSelection()) {
|
2016-10-12 20:24:00 +00:00
|
|
|
screen.HideCursor()
|
2016-07-01 22:12:37 +00:00
|
|
|
}
|
2016-07-15 21:38:42 +00:00
|
|
|
_, screenH := screen.Size()
|
2016-08-24 23:55:44 +00:00
|
|
|
if v.Buf.Settings["statusline"].(bool) {
|
2016-05-15 17:44:07 +00:00
|
|
|
v.sline.Display()
|
2016-11-29 01:28:40 +00:00
|
|
|
} 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))
|
2016-07-15 21:38:42 +00:00
|
|
|
}
|
2016-05-15 17:44:07 +00:00
|
|
|
}
|
2016-03-25 16:14:22 +00:00
|
|
|
}
|