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