]> git.lizzy.rs Git - micro.git/blob - cmd/micro/view.go
8c1bdc52128f0580e5105ee68101f37d3bf638db
[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 // The message is what to print after saying "You have unsaved changes. "
186 func (v *View) CanClose() bool {
187         if v.Buf.IsModified {
188                 char, canceled := messenger.LetterPrompt("Save changes to "+v.Buf.Name+" before closing? (y,n,esc) ", 'y', 'n')
189                 if !canceled {
190                         if char == 'y' {
191                                 v.Save(true)
192                                 return true
193                         } else if char == 'n' {
194                                 return true
195                         }
196                 }
197         } else {
198                 return true
199         }
200         return false
201 }
202
203 // OpenBuffer opens a new buffer in this view.
204 // This resets the topline, event handler and cursor.
205 func (v *View) OpenBuffer(buf *Buffer) {
206         screen.Clear()
207         v.CloseBuffer()
208         v.Buf = buf
209         v.Cursor = &buf.Cursor
210         v.Topline = 0
211         v.leftCol = 0
212         v.Cursor.ResetSelection()
213         v.Relocate()
214         v.Center(false)
215         v.messages = make(map[string][]GutterMessage)
216
217         v.matches = Match(v)
218
219         // Set mouseReleased to true because we assume the mouse is not being pressed when
220         // the editor is opened
221         v.mouseReleased = true
222         v.lastClickTime = time.Time{}
223 }
224
225 // CloseBuffer performs any closing functions on the buffer
226 func (v *View) CloseBuffer() {
227         if v.Buf != nil {
228                 v.Buf.Serialize()
229         }
230 }
231
232 // ReOpen reloads the current buffer
233 func (v *View) ReOpen() {
234         if v.CanClose() {
235                 screen.Clear()
236                 v.Buf.ReOpen()
237                 v.Relocate()
238                 v.matches = Match(v)
239         }
240 }
241
242 // HSplit opens a horizontal split with the given buffer
243 func (v *View) HSplit(buf *Buffer) bool {
244         v.splitNode.HSplit(buf)
245         tabs[v.TabNum].Resize()
246         return false
247 }
248
249 // VSplit opens a vertical split with the given buffer
250 func (v *View) VSplit(buf *Buffer) bool {
251         v.splitNode.VSplit(buf)
252         tabs[v.TabNum].Resize()
253         return false
254 }
255
256 // Relocate moves the view window so that the cursor is in view
257 // This is useful if the user has scrolled far away, and then starts typing
258 func (v *View) Relocate() bool {
259         ret := false
260         cy := v.Cursor.Y
261         scrollmargin := int(v.Buf.Settings["scrollmargin"].(float64))
262         if cy < v.Topline+scrollmargin && cy > scrollmargin-1 {
263                 v.Topline = cy - scrollmargin
264                 ret = true
265         } else if cy < v.Topline {
266                 v.Topline = cy
267                 ret = true
268         }
269         if cy > v.Topline+v.height-1-scrollmargin && cy < v.Buf.NumLines-scrollmargin {
270                 v.Topline = cy - v.height + 1 + scrollmargin
271                 ret = true
272         } else if cy >= v.Buf.NumLines-scrollmargin && cy > v.height {
273                 v.Topline = v.Buf.NumLines - v.height
274                 ret = true
275         }
276
277         cx := v.Cursor.GetVisualX()
278         if cx < v.leftCol {
279                 v.leftCol = cx
280                 ret = true
281         }
282         if cx+v.lineNumOffset+1 > v.leftCol+v.width {
283                 v.leftCol = cx - v.width + v.lineNumOffset + 1
284                 ret = true
285         }
286         return ret
287 }
288
289 // MoveToMouseClick moves the cursor to location x, y assuming x, y were given
290 // by a mouse click
291 func (v *View) MoveToMouseClick(x, y int) {
292         if y-v.Topline > v.height-1 {
293                 v.ScrollDown(1)
294                 y = v.height + v.Topline - 1
295         }
296         if y >= v.Buf.NumLines {
297                 y = v.Buf.NumLines - 1
298         }
299         if y < 0 {
300                 y = 0
301         }
302         if x < 0 {
303                 x = 0
304         }
305
306         x = v.Cursor.GetCharPosInLine(y, x)
307         if x > Count(v.Buf.Line(y)) {
308                 x = Count(v.Buf.Line(y))
309         }
310         v.Cursor.X = x
311         v.Cursor.Y = y
312         v.Cursor.LastVisualX = v.Cursor.GetVisualX()
313 }
314
315 // HandleEvent handles an event passed by the main loop
316 func (v *View) HandleEvent(event tcell.Event) {
317         // This bool determines whether the view is relocated at the end of the function
318         // By default it's true because most events should cause a relocate
319         relocate := true
320
321         v.Buf.CheckModTime()
322
323         switch e := event.(type) {
324         case *tcell.EventResize:
325                 // Window resized
326                 tabs[v.TabNum].Resize()
327         case *tcell.EventKey:
328                 if e.Key() == tcell.KeyRune && (e.Modifiers() == 0 || e.Modifiers() == tcell.ModShift) {
329                         // Insert a character
330                         if v.Cursor.HasSelection() {
331                                 v.Cursor.DeleteSelection()
332                                 v.Cursor.ResetSelection()
333                         }
334                         v.Buf.Insert(v.Cursor.Loc, string(e.Rune()))
335                         v.Cursor.Right()
336
337                         for _, pl := range loadedPlugins {
338                                 _, err := Call(pl+".onRune", string(e.Rune()), v)
339                                 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
340                                         TermMessage(err)
341                                 }
342                         }
343
344                         if recordingMacro {
345                                 curMacro = append(curMacro, e.Rune())
346                         }
347                 } else {
348                         for key, actions := range bindings {
349                                 if e.Key() == key.keyCode {
350                                         if e.Key() == tcell.KeyRune {
351                                                 if e.Rune() != key.r {
352                                                         continue
353                                                 }
354                                         }
355                                         if e.Modifiers() == key.modifiers {
356                                                 relocate = false
357                                                 for _, action := range actions {
358                                                         relocate = action(v, true) || relocate
359                                                         funcName := FuncName(action)
360                                                         if funcName != "main.(*View).ToggleMacro" && funcName != "main.(*View).PlayMacro" {
361                                                                 if recordingMacro {
362                                                                         curMacro = append(curMacro, action)
363                                                                 }
364                                                         }
365                                                 }
366                                         }
367                                 }
368                         }
369                 }
370         case *tcell.EventPaste:
371                 if !PreActionCall("Paste", v) {
372                         break
373                 }
374
375                 leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
376
377                 if v.Cursor.HasSelection() {
378                         v.Cursor.DeleteSelection()
379                         v.Cursor.ResetSelection()
380                 }
381                 clip := e.Text()
382                 clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
383                 v.Buf.Insert(v.Cursor.Loc, clip)
384                 v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
385                 v.freshClip = false
386                 messenger.Message("Pasted clipboard")
387
388                 PostActionCall("Paste", v)
389         case *tcell.EventMouse:
390                 x, y := e.Position()
391                 x -= v.lineNumOffset - v.leftCol + v.x
392                 y += v.Topline - v.y
393                 // Don't relocate for mouse events
394                 relocate = false
395
396                 button := e.Buttons()
397
398                 switch button {
399                 case tcell.Button1:
400                         // Left click
401                         if v.mouseReleased {
402                                 v.MoveToMouseClick(x, y)
403                                 if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold {
404                                         if v.doubleClick {
405                                                 // Triple click
406                                                 v.lastClickTime = time.Now()
407
408                                                 v.tripleClick = true
409                                                 v.doubleClick = false
410
411                                                 v.Cursor.SelectLine()
412                                         } else {
413                                                 // Double click
414                                                 v.lastClickTime = time.Now()
415
416                                                 v.doubleClick = true
417                                                 v.tripleClick = false
418
419                                                 v.Cursor.SelectWord()
420                                         }
421                                 } else {
422                                         v.doubleClick = false
423                                         v.tripleClick = false
424                                         v.lastClickTime = time.Now()
425
426                                         v.Cursor.OrigSelection[0] = v.Cursor.Loc
427                                         v.Cursor.CurSelection[0] = v.Cursor.Loc
428                                         v.Cursor.CurSelection[1] = v.Cursor.Loc
429                                 }
430                                 v.mouseReleased = false
431                         } else if !v.mouseReleased {
432                                 v.MoveToMouseClick(x, y)
433                                 if v.tripleClick {
434                                         v.Cursor.AddLineToSelection()
435                                 } else if v.doubleClick {
436                                         v.Cursor.AddWordToSelection()
437                                 } else {
438                                         v.Cursor.SetSelectionEnd(v.Cursor.Loc)
439                                 }
440                         }
441                 case tcell.Button2:
442                         // Middle mouse button was clicked,
443                         // We should paste primary
444                         v.PastePrimary(true)
445                 case tcell.ButtonNone:
446                         // Mouse event with no click
447                         if !v.mouseReleased {
448                                 // Mouse was just released
449
450                                 // Relocating here isn't really necessary because the cursor will
451                                 // be in the right place from the last mouse event
452                                 // However, if we are running in a terminal that doesn't support mouse motion
453                                 // events, this still allows the user to make selections, except only after they
454                                 // release the mouse
455
456                                 if !v.doubleClick && !v.tripleClick {
457                                         v.MoveToMouseClick(x, y)
458                                         v.Cursor.SetSelectionEnd(v.Cursor.Loc)
459                                 }
460                                 v.mouseReleased = true
461                         }
462                 case tcell.WheelUp:
463                         // Scroll up
464                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
465                         v.ScrollUp(scrollspeed)
466                 case tcell.WheelDown:
467                         // Scroll down
468                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
469                         v.ScrollDown(scrollspeed)
470                 }
471         }
472
473         if relocate {
474                 v.Relocate()
475         }
476         if v.Buf.Settings["syntax"].(bool) {
477                 v.matches = Match(v)
478         }
479 }
480
481 // GutterMessage creates a message in this view's gutter
482 func (v *View) GutterMessage(section string, lineN int, msg string, kind int) {
483         lineN--
484         gutterMsg := GutterMessage{
485                 lineNum: lineN,
486                 msg:     msg,
487                 kind:    kind,
488         }
489         for _, v := range v.messages {
490                 for _, gmsg := range v {
491                         if gmsg.lineNum == lineN {
492                                 return
493                         }
494                 }
495         }
496         messages := v.messages[section]
497         v.messages[section] = append(messages, gutterMsg)
498 }
499
500 // ClearGutterMessages clears all gutter messages from a given section
501 func (v *View) ClearGutterMessages(section string) {
502         v.messages[section] = []GutterMessage{}
503 }
504
505 // ClearAllGutterMessages clears all the gutter messages
506 func (v *View) ClearAllGutterMessages() {
507         for k := range v.messages {
508                 v.messages[k] = []GutterMessage{}
509         }
510 }
511
512 // Opens the given help page in a new horizontal split
513 func (v *View) openHelp(helpPage string) {
514         if v.Help {
515                 helpBuffer := NewBuffer([]byte(helpPages[helpPage]), helpPage+".md")
516                 helpBuffer.Name = "Help"
517                 v.OpenBuffer(helpBuffer)
518         } else {
519                 helpBuffer := NewBuffer([]byte(helpPages[helpPage]), helpPage+".md")
520                 helpBuffer.Name = "Help"
521                 v.HSplit(helpBuffer)
522                 CurView().Help = true
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 v.Buf.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 && !messenger.hasPrompt {
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 v.Buf.Settings["ruler"] == true {
642                         // Write the line number
643                         lineNumStyle := defStyle
644                         if style, ok := colorscheme["line-number"]; ok {
645                                 lineNumStyle = style
646                         }
647                         if style, ok := colorscheme["current-line-number"]; ok {
648                                 if curLineN == v.Cursor.Y && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() {
649                                         lineNumStyle = style
650                                 }
651                         }
652
653                         lineNum := strconv.Itoa(curLineN + 1)
654
655                         // Write the spaces before the line number if necessary
656                         for i := 0; i < maxLineLength-len(lineNum); i++ {
657                                 v.drawCell(screenX, screenY, ' ', nil, lineNumStyle)
658                                 screenX++
659                         }
660                         // Write the actual line number
661                         for _, ch := range lineNum {
662                                 v.drawCell(screenX, screenY, ch, nil, lineNumStyle)
663                                 screenX++
664                         }
665
666                         // Write the extra space
667                         v.drawCell(screenX, screenY, ' ', nil, lineNumStyle)
668                         screenX++
669                 }
670
671                 // Now we actually draw the line
672                 colN := 0
673                 for _, ch := range line {
674                         lineStyle := defStyle
675
676                         if v.Buf.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 v.Buf.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 v.Buf.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(v.Buf.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(v.Buf.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                         colN++
759                 }
760                 // Here we are at a newline
761
762                 // The newline may be selected, in which case we should draw the selection style
763                 // with a space to represent it
764                 if v.Cursor.HasSelection() &&
765                         (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
766                                 charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
767
768                         selectStyle := defStyle.Reverse(true)
769
770                         if style, ok := colorscheme["selection"]; ok {
771                                 selectStyle = style
772                         }
773                         v.drawCell(screenX, screenY, ' ', nil, selectStyle)
774                         screenX++
775                 }
776
777                 charNum = charNum.Move(1, v.Buf)
778
779                 for i := 0; i < v.width; i++ {
780                         lineStyle := defStyle
781                         if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
782                                 if style, ok := colorscheme["cursor-line"]; ok {
783                                         fg, _, _ := style.Decompose()
784                                         lineStyle = lineStyle.Background(fg)
785                                 }
786                         }
787                         if screenX-v.x-v.leftCol+i >= v.lineNumOffset {
788                                 v.drawCell(screenX-v.leftCol+i, screenY, ' ', nil, lineStyle)
789                         }
790                 }
791         }
792 }
793
794 // DisplayCursor draws the current buffer's cursor to the screen
795 func (v *View) DisplayCursor() {
796         // Don't draw the cursor if it is out of the viewport or if it has a selection
797         if (v.Cursor.Y-v.Topline < 0 || v.Cursor.Y-v.Topline > v.height-1) || v.Cursor.HasSelection() {
798                 screen.HideCursor()
799         } else {
800                 screen.ShowCursor(v.x+v.Cursor.GetVisualX()+v.lineNumOffset-v.leftCol, v.Cursor.Y-v.Topline+v.y)
801         }
802 }
803
804 // Display renders the view, the cursor, and statusline
805 func (v *View) Display() {
806         v.DisplayView()
807         if v.Num == tabs[curTab].curView {
808                 v.DisplayCursor()
809         }
810         _, screenH := screen.Size()
811         if v.Buf.Settings["statusline"].(bool) {
812                 v.sline.Display()
813         } else if (v.y + v.height) != screenH-1 {
814                 for x := 0; x < v.width; x++ {
815                         screen.SetContent(v.x+x, v.y+v.height, '-', nil, defStyle.Reverse(true))
816                 }
817         }
818 }