]> git.lizzy.rs Git - micro.git/blob - cmd/micro/view.go
Add function to load runtime files from a directory for a plugin
[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                                         }
368                                 }
369                         }
370                 }
371                 if !isBinding && e.Key() == tcell.KeyRune {
372                         // Insert a character
373                         if v.Cursor.HasSelection() {
374                                 v.Cursor.DeleteSelection()
375                                 v.Cursor.ResetSelection()
376                         }
377                         v.Buf.Insert(v.Cursor.Loc, string(e.Rune()))
378                         v.Cursor.Right()
379
380                         for _, pl := range loadedPlugins {
381                                 _, err := Call(pl+".onRune", string(e.Rune()), v)
382                                 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
383                                         TermMessage(err)
384                                 }
385                         }
386
387                         if recordingMacro {
388                                 curMacro = append(curMacro, e.Rune())
389                         }
390                 }
391         case *tcell.EventPaste:
392                 if !PreActionCall("Paste", v) {
393                         break
394                 }
395
396                 leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
397
398                 if v.Cursor.HasSelection() {
399                         v.Cursor.DeleteSelection()
400                         v.Cursor.ResetSelection()
401                 }
402                 clip := e.Text()
403                 clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
404                 v.Buf.Insert(v.Cursor.Loc, clip)
405                 v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
406                 v.freshClip = false
407                 messenger.Message("Pasted clipboard")
408
409                 PostActionCall("Paste", v)
410         case *tcell.EventMouse:
411                 x, y := e.Position()
412                 x -= v.lineNumOffset - v.leftCol + v.x
413                 y += v.Topline - v.y
414                 // Don't relocate for mouse events
415                 relocate = false
416
417                 button := e.Buttons()
418
419                 switch button {
420                 case tcell.Button1:
421                         // Left click
422                         if v.mouseReleased {
423                                 v.MoveToMouseClick(x, y)
424                                 if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold {
425                                         if v.doubleClick {
426                                                 // Triple click
427                                                 v.lastClickTime = time.Now()
428
429                                                 v.tripleClick = true
430                                                 v.doubleClick = false
431
432                                                 v.Cursor.SelectLine()
433                                         } else {
434                                                 // Double click
435                                                 v.lastClickTime = time.Now()
436
437                                                 v.doubleClick = true
438                                                 v.tripleClick = false
439
440                                                 v.Cursor.SelectWord()
441                                         }
442                                 } else {
443                                         v.doubleClick = false
444                                         v.tripleClick = false
445                                         v.lastClickTime = time.Now()
446
447                                         v.Cursor.OrigSelection[0] = v.Cursor.Loc
448                                         v.Cursor.CurSelection[0] = v.Cursor.Loc
449                                         v.Cursor.CurSelection[1] = v.Cursor.Loc
450                                 }
451                                 v.mouseReleased = false
452                         } else if !v.mouseReleased {
453                                 v.MoveToMouseClick(x, y)
454                                 if v.tripleClick {
455                                         v.Cursor.AddLineToSelection()
456                                 } else if v.doubleClick {
457                                         v.Cursor.AddWordToSelection()
458                                 } else {
459                                         v.Cursor.SetSelectionEnd(v.Cursor.Loc)
460                                 }
461                         }
462                 case tcell.Button2:
463                         // Middle mouse button was clicked,
464                         // We should paste primary
465                         v.PastePrimary(true)
466                 case tcell.ButtonNone:
467                         // Mouse event with no click
468                         if !v.mouseReleased {
469                                 // Mouse was just released
470
471                                 // Relocating here isn't really necessary because the cursor will
472                                 // be in the right place from the last mouse event
473                                 // However, if we are running in a terminal that doesn't support mouse motion
474                                 // events, this still allows the user to make selections, except only after they
475                                 // release the mouse
476
477                                 if !v.doubleClick && !v.tripleClick {
478                                         v.MoveToMouseClick(x, y)
479                                         v.Cursor.SetSelectionEnd(v.Cursor.Loc)
480                                 }
481                                 v.mouseReleased = true
482                         }
483                 case tcell.WheelUp:
484                         // Scroll up
485                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
486                         v.ScrollUp(scrollspeed)
487                 case tcell.WheelDown:
488                         // Scroll down
489                         scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
490                         v.ScrollDown(scrollspeed)
491                 }
492         }
493
494         if relocate {
495                 v.Relocate()
496         }
497 }
498
499 // GutterMessage creates a message in this view's gutter
500 func (v *View) GutterMessage(section string, lineN int, msg string, kind int) {
501         lineN--
502         gutterMsg := GutterMessage{
503                 lineNum: lineN,
504                 msg:     msg,
505                 kind:    kind,
506         }
507         for _, v := range v.messages {
508                 for _, gmsg := range v {
509                         if gmsg.lineNum == lineN {
510                                 return
511                         }
512                 }
513         }
514         messages := v.messages[section]
515         v.messages[section] = append(messages, gutterMsg)
516 }
517
518 // ClearGutterMessages clears all gutter messages from a given section
519 func (v *View) ClearGutterMessages(section string) {
520         v.messages[section] = []GutterMessage{}
521 }
522
523 // ClearAllGutterMessages clears all the gutter messages
524 func (v *View) ClearAllGutterMessages() {
525         for k := range v.messages {
526                 v.messages[k] = []GutterMessage{}
527         }
528 }
529
530 // Opens the given help page in a new horizontal split
531 func (v *View) openHelp(helpPage string) {
532         if data, err := FindRuntimeFile(RTHelp, helpPage).Data(); err != nil {
533                 TermMessage("Unable to load help text", helpPage, "\n", err)
534         } else {
535                 helpBuffer := NewBuffer(data, helpPage+".md")
536                 helpBuffer.Name = "Help"
537
538                 if v.Help {
539                         v.OpenBuffer(helpBuffer)
540                 } else {
541                         v.HSplit(helpBuffer)
542                         CurView().Help = true
543                 }
544         }
545 }
546
547 func (v *View) drawCell(x, y int, ch rune, combc []rune, style tcell.Style) {
548         if x >= v.x && x < v.x+v.width && y >= v.y && y < v.y+v.height {
549                 screen.SetContent(x, y, ch, combc, style)
550         }
551 }
552
553 // DisplayView renders the view to the screen
554 func (v *View) DisplayView() {
555         if v.Buf.Settings["syntax"].(bool) {
556                 v.matches = Match(v)
557         }
558         // The charNum we are currently displaying
559         // starts at the start of the viewport
560         charNum := Loc{0, v.Topline}
561
562         // Convert the length of buffer to a string, and get the length of the string
563         // We are going to have to offset by that amount
564         maxLineLength := len(strconv.Itoa(v.Buf.NumLines))
565
566         if v.Buf.Settings["ruler"] == true {
567                 // + 1 for the little space after the line number
568                 v.lineNumOffset = maxLineLength + 1
569         } else {
570                 v.lineNumOffset = 0
571         }
572
573         // We need to add to the line offset if there are gutter messages
574         var hasGutterMessages bool
575         for _, v := range v.messages {
576                 if len(v) > 0 {
577                         hasGutterMessages = true
578                 }
579         }
580         if hasGutterMessages {
581                 v.lineNumOffset += 2
582         }
583
584         if v.x != 0 {
585                 // One space for the extra split divider
586                 v.lineNumOffset++
587         }
588
589         // These represent the current screen coordinates
590         screenX, screenY := 0, 0
591
592         highlightStyle := defStyle
593
594         // ViewLine is the current line from the top of the viewport
595         for viewLine := 0; viewLine < v.height; viewLine++ {
596                 screenY = v.y + viewLine
597                 screenX = v.x
598
599                 // This is the current line number of the buffer that we are drawing
600                 curLineN := viewLine + v.Topline
601
602                 if v.x != 0 {
603                         // Draw the split divider
604                         v.drawCell(screenX, screenY, '|', nil, defStyle.Reverse(true))
605                         screenX++
606                 }
607
608                 // If the buffer is smaller than the view height we have to clear all this space
609                 if curLineN >= v.Buf.NumLines {
610                         for i := screenX; i < v.x+v.width; i++ {
611                                 v.drawCell(i, screenY, ' ', nil, defStyle)
612                         }
613
614                         continue
615                 }
616                 line := v.Buf.Line(curLineN)
617
618                 // If there are gutter messages we need to display the '>>' symbol here
619                 if hasGutterMessages {
620                         // msgOnLine stores whether or not there is a gutter message on this line in particular
621                         msgOnLine := false
622                         for k := range v.messages {
623                                 for _, msg := range v.messages[k] {
624                                         if msg.lineNum == curLineN {
625                                                 msgOnLine = true
626                                                 gutterStyle := defStyle
627                                                 switch msg.kind {
628                                                 case GutterInfo:
629                                                         if style, ok := colorscheme["gutter-info"]; ok {
630                                                                 gutterStyle = style
631                                                         }
632                                                 case GutterWarning:
633                                                         if style, ok := colorscheme["gutter-warning"]; ok {
634                                                                 gutterStyle = style
635                                                         }
636                                                 case GutterError:
637                                                         if style, ok := colorscheme["gutter-error"]; ok {
638                                                                 gutterStyle = style
639                                                         }
640                                                 }
641                                                 v.drawCell(screenX, screenY, '>', nil, gutterStyle)
642                                                 screenX++
643                                                 v.drawCell(screenX, screenY, '>', nil, gutterStyle)
644                                                 screenX++
645                                                 if v.Cursor.Y == curLineN && !messenger.hasPrompt {
646                                                         messenger.Message(msg.msg)
647                                                         messenger.gutterMessage = true
648                                                 }
649                                         }
650                                 }
651                         }
652                         // If there is no message on this line we just display an empty offset
653                         if !msgOnLine {
654                                 v.drawCell(screenX, screenY, ' ', nil, defStyle)
655                                 screenX++
656                                 v.drawCell(screenX, screenY, ' ', nil, defStyle)
657                                 screenX++
658                                 if v.Cursor.Y == curLineN && messenger.gutterMessage {
659                                         messenger.Reset()
660                                         messenger.gutterMessage = false
661                                 }
662                         }
663                 }
664
665                 if v.Buf.Settings["ruler"] == true {
666                         // Write the line number
667                         lineNumStyle := defStyle
668                         if style, ok := colorscheme["line-number"]; ok {
669                                 lineNumStyle = style
670                         }
671                         if style, ok := colorscheme["current-line-number"]; ok {
672                                 if curLineN == v.Cursor.Y && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() {
673                                         lineNumStyle = style
674                                 }
675                         }
676
677                         lineNum := strconv.Itoa(curLineN + 1)
678
679                         // Write the spaces before the line number if necessary
680                         for i := 0; i < maxLineLength-len(lineNum); i++ {
681                                 v.drawCell(screenX, screenY, ' ', nil, lineNumStyle)
682                                 screenX++
683                         }
684                         // Write the actual line number
685                         for _, ch := range lineNum {
686                                 v.drawCell(screenX, screenY, ch, nil, lineNumStyle)
687                                 screenX++
688                         }
689
690                         // Write the extra space
691                         v.drawCell(screenX, screenY, ' ', nil, lineNumStyle)
692                         screenX++
693                 }
694
695                 // Now we actually draw the line
696                 colN := 0
697                 for _, ch := range line {
698                         lineStyle := defStyle
699
700                         if v.Buf.Settings["syntax"].(bool) {
701                                 // Syntax highlighting is enabled
702                                 highlightStyle = v.matches[viewLine][colN]
703                         }
704
705                         if v.Cursor.HasSelection() &&
706                                 (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
707                                         charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
708                                 // The current character is selected
709                                 lineStyle = defStyle.Reverse(true)
710
711                                 if style, ok := colorscheme["selection"]; ok {
712                                         lineStyle = style
713                                 }
714                         } else {
715                                 lineStyle = highlightStyle
716                         }
717
718                         // We need to display the background of the linestyle with the correct color if cursorline is enabled
719                         // and this is the current view and there is no selection on this line and the cursor is on this line
720                         if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
721                                 if style, ok := colorscheme["cursor-line"]; ok {
722                                         fg, _, _ := style.Decompose()
723                                         lineStyle = lineStyle.Background(fg)
724                                 }
725                         }
726
727                         if ch == '\t' {
728                                 // If the character we are displaying is a tab, we need to do a bunch of special things
729
730                                 // First the user may have configured an `indent-char` to be displayed to show that this
731                                 // is a tab character
732                                 lineIndentStyle := defStyle
733                                 if style, ok := colorscheme["indent-char"]; ok {
734                                         lineIndentStyle = style
735                                 }
736                                 if v.Cursor.HasSelection() &&
737                                         (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
738                                                 charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
739
740                                         lineIndentStyle = defStyle.Reverse(true)
741
742                                         if style, ok := colorscheme["selection"]; ok {
743                                                 lineIndentStyle = style
744                                         }
745                                 }
746                                 if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
747                                         if style, ok := colorscheme["cursor-line"]; ok {
748                                                 fg, _, _ := style.Decompose()
749                                                 lineIndentStyle = lineIndentStyle.Background(fg)
750                                         }
751                                 }
752                                 // Here we get the indent char
753                                 indentChar := []rune(v.Buf.Settings["indentchar"].(string))
754                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
755                                         v.drawCell(screenX-v.leftCol, screenY, indentChar[0], nil, lineIndentStyle)
756                                 }
757                                 // Now the tab has to be displayed as a bunch of spaces
758                                 tabSize := int(v.Buf.Settings["tabsize"].(float64))
759                                 for i := 0; i < tabSize-1; i++ {
760                                         screenX++
761                                         if screenX-v.x-v.leftCol >= v.lineNumOffset {
762                                                 v.drawCell(screenX-v.leftCol, screenY, ' ', nil, lineStyle)
763                                         }
764                                 }
765                         } else if runewidth.RuneWidth(ch) > 1 {
766                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
767                                         v.drawCell(screenX, screenY, ch, nil, lineStyle)
768                                 }
769                                 for i := 0; i < runewidth.RuneWidth(ch)-1; i++ {
770                                         screenX++
771                                         if screenX-v.x-v.leftCol >= v.lineNumOffset {
772                                                 v.drawCell(screenX-v.leftCol, screenY, '<', nil, lineStyle)
773                                         }
774                                 }
775                         } else {
776                                 if screenX-v.x-v.leftCol >= v.lineNumOffset {
777                                         v.drawCell(screenX-v.leftCol, screenY, ch, nil, lineStyle)
778                                 }
779                         }
780                         charNum = charNum.Move(1, v.Buf)
781                         screenX++
782                         colN++
783                 }
784                 // Here we are at a newline
785
786                 // The newline may be selected, in which case we should draw the selection style
787                 // with a space to represent it
788                 if v.Cursor.HasSelection() &&
789                         (charNum.GreaterEqual(v.Cursor.CurSelection[0]) && charNum.LessThan(v.Cursor.CurSelection[1]) ||
790                                 charNum.LessThan(v.Cursor.CurSelection[0]) && charNum.GreaterEqual(v.Cursor.CurSelection[1])) {
791
792                         selectStyle := defStyle.Reverse(true)
793
794                         if style, ok := colorscheme["selection"]; ok {
795                                 selectStyle = style
796                         }
797                         v.drawCell(screenX, screenY, ' ', nil, selectStyle)
798                         screenX++
799                 }
800
801                 charNum = charNum.Move(1, v.Buf)
802
803                 for i := 0; i < v.width; i++ {
804                         lineStyle := defStyle
805                         if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
806                                 if style, ok := colorscheme["cursor-line"]; ok {
807                                         fg, _, _ := style.Decompose()
808                                         lineStyle = lineStyle.Background(fg)
809                                 }
810                         }
811                         if screenX-v.x-v.leftCol+i >= v.lineNumOffset {
812                                 colorcolumn := int(v.Buf.Settings["colorcolumn"].(float64))
813                                 if colorcolumn != 0 && screenX-v.leftCol+i == colorcolumn-1 {
814                                         if style, ok := colorscheme["color-column"]; ok {
815                                                 fg, _, _ := style.Decompose()
816                                                 lineStyle = lineStyle.Background(fg)
817                                         }
818                                         v.drawCell(screenX-v.leftCol+i, screenY, ' ', nil, lineStyle)
819                                 } else {
820                                         v.drawCell(screenX-v.leftCol+i, screenY, ' ', nil, lineStyle)
821                                 }
822                         }
823                 }
824         }
825 }
826
827 // DisplayCursor draws the current buffer's cursor to the screen
828 func (v *View) DisplayCursor() {
829         // Don't draw the cursor if it is out of the viewport or if it has a selection
830         if (v.Cursor.Y-v.Topline < 0 || v.Cursor.Y-v.Topline > v.height-1) || v.Cursor.HasSelection() {
831                 screen.HideCursor()
832         } else {
833                 screen.ShowCursor(v.x+v.Cursor.GetVisualX()+v.lineNumOffset-v.leftCol, v.Cursor.Y-v.Topline+v.y)
834         }
835 }
836
837 // Display renders the view, the cursor, and statusline
838 func (v *View) Display() {
839         v.DisplayView()
840         if v.Num == tabs[curTab].curView {
841                 v.DisplayCursor()
842         }
843         _, screenH := screen.Size()
844         if v.Buf.Settings["statusline"].(bool) {
845                 v.sline.Display()
846         } else if (v.y + v.height) != screenH-1 {
847                 for x := 0; x < v.width; x++ {
848                         screen.SetContent(v.x+x, v.y+v.height, '-', nil, defStyle.Reverse(true))
849                 }
850         }
851 }