6 from Delegator
import Delegator
8 #$ event <<toggle-auto-coloring>>
10 #$ unix <Control-slash>
16 return "(?P<%s>" % name
+ string
.join(list, "|") + ")"
19 kw
= r
"\b" + any("KEYWORD", keyword
.kwlist
) + r
"\b"
20 comment
= any("COMMENT", [r
"#[^\n]*"])
21 sqstring
= r
"(\b[rR])?'([^'\\\n]|\\.)*'?"
22 dqstring
= r
'(\b[rR])?"([^"\\\n]|\\.)*"?'
23 sq3string
= r
"(\b[rR])?'''([^'\\]|\\.|'(?!''))*(''')?"
24 dq3string
= r
'(\b[rR])?"""([^"\\]|\\.|"(?!""))*(""")?'
25 string
= any("STRING", [sq3string
, dq3string
, sqstring
, dqstring
])
26 return kw
+ "|" + comment
+ "|" + string
+ "|" + any("SYNC", [r
"\n"])
28 prog
= re
.compile(make_pat(), re
.S
)
29 idprog
= re
.compile(r
"\s+(\w+)", re
.S
)
31 class ColorDelegator(Delegator
):
34 Delegator
.__init
__(self
)
38 def setdelegate(self
, delegate
):
39 if self
.delegate
is not None:
40 self
.unbind("<<toggle-auto-coloring>>")
41 Delegator
.setdelegate(self
, delegate
)
42 if delegate
is not None:
44 self
.bind("<<toggle-auto-coloring>>", self
.toggle_colorize_event
)
45 self
.notify_range("1.0", "end")
47 def config_colors(self
):
48 for tag
, cnf
in self
.tagdefs
.items():
50 apply(self
.tag_configure
, (tag
,), cnf
)
54 "COMMENT": {"foreground": "#dd0000"},
55 "KEYWORD": {"foreground": "#ff7700"},
56 "STRING": {"foreground": "#00aa00"},
57 "DEFINITION": {"foreground": "#0000ff"},
59 "SYNC": {}, #{"background": "#ffff00"},
60 "TODO": {}, #{"background": "#cccccc"},
62 "BREAK": {"background": "#FF7777"},
64 # The following is used by ReplaceDialog:
65 "hit": {"foreground": "#FFFFFF", "background": "#000000"},
68 def insert(self
, index
, chars
, tags
=None):
69 index
= self
.index(index
)
70 self
.delegate
.insert(index
, chars
, tags
)
71 self
.notify_range(index
, index
+ "+%dc" % len(chars
))
73 def delete(self
, index1
, index2
=None):
74 index1
= self
.index(index1
)
75 self
.delegate
.delete(index1
, index2
)
76 self
.notify_range(index1
)
82 def notify_range(self
, index1
, index2
=None):
83 self
.tag_add("TODO", index1
, index2
)
85 if __debug__
: print "colorizing already scheduled"
88 self
.stop_colorizing
= 1
89 if __debug__
: print "stop colorizing"
90 if self
.allow_colorizing
:
91 if __debug__
: print "schedule colorizing"
92 self
.after_id
= self
.after(1, self
.recolorize
)
96 after_id
= self
.after_id
98 if __debug__
: print "cancel scheduled recolorizer"
99 self
.after_cancel(after_id
)
100 self
.allow_colorizing
= 0
101 self
.stop_colorizing
= 1
103 def toggle_colorize_event(self
, event
):
105 after_id
= self
.after_id
107 if __debug__
: print "cancel scheduled recolorizer"
108 self
.after_cancel(after_id
)
109 if self
.allow_colorizing
and self
.colorizing
:
110 if __debug__
: print "stop colorizing"
111 self
.stop_colorizing
= 1
112 self
.allow_colorizing
= not self
.allow_colorizing
113 if self
.allow_colorizing
and not self
.colorizing
:
114 self
.after_id
= self
.after(1, self
.recolorize
)
116 print "auto colorizing turned", self
.allow_colorizing
and "on" or "off"
119 def recolorize(self
):
121 if not self
.delegate
:
122 if __debug__
: print "no delegate"
124 if not self
.allow_colorizing
:
125 if __debug__
: print "auto colorizing is off"
128 if __debug__
: print "already colorizing"
131 self
.stop_colorizing
= 0
133 if __debug__
: print "colorizing..."
135 self
.recolorize_main()
137 if __debug__
: print "%.3f seconds" % (t1
-t0
)
140 if self
.allow_colorizing
and self
.tag_nextrange("TODO", "1.0"):
141 if __debug__
: print "reschedule colorizing"
142 self
.after_id
= self
.after(1, self
.recolorize
)
144 def recolorize_main(self
):
148 item
= self
.tag_nextrange("TODO", next
)
152 self
.tag_remove("SYNC", head
, tail
)
153 item
= self
.tag_prevrange("SYNC", head
)
162 while not (was_ok
and is_ok
):
163 next
= self
.index(mark
+ " lineend +1c")
164 was_ok
= "SYNC" in self
.tag_names(next
+ "-1c")
165 line
= self
.get(mark
, next
)
166 ##print head, "get", mark, next, "->", `line`
169 for tag
in self
.tagdefs
.keys():
170 self
.tag_remove(tag
, mark
, next
)
172 m
= self
.prog
.search(chars
)
175 for key
, value
in m
.groupdict().items():
181 if value
in ("def", "class"):
182 m1
= self
.idprog
.match(chars
, b
)
185 self
.tag_add("DEFINITION",
188 m
= self
.prog
.search(chars
, j
)
189 is_ok
= "SYNC" in self
.tag_names(next
+ "-1c")
195 if self
.stop_colorizing
:
196 if __debug__
: print "colorizing stopped"
201 from Percolator
import Percolator
203 root
.wm_protocol("WM_DELETE_WINDOW", root
.quit
)
204 text
= Text(background
="white")
205 text
.pack(expand
=1, fill
="both")
212 if __name__
== "__main__":