junegunn / goyo.vim

:tulip: Distraction-free writing in Vim
MIT License
4.49k stars 115 forks source link

EndOfBuffer highlighting not preserved after entering and exiting Goyo #165

Closed fvgs closed 6 years ago

fvgs commented 6 years ago

Vim 8.0.1200 macOS 10.12.6 iTerm2

I have the following in my vimrc:

...
set termguicolors
highlight EndOfBuffer guifg=bg
...

The first line allows Vim to use TrueColors and the second line hides the ~ characters from being shown for empty lines in the buffer.

When I enter Goyo there is no issue and the ~ characters continue to be hidden. However, when I exit Goyo, the ~ characters become visible whereas I would expect them to stay hidden.

Looking through the plugin source, there is no mention of the EndOfBuffer highlight group. Perhaps it's related to the NonText highlight group?

mboughaba commented 6 years ago

@fvgs, NonText indeed includes EndOfBuffer. I have the same problem SignColumn LineNr highlighting is not mess up when I quit Goyo.

I suspected this to be the root cause

function! s:tranquilize()
  let bg = s:get_color('Normal', 'bg#')
  for grp in ['NonText', 'FoldColumn', 'ColorColumn', 'VertSplit',
            \ 'StatusLine', 'StatusLineNC', 'SignColumn']
    " -1 on Vim / '' on GVim
    if bg == -1 || empty(bg)
      call s:set_color(grp, 'fg', get(g:, 'goyo_bg', 'black'))
      call s:set_color(grp, 'bg', 'NONE')
    else
      call s:set_color(grp, 'fg', bg)
      call s:set_color(grp, 'bg', bg)
    endif
    call s:set_color(grp, '', 'NONE')
  endfor
endfunction

I suggest to cache the original values somewhere and restore them on Goyo exit command.

junegunn commented 6 years ago

Goyo reloads your color scheme on exit, and your custom patches are lost. This also means that your adjustments will be lost whenever you simply switch color schemes.

To reliably apply custom colors, you might want to use autocmd ColorScheme as suggested in the following link.

See https://github.com/junegunn/goyo.vim/issues/84

mboughaba commented 6 years ago

Works indeed as a charm :+1: and it is much cleaner to patch colorscheme this way. I will make a PR to mention this in the README. It could be useful for newcomers ;)

fvgs commented 6 years ago

Works for me too

This was my final script based on https://github.com/junegunn/goyo.vim/issues/84#issuecomment-156298019:

...
colorscheme jellybeans

" Apply custom highlights
fun! s:Highlight()
  ...
  " Hide ~ characters shown for empty lines
  highlight EndOfBuffer guifg=bg
  ...
endfun

call s:Highlight()

" Reapply custom highlights when the color scheme is reloaded
augroup Highlight
  autocmd! ColorScheme * call s:Highlight()
augroup end
...

Thanks for your response and making a great plugin @junegunn! 😄