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["ruler"].(bool) {
216 vloc.X += maxLineNumLength + 1
219 line := b.LineBytes(bloc.Y)
220 line, nColsBeforeStart, bslice := util.SliceVisualEnd(line, w.StartCol, tabsize)
224 if nColsBeforeStart <= 0 {
230 totalwidth := w.StartCol - nColsBeforeStart
232 if svloc.X <= vloc.X+w.X && vloc.Y+w.Y == svloc.Y {
236 if vloc.X+w.X == svloc.X && vloc.Y+w.Y == svloc.Y {
240 r, size := utf8.DecodeRune(line)
246 ts := tabsize - (totalwidth % tabsize)
249 width = runewidth.RuneWidth(r)
252 // Draw any extra characters either spaces for tabs or @ for incomplete wide runes
254 for i := 1; i < width; i++ {
255 if vloc.X+w.X == svloc.X && vloc.Y+w.Y == svloc.Y {
266 // If we reach the end of the window then we either stop or we wrap for softwrap
267 if vloc.X >= bufWidth {
272 if vloc.Y >= bufHeight {
276 // This will draw an empty line number because the current line is wrapped
277 if b.Settings["ruler"].(bool) {
278 vloc.X += maxLineNumLength + 1
283 if vloc.Y+w.Y == svloc.Y {
287 if bloc.Y+1 >= b.LinesNum() || vloc.Y+1 >= bufHeight {
298 func (w *BufWindow) drawGutter(vloc *buffer.Loc, bloc *buffer.Loc) {
301 for _, m := range w.Buf.Messages {
302 if m.Start.Y == bloc.Y || m.End.Y == bloc.Y {
308 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, char, nil, s)
310 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, char, nil, s)
314 func (w *BufWindow) drawLineNum(lineNumStyle tcell.Style, softwrapped bool, maxLineNumLength int, vloc *buffer.Loc, bloc *buffer.Loc) {
315 lineNum := strconv.Itoa(bloc.Y + 1)
317 // Write the spaces before the line number if necessary
318 for i := 0; i < maxLineNumLength-len(lineNum); i++ {
319 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
322 // Write the actual line number
323 for _, ch := range lineNum {
325 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
327 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ch, nil, lineNumStyle)
332 // Write the extra space
333 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
337 // getStyle returns the highlight style for the given character position
338 // If there is no change to the current highlight style it just returns that
339 func (w *BufWindow) getStyle(style tcell.Style, bloc buffer.Loc, r rune) (tcell.Style, bool) {
340 if group, ok := w.Buf.Match(bloc.Y)[bloc.X]; ok {
341 s := config.GetColor(group.String())
347 func (w *BufWindow) showCursor(x, y int, main bool) {
350 screen.ShowCursor(x, y)
352 screen.ShowFakeCursorMulti(x, y)
357 // displayBuffer draws the buffer being shown in this window on the screen.Screen
358 func (w *BufWindow) displayBuffer() {
361 if w.Height <= 0 || w.Width <= 0 {
365 hasMessage := len(b.Messages) > 0
366 bufHeight := w.Height
372 if w.Buf.Settings["scrollbar"].(bool) && w.Buf.LinesNum() > w.Height {
376 if b.Settings["syntax"].(bool) && b.SyntaxDef != nil {
377 for _, r := range b.Modifications {
379 for i := r.X; i <= r.Y; i++ {
380 final = util.Max(b.Highlighter.ReHighlightStates(b, i), final)
382 b.Highlighter.HighlightMatches(b, r.X, final+1)
384 b.ClearModifications()
387 var matchingBraces []buffer.Loc
388 // bracePairs is defined in buffer.go
389 if b.Settings["matchbrace"].(bool) {
390 for _, bp := range buffer.BracePairs {
391 for _, c := range b.GetCursors() {
392 if c.HasSelection() {
398 r := c.RuneUnder(curX)
399 rl := c.RuneUnder(curX - 1)
400 if r == bp[0] || r == bp[1] || rl == bp[0] || rl == bp[1] {
401 mb, left := b.FindMatchingBrace(bp, curLoc)
402 matchingBraces = append(matchingBraces, mb)
404 matchingBraces = append(matchingBraces, curLoc)
406 matchingBraces = append(matchingBraces, curLoc.Move(-1, b))
413 lineNumStyle := config.DefStyle
414 if style, ok := config.Colorscheme["line-number"]; ok {
417 curNumStyle := config.DefStyle
418 if style, ok := config.Colorscheme["current-line-number"]; ok {
420 if !b.Settings["cursorline"].(bool) {
421 _, bg, _ := lineNumStyle.Decompose()
422 curNumStyle = curNumStyle.Background(bg)
426 // We need to know the string length of the largest line number
427 // so we can pad appropriately when displaying line numbers
428 maxLineNumLength := len(strconv.Itoa(b.LinesNum()))
430 softwrap := b.Settings["softwrap"].(bool)
431 tabsize := util.IntOpt(b.Settings["tabsize"])
432 colorcolumn := util.IntOpt(b.Settings["colorcolumn"])
434 // this represents the current draw position
435 // within the current window
436 vloc := buffer.Loc{X: 0, Y: 0}
438 // this represents the current draw position in the buffer (char positions)
439 bloc := buffer.Loc{X: -1, Y: w.StartLine}
441 cursors := b.GetCursors()
443 curStyle := config.DefStyle
444 for vloc.Y = 0; vloc.Y < bufHeight; vloc.Y++ {
448 w.drawGutter(&vloc, &bloc)
451 if b.Settings["ruler"].(bool) {
453 for _, c := range cursors {
454 if bloc.Y == c.Y && w.active {
459 w.drawLineNum(s, false, maxLineNumLength, &vloc, &bloc)
462 w.gutterOffset = vloc.X
464 line, nColsBeforeStart, bslice, startStyle := w.getStartInfo(w.StartCol, bloc.Y)
465 if startStyle != nil {
466 curStyle = *startStyle
470 draw := func(r rune, style tcell.Style, showcursor bool) {
471 if nColsBeforeStart <= 0 {
472 for _, c := range cursors {
473 if c.HasSelection() &&
474 (bloc.GreaterEqual(c.CurSelection[0]) && bloc.LessThan(c.CurSelection[1]) ||
475 bloc.LessThan(c.CurSelection[0]) && bloc.GreaterEqual(c.CurSelection[1])) {
476 // The current character is selected
477 style = config.DefStyle.Reverse(true)
479 if s, ok := config.Colorscheme["selection"]; ok {
484 if b.Settings["cursorline"].(bool) && w.active &&
485 !c.HasSelection() && c.Y == bloc.Y {
486 if s, ok := config.Colorscheme["cursor-line"]; ok {
487 fg, _, _ := s.Decompose()
488 style = style.Background(fg)
493 for _, m := range b.Messages {
494 if bloc.GreaterEqual(m.Start) && bloc.LessThan(m.End) ||
495 bloc.LessThan(m.End) && bloc.GreaterEqual(m.Start) {
496 style = style.Underline(true)
502 indentrunes := []rune(b.Settings["indentchar"].(string))
503 // if empty indentchar settings, use space
504 if indentrunes == nil || len(indentrunes) == 0 {
505 indentrunes = []rune{' '}
509 if s, ok := config.Colorscheme["indent-char"]; ok && r != ' ' {
510 fg, _, _ := s.Decompose()
511 style = style.Foreground(fg)
515 if s, ok := config.Colorscheme["color-column"]; ok {
516 if colorcolumn != 0 && vloc.X-w.gutterOffset == colorcolumn {
517 fg, _, _ := s.Decompose()
518 style = style.Background(fg)
522 for _, mb := range matchingBraces {
523 if mb.X == bloc.X && mb.Y == bloc.Y {
524 style = style.Underline(true)
528 screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, r, nil, style)
531 for _, c := range cursors {
532 if c.X == bloc.X && c.Y == bloc.Y && !c.HasSelection() {
533 w.showCursor(w.X+vloc.X, w.Y+vloc.Y, c.Num == 0)
542 totalwidth := w.StartCol - nColsBeforeStart
544 r, size := utf8.DecodeRune(line)
545 curStyle, _ = w.getStyle(curStyle, bloc, r)
547 draw(r, curStyle, true)
554 ts := tabsize - (totalwidth % tabsize)
557 width = runewidth.RuneWidth(r)
561 // Draw any extra characters either spaces for tabs or @ for incomplete wide runes
563 for i := 1; i < width; i++ {
564 draw(char, curStyle, false)
572 // If we reach the end of the window then we either stop or we wrap for softwrap
573 if vloc.X >= bufWidth {
578 if vloc.Y >= bufHeight {
582 // This will draw an empty line number because the current line is wrapped
583 if b.Settings["ruler"].(bool) {
584 w.drawLineNum(lineNumStyle, true, maxLineNumLength, &vloc, &bloc)
590 style := config.DefStyle
591 for _, c := range cursors {
592 if b.Settings["cursorline"].(bool) && w.active &&
593 !c.HasSelection() && c.Y == bloc.Y {
594 if s, ok := config.Colorscheme["cursor-line"]; ok {
595 fg, _, _ := s.Decompose()
596 style = style.Background(fg)
600 for i := vloc.X; i < bufWidth; i++ {
602 if s, ok := config.Colorscheme["color-column"]; ok {
603 if colorcolumn != 0 && i-w.gutterOffset == colorcolumn {
604 fg, _, _ := s.Decompose()
605 curStyle = style.Background(fg)
608 screen.SetContent(i+w.X, vloc.Y+w.Y, ' ', nil, curStyle)
611 if vloc.X != bufWidth {
612 draw(' ', curStyle, true)
617 if bloc.Y >= b.LinesNum() {
623 func (w *BufWindow) displayStatusLine() {
624 _, h := screen.Screen.Size()
626 if config.GetGlobalOption("infobar").(bool) {
630 if w.Buf.Settings["statusline"].(bool) {
633 } else if w.Y+w.Height != infoY {
635 for x := w.X; x < w.X+w.Width; x++ {
636 screen.SetContent(x, w.Y+w.Height-1, '-', nil, config.DefStyle.Reverse(true))
643 func (w *BufWindow) displayScrollBar() {
644 if w.Buf.Settings["scrollbar"].(bool) && w.Buf.LinesNum() > w.Height {
645 scrollX := w.X + w.Width - 1
646 bufHeight := w.Height
650 barsize := int(float64(w.Height) / float64(w.Buf.LinesNum()) * float64(w.Height))
654 barstart := w.Y + int(float64(w.StartLine)/float64(w.Buf.LinesNum())*float64(w.Height))
655 for y := barstart; y < util.Min(barstart+barsize, w.Y+bufHeight); y++ {
656 screen.SetContent(scrollX, y, '|', nil, config.DefStyle.Reverse(true))
661 // Display displays the buffer and the statusline
662 func (w *BufWindow) Display() {
663 w.displayStatusLine()