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