delete some logs
[view.love.git] / colorize.lua
blobc0d211717267135b82c42b44c5f0348c94d088b7
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
4 -- at word boundaries.
5 Next_state = {
6 normal={
7 {prefix='--', target='comment'},
8 {prefix='"', target='dstring'},
9 {prefix="'", target='sstring'},
11 dstring={
12 {suffix='"', target='normal'},
14 sstring={
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}
24 Colors = {
25 normal=Text_color,
26 comment=Comments_color,
27 sstring=String_color,
28 dstring=String_color
31 Current_state = 'normal'
33 function initialize_color()
34 --? print('new line')
35 Current_state = 'normal'
36 end
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)
45 end
47 function switch_color_based_on_prefix(frag)
48 if Next_state[Current_state] == nil then
49 return
50 end
51 frag = rtrim(frag)
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
55 break
56 end
57 end
58 end
60 function switch_color_based_on_suffix(frag)
61 if Next_state[Current_state] == nil then
62 return
63 end
64 frag = rtrim(frag)
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
68 break
69 end
70 end
71 end
73 function trim(s)
74 return s:gsub('^%s+', ''):gsub('%s+$', '')
75 end
77 function ltrim(s)
78 return s:gsub('^%s+', '')
79 end
81 function rtrim(s)
82 return s:gsub('%s+$', '')
83 end