@@ -494,7 +494,83 @@ func (w *BufWindow) displayBuffer() {
494494 }
495495 bloc .X = bslice
496496
497- draw := func (r rune , combc []rune , style tcell.Style , highlight bool , showcursor bool ) {
497+ // returns the rune to be drawn, style of it and if the bg should be preserved
498+ getRuneStyle := func (r rune , style tcell.Style , isplaceholder bool ) (rune , tcell.Style , bool ) {
499+ if nColsBeforeStart > 0 || vloc .Y < 0 || isplaceholder {
500+ return r , style , false
501+ }
502+
503+ for _ , mb := range matchingBraces {
504+ if mb .X == bloc .X && mb .Y == bloc .Y {
505+ if b .Settings ["matchbracestyle" ].(string ) == "highlight" {
506+ if s , ok := config .Colorscheme ["match-brace" ]; ok {
507+ return r , s , false
508+ } else {
509+ return r , style .Reverse (true ), false
510+ }
511+ } else {
512+ return r , style .Underline (true ), false
513+ }
514+ }
515+ }
516+
517+ if r != '\t' && r != ' ' {
518+ return r , style , false
519+ }
520+
521+ var drawrune rune
522+ if r == '\t' {
523+ indentrunes := []rune (b .Settings ["indentchar" ].(string ))
524+ // if empty indentchar settings, use space
525+ if len (indentrunes ) == 0 {
526+ indentrunes = []rune {' ' }
527+ }
528+
529+ drawrune = indentrunes [0 ]
530+ if s , ok := config .Colorscheme ["indent-char" ]; ok {
531+ fg , _ , _ := s .Decompose ()
532+ style = style .Foreground (fg )
533+ }
534+ }
535+
536+ preservebg := false
537+ if b .Settings ["hltaberrors" ].(bool ) && bloc .X < leadingwsEnd {
538+ if s , ok := config .Colorscheme ["tab-error" ]; ok {
539+ if b .Settings ["tabstospaces" ].(bool ) && r == '\t' {
540+ fg , _ , _ := s .Decompose ()
541+ style = style .Background (fg )
542+ preservebg = true
543+ } else if ! b .Settings ["tabstospaces" ].(bool ) && r == ' ' {
544+ fg , _ , _ := s .Decompose ()
545+ style = style .Background (fg )
546+ preservebg = true
547+ }
548+ }
549+ }
550+
551+ if b .Settings ["hltrailingws" ].(bool ) {
552+ if s , ok := config .Colorscheme ["trailingws" ]; ok {
553+ if bloc .X >= trailingwsStart && bloc .X < blineLen {
554+ hl := true
555+ for _ , c := range cursors {
556+ if c .NewTrailingWsY == bloc .Y {
557+ hl = false
558+ break
559+ }
560+ }
561+ if hl {
562+ fg , _ , _ := s .Decompose ()
563+ style = style .Background (fg )
564+ preservebg = true
565+ }
566+ }
567+ }
568+ }
569+
570+ return drawrune , style , preservebg
571+ }
572+
573+ draw := func (r rune , combc []rune , style tcell.Style , highlight bool , showcursor bool , preservebg bool ) {
498574 if nColsBeforeStart <= 0 && vloc .Y >= 0 {
499575 if highlight {
500576 if w .Buf .HighlightSearch && w .Buf .SearchMatch (bloc ) {
@@ -509,37 +585,8 @@ func (w *BufWindow) displayBuffer() {
509585
510586 // syntax or hlsearch highlighting with non-default background takes precedence
511587 // over cursor-line and color-column
512- dontOverrideBackground := origBg != defBg
513-
514- if b .Settings ["hltaberrors" ].(bool ) {
515- if s , ok := config .Colorscheme ["tab-error" ]; ok {
516- isTab := (r == '\t' ) || (r == ' ' && ! showcursor )
517- if (b .Settings ["tabstospaces" ].(bool ) && isTab ) ||
518- (! b .Settings ["tabstospaces" ].(bool ) && bloc .X < leadingwsEnd && r == ' ' && ! isTab ) {
519- fg , _ , _ := s .Decompose ()
520- style = style .Background (fg )
521- dontOverrideBackground = true
522- }
523- }
524- }
525-
526- if b .Settings ["hltrailingws" ].(bool ) {
527- if s , ok := config .Colorscheme ["trailingws" ]; ok {
528- if bloc .X >= trailingwsStart && bloc .X < blineLen {
529- hl := true
530- for _ , c := range cursors {
531- if c .NewTrailingWsY == bloc .Y {
532- hl = false
533- break
534- }
535- }
536- if hl {
537- fg , _ , _ := s .Decompose ()
538- style = style .Background (fg )
539- dontOverrideBackground = true
540- }
541- }
542- }
588+ if ! preservebg && origBg != defBg {
589+ preservebg = true
543590 }
544591
545592 for _ , c := range cursors {
@@ -554,7 +601,7 @@ func (w *BufWindow) displayBuffer() {
554601 }
555602 }
556603
557- if b .Settings ["cursorline" ].(bool ) && w .active && ! dontOverrideBackground &&
604+ if b .Settings ["cursorline" ].(bool ) && w .active && ! preservebg &&
558605 ! c .HasSelection () && c .Y == bloc .Y {
559606 if s , ok := config .Colorscheme ["cursor-line" ]; ok {
560607 fg , _ , _ := s .Decompose ()
@@ -571,40 +618,12 @@ func (w *BufWindow) displayBuffer() {
571618 }
572619 }
573620
574- if r == '\t' {
575- indentrunes := []rune (b .Settings ["indentchar" ].(string ))
576- // if empty indentchar settings, use space
577- if len (indentrunes ) == 0 {
578- indentrunes = []rune {' ' }
579- }
580-
581- r = indentrunes [0 ]
582- if s , ok := config .Colorscheme ["indent-char" ]; ok && r != ' ' {
583- fg , _ , _ := s .Decompose ()
584- style = style .Foreground (fg )
585- }
586- }
587-
588621 if s , ok := config .Colorscheme ["color-column" ]; ok {
589- if colorcolumn != 0 && vloc .X - w .gutterOffset + w .StartCol == colorcolumn && ! dontOverrideBackground {
622+ if colorcolumn != 0 && vloc .X - w .gutterOffset + w .StartCol == colorcolumn && ! preservebg {
590623 fg , _ , _ := s .Decompose ()
591624 style = style .Background (fg )
592625 }
593626 }
594-
595- for _ , mb := range matchingBraces {
596- if mb .X == bloc .X && mb .Y == bloc .Y {
597- if b .Settings ["matchbracestyle" ].(string ) == "highlight" {
598- if s , ok := config .Colorscheme ["match-brace" ]; ok {
599- style = s
600- } else {
601- style = style .Reverse (true )
602- }
603- } else {
604- style = style .Underline (true )
605- }
606- }
607- }
608627 }
609628
610629 screen .SetContent (w .X + vloc .X , w .Y + vloc .Y , r , combc , style )
@@ -692,7 +711,7 @@ func (w *BufWindow) displayBuffer() {
692711 // If a word (or just a wide rune) does not fit in the window
693712 if vloc .X + wordwidth > maxWidth && vloc .X > w .gutterOffset {
694713 for vloc .X < maxWidth {
695- draw (' ' , nil , config .DefStyle , false , false )
714+ draw (' ' , nil , config .DefStyle , false , false , true )
696715 }
697716
698717 // We either stop or we wrap to draw the word in the next line
@@ -708,18 +727,17 @@ func (w *BufWindow) displayBuffer() {
708727 }
709728
710729 for _ , r := range word {
711- draw (r .r , r .combc , r .style , true , true )
712-
713- // Draw any extra characters either spaces for tabs or @ for incomplete wide runes
714- if r .width > 1 {
715- char := ' '
716- if r .r != '\t' {
717- char = '@'
718- }
719-
720- for i := 1 ; i < r .width ; i ++ {
721- draw (char , nil , r .style , true , false )
730+ drawrune , drawstyle , preservebg := getRuneStyle (r .r , r .style , false )
731+ draw (drawrune , r .combc , drawstyle , true , true , preservebg )
732+
733+ // Draw extra characters for tabs or wide runes
734+ for i := 1 ; i < r .width ; i ++ {
735+ if r .r == '\t' {
736+ drawrune , drawstyle , preservebg = getRuneStyle ('\t' , r .style , false )
737+ } else {
738+ drawrune , drawstyle , preservebg = getRuneStyle (' ' , r .style , true )
722739 }
740+ draw (drawrune , nil , drawstyle , true , false , preservebg )
723741 }
724742 bloc .X ++
725743 }
@@ -764,7 +782,8 @@ func (w *BufWindow) displayBuffer() {
764782
765783 if vloc .X != maxWidth {
766784 // Display newline within a selection
767- draw (' ' , nil , config .DefStyle , true , true )
785+ drawrune , drawstyle , preservebg := getRuneStyle (' ' , config .DefStyle , true )
786+ draw (drawrune , nil , drawstyle , true , true , preservebg )
768787 }
769788
770789 bloc .X = w .StartCol
0 commit comments