]> git.lizzy.rs Git - micro.git/blob - cmd/micro/view.go
Fix some small glitches with cursor positioning
[micro.git] / cmd / micro / view.go
1 package main
2
3 import (
4         "os"
5         "strconv"
6         "strings"
7         "time"
8
9         "github.com/mitchellh/go-homedir"
10         "github.com/zyedidia/tcell"
11 )
12
13 type ViewType struct {
14         readonly bool // The file cannot be edited
15         scratch  bool // The file cannot be saved
16 }
17
18 var (
19         vtDefault = ViewType{false, false}
20         vtHelp    = ViewType{true, true}
21         vtLog     = ViewType{true, true}
22         vtScratch = ViewType{false, true}
23 )
24
25 // The View struct stores information about a view into a buffer.
26 // It stores information about the cursor, and the viewport
27 // that the user sees the buffer from.
28 type View struct {
29         // A pointer to the buffer's cursor for ease of access
30         Cursor *Cursor
31
32         // The topmost line, used for vertical scrolling
33         Topline int
34         // The leftmost column, used for horizontal scrolling
35         leftCol int
36
37         // Specifies whether or not this view holds a help buffer
38         Type ViewType
39
40         // Actual width and height
41         Width  int
42         Height int
43
44         LockWidth  bool
45         LockHeight bool
46
47         // Where this view is located
48         x, y int
49
50         // How much to offset because of line numbers
51         lineNumOffset int
52
53         // Holds the list of gutter messages
54         messages map[string][]GutterMessage
55
56         // This is the index of this view in the views array
57         Num int
58         // What tab is this view stored in
59         TabNum int
60
61         // The buffer
62         Buf *Buffer
63         // The statusline
64         sline Statusline
65
66         // Since tcell doesn't differentiate between a mouse release event
67         // and a mouse move event with no keys pressed, we need to keep
68         // track of whether or not the mouse was pressed (or not released) last event to determine
69         // mouse release events
70         mouseReleased bool
71
72         // This stores when the last click was
73         // This is useful for detecting double and triple clicks
74         lastClickTime time.Time
75
76         // lastCutTime stores when the last ctrl+k was issued.
77         // It is used for clearing the clipboard to replace it with fresh cut lines.
78         lastCutTime time.Time
79
80         // freshClip returns true if the clipboard has never been pasted.
81         freshClip bool
82
83         // Was the last mouse event actually a double click?
84         // Useful for detecting triple clicks -- if a double click is detected
85         // but the last mouse event was actually a double click, it's a triple click
86         doubleClick bool
87         // Same here, just to keep track for mouse move events
88         tripleClick bool
89
90         cellview *CellView
91
92         splitNode *LeafNode
93 }
94
95 // NewView returns a new fullscreen view
96 func NewView(buf *Buffer) *View {
97         screenW, screenH := screen.Size()
98         return NewViewWidthHeight(buf, screenW, screenH)
99 }
100
101 // NewViewWidthHeight returns a new view with the specified width and height
102 // Note that w and h are raw column and row values
103 func NewViewWidthHeight(buf *Buffer, w, h int) *View {
104         v := new(View)
105
106         v.x, v.y = 0, 0
107
108         v.Width = w
109         v.Height = h
110         v.cellview = new(CellView)
111
112         v.ToggleTabbar()
113
114         v.OpenBuffer(buf)
115
116         v.messages = make(map[string][]GutterMessage)
117
118         v.sline = Statusline{
119                 view: v,
120         }
121
122         if v.Buf.Settings["statusline"].(bool) {
123                 v.Height--
124         }
125
126         for pl := range loadedPlugins {
127                 _, err := Call(pl+".onViewOpen", v)
128                 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
129                         TermMessage(err)
130                         continue
131                 }
132         }
133
134         return v
135 }
136
137 // ToggleStatusLine creates an extra row for the statusline if necessary
138 func (v *View) ToggleStatusLine() {
139         if v.Buf.Settings["statusline"].(bool) {
140                 v.Height--
141         } else {
142                 v.Height++
143         }
144 }
145
146 // ToggleTabbar creates an extra row for the tabbar if necessary
147 func (v *View) ToggleTabbar() {
148         if len(tabs) > 1 {
149                 if v.y == 0 {
150                         // Include one line for the tab bar at the top
151                         v.Height--
152                         v.y = 1
153                 }
154         } else {
155                 if v.y == 1 {
156                         v.y = 0
157                         v.Height++
158                 }
159         }
160 }
161
162 func (v *View) paste(clip string) {
163         leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
164
165         if v.Cursor.HasSelection() {
166                 v.Cursor.DeleteSelection()
167                 v.Cursor.ResetSelection()
168         }
169         clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
170         v.Buf.Insert(v.Cursor.Loc, clip)
171         v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
172         v.freshClip = false
173         messenger.Message("Pasted clipboard")
174 }
175
176 // ScrollUp scrolls the view up n lines (if possible)
177 func (v *View) ScrollUp(n int) {
178         // Try to scroll by n but if it would overflow, scroll by 1
179         if v.Topline-n >= 0 {
180                 v.Topline -= n
181         } else if v.Topline > 0 {
182                 v.Topline--
183         }
184 }
185
186 // ScrollDown scrolls the view down n lines (if possible)
187 func (v *View) ScrollDown(n int) {
188         // Try to scroll by n but if it would overflow, scroll by 1
189         if v.Topline+n <= v.Buf.NumLines {
190                 v.Topline += n
191         } else if v.Topline < v.Buf.NumLines-1 {
192                 v.Topline++
193         }
194 }
195
196 // CanClose returns whether or not the view can be closed
197 // If there are unsaved changes, the user will be asked if the view can be closed
198 // causing them to lose the unsaved changes
199 func (v *View) CanClose() bool {
200         if v.Type == vtDefault && v.Buf.IsModified {
201                 var char rune
202                 var canceled bool
203                 if v.Buf.Settings["autosave"].(bool) {
204                         char = 'y'
205                 } else {
206                         char, canceled = messenger.LetterPrompt("Save changes to "+v.Buf.GetName()+" before closing? (y,n,esc) ", 'y', 'n')
207                 }
208                 if !canceled {
209                         if char == 'y' {
210                                 v.Save(true)
211                                 return true
212                         } else if char == 'n' {
213                                 return true
214                         }
215                 }
216         } else {
217                 return true
218         }
219         return false
220 }
221
222 // OpenBuffer opens a new buffer in this view.
223 // This resets the topline, event handler and cursor.
224 func (v *View) OpenBuffer(buf *Buffer) {
225         screen.Clear()
226         v.CloseBuffer()
227         v.Buf = buf
228         v.Cursor = &buf.Cursor
229         v.Topline = 0
230         v.leftCol = 0
231         v.Cursor.ResetSelection()
232         v.Relocate()
233         v.Center(false)
234         v.messages = make(map[string][]GutterMessage)
235
236         // Set mouseReleased to true because we assume the mouse is not being pressed when
237         // the editor is opened
238         v.mouseReleased = true
239         v.lastClickTime = time.Time{}
240 }
241
242 // Open opens the given file in the view
243 func (v *View) Open(filename string) {
244         home, _ := homedir.Dir()
245         filename = strings.Replace(filename, "~", home, 1)
246         file, err := os.Open(filename)
247         defer file.Close()
248
249         var buf *Buffer
250         if err != nil {
251                 messenger.Message(err.Error())
252                 // File does not exist -- create an empty buffer with that name
253                 buf = NewBuffer(strings.NewReader(""), filename)
254         } else {
255                 buf = NewBuffer(file, filename)
256         }
257         v.OpenBuffer(buf)
258 }
259
260 // CloseBuffer performs any closing functions on the buffer
261 func (v *View) CloseBuffer() {
262         if v.Buf != nil {
263                 v.Buf.Serialize()
264         }
265 }
266
267 // ReOpen reloads the current buffer
268 func (v *View) ReOpen() {
269         if v.CanClose() {
270                 screen.Clear()
271                 v.Buf.ReOpen()
272                 v.Relocate()
273         }
274 }
275
276 // HSplit opens a horizontal split with the given buffer
277 func (v *View) HSplit(buf *Buffer) {
278         i := 0
279         if v.Buf.Settings["splitBottom"].(bool) {
280                 i = 1
281         }
282         v.splitNode.HSplit(buf, v.Num+i)
283 }
284
285 // VSplit opens a vertical split with the given buffer
286 func (v *View) VSplit(buf *Buffer) {
287         i := 0
288         if v.Buf.Settings["splitRight"].(bool) {
289                 i = 1
290         }
291         v.splitNode.VSplit(buf, v.Num+i)
292 }
293
294 // HSplitIndex opens a horizontal split with the given buffer at the given index
295 func (v *View) HSplitIndex(buf *Buffer, splitIndex int) {
296         v.splitNode.HSplit(buf, splitIndex)
297 }
298
299 // VSplitIndex opens a vertical split with the given buffer at the given index
300 func (v *View) VSplitIndex(buf *Buffer, splitIndex int) {
301         v.splitNode.VSplit(buf, splitIndex)
302 }
303
304 // GetSoftWrapLocation gets the location of a visual click on the screen and converts it to col,line
305 func (v *View) GetSoftWrapLocation(vx, vy int) (int, int) {
306         if !v.Buf.Settings["softwrap"].(bool) {
307                 if vy >= v.Buf.NumLines {
308                         vy = v.Buf.NumLines - 1
309                 }
310                 vx = v.Cursor.GetCharPosInLine(vy, vx)
311                 return vx, vy
312         }
313
314         screenX, screenY := 0, v.Topline
315         for lineN := v.Topline; lineN < v.Bottomline(); lineN++ {
316                 line := v.Buf.Line(lineN)
317                 if lineN >= v.Buf.NumLines {
318                         return 0, v.Buf.NumLines - 1
319                 }
320
321                 colN := 0
322                 for _, ch := range line {
323                         if screenX >= v.Width-v.lineNumOffset {
324                                 screenX = 0
325                                 screenY++
326                         }
327
328                         if screenX == vx && screenY == vy {
329                                 return colN, lineN
330                         }
331
332                         if ch == '\t' {
333                                 screenX += int(v.Buf.Settings["tabsize"].(float64)) - 1
334                         }
335
336                         screenX++
337                         colN++
338                 }
339                 if screenY == vy {
340                         return colN, lineN
341                 }
342                 screenX = 0
343                 screenY++
344         }
345
346         return 0, 0
347 }
348
349 func (v *View) Bottomline() int {
350         if !v.Buf.Settings["softwrap"].(bool) {
351                 return v.Topline + v.Height
352         }
353
354         screenX, screenY := 0, 0
355         numLines := 0
356         for lineN := v.Topline; lineN < v.Topline+v.Height; lineN++ {
357                 line := v.Buf.Line(lineN)
358
359                 colN := 0
360                 for _, ch := range line {
361                         if screenX >= v.Width-v.lineNumOffset {
362                                 screenX = 0
363                                 screenY++
364                         }
365
366                         if ch == '\t' {
367                                 screenX += int(v.Buf.Settings["tabsize"].(float64)) - 1
368                         }
369
370                         screenX++
371                         colN++
372                 }
373                 screenX = 0
374                 screenY++
375                 numLines++
376
377                 if screenY >= v.Height {
378                         break
379                 }
380         }
381         return numLines + v.Topline
382 }
383
384 // Relocate moves the view window so that the cursor is in view
385 // This is useful if the user has scrolled far away, and then starts typing
386 func (v *View) Relocate() bool {
387         height := v.Bottomline() - v.Topline
388         ret := false
389         cy := v.Cursor.Y
390         scrollmargin := int(v.Buf.Settings["scrollmargin"].(float64))
391         if cy < v.Topline+scrollmargin && cy > scrollmargin-1 {
392                 v.Topline = cy - scrollmargin
393                 ret = true
394         } else if cy < v.Topline {
395                 v.Topline = cy
396                 ret = true
397         }
398         if cy > v.Topline+height-1-scrollmargin && cy < v.Buf.NumLines-scrollmargin {
399                 v.Topline = cy - height + 1 + scrollmargin
400                 ret = true
401         } else if cy >= v.Buf.NumLines-scrollmargin && cy > height {
402                 v.Topline = v.Buf.NumLines - height
403                 ret = true
404         }
405
406         if !v.Buf.Settings["softwrap"].(bool) {
407                 cx := v.Cursor.GetVisualX()
408                 if cx < v.leftCol {
409                         v.leftCol = cx
410                         ret = true
411                 }
412                 if cx+v.lineNumOffset+1 > v.leftCol+v.Width {
413                         v.leftCol = cx - v.Width + v.lineNumOffset + 1
414                         ret = true
415                 }
416         }
417         return ret
418 }
419
420 // MoveToMouseClick moves the cursor to location x, y assuming x, y were given
421 // by a mouse click
422 func (v *View) MoveToMouseClick(x, y int) {
423         if y-v.Topline > v.Height-1 {
424                 v.ScrollDown(1)
425                 y = v.Height + v.Topline - 1
426         }
427         if y < 0 {
428                 y = 0
429         }
430         if x < 0 {
431                 x = 0
432         }
433
434         x, y = v.GetSoftWrapLocation(x, y)
435         // x = v.Cursor.GetCharPosInLine(y, x)
436         if x > Count(v.Buf.Line(y)) {
437                 x = Count(v.Buf.Line(y))
438         }
439         v.Cursor.X = x
440         v.Cursor.Y = y
441         v.Cursor.LastVisualX = v.Cursor.GetVisualX()
442 }
443
444 // HandleEvent handles an event passed by the main loop
445 func (v *View) HandleEvent(event tcell.Event) {
446         // This bool determines whether the view is relocated at the end of the function
447         // By default it's true because most events should cause a relocate
448         relocate := true
449
450         v.Buf.CheckModTime()
451
452         switch e := event.(type) {
453         case *tcell.EventKey:
454                 // Check first if input is a key binding, if it is we 'eat' the input and don't insert a rune
455                 isBinding := false
456                 if e.Key() != tcell.KeyRune || e.Modifiers() != 0 {
457                         for key, actions := range bindings {
458                                 if e.Key() == key.keyCode {
459                                         if e.Key() == tcell.KeyRune {
460                                                 if e.Rune() != key.r {
461                                                         continue
462                                                 }
463                                         }
464                                         if e.Modifiers() == key.modifiers {
465                                                 relocate = false
466                                                 isBinding = true
467                                                 for _, action := range actions {
468                                                         relocate = action(v, true) || relocate
469                                                         funcName := FuncName(action)
470                                                         if funcName != "main.(*View).ToggleMacro" && funcName != "main.(*View).PlayMacro" {
471                                                                 if recordingMacro {
472                                                                         curMacro = append(curMacro, action)
473                                                                 }
474                                                         }
475                                                 }
476                                                 break
477                                         }
478                                 }
479                         }
480                 }
481                 if !isBinding && e.Key() == tcell.KeyRune {
482                         // Insert a character
483                         if v.Cursor.HasSelection() {
484                                 v.Cursor.DeleteSelection()
485                                 v.Cursor.ResetSelection()
486                         }
487                         v.Buf.Insert(v.Cursor.Loc, string(e.Rune()))
488                         v.Cursor.Right()
489
490                         for pl := range loadedPlugins {
491                                 _, err := Call(pl+".onRune", string(e.Rune()), v)
492                                 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
493                                         TermMessage(err)
494                                 }
495                         }
496
497                         if recordingMacro {
498                                 curMacro = append(curMacro, e.Rune())
499                         }
500                 }
501         case *tcell.EventPaste:
502                 if !PreActionCall("Paste", v) {
503                         break
504                 }
505
506                 v.paste(e.Text())
507
508                 PostActionCall("Paste", v)
509         case *tcell.EventMouse:
510                 x, y := e.Position()
511                 x -= v.lineNumOffset - v.leftCol + v.x
512                 y += v.Topline - v.y
513                 // Don't relocate for mouse events
514                 relocate = false
515
516                 button := e.Buttons()
517
518                 switch button {
519                 case tcell.Button1:
520                         // Left click
521                         if v.mouseReleased {
522                                 v.MoveToMouseClick(x, y)
523                                 if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold {
524                                         if v.doubleClick {
525                                                 // Triple click
526                                                 v.lastClickTime = time.Now()
527
528                                                 v.tripleClick = true
529                                                 v.doubleClick = false
530
531                                                 v.Cursor.SelectLine()
532                                                 v.Cursor.CopySelection("primary")
533                                         } else {
534                                                 // Double click
535                                                 v.lastClickTime = time.Now()
536
537                                                 v.doubleClick = true
538                                                 v.tripleClick = false
539
540                                                 v.Cursor.SelectWord()
541                                                 v.Cursor.CopySelection("primary")
542                                         }
543                                 } else {
544                                         v.doubleClick = false
545                                         v.tripleClick = false
546                                         v.lastClickTime = time.Now()
547
548                                         v.Cursor.OrigSelection[0] = v.Cursor.Loc
549                                         v.Cursor.CurSelection[0] = v.Cursor.Loc
550                                         v.Cursor.CurSelection[1] = v.Cursor.Loc
551                                 }
552                                 v.mouseReleased = false
553                         } else if !v.mouseReleased {
554                                 v.MoveToMouseClick(x, y)
555                                 if v.tripleClick {
556                                         v.Cursor.AddLineToSelection()
557                                 } else if v.doubleClick {
558                                         v.Cursor.AddWordToSelection()
559                                 } else {
560                                         v.Cursor.SetSelectionEnd(v.Cursor.Loc)
561                                         v.Cursor.CopySelection("primary")
562                                 }
563                         }
564                 case tcell.Button2:
565                         // Middle mouse button was clicked,
566                         // We should paste primary
567                         v.PastePrimary(true)
568                 case tcell.ButtonNone:
569                         // Mouse event with no click
570                         if !v.mouseReleased {
571                                 // Mouse was just released
572
573                                 // Relocating here isn't really necessary because the cursor will
574                                 // be in the right place from the last mouse event
575                                 // However, if we are running in a terminal that doesn't support mouse motion
576                                 // events, this still allows the user to make selections, except only after they
577                                 // release the mouse
578
579                                 if !v.doubleClick && !v.tripleClick {
580                                         v.MoveToMouseClick(x, y)
581                                         v.Cursor.SetSelectionEnd(v.Cursor.Loc)
582                                         v.Cursor.CopySelection("primary")
583                                 }
584                                 v.mouseReleased = true
585                         }
586                 case tcell.WheelUp:
587                         // Scroll up
588                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
589                         v.ScrollUp(scrollspeed)
590                 case tcell.WheelDown:
591                         // Scroll down
592                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
593                         v.ScrollDown(scrollspeed)
594                 }
595         }
596
597         if relocate {
598                 v.Relocate()
599         }
600 }
601
602 // GutterMessage creates a message in this view's gutter
603 func (v *View) GutterMessage(section string, lineN int, msg string, kind int) {
604         lineN--
605         gutterMsg := GutterMessage{
606                 lineNum: lineN,
607                 msg:     msg,
608                 kind:    kind,
609         }
610         for _, v := range v.messages {
611                 for _, gmsg := range v {
612                         if gmsg.lineNum == lineN {
613                                 return
614                         }
615                 }
616         }
617         messages := v.messages[section]
618         v.messages[section] = append(messages, gutterMsg)
619 }
620
621 // ClearGutterMessages clears all gutter messages from a given section
622 func (v *View) ClearGutterMessages(section string) {
623         v.messages[section] = []GutterMessage{}
624 }
625
626 // ClearAllGutterMessages clears all the gutter messages
627 func (v *View) ClearAllGutterMessages() {
628         for k := range v.messages {
629                 v.messages[k] = []GutterMessage{}
630         }
631 }
632
633 // Opens the given help page in a new horizontal split
634 func (v *View) openHelp(helpPage string) {
635         if data, err := FindRuntimeFile(RTHelp, helpPage).Data(); err != nil {
636                 TermMessage("Unable to load help text", helpPage, "\n", err)
637         } else {
638                 helpBuffer := NewBuffer(strings.NewReader(string(data)), helpPage+".md")
639                 helpBuffer.name = "Help"
640
641                 if v.Type == vtHelp {
642                         v.OpenBuffer(helpBuffer)
643                 } else {
644                         v.HSplit(helpBuffer)
645                         CurView().Type = vtHelp
646                 }
647         }
648 }
649
650 func (v *View) DisplayView() {
651         if v.Type == vtLog {
652                 // Log views should always follow the cursor...
653                 v.Relocate()
654         }
655
656         // We need to know the string length of the largest line number
657         // so we can pad appropriately when displaying line numbers
658         maxLineNumLength := len(strconv.Itoa(v.Buf.NumLines))
659
660         if v.Buf.Settings["ruler"] == true {
661                 // + 1 for the little space after the line number
662                 v.lineNumOffset = maxLineNumLength + 1
663         } else {
664                 v.lineNumOffset = 0
665         }
666
667         // We need to add to the line offset if there are gutter messages
668         var hasGutterMessages bool
669         for _, v := range v.messages {
670                 if len(v) > 0 {
671                         hasGutterMessages = true
672                 }
673         }
674         if hasGutterMessages {
675                 v.lineNumOffset += 2
676         }
677
678         if v.x != 0 {
679                 // One space for the extra split divider
680                 v.lineNumOffset++
681         }
682
683         xOffset := v.x + v.lineNumOffset
684         yOffset := v.y
685
686         height := v.Height
687         width := v.Width
688         left := v.leftCol
689         top := v.Topline
690
691         v.cellview.Draw(v.Buf, top, height, left, width-v.lineNumOffset)
692
693         screenX := v.x
694         realLineN := top - 1
695         visualLineN := 0
696         var line []*Char
697         for visualLineN, line = range v.cellview.lines {
698                 var firstChar *Char
699                 if len(line) > 0 {
700                         firstChar = line[0]
701                 }
702
703                 var softwrapped bool
704                 if firstChar != nil {
705                         if firstChar.realLoc.Y == realLineN {
706                                 softwrapped = true
707                         }
708                         realLineN = firstChar.realLoc.Y
709                 } else {
710                         realLineN++
711                 }
712
713                 screenX = v.x
714
715                 if v.x != 0 {
716                         // Draw the split divider
717                         screen.SetContent(screenX, yOffset+visualLineN, '|', nil, defStyle.Reverse(true))
718                         screenX++
719                 }
720
721                 // If there are gutter messages we need to display the '>>' symbol here
722                 if hasGutterMessages {
723                         // msgOnLine stores whether or not there is a gutter message on this line in particular
724                         msgOnLine := false
725                         for k := range v.messages {
726                                 for _, msg := range v.messages[k] {
727                                         if msg.lineNum == realLineN {
728                                                 msgOnLine = true
729                                                 gutterStyle := defStyle
730                                                 switch msg.kind {
731                                                 case GutterInfo:
732                                                         if style, ok := colorscheme["gutter-info"]; ok {
733                                                                 gutterStyle = style
734                                                         }
735                                                 case GutterWarning:
736                                                         if style, ok := colorscheme["gutter-warning"]; ok {
737                                                                 gutterStyle = style
738                                                         }
739                                                 case GutterError:
740                                                         if style, ok := colorscheme["gutter-error"]; ok {
741                                                                 gutterStyle = style
742                                                         }
743                                                 }
744                                                 screen.SetContent(screenX, yOffset+visualLineN, '>', nil, gutterStyle)
745                                                 screenX++
746                                                 screen.SetContent(screenX, yOffset+visualLineN, '>', nil, gutterStyle)
747                                                 screenX++
748                                                 if v.Cursor.Y == realLineN && !messenger.hasPrompt {
749                                                         messenger.Message(msg.msg)
750                                                         messenger.gutterMessage = true
751                                                 }
752                                         }
753                                 }
754                         }
755                         // If there is no message on this line we just display an empty offset
756                         if !msgOnLine {
757                                 screen.SetContent(screenX, yOffset+visualLineN, ' ', nil, defStyle)
758                                 screenX++
759                                 screen.SetContent(screenX, yOffset+visualLineN, ' ', nil, defStyle)
760                                 screenX++
761                                 if v.Cursor.Y == realLineN && messenger.gutterMessage {
762                                         messenger.Reset()
763                                         messenger.gutterMessage = false
764                                 }
765                         }
766                 }
767
768                 lineNumStyle := defStyle
769                 if v.Buf.Settings["ruler"] == true {
770                         // Write the line number
771                         if style, ok := colorscheme["line-number"]; ok {
772                                 lineNumStyle = style
773                         }
774                         if style, ok := colorscheme["current-line-number"]; ok {
775                                 if realLineN == v.Cursor.Y && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() {
776                                         lineNumStyle = style
777                                 }
778                         }
779
780                         lineNum := strconv.Itoa(realLineN + 1)
781
782                         // Write the spaces before the line number if necessary
783                         for i := 0; i < maxLineNumLength-len(lineNum); i++ {
784                                 screen.SetContent(screenX, yOffset+visualLineN, ' ', nil, lineNumStyle)
785                                 screenX++
786                         }
787                         if softwrapped && visualLineN != 0 {
788                                 // Pad without the line number because it was written on the visual line before
789                                 for range lineNum {
790                                         screen.SetContent(screenX, yOffset+visualLineN, ' ', nil, lineNumStyle)
791                                         screenX++
792                                 }
793                         } else {
794                                 // Write the actual line number
795                                 for _, ch := range lineNum {
796                                         screen.SetContent(screenX, yOffset+visualLineN, ch, nil, lineNumStyle)
797                                         screenX++
798                                 }
799                         }
800
801                         // Write the extra space
802                         screen.SetContent(screenX, yOffset+visualLineN, ' ', nil, lineNumStyle)
803                         screenX++
804                 }
805
806                 var lastChar *Char
807                 cursorSet := false
808                 for _, char := range line {
809                         if char != nil {
810                                 lineStyle := char.style
811
812                                 charLoc := char.realLoc
813                                 if v.Cursor.HasSelection() &&
814                                         (charLoc.GreaterEqual(v.Cursor.CurSelection[0]) && charLoc.LessThan(v.Cursor.CurSelection[1]) ||
815                                                 charLoc.LessThan(v.Cursor.CurSelection[0]) && charLoc.GreaterEqual(v.Cursor.CurSelection[1])) {
816                                         // The current character is selected
817                                         lineStyle = defStyle.Reverse(true)
818
819                                         if style, ok := colorscheme["selection"]; ok {
820                                                 lineStyle = style
821                                         }
822                                 }
823
824                                 if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
825                                         v.Cursor.Y == char.realLoc.Y && v.Cursor.X == char.realLoc.X && !cursorSet {
826                                         screen.ShowCursor(xOffset+char.visualLoc.X, yOffset+char.visualLoc.Y)
827                                         cursorSet = true
828                                 }
829
830                                 if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num &&
831                                         !v.Cursor.HasSelection() && v.Cursor.Y == realLineN {
832                                         style := GetColor("cursor-line")
833                                         fg, _, _ := style.Decompose()
834                                         lineStyle = lineStyle.Background(fg)
835                                 }
836
837                                 screen.SetContent(xOffset+char.visualLoc.X, yOffset+char.visualLoc.Y, char.drawChar, nil, lineStyle)
838
839                                 lastChar = char
840                         }
841                 }
842
843                 lastX := 0
844                 var realLoc Loc
845                 var visualLoc Loc
846                 if lastChar != nil {
847                         lastX = xOffset + lastChar.visualLoc.X + lastChar.width
848                         if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
849                                 v.Cursor.Y == lastChar.realLoc.Y && v.Cursor.X == lastChar.realLoc.X+1 {
850                                 screen.ShowCursor(lastX, yOffset+lastChar.visualLoc.Y)
851                         }
852                         realLoc = Loc{lastChar.realLoc.X, realLineN}
853                         visualLoc = Loc{lastX - xOffset, lastChar.visualLoc.Y}
854                 } else if len(line) == 0 {
855                         if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
856                                 v.Cursor.Y == realLineN {
857                                 screen.ShowCursor(xOffset, yOffset+visualLineN)
858                         }
859                         lastX = xOffset
860                         realLoc = Loc{0, realLineN}
861                         visualLoc = Loc{0, visualLineN}
862                 }
863
864                 if v.Cursor.HasSelection() &&
865                         (realLoc.GreaterEqual(v.Cursor.CurSelection[0]) && realLoc.LessThan(v.Cursor.CurSelection[1]) ||
866                                 realLoc.LessThan(v.Cursor.CurSelection[0]) && realLoc.GreaterEqual(v.Cursor.CurSelection[1])) {
867                         // The current character is selected
868                         selectStyle := defStyle.Reverse(true)
869
870                         if style, ok := colorscheme["selection"]; ok {
871                                 selectStyle = style
872                         }
873                         screen.SetContent(xOffset+visualLoc.X, yOffset+visualLoc.Y, ' ', nil, selectStyle)
874                 }
875
876                 if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num &&
877                         !v.Cursor.HasSelection() && v.Cursor.Y == realLineN {
878                         for i := lastX; i < xOffset+v.Width; i++ {
879                                 style := GetColor("cursor-line")
880                                 fg, _, _ := style.Decompose()
881                                 style = style.Background(fg)
882                                 screen.SetContent(i, yOffset+visualLineN, ' ', nil, style)
883                         }
884                 }
885         }
886
887         if v.x != 0 && visualLineN < v.Height {
888                 for i := visualLineN + 1; i < v.Height; i++ {
889                         screen.SetContent(v.x, yOffset+i, '|', nil, defStyle.Reverse(true))
890                 }
891         }
892 }
893
894 // DisplayCursor draws the current buffer's cursor to the screen
895 func (v *View) DisplayCursor(x, y int) {
896         // screen.ShowCursor(v.x+v.Cursor.GetVisualX()+v.lineNumOffset-v.leftCol, y)
897         screen.ShowCursor(x, y)
898 }
899
900 // Display renders the view, the cursor, and statusline
901 func (v *View) Display() {
902         v.DisplayView()
903         // Don't draw the cursor if it is out of the viewport or if it has a selection
904         if (v.Cursor.Y-v.Topline < 0 || v.Cursor.Y-v.Topline > v.Height-1) || v.Cursor.HasSelection() {
905                 screen.HideCursor()
906         }
907         _, screenH := screen.Size()
908         if v.Buf.Settings["statusline"].(bool) {
909                 v.sline.Display()
910         } else if (v.y + v.Height) != screenH-1 {
911                 for x := 0; x < v.Width; x++ {
912                         screen.SetContent(v.x+x, v.y+v.Height, '-', nil, defStyle.Reverse(true))
913                 }
914         }
915 }