9 "github.com/gdamore/tcell"
12 // The View struct stores information about a view into a buffer.
13 // It has a stores information about the cursor, and the viewport
14 // that the user sees the buffer from.
18 // The topmost line, used for vertical scrolling
20 // The leftmost column, used for horizontal scrolling
23 // Percentage of the terminal window that this view takes up (from 0 to 100)
27 // Actual with and height
31 // How much to offset because of line numbers
34 // The eventhandler for undo/redo
42 // Since tcell doesn't differentiate between a mouse release event
43 // and a mouse move event with no keys pressed, we need to keep
44 // track of whether or not the mouse was pressed (or not released) last event to determine
45 // mouse release events
48 // This stores when the last click was
49 // This is useful for detecting double and triple clicks
50 lastClickTime time.Time
52 // Was the last mouse event actually a double click?
53 // Useful for detecting triple clicks -- if a double click is detected
54 // but the last mouse event was actually a double click, it's a triple click
56 // Same here, just to keep track for mouse move events
59 // Syntax highlighting matches
61 // The matches from the last frame
62 lastMatches SyntaxMatches
65 // NewView returns a new fullscreen view
66 func NewView(buf *Buffer) *View {
67 return NewViewWidthHeight(buf, 100, 100)
70 // NewViewWidthHeight returns a new view with the specified width and height percentages
71 // Note that w and h are percentages not actual values
72 func NewViewWidthHeight(buf *Buffer, w, h int) *View {
77 v.Resize(screen.Size())
81 v.eh = NewEventHandler(v)
90 // Resize recalculates the actual width and height of the view from the width and height
92 // This is usually called when the window is resized, or when a split has been added and
93 // the percentages have changed
94 func (v *View) Resize(w, h int) {
95 // Always include 1 line for the command line at the bottom
97 v.width = int(float32(w) * float32(v.widthPercent) / 100)
98 // We subtract 1 for the statusline
99 v.height = int(float32(h)*float32(v.heightPercent)/100) - 1
102 // ScrollUp scrolls the view up n lines (if possible)
103 func (v *View) ScrollUp(n int) {
104 // Try to scroll by n but if it would overflow, scroll by 1
105 if v.topline-n >= 0 {
107 } else if v.topline > 0 {
112 // ScrollDown scrolls the view down n lines (if possible)
113 func (v *View) ScrollDown(n int) {
114 // Try to scroll by n but if it would overflow, scroll by 1
115 if v.topline+n <= len(v.buf.lines)-v.height {
117 } else if v.topline < len(v.buf.lines)-v.height {
122 // CanClose returns whether or not the view can be closed
123 // If there are unsaved changes, the user will be asked if the view can be closed
124 // causing them to lose the unsaved changes
125 // The message is what to print after saying "You have unsaved changes. "
126 func (v *View) CanClose(msg string) bool {
128 quit, canceled := messenger.Prompt("You have unsaved changes. " + msg)
130 if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
132 } else if strings.ToLower(quit) == "save" || strings.ToLower(quit) == "s" {
143 // OpenBuffer opens a new buffer in this view.
144 // This resets the topline, event handler and cursor.
145 func (v *View) OpenBuffer(buf *Buffer) {
149 // Put the cursor at the first spot
155 v.cursor.ResetSelection()
157 v.eh = NewEventHandler(v)
160 // Set mouseReleased to true because we assume the mouse is not being pressed when
161 // the editor is opened
162 v.mouseReleased = true
163 v.lastClickTime = time.Time{}
166 // Close and Re-open the current file.
167 func (v *View) reOpen() {
168 if v.CanClose("Continue? (yes, no, save) ") {
169 file, err := ioutil.ReadFile(v.buf.path)
170 filename := v.buf.name
173 messenger.Error(err.Error())
176 buf := NewBuffer(string(file), filename)
183 // OpenFile opens a new file in the current view
184 // It makes sure that the current buffer can be closed first (unsaved changes)
185 func (v *View) OpenFile() {
188 // Relocate moves the view window so that the cursor is in view
189 // This is useful if the user has scrolled far away, and then starts typing
190 func (v *View) Relocate() bool {
197 if cy > v.topline+v.height-1 {
198 v.topline = cy - v.height + 1
202 cx := v.cursor.GetVisualX()
207 if cx+v.lineNumOffset+1 > v.leftCol+v.width {
208 v.leftCol = cx - v.width + v.lineNumOffset + 1
214 // MoveToMouseClick moves the cursor to location x, y assuming x, y were given
216 func (v *View) MoveToMouseClick(x, y int) {
217 if y-v.topline > v.height-1 {
219 y = v.height + v.topline - 1
221 if y >= len(v.buf.lines) {
222 y = len(v.buf.lines) - 1
231 x = v.cursor.GetCharPosInLine(y, x)
232 if x > Count(v.buf.lines[y]) {
233 x = Count(v.buf.lines[y])
237 v.cursor.lastVisualX = v.cursor.GetVisualX()
240 // HandleEvent handles an event passed by the main loop
241 func (v *View) HandleEvent(event tcell.Event) {
242 // This bool determines whether the view is relocated at the end of the function
243 // By default it's true because most events should cause a relocate
246 switch e := event.(type) {
247 case *tcell.EventResize:
250 case *tcell.EventKey:
251 if e.Key() == tcell.KeyRune {
252 // Insert a character
253 if v.cursor.HasSelection() {
254 v.cursor.DeleteSelection()
255 v.cursor.ResetSelection()
257 v.eh.Insert(v.cursor.Loc(), string(e.Rune()))
260 for key, action := range bindings {
266 case *tcell.EventMouse:
268 x -= v.lineNumOffset - v.leftCol
271 button := e.Buttons()
276 origX, origY := v.cursor.x, v.cursor.y
277 v.MoveToMouseClick(x, y)
280 if (time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold) &&
281 (origX == v.cursor.x && origY == v.cursor.y) {
284 v.lastClickTime = time.Now()
287 v.doubleClick = false
289 v.cursor.SelectLine()
292 v.lastClickTime = time.Now()
295 v.tripleClick = false
297 v.cursor.SelectWord()
300 v.doubleClick = false
301 v.tripleClick = false
302 v.lastClickTime = time.Now()
304 loc := v.cursor.Loc()
305 v.cursor.curSelection[0] = loc
306 v.cursor.curSelection[1] = loc
310 v.cursor.AddLineToSelection()
311 } else if v.doubleClick {
312 v.cursor.AddWordToSelection()
314 v.cursor.curSelection[1] = v.cursor.Loc()
317 v.mouseReleased = false
318 case tcell.ButtonNone:
319 // Mouse event with no click
320 if !v.mouseReleased {
321 // Mouse was just released
323 // Relocating here isn't really necessary because the cursor will
324 // be in the right place from the last mouse event
325 // However, if we are running in a terminal that doesn't support mouse motion
326 // events, this still allows the user to make selections, except only after they
329 if !v.doubleClick && !v.tripleClick {
330 v.MoveToMouseClick(x, y)
331 v.cursor.curSelection[1] = v.cursor.Loc()
333 v.mouseReleased = true
335 // We don't want to relocate because otherwise the view will be relocated
336 // every time the user moves the cursor
339 // Scroll up two lines
341 // We don't want to relocate if the user is scrolling
343 case tcell.WheelDown:
344 // Scroll down two lines
346 // We don't want to relocate if the user is scrolling
359 // DisplayView renders the view to the screen
360 func (v *View) DisplayView() {
361 // The character number of the character in the top left of the screen
362 charNum := ToCharPos(0, v.topline, v.buf)
364 // Convert the length of buffer to a string, and get the length of the string
365 // We are going to have to offset by that amount
366 maxLineLength := len(strconv.Itoa(len(v.buf.lines)))
367 // + 1 for the little space after the line number
368 if settings.Ruler == true {
369 v.lineNumOffset = maxLineLength + 1
373 var highlightStyle tcell.Style
375 for lineN := 0; lineN < v.height; lineN++ {
377 // If the buffer is smaller than the view height
378 // and we went too far, break
379 if lineN+v.topline >= len(v.buf.lines) {
382 line := v.buf.lines[lineN+v.topline]
384 // Write the line number
385 lineNumStyle := defStyle
386 if style, ok := colorscheme["line-number"]; ok {
389 // Write the spaces before the line number if necessary
391 if settings.Ruler == true {
392 lineNum = strconv.Itoa(lineN + v.topline + 1)
393 for i := 0; i < maxLineLength-len(lineNum); i++ {
394 screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
397 // Write the actual line number
398 for _, ch := range lineNum {
399 screen.SetContent(x, lineN, ch, nil, lineNumStyle)
403 if settings.Ruler == true {
404 // Write the extra space
405 screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
411 for colN, ch := range line {
412 var lineStyle tcell.Style
415 // Syntax highlighting is enabled
416 highlightStyle = v.matches[lineN][colN]
419 if v.cursor.HasSelection() &&
420 (charNum >= v.cursor.curSelection[0] && charNum < v.cursor.curSelection[1] ||
421 charNum < v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
423 lineStyle = tcell.StyleDefault.Reverse(true)
425 if style, ok := colorscheme["selection"]; ok {
429 lineStyle = highlightStyle
433 screen.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)
434 tabSize := settings.TabSize
435 for i := 0; i < tabSize-1; i++ {
437 if x-v.leftCol+tabchars >= v.lineNumOffset {
438 screen.SetContent(x-v.leftCol+tabchars, lineN, ' ', nil, lineStyle)
442 if x-v.leftCol+tabchars >= v.lineNumOffset {
443 screen.SetContent(x-v.leftCol+tabchars, lineN, ch, nil, lineStyle)
449 // Here we are at a newline
451 // The newline may be selected, in which case we should draw the selection style
452 // with a space to represent it
453 if v.cursor.HasSelection() &&
454 (charNum >= v.cursor.curSelection[0] && charNum < v.cursor.curSelection[1] ||
455 charNum < v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
457 selectStyle := defStyle.Reverse(true)
459 if style, ok := colorscheme["selection"]; ok {
462 screen.SetContent(x-v.leftCol+tabchars, lineN, ' ', nil, selectStyle)
469 // Display renders the view, the cursor, and statusline
470 func (v *View) Display() {