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