]> git.lizzy.rs Git - micro.git/blob - cmd/micro/view.go
7351732b03ef134b53f02d83fed4f9401c98684c
[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, v.Buf.Settings["splitRight"].(bool))
278         return false
279 }
280
281 // VSplit opens a vertical split with the given buffer
282 func (v *View) VSplit(buf *Buffer) bool {
283         v.splitNode.VSplit(buf, v.Buf.Settings["splitBottom"].(bool))
284         return false
285 }
286
287 // GetSoftWrapLocation gets the location of a visual click on the screen and converts it to col,line
288 func (v *View) GetSoftWrapLocation(vx, vy int) (int, int) {
289         if !v.Buf.Settings["softwrap"].(bool) {
290                 vx = v.Cursor.GetCharPosInLine(vy, vx)
291                 return vx, vy
292         }
293
294         screenX, screenY := 0, v.Topline
295         for lineN := v.Topline; lineN < v.Bottomline(); lineN++ {
296                 line := v.Buf.Line(lineN)
297
298                 colN := 0
299                 for _, ch := range line {
300                         if screenX >= v.width-v.lineNumOffset {
301                                 screenX = 0
302                                 screenY++
303                         }
304
305                         if screenX == vx && screenY == vy {
306                                 return colN, lineN
307                         }
308
309                         if ch == '\t' {
310                                 screenX += int(v.Buf.Settings["tabsize"].(float64)) - 1
311                         }
312
313                         screenX++
314                         colN++
315                 }
316                 if screenY == vy {
317                         return colN, lineN
318                 }
319                 screenX = 0
320                 screenY++
321         }
322
323         return 0, 0
324 }
325
326 func (v *View) Bottomline() int {
327         if !v.Buf.Settings["softwrap"].(bool) {
328                 return v.Topline + v.height
329         }
330
331         screenX, screenY := 0, 0
332         numLines := 0
333         for lineN := v.Topline; lineN < v.Topline+v.height; lineN++ {
334                 line := v.Buf.Line(lineN)
335
336                 colN := 0
337                 for _, ch := range line {
338                         if screenX >= v.width-v.lineNumOffset {
339                                 screenX = 0
340                                 screenY++
341                         }
342
343                         if ch == '\t' {
344                                 screenX += int(v.Buf.Settings["tabsize"].(float64)) - 1
345                         }
346
347                         screenX++
348                         colN++
349                 }
350                 screenX = 0
351                 screenY++
352                 numLines++
353
354                 if screenY >= v.height {
355                         break
356                 }
357         }
358         return numLines + v.Topline
359 }
360
361 // Relocate moves the view window so that the cursor is in view
362 // This is useful if the user has scrolled far away, and then starts typing
363 func (v *View) Relocate() bool {
364         height := v.Bottomline() - v.Topline
365         ret := false
366         cy := v.Cursor.Y
367         scrollmargin := int(v.Buf.Settings["scrollmargin"].(float64))
368         if cy < v.Topline+scrollmargin && cy > scrollmargin-1 {
369                 v.Topline = cy - scrollmargin
370                 ret = true
371         } else if cy < v.Topline {
372                 v.Topline = cy
373                 ret = true
374         }
375         if cy > v.Topline+height-1-scrollmargin && cy < v.Buf.NumLines-scrollmargin {
376                 v.Topline = cy - height + 1 + scrollmargin
377                 ret = true
378         } else if cy >= v.Buf.NumLines-scrollmargin && cy > height {
379                 v.Topline = v.Buf.NumLines - height
380                 ret = true
381         }
382
383         if !v.Buf.Settings["softwrap"].(bool) {
384                 cx := v.Cursor.GetVisualX()
385                 if cx < v.leftCol {
386                         v.leftCol = cx
387                         ret = true
388                 }
389                 if cx+v.lineNumOffset+1 > v.leftCol+v.width {
390                         v.leftCol = cx - v.width + v.lineNumOffset + 1
391                         ret = true
392                 }
393         }
394         return ret
395 }
396
397 // MoveToMouseClick moves the cursor to location x, y assuming x, y were given
398 // by a mouse click
399 func (v *View) MoveToMouseClick(x, y int) {
400         if y-v.Topline > v.height-1 {
401                 v.ScrollDown(1)
402                 y = v.height + v.Topline - 1
403         }
404         if y >= v.Buf.NumLines {
405                 y = v.Buf.NumLines - 1
406         }
407         if y < 0 {
408                 y = 0
409         }
410         if x < 0 {
411                 x = 0
412         }
413
414         x, y = v.GetSoftWrapLocation(x, y)
415         // x = v.Cursor.GetCharPosInLine(y, x)
416         if x > Count(v.Buf.Line(y)) {
417                 x = Count(v.Buf.Line(y))
418         }
419         v.Cursor.X = x
420         v.Cursor.Y = y
421         v.Cursor.LastVisualX = v.Cursor.GetVisualX()
422 }
423
424 // HandleEvent handles an event passed by the main loop
425 func (v *View) HandleEvent(event tcell.Event) {
426         // This bool determines whether the view is relocated at the end of the function
427         // By default it's true because most events should cause a relocate
428         relocate := true
429
430         v.Buf.CheckModTime()
431
432         switch e := event.(type) {
433         case *tcell.EventResize:
434                 // Window resized
435                 tabs[v.TabNum].Resize()
436         case *tcell.EventKey:
437                 // Check first if input is a key binding, if it is we 'eat' the input and don't insert a rune
438                 isBinding := false
439                 if e.Key() != tcell.KeyRune || e.Modifiers() != 0 {
440                         for key, actions := range bindings {
441                                 if e.Key() == key.keyCode {
442                                         if e.Key() == tcell.KeyRune {
443                                                 if e.Rune() != key.r {
444                                                         continue
445                                                 }
446                                         }
447                                         if e.Modifiers() == key.modifiers {
448                                                 relocate = false
449                                                 isBinding = true
450                                                 for _, action := range actions {
451                                                         relocate = action(v, true) || relocate
452                                                         funcName := FuncName(action)
453                                                         if funcName != "main.(*View).ToggleMacro" && funcName != "main.(*View).PlayMacro" {
454                                                                 if recordingMacro {
455                                                                         curMacro = append(curMacro, action)
456                                                                 }
457                                                         }
458                                                 }
459                                                 break
460                                         }
461                                 }
462                         }
463                 }
464                 if !isBinding && e.Key() == tcell.KeyRune {
465                         // Insert a character
466                         if v.Cursor.HasSelection() {
467                                 v.Cursor.DeleteSelection()
468                                 v.Cursor.ResetSelection()
469                         }
470                         v.Buf.Insert(v.Cursor.Loc, string(e.Rune()))
471                         v.Cursor.Right()
472
473                         for _, pl := range loadedPlugins {
474                                 _, err := Call(pl+".onRune", string(e.Rune()), v)
475                                 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
476                                         TermMessage(err)
477                                 }
478                         }
479
480                         if recordingMacro {
481                                 curMacro = append(curMacro, e.Rune())
482                         }
483                 }
484         case *tcell.EventPaste:
485                 if !PreActionCall("Paste", v) {
486                         break
487                 }
488
489                 v.paste(e.Text())
490
491                 PostActionCall("Paste", v)
492         case *tcell.EventMouse:
493                 x, y := e.Position()
494                 x -= v.lineNumOffset - v.leftCol + v.x
495                 y += v.Topline - v.y
496                 // Don't relocate for mouse events
497                 relocate = false
498
499                 button := e.Buttons()
500
501                 switch button {
502                 case tcell.Button1:
503                         // Left click
504                         if v.mouseReleased {
505                                 v.MoveToMouseClick(x, y)
506                                 if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold {
507                                         if v.doubleClick {
508                                                 // Triple click
509                                                 v.lastClickTime = time.Now()
510
511                                                 v.tripleClick = true
512                                                 v.doubleClick = false
513
514                                                 v.Cursor.SelectLine()
515                                         } else {
516                                                 // Double click
517                                                 v.lastClickTime = time.Now()
518
519                                                 v.doubleClick = true
520                                                 v.tripleClick = false
521
522                                                 v.Cursor.SelectWord()
523                                         }
524                                 } else {
525                                         v.doubleClick = false
526                                         v.tripleClick = false
527                                         v.lastClickTime = time.Now()
528
529                                         v.Cursor.OrigSelection[0] = v.Cursor.Loc
530                                         v.Cursor.CurSelection[0] = v.Cursor.Loc
531                                         v.Cursor.CurSelection[1] = v.Cursor.Loc
532                                 }
533                                 v.mouseReleased = false
534                         } else if !v.mouseReleased {
535                                 v.MoveToMouseClick(x, y)
536                                 if v.tripleClick {
537                                         v.Cursor.AddLineToSelection()
538                                 } else if v.doubleClick {
539                                         v.Cursor.AddWordToSelection()
540                                 } else {
541                                         v.Cursor.SetSelectionEnd(v.Cursor.Loc)
542                                 }
543                         }
544                 case tcell.Button2:
545                         // Middle mouse button was clicked,
546                         // We should paste primary
547                         v.PastePrimary(true)
548                 case tcell.ButtonNone:
549                         // Mouse event with no click
550                         if !v.mouseReleased {
551                                 // Mouse was just released
552
553                                 // Relocating here isn't really necessary because the cursor will
554                                 // be in the right place from the last mouse event
555                                 // However, if we are running in a terminal that doesn't support mouse motion
556                                 // events, this still allows the user to make selections, except only after they
557                                 // release the mouse
558
559                                 if !v.doubleClick && !v.tripleClick {
560                                         v.MoveToMouseClick(x, y)
561                                         v.Cursor.SetSelectionEnd(v.Cursor.Loc)
562                                 }
563                                 v.mouseReleased = true
564                         }
565                 case tcell.WheelUp:
566                         // Scroll up
567                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
568                         v.ScrollUp(scrollspeed)
569                 case tcell.WheelDown:
570                         // Scroll down
571                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
572                         v.ScrollDown(scrollspeed)
573                 }
574         }
575
576         if relocate {
577                 v.Relocate()
578         }
579 }
580
581 // GutterMessage creates a message in this view's gutter
582 func (v *View) GutterMessage(section string, lineN int, msg string, kind int) {
583         lineN--
584         gutterMsg := GutterMessage{
585                 lineNum: lineN,
586                 msg:     msg,
587                 kind:    kind,
588         }
589         for _, v := range v.messages {
590                 for _, gmsg := range v {
591                         if gmsg.lineNum == lineN {
592                                 return
593                         }
594                 }
595         }
596         messages := v.messages[section]
597         v.messages[section] = append(messages, gutterMsg)
598 }
599
600 // ClearGutterMessages clears all gutter messages from a given section
601 func (v *View) ClearGutterMessages(section string) {
602         v.messages[section] = []GutterMessage{}
603 }
604
605 // ClearAllGutterMessages clears all the gutter messages
606 func (v *View) ClearAllGutterMessages() {
607         for k := range v.messages {
608                 v.messages[k] = []GutterMessage{}
609         }
610 }
611
612 // Opens the given help page in a new horizontal split
613 func (v *View) openHelp(helpPage string) {
614         if data, err := FindRuntimeFile(RTHelp, helpPage).Data(); err != nil {
615                 TermMessage("Unable to load help text", helpPage, "\n", err)
616         } else {
617                 helpBuffer := NewBuffer(data, helpPage+".md")
618                 helpBuffer.name = "Help"
619
620                 if v.Type == vtHelp {
621                         v.OpenBuffer(helpBuffer)
622                 } else {
623                         v.HSplit(helpBuffer)
624                         CurView().Type = vtHelp
625                 }
626         }
627 }
628
629 func (v *View) drawCell(x, y int, ch rune, combc []rune, style tcell.Style) {
630         if x >= v.x && x < v.x+v.width && y >= v.y && y < v.y+v.height {
631                 screen.SetContent(x, y, ch, combc, style)
632         }
633 }
634
635 // DisplayView renders the view to the screen
636 func (v *View) DisplayView() {
637         if v.Type == vtLog {
638                 // Log views should always follow the cursor...
639                 v.Relocate()
640         }
641
642         if v.Buf.Settings["syntax"].(bool) {
643                 v.matches = Match(v)
644         }
645
646         // The charNum we are currently displaying
647         // starts at the start of the viewport
648         charNum := Loc{0, v.Topline}
649
650         // Convert the length of buffer to a string, and get the length of the string
651         // We are going to have to offset by that amount
652         maxLineLength := len(strconv.Itoa(v.Buf.NumLines))
653
654         if v.Buf.Settings["ruler"] == true {
655                 // + 1 for the little space after the line number
656                 v.lineNumOffset = maxLineLength + 1
657         } else {
658                 v.lineNumOffset = 0
659         }
660
661         // We need to add to the line offset if there are gutter messages
662         var hasGutterMessages bool
663         for _, v := range v.messages {
664                 if len(v) > 0 {
665                         hasGutterMessages = true
666                 }
667         }
668         if hasGutterMessages {
669                 v.lineNumOffset += 2
670         }
671
672         if v.x != 0 {
673                 // One space for the extra split divider
674                 v.lineNumOffset++
675         }
676
677         // These represent the current screen coordinates
678         screenX, screenY := v.x, v.y-1
679
680         highlightStyle := defStyle
681         curLineN := 0
682
683         // ViewLine is the current line from the top of the viewport
684         for viewLine := 0; viewLine < v.height; viewLine++ {
685                 screenY++
686                 screenX = v.x
687
688                 // This is the current line number of the buffer that we are drawing
689                 curLineN = viewLine + v.Topline
690
691                 if screenY-v.y >= v.height {
692                         break
693                 }
694
695                 if v.x != 0 {
696                         // Draw the split divider
697                         v.drawCell(screenX, screenY, '|', nil, defStyle.Reverse(true))
698                         screenX++
699                 }
700
701                 // If the buffer is smaller than the view height we have to clear all this space
702                 if curLineN >= v.Buf.NumLines {
703                         for i := screenX; i < v.x+v.width; i++ {
704                                 v.drawCell(i, screenY, ' ', nil, defStyle)
705                         }
706
707                         continue
708                 }
709                 line := v.Buf.Line(curLineN)
710
711                 // If there are gutter messages we need to display the '>>' symbol here
712                 if hasGutterMessages {
713                         // msgOnLine stores whether or not there is a gutter message on this line in particular
714                         msgOnLine := false
715                         for k := range v.messages {
716                                 for _, msg := range v.messages[k] {
717                                         if msg.lineNum == curLineN {
718                                                 msgOnLine = true
719                                                 gutterStyle := defStyle
720                                                 switch msg.kind {
721                                                 case GutterInfo:
722                                                         if style, ok := colorscheme["gutter-info"]; ok {
723                                                                 gutterStyle = style
724                                                         }
725                                                 case GutterWarning:
726                                                         if style, ok := colorscheme["gutter-warning"]; ok {
727                                                                 gutterStyle = style
728                                                         }
729                                                 case GutterError:
730                                                         if style, ok := colorscheme["gutter-error"]; ok {
731                                                                 gutterStyle = style
732                                                         }
733                                                 }
734                                                 v.drawCell(screenX, screenY, '>', nil, gutterStyle)
735                                                 screenX++
736                                                 v.drawCell(screenX, screenY, '>', nil, gutterStyle)
737                                                 screenX++
738                                                 if v.Cursor.Y == curLineN && !messenger.hasPrompt {
739                                                         messenger.Message(msg.msg)
740                                                         messenger.gutterMessage = true
741                                                 }
742                                         }
743                                 }
744                         }
745                         // If there is no message on this line we just display an empty offset
746                         if !msgOnLine {
747                                 v.drawCell(screenX, screenY, ' ', nil, defStyle)
748                                 screenX++
749                                 v.drawCell(screenX, screenY, ' ', nil, defStyle)
750                                 screenX++
751                                 if v.Cursor.Y == curLineN && messenger.gutterMessage {
752                                         messenger.Reset()
753                                         messenger.gutterMessage = false
754                                 }
755                         }
756                 }
757
758                 lineNumStyle := defStyle
759                 if v.Buf.Settings["ruler"] == true {
760                         // Write the line number
761                         if style, ok := colorscheme["line-number"]; ok {
762                                 lineNumStyle = style
763                         }
764                         if style, ok := colorscheme["current-line-number"]; ok {
765                                 if curLineN == v.Cursor.Y && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() {
766                                         lineNumStyle = style
767                                 }
768                         }
769
770                         lineNum := strconv.Itoa(curLineN + 1)
771
772                         // Write the spaces before the line number if necessary
773                         for i := 0; i < maxLineLength-len(lineNum); i++ {
774                                 v.drawCell(screenX, screenY, ' ', nil, lineNumStyle)
775                                 screenX++
776                         }
777                         // Write the actual line number
778                         for _, ch := range lineNum {
779                                 v.drawCell(screenX, screenY, ch, nil, lineNumStyle)
780                                 screenX++
781                         }
782
783                         // Write the extra space
784                         v.drawCell(screenX, screenY, ' ', nil, lineNumStyle)
785                         screenX++
786                 }
787
788                 // Now we actually draw the line
789                 colN := 0
790                 strWidth := 0
791                 tabSize := int(v.Buf.Settings["tabsize"].(float64))
792                 for _, ch := range line {
793                         if v.Buf.Settings["softwrap"].(bool) {
794                                 if screenX-v.x >= v.width {
795                                         screenY++
796                                         for i := 0; i < v.lineNumOffset; i++ {
797                                                 screen.SetContent(v.x+i, screenY, ' ', nil, lineNumStyle)
798                                         }
799                                         screenX = v.x + v.lineNumOffset
800                                 }
801                         }
802
803                         if tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN && colN == v.Cursor.X {
804                                 v.DisplayCursor(screenX-v.leftCol, screenY)
805                         }
806
807                         lineStyle := defStyle
808
809                         if v.Buf.Settings["syntax"].(bool) {
810                                 // Syntax highlighting is enabled
811                                 highlightStyle = v.matches[viewLine][colN]
812                         }
813
814                         if v.Cursor.HasSelection() &&
815                                 (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
816                                         charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
817                                 // The current character is selected
818                                 lineStyle = defStyle.Reverse(true)
819
820                                 if style, ok := colorscheme["selection"]; ok {
821                                         lineStyle = style
822                                 }
823                         } else {
824                                 lineStyle = highlightStyle
825                         }
826
827                         // We need to display the background of the linestyle with the correct color if cursorline is enabled
828                         // and this is the current view and there is no selection on this line and the cursor is on this line
829                         if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
830                                 if style, ok := colorscheme["cursor-line"]; ok {
831                                         fg, _, _ := style.Decompose()
832                                         lineStyle = lineStyle.Background(fg)
833                                 }
834                         }
835
836                         if ch == '\t' {
837                                 // If the character we are displaying is a tab, we need to do a bunch of special things
838
839                                 // First the user may have configured an `indent-char` to be displayed to show that this
840                                 // is a tab character
841                                 lineIndentStyle := defStyle
842                                 if style, ok := colorscheme["indent-char"]; ok && v.Buf.Settings["indentchar"].(string) != " " {
843                                         lineIndentStyle = style
844                                 }
845                                 if v.Cursor.HasSelection() &&
846                                         (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
847                                                 charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
848
849                                         lineIndentStyle = defStyle.Reverse(true)
850
851                                         if style, ok := colorscheme["selection"]; ok {
852                                                 lineIndentStyle = style
853                                         }
854                                 }
855                                 if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
856                                         if style, ok := colorscheme["cursor-line"]; ok {
857                                                 fg, _, _ := style.Decompose()
858                                                 lineIndentStyle = lineIndentStyle.Background(fg)
859                                         }
860                                 }
861                                 // Here we get the indent char
862                                 indentChar := []rune(v.Buf.Settings["indentchar"].(string))
863                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
864                                         v.drawCell(screenX-v.leftCol, screenY, indentChar[0], nil, lineIndentStyle)
865                                 }
866                                 // Now the tab has to be displayed as a bunch of spaces
867                                 visLoc := strWidth
868                                 remainder := tabSize - (visLoc % tabSize)
869                                 for i := 0; i < remainder-1; i++ {
870                                         screenX++
871                                         if screenX-v.x-v.leftCol >= v.lineNumOffset {
872                                                 v.drawCell(screenX-v.leftCol, screenY, ' ', nil, lineStyle)
873                                         }
874                                 }
875                         } else if runewidth.RuneWidth(ch) > 1 {
876                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
877                                         v.drawCell(screenX, screenY, ch, nil, lineStyle)
878                                 }
879                                 for i := 0; i < runewidth.RuneWidth(ch)-1; i++ {
880                                         screenX++
881                                         if screenX-v.x-v.leftCol >= v.lineNumOffset {
882                                                 v.drawCell(screenX-v.leftCol, screenY, '<', nil, lineStyle)
883                                         }
884                                 }
885                         } else {
886                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
887                                         v.drawCell(screenX-v.leftCol, screenY, ch, nil, lineStyle)
888                                 }
889                         }
890                         charNum = charNum.Move(1, v.Buf)
891                         screenX++
892                         colN++
893                         strWidth += StringWidth(string(ch), tabSize)
894                 }
895                 // Here we are at a newline
896
897                 if tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN && colN == v.Cursor.X {
898                         v.DisplayCursor(screenX-v.leftCol, screenY)
899                 }
900
901                 // The newline may be selected, in which case we should draw the selection style
902                 // with a space to represent it
903                 if v.Cursor.HasSelection() &&
904                         (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
905                                 charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
906
907                         selectStyle := defStyle.Reverse(true)
908
909                         if style, ok := colorscheme["selection"]; ok {
910                                 selectStyle = style
911                         }
912                         v.drawCell(screenX, screenY, ' ', nil, selectStyle)
913                         screenX++
914                 }
915
916                 charNum = charNum.Move(1, v.Buf)
917
918                 for i := 0; i < v.width; i++ {
919                         lineStyle := defStyle
920                         if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
921                                 if style, ok := colorscheme["cursor-line"]; ok {
922                                         fg, _, _ := style.Decompose()
923                                         lineStyle = lineStyle.Background(fg)
924                                 }
925                         }
926                         if screenX-v.x-v.leftCol+i >= v.lineNumOffset {
927                                 colorcolumn := int(v.Buf.Settings["colorcolumn"].(float64))
928                                 if colorcolumn != 0 && screenX-v.lineNumOffset+i == colorcolumn-1 {
929                                         if style, ok := colorscheme["color-column"]; ok {
930                                                 fg, _, _ := style.Decompose()
931                                                 lineStyle = lineStyle.Background(fg)
932                                         }
933                                 }
934                                 v.drawCell(screenX-v.leftCol+i, screenY, ' ', nil, lineStyle)
935                         }
936                 }
937         }
938 }
939
940 // DisplayCursor draws the current buffer's cursor to the screen
941 func (v *View) DisplayCursor(x, y int) {
942         // screen.ShowCursor(v.x+v.Cursor.GetVisualX()+v.lineNumOffset-v.leftCol, y)
943         screen.ShowCursor(x, y)
944 }
945
946 // Display renders the view, the cursor, and statusline
947 func (v *View) Display() {
948         v.DisplayView()
949         // Don't draw the cursor if it is out of the viewport or if it has a selection
950         if (v.Cursor.Y-v.Topline < 0 || v.Cursor.Y-v.Topline > v.height-1) || v.Cursor.HasSelection() {
951                 screen.HideCursor()
952         }
953         _, screenH := screen.Size()
954         if v.Buf.Settings["statusline"].(bool) {
955                 v.sline.Display()
956         } else if (v.y + v.height) != screenH-1 {
957                 for x := 0; x < v.width; x++ {
958                         screen.SetContent(v.x+x, v.y+v.height, '-', nil, defStyle.Reverse(true))
959                 }
960         }
961 }