7 runewidth "github.com/mattn/go-runewidth"
8 "github.com/zyedidia/micro/internal/buffer"
9 "github.com/zyedidia/micro/internal/config"
10 "github.com/zyedidia/micro/internal/screen"
11 "github.com/zyedidia/micro/internal/util"
12 "github.com/zyedidia/tcell"
15 // The BufWindow provides a way of displaying a certain section
17 type BufWindow struct {
20 // Buffer being shown in this window
31 // NewBufWindow creates a new window at a location in the screen with a width and height
32 func NewBufWindow(x, y, width, height int, buf *buffer.Buffer) *BufWindow {
35 w.X, w.Y, w.Width, w.Height, w.Buf = x, y, width, height, buf
38 w.sline = NewStatusLine(w)
43 func (w *BufWindow) SetBuffer(b *buffer.Buffer) {
47 func (w *BufWindow) GetView() *View {
51 func (w *BufWindow) SetView(view *View) {
55 func (w *BufWindow) Resize(width, height int) {
56 w.Width, w.Height = width, height
60 func (w *BufWindow) SetActive(b bool) {
64 func (w *BufWindow) IsActive() bool {
68 func (w *BufWindow) getStartInfo(n, lineN int) ([]byte, int, int, *tcell.Style) {
69 tabsize := util.IntOpt(w.Buf.Settings["tabsize"])
71 bloc := buffer.Loc{0, lineN}
72 b := w.Buf.LineBytes(lineN)
73 curStyle := config.DefStyle
76 r, size := utf8.DecodeRune(b)
78 curStyle, found := w.getStyle(curStyle, bloc, r)
86 ts := tabsize - (width % tabsize)
89 w = runewidth.RuneWidth(r)
92 return b, n - width, bloc.X, s
98 return b, n - width, bloc.X, s
101 // Clear resets all cells in this window to the default style
102 func (w *BufWindow) Clear() {
103 for y := 0; y < w.Height; y++ {
104 for x := 0; x < w.Width; x++ {
105 screen.SetContent(w.X+x, w.Y+y, ' ', nil, config.DefStyle)
110 // Bottomline returns the line number of the lowest line in the view
111 // You might think that this is obviously just v.StartLine + v.Height
112 // but if softwrap is enabled things get complicated since one buffer
113 // line can take up multiple lines in the view
114 func (w *BufWindow) Bottomline() int {
115 if !w.Buf.Settings["softwrap"].(bool) {
116 h := w.StartLine + w.Height - 1
123 l := w.LocFromVisual(buffer.Loc{0, w.Y + w.Height})
128 // Relocate moves the view window so that the cursor is in view
129 // This is useful if the user has scrolled far away, and then starts typing
130 // Returns true if the window location is moved
131 func (w *BufWindow) Relocate() bool {
133 // how many buffer lines are in the view
134 height := w.Bottomline() + 1 - w.StartLine
139 if b.LinesNum() <= h {
143 activeC := w.Buf.GetActiveCursor()
145 scrollmargin := int(b.Settings["scrollmargin"].(float64))
146 if cy < w.StartLine+scrollmargin && cy > scrollmargin-1 {
147 w.StartLine = cy - scrollmargin
149 } else if cy < w.StartLine {
153 if cy > w.StartLine+height-1-scrollmargin && cy < b.LinesNum()-scrollmargin {
154 w.StartLine = cy - height + 1 + scrollmargin
156 } else if cy >= b.LinesNum()-scrollmargin && cy >= height {
157 w.StartLine = b.LinesNum() - height
161 // horizontal relocation (scrolling)
162 if !b.Settings["softwrap"].(bool) {
163 cx := activeC.GetVisualX()
168 if cx+w.gutterOffset+1 > w.StartCol+w.Width {
169 w.StartCol = cx - w.Width + w.gutterOffset + 1
176 // LocFromVisual takes a visual location (x and y position) and returns the
177 // position in the buffer corresponding to the visual location
178 // Computing the buffer location requires essentially drawing the entire screen
179 // to account for complications like softwrap, wide characters, and horizontal scrolling
180 // If the requested position does not correspond to a buffer location it returns
181 // the nearest position
182 func (w *BufWindow) LocFromVisual(svloc buffer.Loc) buffer.Loc {
185 hasMessage := len(b.Messages) > 0
186 bufHeight := w.Height
192 if w.Buf.Settings["scrollbar"].(bool) && w.Buf.LinesNum() > w.Height {
196 // We need to know the string length of the largest line number
197 // so we can pad appropriately when displaying line numbers
198 maxLineNumLength := len(strconv.Itoa(b.LinesNum()))
200 tabsize := int(b.Settings["tabsize"].(float64))
201 softwrap := b.Settings["softwrap"].(bool)
203 // this represents the current draw position
204 // within the current window
205 vloc := buffer.Loc{X: 0, Y: 0}
207 // this represents the current draw position in the buffer (char positions)
208 bloc := buffer.Loc{X: -1, Y: w.StartLine}
210 for vloc.Y = 0; vloc.Y < bufHeight; vloc.Y++ {
215 if b.Settings["diffgutter"].(bool) {
218 if b.Settings["ruler"].(bool) {
219 vloc.X += maxLineNumLength + 1
222 line := b.LineBytes(bloc.Y)
223 line, nColsBeforeStart, bslice := util.SliceVisualEnd(line, w.StartCol, tabsize)
227 if nColsBeforeStart <= 0 {
233 totalwidth := w.StartCol - nColsBeforeStart
235 if svloc.X <= vloc.X+w.X && vloc.Y+w.Y == svloc.Y {
239 if vloc.X+w.X == svloc.X && vloc.Y+w.Y == svloc.Y {
243 r, size := utf8.DecodeRune(line)
249 ts := tabsize - (totalwidth % tabsize)
252 width = runewidth.RuneWidth(r)
255 // Draw any extra characters either spaces for tabs or @ for incomplete wide runes
257 for i := 1; i < width; i++ {
258 if vloc.X+w.X == svloc.X && vloc.Y+w.Y == svloc.Y {
269 // If we reach the end of the window then we either stop or we wrap for softwrap
270 if vloc.X >= bufWidth {
275 if vloc.Y >= bufHeight {
279 if b.Settings["diffgutter"].(bool) {
282 // This will draw an empty line number because the current line is wrapped
283 if b.Settings["ruler"].(bool) {
284 vloc.X += maxLineNumLength + 1
289 if vloc.Y+w.Y == svloc.Y {
293 if bloc.Y+1 >= b.LinesNum() || vloc.Y+1 >= bufHeight {
304 func (w *BufWindow) drawGutter(vloc *buffer.Loc, bloc *buffer.Loc) {
307 for _, m := range w.Buf.Messages {
308 if m.Start.Y == bloc.Y || m.End.Y == bloc.Y {
314 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, char, nil, s)
316 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, char, nil, s)
320 func (w *BufWindow) drawDiffGutter(backgroundStyle tcell.Style, softwrapped bool, vloc *buffer.Loc, bloc *buffer.Loc) {
324 switch w.Buf.DiffStatus(bloc.Y) {
326 symbol = '\u258C' // Left half block
327 styleName = "diff-added"
328 case buffer.DSModified:
329 symbol = '\u258C' // Left half block
330 styleName = "diff-modified"
331 case buffer.DSDeletedAbove:
333 symbol = '\u2594' // Upper one eighth block
334 styleName = "diff-deleted"
338 style := backgroundStyle
339 if s, ok := config.Colorscheme[styleName]; ok {
340 foreground, _, _ := s.Decompose()
341 style = style.Foreground(foreground)
344 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, symbol, nil, style)
348 func (w *BufWindow) drawLineNum(lineNumStyle tcell.Style, softwrapped bool, maxLineNumLength int, vloc *buffer.Loc, bloc *buffer.Loc) {
349 lineNum := strconv.Itoa(bloc.Y + 1)
351 // Write the spaces before the line number if necessary
352 for i := 0; i < maxLineNumLength-len(lineNum); i++ {
353 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
356 // Write the actual line number
357 for _, ch := range lineNum {
359 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
361 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ch, nil, lineNumStyle)
366 // Write the extra space
367 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
371 // getStyle returns the highlight style for the given character position
372 // If there is no change to the current highlight style it just returns that
373 func (w *BufWindow) getStyle(style tcell.Style, bloc buffer.Loc, r rune) (tcell.Style, bool) {
374 if group, ok := w.Buf.Match(bloc.Y)[bloc.X]; ok {
375 s := config.GetColor(group.String())
381 func (w *BufWindow) showCursor(x, y int, main bool) {
384 screen.ShowCursor(x, y)
386 screen.ShowFakeCursorMulti(x, y)
391 // displayBuffer draws the buffer being shown in this window on the screen.Screen
392 func (w *BufWindow) displayBuffer() {
395 if w.Height <= 0 || w.Width <= 0 {
399 hasMessage := len(b.Messages) > 0
400 bufHeight := w.Height
406 if w.Buf.Settings["scrollbar"].(bool) && w.Buf.LinesNum() > w.Height {
410 if len(b.Modifications) > 0 {
411 if b.Settings["syntax"].(bool) && b.SyntaxDef != nil {
412 for _, r := range b.Modifications {
414 for i := r.X; i <= r.Y; i++ {
415 final = util.Max(b.Highlighter.ReHighlightStates(b, i), final)
417 b.Highlighter.HighlightMatches(b, r.X, final+1)
421 b.ClearModifications()
423 if b.Settings["diffgutter"].(bool) {
424 b.UpdateDiff(func(synchronous bool) {
425 // If the diff was updated asynchronously, the outer call to
426 // displayBuffer might already be completed and we need to
427 // schedule a redraw in order to display the new diff.
428 // Note that this cannot lead to an infinite recursion
429 // because the modifications were cleared above so there won't
430 // be another call to UpdateDiff when displayBuffer is called
431 // during the redraw.
439 var matchingBraces []buffer.Loc
440 // bracePairs is defined in buffer.go
441 if b.Settings["matchbrace"].(bool) {
442 for _, bp := range buffer.BracePairs {
443 for _, c := range b.GetCursors() {
444 if c.HasSelection() {
450 r := c.RuneUnder(curX)
451 rl := c.RuneUnder(curX - 1)
452 if r == bp[0] || r == bp[1] || rl == bp[0] || rl == bp[1] {
453 mb, left := b.FindMatchingBrace(bp, curLoc)
454 matchingBraces = append(matchingBraces, mb)
456 matchingBraces = append(matchingBraces, curLoc)
458 matchingBraces = append(matchingBraces, curLoc.Move(-1, b))
465 lineNumStyle := config.DefStyle
466 if style, ok := config.Colorscheme["line-number"]; ok {
469 curNumStyle := config.DefStyle
470 if style, ok := config.Colorscheme["current-line-number"]; ok {
471 if !b.Settings["cursorline"].(bool) {
472 curNumStyle = lineNumStyle
478 // We need to know the string length of the largest line number
479 // so we can pad appropriately when displaying line numbers
480 maxLineNumLength := len(strconv.Itoa(b.LinesNum()))
482 softwrap := b.Settings["softwrap"].(bool)
483 tabsize := util.IntOpt(b.Settings["tabsize"])
484 colorcolumn := util.IntOpt(b.Settings["colorcolumn"])
486 // this represents the current draw position
487 // within the current window
488 vloc := buffer.Loc{X: 0, Y: 0}
490 // this represents the current draw position in the buffer (char positions)
491 bloc := buffer.Loc{X: -1, Y: w.StartLine}
493 cursors := b.GetCursors()
495 curStyle := config.DefStyle
496 for vloc.Y = 0; vloc.Y < bufHeight; vloc.Y++ {
500 for _, c := range cursors {
501 if bloc.Y == c.Y && w.active {
513 w.drawGutter(&vloc, &bloc)
516 if b.Settings["diffgutter"].(bool) {
517 w.drawDiffGutter(s, false, &vloc, &bloc)
520 if b.Settings["ruler"].(bool) {
521 w.drawLineNum(s, false, maxLineNumLength, &vloc, &bloc)
524 w.gutterOffset = vloc.X
526 line, nColsBeforeStart, bslice, startStyle := w.getStartInfo(w.StartCol, bloc.Y)
527 if startStyle != nil {
528 curStyle = *startStyle
532 draw := func(r rune, style tcell.Style, showcursor bool) {
533 if nColsBeforeStart <= 0 {
534 for _, c := range cursors {
535 if c.HasSelection() &&
536 (bloc.GreaterEqual(c.CurSelection[0]) && bloc.LessThan(c.CurSelection[1]) ||
537 bloc.LessThan(c.CurSelection[0]) && bloc.GreaterEqual(c.CurSelection[1])) {
538 // The current character is selected
539 style = config.DefStyle.Reverse(true)
541 if s, ok := config.Colorscheme["selection"]; ok {
546 if b.Settings["cursorline"].(bool) && w.active &&
547 !c.HasSelection() && c.Y == bloc.Y {
548 if s, ok := config.Colorscheme["cursor-line"]; ok {
549 fg, _, _ := s.Decompose()
550 style = style.Background(fg)
555 for _, m := range b.Messages {
556 if bloc.GreaterEqual(m.Start) && bloc.LessThan(m.End) ||
557 bloc.LessThan(m.End) && bloc.GreaterEqual(m.Start) {
558 style = style.Underline(true)
564 indentrunes := []rune(b.Settings["indentchar"].(string))
565 // if empty indentchar settings, use space
566 if indentrunes == nil || len(indentrunes) == 0 {
567 indentrunes = []rune{' '}
571 if s, ok := config.Colorscheme["indent-char"]; ok && r != ' ' {
572 fg, _, _ := s.Decompose()
573 style = style.Foreground(fg)
577 if s, ok := config.Colorscheme["color-column"]; ok {
578 if colorcolumn != 0 && vloc.X-w.gutterOffset == colorcolumn {
579 fg, _, _ := s.Decompose()
580 style = style.Background(fg)
584 for _, mb := range matchingBraces {
585 if mb.X == bloc.X && mb.Y == bloc.Y {
586 style = style.Underline(true)
590 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, r, nil, style)
593 for _, c := range cursors {
594 if c.X == bloc.X && c.Y == bloc.Y && !c.HasSelection() {
595 w.showCursor(w.X+vloc.X, w.Y+vloc.Y, c.Num == 0)
604 totalwidth := w.StartCol - nColsBeforeStart
606 r, size := utf8.DecodeRune(line)
607 curStyle, _ = w.getStyle(curStyle, bloc, r)
609 draw(r, curStyle, true)
616 ts := tabsize - (totalwidth % tabsize)
619 width = runewidth.RuneWidth(r)
623 // Draw any extra characters either spaces for tabs or @ for incomplete wide runes
625 for i := 1; i < width; i++ {
626 draw(char, curStyle, false)
634 // If we reach the end of the window then we either stop or we wrap for softwrap
635 if vloc.X >= bufWidth {
640 if vloc.Y >= bufHeight {
644 if b.Settings["diffgutter"].(bool) {
645 w.drawDiffGutter(lineNumStyle, true, &vloc, &bloc)
647 // This will draw an empty line number because the current line is wrapped
648 if b.Settings["ruler"].(bool) {
649 w.drawLineNum(lineNumStyle, true, maxLineNumLength, &vloc, &bloc)
655 style := config.DefStyle
656 for _, c := range cursors {
657 if b.Settings["cursorline"].(bool) && w.active &&
658 !c.HasSelection() && c.Y == bloc.Y {
659 if s, ok := config.Colorscheme["cursor-line"]; ok {
660 fg, _, _ := s.Decompose()
661 style = style.Background(fg)
665 for i := vloc.X; i < bufWidth; i++ {
667 if s, ok := config.Colorscheme["color-column"]; ok {
668 if colorcolumn != 0 && i-w.gutterOffset == colorcolumn {
669 fg, _, _ := s.Decompose()
670 curStyle = style.Background(fg)
673 screen.SetContent(i+w.X, vloc.Y+w.Y, ' ', nil, curStyle)
676 if vloc.X != bufWidth {
677 draw(' ', curStyle, true)
682 if bloc.Y >= b.LinesNum() {
688 func (w *BufWindow) displayStatusLine() {
689 _, h := screen.Screen.Size()
691 if config.GetGlobalOption("infobar").(bool) {
695 if w.Buf.Settings["statusline"].(bool) {
698 } else if w.Y+w.Height != infoY {
700 for x := w.X; x < w.X+w.Width; x++ {
701 screen.SetContent(x, w.Y+w.Height-1, '-', nil, config.DefStyle.Reverse(true))
708 func (w *BufWindow) displayScrollBar() {
709 if w.Buf.Settings["scrollbar"].(bool) && w.Buf.LinesNum() > w.Height {
710 scrollX := w.X + w.Width - 1
711 bufHeight := w.Height
715 barsize := int(float64(w.Height) / float64(w.Buf.LinesNum()) * float64(w.Height))
719 barstart := w.Y + int(float64(w.StartLine)/float64(w.Buf.LinesNum())*float64(w.Height))
720 for y := barstart; y < util.Min(barstart+barsize, w.Y+bufHeight); y++ {
721 screen.SetContent(scrollX, y, '|', nil, config.DefStyle.Reverse(true))
726 // Display displays the buffer and the statusline
727 func (w *BufWindow) Display() {
728 w.displayStatusLine()