1 -- State transitions while colorizing a single line.
2 -- Just for comments and strings.
3 -- Limitation: each fragment gets a uniform color so we can only change color
7 {prefix
='--', target
='comment'},
8 {prefix
='"', target
='dstring'},
9 {prefix
="'", target
='sstring'},
12 {suffix
='"', target
='normal'},
15 {suffix
="'", target
='normal'},
17 -- comments are a sink
20 Comments_color
= {r
=0, g
=0, b
=1}
21 String_color
= {r
=0, g
=0.5, b
=0.5}
22 Divider_color
= {r
=0.7, g
=0.7, b
=0.7}
26 comment
=Comments_color
,
31 Current_state
= 'normal'
33 function initialize_color()
35 Current_state
= 'normal'
38 function select_color(frag
)
39 --? print('before', '^'..frag..'$', Current_state)
40 switch_color_based_on_prefix(frag
)
41 --? print('using color', Current_state, Colors[Current_state])
42 App
.color(Colors
[Current_state
])
43 switch_color_based_on_suffix(frag
)
44 --? print('state after suffix', Current_state)
47 function switch_color_based_on_prefix(frag
)
48 if Next_state
[Current_state
] == nil then
52 for _
,edge
in pairs(Next_state
[Current_state
]) do
53 if edge
.prefix
and find(frag
, edge
.prefix
, nil, --[[plain]] true) == 1 then
54 Current_state
= edge
.target
60 function switch_color_based_on_suffix(frag
)
61 if Next_state
[Current_state
] == nil then
65 for _
,edge
in pairs(Next_state
[Current_state
]) do
66 if edge
.suffix
and rfind(frag
, edge
.suffix
, nil, --[[plain]] true) == #frag
then
67 Current_state
= edge
.target
74 return s
:gsub('^%s+', ''):gsub('%s+$', '')
78 return s
:gsub('^%s+', '')
82 return s
:gsub('%s+$', '')