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