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