daily pastebin goal
2%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. " This script tests a color scheme for some errors. Load the scheme and source
  2. " this script. e.g. :e colors/desert.vim | :so test_colors.vim
  3. " Will output possible errors.
  4.  
  5. func! Test_check_colors()
  6.   call cursor(1,1)
  7.   let err={}
  8.  
  9.   " 1) Check g:colors_name is existing
  10.   if !search('\<\%(g:\)\?colors_name\>', 'cnW')
  11.     let err['colors_name'] = 'g:colors_name not set'
  12.   else
  13.     let err['colors_name'] = 'OK'
  14.   endif
  15.  
  16.   " 2) Check for some well-defined highlighting groups
  17.   " Some items, check several groups, e.g. Diff, Spell
  18.   let hi_groups = ['ColorColumn', 'Diff', 'ErrorMsg', 'Folded',
  19.         \ 'FoldColumn', 'IncSearch', 'LineNr', 'ModeMsg', 'MoreMsg', 'NonText',
  20.         \ 'Normal', 'Pmenu', 'Todo', 'Search', 'Spell', 'StatusLine', 'TabLine',
  21.         \ 'Title', 'Visual', 'WarningMsg', 'WildMenu']
  22.   let groups={}
  23.   for group in hi_groups
  24.     if search('\c@suppress\s\+'.group, 'cnW')
  25.       " skip check, if the script contains a line like
  26.       " @suppress Visual:
  27.       let groups[group] = 'Ignoring '.group
  28.       continue
  29.     endif
  30.     if !search('hi\%[ghlight] \+'.group, 'cnW')
  31.       let groups[group] = 'No highlight definition for '.group
  32.       continue
  33.     endif
  34.     if !search('hi\%[ghlight] \+'.group. '.*fg=', 'cnW')
  35.       let groups[group] = 'Missing foreground color for '.group
  36.       continue
  37.     endif
  38.     if search('hi\%[ghlight] \+'.group. '.*guibg=', 'cnW') &&
  39.         \ !search('hi\%[ghlight] \+'.group. '.*ctermbg=', 'cnW')
  40.       let groups[group] = 'Missing bg terminal color for '.group
  41.       continue
  42.     endif
  43.     call search('hi\%[ghlight] \+'.group, 'cW')
  44.     " only check in the current line
  45.     if !search('guifg', 'cnW', line('.'))   || !search('ctermfg', 'cnW', line('.'))
  46.       " do not check for background colors, they could be intentionally left out
  47.       let groups[group] = 'Missing fg definition for '.group
  48.     endif
  49.     call cursor(1,1)
  50.   endfor
  51.   let err['highlight'] = groups
  52.  
  53.   " 3) Check, that it does not set background highlighting
  54.   " Doesn't ':hi Normal ctermfg=253 ctermfg=233' also set the background sometimes?
  55.   let bg_set='\(set\?\|setl\(ocal\)\?\) .*\(background\|bg\)=\(dark\|light\)'
  56.   let bg_let='let \%([&]\%([lg]:\)\?\)\%(background\|bg\)\s*=\s*\([''"]\?\)\w\+\1'
  57.   let bg_pat='\%('.bg_set. '\|'.bg_let.'\)'
  58.   let line=search(bg_pat, 'cnW')
  59.   if search(bg_pat, 'cnW')
  60.     exe line
  61.     if search('hi \U\w\+\s\+\S', 'cbnW')
  62.       let err['background'] = 'Should not set background option after :hi statement'
  63.     endif
  64.   else
  65.     let err['background'] = 'OK'
  66.   endif
  67.   call cursor(1,1)
  68.  
  69.   " 4) Check, that t_Co is checked
  70.   let pat = '[&]t_Co\s*[<>=]=\?\s*\d\+'
  71.   if !search(pat, 'ncW')
  72.     let err['t_Co'] = 'Does not check terminal for capable colors'
  73.   endif
  74.  
  75.   " 5) Initializes correctly, e.g. should have a section like
  76.   " hi clear
  77.   " if exists("syntax_on")
  78.   " syntax reset
  79.   " endif
  80.   let pat='hi\%[ghlight]\s*clear\n\s*if\s*exists(\([''"]\)syntax_on\1)\n\s*syn\%[tax]\s*reset\n\s*endif'
  81.   if !search(pat, 'cnW')
  82.     let err['init'] = 'No initialization'
  83.   endif
  84.  
  85.   " 6) Does not use :syn on
  86.   if search('syn\%[tax]\s\+on', 'cnW')
  87.     let err['background'] = 'Should not issue :syn on'
  88.   endif
  89.  
  90.   " 7) Does not define filetype specfic groups like vimCommand, htmlTag,
  91.   let hi_groups = ['vim', 'html', 'python', 'sh', 'ruby']
  92.   for group in hi_groups
  93.     let pat='\Chi\%[ghlight]\s*\zs'.group.'\w\+\>'
  94.     if search(pat, 'cnW')
  95.       let line = search(pat, 'cW')
  96.       let err['filetype'] = get(err, 'filetype', 'Should not define: ') . matchstr(getline('.'), pat). ' '
  97.     endif
  98.     call cursor(1,1)
  99.   endfor
  100.   let g:err = err
  101.  
  102.   " print Result
  103.   call Result(err)
  104. endfu
  105.  
  106. fu! Result(err)
  107.   let do_roups = 0
  108.   echohl Title|echomsg "---------------"|echohl Normal
  109.   for key in sort(keys(a:err))
  110.     if key is# 'highlight'
  111.       let do_groups = 1
  112.       continue
  113.     else
  114.       if a:err[key] !~ 'OK'
  115.         echohl Title
  116.       endif
  117.       echomsg printf("%15s: %s", key, a:err[key])
  118.       echohl Normal
  119.     endif
  120.   endfor
  121.   echohl Title|echomsg "---------------"|echohl Normal
  122.   if do_groups
  123.     echohl Title | echomsg "Groups" | echohl Normal
  124.     for v1 in sort(keys(a:err['highlight']))
  125.       echomsg printf("%25s: %s", v1, a:err['highlight'][v1])
  126.     endfor
  127.   endif
  128. endfu
  129.  
  130. call Test_check_colors()
RAW Paste Data
Top