6 runewidth "github.com/mattn/go-runewidth"
7 "github.com/zyedidia/micro/v2/internal/buffer"
8 "github.com/zyedidia/micro/v2/internal/config"
9 "github.com/zyedidia/micro/v2/internal/screen"
10 "github.com/zyedidia/micro/v2/internal/util"
11 "github.com/zyedidia/tcell/v2"
14 // The BufWindow provides a way of displaying a certain section
16 type BufWindow struct {
19 // Buffer being shown in this window
34 // NewBufWindow creates a new window at a location in the screen with a width and height
35 func NewBufWindow(x, y, width, height int, buf *buffer.Buffer) *BufWindow {
38 w.X, w.Y, w.Width, w.Height = x, y, width, height
42 w.sline = NewStatusLine(w)
47 func (w *BufWindow) SetBuffer(b *buffer.Buffer) {
49 b.OptionCallback = func(option string, nativeValue interface{}) {
50 if option == "softwrap" {
51 if nativeValue.(bool) {
58 for _, c := range w.Buf.GetCursors() {
59 c.LastVisualX = c.GetVisualX()
63 b.GetVisualX = func(loc buffer.Loc) int {
64 return w.VLocFromLoc(loc).VisualX
68 func (w *BufWindow) GetView() *View {
72 func (w *BufWindow) SetView(view *View) {
76 func (w *BufWindow) Resize(width, height int) {
77 w.Width, w.Height = width, height
82 if w.Buf.Settings["softwrap"].(bool) {
83 for _, c := range w.Buf.GetCursors() {
84 c.LastVisualX = c.GetVisualX()
89 func (w *BufWindow) SetActive(b bool) {
93 func (w *BufWindow) IsActive() bool {
97 // BufView returns the width, height and x,y location of the actual buffer.
98 // It is not exactly the same as the whole window which also contains gutter,
99 // ruler, scrollbar and statusline.
100 func (w *BufWindow) BufView() View {
102 X: w.X + w.gutterOffset,
106 StartLine: w.StartLine,
107 StartCol: w.StartCol,
111 func (w *BufWindow) updateDisplayInfo() {
114 w.drawDivider = false
115 if !b.Settings["statusline"].(bool) {
116 _, h := screen.Screen.Size()
118 if config.GetGlobalOption("infobar").(bool) {
121 if w.Y+w.Height != infoY {
126 w.bufHeight = w.Height
127 if b.Settings["statusline"].(bool) || w.drawDivider {
131 w.hasMessage = len(b.Messages) > 0
133 // We need to know the string length of the largest line number
134 // so we can pad appropriately when displaying line numbers
135 w.maxLineNumLength = len(strconv.Itoa(b.LinesNum()))
141 if b.Settings["diffgutter"].(bool) {
144 if b.Settings["ruler"].(bool) {
145 w.gutterOffset += w.maxLineNumLength + 1
148 w.bufWidth = w.Width - w.gutterOffset
149 if w.Buf.Settings["scrollbar"].(bool) && w.Buf.LinesNum() > w.Height {
154 func (w *BufWindow) getStartInfo(n, lineN int) ([]byte, int, int, *tcell.Style) {
155 tabsize := util.IntOpt(w.Buf.Settings["tabsize"])
157 bloc := buffer.Loc{0, lineN}
158 b := w.Buf.LineBytes(lineN)
159 curStyle := config.DefStyle
162 r, _, size := util.DecodeCharacter(b)
164 curStyle, found := w.getStyle(curStyle, bloc)
172 ts := tabsize - (width % tabsize)
175 w = runewidth.RuneWidth(r)
178 return b, n - width, bloc.X, s
184 return b, n - width, bloc.X, s
187 // Clear resets all cells in this window to the default style
188 func (w *BufWindow) Clear() {
189 for y := 0; y < w.Height; y++ {
190 for x := 0; x < w.Width; x++ {
191 screen.SetContent(w.X+x, w.Y+y, ' ', nil, config.DefStyle)
196 // Relocate moves the view window so that the cursor is in view
197 // This is useful if the user has scrolled far away, and then starts typing
198 // Returns true if the window location is moved
199 func (w *BufWindow) Relocate() bool {
201 height := w.bufHeight
203 activeC := w.Buf.GetActiveCursor()
204 scrollmargin := int(b.Settings["scrollmargin"].(float64))
206 c := w.SLocFromLoc(activeC.Loc)
208 bEnd := w.SLocFromLoc(b.End())
210 if c.LessThan(w.Scroll(w.StartLine, scrollmargin)) && c.GreaterThan(w.Scroll(bStart, scrollmargin-1)) {
211 w.StartLine = w.Scroll(c, -scrollmargin)
213 } else if c.LessThan(w.StartLine) {
217 if c.GreaterThan(w.Scroll(w.StartLine, height-1-scrollmargin)) && c.LessThan(w.Scroll(bEnd, -scrollmargin+1)) {
218 w.StartLine = w.Scroll(c, -height+1+scrollmargin)
220 } else if c.GreaterThan(w.Scroll(bEnd, -scrollmargin)) && c.GreaterThan(w.Scroll(w.StartLine, height-1)) {
221 w.StartLine = w.Scroll(bEnd, -height+1)
225 // horizontal relocation (scrolling)
226 if !b.Settings["softwrap"].(bool) {
227 cx := activeC.GetVisualX()
228 rw := runewidth.RuneWidth(activeC.RuneUnder(activeC.X))
230 rw = 1 // tab or newline
237 if cx+w.gutterOffset+rw > w.StartCol+w.Width {
238 w.StartCol = cx - w.Width + w.gutterOffset + rw
245 // LocFromVisual takes a visual location (x and y position) and returns the
246 // position in the buffer corresponding to the visual location
247 // If the requested position does not correspond to a buffer location it returns
248 // the nearest position
249 func (w *BufWindow) LocFromVisual(svloc buffer.Loc) buffer.Loc {
250 vx := svloc.X - w.X - w.gutterOffset
255 SLoc: w.Scroll(w.StartLine, svloc.Y-w.Y),
256 VisualX: vx + w.StartCol,
258 return w.LocFromVLoc(vloc)
261 func (w *BufWindow) drawGutter(vloc *buffer.Loc, bloc *buffer.Loc) {
264 for _, m := range w.Buf.Messages {
265 if m.Start.Y == bloc.Y || m.End.Y == bloc.Y {
271 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, char, nil, s)
273 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, char, nil, s)
277 func (w *BufWindow) drawDiffGutter(backgroundStyle tcell.Style, softwrapped bool, vloc *buffer.Loc, bloc *buffer.Loc) {
281 switch w.Buf.DiffStatus(bloc.Y) {
283 symbol = '\u258C' // Left half block
284 styleName = "diff-added"
285 case buffer.DSModified:
286 symbol = '\u258C' // Left half block
287 styleName = "diff-modified"
288 case buffer.DSDeletedAbove:
290 symbol = '\u2594' // Upper one eighth block
291 styleName = "diff-deleted"
295 style := backgroundStyle
296 if s, ok := config.Colorscheme[styleName]; ok {
297 foreground, _, _ := s.Decompose()
298 style = style.Foreground(foreground)
301 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, symbol, nil, style)
305 func (w *BufWindow) drawLineNum(lineNumStyle tcell.Style, softwrapped bool, vloc *buffer.Loc, bloc *buffer.Loc) {
306 cursorLine := w.Buf.GetActiveCursor().Loc.Y
308 if w.Buf.Settings["relativeruler"] == false || cursorLine == bloc.Y {
311 lineInt = bloc.Y - cursorLine
313 lineNum := strconv.Itoa(util.Abs(lineInt))
315 // Write the spaces before the line number if necessary
316 for i := 0; i < w.maxLineNumLength-len(lineNum); i++ {
317 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
320 // Write the actual line number
321 for _, ch := range lineNum {
323 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
325 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ch, nil, lineNumStyle)
330 // Write the extra space
331 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
335 // getStyle returns the highlight style for the given character position
336 // If there is no change to the current highlight style it just returns that
337 func (w *BufWindow) getStyle(style tcell.Style, bloc buffer.Loc) (tcell.Style, bool) {
338 if group, ok := w.Buf.Match(bloc.Y)[bloc.X]; ok {
339 s := config.GetColor(group.String())
345 func (w *BufWindow) showCursor(x, y int, main bool) {
348 screen.ShowCursor(x, y)
350 screen.ShowFakeCursorMulti(x, y)
355 // displayBuffer draws the buffer being shown in this window on the screen.Screen
356 func (w *BufWindow) displayBuffer() {
359 if w.Height <= 0 || w.Width <= 0 {
363 maxWidth := w.gutterOffset + w.bufWidth
365 if b.ModifiedThisFrame {
366 if b.Settings["diffgutter"].(bool) {
367 b.UpdateDiff(func(synchronous bool) {
368 // If the diff was updated asynchronously, the outer call to
369 // displayBuffer might already be completed and we need to
370 // schedule a redraw in order to display the new diff.
371 // Note that this cannot lead to an infinite recursion
372 // because the modifications were cleared above so there won't
373 // be another call to UpdateDiff when displayBuffer is called
374 // during the redraw.
380 b.ModifiedThisFrame = false
383 var matchingBraces []buffer.Loc
384 // bracePairs is defined in buffer.go
385 if b.Settings["matchbrace"].(bool) {
386 for _, bp := range buffer.BracePairs {
387 for _, c := range b.GetCursors() {
388 if c.HasSelection() {
394 r := c.RuneUnder(curX)
395 rl := c.RuneUnder(curX - 1)
396 if r == bp[0] || r == bp[1] || rl == bp[0] || rl == bp[1] {
397 mb, left, found := b.FindMatchingBrace(bp, curLoc)
399 matchingBraces = append(matchingBraces, mb)
401 matchingBraces = append(matchingBraces, curLoc)
403 matchingBraces = append(matchingBraces, curLoc.Move(-1, b))
411 lineNumStyle := config.DefStyle
412 if style, ok := config.Colorscheme["line-number"]; ok {
415 curNumStyle := config.DefStyle
416 if style, ok := config.Colorscheme["current-line-number"]; ok {
417 if !b.Settings["cursorline"].(bool) {
418 curNumStyle = lineNumStyle
424 softwrap := b.Settings["softwrap"].(bool)
425 wordwrap := softwrap && b.Settings["wordwrap"].(bool)
427 tabsize := util.IntOpt(b.Settings["tabsize"])
428 colorcolumn := util.IntOpt(b.Settings["colorcolumn"])
430 // this represents the current draw position
431 // within the current window
432 vloc := buffer.Loc{X: 0, Y: 0}
434 // the start line may be partially out of the current window
435 vloc.Y = -w.StartLine.Row
438 // this represents the current draw position in the buffer (char positions)
439 bloc := buffer.Loc{X: -1, Y: w.StartLine.Line}
441 cursors := b.GetCursors()
443 curStyle := config.DefStyle
444 for ; vloc.Y < w.bufHeight; vloc.Y++ {
448 for _, c := range cursors {
449 if bloc.Y == c.Y && w.active {
462 w.drawGutter(&vloc, &bloc)
465 if b.Settings["diffgutter"].(bool) {
466 w.drawDiffGutter(s, false, &vloc, &bloc)
469 if b.Settings["ruler"].(bool) {
470 w.drawLineNum(s, false, &vloc, &bloc)
473 vloc.X = w.gutterOffset
476 line, nColsBeforeStart, bslice, startStyle := w.getStartInfo(w.StartCol, bloc.Y)
477 if startStyle != nil {
478 curStyle = *startStyle
482 draw := func(r rune, combc []rune, style tcell.Style, highlight bool, showcursor bool) {
483 if nColsBeforeStart <= 0 && vloc.Y >= 0 {
485 _, origBg, _ := style.Decompose()
486 _, defBg, _ := config.DefStyle.Decompose()
488 // syntax highlighting with non-default background takes precedence
489 // over cursor-line and color-column
490 dontOverrideBackground := origBg != defBg
492 for _, c := range cursors {
493 if c.HasSelection() &&
494 (bloc.GreaterEqual(c.CurSelection[0]) && bloc.LessThan(c.CurSelection[1]) ||
495 bloc.LessThan(c.CurSelection[0]) && bloc.GreaterEqual(c.CurSelection[1])) {
496 // The current character is selected
497 style = config.DefStyle.Reverse(true)
499 if s, ok := config.Colorscheme["selection"]; ok {
504 if b.Settings["cursorline"].(bool) && w.active && !dontOverrideBackground &&
505 !c.HasSelection() && c.Y == bloc.Y {
506 if s, ok := config.Colorscheme["cursor-line"]; ok {
507 fg, _, _ := s.Decompose()
508 style = style.Background(fg)
513 for _, m := range b.Messages {
514 if bloc.GreaterEqual(m.Start) && bloc.LessThan(m.End) ||
515 bloc.LessThan(m.End) && bloc.GreaterEqual(m.Start) {
516 style = style.Underline(true)
522 indentrunes := []rune(b.Settings["indentchar"].(string))
523 // if empty indentchar settings, use space
524 if len(indentrunes) == 0 {
525 indentrunes = []rune{' '}
529 if s, ok := config.Colorscheme["indent-char"]; ok && r != ' ' {
530 fg, _, _ := s.Decompose()
531 style = style.Foreground(fg)
535 if s, ok := config.Colorscheme["color-column"]; ok {
536 if colorcolumn != 0 && vloc.X-w.gutterOffset+w.StartCol == colorcolumn && !dontOverrideBackground {
537 fg, _, _ := s.Decompose()
538 style = style.Background(fg)
542 for _, mb := range matchingBraces {
543 if mb.X == bloc.X && mb.Y == bloc.Y {
544 style = style.Underline(true)
549 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, r, combc, style)
552 for _, c := range cursors {
553 if c.X == bloc.X && c.Y == bloc.Y && !c.HasSelection() {
554 w.showCursor(w.X+vloc.X, w.Y+vloc.Y, c.Num == 0)
559 if nColsBeforeStart <= 0 {
568 w.drawGutter(&vloc, &bloc)
570 if b.Settings["diffgutter"].(bool) {
571 w.drawDiffGutter(lineNumStyle, true, &vloc, &bloc)
574 // This will draw an empty line number because the current line is wrapped
575 if b.Settings["ruler"].(bool) {
576 w.drawLineNum(lineNumStyle, true, &vloc, &bloc)
589 word = make([]glyph, 0, w.bufWidth)
591 word = make([]glyph, 0, 1)
595 totalwidth := w.StartCol - nColsBeforeStart
597 r, combc, size := util.DecodeCharacter(line)
600 loc := buffer.Loc{X: bloc.X + len(word), Y: bloc.Y}
601 curStyle, _ = w.getStyle(curStyle, loc)
607 ts := tabsize - (totalwidth % tabsize)
608 width = util.Min(ts, maxWidth-vloc.X)
611 width = runewidth.RuneWidth(r)
615 word = append(word, glyph{r, combc, curStyle, width})
618 // Collect a complete word to know its width.
619 // If wordwrap is off, every single character is a complete "word".
621 if !util.IsWhitespace(r) && len(line) > 0 && wordwidth < w.bufWidth {
626 // If a word (or just a wide rune) does not fit in the window
627 if vloc.X+wordwidth > maxWidth && vloc.X > w.gutterOffset {
628 for vloc.X < maxWidth {
629 draw(' ', nil, config.DefStyle, false, false)
632 // We either stop or we wrap to draw the word in the next line
637 if vloc.Y >= w.bufHeight {
644 for _, r := range word {
645 draw(r.r, r.combc, r.style, true, true)
647 // Draw any extra characters either spaces for tabs or @ for incomplete wide runes
654 for i := 1; i < r.width; i++ {
655 draw(char, nil, r.style, true, false)
664 // If we reach the end of the window then we either stop or we wrap for softwrap
665 if vloc.X >= maxWidth {
670 if vloc.Y >= w.bufHeight {
678 style := config.DefStyle
679 for _, c := range cursors {
680 if b.Settings["cursorline"].(bool) && w.active &&
681 !c.HasSelection() && c.Y == bloc.Y {
682 if s, ok := config.Colorscheme["cursor-line"]; ok {
683 fg, _, _ := s.Decompose()
684 style = style.Background(fg)
688 for i := vloc.X; i < maxWidth; i++ {
690 if s, ok := config.Colorscheme["color-column"]; ok {
691 if colorcolumn != 0 && i-w.gutterOffset+w.StartCol == colorcolumn {
692 fg, _, _ := s.Decompose()
693 curStyle = style.Background(fg)
696 screen.SetContent(i+w.X, vloc.Y+w.Y, ' ', nil, curStyle)
699 if vloc.X != maxWidth {
700 // Display newline within a selection
701 draw(' ', nil, config.DefStyle, true, true)
706 if bloc.Y >= b.LinesNum() {
712 func (w *BufWindow) displayStatusLine() {
713 if w.Buf.Settings["statusline"].(bool) {
715 } else if w.drawDivider {
716 divchars := config.GetGlobalOption("divchars").(string)
717 if util.CharacterCountInString(divchars) != 2 {
721 _, _, size := util.DecodeCharacterInString(divchars)
722 divchar, combc, _ := util.DecodeCharacterInString(divchars[size:])
724 dividerStyle := config.DefStyle
725 if style, ok := config.Colorscheme["divider"]; ok {
729 divreverse := config.GetGlobalOption("divreverse").(bool)
731 dividerStyle = dividerStyle.Reverse(true)
734 for x := w.X; x < w.X+w.Width; x++ {
735 screen.SetContent(x, w.Y+w.Height-1, divchar, combc, dividerStyle)
740 func (w *BufWindow) displayScrollBar() {
741 if w.Buf.Settings["scrollbar"].(bool) && w.Buf.LinesNum() > w.Height {
742 scrollX := w.X + w.Width - 1
743 barsize := int(float64(w.Height) / float64(w.Buf.LinesNum()) * float64(w.Height))
747 barstart := w.Y + int(float64(w.StartLine.Line)/float64(w.Buf.LinesNum())*float64(w.Height))
749 scrollBarStyle := config.DefStyle.Reverse(true)
750 if style, ok := config.Colorscheme["scrollbar"]; ok {
751 scrollBarStyle = style
754 for y := barstart; y < util.Min(barstart+barsize, w.Y+w.bufHeight); y++ {
755 screen.SetContent(scrollX, y, '|', nil, scrollBarStyle)
760 // Display displays the buffer and the statusline
761 func (w *BufWindow) Display() {
762 w.updateDisplayInfo()
764 w.displayStatusLine()