]> git.lizzy.rs Git - micro.git/blob - cmd/micro/view.go
Use text from the paste event, not the clipboard
[micro.git] / cmd / micro / view.go
1 package main
2
3 import (
4         "strconv"
5         "strings"
6         "time"
7
8         "github.com/mattn/go-runewidth"
9         "github.com/zyedidia/tcell"
10 )
11
12 // The View struct stores information about a view into a buffer.
13 // It stores information about the cursor, and the viewport
14 // that the user sees the buffer from.
15 type View struct {
16         // A pointer to the buffer's cursor for ease of access
17         Cursor *Cursor
18
19         // The topmost line, used for vertical scrolling
20         Topline int
21         // The leftmost column, used for horizontal scrolling
22         leftCol int
23
24         // Percentage of the terminal window that this view takes up (from 0 to 100)
25         widthPercent  int
26         heightPercent int
27
28         // Specifies whether or not this view holds a help buffer
29         Help bool
30
31         // Actual with and height
32         width  int
33         height int
34
35         // Where this view is located
36         x, y int
37
38         // How much to offset because of line numbers
39         lineNumOffset int
40
41         // Holds the list of gutter messages
42         messages map[string][]GutterMessage
43
44         // This is the index of this view in the views array
45         Num int
46         // What tab is this view stored in
47         TabNum int
48
49         // The buffer
50         Buf *Buffer
51         // The statusline
52         sline Statusline
53
54         // Since tcell doesn't differentiate between a mouse release event
55         // and a mouse move event with no keys pressed, we need to keep
56         // track of whether or not the mouse was pressed (or not released) last event to determine
57         // mouse release events
58         mouseReleased bool
59
60         // This stores when the last click was
61         // This is useful for detecting double and triple clicks
62         lastClickTime time.Time
63
64         // lastCutTime stores when the last ctrl+k was issued.
65         // It is used for clearing the clipboard to replace it with fresh cut lines.
66         lastCutTime time.Time
67
68         // freshClip returns true if the clipboard has never been pasted.
69         freshClip bool
70
71         // Was the last mouse event actually a double click?
72         // Useful for detecting triple clicks -- if a double click is detected
73         // but the last mouse event was actually a double click, it's a triple click
74         doubleClick bool
75         // Same here, just to keep track for mouse move events
76         tripleClick bool
77
78         // Syntax highlighting matches
79         matches SyntaxMatches
80
81         splitNode *LeafNode
82 }
83
84 // NewView returns a new fullscreen view
85 func NewView(buf *Buffer) *View {
86         screenW, screenH := screen.Size()
87         return NewViewWidthHeight(buf, screenW, screenH-1)
88 }
89
90 // NewViewWidthHeight returns a new view with the specified width and height
91 // Note that w and h are raw column and row values
92 func NewViewWidthHeight(buf *Buffer, w, h int) *View {
93         v := new(View)
94
95         v.x, v.y = 0, 0
96
97         v.width = w
98         v.height = h
99
100         v.ToggleTabbar()
101
102         v.OpenBuffer(buf)
103
104         v.messages = make(map[string][]GutterMessage)
105
106         v.sline = Statusline{
107                 view: v,
108         }
109
110         if v.Buf.Settings["statusline"].(bool) {
111                 v.height--
112         }
113
114         for _, pl := range loadedPlugins {
115                 _, err := Call(pl+".onViewOpen", v)
116                 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
117                         TermMessage(err)
118                         continue
119                 }
120         }
121
122         return v
123 }
124
125 func (v *View) ToggleStatusLine() {
126         if v.Buf.Settings["statusline"].(bool) {
127                 v.height--
128         } else {
129                 v.height++
130         }
131 }
132
133 func (v *View) ToggleTabbar() {
134         if len(tabs) > 1 {
135                 if v.y == 0 {
136                         // Include one line for the tab bar at the top
137                         v.height--
138                         v.y = 1
139                 }
140         } else {
141                 if v.y == 1 {
142                         v.y = 0
143                         v.height++
144                 }
145         }
146 }
147
148 // ScrollUp scrolls the view up n lines (if possible)
149 func (v *View) ScrollUp(n int) {
150         // Try to scroll by n but if it would overflow, scroll by 1
151         if v.Topline-n >= 0 {
152                 v.Topline -= n
153         } else if v.Topline > 0 {
154                 v.Topline--
155         }
156 }
157
158 // ScrollDown scrolls the view down n lines (if possible)
159 func (v *View) ScrollDown(n int) {
160         // Try to scroll by n but if it would overflow, scroll by 1
161         if v.Topline+n <= v.Buf.NumLines-v.height {
162                 v.Topline += n
163         } else if v.Topline < v.Buf.NumLines-v.height {
164                 v.Topline++
165         }
166 }
167
168 // CanClose returns whether or not the view can be closed
169 // If there are unsaved changes, the user will be asked if the view can be closed
170 // causing them to lose the unsaved changes
171 // The message is what to print after saying "You have unsaved changes. "
172 func (v *View) CanClose(msg string, responses ...rune) bool {
173         if v.Buf.IsModified {
174                 char, canceled := messenger.LetterPrompt("You have unsaved changes. "+msg, responses...)
175                 if !canceled {
176                         if char == 'y' {
177                                 return true
178                         } else if char == 's' {
179                                 v.Save(true)
180                                 return true
181                         }
182                 }
183         } else {
184                 return true
185         }
186         return false
187 }
188
189 // OpenBuffer opens a new buffer in this view.
190 // This resets the topline, event handler and cursor.
191 func (v *View) OpenBuffer(buf *Buffer) {
192         screen.Clear()
193         v.CloseBuffer()
194         v.Buf = buf
195         v.Cursor = &buf.Cursor
196         v.Topline = 0
197         v.leftCol = 0
198         v.Cursor.ResetSelection()
199         v.Relocate()
200         v.Center(false)
201         v.messages = make(map[string][]GutterMessage)
202
203         v.matches = Match(v)
204
205         // Set mouseReleased to true because we assume the mouse is not being pressed when
206         // the editor is opened
207         v.mouseReleased = true
208         v.lastClickTime = time.Time{}
209 }
210
211 // CloseBuffer performs any closing functions on the buffer
212 func (v *View) CloseBuffer() {
213         if v.Buf != nil {
214                 v.Buf.Serialize()
215         }
216 }
217
218 // ReOpen reloads the current buffer
219 func (v *View) ReOpen() {
220         if v.CanClose("Continue? (y,n,s) ", 'y', 'n', 's') {
221                 screen.Clear()
222                 v.Buf.ReOpen()
223                 v.Relocate()
224                 v.matches = Match(v)
225         }
226 }
227
228 // HSplit opens a horizontal split with the given buffer
229 func (v *View) HSplit(buf *Buffer) bool {
230         v.splitNode.HSplit(buf)
231         tabs[v.TabNum].Resize()
232         return false
233 }
234
235 // VSplit opens a vertical split with the given buffer
236 func (v *View) VSplit(buf *Buffer) bool {
237         v.splitNode.VSplit(buf)
238         tabs[v.TabNum].Resize()
239         return false
240 }
241
242 // Relocate moves the view window so that the cursor is in view
243 // This is useful if the user has scrolled far away, and then starts typing
244 func (v *View) Relocate() bool {
245         ret := false
246         cy := v.Cursor.Y
247         scrollmargin := int(v.Buf.Settings["scrollmargin"].(float64))
248         if cy < v.Topline+scrollmargin && cy > scrollmargin-1 {
249                 v.Topline = cy - scrollmargin
250                 ret = true
251         } else if cy < v.Topline {
252                 v.Topline = cy
253                 ret = true
254         }
255         if cy > v.Topline+v.height-1-scrollmargin && cy < v.Buf.NumLines-scrollmargin {
256                 v.Topline = cy - v.height + 1 + scrollmargin
257                 ret = true
258         } else if cy >= v.Buf.NumLines-scrollmargin && cy > v.height {
259                 v.Topline = v.Buf.NumLines - v.height
260                 ret = true
261         }
262
263         cx := v.Cursor.GetVisualX()
264         if cx < v.leftCol {
265                 v.leftCol = cx
266                 ret = true
267         }
268         if cx+v.lineNumOffset+1 > v.leftCol+v.width {
269                 v.leftCol = cx - v.width + v.lineNumOffset + 1
270                 ret = true
271         }
272         return ret
273 }
274
275 // MoveToMouseClick moves the cursor to location x, y assuming x, y were given
276 // by a mouse click
277 func (v *View) MoveToMouseClick(x, y int) {
278         if y-v.Topline > v.height-1 {
279                 v.ScrollDown(1)
280                 y = v.height + v.Topline - 1
281         }
282         if y >= v.Buf.NumLines {
283                 y = v.Buf.NumLines - 1
284         }
285         if y < 0 {
286                 y = 0
287         }
288         if x < 0 {
289                 x = 0
290         }
291
292         x = v.Cursor.GetCharPosInLine(y, x)
293         if x > Count(v.Buf.Line(y)) {
294                 x = Count(v.Buf.Line(y))
295         }
296         v.Cursor.X = x
297         v.Cursor.Y = y
298         v.Cursor.LastVisualX = v.Cursor.GetVisualX()
299 }
300
301 // HandleEvent handles an event passed by the main loop
302 func (v *View) HandleEvent(event tcell.Event) {
303         // This bool determines whether the view is relocated at the end of the function
304         // By default it's true because most events should cause a relocate
305         relocate := true
306
307         v.Buf.CheckModTime()
308
309         switch e := event.(type) {
310         case *tcell.EventResize:
311                 // Window resized
312                 tabs[v.TabNum].Resize()
313         case *tcell.EventKey:
314                 if e.Key() == tcell.KeyRune && (e.Modifiers() == 0 || e.Modifiers() == tcell.ModShift) {
315                         // Insert a character
316                         if v.Cursor.HasSelection() {
317                                 v.Cursor.DeleteSelection()
318                                 v.Cursor.ResetSelection()
319                         }
320                         v.Buf.Insert(v.Cursor.Loc, string(e.Rune()))
321                         v.Cursor.Right()
322
323                         for _, pl := range loadedPlugins {
324                                 _, err := Call(pl+".onRune", string(e.Rune()), v)
325                                 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
326                                         TermMessage(err)
327                                 }
328                         }
329                 } else {
330                         for key, actions := range bindings {
331                                 if e.Key() == key.keyCode {
332                                         if e.Key() == tcell.KeyRune {
333                                                 if e.Rune() != key.r {
334                                                         continue
335                                                 }
336                                         }
337                                         if e.Modifiers() == key.modifiers {
338                                                 relocate = false
339                                                 for _, action := range actions {
340                                                         relocate = action(v, true) || relocate
341                                                 }
342                                         }
343                                 }
344                         }
345                 }
346         case *tcell.EventPaste:
347                 if !PreActionCall("Paste", v) {
348                         break
349                 }
350
351                 leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
352
353                 if v.Cursor.HasSelection() {
354                         v.Cursor.DeleteSelection()
355                         v.Cursor.ResetSelection()
356                 }
357                 clip := e.Text()
358                 clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
359                 v.Buf.Insert(v.Cursor.Loc, clip)
360                 v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
361                 v.freshClip = false
362                 messenger.Message("Pasted clipboard")
363
364                 PostActionCall("Paste", v)
365         case *tcell.EventMouse:
366                 x, y := e.Position()
367                 x -= v.lineNumOffset - v.leftCol + v.x
368                 y += v.Topline - v.y
369                 // Don't relocate for mouse events
370                 relocate = false
371
372                 button := e.Buttons()
373
374                 switch button {
375                 case tcell.Button1:
376                         // Left click
377                         if v.mouseReleased {
378                                 v.MoveToMouseClick(x, y)
379                                 if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold {
380                                         if v.doubleClick {
381                                                 // Triple click
382                                                 v.lastClickTime = time.Now()
383
384                                                 v.tripleClick = true
385                                                 v.doubleClick = false
386
387                                                 v.Cursor.SelectLine()
388                                         } else {
389                                                 // Double click
390                                                 v.lastClickTime = time.Now()
391
392                                                 v.doubleClick = true
393                                                 v.tripleClick = false
394
395                                                 v.Cursor.SelectWord()
396                                         }
397                                 } else {
398                                         v.doubleClick = false
399                                         v.tripleClick = false
400                                         v.lastClickTime = time.Now()
401
402                                         v.Cursor.OrigSelection[0] = v.Cursor.Loc
403                                         v.Cursor.CurSelection[0] = v.Cursor.Loc
404                                         v.Cursor.CurSelection[1] = v.Cursor.Loc
405                                 }
406                                 v.mouseReleased = false
407                         } else if !v.mouseReleased {
408                                 v.MoveToMouseClick(x, y)
409                                 if v.tripleClick {
410                                         v.Cursor.AddLineToSelection()
411                                 } else if v.doubleClick {
412                                         v.Cursor.AddWordToSelection()
413                                 } else {
414                                         v.Cursor.CurSelection[1] = v.Cursor.Loc
415                                 }
416                         }
417                 case tcell.ButtonNone:
418                         // Mouse event with no click
419                         if !v.mouseReleased {
420                                 // Mouse was just released
421
422                                 // Relocating here isn't really necessary because the cursor will
423                                 // be in the right place from the last mouse event
424                                 // However, if we are running in a terminal that doesn't support mouse motion
425                                 // events, this still allows the user to make selections, except only after they
426                                 // release the mouse
427
428                                 if !v.doubleClick && !v.tripleClick {
429                                         v.MoveToMouseClick(x, y)
430                                         v.Cursor.CurSelection[1] = v.Cursor.Loc
431                                 }
432                                 v.mouseReleased = true
433                         }
434                 case tcell.WheelUp:
435                         // Scroll up
436                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
437                         v.ScrollUp(scrollspeed)
438                 case tcell.WheelDown:
439                         // Scroll down
440                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
441                         v.ScrollDown(scrollspeed)
442                 }
443         }
444
445         if relocate {
446                 v.Relocate()
447         }
448         if v.Buf.Settings["syntax"].(bool) {
449                 v.matches = Match(v)
450         }
451 }
452
453 // GutterMessage creates a message in this view's gutter
454 func (v *View) GutterMessage(section string, lineN int, msg string, kind int) {
455         lineN--
456         gutterMsg := GutterMessage{
457                 lineNum: lineN,
458                 msg:     msg,
459                 kind:    kind,
460         }
461         for _, v := range v.messages {
462                 for _, gmsg := range v {
463                         if gmsg.lineNum == lineN {
464                                 return
465                         }
466                 }
467         }
468         messages := v.messages[section]
469         v.messages[section] = append(messages, gutterMsg)
470 }
471
472 // ClearGutterMessages clears all gutter messages from a given section
473 func (v *View) ClearGutterMessages(section string) {
474         v.messages[section] = []GutterMessage{}
475 }
476
477 // ClearAllGutterMessages clears all the gutter messages
478 func (v *View) ClearAllGutterMessages() {
479         for k := range v.messages {
480                 v.messages[k] = []GutterMessage{}
481         }
482 }
483
484 // Opens the given help page in a new horizontal split
485 func (v *View) openHelp(helpPage string) {
486         if v.Help {
487                 helpBuffer := NewBuffer([]byte(helpPages[helpPage]), helpPage+".md")
488                 helpBuffer.Name = "Help"
489                 v.OpenBuffer(helpBuffer)
490         } else {
491                 helpBuffer := NewBuffer([]byte(helpPages[helpPage]), helpPage+".md")
492                 helpBuffer.Name = "Help"
493                 v.HSplit(helpBuffer)
494                 CurView().Help = true
495         }
496 }
497
498 func (v *View) drawCell(x, y int, ch rune, combc []rune, style tcell.Style) {
499         if x >= v.x && x < v.x+v.width && y >= v.y && y < v.y+v.height {
500                 screen.SetContent(x, y, ch, combc, style)
501         }
502 }
503
504 // DisplayView renders the view to the screen
505 func (v *View) DisplayView() {
506         // The charNum we are currently displaying
507         // starts at the start of the viewport
508         charNum := Loc{0, v.Topline}
509
510         // Convert the length of buffer to a string, and get the length of the string
511         // We are going to have to offset by that amount
512         maxLineLength := len(strconv.Itoa(v.Buf.NumLines))
513
514         if v.Buf.Settings["ruler"] == true {
515                 // + 1 for the little space after the line number
516                 v.lineNumOffset = maxLineLength + 1
517         } else {
518                 v.lineNumOffset = 0
519         }
520
521         // We need to add to the line offset if there are gutter messages
522         var hasGutterMessages bool
523         for _, v := range v.messages {
524                 if len(v) > 0 {
525                         hasGutterMessages = true
526                 }
527         }
528         if hasGutterMessages {
529                 v.lineNumOffset += 2
530         }
531
532         if v.x != 0 {
533                 // One space for the extra split divider
534                 v.lineNumOffset++
535         }
536
537         // These represent the current screen coordinates
538         screenX, screenY := 0, 0
539
540         highlightStyle := defStyle
541
542         // ViewLine is the current line from the top of the viewport
543         for viewLine := 0; viewLine < v.height; viewLine++ {
544                 screenY = v.y + viewLine
545                 screenX = v.x
546
547                 // This is the current line number of the buffer that we are drawing
548                 curLineN := viewLine + v.Topline
549
550                 if v.x != 0 {
551                         // Draw the split divider
552                         v.drawCell(screenX, screenY, '|', nil, defStyle.Reverse(true))
553                         screenX++
554                 }
555
556                 // If the buffer is smaller than the view height we have to clear all this space
557                 if curLineN >= v.Buf.NumLines {
558                         for i := screenX; i < v.x+v.width; i++ {
559                                 v.drawCell(i, screenY, ' ', nil, defStyle)
560                         }
561
562                         continue
563                 }
564                 line := v.Buf.Line(curLineN)
565
566                 // If there are gutter messages we need to display the '>>' symbol here
567                 if hasGutterMessages {
568                         // msgOnLine stores whether or not there is a gutter message on this line in particular
569                         msgOnLine := false
570                         for k := range v.messages {
571                                 for _, msg := range v.messages[k] {
572                                         if msg.lineNum == curLineN {
573                                                 msgOnLine = true
574                                                 gutterStyle := defStyle
575                                                 switch msg.kind {
576                                                 case GutterInfo:
577                                                         if style, ok := colorscheme["gutter-info"]; ok {
578                                                                 gutterStyle = style
579                                                         }
580                                                 case GutterWarning:
581                                                         if style, ok := colorscheme["gutter-warning"]; ok {
582                                                                 gutterStyle = style
583                                                         }
584                                                 case GutterError:
585                                                         if style, ok := colorscheme["gutter-error"]; ok {
586                                                                 gutterStyle = style
587                                                         }
588                                                 }
589                                                 v.drawCell(screenX, screenY, '>', nil, gutterStyle)
590                                                 screenX++
591                                                 v.drawCell(screenX, screenY, '>', nil, gutterStyle)
592                                                 screenX++
593                                                 if v.Cursor.Y == curLineN && !messenger.hasPrompt {
594                                                         messenger.Message(msg.msg)
595                                                         messenger.gutterMessage = true
596                                                 }
597                                         }
598                                 }
599                         }
600                         // If there is no message on this line we just display an empty offset
601                         if !msgOnLine {
602                                 v.drawCell(screenX, screenY, ' ', nil, defStyle)
603                                 screenX++
604                                 v.drawCell(screenX, screenY, ' ', nil, defStyle)
605                                 screenX++
606                                 if v.Cursor.Y == curLineN && messenger.gutterMessage {
607                                         messenger.Reset()
608                                         messenger.gutterMessage = false
609                                 }
610                         }
611                 }
612
613                 if v.Buf.Settings["ruler"] == true {
614                         // Write the line number
615                         lineNumStyle := defStyle
616                         if style, ok := colorscheme["line-number"]; ok {
617                                 lineNumStyle = style
618                         }
619                         if style, ok := colorscheme["current-line-number"]; ok {
620                                 if curLineN == v.Cursor.Y && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() {
621                                         lineNumStyle = style
622                                 }
623                         }
624
625                         lineNum := strconv.Itoa(curLineN + 1)
626
627                         // Write the spaces before the line number if necessary
628                         for i := 0; i < maxLineLength-len(lineNum); i++ {
629                                 v.drawCell(screenX, screenY, ' ', nil, lineNumStyle)
630                                 screenX++
631                         }
632                         // Write the actual line number
633                         for _, ch := range lineNum {
634                                 v.drawCell(screenX, screenY, ch, nil, lineNumStyle)
635                                 screenX++
636                         }
637
638                         // Write the extra space
639                         v.drawCell(screenX, screenY, ' ', nil, lineNumStyle)
640                         screenX++
641                 }
642
643                 // Now we actually draw the line
644                 colN := 0
645                 for _, ch := range line {
646                         lineStyle := defStyle
647
648                         if v.Buf.Settings["syntax"].(bool) {
649                                 // Syntax highlighting is enabled
650                                 highlightStyle = v.matches[viewLine][colN]
651                         }
652
653                         if v.Cursor.HasSelection() &&
654                                 (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
655                                         charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
656                                 // The current character is selected
657                                 lineStyle = defStyle.Reverse(true)
658
659                                 if style, ok := colorscheme["selection"]; ok {
660                                         lineStyle = style
661                                 }
662                         } else {
663                                 lineStyle = highlightStyle
664                         }
665
666                         // We need to display the background of the linestyle with the correct color if cursorline is enabled
667                         // and this is the current view and there is no selection on this line and the cursor is on this line
668                         if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
669                                 if style, ok := colorscheme["cursor-line"]; ok {
670                                         fg, _, _ := style.Decompose()
671                                         lineStyle = lineStyle.Background(fg)
672                                 }
673                         }
674
675                         if ch == '\t' {
676                                 // If the character we are displaying is a tab, we need to do a bunch of special things
677
678                                 // First the user may have configured an `indent-char` to be displayed to show that this
679                                 // is a tab character
680                                 lineIndentStyle := defStyle
681                                 if style, ok := colorscheme["indent-char"]; ok {
682                                         lineIndentStyle = style
683                                 }
684                                 if v.Cursor.HasSelection() &&
685                                         (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
686                                                 charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
687
688                                         lineIndentStyle = defStyle.Reverse(true)
689
690                                         if style, ok := colorscheme["selection"]; ok {
691                                                 lineIndentStyle = style
692                                         }
693                                 }
694                                 if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
695                                         if style, ok := colorscheme["cursor-line"]; ok {
696                                                 fg, _, _ := style.Decompose()
697                                                 lineIndentStyle = lineIndentStyle.Background(fg)
698                                         }
699                                 }
700                                 // Here we get the indent char
701                                 indentChar := []rune(v.Buf.Settings["indentchar"].(string))
702                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
703                                         v.drawCell(screenX-v.leftCol, screenY, indentChar[0], nil, lineIndentStyle)
704                                 }
705                                 // Now the tab has to be displayed as a bunch of spaces
706                                 tabSize := int(v.Buf.Settings["tabsize"].(float64))
707                                 for i := 0; i < tabSize-1; i++ {
708                                         screenX++
709                                         if screenX-v.x-v.leftCol >= v.lineNumOffset {
710                                                 v.drawCell(screenX-v.leftCol, screenY, ' ', nil, lineStyle)
711                                         }
712                                 }
713                         } else if runewidth.RuneWidth(ch) > 1 {
714                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
715                                         v.drawCell(screenX, screenY, ch, nil, lineStyle)
716                                 }
717                                 for i := 0; i < runewidth.RuneWidth(ch)-1; i++ {
718                                         screenX++
719                                         if screenX-v.x-v.leftCol >= v.lineNumOffset {
720                                                 v.drawCell(screenX-v.leftCol, screenY, '<', nil, lineStyle)
721                                         }
722                                 }
723                         } else {
724                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
725                                         v.drawCell(screenX-v.leftCol, screenY, ch, nil, lineStyle)
726                                 }
727                         }
728                         charNum = charNum.Move(1, v.Buf)
729                         screenX++
730                         colN++
731                 }
732                 // Here we are at a newline
733
734                 // The newline may be selected, in which case we should draw the selection style
735                 // with a space to represent it
736                 if v.Cursor.HasSelection() &&
737                         (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
738                                 charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
739
740                         selectStyle := defStyle.Reverse(true)
741
742                         if style, ok := colorscheme["selection"]; ok {
743                                 selectStyle = style
744                         }
745                         v.drawCell(screenX, screenY, ' ', nil, selectStyle)
746                         screenX++
747                 }
748
749                 charNum = charNum.Move(1, v.Buf)
750
751                 for i := 0; i < v.width; i++ {
752                         lineStyle := defStyle
753                         if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
754                                 if style, ok := colorscheme["cursor-line"]; ok {
755                                         fg, _, _ := style.Decompose()
756                                         lineStyle = lineStyle.Background(fg)
757                                 }
758                         }
759                         if screenX-v.x-v.leftCol+i >= v.lineNumOffset {
760                                 v.drawCell(screenX-v.leftCol+i, screenY, ' ', nil, lineStyle)
761                         }
762                 }
763         }
764 }
765
766 // DisplayCursor draws the current buffer's cursor to the screen
767 func (v *View) DisplayCursor() {
768         // Don't draw the cursor if it is out of the viewport or if it has a selection
769         if (v.Cursor.Y-v.Topline < 0 || v.Cursor.Y-v.Topline > v.height-1) || v.Cursor.HasSelection() {
770                 screen.HideCursor()
771         } else {
772                 screen.ShowCursor(v.x+v.Cursor.GetVisualX()+v.lineNumOffset-v.leftCol, v.Cursor.Y-v.Topline+v.y)
773         }
774 }
775
776 // Display renders the view, the cursor, and statusline
777 func (v *View) Display() {
778         v.DisplayView()
779         if v.Num == tabs[curTab].curView {
780                 v.DisplayCursor()
781         }
782         _, screenH := screen.Size()
783         if v.Buf.Settings["statusline"].(bool) {
784                 v.sline.Display()
785         } else if (v.y + v.height) != screenH-1 {
786                 for x := 0; x < v.width; x++ {
787                         screen.SetContent(v.x+x, v.y+v.height, '-', nil, defStyle.Reverse(true))
788                 }
789         }
790 }