check_colors.vim (6653B)
1 vim9script 2 # This script tests a color scheme for some errors and lists potential errors. 3 # Load the scheme and source this script, like this: 4 # :edit colors/desert.vim | :ru colors/tools/check_colors.vim 5 6 def Test_check_colors() 7 const savedview = winsaveview() 8 cursor(1, 1) 9 10 # err is 11 # { 12 # colors_name: "message", 13 # init: "message", 14 # background: "message", 15 # ....etc 16 # highlight: { 17 # 'Normal': "Missing ...", 18 # 'Conceal': "Missing ..." 19 # ....etc 20 # } 21 # } 22 var err: dict<any> = {} 23 24 # 1) Check g:colors_name is existing 25 if search('\<\%(g:\)\?colors_name\>', 'cnW') == 0 26 err['colors_name'] = 'g:colors_name not set' 27 else 28 err['colors_name'] = 'OK' 29 endif 30 31 # 2) Check for some well-defined highlighting groups 32 const hi_groups = [ 33 'ColorColumn', 34 'Comment', 35 'Conceal', 36 'Constant', 37 'CurSearch', 38 'Cursor', 39 'CursorColumn', 40 'CursorLine', 41 'CursorLineNr', 42 'CursorLineFold', 43 'CursorLineSign', 44 'DiffAdd', 45 'DiffChange', 46 'DiffDelete', 47 'DiffText', 48 'Directory', 49 'EndOfBuffer', 50 'Error', 51 'ErrorMsg', 52 'FoldColumn', 53 'Folded', 54 'Identifier', 55 'Ignore', 56 'IncSearch', 57 'LineNr', 58 'LineNrAbove', 59 'LineNrBelow', 60 'MatchParen', 61 'ModeMsg', 62 'MoreMsg', 63 'NonText', 64 'Normal', 65 'Pmenu', 66 'PmenuSbar', 67 'PmenuSel', 68 'PmenuThumb', 69 'PopupNotification', 70 'PreProc', 71 'Question', 72 'QuickFixLine', 73 'Search', 74 'SignColumn', 75 'Special', 76 'SpecialKey', 77 'SpellBad', 78 'SpellCap', 79 'SpellLocal', 80 'SpellRare', 81 'Statement', 82 'StatusLine', 83 'StatusLineNC', 84 'StatusLineTerm', 85 'StatusLineTermNC', 86 'TabLine', 87 'TabLineFill', 88 'TabLineSel', 89 'Title', 90 'Todo', 91 'ToolbarButton', 92 'ToolbarLine', 93 'Type', 94 'Underlined', 95 'VertSplit', 96 'Visual', 97 'VisualNOS', 98 'WarningMsg', 99 'WildMenu', 100 'debugPC', 101 'debugBreakpoint', 102 ] 103 var groups = {} 104 for group in hi_groups 105 if search('\c@suppress\s\+\<' .. group .. '\>', 'cnW') != 0 106 # skip check, if the script contains a line like 107 # @suppress Visual: 108 continue 109 endif 110 if search('hi\%[ghlight]!\= \+link \+' .. group, 'cnW') != 0 # Linked group 111 continue 112 endif 113 if search('hi\%[ghlight] \+\<' .. group .. '\>', 'cnW') == 0 114 groups[group] = 'No highlight definition for ' .. group 115 continue 116 endif 117 if search('hi\%[ghlight] \+\<' .. group .. '\>.*[bf]g=', 'cnW') == 0 118 groups[group] = 'Missing foreground or background color for ' .. group 119 continue 120 endif 121 if search('hi\%[ghlight] \+\<' .. group .. '\>.*guibg=', 'cnW') != 0 122 && search('hi\%[ghlight] \+\<' .. group .. '\>.*ctermbg=', 'cnW') == 0 123 && group != 'Cursor' 124 groups[group] = 'Missing bg terminal color for ' .. group 125 continue 126 endif 127 if search('hi\%[ghlight] \+\<' .. group .. '\>.*guifg=', 'cnW') == 0 128 && group !~ '^Diff' 129 groups[group] = 'Missing guifg definition for ' .. group 130 continue 131 endif 132 if search('hi\%[ghlight] \+\<' .. group .. '\>.*ctermfg=', 'cnW') == 0 133 && group !~ '^Diff' 134 && group != 'Cursor' 135 groups[group] = 'Missing ctermfg definition for ' .. group 136 continue 137 endif 138 # do not check for background colors, they could be intentionally left out 139 cursor(1, 1) 140 endfor 141 err['highlight'] = groups 142 143 # 3) Check, that it does not set background highlighting 144 # Doesn't ':hi Normal ctermfg=253 ctermfg=233' also set the background sometimes? 145 const bg_set = '\(set\?\|setl\(ocal\)\?\) .*\(background\|bg\)=\(dark\|light\)' 146 const bg_let = 'let \%([&]\%([lg]:\)\?\)\%(background\|bg\)\s*=\s*\([''"]\?\)\w\+\1' 147 const bg_pat = '\%(' .. bg_set .. '\|' .. bg_let .. '\)' 148 const line = search(bg_pat, 'cnW') 149 if search(bg_pat, 'cnW') != 0 150 exe ":" .. line 151 if search('hi \U\w\+\s\+\S', 'cbnW') != 0 152 err['background'] = 'Should not set background option after :hi statement' 153 endif 154 else 155 err['background'] = 'OK' 156 endif 157 cursor(1, 1) 158 159 # 4) Check, that t_Co is checked 160 var pat = '[&]t_Co)\?\s*\%(\%([<>=]=\?\)\|??\)\s*\d\+' 161 if search(pat, 'ncW') == 0 162 err['t_Co'] = 'Does not check terminal for capable colors' 163 endif 164 165 # 5) Initializes correctly, e.g. should have at least: 166 # hi clear 167 pat = '^\s*hi\%[ghlight]\s*clear\s*$' 168 if search(pat, 'cnW') == 0 169 err['init'] = 'No initialization' 170 endif 171 172 # 6) Does not use :syn on 173 if search('syn\%[tax]\s\+on', 'cnW') != 0 174 err['background'] = 'Should not issue :syn on' 175 endif 176 177 # 7) Normal should be defined first, not use reverse, fg or bg 178 cursor(1, 1) 179 pat = 'hi\%[ghlight] \+\%(link\|clear\)\@!\w\+\>' 180 search(pat, 'cW') # Look for the first hi def, skipping `hi link` and `hi clear` 181 if getline('.') !~# '\m\<Normal\>' 182 err['highlight']['Normal'] = 'Should be defined first' 183 elseif getline('.') =~# '\m\%(=\%(fg\|bg\)\)' 184 err['highlight']['Normal'] = "Should not use 'fg' or 'bg'" 185 elseif getline('.') =~# '\m=\%(inv\|rev\)erse' 186 err['highlight']['Normal'] = 'Should not use reverse mode' 187 endif 188 189 # 8) TODO: XXX: Check if g:terminal_ansi_colors are defined 190 191 winrestview(savedview) 192 g:err = err 193 194 Result(err) 195 enddef 196 197 198 def Result(err: dict<any>) 199 var do_groups: bool = v:false 200 echohl Title | echomsg "---------------" | echohl Normal 201 for key in sort(keys(err)) 202 if key == 'highlight' 203 do_groups = !empty(err[key]) 204 continue 205 else 206 if err[key] !~ 'OK' 207 echohl Title 208 endif 209 echomsg printf("%15s: %s", key, err[key]) 210 echohl Normal 211 endif 212 endfor 213 echohl Title | echomsg "---------------" | echohl Normal 214 if do_groups 215 echohl Title | echomsg "Groups" | echohl Normal 216 for v1 in sort(keys(err['highlight'])) 217 echomsg printf("%25s: %s", v1, err['highlight'][v1]) 218 endfor 219 endif 220 enddef 221 222 Test_check_colors()