tpope / vim-unimpaired

unimpaired.vim: Pairs of handy bracket mappings
https://www.vim.org/scripts/script.php?script_id=1590
3.31k stars 205 forks source link

Protect against color schemes that don't support changing the background #235

Open micampe opened 11 months ago

micampe commented 11 months ago

Some color schemes don't support toggling light/dark background and trying to do it results in a mangled color scheme.

I fixed this by overriding the yob map, but I was wondering if there is interest to merge this here.

This is my function (I'm not good at vimscript so let me know any feedback):

function! s:ToggleBackground() abort
  let oldcolors = g:colors_name

  if &background == 'light'
    set background=dark
  else
    set background=light
  endif

  if !exists("g:colors_name") || oldcolors != g:colors_name
    execute 'colorscheme ' . oldcolors
    redraw | echo 'Color scheme ' . oldcolors . ' does not support changing the background'
  endif
endfunction

if this is interesting for the project I can create a pull request (or just take the function from here, I don't mind).

To integrate properly it would also have to support the enable/disable commands too. I don't use those so I didn't do it here but I can add it.