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