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