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
33 // NewBufWindow creates a new window at a location in the screen with a width and height
34 func NewBufWindow(x, y, width, height int, buf *buffer.Buffer) *BufWindow {
37 w.X, w.Y, w.Width, w.Height, w.Buf = x, y, width, height, buf
38 w.lineHeight = make([]int, height)
41 w.sline = NewStatusLine(w)
46 func (w *BufWindow) SetBuffer(b *buffer.Buffer) {
50 func (v *View) GetView() *View {
54 func (v *View) SetView(view *View) {
58 func (w *BufWindow) Resize(width, height int) {
59 w.Width, w.Height = width, height
60 w.lineHeight = make([]int, height)
61 w.hasCalcHeight = false
62 // This recalculates lineHeight
63 w.GetMouseLoc(buffer.Loc{width, height})
66 func (w *BufWindow) SetActive(b bool) {
70 func (w *BufWindow) getStartInfo(n, lineN int) ([]byte, int, int, *tcell.Style) {
71 tabsize := util.IntOpt(w.Buf.Settings["tabsize"])
73 bloc := buffer.Loc{0, lineN}
74 b := w.Buf.LineBytes(lineN)
75 curStyle := config.DefStyle
78 r, size := utf8.DecodeRune(b)
80 curStyle, found := w.getStyle(curStyle, bloc, r)
88 ts := tabsize - (width % tabsize)
91 w = runewidth.RuneWidth(r)
94 return b, n - width, bloc.X, s
100 return b, n - width, bloc.X, s
103 // Clear resets all cells in this window to the default style
104 func (w *BufWindow) Clear() {
105 for y := 0; y < w.Height; y++ {
106 for x := 0; x < w.Width; x++ {
107 screen.Screen.SetContent(w.X+x, w.Y+y, ' ', nil, config.DefStyle)
112 // Bottomline returns the line number of the lowest line in the view
113 // You might think that this is obviously just v.StartLine + v.Height
114 // but if softwrap is enabled things get complicated since one buffer
115 // line can take up multiple lines in the view
116 func (w *BufWindow) Bottomline() int {
119 // TODO: possible non-softwrap optimization
120 // if !b.Settings["softwrap"].(bool) {
121 // return w.StartLine + w.Height
125 for _, l := range w.lineHeight {
135 // Relocate moves the view window so that the cursor is in view
136 // This is useful if the user has scrolled far away, and then starts typing
137 // Returns true if the window location is moved
138 func (w *BufWindow) Relocate() bool {
140 // how many buffer lines are in the view
141 height := w.Bottomline() + 1 - w.StartLine
146 if b.LinesNum() <= h || !w.hasCalcHeight {
150 activeC := w.Buf.GetActiveCursor()
152 scrollmargin := int(b.Settings["scrollmargin"].(float64))
153 if cy < w.StartLine+scrollmargin && cy > scrollmargin-1 {
154 w.StartLine = cy - scrollmargin
156 } else if cy < w.StartLine {
160 if cy > w.StartLine+height-1-scrollmargin && cy < b.LinesNum()-scrollmargin {
161 w.StartLine = cy - height + 1 + scrollmargin
163 } else if cy >= b.LinesNum()-scrollmargin && cy >= height {
164 w.StartLine = b.LinesNum() - height
168 // horizontal relocation (scrolling)
169 if !b.Settings["softwrap"].(bool) {
170 cx := activeC.GetVisualX()
175 if cx+w.gutterOffset+1 > w.StartCol+w.Width {
176 w.StartCol = cx - w.Width + w.gutterOffset + 1
183 func (w *BufWindow) GetMouseLoc(svloc buffer.Loc) buffer.Loc {
186 // TODO: possible non-softwrap optimization
187 // if !b.Settings["softwrap"].(bool) {
188 // l := b.LineBytes(svloc.Y)
189 // return buffer.Loc{b.GetActiveCursor().GetCharPosInLine(l, svloc.X), svloc.Y}
192 hasMessage := len(b.Messages) > 0
193 bufHeight := w.Height
198 // We need to know the string length of the largest line number
199 // so we can pad appropriately when displaying line numbers
200 maxLineNumLength := len(strconv.Itoa(b.LinesNum()))
202 tabsize := int(b.Settings["tabsize"].(float64))
203 softwrap := b.Settings["softwrap"].(bool)
205 // this represents the current draw position
206 // within the current window
207 vloc := buffer.Loc{X: 0, Y: 0}
209 // this represents the current draw position in the buffer (char positions)
210 bloc := buffer.Loc{X: -1, Y: w.StartLine}
212 for vloc.Y = 0; vloc.Y < bufHeight; vloc.Y++ {
217 if b.Settings["ruler"].(bool) {
218 vloc.X += maxLineNumLength + 1
221 line := b.LineBytes(bloc.Y)
222 line, nColsBeforeStart, bslice := util.SliceVisualEnd(line, w.StartCol, tabsize)
226 if nColsBeforeStart <= 0 {
232 totalwidth := w.StartCol - nColsBeforeStart
234 if svloc.X <= vloc.X+w.X && vloc.Y+w.Y == svloc.Y {
238 if vloc.X+w.X == svloc.X && vloc.Y+w.Y == svloc.Y {
242 r, size := utf8.DecodeRune(line)
248 ts := tabsize - (totalwidth % tabsize)
251 width = runewidth.RuneWidth(r)
254 // Draw any extra characters either spaces for tabs or @ for incomplete wide runes
256 for i := 1; i < width; i++ {
257 if vloc.X+w.X == svloc.X && vloc.Y+w.Y == svloc.Y {
268 // If we reach the end of the window then we either stop or we wrap for softwrap
269 if vloc.X >= w.Width {
274 if vloc.Y >= bufHeight {
278 // This will draw an empty line number because the current line is wrapped
279 vloc.X += maxLineNumLength + 1
283 if vloc.Y+w.Y == svloc.Y {
289 if bloc.Y >= b.LinesNum() {
297 func (w *BufWindow) drawGutter(vloc *buffer.Loc, bloc *buffer.Loc) {
300 for _, m := range w.Buf.Messages {
301 if m.Start.Y == bloc.Y || m.End.Y == bloc.Y {
307 screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, char, nil, s)
309 screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, char, nil, s)
313 func (w *BufWindow) drawLineNum(lineNumStyle tcell.Style, softwrapped bool, maxLineNumLength int, vloc *buffer.Loc, bloc *buffer.Loc) {
314 lineNum := strconv.Itoa(bloc.Y + 1)
316 // Write the spaces before the line number if necessary
317 for i := 0; i < maxLineNumLength-len(lineNum); i++ {
318 screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
321 // Write the actual line number
322 for _, ch := range lineNum {
324 screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
326 screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ch, nil, lineNumStyle)
331 // Write the extra space
332 screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
336 // getStyle returns the highlight style for the given character position
337 // If there is no change to the current highlight style it just returns that
338 func (w *BufWindow) getStyle(style tcell.Style, bloc buffer.Loc, r rune) (tcell.Style, bool) {
339 if group, ok := w.Buf.Match(bloc.Y)[bloc.X]; ok {
340 s := config.GetColor(group.String())
346 func (w *BufWindow) showCursor(x, y int, main bool) {
349 screen.Screen.ShowCursor(x, y)
351 r, _, _, _ := screen.Screen.GetContent(x, y)
352 screen.Screen.SetContent(x, y, r, nil, config.DefStyle.Reverse(true))
357 // displayBuffer draws the buffer being shown in this window on the screen.Screen
358 func (w *BufWindow) displayBuffer() {
361 hasMessage := len(b.Messages) > 0
362 bufHeight := w.Height
367 w.hasCalcHeight = true
369 if b.Settings["syntax"].(bool) && b.SyntaxDef != nil {
370 if start > 0 && b.Rehighlight(start-1) {
371 b.Highlighter.ReHighlightLine(b, start-1)
372 b.SetRehighlight(start-1, false)
375 b.Highlighter.ReHighlightStates(b, start)
377 b.Highlighter.HighlightMatches(b, w.StartLine, w.StartLine+bufHeight)
380 lineNumStyle := config.DefStyle
381 if style, ok := config.Colorscheme["line-number"]; ok {
384 curNumStyle := config.DefStyle
385 if style, ok := config.Colorscheme["current-line-number"]; ok {
389 // We need to know the string length of the largest line number
390 // so we can pad appropriately when displaying line numbers
391 maxLineNumLength := len(strconv.Itoa(b.LinesNum()))
393 softwrap := b.Settings["softwrap"].(bool)
394 tabsize := util.IntOpt(b.Settings["tabsize"])
395 colorcolumn := util.IntOpt(b.Settings["colorcolumn"])
397 // this represents the current draw position
398 // within the current window
399 vloc := buffer.Loc{X: 0, Y: 0}
401 // this represents the current draw position in the buffer (char positions)
402 bloc := buffer.Loc{X: -1, Y: w.StartLine}
404 cursors := b.GetCursors()
406 curStyle := config.DefStyle
407 for vloc.Y = 0; vloc.Y < bufHeight; vloc.Y++ {
411 w.drawGutter(&vloc, &bloc)
414 if b.Settings["ruler"].(bool) {
416 for _, c := range cursors {
417 if bloc.Y == c.Y && w.active {
422 w.drawLineNum(s, false, maxLineNumLength, &vloc, &bloc)
425 w.gutterOffset = vloc.X
427 line, nColsBeforeStart, bslice, startStyle := w.getStartInfo(w.StartCol, bloc.Y)
428 if startStyle != nil {
429 curStyle = *startStyle
433 draw := func(r rune, style tcell.Style, showcursor bool) {
434 if nColsBeforeStart <= 0 {
435 for _, c := range cursors {
436 if c.HasSelection() &&
437 (bloc.GreaterEqual(c.CurSelection[0]) && bloc.LessThan(c.CurSelection[1]) ||
438 bloc.LessThan(c.CurSelection[0]) && bloc.GreaterEqual(c.CurSelection[1])) {
439 // The current character is selected
440 style = config.DefStyle.Reverse(true)
442 if s, ok := config.Colorscheme["selection"]; ok {
447 if b.Settings["cursorline"].(bool) && w.active &&
448 !c.HasSelection() && c.Y == bloc.Y {
449 if s, ok := config.Colorscheme["cursor-line"]; ok {
450 fg, _, _ := s.Decompose()
451 style = style.Background(fg)
456 if s, ok := config.Colorscheme["color-column"]; ok {
457 if colorcolumn != 0 && vloc.X-w.gutterOffset == colorcolumn {
458 fg, _, _ := s.Decompose()
459 style = style.Background(fg)
463 screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, r, nil, style)
466 for _, c := range cursors {
467 if c.X == bloc.X && c.Y == bloc.Y && !c.HasSelection() {
468 w.showCursor(w.X+vloc.X, w.Y+vloc.Y, c.Num == 0)
477 w.lineHeight[vloc.Y] = bloc.Y
479 totalwidth := w.StartCol - nColsBeforeStart
481 r, size := utf8.DecodeRune(line)
482 curStyle, _ = w.getStyle(curStyle, bloc, r)
484 draw(r, curStyle, true)
491 ts := tabsize - (totalwidth % tabsize)
494 width = runewidth.RuneWidth(r)
498 // Draw any extra characters either spaces for tabs or @ for incomplete wide runes
500 for i := 1; i < width; i++ {
501 draw(char, curStyle, false)
509 // If we reach the end of the window then we either stop or we wrap for softwrap
510 if vloc.X >= w.Width {
515 if vloc.Y >= bufHeight {
519 w.lineHeight[vloc.Y] = bloc.Y
520 // This will draw an empty line number because the current line is wrapped
521 w.drawLineNum(lineNumStyle, true, maxLineNumLength, &vloc, &bloc)
526 style := config.DefStyle
527 for _, c := range cursors {
528 if b.Settings["cursorline"].(bool) && w.active &&
529 !c.HasSelection() && c.Y == bloc.Y {
530 if s, ok := config.Colorscheme["cursor-line"]; ok {
531 fg, _, _ := s.Decompose()
532 style = style.Background(fg)
536 for i := vloc.X; i < w.Width; i++ {
538 if s, ok := config.Colorscheme["color-column"]; ok {
539 if colorcolumn != 0 && i-w.gutterOffset == colorcolumn {
540 fg, _, _ := s.Decompose()
541 curStyle = style.Background(fg)
544 screen.Screen.SetContent(i+w.X, vloc.Y+w.Y, ' ', nil, curStyle)
547 for _, c := range cursors {
548 if c.X == bloc.X && c.Y == bloc.Y && !c.HasSelection() {
549 w.showCursor(w.X+vloc.X, w.Y+vloc.Y, c.Num == 0)
555 if bloc.Y >= b.LinesNum() {
561 func (w *BufWindow) displayStatusLine() {
562 _, h := screen.Screen.Size()
564 if config.GetGlobalOption("infobar").(bool) {
568 if w.Buf.Settings["statusline"].(bool) {
571 } else if w.Y+w.Height != infoY {
573 for x := w.X; x < w.X+w.Width; x++ {
574 screen.Screen.SetContent(x, w.Y+w.Height-1, '-', nil, config.DefStyle.Reverse(true))
581 // Display displays the buffer and the statusline
582 func (w *BufWindow) Display() {
583 w.displayStatusLine()