Skip to content

Commit 200d0e3

Browse files
committed
patch 8.0.1395: it is not easy to see if a colorscheme is well written
Problem: It is not easy to see if a colorscheme is well written. Solution: Add a script that checks for common mistakes. (Christian Brabandt)
1 parent 7e1652c commit 200d0e3

3 files changed

Lines changed: 139 additions & 0 deletions

File tree

runtime/colors/README.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Search for "highlight_init".
6464
If you think you have a color scheme that is good enough to be used by others,
6565
please check the following items:
6666

67+
- Source the check_colors.vim script to check for common mistakes.
6768
- Does it work in a color terminal as well as in the GUI?
6869
- Is "g:colors_name" set to a meaningful value? In case of doubt you can do
6970
it this way:

runtime/colors/check_colors.vim

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

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ static char *(features[]) =
771771

772772
static int included_patches[] =
773773
{ /* Add new patch number below this line */
774+
/**/
775+
1395,
774776
/**/
775777
1394,
776778
/**/

0 commit comments

Comments
 (0)