Vim-airline: airline broken after filetype-based colorscheme change

Created on 14 Jun 2020  路  19Comments  路  Source: vim-airline/vim-airline

Environment

  • vim: Neovim v0.4.3
  • vim-airline: ed85ed4
  • OS: Debian testing (bullseye)
  • Have you reproduced with a minimal vimrc: no
  • What is your airline configuration: see below
  • terminal: gnome-terminal
  • $TERM variable: xterm-256color
  • color configuration (:set t_Co?): Key code not set
  • does it happen in Vim: not tested, don't know how to easily transfer my configuration

Hi,

I am changing my colorscheme for certain filetyped. Specifically, I have "colorscheme github' in ../ftplugin/tex.vim. When I then open a file of this type, airline colors are overall messed up. I realize that is vague, but I find that the following (also in ../ftplugin/tex.vim below the colorscheme command) mostly fixes things:

hi! link airline_tab airline_x
hi! link airline_tabfill airline_x
hi! link airline_tabmod airline_a
hi! link airline_tabsel airline_a
hi! link airline_tabtype airline_z_red
hi! link airline_tablabel airline_b
hi! link airline_tabmod_unsel airline_x
hi! link airline_tabhid airline_x
hi! link airline_tablabel_right airline_tablabel
hi! link airline_tab_right airline_z
hi! link airline_tabsel_right airline_tab_right
hi! link airline_tabmod_right airline_tab_right
hi! link airline_tabhid_right airline_tabhid
hi! link airline_tabmod_unsel_right airline_tabmod_unsel

It looks too much like a hack to me to be a solution I am happy with....

Expected behavior

If I don't go through ftplugin but manually execute ":colorscheme github" after opening the file, everything is fine.

I hope this can be fixed or maybe someone can tell me what I am doing wrong. Please le me know if you need further information or would like me to test a tentative fix.

Best regards,

Sebastian

Airline configuration

let g:airline_theme='papercolor'
let g:airline_powerline_fonts = 1

let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#left_sep = ' '
let g:airline#extensions#tabline#left_alt_sep = '|'
let g:airline#extensions#tabline#formatter = 'unique_tail_improved'

let g:airline#extensions#ctrlp#enabled = 1

let g:airline#extensions#tagbar#flags = 'f'

if !exists("g:airline_symbols")
  let g:airline_symbols={}
endif

let g:airline_symbols.linenr = ':: ln'
let g:airline_symbols.maxlinenr = ''
in progress

All 19 comments

I have "colorscheme github' in ../ftplugin/tex.vim

This looks wrong, as colorschemes are global, they will also affect other buffers.

If I don't go through ftplugin but manually execute ":colorscheme github" after opening the file, everything is fine.

If this is true, try using an after/ftplugin directory.

I have "colorscheme github' in ../ftplugin/tex.vim

This looks wrong, as colorschemes are global, they will also affect other buffers.

That is not an issue for my workflow since I normally use separate nvim instances for different things. Essentially, I only want the different colorscheme for editing LaTeX, and another one for all else.

If I don't go through ftplugin but manually execute ":colorscheme github" after opening the file, everything is fine.

If this is true, try using an after/ftplugin directory.

Sorry, I should have mentioned I already tried that without success. But I realize I should also specify the issue more precisely: if I start nvim with a single file, everything looks still fine. But as soon as I oben another file with :e or similiar the top tabline colors turn all gray, without any indication of which tab/buffer is active.

The proper behavior would be (for my airline colorscheme) that the tabline background is blue and the current tab/buffer is indicated by a light gray background. My "fix" restores this behavior after switching once between tabs/buffers.

Thinkin about it a bit more, I think we might need an nested here:

https://github.com/vim-airline/vim-airline/blob/ee85ed4c673b859cf8c3626a6669b8e3cd2392fa/plugin/airline.vim#L142

So can you check if changing this line to this:

autocmd GUIEnter,ColorScheme * ++nested call on_colorscheme_changed()

fixes it?

Thinkin about it a bit more, I think we might need an nested here:

https://github.com/vim-airline/vim-airline/blob/ee85ed4c673b859cf8c3626a6669b8e3cd2392fa/plugin/airline.vim#L142

So can you check if changing this line to this:

autocmd GUIEnter,ColorScheme * ++nested call on_colorscheme_changed()

fixes it?

Unfortunately it does not. But I wonder now, given the precise nature of the issue I obseve, is that the right place to add something? I may be totally off, but should something perhaps be added for the BufAdd autocmd?

For clarity, let me summarize the problem again. With the following sequence of events, everything is fine:

  1. $ nvim file1.tex
  2. :colorscheme github
  3. :e file2.tex
  4. :colorscheme github

But if create after/ftplugin/tex.vim with "colorscheme github" as single line (which I would think merely automates 2 and 4 above) and run

  1. $nvim file1.tex
  2. :e file2.tex,

then after step 2 the tabline colors are broken.

While I still believe this is an issue in principle, I found there is a surprisingly simple workaround: if I merely wrap the colorscheme change in a conditional to avoid redundant execution, everything is file. That is, I have the following now in my after/ftplugin/tex.vim:
if g:colors_name != "github" colorscheme github endif

can you reproduce in Vim?

can you reproduce in Vim?

Okay, I managed to transplant my setup from Neovim to Vim. Indeed I can reproduce the same behavior, and I can resolve it with the same workaround mentioned above. So it looks like Neovim is not the culprit here.

For completeness, I tested with Vi IMproved 8.2 (2019 Dec 12, compiled May 12 2020 02:37:13), the current version in Debian bullseye.

I am not exactly sure what causes this, it looks like the autocommand to load the matching airline theme is not run in your case and the default changing of color schemes resets the coloring.

What works, and perhaps it is the easiest way is to add the following line to your tex.vim file. I tested it with the molokai colorscheme, because I do not have github installed.

colorscheme molokai
AirlineTheme molokai

I am not sure what AirlineTheme you want to re-load, so put the correct name there (you can check with the output from :AirlineTheme).

Thanks, Chris, I can confirm that setting the AirlineTheme (papercolor, in my case) in my tex.vim fixes the problem as well.

But there is something interesting I noticed only now: Airline is in fact not the only thing that is affected by setting the colorscheme this way, linting colors are off after opening the second file as well! My workaround with the conditional to avoid the redundant colorscheme change fixes that issue as well, so I will stick to that instead of calling AirlineTheme.

This observation makes me believe the problem is something deeper than an airline bug.
Seeing the same problem with Vim and Neovim makes me wonder if either it is causes by some other plugin I am using, or if this way of setting colorschemes is fundamantally not supported.

I find it a rather natural thing to do, though. Overall, I suggest to close the issue here and hope the workarounds might help others experiencing the same behavior.

I find it a rather natural thing to do, though. Overall, I suggest to close the issue here and hope the workarounds might help others experiencing the same behavior.

@chrisbra
Could I close this issue?

please keep open, I'd like to test if we can improve this at least a bit.

OK, I got it.

linting colors are off after opening the second file as well!

What does _off_ mean? Different than what you expected? Or Disabled?

If it's the former, then you can try this -


" Workaround for correctly switching colorschemes   " {{{1
" https://github.com/altercation/solarized/issues/102#issuecomment-275269574
" https://github.com/altercation/solarized/issues/102#issuecomment-352329521
" -------------------------------------------------------------------------------------------------------------------------
if !exists('s:known_links')
    let s:known_links = {}
endif

function! s:Find_links() " {{{2
    " Find and remember links between highlighting groups.
    redir => listing
    try
        silent highlight
    finally
        redir END
    endtry
    for line in split(listing, "\n")
        let tokens = split(line)
        " We're looking for lines like "String xxx links to Constant" in the
        " output of the :highlight command.
        if len(tokens) ==# 5 && tokens[1] ==# 'xxx' && tokens[2] ==# 'links' && tokens[3] ==# 'to'
            let fromgroup = tokens[0]
            let togroup = tokens[4]
            let s:known_links[fromgroup] = togroup
        endif
    endfor
endfunction

function! s:Restore_links() " {{{2
    " Restore broken links between highlighting groups.
    redir => listing
    try
        silent highlight
    finally
        redir END
    endtry
    let num_restored = 0
    for line in split(listing, "\n")
        let tokens = split(line)
        " We're looking for lines like "String xxx cleared" in the
        " output of the :highlight command.
        if len(tokens) ==# 3 && tokens[1] ==# 'xxx' && tokens[2] ==# 'cleared'
            let fromgroup = tokens[0]
            let togroup = get(s:known_links, fromgroup, '')
            if !empty(togroup)
                execute 'hi link' fromgroup togroup
                let num_restored += 1
            endif
        endif
    endfor
endfunction " }}}

function! s:AccurateColorscheme(colo_name)
    call <SID>Find_links()
    exec 'colorscheme ' a:colo_name
    call <SID>Restore_links()
endfunction

command! -nargs=1 -complete=color MyColorscheme call <SID>AccurateColorscheme(<q-args>)
" --------------------------------------------------------------------------------------------------------------    " }}}

Then, instead of colorscheme github do MyColorscheme github
Please report if it fixes the problem

Hi, thanks for following up on this!

By the linting colors being "off" I meant "enabled but wrong". Indeed I realize the way I wrote it wasn't clear.

I have meanwhile been using a semi-satisfactor solution:

"Source: https://vi.stackexchange.com/questions/4488/how-do-i-define...              
"-an-autocommand-to-work-on-all-files-excepted-a-certain-filetype                    

let ftToIgnore = ['tex', 'latex', 'mail']                                            

let colorscheme_set = 0                                                              

autocmd FileType *                                                                   
  \ if (index(ftToIgnore, &ft) < 0 && colorscheme_set == 0)                          
  \   | colorscheme one                                                              
  \   | hi Normal ctermbg=231                                                        
  \   | AirlineTheme papercolor                                                      
  \   | let colorscheme_set = 1

This is working together with ftplugin, the idea is that for those filetypes where the colorschemes are set individually, the global setting is not called.

Is your solution designed to fix both issues I had, both lining and the airline probelm that led to my post here originally?

Is your solution designed to fix both issues I had, both lining and the airline probelm

No, it was meant for the linting highlighting problem only.

Actually, IIRC, the highlighting problem is a bug in vim itself. It always occurs when colorscheme is changed, only the effects of it is sometimes more pronounced, so the user notices, and sometimes less pronounced, so the user doesn't notice. It had something to do with highlight links not being cleared properly.
More details can be obtained if you go to the links mentioned at the top of the code block in my above comment.


To fix the airline problem, simply add this command to the __end__ of tex.vim

AirlineRefresh

@sekoenig Did my abovementioned suggestion work?

I can confirm the AirlineRefresh fixes the problem I reported here originally.

As for the linting, I do not seem to have any right now, with or without the fix. This may have been broken by either a system update (I am running Debian testing), or by some vim plugin update. Unfortunately, I do note have time to investigate this right now, but I will follow up on this as soon as I can.

@subnut My linting is working again, and I am happy to confirm now that your solution above fixes the highlighting problem. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dea1993 picture Dea1993  路  22Comments

brotatos picture brotatos  路  21Comments

zdm picture zdm  路  46Comments

tnguyen14 picture tnguyen14  路  24Comments

a14m picture a14m  路  44Comments