]> git.lizzy.rs Git - micro.git/blob - cmd/micro/view.go
5f80f55d632983bd8edc033ebc099aaa2b473c55
[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.Buf.Settings["softwrap"].(bool) && v.leftCol != 0 {
666                 v.leftCol = 0
667         }
668
669         if v.Type == vtLog {
670                 // Log views should always follow the cursor...
671                 v.Relocate()
672         }
673
674         // We need to know the string length of the largest line number
675         // so we can pad appropriately when displaying line numbers
676         maxLineNumLength := len(strconv.Itoa(v.Buf.NumLines))
677
678         if v.Buf.Settings["ruler"] == true {
679                 // + 1 for the little space after the line number
680                 v.lineNumOffset = maxLineNumLength + 1
681         } else {
682                 v.lineNumOffset = 0
683         }
684
685         // We need to add to the line offset if there are gutter messages
686         var hasGutterMessages bool
687         for _, v := range v.messages {
688                 if len(v) > 0 {
689                         hasGutterMessages = true
690                 }
691         }
692         if hasGutterMessages {
693                 v.lineNumOffset += 2
694         }
695
696         divider := 0
697         if v.x != 0 {
698                 // One space for the extra split divider
699                 v.lineNumOffset++
700                 divider = 1
701         }
702
703         xOffset := v.x + v.lineNumOffset
704         yOffset := v.y
705
706         height := v.Height
707         width := v.Width
708         left := v.leftCol
709         top := v.Topline
710
711         v.cellview.Draw(v.Buf, top, height, left, width-v.lineNumOffset)
712
713         screenX := v.x
714         realLineN := top - 1
715         visualLineN := 0
716         var line []*Char
717         for visualLineN, line = range v.cellview.lines {
718                 var firstChar *Char
719                 if len(line) > 0 {
720                         firstChar = line[0]
721                 }
722
723                 var softwrapped bool
724                 if firstChar != nil {
725                         if firstChar.realLoc.Y == realLineN {
726                                 softwrapped = true
727                         }
728                         realLineN = firstChar.realLoc.Y
729                 } else {
730                         realLineN++
731                 }
732
733                 colorcolumn := int(v.Buf.Settings["colorcolumn"].(float64))
734                 if colorcolumn != 0 {
735                         style := GetColor("color-column")
736                         fg, _, _ := style.Decompose()
737                         st := defStyle.Background(fg)
738                         screen.SetContent(xOffset+colorcolumn, yOffset+visualLineN, ' ', nil, st)
739                 }
740
741                 screenX = v.x
742
743                 // If there are gutter messages we need to display the '>>' symbol here
744                 if hasGutterMessages {
745                         // msgOnLine stores whether or not there is a gutter message on this line in particular
746                         msgOnLine := false
747                         for k := range v.messages {
748                                 for _, msg := range v.messages[k] {
749                                         if msg.lineNum == realLineN {
750                                                 msgOnLine = true
751                                                 gutterStyle := defStyle
752                                                 switch msg.kind {
753                                                 case GutterInfo:
754                                                         if style, ok := colorscheme["gutter-info"]; ok {
755                                                                 gutterStyle = style
756                                                         }
757                                                 case GutterWarning:
758                                                         if style, ok := colorscheme["gutter-warning"]; ok {
759                                                                 gutterStyle = style
760                                                         }
761                                                 case GutterError:
762                                                         if style, ok := colorscheme["gutter-error"]; ok {
763                                                                 gutterStyle = style
764                                                         }
765                                                 }
766                                                 screen.SetContent(screenX, yOffset+visualLineN, '>', nil, gutterStyle)
767                                                 screenX++
768                                                 screen.SetContent(screenX, yOffset+visualLineN, '>', nil, gutterStyle)
769                                                 screenX++
770                                                 if v.Cursor.Y == realLineN && !messenger.hasPrompt {
771                                                         messenger.Message(msg.msg)
772                                                         messenger.gutterMessage = true
773                                                 }
774                                         }
775                                 }
776                         }
777                         // If there is no message on this line we just display an empty offset
778                         if !msgOnLine {
779                                 screen.SetContent(screenX, yOffset+visualLineN, ' ', nil, defStyle)
780                                 screenX++
781                                 screen.SetContent(screenX, yOffset+visualLineN, ' ', nil, defStyle)
782                                 screenX++
783                                 if v.Cursor.Y == realLineN && messenger.gutterMessage {
784                                         messenger.Reset()
785                                         messenger.gutterMessage = false
786                                 }
787                         }
788                 }
789
790                 lineNumStyle := defStyle
791                 if v.Buf.Settings["ruler"] == true {
792                         // Write the line number
793                         if style, ok := colorscheme["line-number"]; ok {
794                                 lineNumStyle = style
795                         }
796                         if style, ok := colorscheme["current-line-number"]; ok {
797                                 if realLineN == v.Cursor.Y && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() {
798                                         lineNumStyle = style
799                                 }
800                         }
801
802                         lineNum := strconv.Itoa(realLineN + 1)
803
804                         // Write the spaces before the line number if necessary
805                         for i := 0; i < maxLineNumLength-len(lineNum); i++ {
806                                 screen.SetContent(screenX+divider, yOffset+visualLineN, ' ', nil, lineNumStyle)
807                                 screenX++
808                         }
809                         if softwrapped && visualLineN != 0 {
810                                 // Pad without the line number because it was written on the visual line before
811                                 for range lineNum {
812                                         screen.SetContent(screenX+divider, yOffset+visualLineN, ' ', nil, lineNumStyle)
813                                         screenX++
814                                 }
815                         } else {
816                                 // Write the actual line number
817                                 for _, ch := range lineNum {
818                                         screen.SetContent(screenX+divider, yOffset+visualLineN, ch, nil, lineNumStyle)
819                                         screenX++
820                                 }
821                         }
822
823                         // Write the extra space
824                         screen.SetContent(screenX+divider, yOffset+visualLineN, ' ', nil, lineNumStyle)
825                         screenX++
826                 }
827
828                 var lastChar *Char
829                 cursorSet := false
830                 for _, char := range line {
831                         if char != nil {
832                                 lineStyle := char.style
833
834                                 colorcolumn := int(v.Buf.Settings["colorcolumn"].(float64))
835                                 if colorcolumn != 0 && char.visualLoc.X == colorcolumn {
836                                         style := GetColor("color-column")
837                                         fg, _, _ := style.Decompose()
838                                         lineStyle = lineStyle.Background(fg)
839                                 }
840
841                                 charLoc := char.realLoc
842                                 if v.Cursor.HasSelection() &&
843                                         (charLoc.GreaterEqual(v.Cursor.CurSelection[0]) && charLoc.LessThan(v.Cursor.CurSelection[1]) ||
844                                                 charLoc.LessThan(v.Cursor.CurSelection[0]) && charLoc.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                                 }
852
853                                 if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
854                                         v.Cursor.Y == char.realLoc.Y && v.Cursor.X == char.realLoc.X && !cursorSet {
855                                         screen.ShowCursor(xOffset+char.visualLoc.X, yOffset+char.visualLoc.Y)
856                                         cursorSet = true
857                                 }
858
859                                 if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num &&
860                                         !v.Cursor.HasSelection() && v.Cursor.Y == realLineN {
861                                         style := GetColor("cursor-line")
862                                         fg, _, _ := style.Decompose()
863                                         lineStyle = lineStyle.Background(fg)
864                                 }
865
866                                 screen.SetContent(xOffset+char.visualLoc.X, yOffset+char.visualLoc.Y, char.drawChar, nil, lineStyle)
867
868                                 lastChar = char
869                         }
870                 }
871
872                 lastX := 0
873                 var realLoc Loc
874                 var visualLoc Loc
875                 var cx, cy int
876                 if lastChar != nil {
877                         lastX = xOffset + lastChar.visualLoc.X + lastChar.width
878                         if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
879                                 v.Cursor.Y == lastChar.realLoc.Y && v.Cursor.X == lastChar.realLoc.X+1 {
880                                 screen.ShowCursor(lastX, yOffset+lastChar.visualLoc.Y)
881                                 cx, cy = lastX, yOffset+lastChar.visualLoc.Y
882                         }
883                         realLoc = Loc{lastChar.realLoc.X, realLineN}
884                         visualLoc = Loc{lastX - xOffset, lastChar.visualLoc.Y}
885                 } else if len(line) == 0 {
886                         if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
887                                 v.Cursor.Y == realLineN {
888                                 screen.ShowCursor(xOffset, yOffset+visualLineN)
889                                 cx, cy = xOffset, yOffset+visualLineN
890                         }
891                         lastX = xOffset
892                         realLoc = Loc{0, realLineN}
893                         visualLoc = Loc{0, visualLineN}
894                 }
895
896                 if v.Cursor.HasSelection() &&
897                         (realLoc.GreaterEqual(v.Cursor.CurSelection[0]) && realLoc.LessThan(v.Cursor.CurSelection[1]) ||
898                                 realLoc.LessThan(v.Cursor.CurSelection[0]) && realLoc.GreaterEqual(v.Cursor.CurSelection[1])) {
899                         // The current character is selected
900                         selectStyle := defStyle.Reverse(true)
901
902                         if style, ok := colorscheme["selection"]; ok {
903                                 selectStyle = style
904                         }
905                         screen.SetContent(xOffset+visualLoc.X, yOffset+visualLoc.Y, ' ', nil, selectStyle)
906                 }
907
908                 if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num &&
909                         !v.Cursor.HasSelection() && v.Cursor.Y == realLineN {
910                         for i := lastX; i < xOffset+v.Width; i++ {
911                                 style := GetColor("cursor-line")
912                                 fg, _, _ := style.Decompose()
913                                 style = style.Background(fg)
914                                 if !(tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && i == cx && yOffset+visualLineN == cy) {
915                                         screen.SetContent(i, yOffset+visualLineN, ' ', nil, style)
916                                 }
917                         }
918                 }
919         }
920
921         if divider != 0 {
922                 dividerStyle := defStyle
923                 if style, ok := colorscheme["divider"]; ok {
924                         dividerStyle = style
925                 }
926                 for i := 0; i < v.Height; i++ {
927                         screen.SetContent(v.x, yOffset+i, '|', nil, dividerStyle.Reverse(true))
928                 }
929         }
930 }
931
932 // DisplayCursor draws the current buffer's cursor to the screen
933 func (v *View) DisplayCursor(x, y int) {
934         // screen.ShowCursor(v.x+v.Cursor.GetVisualX()+v.lineNumOffset-v.leftCol, y)
935         screen.ShowCursor(x, y)
936 }
937
938 // Display renders the view, the cursor, and statusline
939 func (v *View) Display() {
940         if GetGlobalOption("termtitle").(bool) {
941                 screen.SetTitle("micro: " + v.Buf.GetName())
942         }
943         v.DisplayView()
944         // Don't draw the cursor if it is out of the viewport or if it has a selection
945         if (v.Cursor.Y-v.Topline < 0 || v.Cursor.Y-v.Topline > v.Height-1) || v.Cursor.HasSelection() {
946                 screen.HideCursor()
947         }
948         _, screenH := screen.Size()
949         if v.Buf.Settings["statusline"].(bool) {
950                 v.sline.Display()
951         } else if (v.y + v.Height) != screenH-1 {
952                 for x := 0; x < v.Width; x++ {
953                         screen.SetContent(v.x+x, v.y+v.Height, '-', nil, defStyle.Reverse(true))
954                 }
955         }
956 }