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