Skip to content

Commit

Permalink
vim: switch colorscheme without syntax issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sim590 committed Oct 11, 2017
1 parent f25b03e commit c234959
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
65 changes: 65 additions & 0 deletions vim/functions.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,68 @@ fun! s:StringBaseConvert(str, a, b)
"TODO: parse '0x' prefix string.
return system('echo "obase='.a:b.'; $(('.a:a.'#'.a:str.'))" | bc')
endf

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" switch vim colorscheme without breaking colors "
" see: https://github.com/altercation/solarized/issues/102 "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

if !exists('s:known_links')
let s:known_links = {}
endif

function! s:Find_links() " {{{1
" 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() " {{{1
" 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()
highlight ExtraWhiteSpaces ctermbg=red guibg=red
autocmd ColorScheme highlight ExtraWhiteSpaces ctermbg=red guibg=red
match ExtraWhiteSpaces /\s\+$\| \+\ze\t/
endfunction

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

17 changes: 12 additions & 5 deletions vimrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
" PLUGIN CONFIGURATION =============================
call plug#begin('~/.vim/plugged')
Plug 'L9'
Plug 'Colour-Sampler-Pack'
" Colors ============================
Plug 'michalbachowski/vim-wombat256mod'
Plug 'crusoexia/vim-monokai'
Plug 'preocanin/greenwint'
Plug 'prognostic/plasticine'
"====================================
Plug 'terryma/vim-multiple-cursors'
Plug 'itchyny/lightline.vim'
Plug 'MarcWeber/vim-addon-mw-utils'
Expand Down Expand Up @@ -51,6 +56,7 @@ Plug 'Raimondi/delimitMate'
Plug 'glts/vim-radical'
Plug 'google/vim-maktaba'
Plug 'glts/vim-magnum'
" ------------------------------
Plug 'vim-auto-save', 'noautocmd'
Plug 'vim-pandoc/vim-pandoc'
Plug 'vim-pandoc/vim-pandoc-syntax'
Expand Down Expand Up @@ -160,10 +166,6 @@ au BufRead,BufNewFile,FileType python,c,cpp,java,cs,html,css,php
" others
au BufRead,BufNewFile ~/.mutt/* set filetype=muttrc

" COLORSCHEME ==========
colorscheme wombat256mod
" ======================

" handle unwanted characters in file ====================================

" replace false spaces by spaces
Expand Down Expand Up @@ -213,3 +215,8 @@ source ~/.vim/abbreviations.vim
source ~/.vim/functions.vim
source ~/.vim/mappings.vim
" ===============================

" COLORSCHEME ==========
MyColorscheme wombat256mod
" ======================

0 comments on commit c234959

Please sign in to comment.