| 1 |
package display |
| 2 |
|
| 3 |
import ( |
| 4 |
"strconv" |
| 5 |
"strings" |
| 6 |
|
| 7 |
runewidth "github.com/mattn/go-runewidth" |
| 8 |
"github.com/micro-editor/tcell/v2" |
| 9 |
"github.com/zyedidia/micro/v2/internal/buffer" |
| 10 |
"github.com/zyedidia/micro/v2/internal/config" |
| 11 |
"github.com/zyedidia/micro/v2/internal/screen" |
| 12 |
"github.com/zyedidia/micro/v2/internal/util" |
| 13 |
) |
| 14 |
|
| 15 |
// The BufWindow provides a way of displaying a certain section of a buffer. |
| 16 |
type BufWindow struct { |
| 17 |
*View |
| 18 |
|
| 19 |
// Buffer being shown in this window |
| 20 |
Buf *buffer.Buffer |
| 21 |
|
| 22 |
active bool |
| 23 |
|
| 24 |
sline *StatusLine |
| 25 |
|
| 26 |
bufWidth int |
| 27 |
bufHeight int |
| 28 |
gutterOffset int |
| 29 |
hasMessage bool |
| 30 |
maxLineNumLength int |
| 31 |
drawDivider bool |
| 32 |
} |
| 33 |
|
| 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 { |
| 36 |
w := new(BufWindow) |
| 37 |
w.View = new(View) |
| 38 |
w.X, w.Y, w.Width, w.Height = x, y, width, height |
| 39 |
w.SetBuffer(buf) |
| 40 |
w.active = true |
| 41 |
|
| 42 |
w.sline = NewStatusLine(w) |
| 43 |
|
| 44 |
return w |
| 45 |
} |
| 46 |
|
| 47 |
// SetBuffer sets this window's buffer. |
| 48 |
func (w *BufWindow) SetBuffer(b *buffer.Buffer) { |
| 49 |
w.Buf = b |
| 50 |
b.OptionCallback = func(option string, nativeValue any) { |
| 51 |
if option == "softwrap" { |
| 52 |
if nativeValue.(bool) { |
| 53 |
w.StartCol = 0 |
| 54 |
} else { |
| 55 |
w.StartLine.Row = 0 |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if option == "softwrap" || option == "wordwrap" { |
| 60 |
w.Relocate() |
| 61 |
for _, c := range w.Buf.GetCursors() { |
| 62 |
c.LastWrappedVisualX = c.GetVisualX(true) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if option == "diffgutter" || option == "ruler" || option == "scrollbar" || |
| 67 |
option == "statusline" { |
| 68 |
w.updateDisplayInfo() |
| 69 |
w.Relocate() |
| 70 |
} |
| 71 |
} |
| 72 |
b.GetVisualX = func(loc buffer.Loc) int { |
| 73 |
return w.VLocFromLoc(loc).VisualX |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// GetView gets the view. |
| 78 |
func (w *BufWindow) GetView() *View { |
| 79 |
return w.View |
| 80 |
} |
| 81 |
|
| 82 |
// GetView sets the view. |
| 83 |
func (w *BufWindow) SetView(view *View) { |
| 84 |
w.View = view |
| 85 |
} |
| 86 |
|
| 87 |
// Resize resizes this window. |
| 88 |
func (w *BufWindow) Resize(width, height int) { |
| 89 |
w.Width, w.Height = width, height |
| 90 |
w.updateDisplayInfo() |
| 91 |
|
| 92 |
w.Relocate() |
| 93 |
} |
| 94 |
|
| 95 |
// SetActive marks the window as active. |
| 96 |
func (w *BufWindow) SetActive(b bool) { |
| 97 |
w.active = b |
| 98 |
} |
| 99 |
|
| 100 |
// IsActive returns true if this window is active. |
| 101 |
func (w *BufWindow) IsActive() bool { |
| 102 |
return w.active |
| 103 |
} |
| 104 |
|
| 105 |
// BufView returns the width, height and x,y location of the actual buffer. |
| 106 |
// It is not exactly the same as the whole window which also contains gutter, |
| 107 |
// ruler, scrollbar and statusline. |
| 108 |
func (w *BufWindow) BufView() View { |
| 109 |
return View{ |
| 110 |
X: w.X + w.gutterOffset, |
| 111 |
Y: w.Y, |
| 112 |
Width: w.bufWidth, |
| 113 |
Height: w.bufHeight, |
| 114 |
StartLine: w.StartLine, |
| 115 |
StartCol: w.StartCol, |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func (w *BufWindow) updateDisplayInfo() { |
| 120 |
b := w.Buf |
| 121 |
|
| 122 |
w.drawDivider = false |
| 123 |
if !b.Settings["statusline"].(bool) { |
| 124 |
_, h := screen.Screen.Size() |
| 125 |
infoY := h |
| 126 |
if config.GetGlobalOption("infobar").(bool) { |
| 127 |
infoY-- |
| 128 |
} |
| 129 |
if w.Y+w.Height != infoY { |
| 130 |
w.drawDivider = true |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
w.bufHeight = w.Height |
| 135 |
if b.Settings["statusline"].(bool) || w.drawDivider { |
| 136 |
w.bufHeight-- |
| 137 |
} |
| 138 |
|
| 139 |
scrollbarWidth := 0 |
| 140 |
if w.Buf.Settings["scrollbar"].(bool) && w.Buf.LinesNum() > w.Height && w.Width > 0 { |
| 141 |
scrollbarWidth = 1 |
| 142 |
} |
| 143 |
|
| 144 |
w.hasMessage = len(b.Messages) > 0 |
| 145 |
|
| 146 |
// We need to know the string length of the largest line number |
| 147 |
// so we can pad appropriately when displaying line numbers |
| 148 |
w.maxLineNumLength = len(strconv.Itoa(b.LinesNum())) |
| 149 |
|
| 150 |
w.gutterOffset = 0 |
| 151 |
if w.hasMessage { |
| 152 |
w.gutterOffset += 2 |
| 153 |
} |
| 154 |
if b.Settings["diffgutter"].(bool) { |
| 155 |
w.gutterOffset++ |
| 156 |
} |
| 157 |
if b.Settings["ruler"].(bool) { |
| 158 |
w.gutterOffset += w.maxLineNumLength + 1 |
| 159 |
} |
| 160 |
|
| 161 |
if w.gutterOffset > w.Width-scrollbarWidth { |
| 162 |
w.gutterOffset = w.Width - scrollbarWidth |
| 163 |
} |
| 164 |
|
| 165 |
prevBufWidth := w.bufWidth |
| 166 |
w.bufWidth = w.Width - w.gutterOffset - scrollbarWidth |
| 167 |
|
| 168 |
if w.bufWidth != prevBufWidth && w.Buf.Settings["softwrap"].(bool) { |
| 169 |
for _, c := range w.Buf.GetCursors() { |
| 170 |
c.LastWrappedVisualX = c.GetVisualX(true) |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
func (w *BufWindow) getStartInfo(n, lineN int) ([]byte, int, int, *tcell.Style) { |
| 176 |
tabsize := util.IntOpt(w.Buf.Settings["tabsize"]) |
| 177 |
width := 0 |
| 178 |
bloc := buffer.Loc{0, lineN} |
| 179 |
b := w.Buf.LineBytes(lineN) |
| 180 |
curStyle := config.DefStyle |
| 181 |
var s *tcell.Style |
| 182 |
for len(b) > 0 { |
| 183 |
r, _, size := util.DecodeCharacter(b) |
| 184 |
|
| 185 |
curStyle, found := w.getStyle(curStyle, bloc) |
| 186 |
if found { |
| 187 |
s = &curStyle |
| 188 |
} |
| 189 |
|
| 190 |
w := 0 |
| 191 |
switch r { |
| 192 |
case '\t': |
| 193 |
ts := tabsize - (width % tabsize) |
| 194 |
w = ts |
| 195 |
default: |
| 196 |
w = runewidth.RuneWidth(r) |
| 197 |
} |
| 198 |
if width+w > n { |
| 199 |
return b, n - width, bloc.X, s |
| 200 |
} |
| 201 |
width += w |
| 202 |
b = b[size:] |
| 203 |
bloc.X++ |
| 204 |
} |
| 205 |
return b, n - width, bloc.X, s |
| 206 |
} |
| 207 |
|
| 208 |
// Clear resets all cells in this window to the default style |
| 209 |
func (w *BufWindow) Clear() { |
| 210 |
for y := 0; y < w.Height; y++ { |
| 211 |
for x := 0; x < w.Width; x++ { |
| 212 |
screen.SetContent(w.X+x, w.Y+y, ' ', nil, config.DefStyle) |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
// Relocate moves the view window so that the cursor is in view |
| 218 |
// This is useful if the user has scrolled far away, and then starts typing |
| 219 |
// Returns true if the window location is moved |
| 220 |
func (w *BufWindow) Relocate() bool { |
| 221 |
b := w.Buf |
| 222 |
height := w.bufHeight |
| 223 |
ret := false |
| 224 |
activeC := w.Buf.GetActiveCursor() |
| 225 |
scrollmargin := int(b.Settings["scrollmargin"].(float64)) |
| 226 |
|
| 227 |
c := w.SLocFromLoc(activeC.Loc) |
| 228 |
bStart := SLoc{0, 0} |
| 229 |
bEnd := w.SLocFromLoc(b.End()) |
| 230 |
|
| 231 |
if c.LessThan(w.Scroll(w.StartLine, scrollmargin)) && c.GreaterThan(w.Scroll(bStart, scrollmargin-1)) { |
| 232 |
w.StartLine = w.Scroll(c, -scrollmargin) |
| 233 |
ret = true |
| 234 |
} else if c.LessThan(w.StartLine) { |
| 235 |
w.StartLine = c |
| 236 |
ret = true |
| 237 |
} |
| 238 |
if c.GreaterThan(w.Scroll(w.StartLine, height-1-scrollmargin)) && c.LessEqual(w.Scroll(bEnd, -scrollmargin)) { |
| 239 |
w.StartLine = w.Scroll(c, -height+1+scrollmargin) |
| 240 |
ret = true |
| 241 |
} else if c.GreaterThan(w.Scroll(bEnd, -scrollmargin)) && c.GreaterThan(w.Scroll(w.StartLine, height-1)) { |
| 242 |
w.StartLine = w.Scroll(bEnd, -height+1) |
| 243 |
ret = true |
| 244 |
} |
| 245 |
|
| 246 |
// horizontal relocation (scrolling) |
| 247 |
if !b.Settings["softwrap"].(bool) { |
| 248 |
cx := activeC.GetVisualX(false) |
| 249 |
rw := runewidth.RuneWidth(activeC.RuneUnder(activeC.X)) |
| 250 |
if rw == 0 { |
| 251 |
rw = 1 // tab or newline |
| 252 |
} |
| 253 |
|
| 254 |
if cx < w.StartCol { |
| 255 |
w.StartCol = cx |
| 256 |
ret = true |
| 257 |
} |
| 258 |
if cx+rw > w.StartCol+w.bufWidth { |
| 259 |
w.StartCol = cx - w.bufWidth + rw |
| 260 |
ret = true |
| 261 |
} |
| 262 |
} |
| 263 |
return ret |
| 264 |
} |
| 265 |
|
| 266 |
// LocFromVisual takes a visual location (x and y position) and returns the |
| 267 |
// position in the buffer corresponding to the visual location |
| 268 |
// If the requested position does not correspond to a buffer location it returns |
| 269 |
// the nearest position |
| 270 |
func (w *BufWindow) LocFromVisual(svloc buffer.Loc) buffer.Loc { |
| 271 |
vx := svloc.X - w.X - w.gutterOffset |
| 272 |
if vx < 0 { |
| 273 |
vx = 0 |
| 274 |
} |
| 275 |
vloc := VLoc{ |
| 276 |
SLoc: w.Scroll(w.StartLine, svloc.Y-w.Y), |
| 277 |
VisualX: vx + w.StartCol, |
| 278 |
} |
| 279 |
return w.LocFromVLoc(vloc) |
| 280 |
} |
| 281 |
|
| 282 |
func (w *BufWindow) drawGutter(vloc *buffer.Loc, bloc *buffer.Loc) { |
| 283 |
char := ' ' |
| 284 |
s := config.DefStyle |
| 285 |
for _, m := range w.Buf.Messages { |
| 286 |
if m.Start.Y == bloc.Y || m.End.Y == bloc.Y { |
| 287 |
s = m.Style() |
| 288 |
char = '>' |
| 289 |
break |
| 290 |
} |
| 291 |
} |
| 292 |
for i := 0; i < 2 && vloc.X < w.gutterOffset; i++ { |
| 293 |
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, char, nil, s) |
| 294 |
vloc.X++ |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
func (w *BufWindow) drawDiffGutter(backgroundStyle tcell.Style, softwrapped bool, vloc *buffer.Loc, bloc *buffer.Loc) { |
| 299 |
if vloc.X >= w.gutterOffset { |
| 300 |
return |
| 301 |
} |
| 302 |
|
| 303 |
symbol := ' ' |
| 304 |
styleName := "" |
| 305 |
|
| 306 |
switch w.Buf.DiffStatus(bloc.Y) { |
| 307 |
case buffer.DSAdded: |
| 308 |
symbol = '\u258C' // Left half block |
| 309 |
styleName = "diff-added" |
| 310 |
case buffer.DSModified: |
| 311 |
symbol = '\u258C' // Left half block |
| 312 |
styleName = "diff-modified" |
| 313 |
case buffer.DSDeletedAbove: |
| 314 |
if !softwrapped { |
| 315 |
symbol = '\u2594' // Upper one eighth block |
| 316 |
styleName = "diff-deleted" |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
style := backgroundStyle |
| 321 |
if s, ok := config.Colorscheme[styleName]; ok { |
| 322 |
foreground, _, _ := s.Decompose() |
| 323 |
style = style.Foreground(foreground) |
| 324 |
} |
| 325 |
|
| 326 |
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, symbol, nil, style) |
| 327 |
vloc.X++ |
| 328 |
} |
| 329 |
|
| 330 |
func (w *BufWindow) drawLineNum(lineNumStyle tcell.Style, softwrapped bool, vloc *buffer.Loc, bloc *buffer.Loc) { |
| 331 |
cursorLine := w.Buf.GetActiveCursor().Loc.Y |
| 332 |
var lineInt int |
| 333 |
if w.Buf.Settings["relativeruler"] == false || cursorLine == bloc.Y { |
| 334 |
lineInt = bloc.Y + 1 |
| 335 |
} else { |
| 336 |
lineInt = bloc.Y - cursorLine |
| 337 |
} |
| 338 |
lineNum := []rune(strconv.Itoa(util.Abs(lineInt))) |
| 339 |
|
| 340 |
// Write the spaces before the line number if necessary |
| 341 |
for i := 0; i < w.maxLineNumLength-len(lineNum) && vloc.X < w.gutterOffset; i++ { |
| 342 |
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle) |
| 343 |
vloc.X++ |
| 344 |
} |
| 345 |
// Write the actual line number |
| 346 |
for i := 0; i < len(lineNum) && vloc.X < w.gutterOffset; i++ { |
| 347 |
if softwrapped || (w.bufWidth == 0 && w.Buf.Settings["softwrap"] == true) { |
| 348 |
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle) |
| 349 |
} else { |
| 350 |
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, lineNum[i], nil, lineNumStyle) |
| 351 |
} |
| 352 |
vloc.X++ |
| 353 |
} |
| 354 |
|
| 355 |
// Write the extra space |
| 356 |
if vloc.X < w.gutterOffset { |
| 357 |
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle) |
| 358 |
vloc.X++ |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
// getStyle returns the highlight style for the given character position |
| 363 |
// If there is no change to the current highlight style it just returns that |
| 364 |
func (w *BufWindow) getStyle(style tcell.Style, bloc buffer.Loc) (tcell.Style, bool) { |
| 365 |
if group, ok := w.Buf.Match(bloc.Y)[bloc.X]; ok { |
| 366 |
s := config.GetColor(group.String()) |
| 367 |
return s, true |
| 368 |
} |
| 369 |
return style, false |
| 370 |
} |
| 371 |
|
| 372 |
func (w *BufWindow) showCursor(x, y int, main bool) { |
| 373 |
if w.active { |
| 374 |
if main { |
| 375 |
screen.ShowCursor(x, y) |
| 376 |
} else { |
| 377 |
screen.ShowFakeCursorMulti(x, y) |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
// displayBuffer draws the buffer being shown in this window on the screen.Screen |
| 383 |
func (w *BufWindow) displayBuffer() { |
| 384 |
b := w.Buf |
| 385 |
|
| 386 |
if w.Height <= 0 || w.Width <= 0 { |
| 387 |
return |
| 388 |
} |
| 389 |
|
| 390 |
maxWidth := w.gutterOffset + w.bufWidth |
| 391 |
|
| 392 |
if b.ModifiedThisFrame { |
| 393 |
if b.Settings["diffgutter"].(bool) { |
| 394 |
b.UpdateDiff() |
| 395 |
} |
| 396 |
b.ModifiedThisFrame = false |
| 397 |
} |
| 398 |
|
| 399 |
var matchingBraces []buffer.Loc |
| 400 |
// bracePairs is defined in buffer.go |
| 401 |
if b.Settings["matchbrace"].(bool) { |
| 402 |
for _, c := range b.GetCursors() { |
| 403 |
if c.HasSelection() { |
| 404 |
continue |
| 405 |
} |
| 406 |
|
| 407 |
mb, left, found := b.FindMatchingBrace(c.Loc) |
| 408 |
if found { |
| 409 |
matchingBraces = append(matchingBraces, mb) |
| 410 |
if !left { |
| 411 |
if b.Settings["matchbracestyle"].(string) != "highlight" { |
| 412 |
matchingBraces = append(matchingBraces, c.Loc) |
| 413 |
} |
| 414 |
} else { |
| 415 |
matchingBraces = append(matchingBraces, c.Loc.Move(-1, b)) |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
lineNumStyle := config.DefStyle |
| 422 |
if style, ok := config.Colorscheme["line-number"]; ok { |
| 423 |
lineNumStyle = style |
| 424 |
} |
| 425 |
curNumStyle := config.DefStyle |
| 426 |
if style, ok := config.Colorscheme["current-line-number"]; ok { |
| 427 |
if !b.Settings["cursorline"].(bool) { |
| 428 |
curNumStyle = lineNumStyle |
| 429 |
} else { |
| 430 |
curNumStyle = style |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
softwrap := b.Settings["softwrap"].(bool) |
| 435 |
wordwrap := softwrap && b.Settings["wordwrap"].(bool) |
| 436 |
|
| 437 |
tabsize := util.IntOpt(b.Settings["tabsize"]) |
| 438 |
colorcolumn := util.IntOpt(b.Settings["colorcolumn"]) |
| 439 |
|
| 440 |
// this represents the current draw position |
| 441 |
// within the current window |
| 442 |
vloc := buffer.Loc{X: 0, Y: 0} |
| 443 |
if softwrap { |
| 444 |
// the start line may be partially out of the current window |
| 445 |
vloc.Y = -w.StartLine.Row |
| 446 |
} |
| 447 |
|
| 448 |
// this represents the current draw position in the buffer (char positions) |
| 449 |
bloc := buffer.Loc{X: -1, Y: w.StartLine.Line} |
| 450 |
|
| 451 |
cursors := b.GetCursors() |
| 452 |
|
| 453 |
curStyle := config.DefStyle |
| 454 |
|
| 455 |
// Parse showchars which is in the format of key1=val1,key2=val2,... |
| 456 |
spacechars := " " |
| 457 |
tabchars := b.Settings["indentchar"].(string) |
| 458 |
var indentspacechars string |
| 459 |
var indenttabchars string |
| 460 |
for _, entry := range strings.Split(b.Settings["showchars"].(string), ",") { |
| 461 |
split := strings.SplitN(entry, "=", 2) |
| 462 |
if len(split) < 2 { |
| 463 |
continue |
| 464 |
} |
| 465 |
key, val := split[0], split[1] |
| 466 |
switch key { |
| 467 |
case "space": |
| 468 |
spacechars = val |
| 469 |
case "tab": |
| 470 |
tabchars = val |
| 471 |
case "ispace": |
| 472 |
indentspacechars = val |
| 473 |
case "itab": |
| 474 |
indenttabchars = val |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
for ; vloc.Y < w.bufHeight; vloc.Y++ { |
| 479 |
vloc.X = 0 |
| 480 |
|
| 481 |
currentLine := false |
| 482 |
for _, c := range cursors { |
| 483 |
if !c.HasSelection() && bloc.Y == c.Y && w.active { |
| 484 |
currentLine = true |
| 485 |
break |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
s := lineNumStyle |
| 490 |
if currentLine { |
| 491 |
s = curNumStyle |
| 492 |
} |
| 493 |
|
| 494 |
if vloc.Y >= 0 { |
| 495 |
if w.hasMessage { |
| 496 |
w.drawGutter(&vloc, &bloc) |
| 497 |
} |
| 498 |
|
| 499 |
if b.Settings["diffgutter"].(bool) { |
| 500 |
w.drawDiffGutter(s, false, &vloc, &bloc) |
| 501 |
} |
| 502 |
|
| 503 |
if b.Settings["ruler"].(bool) { |
| 504 |
w.drawLineNum(s, false, &vloc, &bloc) |
| 505 |
} |
| 506 |
} else { |
| 507 |
vloc.X = w.gutterOffset |
| 508 |
} |
| 509 |
|
| 510 |
bline := b.LineBytes(bloc.Y) |
| 511 |
blineLen := util.CharacterCount(bline) |
| 512 |
|
| 513 |
leadingwsEnd := len(util.GetLeadingWhitespace(bline)) |
| 514 |
trailingwsStart := blineLen - util.CharacterCount(util.GetTrailingWhitespace(bline)) |
| 515 |
|
| 516 |
line, nColsBeforeStart, bslice, startStyle := w.getStartInfo(w.StartCol, bloc.Y) |
| 517 |
if startStyle != nil { |
| 518 |
curStyle = *startStyle |
| 519 |
} |
| 520 |
bloc.X = bslice |
| 521 |
|
| 522 |
// returns the rune to be drawn, style of it and if the bg should be preserved |
| 523 |
getRuneStyle := func(r rune, style tcell.Style, showoffset int, linex int, isplaceholder bool) (rune, tcell.Style, bool) { |
| 524 |
if nColsBeforeStart > 0 || vloc.Y < 0 || isplaceholder { |
| 525 |
return r, style, false |
| 526 |
} |
| 527 |
|
| 528 |
for _, mb := range matchingBraces { |
| 529 |
if mb.X == bloc.X && mb.Y == bloc.Y { |
| 530 |
if b.Settings["matchbracestyle"].(string) == "highlight" { |
| 531 |
if s, ok := config.Colorscheme["match-brace"]; ok { |
| 532 |
return r, s, false |
| 533 |
} else { |
| 534 |
return r, style.Reverse(true), false |
| 535 |
} |
| 536 |
} else { |
| 537 |
return r, style.Underline(true), false |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
if r != '\t' && r != ' ' { |
| 543 |
return r, style, false |
| 544 |
} |
| 545 |
|
| 546 |
var indentrunes []rune |
| 547 |
switch r { |
| 548 |
case '\t': |
| 549 |
if bloc.X < leadingwsEnd && indenttabchars != "" { |
| 550 |
indentrunes = []rune(indenttabchars) |
| 551 |
} else { |
| 552 |
indentrunes = []rune(tabchars) |
| 553 |
} |
| 554 |
case ' ': |
| 555 |
if linex%tabsize == 0 && bloc.X < leadingwsEnd && indentspacechars != "" { |
| 556 |
indentrunes = []rune(indentspacechars) |
| 557 |
} else { |
| 558 |
indentrunes = []rune(spacechars) |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
var drawrune rune |
| 563 |
if showoffset < len(indentrunes) { |
| 564 |
drawrune = indentrunes[showoffset] |
| 565 |
} else { |
| 566 |
// use space if no showchars or after we showed showchars |
| 567 |
drawrune = ' ' |
| 568 |
} |
| 569 |
|
| 570 |
if s, ok := config.Colorscheme["indent-char"]; ok { |
| 571 |
fg, _, _ := s.Decompose() |
| 572 |
style = style.Foreground(fg) |
| 573 |
} |
| 574 |
|
| 575 |
preservebg := false |
| 576 |
if b.Settings["hltaberrors"].(bool) && bloc.X < leadingwsEnd { |
| 577 |
if s, ok := config.Colorscheme["tab-error"]; ok { |
| 578 |
if b.Settings["tabstospaces"].(bool) && r == '\t' { |
| 579 |
fg, _, _ := s.Decompose() |
| 580 |
style = style.Background(fg) |
| 581 |
preservebg = true |
| 582 |
} else if !b.Settings["tabstospaces"].(bool) && r == ' ' { |
| 583 |
fg, _, _ := s.Decompose() |
| 584 |
style = style.Background(fg) |
| 585 |
preservebg = true |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
if b.Settings["hltrailingws"].(bool) { |
| 591 |
if s, ok := config.Colorscheme["trailingws"]; ok { |
| 592 |
if bloc.X >= trailingwsStart && bloc.X < blineLen { |
| 593 |
hl := true |
| 594 |
for _, c := range cursors { |
| 595 |
if c.NewTrailingWsY == bloc.Y { |
| 596 |
hl = false |
| 597 |
break |
| 598 |
} |
| 599 |
} |
| 600 |
if hl { |
| 601 |
fg, _, _ := s.Decompose() |
| 602 |
style = style.Background(fg) |
| 603 |
preservebg = true |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
return drawrune, style, preservebg |
| 610 |
} |
| 611 |
|
| 612 |
draw := func(r rune, combc []rune, style tcell.Style, highlight bool, showcursor bool, preservebg bool) { |
| 613 |
defer func() { |
| 614 |
if nColsBeforeStart <= 0 { |
| 615 |
vloc.X++ |
| 616 |
} |
| 617 |
nColsBeforeStart-- |
| 618 |
}() |
| 619 |
|
| 620 |
if nColsBeforeStart > 0 || vloc.Y < 0 { |
| 621 |
return |
| 622 |
} |
| 623 |
|
| 624 |
if highlight { |
| 625 |
if w.Buf.HighlightSearch && w.Buf.SearchMatch(bloc) { |
| 626 |
style = config.DefStyle.Reverse(true) |
| 627 |
if s, ok := config.Colorscheme["hlsearch"]; ok { |
| 628 |
style = s |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
_, origBg, _ := style.Decompose() |
| 633 |
_, defBg, _ := config.DefStyle.Decompose() |
| 634 |
|
| 635 |
// syntax or hlsearch highlighting with non-default background takes precedence |
| 636 |
// over cursor-line and color-column |
| 637 |
if !preservebg && origBg != defBg { |
| 638 |
preservebg = true |
| 639 |
} |
| 640 |
|
| 641 |
for _, c := range cursors { |
| 642 |
if c.HasSelection() && |
| 643 |
(bloc.GreaterEqual(c.CurSelection[0]) && bloc.LessThan(c.CurSelection[1]) || |
| 644 |
bloc.LessThan(c.CurSelection[0]) && bloc.GreaterEqual(c.CurSelection[1])) { |
| 645 |
// The current character is selected |
| 646 |
style = config.DefStyle.Reverse(true) |
| 647 |
|
| 648 |
if s, ok := config.Colorscheme["selection"]; ok { |
| 649 |
style = s |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
if b.Settings["cursorline"].(bool) && w.active && !preservebg && |
| 654 |
!c.HasSelection() && c.Y == bloc.Y { |
| 655 |
if s, ok := config.Colorscheme["cursor-line"]; ok { |
| 656 |
fg, _, _ := s.Decompose() |
| 657 |
style = style.Background(fg) |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
for _, m := range b.Messages { |
| 663 |
if bloc.GreaterEqual(m.Start) && bloc.LessThan(m.End) || |
| 664 |
bloc.LessThan(m.End) && bloc.GreaterEqual(m.Start) { |
| 665 |
style = style.Underline(true) |
| 666 |
break |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
if s, ok := config.Colorscheme["color-column"]; ok { |
| 671 |
if colorcolumn != 0 && vloc.X-w.gutterOffset+w.StartCol == colorcolumn && !preservebg { |
| 672 |
fg, _, _ := s.Decompose() |
| 673 |
style = style.Background(fg) |
| 674 |
} |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, r, combc, style) |
| 679 |
|
| 680 |
if showcursor { |
| 681 |
for _, c := range cursors { |
| 682 |
if c.X == bloc.X && c.Y == bloc.Y && !c.HasSelection() { |
| 683 |
w.showCursor(w.X+vloc.X, w.Y+vloc.Y, c.Num == 0) |
| 684 |
} |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
wrap := func() { |
| 690 |
vloc.X = 0 |
| 691 |
|
| 692 |
if vloc.Y >= 0 { |
| 693 |
if w.hasMessage { |
| 694 |
w.drawGutter(&vloc, &bloc) |
| 695 |
} |
| 696 |
if b.Settings["diffgutter"].(bool) { |
| 697 |
w.drawDiffGutter(lineNumStyle, true, &vloc, &bloc) |
| 698 |
} |
| 699 |
|
| 700 |
// This will draw an empty line number because the current line is wrapped |
| 701 |
if b.Settings["ruler"].(bool) { |
| 702 |
w.drawLineNum(lineNumStyle, true, &vloc, &bloc) |
| 703 |
} |
| 704 |
} else { |
| 705 |
vloc.X = w.gutterOffset |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
type glyph struct { |
| 710 |
r rune |
| 711 |
combc []rune |
| 712 |
style tcell.Style |
| 713 |
width int |
| 714 |
} |
| 715 |
|
| 716 |
var word []glyph |
| 717 |
if wordwrap { |
| 718 |
word = make([]glyph, 0, w.bufWidth) |
| 719 |
} else { |
| 720 |
word = make([]glyph, 0, 1) |
| 721 |
} |
| 722 |
wordwidth := 0 |
| 723 |
|
| 724 |
totalwidth := w.StartCol - nColsBeforeStart |
| 725 |
for len(line) > 0 && vloc.X < maxWidth { |
| 726 |
r, combc, size := util.DecodeCharacter(line) |
| 727 |
line = line[size:] |
| 728 |
|
| 729 |
loc := buffer.Loc{X: bloc.X + len(word), Y: bloc.Y} |
| 730 |
curStyle, _ = w.getStyle(curStyle, loc) |
| 731 |
|
| 732 |
width := 0 |
| 733 |
|
| 734 |
linex := totalwidth |
| 735 |
switch r { |
| 736 |
case '\t': |
| 737 |
ts := tabsize - (totalwidth % tabsize) |
| 738 |
width = util.Min(ts, maxWidth-vloc.X) |
| 739 |
totalwidth += ts |
| 740 |
default: |
| 741 |
width = runewidth.RuneWidth(r) |
| 742 |
totalwidth += width |
| 743 |
} |
| 744 |
|
| 745 |
word = append(word, glyph{r, combc, curStyle, width}) |
| 746 |
wordwidth += width |
| 747 |
|
| 748 |
// Collect a complete word to know its width. |
| 749 |
// If wordwrap is off, every single character is a complete "word". |
| 750 |
if wordwrap { |
| 751 |
if !util.IsWhitespace(r) && len(line) > 0 && wordwidth < w.bufWidth { |
| 752 |
continue |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
// If a word (or just a wide rune) does not fit in the window |
| 757 |
if vloc.X+wordwidth > maxWidth && vloc.X > w.gutterOffset { |
| 758 |
for vloc.X < maxWidth { |
| 759 |
draw(' ', nil, config.DefStyle, false, false, true) |
| 760 |
} |
| 761 |
|
| 762 |
// We either stop or we wrap to draw the word in the next line |
| 763 |
if !softwrap { |
| 764 |
break |
| 765 |
} else { |
| 766 |
vloc.Y++ |
| 767 |
if vloc.Y >= w.bufHeight { |
| 768 |
break |
| 769 |
} |
| 770 |
wrap() |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
for _, r := range word { |
| 775 |
drawrune, drawstyle, preservebg := getRuneStyle(r.r, r.style, 0, linex, false) |
| 776 |
draw(drawrune, r.combc, drawstyle, true, true, preservebg) |
| 777 |
|
| 778 |
// Draw extra characters for tabs or wide runes |
| 779 |
for i := 1; i < r.width; i++ { |
| 780 |
if r.r == '\t' { |
| 781 |
drawrune, drawstyle, preservebg = getRuneStyle('\t', r.style, i, linex+i, false) |
| 782 |
} else { |
| 783 |
drawrune, drawstyle, preservebg = getRuneStyle(' ', r.style, i, linex+i, true) |
| 784 |
} |
| 785 |
draw(drawrune, nil, drawstyle, true, false, preservebg) |
| 786 |
} |
| 787 |
bloc.X++ |
| 788 |
} |
| 789 |
|
| 790 |
word = word[:0] |
| 791 |
wordwidth = 0 |
| 792 |
|
| 793 |
// If we reach the end of the window then we either stop or we wrap for softwrap |
| 794 |
if vloc.X >= maxWidth { |
| 795 |
if !softwrap { |
| 796 |
break |
| 797 |
} else { |
| 798 |
vloc.Y++ |
| 799 |
if vloc.Y >= w.bufHeight { |
| 800 |
break |
| 801 |
} |
| 802 |
wrap() |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
style := config.DefStyle |
| 808 |
for _, c := range cursors { |
| 809 |
if b.Settings["cursorline"].(bool) && w.active && |
| 810 |
!c.HasSelection() && c.Y == bloc.Y { |
| 811 |
if s, ok := config.Colorscheme["cursor-line"]; ok { |
| 812 |
fg, _, _ := s.Decompose() |
| 813 |
style = style.Background(fg) |
| 814 |
} |
| 815 |
} |
| 816 |
} |
| 817 |
for i := vloc.X; i < maxWidth; i++ { |
| 818 |
curStyle := style |
| 819 |
if s, ok := config.Colorscheme["color-column"]; ok { |
| 820 |
if colorcolumn != 0 && i-w.gutterOffset+w.StartCol == colorcolumn { |
| 821 |
fg, _, _ := s.Decompose() |
| 822 |
curStyle = style.Background(fg) |
| 823 |
} |
| 824 |
} |
| 825 |
screen.SetContent(i+w.X, vloc.Y+w.Y, ' ', nil, curStyle) |
| 826 |
} |
| 827 |
|
| 828 |
if vloc.X != maxWidth { |
| 829 |
// Display newline within a selection |
| 830 |
drawrune, drawstyle, preservebg := getRuneStyle(' ', config.DefStyle, 0, totalwidth, true) |
| 831 |
draw(drawrune, nil, drawstyle, true, true, preservebg) |
| 832 |
} |
| 833 |
|
| 834 |
bloc.X = w.StartCol |
| 835 |
bloc.Y++ |
| 836 |
if bloc.Y >= b.LinesNum() { |
| 837 |
break |
| 838 |
} |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
func (w *BufWindow) displayStatusLine() { |
| 843 |
if w.Buf.Settings["statusline"].(bool) { |
| 844 |
w.sline.Display() |
| 845 |
} else if w.drawDivider { |
| 846 |
divchars := config.GetGlobalOption("divchars").(string) |
| 847 |
if util.CharacterCountInString(divchars) != 2 { |
| 848 |
divchars = "|-" |
| 849 |
} |
| 850 |
|
| 851 |
_, _, size := util.DecodeCharacterInString(divchars) |
| 852 |
divchar, combc, _ := util.DecodeCharacterInString(divchars[size:]) |
| 853 |
|
| 854 |
dividerStyle := config.DefStyle |
| 855 |
if style, ok := config.Colorscheme["divider"]; ok { |
| 856 |
dividerStyle = style |
| 857 |
} |
| 858 |
|
| 859 |
divreverse := config.GetGlobalOption("divreverse").(bool) |
| 860 |
if divreverse { |
| 861 |
dividerStyle = dividerStyle.Reverse(true) |
| 862 |
} |
| 863 |
|
| 864 |
for x := w.X; x < w.X+w.Width; x++ { |
| 865 |
screen.SetContent(x, w.Y+w.Height-1, divchar, combc, dividerStyle) |
| 866 |
} |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
func (w *BufWindow) displayScrollBar() { |
| 871 |
if w.Buf.Settings["scrollbar"].(bool) && w.Buf.LinesNum() > w.Height { |
| 872 |
scrollX := w.X + w.Width - 1 |
| 873 |
barsize := int(float64(w.Height) / float64(w.Buf.LinesNum()) * float64(w.Height)) |
| 874 |
if barsize < 1 { |
| 875 |
barsize = 1 |
| 876 |
} |
| 877 |
barstart := w.Y + int(float64(w.StartLine.Line)/float64(w.Buf.LinesNum())*float64(w.Height)) |
| 878 |
|
| 879 |
scrollBarStyle := config.DefStyle.Reverse(true) |
| 880 |
if style, ok := config.Colorscheme["scrollbar"]; ok { |
| 881 |
scrollBarStyle = style |
| 882 |
} |
| 883 |
|
| 884 |
scrollBarChar := config.GetGlobalOption("scrollbarchar").(string) |
| 885 |
if util.CharacterCountInString(scrollBarChar) != 1 { |
| 886 |
scrollBarChar = "|" |
| 887 |
} |
| 888 |
scrollBarRune := []rune(scrollBarChar) |
| 889 |
|
| 890 |
for y := barstart; y < util.Min(barstart+barsize, w.Y+w.bufHeight); y++ { |
| 891 |
screen.SetContent(scrollX, y, scrollBarRune[0], nil, scrollBarStyle) |
| 892 |
} |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
// Display displays the buffer and the statusline |
| 897 |
func (w *BufWindow) Display() { |
| 898 |
w.updateDisplayInfo() |
| 899 |
|
| 900 |
w.displayStatusLine() |
| 901 |
w.displayScrollBar() |
| 902 |
w.displayBuffer() |
| 903 |
} |