-
Notifications
You must be signed in to change notification settings - Fork 26
/
colortemplate.vim
2371 lines (2127 loc) · 69.7 KB
/
colortemplate.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" vim: foldmethod=marker nowrap
let s:VERSION = '2.0.0b1'
" Informal grammar {{{
" <Template> ::= <Line>*
" <Line> ::= <EmptyLine> | <Comment> | <KeyValuePair> | <HiGroupDef> |
" <VerbatimText> | <Command> | <AuxFile> | <Documentation>
" <Command> ::= #if <Anything> | #elseif <Anything> | #else | #endif
" <VerbatimText> ::= verbatim <Anything> endverbatim
" <AuxFile> ::= auxfile <Path> <Anything> endauxfile
" <Path> ::= .+
" <Documentation> ::= documentation <Anything> enddocumentation
" <Anything> ::= .*
" <Comment> ::= # .*
" <KeyValuePair> ::= <ColorDef> | <Key> : <Value>
" <Key> ::= Full name | Short name | Author | Background | ...
" <Value> ::= .*
" <ColorDef> ::= Color : <ColorName> <GUIValue> <Base256Value> [ <Base16Value> ]
" <ColorName> ::= [a-z1-9_]+
" <GUIValue> ::= <HexValue> | <RGBValue> | <RGBColorName>
" <HexValue> ::= #[a-f0-9]{6}
" <RGBValue> ::= rgb ( <8BitNumber> , <8BitNumber> , <8BitNumber> )
" <RGBColorName> ::= See $VIMRUNTIME/rgb.txt
" <Base256Value> ::= ~ | <8BitNumber>
" <8BitNumber> ::= 0 | 1 | ... | 255
" <Base16Value> ::= 0 | 1 | ... | 15 | Black | DarkRed | ...
" <HiGroupDef> ::= <LinkedGroup> | <BaseGroup>
" <LinkedGroup> ::= <HiGroupName> -> <HiGroupName>
" <BaseGroup> ::= <HiGroupName> <FgColor> <BgColor> <Attributes>
" <FgColor> ::= <ColorName>
" <BgColor> ::= <ColorName>
" <Attributes> ::= <StyleDef>*
" <StyleDef> ::= t[term] = <AttrList> | g[ui] = <AttrList> | guisp = <ColorName> | s = <ColorName> | <AttrList>
" <AttrList> ::= <Attr> [, <AttrList]
" <Attr> ::= bold | italic | reverse | inverse | underline | ...
" }}}
" Generic helper functions {{{
" Working directory {{{
fun! s:setwd(path)
if !isdirectory(a:path)
throw 'FATAL: Path is not a directory: ' . a:path
endif
let s:wd = fnamemodify(a:path, ":p")
execute 'lcd' s:wd
endf
fun! s:getwd()
return s:wd
endf
" }}}
" Stack functions {{{
fun! s:push(S, e)
return add(a:S, a:e)
endf
fun! s:pop(S)
return remove(a:S, -1)
endf
fun! s:top(S)
return a:S[-1]
endf
" }}}
" File and Path manipulation {{{
fun! s:slash() abort " Code borrowed from Pathogen (thanks T. Pope!)
return !exists("+shellslash") || &shellslash ? '/' : '\'
endf
fun! s:is_absolute(path) abort " Code borrowed from Pathogen (thanks T. Pope)
return a:path =~# (has('win32') ? '^\%([\\/]\|\w:\)[\\/]\|^[~$]' : '^[/~$]')
endf
fun! s:match_path(path, regexp)
if exists('+shellslash') && !&shellslash
return match(tr(a:path, '\', '/'), tr(a:regexp, '\', '/')) > -1
else
return match(a:path, a:regexp) > -1
endif
endf
" Returns path as an absolute path, after verifying that path is valid,
" i.e., it is inside the directory specified in env.
"
" path: a String specifying a relative or absolute path
" env: a Dictionary with a 'dir' key specifying a valid directory for path.
fun! s:full_path(path, env)
if s:is_absolute(a:path)
let l:path = simplify(fnamemodify(a:path, ":p"))
else
let l:path = simplify(fnamemodify(a:env['dir'] . s:slash() . a:path, ":p"))
endif
let l:dir = simplify(fnamemodify(a:env['dir'], ":p"))
if !isdirectory(l:dir)
throw 'FATAL: Path is not a directory: ' . l:dir
endif
if !s:match_path(l:path, '^' . l:dir)
throw 'Path ' . l:path . ' outside valid directory: ' . l:dir
endif
return l:path
endf
fun! s:make_dir(dirpath)
let l:dirpath = fnamemodify(a:dirpath, ":p")
if isdirectory(l:dirpath)
return
endif
try
call mkdir(fnameescape(l:dirpath), "p")
catch /.*/
throw 'Could not create directory: ' . l:dirpath
endtry
endf
" }}}
" Buffer and file manipulation {{{
if has('patch-8.0.1039')
fun! s:getbufline(bufnr, start, stop)
return getbufline(a:bufnr, a:start, a:stop)
endf
fun! s:setbufline(bufnr, linenr, line)
call setbufline(a:bufnr, a:linenr, a:line)
endf
fun! s:appendbufline(bufnr, linenr, line)
call appendbufline(a:bufnr, a:linenr, a:line)
endf
fun! s:new_work_buffer()
botright 1new +setlocal\ ft=vim\ et\ ts=2\ sw=2\ norl\ nowrap
let l:bufnr = bufnr("%")
wincmd p
execute winnr('#') 'wincmd c'
return l:bufnr
endf
fun! s:reindent_buffer(bufnr)
execute '1split +'.a:bufnr.'buffer'
normal gg=G
wincmd c
endf
else
fun! s:getbufline(bufnr, start, stop)
return getline(a:start, a:stop)
endf
fun! s:setbufline(bufnr, linenr, line)
call setline(a:linenr, a:line)
endf
fun! s:appendbufline(bufnr, linenr, line)
call append(a:linenr, a:line)
endf
fun! s:new_work_buffer()
botright 1new +setlocal\ ft=vim\ et\ ts=2\ sw=2\ norl\ nowrap
return bufnr("%")
endf
fun! s:reindent_buffer(bufnr)
silent execute a:bufnr "bufdo norm gg=G"
endf
endif
fun! s:destroy_buffer(bufnr)
execute a:bufnr 'bwipe!'
endf
" Write the specified buffer into path. The path must be inside env['dir'].
fun! s:write_buffer(bufnr, path, env, overwrite)
let l:path = s:full_path(a:path, a:env)
call s:make_dir(fnamemodify(l:path, ":h"))
if bufloaded(l:path)
if a:overwrite
execute "bdelete" bufname(a:path)
else
throw "Buffer " . l:path . " exists. Use ! to overwrite."
endif
endif
if a:overwrite || !filereadable(l:path)
if writefile(s:getbufline(a:bufnr, 1, "$"), l:path) < 0
throw 'Could not write ' . l:path . ': ' . v:exception
endif
else
throw 'File exists: ' . l:path . '. Use ! to overwrite it.'
endif
endf
" Ditto, but writes a List
fun! s:write_list(lines, path, env, overwrite)
let l:path = s:full_path(a:path, a:env)
call s:make_dir(fnamemodify(l:path, ":h"))
if a:overwrite || !filereadable(l:path)
if writefile(a:lines, l:path) < 0
throw 'Could not write ' . l:path . ': ' . v:exception
endif
else
throw 'File exists: ' . l:path . '. Use ! to overwrite it.'
endif
endf
" }}}
" Errors and warnings {{{
fun! s:add_error(path, line, col, msg)
call setqflist([{'filename': a:path, 'lnum' : a:line, 'col': a:col, 'text' : a:msg, 'type' : 'E'}], 'a')
endf
fun! s:add_warning(path, line, col, msg)
if get(g:, 'colortemplate_warnings', 1)
call setqflist([{'filename': a:path, 'lnum' : a:line, 'col': a:col, 'text' : a:msg, 'type' : 'W'}], 'a')
endif
endf
fun! s:add_generic_error(msg)
call s:add_error(bufname('%'), 1, 1, a:msg)
endf
fun! s:add_generic_warning(msg)
call s:add_warning(bufname('%'), 1, 1, a:msg)
endf
fun! s:print_error_msg(msg, rethrow)
call s:clearscreen()
if a:rethrow
unsilent echoerr '[Colortemplate]' a:msg
else
echohl Error
unsilent echomsg '[Colortemplate]' a:msg
echohl None
endif
endf
fun! s:print_notice(msg)
call s:clearscreen()
unsilent echomsg '[Colortemplate]' a:msg
endf
fun! s:clearscreen()
redraw
echo "\r"
endf
fun! s:show_errors(errmsg)
botright cwindow
if getbufvar('', '&ft', '') ==# 'qf'
wincmd p
endif
if !empty(filter(getqflist(), { i,v -> v['type'] !=# 'W' }))
throw a:errmsg
endif
endf
" }}}
" Misc {{{
if has('nvim')
fun! s:isnan(x)
return printf("%f", a:x) ==# 'nan'
endf
else
fun! s:isnan(x)
return isnan(a:x)
endf
endif
" }}}
" }}} Helper functions
" Data structures {{{
" Data and functions common to the parser and the generator
" Source code {{{
fun! s:init_source_code()
let s:source = [] " Keep the source lines here
endf
fun! s:destroy_source_code()
unlet! s:source
endf
fun! s:add_source_line(line)
call add(s:source, a:line)
endf
fun! s:source_lines()
return s:source
endf
" }}}
" Color palette {{{
fun! s:init_color_palette()
let s:guicol = { 'dark': {'fg':'fg', 'bg':'bg', 'none': 'NONE'},
\ 'light': {'fg':'fg', 'bg':'bg', 'none': 'NONE'},
\ 'preamble': {'fg':'fg', 'bg':'bg', 'none': 'NONE'},
\ }
let s:col256 = { 'dark': {'fg':'fg', 'bg':'bg', 'none': 'NONE'},
\ 'light': {'fg':'fg', 'bg':'bg', 'none': 'NONE'},
\ 'preamble': {'fg':'fg', 'bg':'bg', 'none': 'NONE'},
\ }
let s:col16 = { 'dark': {'fg':'fg', 'bg':'bg', 'none': 'NONE'},
\ 'light': {'fg':'fg', 'bg':'bg', 'none': 'NONE'},
\ 'preamble': {'fg':'fg', 'bg':'bg', 'none': 'NONE'},
\ }
let s:term_colors = { 'dark': [], 'light': [], 'preamble': [] } " 16 ASCII colors
endf
fun! s:destroy_color_palette()
unlet! g:guicol s:col256 s:col16 s:term_colors
endf
" section: 'preamble, 'dark' or 'light'
" name: color name as defined by the user
" gui: GUI value (either a hex value or a standard name)
" base256: a numeric value between 16 and 255 or -1 (=infer the value)
" base16: a numeric value between 0 and 15
fun! s:add_color(section, name, gui, base256, base16)
" If the GUI color is given by name, quote it if the name contains spaces
let l:gui = match(a:gui, '\s') > - 1 ? "'".a:gui."'" : a:gui
let s:guicol[a:section][a:name] = l:gui
let s:col256[a:section][a:name] = a:base256
let s:col16[a:section][a:name] = a:base16
if a:section ==# 'preamble'
let s:guicol['dark'][a:name] = l:gui
let s:col256['dark'][a:name] = a:base256
let s:col16['dark'][a:name] = a:base16
let s:guicol['light'][a:name] = l:gui
let s:col256['light'][a:name] = a:base256
let s:col16['light'][a:name] = a:base16
endif
endf
fun! s:add_term_ansi_color(name, section)
call add(s:term_colors[a:section], a:name)
if a:section ==# 'preamble'
call add(s:term_colors['dark'], a:name)
call add(s:term_colors['light'], a:name)
endif
endf
fun! s:col16(name, section)
return s:col16[a:section][a:name]
endf
fun! s:col256(name, section)
if s:col256[a:section][a:name] == -1 " Infer the value from GUI color
let s:col256[a:section][a:name] =
\ colortemplate#colorspace#approx(s:guicol[a:section][a:name])['index']
endif
return s:col256[a:section][a:name]
endf
fun! s:termcol(name, section, t_Co)
return a:t_Co <= 16 ? s:col16(a:name, a:section) : s:col256(a:name, a:section)
endf
fun! s:guicol(name, section)
return s:guicol[a:section][a:name]
endf
fun! s:is_color_defined(name, section)
return has_key(s:guicol[a:section], a:name)
endf
fun! s:term_colors(section)
return s:term_colors[a:section]
endf
fun! s:color_names(section)
return filter(copy(keys(s:guicol[a:section])), { _,v -> v !=# 'fg' && v !=# 'bg' && v !=# 'none' })
endf
" }}}
" Highlight groups {{{
fun! s:init_highlight_groups()
endf
fun! s:destroy_highlight_groups()
endf
fun! s:new_hi_group(name)
return {
\ 'name': a:name,
\ 'fg': '',
\ 'bg': '',
\ 'sp': 'none',
\ 'term': [],
\ 'gui': [],
\ 'start': '',
\ 'stop': '',
\}
endf
fun! s:hi_name(hg)
return a:hg['name']
endf
fun! s:fg(hg)
return a:hg['fg']
endf
fun! s:bg(hg)
return a:hg['bg']
endf
fun! s:sp(hg)
return a:hg['sp']
endf
fun! s:term_attr_list(hg)
return a:hg['term']
endf
fun! s:gui_attr_list(hg)
return a:hg['gui']
endf
fun! s:fg16(hg, section)
return s:col16(a:hg['fg'], a:section)
endf
fun! s:bg16(hg, section)
return s:col16(a:hg['bg'], a:section)
endf
fun! s:fg256(hg, section)
return s:col256(a:hg['fg'], a:section)
endf
fun! s:bg256(hg, section)
return s:col256(a:hg['bg'], a:section)
endf
fun! s:guifg(hg, section)
return s:guicol(a:hg['fg'], a:section)
endf
fun! s:guibg(hg, section)
return s:guicol(a:hg['bg'], a:section)
endf
fun! s:guisp(hg, section)
return s:guicol(a:hg['sp'], a:section)
endf
fun! s:term_attr(hg)
return empty(a:hg['term']) ? 'NONE' : join(a:hg['term'], ',')
endf
fun! s:gui_attr(hg)
return empty(a:hg['gui']) ? 'NONE' : join(a:hg['gui'], ',')
endf
fun! s:term_attr_no_italics(hg)
let l:attr = filter(copy(a:hg['term']), { _,v -> v !=# 'italic' })
return empty(l:attr) ? 'NONE' : join(l:attr, ',')
endf
fun! s:gui_attr_no_italics(hg)
let l:attr = filter(copy(a:hg['gui']), { _,v -> v !=# 'italic' })
return empty(l:attr) ? 'NONE' : join(l:attr, ',')
endf
fun! s:set_fg(hg, colorname)
let a:hg['fg'] = a:colorname
endf
fun! s:set_bg(hg, colorname)
let a:hg['bg'] = a:colorname
endf
fun! s:set_sp(hg, colorname)
let a:hg['sp'] = a:colorname
endf
" type can be only 'start' or 'stop'
fun! s:set_terminal_code(hg, type, value)
let a:hg[a:type] = a:value
endf
fun! s:has_term_attr(hg)
return !empty(a:hg['term'])
endf
fun! s:has_term_italics(hg)
return index(a:hg['term'], 'italic') > -1
endf
fun! s:has_gui_attr(hg)
return !empty(a:hg['gui'])
endf
fun! s:has_gui_italics(hg)
return index(a:hg['gui'], 'italic') > -1
endf
fun! s:is_neovim_group(name)
return a:name =~? '^TermCursor\%[NC]$'
endf
fun! s:add_term_attr(hg, attrlist)
call extend(a:hg['term'], a:attrlist)
call uniq(sort(a:hg['term']))
endf
fun! s:add_gui_attr(hg, attrlist)
call extend(a:hg['gui'], a:attrlist)
call uniq(sort(a:hg['gui']))
endf
" }}}
" Colorscheme metadata {{{
fun! s:init_metadata()
let s:supports_dark = 0
let s:supports_light = 0
let s:uses_italics = 0
let s:supports_neovim = 0
let s:supported_variants = []
let s:info = {
\ 'fullname': '',
\ 'shortname': '',
\ 'fullauthor': '',
\ 'author': '',
\ 'maintainer': '',
\ 'website': '',
\ 'description': '',
\ 'license': 'Vim License (see `:help license`)',
\ 'optionprefix': ''
\ }
endf
fun! s:destroy_metadata()
unlet! s:supports_dark s:supports_light s:uses_italics
\ s:supports_neovim s:supported_variants s:info
endf
fun! s:supports_neovim()
return s:supports_neovim
endf
fun! s:set_supports_neovim()
let s:supports_neovim = 1
endf
fun! s:uses_italics()
return s:uses_italics
endf
fun! s:set_uses_italics()
let s:uses_italics = 1
endf
fun! s:has_dark_and_light()
return s:supports_dark && s:supports_light
endf
fun! s:has_dark()
return s:supports_dark
endf
fun! s:has_light()
return s:supports_light
endf
fun! s:set_has_dark()
let s:supports_dark = 1
endf
fun! s:set_has_light()
let s:supports_light = 1
endf
fun! s:supported_backgrounds()
return (s:has_dark() ? ['dark'] : []) + (s:has_light() ? ['light'] : [])
endf
fun! s:supported_variants()
return s:supported_variants
endf
fun! s:has_variant(v)
return index(s:supported_variants, a:v) > -1
endf
fun! s:add_variant(variant)
call add(s:supported_variants, a:variant)
call reverse(uniq(sort(s:supported_variants, 'N')))
endf
fun! s:get_info(key)
return s:info[a:key]
endf
fun! s:set_info(key, value)
if !has_key(s:info, a:key)
throw 'Unknown key: ' . a:key
endif
if type(a:value) != type('')
throw "FATAL: key value must be a String (set_info)" " Should never happen
endif
if a:key ==# 'author'
let s:info['fullauthor'] = a:value
let s:info['author'] = matchstr(a:value, '^[^< ]\+')
return
endif
let s:info[a:key] = a:value
if a:key ==# 'shortname'
if empty(a:value)
throw 'Missing value for short name key'
elseif len(a:value) > 24
throw 'The short name must be at most 24 characters long'
elseif a:value !~? '\m^\w\+$'
throw 'The short name may contain only letters, numbers and underscore'
endif
if empty(s:info['optionprefix'])
let s:info['optionprefix'] = s:info['shortname']
endif
elseif a:key ==# 'optionprefix'
if empty(a:value)
throw 'Missing value for option prefix key'
elseif len(a:value) > 24
throw 'The option prefix must be at most 24 characters long'
elseif a:value !~? '\m\w\+$'
throw 'The option prefix may contain only letters, numbers and underscore'
endif
endif
endf
fun! s:fullname()
return s:info['fullname']
endf
fun! s:shortname()
return s:info['shortname']
endf
fun! s:fullauthor()
return s:info['fullauthor']
endf
fun! s:author()
return s:info['author']
endf
fun! s:maintainer()
return s:info['maintainer']
endf
fun! s:set_default_maintainer()
let s:info['maintainer'] = s:info['fullauthor']
endf
fun! s:description()
return s:info['description']
endf
fun! s:website()
return s:info['website']
endf
fun! s:license()
return s:info['license']
endf
fun! s:optionprefix()
return s:info['optionprefix']
endf
" }}}
" Colorscheme definition {{{
let s:GUI = '65536' " GUI or termguicolors
fun! s:init_colorscheme_definition()
let s:data = { 'global': { 'preamble': [] } }
let s:italics = { 'global': {'preamble': [] } } " Global is never used for italics
let s:nvim = { 'global': { 'preamble': [] } }
let s:has_normal = { }
endf
fun! s:destroy_colorscheme_definition()
unlet! s:data s:italics s:nvim s:has_normal
endf
fun! s:add_colorscheme_variant(v)
call s:add_variant(a:v)
if !has_key(s:data, a:v)
let s:data[a:v] = { 'preamble': [], 'dark': [], 'light': [] }
let s:italics[a:v] = { 'preamble': [], 'dark': [], 'light': [] }
let s:nvim[a:v] = { 'preamble': [], 'dark': [], 'light': [] }
let s:has_normal[a:v] = { 'preamble': 0, 'dark': 0, 'light': 0 }
endif
endf
fun! s:has_normal(variant, section)
return s:has_normal[a:variant][a:section]
endf
fun! s:set_has_normal(variant, section)
let s:has_normal[a:variant][a:section] = 1
if a:section ==# 'preamble'
let s:has_normal[a:variant]['dark'] = 1
let s:has_normal[a:variant]['light'] = 1
endif
endf
fun! s:make_item(item, type)
return [a:type, a:item]
endf
fun! s:is_raw_type(item)
return a:item[0] ==# 'raw'
endf
fun! s:is_verb_type(item)
return a:item[0] ==# 'verb'
endf
fun! s:is_higroup_type(item)
return a:item[0] ==# 'group'
endf
fun! s:is_linked_type(item)
return a:item[0] ==# 'link'
endf
fun! s:is_italic_type(item)
return a:item[0] ==# 'it'
endf
fun! s:item_type(item)
return a:item[0]
endf
fun! s:item_value(item)
return a:item[1]
endf
fun! s:add_item(variant, section, item, type)
call add(s:data[a:variant][a:section], s:make_item(a:item, a:type))
endf
fun! s:add_raw_item(variant, section, item)
call s:add_item(a:variant, a:section, a:item, 'raw')
endf
fun! s:add_verbatim_item(variant, section, item)
call s:add_item(a:variant, a:section, a:item, 'verb')
endf
fun! s:add_higroup_item(variant, section, hg)
if s:is_neovim_group(s:hi_name(a:hg))
call s:add_neovim_higroup_item(a:variant, a:section, a:hg)
return
endif
call s:add_item(a:variant, a:section, a:hg, 'group')
if a:variant ==# s:GUI
if s:has_gui_italics(a:hg)
call s:set_uses_italics()
call add(s:italics[a:variant][a:section], s:make_item(a:hg, 'it'))
endif
elseif s:has_term_italics(a:hg)
call s:set_uses_italics()
call add(s:italics[a:variant][a:section], s:make_item(a:hg, 'it'))
endif
endf
fun! s:add_neovim_higroup_item(variant, section, item)
call add(s:nvim[a:variant][a:section], s:make_item(a:item, 'group'))
endf
fun! s:add_linked_item(variant, section, source, target)
if s:is_neovim_group(a:source)
call add(s:nvim[a:variant][a:section], s:make_item([a:source, a:target], 'link'))
else
call s:add_item(a:variant, a:section, [a:source, a:target], 'link')
endif
endf
fun! s:add_italic_item(variant, section, item)
call s:add_item(a:variant, a:section, a:item, 'it')
endf
fun! s:global_preamble()
return s:data['global']['preamble']
\ + (s:supports_neovim() && !empty(s:nvim['global']['preamble'])
\ ? [['raw', "if has('nvim')"]] + s:nvim['global']['preamble'] + [['raw', 'endif']]
\ : []
\ )
endf
" Add italics definitions accumulated so far to the colorscheme at the current point
fun! s:flush_italics(variant, section)
if s:is_global_preamble() || empty(s:italics[a:variant][a:section])
return
endif
call s:add_raw_item(a:variant, a:section, 'if !s:italics')
call extend(s:data[a:variant][a:section], s:italics[a:variant][a:section])
call s:add_raw_item(a:variant, a:section, 'endif')
let s:italics[a:variant][a:section] = []
endf
fun! s:flush_terminal_colors(variant, section)
if a:section ==# 'preamble' || !s:is_gui(a:variant) || empty(s:term_colors(a:section))
return
endif
let l:tc = s:term_colors(a:section)
let l:col0_3 = join(map(copy(l:tc[0:3]), { _,c -> "'".s:guicol(c, a:section)."'" }), ', ')
let l:col4_9 = join(map(copy(l:tc[4:9]), { _,c -> "'".s:guicol(c, a:section)."'" }), ', ')
let l:col10_15 = join(map(copy(l:tc[10:15]), { _,c -> "'".s:guicol(c, a:section)."'" }), ', ')
call s:add_raw_item(s:GUI, a:section, 'let g:terminal_ansi_colors = ['.l:col0_3.',')
call s:add_raw_item(s:GUI, a:section, '\ '.l:col4_9.',')
call s:add_raw_item(s:GUI, a:section, '\ '.l:col10_15.']')
endf
fun! s:flush_neovim(variant, section, flush_terminal)
if !s:supports_neovim()
return
endif
if empty(s:nvim[a:variant][a:section]) &&
\ (s:is_term(a:variant) || !a:flush_terminal || empty(s:term_colors(a:section)))
return
endif
call s:add_raw_item(a:variant, a:section, "if has('nvim')")
call extend(s:data[a:variant][a:section], s:nvim[a:variant][a:section])
if s:is_gui(a:variant) && a:flush_terminal
let l:n = 0
for l:color in s:term_colors(a:section)
call s:add_raw_item(a:variant, a:section,
\ "let g:terminal_color_".string(l:n)." = '".s:guicol(l:color, a:section)."'")
let l:n += 1
endfor
endif
call s:add_raw_item(a:variant, a:section, 'endif')
let s:nvim[a:variant][a:section] = []
endf
fun! s:is_gui(variant)
return a:variant ==# s:GUI
endf
fun! s:is_term(variant)
return a:variant !=# s:GUI
endf
fun! s:flush_definitions(variant, section)
call s:flush_italics(a:variant, a:section)
call s:flush_terminal_colors(a:variant, a:section)
call s:flush_neovim(a:variant, a:section, a:section !=# 'preamble')
endf
fun! s:colorscheme_definitions(variant, section)
return s:data[a:variant][a:section]
endf
fun! s:has_colorscheme_definitions(variant, section)
return !empty(s:data[a:variant][a:section])
endf
" }}}
" Aux files {{{
fun! s:init_aux_files()
let s:auxfiles = {} " Mappings from paths to list of lines
let s:help_path = ''
endf
fun! s:destroy_aux_files()
unlet! s:auxfiles s:help_path
endf
" path: path of the aux file as specified in the template
fun! s:register_aux_file(path)
if !has_key(s:auxfiles, a:path)
let s:auxfiles[a:path] = []
endif
return a:path
endf
fun! s:register_help_file()
if empty(s:help_path)
if empty(s:shortname())
throw "The colorscheme's short name must be defined before the first documentation block"
endif
let s:help_path = 'doc' . s:slash() . s:shortname() . '.txt'
endif
if !has_key(s:auxfiles, s:help_path)
let s:auxfiles[s:help_path] = []
endif
return s:help_path
endf
fun! s:help_path()
return s:help_path
endf
fun! s:add_line_to_aux_file(line, linenr, path, sourcefile)
call add(s:auxfiles[a:path], { 'line': a:line, 'linenr': a:linenr, 'file': a:sourcefile })
endf
fun! s:auxfile_paths()
return keys(s:auxfiles)
endf
fun! s:auxfile(path)
return s:auxfiles[a:path]
endf
" }}}
" Init/clear data structures {{{
fun! s:init_data_structures()
let g:colortemplate_exit_status = 0
call s:init_source_code()
call s:init_color_palette()
call s:init_highlight_groups()
call s:init_metadata()
call s:init_colorscheme_definition()
call s:init_aux_files()
endf
fun! s:destroy_data_structures()
call s:destroy_source_code()
call s:destroy_color_palette()
call s:destroy_highlight_groups()
call s:destroy_metadata()
call s:destroy_colorscheme_definition()
call s:destroy_aux_files()
endf
" }}}
" }}}
" Color stats {{{
" Print details about the color palette for the specified background
fun! s:print_similarity_table(bg, bufnr)
let l:colors = s:color_names(a:bg)
if empty(l:colors)
return
endif
let l:delta = {}
let l:colnames = []
for l:c in l:colors
" Skip color names and ASCII colors (0-15)
if s:guicol(l:c, a:bg) !~# '\m^#' || s:col256(l:c, a:bg) < 16
continue
endif
call add(l:colnames, l:c)
let l:delta[l:c] = colortemplate#colorspace#hex_delta_e(
\ s:guicol(l:c, a:bg),
\ colortemplate#colorspace#xterm256_hexvalue(s:col256(l:c, a:bg))
\ )
endfor
" Find maximum length of color names (used for formatting)
let l:len = max(map(copy(l:colnames), { _,v -> len(v)}))
" Sort colors by increasing delta
call sort(l:colnames, { c1,c2 -> l:delta[c1] < l:delta[c2] ? -1 : 1 })
call s:put(a:bufnr, '{{{ Color Similarity Table (' . a:bg . ' background)')
for l:c in l:colnames
let l:colgui = s:guicol(l:c, a:bg)
let l:rgbgui = colortemplate#colorspace#hex2rgb(l:colgui)
let l:col256 = s:col256(l:c, a:bg)
let l:d = l:delta[l:c]
if l:col256 > 15 && l:col256 < 256
let l:hex256 = g:colortemplate#colorspace#xterm256[l:col256 - 16]
let l:rgb256 = colortemplate#colorspace#hex2rgb(l:hex256)
let l:def256 = l:hex256 . printf('/rgb(%3d,%3d,%3d)', l:rgb256[0], l:rgb256[1], l:rgb256[2])
else
let l:def256 = repeat(' ', 24)
endif
let l:fmt = '%'.l:len.'s: GUI=%s/rgb(%3d,%3d,%3d) Term=%3d %s [delta=%f]'
call s:put(a:bufnr, printf(l:fmt, l:c, l:colgui, l:rgbgui[0], l:rgbgui[1], l:rgbgui[2], l:col256, l:def256, l:d))
endfor
call s:put(a:bufnr, '}}} Color Similarity Table')
endf
fun! s:print_matrix(bufnr, matrix, labels, gui, bg)
call s:put(a:bufnr, '{{{ '.(a:gui ? 'GUI (' : 'Terminal (').a:bg.')')
call s:put(a:bufnr, "\t".join(a:labels, "\t"))
for l:i in range(len(a:matrix))
call s:put(a:bufnr, a:labels[l:i]."\t".join(map(a:matrix[l:i], { j,v -> j ==# l:i ? '' : printf("%5.02f", v) }), "\t")."\t".a:labels[l:i])
endfor
call s:put(a:bufnr, "\t".join(a:labels, "\t"))
call s:put(a:bufnr, '}}}')
endf
fun! s:print_contrast_ratio_matrices(bufnr, colors, colnames, bg)
let l:M = {}
let l:M['gui'] = colortemplate#colorspace#contrast_matrix(a:colors['gui'])
let l:M['term'] = colortemplate#colorspace#contrast_matrix(a:colors['term'])
call s:put(a:bufnr, '{{{ Contrast Ratio Matrix (' . a:bg . ' background)')
call s:put(a:bufnr, 'Pairs of colors with contrast ≥4.5 can be safely used as a fg/bg combo')
call s:put(a:bufnr, "█ Not W3C conforming █ Not ISO-9241-3 conforming")
call s:print_matrix(a:bufnr, M['gui'], a:colnames, 1, a:bg)
call s:print_matrix(a:bufnr, M['term'], a:colnames, 0, a:bg)
call s:put(a:bufnr, '}}} Contrast Ratio Matrix')
endf
fun! s:print_colordiff_matrix(bufnr, colors, colnames, bg)
let l:M = {}
let l:M['gui'] = colortemplate#colorspace#coldiff_matrix(a:colors['gui'])
let l:M['term'] = colortemplate#colorspace#coldiff_matrix(a:colors['term'])
call s:put(a:bufnr, '{{{ Color Difference Matrix (' . a:bg . ' background)')
call s:put(a:bufnr, 'Pairs of colors whose color difference is ≥500 can be safely used as a fg/bg combo')
call s:print_matrix(a:bufnr, M['gui'], a:colnames, 1, a:bg)
call s:print_matrix(a:bufnr, M['term'], a:colnames, 0, a:bg)
call s:put(a:bufnr, '}}} Color Difference Matrix')
endf
fun! s:print_brightness_diff_matrix(bufnr, colors, colnames, bg)
let l:M = {}
let l:M['gui'] = colortemplate#colorspace#brightness_diff_matrix(a:colors['gui'])
let l:M['term'] = colortemplate#colorspace#brightness_diff_matrix(a:colors['term'])
call s:put(a:bufnr, '{{{ Brightness Difference Matrix (' . a:bg . ' background)')
call s:put(a:bufnr, 'Pairs of colors whose brightness difference is ≥125 can be safely used as a fg/bg combo')
call s:print_matrix(a:bufnr, M['gui'], a:colnames, 1, a:bg)