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