]> git.lizzy.rs Git - micro.git/blob - cmd/micro/view.go
34e29939d667c26db85dd39d9ebcc932add743c3
[micro.git] / cmd / micro / view.go
1 package main
2
3 import (
4         "os"
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 := os.Open(filename)
246         defer file.Close()
247
248         var buf *Buffer
249         if err != nil {
250                 messenger.Message(err.Error())
251                 // File does not exist -- create an empty buffer with that name
252                 buf = NewBuffer(strings.NewReader(""), filename)
253         } else {
254                 buf = NewBuffer(file, filename)
255         }
256         v.OpenBuffer(buf)
257 }
258
259 // CloseBuffer performs any closing functions on the buffer
260 func (v *View) CloseBuffer() {
261         if v.Buf != nil {
262                 v.Buf.Serialize()
263         }
264 }
265
266 // ReOpen reloads the current buffer
267 func (v *View) ReOpen() {
268         if v.CanClose() {
269                 screen.Clear()
270                 v.Buf.ReOpen()
271                 v.Relocate()
272                 v.matches = Match(v)
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.EventResize:
454                 // Window resized
455                 tabs[v.TabNum].Resize()
456         case *tcell.EventKey:
457                 // Check first if input is a key binding, if it is we 'eat' the input and don't insert a rune
458                 isBinding := false
459                 if e.Key() != tcell.KeyRune || e.Modifiers() != 0 {
460                         for key, actions := range bindings {
461                                 if e.Key() == key.keyCode {
462                                         if e.Key() == tcell.KeyRune {
463                                                 if e.Rune() != key.r {
464                                                         continue
465                                                 }
466                                         }
467                                         if e.Modifiers() == key.modifiers {
468                                                 relocate = false
469                                                 isBinding = true
470                                                 for _, action := range actions {
471                                                         relocate = action(v, true) || relocate
472                                                         funcName := FuncName(action)
473                                                         if funcName != "main.(*View).ToggleMacro" && funcName != "main.(*View).PlayMacro" {
474                                                                 if recordingMacro {
475                                                                         curMacro = append(curMacro, action)
476                                                                 }
477                                                         }
478                                                 }
479                                                 break
480                                         }
481                                 }
482                         }
483                 }
484                 if !isBinding && e.Key() == tcell.KeyRune {
485                         // Insert a character
486                         if v.Cursor.HasSelection() {
487                                 v.Cursor.DeleteSelection()
488                                 v.Cursor.ResetSelection()
489                         }
490                         v.Buf.Insert(v.Cursor.Loc, string(e.Rune()))
491                         v.Cursor.Right()
492
493                         for pl := range loadedPlugins {
494                                 _, err := Call(pl+".onRune", string(e.Rune()), v)
495                                 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
496                                         TermMessage(err)
497                                 }
498                         }
499
500                         if recordingMacro {
501                                 curMacro = append(curMacro, e.Rune())
502                         }
503                 }
504         case *tcell.EventPaste:
505                 if !PreActionCall("Paste", v) {
506                         break
507                 }
508
509                 v.paste(e.Text())
510
511                 PostActionCall("Paste", v)
512         case *tcell.EventMouse:
513                 x, y := e.Position()
514                 x -= v.lineNumOffset - v.leftCol + v.x
515                 y += v.Topline - v.y
516                 // Don't relocate for mouse events
517                 relocate = false
518
519                 button := e.Buttons()
520
521                 switch button {
522                 case tcell.Button1:
523                         // Left click
524                         if v.mouseReleased {
525                                 v.MoveToMouseClick(x, y)
526                                 if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold {
527                                         if v.doubleClick {
528                                                 // Triple click
529                                                 v.lastClickTime = time.Now()
530
531                                                 v.tripleClick = true
532                                                 v.doubleClick = false
533
534                                                 v.Cursor.SelectLine()
535                                         } else {
536                                                 // Double click
537                                                 v.lastClickTime = time.Now()
538
539                                                 v.doubleClick = true
540                                                 v.tripleClick = false
541
542                                                 v.Cursor.SelectWord()
543                                         }
544                                 } else {
545                                         v.doubleClick = false
546                                         v.tripleClick = false
547                                         v.lastClickTime = time.Now()
548
549                                         v.Cursor.OrigSelection[0] = v.Cursor.Loc
550                                         v.Cursor.CurSelection[0] = v.Cursor.Loc
551                                         v.Cursor.CurSelection[1] = v.Cursor.Loc
552                                 }
553                                 v.mouseReleased = false
554                         } else if !v.mouseReleased {
555                                 v.MoveToMouseClick(x, y)
556                                 if v.tripleClick {
557                                         v.Cursor.AddLineToSelection()
558                                 } else if v.doubleClick {
559                                         v.Cursor.AddWordToSelection()
560                                 } else {
561                                         v.Cursor.SetSelectionEnd(v.Cursor.Loc)
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                                 }
583                                 v.mouseReleased = true
584                         }
585                 case tcell.WheelUp:
586                         // Scroll up
587                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
588                         v.ScrollUp(scrollspeed)
589                 case tcell.WheelDown:
590                         // Scroll down
591                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
592                         v.ScrollDown(scrollspeed)
593                 }
594         }
595
596         if relocate {
597                 v.Relocate()
598         }
599 }
600
601 // GutterMessage creates a message in this view's gutter
602 func (v *View) GutterMessage(section string, lineN int, msg string, kind int) {
603         lineN--
604         gutterMsg := GutterMessage{
605                 lineNum: lineN,
606                 msg:     msg,
607                 kind:    kind,
608         }
609         for _, v := range v.messages {
610                 for _, gmsg := range v {
611                         if gmsg.lineNum == lineN {
612                                 return
613                         }
614                 }
615         }
616         messages := v.messages[section]
617         v.messages[section] = append(messages, gutterMsg)
618 }
619
620 // ClearGutterMessages clears all gutter messages from a given section
621 func (v *View) ClearGutterMessages(section string) {
622         v.messages[section] = []GutterMessage{}
623 }
624
625 // ClearAllGutterMessages clears all the gutter messages
626 func (v *View) ClearAllGutterMessages() {
627         for k := range v.messages {
628                 v.messages[k] = []GutterMessage{}
629         }
630 }
631
632 // Opens the given help page in a new horizontal split
633 func (v *View) openHelp(helpPage string) {
634         if data, err := FindRuntimeFile(RTHelp, helpPage).Data(); err != nil {
635                 TermMessage("Unable to load help text", helpPage, "\n", err)
636         } else {
637                 helpBuffer := NewBuffer(strings.NewReader(string(data)), helpPage+".md")
638                 helpBuffer.name = "Help"
639
640                 if v.Type == vtHelp {
641                         v.OpenBuffer(helpBuffer)
642                 } else {
643                         v.HSplit(helpBuffer)
644                         CurView().Type = vtHelp
645                 }
646         }
647 }
648
649 func (v *View) drawCell(x, y int, ch rune, combc []rune, style tcell.Style) {
650         if x >= v.x && x < v.x+v.Width && y >= v.y && y < v.y+v.Height {
651                 screen.SetContent(x, y, ch, combc, style)
652         }
653 }
654
655 // DisplayView renders the view to the screen
656 func (v *View) DisplayView() {
657         if v.Type == vtLog {
658                 // Log views should always follow the cursor...
659                 v.Relocate()
660         }
661
662         if v.Buf.Settings["syntax"].(bool) {
663                 v.matches = Match(v)
664         }
665
666         // The charNum we are currently displaying
667         // starts at the start of the viewport
668         charNum := Loc{0, v.Topline}
669
670         // Convert the length of buffer to a string, and get the length of the string
671         // We are going to have to offset by that amount
672         maxLineLength := len(strconv.Itoa(v.Buf.NumLines))
673
674         if v.Buf.Settings["ruler"] == true {
675                 // + 1 for the little space after the line number
676                 v.lineNumOffset = maxLineLength + 1
677         } else {
678                 v.lineNumOffset = 0
679         }
680
681         // We need to add to the line offset if there are gutter messages
682         var hasGutterMessages bool
683         for _, v := range v.messages {
684                 if len(v) > 0 {
685                         hasGutterMessages = true
686                 }
687         }
688         if hasGutterMessages {
689                 v.lineNumOffset += 2
690         }
691
692         if v.x != 0 {
693                 // One space for the extra split divider
694                 v.lineNumOffset++
695         }
696
697         // These represent the current screen coordinates
698         screenX, screenY := v.x, v.y-1
699
700         highlightStyle := defStyle
701         curLineN := 0
702
703         // ViewLine is the current line from the top of the viewport
704         for viewLine := 0; viewLine < v.Height; viewLine++ {
705                 screenY++
706                 screenX = v.x
707
708                 // This is the current line number of the buffer that we are drawing
709                 curLineN = viewLine + v.Topline
710
711                 if screenY-v.y >= v.Height {
712                         break
713                 }
714
715                 if v.x != 0 {
716                         // Draw the split divider
717                         v.drawCell(screenX, screenY, '|', nil, defStyle.Reverse(true))
718                         screenX++
719                 }
720
721                 // If the buffer is smaller than the view height we have to clear all this space
722                 if curLineN >= v.Buf.NumLines {
723                         for i := screenX; i < v.x+v.Width; i++ {
724                                 v.drawCell(i, screenY, ' ', nil, defStyle)
725                         }
726
727                         continue
728                 }
729                 line := v.Buf.Line(curLineN)
730
731                 // If there are gutter messages we need to display the '>>' symbol here
732                 if hasGutterMessages {
733                         // msgOnLine stores whether or not there is a gutter message on this line in particular
734                         msgOnLine := false
735                         for k := range v.messages {
736                                 for _, msg := range v.messages[k] {
737                                         if msg.lineNum == curLineN {
738                                                 msgOnLine = true
739                                                 gutterStyle := defStyle
740                                                 switch msg.kind {
741                                                 case GutterInfo:
742                                                         if style, ok := colorscheme["gutter-info"]; ok {
743                                                                 gutterStyle = style
744                                                         }
745                                                 case GutterWarning:
746                                                         if style, ok := colorscheme["gutter-warning"]; ok {
747                                                                 gutterStyle = style
748                                                         }
749                                                 case GutterError:
750                                                         if style, ok := colorscheme["gutter-error"]; ok {
751                                                                 gutterStyle = style
752                                                         }
753                                                 }
754                                                 v.drawCell(screenX, screenY, '>', nil, gutterStyle)
755                                                 screenX++
756                                                 v.drawCell(screenX, screenY, '>', nil, gutterStyle)
757                                                 screenX++
758                                                 if v.Cursor.Y == curLineN && !messenger.hasPrompt {
759                                                         messenger.Message(msg.msg)
760                                                         messenger.gutterMessage = true
761                                                 }
762                                         }
763                                 }
764                         }
765                         // If there is no message on this line we just display an empty offset
766                         if !msgOnLine {
767                                 v.drawCell(screenX, screenY, ' ', nil, defStyle)
768                                 screenX++
769                                 v.drawCell(screenX, screenY, ' ', nil, defStyle)
770                                 screenX++
771                                 if v.Cursor.Y == curLineN && messenger.gutterMessage {
772                                         messenger.Reset()
773                                         messenger.gutterMessage = false
774                                 }
775                         }
776                 }
777
778                 lineNumStyle := defStyle
779                 if v.Buf.Settings["ruler"] == true {
780                         // Write the line number
781                         if style, ok := colorscheme["line-number"]; ok {
782                                 lineNumStyle = style
783                         }
784                         if style, ok := colorscheme["current-line-number"]; ok {
785                                 if curLineN == v.Cursor.Y && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() {
786                                         lineNumStyle = style
787                                 }
788                         }
789
790                         lineNum := strconv.Itoa(curLineN + 1)
791
792                         // Write the spaces before the line number if necessary
793                         for i := 0; i < maxLineLength-len(lineNum); i++ {
794                                 v.drawCell(screenX, screenY, ' ', nil, lineNumStyle)
795                                 screenX++
796                         }
797                         // Write the actual line number
798                         for _, ch := range lineNum {
799                                 v.drawCell(screenX, screenY, ch, nil, lineNumStyle)
800                                 screenX++
801                         }
802
803                         // Write the extra space
804                         v.drawCell(screenX, screenY, ' ', nil, lineNumStyle)
805                         screenX++
806                 }
807
808                 // Now we actually draw the line
809                 colN := 0
810                 strWidth := 0
811                 tabSize := int(v.Buf.Settings["tabsize"].(float64))
812                 for _, ch := range line {
813                         if v.Buf.Settings["softwrap"].(bool) {
814                                 if screenX-v.x >= v.Width {
815                                         screenY++
816
817                                         x := 0
818                                         if hasGutterMessages {
819                                                 v.drawCell(v.x+x, screenY, ' ', nil, defStyle)
820                                                 x++
821                                                 v.drawCell(v.x+x, screenY, ' ', nil, defStyle)
822                                                 x++
823                                         }
824                                         for i := 0; i < v.lineNumOffset; i++ {
825                                                 screen.SetContent(v.x+i+x, screenY, ' ', nil, lineNumStyle)
826                                         }
827                                         screenX = v.x + v.lineNumOffset
828                                 }
829                         }
830
831                         if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN && colN == v.Cursor.X {
832                                 v.DisplayCursor(screenX-v.leftCol, screenY)
833                         }
834
835                         lineStyle := defStyle
836
837                         if v.Buf.Settings["syntax"].(bool) {
838                                 // Syntax highlighting is enabled
839                                 highlightStyle = v.matches[viewLine][colN]
840                         }
841
842                         if v.Cursor.HasSelection() &&
843                                 (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
844                                         charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
845                                 // The current character is selected
846                                 lineStyle = defStyle.Reverse(true)
847
848                                 if style, ok := colorscheme["selection"]; ok {
849                                         lineStyle = style
850                                 }
851                         } else {
852                                 lineStyle = highlightStyle
853                         }
854
855                         // We need to display the background of the linestyle with the correct color if cursorline is enabled
856                         // and this is the current view and there is no selection on this line and the cursor is on this line
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                                         lineStyle = lineStyle.Background(fg)
861                                 }
862                         }
863
864                         if ch == '\t' {
865                                 // If the character we are displaying is a tab, we need to do a bunch of special things
866
867                                 // First the user may have configured an `indent-char` to be displayed to show that this
868                                 // is a tab character
869                                 lineIndentStyle := defStyle
870                                 if style, ok := colorscheme["indent-char"]; ok && v.Buf.Settings["indentchar"].(string) != " " {
871                                         lineIndentStyle = style
872                                 }
873                                 if v.Cursor.HasSelection() &&
874                                         (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
875                                                 charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
876
877                                         lineIndentStyle = defStyle.Reverse(true)
878
879                                         if style, ok := colorscheme["selection"]; ok {
880                                                 lineIndentStyle = style
881                                         }
882                                 }
883                                 if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
884                                         if style, ok := colorscheme["cursor-line"]; ok {
885                                                 fg, _, _ := style.Decompose()
886                                                 lineIndentStyle = lineIndentStyle.Background(fg)
887                                         }
888                                 }
889                                 // Here we get the indent char
890                                 indentChar := []rune(v.Buf.Settings["indentchar"].(string))
891                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
892                                         v.drawCell(screenX-v.leftCol, screenY, indentChar[0], nil, lineIndentStyle)
893                                 }
894                                 // Now the tab has to be displayed as a bunch of spaces
895                                 visLoc := strWidth
896                                 remainder := tabSize - (visLoc % tabSize)
897                                 for i := 0; i < remainder-1; i++ {
898                                         screenX++
899                                         if screenX-v.x-v.leftCol >= v.lineNumOffset {
900                                                 v.drawCell(screenX-v.leftCol, screenY, ' ', nil, lineStyle)
901                                         }
902                                 }
903                                 strWidth += remainder
904                         } else if runewidth.RuneWidth(ch) > 1 {
905                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
906                                         v.drawCell(screenX, screenY, ch, nil, lineStyle)
907                                 }
908                                 for i := 0; i < runewidth.RuneWidth(ch)-1; i++ {
909                                         screenX++
910                                         if screenX-v.x-v.leftCol >= v.lineNumOffset {
911                                                 v.drawCell(screenX-v.leftCol, screenY, '<', nil, lineStyle)
912                                         }
913                                 }
914                                 strWidth += StringWidth(string(ch), tabSize)
915                         } else {
916                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
917                                         v.drawCell(screenX-v.leftCol, screenY, ch, nil, lineStyle)
918                                 }
919                                 strWidth += StringWidth(string(ch), tabSize)
920                         }
921                         charNum = charNum.Move(1, v.Buf)
922                         screenX++
923                         colN++
924                 }
925                 // Here we are at a newline
926
927                 if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN && colN == v.Cursor.X {
928                         v.DisplayCursor(screenX-v.leftCol, screenY)
929                 }
930
931                 // The newline may be selected, in which case we should draw the selection style
932                 // with a space to represent it
933                 if v.Cursor.HasSelection() &&
934                         (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
935                                 charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
936
937                         selectStyle := defStyle.Reverse(true)
938
939                         if style, ok := colorscheme["selection"]; ok {
940                                 selectStyle = style
941                         }
942                         v.drawCell(screenX, screenY, ' ', nil, selectStyle)
943                         screenX++
944                 }
945
946                 charNum = charNum.Move(1, v.Buf)
947
948                 for i := 0; i < v.Width; i++ {
949                         lineStyle := defStyle
950                         if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
951                                 if style, ok := colorscheme["cursor-line"]; ok {
952                                         fg, _, _ := style.Decompose()
953                                         lineStyle = lineStyle.Background(fg)
954                                 }
955                         }
956                         if screenX-v.x-v.leftCol+i >= v.lineNumOffset {
957                                 colorcolumn := int(v.Buf.Settings["colorcolumn"].(float64))
958                                 if colorcolumn != 0 && screenX-v.lineNumOffset+i == colorcolumn-1 {
959                                         if style, ok := colorscheme["color-column"]; ok {
960                                                 fg, _, _ := style.Decompose()
961                                                 lineStyle = lineStyle.Background(fg)
962                                         }
963                                 }
964                                 v.drawCell(screenX-v.leftCol+i, screenY, ' ', nil, lineStyle)
965                         }
966                 }
967         }
968 }
969
970 // DisplayCursor draws the current buffer's cursor to the screen
971 func (v *View) DisplayCursor(x, y int) {
972         // screen.ShowCursor(v.x+v.Cursor.GetVisualX()+v.lineNumOffset-v.leftCol, y)
973         screen.ShowCursor(x, y)
974 }
975
976 // Display renders the view, the cursor, and statusline
977 func (v *View) Display() {
978         v.DisplayView()
979         // Don't draw the cursor if it is out of the viewport or if it has a selection
980         if (v.Cursor.Y-v.Topline < 0 || v.Cursor.Y-v.Topline > v.Height-1) || v.Cursor.HasSelection() {
981                 screen.HideCursor()
982         }
983         _, screenH := screen.Size()
984         if v.Buf.Settings["statusline"].(bool) {
985                 v.sline.Display()
986         } else if (v.y + v.Height) != screenH-1 {
987                 for x := 0; x < v.Width; x++ {
988                         screen.SetContent(v.x+x, v.y+v.Height, '-', nil, defStyle.Reverse(true))
989                 }
990         }
991 }