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