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