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