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